Coverage Report

Created: 2025-08-26 06:26

/src/cpython/Python/legacy_tracing.c
Line
Count
Source (jump to first uncovered line)
1
/* Support for legacy tracing on top of PEP 669 instrumentation
2
 * Provides callables to forward PEP 669 events to legacy events.
3
 */
4
5
#include "Python.h"
6
#include "pycore_audit.h"         // _PySys_Audit()
7
#include "pycore_ceval.h"         // export _PyEval_SetProfile()
8
#include "pycore_frame.h"         // PyFrameObject members
9
#include "pycore_interpframe.h"   // _PyFrame_GetCode()
10
11
#include "opcode.h"
12
#include <stddef.h>
13
14
15
typedef struct _PyLegacyEventHandler {
16
    PyObject_HEAD
17
    vectorcallfunc vectorcall;
18
    int event;
19
} _PyLegacyEventHandler;
20
21
0
#define _PyLegacyEventHandler_CAST(op)  ((_PyLegacyEventHandler *)(op))
22
23
/* The Py_tracefunc function expects the following arguments:
24
 *   obj: the trace object (PyObject *)
25
 *   frame: the current frame (PyFrameObject *)
26
 *   kind: the kind of event, see PyTrace_XXX #defines (int)
27
 *   arg: The arg (a PyObject *)
28
 */
