Skip to content

Commit ea0bce1

Browse files
committed
Better "list" command support for pyasm files
Loop differently for pyasm files that is used in python source files. Support for listing Python pyasm assembly files. Add routine for determining if a file is an assembly file.
1 parent 3ad403e commit ea0bce1

4 files changed

Lines changed: 71 additions & 55 deletions

File tree

trepan/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ def write_wrapper(*args, **kwargs):
236236
% (__title__, embedded_filename, pyasm_name),
237237
file=sys.stderr,
238238
)
239-
pyficache.remap_file(pyasm_name, embedded_filename)
239+
pyficache.remap_file(pyasm_name, embedded_filename, is_pyasm=True)
240240

241241
else:
242242
decompile_file = fd.name

trepan/processor/cmdproc.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def event_processor(self, frame, event, event_arg, prompt="trepan3k"):
329329
self.event_arg = event_arg
330330

331331
filename = frame.f_code.co_filename
332-
lineno = frame.f_lineno
333-
line = linecache.getline(filename, lineno, frame.f_globals)
332+
self.list_lineno = frame.f_lineno
333+
line = linecache.getline(filename, self.list_lineno, frame.f_globals)
334334
if not line:
335335
opts = {
336336
"output": "plain",
@@ -340,7 +340,7 @@ def event_processor(self, frame, event, event_arg, prompt="trepan3k"):
340340
m = re.search("^<frozen (.*)>", filename)
341341
if m and m.group(1):
342342
filename = pyficache.unmap_file(m.group(1))
343-
line = pyficache.getline(filename, lineno, opts)
343+
line = pyficache.getline(filename, self.list_lineno, opts)
344344
self.current_source_text = line
345345
if self.settings("skip") is not None:
346346
if is_def_stmt(line, frame):

trepan/processor/command/list.py

Lines changed: 56 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ def run(self, args):
115115
return
116116

117117
filename = pyficache.unmap_file(resolved_name)
118-
is_pyasm = filename.endswith(".pyasm")
118+
is_pyasm = pyficache.is_python_assembly_file(filename)
119119

120120
# We now have range information. Do the listing.
121121
max_line = pyficache.size(filename)
@@ -157,55 +157,65 @@ def run(self, args):
157157
if field in self.settings:
158158
opts[field] = self.settings[field]
159159

160-
if first <= 0:
161-
first = 1
162160
try:
163-
for lineno in range(first, last + 1):
164-
line = pyficache.getline(filename, lineno, opts)
161+
if is_pyasm:
162+
lineno = first
163+
# FIXME add approximate
164+
line, pyasm_line_index = pyficache.get_pyasm_line(filename, lineno, is_source_line=True, opts=opts)
165+
proc.list_lineno = lineno
165166
if line is None:
166-
line = linecache.getline(filename, lineno, proc.frame.f_globals)
167-
pass
168-
if line is None:
169-
self.msg("[EOF]")
170-
break
171-
else:
172-
if is_pyasm:
173-
self.msg(line)
174-
proc.list_lineno = lineno
175-
continue
176-
177-
line = line.rstrip("\n")
178-
s = proc._saferepr(lineno).rjust(3)
179-
if len(s) < 5:
180-
s += " "
181-
if (
182-
canonic_filename,
183-
lineno,
184-
) in list(bplist.keys()):
185-
bp = bplist[
186-
(
187-
canonic_filename,
188-
lineno,
189-
)
190-
][0]
191-
a_pad = "%02d" % bp.number
192-
s += bp.icon_char()
193-
else:
194-
s += " "
195-
a_pad = " "
167+
self.errmsg(f"cannot find assembly for line number {lineno}")
168+
return
169+
while lineno <= last:
170+
self.msg(line)
171+
pyasm_line_index += 1
172+
line, pyasm_line_index = pyficache.get_pyasm_line(filename, pyasm_line_index, is_source_line=False, opts=opts)
173+
if line is None:
174+
break
175+
else:
176+
if first <= 0:
177+
first = 1
178+
for lineno in range(first, last + 1):
179+
line = pyficache.getline(filename, lineno, opts)
180+
if line is None:
181+
line = linecache.getline(filename, lineno, proc.frame.f_globals)
196182
pass
197-
if curframe and lineno == inspect.getlineno(curframe):
198-
s += "->"
199-
if "plain" != self.settings["highlight"]:
200-
s = colorize("bold", s)
183+
if line is None:
184+
self.msg("[EOF]")
185+
break
201186
else:
202-
s += a_pad
187+
line = line.rstrip("\n")
188+
s = proc._saferepr(lineno).rjust(3)
189+
if len(s) < 5:
190+
s += " "
191+
if (
192+
canonic_filename,
193+
lineno,
194+
) in list(bplist.keys()):
195+
bp = bplist[
196+
(
197+
canonic_filename,
198+
lineno,
199+
)
200+
][0]
201+
a_pad = "%02d" % bp.number
202+
s += bp.icon_char()
203+
else:
204+
s += " "
205+
a_pad = " "
206+
pass
207+
if curframe and lineno == inspect.getlineno(curframe):
208+
s += "->"
209+
if "plain" != self.settings["highlight"]:
210+
s = colorize("bold", s)
211+
else:
212+
s += a_pad
213+
pass
214+
215+
self.msg(s + "\t" + line)
216+
proc.list_lineno = lineno
203217
pass
204-
205-
self.msg(s + "\t" + line)
206-
proc.list_lineno = lineno
207218
pass
208-
pass
209219
pass
210220
except KeyboardInterrupt:
211221
pass
@@ -231,6 +241,8 @@ def doit(cmd, args):
231241
cmdproc.setup()
232242
lcmd = ListCommand(cmdproc)
233243

244+
# doit(lcmd, ['list', "python3-trepan/test/example/00_chained-compare-cpython-314.pyasm:3"])
245+
234246
# Note: osp is defined abouve
235247
doit(lcmd, ['list', "osp:1"])
236248
# print('--' * 10)

trepan/processor/print.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ def prefix_for_filename(deparsed_text: str) -> str:
144144
"""
145145
lines = deparsed_text.split("\n")
146146

147-
148147
# FIXME Rather than blindly take the first line,
149148
# check if it is blank and if so use other lines.
150149
for line in lines:
@@ -208,7 +207,9 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
208207
filename = pyficache.unmap_file(filename)
209208
# FIXME: DRY
210209
if "<string>" == filename:
211-
if not (remapped_file := pyficache.main.code2tempfile.get(frame.f_code)):
210+
if not (
211+
remapped_file := pyficache.main.code2tempfile.get(frame.f_code)
212+
):
212213
remapped_file = cmdfns.source_tempfile_remap(
213214
"eval_string",
214215
dbgr_obj.eval_string,
@@ -248,7 +249,9 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
248249
# print("Can't deparse", frame.f_code)
249250
if source_text is None and eval_kind:
250251
if source_text := get_exec_or_eval_string(frame):
251-
filename = "string-" + prefix_for_filename(source_text) + "-"
252+
filename = (
253+
"string-" + prefix_for_filename(source_text) + "-"
254+
)
252255
else:
253256
source_text = f"{eval_kind}(...)"
254257
pass
@@ -288,10 +291,11 @@ def prefix_for_source_text(source_text: str, maxwidth: int) -> str:
288291
is_pyasm = filename.endswith(".pyasm")
289292
if is_pyasm:
290293
opts = opts.copy()
291-
opts["style"] = "plain"
292-
opts["output"] = "plain"
293-
294-
line = pyficache.getline(filename, line_number, opts)
294+
line, _ = pyficache.get_pyasm_line(
295+
filename, line_number, is_source_line=True, opts=opts
296+
)
297+
else:
298+
line = pyficache.getline(filename, line_number, opts)
295299

296300
if not line:
297301
if (

0 commit comments

Comments
 (0)