views:

382

answers:

6

Is it possible to find any information about what a Python program running right now is doing without interrupting it?

Also, if it isn't possible, is there anyway to crash a running Python program so that I can at least get a stacktrace (using PyDev on Ubuntu)?

I know I should have used logs or run it in debug mode or inserted a statement to run the debugger...

Related questions

+4  A: 

To "crash" a python program with a stacktrace you can send it SIGINT, that is unless you trap it or catch KeyboardInterrupt (python installs a SIGINT handler by default, that raises KeyboardInterrupt).

As for debugging, doesn't PyDev have built-in debugging support (through pdb)?

shylent
Again, the program is running right now
Casebash
which means you get the process id and do 'kill -INT $pid' . That will kill it, but should give you a stack trace. It does assume you have some way to see the stderr output.
Andrew Dalke
+21  A: 

If you place

import code
code.interact(local=locals())

at any point in your script, python will instantiate a python shell at exactly that point that has access to everything in the state of the script at that point. ^D exits the shell and resumes execution past that point.

You can even modify the state at that point from the shell, call functions, etc.

ʞɔıu
Nice. I didn't know that (+1)
shylent
Interesting. How does this differ from import pdb; pdb.set_trace( )?
Jon Hadley
pdb gives you stepInto stepOut etc, etc and is its own special shell that you have to learn how to use that has step control over execution of the entire program. code.interact() is exactly the normal python shell that we all know and love. pdb is much more powerful and often overkill and difficult to use unless you know it well.
ʞɔıu
Sorry for being unclear. I can't modify the script as it is already running
Casebash
Your options for inspecting script execution (that I know of) are this thing, pdb or trace, but all of these require being able to modify the script. I don't think it's feasible while the script is running without modifying it.
ʞɔıu
A: 

Install signal handler that sets a trace function with sys.settrace() that prints traceback and clears clears trace function. This will allow you to see where your program is at any moment without interrupting it. Note, that signal is handled after each sys.getcheckinterval() python instructions.

Denis Otkidach
Or have the signal handler set/unset the trace, so there's no overhead for the normal case.
Andrew Dalke
Good point, I've updated the answer.
Denis Otkidach
+2  A: 

Personally, I prefer ipdb. It's pdb with added IPython goodness. It seems to be more of an interactive Python interpreter with a few shortcuts for debugging functions.

Walter
+4  A: 

If you have a running Python, which wasn't built with any sort of trace or logging mechanism, and you want to see what it's doing internally, then two options are:

Andrew Dalke
Dtrace is awesome, unfortunately I am on Ubuntu
Casebash
A: 

If you're happy with a crash, inserting "1/0" will create a quick and dirty breakpoint, with a complete backtrace!

EOL