2019-08-14 10:11:25 -04:00
|
|
|
import sys
|
|
|
|
|
|
|
|
try:
|
|
|
|
sys.settrace
|
|
|
|
except AttributeError:
|
|
|
|
print("SKIP")
|
|
|
|
raise SystemExit
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def print_stacktrace(frame, level=0):
|
|
|
|
# Ignore CPython specific helpers.
|
2020-03-22 22:26:08 -04:00
|
|
|
if frame.f_globals["__name__"].find("importlib") != -1:
|
2019-08-14 10:11:25 -04:00
|
|
|
print_stacktrace(frame.f_back, level)
|
|
|
|
return
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
print(
|
|
|
|
"%2d: %s@%s:%s => %s:%d"
|
|
|
|
% (
|
|
|
|
level,
|
|
|
|
" ",
|
|
|
|
frame.f_globals["__name__"],
|
|
|
|
frame.f_code.co_name,
|
2020-12-15 05:54:34 -05:00
|
|
|
# Keep just the filename.
|
|
|
|
"sys_settrace_" + frame.f_code.co_filename.split("sys_settrace_")[-1],
|
2020-03-22 22:26:08 -04:00
|
|
|
frame.f_lineno,
|
|
|
|
)
|
|
|
|
)
|
2019-08-14 10:11:25 -04:00
|
|
|
|
|
|
|
if frame.f_back:
|
|
|
|
print_stacktrace(frame.f_back, level + 1)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
class _Prof:
|
2020-03-22 22:26:08 -04:00
|
|
|
trace_count = 0
|
2019-08-14 10:11:25 -04:00
|
|
|
|
|
|
|
def trace_tick(self, frame, event, arg):
|
|
|
|
self.trace_count += 1
|
|
|
|
print_stacktrace(frame)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
__prof__ = _Prof()
|
|
|
|
|
|
|
|
alice_handler_set = False
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def trace_tick_handler_alice(frame, event, arg):
|
|
|
|
print("### trace_handler::Alice event:", event)
|
|
|
|
__prof__.trace_tick(frame, event, arg)
|
|
|
|
return trace_tick_handler_alice
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
bob_handler_set = False
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def trace_tick_handler_bob(frame, event, arg):
|
|
|
|
print("### trace_handler::Bob event:", event)
|
|
|
|
__prof__.trace_tick(frame, event, arg)
|
|
|
|
return trace_tick_handler_bob
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def trace_tick_handler(frame, event, arg):
|
|
|
|
# Ignore CPython specific helpers.
|
2020-12-13 18:29:57 -05:00
|
|
|
frame_name = frame.f_globals["__name__"]
|
|
|
|
if frame_name.find("importlib") != -1 or frame_name.find("zipimport") != -1:
|
2019-08-14 10:11:25 -04:00
|
|
|
return
|
|
|
|
|
|
|
|
print("### trace_handler::main event:", event)
|
|
|
|
__prof__.trace_tick(frame, event, arg)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
if frame.f_code.co_name != "factorial":
|
2019-08-14 10:11:25 -04:00
|
|
|
return trace_tick_handler
|
|
|
|
|
|
|
|
global alice_handler_set
|
2020-03-22 22:26:08 -04:00
|
|
|
if event == "call" and not alice_handler_set:
|
2019-08-14 10:11:25 -04:00
|
|
|
alice_handler_set = True
|
|
|
|
return trace_tick_handler_alice
|
|
|
|
|
|
|
|
global bob_handler_set
|
2020-03-22 22:26:08 -04:00
|
|
|
if event == "call" and not bob_handler_set:
|
2019-08-14 10:11:25 -04:00
|
|
|
bob_handler_set = True
|
|
|
|
return trace_tick_handler_bob
|
|
|
|
|
|
|
|
return trace_tick_handler
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def factorial(n):
|
|
|
|
if n == 0:
|
|
|
|
return 1
|
|
|
|
else:
|
|
|
|
return n * factorial(n - 1)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
def do_tests():
|
|
|
|
# These commands are here to demonstrate some execution being traced.
|
|
|
|
print("Who loves the sun?")
|
|
|
|
print("Not every-", factorial(3))
|
|
|
|
|
2020-12-15 05:54:34 -05:00
|
|
|
from sys_settrace_subdir import sys_settrace_generic
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2020-12-15 05:54:34 -05:00
|
|
|
sys_settrace_generic.run_tests()
|
2019-08-14 10:11:25 -04:00
|
|
|
return
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
2019-08-14 10:11:25 -04:00
|
|
|
sys.settrace(trace_tick_handler)
|
|
|
|
do_tests()
|
|
|
|
sys.settrace(None)
|
|
|
|
|
|
|
|
print("\n------------------ script exited ------------------")
|
|
|
|
print("Total traces executed: ", __prof__.trace_count)
|