Skip to content

Commit

Permalink
Additional auto PEP8-ification
Browse files Browse the repository at this point in the history
  • Loading branch information
domenukk committed Jun 27, 2017
1 parent 07bc6e7 commit 3134443
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 37 deletions.
15 changes: 8 additions & 7 deletions avatar2/archs/arm.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand All @@ -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):
Expand Down
16 changes: 8 additions & 8 deletions avatar2/archs/x86.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from .architecture import Architecture


class X86(Architecture):
qemu_name = 'i386'
gdb_name = 'i386'
Expand All @@ -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'
Expand All @@ -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 = []
22 changes: 9 additions & 13 deletions avatar2/protocols/qmp.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys

if sys.version_info < (3, 0):
import Queue as queue
else:
Expand All @@ -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()

Expand All @@ -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
Expand All @@ -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):
"""
Expand All @@ -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
Expand All @@ -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])
4 changes: 2 additions & 2 deletions avatar2/targets/gdb_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand All @@ -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
Expand Down
7 changes: 3 additions & 4 deletions avatar2/targets/target.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -46,6 +47,7 @@ class TargetStates(Enum):
SYNCING = 0x10
EXITED = 0x20


class TargetRegs(object):
def __init__(self, target):
self.target = target
Expand All @@ -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.
Expand Down Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 4 additions & 3 deletions avatar2/watchmen.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from threading import Thread
from functools import wraps


class WatchedTypes(object):
watched_types = [
'StateTransfer',
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 3134443

Please sign in to comment.