Skip to content

Commit 72cce09

Browse files
committed
m1n1.hv: Improve context printout
Make the addresses virtual, add symbols Signed-off-by: Hector Martin <[email protected]>
1 parent 12bff05 commit 72cce09

3 files changed

Lines changed: 38 additions & 17 deletions

File tree

proxyclient/m1n1/asm.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,10 @@ def disassemble(self):
8585
output = self._get(OBJDUMP, f"-zd {self.elffile}")
8686

8787
for line in output.split("\n"):
88-
if not line or line[0] != " ":
88+
if not line or line.startswith("/"):
89+
continue
90+
sl = line.split()
91+
if not sl or sl[0][-1] != ":":
8992
continue
9093
yield line
9194

proxyclient/m1n1/hv/__init__.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -615,6 +615,13 @@ def sym(self, addr):
615615

616616
return self.symbols[idx]
617617

618+
def get_sym(self, addr):
619+
a, name = self.sym(addr)
620+
if addr == a:
621+
return name
622+
else:
623+
return None
624+
618625
def handle_msr(self, ctx, iss=None):
619626
if iss is None:
620627
iss = ctx.esr.ISS
@@ -734,7 +741,7 @@ def handle_hvc(self, ctx):
734741
if far is not None:
735742
self.log(f" FAR={self.addr(far)}")
736743
if elr_phys:
737-
self.u.disassemble_at(elr_phys - 4 * 4, 9 * 4, elr_phys)
744+
self.u.disassemble_at(elr_phys - 4 * 4, 9 * 4, elr - 4 * 4, elr, sym=self.get_sym)
738745
if self.sym(elr)[1] == "com.apple.kernel:_panic_trap_to_debugger":
739746
self.log("Panic! Trying to decode panic...")
740747
try:
@@ -905,7 +912,7 @@ def handle_exception(self, reason, code, info):
905912
handled = self.handle_sync(ctx)
906913
elif code == EXC.FIQ:
907914
self.u.msr(CNTV_CTL_EL0, 0)
908-
self.u.print_context(ctx, False)
915+
self.u.print_context(ctx, False, sym=self.get_sym)
909916
handled = True
910917
elif reason == START.HV:
911918
code = HV_EVENT(code)
@@ -926,7 +933,7 @@ def handle_exception(self, reason, code, info):
926933
else:
927934
self.log(f"Guest exception: {reason.name}/{code.name}")
928935
self.update_pac_mask()
929-
self.u.print_context(ctx, self.is_fault)
936+
self.u.print_context(ctx, self.is_fault, sym=self.get_sym)
930937

931938
if self._sigint_pending or not handled or user_interrupt:
932939
self._sigint_pending = False
@@ -1129,7 +1136,7 @@ def context(self):
11291136
f = f" (orig: #{self.exc_orig_cpu})" if self.ctx.cpu_id != self.exc_orig_cpu else ""
11301137
print(f" == On CPU #{self.ctx.cpu_id}{f} ==")
11311138
print(f" Reason: {self.exc_reason.name}/{self.exc_code.name}")
1132-
self.u.print_context(self.ctx, self.is_fault)
1139+
self.u.print_context(self.ctx, self.is_fault, sym=self.get_sym)
11331140

11341141
def bt(self, frame=None, lr=None):
11351142
if frame is None:

proxyclient/m1n1/proxyutils.py

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -206,21 +206,30 @@ def push_adt(self):
206206
print(f"Pushing ADT ({adt_size} bytes)...")
207207
self.iface.writemem(adt_base, self.adt_data)
208208

209-
def disassemble_at(self, start, size, pc=None):
209+
def disassemble_at(self, start, size, pc=None, vstart=None, sym=None):
210210
'''disassemble len bytes of memory from start
211211
optional pc address will mark that line with a '*' '''
212212
code = struct.unpack(f"<{size // 4}I", self.iface.readmem(start, size))
213+
if vstart is None:
214+
vstart = start
213215

214-
c = ARMAsm(".inst " + ",".join(str(i) for i in code), start)
215-
lines = list(c.disassemble())
216-
if pc is not None:
217-
idx = (pc - start) // 4
216+
c = ARMAsm(".inst " + ",".join(str(i) for i in code), vstart)
217+
lines = list()
218+
for line in c.disassemble():
219+
sl = line.split()
218220
try:
219-
lines[idx] = " *" + lines[idx][2:]
220-
except IndexError:
221-
pass
222-
for i in lines:
223-
print(" " + i)
221+
addr = int(sl[0].rstrip(":"), 16)
222+
except:
223+
addr = None
224+
if pc == addr:
225+
line = " *" + line
226+
else:
227+
line = " " + line
228+
if sym:
229+
if s := sym(addr):
230+
print()
231+
print(f"{' '*len(sl[0])} {s}:")
232+
print(line)
224233

225234
def print_l2c_regs(self):
226235
print()
@@ -234,7 +243,7 @@ def print_l2c_regs(self):
234243
self.msr(L2C_ERR_STS_EL1, l2c_err_sts) # Clear the flag bits
235244
self.msr(DAIF, self.mrs(DAIF) | 0x100) # Re-enable SError exceptions
236245

237-
def print_context(self, ctx, is_fault=True, addr=lambda a: f"0x{a:x}"):
246+
def print_context(self, ctx, is_fault=True, addr=lambda a: f"0x{a:x}", sym=None, num_ctx=9):
238247
print(f" == Exception taken from {ctx.spsr.M.name} ==")
239248
el = ctx.spsr.M >> 2
240249
print(f" SPSR = {ctx.spsr}")
@@ -252,7 +261,9 @@ def print_context(self, ctx, is_fault=True, addr=lambda a: f"0x{a:x}"):
252261
print()
253262
print(" == Code context ==")
254263

255-
self.disassemble_at(ctx.elr_phys - 4 * 4, 9 * 4, ctx.elr_phys)
264+
off = -(num_ctx // 2)
265+
266+
self.disassemble_at(ctx.elr_phys + 4 * off, num_ctx * 4, ctx.elr, ctx.elr + 4 * off, sym=sym)
256267

257268
if is_fault:
258269
if ctx.esr.EC == ESR_EC.MSR or ctx.esr.EC == ESR_EC.IMPDEF and ctx.esr.ISS == 0x20:

0 commit comments

Comments
 (0)