Skip to content

Instantly share code, notes, and snippets.

@mumumu
Last active March 23, 2017 21:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mumumu/857c17f381dfa1976f7cf0694c7c8cc4 to your computer and use it in GitHub Desktop.
Save mumumu/857c17f381dfa1976f7cf0694c7c8cc4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# encoding: utf-8
import sys
#
# Note: This code works on Python2 only!
#
ignore_functions = [
'write', # from print statement
'_remove', # from internal last cleanup
]
def trace_calls(frame, event, arg):
if event not in ['call', 'return']:
return
if not ignore_functions:
# on last cleanup, global variable may be already removed
return
co = frame.f_code
func_name = co.co_name
if func_name in ignore_functions:
return
func_line_no = frame.f_lineno
local_variables = frame.f_locals
if event == 'call':
func_filename = co.co_filename
caller = frame.f_back
caller_line_no = caller.f_lineno
caller_filename = caller.f_code.co_filename
print 'Call to %s on line %s of %s from line %s of %s with arg (%s)' % \
(func_name, func_line_no, func_filename,
caller_line_no, caller_filename, local_variables)
return trace_calls # for return value can be monitored.
elif event == 'return':
print local_variables
print 'return from [%s] line %s => %s' % (func_name, func_line_no, arg)
return
class Hoge(object):
def __init__(self):
pass
def d(self, args):
return [args, args]
def b():
def c(arg):
print "in c()"
hoge = Hoge()
hoge.d(arg)
return ['hoge', 'fuga', arg]
print 'in b()'
c(2)
def a():
local_integers = [1, 2, 3] # noqa
print 'in a()'
b()
sys.settrace(trace_calls)
a()
@mumumu
Copy link
Author

mumumu commented Mar 23, 2017

$ python sys_settrace_example_code.py 
Call to a on line 60 of sys_settrace_example_code.py from line 66 of sys_settrace_example_code.py with arg ({})
in a()
Call to b on line 50 of sys_settrace_example_code.py from line 63 of sys_settrace_example_code.py with arg ({})
in b()
Call to c on line 51 of sys_settrace_example_code.py from line 57 of sys_settrace_example_code.py with arg ({'arg': 2})
in c()
Call to __init__ on line 43 of sys_settrace_example_code.py from line 53 of sys_settrace_example_code.py with arg ({'self': <__main__.Hoge object at 0x7f03e8b78110>})
{'self': <__main__.Hoge object at 0x7f03e8b78110>}
return from [__init__] line 44 => None
Call to d on line 46 of sys_settrace_example_code.py from line 54 of sys_settrace_example_code.py with arg ({'self': <__main__.Hoge object at 0x7f03e8b78110>, 'args': 2})
{'self': <__main__.Hoge object at 0x7f03e8b78110>, 'args': 2}
return from [d] line 47 => [2, 2]
{'hoge': <__main__.Hoge object at 0x7f03e8b78110>, 'arg': 2}
return from [c] line 55 => ['hoge', 'fuga', 2]
{'c': <function c at 0x7f03e8b6e8c0>}
return from [b] line 57 => None
{'local_integers': [1, 2, 3]}
return from [a] line 63 => None

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment