lptrace

trace any Python program, anywhere.

Download .tar.gz View on GitHub

What is lptrace?


lptrace is a Python script you can use to trace any running Python process. It's lightweight and runs anywhere, including on production servers. It only depends on a moderately recent version of Python (2.6+) and GDB (7+).

How do I use it?


Step 1: Get the PID of the misbehaving program:

vagrant@precise32:/vagrant$ ps ax | grep "misbehaving-daemon"
1289 ?        S      0:00 python /usr/share/bin/misbehaving-daemon
            
Step 2: Attach lptrace to it

vagrant@precise32:/vagrant$ sudo python lptrace -p 1289
fileno (/usr/lib/python2.7/SocketServer.py:438)
meth (/usr/lib/python2.7/socket.py:223)

fileno (/usr/lib/python2.7/SocketServer.py:438)
meth (/usr/lib/python2.7/socket.py:223)

...
Step 3: Stop tracing the program by pressing Ctrl-C

^CReceived Ctrl-C, quitting
vagrant@precise32:/vagrant$

That's about it! Happy hacking!

How does it work?


lptrace uses gdb to inject code into the Python process it attaches itself to. More details here.

Who wrote it?


Hi there, I'm Karim! I've debugged my share of misbehaving daemons in production and along the way I've learnt a couple interesting tricks.

I created lptrace after randomly stumbing upon the pyrasite source code and realizing it would be relatively easy to create a tracer for Python programs.

Since then it has saved me hours of head scratching. Hopefully it'll save you a lot of time too!