29
30
static PyObject *
31
call_profile_func(_PyLegacyEventHandler *self, PyObject *arg)
32
0
{
33
0
    PyThreadState *tstate = _PyThreadState_GET();
34
0
    if (tstate->c_profilefunc == NULL) {
35
0
        Py_RETURN_NONE;
36
0
    }
37
0
    PyFrameObject *frame = PyEval_GetFrame();
38
0
    if (frame == NULL) {
39
0
        PyErr_SetString(PyExc_SystemError,
40
0
                        "Missing frame when calling profile function.");
41
0
        return NULL;
42
0
    }
43
0
    Py_INCREF(frame);
44
0
    int err = tstate->c_profilefunc(tstate->c_profileobj, frame, self->event, arg);
45
0
    Py_DECREF(frame);
46
0
    if (err) {
47
0
        return NULL;
48
0
    }
49
0
    Py_RETURN_NONE;
50
0
}
51
52
static PyObject *
53
sys_profile_start(
54
    PyObject *callable, PyObject *const *args,
55
    size_t nargsf, PyObject *kwnames
56
0
) {
57
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
58
0
    assert(kwnames == NULL);
59
0
    assert(PyVectorcall_NARGS(nargsf) == 2);
60
0
    return call_profile_func(self, Py_None);
61
0
}
62
63
static PyObject *
64
sys_profile_throw(
65
    PyObject *callable, PyObject *const *args,
66
    size_t nargsf, PyObject *kwnames
67
0
) {
68
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
69
0
    assert(kwnames == NULL);
70
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
71
0
    return call_profile_func(self, Py_None);
72
0
}
73
74
static PyObject *
75
sys_profile_return(
76
    PyObject *callable, PyObject *const *args,
77
    size_t nargsf, PyObject *kwnames
78
0
) {
79
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
80
0
    assert(kwnames == NULL);
81
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
82
0
    return call_profile_func(self, args[2]);
83
0
}
84
85
static PyObject *
86
sys_profile_unwind(
87
    PyObject *callable, PyObject *const *args,
88
    size_t nargsf, PyObject *kwnames
89
0
) {
90
0
     _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
91
0
    assert(kwnames == NULL);
92
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
93
0
   return call_profile_func(self, NULL);
94
0
}
95
96
static PyObject *
97
sys_profile_call_or_return(
98
    PyObject *op, PyObject *const *args,
99
    size_t nargsf, PyObject *kwnames
100
0
) {
101
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(op);
102
0
    assert(kwnames == NULL);
103
0
    assert(PyVectorcall_NARGS(nargsf) == 4);
104
0
    PyObject *callable = args[2];
105
0
    if (PyCFunction_Check(callable)) {
106
0
        return call_profile_func(self, callable);
107
0
    }
108
0
    if (Py_TYPE(callable) == &PyMethodDescr_Type) {
109
0
        PyObject *self_arg = args[3];
110
        /* For backwards compatibility need to
111
         * convert to builtin method */
112
113
        /* If no arg, skip */
114
0
        if (self_arg == &_PyInstrumentation_MISSING) {
115
0
            Py_RETURN_NONE;
116
0
        }
117
0
        PyObject *meth = Py_TYPE(callable)->tp_descr_get(
118
0
            callable, self_arg, (PyObject*)Py_TYPE(self_arg));
119
0
        if (meth == NULL) {
120
0
            return NULL;
121
0
        }
122
0
        PyObject *res =  call_profile_func(self, meth);
123
0
        Py_DECREF(meth);
124
0
        return res;
125
0
    }
126
0
    Py_RETURN_NONE;
127
0
}
128
129
static int
130
set_opcode_trace_world_stopped(PyCodeObject *code, bool enable)
131
0
{
132
0
    _PyMonitoringEventSet events = 0;
133
0
    if (_PyMonitoring_GetLocalEvents(code, PY_MONITORING_SYS_TRACE_ID, &events) < 0) {
134
0
        return -1;
135
0
    }
136
137
0
    if (enable) {
138
0
        if (events & (1 << PY_MONITORING_EVENT_INSTRUCTION)) {
139
0
            return 0;
140
0
        }
141
0
        events |= (1 << PY_MONITORING_EVENT_INSTRUCTION);
142
0
    } else {
143
0
        if (!(events & (1 << PY_MONITORING_EVENT_INSTRUCTION))) {
144
0
            return 0;
145
0
        }
146
0
        events &= (~(1 << PY_MONITORING_EVENT_INSTRUCTION));
147
0
    }
148
0
    return _PyMonitoring_SetLocalEvents(code, PY_MONITORING_SYS_TRACE_ID, events);
149
0
}
150
151
int
152
_PyEval_SetOpcodeTrace(PyFrameObject *frame, bool enable)
153
0
{
154
0
    assert(frame != NULL);
155
156
0
    PyCodeObject *code = _PyFrame_GetCode(frame->f_frame);
157
158
#ifdef Py_GIL_DISABLED
159
    // First check if a change is necessary outside of the stop-the-world pause
160
    _PyMonitoringEventSet events = 0;
161
    if (_PyMonitoring_GetLocalEvents(code, PY_MONITORING_SYS_TRACE_ID, &events) < 0) {
162
        return -1;
163
    }
164
    int is_enabled = (events & (1 << PY_MONITORING_EVENT_INSTRUCTION)) != 0;
165
    if (is_enabled == enable) {
166
        return 0;  // No change needed
167
    }
168
#endif
169
170
0
    PyInterpreterState *interp = _PyInterpreterState_GET();
171
0
    _PyEval_StopTheWorld(interp);
172
0
    int res = set_opcode_trace_world_stopped(code, enable);
173
0
    _PyEval_StartTheWorld(interp);
174
0
    return res;
175
0
}
176
177
static PyObject *
178
call_trace_func(_PyLegacyEventHandler *self, PyObject *arg)
179
0
{
180
0
    PyThreadState *tstate = _PyThreadState_GET();
181
0
    if (tstate->c_tracefunc == NULL) {
182
0
        Py_RETURN_NONE;
183
0
    }
184
0
    PyFrameObject *frame = PyEval_GetFrame();
185
0
    if (frame == NULL) {
186
0
        PyErr_SetString(PyExc_SystemError,
187
0
                        "Missing frame when calling trace function.");
188
0
        return NULL;
189
0
    }
190
0
    if (frame->f_trace_opcodes) {
191
0
        if (_PyEval_SetOpcodeTrace(frame, true) != 0) {
192
0
            return NULL;
193
0
        }
194
0
    }
195
196
0
    Py_INCREF(frame);
197
0
    int err = tstate->c_tracefunc(tstate->c_traceobj, frame, self->event, arg);
198
0
    frame->f_lineno = 0;
199
0
    Py_DECREF(frame);
200
0
    if (err) {
201
0
        return NULL;
202
0
    }
203
0
    Py_RETURN_NONE;
204
0
}
205
206
static PyObject *
207
sys_trace_exception_func(
208
    PyObject *callable, PyObject *const *args,
209
    size_t nargsf, PyObject *kwnames
210
0
) {
211
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
212
0
    assert(kwnames == NULL);
213
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
214
0
    PyObject *exc = args[2];
215
0
    assert(PyExceptionInstance_Check(exc));
216
0
    PyObject *type = (PyObject *)Py_TYPE(exc);
217
0
    PyObject *tb = PyException_GetTraceback(exc);
218
0
    if (tb == NULL) {
219
0
        tb = Py_NewRef(Py_None);
220
0
    }
221
0
    PyObject *tuple = PyTuple_Pack(3, type, exc, tb);
222
0
    Py_DECREF(tb);
223
0
    if (tuple == NULL) {
224
0
        return NULL;
225
0
    }
226
0
    PyObject *res = call_trace_func(self, tuple);
227
0
    Py_DECREF(tuple);
228
0
    return res;
229
0
}
230
231
static PyObject *
232
sys_trace_start(
233
    PyObject *callable, PyObject *const *args,
234
    size_t nargsf, PyObject *kwnames
235
0
) {
236
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
237
0
    assert(kwnames == NULL);
238
0
    assert(PyVectorcall_NARGS(nargsf) == 2);
239
0
    return call_trace_func(self, Py_None);
240
0
}
241
242
static PyObject *
243
sys_trace_throw(
244
    PyObject *callable, PyObject *const *args,
245
    size_t nargsf, PyObject *kwnames
246
0
) {
247
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
248
0
    assert(kwnames == NULL);
249
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
250
0
    return call_trace_func(self, Py_None);
251
0
}
252
253
static PyObject *
254
sys_trace_unwind(
255
    PyObject *callable, PyObject *const *args,
256
    size_t nargsf, PyObject *kwnames
257
0
) {
258
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
259
0
    assert(kwnames == NULL);
260
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
261
0
    return call_trace_func(self, NULL);
262
0
}
263
264
static PyObject *
265
sys_trace_return(
266
    PyObject *callable, PyObject *const *args,
267
    size_t nargsf, PyObject *kwnames
268
0
) {
269
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
270
0
    assert(!PyErr_Occurred());
271
0
    assert(kwnames == NULL);
272
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
273
0
    assert(PyCode_Check(args[0]));
274
0
    PyObject *val = args[2];
275
0
    PyObject *res = call_trace_func(self, val);
276
0
    return res;
277
0
}
278
279
static PyObject *
280
sys_trace_yield(
281
    PyObject *callable, PyObject *const *args,
282
    size_t nargsf, PyObject *kwnames
283
0
) {
284
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
285
0
    assert(kwnames == NULL);
286
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
287
0
    return call_trace_func(self, args[2]);
288
0
}
289
290
static PyObject *
291
sys_trace_instruction_func(
292
    PyObject *callable, PyObject *const *args,
293
    size_t nargsf, PyObject *kwnames
294
0
) {
295
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
296
0
    assert(kwnames == NULL);
297
0
    assert(PyVectorcall_NARGS(nargsf) == 2);
298
0
    PyFrameObject *frame = PyEval_GetFrame();
299
0
    if (frame == NULL) {
300
0
        PyErr_SetString(PyExc_SystemError,
301
0
                        "Missing frame when calling trace function.");
302
0
        return NULL;
303
0
    }
304
0
    PyThreadState *tstate = _PyThreadState_GET();
305
0
    if (!tstate->c_tracefunc || !frame->f_trace_opcodes) {
306
0
        if (_PyEval_SetOpcodeTrace(frame, false) != 0) {
307
0
            return NULL;
308
0
        }
309
0
        Py_RETURN_NONE;
310
0
    }
311
0
    Py_INCREF(frame);
312
0
    int err = tstate->c_tracefunc(tstate->c_traceobj, frame, self->event, Py_None);
313
0
    frame->f_lineno = 0;
314
0
    Py_DECREF(frame);
315
0
    if (err) {
316
0
        return NULL;
317
0
    }
318
0
    Py_RETURN_NONE;
319
0
}
320
321
static PyObject *
322
trace_line(
323
    PyThreadState *tstate, _PyLegacyEventHandler *self,
324
    PyFrameObject *frame, int line
325
0
) {
326
0
    if (!frame->f_trace_lines) {
327
0
        Py_RETURN_NONE;
328
0
    }
329
0
    if (line < 0) {
330
0
        Py_RETURN_NONE;
331
0
    }
332
0
    Py_INCREF(frame);
333
0
    frame->f_lineno = line;
334
0
    int err = tstate->c_tracefunc(tstate->c_traceobj, frame, self->event, Py_None);
335
0
    frame->f_lineno = 0;
336
0
    Py_DECREF(frame);
337
0
    if (err) {
338
0
        return NULL;
339
0
    }
340
0
    Py_RETURN_NONE;
341
0
}
342
343
static PyObject *
344
sys_trace_line_func(
345
    PyObject *callable, PyObject *const *args,
346
    size_t nargsf, PyObject *kwnames
347
0
) {
348
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
349
0
    assert(kwnames == NULL);
350
0
    PyThreadState *tstate = _PyThreadState_GET();
351
0
    if (tstate->c_tracefunc == NULL) {
352
0
        Py_RETURN_NONE;
353
0
    }
354
0
    assert(PyVectorcall_NARGS(nargsf) == 2);
355
0
    int line = PyLong_AsInt(args[1]);
356
0
    assert(line >= 0);
357
0
    PyFrameObject *frame = PyEval_GetFrame();
358
0
    if (frame == NULL) {
359
0
        PyErr_SetString(PyExc_SystemError,
360
0
                        "Missing frame when calling trace function.");
361
0
        return NULL;
362
0
    }
363
0
    assert(args[0] == (PyObject *)_PyFrame_GetCode(frame->f_frame));
364
0
    return trace_line(tstate, self, frame, line);
365
0
}
366
367
/* sys.settrace generates line events for all backward
368
 * edges, even if on the same line.
369
 * Handle that case here */
