Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check what a running process is doing: print stack trace of an uninstrumented Python program

Is there a way on Linux to check what a running Python daemon process is doing? That is, without instrumenting the code and without terminating it? Preferably I'd like to get the name of the module and the line number in it that is currently running.

Conventional debugging tools such as strace, pstack and gdb are not very useful for Python code. Most stack frames just contain functions from the interpreter code like PyEval_EvalFrameEx and and PyEval_EvalCodeEx, it doesn't give you any hint on were in the .py-file the execution is.

like image 708
Björn Lindqvist Avatar asked Jul 27 '11 18:07

Björn Lindqvist


People also ask

How do you get a stack trace in Python?

Print Stack Trace in Python Using traceback Module The traceback. format_exc() method returns a string that contains the information about exception and stack trace entries from the traceback object. We can use the format_exc() method to print the stack trace with the try and except statements.

How does stack trace work in Python?

The Python stack trace is a valuable piece of information that you can use to debug your code. It contains information about the call stack and points out where things have gone wrong. At the end of a stack trace, you can always find the exact exception type and a detailed message of what's gone wrong.


4 Answers

Some of the answers in Showing the stack trace from a running Python application are applicable in this situation:

  • pyrasite (this was the one that worked for me):

    $ sudo pip install pyrasite $ echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope $ sudo pyrasite 16262 dump_stacks.py # dumps stacks to stdout/stderr of the python program 
  • pyringe

  • pydbattach - couldn't get this to work, but the repository https://github.com/albertz/pydbattach contains pointer to other tools
  • pstack reportedly prints the python stack on Solaris
like image 194
Nickolay Avatar answered Sep 21 '22 15:09

Nickolay


winpdb allows you to attach to a running python process, but to do this, you must start the python process this way:

 rpdb2 -d -r script.py 

Then, after setting a password:

A password should be set to secure debugger client-server communication. Please type a password:mypassword 

you could launch winpdb to File>Attach to (or File>Detach from) the process.

like image 27
unutbu Avatar answered Sep 19 '22 15:09

unutbu


py-spy (https://github.com/benfred/py-spy) has a few useful tools for inspecting running Python processes. In particular, py-spy dump will print a stack trace (including function, file, and line) for every thread.

like image 43
Sophie Alpert Avatar answered Sep 21 '22 15:09

Sophie Alpert


on POSIX systems like Linux, you can use good old GDB, see

  • https://t37.net/debug-a-running-python-process-without-printf.html and
  • https://wiki.python.org/moin/DebuggingWithGdb

There's also the excellent PyCharm IDE (free community version available) that can attach to a running Python process right from within the IDE, using Pdb 4 under the hood, see this blog entry:

  • http://blog.jetbrains.com/pycharm/2015/02/feature-spotlight-python-debugger-and-attach-to-process/
like image 27
Gregor Avatar answered Sep 18 '22 15:09

Gregor