slight adjust to prep debugger readyness for mcu

This commit is contained in:
xwings
2021-10-20 16:36:32 +08:00
parent b6cf49a4a0
commit ac1e85e464
4 changed files with 39 additions and 27 deletions
@@ -8,7 +8,7 @@ from qiling.const import QL_VERBOSE
def test_mcu_gpio_stm32f411():
ql = Qiling(["../../examples/rootfs/mcu/stm32f411/hello_gpioA.hex"],
archtype="cortex_m", profile="stm32f411", verbose=QL_VERBOSE.DEBUG)
ql.debugger = 'gdb:127.0.0.1:9998'
ql.hw.create('usart2')
ql.hw.create('rcc')
ql.hw.create('gpioa')
+16 -15
View File
@@ -694,33 +694,34 @@ class Qiling(QlCoreHooks, QlCoreStructs):
self.timeout = timeout
self.count = count
# init debugger
if self._debugger != False and self._debugger != None:
self._debugger = debugger_setup(self._debugger, self)
if self.archtype not in QL_ARCH_NONEOS and self.archtype not in QL_ARCH_HARDWARE:
self.write_exit_trap()
# patch binary
self.__enable_bin_patch()
# emulate the binary
self.os.run()
if self.archtype in QL_ARCH_NONEOS:
if code == None:
return self.arch.run(self._code)
else:
return self.arch.run(code)
if self.archtype in QL_ARCH_HARDWARE:
self.__enable_bin_patch()
if self.count <= 0:
self.count = -1
return self.arch.run(count=self.count, end=self.exit_point)
self.write_exit_trap()
# init debugger
if self._debugger != False and self._debugger != None:
self._debugger = debugger_setup(self._debugger, self)
# patch binary
self.__enable_bin_patch()
# emulate the binary
self.os.run()
self.arch.run(count=self.count, end=self.exit_point)
# run debugger
if self._debugger != False and self._debugger != None:
self._debugger.run()
# patch code to memory address
+18 -10
View File
@@ -51,14 +51,20 @@ class QlGdb(QlDebugger, object):
self.ip = ip
self.port = port
if ql.code:
if ql.archtype in QL_ARCH_HARDWARE:
load_address = ql.loader.load_address
exit_point = load_address + os.path.getsize(ql.path)
elif ql.code:
load_address = ql.os.entry_point
exit_point = load_address + len(ql.code)
else:
load_address = ql.loader.load_address
exit_point = load_address + os.path.getsize(ql.path)
if self.ql.ostype in (QL_OS.LINUX, QL_OS.FREEBSD) and not self.ql.code:
if ql.archtype in QL_ARCH_HARDWARE:
self.entry_point = ql.loader.entry_point
elif self.ql.ostype in (QL_OS.LINUX, QL_OS.FREEBSD) and not self.ql.code:
self.entry_point = self.ql.os.elf_entry
else:
self.entry_point = self.ql.os.entry_point
@@ -72,12 +78,13 @@ class QlGdb(QlDebugger, object):
#Setup register tables, order of tables is important
self.tables = {
QL_ARCH.A8086 : list({**x86_reg_map_16, **x86_reg_map_misc}.keys()),
QL_ARCH.X86 : list({**x86_reg_map_32, **x86_reg_map_misc, **x86_reg_map_st}.keys()),
QL_ARCH.X8664 : list({**x86_reg_map_64, **x86_reg_map_misc, **x86_reg_map_st}.keys()),
QL_ARCH.ARM : list({**arm_reg_map}.keys()),
QL_ARCH.ARM64 : list({**arm64_reg_map}.keys()),
QL_ARCH.MIPS : list({**mips_reg_map}.keys()),
QL_ARCH.A8086 : list({**x86_reg_map_16, **x86_reg_map_misc}.keys()),
QL_ARCH.X86 : list({**x86_reg_map_32, **x86_reg_map_misc, **x86_reg_map_st}.keys()),
QL_ARCH.X8664 : list({**x86_reg_map_64, **x86_reg_map_misc, **x86_reg_map_st}.keys()),
QL_ARCH.ARM : list({**arm_reg_map}.keys()),
QL_ARCH.CORTEX_M : list({**arm_reg_map}.keys()),
QL_ARCH.ARM64 : list({**arm64_reg_map}.keys()),
QL_ARCH.MIPS : list({**mips_reg_map}.keys()),
}
def addr_to_str(self, addr: int, short: bool = False, endian: Literal['little', 'big'] = 'big') -> str:
@@ -168,6 +175,7 @@ class QlGdb(QlDebugger, object):
QL_ARCH.X8664 : [ 0x06, 0x07, 0x10 ],
QL_ARCH.MIPS : [ 0x1d, 0x00, 0x25 ],
QL_ARCH.ARM : [ 0x0b, 0x0d, 0x0f ],
QL_ARCH.CORTEX_M : [ 0x0b, 0x0d, 0x0f ],
QL_ARCH.ARM64 : [ 0x1d, 0xf1, 0x20 ]
}
return adapter.get(arch)
@@ -498,7 +506,7 @@ class QlGdb(QlDebugger, object):
elif subcmd.startswith('Xfer:threads:read::0,'):
if self.ql.ostype in QL_OS_NONPID:
if self.ql.ostype in QL_OS_NONPID or self.ql.archtype in QL_ARCH_HARDWARE:
self.send("l")
else:
file_contents = ("<threads>\r\n<thread id=\""+ str(self.ql.os.pid) + "\" core=\"1\" name=\"" + self.ql.targetname + "\"/>\r\n</threads>")
@@ -613,7 +621,7 @@ class QlGdb(QlDebugger, object):
self.send("")
elif subcmd.startswith('File:open'):
if self.ql.ostype == QL_OS.UEFI:
if self.ql.ostype == QL_OS.UEFI or self.ql.archtype in QL_ARCH_HARDWARE:
self.send("F-1")
return
+4 -1
View File
@@ -26,7 +26,10 @@ class QlGdbUtils(object):
def initialize(self, ql, hook_address, exit_point=None, mappings=None):
self.ql = ql
self.current_address = self.entry_point = self.ql.os.entry_point
if self.ql.archtype in QL_ARCH_HARDWARE:
self.current_address = self.entry_point
else:
self.current_address = self.entry_point = self.ql.os.entry_point
self.exit_point = exit_point
self.mapping = mappings
self._tmp_hook = self.ql.hook_address(self.entry_point_hook, hook_address)