Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why isn't LocalBuilder.SetLocalSymInfo emitting variable names?

I tried running the sample code which appears on the documentation page for the System.Reflection.Emit.LocalBuilder class but it appears that the calls to LocalBuilder.SetLocalSymInfo(string, int, int) aren't doing anything since the IL Dissasembler shows this as the IL for SampleAssembly.dll:

.method public static string  Function1(int32 A_0) cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init (string V_0,
           int32 V_1)
  IL_0000:  ldarg.0
  IL_0001:  stloc.1
  IL_0002:  ldstr      "string value"
  IL_0007:  stloc.0
  IL_0008:  ldloc.0
  IL_0009:  ret
} // end of method Example::Function1

Why aren't the variable names (myString and myInt) listed in the Dissasembler?

Enviroment Info:

  • Windows 7 64 bit
  • Visual Studio 2010 Professional SP1
  • .Net 4.0.30319 SP1
  • Target Framework: .Net 4 Client Profile
  • Debug configuration (for the program using System.Reflection.Emit)

Edit: As I noted in a comment, there is a SampleAssembly.pdb file being generated along with the SampleAssembly.dll file.

like image 575
Wesley Wiser Avatar asked Feb 12 '12 22:02

Wesley Wiser


2 Answers

The debugging support in System.Reflection.Emit is pretty poor and quirky (and to a certain extent this is also true for IKVM.Reflection, because it inherits some of the brokenness from the underlying .pdb writer API that has to be used since the .pdb file format is not documented).

Anyway, the reason the sample doesn't work is that it is missing the following code:

ISymbolDocumentWriter doc = myModule.DefineDocument("sourcefile", Guid.Empty, Guid.Empty, Guid.Empty);

myMethodIL.MarkSequencePoint(doc, 1, 0, 1, 0);

There must be at least a single sequence point in the method, because that is the way the internal data structures are tied together.

like image 73
Jeroen Frijters Avatar answered Oct 01 '22 05:10

Jeroen Frijters


Symbol names are stored in the PDB file and not in the assembly.

A tool like Reflector.NET will load the PDB file if present to give your disassembled code better names.

You can also verify this by debugging the code in a debugger with and without the PDB file.

like image 20
leppie Avatar answered Oct 01 '22 05:10

leppie