GDB: Step-by-Step C++ Debugging Without Fear of Segfaults
GDB lets you pinpoint where your program crashes in just 15–30 seconds. Instead of sprinkling print statements and recompiling, use the debugger to inspect the call stack and variable values in real time. Compile with -g -O0 to include debug information:
g++ -g -O0 -o myprogram myprogram.cpp
Verify debug info is present using file myprogram. Start debugging with gdb ./myprogram, then run run. On a segfault, GDB halts and shows the exact location via backtrace (bt).
A crash in std::string::operator= with a null this pointer appears instantly in the stack trace:
#0 0x... in std::string::operator= (this=0x0, ...)
#1 0x... in UserData::setName (...)
#2 0x... in main ()
Enable readable output: set print pretty on and set print demangle on.
Breakpoints and Stepping Through Code
Set breakpoints to control execution:
break main— at the start of mainbreak program.cpp:42— on a specific linebreak UserData::setName— when entering a methodinfo break— list all breakpoints
For overloaded C++ functions, specify the signature: break print(int) or break print(std::string). Advanced option: rbreak ^.*::print$ to match all print methods using regex.
Navigate through code:
| Command | Shortcut | Action |
|---------|----------|--------|
| next | n | Execute line without stepping into functions |
| step | s | Execute line and step into functions |
| finish | fin | Run until function ends |
| continue | c | Resume until next breakpoint |
In C++, avoid step into STL calls like push_back — use next to stay out of library internals.
Inspecting Variables and Objects
Use these commands to examine data:
print variable_name— view variable valueprint *pointer— dereference pointerprint vec.size()— call a methoddisplay variable_name— auto-display after each stepinfo locals— show local variablesinfo args— display function arguments
GDB correctly renders C++ types:
print myVector
$1 = std::vector of length 5, capacity 8 = {1, 2, 3, 4, 5}
print myString
$2 = "Hello, World!"
If you see <optimized out>, recompile with -O0. Without -g, variable names won’t appear.
TUI Mode for Visual Debugging
Enable tui enable — the screen splits: top shows source code with current line highlighted, bottom displays commands. Use:
layout src— source code viewlayout asm— assembly viewlayout split— source + assemblylayout regs— register windowCtrl + X, 2— two windowsCtrl + X, o— switch between windowsCtrl + X, a— exit TUI mode
TUI simplifies navigation by showing context without relying on line numbers.
Common Issues and Fixes
| Problem | Solution |
|--------|----------|
| <optimized out> | Recompile with -O0 |
| No variable names | Use -g flag |
| Local variables missing | Run info locals inside function |
| Vector shows garbage | Use print vec._M_impl._M_start in older libstdc++ |
Essential Commands for Mid-Level Developers
Master these 10 commands for 90% of debugging tasks:
r— runb— set breakpointbt— backtracen— nexts— stepfin— finishc— continuep— printi locals— localsq— quit
Key Takeaways
- Call stack (
bt) solves 70% of segfaults in seconds, revealing the full path back tomain. - TUI mode visualizes code, reducing cognitive load on complex projects.
-g -O0are mandatory; without them, debugging is useless.nextvsstepis critical in C++ with STL — avoid deep dives into library code.displayandinfo locals/argsgive full visibility into state without cluttering code with prints.
— Editorial Team
No comments yet.