How to make a GDB breakpoint only break after the point is reached a given number times?

breakpoints, gdb

Solution

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

Problem

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.

Original source