Easy debugging of C/C++/CUDA python extensions


Writing an extension called by python (in C, C++ or CUDA)? Not working? Typical.

When doing the same from R it’s pretty easy to debug, just run with R -d <debugger name> e.g. R -d valgrind or R -d gdb you get into the debugger, continue, then run interactively as usual. (For a more complex example using both at once see this blog post).

Doing this from python seems trickier to me. I started off following this guide: But I’m not really an ipython user, and prefer gdb over lldb (due to familiarity). I think this is a good way to do this if you need an interactive python session, but really this is overcomplicated for my typical use case.

Now all I do is run (say I’m debugging CUDA code): cuda-gdb python

Then in the debugger (an example with pp-sketchlib):

set args ~/installs/pp-sketchlib/ --sketch --rfile rfile_test.txt --ref-db Pf6 --cpus 8 --use-gpu

Works just fine, just remember to compile with the debug flags on:

python build_ext --debug install