Back to Home

GDB debugging C++: find segfault in minutes

GDB guide for C++ developers focuses on finding segfault, setting breakpoints, viewing variables and TUI mode. Based on real crash examples and code navigation without recompilations.

GDB in C++: from segfault to fast debugging
Advertisement 728x90

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:

Google AdInline article slot
#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 main
  • break program.cpp:42 — on a specific line
  • break UserData::setName — when entering a method
  • info 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.

Google AdInline article slot

Navigate through code:

| Command | Shortcut | Action |

|---------|----------|--------|

Google AdInline article slot

| 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 value
  • print *pointer — dereference pointer
  • print vec.size() — call a method
  • display variable_name — auto-display after each step
  • info locals — show local variables
  • info 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 view
  • layout asm — assembly view
  • layout split — source + assembly
  • layout regs — register window
  • Ctrl + X, 2 — two windows
  • Ctrl + X, o — switch between windows
  • Ctrl + 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 — run
  • b — set breakpoint
  • bt — backtrace
  • n — next
  • s — step
  • fin — finish
  • c — continue
  • p — print
  • i locals — locals
  • q — quit

Key Takeaways

  • Call stack (bt) solves 70% of segfaults in seconds, revealing the full path back to main.
  • TUI mode visualizes code, reducing cognitive load on complex projects.
  • -g -O0 are mandatory; without them, debugging is useless.
  • next vs step is critical in C++ with STL — avoid deep dives into library code.
  • display and info locals/args give full visibility into state without cluttering code with prints.

— Editorial Team

Advertisement 728x90

Read Next