Skip to content

GDB ​


帮助 ​

在线文档

shell
gdb -h

启动 ​

shell
gdb program_name
gdb program_name core
gdb program_name pid
gdb -p pid
gdb --args parogram_name args


# 读符号表
-symbols file
-s file
# 增加源码搜索路径
-directory directory
-d directory
# 指定core文件
-core file
-c file

退出 ​

shell
exit
#或 
Ctrl+d

运行时设置 ​

shell
# gui与命令行切换
ctrl+x+a
# 切换窗口
crtl+x+o
# 打印数组
set print array on/num
set print array off
# 屏蔽信号
handle SIGPIPE nostop
#跟踪子进程
set follow-fork-mode child
#跟踪父进程
set follow-fork-mode parent  
#子进程也不 detach 
set detach-on-fork off

运行时命令 ​

gdb里可以用TAB键补全

命令缩写作用示例
info查看信息
breakb增加断点b file:line
b fun
break locspec thread thread-id
break locspec thread thread-id if …
clear清除断点
delete删除断点
enable/disable breakpoints
runr开始执行
continuec继续执行
printp打印变量p var
watch监视变量
rwatch变量被写时暂停
awatch变量被读或者被写时暂停
bt查看堆栈信息

多线程调试 ​

命令说明
info threads查询当前线程ID
thread id切换线程
thread apply [thread-id-list] [all] args在线程上执行命令
set print thread-events控制打印线程启动或结束是的信息
set scheduler-locking off/on/stepoff:不锁定任何线程,也就是所有的线程都执行,这是默认值。
on:只有当前被调试的线程能够执行。
step:阻止其他线程在当前线程单步调试时,抢占当前线程。只有当next、continue、util以及finish的时候,其他线程才会获得重新运行的机会。

日志输出 ​

shell
#Enable logging.
set logging on
#Disable logging.
set logging off
#Show the current values of the logging settings.
show logging

宏信息 ​

shell
gcc -ggdb3
info macro MCRO_NAME
macro expand MCRO_NAME
p MCRO_NAME