addr2line&readelf&objdump

测试程序:

zhoukangli@ubuntu:~/Documents$ cat hello.c
#include<stdio.h>
#include<stdlib.h>
int main(){
	int x=0;
	printf("hello\n");
	int countk=0;
	char *p=NULL;
	*p=0x01;
	
	printf("countk=%d\n", countk);
return 0;
}

gcc -g hello hello.c 生成带调试信息的elf文件

使用gdb 进行监控执行

gdb ./test

zhoukangli@ubuntu:~/Documents$ gdb ./hello
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./hello...done.
(gdb) r ----------->执行代码
Starting program: /home/zhoukangli/Documents/hello 
hello

Program received signal SIGSEGV, Segmentation fault.
0x00000000004005a9 in main () at hello.c:8 ------------->会提醒到底在那个地址,哪一行内容出现了段错误
8		*p=0x01;
(gdb) 

反汇编:

objdump -s hello


000000000040057d <main>:
  40057d:	55                   	push   %rbp
  40057e:	48 89 e5             	mov    %rsp,%rbp
  400581:	48 83 ec 10          	sub    $0x10,%rsp
  400585:	c7 45 f0 00 00 00 00 	movl   $0x0,-0x10(%rbp)
  40058c:	bf 54 06 40 00       	mov    $0x400654,%edi
  400591:	e8 ba fe ff ff       	callq  400450 <puts@plt>
  400596:	c7 45 f4 00 00 00 00 	movl   $0x0,-0xc(%rbp)
  40059d:	48 c7 45 f8 00 00 00 	movq   $0x0,-0x8(%rbp)
  4005a4:	00 
  4005a5:	48 8b 45 f8          	mov    -0x8(%rbp),%rax
  4005a9:	c6 00 01             	movb   $0x1,(%rax) ------------->赋值
  4005ac:	8b 45 f4             	mov    -0xc(%rbp),%eax
  4005af:	89 c6                	mov    %eax,%esi
  4005b1:	bf 5a 06 40 00       	mov    $0x40065a,%edi
  4005b6:	b8 00 00 00 00       	mov    $0x0,%eax
  4005bb:	e8 a0 fe ff ff       	callq  400460 <printf@plt>
  4005c0:	b8 00 00 00 00       	mov    $0x0,%eax
  4005c5:	c9                   	leaveq 
  4005c6:	c3                   	retq   
  4005c7:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
  4005ce:	00 00 

通过addr2line 得到对应地址的对应于源码的行数:

zhoukangli@ubuntu:~/Documents$ addr2line -e hello 0x4005a9
/home/zhoukangli/Documents/hello.c:8
显示elf文件的debug 区域
zhoukangli@ubuntu:~/Documents$ readelf -w hello
Contents of the .eh_frame section:

00000000 0000000000000014 00000000 CIE
  Version:               1
  Augmentation:          "zR"
  Code alignment factor: 1
  Data alignment factor: -8
  Return address column: 16
  Augmentation data:     1b

  DW_CFA_def_cfa: r7 (rsp) ofs 8
  DW_CFA_offset: r16 (rip) at cfa-8
  DW_CFA_undefined: r16 (rip)

00000018 0000000000000014 0000001c FDE cie=00000000 pc=0000000000400490..00000000004004ba
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop

00000030 0000000000000014 00000000 CIE
  Version:               1
  Augmentation:          "zR"
  Code alignment factor: 1
  Data alignment factor: -8
  Return address column: 16
  Augmentation data:     1b

  DW_CFA_def_cfa: r7 (rsp) ofs 8
  DW_CFA_offset: r16 (rip) at cfa-8
  DW_CFA_nop
  DW_CFA_nop

00000048 0000000000000024 0000001c FDE cie=00000030 pc=0000000000400440..0000000000400490
  DW_CFA_def_cfa_offset: 16
  DW_CFA_advance_loc: 6 to 0000000000400446
  DW_CFA_def_cfa_offset: 24
  DW_CFA_advance_loc: 10 to 0000000000400450
  DW_CFA_def_cfa_expression (DW_OP_breg7 (rsp): 8; DW_OP_breg16 (rip): 0; DW_OP_lit15; DW_OP_and; DW_OP_lit11; DW_OP_ge; DW_OP_lit3; DW_OP_shl; DW_OP_plus)
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop

