5.5 Using a debugger
Contents
5.5 Using a debugger#
Estimated time for this notebook: 10 minutes
Stepping through the code#
Debuggers are programs that can be used to test other programs. They allow programmers to suspend execution of the target program and inspect variables at that point.
Mac - compiled languages: Xcode
Windows - compiled languages: Visual Studio
Linux: DDD
all platforms: eclipse, gdb (DDD and eclipse are GUIs for gdb)
python: spyder,
[pdb](https://docs.python.org/3.8/library/pdb.html)
NB. If you are using the Windows command prompt, you will have to replace all %%bash
directives in this notebook with %%cmd
Using the python debugger#
Unfortunately this doesn’t work nicely in the notebook. But from the command line, you can run a python program with:
python -m pdb my_program.py
Breakpoints#
Break points tell debugger where and when to stop We say
b somefunctionname
%%writefile energy_example.py
from diffusion.model import energy
print(energy([5, 6, 7, 8, 0, 1]))
Overwriting energy_example.py
The debugger is, of course, most used interactively, but here I’m showing a prewritten debugger script:
%%writefile commands
restart # restart session
n
b energy # program will stop when entering energy
c # continue program until break point is reached
print(density) # We are now "inside" the energy function and can print any variable.
Overwriting commands
%%bash
python -m pdb energy_example.py < commands
> /home/runner/work/rse-course/rse-course/module05_testing_your_code/energy_example.py(1)<module>()
-> from diffusion.model import energy
(Pdb) Restarting /home/runner/work/rse-course/rse-course/module05_testing_your_code/energy_example.py with arguments:
# restart session
> /home/runner/work/rse-course/rse-course/module05_testing_your_code/energy_example.py(1)<module>()
-> from diffusion.model import energy
(Pdb) > /home/runner/work/rse-course/rse-course/module05_testing_your_code/energy_example.py(3)<module>()
-> print(energy([5, 6, 7, 8, 0, 1]))
(Pdb) Breakpoint 1 at /home/runner/work/rse-course/rse-course/module05_testing_your_code/diffusion/model.py:5
(Pdb) > /home/runner/work/rse-course/rse-course/module05_testing_your_code/diffusion/model.py(13)energy()
-> density = array(density)
(Pdb) [5, 6, 7, 8, 0, 1]
(Pdb)
Alternatively, break-points can be set on files: b file.py:20
will stop on line 20 of file.py
.
Post-mortem#
Debugging when something goes wrong:
Have a crash somewhere in the code
run
python -m pdb file.py
or run the cell with%pdb on
The program should stop where the exception was raised
use
w
andl
for position in code and in call stackuse
up
anddown
to navigate up and down the call stackinspect variables along the way to understand failure
Note Running interactively like in the following example does work in the notebook. Try it out!
%pdb on
from diffusion.model import energy
partial_derivative(energy, [5, 6, 7, 8, 0, 1], 5)