18

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;
}
CrazyCasta
  • 26,917
  • 4
  • 45
  • 72

2 Answers2

20

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.

scottt
  • 7,008
  • 27
  • 37
  • I believe the trouble was that the function was completely optimized out. I know the -g flag was set because other functions here and there and plenty of information, just not py_node_make. – CrazyCasta Mar 09 '13 at 05:41
  • I see. py_node_make() was a small static function and a prime target for inlineing. Setting a breakpoint on it still worked, but the argument name **node** is lost. – scottt Mar 09 '13 at 09:24
4

The problem is the optimization. I'm not sure how to do it from the command line, but in the setup.py script I just added extra_compile_args=['-O0'], to the Extension constructor and everything worked.

Would still like (and would accept) an answer that involved a command line arg (something after python setup.py build) that would accomplish the same thing so I don't have to have a compiler specific line in my setup.py file.

CrazyCasta
  • 26,917
  • 4
  • 45
  • 72