2

I have a .NET program which I have disassembled with ildasm. I then recompiled it with ilasm using the /debug flag in order to create a .pdb which links to the .il file. Everything works fine and I can now debug that program in Visual Studio 2013 Professional and put breakpoints on individual instructions and then step through them one by one. It's great to be able to step through the instructions but I don't know how to view the stack that they're operating on, so all I can do is just step through the instructions without having any idea what data they're working with. How can I view the objects the stack?

1 Answer 1

5

That's not possible. CIL are instructions for a virtual machine, your machine is not virtual. CIL instructions are always translated to machine code when your program runs, the job of the Just In Time compiler (aka jitter). Machine code are the actual instructions executed by the processor. CIL is a nice abstraction between the language compiler and the actual machine that executes the program, the reason that AnyCPU can be the Platform target of your project. Different processors have different jitters.

You can only ever look at the real instructions that are executed, Debug + Windows + Disassembly. Some understanding of assembly code required. And you can only ever look at the real stack, Debug + Windows + Call Stack for the methods, Debug + Windows + Locals for method arguments and local variables. Which are easy to understand.

Another detail you probably ought to know about is that the machine code you are looking at in the Disassembly window is not the code that will execute on the user's machine. The jitter has an optimizer built-in that does extra work to try to make the machine code as efficient as possible. That optimizer is turned off in the Debug build and/or when you have a debugger attached. Making it a lot easier to debug the program. If you want to see the real code then you have to debug the Release build and change an option. Tools + Options, Debugging, General, untick the "Suppress JIT optimization on module load" option. Optimized code can be a drastic mismatch with the CIL and make those debug windows act funny when methods are inlined, statements are rearranged and variables are eliminated.

2
  • As far as I know, it is possible: stackoverflow.com/questions/13026449/… (I've heard its also possible with windbg and extensions) The problem is just getting it to work inside VS2013. I am completely aware that CIL runs on a virtual machine and that the stack doesn't actually exist, but at the end of the day it must virtually exist somewhere or obviously CIL wouldn't work.
    – Sea Erchin
    Commented Aug 3, 2014 at 9:51
  • Asked and answered, you cannot get this kind of view inside VS. If you want something similar to what you saw in that question then I suppose you'll have to use MDbg. Not sure, it is not a common debugging tool, it was meant as a sample for programmers to implement their own managed debugger. It could of course never debug optimized code in that view since there is no one-to-one correlation anymore between the CIL and the machine code. Commented Aug 3, 2014 at 10:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.