Skip to content

Commit 099f756

Browse files
authored
bpo-45923: Decouple suspension of tracing from tracing flag. (GH-31908)
1 parent a8c728b commit 099f756

File tree

3 files changed

+29
-39
lines changed

3 files changed

+29
-39
lines changed

Include/internal/pycore_pystate.h

+1-6
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,9 @@ PyAPI_FUNC(void) _PyThreadState_DeleteExcept(
139139
_PyRuntimeState *runtime,
140140
PyThreadState *tstate);
141141

142-
static inline void
143-
_PyThreadState_PauseTracing(PyThreadState *tstate)
144-
{
145-
tstate->cframe->use_tracing = 0;
146-
}
147142

148143
static inline void
149-
_PyThreadState_ResumeTracing(PyThreadState *tstate)
144+
_PyThreadState_UpdateTracingState(PyThreadState *tstate)
150145
{
151146
int use_tracing = (tstate->c_tracefunc != NULL
152147
|| tstate->c_profilefunc != NULL);

Python/ceval.c

+28-16
Original file line numberDiff line numberDiff line change
@@ -5439,10 +5439,12 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
54395439
}
54405440

54415441
#if USE_COMPUTED_GOTOS
5442-
TARGET_DO_TRACING: {
5442+
TARGET_DO_TRACING:
54435443
#else
5444-
case DO_TRACING: {
5444+
case DO_TRACING:
54455445
#endif
5446+
{
5447+
if (tstate->tracing == 0) {
54465448
int instr_prev = skip_backwards_over_extended_args(frame->f_code, frame->f_lasti);
54475449
frame->f_lasti = INSTR_OFFSET();
54485450
TRACING_NEXTOPARG();
@@ -5482,11 +5484,11 @@ _PyEval_EvalFrameDefault(PyThreadState *tstate, _PyInterpreterFrame *frame, int
54825484
frame->stacktop = -1;
54835485
}
54845486
}
5485-
TRACING_NEXTOPARG();
5486-
PRE_DISPATCH_GOTO();
5487-
DISPATCH_GOTO();
54885487
}
5489-
5488+
TRACING_NEXTOPARG();
5489+
PRE_DISPATCH_GOTO();
5490+
DISPATCH_GOTO();
5491+
}
54905492

54915493
#if USE_COMPUTED_GOTOS
54925494
_unknown_opcode:
@@ -6673,27 +6675,38 @@ initialize_trace_info(PyTraceInfo *trace_info, _PyInterpreterFrame *frame)
66736675
}
66746676
}
66756677

6678+
void
6679+
PyThreadState_EnterTracing(PyThreadState *tstate)
6680+
{
6681+
tstate->tracing++;
6682+
}
6683+
6684+
void
6685+
PyThreadState_LeaveTracing(PyThreadState *tstate)
6686+
{
6687+
tstate->tracing--;
6688+
}
6689+
66766690
static int
66776691
call_trace(Py_tracefunc func, PyObject *obj,
66786692
PyThreadState *tstate, _PyInterpreterFrame *frame,
66796693
int what, PyObject *arg)
66806694
{
66816695
int result;
6682-
if (tstate->tracing)
6696+
if (tstate->tracing) {
66836697
return 0;
6684-
tstate->tracing++;
6685-
_PyThreadState_PauseTracing(tstate);
6698+
}
66866699
PyFrameObject *f = _PyFrame_GetFrameObject(frame);
66876700
if (f == NULL) {
66886701
return -1;
66896702
}
6703+
PyThreadState_EnterTracing(tstate);
66906704
assert (frame->f_lasti >= 0);
66916705
initialize_trace_info(&tstate->trace_info, frame);
66926706
f->f_lineno = _PyCode_CheckLineNumber(frame->f_lasti*sizeof(_Py_CODEUNIT), &tstate->trace_info.bounds);
66936707
result = func(obj, f, what, arg);
66946708
f->f_lineno = 0;
6695-
_PyThreadState_ResumeTracing(tstate);
6696-
tstate->tracing--;
6709+
PyThreadState_LeaveTracing(tstate);
66976710
return result;
66986711
}
66996712

@@ -6706,7 +6719,6 @@ _PyEval_CallTracing(PyObject *func, PyObject *args)
67066719
PyObject *result;
67076720

67086721
tstate->tracing = 0;
6709-
_PyThreadState_ResumeTracing(tstate);
67106722
result = PyObject_Call(func, args, NULL);
67116723
tstate->tracing = save_tracing;
67126724
tstate->cframe->use_tracing = save_use_tracing;
@@ -6773,15 +6785,15 @@ _PyEval_SetProfile(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
67736785
tstate->c_profilefunc = NULL;
67746786
tstate->c_profileobj = NULL;
67756787
/* Must make sure that tracing is not ignored if 'profileobj' is freed */
6776-
_PyThreadState_ResumeTracing(tstate);
6788+
_PyThreadState_UpdateTracingState(tstate);
67776789
Py_XDECREF(profileobj);
67786790

67796791
Py_XINCREF(arg);
67806792
tstate->c_profileobj = arg;
67816793
tstate->c_profilefunc = func;
67826794

67836795
/* Flag that tracing or profiling is turned on */
6784-
_PyThreadState_ResumeTracing(tstate);
6796+
_PyThreadState_UpdateTracingState(tstate);
67856797
return 0;
67866798
}
67876799

@@ -6814,15 +6826,15 @@ _PyEval_SetTrace(PyThreadState *tstate, Py_tracefunc func, PyObject *arg)
68146826
tstate->c_tracefunc = NULL;
68156827
tstate->c_traceobj = NULL;
68166828
/* Must make sure that profiling is not ignored if 'traceobj' is freed */
6817-
_PyThreadState_ResumeTracing(tstate);
6829+
_PyThreadState_UpdateTracingState(tstate);
68186830
Py_XDECREF(traceobj);
68196831

68206832
Py_XINCREF(arg);
68216833
tstate->c_traceobj = arg;
68226834
tstate->c_tracefunc = func;
68236835

68246836
/* Flag that tracing or profiling is turned on */
6825-
_PyThreadState_ResumeTracing(tstate);
6837+
_PyThreadState_UpdateTracingState(tstate);
68266838

68276839
return 0;
68286840
}

Python/pystate.c

-17
Original file line numberDiff line numberDiff line change
@@ -1333,23 +1333,6 @@ PyThreadState_SetAsyncExc(unsigned long id, PyObject *exc)
13331333
return 0;
13341334
}
13351335

1336-
1337-
void
1338-
PyThreadState_EnterTracing(PyThreadState *tstate)
1339-
{
1340-
tstate->tracing++;
1341-
_PyThreadState_PauseTracing(tstate);
1342-
}
1343-
1344-
void
1345-
PyThreadState_LeaveTracing(PyThreadState *tstate)
1346-
{
1347-
tstate->tracing--;
1348-
_PyThreadState_ResumeTracing(tstate);
1349-
}
1350-
1351-
1352-
13531336
/* Routines for advanced debuggers, requested by David Beazley.
13541337
Don't use unless you know what you are doing! */
13551338

0 commit comments

Comments
 (0)