8000 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
Next Next commit
Support assembly display on pdb
  • Loading branch information
gaogaotiantian committed Apr 8, 2023
commit 5e4538ff95ce272b6435ec2cb6d3443c41185b16
38 changes: 35 additions & 3 deletions Lib/pdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ def __init__(self, completekey='tab', stdin=None, stdout=None, skip=None,
pass
self.allow_kbdint = False
self.nosigint = nosigint
self.assem_mode = False

# Read ~/.pdbrc and ./.pdbrc
self.rcLines = []
Expand Down Expand Up @@ -1287,6 +1288,19 @@ def do_pp(self, arg):
complete_p = _complete_expression
complete_pp = _complete_expression

def do_assem(self, arg):
"""assem [on | off]
Toggle/Set assembly mode
"""
if not arg:
self.assem_mode = not self.assem_mode
elif arg == "on":
self.assem_mode = True
8000 elif arg == "off":
self.assem_mode = False
else:
self.message("usage: assem [on | off]")

def do_list(self, arg):
"""l(ist) [first [,last] | .]

Expand Down Expand Up @@ -1336,7 +1350,8 @@ def do_list(self, arg):
try:
lines = linecache.getlines(filename, self.curframe.f_globals)
self._print_lines(lines[first-1:last], first, breaklist,
self.curframe)
self.curframe,
dis.get_instructions(self.curframe.f_code))
self.lineno = min(last, len(lines))
if len(lines) < last:
self.message('[EOF]')
Expand All @@ -1355,7 +1370,8 @@ def do_longlist(self, arg):
except OSError as err:
self.error(err)
return
self._print_lines(lines, lineno, breaklist, self.curframe)
self._print_lines(lines, lineno, breaklist, self.curframe,
dis.get_instructions(self.curframe.f_code))
do_ll = do_longlist

def do_source(self, arg):
Expand All @@ -1375,13 +1391,15 @@ def do_source(self, arg):

complete_source = _complete_expression

def _print_lines(self, lines, start, breaks=(), frame=None):
def _print_lines(self, lines, start, breaks=(), frame=None, instructions=None):
"""Print a range of lines."""
if frame:
current_lineno = frame.f_lineno
exc_lineno = self.tb_lineno.get(frame, -1)
else:
current_lineno = exc_lineno = -1
if self.assem_mode and instructions:
inst = next(instructions)
for lineno, line in enumerate(lines, start):
s = str(lineno).rjust(3)
if len(s) < 4:
Expand All @@ -1395,6 +1413,20 @@ def _print_lines(self, lines, start, breaks=(), frame=None):
elif lineno == exc_lineno:
s += '>>'
self.message(s + '\t' + line.rstrip())
if self.assem_mode and instructions:
while True:
if inst.positions.lineno == lineno:
current_inst = frame and frame.f_lasti == inst.offset
disassem = inst._disassemble(lineno_width=None,
mark_as_current=current_inst)
self.message(f" {disassem}")
elif inst.positions.lineno is not None and inst.positions.lineno > lineno:
break
try:
inst = next(instructions)
except StopIteration:
break


def do_whatis(self, arg):
"""whatis arg
Expand Down
0