Site Tools


wiki:gdb-basics

Table of Contents

GDB Basics

GDB (GNU Debugger) inspects and controls running programs. Start with the binary as an argument: gdb ./program. Compile with -O0 -g for reliable debugging (no optimization, keep debug symbols).

$ gcc -O0 -g -o app app.c       # compile with debug symbols
$ gdb ./app                      # start GDB
$ gdb --args ./app arg1 arg2    # pass arguments to program
$ gdb -p PID                     # attach to running process

GDB is command-line based. Type commands (abbreviations work: b for break, c for continue). Press Enter to repeat last command.

Key workflow:

  1. Set breakpoints (where execution pauses)
  2. Run the program (run)
  3. Inspect state when breakpoint hits (print variables, show stack)
  4. Step through code (line-by-line or to next breakpoint)
  5. Repeat until you understand the issue

Basic commands:

run                start program
break function     set breakpoint at function
continue (c)       resume execution
next (n)           execute next line (skip into calls)
step (s)           execute next line (step into calls)
print variable (p) print variable value
quit (q)           exit GDB
help command       show help for command

Debug symbols are essential. Without -g, GDB shows only memory addresses and registers, not source lines or variable names. -O0 disables optimization, making debugging predictable.

wiki/gdb-basics.md · Last modified: by 127.0.0.1