7 routes42 sections324 units

Unit 5.4 · about 3 h

30 / 324

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.

  • measure

You can now

  • 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 observations
  • Competing hypotheses, evidence and root-cause notes

Study, in this order

4 steps

  1. Debugging and Profiling: the Debugging half
    The 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

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

  2. Fast debugging methodology
    The 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 programs
    The 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 recipes
    Stas' 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

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.

Reproduce closed-book

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.

Traps

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

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.

Self-test

  1. Two hypotheses: wrong constant versus wrong index. What is one distinguishing observation?

  2. A program hangs on startup with no output. Which tool first, and with what flags?

  3. Why convert prints to log statements after the fix?

  4. rr records a passing run of a race bug. What now?

Completion criterion

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

Mark passed, then open 5.5 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • The Missing Semester of Your CS Education
    MITprimarycourse
    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 Debugging
    Stas Bekmansupportpractice
    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 testing
    Stas Bekmansupportreference
    Use Part III and the packaging sections for debugger, traceback, pytest, logging, profiling and clean-environment recipes.

Also in this section

  • pytest documentation
    pytestrequiredimplementation
    Use Get Started, fixtures, parametrization, monkeypatch, caplog and temporary paths to build behavior and failure-path tests without order dependence.
  • Hypothesis documentation
    Hypothesisrequiredpractice
    Follow the Quick start and property-based testing guides, then generate operation histories and retain the minimized example for one real defect.
  • Pro Git
    Git projectsupportreference
    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 - mocking
    AlgoMastersupportcourse
    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.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels