Skip to content
systemcoding.dev
Debugging

Use Conditional Breakpoints and Ignore Counts in GDB

Stop only when it matters. Set a condition with break ... if, change it with condition, skip hits with ignore, and learn what conditions cost on hot paths.

6 min read
Summarize in AI
Flat illustration of a debugger stopped at a breakpoint beside the register values, with the GNU, C and Arm logos.

A breakpoint on a function that runs ten thousand times is a breakpoint you will stop using after the third continue. The bug usually lives in one call: the one where the argument is negative, or the 4,000th iteration, or the call that comes from a particular caller. GDB gives you two tools for this. A condition is an expression the breakpoint evaluates each time it is reached, and it stops only if the expression is true. An ignore count tells the breakpoint to let the next N hits pass. This post covers both, how to set and change them, and the cost of conditions on a hot path. Output is from GDB 16.3 on x86-64 Linux, built with GCC 14 at -g -O0.

The program

A loop processes a table of measurements. One value is bad, and we want to stop exactly there.

scale.cc
#include <stdio.h>

struct reading {
    int sensor;
    int raw;
};

static int calibrate(int raw)
{
    return raw * 3 - 7;
}

int main(void)
{
    struct reading table[] = {
        {1, 10}, {2, 12}, {3, -50}, {4, 11}, {5, 14}, {6, -2}, {7, 9},
    };
    int count = sizeof table / sizeof table[0];
    long sum = 0;
    for (int i = 0; i < count; i++) {
        int value = calibrate(table[i].raw);
        sum += value;
    }
    printf("sum = %ld\n", sum);
    return 0;
}

The raw values are 10, 12, -50, 11, 14, -2 and 9. Calibrated, they become 23, 29, -157, 26, 35, -13 and 20, which add to -37, so the program prints sum = -37. The negative raw readings are the cases we will hunt. Build and start GDB.

Shell
gcc -g -O0 -Wall -Wextra -o scale scale.c
gdb -q ./scale

Break if: a condition at creation

Add if and an expression after the location. The expression is C, evaluated in the frame of the breakpoint, and may use any variable visible there.

Text
(gdb) break calibrate if raw < 0
Breakpoint 1 at 0x1139: file scale.c, line 10.
(gdb) run
Starting program: /home/dev/scale

Breakpoint 1, calibrate (raw=-50) at scale.c:10
10	    return raw * 3 - 7;
(gdb) continue
Continuing.

Breakpoint 1, calibrate (raw=-2) at scale.c:10
10	    return raw * 3 - 7;
(gdb) continue
Continuing.
sum = -37
[Inferior 1 (process 52211) exited normally]

Seven calls were made and GDB stopped on the two where raw was negative. The other five were evaluated and silently resumed. The if clause is part of the location syntax, so there is no comma or parenthesis; for a location with spaces or a colon, such as break scale.c:21 if i == 3, the if still follows directly.

Adding or removing a condition later

You do not have to decide up front. The condition command attaches an expression to an existing breakpoint by number, and with no expression it removes the condition.

Text
(gdb) break scale.c:21
Breakpoint 2 at 0x1198: file scale.c, line 21.
(gdb) condition 2 i == 5
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
2       breakpoint     keep y   0x0000000000001198 in main at scale.c:21
	stop only if i == 5
(gdb) condition 2
Breakpoint 2 now unconditional.

info breakpoints shows stop only if i == 5 under the entry, which is the quickest way to find a condition you forgot you had set. A mistake in the expression is caught when you set it. If i is not visible at that location, GDB refuses with No symbol "i" in current context. and the breakpoint keeps its previous condition, so you do not end up with a silent condition that never fires.

What you can write in a condition

Anything the expression evaluator supports works. Comparisons and logical operators are the common ones: raw < 0 && raw > -10. String comparison for a char * uses the helper $_streq.

Text
(gdb) break open_file if $_streq(path, "/etc/passwd")

