Custom Search

Monday, April 7, 2014

How to print stack trace from a running Python application

1)
Add following code in the begining of shell.py

#sudo vim /opt/stack/python-heatclient/heatclient/shell.py

import code, traceback, signal

def debug(sig, frame):
    """Interrupt running process, and provide a python prompt for
    interactive debugging."""
    d={'_frame':frame}         # Allow access to frame object.
    d.update(frame.f_globals)  # Unless shadowed by global
    d.update(frame.f_locals)

    i = code.InteractiveConsole(d)
    message  = "Signal recieved : entering python shell.\nTraceback:\n"
    message += ''.join(traceback.format_stack(frame))
    i.interact(message)

def listen():
    signal.signal(signal.SIGUSR1, debug)  # Register handler




2)
Edit shell.py and invoke the newly added "listen" from the "main" method.
So that will get called when we run a heat command like #heat list

#sudo vim /opt/stack/python-heatclient/heatclient/shell.py

class HeatShell(object):
    def main(self, argv):
        listen()


3)
Goto devstack folder and run

#source openrc

4)
Run the heat command

#heat list

5)
Open another terminal and type following command and note the pid of CLI process.

#ps -aux | grep heat
saju     12772  0.6  1.1 119040 23072 pts/11   S+   18:57   0:00 /usr/bin/python /usr/local/bin/heat list

6)
Then open python prompt and type following command

#python
>>> import os, signal
>>> os.kill(12772, signal.SIGUSR1)


7)
Check the terminal where you run the command 

#heat list

8)
Press Ctrl + d to continue



1 comment:

  1. http://stackoverflow.com/questions/132058/showing-the-stack-trace-from-a-running-python-application

    ReplyDelete