Skip to content

Commit

Permalink
test: improvements to run_sysmon.py for new sys.monitoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed Aug 31, 2024
1 parent b5408df commit 9c4528d
Showing 1 changed file with 47 additions and 25 deletions.
72 changes: 47 additions & 25 deletions lab/run_sysmon.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
print(sys.version)
the_program = sys.argv[1]

code = open(the_program).read()
code = compile(open(the_program).read(), filename=the_program, mode="exec")

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


def show_off(label, code, instruction_offset):
if code.co_filename == the_program:
b2l = bytes_to_lines(code)
print(f"{label}: {code.co_filename}@{instruction_offset} #{b2l[instruction_offset]}")

def show_line(label, code, line_number):
if code.co_filename == the_program:
print(f"{label}: {code.co_filename} #{line_number}")

def show_off_off(label, code, instruction_offset, destination_offset):
if code.co_filename == the_program:
b2l = bytes_to_lines(code)
print(
f"{label}: {code.co_filename}@{instruction_offset}->{destination_offset} "
+ f"#{b2l[instruction_offset]}->{b2l[destination_offset]}"
)

def sysmon_py_start(code, instruction_offset):
print(f"PY_START: {code.co_filename}@{instruction_offset}")
show_off("PY_START", code, instruction_offset)
sys.monitoring.set_local_events(
my_id,
code,
events.PY_RETURN | events.PY_RESUME | events.LINE | events.BRANCH | events.JUMP,
events.PY_RETURN
| events.PY_RESUME
| events.LINE
| events.BRANCH_TAKEN
| events.BRANCH_NOT_TAKEN
| events.JUMP,
)


def sysmon_py_resume(code, instruction_offset):
b2l = bytes_to_lines(code)
print(
f"PY_RESUME: {code.co_filename}@{instruction_offset}, "
+ f"{b2l[instruction_offset]}"
)
show_off("PY_RESUME", code, instruction_offset)
return sys.monitoring.DISABLE


def sysmon_py_return(code, instruction_offset, retval):
b2l = bytes_to_lines(code)
print(
f"PY_RETURN: {code.co_filename}@{instruction_offset}, "
+ f"{b2l[instruction_offset]}"
)
show_off("PY_RETURN", code, instruction_offset)
return sys.monitoring.DISABLE


def sysmon_line(code, line_number):
print(f"LINE: {code.co_filename}@{line_number}")
show_line("LINE", code, line_number)
return sys.monitoring.DISABLE


def sysmon_branch(code, instruction_offset, destination_offset):
b2l = bytes_to_lines(code)
print(
f"BRANCH: {code.co_filename}@{instruction_offset}->{destination_offset}, "
+ f"{b2l[instruction_offset]}->{b2l[destination_offset]}"
)
show_off_off("BRANCH", code, instruction_offset, destination_offset)
return sys.monitoring.DISABLE


def sysmon_branch_taken(code, instruction_offset, destination_offset):
show_off_off("BRANCH_TAKEN", code, instruction_offset, destination_offset)
return sys.monitoring.DISABLE


def sysmon_branch_not_taken(code, instruction_offset, destination_offset):
show_off_off("BRANCH_NOT_TAKEN", code, instruction_offset, destination_offset)
return sys.monitoring.DISABLE


def sysmon_jump(code, instruction_offset, destination_offset):
b2l = bytes_to_lines(code)
print(
f"JUMP: {code.co_filename}@{instruction_offset}->{destination_offset}, "
+ f"{b2l[instruction_offset]}->{b2l[destination_offset]}"
)
show_off_off("JUMP", code, instruction_offset, destination_offset)
return sys.monitoring.DISABLE


sys.monitoring.set_events(
Expand All @@ -82,7 +102,9 @@ def sysmon_jump(code, instruction_offset, destination_offset):
register(events.PY_RETURN, sysmon_py_return)
# register(events.PY_UNWIND, sysmon_py_unwind_arcs)
register(events.LINE, sysmon_line)
register(events.BRANCH, sysmon_branch)
#register(events.BRANCH, sysmon_branch)
register(events.BRANCH_TAKEN, sysmon_branch_taken)
register(events.BRANCH_NOT_TAKEN, sysmon_branch_not_taken)
register(events.JUMP, sysmon_jump)

exec(code)

0 comments on commit 9c4528d

Please sign in to comment.