E671 gh-103049: Support instruction level debugging in pdb by gaogaotiantian · Pull Request #103050 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-103049: Support instruction level debugging in pdb #103050

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Implement ni and si, do li and lli instead of assem
  • Loading branch information
gaogaotiantian committed Apr 8, 2023
commit fc50e5190f45ac0dad5692de549a2f7b440bf369
72 changes: 69 additions & 3 deletions Lib/bdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ def __init__(self, skip=None):
self.skip = set(skip) if skip else None
self.breaks = {}
self.fncache = {}
self._curframe = None
self.lasti = -1
self.trace_opcodes = False
self.frame_returning = None

self._load_breaks()
Expand Down Expand Up @@ -84,6 +87,8 @@ def trace_dispatch(self, frame, event, arg):

The arg parameter depends on the previous event.
"""
self._curframe = frame

if self.quitting:
return # None
if event == 'line':
Expand All @@ -94,6 +99,8 @@ def trace_dispatch(self, frame, event, arg):
return self.dispatch_return(frame, arg)
if event == 'exception':
return self.dispatch_exception(frame, arg)
if event == 'opcode':
return self.dispatch_opcode(frame)
if event == 'c_call':
return self.trace_dispatch
if event == 'c_exception':
Expand All @@ -115,13 +122,30 @@ def dispatch_line(self, frame):
if self.quitting: raise BdbQuit
return self.trace_dispatch

def dispatch_opcode(self, frame):
"""Invoke user function and return trace function for opcode event.

If the debugger stops on the current opcode, invoke
self.user_opcode(). Raise BdbQuit if self.quitting is set.
Return self.trace_dispatch to continue tracing in this scope.
"""
if self.stop_here(frame) or self.break_here(frame):
self.user_opcode(frame)
if self.quitting: raise BdbQuit
return self.trace_dispatch

def dispatch_call(self, frame, arg):
"""Invoke user function and return trace function for call event.

If the debugger stops on this function call, invoke
self.user_call(). Raise BdbQuit if self.quitting is set.
Return self.trace_dispatch to continue tracing in this scope.
"""
if self.trace_opcodes:
frame.f_trace_opcodes = True
else:
frame.f_trace_opcodes = False

# XXX 'arg' is no longer used
if self.botframe is None:
# First call of dispatch since reset()
Expand Down Expand Up @@ -209,9 +233,15 @@ def stop_here(self, frame):
if frame is self.stopframe:
if self.stoplineno == -1:
return False
return frame.f_lineno >= self.stoplineno
if self.trace_opcodes:
return self.lasti != frame.f_lasti
else:
return frame.f_lineno >= self.stoplineno
if not self.stopframe:
return True
if self.trace_opcodes:
return self.lasti != frame.f_lasti
else:
return True
return False

def break_here(self, frame):
Expand Down Expand Up @@ -272,7 +302,21 @@ def user_exception(self, frame, exc_info):
"""Called when we stop on an exception."""
pass

def _set_stopinfo(self, stopframe, returnframe, stoplineno=0):
def user_opcode(self, frame):
"""Called when we stop or break at a opcode."""
pass

def _set_trace_opcodes(self, trace_opcodes):
if trace_opcodes != self.trace_opcodes:
self.trace_opcodes = trace_opcodes
frame = self._curframe
while frame is not None:
frame.f_trace_opcodes = trace_opcodes
if frame is self.botframe:
break
frame = frame.f_back

def _set_stopinfo(self, stopframe, returnframe, stoplineno=0, lasti=None):
"""Set the attributes for stopping.

If stoplineno is greater than or equal to 0, then stop at line
Expand All @@ -285,6 +329,12 @@ def _set_stopinfo(self, stopframe, returnframe, stoplineno=0):
# stoplineno >= 0 means: stop at line >= the stoplineno
# stoplineno -1 means: don't stop at all
self.stoplineno = stoplineno
if lasti:
# We are stopping at opcode level
self._set_trace_opcodes(True)
self.lasti = lasti
else:
self._set_trace_opcodes(False)

# Derived classes and clients can call the following methods
# to affect the stepping state.
Expand All @@ -309,10 +359,26 @@ def set_step(self):
caller_frame.f_trace = self.trace_dispatch
self._set_stopinfo(None, None)

def set_stepinst(self, frame):
"""Stop after one opcode."""
# Issue #13183: pdb skips frames after hitting a breakpoint and running
# step commands.
# Restore the trace function in the caller (that may not have been set
# for performance reasons) when returning from the current frame.
if self.frame_returning:
caller_frame = self.frame_returning.f_back
if caller_frame and not caller_frame.f_trace:
caller_frame.f_trace = self.trace_dispatch
self._set_stopinfo(None, None, lasti=frame.f_lasti)

def set_next(self, frame):
"""Stop on the next line in or below the given frame."""
self._set_stopinfo(frame, None)

def set_nextinst(self, frame):
"""Stop on the next line in or below the given frame."""
self._set_stopinfo(frame, None, lasti=frame.f_lasti)

def set_return(self, frame):
"""Stop when returning from the given frame."""
if frame.f_code.co_flags & GENERATOR_AND_COROUTINE_FLAGS:
Expand Down
Loading
0