Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-111744: Support opcode events in bdb#111834

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from1 commit
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
NextNext commit
Support opcode events in bdb
  • Loading branch information
@gaogaotiantian
gaogaotiantian committedNov 7, 2023
commitc31348069341e26a17d3bfcfd40f4a28f116730d
69 changes: 60 additions & 9 deletionsLib/bdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,10 @@ def __init__(self, skip=None):
self.breaks = {}
self.fncache = {}
self.frame_trace_lines = {}
self.frame_trace_opcodes = {}
self.frame_returning = None
self.trace_opcodes = False
self.__curframe = None

self._load_breaks()

Expand DownExpand Up@@ -85,6 +88,9 @@ 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@@ -101,6 +107,8 @@ def trace_dispatch(self, frame, event, arg):
return self.trace_dispatch
if event == 'c_return':
return self.trace_dispatch
if event == 'opcode':
return self.dispatch_opcode(frame, arg)
print('bdb.Bdb.dispatch: unknown debugging event:', repr(event))
return self.trace_dispatch

Expand DownExpand Up@@ -187,6 +195,17 @@ def dispatch_exception(self, frame, arg):

return self.trace_dispatch

def dispatch_opcode(self, frame, arg):
"""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

# Normally derived classes don't override the following
# methods, but they may if they want to redefine the
# definition of stopping and breakpoints.
Expand DownExpand Up@@ -273,7 +292,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 are about to execute an opcode."""
pass

def _set_trace_opcodes(self, trace_opcodes):
if trace_opcodes != self.trace_opcodes:
self.trace_opcodes = trace_opcodes
frame = self.__curframe
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

And just to jog my memory:self.__curframe is needed because we only want to change events at or above the frame whereset_trace was called (these are the same frames wheref_trace_lines was set), but we may be much deeper at this point. Right?

But why is it double-underscored again? Do we need the name-mangling? I seepdb.Pdb has acurframe attribute, so maybe it's to avoid confusion with that?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yes the reason for the double-underscore is to avoid the conflict withpdb.Pdb.curframe. And yes, we only want to settrace_opcodes in the "user frames", which means frames above (including) the caller frame into the pdb. So we need to record the entering frame.

Thecurframe attribute ofpdb means the current frame being debugged. Do you prefer to change the the name__curframe to something likeentering_frame?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yeah, that feels a lot less cryptic and scary. :) Especially considering we already havestopframe,returnframe, andbotframe.

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, opcode=False):
"""Set the attributes for stopping.

If stoplineno is greater than or equal to 0, then stop at line
Expand All@@ -286,6 +319,21 @@ 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 opcode:
# We are stopping at opcode level
self._set_trace_opcodes(True)
else:
self._set_trace_opcodes(False)

def _set_caller_tracefunc(self):
# 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

# Derived classes and clients can call the following methods
# to affect the stepping state.
Expand All@@ -300,16 +348,14 @@ def set_until(self, frame, lineno=None):

def set_step(self):
"""Stop after one line of code."""
# 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_caller_tracefunc()
self._set_stopinfo(None, None)

def set_stepinstr(self):
"""Stop before the next instruction."""
self._set_caller_tracefunc()
self._set_stopinfo(None, None, opcode=True)

def set_next(self, frame):
"""Stop on the next line in or below the given frame."""
self._set_stopinfo(frame, None)
Expand All@@ -329,11 +375,13 @@ def set_trace(self, frame=None):
if frame is None:
frame = sys._getframe().f_back
self.reset()
self.__curframe = frame
while frame:
frame.f_trace = self.trace_dispatch
self.botframe = frame
# We need f_trace_liens == True for the debugger to work
self.frame_trace_lines[frame] = frame.f_trace_lines
self.frame_trace_opcodes[frame] = frame.f_trace_opcodes
frame.f_trace_lines = True
frame = frame.f_back
self.set_step()
Expand All@@ -355,7 +403,10 @@ def set_continue(self):
frame = frame.f_back
for frame, prev_trace_lines in self.frame_trace_lines.items():
frame.f_trace_lines = prev_trace_lines
for frame, prev_trace_opcodes in self.frame_trace_opcodes.items():
frame.f_trace_opcodes = prev_trace_opcodes
self.frame_trace_lines = {}
self.frame_trace_opcodes = {}

def set_quit(self):
"""Set quitting attribute to True.
Expand Down
1 change: 0 additions & 1 deletionLib/test/test_pdb.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2343,7 +2343,6 @@ def test_pdb_issue_gh_108976():
... 'continue'
... ]):
... test_function()
bdb.Bdb.dispatch: unknown debugging event: 'opcode'
> <doctest test.test_pdb.test_pdb_issue_gh_108976[0]>(5)test_function()
-> a = 1
(Pdb) continue
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp