Python debugging
This task describes how to debug Python scripts.
About this task
These Linaro DDT features are supported in Python debugging:
Debugs Python scripts running under the CPython interpreter. For supported versions, see Reference table.
Decodes the stack to show Python frames, function names, and line numbers.
Displays both stacks in mixed Python/native programs where the script calls out into a native C library.
Displays Python local variables when a Python frame is selected.
Evaluations which can also include Python expressions and statements.
The Multi-Dimensional Array Viewer. See Multi-Dimensional Array Viewer (MDA) for more information.
Note
Python sequence types (list
, tuple
, range
) are supported by the
Multi-Dimensional Array Viewer. Other built-in iterable types are also
supported when evaluated as list
, for example list(my_set)
.
Breakpoints, Tracepoints, and stepping in Python code.
Stop on raised and unhandled exception. See Default breakpoints for more information.
After a module is imported, you can see its Python source files listed in the Project Files tree.
Debugs MPI programs written in Python using mpi4py.
This feature is useful when debugging a mixed C, C++, Fortran, and Python program.
Note
Python global variables are only shown in the Locals tab if the
selected frame is at the module level. To see a global variable, you can add
it in the Evaluate window. You can see all the global variables,
if you add globals()
.
These Linaro DDT features are not supported in Python debugging:
Offline Python debugging.
Manual launch and attaching to a Python process.
Python debugging by opening a core file.
Watchpoints.
Python programs with multiple Python threads running concurrently as this causes Linaro DDT to hang.
Sub-processing libraries (such as
multiprocessing
) because it forks separate processes.Stepping from Python frames into native frames.
Memory debugging only covers the Python interpreter.
Procedure
To debug Python scripts, specify the Python interpreter followed by
%allinea_python_debug%
and then the path to the script that you wish to debug. For example:$ ddt python3 %allinea_python_debug% my-script.py
To debug Python scripts that use MPI, the same applies, except mpirun is also appended to the beginning:
$ ddt mpirun -np 4 python3 %allinea_python_debug% my-mpi-script.py
When passing arguments, they must appear after
%allinea_python_debug%
and the name of your script. To run the demo in the examples folder, change into the examples folder.Run:
$ make -f python.makefile
Run:
$ ../bin/ddt python3 %allinea_python_debug% python-debugging.py
Note
On loading into Linaro DDT you will be inside the C code. This is normal as you are debugging the python binary. Depending on the interpreter that you are debugging you may also see a message about missing debug symbols. Clicking Play/Continue once after launching will bring you to the first line of your script.
Click Run.
Click Play/Continue to run to the first line of the script.
Set a breakpoint on a line inside the
call_out_to_a_library
function.Use the Add breakpoint dialog to set a breakpoint on the function name library_function.
Click Play/Continue to run to the Python function and observe that local variables are visible.
Click Play/Continue again to run to the native function and observe how the stack appears when calling out of the interpreter.
Note
To disable Python debugging, omit %allinea_python_debug%
from the command line.