Debugger Discipline
advanced11 min readLesson 179 of 204
Breakpoints, watchpoints, stepping, and the hypothesis loop: debugging as experiment design, not printf archaeology.
The hypothesis loop
- Reproduce reliably (same input, same failure).
- Localize with the largest step that keeps the failure: binary search over the pipeline.
- Hypothesize one mechanism ("the index is stale after the erase").
- Experiment: breakpoint/watchpoint that distinguishes hypothesis A from B.
- Confirm, fix, and add the regression test that would have caught it.
printf-debugging is a for-loop over guesses; the debugger is a binary search. Both are tools, but only one scales to heisenbugs.
The moves that matter
- Breakpoints โ stop at a line/function. Conditional breakpoints (
i == 999) skip 998 useless stops. - Watchpoints โ break when a variable changes: the killer feature for "who corrupts this field?" (
watch -l s->count). - Stepping โ
stepinto,nextover,finishout. Stepping through library code is noise; step your code, skip the standard library. - Backtrace + frame inspection โ after a crash,
btlists the stack; frame 0 is where it died, frames up are why.
Post-mortem state of mind
A crash dump is a recording of the past. The skill is reading it: which frame is library-internal (skim), which is yours (study), which values are plausible vs corrupted (distrust pointers that look like small integers).