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

标签 python debugging ubuntu gdb centos

我想使用 gdb 7 的支持来调试 Python“劣质进程”。

我需要什么才能做到这一点?

例如:

  • 低级 Python 的配置脚本需要用什么标志运行?

  • 劣质 Python 进程是否必须是 Python 2.7 或更新版本(我看到那是在 Python 源代码树中提交 gdb 支持的部分时)?还是仅 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 扩展(参见下面的调试 session )。至少在 Ubuntu 12.04 上,安装的定义 gdb 扩展的文件称为 libpython.py,而不是 python-gdb.py(出于某种原因,构建 Python 会产生包含这两个文件的构建目录——它们是相同的)。

但是,我认为目前无法使用生产核心文件进行调试:看起来 Python 劣质进程的 gdb 扩展试图获取在生产二进制文件中优化掉的变量(例如, f PyEval_EvalFrameEx 中的变量)。 Linux/gdb 和 Python 似乎还没有达到 Illumos 上 JavaScript 所达到的令人敬畏的水平,Bryan Cantrill 在此处报告能够以这种方式调试生产核心转储:

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

这是 Ubuntu 12.04 上的调试 session ,显示 gdb 使用 Python 2.7 的 gdb 扩展运行 Python 2.6 低级进程来调试段错误。首先是导致段错误的代码:

~/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()

和调试 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) 

关于python - 使用 gdb 7 对调试 Python 程序的支持需要什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14885328/

相关文章:

javascript - 如何在 WooCommerce 3 中进行调试

perl - Net::FTPSSL 在命令 channel [0] (0, 1) () 上给出错误 Unexpected EOF...

python - 使用 Python 写入 CSV 会添加空行

python - Formset 对象 - 对象没有属性 'fields'

c++ - 跟踪/跟踪在不同寄存器/内存位置传递的值?

javascript 调试 - 有没有办法告诉正在执行的功能?

ubuntu - 使用启用的防火墙无法访问 GitLab (OS : Ubuntu 16. 04)

ubuntu - Heroku登录无法打开浏览器

python - 我怎样才能将我的两个 for 循环重新组合成一个,两者之间只有微小的差异?

python - 如何根据列条件重命名 pandas DataFrame 索引