mark
mark

Reputation: 62846

Is it possible to reflect a running .NET process?

I have an application with dynamic types emitted at run-time. In order to inspect the generated .NET code, we save the dynamically generated assemblies and use the Reflector.

I am wondering whether there is a way to reflect them the Reflector way while the respective process is running, without having to save the dynamic assemblies? In other words to have sort of "Attach to process ..." button in Reflector.

Thanks.

EDIT: The only raison d'etre for the feature is to reflect on dynamically generated assemblies.

Upvotes: 2

Views: 1438

Answers (2)

John Gietzen
John Gietzen

Reputation: 49554

I know that you are probably looking for C# code to come out of Dynamic Assemblies, and that this question is quite old, however...

You can get at the IL of a running dynamic assembly using the Visual Studio Immediate window or using WinDbg. (You must use WinDbg if you are debugging a 64-bit process, since Visual Studio is still a 32-bit product.)

  1. Attach to the process.
    • WinDbg: This needs to be done "noninvasively" if another process is already attached as the debugger. Hit F6 (or choose File -> Attach to Process) and select the "Noninvasive" option if VS is already attached.
    • Visual Studio: VS does this automatically when you choose "Start Debugging" or hit F5, but can be done manually by choosing 'Attach to Process' in the debug menu.
  2. Load SOS.dll using the command !load SOS.dll or simply !load SOS.
    • WinDbg: In noninvasive mode, WinDbg needs the full path of SOS.dll. For .NET 4 the path is something like C:\Windows\Microsoft.NET\Framework64\v4.0.30319\SOS.dll
  3. Break execution and selet your thread.
    You can do this automatically in the either debugger by setting a breakpoint, or in VS by having the exception helper pop up.
    • WinDbg: In non-invasive mode, you will need to select the thread manually using the ~ command and the ~n s command. Use ~ to list all threads and use ~n s command to switch. For example, use ~12 s to switch to thread number 12.
  4. Get the stack trace, along with instruction pointers. Command: !clrstack
  5. Grab the IP and find which method that points to. Command: !ip2md [address]
    • For example, if the IP for the method you want to dump is 0123456, you would issue the command !ip2md 0123456.
  6. Dump the IL for the method. Command: !dumpil [method descriptor]
    • For example, if the IP2MD command listed '0A1B2C3Das the address of the method descriptor, you would issue the command!dumpil 0A1B2C3D`.

That should allow you to see into the Dynamic Assembly, albeit in IL assembly.

Upvotes: 2

Nicole Calinoiu
Nicole Calinoiu

Reputation: 20992

There is a running assembly add-in for Reflector (http://weblogs.asp.net/kdente/articles/438539.aspx). However, I suspect that it simply helps retrieve paths for running assemblies, with the assemblies subsequently loaded from disk by Reflector. However, it's probably worth a try. Also, creating reflector add-ins isn't all that difficult, so you might be able to extend the running assembly add-in approach to automatically save the assembly to disk so that it can be loaded by Reflector (assuming the existing add-in doesn't already do this.)

Upvotes: -1

Related Questions