Python debugging

This task describes how to debug Python scripts.

About this task

These Arm® 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.
  • Breakpoints and stepping in Python code.
  • 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 Arm® DDT features are not supported in Python debugging:

  • The Multi-Dimensional Array Viewer is not supported in Python frames.
  • Offline Python debugging.
  • Manual launch and attaching to a Python process.
  • Python debugging by opening a core file.
  • Watchpoints and Tracepoints.
  • Python programs with multiple Python threads running concurrently as this causes Arm® 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

  1. 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
    
  2. 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
    
  3. 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.

  4. Run:

    $ make -f python.makefile
    
  5. Run:

    $ ../bin/ddt python3 %allinea_python_debug% python-debugging.py
    

    Note

    On loading into 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.

  6. Click Run.

  7. Click Play/Continue to run to the first line of the script.

  8. Set a breakpoint on a line inside the call_out_to_a_library function.

  9. Use the Add breakpoint dialog to set a breakpoint on the function name library_function.

  10. Click Play/Continue to run to the Python function and observe that local variables are visible.

  11. 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.