GDB command quick reference
GDB, the GNU Debugger, lets you inspect and control a running program: pause at breakpoints, step line by line, examine memory and variables, and modify state on the fly. This reference groups the most-used commands by task so you can find the right syntax without paging through the manual.
Most commands have a short alias — b for break, r for run, c for continue, bt for backtrace — shown alongside each entry. Filter the table below by command, alias, or description.
How it works
GDB attaches to a process (by launching it, loading a core file, or attaching to a PID) and uses the host’s debugging facilities (ptrace on Linux) to control execution. Breakpoints work by patching the target instruction with a trap; when hit, control returns to GDB. Watchpoints use hardware debug registers when available, falling back to single-stepping otherwise. The TUI is a curses-based front end layered over the same command engine.
Stepping commands operate at the source-line granularity using the line-number information in the program’s debug symbols (compile with -g). Without symbols you fall back to instruction-level stepping (stepi, nexti) and raw addresses.
Essential command groups
Starting and attaching
gdb ./myprog # Launch and load binary
gdb ./myprog core # Load a core dump
gdb -p 1234 # Attach to running process by PID
gdb --args ./myprog arg1 arg2 # Specify program arguments
Breakpoints
break main # (b) Break at function entry
break file.c:42 # Break at specific line
break *0xdeadbeef # Break at address
break func if x > 0 # Conditional breakpoint
info breakpoints # (i b) List all breakpoints
delete 2 # Delete breakpoint number 2
disable 3 # Disable without deleting
Execution control
run # (r) Start the program
continue # (c) Resume until next breakpoint
next # (n) Step over — does not enter calls
step # (s) Step into — enters function calls
finish # Run until current function returns
until 55 # Run until line 55
Inspection
print x # (p) Print value of expression
print *ptr # Dereference pointer
x/4xw 0xffff1234 # Examine 4 words at address in hex
info locals # Show all local variables
info args # Show function arguments
backtrace # (bt) Print call stack
bt full # Full backtrace with local vars
frame 2 # Switch to stack frame 2
Watchpoints
watch x # Break when x is written
rwatch y # Break when y is read
awatch z # Break on read or write
Typical debug session
gdb ./myprog
(gdb) break main
(gdb) run arg1 arg2
(gdb) next
(gdb) print someVar
(gdb) backtrace
(gdb) watch counter
(gdb) continue
To debug a crash from a core dump: gdb ./myprog core. Then bt full prints a full backtrace with local variables. Use frame N to switch stack frames and info locals to inspect that frame’s variables.
Useful configuration
Save repetitive setup in a .gdbinit file in the working directory:
set print pretty on
set pagination off
break main
Enable the TUI split-screen view (source + command prompt) with Ctrl-X A or start with gdb -tui ./myprog. The TUI is useful when single-stepping through source code to see context without constantly running list.