$_streq(a, b) compares two C strings, $_strlen(s) returns a length and $_regex(s, pattern) matches a regular expression. They are GDB convenience functions, and they exist because path == "x" in a condition compares pointers, not text. Another common condition is on the call site: $_caller_is("main") is true when the calling function is main. The functions $_any_caller_is and $_any_caller_matches look further up the stack. These work only if GDB was built with Python support, which every mainstream distribution package is; confirm with show convenience if one is missing.

A condition can also call a function in the program, but that runs real code on every hit, so avoid it unless it is cheap and side-effect free.

Ignore counts: skip the first N hits

Sometimes the failing call is not distinguishable by value, only by position: it is the 5th call, or the 1000th. ignore takes a breakpoint number and a count, and tells GDB to let that many hits pass before stopping.

Text
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) break calibrate
Breakpoint 3 at 0x1139: file scale.c, line 10.
(gdb) ignore 3 4
Will ignore next 4 crossings of breakpoint 3.
(gdb) run
Starting program: /home/dev/scale

Breakpoint 3, calibrate (raw=14) at scale.c:10
10	    return raw * 3 - 7;

The first four calls (raw values 10, 12, -50, 11) passed, and the fifth, with raw 14, stopped. The info breakpoints table tracks the count as it falls: breakpoint already hit 5 times, since GDB counts ignored hits as hits. To find out how many times a place is reached without stopping, set a very large ignore count, run the program to the end, and read the hit count.

Text
(gdb) delete
Delete all breakpoints? (y or n) y
(gdb) break calibrate
Breakpoint 4 at 0x1139: file scale.c, line 10.
(gdb) ignore 4 1000000
Will ignore next 1000000 crossings of breakpoint 4.
(gdb) run
Starting program: /home/dev/scale
sum = -37
[Inferior 1 (process 52290) exited normally]
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
4       breakpoint     keep y   0x0000000000001139 in calibrate at scale.c:10
	breakpoint already hit 7 times

The function was called seven times. This is a clean way to count calls to something without editing the source, and it is a good first step before choosing an ignore count for the real session.

Reading the hit count

Every breakpoint keeps a hit counter, and info breakpoints prints it under the entry as breakpoint already hit N times. The counter is useful beyond debugging a single run. After a conditional breakpoint has fired, the count tells you how many times the condition was true, and after an ignore count it tells you how many crossings happened in total. Two habits make this pay off. Before choosing an ignore count for a hard-to-reproduce stop, run once with a huge count to learn the total number of calls, as above, and pick a number inside that range. And when a conditional breakpoint never fires, check its hit count: zero hits with the code clearly running means the location is wrong or the code path is never taken, while many hits and no stop means the condition is never true, which usually points at a wrong assumption about a value.

Condition versus ignore

UseWhen the difference isExample
Conditiona value, a state or a callerbreak f if n > 100
Ignore counta position in the sequenceignore 1 4999
Botha rare state after a long warm-upcondition plus ignore

If a breakpoint has both a condition and an ignore count, GDB checks the condition first, and only hits where it is true count against the ignore count. So condition 1 raw < 0 followed by ignore 1 1 skips the first negative reading and stops on the second one.

The cost

A condition is evaluated by GDB, not by the program. On every hit the program stops, GDB reads the variables, evaluates the expression and, if false, resumes the program. That is two context switches and some ptrace calls per hit, many times slower than the program's own loop. For a function hit a few thousand times, you will not notice. For one hit millions of times, a condition can turn a one-second run into minutes. Options when that happens: add the test to the source and put a plain breakpoint on its body, use ignore with a computed count, use a hardware watchpoint on the variable that changes, or bring in rr and replay the failing run rather than chase it live.

Conditions also change timing, so a data race may vanish when a conditional breakpoint slows one thread. If a bug disappears under GDB, suspect that before suspecting the condition.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.