Product SiteDocumentation Site

5.2.3. Conditional Breakpoints

In many real-world cases, a program may perform its task well during the first few thousand times; it may then start crashing or encountering errors during its eight thousandth iteration of the task. Debugging programs like this can be difficult, as it is hard to imagine a programmer with the patience to issue a continue command thousands of times just to get to the iteration that crashed.
Situations like this are common in real life, which is why GDB allows programmers to attach conditions to a breakpoint. For example, consider the following program:
simple.c
      #include <stdio.h>
      
      main()
      {
        int i;

        for (i = 0;; i++) {
          fprintf (stdout, "i = %d\n", i);
        }
      }
To set a conditional breakpoint at the GDB prompt:
      (gdb) br 8 if i == 8936
      Breakpoint 1 at 0x80483f5: file iterations.c, line 8.
      (gdb) r
With this condition, the program execution will eventually stop with the following output:
      i = 8931
      i = 8932
      i = 8933
      i = 8934
      i = 8935
      
      Breakpoint 1, main () at iterations.c:8
      8           fprintf (stdout, "i = %d\n", i);
Inspect the breakpoint information (using info br) to review the breakpoint status:
      (gdb) info br
      Num     Type           Disp Enb Address    What
      1       breakpoint     keep y   0x080483f5 in main at iterations.c:8
              stop only if i == 8936
              breakpoint already hit 1 time