Debugging C/C++ based Python Code

When things go south in Python it usually gives us a sensible stack trace that helps us track down the issue. However, when you are invoking Python code that depends on C/C++ library and things go awry deep inside, you are often left with merely annoying “segment fault” or “core dumped” and nothing really useful.

If you are able to get your hands on the C/C++ source code, there is a quick and easy fix to this. Compile your C code with debug flag (e.g. with CMAKE you add -DCMAKE_BUILD_TYPE=Debug which will build your code in debug mode. How do you know that? If you use objdump --source <your_built_binarty> you will find C source code embedded in the sea of assembly code.

Now you can use to gdb --args python <arg1> <arg2> <arg3> to start gdb, type r to kick off your program. When it stops at a crash, type bt to see the wonderful stack trace that you are craving for.

Caveat: there is no silver bullet, stack trace does not guarantee your issue will be found easily. In my case, the crash does not happen at memory access violation. It manifests much later. So be cautious when you are using C!