Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to debug code compiled at runtime?

I have a need to compile some code using CodeDomProvider.CompileAssemblyFromSource. How would one go about debugging it? Basically, I want to compile it, create instance of a type and then step into the code for the type.

like image 305
epitka Avatar asked Apr 08 '11 13:04

epitka


People also ask

What is runtime debug?

Runtime debugging involves inspecting the state of a running application, potentially pausing execution at certain points so that the contents of variables in memory can be inspected.

Is debugging the same as compiling?

The main difference between compiler and debugger is that a compiler converts the source code to equivalent machine code to execute the tasks defined in the program, while a debugger helps to recognize the errors of a program and to fix them.


2 Answers

After I posted a question, I realized that my problem was that I was generating the assembly from string, not from file. I went back and changed the code to run with different options when in DEBUG and I am able to step right in from unit test code. Also one has to set GenerateInMemory to false and IncludeDebugInformation to true.

#if DEBUG
            @params.IncludeDebugInformation = compilationContext.IncludeDebugInformation;
            @params.GenerateInMemory = compilationContext.GenerateInMemory;
            var fileName = Path.GetFullPath(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,@"..\..\" + compilationContext.AssemblyOutputName + ".cs"));
            File.WriteAllText(fileName,compilationContext.StringToCompile);
            return _codeDomProvider.CompileAssemblyFromFile(@params,fileName);
#else
            return _codeDomProvider.CompileAssemblyFromSource(@params, compilationContext.StringToCompile);
#endif
like image 105
epitka Avatar answered Oct 19 '22 06:10

epitka


Interesting question. I think your best bet would be to use WinDbg to attach to the running .NET exe process (I think you will have to do this after the tyoe has been compiled in memory, since the memory addresses for the EXE will change - I assume).

Then, when the type is compiled and running in memory, you can then search for that type using commands found in SOS.dll. You can also place breakpoints in memory using SOS.dll

Getting started with SOS link

http://rionisimpsoni.wordpress.com/2009/10/08/getting-started-with-windbg-and-sos-dll/

This is a bit of light answer, since explaining how to use WinDbg and SOS.dll has been covered many times on the web.

Edit:

One of the cons of this method is that you won't be able to see the source code like Visual Studio displays it. You will see assembly language displayed as you step through the code. This might put you off already :), but if you stick with it, and understand a bit of assembly, you could do enough to debug errors for example.

Another thing you could do is dump the .NET assembly from memory into a file on disk. The SOS.dll command to do that escapes me now, I'll go look for it...

Ahh, it's SaveModule. An example of this can be found in the comments here.

like image 41
Jason Evans Avatar answered Oct 19 '22 05:10

Jason Evans