如何从clang获得更细​​粒度的行/列调试信息?

bdd*_*ken 5 c compiler-construction debugging clang dwarf

我正在编写软件,对其他程序进行一些相当复杂的静态分析和动态跟踪.该程序使用大量静态DWARF信息来协助跟踪,包括.debug_lineDWARF部分的行/列信息.为了使该程序具有我们所需的精度,必须在DWARF调试信息中填充细粒度和准确的行号和列号信息.使用clang我可以强制使用-g -Xclang -dwarf-column-info选项一起填充行和列信息.

但是,在某些情况下,clang不会产生细粒度足够的列信息.一个特定的例子是for循环.请参考以下示例程序,我将其称为source01.c:

  1      
  2 int main ()
  3 {    
  4     int number1 = 10, number2 = 20;
  5     for (int i=0; i < 10; ++i) {                                                                                                                           
  6         number1++;
  7         number2++;
  8     }
  9     return 0;
 10 } 
Run Code Online (Sandbox Code Playgroud)

我可以像这样编译它:

clang -g -Xclang -dwarf-column-info source01.c
Run Code Online (Sandbox Code Playgroud)

哪个产生可执行文件a.out.然后我dwarfdump用来检查行/列信息的填充方式:

dwarfdump a.out > dwarf_info
Run Code Online (Sandbox Code Playgroud)

看一下该.debug_line部分,我看到调试信息中包含的所有行/列对此可执行文件:

.debug_line: line number info for a single cu
Source lines (from CU-DIE at .debug_info offset 0x0000000b):

<pc>        [row,col] NS BB ET PE EB IS= DI= uri: "filepath"
NS new statement, BB new basic block, ET end of text sequence
PE prologue end, EB epilogue begin
IA=val ISA number, DI=val discriminator value
0x004004f0  [   3, 0] NS uri: "/xxx/loop_01/source01.c"
0x004004fb  [   4, 5] NS PE
0x00400509  [   5,10] NS
0x0040051d  [   6, 9] NS
0x00400528  [   7, 9] NS
0x00400533  [   5,27] NS
0x00400548  [   9, 5] NS
0x0040054a  [   9, 5] NS ET
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,有对(5,10),对应于int i=0;,和对(5,27)对应++i.但是,我希望(并且需要)那里也是对(5,19),这对应于i < 10,但它不存在.我已经检查了可执行文件的指令objdump,并且已经确认确实存在与比较相对应的指令i < 10(因此,它不是简单地"优化掉").

你有没有直觉为什么clang不会填充这些信息?或者有没有办法强制clang生成更细粒度的列信息?似乎clang应该具有此功能,因为clang生成的AST 在其自身与源代码行和列之间具有非常细粒度的映射.

谢谢.

小智 0

This isn't really a solution so much as an excuse but...

I believe the first entry (5, 8) includes code for both the initializer and condition statements in the for loop. When I compile a program with a for loop, these two statements end up in a contiguous range of addresses.

强制 clang 为每个语句生成一个单独的条目会很好,但我似乎找不到任何可以做到这一点的东西。