Monitoring C API

Added in version 3.13.

An extension may need to interact with the event monitoring system. Subscribingto events and registering callbacks can be done via the Python API exposed insys.monitoring.

Generating Execution Events

The functions below make it possible for an extension to fire monitoringevents as it emulates the execution of Python code. Each of these functionsaccepts aPyMonitoringState struct which contains concise informationabout the activation state of events, as well as the event arguments, whichinclude aPyObject* representing the code object, the instruction offsetand sometimes additional, event-specific arguments (seesys.monitoringfor details about the signatures of the different event callbacks).Thecodelike argument should be an instance oftypes.CodeTypeor of a type that emulates it.

The VM disables tracing when firing an event, so there is no need for usercode to do that.

Monitoring functions should not be called with an exception set,except those listed below as working with the current exception.

typePyMonitoringState

Representation of the state of an event type. It is allocated by the userwhile its contents are maintained by the monitoring API functions described below.

All of the functions below return 0 on success and -1 (with an exception set) on error.

Seesys.monitoring for descriptions of the events.

intPyMonitoring_FirePyStartEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aPY_START event.

intPyMonitoring_FirePyResumeEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aPY_RESUME event.

intPyMonitoring_FirePyReturnEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*retval)

Fire aPY_RETURN event.

intPyMonitoring_FirePyYieldEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*retval)

Fire aPY_YIELD event.

intPyMonitoring_FireCallEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*callable,PyObject*arg0)

Fire aCALL event.

intPyMonitoring_FireLineEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,intlineno)

Fire aLINE event.

intPyMonitoring_FireJumpEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*target_offset)

Fire aJUMP event.

intPyMonitoring_FireBranchEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*target_offset)

Fire aBRANCH event.

intPyMonitoring_FireCReturnEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*retval)

Fire aC_RETURN event.

intPyMonitoring_FirePyThrowEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aPY_THROW event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FireRaiseEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aRAISE event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FireCRaiseEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aC_RAISE event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FireReraiseEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aRERAISE event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FireExceptionHandledEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire anEXCEPTION_HANDLED event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FirePyUnwindEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset)

Fire aPY_UNWIND event with the current exception (as returned byPyErr_GetRaisedException()).

intPyMonitoring_FireStopIterationEvent(PyMonitoringState*state,PyObject*codelike,int32_toffset,PyObject*value)

Fire aSTOP_ITERATION event. Ifvalue is an instance ofStopIteration, it is used. Otherwise,a newStopIteration instance is created withvalue as its argument.

Managing the Monitoring State

Monitoring states can be managed with the help of monitoring scopes. A scopewould typically correspond to a python function.

intPyMonitoring_EnterScope(PyMonitoringState*state_array,uint64_t*version,constuint8_t*event_types,Py_ssize_tlength)

Enter a monitored scope.event_types is an array of the event IDs forevents that may be fired from the scope. For example, the ID of aPY_STARTevent is the valuePY_MONITORING_EVENT_PY_START, which is numerically equalto the base-2 logarithm ofsys.monitoring.events.PY_START.state_array is an array with a monitoring state entry for each event inevent_types, it is allocated by the user but populated byPyMonitoring_EnterScope() with information about the activation state ofthe event. The size ofevent_types (and hence also ofstate_array)is given inlength.

Theversion argument is a pointer to a value which should be allocatedby the user together withstate_array and initialized to 0,and then set only byPyMonitoring_EnterScope() itself. It allows thisfunction to determine whether event states have changed since the previous call,and to return quickly if they have not.

The scopes referred to here are lexical scopes: a function, class or method.PyMonitoring_EnterScope() should be called whenever the lexical scope isentered. Scopes can be reentered, reusing the samestate_array andversion,in situations like when emulating a recursive Python function. When a code-like’sexecution is paused, such as when emulating a generator, the scope needs tobe exited and re-entered.

The macros forevent_types are:

Macro

Event

PY_MONITORING_EVENT_BRANCH

BRANCH

PY_MONITORING_EVENT_CALL

CALL

PY_MONITORING_EVENT_C_RAISE

C_RAISE

PY_MONITORING_EVENT_C_RETURN

C_RETURN

PY_MONITORING_EVENT_EXCEPTION_HANDLED

EXCEPTION_HANDLED

PY_MONITORING_EVENT_INSTRUCTION

INSTRUCTION

PY_MONITORING_EVENT_JUMP

JUMP

PY_MONITORING_EVENT_LINE

LINE

PY_MONITORING_EVENT_PY_RESUME

PY_RESUME

PY_MONITORING_EVENT_PY_RETURN

PY_RETURN

PY_MONITORING_EVENT_PY_START

PY_START

PY_MONITORING_EVENT_PY_THROW

PY_THROW

PY_MONITORING_EVENT_PY_UNWIND

PY_UNWIND

PY_MONITORING_EVENT_PY_YIELD

PY_YIELD

PY_MONITORING_EVENT_RAISE

RAISE

PY_MONITORING_EVENT_RERAISE

RERAISE

PY_MONITORING_EVENT_STOP_ITERATION

STOP_ITERATION

intPyMonitoring_ExitScope(void)

Exit the last scope that was entered withPyMonitoring_EnterScope().

intPY_MONITORING_IS_INSTRUMENTED_EVENT(uint8_tev)

Return true if the event corresponding to the event IDev isalocal event.

Added in version 3.13.

Deprecated since version 3.13.3:This function issoft deprecated.