|
| 1 | +#!/usr/bin/env python |
| 2 | +# encoding: utf-8 |
| 3 | + |
| 4 | +import sys |
| 5 | + |
| 6 | +def trace_lines(frame, event, arg): |
| 7 | + if event != 'line': |
| 8 | + return |
| 9 | + co = frame.f_code |
| 10 | + func_name = co.co_name |
| 11 | + line_no = frame.f_lineno |
| 12 | + filename = co.co_filename |
| 13 | + print ' %s line %s' % (func_name, line_no) |
| 14 | + |
| 15 | +def trace_calls(frame, event, arg): |
| 16 | + if event != 'call': |
| 17 | + return |
| 18 | + co = frame.f_code |
| 19 | + func_name = co.co_name |
| 20 | + if func_name == 'write': |
| 21 | + # Ignore write() calls from print statements |
| 22 | + return |
| 23 | + line_no = frame.f_lineno |
| 24 | + filename = co.co_filename |
| 25 | + print 'Call to %s on line %s of %s' % (func_name, line_no, filename) |
| 26 | + if func_name in TRACE_INTO: |
| 27 | + # Trace into this function |
| 28 | + return trace_lines |
| 29 | + return |
| 30 | + |
| 31 | +def trace_calls(frame, event, arg): |
| 32 | + if event != 'call': |
| 33 | + return |
| 34 | + co = frame.f_code |
| 35 | + func_name = co.co_name |
| 36 | + if func_name == 'write': |
| 37 | + # Ignore write() calls from print statements |
| 38 | + return |
| 39 | + func_line_no = frame.f_lineno |
| 40 | + func_filename = co.co_filename |
| 41 | + caller = frame.f_back |
| 42 | + caller_line_no = caller.f_lineno |
| 43 | + caller_filename = caller.f_code.co_filename |
| 44 | + print 'Call to %s on line %s of %s from line %s of %s' % \ |
| 45 | + (func_name, func_line_no, func_filename, |
| 46 | + caller_line_no, caller_filename) |
| 47 | + return |
| 48 | + |
| 49 | +def trace_calls_and_returns(frame, event, arg): |
| 50 | + co = frame.f_code |
| 51 | + func_name = co.co_name |
| 52 | + if func_name == 'write': |
| 53 | + # Ignore write() calls from print statements |
| 54 | + return |
| 55 | + line_no = frame.f_lineno |
| 56 | + filename = co.co_filename |
| 57 | + if event == 'call': |
| 58 | + print 'Call to %s on line %s of %s' % (func_name, line_no, filename) |
| 59 | + return trace_calls_and_returns |
| 60 | + elif event == 'return': |
| 61 | + print '%s => %s' % (func_name, arg) |
| 62 | + return |
| 63 | + |
| 64 | +''' settrace usage ''' |
| 65 | +sys.settrace(trace_calls_and_returns) |
| 66 | + |
0 commit comments