如何使用GDB获取python堆栈跟踪信息?


11

我正在使用GDB在Kubuntu 12.04上的python应用程序中调试分段错误。据说GDB版本7具有用于提取有关python堆栈的信息的内置宏(http://docs.python.org/devguide/gdb.html),但是我很难使其正常工作。我已经安装了python-dbg。

当我在GDB中请求python堆栈跟踪时,结果如下所示:

(gdb) py-bt
#5 (unable to read python frame information)
#16 (unable to read python frame information)
#26 (unable to read python frame information)
...

我的GDB版本是7.4-2012.04-0ubuntu2,Python是2.7.3-0ubuntu3。

Answers:


16

问题出在这里:要访问GDB中的调试符号,您必须调用另一个二进制文件:“ python-dbg”而不是“ python”(可在/usr/share/doc/python2.7-dbg/README.debug中找到它) )。


1
令人惊讶的是,在fedoraproject.org/wiki/Features/EasierPythonDebugging或我能找到的其他任何地方都没有提及。谢谢卢克。
quimnuss 2014年

这不是真的 您只需要获取与正在使用的python匹配的调试符号即可。如果处理virtualenv可能会有些麻烦,因为该python可能与您系统的python不匹配。podoliaka.org/2016/04/10/debugging-cpython-gdb上
aggieNick02 '19

6

在Ubuntu 16.04上,我通过以下方式设法在Python 3.5中获得了Python堆栈跟踪:

  1. 安装python3-dbgpython3-dev

    $ sudo apt install python3-dbg python3-dev

    python3-dbg软件包随附了简短的文档,以及如何使用它/usr/share/doc/python3-dbg/README.debug,我将在下一步中使用它。

  2. 将解压缩的GDB帮助程序脚本附加/usr/share/doc/python3.5/gdbinit.gz~/.gdbinit

    zcat /usr/share/doc/python3.5/gdbinit.gz >> ~/.gdbinit

现在,gdb将能够找到Python二进制文件的符号,并py-bt可以在gdb中显示Python堆栈跟踪:

$ gdb -p 4762
GNU gdb (Ubuntu 7.11.1-0ubuntu1~16.04) 7.11.1
Copyright (C) 2016 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".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
Attaching to process 4762
[New LWP 4852]
[New LWP 4853]
[New LWP 4854]
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
0x00007f38e43deb5d in poll () at ../sysdeps/unix/syscall-template.S:84
84      ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) py-bt
Traceback (most recent call first):
  File "/usr/bin/indicator-cpufreq", line 80, in <module>
    Gtk.main()
(gdb)

我在Ubuntu 16.04上的gdbinit.gz包含许多类似的命令,pystack但没有py-bt。知道发生了什么吗?
安东

为什么python 3.5即使我已经安装了python 3.6
skytree

0

也许这对某人有帮助:二进制文件是python2.7-dbg在我的Debian系统上命名的,来自python2.7-dbg软件包。我还安装了python2.7-dev软件包和apt-get source python2.7-dbg,以便gdb可以找到Python解释器的源文件。

完成所有这些操作后,我设法调试了SIGSEGV正在运行的程序:https : //bugs.python.org/issue34870

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.