Why isn't LocalBuilder.SetLocalSymInfo emitting variable names?
Asked Answered
H

3

6

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.

Herriot answered 12/2, 2012 at 22:56 Comment(0)
C
5

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.

Cowpuncher answered 5/2, 2013 at 16:3 Comment(2)
Thank you, I will award you my bounty when I can, (in 17 hours, +200 pts) but cant mark this as the correct answer (which it is) as I didn't ask the original question!Subscapular
There we go, bounty awarded, sorry for the delay :)Subscapular
C
2

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.

Curler answered 5/2, 2013 at 10:34 Comment(1)
I have inspected the .pdb file, there is nothing there!Subscapular
N
1

I suspect this is because you are building the module as a release DLL.

Try passing true as the second parameter to AssemblyBuilder.DefineDynamicModule

Nacred answered 13/2, 2012 at 11:46 Comment(1)
If you look in the sample I linked, the author is calling AssemblyBuilder.DefinDynamicModule(string, string, true). I can also see that there is a SampleAssembly.pdb file in the current directory.Herriot

© 2022 - 2024 — McMap. All rights reserved.