Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simultaneously debug through intermediate language (IL) and C# in Visual Studio

I'm looking for an extension for Visual Studio where in debug mode it's possible to single step through the intermediate language beside C#.

I'm not looking for a solution to debug managed and unmanaged code.

like image 929
Raphael Bossek Avatar asked Mar 13 '11 21:03

Raphael Bossek


2 Answers

Although not strictly a Visual Studio extension as the OP requested, there's now a maybe even better way to do this using dnSpy, a comprehensive, standalone, open-source .NET debugging tool. The tool actually does much more than just debugging; for example it allows direct editing of .NET and native (PEFile) assemblies, de-obfuscating them, browsing and modifying the raw managed and native headers, content, resources, BAML, and metadata, and more that I probably haven't discovered.

enter image description here

For the purposes of the discussion on this page, be sure to check out the IL interpreter section of the dnSpy project. Exactly as requested by the OP, this is the library that implements an IL interpreter for simulating the (pretend) execution of the IL code in parallel with the debugger's (actual) native instruction single-stepping, for the purposes of displaying the state of the (logical) IL execution stack. I believe there's some excellent x86/x64 disassembly rendering built-in to the debugger as well, if needed. Kudos to the developer of this tour-de-force app.

like image 75
Glenn Slayden Avatar answered Oct 27 '22 14:10

Glenn Slayden


What is your purpose? Is your IL generated by C# compiler or dynamically produced at run time? If the former one, you can use a trick of re-compiling your binary through ilasm.

  1. Compile C# code as you normally would. It does not matter if it is optimized or not, but you have to specify compilation option to produce full PDB symbols.
  2. Use ildasm to convert your binary to .il file. It is option Dump in the menu.
  3. re-compile the .il file to get a new binary (and a new symbols)

    ilasm .il [/exe|/dll] /debug

  4. Now when debugging that specific assembly you will see IL code rather than C# code. You will also see a matching lines from original C# file if you select appropriate option in step 2.

For the case of dynamically generated IL, I would simply use WinDbg with SOS extension. It can dump IL and step through it, but it takes a bit to get used to.

like image 6
seva titov Avatar answered Oct 27 '22 15:10

seva titov