Skip to content

GDB Reference

Brad Northern edited this page Nov 30, 2026 · 1 revision

GDB Reference & Cheat Sheet

A concise command reference and curated learning directory for GNU GDB (GDB/MI3), mapped to GridLock's graphical debugging workstation and built-in GDB Console.


1. Quick-Start & Compilation Flags

Compile C, C++, and Fortran targets with DWARF debug symbols so source lines, local variables, macros, and call frames resolve cleanly in both standalone GDB and GridLock:

Flag Combination Purpose
-g3 -O0 Full DWARF symbols plus preprocessor macro expansion (#define) with no optimization reordering (recommended for interactive debugging).
-Og -g Debug-friendly optimization level for performance-sensitive HPC kernels that are too slow at -O0.
-fno-omit-frame-pointer Preserves the frame pointer (RBP) for reliable stack unwinding across optimized frames and signal handlers.
-fcheck=bounds,do,mem GFortran runtime array-bounds and loop checks alongside -g -O0 (see Fortran + OpenMP Tutorial).

Starting GDB (CLI vs. GridLock)

gdb ./app                        # Load binary and enter interactive prompt
gdb --args ./app --steps 100     # Pass CLI arguments to the inferior program
gdb -p <pid>                     # Attach to an already-running process
gdb ./app core.12345             # Post-mortem inspection of a core dump

Tip

Using GDB Inside GridLock: GridLock launches GDB in GDB/MI3 mode (--quiet --nx -iex "set debuginfod enabled off" --interpreter=mi3) and routes interactive commands you type in the GDB Console (Alt+4) to the currently focused MPI rank (or broadcast across all ranks). To load your personal ~/.gdbinit and custom Python pretty-printers, enable Load user .gdbinit / Python pretty-printers in Preferences -> Debugger (disabled by default for safety).


2. Core GDB Command Cheat Sheet

Execution & Breakpoints

Command Short Description GridLock Equivalent
run [args] r Start target execution from main Run Target (Alt+R)
continue c Resume execution until next stop Continue (F5)
next n Step over next source line Step Over (F10)
step s Step into function call Step Into (F11)
finish fin Run until current function returns Step Out (Shift+F11)
until [line] u Run until a later line or loop exit Run to Cursor (Ctrl+F10)
break file:line b Set breakpoint at source line or function Click editor gutter (F9)
break func if cond b ... if Conditional breakpoint (stop only when cond is true) Right-click gutter -> Edit Condition...
watch expr Hardware write watchpoint (stop when expr changes) Right-click variable -> Add Data Watchpoint
rwatch expr / awatch expr Stop on read (rwatch) or read/write (awatch) Add Data Watchpoint (Read / Access mode)
info breakpoints i b List all breakpoints and watchpoints with IDs Breakpoint popover / gutter
delete [id] d Delete breakpoint by ID (or all if omitted) Click gutter marker / Clear All
disable [id] / enable [id] Toggle breakpoint without removing it Right-click gutter -> Disable / Enable

Call Stack & Threads

Command Short Description GridLock Equivalent
backtrace [N] bt Print call stack (optionally top N frames) Call Stack dock
bt full Print call stack including local variables for every frame Call Stack + Variables dock
frame N f N Select stack frame N for inspection Click frame row in Call Stack
up / down Move one frame up (caller) or down (callee) Select adjacent row in Call Stack
info threads List all OS / OpenMP threads in current rank Threads selector in Call Stack
thread N Switch active inspection context to thread N Click thread entry
thread apply all bt t a a bt Print backtraces across all threads at once GDB Console

Variables, Memory, & Disassembly

Command Short Description GridLock Equivalent
print expr p expr Evaluate and print a C/C++/Fortran expression Evaluator dock / inline hover
p/x expr / p/t expr Print in hexadecimal (/x) or binary (/t) Programmer's Calculator (Ctrl+Alt+C)
p *ptr@len Print len contiguous elements starting at ptr (C/C++) Array & Field Plotter (Ctrl+Alt+G)
display expr Auto-print expr on every stop WATCH dock (DifferentialGrid)
ptype var Print full static/struct type definition of var Variables dock type column
info locals Print all local variables in the current stack frame Variables dock
x/16gx addr Examine 16 giant-words (8-byte hex) starting at addr Memory dock (MemView)
disassemble /m func disas Disassemble function interleaved with source lines Disassembly view (Space for CFG)
info registers i r Print general-purpose CPU registers Registers dock

Reverse Debugging & Checkpoints

GridLock's Timeline reverse stepping and Copy-on-Write Checkpoints (Ctrl+Alt+K) drive these native GDB primitives under the hood:

Command Short Description
record full Begin recording instruction execution history for reverse stepping
reverse-next rn Step backward one source line (stepping over calls)
reverse-step rs Step backward into the previous function call
reverse-continue rc Run backward until the previous breakpoint or watchpoint hit
checkpoint Fork a lightweight Copy-on-Write snapshot of the current process state
info checkpoints List active saved process checkpoints
restart N Restore execution and memory state back to checkpoint N

MPI & Multi-Rank Tips

  • Manual CLI Multi-Rank Launch: Without a parallel frontend, debugging N MPI ranks manually requires spawning separate terminals (for example mpirun -np 4 xterm -e gdb ./app) or sleeping at startup and running gdb -p <pid> in separate shells.
  • GridLock Multi-Rank Coordination: GridLock automates MPMD per-rank GDB/MI3 orchestration across up to 256 ranks in one workstation, letting you switch between All Ranks and Focused Rank stepping, compare variable values across ranks in WATCH, inspect wait-for deadlocks in MPI Diagnostics, and attach to already-running OpenMPI, MPICH, PMIx, or Slurm jobs via File -> Attach to Process... (Ctrl+Alt+P).

3. Official Documentation & Reference Links


4. Curated YouTube Video Tutorials & Conference Talks

  • CppCon / Greg Law ("Give me 15 minutes & I'll change your view of GDB"): Watch on YouTube - Fast-paced tour of TUI mode, Python scripting, reverse debugging, and power-user shortcuts.
  • Chris Kanich: GDB Debugging Tutorial Series (University of Illinois Chicago): Watch on YouTube - Clear step-by-step C/systems debugging lessons covering frames, memory examination, and segmentation faults.
  • Jacob Sorber: Practical C/C++ Debugging with GDB: Watch on YouTube - Short, focused videos on breakpoints, watchpoints, core dumps, and debugging segfaults.
  • Low Level Learning: GDB & Binary/Reverse Debugging Walkthroughs: Watch on YouTube - Hands-on demonstrations of assembly-level inspection, registers, stack layouts, and binary analysis.
  • OpenMPI / HPC Multi-Rank GDB Debugging Walkthroughs: Watch on YouTube - Practical walkthroughs for attaching GDB to multi-rank MPI processes and diagnosing parallel hangs.

Clone this wiki locally