Python extension debugging

CrazyCasta picture CrazyCasta · Mar 6, 2013 · Viewed 10.2k times · Source

I'm trying to debug an extension module for python that I wrote in C. I compiled it using the following:

python setup.py build -g install --user

I then debug with:

gdb python
...
b py_node_make
run test.py

It breaks at py_node_make (one of the functions I defined), but then I try:

(gdb) print node
No symbol "node" in current context.

The function I'm trying to debug is:

static Python_node_t* py_node_make(
        node_t* node)
{
    Python_node_t* pyNode;

    pyNode = PyObject_New(Python_node_t, &t_node);
    pyNode->node = node;
    pyNode->borrowed = true;

    return pyNode;
}

Answer

scottt picture scottt · Mar 9, 2013

For source debugging to work, your C extensions must be built with debug info (gcc -g). Since you're driving the compilation process with distutils, you can specify the compiler flags used through the CFLAGS environment variable (Installing Python Modules: Tweaking compiler/linker flags):

CFLAGS='-Wall -O0 -g' python setup.py build

Note that even if distutils defaults to a higher optimization level than -O0, you really shouldn't get that No symbol "node" in current context error as long as -g is passed and most Python builds do pass -g by default.