How to see the real native code after the JIT-compiler pass?

I know how to do it in .Net:

  1. Insert Trace.Fail(“breakpoint”) somewhere near the place you want to examine.
  2. Make a “release” build.
  3. Launch the executable outside of Visual Studio.
  4. When the execution reaches the Trace.Fail call, it stops, and Windows shows “Assertion Failed” message box with Abort, Retry and Ignore buttons.
  5. Press “Retry”.
  6. Windows offers to attach a debugger. Answer “Yes”.
  7. Visual Studio launches with debug-mode active.
  8. If it says “no source code available”, click “show disassembly”.
  9. In Call Stack window click on the method you want to examine.

That’s it. You’ll see the actual native code that executes on CPU.

How to do something like this in Unity/Mono? They just ignore the Trace.Fail call.

I could do the same steps without Trace.Fail and stop the execution in the debugger manually, but this way I can’t find the place in the program I want to examine, because the final code is a heavily optimized mess. I need to mark somehow the right spot in the program but don’t know how.

Do you have a good way to get to the IL period? I was hoping View > Show disassembly in MonoDevelop would do the trick, but all it shows is the source without syntax colouring. I can open my script assembly with ILSpy, but is there a convenient way to view IL from within MonoDevelop? Thanks!

I use .NET Reflector for this. JetBrains also promises their dotPeek decompiler will support IL in the future version(s).

OK, similar to what I’m doing then. (I stopped using .NET Reflector when it became a paid product. ILSpy is similar, but free.)

For those who used Reflector before it became paid, there’s a free version 6.8 that works like the previous ones, but never expires. I personally see no reason to update.

I have found a way to replace Trace.Fail(). I simply put an infinite loop instead. And when the program hangs I attach the debugger and find myself inside that loop.

while (true);