Skip to content

gh-109094: remove unnecessary updates of frame->prev_instr in instrumentation functions #109076

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Lib/test/test_sys_settrace.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,13 @@ def generator_example():
[(5, 'line'), (5, 'return')])


def lineno_matches_lasti(frame):
last_line = None
for start, end, line in frame.f_code.co_lines():
if start <= frame.f_lasti < end:
last_line = line
return last_line == frame.f_lineno

class Tracer:
def __init__(self, trace_line_events=None, trace_opcode_events=None):
self.trace_line_events = trace_line_events
Expand All @@ -330,6 +337,7 @@ def _reconfigure_frame(self, frame):
frame.f_trace_opcodes = self.trace_opcode_events

def trace(self, frame, event, arg):
assert lineno_matches_lasti(frame)
self._reconfigure_frame(frame)
self.events.append((frame.f_lineno, event))
return self.trace
Expand Down Expand Up @@ -1890,6 +1898,7 @@ def __init__(self, function, jumpFrom, jumpTo, event='line',
def trace(self, frame, event, arg):
if self.done:
return
assert lineno_matches_lasti(frame)
# frame.f_code.co_firstlineno is the first line of the decorator when
# 'function' is decorated and the decorator may be written using
# multiple physical lines when it is too long. Use the first line
Expand Down
8 changes: 2 additions & 6 deletions Python/instrumentation.c
Original file line number Diff line number Diff line change
Expand Up @@ -1057,8 +1057,6 @@ _Py_call_instrumentation_jump(
assert(event == PY_MONITORING_EVENT_JUMP ||
event == PY_MONITORING_EVENT_BRANCH);
assert(frame->prev_instr == instr);
/* Event should occur after the jump */
frame->prev_instr = target;
PyCodeObject *code = _PyFrame_GetCode(frame);
int to = (int)(target - _PyCode_CODE(code));
PyObject *to_obj = PyLong_FromLong(to * (int)sizeof(_Py_CODEUNIT));
Expand All @@ -1071,12 +1069,10 @@ _Py_call_instrumentation_jump(
if (err) {
return NULL;
}
if (frame->prev_instr != target) {
if (frame->prev_instr != instr) {
/* The callback has caused a jump (by setting the line number) */
return frame->prev_instr;
}
/* Reset prev_instr for INSTRUMENTED_LINE */
frame->prev_instr = instr;
return target;
}

Expand Down Expand Up @@ -1125,7 +1121,7 @@ _Py_Instrumentation_GetLine(PyCodeObject *code, int index)
int
_Py_call_instrumentation_line(PyThreadState *tstate, _PyInterpreterFrame* frame, _Py_CODEUNIT *instr, _Py_CODEUNIT *prev)
{
frame->prev_instr = instr;
assert(frame->prev_instr == instr);
PyCodeObject *code = _PyFrame_GetCode(frame);
assert(is_version_up_to_date(code, tstate->interp));
assert(instrumentation_cross_checks(tstate->interp, code));
Expand Down