2.断点 观察点 单步执行
2.1常用命令列表
| 命令格式 | 缩写 | 简介 |
|---|---|---|
| break [file:]function |
b | 在(file文件的)function函数中设置一个断点 |
| run [arglist] | r | 运行您的程序(如果指定了arglist,则将arglist作为参数运行程序) |
| continue | c | 继续运行您的程序(在停止之后,比如在一个断点之后) |
| list | l | 列出产生执行文件的源代码的一部分 |
| next | n | 单步执行(在停止之后)跳过函数调用 |
| step | s | 单步执行(在停止之后)进入函数调用 |
| watch | / | 使你能监视一个变量的值而不管它何时被改变 |
| finish | f | 继续执行,直到当前函数返回 |
| return | r | 强制从当前函数返回 |
| shell | ! | 使你能不离开gdb就执行UNIX shell命令 |
| quit | q | 推出GDB |
[!NOTE]
注意:一般命令都以首字母作为命令缩写
2.2 list 命令
| 命令 | 作用 |
|---|---|
| list 1 | 定位到第一行 |
| list - | 显示上一行 |
| list + | 显示下一行 |
| set listsize 30 | 每次list 显示30行 |
| show listsize | 显示行号 |
| %% 注意:list显示上一行 %% |
(gdb) set listsize 30
(gdb) l
11 printf("FUNC FUNC FUNC\n");
12 }
13 };
14
15
16 void testFun(std::string str) {
17 printf("str: %s \n", str.c_str());
18
19 str = "vvvv";
20
21 printf("str: %s \n", str.c_str());
22 }
23
24
25 int testFun(int num) {
26 printf("num: %d\n", num);
27 int cnt = 190;
28 printf("cnt: %d\n", cnt);
29 return -2;
30 }
31
32
33 int main(int argc, char *argv[]) {
--Type <RET> for more, q to quit, c to continue without paging-- c
34 int a = 123;
35 char buf[128] = "456";
36 std::string str = "abc";
37
38
39 for (int i = 0; i < argc; i++) {
40 printf("argv[%d]: %s\n", i, argv[i]);
2.3 添加断点与相关执行命令
b 52 # 行号
b main # 函数名称
n #执行下一行
continue #执行到下一断点处
注意:next 命令 不会进入函数内部
2.4 硬观察点与软观察点
| 类型 | 硬观察点 | 软观察点 |
|---|---|---|
| 触发条件 | 硬件直接执行 | 软件的中断执行 |
| 数量 | 一般系统有4个 | |
| 性能 | 高 | 低 (会执行中断) |
(gdb) i watchpoints
Num Type Disp Enb Address What
1 hw watchpoint keep y g_num
使用效果
(gdb) r
Starting program: /mnt/d/CMD/cmd
This GDB supports auto-downloading debuginfo from the following URLs:
<https://debuginfod.ubuntu.com>
Enable debuginfod for this session? (y or [n]) n
Debuginfod has been disabled.
To make this setting permanent, add 'set debuginfod enabled off' to .gdbinit.
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
argv[0]: /mnt/d/CMD/cmd
num: 9988
Hardware watchpoint 1: g_num
Old value = 0
New value = 111
testFun (num=9988) at main.cpp:32
32 printf("cnt: %d\n", cnt);
(gdb) c
Continuing.
cnt: 190
buf:zzxx
str: xxxx
Hardware watchpoint 1: g_num
Old value = 111
New value = 222
testFun (str="vvvv") at main.cpp:24
24 printf("str: %s \n", str.c_str());
当观察点变量修改时被触发。

浙公网安备 33010602011771号