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

我有一个被称为大量次的函数,并最终segfaults。

但是,我不想在这个函数中设置一个断点,每次调用它之后都会停下来,因为我会在这里呆上好几年。

我听说我可以在GDB中设置一个counter作为断点,每次命中断点时,计数器递减,只有当counter = 0时才触发。

这是准确的,如果是的话,我该怎么做? 请给gdb代码设置这样一个断点。

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

如果您不知道需要多less次忽略断点,而又不想手动计算,则可能有以下帮助:

  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 

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) pi $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).