Using PyCortexMDebug with Zephyr RTOS

In this blog I will shortly document how to setup PyCortexMDebug with the Zephyr toolchain (specifically arm-zephyr-eabi-gdb).

OS: Ubuntu 22.04

For a more general introduction, see this blog (which I will borrow from heavily).

Using SVD files during debugging session of a Cortex-M microcontroller allows easy access to system registers value. The SVD file is an XML file that contains the necessary information about the register’s name and address. Using SVD, the IDE can read the value of system registers and display it during the debugging session. I will show here how to use SVD during GDB debug session.

Installation

  1. Download PyCortexMDebug
# Original repo:
#git clone git@github.com:bnahill/PyCortexMDebug.git 
# Does not work due to issue with "lxml"
# See: https://flameeyes.blog/2023/09/10/remote-debugging-with-gdb-part-3-swd/
# Use this:
git clone https://github.com/Flameeyes/PyCortexMDebug
# Another fork here: https://github.com/maksimdrachov/PyCortexMDebug
  1. Install the module
cd PyCortexMDebug
python setup.py install
  1. Setup .gdbinit as follows:
# Setup GDB to interpret in Python
pi
import os,subprocess,sys
# Execute a Python using the user's shell and pull out the sys.path (for site-packages)
paths = subprocess.check_output('python -c "import os,sys;print(os.linesep.join(sys.path).strip())"',shell=True).decode("utf-8").split()
# Extend GDB's Python's search path
sys.path.extend(paths)
sys.path.append('/home/legend27/PyCortexMDebug')  # CHANGE THIS!

# load svd tools
from cmdebug.svd_gdb import LoadSVD
from cmdebug.dwt_gdb import DWT
DWT()
LoadSVD()
  1. If we now try to use it, you will most likely get the following error:

  1. Intall libpython3.8.so:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install libpython3.8-dev
  1. If we now try to launch gdb, we will see the following:

  1. Add safe path in your config
cd ~/.config
mkdir gdb
cd gdb
touch gdbinit
nano gdbinit 
# Add the following line to gdbinit:
add-auto-load-safe-path /home/legend27/fluxgrip_software/.gdbinit
  1. Launch gdb:
arm-zephyr-eabi-gdb-py ./build/zephyr/zephyr.elf -tui -ex "tar ext:3333" -ex "load"
(gdb) source /home/legend27/PyCortexMDebug/scripts/gdb.py
(gdb) svd
(gdb) svd TCCO

1 Like