Use the following debug_info.c file:
#include<stdio.h>
void func(int arg)
{
int i = 0;
int local = arg +42;
while (i<local)
{
printf("i = %d\n", i++);
}
}
int main()
{
func(23);
return (0);
}
Compile debug_info.c to debug_info using -g:
$ gcc -g -o debug_info debug_info.c
Show the sections .debug_*:
$ objdump -s debug_info|grep "section .debug_" -A 15
Contents of section .debug_aranges:
0000 2c000000 02000000 00000800 00000000 ,...............
0010 26054000 00000000 57000000 00000000 &.@.....W.......
0020 00000000 00000000 00000000 00000000 ................
Contents of section .debug_info:
0000 d3000000 04000000 00000801 66000000 ............f...
0010 0c590000 00250000 00260540 00000000 .Y...%...&.@....
0020 00570000 00000000 00000000 00020807 .W..............
0030 13000000 0201083d 00000002 02070000 .......=........
0040 00000204 07180000 00020106 3f000000 ............?...
0050 020205c1 00000003 0405696e 74000208 ..........int...
0060 05500000 00020807 cb000000 02010646 .P.............F
0070 00000004 bc000000 01032605 40000000 ..........&.@...
0080 00004200 00000000 0000019c b9000000 ..B.............
0090 05617267 00010357 00000002 915c0669 .arg...W.....\.i
00a0 00010557 00000002 916807b6 00000001 ...W.....h......
00b0 06570000 0002916c 00084b00 0000010d .W.....l..K.....
00c0 57000000 68054000 00000000 15000000 W...h.@.........
00d0 00000000 019c00 .......
Contents of section .debug_abbrev:
0000 01110125 0e130b03 0e1b0e11 01120710 ...%............
0010 17000002 24000b0b 3e0b030e 00000324 ....$...>......$
0020 000b0b3e 0b030800 00042e01 3f19030e ...>........?...
0030 3a0b3b0b 27191101 12074018 96421901 :.;.'.....@..B..
0040 13000005 05000308 3a0b3b0b 49130218 ........:.;.I...
0050 00000634 0003083a 0b3b0b49 13021800 ...4...:.;.I....
0060 00073400 030e3a0b 3b0b4913 02180000 ..4...:.;.I.....
0070 082e003f 19030e3a 0b3b0b49 13110112 ...?...:.;.I....
0080 07401896 42190000 00 .@..B....
Contents of section .debug_line:
0000 45000000 02002300 00000101 fb0e0d00 E.....#.........
0010 01010101 00000001 00000100 64656275 ............debu
0020 675f696e 666f2e63 00000000 00000902 g_info.c........
0030 26054000 00000000 15ad7591 30088e86 &.@.......u.0...
0040 3f4b9f59 02020001 01 ?K.Y.....
Contents of section .debug_str:
0000 73686f72 7420756e 7369676e 65642069 short unsigned i
0010 6e74006c 6f6e6720 756e7369 676e6564 nt.long unsigned
0020 20696e74 002f686f 6d652f61 6c69656e int./home/alien
0030 74656b2f 74657374 64697231 00756e73 tek/testdir1.uns
0040 69676e65 64206368 6172006d 61696e00 igned char.main.
0050 6c6f6e67 20696e74 00646562 75675f69 long int.debug_i
0060 6e666f2e 6300474e 55204331 3120352e nfo.c.GNU C11 5.
0070 342e3020 32303136 30363039 202d6d74 4.0 20160609 -mt
0080 756e653d 67656e65 72696320 2d6d6172 une=generic -mar
0090 63683d78 38362d36 34202d67 202d6673 ch=x86-64 -g -fs
00a0 7461636b 2d70726f 74656374 6f722d73 tack-protector-s
00b0 74726f6e 67006c6f 63616c00 66756e63 trong.local.func
00c0 0073686f 72742069 6e740073 697a6574 .short int.sizet
00d0 79706500 ype.
Resolve section .debug_aranges:
0000 2c000000 02000000 00000800 00000000 ,...............
0010 26054000 00000000 57000000 00000000 &.@.....W.......
0020 00000000 00000000 00000000 00000000 ................
(Ref DWARF4 6.1.2 p 107)
2c000000: section length 0x2c, not include length field
0200: version 2
00000000: debug_info_offset 0
08: address_size, 8 bytes of an address
00: segment_size 0
00000000: segment sector 0
26054000 00000000: begining address 400526
57000000 00000000: range 57
00000000 00000000 00000000 00000000: terminate
Resolve section .debug_info:
0000 d3000000 04000000 00000801 66000000 ............f...
0010 0c590000 00250000 00260540 00000000 .Y...%...&.@....
0020 00570000 00000000 00000000 00020807 .W..............
(see DWARF4 7.5.1 p 143)
d3000000: section length 0xd3, not include length field
0400: version 4
0000 0000: debug_abbrev_offset 0
08: address size 8 bytes
01: abbrev No 1
format of abbrev No 1
(see DWARF4 p 154)
Contents of section .debug_abbrev:
0000 01110125 0e130b03 0e1b0e11 01120710
0010 17000002
01: abbreviation code 1
11: DW_TAG_compile_unit (Figure 18, p 151)
01: DW_CHILDREN_yes (Figure 19, p154)
25: DW_AT_producer,(Figure 20, p155)
0e: DW_FORM_strp, string (p160)
13 0b: DW_AT_language; DW_Form_data1, constant
03 0e: DW_AT_name; DW_FORM_strp, string
1b 0e: DW_AT_dir; DW_FORM_strp, string
11 01: DW_AT_low_pc; address
12 07: DW_AT_high_pc; DW_FORM_data8
10 17: DW_AT_stmt_list; DW_FORM_sec_offset
00 00: terminate
so the format of abbrev No 1 include fields:
DW_AT_producer, DW_AT_language, DW_AT_name, DW_AT_dir, DW_AT_low_pc, DW_AT_high_pc, DW_AT_stmt_list
Return to the section .debug_info
66000000: producer, offset 0x66 in section debug_str,
0060 ........ ..00474e 55204331 3120352e GNU C11 5.
0070 342e3020 32303136 30363039 202d6d74 4.0 20160609 -mt
0080 756e653d 67656e65 72696320 2d6d6172 une=generic -mar
0090 63683d78 38362d36 34202d67 202d6673 ch=x86-64 -g -fs
00a0 7461636b 2d70726f 74656374 6f722d73 tack-protector-s
00b0 74726f6e 6700.... trong.
0c: DW_AT_language, 12 (ANSI c99)
59000000: DW_AT_name, offset 0x59 in section debug_str, debug_info.c
0050 ........ ........ 00646562 75675f69 debug_i
0060 6e666f2e 6300.... ........ ........ nfo.c
25000000: DW_AT_dir, offset 0x25 in section debug_str, /home/alientek/testdir1
260540 00000000 00: DW_AT_low_pc, 0x400526
570000 00000000 00: DW_AT_high_pc, 0x57
00000000: DW_AT_stmt_list 0
0020 ........ ........ ........ ..020807 .W..............
0030 13000000 0201083d 00000002 02070000 .......=........
02: abbrev No 2
format of abbrev No 2
Contents of section .debug_abbrev:
0010 ......02 24000b0b 3e0b030e 0000....
02: abbreviation code 2
24: DW_TAG_base_type
00: DW_CHILDREN_no
0b 0b: DW_AT_byte_size; constant
3e 0b: DW_AT_encoding; constant
03 0e: DW_AT_name; DW_FORM_strp, string
Return to the section .debug_info
08: DW_AT_byte_size 8
07: DW_AT_encoding 7 (unsigned)
13000000: DW_AT_name, offset 0x13 in section debug_str, long unsigned int
0010 ....006c 6f6e6720 756e7369 676e6564
0020 20696e74 00......
Resolve section .debug_line:
see DWARF4 6.2
0000 45000000 02002300 00000101 fb0e0d00 E.....#.........
0010 01010101 00000001 00000100 64656275 ............debu
0020 675f696e 666f2e63 00000000 00000902 g_info.c........
0030 26054000 00000000 15ad7591 30088e86 &.@.......u.0...
0040 3f4b9f59 02020001 01 ?K.Y.....
45000000: section length 0x45, not include length field
0200: version 2
23000000: header length 0x45, not include length field
01: mimimum_instruction_length, 1 bytes
01: default_is_stmt 1
fb: line_base -5
0e: line_range 14
0d: opcode_base 13
00 01010101 00000001 000001: standard_opcode_lengths 0, 1, 1, 1, 1, 0 ,0, 0, 1, 0, 0, 1
00 64656275 675f696e 666f2e63 00: file name, debug_info.c
000000 00: Padding
00 09 02: Extended code 02, DW_LNE_set_address, length 09
26054000 00000000: set address 0x400526
desired line increment =(opcode - opcode_base) % line_range + line_base
operation advance = (opcode - opcode_base) / line_range
line: line number in source file, line += line increment
operation, machine instruction address, operation += operation adv
15: Special opcode 8 (0x15 -0x0d = 8), line increment 3, operation advance 0,
advance Address by 0 to 0x400526 and Line by 3 to 4
ad: Special opcode 160: advance Address by 11 to 0x400531 and Line by 1 to 5
75: Special opcode 104: advance Address by 7 to 0x400538 and Line by 1 to 6
91: Special opcode 132: advance Address by 9 to 0x400541 and Line by 1 to 7
30: Special opcode 35: advance Address by 2 to 0x400543 and Line by 2 to 9
08: Advance PC by constant 17 to 0x400554 (corresponding to Special opcode 255, p120)
8e: Special opcode 129: advance Address by 9 to 0x40055d and Line by -2 to 7
......
0202: DW_LNS_advance_pc 2, Advance PC by 2 to 0x40057d (p119)
000101: Extended code 01, DW_LNE_end_sequence, length 01
void func(int arg)
{
400526: 55 push %rbp
400527: 48 89 e5 mov %rsp,%rbp
40052a: 48 83 ec 20 sub $0x20,%rsp
40052e: 89 7d ec mov %edi,-0x14(%rbp)
int i = 0; //line 5
400531: c7 45 f8 00 00 00 00 movl $0x0,-0x8(%rbp)
int local = arg +42; // line 6
400538: 8b 45 ec mov -0x14(%rbp),%eax
40053b: 83 c0 2a add $0x2a,%eax
40053e: 89 45 fc mov %eax,-0x4(%rbp)
while (i<local) // line 7
400541: eb 1a jmp 40055d <func+0x37>
{
printf("i = %d\n", i++); //line 9
400543: 8b 45 f8 mov -0x8(%rbp),%eax
400546: 8d 50 01 lea 0x1(%rax),%edx
400549: 89 55 f8 mov %edx,-0x8(%rbp)
40054c: 89 c6 mov %eax,%esi
40054e: bf 04 06 40 00 mov $0x400604,%edi
400553: b8 00 00 00 00 mov $0x0,%eax
400558: e8 a3 fe ff ff callq 400400 <printf@plt>
void func(int arg)
{
int i = 0;
int local = arg +42;
while (i<local) //line 7
40055d: 8b 45 f8 mov -0x8(%rbp),%eax
400560: 3b 45 fc cmp -0x4(%rbp),%eax
400563: 7c de jl 400543 <func+0x1d>
{
printf("i = %d\n", i++);
}
} //line 11
400565: 90 nop
400566: c9 leaveq
400567: c3 retq
0000000000400568 <main>:
int main()
{ //line 14
400568: 55 push %rbp
400569: 48 89 e5 mov %rsp,%rbp
func(23); //line 15
40056c: bf 17 00 00 00 mov $0x17,%edi
400571: e8 b0 ff ff ff callq 400526 <func>
return (0); //line 16
400576: b8 00 00 00 00 mov $0x0,%eax
40057b: 5d pop %rbp
40057c: c3 retq
40057d: 0f 1f 00 nopl (%rax)
$ objdump -g debug_info
debug_info: 文件格式 elf64-x86-64
.debug_aranges 节的内容:
长度: 44
版本: 2
.debug_info 节中的偏移量: 0x0
指针大小: 8
节区大小: 0
地址 长度
0000000000400526 0000000000000057
0000000000000000 0000000000000000
.debug_info 节的内容:
编译单元 @ 偏移 0x0:
长度: 0xd3 (32-bit)
版本: 4
缩写偏移量: 0x0
指针大小: 8
<0><b>:缩写编号:1 (DW_TAG_compile_unit)
<c> DW_AT_producer : (间接字串,偏移量:0x66): GNU C11 5.4.0 20160609 -mtune=generic -march=x86-64 -g -fstack-protector-strong
<10> DW_AT_language : 12 (ANSI C99)
<11> DW_AT_name : (间接字串,偏移量:0x59): debug_info.c
<15> DW_AT_comp_dir : (间接字串,偏移量:0x25): /home/alientek/testdir1
<19> DW_AT_low_pc : 0x400526
<21> DW_AT_high_pc : 0x57
<29> DW_AT_stmt_list : 0x0
<1><2d>:缩写编号:2 (DW_TAG_base_type)
<2e> DW_AT_byte_size : 8
<2f> DW_AT_encoding : 7 (unsigned)
<30> DW_AT_name : (间接字串,偏移量:0x13): long unsigned int
<1><34>:缩写编号:2 (DW_TAG_base_type)
<35> DW_AT_byte_size : 1
<36> DW_AT_encoding : 8 (unsigned char)
<37> DW_AT_name : (间接字串,偏移量:0x3d): unsigned char
<1><3b>:缩写编号:2 (DW_TAG_base_type)
<3c> DW_AT_byte_size : 2
<3d> DW_AT_encoding : 7 (unsigned)
<3e> DW_AT_name : (间接字串,偏移量:0x0): short unsigned int
<1><42>:缩写编号:2 (DW_TAG_base_type)
<43> DW_AT_byte_size : 4
<44> DW_AT_encoding : 7 (unsigned)
<45> DW_AT_name : (间接字串,偏移量:0x18): unsigned int
<1><49>:缩写编号:2 (DW_TAG_base_type)
<4a> DW_AT_byte_size : 1
<4b> DW_AT_encoding : 6 (signed char)
<4c> DW_AT_name : (间接字串,偏移量:0x3f): signed char
<1><50>:缩写编号:2 (DW_TAG_base_type)
<51> DW_AT_byte_size : 2
<52> DW_AT_encoding : 5 (signed)
<53> DW_AT_name : (间接字串,偏移量:0xc1): short int
<1><57>:缩写编号:3 (DW_TAG_base_type)
<58> DW_AT_byte_size : 4
<59> DW_AT_encoding : 5 (signed)
<5a> DW_AT_name : int
<1><5e>:缩写编号:2 (DW_TAG_base_type)
<5f> DW_AT_byte_size : 8
<60> DW_AT_encoding : 5 (signed)
<61> DW_AT_name : (间接字串,偏移量:0x50): long int
<1><65>:缩写编号:2 (DW_TAG_base_type)
<66> DW_AT_byte_size : 8
<67> DW_AT_encoding : 7 (unsigned)
<68> DW_AT_name : (间接字串,偏移量:0xcb): sizetype
<1><6c>:缩写编号:2 (DW_TAG_base_type)
<6d> DW_AT_byte_size : 1
<6e> DW_AT_encoding : 6 (signed char)
<6f> DW_AT_name : (间接字串,偏移量:0x46): char
<1><73>:缩写编号:4 (DW_TAG_subprogram)
<74> DW_AT_external : 1
<74> DW_AT_name : (间接字串,偏移量:0xbc): func
<78> DW_AT_decl_file : 1
<79> DW_AT_decl_line : 3
<7a> DW_AT_prototyped : 1
<7a> DW_AT_low_pc : 0x400526
<82> DW_AT_high_pc : 0x42
<8a> DW_AT_frame_base : 1 字节区块: 9c (DW_OP_call_frame_cfa)
<8c> DW_AT_GNU_all_tail_call_sites: 1
<8c> DW_AT_sibling : <0xb9>
<2><90>:缩写编号:5 (DW_TAG_formal_parameter)
<91> DW_AT_name : arg
<95> DW_AT_decl_file : 1
<96> DW_AT_decl_line : 3
<97> DW_AT_type : <0x57>
<9b> DW_AT_location : 2 字节区块: 91 5c (DW_OP_fbreg: -36)
<2><9e>:缩写编号:6 (DW_TAG_variable)
<9f> DW_AT_name : i
<a1> DW_AT_decl_file : 1
<a2> DW_AT_decl_line : 5
<a3> DW_AT_type : <0x57>
<a7> DW_AT_location : 2 字节区块: 91 68 (DW_OP_fbreg: -24)
<2><aa>:缩写编号:7 (DW_TAG_variable)
<ab> DW_AT_name : (间接字串,偏移量:0xb6): local
<af> DW_AT_decl_file : 1
<b0> DW_AT_decl_line : 6
<b1> DW_AT_type : <0x57>
<b5> DW_AT_location : 2 字节区块: 91 6c (DW_OP_fbreg: -20)
<2><b8>:缩写编号:0
<1><b9>:缩写编号:8 (DW_TAG_subprogram)
<ba> DW_AT_external : 1
<ba> DW_AT_name : (间接字串,偏移量:0x4b): main
<be> DW_AT_decl_file : 1
<bf> DW_AT_decl_line : 13
<c0> DW_AT_type : <0x57>
<c4> DW_AT_low_pc : 0x400568
<cc> DW_AT_high_pc : 0x15
<d4> DW_AT_frame_base : 1 字节区块: 9c (DW_OP_call_frame_cfa)
<d6> DW_AT_GNU_all_tail_call_sites: 1
<1><d6>:缩写编号:0
.debug_abbrev 节的内容:
Number TAG (0x0)
1 DW_TAG_compile_unit [has children]
DW_AT_producer DW_FORM_strp
DW_AT_language DW_FORM_data1
DW_AT_name DW_FORM_strp
DW_AT_comp_dir DW_FORM_strp
DW_AT_low_pc DW_FORM_addr
DW_AT_high_pc DW_FORM_data8
DW_AT_stmt_list DW_FORM_sec_offset
DW_AT value: 0 DW_FORM value: 0
2 DW_TAG_base_type [no children]
DW_AT_byte_size DW_FORM_data1
DW_AT_encoding DW_FORM_data1
DW_AT_name DW_FORM_strp
DW_AT value: 0 DW_FORM value: 0
3 DW_TAG_base_type [no children]
DW_AT_byte_size DW_FORM_data1
DW_AT_encoding DW_FORM_data1
DW_AT_name DW_FORM_string
DW_AT value: 0 DW_FORM value: 0
4 DW_TAG_subprogram [has children]
DW_AT_external DW_FORM_flag_present
DW_AT_name DW_FORM_strp
DW_AT_decl_file DW_FORM_data1
DW_AT_decl_line DW_FORM_data1
DW_AT_prototyped DW_FORM_flag_present
DW_AT_low_pc DW_FORM_addr
DW_AT_high_pc DW_FORM_data8
DW_AT_frame_base DW_FORM_exprloc
DW_AT_GNU_all_tail_call_sites DW_FORM_flag_present
DW_AT_sibling DW_FORM_ref4
DW_AT value: 0 DW_FORM value: 0
5 DW_TAG_formal_parameter [no children]
DW_AT_name DW_FORM_string
DW_AT_decl_file DW_FORM_data1
DW_AT_decl_line DW_FORM_data1
DW_AT_type DW_FORM_ref4
DW_AT_location DW_FORM_exprloc
DW_AT value: 0 DW_FORM value: 0
6 DW_TAG_variable [no children]
DW_AT_name DW_FORM_string
DW_AT_decl_file DW_FORM_data1
DW_AT_decl_line DW_FORM_data1
DW_AT_type DW_FORM_ref4
DW_AT_location DW_FORM_exprloc
DW_AT value: 0 DW_FORM value: 0
7 DW_TAG_variable [no children]
DW_AT_name DW_FORM_strp
DW_AT_decl_file DW_FORM_data1
DW_AT_decl_line DW_FORM_data1
DW_AT_type DW_FORM_ref4
DW_AT_location DW_FORM_exprloc
DW_AT value: 0 DW_FORM value: 0
8 DW_TAG_subprogram [no children]
DW_AT_external DW_FORM_flag_present
DW_AT_name DW_FORM_strp
DW_AT_decl_file DW_FORM_data1
DW_AT_decl_line DW_FORM_data1
DW_AT_type DW_FORM_ref4
DW_AT_low_pc DW_FORM_addr
DW_AT_high_pc DW_FORM_data8
DW_AT_frame_base DW_FORM_exprloc
DW_AT_GNU_all_tail_call_sites DW_FORM_flag_present
DW_AT value: 0 DW_FORM value: 0
.debug_line 节的调试内容转储:
偏移: 0x0
长度: 69
DWARF 版本: 2
导言长度: 35
最小指令长度: 1
“is_stmt”的初始值: 1
行基数: -5
行范围: 14
操作码基数: 13
操作码:
操作码 1 具有 0 个参数
操作码 2 具有 1 个参数
操作码 3 具有 1 个参数
操作码 4 具有 1 个参数
操作码 5 具有 1 个参数
操作码 6 具有 0 个参数
操作码 7 具有 0 个参数
操作码 8 具有 0 个参数
操作码 9 具有 1 个参数
操作码 10 具有 0 个参数
操作码 11 具有 0 个参数
操作码 12 具有 1 个参数
目录表为空。
文件名表 (偏移 0x1c):
条目 目录 时间 大小 名称
1 0 0 0 debug_info.c
行号语句:
[0x0000002d] 扩充操作码 2: 设置地址为 0x400526
[0x00000038] Special opcode 8: advance Address by 0 to 0x400526 and Line by 3 to 4
[0x00000039] Special opcode 160: advance Address by 11 to 0x400531 and Line by 1 to 5
[0x0000003a] Special opcode 104: advance Address by 7 to 0x400538 and Line by 1 to 6
[0x0000003b] Special opcode 132: advance Address by 9 to 0x400541 and Line by 1 to 7
[0x0000003c] Special opcode 35: advance Address by 2 to 0x400543 and Line by 2 to 9
[0x0000003d] Advance PC by constant 17 to 0x400554
[0x0000003e] Special opcode 129: advance Address by 9 to 0x40055d and Line by -2 to 7
[0x0000003f] Special opcode 121: advance Address by 8 to 0x400565 and Line by 4 to 11
[0x00000040] Special opcode 50: advance Address by 3 to 0x400568 and Line by 3 to 14
[0x00000041] Special opcode 62: advance Address by 4 to 0x40056c and Line by 1 to 15
[0x00000042] Special opcode 146: advance Address by 10 to 0x400576 and Line by 1 to 16
[0x00000043] Special opcode 76: advance Address by 5 to 0x40057b and Line by 1 to 17
[0x00000044] Advance PC by 2 to 0x40057d
[0x00000046] 扩充操作码 1: 序列结束
.debug_str 节的内容:
0x00000000 73686f72 7420756e 7369676e 65642069 short unsigned i
0x00000010 6e74006c 6f6e6720 756e7369 676e6564 nt.long unsigned
0x00000020 20696e74 002f686f 6d652f61 6c69656e int./home/alien
0x00000030 74656b2f 74657374 64697231 00756e73 tek/testdir1.uns
0x00000040 69676e65 64206368 6172006d 61696e00 igned char.main.
0x00000050 6c6f6e67 20696e74 00646562 75675f69 long int.debug_i
0x00000060 6e666f2e 6300474e 55204331 3120352e nfo.c.GNU C11 5.
0x00000070 342e3020 32303136 30363039 202d6d74 4.0 20160609 -mt
0x00000080 756e653d 67656e65 72696320 2d6d6172 une=generic -mar
0x00000090 63683d78 38362d36 34202d67 202d6673 ch=x86-64 -g -fs
0x000000a0 7461636b 2d70726f 74656374 6f722d73 tack-protector-s
0x000000b0 74726f6e 67006c6f 63616c00 66756e63 trong.local.func
0x000000c0 0073686f 72742069 6e740073 697a6574 .short int.sizet
0x000000d0 79706500 ype.