370
static PyObject *
371
sys_trace_jump_func(
372
    PyObject *callable, PyObject *const *args,
373
    size_t nargsf, PyObject *kwnames
374
0
) {
375
0
    _PyLegacyEventHandler *self = _PyLegacyEventHandler_CAST(callable);
376
0
    assert(kwnames == NULL);
377
0
    PyThreadState *tstate = _PyThreadState_GET();
378
0
    if (tstate->c_tracefunc == NULL) {
379
0
        Py_RETURN_NONE;
380
0
    }
381
0
    assert(PyVectorcall_NARGS(nargsf) == 3);
382
0
    int from = PyLong_AsInt(args[1])/sizeof(_Py_CODEUNIT);
383
0
    assert(from >= 0);
384
0
    int to = PyLong_AsInt(args[2])/sizeof(_Py_CODEUNIT);
385
0
    assert(to >= 0);
386
0
    if (to > from) {
387
        /* Forward jump */
388
0
        return &_PyInstrumentation_DISABLE;
389
0
    }
390
0
    PyCodeObject *code = (PyCodeObject *)args[0];
391
0
    assert(PyCode_Check(code));
392
    /* We can call _Py_Instrumentation_GetLine because we always set
393
    * line events for tracing */
394
0
    int to_line = _Py_Instrumentation_GetLine(code, to);
395
0
    int from_line = _Py_Instrumentation_GetLine(code, from);
396
0
    if (to_line != from_line) {
397
        /* Will be handled by target INSTRUMENTED_LINE */
398
0
        return &_PyInstrumentation_DISABLE;
399
0
    }
400
0
    PyFrameObject *frame = PyEval_GetFrame();
401
0
    if (frame == NULL) {
402
0
        PyErr_SetString(PyExc_SystemError,
403
0
                        "Missing frame when calling trace function.");
404
0
        return NULL;
405
0
    }
406
0
    if (!frame->f_trace_lines) {
407
0
        Py_RETURN_NONE;
408
0
    }
409
0
    return trace_line(tstate, self, frame, to_line);
410
0
}
411
412
PyTypeObject _PyLegacyEventHandler_Type = {
413
    PyVarObject_HEAD_INIT(&PyType_Type, 0)
414
    "sys.legacy_event_handler",
415
    sizeof(_PyLegacyEventHandler),
416
    .tp_vectorcall_offset = offsetof(_PyLegacyEventHandler, vectorcall),
417
    .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
418
        Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_DISALLOW_INSTANTIATION,
419
    .tp_call = PyVectorcall_Call,
420
};
421
422
static int
423
set_callbacks(int tool, vectorcallfunc vectorcall, int legacy_event, int event1, int event2)
424
0
{
425
0
    _PyLegacyEventHandler *callback =
426
0
        PyObject_NEW(_PyLegacyEventHandler, &_PyLegacyEventHandler_Type);
427
0
    if (callback == NULL) {
428
0
        return -1;
429
0
    }
430
0
    callback->vectorcall = vectorcall;
431
0
    callback->event = legacy_event;
432
0
    Py_XDECREF(_PyMonitoring_RegisterCallback(tool, event1, (PyObject *)callback));
433
0
    if (event2 >= 0) {
434
0
        Py_XDECREF(_PyMonitoring_RegisterCallback(tool, event2, (PyObject *)callback));
435
0
    }
436
0
    Py_DECREF(callback);
437
0
    return 0;
438
0
}
439
440
#ifndef NDEBUG
441
/* Ensure that tstate is valid: sanity check for PyEval_AcquireThread() and
442
   PyEval_RestoreThread(). Detect if tstate memory was freed. It can happen
443
   when a thread continues to run after Python finalization, especially
444
   daemon threads. */
