Debugging Dynamically Loaded C Extensions with gdb
Credit: Joseph VanAndel, Michael Aivazis
Problem
A dynamically loaded C/C++ Python extension is giving you trouble on Unix or a Unix-like platform, and you would like to use the interactive debugger gdb to find out more about what’s wrong.
Solution
One way to find the cause of core dumps or other serious trouble with
a C Python extension is to compile the extension source with
-g and then follow these steps (you may also want
to recompile any other extensions you use, such as Numeric, with
-g):
% gdb /usr/bin/python2.1 (gdb) br _PyImport_LoadDynamicModule (gdb) cont # Repeat until your extension is loaded (gdb) finish # to load your extension (gdb) br wrap_myfunction # the entry point in your code (gdb) disable 1 # don't want to break for more modules being loaded (gdb) continue
Discussion
If a dynamically loaded C/C++ extension is causing Python to core
dump, or causing some other kind of serious trouble, this recipe can
help you find out the root cause by showing a technique to debug your
extension using
gdb
(if you use Unix or a Unix-like platform, and
gdb is your debugger of choice). Note that the
overall concept generalizes to other debuggers with abilities similar
to gdb’s. You cannot set a
break on your function all at once, because your function lives in a
dynamic library (shared object) that isn’t initially
loaded. However, you can break in the
PyImport_LoadDynamicModule function and eventually (when your module is at long last ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access