9
9
print (sys .version )
10
10
the_program = sys .argv [1 ]
11
11
12
- code = open (the_program ).read ()
12
+ code = compile ( open (the_program ).read (), filename = the_program , mode = "exec" )
13
13
14
14
my_id = sys .monitoring .COVERAGE_ID
15
15
sys .monitoring .use_tool_id (my_id , "run_sysmon.py" )
@@ -27,50 +27,70 @@ def bytes_to_lines(code):
27
27
return b2l
28
28
29
29
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
+
30
47
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 )
32
49
sys .monitoring .set_local_events (
33
50
my_id ,
34
51
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 ,
36
58
)
37
59
38
60
39
61
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
45
64
46
65
47
66
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
53
69
54
70
55
71
def sysmon_line (code , line_number ):
56
- print ( f "LINE: { code . co_filename } @ { line_number } " )
72
+ show_line ( "LINE" , code , line_number )
57
73
return sys .monitoring .DISABLE
58
74
59
75
60
76
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
66
89
67
90
68
91
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
74
94
75
95
76
96
sys .monitoring .set_events (
@@ -82,7 +102,9 @@ def sysmon_jump(code, instruction_offset, destination_offset):
82
102
register (events .PY_RETURN , sysmon_py_return )
83
103
# register(events.PY_UNWIND, sysmon_py_unwind_arcs)
84
104
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 )
86
108
register (events .JUMP , sysmon_jump )
87
109
88
110
exec (code )
0 commit comments