(gdb) break ... if cond
... 参数用于指定生成断点的具体位置;cond 参数用于代指某个表达式。通过此方式建立的普通断点,只有当表达式 cond 成立(值为 True)时,才会发挥它的作用;反之,断点并不会使程序停止执行。(gdb) watch expr if cond
参数 expr 表示要观察的变量或表达式;参数 cond 用于代指某个表达式。
(gdb) condition bnum expression
(gdb) condition bnum
#include <iostream> using namespace std; int main(){ int num = 1; while(num<20){ try{ throw num; }catch(int &e){ num++; } } cout << num << endl; return 0; }程序存储位置为
~/demo/main.cpp
,并已经生成了可供 GDB 调试器使用的执行文件:
[root@bogon demo]# ls
main.cpp main.exe
[root@bogon demo]# gdb main.exe -q
Reading symbols from ~/demo/main.exe...done.
(gdb) l
1 #include <iostream>
2 using namespace std;
3 int main(){
4 int num = 1;
5 while(num<20){
6 try{
7 throw num;
8 }catch(int &e){
9 num++;
10 }
(gdb)
11 }
12 cout << num << endl;
13 return 0;
14 }
(gdb)
(gdb) b 9 <--添加普通断点
Breakpoint 1 at 0x12d0: file main.cpp, line 9.
(gdb) r
Starting program: /home/test/demo/main.exe
Breakpoint 1, main () at main.cpp:9
9 num++;
(gdb) rwatch num <-- 添加观察断点
Hardware read watchpoint 2: num
(gdb) catch throw int <-- 添加捕捉断点
Catchpoint 3 (throw)
(gdb) info break
Num Type Disp Enb Address What
1 breakpoint keep y 0x00005555555552d0 in main() at main.cpp:9 breakpoint already hit 1 time
2 read watchpoint keep y num
3 catchpoint keep y exception throw matching: int
(gdb) condition 1 num==3 <-- 为普通断点添加条件表达式
(gdb) condition 2 num==5 <-- 为观察断点添加条件表达式
(gdb) condition 3 num==7 <-- 为捕捉断点添加条件表达式
(gdb) c
Continuing.
Breakpoint 1, main () at main.cpp:9 <-- 普通条件断点触发
9 num++;
(gdb) p num
$1 = 3
(gdb) c
Continuing.
Hardware read watchpoint 2: num <-- 观察条件断点触发
Value = 5
0x000055555555526f in main () at main.cpp:7
7 throw num;
(gdb) c
Continuing.
Catchpoint 3 (exception thrown), 0x00007ffff7e81762 in __cxa_throw () <-- 捕捉条件断点触发
from /lib/x86_64-linux-gnu/libstdc++.so.6
(gdb) up
#1 0x0000555555555285 in main () at main.cpp:7
7 throw num;
(gdb) p num
$2 = 7
(gdb)
ignore bnum count
参数 bnum 为某个断点的编号;参数 count 用于指定该断点失效的次数。
(gdb) b 9
Breakpoint 1 at 0x400a33: file main.cpp, line 9.
(gdb) r
Starting program: ~/demo/main.exe
Breakpoint 1, main () at main.cpp:9
9 num++;
(gdb) p num
$1 = 1
(gdb) ignore 1 3
Will ignore next 3 crossings of breakpoint 1.
(gdb) c
Continuing.
Breakpoint 1, main () at main.cpp:9
9 num++;
(gdb) p num
$2 = 5
(gdb)
本文链接:http://task.lmcjl.com/news/14291.html