使用GDB进行Python内存调试 [英] Python memory debugging with GDB

查看:288
本文介绍了使用GDB进行Python内存调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个使用OpenSSL的Python绑定的Linux应用程序,我怀疑它是造成随机崩溃。偶尔,我们看到它崩溃了消息Python致命错误:GC对象已被跟踪,这似乎是库的编程错误或内存损坏的症状。有没有办法知道执行的Python源代码的最后一行,给定一个核心文件?或者如果它附加在GDB中?我意识到这可能是所有编译的字节码,但我希望有人可能会处理这个。目前它正在跟踪模块运行,我们希望它会再次发生,但可能会很长一段时间。

解决方案

p>是的,你可以做这样的事情:

 (gdb)print PyRun_SimpleString(import traceback; traceback.print_stack ))
文件< string>,第1行在< module>
文件/var/tmp/foo.py,第2行在< module>
i ** 2
文件< string>,第1行,< module>
$ 1 = 0

还可以使用 python中定义的pystack 命令 gdbinit 文件,但它不适用于我。如果您想查看此处,请参阅



此外,如果您怀疑内存问题,值得一提的是,您可以使用 valgrind 与python,如果你准备重新编译它。该过程描述为此处


We have a Linux application that makes use of OpenSSL's Python bindings and I suspect it is causing random crashes. Occasionally, we see it crash with the message "Python Fatal Error: GC Object already tracked," which would appear to be either a programming error on the part of the library, or a symptom of memory corruption. Is there any way to know the last line of Python source code it executed, given a core file? Or if it is attached in GDB? I realize it is probably all compiled bytecode, but I'm hoping there someone out there may have dealt with this. Currently it is running with the trace module active and we're hoping it will happen again, but it could be a long while.

解决方案

Yes, you can do this kind of thing:

(gdb) print PyRun_SimpleString("import traceback; traceback.print_stack()")
  File "<string>", line 1, in <module>
  File "/var/tmp/foo.py", line 2, in <module>
    i**2
  File "<string>", line 1, in <module>
$1 = 0

It should also be possible to use the pystack command defined in the python gdbinit file, but it's not working for me. It's discussed here if you want to look into it.

Also, if you suspect memory issues, it's worth noting that you can use valgrind with python, if you're prepared to recompile it. The procedure is described here.

这篇关于使用GDB进行Python内存调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