gdb按照指定条件设置断点进行调试

gdb调试有时候会遇到多次迭代debug某个函数,例如查看判断第n次进入该函数的情况,主要用到的方法是条件(condition)断点,下面介绍下条件断点的使用方法,一个简单的例子:

#include <iostream>
int main() {
    int i = 0;
    for (int k = 0; k < 10; k++) {
        i = k + 1;
        std::cout << k << std::endl;
        std::cout << i << std::endl;
    }
    return 0;
}

编译后进行调试:

g++ debug_test.cc  -g -o debug_test
gdb ./debug_test

设置一个断点进行调试,结果如下:

(gdb) b 6
Breakpoint 1 at 0x401167: file debug_test.cc, line 6.
(gdb) r
Starting program: /home/s/shenzhou/c_test/debug_test 
warning: File "/usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py
line to your configuration file "/home/s/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/s/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) c
Continuing.
0
1

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) c
Continuing.
1
2

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) c
Continuing.
2
3

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) c
Continuing.
3
4

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) 

可以看到,在第6行设置断点后,每次运行到这里就会停止,那么如果想直接看变量i = 6的情况,是否可以直接跑到这个位置呢?这里需要condition命令:

condition bnum expression

bnum为断点的标号(可通过info break查看),expression为条件表达式,例如我们希望直接跳到i=5,操作如下:

(gdb) b 6
Breakpoint 1 at 0x401167: file debug_test.cc, line 6.
(gdb) r
Starting program: /home/s/shenzhou/c_test/debug_test 
warning: File "/usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py
line to your configuration file "/home/s/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/s/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401167 in main() at debug_test.cc:6
        breakpoint already hit 1 time
(gdb) condition 1 i==5
(gdb) c
Continuing.
0
1
1
2
2
3
3
4

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) p i
$1 = 5
(gdb) p k
$2 = 4

这里首先设置断点,通过break info查看断点的num信息,最后通过condition来实现具体条件下的条件断点,之后continue就可以运行到指定的条件下了。这里需要关联具体变量作条件,我们也可以不需要具体变量,直接跳到第n次运行到该断点,这里需要用到ignore命令:

ignore bnum count

同样bnum是断点的编号,count是断点的失效次数,即超过指定次数后才会恢复,例如我们希望循环5次后停下,操作如下:

(gdb) b 6
Breakpoint 1 at 0x401167: file debug_test.cc, line 6.
(gdb) info break
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401167 in main() at debug_test.cc:6
(gdb) r
Starting program: /home/s/shenzhou/c_test/debug_test 
warning: File "/usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py" auto-loading has been declined by your `auto-load safe-path' set to "$debugdir:$datadir/auto-load".
To enable execution of this file add
        add-auto-load-safe-path /usr/local/gcc12/lib64/libstdc++.so.6.0.33-gdb.py
line to your configuration file "/home/s/.gdbinit".
To completely disable this security protection add
        set auto-load safe-path /
line to your configuration file "/home/s/.gdbinit".
For more information about this security protection see the
"Auto-loading safe path" section in the GDB manual.  E.g., run from the shell:
        info "(gdb)Auto-loading safe path"

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) ignore 1 5
Will ignore next 5 crossings of breakpoint 1.
(gdb) c
Continuing.
0
1
1
2
2
3
3
4
4
5
5
6

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) p i
$1 = 7
(gdb) p k
$2 = 6
(gdb) c
Continuing.
6
7

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) c
Continuing.
7
8

Breakpoint 1, main () at debug_test.cc:6
6               std::cout << k << std::endl;
(gdb) 

这里,通过命令

ignore 1 5

会在第5次执行到断点1的位置才会停下来。

  • 11
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值