From af3d7ea70f3090680dda75cb97d3dea1042800e3 Mon Sep 17 00:00:00 2001 From: Karim Hamidou Date: Wed, 19 Oct 2016 07:18:54 -0700 Subject: [PATCH] Better output --- README.md | 58 +++++++++++++++++++++++++++++------------------------- lptrace.py | 5 ++++- 2 files changed, 35 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index fa00027..fcfd848 100644 --- a/README.md +++ b/README.md @@ -4,42 +4,46 @@ lptrace is strace for Python programs. It lets you see in real-time what functions a Python program is running. It's particularly useful to debug weird issues on production. -For example, let's take the following toy Python program: +For example, let's debugging a non-trivial program, the Python SimpleHTTPServer. +First, let's run the server: -```python -import time - -def f(): - time.sleep(1) - g() - -def g(): - time.sleep(1) - f() - -f() +``` +vagrant@precise32:/vagrant$ python -m SimpleHTTPServer 8080 & +[1] 1818 +vagrant@precise32:/vagrant$ Serving HTTP on 0.0.0.0 port 8080 ... ``` -Let's run it and connect lptrace to it: +Now let's connect lptrace to it: ``` -vagrant@precise32:/vagrant$ python /tmp/t.py & -[1] 1765 -vagrant@precise32:/vagrant$ sudo python ttrace.py -p 1765 -f (/tmp/t.py:3) -g (/tmp/t.py:7) -f (/tmp/t.py:3) -g (/tmp/t.py:7) -f (/tmp/t.py:3) -g (/tmp/t.py:7) -f (/tmp/t.py:3) -g (/tmp/t.py:7) -f (/tmp/t.py:3) +vagrant@precise32:/vagrant$ sudo python ttrace.py -p 1818 +... +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) + +_handle_request_noblock (/usr/lib/python2.7/SocketServer.py:271) +get_request (/usr/lib/python2.7/SocketServer.py:446) +accept (/usr/lib/python2.7/socket.py:201) +__init__ (/usr/lib/python2.7/socket.py:185) +verify_request (/usr/lib/python2.7/SocketServer.py:296) +process_request (/usr/lib/python2.7/SocketServer.py:304) +finish_request (/usr/lib/python2.7/SocketServer.py:321) +__init__ (/usr/lib/python2.7/SocketServer.py:632) +setup (/usr/lib/python2.7/SocketServer.py:681) +makefile (/usr/lib/python2.7/socket.py:212) +__init__ (/usr/lib/python2.7/socket.py:246) +makefile (/usr/lib/python2.7/socket.py:212) +__init__ (/usr/lib/python2.7/socket.py:246) +handle (/usr/lib/python2.7/BaseHTTPServer.py:336) +handle_one_request (/usr/lib/python2.7/BaseHTTPServer.py:301) ^CReceived Ctrl-C, quitting vagrant@precise32:/vagrant$ ``` -That's it! After pressing Ctrl-C, the trace is removed and the program +You can see that the server is handling the request normally! After pressing Ctrl-C, the trace is removed and the program execution resumes normally. # Running lptrace diff --git a/lptrace.py b/lptrace.py index 207499c..38d8e30 100644 --- a/lptrace.py +++ b/lptrace.py @@ -1,3 +1,6 @@ +# lptrace - Copyright (c) 2016 Karim Hamidou. +# Portions inspired by Pyrasite. + import os import sys import signal @@ -12,7 +15,7 @@ trace_fn = """def __lptrace_trace_calls__(frame, event, arg): code = frame.f_code with open(__LPTRACE_FIFO_NAME__, 'w') as fifo: - fifo.write("{} ({}:{})".format(code.co_name, + fifo.write("{} ({}:{})\\n".format(code.co_name, code.co_filename, frame.f_lineno)) __LPTRACE_FIFO_NAME__ = "%s" -- 2.43.0