python debugging symbols需要安装吗,使用gdb 7支持调试Python程序需要什么?

为了使用gdb 7调试Python程序,需要确保Python被编译时带有--with-pydebug标志,例如在Ubuntu 12.04中,可以使用python-dbg包。Python 2.7并不是必需的,2.6版本也能加载2.7的gdb扩展。关键文件是libpython.py,它定义了gdb扩展。然而,调试生产环境的core文件可能会遇到问题,因为某些变量在优化后的二进制文件中可能被移除。文中通过一个调试示例展示了如何使用gdb调试导致segfault的Python代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I'd like to use gdb 7's support for debugging Python "inferior processes".

What do I need to be able to do that?

For example:

What flags does the inferior Python's configure script need to have been run with?

Does the inferior Python process have to be Python 2.7 or newer (I see that's when the part of the gdb support for this that's in Python source tree was committed)? Or is Python 2.7 only needed by the gdb process itself?

What files need to have been installed that might not be packaged by all distributions? For example, on packages.ubuntu.com I don't get any hits for python-gdb.py, which I believe is needed.

It would be very handy to know what's needed on particular distributions. I am particularly interested in what's needed for Ubuntu and Centos.

解决方案

Python seems to need to have been compiled with --with-pydebug (on Ubuntu 12.04, package python-dbg contains a suitable Python executable, itself called python-dbg). The inferior Python does not need to be Python 2.7 -- 2.6 loads the 2.7 gdb extensions successfully (see the debugging session below). At least on Ubuntu 12.04, the file that gets installed that defines the gdb extensions is called libpython.py, not python-gdb.py (for some reason, building Python yields a build directory containing both those files -- they are identical).

However, I don't think it's currently possible to debug using production core files: it looks like the gdb extensions for Python inferior processes try to get hold of variables that get optimized away in a production binary (for example, the f variable in PyEval_EvalFrameEx). It seems Linux / gdb, and Python has yet to reach the level of awesomeness achieved for JavaScript on Illumos which Bryan Cantrill reports here is able to debug production core dumps in this way:

Here's the debug session on Ubuntu 12.04 showing gdb running a Python 2.6 inferior process to debug a segfault, using Python 2.7's gdb extensions. First the code to cause the segfault:

~/Downloads/Python-2.6.4$ cat ~/bin/dumpcore.py

class Foo:

def bar(self):

from ctypes import string_at

string_at(0xDEADBEEF) # this code will cause Python to segfault

def main():

f = Foo()

f.someattr = 42

f.someotherattr = {'one':1, 'two':2L, 'three':[(), (None,), (None, None)]}

f.bar()

if __name__ == "__main__":

main()

and the debugging session:

~/Downloads/Python-2.6.4$ gdb --args ./python ~/bin/dumpcore.py

GNU gdb (Ubuntu/Linaro 7.4-2012.04-0ubuntu2.1) 7.4-2012.04

Copyright (C) 2012 Free Software Foundation, Inc.

License GPLv3+: GNU GPL version 3 or later

This is free software: you are free to change and redistribute it.

There is NO WARRANTY, to the extent permitted by law. Type "show copying"

and "show warranty" for details.

This GDB was configured as "x86_64-linux-gnu".

For bug reporting instructions, please see:

...

Reading symbols from /home/john/Downloads/Python-2.6.4/python...done.

(gdb) run

Starting program: /home/john/Downloads/Python-2.6.4/python /home/john/bin/dumpcore.py

[Thread debugging using libthread_db enabled]

Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".

Program received signal SIGSEGV, Segmentation fault.

0x0000000000468d67 in PyString_FromString (str=0xdeadbeef

) at Objects/stringobject.c:116

116 size = strlen(str);

(gdb) py-bt

Undefined command: "py-bt". Try "help".

(gdb) python

>import sys

>sys.path.insert(0, "/home/john/Downloads/Python-2.7/Tools/gdb")

>import libpython

>(gdb) py-bt

#10 Frame 0x8f0f90, for file /home/john/Downloads/Python-2.6.4/Lib/ctypes/__init__.py, line 496, in string_at (ptr=3735928559, size=-1)

return _string_at(ptr, size)

#14 Frame 0x8ebf90, for file /home/john/bin/dumpcore.py, line 5, in bar (self=, string_at=)

string_at(0xDEADBEEF) # this code will cause Python to segfault

#17 Frame 0x8ebd80, for file /home/john/bin/dumpcore.py, line 12, in main (f=)

f.bar()

#20 Frame 0x8eb680, for file /home/john/bin/dumpcore.py, line 16, in ()

main()

(gdb)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值