445
static int
446
is_tstate_valid(PyThreadState *tstate)
447
{
448
    assert(!_PyMem_IsPtrFreed(tstate));
449
    assert(!_PyMem_IsPtrFreed(tstate->interp));
450
    return 1;
451
}
452
#endif
453
454
static int
455
setup_profile_callbacks(void *Py_UNUSED(arg))
456
0
{
457
    /* Setup PEP 669 monitoring callbacks and events. */
458
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
459
0
                      sys_profile_start, PyTrace_CALL,
460
0
                      PY_MONITORING_EVENT_PY_START,
461
0
                      PY_MONITORING_EVENT_PY_RESUME)) {
462
0
        return -1;
463
0
    }
464
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
465
0
                      sys_profile_throw, PyTrace_CALL,
466
0
                      PY_MONITORING_EVENT_PY_THROW, -1)) {
467
0
        return -1;
468
0
    }
469
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
470
0
                      sys_profile_return, PyTrace_RETURN,
471
0
                      PY_MONITORING_EVENT_PY_RETURN,
472
0
                      PY_MONITORING_EVENT_PY_YIELD)) {
473
0
        return -1;
474
0
    }
475
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
476
0
                      sys_profile_unwind, PyTrace_RETURN,
477
0
                      PY_MONITORING_EVENT_PY_UNWIND, -1)) {
478
0
        return -1;
479
0
    }
