使用gdb 7的支持来调试Python程序需要什么?

What is needed to use gdb 7's support for debugging Python programs?

我想使用gdb 7的支持来调试Python"劣等进程"。

我需要做些什么?

例如:

  • 劣等Python的configure脚本需要与哪些标志一起运行?

  • 劣等的Python进程是否必须是Python 2.7或更高版本(我看到那是提交了gdb对Python源代码树的支持的一部分时)? 还是gdb进程本身仅需要Python 2.7?

  • 需要安装哪些文件,可能不是所有发行版都打包了这些文件? 例如,在packages.ubuntu.com上,我没有找到python-gdb.py的任何点击,我认为这是必需的。

知道特定发行版需要些什么非常方便。 我对Ubuntu和Centos需要什么特别感兴趣。


Python似乎需要使用--with-pydebug进行编译(在Ubuntu 12.04上,软件包python-dbg包含一个合适的Python可执行文件,其本身称为python-dbg)。劣质的Python不必是Python 2.7-2.6成功加载了2.7 gdb扩展(请参见下面的调试会话)。至少在Ubuntu 12.04上,安装的定义gdb扩展名的文件称为libpython.py,而不是python-gdb.py(出于某种原因,构建Python会生成包含这两个文件的构建目录-它们是相同的)。

但是,我认为目前无法使用生产核心文件进行调试:Python劣质进程的gdb扩展似乎试图获取在生产二进制文件中得到优化的变量(例如,< x5>)。似乎Linux / gdb和Python尚未达到Illumos上JavaScript所达到的出色水平,Bryan Cantrill报告在这里能够以这种方式调试生产核心转储:

http://www.infoq.com/presentations/Debugging-Production-Systems

这是Ubuntu 12.04上的调试会话,其中显示了gdb使用Python 2.7的gdb扩展名运行Python 2.6劣等程序来调试段错误。首先,导致段错误的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
~/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()

和调试会话:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
~/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 <http://gnu.org/licenses/gpl.html>
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:
<http://bugs.launchpad.net/gdb-linaro/>...
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 <Address 0xdeadbeef out of bounds>) 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=<Foo(someattr=42, someotherattr={'three': [(), (None,), (None, None)], 'two': 2L, 'one': 1}) at remote 0x7ffff6e03240>, string_at=<function at remote 0x7ffff6e1c990>)
        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=<Foo(someattr=42, someotherattr={'three': [(), (None,), (None, None)], 'two': 2L, 'one': 1}) at remote 0x7ffff6e03240>)
    f.bar()
#20 Frame 0x8eb680, for file /home/john/bin/dumpcore.py, line 16, in <module> ()
    main()
(gdb)

对于Centos 6,您只需要执行以下操作:

1
2
yum install gdb python-debuginfo
debuginfo-install python

然后,您可以通过简单地使用gdb附加运行中的python进程来调试它们:

1
gdb python [process id]

连接后,只需键入:

1
py-bt