Skip to content

Commit be2f449

Browse files
committed
proper error message for examine_mem
1 parent 1089afb commit be2f449

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

qiling/debugger/qdb/frontend.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
from qiling.const import QL_ARCH
1313

14+
import unicorn
15+
1416
from .utils import dump_regs, get_arm_flags, disasm, _parse_int, handle_bnj
1517
from .const import *
1618

@@ -83,7 +85,14 @@ def unpack(bs, sz):
8385
else:
8486
lines = 1 if ct <= 4 else math.ceil(ct / 4)
8587

86-
mem_read = [ql.mem.read(addr+(offset*sz), sz) for offset in range(ct)]
88+
mem_read = []
89+
for offset in range(ct):
90+
# append data if read successfully, otherwise return error message
91+
if (data := _try_read(ql, addr+(offset*sz), sz))[0]:
92+
mem_read.append(data[0])
93+
94+
else:
95+
return data[1]
8796

8897
for line in range(lines):
8998
offset = line * sz * 4
@@ -95,7 +104,6 @@ def unpack(bs, sz):
95104
prefix = "0x" if ft in ("x", "a") else ""
96105
pad = '0' + str(sz*2) if ft in ('x', 'a', 't') else ''
97106
ft = ft.lower() if ft in ("x", "o", "b", "d") else ft.lower().replace("t", "b").replace("a", "x")
98-
99107
print(f"{prefix}{data:{pad}{ft}}\t", end="")
100108

101109
print()
@@ -110,12 +118,20 @@ def get_terminal_size() -> Iterable:
110118

111119
# try to read data from ql memory
112120
def _try_read(ql: Qiling, address: int, size: int) -> Optional[bytes]:
121+
122+
result = None
123+
err_msg = ""
113124
try:
114125
result = ql.mem.read(address, size)
126+
127+
except unicorn.unicorn.UcError as err:
128+
if err.errno == 6: # Invalid memory read (UC_ERR_READ_UNMAPPED)
129+
err_msg = f"Can not access memory at address 0x{address:08x}"
130+
115131
except:
116-
result = None
132+
pass
117133

118-
return result
134+
return (result, err_msg)
119135

120136

121137
# divider printer

qiling/debugger/qdb/qdb.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -302,11 +302,8 @@ def do_examine(self: QlQdb, line: str) -> None:
302302
e.g. x/4wx 0x41414141 , print 4 word size begin from address 0x41414141 in hex
303303
"""
304304

305-
try:
306-
if not examine_mem(self.ql, line):
307-
self.do_help("examine")
308-
except:
309-
print(f"{color.RED}[!] something went wrong ...{color.END}")
305+
if type(err_msg := examine_mem(self.ql, line)) is str:
306+
print(f"{color.RED}[!] {err_msg} ...{color.END}")
310307

311308
def do_show(self: QlQdb, *args) -> None:
312309
"""

0 commit comments

Comments
 (0)