480
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
481
0
                      sys_profile_call_or_return, PyTrace_C_CALL,
482
0
                      PY_MONITORING_EVENT_CALL, -1)) {
483
0
        return -1;
484
0
    }
485
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
486
0
                      sys_profile_call_or_return, PyTrace_C_RETURN,
487
0
                      PY_MONITORING_EVENT_C_RETURN, -1)) {
488
0
        return -1;
489
0
    }
490
0
    if (set_callbacks(PY_MONITORING_SYS_PROFILE_ID,
491
0
                      sys_profile_call_or_return, PyTrace_C_EXCEPTION,
492
0
                      PY_MONITORING_EVENT_C_RAISE, -1)) {
493
0
        return -1;
494
0
    }
495
0
    return 0;
496
0
}
497
498
static PyObject *
499
swap_profile_func_arg(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
500
0
{
501
0
    int delta = (func != NULL) - (tstate->c_profilefunc != NULL);
502
0
    tstate->c_profilefunc = func;
503
0
    PyObject *old_profileobj = tstate->c_profileobj;
504
0
    tstate->c_profileobj = Py_XNewRef(arg);
505
0
    tstate->interp->sys_profiling_threads += delta;
506
0
    assert(tstate->interp->sys_profiling_threads >= 0);
507
0
    return old_profileobj;
508
0
}
509
510
static int
511
set_monitoring_profile_events(PyInterpreterState *interp)
512
0
{
513
0
    uint32_t events = 0;
514
0
    if (interp->sys_profiling_threads) {
515
0
        events =
516
0
            (1 << PY_MONITORING_EVENT_PY_START) | (1 << PY_MONITORING_EVENT_PY_RESUME) |
517
0
            (1 << PY_MONITORING_EVENT_PY_RETURN) | (1 << PY_MONITORING_EVENT_PY_YIELD) |
518
0
            (1 << PY_MONITORING_EVENT_CALL) | (1 << PY_MONITORING_EVENT_PY_UNWIND) |
519
0
            (1 << PY_MONITORING_EVENT_PY_THROW);
520
0
    }
521
0
    return _PyMonitoring_SetEvents(PY_MONITORING_SYS_PROFILE_ID, events);
522
0
}
523
524
int
525
_PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
526
0
{
527
0
    assert(is_tstate_valid(tstate));
528
    /* The caller must hold a thread state */
529
0
    _Py_AssertHoldsTstate();
530
531
    /* Call _PySys_Audit() in the context of the current thread state,
532
       even if tstate is not the current thread state. */
533
0
    PyThreadState *current_tstate = _PyThreadState_GET();
534
0
    if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
535
0
        return -1;
536
0
    }
537
538
0
    PyInterpreterState *interp = tstate->interp;
539
0
    if (_PyOnceFlag_CallOnce(&interp->sys_profile_once_flag,
540
0
                             setup_profile_callbacks, NULL) < 0) {
541
0
        return -1;
542
0
    }
543
544
0
    _PyEval_StopTheWorld(interp);
545
0
    PyObject *old_profileobj = swap_profile_func_arg(tstate, func, arg);
546
0
    int ret = set_monitoring_profile_events(interp);
547
0
    _PyEval_StartTheWorld(interp);
548
0
    Py_XDECREF(old_profileobj);  // needs to be decref'd outside of stop-the-world
549
0
    return ret;
550
0
}
551
552
int
553
_PyEval_SetProfileAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg)
554
0
{
555
0
    PyThreadState *current_tstate = _PyThreadState_GET();
556
0
    assert(is_tstate_valid(current_tstate));
557
0
    assert(current_tstate->interp == interp);
558
559
0
    if (_PySys_Audit(current_tstate, "sys.setprofile", NULL) < 0) {
560
0
        return -1;
561
0
    }
562
563
0
    if (_PyOnceFlag_CallOnce(&interp->sys_profile_once_flag,
564
0
                             setup_profile_callbacks, NULL) < 0) {
565
0
        return -1;
566
0
    }
567
568
0
    PyObject *old_profileobjs = NULL;
569
0
    _PyEval_StopTheWorld(interp);
570
0
    HEAD_LOCK(&_PyRuntime);
571
0
    Py_ssize_t num_thread_states = 0;
572
0
    _Py_FOR_EACH_TSTATE_UNLOCKED(interp, p) {
573
0
        num_thread_states++;
574
0
    }
575
0
    old_profileobjs = PyTuple_New(num_thread_states);
576
0
    if (old_profileobjs == NULL) {
577
0
        HEAD_UNLOCK(&_PyRuntime);
578
0
        _PyEval_StartTheWorld(interp);
579
0
        return -1;
580
0
    }
581
0
    _Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
582
0
        PyObject *old = swap_profile_func_arg(tstate, func, arg);
583
0
        PyTuple_SET_ITEM(old_profileobjs, --num_thread_states, old);
584
0
    }
