如何在达到给定次数的点之后使GDB断点断开?

问题描述 投票:75回答:2

我有一个被称为大量次数的函数,最终是段错误。

但是,我不想在这个函数上设置一个断点,并且每次调用它都会停止,因为我会在这里待多年。

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

这是否准确,如果是这样,我该怎么做?请提供gdb代码来设置这样的断点。

gdb breakpoints
2个回答
141
投票

阅读GDB手册的section 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

8
投票

continue <n>

这是一个方便的方法,跳过最后一个命中断点n - 1次:

gdb -n -q tmp.out
Reading symbols from tmp.out...done.
(gdb) l
1       #include <stdio.h>
2
3       int main(void) {
4           int i = 0;
5           while (1) {
6               i++;
7               printf("%d\n", i);
8           }
9       }
(gdb) start
Temporary breakpoint 1 at 0x6a8: file tmp.c, line 4.
Starting program: /home/ciro/bak/git/cpp-cheat/gdb/tmp.out

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

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

Breakpoint 2, main () at tmp.c:6
6               i++;
(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).
© www.soinside.com 2019 - 2024. All rights reserved.