There are somewhat undocumented python extensions for gdb.
From the Python source grab Tools/gdb/libpython.py
(it is not included in a normal install).
Put this in sys.path
Then:
# gdb /gps/python2.7_x64/bin/python coredump
...
Core was generated by `/usr/bin/python script.py'.
Program terminated with signal 11, Segmentation fault.
#0 call_function (oparg=<optimized out>, pp_stack=0x7f9084d15dc0) at Python/ceval.c:4037
...
(gdb) python
>import libpython
>
>end
(gdb) bt
#0 call_function (oparg=<optimized out>, pp_stack=0x7f9084d15dc0) at Python/ceval.c:4037
#1 PyEval_EvalFrameEx (f=f@entry=
Frame 0x7f9084d20ad0,
for file /usr/lib/python2.7/site-packages/librabbitmq/__init__.py, line 220,
in drain_events (self=<Connection(channels={1: <Channel(channel_id=1, connection=<...>, is_open=True, connect_timeout=4, _default_channel=<....(truncated), throwflag=throwflag@entry=0) at Python/ceval.c:2681
...
(gdb) py-list
218 else:
219 timeout = float(timeout)
>220 self._basic_recv(timeout)
221
222 def channel(self, channel_id=None):
As you can see we now have visibility into the Python stack corresponding with the CPython call chain.
Some caveats:
- Your version of gdb needs to be greater than 7 and it needs to have been compiled with
--with-python
gdb
embeds python (by linking to libpython
), it doesn't run it in a subshell. This means that It may not necessarily match the version of python that is on $PATH
.
- You need to download
libpython.py
from whatever version of the Python source that matches whatever gdb
is linked to.
- You may have to run gdb as root - if so you may need to set up
sys.path
to match that of the code that you are debugging.
If you cannot copy libpython.py
into sys.path
then you can add it's location to sys.path
like this:
(gdb) python
>import sys
>sys.path.append('/path/to/containing/dir/')
>import libpython
>
>end
This is somewhat poorly documented in the python dev docs, the fedora wiki and the python wiki
If you have an older gdb
or just can't get this working there is also a gdbinit in the Python source that you can copy to ~/.gdbinit
which add some similar functionality