Skip to content
systemcoding.dev
Debugging

Set a one-time breakpoint with tbreak

Set a breakpoint that deletes itself after the first hit with tbreak, so a one-off stop never fires again on later passes.

1 min read
Summarize in AI

tbreak creates a temporary breakpoint: it behaves like break, then deletes itself the first time it stops the program. It is the right tool when you want to get into a function once, look around, and not be interrupted again by the loop that calls it.

Text
(gdb) tbreak handle_request
Temporary breakpoint 1 at 0x40125a: file server.c, line 52.
(gdb) run
Temporary breakpoint 1, handle_request (fd=4) at server.c:52
52	    char buf[512];
(gdb) info breakpoints
No breakpoints or watchpoints.

The Disp column of info breakpoints would have shown del instead of keep before the hit, and after it the table is simply empty. GDB's own start command is a tbreak main followed by run, which is why the stop in main never comes back.

tbreak takes the same location forms as break, including file:line, *address and if conditions, so tbreak server.c:70 if fd == 7 stops once, the first time the condition is true. It is one hit and gone: if the first hit is the wrong one, the breakpoint is already deleted and you must set it again. Documented in the GDB manual on breakpoints, written against GDB 16.

Taggedclinuxgdb
Share

More snippets

Systems Programming

Get the page size with sysconf(_SC_PAGESIZE)

Read the memory page size at run time with sysconf(_SC_PAGESIZE) instead of hard-coding 4096, which is wrong on arm64 kernels built with 16K or 64K pages.

1 min read

Get new posts by email

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

Unsubscribe in one click. No spam, ever.