Lesson 3 of 5
gdb - Debugging Workflow (Breakpoints, Watchpoints, Backtraces)
GNU Debugger (`gdb`) is the standard C debugger on Linux. With a debug build (`-g`), you can stop execution at any line, inspect variable values, trace the call stack when a crash occurs, and set watchpoints that trigger when a variable changes. Knowing gdb is essential for debugging segfaults and memory corruption.
gdb Workflow - 20 Essential Commands with Annotated Session
# ── Build with debug symbols (required for gdb) ─────────────
gcc -Wall -std=c99 -g -O0 program.c -o program
# ── Start gdb ────────────────────────────────────────────────
gdb ./program # start
gdb ./program core # post-mortem: analyse core dump
gdb --args ./program arg1 arg2 # with program arguments
# ────────────────────────────────────────────────────────────
# COMPLETE gdb SESSION REFERENCE
# ────────────────────────────────────────────────────────────
(gdb) run # start program execution
(gdb) run arg1 arg2 # start with arguments
(gdb) start # run and stop at main()
# ── Breakpoints ──────────────────────────────────────────────
(gdb) break main # break at start of function
(gdb) break program.c:42 # break at specific line
(gdb) break malloc # break when malloc is called
(gdb) info breakpoints # list all breakpoints
(gdb) delete 2 # delete breakpoint #2
(gdb) disable 1 # temporarily disable bp #1
(gdb) condition 1 x == 5 # break #1 only when x == 5
# ── Execution control ────────────────────────────────────────
(gdb) continue # (c) run until next breakpoint or exit
(gdb) next # (n) step OVER function calls
(gdb) step # (s) step INTO function calls
(gdb) finish # run until current function returns
(gdb) until 60 # run until line 60
# ── Inspecting state ─────────────────────────────────────────
(gdb) print x # print value of x
(gdb) print *ptr # dereference a pointer
(gdb) print arr[0] # array element
(gdb) print student.name # struct member
(gdb) display x # auto-print x after every step
(gdb) info locals # all local variables
(gdb) info args # current function's arguments
# ── Backtrace (call stack) ───────────────────────────────────
(gdb) backtrace # (bt) full call stack
(gdb) frame 2 # switch to frame 2
(gdb) up / down # move between frames
# ── Memory inspection ────────────────────────────────────────
(gdb) x/10xb ptr # examine 10 bytes as hex at ptr
(gdb) x/4wd &arr # examine 4 words as decimal
(gdb) x/s str_ptr # examine as string
# ── Watchpoints ──────────────────────────────────────────────
(gdb) watch x # break when x changes
(gdb) watch *(int*)addr # break when memory at addr changes
(gdb) rwatch x # break when x is read
(gdb) awatch x # break on read or write
# ── Post-mortem crash analysis ───────────────────────────────
# After a segfault:
ulimit -c unlimited # enable core dumps (bash)
./program # crashes, creates 'core'
gdb ./program core
(gdb) backtrace # see where it crashed
(gdb) print ptr # examine the bad pointer
# ── gdb for ASAN crash ───────────────────────────────────────
# Compile: gcc -g -fsanitize=address program.c -o program
# ASAN already prints a detailed stack trace. Read it!
# ASAN output lines starting with "==" are the error.
# Lines with "#0 #1 #2" are the call stack.
# ── Useful .gdbinit settings ─────────────────────────────────
# ~/.gdbinit:
# set print pretty on # pretty-print structs
# set pagination off # don't page output
# set history save on # save command history
# break main # auto-break at main
# run # auto-runCommon Mistakes
- Debugging an optimised binary - `gcc -O2` rearranges code making debugger output misleading (variables 'optimised out', wrong line numbers). Always use `-O0 -g` for debugging.
- Not using `backtrace` immediately after a crash - when gdb catches a segfault, immediately run `backtrace` and `info locals` before any further commands change the state.
- Forgetting `ulimit -c unlimited` - without this, core dumps are not written (their max size is 0 by default). Set it in `.bashrc` for persistent debugging capability.
Tip
Tip
Practice gdb Debugging Workflow Breakpoints Watchpoints Backtraces in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
GDB enables runtime inspection, stepping through instructions, and analyzing crash call stacks
Practice Task
Note
Practice Task - (1) Write a working example of gdb Debugging Workflow Breakpoints Watchpoints Backtraces from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with gdb Debugging Workflow Breakpoints Watchpoints Backtraces is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- GNU Debugger (`gdb`) is the standard C debugger on Linux.
- Debugging an optimised binary - `gcc -O2` rearranges code making debugger output misleading (variables 'optimised out', wrong line numbers). Always use `-O0 -g` for debugging.
- Not using `backtrace` immediately after a crash - when gdb catches a segfault, immediately run `backtrace` and `info locals` before any further commands change the state.
- Forgetting `ulimit -c unlimited` - without this, core dumps are not written (their max size is 0 by default). Set it in `.bashrc` for persistent debugging capability.
Finished this lesson?