GDB Learning
Powerful debugging for C/C++ programs with step-by-step execution, breakpoints and memory inspection.
Core Concepts
- Breakpoints: Pause execution at specific lines or functions to inspect program state.
- Watchpoints: Pause when a variable or memory location changes its value.
- Call Stack & Frames: Navigate frames to see where you are and how you got there (backtrace, frame).
- Inspecting Memory: Use print/x and examine commands to inspect variables and memory regions.
Common Use Cases
- Compile with debug information: Use -g and -O0 flags to enable effective debugging
- Set strategic breakpoints: Place breakpoints at key points to control execution
- Inspect variables and memory: Examine the state of variables and data structures
- Analyze stack traces: Understand execution flow through stack analysis
GDB Cheat Sheet
Essential GDB commands
run— Run the programbreak— Set breakpoint at line Nbreak file:line— Set breakpoint at file:linenext— Step over (don't enter functions)step— Step into (enter functions)continue— Continue until next breakpointprint— Print a variable valuebacktrace— Show call stackquit— Quit GDB