0%

GDB Tips

GDB 使用技巧

  • 启动
  • dump
  • ptype
  • set
  • info
  • disassemble

启动GDB

默认读取当前目录下的 .gdbinit,使用 -nx不读取 .gdbinit 配置

$ gdb test # 常用于下载调试test
$ gdb -nx test # 常用于查看当前状态

独立调试信息记载

test 编译参数为 -O0 -g -ggdb

  • 获取调试信息 objcopy --only-keep-debug test test.debug
  • gdb 中加载 symbol-file test.debug
  • 分离的调试信息重新链接到可执行问题 objcopy --add-gnu-debuglink test.debug test

Dump Memory

(gdb) help dump
Dump target code/data to a local file.

List of dump subcommands:

dump binary -- Write target code/data to a raw binary file
dump ihex -- Write target code/data to an intel hex file
dump memory -- Write contents of memory to a raw binary file
dump srec -- Write target code/data to an srec file
dump tekhex -- Write target code/data to a tekhex file
dump value -- Write the value of an expression to a raw binary file
dump verilog -- Write target code/data to a verilog hex file

Type "help dump" followed by dump subcommand name for full documentation.
Type "apropos word" to search for commands related to "word".
Command name abbreviations are allowed if unambiguous.
(gdb) help dump memory
Write contents of memory to a raw binary file.
Arguments are FILE START STOP.  Writes the contents of memory within the
range [START .. STOP) to the specified FILE in raw target ordered bytes.

语法 dump memory file $START $END

汇编

  • b *func 断点在 func 入栈之前
  • ni/si 汇编指令级单步执行
  • disassemble func 反汇编 func
  • disassemble /m func 反汇编 func,带有源代码信息
  • disassemble $pc-20,$pc+20 反汇编执行区间代码
  • watch *(int *)0x8049aa4 内存读写断点
  • rwatch *(int *)0x8049aa4 内存读断点
  • awatch *(int *)0x8049aa4 内存读写断点
  • display /i $pc 显示当前汇编指令
  • info r 查看寄存器
  • i r rx 查看rx寄存器

ptype 查看类型

用于查看符号类型 ptype symbol|var

(gdb) ptype struct file
type = struct file {
    cyg_uint32 f_flag;
    cyg_uint16 f_ucount;
    cyg_uint16 f_type;
    cyg_uint32 f_syncmode;
    fileops *f_ops;
    off_t f_offset;
    CYG_ADDRWORD f_data;
    CYG_ADDRWORD f_xops;
    cyg_mtab_entry *f_mte;
    list_head f_node;
    cyg_uint8 f_valid;
}

使用 set print type xxx 配置

whatis 查看类型

whatis var

info address

info address symbol

Describe where the data for symbol is stored. For a register variable, this says which register it is kept in. For a non-register local variable, this prints the stack-frame offset at which the variable is always stored.

查找给定符号的存储地址

info symbol

info symbol addr

Print the name of a symbol which is stored at the address addr. If no symbol is stored exactly at addr, GDB prints the nearest symbol and an offset from it

(gdb) info symbol 0x54320
_initialize_vx + 396 in section .text

查找给定地址的变量或函数的名称

Refs

  1. GDB技巧整理
  2. 16 Examining the Symbol Table
  3. How to generate gcc debug symbol outside the build target?