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.

CommandShortStops at
break file.c:42b file.c:42that line in that file
break 42b 42line 42 of the current source file
break parse_rowb parse_rowfirst line of the function body
break Klass::method—a C++ member function
break *0x401136b *0x401136an 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
breakbthe 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.

Pending Breakpoints on Shared Libraries

A symbol in a dlopened plugin or a not-yet-loaded .so does not exist when gdb starts. Answer y and gdb keeps the breakpoint pending, then resolves it when the library loads.

(gdb) b libplug.so:handle_event
Function "handle_event" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (libplug.so:handle_event) pending.
(gdb) set breakpoint pending on     # stop asking, always make it pending
(gdb) set stop-on-solib-events 1    # stop each time a library loads
(gdb) sharedlibrary                 # force a symbol re-read

info breakpoints shows <PENDING> in the address column until it binds. If it never binds, the library was not loaded, or the symbol is local and stripped.

Conditions, Counts, and Commands

Conditions are the difference between a useful breakpoint and hitting continue four hundred times.

CommandEffect
break f if x == 0condition set at creation
condition 3 argc > 2add or change the condition on breakpoint 3
condition 3remove the condition
ignore 3 500skip the next 500 hits of breakpoint 3
enable once 3fire once, then disable itself
enable count 5 3fire 5 times, then disable
enable delete 3fire once, then delete
commands 3 ... endscript 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
CommandEffect
info breakpoints / i blist all, with conditions and hit counts
disable / enableno argument means every breakpoint
delete Nremove by number, ranges like 3-5 work
clear LOCATIONremove whatever is at that location
save breakpoints bp.txtwrite them all out as a source-able script
source bp.txtrestore 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.