Saturday, January 07, 2006

Debugging Freya applications

I have just added PDB file generation to the Freya compiler, to support debugging. Piece of cake! It has been extremely easy… so far.
  1. Mark the dynamic assembly with DebuggableAttribute, passing both the DisableOptimizations and Default constants to the attribute constructor.
  2. Create the dynamic module as "debuggable", passing true as the last parameter for AssemblyBuilder.DefineDynamicModule.
  3. Associate an ISymbolDocumentWriter to each source file in the project. Use the DefineDocument method from MethodBuilder for this task.
  4. Since every node in the Abstract Syntax Tree has a SourceRange field pointing to its source file, each AST node, including statement nodes, may access their corresponding ISymbolDocumentWriter instance.
  5. Code generation for statements is achieved by calling the Generate virtual method defined at the root AstStatement class. I just inserted a call to ILGenerator.MarkSequencePoint at the very beginning of the base Generate method. This Reflection.Emit method requires an ISymbolDocumentWriter as its first parameter, and a range inside the source file... and both things can be extracted from the SourceRange stored at the node.
  6. Additionally, local variables declared by the programmed must apply the SetLocalSymInfo method to their local variable builders, since IL code does not contain metadata about the names of local variables.
This is all what you need to load a Freya source file in Microsoft's CLR Debugger, as shown in this image:

A Freya debugging session with Microsoft's CLR Debugger

You can find more information on this topic at Mike Stall's .NET Debugging blog. Of course, this is just a first step towards integrating the compiler in the Visual Studio 2005 IDE.



Post a Comment

<< Home