Data files · debugging
Breakpoints in Scripts: Pausing Execution to Inspect State
A breakpoint tells the interpreter to stop before a chosen line runs and hand control to the person debugging. From that pause the variables can be printed, changed, and the program advanced one line at a time. For numerical code this beats scattering print statements, because the whole state is available and the next question can be decided after seeing the last answer. The notes below describe the kinds of breakpoint, the stepping commands, and the habits that make a session short. They continue in setting up a debugger session and belong to the Data and files collection.
Two ways to set one
A debugger breakpoint is a marker attached to a file and a line number, kept outside the source. The program text stays untouched, and the marker disappears when the session ends. A break statement placed in the code itself does the same job from inside: in Python, a call to breakpoint() opens the standard debugger at that point, and Octave offers a keyboard command that drops to a prompt. The in-code version is quick for a one-off look, but it is easy to forget and ship, so search for stray ones before committing.
A conditional breakpoint stops only when an expression is true. For a loop of 10,000 iterations that goes wrong somewhere past iteration 6,000, the condition i == 6200 or err > 1e-3 saves clicking through thousands of stops. If the debugger cannot attach conditions, write the condition as an ordinary if statement around the break call.
for i in range(10000):
r = residual(x, i)
if r > 1e-3:
breakpoint() # stops only when the residual blows up
x = update(x, r)
Moving from the stop
At the pause, four actions cover nearly everything:
- Step over runs the current line without entering any function it calls.
- Step into enters the function on the current line.
- Step out runs until the current function returns.
- Continue resumes until the next breakpoint or the end.
With the standard Python debugger these are the commands n, s, r and c, and p prints an expression. The mental model matters more than the keys. Step over when the line is trusted, step into when it is suspected, and step out after finding that the current function is fine.
What to look at
On the first stop, look at shapes and types before values. A vector that should have 100 entries and has 99, or a matrix that was transposed, explains many failures by itself. Next look at the extremes: the smallest and largest entries, and whether anything is NaN or infinite. A single NaN in a matrix spreads through every product and sum that touches it, so the earliest place it appears is the place to investigate.
Then test an assumption directly. If a step is supposed to leave a matrix symmetric, evaluate the largest absolute difference between the matrix and its transpose right there. Debuggers let you run any expression, so use that to check invariants instead of eyeballing numbers.
Pitfalls
Breakpoints change timing. A bug that depends on thread order or on a race may vanish while paused. Floating-point behavior can also differ if the debugger evaluates an expression in a different precision than the running program. When a failure disappears under the debugger, fall back on logging.
Errors that raise exceptions need less work: set the debugger to stop at the point of the raise, or use a post-mortem command that opens the debugger on the failed frame afterwards. Handling the same errors in production code is the subject of trapping errors in scripts. Terms used here are explained in the glossary.