使用 GDB 调试 Python 内存

发布于 2024-07-07 21:19:17 字数 315 浏览 8 评论 0原文

我们有一个使用 OpenSSL 的 Python 绑定的 Linux 应用程序,我怀疑它会导致随机崩溃。 有时,我们会看到它崩溃并显示以下消息:

Python 致命错误:GC 对象已被跟踪

这似乎是库方面的编程错误,或者是内存损坏的症状。 给定一个核心文件,有什么方法可以知道它执行的Python源代码的最后一行吗? 或者如果它附加在GDB中? 我意识到这可能都是编译后的字节码,但我希望有人可能已经处理过这个问题。 目前它正在运行,跟踪模块处于活动状态,我们希望它会再次发生,但这可能需要很长时间。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你不是我要的菜∠ 2024-07-14 21:19:17

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

(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

也应该可以使用 python gdbinit 文件,但它对我不起作用。 如果您想深入了解,请在此处进行讨论。

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

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.

可是我不能没有你 2024-07-14 21:19:17

如果你有 mac 或 sun box,你可以使用 dtrace 和编译的 python 版本dtrace 来找出应用程序当时在做什么。 注意:在 10.5 中,python 是使用 dtrace 进行预编译的,这非常好用且方便。

如果您不可用,那么您可以 import gc 并启用调试,然后您可以将其输出到日志文件中。

要具体回答有关使用 GDB 进行调试的问题,您可能需要阅读“使用 GDB 进行调试” Python 维基百科。

If you have mac or sun box kicking around you could use dtrace and a version of python compiled with dtrace to figure out what the application was doing at the time. Note: in 10.5 python is pre-compiled with dtrace which is really nice and handy.

If that isn't available to you, then you can import gc and enable debugging which you can then put out to a log file.

To specifically answer your question regarding debugging with GDB you might want to read "Debugging With GDB" on the python wiki.

一抹淡然 2024-07-14 21:19:17

如果您使用 CDLL 在 python 中包装 C 库,并且这是 64 位 Linux,那么您的 CDLL 包装器很可能配置错误。 CDLL 在所有平台上默认为 int 返回类型(在 64 位系统上应该是 long long),并且只希望您传入正确的参数。在这种情况下,您可能需要验证 CDLL 包装器...

If you're using CDLL to wrap a C library in python, and this is 64-bit linux, there's a good chance that you're CDLL wrapper is misconfigured. CDLL defaults to int return types on all platforms (should be a long long on 64-bit systems) and just expects you to pass the right arguments in. You may need to verify the CDLL wrapper in this case...

稳稳的幸福 2024-07-14 21:19:17

除了上述所有内容之外,还可以通过 快速实现临时跟踪器跟踪模块

In addition to all above one can quickly implement an adhoc tracer via the trace module.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文