GDB Cheatsheet
Breakpoints
Use this GDB reference while you build software engineering projects, review code, or refresh the syntax you reach for most.
Setting Them
break is b. A location is a file and line, a function, an address, or a
+/- offset from the current line.
| Command | Short | Stops at |
|---|---|---|
break file.c:42 | b file.c:42 | that line in that file |
break 42 | b 42 | line 42 of the current source file |
break parse_row | b parse_row | first line of the function body |
break Klass::method | — | a C++ member function |
break *0x401136 | b *0x401136 | an exact address, no source needed |
break *parse_row | — | the function entry, before the prologue |
break +2 / break -1 | — | relative to the current line |
break file.c:42 if i > 9 | — | conditional, see below |
break | b | the next instruction, useful after a stop |
tbreak main | — | temporary, deleted after it fires once |
rbreak ^http_ | — | every function matching a regex |
break -qualified main | — | exactly main, not Ns::main |
(gdb) b main Breakpoint 1 at 0x1149: file prog.c, line 12. (gdb) b prog.c:42 (gdb) tbreak parse_row (gdb) rbreak ^str.*_dup$
rbreak is a fast way to trap a whole subsystem, but it can set hundreds of
breakpoints and slow the program to a crawl. Check info breakpoints after.
Conditions, Counts, and Commands
Conditions are the difference between a useful breakpoint and hitting continue four hundred times.
| Command | Effect |
|---|---|
break f if x == 0 | condition set at creation |
condition 3 argc > 2 | add or change the condition on breakpoint 3 |
condition 3 | remove the condition |
ignore 3 500 | skip the next 500 hits of breakpoint 3 |
enable once 3 | fire once, then disable itself |
enable count 5 3 | fire 5 times, then disable |
enable delete 3 | fire once, then delete |
commands 3 ... end | script what happens on each hit |
(gdb) b insert if key == NULL (gdb) b list.c:88 if n->next == n # catch a self-loop (gdb) condition 2 strcmp(name, "root") == 0 (gdb) ignore 1 999 Will ignore next 999 crossings of breakpoint 1.
commands with continue at the end turns a breakpoint into a trace point that
prints and keeps going. silent must be the first line to suppress the usual
stop banner.
(gdb) commands 1 Type commands for breakpoint(s) 1, one per line. >silent >printf "n=%d ptr=%p\n", n, ptr >continue >end
Conditions are evaluated by gdb in the inferior's stopped state, so a condition on
a hot function costs real time. If a loop crawls, use ignore or a hardware
watchpoint instead.
Listing, Disabling, Deleting
(gdb) info breakpoints # i b Num Type Disp Enb Address What 1 breakpoint keep y 0x0000000000001149 in main at prog.c:12 stop only if argc > 1 breakpoint already hit 1 time 2 hw watchpoint keep y total (gdb) disable 2 (gdb) enable (gdb) delete 1 3-5 (gdb) delete # all of them, asks first (gdb) clear prog.c:42 # delete by location instead of number
| Command | Effect |
|---|---|
info breakpoints / i b | list all, with conditions and hit counts |
disable / enable | no argument means every breakpoint |
delete N | remove by number, ranges like 3-5 work |
clear LOCATION | remove whatever is at that location |
save breakpoints bp.txt | write them all out as a source-able script |
source bp.txt | restore them in a later session |
Numbers are never reused inside a session, so breakpoint 12 stays 12 after you
delete 1 through 11. save breakpoints plus source is the cheap way to keep a
setup across restarts without editing .gdbinit.
Dprintf and Tracepoints
dprintf is a breakpoint that only prints, with no scripting block to write.
(gdb) dprintf list.c:88, "visiting %d\n", n->key (gdb) set dprintf-style gdb # gdb, call, or agent (gdb) set dprintf-function fprintf (gdb) set dprintf-channel stderr
With set dprintf-style call the printing happens in the inferior through its own
printf, which is much faster than stopping and resuming for every hit, but it
mixes into the program's own output buffering.