forked from nedbat/byterun
-
-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathvmtrace.py
More file actions
425 lines (369 loc) · 13.9 KB
/
vmtrace.py
File metadata and controls
425 lines (369 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
"""A variant of VirtualMachine that adds a callback.
This can be used in a debugger or profiler.
"""
import logging
from types import TracebackType
from xdis import IS_PYPY, PYTHON_VERSION_TRIPLE, codeType2Portable
# We will add a new "DEBUG" opcode
from xdis.opcodes.base import def_op
from xpython.pyobj import Frame, Traceback, traceback_from_frame
from xpython.vm import PyVM, PyVMError, PyVMUncaughtException, byteint, format_instruction
log = logging.getLogger(__name__)
BREAKPOINT_OP = 8
PyVMEVENT_INSTRUCTION = 1 # tracing an instruction
PyVMEVENT_LINE = (
2 # tracing an instruction which has has a line number. Above includes this.
)
PyVMEVENT_CALL = 4 # tracing calls. Note "Step over" disables this kind of trace
PyVMEVENT_RETURN = 8 # tracing returns
PyVMEVENT_EXCEPTION = 16 # tracing exceptions
PyVMEVENT_YIELD = 32 # tracing "yield"
PyVMEVENT_FATAL = 64 # Final fatal error
PyVMEVENT_STEP_OVER = 128 # tracing using step over - don't trace into calls
PyVMEVENT_FLAG_NAMES = {
1: "instruction",
2: "line",
4: "call",
8: "return",
16: "exception",
32: "yield",
64: "fatal",
128: "step_over",
}
PyVMEVENT_FLAG_BITS = {name: bit for bit, name in PyVMEVENT_FLAG_NAMES.items()}
# All flags except STEP_OVER which is a kind of negation
PyVMEVENT_ALL = (
PyVMEVENT_INSTRUCTION
| PyVMEVENT_LINE
| PyVMEVENT_CALL
| PyVMEVENT_RETURN
| PyVMEVENT_EXCEPTION
| PyVMEVENT_YIELD
| PyVMEVENT_FATAL
)
# All flags cleared
PyVMEVENT_NONE = 0
def pretty_event_flags(flags) -> str:
"""Return pretty representation of trace event flags."""
names = []
result = f"0x{flags:08x}"
for i in range(32):
flag = 1 << i
if flags & flag:
names.append(PyVMEVENT_FLAG_NAMES.get(flag, hex(flag)))
flags ^= flag
if not flags:
break
else:
names.append(hex(flags))
names.reverse()
return f"{result} ({' | '.join(names)})"
class PyVMTraced(PyVM):
def __init__(
self,
callback,
python_version: tuple[int, ...]=PYTHON_VERSION_TRIPLE,
is_pypy: bool=IS_PYPY,
vmtest_testing: bool=False,
event_flags: int=PyVMEVENT_ALL,
format_instruction_func=format_instruction,
) -> None:
super().__init__(
python_version,
is_pypy,
vmtest_testing,
format_instruction_func=format_instruction_func,
)
self.event_flags = event_flags
self.callback = callback
# Add a new opcode to allow us high-speed breakpoints
# FIXME: older xdis uses "self.opc.l" instead of "self.opc.loc"
if not hasattr(self.opc, "loc"):
if hasattr(self.opc, "l"):
self.opc.loc = self.opc.l
def_op(self.opc.loc, "BRKPT", BREAKPOINT_OP, 0, 0)
def add_breakpoint(self, frame: Frame, offset: int) -> None:
"""
Adds a breakpoint at `offset` of `frame`. This is done by modifying the
bytecode opcode at the given offset by replacing it with a pseudo-op BRKPT
instruction. The old opcode is squirreled a way though.
"""
# FIXME: to be more useful we need to work on a code object, and modified code
# objects then get replaced when creating frames.
# Convert code to something we can change, then
# Convert its bytecode bytes to a list, update the list and replace this back in
# the code.
code = codeType2Portable(frame.f_code, self.version)
frame.brkpt[offset] = code.co_code[offset]
bytecode = list(code.co_code)
bytecode[offset] = BREAKPOINT_OP
code.co_code = bytes(bytecode)
frame.f_code = code
def remove_breakpoint(self, frame: Frame, offset: int) -> None:
"""
Removes a breakpoint at `offset` of `frame`. This is done by restoring the
opcode that was previously smashed using `add_breakpoint()`
"""
# Convert code to something we can change, then
# Convert its bytecode ytes to a list, update the list and replace this back in
# the code.
code = frame.f_code
bytecode = list(code.co_code)
bytecode[offset] = frame.brkpt[offset]
code.co_code = bytes(bytecode)
# FIXME: put callback in f_trace, and update it accordingly
# Interpreter main loop
# This is analogous to CPython's _PyEval_EvalFrameDefault() (in 3.x newer Python)
# or eval_frame() in older 2.x code.
def eval_frame(self, frame: Frame) -> None:
"""Run a frame until it returns (somehow).
Exceptions are raised, the return value is returned.
This code includes frame tracing (ftrace) support used in debugging. For code
without tracing see the corresponding code in vm.py.
"""
# Extra tracing code
if self.frame:
# Inherit values from self.frame
frame.f_trace = self.frame.f_trace
frame.event_flags = self.frame.event_flags
else:
# Get (presumably initial) values from vm
frame.f_trace = self.callback
frame.event_flags = self.event_flags
if frame.event_flags & PyVMEVENT_STEP_OVER:
frame.event_flags = PyVMEVENT_NONE
result = None
# End extra tracing code
self.f_code = frame.f_code
if frame.f_lasti == -1:
# We were started new, not yielded back from.
frame.f_lasti = 0
# Don't increment before fetching next instruction.
frame.fallthrough = False
byte_code = None
# Extra tracing code #
last_i = frame.f_back.f_lasti if frame.f_back else -1
self.push_frame(frame)
if frame.f_trace and (frame.event_flags & PyVMEVENT_CALL):
if frame.event_flags & PyVMEVENT_STEP_OVER:
# Since we are about to enter a function, but not
# tracing it, clear return-like events return and
# yield
frame.event_flags &= ~(PyVMEVENT_RETURN | PyVMEVENT_YIELD)
else:
result = frame.f_trace(
"call",
last_i,
"CALL",
byte_code,
frame.f_lineno,
None,
[],
self,
)
pass
# End extra tracing code #
else:
byte_code = byteint(frame.f_code.co_code[frame.f_lasti])
self.push_frame(frame)
if frame.f_trace and frame.event_flags & PyVMEVENT_YIELD:
result = frame.f_trace(
"yield",
frame.f_lasti,
"YIELD_VALUE",
self.opc.YIELD_VALUE,
frame.f_lineno,
None,
[],
self,
)
pass
# byte_code == opcode["YIELD_VALUE"]?
# FIXME: DRY with BRKPT op code
if result:
if result == "finish":
frame.f_trace = None
frame.event_flags = PyVMEVENT_RETURN | PyVMEVENT_YIELD
elif result == "return":
return self.return_value
self.frame.linestarts = dict(
self.opc.findlinestarts(frame.f_code, dup_lines=True)
)
offset = 0
while True:
(
bytecode_name,
byte_code,
int_arg,
arguments,
offset,
line_number,
) = self.parse_byte_and_args(byte_code)
if log.isEnabledFor(logging.INFO):
self.log(bytecode_name, int_arg, arguments, offset, line_number)
if (
frame.f_trace
and line_number is not None
and frame.event_flags & (PyVMEVENT_LINE | PyVMEVENT_INSTRUCTION)
):
result = frame.f_trace(
"line",
offset,
bytecode_name,
byte_code,
line_number,
int_arg,
arguments,
self,
)
elif frame.f_trace and frame.event_flags & PyVMEVENT_INSTRUCTION:
result = frame.f_trace(
"instruction",
offset,
bytecode_name,
byte_code,
line_number,
int_arg,
arguments,
self,
)
else:
result = True
if result is None:
# As per https://docs.python.org/3/library/sys.html#sys.settrace
# None indicates turning off tracing in this scope.
# We could imagine a fancier code organization where we use
# eval_frame() of PyVM instead of PyVMTrace, but save that for later.
frame.event_flags = 0
elif callable(result):
pass
elif isinstance(result, str):
if result == "skip":
# Don't run instruction
continue
elif result == "return":
# Immediate return with value
why = result
break
elif result == "finish":
# Continue execution without tracing
frame.f_trace = None
# When unwinding the block stack, we need to keep track of why we
# are doing it.
why = self.dispatch(bytecode_name, int_arg, arguments, offset, line_number)
if why == "exception":
# Deal with exceptions encountered while executing the op.
# TODO: ceval calls PyTraceBack_Here, not sure what that does.
if self.version >= (3, 11):
self.exception_handling_311()
if not self.in_exception_processing:
self.last_traceback = traceback_from_frame(self.frame)
self.in_exception_processing = True
elif why == "reraise":
why = "exception"
if self.version >= (3, 11):
self.exception_handling_311()
if why != "yield":
while why and frame.block_stack:
# Deal with any block management we need to do.
why = self.manage_block_stack(why)
if why:
break
pass # while True
# Extra tracing code...
callback = frame.f_trace or self.callback
if why == "exception":
if (
callback
and frame
and (not frame or frame.event_flags & PyVMEVENT_EXCEPTION)
):
frame.f_trace(
"exception",
offset,
bytecode_name,
byte_code,
line_number,
int_arg,
self.last_exception,
self,
)
elif callback and (not frame or frame.event_flags & PyVMEVENT_RETURN):
callback(
"return",
offset,
bytecode_name,
byte_code,
line_number,
int_arg,
self.return_value,
self,
)
pass
# End extra tracing code
# TODO: handle generator exception state
self.pop_frame()
if why == "exception":
last_exception = self.last_exception
if last_exception and last_exception[0]:
if isinstance(last_exception[2], (Traceback, TracebackType)):
if not self.frame:
if isinstance(last_exception, tuple):
self.last_exception = PyVMUncaughtException.from_tuple(
last_exception
)
raise self.last_exception
else:
raise last_exception[0]
pass
pass
else:
raise PyVMError("Borked exception recording")
# if self.exception and .... ?
# log.error("Haven't finished traceback handling, nulling traceback "
# "information for now")
# six.reraise(self.last_exception[0], None)
self.in_exception_processing = False
# Extra tracing code:
if callback and frame.event_flags & PyVMEVENT_RETURN:
callback(
"return",
offset,
bytecode_name,
byte_code,
line_number,
None,
self.return_value,
self,
)
return self.return_value
if __name__ == "__main__":
def sample_callback_hook(
event, offset, bytecode_name, byte_code, line_number, int_arg, event_arg, vm
) -> None:
print(
"CALLBACK",
event,
offset,
bytecode_name,
byte_code,
line_number,
int_arg,
event_arg,
)
# Simplest of tests
def five() -> int:
return 5
# Test with a conditional in it
a, b = 10, 3
def mymax() -> int:
return a if a > b else b
logging.basicConfig(level=logging.DEBUG)
vm = PyVMTraced(sample_callback_hook)
frame = vm.make_frame(five.__code__)
vm.add_breakpoint(frame, 0)
print("five() is", vm.eval_frame(frame))
frame.f_lasti = -1 # Reset where we were
vm.remove_breakpoint(frame, 0)
print("five() is now", vm.eval_frame(frame))
print(vm.run_code(mymax.__code__, f_globals=globals(), f_locals=locals()))