Module 4 · Debugging
On this page 14 headings
Learning goals: Find and explain faults with input manipulation, logging, tracing, profilers and Python pdb.
Error, Failure and Fault
Error = discrepancy between actual behaviour and intended behaviour. Failure = an observable error (incorrect output value, exception etc.). Fault = where the failure occurred (lines in code). Debugging = determining the cause of the failure and rectifying the code.
Remember: Observed wrong output points you toward the responsible source-code defect.
Five Steps of Debugging
Debugging steps: reproduce the problem, find the cause of the defect, investigate a fix, implement the fix, test the fix.
Remember: Reproduce → Find Cause → Investigate Fix → Implement Fix → Test Fix.
Logging: Leave Breadcrumbs
Logging records helpful values or execution events while a program runs. Simple print statements can show which candidate number is being checked and what the prime flag contains. These messages provide a trail of breadcrumbs: they help identify where execution took an unexpected path.
Dump and Diff: Two Runs
Dump & diff: use a diff tool to compare logging data between a correct and an incorrect execution.
Remember: Compare two runs to locate the point where behavior begins to diverge.
Debugging with a Profiler
Profiling tools report how often and how long various parts of the program are executed.
Input Manipulation: First n Primes
Input manipulation means deliberately trying different inputs and comparing results. For the first-n-primes problem, asking for two primes may work, while three or four produces too few values. The pattern suggests a counting error rather than an entirely broken primality test.
Remember: Try nearby and boundary inputs to understand when a failure appears.
Find the Misplaced Counter Increment
The prime-counting variable i should count how many prime numbers were actually added to the result. In the faulty code it increments even when the candidate is not prime, so the loop reaches n too soon. Moving i += 1 inside the if prime block fixes the meaning of the counter.
Remember: Increment the count only when a prime is appended.
Backward Debugging: Why Infinite Loop?
Backward strategy: find the statement with the incorrect output, then follow data and control dependencies backward. Loop not terminating → i not incremented → prime flag stays False because it is not reset to True at the start of each while iteration.
Remember: Trace the failure backward through variable and control dependencies.
Forward vs Backward Debugging
Forward strategy: find the event that triggered the incorrect behaviour, then follow control flow forward until the incorrect state is reached. Backward strategy: find the statement with the incorrect output and follow data and control dependencies backward.
Remember: Forward follows execution; backward follows dependencies from the symptom.
Black-Box Debugging an API
Black-box debugging uses documentation and code examples to understand correct use of an API.
Remember: Work with the behavior you can observe and the contract the API promises.
Python PDB: Pause the Program
The lecture states: install with pip install pdb. Run the file under the pdb module, or add import pdb; pdb.set_trace() in the code.
Remember: A breakpoint pauses execution so you can investigate.
PDB Step and Next
Two useful pdb commands are n (next) and s (step). Next executes the current line and stops at the next suitable line in the same function, normally stepping over calls. Step can enter a function call so you can inspect its internal statements.
Remember: Use next to step over; use step to enter a called function.
PDB: Inspect, Break, Continue
PDB options: p expression prints an expression; b(reak), e.g. b first_n_primes.py:17, sets a breakpoint; c(ont(inue)) runs until the breakpoint.
Remember: Inspect variables, stop at important lines and continue when ready.
Quick revision and self-check
Question: Why did the first prime counter stop too early?
Answer: It incremented for every candidate instead of only for prime candidates.
Question: Why could the second prime program loop forever?
Answer: The prime flag remained False, preventing the count from increasing.
Question: What is the difference between pdb n and s?
Answer: n steps over a function call; s steps into it.
