Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 559
Tony Stark Avatar asked Jun 02 '10 10:06

Tony Stark


People also ask

How do I apply conditional break point in GDB?

A conditional breakpoint insertion can be done by using one single command by using the breakpoint command followed by the word if followed by a condition. In the previous example, the breakpoint can be inserted with the line b 29 if (y == 999).

How do you set a breakpoint which only occurs when a set of conditions is true?

Use conditional breakpoints to conditionally stop program execution. Breakpoints normally stop the execution every time a certain line or function is reached. However, using the condition keyword, a breakpoint will only be activated if a certain condition is true.

How do I stop GDB after continue?

You should interrupt the process that is attached by gdb. Do not interrupt gdb itself. Interrupt the process by either ctrl-c in the terminal in which the process was started or send the process the SIGINT by kill -2 procid. With procid the id of the process being attached.


2 Answers

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 
like image 142
Kilian Foth Avatar answered Sep 23 '22 02:09

Kilian Foth


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