Skip to content

Commit 60642af

Browse files
authored
Merge pull request #912 from elicn/os-loader-improv
Minor improvements to QlOs and QlLoader
2 parents 4a20ef2 + 942daa1 commit 60642af

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

qiling/loader/loader.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
Image = NamedTuple('Image', (('base', int), ('end', int), ('path', str)))
1111

12-
class QlLoader():
12+
class QlLoader:
1313
def __init__(self, ql: Qiling):
1414
self.ql = ql
1515
self.env = self.ql.env
@@ -26,3 +26,7 @@ def save(self) -> Mapping[str, Any]:
2626

2727
def restore(self, saved_state: Mapping[str, Any]):
2828
self.images = [Image(*img) for img in saved_state['images']]
29+
30+
# loader main method; derivatives must implement one of their own
31+
def run(self) -> None:
32+
raise NotImplementedError

qiling/os/os.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import sys
77
from typing import Any, Iterable, Optional, Callable, Mapping, Sequence, TextIO, Tuple
88

9+
from unicorn import UcError
10+
911
from qiling import Qiling
1012
from qiling.const import QL_OS, QL_INTERCEPT, QL_OS_POSIX
1113
from qiling.os.const import STRING, WSTRING, GUID
@@ -202,36 +204,39 @@ def find_containing_image(self, pc):
202204
if image.base <= pc < image.end:
203205
return image
204206

207+
# os main method; derivatives must implement one of their own
208+
def run(self) -> None:
209+
raise NotImplementedError
210+
205211
def stop(self):
206212
if self.ql.multithread:
207213
self.thread_management.stop()
208214
else:
209215
self.ql.emu_stop()
210216

211217
def emu_error(self):
212-
self.ql.log.error("\n")
213-
218+
self.ql.log.error(f'CPU Context:')
214219
for reg in self.ql.reg.register_mapping:
215220
if isinstance(reg, str):
216-
REG_NAME = reg
217-
REG_VAL = self.ql.reg.read(reg)
218-
self.ql.log.error("%s\t:\t 0x%x" % (REG_NAME, REG_VAL))
219-
220-
self.ql.log.error("\n")
221-
self.ql.log.error("PC = 0x%x" % (self.ql.reg.arch_pc))
222-
containing_image = self.find_containing_image(self.ql.reg.arch_pc)
223-
if containing_image:
224-
offset = self.ql.reg.arch_pc - containing_image.base
225-
self.ql.log.error(" (%s+0x%x)" % (containing_image.path, offset))
226-
else:
227-
self.ql.log.info("\n")
228-
self.ql.mem.show_mapinfo()
221+
self.ql.log.error(f'{reg}\t: {self.ql.reg.read(reg):#x}')
222+
223+
pc = self.ql.reg.arch_pc
229224

230225
try:
231-
buf = self.ql.mem.read(self.ql.reg.arch_pc, 8)
232-
self.ql.log.error("%r" % ([hex(_) for _ in buf]))
226+
data = self.ql.mem.read(pc, size=8)
227+
except UcError:
228+
pc_info = ' (unreachable)'
229+
else:
230+
self.ql.log.error('Hexdump:')
231+
self.ql.log.error(data.hex(' '))
232+
233+
self.ql.log.error('Disassembly:')
234+
self.ql.arch.utils.disassembler(self.ql, pc, 64)
235+
236+
containing_image = self.find_containing_image(pc)
237+
pc_info = f' ({containing_image.path} + {pc - containing_image.base:#x})' if containing_image else ''
238+
finally:
239+
self.ql.log.error(f'PC = {pc:#0{self.ql.pointersize * 2 + 2}x}{pc_info}\n')
233240

234-
self.ql.log.info("\n")
235-
self.utils.disassembler(self.ql, self.ql.reg.arch_pc, 64)
236-
except:
237-
self.ql.log.error("Error: PC(0x%x) Unreachable" % self.ql.reg.arch_pc)
241+
self.ql.log.info(f'Memory map:')
242+
self.ql.mem.show_mapinfo()

0 commit comments

Comments
 (0)