In Python, we can use the sys module’s settrace() method to setup trace hooks and monitor the functions inside a program.
You need to define a trace callback method and pass it to the settrace() function. The callback should specify three arguments as shown below.
import sys
def trace_calls(frame, event, arg):
# The 'call' event occurs before a function gets executed.
if event != 'call':
return
# Next, inspect the frame data and print information.
print 'Function name=%s, line num=%s' % (frame.f_code.co_name, frame.f_lineno)
return
def demo2():
print 'in demo2()'
def demo1():
print 'in demo1()'
demo2()
sys.settrace(trace_calls)
demo1()