Skip to content

Commit 1e9b4f0

Browse files
committed
Improve disassembly
Respect starting offset. Remove some dead code. Turn optional ints to ints and use -1 as a sentinel.
1 parent 7b29969 commit 1e9b4f0

2 files changed

Lines changed: 18 additions & 99 deletions

File tree

trepan/lib/disassemble.py

Lines changed: 12 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def dis(
7272
errmsg,
7373
x=None,
7474
start_line=-1,
75-
end_line=None,
75+
end_line=-1,
7676
relative_pos=False,
7777
style="none",
7878
start_offset=0,
@@ -197,7 +197,7 @@ def dis_from_file(
197197
errmsg,
198198
filename,
199199
start_line=-1,
200-
end_line=None,
200+
end_line=-1,
201201
style="none",
202202
include_header=False,
203203
asm_format="extended",
@@ -212,8 +212,9 @@ def dis_from_file(
212212
msg(f"Starting line {start_line} not found; adjusting up to {new_start}")
213213
start_line = new_start
214214

215-
code_object = next((k for k, val in linecache_info.code_map.items() if val == filename), None)
215+
code_object = next((value for value in linecache_info.code_map.values() if value.co_filename == filename), None)
216216
if code_object is not None:
217+
217218
dis(msg, msg_nocr, section, errmsg,
218219
x=code_object,
219220
start_line=start_line,
@@ -433,14 +434,13 @@ def null_print(_):
433434

434435
labels = findlabels(code, opc)
435436

436-
if start_line > cur_line:
437+
if start_line > cur_line or start_offset > 0:
437438
msg_nocr = null_print
438439
msg = null_print
439440
else:
440441
msg_nocr = orig_msg_nocr
441442
msg = orig_msg
442443

443-
444444
offset = -1
445445
for instr in get_instructions_bytes(code, opc):
446446
instructions.append(instr)
@@ -493,93 +493,6 @@ def null_print(_):
493493

494494
return code, offset
495495

496-
def disassemble_instruction(
497-
orig_msg: Callable,
498-
orig_msg_nocr: Callable,
499-
code,
500-
offset,
501-
lasti=-1,
502-
cur_line=0,
503-
end_line=None,
504-
relative_pos=False,
505-
varnames=(),
506-
names=(),
507-
constants=(),
508-
cells=(),
509-
line_starts={},
510-
style="none",
511-
opc=PYTHON_OPCODES,
512-
asm_format="extended",
513-
) -> tuple:
514-
"""Disassemble byte string of code. If end_line is negative
515-
it counts the number of statement line starts to use."""
516-
517-
def null_print(_):
518-
return None
519-
520-
instructions = []
521-
if end_line is None:
522-
end_line = 10000
523-
elif relative_pos:
524-
end_line += start_line - 1
525-
pass
526-
527-
labels = findlabels(code, opc)
528-
529-
if start_line > cur_line:
530-
msg_nocr = null_print
531-
msg = null_print
532-
else:
533-
msg_nocr = orig_msg_nocr
534-
msg = orig_msg
535-
536-
offset = -1
537-
for instr in get_instructions_bytes(
538-
code, opc, varnames, names, constants, cells, line_starts, labels
539-
):
540-
instructions.append(instr)
541-
542-
offset = instr.offset
543-
544-
if end_offset and offset > end_offset:
545-
break
546-
547-
# Python 3.11 introduces "CACHE" and the convention seems to be
548-
# to not print these normally.
549-
if instr.opname == "CACHE" and asm_format not in (
550-
"extended_bytes",
551-
"bytes",
552-
):
553-
continue
554-
555-
# Column: Source code line number
556-
if instr.starts_line:
557-
cur_line = instr.starts_line
558-
if start_line and (
559-
(start_line > cur_line) or start_offset and start_offset > offset
560-
):
561-
continue
562-
563-
if (cur_line > end_line) or (end_offset and offset > end_offset):
564-
break
565-
elif start_offset and offset and start_offset <= offset:
566-
continue
567-
568-
print_instruction(
569-
instr,
570-
instructions,
571-
msg,
572-
msg_nocr,
573-
labels=labels,
574-
lasti=lasti,
575-
line_starts=line_starts,
576-
style=style,
577-
opc=opc,
578-
asm_format=asm_format,
579-
)
580-
581-
return code, offset
582-
583496
# Demo it
584497
if __name__ == "__main__":
585498

@@ -604,11 +517,14 @@ def fib(x):
604517
return 1
605518
return fib(x - 1) + fib(x - 2)
606519

607-
dis_from_file(msg, msg_nocr, section, errmsg, __file__, start_line=45)
608-
# curframe = inspect.currentframe()
609-
# dis(msg, msg_nocr, errmsg, section, curframe,
610-
# start_line=10, end_line=40, highlight='dark')
520+
# dis_from_file(msg, msg_nocr, section, errmsg, __file__, start_line=45)
611521
# print('-' * 40)
522+
523+
curframe = inspect.currentframe()
524+
dis(msg, msg_nocr, errmsg, section, curframe, start_offset=4, end_offset=10)
525+
print('-' * 40)
526+
527+
import sys; sys.exit(0)
612528
# does nothing because start_offset is too high:
613529
# dis(msg, msg_nocr, errmsg, section, curframe,
614530
# start_offset=10, end_offset=20, highlight='dark')

trepan/processor/command/list.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2009, 2012-2017, 2020-2021, 2023-2024
2+
# Copyright (C) 2009, 2012-2017, 2020-2021, 2023-2024, 2026
33
# Rocky Bernstein
44
#
55
# This program is free software: you can redistribute it and/or modify
@@ -100,7 +100,7 @@ def run(self, args):
100100
filename = resolved_name = remapped_file
101101

102102
if not osp.exists(resolved_name):
103-
# See of resuled_filename is a module name:
103+
# See if resolved_filename is a module name:
104104
# START HERE with try: eval, except
105105
try:
106106
obj = self.proc.eval(filename, show_error=False)
@@ -143,7 +143,7 @@ def run(self, args):
143143
return
144144

145145
if last > max_line:
146-
self.msg("End position changed to last line %d " % max_line)
146+
self.msg("End position changed from %d to last line %d " % (last, max_line))
147147
last = max_line
148148

149149
bplist = self.core.bpmgr.bplist
@@ -209,6 +209,9 @@ def run(self, args):
209209
pass
210210
except KeyboardInterrupt:
211211
pass
212+
if lineno >= max_line:
213+
proc.list_lineno = max_line - 1
214+
212215
return False
213216

214217

0 commit comments

Comments
 (0)