如何转储整个 Python 进程以供以后调试检查?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/141802/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 19:33:12  来源:igfitidea点击:

How do I dump an entire Python process for later debugging inspection?

pythondebuggingcoredump

提问by keturn

I have a Python application in a strange state. I don't want to do live debugging of the process. Can I dump it to a file and examine its state later? I know I've restored corefiles of C programs in gdb later, but I don't know how to examine a Python application in a useful way from gdb.

我有一个处于奇怪状态的 Python 应用程序。我不想对流程进行实时调试。我可以将其转储到文件中并稍后检查其状态吗?我知道我后来在 gdb 中恢复了 C 程序的核心文件,但我不知道如何从 gdb 以有用的方式检查 Python 应用程序。

(This is a variation on my question about debugging memleaks in a production system.)

(这是我关于在生产系统中调试 memleaks 的问题的一个变体。)

采纳答案by Thomas Wouters

There is no builtin way other than aborting (with os.abort(), causing the coredump if resource limits allow it) -- although you can certainly build your own 'dump' function that dumps relevant information about the data you care about. There are no ready-made tools for it.

除了中止(使用 os.abort(),如果资源限制允许,则导致核心转储)没有其他内置方法——尽管您当然可以构建自己的“转储”功能,转储有关您关心的数据的相关信息。没有现成的工具可以用于它。

As for handling the corefile of a Python process, the Python source has a gdbinit filethat contains useful macros. It's still a lot more painful than somehow getting into the process itself (with pdb or the interactive interpreter) but it makes life a little easier.

至于处理 Python 进程的核心文件,Python 源代码有一个 gdbinit 文件,其中包含有用的宏。它仍然比以某种方式进入过程本身(使用 pdb 或交互式解释器)要痛苦得多,但它使生活更轻松一些。

回答by Douglas Mayle

Someone above said that there is no builtin way to perform this, but that's not entirely true. For an example, you could take a look at the pylons debugging tools. Whene there is an exception, the exception handler saves the stack trace and prints a URL on the console that can be used to retrieve the debugging session over HTTP.

上面有人说没有内置的方法来执行此操作,但这并不完全正确。例如,您可以查看 pylons 调试工具。当出现异常时,异常处理程序会保存堆栈跟踪并在控制台上打印一个 URL,该 URL 可用于通过 HTTP 检索调试会话。

While they're probably keeping these sessions in memory, they're just python objects, so there's nothing to stop you from pickling a stack dump and restoring it later for inspection. It would mean some changes to the app, but it should be possible...

虽然他们可能将这些会话保存在内存中,但它们只是 python 对象,因此没有什么可以阻止您酸洗堆栈转储并在以后恢复它以供检查。这意味着对应用程序进行一些更改,但应该是可能的......

After some research, it turns out the relevant code is actually coming from Paste's EvalException module. You should be able to look there to figure out what you need.

经过一番研究,发现相关代码实际上来自 Paste 的EvalException 模块。你应该能够在那里看看你需要什么。

回答by HoverHell

It's also possible to write something that would dump all the datafrom the process, e.g.

也可以编写一些东西来转储进程中的所有数据,例如

  • Pickler that ignores the objects it can't pickle (replacing them with something else) (e.g. Python: Pickling a dict with some unpicklable items)
  • Method that recursively converts everything into serializable stuff (e.g. this, except it needs a check for infinitely recursing objects and do something with those; also it could try dir()and getattr()to process some of the unknown objects, e.g. extension classes).
  • Pickler 忽略它不能腌制的对象(用其他东西代替它们)(例如Python: Pickling a dict with some unpicklable items
  • 方法递归转换成的一切东西序列(如这个,但它需要一个支票无限递归对象,做这些事;它也可以尝试dir()getattr()处理一些不明物体,例如扩展类)。

But leaving a running process with manhole or pylons or something like that certainly seems more convenient when possible.

但是在可能的情况下,使用检修孔或塔架或类似的东西离开运行过程似乎更方便。

(also, I wonder if something more convenient was written since this question was first asked)

(另外,我想知道自从第一次问这个问题以来是否写了一些更方便的东西)

回答by Alex Coventry

This answersuggests making your program core dump and then continuing execution on another sufficiently similar box.

这个答案建议让你的程序核心转储,然后在另一个足够相似的盒子上继续执行。