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

gdb.md · Last modified: by 127.0.0.1