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
|
:mod:`sys.monitoring` --- Execution event monitoring
====================================================
.. module:: sys.monitoring
:synopsis: Access and control event monitoring
.. versionadded:: 3.12
-----------------
.. note::
:mod:`sys.monitoring` is a namespace within the :mod:`sys` module,
not an independent module, so there is no need to
``import sys.monitoring``, simply ``import sys`` and then use
``sys.monitoring``.
This namespace provides access to the functions and constants necessary to
activate and control event monitoring.
As programs execute, events occur that might be of interest to tools that
monitor execution. The :mod:`sys.monitoring` namespace provides means to
receive callbacks when events of interest occur.
The monitoring API consists of three components:
* `Tool identifiers`_
* `Events`_
* :ref:`Callbacks <callbacks>`
Tool identifiers
----------------
A tool identifier is an integer and the associated name.
Tool identifiers are used to discourage tools from interfering with each
other and to allow multiple tools to operate at the same time.
Currently tools are completely independent and cannot be used to
monitor each other. This restriction may be lifted in the future.
Before registering or activating events, a tool should choose an identifier.
Identifiers are integers in the range 0 to 5 inclusive.
Registering and using tools
'''''''''''''''''''''''''''
.. function:: use_tool_id(id: int, name: str) -> None
Must be called before *id* can be used.
*id* must be in the range 0 to 5 inclusive.
Raises a :exc:`ValueError` if *id* is in use.
.. function:: free_tool_id(id: int) -> None
Should be called once a tool no longer requires *id*.
.. function:: get_tool(id: int) -> str | None
Returns the name of the tool if *id* is in use,
otherwise it returns ``None``.
*id* must be in the range 0 to 5 inclusive.
All IDs are treated the same by the VM with regard to events, but the
following IDs are pre-defined to make co-operation of tools easier::
sys.monitoring.DEBUGGER_ID = 0
sys.monitoring.COVERAGE_ID = 1
sys.monitoring.PROFILER_ID = 2
sys.monitoring.OPTIMIZER_ID = 5
There is no obligation to set an ID, nor is there anything preventing a tool
from using an ID even it is already in use.
However, tools are encouraged to use a unique ID and respect other tools.
Events
------
The following events are supported:
.. monitoring-event:: BRANCH
A conditional branch is taken (or not).
.. monitoring-event:: CALL
A call in Python code (event occurs before the call).
.. monitoring-event:: C_RAISE
An exception raised from any callable, except for Python functions (event occurs after the exit).
.. monitoring-event:: C_RETURN
Return from any callable, except for Python functions (event occurs after the return).
.. monitoring-event:: EXCEPTION_HANDLED
An exception is handled.
.. monitoring-event:: INSTRUCTION
A VM instruction is about to be executed.
.. monitoring-event:: JUMP
An unconditional jump in the control flow graph is made.
.. monitoring-event:: LINE
An instruction is about to be executed that has a different line number from the preceding instruction.
.. monitoring-event:: PY_RESUME
Resumption of a Python function (for generator and coroutine functions), except for ``throw()`` calls.
.. monitoring-event:: PY_RETURN
Return from a Python function (occurs immediately before the return, the callee's frame will be on the stack).
.. monitoring-event:: PY_START
Start of a Python function (occurs immediately after the call, the callee's frame will be on the stack)
.. monitoring-event:: PY_THROW
A Python function is resumed by a ``throw()`` call.
.. monitoring-event:: PY_UNWIND
Exit from a Python function during exception unwinding.
.. monitoring-event:: PY_YIELD
Yield from a Python function (occurs immediately before the yield, the callee's frame will be on the stack).
.. monitoring-event:: RAISE
An exception is raised, except those that cause a :monitoring-event:`STOP_ITERATION` event.
.. monitoring-event:: RERAISE
An exception is re-raised, for example at the end of a :keyword:`finally` block.
.. monitoring-event:: STOP_ITERATION
An artificial :exc:`StopIteration` is raised; see `the STOP_ITERATION event`_.
More events may be added in the future.
These events are attributes of the :mod:`!sys.monitoring.events` namespace.
Each event is represented as a power-of-2 integer constant.
To define a set of events, simply bitwise or the individual events together.
For example, to specify both :monitoring-event:`PY_RETURN` and :monitoring-event:`PY_START`
events, use the expression ``PY_RETURN | PY_START``.
.. monitoring-event:: NO_EVENTS
An alias for ``0`` so users can do explict comparisions like::
if get_events(DEBUGGER_ID) == NO_EVENTS:
...
Events are divided into three groups:
Local events
''''''''''''
Local events are associated with normal execution of the program and happen
at clearly defined locations. All local events can be disabled.
The local events are:
* :monitoring-event:`PY_START`
* :monitoring-event:`PY_RESUME`
* :monitoring-event:`PY_RETURN`
* :monitoring-event:`PY_YIELD`
* :monitoring-event:`CALL`
* :monitoring-event:`LINE`
* :monitoring-event:`INSTRUCTION`
* :monitoring-event:`JUMP`
* :monitoring-event:`BRANCH`
* :monitoring-event:`STOP_ITERATION`
Ancillary events
''''''''''''''''
Ancillary events can be monitored like other events, but are controlled
by another event:
* :monitoring-event:`C_RAISE`
* :monitoring-event:`C_RETURN`
The :monitoring-event:`C_RETURN` and :monitoring-event:`C_RAISE` events
are controlled by the :monitoring-event:`CALL` event.
:monitoring-event:`C_RETURN` and :monitoring-event:`C_RAISE` events will only be seen if the
corresponding :monitoring-event:`CALL` event is being monitored.
Other events
''''''''''''
Other events are not necessarily tied to a specific location in the
program and cannot be individually disabled.
The other events that can be monitored are:
* :monitoring-event:`PY_THROW`
* :monitoring-event:`PY_UNWIND`
* :monitoring-event:`RAISE`
* :monitoring-event:`EXCEPTION_HANDLED`
The STOP_ITERATION event
''''''''''''''''''''''''
:pep:`PEP 380 <380#use-of-stopiteration-to-return-values>`
specifies that a :exc:`StopIteration` exception is raised when returning a value
from a generator or coroutine. However, this is a very inefficient way to
return a value, so some Python implementations, notably CPython 3.12+, do not
raise an exception unless it would be visible to other code.
To allow tools to monitor for real exceptions without slowing down generators
and coroutines, the :monitoring-event:`STOP_ITERATION` event is provided.
:monitoring-event:`STOP_ITERATION` can be locally disabled, unlike :monitoring-event:`RAISE`.
Turning events on and off
-------------------------
In order to monitor an event, it must be turned on and a corresponding callback
must be registered.
Events can be turned on or off by setting the events either globally or
for a particular code object.
Setting events globally
'''''''''''''''''''''''
Events can be controlled globally by modifying the set of events being monitored.
.. function:: get_events(tool_id: int) -> int
Returns the ``int`` representing all the active events.
.. function:: set_events(tool_id: int, event_set: int)
Activates all events which are set in *event_set*.
Raises a :exc:`ValueError` if *tool_id* is not in use.
No events are active by default.
Per code object events
''''''''''''''''''''''
Events can also be controlled on a per code object basis.
.. function:: get_local_events(tool_id: int, code: CodeType) -> int
Returns all the local events for *code*
.. function:: set_local_events(tool_id: int, code: CodeType, event_set: int)
Activates all the local events for *code* which are set in *event_set*.
Raises a :exc:`ValueError` if *tool_id* is not in use.
Local events add to global events, but do not mask them.
In other words, all global events will trigger for a code object,
regardless of the local events.
Disabling events
''''''''''''''''
.. data:: DISABLE
A special value that can be returned from a callback function to disable
events for the current code location.
Local events can be disabled for a specific code location by returning
:data:`sys.monitoring.DISABLE` from a callback function. This does not change
which events are set, or any other code locations for the same event.
Disabling events for specific locations is very important for high
performance monitoring. For example, a program can be run under a
debugger with no overhead if the debugger disables all monitoring
except for a few breakpoints.
.. _callbacks:
Registering callback functions
------------------------------
To register a callable for events call
.. function:: register_callback(tool_id: int, event: int, func: Callable | None) -> Callable | None
Registers the callable *func* for the *event* with the given *tool_id*
If another callback was registered for the given *tool_id* and *event*,
it is unregistered and returned.
Otherwise :func:`register_callback` returns ``None``.
Functions can be unregistered by calling
``sys.monitoring.register_callback(tool_id, event, None)``.
Callback functions can be registered and unregistered at any time.
Registering or unregistering a callback function will generate a :func:`sys.audit` event.
Callback function arguments
'''''''''''''''''''''''''''
.. data:: MISSING
A special value that is passed to a callback function to indicate
that there are no arguments to the call.
When an active event occurs, the registered callback function is called.
Different events will provide the callback function with different arguments, as follows:
* :monitoring-event:`PY_START` and :monitoring-event:`PY_RESUME`::
func(code: CodeType, instruction_offset: int) -> DISABLE | Any
* :monitoring-event:`PY_RETURN` and :monitoring-event:`PY_YIELD`::
func(code: CodeType, instruction_offset: int, retval: object) -> DISABLE | Any
* :monitoring-event:`CALL`, :monitoring-event:`C_RAISE` and :monitoring-event:`C_RETURN`::
func(code: CodeType, instruction_offset: int, callable: object, arg0: object | MISSING) -> DISABLE | Any
If there are no arguments, *arg0* is set to :data:`sys.monitoring.MISSING`.
* :monitoring-event:`RAISE`, :monitoring-event:`RERAISE`, :monitoring-event:`EXCEPTION_HANDLED`,
:monitoring-event:`PY_UNWIND`, :monitoring-event:`PY_THROW` and :monitoring-event:`STOP_ITERATION`::
func(code: CodeType, instruction_offset: int, exception: BaseException) -> DISABLE | Any
* :monitoring-event:`LINE`::
func(code: CodeType, line_number: int) -> DISABLE | Any
* :monitoring-event:`BRANCH` and :monitoring-event:`JUMP`::
func(code: CodeType, instruction_offset: int, destination_offset: int) -> DISABLE | Any
Note that the *destination_offset* is where the code will next execute.
For an untaken branch this will be the offset of the instruction following
the branch.
* :monitoring-event:`INSTRUCTION`::
func(code: CodeType, instruction_offset: int) -> DISABLE | Any
|