How can I retrospectively debug python exception

I am looking for a way to debug a python exception "retrospectively." Essentially, if my program raises an exception that is not being processed, I want it to save the state of the program so that I can return later and debug the problem.

I looked through the pdb docs and it seems that you can do this, but only if you can interact with the program at the exception point. This will not work for me, as the program will work in the background (without a control terminal).

My first (doomed!) Approach was to put the try / except block at the highest level of my program, and in the exception block, extract the trace object from the current exception and write it to disk using pickle. I then planned to write a separate program that would scatter the object and use pdb.post_mortem to debug the broken program. But trace objects are not legible, but I would not expect this to work, as this will not save the entire state of the program.

+6
python debugging
source share
6 answers

As far as I know, there is no way to do what you ask. However, it looks like you can look for a remote debugger. There are several options:

  • rconsole is not really a debugger, but it allows you to get an interactive prompt inside another process. This may be useful for debugging purposes. I have not tried this, but it looks pretty simple.
  • rpdb2 built-in debugger - This allows you to run the debugger and then connect to it from another shell.
+1
source share

What you can do is use twisted.python and write the trace to a file, it gives an exact trace, including an exception

0
source share

Well, as far as I know, there is no simple solution to this problem. You might want to try this approach: How do I find out what memory uses in a Python process on a production system?

0
source share

You can create a completely separate runtime at the top level:

myEnv = {} myEnv.update(globals) 

Then execute your code in this runtime. If an exception occurs, you have a trace (stack) and all global variables, so you can restore the program state pretty well.

0
source share

Currently, the exception is caught before the stack is unwound, the state is available for checking with the check module: http://docs.python.org/2/library/inspect.html

In general, you should use inspect.getinnerframes for the traceback object. Local variables in each frame of the stack are available as .f_locals so you can see what they are.

The hard part serializes all of them correctly: depending on what types you have in the local area, you may or may not be able to sort them, dump them in JSON, or whatever.

0
source share

I hope this helps (it helped me):

 import logging, traceback _logger = logging.getLogger(__name__) try: something_bad() except Exception as error: _logger.exception("Oh no!") # Logs original traceback store_exception_somewhere(error) 

In addition, there are several new options in Python 3 , such as raise new_exc from original_exc or raise OtherException(...).with_traceback(tb) .

0
source share

All Articles