如何使 GDB 断点仅在到达给定次数后才中断?

发布于 2024-09-04 02:03:50 字数 190 浏览 5 评论 0原文

我有一个被多次调用的函数,最终出现段错误。

但是,我不想在这个函数上设置断点并在每次调用它后停止,因为我将在这里工作很多年。

我听说我可以在 GDB 中为断点设置一个计数器,每次命中断点时,计数器都会递减,并且只有在计数器达到断点时才会触发= 0。

这准确吗?如果准确,我该怎么做?请给出设置此类断点的 gdb 代码。

I have a function that is called some large number of times, and eventually segfaults.

However, I don't want to set a breakpoint at this function and stop after every time it's called, because I will be here for years.

I've heard that I can set a counter in GDB for a breakpoint, and each time the breakpoint is hit, the counter is decremented, and only gets triggered when the counter = 0.

Is this accurate, and if so how do I do it? Please give the gdb code for setting such a breakpoint.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凉世弥音 2024-09-11 02:03:50

阅读 GDB 手册的第 5.1.6 节。您要做的就是首先设置一个断点,然后为该断点编号设置一个“忽略计数”,例如ignore 23 1000

如果您不知道要忽略断点多少次,并且不想手动计数,以下内容可能会有所帮助:

  ignore 23 1000000   # set ignore count very high.

  run                 # the program will SIGSEGV before reaching the ignore count.
                      # Once it stops with SIGSEGV:

  info break 23       # tells you how many times the breakpoint has been hit, 
                      # which is exactly the count you want

Read section 5.1.6 of the GDB manual. What you have to do is first set a breakpoint, then set an 'ignore count' for that breakpoint number, e.g. ignore 23 1000.

If you don't know how many times to ignore the breakpoint, and don't want to count manually, the following may help:

  ignore 23 1000000   # set ignore count very high.

  run                 # the program will SIGSEGV before reaching the ignore count.
                      # Once it stops with SIGSEGV:

  info break 23       # tells you how many times the breakpoint has been hit, 
                      # which is exactly the count you want
看海 2024-09-11 02:03:50

继续

这是一种方便的方法,可以跳过最后一个命中断点 n - 1 次(因此在第 n 个处停止) hit):

main.c

#include <stdio.h>

int main(void) {
    int i = 0;
    while (1) {
        i++; /* Line 6 */
        printf("%d\n", i);
    }
}

用法:

gdb -n -q main.out

GDB 会话:

Reading symbols from main.out...done.
(gdb) start
Temporary breakpoint 1 at 0x6a8: file main.c, line 4.
Starting program: /home/ciro/bak/git/cpp-cheat/gdb/main.out

Temporary breakpoint 1, main () at main.c:4
4           int i = 0;
(gdb) b 6
Breakpoint 2 at 0x5555555546af: file main.c, line 6.
(gdb) c
Continuing.

Breakpoint 2, main () at main.c:6
6               i++; /* Line 6 */
(gdb) c 5
Will ignore next 4 crossings of breakpoint 2.  Continuing.
1
2
3
4
5

Breakpoint 2, main () at main.c:6
6               i++; /* Line 6 */
(gdb) p i
$1 = 5
(gdb)
(gdb) help c
Continue program being debugged, after signal or breakpoint.
Usage: continue [N]
If proceeding from breakpoint, a number N may be used as an argument,
which means to set the ignore count of that breakpoint to N - 1 (so that
the breakpoint won't break until the Nth time it is reached).

continue <n>

This is a convenient method that skips the last hit breakpoint n - 1 times (and therefore stops at the n-th hit):

main.c

#include <stdio.h>

int main(void) {
    int i = 0;
    while (1) {
        i++; /* Line 6 */
        printf("%d\n", i);
    }
}

Usage:

gdb -n -q main.out

GDB session:

Reading symbols from main.out...done.
(gdb) start
Temporary breakpoint 1 at 0x6a8: file main.c, line 4.
Starting program: /home/ciro/bak/git/cpp-cheat/gdb/main.out

Temporary breakpoint 1, main () at main.c:4
4           int i = 0;
(gdb) b 6
Breakpoint 2 at 0x5555555546af: file main.c, line 6.
(gdb) c
Continuing.

Breakpoint 2, main () at main.c:6
6               i++; /* Line 6 */
(gdb) c 5
Will ignore next 4 crossings of breakpoint 2.  Continuing.
1
2
3
4
5

Breakpoint 2, main () at main.c:6
6               i++; /* Line 6 */
(gdb) p i
$1 = 5
(gdb)
(gdb) help c
Continue program being debugged, after signal or breakpoint.
Usage: continue [N]
If proceeding from breakpoint, a number N may be used as an argument,
which means to set the ignore count of that breakpoint to N - 1 (so that
the breakpoint won't break until the Nth time it is reached).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文