Site Tools


gdb

Table of Contents

GDB

GDB (the GNU Debugger) lets you inspect and control a running program: pause execution at any line, examine variables, walk the call stack, and step through code. It works on C, C++, Fortran, and other compiled languages by reading debug symbols embedded during compilation.

Instead of scattering printf statements and recompiling after every guess, GDB lets you attach to a running process or post-mortem core dump and ask questions directly: what's in this variable, how did we get here, what did the caller pass in?

GDB requires debug information to be useful. Compile with -O0 -g for reliable debugging (no optimization, keep debug symbols). Binaries compiled with -O3 and no -g are nearly impossible to debug—the optimizer eliminates the structure GDB needs.

$ gcc -O0 -g -o app app.c         # compile with debug symbols
$ gdb ./app                         # start debugger
(gdb) break main                    # set breakpoint
(gdb) run                           # run until breakpoint
(gdb) next                          # step over next line
(gdb) print variable                # print variable value
(gdb) quit                          # exit GDB

GDB is command-line based. Commands can be abbreviated (e.g., b for break, c for continue, p for print). Press Enter to repeat the last command.

Concepts

  1. Basics
  2. Breakpoints
  3. Running and stepping
  4. Examining state
  5. Inspecting memory
  6. Call stack
  7. Watching expressions
  8. Debugging crashes
  9. Commands
  10. Configuration
  11. TUI (Text User Interface)
  12. Advanced features
gdb.md · Last modified: