GDB Debugger Commands Reference

Core GDB commands — break, run, step, backtrace, watch — with syntax and TUI notes.

Searchable GDB command reference covering breakpoints, stepping, inspection, watchpoints and TUI mode. Look up syntax, shorthand and common usage for the GNU debugger fast. It runs free in your browser on Gera Tools, with nothing uploaded.

Last updated Source: Gera Tools

What is the difference between step and next in GDB?

step (s) executes one source line and descends into any function call it encounters. next (n) executes one source line but treats function calls as a single step, running them to completion without stopping inside.

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.