diff --git a/avatar2/archs/arm.py b/avatar2/archs/arm.py index db2f13b95b..22d49925ea 100644 --- a/avatar2/archs/arm.py +++ b/avatar2/archs/arm.py @@ -1,4 +1,4 @@ -#from capstone import CS_ARCH_ARM, CS_MODE_LITTLE_ENDIAN, CS_MODE_BIG_ENDIAN +# from capstone import CS_ARCH_ARM, CS_MODE_LITTLE_ENDIAN, CS_MODE_BIG_ENDIAN from capstone import * from .architecture import Architecture @@ -7,19 +7,20 @@ class ARM(Architecture): qemu_name = 'arm' - gdb_name = 'arm' + gdb_name = 'arm' registers = {'r0': 0, 'r1': 1, 'r2': 2, 'r3': 3, 'r4': 4, 'r5': 5, 'r6': 6, - 'r7': 7, 'r8': 8, 'r9': 9, 'r10': 10, 'r11': 11, 'r12': 12, - 'sp': 13, 'lr': 14, 'pc': 15, 'cpsr': 25, - } + 'r7': 7, 'r8': 8, 'r9': 9, 'r10': 10, 'r11': 11, 'r12': 12, + 'sp': 13, 'lr': 14, 'pc': 15, 'cpsr': 25, + } unemulated_instructions = ['mcr', 'mrc'] capstone_arch = CS_ARCH_ARM capstone_mode = CS_MODE_LITTLE_ENDIAN + class ARM_CORTEX_M3(ARM): cpu_model = 'cortex-m3' qemu_name = 'arm' - gdb_name = 'arm' + gdb_name = 'arm' capstone_arch = CS_ARCH_ARM capstone_mode = CS_MODE_LITTLE_ENDIAN | CS_MODE_THUMB @@ -36,7 +37,7 @@ def register_write_cb(avatar, *args, **kwargs): return else: cpsr |= 0x20 - qemu.write_register('cpsr',cpsr) + qemu.write_register('cpsr', cpsr) @staticmethod def init(avatar): diff --git a/avatar2/archs/x86.py b/avatar2/archs/x86.py index e982386491..bc520d5800 100644 --- a/avatar2/archs/x86.py +++ b/avatar2/archs/x86.py @@ -2,6 +2,7 @@ from .architecture import Architecture + class X86(Architecture): qemu_name = 'i386' gdb_name = 'i386' @@ -26,7 +27,6 @@ class X86(Architecture): capstone_mode = CS_MODE_32 - class X86_64(Architecture): qemu_name = 'x86_64' gdb_name = 'i386:x86-64' @@ -49,11 +49,11 @@ class X86_64(Architecture): 'rip': 16, 'pc': 16, 'eflags': 17, - 'cs' : 18, - 'ss' : 19, - 'ds' : 20, - 'es' : 21, - 'fs' : 22, - 'gs' : 23, - } + 'cs': 18, + 'ss': 19, + 'ds': 20, + 'es': 21, + 'fs': 22, + 'gs': 23, + } unemulated_instructions = [] diff --git a/avatar2/protocols/qmp.py b/avatar2/protocols/qmp.py index e536e8d089..3960acdcc3 100644 --- a/avatar2/protocols/qmp.py +++ b/avatar2/protocols/qmp.py @@ -1,4 +1,5 @@ import sys + if sys.version_info < (3, 0): import Queue as queue else: @@ -11,17 +12,15 @@ class QMPProtocol(object): - def __init__(self, port, origin=None): - + self.port = port - self.log = logging.getLogger('%s.%s' % + self.log = logging.getLogger('%s.%s' % (origin.log.name, self.__class__.__name__) - ) if origin else \ - logging.getLogger(self.__class__.__name__) + ) if origin else \ + logging.getLogger(self.__class__.__name__) self.id = 0 - def __del__(self): self.shutdown() @@ -31,7 +30,6 @@ def connect(self): self.execute_command('qmp_capabilities') return True - def execute_command(self, cmd, args=None): command = {} command['execute'] = cmd @@ -55,7 +53,6 @@ def execute_command(self, cmd, args=None): if 'return' in resp: return resp['return'] raise Exception("Response contained neither an error nor an return") - def reset(self): """ @@ -68,9 +65,9 @@ def shutdown(self): """ returns: True on success, else False """ - #self._communicator.stop() + # self._communicator.stop() pass - + def get_registers(self): """ Gets the current register state based on the hmp info registers @@ -79,7 +76,6 @@ def get_registers(self): returns: A dictionary with the registers """ regs_s = self.execute_command("human-monitor-command", - {"command-line":"info registers"}) + {"command-line": "info registers"}) regs_r = re.findall('(...)=([0-9a-f]{8})', regs_s) - return dict([(r.lower(), int(v,16)) for r,v in regs_r]) - + return dict([(r.lower(), int(v, 16)) for r, v in regs_r]) diff --git a/avatar2/targets/gdb_target.py b/avatar2/targets/gdb_target.py index b6b95ea947..5c38147c0d 100644 --- a/avatar2/targets/gdb_target.py +++ b/avatar2/targets/gdb_target.py @@ -4,7 +4,7 @@ class GDBTarget(Target): def __init__(self, avatar, - gdb_executable='gdb', gdb_additional_args=[], gdb_port=3333, + gdb_executable='gdb', gdb_additional_args=None, gdb_port=3333, gdb_serial_device='/dev/ttyACM0', gdb_serial_baud_rate=38400, gdb_serial_parity='none', @@ -15,7 +15,7 @@ def __init__(self, avatar, super(GDBTarget, self).__init__(avatar, **kwargs) self.gdb_executable = gdb_executable - self.gdb_additional_args = gdb_additional_args + self.gdb_additional_args = gdb_additional_args if gdb_additional_args else [] self.gdb_port = gdb_port self.gdb_serial_device = gdb_serial_device self.gdb_serial_baud_rate = gdb_serial_baud_rate diff --git a/avatar2/targets/target.py b/avatar2/targets/target.py index 6a328a4b50..1c13da844a 100644 --- a/avatar2/targets/target.py +++ b/avatar2/targets/target.py @@ -6,6 +6,7 @@ from ..watchmen import watch + def action_valid_decorator_factory(state, protocol): """ This decorator factory is used to generate decorators which verify that @@ -46,6 +47,7 @@ class TargetStates(Enum): SYNCING = 0x10 EXITED = 0x20 + class TargetRegs(object): def __init__(self, target): self.target = target @@ -63,6 +65,7 @@ def __setattr__(self, name, value): elif name in self.target._arch.registers: return self.target.write_register(name, value) + class Target(object): """The Target object is one of Avatars core concept, as Avatar orchestrate different targets. @@ -100,11 +103,8 @@ def __init__(self, avatar, name=None): # type: ('Avatar', str) -> None log_file.setFormatter(formatter) self.log.addHandler(log_file) - - self.regs = TargetRegs(self) - @watch('TargetInit') def init(self): """ @@ -168,7 +168,6 @@ def step(self): self.wait() return ret - @watch('TargetWriteMemory') @action_valid_decorator_factory(TargetStates.STOPPED, '_memory_protocol') def write_memory(self, address, size, value, num_words=1, raw=False): diff --git a/avatar2/watchmen.py b/avatar2/watchmen.py index 6198ca17af..7f18d2b00a 100644 --- a/avatar2/watchmen.py +++ b/avatar2/watchmen.py @@ -1,6 +1,7 @@ from threading import Thread from functools import wraps + class WatchedTypes(object): watched_types = [ 'StateTransfer', @@ -91,18 +92,18 @@ def run(self): class WatchedEvent(object): # noinspection PyUnusedLocal - def __init__(self, watch_type, when, callback, async, *args, **kwargs): + def __init__(self, watch_type, when, callback, is_async, *args, **kwargs): self._callback = callback self.type = watch_type self.when = when - self.async = async + self.is_async = is_async def react(self, avatar, *args, **kwargs): if self._callback is None: raise Exception("No callback defined for watchmen of type %s" % self.type) else: - if self.async: + if self.is_async: thread = AsyncReaction(avatar, self._callback, *args, **kwargs) thread.start() else: