Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I retrospectively debug a python exception

I'm looking for a way to debug a python exception "retrospectively". Essentially if my program raises an exception that isn't handled, I want it to save off the program state so I can go back later and debug the problem.

I've taken a look at the pdb docs, and it seems that you can do this, but only if you can interact with the program at the point of the exception. This won't work for me as the program will be run in the background (without a controlling terminal).

My first (doomed!) approach was to put a try/except block at the highest level of my program, and in the except block extract the traceback object from the current exception and write it to disk using pickle. I planned to then write a separate program that would unpickle the object and use pdb.post_mortem to debug the crashed program. But traceback objects aren't pickleable, but I wouldn't expect that to work anyway, as it wouldn't save off the entire program state.

like image 485
yoda_alex Avatar asked Nov 26 '10 21:11

yoda_alex


1 Answers

As far as I know, there isn't any way to do what you're asking. That said, it sounds like you might be looking for a remote debugger. There are a couple of options:

  • rconsole - This isn't really a debugger, but it allows you to get an interactive prompt inside another process. This can be useful for debugging purposes. I haven't tried this, but it looks relatively straightforward.
  • rpdb2's embedded debugger - This lets you start a debugger and then connect to it from another shell.
like image 61
Jason Baker Avatar answered Oct 11 '22 14:10

Jason Baker