00000070 000000000000001c 00000044 FDE cie=00000030 pc=000000000040057d..00000000004005c7
  DW_CFA_advance_loc: 1 to 000000000040057e
  DW_CFA_def_cfa_offset: 16
  DW_CFA_offset: r6 (rbp) at cfa-16
  DW_CFA_advance_loc: 3 to 0000000000400581
  DW_CFA_def_cfa_register: r6 (rbp)
  DW_CFA_advance_loc1: 69 to 00000000004005c6
  DW_CFA_def_cfa: r7 (rsp) ofs 8
  DW_CFA_nop
  DW_CFA_nop

00000090 0000000000000044 00000064 FDE cie=00000030 pc=00000000004005d0..0000000000400635
  DW_CFA_advance_loc: 2 to 00000000004005d2
  DW_CFA_def_cfa_offset: 16
  DW_CFA_offset: r15 (r15) at cfa-16
  DW_CFA_advance_loc: 5 to 00000000004005d7
  DW_CFA_def_cfa_offset: 24
  DW_CFA_offset: r14 (r14) at cfa-24
  DW_CFA_advance_loc: 5 to 00000000004005dc
  DW_CFA_def_cfa_offset: 32
  DW_CFA_offset: r13 (r13) at cfa-32
  DW_CFA_advance_loc: 5 to 00000000004005e1
  DW_CFA_def_cfa_offset: 40
  DW_CFA_offset: r12 (r12) at cfa-40
  DW_CFA_advance_loc: 8 to 00000000004005e9
  DW_CFA_def_cfa_offset: 48
  DW_CFA_offset: r6 (rbp) at cfa-48
  DW_CFA_advance_loc: 8 to 00000000004005f1
  DW_CFA_def_cfa_offset: 56
  DW_CFA_offset: r3 (rbx) at cfa-56
  DW_CFA_advance_loc: 13 to 00000000004005fe
  DW_CFA_def_cfa_offset: 64
  DW_CFA_advance_loc: 44 to 000000000040062a
  DW_CFA_def_cfa_offset: 56
  DW_CFA_advance_loc: 1 to 000000000040062b
  DW_CFA_def_cfa_offset: 48
  DW_CFA_advance_loc: 1 to 000000000040062c
  DW_CFA_def_cfa_offset: 40
  DW_CFA_advance_loc: 2 to 000000000040062e
  DW_CFA_def_cfa_offset: 32
  DW_CFA_advance_loc: 2 to 0000000000400630
  DW_CFA_def_cfa_offset: 24
  DW_CFA_advance_loc: 2 to 0000000000400632
  DW_CFA_def_cfa_offset: 16
  DW_CFA_advance_loc: 2 to 0000000000400634
  DW_CFA_def_cfa_offset: 8
  DW_CFA_nop

000000d8 0000000000000014 000000ac FDE cie=00000030 pc=0000000000400640..0000000000400642
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop
  DW_CFA_nop

000000f0 ZERO terminator


Contents of the .debug_aranges section:

  Length:                   44
  Version:                  2
  Offset into .debug_info:  0x0
  Pointer Size:             8
  Segment Size:             0

    Address            Length
    000000000040057d 000000000000004a 
    0000000000000000 0000000000000000 

