Skip to content

Commit 835d889

Browse files
committed
Rename and change how show_mapinfo is used
1 parent 6f3ccb1 commit 835d889

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

qiling/debugger/qdb/qdb.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,9 @@ def do_show(self, *args) -> None:
353353
show some runtime information
354354
"""
355355

356-
self.ql.mem.show_mapinfo()
356+
for info_line in self.ql.mem.get_formatted_mapinfo():
357+
self.ql.log.info(info_line)
358+
357359
qdb_print(QDB_MSG.INFO, f"Breakpoints: {[hex(addr) for addr in self.bp_list.keys()]}")
358360
if self.rr:
359361
qdb_print(QDB_MSG.INFO, f"Snapshots: {len([st for st in self.rr.layers if isinstance(st, self.rr.DiffedState)])}")

qiling/os/memory.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -149,12 +149,12 @@ def change_mapinfo(self, mem_s: int, mem_e: int, mem_p: int = None, mem_info: st
149149
if mem_info is not None:
150150
self.map_info[info_idx] = (tmp_map_info[0], tmp_map_info[1], tmp_map_info[2], mem_info, tmp_map_info[4])
151151

152-
def get_mapinfo(self) -> Sequence[Tuple[int, int, str, str, Optional[str]]]:
152+
def get_mapinfo(self) -> Sequence[Tuple[int, int, str, str, str]]:
153153
"""Get memory map info.
154154
155155
Returns: A sequence of 5-tuples representing the memory map entries. Each
156156
tuple contains range start, range end, permissions, range label and path of
157-
containing image (or None if not contained by any image)
157+
containing image (or an empty string if not contained by any image)
158158
"""
159159

160160
def __perms_mapping(ps: int) -> str:
@@ -166,21 +166,21 @@ def __perms_mapping(ps: int) -> str:
166166

167167
return ''.join(val if idx & ps else '-' for idx, val in perms_d.items())
168168

169-
def __process(lbound: int, ubound: int, perms: int, label: str, is_mmio: bool) -> Tuple[int, int, str, str, Optional[str]]:
169+
def __process(lbound: int, ubound: int, perms: int, label: str, is_mmio: bool) -> Tuple[int, int, str, str, str]:
170170
perms_str = __perms_mapping(perms)
171171

172172
if hasattr(self.ql, 'loader'):
173173
image = self.ql.loader.find_containing_image(lbound)
174-
container = image.path if image and not is_mmio else None
174+
container = image.path if image and not is_mmio else ''
175175
else:
176-
container = None
176+
container = ''
177177

178178
return (lbound, ubound, perms_str, label, container)
179179

180180
return tuple(__process(*entry) for entry in self.map_info)
181181

182-
def show_mapinfo(self):
183-
"""Emit memory map info in a nicely formatted table.
182+
def get_formatted_mapinfo(self) -> Sequence[str]:
183+
"""Get memory map info in a nicely formatted table.
184184
"""
185185

186186
mapinfo = self.get_mapinfo()
@@ -192,12 +192,17 @@ def show_mapinfo(self):
192192
len_addr = max(grouped[0])
193193
len_label = max(grouped[1])
194194

195-
# emit title row
196-
self.ql.log.info(f'{"Start":{len_addr}s} {"End":{len_addr}s} {"Perm":5s} {"Label":{len_label}s} {"Image"}')
195+
# pre-allocate table
196+
table = [''] * (len(mapinfo) + 1)
197197

198-
# emit table rows
199-
for lbound, ubound, perms, label, container in mapinfo:
200-
self.ql.log.info(f'{lbound:0{len_addr}x} - {ubound:0{len_addr}x} {perms:5s} {label:{len_label}s} {container or ""}')
198+
# add title row
199+
table[0] = f'{"Start":{len_addr}s} {"End":{len_addr}s} {"Perm":5s} {"Label":{len_label}s} {"Image"}'
200+
201+
# add table rows
202+
for i, (lbound, ubound, perms, label, container) in enumerate(mapinfo, 1):
203+
table[i] = f'{lbound:0{len_addr}x} - {ubound:0{len_addr}x} {perms:5s} {label:{len_label}s} {container}'
204+
205+
return table
201206

202207
# TODO: relying on the label string is risky; find a more reliable method
203208
def get_lib_base(self, filename: str) -> Optional[int]:

qiling/os/os.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,6 @@ def emu_error(self):
252252
finally:
253253
self.ql.log.error(f'PC = {pc:#0{self.ql.arch.pointersize * 2 + 2}x}{pc_info}\n')
254254

255-
self.ql.log.info(f'Memory map:')
256-
self.ql.mem.show_mapinfo()
255+
self.ql.log.error(f'Memory map:')
256+
for info_line in self.ql.mem.get_formatted_mapinfo():
257+
self.ql.log.error(info_line)

qiling/os/uefi/uefi.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,9 @@ def emu_error(self):
197197
self.emit_stack()
198198

199199
self.ql.log.error(f'Memory map:')
200-
self.ql.mem.show_mapinfo()
200+
for info_line in self.ql.mem.get_formatted_mapinfo():
201+
self.ql.log.error(info_line)
202+
201203

202204
def set_api(self, target: str, handler: Callable, intercept: QL_INTERCEPT = QL_INTERCEPT.CALL):
203205
super().set_api(f'hook_{target}', handler, intercept)

0 commit comments

Comments
 (0)