0%

[linux] 12. gdb dubugger

GDB, the GNU Project debugger,它诞生于 GNU 计划(同时诞生的还有 GCC、Emacs 等),是 Linux 下常用的程序调试器。 本文将对一些常用的gdb指令进行简单介绍。 ## 启动GDB

debug 某个进程#
1
gdb -p 1234
指定参数#

gdb bebug gcc, 并且设置gcc参数为 ‘-O2 -c foo.c’

1
gdb --args gcc -O2 -c foo.c

反汇编相关#

反汇编#

disassemble 是以反汇编清单的形式输出内存的内容,表示的格式由命令set disassembly-flavor确定

1
2
3
4
5
6
7
8
9
10
(gdb) disassemble main
Dump of assembler code for function main:
0x0000000000400506 <+0>: push %rbp
0x0000000000400507 <+1>: mov %rsp,%rbp
0x000000000040050a <+4>: mov $0x4005a4,%edi
0x000000000040050f <+9>: mov $0x0,%eax
0x0000000000400514 <+14>: callq 0x4003e0 <printf@plt>
0x0000000000400519 <+19>: pop %rbp
0x000000000040051a <+20>: retq
End of assembler dump.

将汇编指令格式 设置为intel格式

1
set disassembly-flavor intel 

设置断点 b / break#
1
2
3
# break 0x000000000040050f

# b 0x000000000040050a
列出所有断点#
1
2
3
4
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y <PENDING> 0x000000000040050f
2 breakpoint keep y <PENDING> 0x000000000040050a
执行run到第一个断点处#
1
2
3
4
5
6
7
8
9
10
(gdb) start
Temporary breakpoint 2 at 0x40050a: file hello.c, line 4.
Starting program: /home/xxx/myfiles/cpp_code/hell

Temporary breakpoint 2, main () at hello.c:4
4 printf("hello");
4: /x $eax = 0x400506
3: /x $rsp = 0x7fffffffe4c0
2: /x $edi = 0x1
1: /x $rbp = 0x7fffffffe4c0
显示寄存器的值#
1
display /x $eax
设置和显示参数#
1
2
3
(gdb) set args display /x $eax
(gdb) show args
Argument list to give program being debugged when it is started is "display /x $eax"

环境相关#

path directory#

加入directory到运行查找路径

1
(gdb) path leo/mydir

show paths#

打印运行时查找路径

1
2
3
(gdb) show paths
Executable and object file path: /home/xxx/myfiles/cpp_code/directory:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games:/usr/local/mongodb/bin

show environment#

打印环境变量

1
2
3
4
5
6
7
8
(gdb) show environment
TERM=xterm
SHELL=/bin/bash
HISTSIZE=10000
SSH_CLIENT=10.252.71.151 63267 32200
SSH_TTY=/dev/pts/3
LC_ALL=en_US.UTF-8
HISTFILESIZE=100000

set environment varname [=value]#

设置环境变量

1
set env USER = foo

unset environment varname#

移除环境变量

pwd#

显示当前工作路径

1
2
(gdb) pwd
Working directory /home/xxx/myfiles/cpp_code.

cd#

切换gdb工作路径

1
2
3
4
(gdb) pwd
Working directory /home/xxx/myfiles/cpp_code.
(gdb) cd ~
Working directory /home/xxx.

程序输入与输出#

info terminal#

显示GDB保存的终端信息

1
2
3
4
5
6
7
(gdb) info terminal
Inferior's terminal status (currently saved by GDB):
File descriptor flags = O_RDWR | 0x8000
Process group = 19973
c_iflag = 0xd00, c_oflag = 0x5,
c_cflag = 0xbf, c_lflag = 0x8a3b
c_cc: 0x3 0x1c 0x8 0x15 0x4 0x0 0x1 0x0 0x11 0x13 0x1a 0x0 0x12 0xf 0x17 0x16 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0 0x0

run > outfile#

重定向输出

debug正在运行的进程#

由于之前使用Ctrl+Z强制退出,再次进入时报错 is already traced by xxx

1
2
3
4
5
gdb -p 22512 --quiet
Attaching to process 22512
warning: process 22512 is already traced by process 22307
ptrace: Operation not permitted.
(gdb)

jobs -l 找出正在终端运行的 process-id

1
2
3
jobs -l
[1]- 20412 Stopped sh export.sh
[2]+ 22307 Stopped gdb -p 22512

attach加入进程#

找到当前gdb运行进程 22307,attach进入

1
2
(gdb) attach 22307
Attaching to process 22307

detach#

释放gdb当前占有进程

运行#

step 与 next#
  • step [count] 单步执行,会进入子函数内
  • next [count] 单行执行时,不进入子函数内
step-mode#
1
2
3
set step-mode on

set step-mode off

step-mode on时, 对于没有debug行的function, 程序会停留在function的首条指令而不是跳过它。开启时可以详细查看机器指令

查看当前模式

1
show step-mode

reference#

  1. Debugging with GDB