Contents of the .debug_info section:

  Compilation Unit @ offset 0x0:
   Length:        0xc8 (32-bit)
   Version:       4
   Abbrev Offset: 0x0
   Pointer Size:  8
 <0><b>: Abbrev Number: 1 (DW_TAG_compile_unit)
    <c>   DW_AT_producer    : (indirect string, offset: 0x31): GNU C 4.8.4 -mtune=generic -march=x86-64 -g -fstack-protector	
    <10>   DW_AT_language    : 1	(ANSI C)
    <11>   DW_AT_name        : (indirect string, offset: 0xbe): hello.c	
    <15>   DW_AT_comp_dir    : (indirect string, offset: 0x7d): /home/zhoukangli/Documents	
    <19>   DW_AT_low_pc      : 0x40057d	
    <21>   DW_AT_high_pc     : 0x4a	
    <29>   DW_AT_stmt_list   : 0x0	
 <1><2d>: Abbrev Number: 2 (DW_TAG_base_type)
    <2e>   DW_AT_byte_size   : 8	
    <2f>   DW_AT_encoding    : 7	(unsigned)
    <30>   DW_AT_name        : (indirect string, offset: 0x1f): long unsigned int
 <1><34>: Abbrev Number: 2 (DW_TAG_base_type)
    <35>   DW_AT_byte_size   : 1	
    <36>   DW_AT_encoding    : 8	(unsigned char)
    <37>   DW_AT_name        : (indirect string, offset: 0x6f): unsigned char	
 <1><3b>: Abbrev Number: 2 (DW_TAG_base_type)
    <3c>   DW_AT_byte_size   : 2	
    <3d>   DW_AT_encoding    : 7	(unsigned)
    <3e>   DW_AT_name        : (indirect string, offset: 0x98): short unsigned int	
 <1><42>: Abbrev Number: 2 (DW_TAG_base_type)
    <43>   DW_AT_byte_size   : 4	
    <44>   DW_AT_encoding    : 7	(unsigned)
    <45>   DW_AT_name        : (indirect string, offset: 0x24): unsigned int	
 <1><49>: Abbrev Number: 2 (DW_TAG_base_type)
    <4a>   DW_AT_byte_size   : 1	
    <4b>   DW_AT_encoding    : 6	(signed char)
    <4c>   DW_AT_name        : (indirect string, offset: 0x71): signed char	
 <1><50>: Abbrev Number: 2 (DW_TAG_base_type)
    <51>   DW_AT_byte_size   : 2	
    <52>   DW_AT_encoding    : 5	(signed)
    <53>   DW_AT_name        : (indirect string, offset: 0xab): short int	
 <1><57>: Abbrev Number: 3 (DW_TAG_base_type)
    <58>   DW_AT_byte_size   : 4	
    <59>   DW_AT_encoding    : 5	(signed)
    <5a>   DW_AT_name        : int	
 <1><5e>: Abbrev Number: 2 (DW_TAG_base_type)
    <5f>   DW_AT_byte_size   : 8	
    <60>   DW_AT_encoding    : 5	(signed)
    <61>   DW_AT_name        : (indirect string, offset: 0x5): long int	
 <1><65>: Abbrev Number: 2 (DW_TAG_base_type)
    <66>   DW_AT_byte_size   : 8	
    <67>   DW_AT_encoding    : 7	(unsigned)
    <68>   DW_AT_name        : (indirect string, offset: 0xb5): sizetype	
 <1><6c>: Abbrev Number: 4 (DW_TAG_pointer_type)
    <6d>   DW_AT_byte_size   : 8	
    <6e>   DW_AT_type        : <0x72>	
 <1><72>: Abbrev Number: 2 (DW_TAG_base_type)
    <73>   DW_AT_byte_size   : 1	
    <74>   DW_AT_encoding    : 6	(signed char)
    <75>   DW_AT_name        : (indirect string, offset: 0x78): char	
 <1><79>: Abbrev Number: 2 (DW_TAG_base_type)
    <7a>   DW_AT_byte_size   : 8	
    <7b>   DW_AT_encoding    : 5	(signed)
    <7c>   DW_AT_name        : (indirect string, offset: 0x0): long long int	
 <1><80>: Abbrev Number: 2 (DW_TAG_base_type)
    <81>   DW_AT_byte_size   : 8	
    <82>   DW_AT_encoding    : 7	(unsigned)
    <83>   DW_AT_name        : (indirect string, offset: 0x1a): long long unsigned int	
 <1><87>: Abbrev Number: 5 (DW_TAG_subprogram)
    <88>   DW_AT_external    : 1	
    <88>   DW_AT_name        : (indirect string, offset: 0x15): main	
    <8c>   DW_AT_decl_file   : 1	
    <8d>   DW_AT_decl_line   : 3	
    <8e>   DW_AT_type        : <0x57>	
    <92>   DW_AT_low_pc      : 0x40057d	
    <9a>   DW_AT_high_pc     : 0x4a	
    <a2>   DW_AT_frame_base  : 1 byte block: 9c 	(DW_OP_call_frame_cfa)
    <a4>   DW_AT_GNU_all_tail_call_sites: 1	
 <2><a4>: Abbrev Number: 6 (DW_TAG_variable)
    <a5>   DW_AT_name        : x	
    <a7>   DW_AT_decl_file   : 1	
    <a8>   DW_AT_decl_line   : 4	
    <a9>   DW_AT_type        : <0x57>	
    <ad>   DW_AT_location    : 2 byte block: 91 60 	(DW_OP_fbreg: -32)
 <2><b0>: Abbrev Number: 7 (DW_TAG_variable)
    <b1>   DW_AT_name        : (indirect string, offset: 0xe): countk	
    <b5>   DW_AT_decl_file   : 1	
    <b6>   DW_AT_decl_line   : 6	
    <b7>   DW_AT_type        : <0x57>	
    <bb>   DW_AT_location    : 2 byte block: 91 64 	(DW_OP_fbreg: -28)
 <2><be>: Abbrev Number: 6 (DW_TAG_variable)
    <bf>   DW_AT_name        : p	
    <c1>   DW_AT_decl_file   : 1	
    <c2>   DW_AT_decl_line   : 7	
    <c3>   DW_AT_type        : <0x6c>	
    <c7>   DW_AT_location    : 2 byte block: 91 68 	(DW_OP_fbreg: -24)
 <2><ca>: Abbrev Number: 0
 <1><cb>: Abbrev Number: 0

