Skip to content

Commit 9c4528d

Browse files
committed
test: improvements to run_sysmon.py for new sys.monitoring
1 parent b5408df commit 9c4528d

File tree

1 file changed

+47
-25
lines changed

1 file changed

+47
-25
lines changed

lab/run_sysmon.py

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
print(sys.version)
1010
the_program = sys.argv[1]
1111

12-
code = open(the_program).read()
12+
code = compile(open(the_program).read(), filename=the_program, mode="exec")
1313

1414
my_id = sys.monitoring.COVERAGE_ID
1515
sys.monitoring.use_tool_id(my_id, "run_sysmon.py")
@@ -27,50 +27,70 @@ def bytes_to_lines(code):
2727
return b2l
2828

2929

30+
def show_off(label, code, instruction_offset):
31+
if code.co_filename == the_program:
32+
b2l = bytes_to_lines(code)
33+
print(f"{label}: {code.co_filename}@{instruction_offset} #{b2l[instruction_offset]}")
34+
35+
def show_line(label, code, line_number):
36+
if code.co_filename == the_program:
37+
print(f"{label}: {code.co_filename} #{line_number}")
38+
39+
def show_off_off(label, code, instruction_offset, destination_offset):
40+
if code.co_filename == the_program:
41+
b2l = bytes_to_lines(code)
42+
print(
43+
f"{label}: {code.co_filename}@{instruction_offset}->{destination_offset} "
44+
+ f"#{b2l[instruction_offset]}->{b2l[destination_offset]}"
45+
)
46+
3047
def sysmon_py_start(code, instruction_offset):
31-
print(f"PY_START: {code.co_filename}@{instruction_offset}")
48+
show_off("PY_START", code, instruction_offset)
3249
sys.monitoring.set_local_events(
3350
my_id,
3451
code,
35-
events.PY_RETURN | events.PY_RESUME | events.LINE | events.BRANCH | events.JUMP,
52+
events.PY_RETURN
53+
| events.PY_RESUME
54+
| events.LINE
55+
| events.BRANCH_TAKEN
56+
| events.BRANCH_NOT_TAKEN
57+
| events.JUMP,
3658
)
3759

3860

3961
def sysmon_py_resume(code, instruction_offset):
40-
b2l = bytes_to_lines(code)
41-
print(
42-
f"PY_RESUME: {code.co_filename}@{instruction_offset}, "
43-
+ f"{b2l[instruction_offset]}"
44-
)
62+
show_off("PY_RESUME", code, instruction_offset)
63+
return sys.monitoring.DISABLE
4564

4665

4766
def sysmon_py_return(code, instruction_offset, retval):
48-
b2l = bytes_to_lines(code)
49-
print(
50-
f"PY_RETURN: {code.co_filename}@{instruction_offset}, "
51-
+ f"{b2l[instruction_offset]}"
52-
)
67+
show_off("PY_RETURN", code, instruction_offset)
68+
return sys.monitoring.DISABLE
5369

5470

5571
def sysmon_line(code, line_number):
56-
print(f"LINE: {code.co_filename}@{line_number}")
72+
show_line("LINE", code, line_number)
5773
return sys.monitoring.DISABLE
5874

5975

6076
def sysmon_branch(code, instruction_offset, destination_offset):
61-
b2l = bytes_to_lines(code)
62-
print(
63-
f"BRANCH: {code.co_filename}@{instruction_offset}->{destination_offset}, "
64-
+ f"{b2l[instruction_offset]}->{b2l[destination_offset]}"
65-
)
77+
show_off_off("BRANCH", code, instruction_offset, destination_offset)
78+
return sys.monitoring.DISABLE
79+
80+
81+
def sysmon_branch_taken(code, instruction_offset, destination_offset):
82+
show_off_off("BRANCH_TAKEN", code, instruction_offset, destination_offset)
83+
return sys.monitoring.DISABLE
84+
85+
86+
def sysmon_branch_not_taken(code, instruction_offset, destination_offset):
87+
show_off_off("BRANCH_NOT_TAKEN", code, instruction_offset, destination_offset)
88+
return sys.monitoring.DISABLE
6689

6790

6891
def sysmon_jump(code, instruction_offset, destination_offset):
69-
b2l = bytes_to_lines(code)
70-
print(
71-
f"JUMP: {code.co_filename}@{instruction_offset}->{destination_offset}, "
72-
+ f"{b2l[instruction_offset]}->{b2l[destination_offset]}"
73-
)
92+
show_off_off("JUMP", code, instruction_offset, destination_offset)
93+
return sys.monitoring.DISABLE
7494

7595

7696
sys.monitoring.set_events(
@@ -82,7 +102,9 @@ def sysmon_jump(code, instruction_offset, destination_offset):
82102
register(events.PY_RETURN, sysmon_py_return)
83103
# register(events.PY_UNWIND, sysmon_py_unwind_arcs)
84104
register(events.LINE, sysmon_line)
85-
register(events.BRANCH, sysmon_branch)
105+
#register(events.BRANCH, sysmon_branch)
106+
register(events.BRANCH_TAKEN, sysmon_branch_taken)
107+
register(events.BRANCH_NOT_TAKEN, sysmon_branch_not_taken)
86108
register(events.JUMP, sysmon_jump)
87109

88110
exec(code)

0 commit comments

Comments
 (0)