6

I sometime use gdb to debug python scripts (CPython of course). It is useful typically to debug core dumps (and when it involves c extension modules).

A basic question is how to set breakpoint on a python function in gdb. Say I've a python script with function foo and I want to break right after foo is called. I guess setting a conditional breakpoint on PyEval_EvalFrameEx might work, but not sure how to do it.

amit
  • 10,612
  • 11
  • 61
  • 60

1 Answers1

1

Using the technique you suggested, this works (though it's not pretty):

break PyEval_EvalFrameEx if (strcmp((((PyStringObject *)(f->f_code->co_name))->ob_sval), "foo") == 0)

Here, f is a PyFrameObject. You may also want to check f->f_code->co_filename to make sure you've got the right file. Note that this does slow down the program quite a bit, since you're breaking and comparing a lot.

GDB 7 has some nice helper macros for dealing with CPython, but I'm not familiar with them. There's probably a nicer way to accomplish what you're looking for with those.