Contents of the .debug_abbrev section:

  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_pointer_type    [no children]
    DW_AT_byte_size    DW_FORM_data1
    DW_AT_type         DW_FORM_ref4
    DW_AT value: 0     DW_FORM value: 0
   5      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_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
   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

Raw dump of debug contents of section .debug_line:

  Offset:                      0x0
  Length:                      62
  DWARF Version:               2
  Prologue Length:             30
  Minimum Instruction Length:  1
  Initial value of 'is_stmt':  1
  Line Base:                   -5
  Line Range:                  14
  Opcode Base:                 13

 Opcodes:
  Opcode 1 has 0 args
  Opcode 2 has 1 args
  Opcode 3 has 1 args
  Opcode 4 has 1 args
  Opcode 5 has 1 args
  Opcode 6 has 0 args
  Opcode 7 has 0 args
  Opcode 8 has 0 args
  Opcode 9 has 1 args
  Opcode 10 has 0 args
  Opcode 11 has 0 args
  Opcode 12 has 1 args

 The Directory Table is empty.

 The File Name Table (offset 0x1c):
  Entry	Dir	Time	Size	Name
  1	0	0	0	hello.c

 Line Number Statements:
  [0x00000028]  Extended opcode 2: set Address to 0x40057d
  [0x00000033]  Special opcode 7: advance Address by 0 to 0x40057d and Line by 2 to 3 ---------->对应行号
  [0x00000034]  Special opcode 118: advance Address by 8 to 0x400585 and Line by 1 to 4
  [0x00000035]  Special opcode 104: advance Address by 7 to 0x40058c and Line by 1 to 5
  [0x00000036]  Special opcode 146: advance Address by 10 to 0x400596 and Line by 1 to 6
  [0x00000037]  Special opcode 104: advance Address by 7 to 0x40059d and Line by 1 to 7
  [0x00000038]  Special opcode 118: advance Address by 8 to 0x4005a5 and Line by 1 to 8
  [0x00000039]  Special opcode 105: advance Address by 7 to 0x4005ac and Line by 2 to 10
  [0x0000003a]  Advance PC by constant 17 to 0x4005bd
  [0x0000003b]  Special opcode 48: advance Address by 3 to 0x4005c0 and Line by 1 to 11
  [0x0000003c]  Special opcode 76: advance Address by 5 to 0x4005c5 and Line by 1 to 12
  [0x0000003d]  Advance PC by 2 to 0x4005c7
  [0x0000003f]  Extended opcode 1: End of Sequence


Contents of the .debug_str section:

  0x00000000 6c6f6e67 206c6f6e 6720696e 7400636f long long int.co
  0x00000010 756e746b 006d6169 6e006c6f 6e67206c untk.main.long l
  0x00000020 6f6e6720 756e7369 676e6564 20696e74 ong unsigned int
  0x00000030 00474e55 20432034 2e382e34 202d6d74 .GNU C 4.8.4 -mt
  0x00000040 756e653d 67656e65 72696320 2d6d6172 une=generic -mar
  0x00000050 63683d78 38362d36 34202d67 202d6673 ch=x86-64 -g -fs
  0x00000060 7461636b 2d70726f 74656374 6f720075 tack-protector.u
  0x00000070 6e736967 6e656420 63686172 002f686f nsigned char./ho
  0x00000080 6d652f7a 686f756b 616e676c 692f446f me/zhoukangli/Do
  0x00000090 63756d65 6e747300 73686f72 7420756e cuments.short un
  0x000000a0 7369676e 65642069 6e740073 686f7274 signed int.short
  0x000000b0 20696e74 0073697a 65747970 65006865  int.sizetype.he
  0x000000c0 6c6c6f2e 6300                       llo.c.


反过来可以查到对应的行号是对的

zhoukangli@ubuntu:~/Documents$ addr2line -e hello 0x40057d
/home/zhoukangli/Documents/hello.c:3


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值