Skip to content
systemcoding.dev
Debugging

Add a condition to an existing breakpoint with condition

Turn an existing breakpoint into a conditional one with the condition command, and clear it again without deleting the breakpoint.

1 min read
Summarize in AI

You set a plain breakpoint, hit it forty times, and only the forty-first call matters. You do not need to delete it and retype the location: condition N expr attaches an expression to breakpoint number N, and GDB stops there only when it is true. In GDB 16 the expression is evaluated in the frame of the breakpoint every time the location is reached.

Text
(gdb) break parse_line
Breakpoint 1 at 0x401136: file parse.c, line 18.
(gdb) condition 1 len > 4096
(gdb) info breakpoints
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000000000401136 in parse_line at parse.c:18
	stop only if len > 4096
(gdb) condition 1
Breakpoint 1 now unconditional.

Giving condition just the number removes the test and the breakpoint stays. The same expression can be written at creation time as break parse_line if len > 4096, so the command matters when you already have the breakpoint, or when you want to edit a condition that turned out to be wrong.

The cost is that GDB stops the process on every hit to evaluate the condition, so a hot function called millions of times makes the program crawl. If it is that hot, pair the condition with a cheaper check, such as an ignore count. A condition that names a variable not in scope at the location is rejected when you set it, with No symbol "len" in current context.

Taggedclinuxgdb
Share

More snippets

Get new posts by email

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

Unsubscribe in one click. No spam, ever.