Is it possible to debug code compiled at runtime?
Asked Answered
S

3

12

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.

Schlimazel answered 8/4, 2011 at 13:35 Comment(2)
What do you mean by "with in memory option"? Have you tried simply attaching the debugger to the running process?Ratchford
While this may not what you want, what about just generating the assembly to disk and loading it as if it where an existing assembly, just for debugging purposes? I understand the need to generate the assembly at runtime, but there's nothing wrong with picking certain runtime conditions and reproducing them at compile time :)Cordillera
S
10

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
Schlimazel answered 8/4, 2011 at 14:6 Comment(3)
Interesting observation, assembly generated like this does not have to end with dll to be usable, it does not have to have any extension at all.Schlimazel
+1 Nice question and self-answer and very apropos for me. I'm right in the middle of generating and compiling test code on the fly. This is useful.Weldon
@Wilkins: Thanks, glad that it helped somebody else. My stuff is even little bit more involved, I generate code that generates code that is then compiled and executed. So being able to debug was a must for me here.Schlimazel
D
3

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.

Dearman answered 8/4, 2011 at 13:46 Comment(0)
F
0

Did you try attaching to the process using your code from the Debug > Attach to process feature in VS?

Florance answered 8/4, 2011 at 13:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.