简体   繁体   English

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

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

I'd like to use gdb 7's support for debugging Python "inferior processes". 我想使用gdb 7的支持来调试Python“劣质进程”。

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? 下级Python的配置脚本需要运行哪些标志?

  • 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)? 劣质Python进程是否必须是Python 2.7或更高版本(我看到gdb支持的部分是在Python源代码树中提交的那个)? Or is Python 2.7 only needed by the gdb process itself? 或者只是gdb进程本身需要Python 2.7?

  • 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. 例如,在packages.ubuntu.com上,我没有获得python-gdb.py的任何命中,我相信这是必需的。

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

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 ). 但是,我认为目前不可能使用生产核心文件进行调试:看起来Python下级进程的gdb扩展试图获取在生产二进制文件中优化的变量(例如, PyEval_EvalFrameEx的f变量) )。 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: 它看起来似乎是Linux / gdb,并且Python还没有达到Illumos上JavaScript的强大程度,而Bryan Cantrill在这里报告的是能够以这种方式调试生产核心转储:

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

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. 这是Ubuntu 12.04上的调试会话,显示gdb使用Python 2.7的gdb扩展运行Python 2.6劣质进程来调试segfault。 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 <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) 

For Centos 6, you simply need to do: 对于Centos 6,您只需要:

yum install gdb python-debuginfo
debuginfo-install python

You can then debug running python processes by simply attaching to them with gdb: 然后,您可以通过简单地使用gdb附加到它们来调试运行python进程:

gdb python [process id]

Once connected, just type: 连接后,只需输入:

py-bt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM