Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I view the disassembly of optimised jitted .NET code?

For one reason or another, I sometimes find it useful or just interesting to look at the optimised compiler output for a function.

For unmanaged C/C++ code, my favourite way to do this has been to compile in Release mode, stick a breakpoint in the function of interest, run, and view the disassembly in Visual Studio when it hits the breakpoint.

I recently tried this with a C# project and discovered that that technique doesn't work. Even in Release mode, the disassembly I see is obviously not optimised. I found and disabled (in Visual Studio 2010) the "Debug ... Options and Settings ... Debugging ... General ... Suppress JIT optimization on module load" option, which presumeably gets me closer to what I want, only now it warns me when I try to run it, and I then can't get it to stop on a breakpoint so that I can see the disassembly.

So, if I want to see the disassembled, optimised output of the CLR (4.0) jitter for a function, what's the best way to go about that? To be clear, I'd like to see the x86 (or preferably x86_64) disassembly, not just the IL disassembly (which you can see in Reflector).

like image 967
Wesley Hill Avatar asked Aug 06 '10 12:08

Wesley Hill


People also ask

How can I see disassembly in Vscode?

To open the Disassembly window during debugging, select Windows > Disassembly or press Alt+8.

What is disassembly code?

Machine code disassembly routines form a fundamental component of software systems that statically analyze or modify executable programs. The task of disassembly is complicated by indirect jumps and the presence of non- executable data—jump tables, alignment bytes, etc. —in the instruction stream.

How do I view assemblies in Visual Studio?

You can normally see assembly code while debugging C++ in visual studio (and eclipse too). For this in Visual Studio put a breakpoint on code in question and when debugger hits it rigth click and find "Go To Assembly" ( or press CTRL+ALT+D )

What is disassembly window?

The Disassembly Window shows the program execution in assembly code, or, intermixed with the source code (device dependent). When the Disassembly Window is the active window, then all debug-stepping commands work on assembly level. Open the window with the menu View — Disassembly Window.


2 Answers

Of course, after half a day of searching for the answer on this one, I find the answer myself 5 minutes after I ask on SO.

I was close; the only missing step from what I had in the question was that "Enable Just My Code" must also be unchecked in the options.

The full guide is available here: http://blogs.msdn.com/b/vancem/archive/2006/02/20/535807.aspx

like image 71
Wesley Hill Avatar answered Sep 23 '22 14:09

Wesley Hill


I believe the JIT knows when you're running under a debugger, and generates more "debugger-friendly" x86 code, which would explain why the x86 code you saw was unoptimized.

You might try running the app standalone, executing the code you're interested in at least once (so it JITs without the debugger attached), then attach the debugger to the process and set your breakpoint.

like image 45
Joe White Avatar answered Sep 20 '22 14:09

Joe White