Welcome, guest | Sign In | My Account | Store | Cart

If a dynamically loaded C/C++ extension is causing Python to core dump, here's a technique to debug your extension using gdb.

Python, 13 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
The best way of finding core dumps with a Python extension is to compile
the extension source with '-g', and then follow these steps.  You may want to
re-compile any other extensions you use (like 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

1 comment

Michael Aivazis 20 years, 3 months ago  # | flag

Better but more intrusive trick. I generally find the process of stepping through all the modules that Python reads at startup rather tedius. So I modify the file Modules/main.c before I build my interpreter to include a new function

void Py_DebugTrap(void);

I now can add a call to this function in whatever extension I want to debug. This symbol is available when you type "gdb python" so you can set a breakpoint there immediately. I then set args to my script and continue. It even works in parallel under MPI!