Used to investigate a segfault in a Python C extension within a Python package.
-
Install debug Python build using pyenv then check is available for use:
pyenv install -g 3.8.5 pyenv versions -
Switch to locally using that interpreter within a dir:
cd path/to/some/repo pyenv local 3.8.5-debug which python -
Install certain dependencies for this project using that interpreter:
python -m pip install cython matplotlib -
Edit
setup.pyand ensure theextra_compile_argsparameter is set to['-Wall', '-O', '-g']for relevantsetuptools.Extensions to ensure they can be (re)built with debug support. -
(Re)build all Python C extensions for the package:
python setup.py clean --all python setup.py develop -
Run Python in a debugger:
gdb -ex r --args /bin/bash python tests/segfaulting_script.py
Further resources:
- Debugging Python C extensions: https://pythonextensionpatterns.readthedocs.io/en/latest/debugging/debug.html
- Investigating segfaults using gdb: http://www.unknownroad.com/rtfm/gdbtut/gdbsegfault.html