第 11 章 通过DWARF实现C语言反射

目录

11.1. DWARF简介
11.2. 查找函数
11.3.
11.4.
11.5.
11.6.
11.7.
11.8.
11.9.
11.10.
11.11.

11.1. DWARF简介

DWARF定义了调试信息格式,调试信息项debugging information entries (DIE)是.debug_*段的信息, 结合行号项是符号源码级别调试必须的。 我们来实际看看到底有什么东东。

        例子程序:
            #include <stdio.h>

struct my_s {
    int a;
    char c;
};
struct my_s myidea = {0};
void do_stuff(int my_arg)
{
   int i;
   int my_local = my_arg + 2;

    for (i = 0; i < my_local; ++i)
        printf("i = %d\n", i);
}

int main()
{
    do_stuff(2);
    return 0;
}

gcc -g dd.c -o dd
readelf -a dd, 可以看到多出了一些调试段,
  [27] .debug_aranges    PROGBITS         0000000000000000  00000968
       0000000000000030  0000000000000000           0     0     1
  [28] .debug_info       PROGBITS         0000000000000000  00000998
       00000000000000e0  0000000000000000           0     0     1
  [29] .debug_abbrev     PROGBITS         0000000000000000  00000a78
       0000000000000089  0000000000000000           0     0     1
  [30] .debug_line       PROGBITS         0000000000000000  00000b01
       000000000000004f  0000000000000000           0     0     1
  [31] .debug_str        PROGBITS         0000000000000000  00000b50
       00000000000000a5  0000000000000001  MS       0     0     1
  [32] .debug_loc        PROGBITS         0000000000000000  00000bf5
       00000000000000c0  0000000000000000           0     0     1
    objdump -Wi dd 
 <1><72>: Abbrev Number: 4 (DW_TAG_structure_type)
    <73>   DW_AT_name        : (indirect string, offset: 0x7c): my_s    
    <77>   DW_AT_byte_size   : 8        
    <78>   DW_AT_decl_file   : 1        
    <79>   DW_AT_decl_line   : 3        
    <7a>   DW_AT_sibling     : <0x97>   
 <2><7e>: Abbrev Number: 5 (DW_TAG_member)
    <7f>   DW_AT_name        : a        
    <81>   DW_AT_decl_file   : 1        
    <82>   DW_AT_decl_line   : 4        
    <83>   DW_AT_type        : <0x4f>   
    <87>   DW_AT_data_member_location: 2 byte block: 23 0       (DW_OP_plus_uconst: 0)
 <2><8a>: Abbrev Number: 5 (DW_TAG_member)
    <8b>   DW_AT_name        : c        
    <8d>   DW_AT_decl_file   : 1        
    <8e>   DW_AT_decl_line   : 5        
    <8f>   DW_AT_type        : <0x6b>   
    <93>   DW_AT_data_member_location: 2 byte block: 23 4       (DW_OP_plus_uconst: 4)
 <1><97>: Abbrev Number: 6 (DW_TAG_subprogram)
    <98>   DW_AT_external    : 1        
    <99>   DW_AT_name        : (indirect string, offset: 0x4d): do_stuff        
    <9d>   DW_AT_decl_file   : 1        
    <9e>   DW_AT_decl_line   : 8        
    <9f>   DW_AT_prototyped  : 1        
    <a0>   DW_AT_low_pc      : 0x1000043c       
    <a4>   DW_AT_high_pc     : 0x100004bc       
    <a8>   DW_AT_frame_base  : 0x0      (location list)
    <ac>   DW_AT_sibling     : <0xd9>   
 <2><b0>: Abbrev Number: 7 (DW_TAG_formal_parameter)
    <b1>   DW_AT_name        : (indirect string, offset: 0x75): my_arg  
    <b5>   DW_AT_decl_file   : 1        
    <b6>   DW_AT_decl_line   : 8        
    <b7>   DW_AT_type        : <0x4f>   
    <bb>   DW_AT_location    : 2 byte block: 91 68      (DW_OP_fbreg: -24)
 <2><be>: Abbrev Number: 8 (DW_TAG_variable)
    <bf>   DW_AT_name        : i        
    <c1>   DW_AT_decl_file   : 1        
    <c2>   DW_AT_decl_line   : 10       
    <c3>   DW_AT_type        : <0xd9>   
    <c7>   DW_AT_location    : 2 byte block: 91 58      (DW_OP_fbreg: -40)
 <2><ca>: Abbrev Number: 9 (DW_TAG_variable)
    <cb>   DW_AT_name        : (indirect string, offset: 0x21): my_local        
    <cf>   DW_AT_decl_file   : 1        
    <d0>   DW_AT_decl_line   : 11       
    <d1>   DW_AT_type        : <0xd9>   
    <d5>   DW_AT_location    : 2 byte block: 91 5c      (DW_OP_fbreg: -36)
 <1><d9>: Abbrev Number: 10 (DW_TAG_volatile_type)
    <da>   DW_AT_type        : <0x4f>   
 <1><de>: Abbrev Number: 11 (DW_TAG_subprogram)
    <df>   DW_AT_external    : 1        
    <e0>   DW_AT_name        : (indirect string, offset: 0x69): main    
    <e4>   DW_AT_decl_file   : 1        
    <e5>   DW_AT_decl_line   : 17       
    <e6>   DW_AT_type        : <0x4f>   
    <ea>   DW_AT_low_pc      : 0x100004bc       
    <ee>   DW_AT_high_pc     : 0x100004f8       
    <f2>   DW_AT_frame_base  : 0x44     (location list)
 <1><f6>: Abbrev Number: 12 (DW_TAG_variable)
    <f7>   DW_AT_name        : (indirect string, offset: 0x6e): myidea  
    <fb>   DW_AT_decl_file   : 1        
    <fc>   DW_AT_decl_line   : 7        
    <fd>   DW_AT_type        : <0x72>   
    <101>   DW_AT_external    : 1       
    <102>   DW_AT_location    : 5 byte block: 3 10 1 8 44       (DW_OP_addr: 10010844)

Debugging Information Entry (DIE).每个DIE有自己的标签 –- 它的类型, 一系列的属性.DIEs通过兄弟和儿子互联,属性值可以指向其他的DIE.