1. gdb基础命令
gdb ./myExecutor
(gdb) break main.c:45
(gdb) break func1 断点设置在该函数的开始处
(gdb) info b
(gdb) disable 1
(gdb) enable 1
(gdb) delete 1
(gdb) break test.c:6 if num>0
(gdb) condition 1 num>5 修改断点 1 的条件
(gdb) condition 1 清除断点上的判断条件,但断点本身依然保留
(gdb) list
(gdb) list 6,8 列出6到21行之间的源码
(gdb) handle 2 nostop pass gdb将SIGINT信号传递给gdb的程序
handle <signal> <keywords>
keywords:
nostop GDB should not stop your program when this signal happens. It may still print a message telling you that the signal has come in.
stop GDB should stop your program when this signal happens. This implies the print keyword as well.
print GDB should print a message when this signal happens.
noprint GDB should not mention the occurrence of the signal at all. This implies the nostop keyword as well.
pass / noignore GDB should allow your program to see this signal; your program can handle the signal, or else it may terminate if the signal is fatal and not handled. pass and noignore are synonyms.
nopass / ignore GDB should not allow your program to see this signal. nopass and ignore are synonyms.
(gdb) r config.yaml
(gdb) n
(gdb) c
(gdb) s
(gdb) bt
(gdb) p x
(gdb) info threads
(gdb) whatis width
type = double
(gdb) p width
$4 = 13
(gdb) set var width=47
(gdb) return
(gdb) signal SIGPIPE
(gdb) call x%10
call expr/func
Reference: Debugging with GDB - Table of Contents
2. gdb调试调试运行中的程序
正常可debug程序运行后仍然可以gdb调试,方法如下:
ps -ef | grep [exec]
sudo gdb attach [process-number]
> info thread
> thread 3
> bt
> layout src
3. 调出 GDB TUI, 查看源代码
// 有两种方式:
// 1. Ctrl + X + A
// 2. layout src
默认情况下,GDB TUI 模式会显示 command 窗口和 source 窗口,还有其他窗口,如下列举的四个常用的窗口:
- (cmd)command 命令窗口,可以输入调试命令
- (src)source 源代码窗口, 显示当前行、断点等信息
- (asm)assembly 汇编代码窗口
- (reg)register 寄存器窗口
可以通过“layout + 窗口类型”命令来选择自己需要的窗口,例如,在 cmd 窗口输入 layout asm 则可以切换到汇编代码窗口。
4. cgdb 边调试边查看源码
cgdb安装
sudo apt-get install cgdb
cgdb 使用
启动cgdb
的时候默认光标在gdb
窗口上
- Esc键 :进入cgdb模式
- 按键
i
:回到gdb
窗口 - 鼠标滚轮:滚动浏览源代码
- 按键
s
: 鼠标滚轮可以滚动浏览gdb
的调试窗口 - 按键
-
/+
:按25%的比例来缩小放大源代码窗口 - 按键
Ctrl+W
: 切换源代码窗口和调试窗口的布局(上下/左右) - 按键
空格
:当前行打断点