llvm::DIInstruction getFilename returns filename with a directory, I just want the filename

混江龙づ霸主 提交于 2019-12-13 14:34:21

问题


I'm trying to get debugging metadata from an llvm Instruction using the DILocation class.

However, when I query the DILocation for the filename where the instruction came from, I get a filename with a directory tagged onto the front.

I though it would return just the file and the entire directory path should be retrieved via a call to getDirectory().

For example, instead of XMain_0.c I end up with pbg/XMain_0.c

I compiled my bitcode like this:

XMain_0.o: pbg/XMain_0.c
    $(CC) <snip> -c pbg/XMain_0.c

Does the fact that I passed in my source with a directory on it mean that the metadata saves the source filename as the input?

Here's a cut down example:

  const llvm::Instruction* inst  //passed in
  MDNode *n = inst->getMetadata("dbg");
  DILocation loc(n);  

  file = loc.getFilename().str(); // =>  pbg/XMain_0.c
  dir = loc.getDirectory().str(); // => /projects/pbg/pbg-m/DIR

Are there calls I can make to "normalize" this data or do I need to do it by hand?

Clang 3.1 if that matters.


回答1:


I think it depends on the invocation of the compiler. If you run:

clang -c somedir/somefile.c

Then the full somedir/somefile.c will be the filename.

How does your invocation look like?


There is nothing weird about it. The debugger will look for source files relative to some project root, and if you compile files likes this, it's the way they are going to be found. gcc does the same thing:

/tmp$ pwd
/tmp
/tmp$ cat subdir/test.c 
int foo() {
  return 42;
}

/tmp$ gcc -g -O0 -c subdir/test.c -o test.o
/tmp$ readelf --debug-dump=info test.o | grep -A4 compile_unit
 <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <c>   DW_AT_producer    : (indirect string, offset: 0x0): GNU C 4.6.3   
    <10>   DW_AT_language    : 1    (ANSI C)
    <11>   DW_AT_name        : (indirect string, offset: 0xc): subdir/test.c    
    <15>   DW_AT_comp_dir    : (indirect string, offset: 0x1a): /tmp    


来源:https://stackoverflow.com/questions/14166004/llvmdiinstruction-getfilename-returns-filename-with-a-directory-i-just-want-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!