Debugging with gdb: pystack macro for gdb to show python stack trace

Discussion in 'Python' started by Timothy Madden, Sep 16, 2009.

  1. Hello

    I have an application with several threads that sometimes just freezes.

    I installed the debug symbols for python interpreter, and the gdb
    macros, as instructed on the wiki
    so I can debug with gdb.

    gdb loads many files with python debugging simbols, and it is loading
    the macros file ~/.gdbinit, but still the macros for working with python
    are not working. The pystack command just freezes the debugger until I
    press ^C, and other macros complain about undefined symbols. I would
    like to see the python stack trace so I know where my application
    freezes. All I could see from the gdb native stack trace (with bt) was
    that my 3 threads were waiting to acquire a lock, but I could not figure
    out where and my program logic should not allow this to happen.

    Can I get the python stack trace working under gdb ?

    Synaptic Package Manager on my Ubuntu netbook shows packages
    python2.6 (2.6.2-0ubuntu1)
    python2.6-dbg (2.6.2-0ubuntu1)
    as installed, and I extracted the Misc/gdbinit file from
    Python-2.6.2.tar.bz2 source archive from I have gdb 6.8-debian.
    So I suppose everything should be right ...

    Thank you,
    Timothy Madden
    Timothy Madden, Sep 16, 2009
    1. Advertisements

  2. Ok I found some other gdb macros on linked from the wiki page and I had
    to change them to get the stack trace work.

    I had to change the symbol PyEval_EvalFrame to PyEval_EvalFrameEx and
    then I could see the stack trace. The resulting gdb macro was:

    define pbt
    set $i = 0
    set $j = 0
    while $i < 1000
    select $i
    if $eip >= &PyEval_EvalFrameEx
    if $eip < &PyEval_EvalCodeEx
    echo c frame #
    p $i
    echo py frame #
    p $j
    set $j = $j+1
    x/s ((PyStringObject*)f->f_code->co_filename)->ob_sval
    x/s ((PyStringObject*)f->f_code->co_name)->ob_sval
    echo line #
    p f->f_lineno
    set $i = $i+1
    document pbt
    show python backtrace

    Thank you,
    Timothy Madden
    Timothy Madden, Sep 17, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.