585
0
    HEAD_UNLOCK(&_PyRuntime);
586
0
    int ret = set_monitoring_profile_events(interp);
587
0
    _PyEval_StartTheWorld(interp);
588
0
    Py_XDECREF(old_profileobjs);  // needs to be decref'd outside of stop-the-world
589
0
    return ret;
590
0
}
591
592
static int
593
setup_trace_callbacks(void *Py_UNUSED(arg))
594
0
{
595
    /* Setup PEP 669 monitoring callbacks and events. */
596
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
597
0
                      sys_trace_start, PyTrace_CALL,
598
0
                      PY_MONITORING_EVENT_PY_START,
599
0
                      PY_MONITORING_EVENT_PY_RESUME)) {
600
0
        return -1;
601
0
    }
602
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
603
0
                      sys_trace_throw, PyTrace_CALL,
604
0
                      PY_MONITORING_EVENT_PY_THROW, -1)) {
605
0
        return -1;
606
0
    }
607
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
608
0
                      sys_trace_return, PyTrace_RETURN,
609
0
                      PY_MONITORING_EVENT_PY_RETURN, -1)) {
610
0
        return -1;
611
0
    }
612
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
613
0
                      sys_trace_yield, PyTrace_RETURN,
614
0
                      PY_MONITORING_EVENT_PY_YIELD, -1)) {
615
0
        return -1;
616
0
    }
