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, strace and sanitizer output to locate a defect with tool evidence.
  • Shrink a failure to a minimal reproducer and pin it with a regression test.

Covers

Debugger, logs, traces and minimum distinguishing observationsCompeting hypotheses, evidence and root-cause notes
1

Read

In this order, only the parts listed

0 of 4 done

  1. Debugging and Profiling: the Debugging halfThe Missing Semester of Your CS Education

    Debugging: Printf Debugging and Logging; Debuggers with the gdb command list; Record-Replay Debugging with rr reverse commands; System Call Tracing with strace flags; Memory Debugging with sanitizer compile flags

    Skim Profiling, Timing through Benchmarking. That half belongs to a later section; stop at the Exercises under Debugging.

  2. 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

  3. Debugging Python programsThe Art of Debugging

    Choosing the right approach; Running the code you think you are running; Interactive debugging including pdb attach and embed; Diagnosing hangs with py-spy

  4. Debugger and logging recipesStas' Python Cookbook - debugging and testing

    Debugging: The interactive debugger command list; Printing, Logging and Output Control: the logging block, prefer it over print for real programs

2

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: breakpoint for state, logging for history, strace -f -e trace=file for missing files, strace -f -p PID for a hang, AddressSanitizer for memory corruption, rr reverse-continue for Heisenbugs.
  • On Python 3.14, python -m pdb -p PID attaches to a running process. Under Ubuntu's default ptrace_scope of 1 it fails on a process that is not its descendant (Failed to find the PyRuntime section) unless run with sudo; py-spy dump needs 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, strace and rr's single-core recording all perturb a race; keep instrumentation cheap and in memory, and use rr record --chaos to provoke the failing schedule.
  • Declaring victory without a regression test. The next refactor reintroduces the same defect silently.
3

Try it yourself

Closed-book, before checking anything

Seed an off-by-one in a function you tested in the first unit. Write three competing hypotheses with their killing observations in a log file. Confirm the import path, set one 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.
4

Quiz yourself

Answer first, then mark it. Missed ones go to Review.

0 got · 0 missed

  1. 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. 2.A program hangs on startup with no output. Which tool first, and with what flags?Answer

    strace -f -p PID, or rerun it under strace -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=file to see missing files (ENOENT) on the way.

  3. 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. 4.rr records a passing run of a race bug. What now?Answer

    Record again until a failing run is captured: rr serializes scheduling, so intermittent races may need several attempts. Then replay deterministically with reverse debugging.

5

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.c AddressSanitizer report and the strace file listing

    Compile with gcc -fsanitize=address -g as 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: Mock versus MagicMock, patch targets, autospec and the over-mocking trap. The pytest docs remain the reference for fixtures and monkeypatch.