How do I make a GDB breakpoint break only after it reaches a given number of times?

I have a function called a lot of times, and ultimately segfaults.

However, I do not want to set a breakpoint in this function and stop after each call, because I will be here for years.

I heard that I can set counter in GDB for a breakpoint, and every time a breakpoint is hit, the counter decreases and only starts when counter = 0.

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

+80
breakpoints gdb
Jun 02 '10 at
source share
2 answers

Read section 5.1.6 of the GDB manual. What you need to do is first set a breakpoint and then set a “ignore count” for that breakpoint number, for example. ignore 23 1000 .

If you don’t know how many times to ignore the breakpoint and don’t want to count manually, this can 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 
+152
Jun 02 '10 at
source share

continue <n>

This is a convenient method that skips the last point of interruption of an impact n - 1 times:

 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). 
+10
Sep 16 '17 at 8:45
source share



All Articles