How to read v3.3 dbg metadata using v3.4 LLVM

Hi,

I have bitcode files built by LLVM v3.3 and need to process them using v3.4 tools. While I don’t expect a lot of backward compatibility in LLVM, luckily it seems everything’s working fine, except for reading source line information attached to instructions. I use this piece of code [0] to get source line information. For v3.4, instruction.getMetadata returns NULL.

I used llvm-dis to see what’s going on. For a sample program [1], llvm-dis v3.3, of course, didn’t have problems and dumped [2], showing dbg info. However, llvm-dis v3.4 seemed to have problems grabbing the dbg info [3].

Any idea how I can modify the code [0] to solve the problem? Attached is the bitcode file used as an example in this mail. It was complied with clang v3.3 -g -O0.

Thanks,
James

[0]

llvm::LLVMContext llvmContext;

unsigned dbgKind = llvmContext.getMDKindID(“dbg”);

if (MDNode *N = instruction.getMetadata(dbgKind)){

DILocation location(N);

fileDirectoryName = location.getDirectory();

fileName = location.getFilename();

lineNumber = location.getLineNumber();

columnNumber = location.getColumnNumber();

}

[1]

int main(int argc, char **argv){

return 0;

}

[2]

define i32 @main(i32 %argc, i8** %argv) #0 {

entry:

%retval = alloca i32, align 4

%argc.addr = alloca i32, align 4

%argv.addr = alloca i8**, align 8

store i32 0, i32* %retval

store i32 %argc, i32* %argc.addr, align 4

call void @llvm.dbg.declare(metadata !{i32* %argc.addr}, metadata !12), !dbg !13

store i8** %argv, i8*** %argv.addr, align 8

call void @llvm.dbg.declare(metadata !{i8*** %argv.addr}, metadata !14), !dbg !13

ret i32 0, !dbg !15

}

[3]

define i32 @main(i32 %argc, i8** %argv) #0 {

entry:

%retval = alloca i32, align 4

%argc.addr = alloca i32, align 4

%argv.addr = alloca i8**, align 8

store i32 0, i32* %retval

store i32 %argc, i32* %argc.addr, align 4

store i8** %argv, i8*** %argv.addr, align 8

ret i32 0

}

a.o (1.52 KB)

This is likely going to be difficult if possible. I wouldn’t bet on it working that way. We’ve not been trying to keep compatibility between versions for debug information metadata because the format has been in such flux and upgrading/multiple versions is a significant overhead.

You might be able to walk the instructions and grab the scope + line/column that way for each instruction. You can see the format of each metadata node by looking at DebugInfo.h. Hope this helps, feel free to send mail if you have any more questions.

-eric

Thanks Eric, but could you give me a little bit more hints or pointers please? I looked into DebugInfo.h, but I’m still not sure how to start. It sounds like I’d have to somehow manually extract metadata nodes from an instruction.

Thanks,
JS

Thanks Eric, but could you give me a little bit more hints or pointers
please? I looked into DebugInfo.h, but I'm still not sure how to start. It
sounds like I'd have to somehow manually extract metadata nodes from an
instruction.

You will, yes.

Something like:

LLVMContext Ctx = Module.getContext();
for each Function f in Module::functions():
  for each BasicBlock BB in f:
    for each Instruction I in BB:
       DebugLoc DL = I.getDebugLoc();
       DILocation DIL = cast<DILocation>(DL.getAsMDNode(Ctx));
       unsigned Line = DIL.getLine();
       unsigned Col = DIL.getCol();
       StringRef Filename = DIL.getFilename();

Or something like that.

-eric

Thanks for the pseudo code! I’ll give it a try and see how it goes.