27

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.

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

Community
  • 1
  • 1
keturn
  • 4,780
  • 3
  • 29
  • 40
  • Note also that a QUIT signal (Ctrl-\\) dumps core too. – tzot Jun 26 '10 at 18:10
  • Related: https://stackoverflow.com/questions/6132469/why-cant-i-pickle-an-errors-traceback-in-python https://stackoverflow.com/questions/14519100/is-it-possible-to-save-the-python-interpreters-state-to-a-file – Wilem2 Jan 10 '21 at 17:54
  • to print the stack trace of a running Python process, or of a Python core dump, pystack could be used https://github.com/bloomberg/pystack#pystack – jfs Jul 04 '23 at 15:06

5 Answers5

6

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.

As for handling the corefile of a Python process, the Python source has a gdbinit file that 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.

Thomas Wouters
  • 130,178
  • 23
  • 148
  • 122
  • You could add a signal handler to dump the Python state and open that in a debugger using http://github.com/gooli/pydump. – elifiner Dec 26 '12 at 10:08
  • 1
    Since Thomas Wouters' original response, it became possible to extend `gdb` with Python itself, and David Malcolm wrote an awesome extension that provides much more functionality than the gdbinit file. The great thing is that it all comes with many Linux distros now (like Ubuntu 14.04) and doesn't require any additional compilation. I've documented my own explorations with it in [Adventures in Python Core Dumping](https://gist.github.com/toolness/d56c1aab317377d5d17a) for anyone who's interested. – Atul Varma Jan 17 '16 at 00:59
3

If you only care about storing the traceback object (which is all you need to start a debugging session), you can use debuglater (a fork of pydump). It works with recent versions of Python and has a IPython/Jupyter integration.

If you want to store the entire session, look at dill. It has a dump_session, and load_session functions.

Here are two other relevant projects:

If you're looking for a language agnostic solution, you want to create a core dump file. Here's an example with Python.

Eduardo
  • 1,383
  • 8
  • 13
1

It's also possible to write something that would dump all the data from 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).

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)

Community
  • 1
  • 1
HoverHell
  • 4,739
  • 3
  • 21
  • 23
1

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.

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...

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.

Douglas Mayle
  • 21,063
  • 9
  • 42
  • 57
  • Stack traces are readily obtainable, but I'm more interested in the entire set of allocated objects than I am in what happens to be in the stack at the moment. – keturn Sep 26 '08 at 22:16
0

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

Community
  • 1
  • 1
Alex Coventry
  • 68,681
  • 4
  • 36
  • 40