0

I followed the How to get variable definition line number etc. using dbg metadata? in order to get the line number definition for local variables (allocas), which works fine. But I need the same for globals. So I tried to hack the findDbgGlobalDeclare() method from http://llvm.org/docs/doxygen/html/DbgInfoPrinter_8cpp_source.html#l00062 . However, I have no llvm.dbg.gv in my bytecode, so there is no dbg info to extract. I compile my target code using clang++ -O0 -g -emit-llvm Test.cpp -c -o Test.bc . Some samples from my bytecode:

@r = global i32 3, align 4
%4 = load i32* @r, align 4, !dbg !942
...
%a = alloca i32, align 4
%1 = load i32* %a, align 4, !dbg !939

However, I do have:

!924 = metadata !{i32 786484, i32 0, null, metadata !"r", metadata !"r", metadata !"", metadata !841, i32 19, metadata !56, i32 0, i32 1, i32* @r} ; [ DW_TAG_variable ] [r] [line 19] [def]

with on which !0 is indirectly dependent and there is !llvm.dbg.cu = !{!0} .

Thank you !

Community
  • 1
  • 1
Alex
  • 340
  • 4
  • 17

2 Answers2

1

Yes, !llvm.dbg.cu is the right place now. Quoting from the source-level debugging document:

Compile unit descriptors provide the root context for objects declared in a specific compilation unit. File descriptors are defined using this context. These descriptors are collected by a named metadata !llvm.dbg.cu. They keep track of subprograms, global variables and type information.

Specifically, see "Global variable descriptors".

The code you found is to support the older metadata nodes which are still generated by dragonegg so the readers support them for backwards compatibility. New LLVM code generates !llvm.dbg.cu.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
0

The steps are as follows:

1. NamedMDNode *NMD = M->getNamedMetadata("llvm.dbg.cu");

Then get into the metadata nodes chain till the desired global declaration.

2. DIDescriptor DIG(cast<MDNode>(NMD->getOperand(i)));
3. DIDescriptor DIGG(cast<MDNode>(NMD->getOperand(NMD->getNumOperands()-1)));
4. DIDescriptor DIGF(cast<MDNode>(DIGG->getOperand(0)));
5. Value* VV = cast<Value>(DIGF->getOperand(i));
6. DIDescriptor DIGS(cast<MDNode>(VV));

At this point, do:

7. DIGS->getOperand(j) 

and check http://llvm.org/docs/SourceLevelDebugging.html#c-c-front-end-specific-debug-information for all the fields you desire.

Alex
  • 340
  • 4
  • 17