Unit 4 of 8 · about 3 hours · measure
Debuggers, logs, traces and competing hypotheses
Editing before observing creates new bugs while the old one hides. A short hypothesis log plus one distinguishing observation usually beats an hour of guessing.
You'll be able to
- Rank competing root-cause hypotheses and pick the observation that distinguishes them before editing code.
- Drive
pdb,logging,straceand sanitizer output to locate a defect with tool evidence. - Shrink a failure to a minimal reproducer and pin it with a regression test.
Covers
Read
In this order, only the parts listed
0 of 4 done
- Debugging and Profiling: the Debugging halfThe Missing Semester of Your CS Education
Debugging: Printf Debugging and Logging; Debuggers with the
gdbcommand list; Record-Replay Debugging withrrreverse commands; System Call Tracing withstraceflags; Memory Debugging with sanitizer compile flagsSkim Profiling, Timing through Benchmarking. That half belongs to a later section; stop at the Exercises under Debugging.
- Fast debugging methodologyThe Art of Debugging
Core principles: quick iterations with small payload, real versus random versus synthetic data; Atomic debug cycles; Finding the cause: editing the right file, bisecting revisions
- Debugging Python programsThe Art of Debugging
Choosing the right approach; Running the code you think you are running; Interactive debugging including
pdbattach and embed; Diagnosing hangs withpy-spy - Debugger and logging recipesStas' Python Cookbook - debugging and testing
Debugging: The interactive debugger command list; Printing, Logging and Output Control: the
loggingblock, prefer it overprintfor real programs
Focus on
- Write two or three competing hypotheses first, each with the single observation that would kill it. Run the cheapest distinguishing observation before changing code.
- Confirm you run the code you edit: check the module path for installed copies shadowing the repo. Every later observation misleads otherwise.
- Match the tool to the question:
breakpointfor state,loggingfor history,strace -f -e trace=filefor missing files,strace -f -p PIDfor a hang, AddressSanitizer for memory corruption,rr reverse-continuefor Heisenbugs. - On Python 3.14,
python -m pdb -p PIDattaches to a running process. Under Ubuntu's defaultptrace_scopeof 1 it fails on a process that is not its descendant (Failed to find the PyRuntime section) unless run withsudo;py-spy dumpneeds the same permission.
Common mistakes · 3
- Debugging the wrong copy: an installed package shadows your edited repo file. Check the module path before trusting any observation.
- Trusting an observer that changes timing. Prints,
straceandrr's single-core recording all perturb a race; keep instrumentation cheap and in memory, and userr record --chaosto provoke the failing schedule. - Declaring victory without a regression test. The next refactor reintroduces the same defect silently.
Try it yourself
Closed-book, before checking anything
breakpoint, read the distinguishing variable, fix, and add the regression test. End state: the log shows each rejected hypothesis with its evidence, and the suite is green.Quiz yourself
Answer first, then mark it. Missed ones go to Review.
0 got · 0 missed
1.Two hypotheses: wrong constant versus wrong index. What is one distinguishing observation?Answer
Break at the indexing line and read the index and the constant. One read kills one hypothesis.
2.A program hangs on startup with no output. Which tool first, and with what flags?Answer
strace -f -p PID, or rerun it understrace -f. The last system call shows where it blocks, such as a read on a socket or pipe, a connect, or a futex wait. Add-e trace=fileto see missing files (ENOENT) on the way.3.Why convert prints to log statements after the fix?Answer
Logs keep severity, destination and history for the next similar bug. Removed prints lose the evidence path; the lecture recommends this explicitly.
4.rr records a passing run of a race bug. What now?Answer
Record again until a failing run is captured:
rrserializes scheduling, so intermittent races may need several attempts. Then replay deterministically with reverse debugging.
Practice
- Missing Semester merge-sort debugger exercise
Exercises under Debugging, item 1: implement the buggy merge sort, find the bug with a debugger and fix it
Keep a hypothesis log with at least two competing hypotheses before you set the first
breakpoint. - Missing Semester sanitizer and strace exercises
Exercises under Debugging, items 3 and 4: the
uaf.cAddressSanitizer report and thestracefile listingCompile with
gcc -fsanitize=address -gas the lecture shows and quote the faulting line.
You're done when
A seeded defect you have never seen takes under an hour from log entry to green regression test, with tool output quoted for every rejected hypothesis.
Only when you can do this without notes.
Leads to
Notes
Resources · 3 for this unit, 4 more in the section
- The Missing Semester of Your CS EducationMIT · primary
Complete the 2026 lectures on the shell, command-line environment, development tools, debugging and profiling, Git, packaging and shipping code, and code quality. Apply every exercise to the same stage project.
- The Art of DebuggingStas Bekman · support
Use the Unix, Python and compiled-code sections during a real seeded defect. Follow the evidence and tiny-reproducer workflow rather than reading the unfinished book front to back.
- Stas' Python Cookbook - debugging and testingStas Bekman · support
Use Part III and the packaging sections for debugger, traceback, pytest, logging, profiling and clean-environment recipes.
Also in this section
- pytest documentationpytest · required
Use Get Started, fixtures, parametrization, monkeypatch, caplog and temporary paths to build behavior and failure-path tests without order dependence.
- Hypothesis documentationHypothesis · required
Follow the Quick start and property-based testing guides, then generate operation histories and retain the minimized example for one real defect.
- Pro GitGit project · support
Use Chapters 2-3 and the sections on debugging, internals and recovery while constructing a history for bisect and recovering a deliberately displaced commit.
- AlgoMaster Python - mockingAlgoMaster · support
Use the Mocking lesson for
unittest.mock:MockversusMagicMock,patchtargets,autospecand the over-mocking trap. The pytest docs remain the reference for fixtures andmonkeypatch.