GH-93503: Add thread-specific APIs to set profiling and tracing functions in the C-API (#93504)

* gh-93503: Add APIs to set profiling and tracing functions in all threads in the C-API

* Use a separate API

* Fix NEWS entry

* Add locks around the loop

* Document ignoring exceptions

* Use the new APIs in the sys module

* Update docs
This commit is contained in:
Pablo Galindo Salgado 2022-08-24 23:21:39 +01:00 committed by GitHub
parent 657976ad95
commit e34c82abeb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 271 additions and 4 deletions

View file

@ -1021,6 +1021,36 @@ Set the global debug tracing function. It will be called on each\n\
function call. See the debugger chapter in the library manual."
);
/*[clinic input]
sys._settraceallthreads
arg: object
/
Set the global debug tracing function in all running threads belonging to the current interpreter.
It will be called on each function call. See the debugger chapter
in the library manual.
[clinic start generated code]*/
static PyObject *
sys__settraceallthreads(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=161cca30207bf3ca input=5906aa1485a50289]*/
{
PyObject* argument = NULL;
Py_tracefunc func = NULL;
if (arg != Py_None) {
func = trace_trampoline;
argument = arg;
}
PyEval_SetTraceAllThreads(func, argument);
Py_RETURN_NONE;
}
/*[clinic input]
sys.gettrace
@ -1066,6 +1096,35 @@ Set the profiling function. It will be called on each function call\n\
and return. See the profiler chapter in the library manual."
);
/*[clinic input]
sys._setprofileallthreads
arg: object
/
Set the profiling function in all running threads belonging to the current interpreter.
It will be called on each function call and return. See the profiler chapter
in the library manual.
[clinic start generated code]*/
static PyObject *
sys__setprofileallthreads(PyObject *module, PyObject *arg)
/*[clinic end generated code: output=2d61319e27b309fe input=d1a356d3f4f9060a]*/
{
PyObject* argument = NULL;
Py_tracefunc func = NULL;
if (arg != Py_None) {
func = profile_trampoline;
argument = arg;
}
PyEval_SetProfileAllThreads(func, argument);
Py_RETURN_NONE;
}
/*[clinic input]
sys.getprofile
@ -2035,9 +2094,11 @@ static PyMethodDef sys_methods[] = {
SYS_GETSWITCHINTERVAL_METHODDEF
SYS_SETDLOPENFLAGS_METHODDEF
{"setprofile", sys_setprofile, METH_O, setprofile_doc},
SYS__SETPROFILEALLTHREADS_METHODDEF
SYS_GETPROFILE_METHODDEF
SYS_SETRECURSIONLIMIT_METHODDEF
{"settrace", sys_settrace, METH_O, settrace_doc},
SYS__SETTRACEALLTHREADS_METHODDEF
SYS_GETTRACE_METHODDEF
SYS_CALL_TRACING_METHODDEF
SYS__DEBUGMALLOCSTATS_METHODDEF