This guide explains how to clone NumPy, build it with debug symbols, run tests, and debug the C code using gdb.
Run the following bash file to build the debug environment.
./setup.shYou can also use spin to build the environment as mentioned in the official website. I prefer the vanilla approach.
./setup.sh --spinIf the arguments are provided correctly, the script will automatically:
- create the required Python virtual environment,
- install all necessary dependencies,
- configure the development environment,
- and build NumPy with the appropriate debug configuration.
After the script finishes successfully, the environment will be ready for building, running tests, and debugging.
source numpy-debug312/bin/activate
gdb python3.12For example, if you are debugging the legacy_random_binomial_original you might need to look for a shared library _generator.cpython-312-x86_64-linux-gnu.so by continuing.
(gdb) set stop-on-solib-events 1
(gdb) run check_rnd.py
(gdb) continue(gdb) set breakpoint pending on
(gdb) break legacy_random_binomial_original
(gdb) run check_rnd.py
(gdb) continue- NumPy must be compiled with debug flags (
-O0 -g) for effective debugging (look insidesetup.sh) - Breakpoints can be set directly in NumPy C functions once the shared libraries are loaded.
- When probing into the
randommodule keep it in mind that the actual distributions used are in the legacy directory. Fit breakpoints accordingly innumpy/numpy/lib/random/src/legacy/legacy-distributions.c.