bpo-43760: Add PyThreadState_EnterTracing() (GH-28542)

Add PyThreadState_EnterTracing() and PyThreadState_LeaveTracing()
functions to the limited C API to suspend and resume tracing and
profiling.

Add an unit test on the PyThreadState C API to _testcapi.

Add also internal _PyThreadState_DisableTracing() and
_PyThreadState_ResetTracing().
This commit is contained in:
Victor Stinner 2021-10-15 16:06:30 +02:00 committed by GitHub
parent 354c35220d
commit 547d26aa08
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 146 additions and 25 deletions

View file

@ -256,8 +256,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
}
/* Disallow tracing in hooks unless explicitly enabled */
ts->tracing++;
ts->cframe->use_tracing = 0;
PyThreadState_EnterTracing(ts);
while ((hook = PyIter_Next(hooks)) != NULL) {
_Py_IDENTIFIER(__cantrace__);
PyObject *o;
@ -270,14 +269,12 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
break;
}
if (canTrace) {
ts->cframe->use_tracing = (ts->c_tracefunc || ts->c_profilefunc) ? 255 : 0;
ts->tracing--;
PyThreadState_LeaveTracing(ts);
}
PyObject* args[2] = {eventName, eventArgs};
o = _PyObject_FastCallTstate(ts, hook, args, 2);
if (canTrace) {
ts->tracing++;
ts->cframe->use_tracing = 0;
PyThreadState_EnterTracing(ts);
}
if (!o) {
break;
@ -285,8 +282,7 @@ sys_audit_tstate(PyThreadState *ts, const char *event,
Py_DECREF(o);
Py_CLEAR(hook);
}
ts->cframe->use_tracing = (ts->c_tracefunc || ts->c_profilefunc) ? 255 : 0;
ts->tracing--;
PyThreadState_LeaveTracing(ts);
if (_PyErr_Occurred(ts)) {
goto exit;
}