617
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
618
0
                      sys_trace_exception_func, PyTrace_EXCEPTION,
619
0
                      PY_MONITORING_EVENT_RAISE,
620
0
                      PY_MONITORING_EVENT_STOP_ITERATION)) {
621
0
        return -1;
622
0
    }
623
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
624
0
                      sys_trace_line_func, PyTrace_LINE,
625
0
                      PY_MONITORING_EVENT_LINE, -1)) {
626
0
        return -1;
627
0
    }
628
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
629
0
                      sys_trace_unwind, PyTrace_RETURN,
630
0
                      PY_MONITORING_EVENT_PY_UNWIND, -1)) {
631
0
        return -1;
632
0
    }
633
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
634
0
                      sys_trace_jump_func, PyTrace_LINE,
635
0
                      PY_MONITORING_EVENT_JUMP, -1)) {
636
0
        return -1;
637
0
    }
638
0
    if (set_callbacks(PY_MONITORING_SYS_TRACE_ID,
639
0
                      sys_trace_instruction_func, PyTrace_OPCODE,
640
0
                      PY_MONITORING_EVENT_INSTRUCTION, -1)) {
641
0
        return -1;
642
0
    }
643
0
    return 0;
644
0
}
645
646
static PyObject *
647
swap_trace_func_arg(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
648
0
{
649
0
    int delta = (func != NULL) - (tstate->c_tracefunc != NULL);
650
0
    tstate->c_tracefunc = func;
651
0
    PyObject *old_traceobj = tstate->c_traceobj;
652
0
    tstate->c_traceobj = Py_XNewRef(arg);
653
0
    tstate->interp->sys_tracing_threads += delta;
654
0
    assert(tstate->interp->sys_tracing_threads >= 0);
655
0
    return old_traceobj;
656
0
}
657
658
static int
659
set_monitoring_trace_events(PyInterpreterState *interp)
660
0
{
661
0
    uint32_t events = 0;
662
0
    if (interp->sys_tracing_threads) {
663
0
        events =
664
0
            (1 << PY_MONITORING_EVENT_PY_START) | (1 << PY_MONITORING_EVENT_PY_RESUME) |
665
0
            (1 << PY_MONITORING_EVENT_PY_RETURN) | (1 << PY_MONITORING_EVENT_PY_YIELD) |
666
0
            (1 << PY_MONITORING_EVENT_RAISE) | (1 << PY_MONITORING_EVENT_LINE) |
667
0
            (1 << PY_MONITORING_EVENT_JUMP) |
668
0
            (1 << PY_MONITORING_EVENT_PY_UNWIND) | (1 << PY_MONITORING_EVENT_PY_THROW) |
669
0
            (1 << PY_MONITORING_EVENT_STOP_ITERATION);
670
0
    }
671
0
    return _PyMonitoring_SetEvents(PY_MONITORING_SYS_TRACE_ID, events);
672
0
}
673
674
// Enable opcode tracing for the thread's current frame if needed.
675
static int
676
maybe_set_opcode_trace(PyThreadState *tstate)
677
0
{
678
0
    _PyInterpreterFrame *iframe = tstate->current_frame;
679
0
    if (iframe == NULL) {
680
0
        return 0;
681
0
    }
682
0
    PyFrameObject *frame = iframe->frame_obj;
683
0
    if (frame == NULL || !frame->f_trace_opcodes) {
684
0
        return 0;
685
0
    }
686
0
    return set_opcode_trace_world_stopped(_PyFrame_GetCode(iframe), true);
687
0
}
688
689
int
690
_PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
691
0
{
692
0
    assert(is_tstate_valid(tstate));
693
    /* The caller must hold a thread state */
694
0
    _Py_AssertHoldsTstate();
695
696
    /* Call _PySys_Audit() in the context of the current thread state,
697
       even if tstate is not the current thread state. */
698
0
    PyThreadState *current_tstate = _PyThreadState_GET();
699
0
    if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
700
0
        return -1;
701
0
    }
702
703
0
    PyInterpreterState *interp = tstate->interp;
704
0
    if (_PyOnceFlag_CallOnce(&interp->sys_trace_once_flag,
705
0
                             setup_trace_callbacks, NULL) < 0) {
706
0
        return -1;
707
0
    }
708
709
0
    int err = 0;
710
0
    _PyEval_StopTheWorld(interp);
711
0
    PyObject *old_traceobj = swap_trace_func_arg(tstate, func, arg);
712
0
    err = set_monitoring_trace_events(interp);
713
0
    if (err != 0) {
714
0
        goto done;
715
0
    }
716
0
    if (interp->sys_tracing_threads) {
717
0
        err = maybe_set_opcode_trace(tstate);
718
0
    }
719
0
done:
720
0
    _PyEval_StartTheWorld(interp);
721
0
    Py_XDECREF(old_traceobj);  // needs to be decref'd outside stop-the-world
722
0
    return err;
723
0
}
724
725
int
726
_PyEval_SetTraceAllThreads(PyInterpreterState *interp, Py_tracefunc func, PyObject *arg)
727
0
{
728
0
    PyThreadState *current_tstate = _PyThreadState_GET();
729
0
    assert(is_tstate_valid(current_tstate));
730
0
    assert(current_tstate->interp == interp);
731
732
0
    if (_PySys_Audit(current_tstate, "sys.settrace", NULL) < 0) {
733
0
        return -1;
734
0
    }
735
736
0
    if (_PyOnceFlag_CallOnce(&interp->sys_trace_once_flag,
737
0
                             setup_trace_callbacks, NULL) < 0) {
738
0
        return -1;
739
0
    }
740
741
0
    PyObject *old_trace_objs = NULL;
742
0
    _PyEval_StopTheWorld(interp);
743
0
    HEAD_LOCK(&_PyRuntime);
744
0
    Py_ssize_t num_thread_states = 0;
745
0
    _Py_FOR_EACH_TSTATE_UNLOCKED(interp, p) {
746
0
        num_thread_states++;
747
0
    }
748
0
    old_trace_objs = PyTuple_New(num_thread_states);
749
0
    if (old_trace_objs == NULL) {
750
0
        HEAD_UNLOCK(&_PyRuntime);
751
0
        _PyEval_StartTheWorld(interp);
752
0
        return -1;
753
0
    }
754
0
    _Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
755
0
        PyObject *old = swap_trace_func_arg(tstate, func, arg);
756
0
        PyTuple_SET_ITEM(old_trace_objs, --num_thread_states, old);
757
0
    }
758
0
    if (interp->sys_tracing_threads) {
759
0
        _Py_FOR_EACH_TSTATE_UNLOCKED(interp, tstate) {
760
0
            int err = maybe_set_opcode_trace(tstate);
761
0
            if (err != 0) {
762
0
                HEAD_UNLOCK(&_PyRuntime);
763
0
                _PyEval_StartTheWorld(interp);
764
0
                Py_XDECREF(old_trace_objs);
765
0
                return -1;
766
0
            }
767
0
        }
768
0
    }
769
0
    HEAD_UNLOCK(&_PyRuntime);
770
0
    int err = set_monitoring_trace_events(interp);
771
0
    _PyEval_StartTheWorld(interp);
772
0
    Py_XDECREF(old_trace_objs);  // needs to be decref'd outside of stop-the-world
773
0
    return err;
774
0
}