linux调试工具集

除了GDB以外,linux还提供了很多的调试工具:
注意:以下工具的使用之前,gcc需要使用-g编译出带符号的elf

1) addr2line

用于将可执行文件的虚拟内存地址或者动态库的偏移量转换成函数以及代码行。

动态库的偏移量计算方式:将虚拟内存地址减去/proc/pid/maps中该动态库的虚拟内存起始地址

示例: addr2line -C -f -e xxx.elf 0x1234abcd

查看使用方法:man 1 addr2line

DESCRIPTION
       addr2line translates addresses into file names and line numbers.  Given an address in an executable or an offset in a section of a relocatable object, it uses the debugging information to figure out which file name and line number are associated with it. The executable or relocatable object to use is specified with the -e option.  The default is the file a.out.  The section in the relocatable

2) readelf
读取elf的相关信息,如段信息。

使用方法:man 1 readelf

DESCRIPTION
       readelf displays information about one or more ELF format object files.  The options control what particular information to display.

  用法示例:

readelf -S xxx.elf  Display the sections' header (查看段信息,如数据段,代码段,debug段)
readelf -l xxx.elf  Display the program headers (查看虚拟地址加载信息),如下示例:

Elf file type is EXEC (Executable file)
Entry point 0x4008a300
There are 9 program headers, starting at offset 64

Program Headers:
  Type           Offset             VirtAddr           PhysAddr
                 FileSiz            MemSiz              Flags  Align
  PHDR           0x0000000000000040 0x0000000040000040 0x0000000040000040
                 0x00000000000001f8 0x00000000000001f8  R E    8
......
      [Requesting program interpreter: /lib64/ld-linux-x86-64.so.2]
  LOAD           0x0000000000000000 0x0000000040000000 0x0000000040000000
                 0x0000000003e2f220 0x0000000005328000  RWE    200000
  LOAD           0x0000000003f28000 0x0000000045328000 0x0000000045328000
                 0x00000000000024f0 0x00000000028df3e8  RWE    200000
......

如果想dump某些段内的信息,可以通过readelf -x 来hex-dump 或者通过readelf -p来string-dump,比如c/c++内的string literals

经过编译可能会存放在.rodata 只读数据段内,可以通过readelf -p .rodata xxx.elf > your outputfile 来dump
       
3) objdump
反汇编,查看汇编代码,对于可执行文件elf,第一列为其虚拟内存地址。

用法示例:
objdump -d xxx.elf

xxx.elf:     file format elf64-x86-64


Disassembly of section .init:

000000004008a260 <_init>:
......

Disassembly of section .text:

000000004008a300 <_start>:
    4008a300:   31 ed                   xor    %ebp,%ebp
    4008a302:   49 89 d1                mov    %rdx,%r9
    4008a305:   5e                      pop    %rsi
    4008a306:   48 89 e2                mov    %rsp,%rdx
    4008a309:   48 83 e4 f0             and    $0xfffffffffffffff0,%rsp
    4008a30d:   50                      push   %rax

如果在执行objdump时报:"objdump: can't disassemble for architecture UNKNOWN!",可以通过objdump -i来查看支持的architecture, 如果有合适的,可以执行例如objdump -m i386 -d xxx.elf, 如果没有合适的,那么可以换台机器。

对于C++来说,编译结果会有name mangling,如果想查看清晰的原名称,可以使用 -- demangle

比如objdump --demangle -d test >test.out

4) 查看进程maps
/poc/进程ID号/下的maps文件,包含了可执行文件的虚拟内存地址空间布局。
例如:
40000000-4008a000 rwxp 00000000 00:25 668976                             /opt/bin/xxx
4008a000-43709000 r-xp 0008a000 00:25 668976                             /opt/bin/xxx
43709000-43e30000 rwxp 03709000 00:25 668976                             /opt/bin/xxx
43e30000-45328000 r-xp 00000000 00:00 0
45328000-4532b000 rwxp 03f28000 00:25 668976                             /opt/bin/xxx
4532b000-47c08000 rwxp 00000000 00:00 0
482fc000-483a1000 rwxp 00000000 00:00 0                                  [heap]
7f4ac0efd000-7f4ac0efe000 ---p 00000000 00:00 0
7f4ac0efe000-7f4ac16fe000 rwxp 00000000 00:00 0

......

7f4b31fc7000-7f4b3217f000 r-xp 00000000 00:12 6986                       /lib64/libc-2.17.so
7f4b3217f000-7f4b3237f000 ---p 001b8000 00:12 6986                       /lib64/libc-2.17.so
7f4b3237f000-7f4b32383000 r-xp 001b8000 00:12 6986                       /lib64/libc-2.17.so
7f4b32383000-7f4b32385000 rwxp 001bc000 00:12 6986                     /lib64/libc-2.17.so
7f4b32385000-7f4b3238a000 rwxp 00000000 00:00 0

5) pstack

查看进程Stack,只支持32位系统,并且需要确保__pthread_threads_debug已经定义。
RESTRICTIONS

    pstack currently works only on Linux, only on an x86 machine running 32
    bit   ELF   binaries  (64  bit  not  supported).   Also,  for  symbolic
    information, you need to use a GNU compiler to generate  your  program,
    and  you can't strip symbols from the binaries.  For thread information
    to  be  dumped,  you  have  to  use  the  debug-aware  version  of  the
    LinuxThreads  libpthread.so  library.   (To  check,  run  nm(1) on your
    pthreads    library,    and    make    sure     that     the     symbol
    "__pthread_threads_debug"  is defined.)  Threads are not supported with
    the newer NPTL libpthread.so library.

6) gstack

弥补了pstack的不足,支持64bit

查看某个进程所有线程的FBT:  gstack 进程id

查看某个线程的FBT:                 gstack 线程id(LWP id)

7) ldd
查看可执行文件使用了哪些动态链接库

8) gcore
生成进程core dump

9) strace

查看进程/线程的系统调用

查看某个进程所有线程的系统调用:strace -fp 进程id

查看某个线程的系统调用:               strace -p 线程id (LWP id)

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值