Skip to content

Instantly share code, notes, and snippets.

@willfurnass
Last active August 25, 2020 17:08
Show Gist options
  • Save willfurnass/6821280e560b980704b7989868b72ecb to your computer and use it in GitHub Desktop.
Save willfurnass/6821280e560b980704b7989868b72ecb to your computer and use it in GitHub Desktop.
Setting up a Python debug environment to investigate a segfaulting Python C extension

Notes on setting up a Python debug environment

Used to investigate a segfault in a Python C extension within a Python package.

  1. Install debug Python build using pyenv then check is available for use:

    pyenv install -g 3.8.5
    pyenv versions
    
  2. Switch to locally using that interpreter within a dir:

    cd path/to/some/repo
    pyenv local 3.8.5-debug
    which python
    
  3. Install certain dependencies for this project using that interpreter:

    python -m pip install cython matplotlib
    
  4. Edit setup.py and ensure the extra_compile_args parameter is set to ['-Wall', '-O', '-g'] for relevant setuptools.Extensions to ensure they can be (re)built with debug support.

  5. (Re)build all Python C extensions for the package:

    python setup.py clean --all
    python setup.py develop
    
  6. Run Python in a debugger:

    gdb -ex r --args /bin/bash python tests/segfaulting_script.py
    

Further resources:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment