Show Table of Contents
4.3.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

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.