1. condition
1.1 功能
既可以为现有的普通断点、观察断点以及捕捉断点添加条件表达式,也可以对条件断点的条件表达式进行修改。
1.2 语法
(gdb) condition bnum expression //用于为 bnum 编号的断点添加或修改 expression 条件表达式
(gdb) condition bnum //用于删除 bnum 编号断点的条件表达式,使其变成普通的无条件断点
参数 bnum 用于代指目标断点的编号;参数 expression 表示为断点添加或修改的条件表达式。
1.3 示例
#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;
}
编译并调试:
[root@localhost day4]# g++ test1.cpp -o test1 -g
[root@localhost day4]# gdb test1 -q
Reading symbols from /home/gdb/day4/test1...done.
(gdb) l 0
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)
Line number 15 out of range; test1.cpp has 14 lines.
(gdb) b 9
Breakpoint 1 at 0x400a75: file test1.cpp, line 9.
(gdb) r
Starting program: /home/gdb/day4/test1
Breakpoint 1, main () at test1.cpp:9
9 num++;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64
(gdb) rwatch num
Hardware read watchpoint 2: num
(gdb) catch throw
Catchpoint 3 (throw)
(gdb) info breakpoints
Num Type Disp Enb Address What
1 breakpoint keep y 0x0000000000400a75 in main() at test1.cpp:9
breakpoint already hit 1 time
2 read watchpoint keep y num
3 breakpoint keep y 0x00007ffff7ab2c50 exception throw
(gdb) condition 1 num==3 <-- 为普通断点添加条件表达式
(gdb) condition 2 num==5 <-- 为普通断点添加条件表达式
(gdb) c
Continuing.
Catchpoint 3 (exception thrown), __cxxabiv1::__cxa_throw (obj=0x613ca0, tinfo=0x6010a0 <_ZTIi@@CXXABI_1.3>, dest=0x0) at ../../.././libstdc++-v3/libsupc++/eh_throw.cc:80
80 __cxa_eh_globals *globals = __cxa_get_globals ();
(gdb) p num
No symbol "num" in current context.
(gdb) condition 3 num==7
No symbol "num" in current context.
(gdb) c
Continuing.
Catchpoint 3 (exception thrown), __cxxabiv1::__cxa_throw (obj=0x613ca0, tinfo=0x6010a0 <_ZTIi@@CXXABI_1.3>, dest=0x0) at ../../.././libstdc++-v3/libsupc++/eh_throw.cc:80
80 __cxa_eh_globals *globals = __cxa_get_globals (); <-----捕捉条件断点触发
(gdb) c
Continuing.
Breakpoint 1, main () at test1.cpp:9
9 num++;
(gdb) p num
$1 = 3
(gdb) c
Continuing.
Catchpoint 3 (exception thrown), __cxxabiv1::__cxa_throw (obj=0x613ca0, tinfo=0x6010a0 <_ZTIi@@CXXABI_1.3>, dest=0x0) at ../../.././libstdc++-v3/libsupc++/eh_throw.cc:80
80 __cxa_eh_globals *globals = __cxa_get_globals ();
(gdb) up
#1 0x0000000000400a38 in main () at test1.cpp:7
7 throw num;
(gdb) p num
$2 = 4
(gdb)
可以看到,通过借助 condition 命令为不同类型断点设置条件表达式,只有当条件表达式成立(值为 True)时,相应的断点才会触发从而使程序暂停运行。
2. ignore
2.1 功能
ignore 命令也可以使一个断点成为条件断点,但这里的“条件”并非自定义的表达式,而仅为一个整数,它用来表示该断点失效的次数。也就会说,ignore 命令可以使目标断点暂时失去作用,当断点失效的次数超过指定次数时,断点的功能会自动恢复。
2.2 语法
ignore bnum count
2.3 示例
[root@localhost day4]# gdb test1 -q
Reading symbols from /home/gdb/day4/test1...done.
(gdb) b 9
Breakpoint 1 at 0x400a75: file test1.cpp, line 9.
(gdb) r
Starting program: /home/gdb/day4/test1
Breakpoint 1, main () at test1.cpp:9
9 num++;
Missing separate debuginfos, use: debuginfo-install glibc-2.17-323.el7_9.x86_64 libgcc-4.8.5-44.el7.x86_64
(gdb) p num
$1 = 1
(gdb) ignore 1 3
Will ignore next 3 crossings of breakpoint 1.
(gdb) c
Continuing.
Breakpoint 1, main () at test1.cpp:9
9 num++;
(gdb) p num
$2 = 5
(gdb)
可以看到,执行 ignore 命令之前,num 变量的值为 1。借助 ignore 命令使编号为 1(作用于第 9 行)的断点失效 3 次后,继续执行程序,最终程序仍暂停至第 9 行,此时 num 的值变为 5。这这恰恰证明了 num 从 1 递增至 5 的过程中,编号为 1 的断点失效了 3 次。