SECTION 04 / 6

Module 4 · Debugging

4 min read
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.

Figure 39. Error, Failure and Fault.
Figure 39. Error, Failure and Fault.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 40. Five Steps of Debugging.
Figure 40. Five Steps of Debugging.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 41. Logging: Leave Breadcrumbs.
Figure 41. Logging: Leave Breadcrumbs.
Open image ↗
Illustration: Module 4 · Debugging

Dump and Diff: Two Runs

Dump & diff: use a diff tool to compare logging data between a correct and an incorrect execution.

Figure 42. Dump and Diff: Two Runs.
Figure 42. Dump and Diff: Two Runs.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 43. Debugging with a Profiler.
Figure 43. Debugging with a Profiler.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 44. Input Manipulation: First n Primes.
Figure 44. Input Manipulation: First n Primes.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 45. Find the Misplaced Counter Increment.
Figure 45. Find the Misplaced Counter Increment.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 46. Backward Debugging: Why Infinite Loop?
Figure 46. Backward Debugging: Why Infinite Loop?
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 47. Forward vs Backward Strategy.
Figure 47. Forward vs Backward Strategy.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 48. Black-Box Debugging an API.
Figure 48. Black-Box Debugging an API.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 49. Python PDB: Pause the Program.
Figure 49. Python PDB: Pause the Program.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 50. PDB Step and Next.
Figure 50. PDB Step and Next.
Open image ↗
Illustration: Module 4 · Debugging

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.

Figure 51. PDB: Inspect, Break, Continue.
Figure 51. PDB: Inspect, Break, Continue.
Open image ↗
Illustration: Module 4 · Debugging

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.

YOUR STUDY PROGRESS

Finished this section? Save your progress.

Back to top ↑