# Path: avatar2/archs/arm.py
# class ARM(Architecture):
# 
#     get_qemu_executable = Architecture.resolve(QEMU)
#     get_panda_executable = Architecture.resolve(PANDA)
#     get_gdb_executable  = Architecture.resolve(GDB_MULTI)
#     get_oocd_executable = Architecture.resolve(OPENOCD)
# 
# 
# 
#     qemu_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, 'ip': 12,
#                  'sp': 13, 'lr': 14, 'pc': 15, 'cpsr': 25,
#                  }
#     unicorn_registers = {'r0': UC_ARM_REG_R0, 'r1': UC_ARM_REG_R1, 'r2': UC_ARM_REG_R2,
#                          'r3': UC_ARM_REG_R3, 'r4': UC_ARM_REG_R4, 'r5': UC_ARM_REG_R5,
#                          'r6': UC_ARM_REG_R6, 'r7': UC_ARM_REG_R7, 'r8': UC_ARM_REG_R8,
#                          'r9': UC_ARM_REG_R9, 'r10': UC_ARM_REG_R10, 'r11': UC_ARM_REG_R11,
#                          'r12': UC_ARM_REG_R12, 'sp': UC_ARM_REG_SP, 'lr': UC_ARM_REG_LR,
#                          'pc': UC_ARM_REG_PC, 'cpsr': UC_ARM_REG_CPSR}
#     pc_name = 'pc'
#     sr_name = 'cpsr'
#     unemulated_instructions = ['mcr', 'mrc']
#     capstone_arch = CS_ARCH_ARM
#     capstone_mode = CS_MODE_LITTLE_ENDIAN
#     keystone_arch = KS_ARCH_ARM
#     keystone_mode = KS_MODE_ARM
#     unicorn_arch = UC_ARCH_ARM
#     unicorn_mode = UC_MODE_ARM
# 
# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


import sys
import pylink
import logging
import re
    import Queue as queue
    import queue
from time import sleep
from threading import Thread, Event, Condition
from avatar2.archs.arm import ARM
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage

        if hasattr(self.origin, 'regs'):
            self.origin.regs._update(regs)

    def run(self):
        # Target state management thread
        # This thread needs to poll for the halted state
        # of the target
        # JLink is lame and doesn't let you do this asynch
        # Also, not all targets produce a "moe" (Mode of Entry) 
        # so we have to actually do that here.
        try:
            while not self._shutdown.is_set():
                is_halted = self.jlink.halted()
                if is_halted and self.origin.state == TargetStates.RUNNING:
                    # We just halted
                    # But did we hit a BP?
                    self.log.debug("JLink Target is halting...")
                    avatar_msg = UpdateStateMessage(self.origin, TargetStates.STOPPED)
                    self.avatar.fast_queue.put(avatar_msg)
                    self.origin.wait()
                    self.log.debug("JLink target has halted")
                    pc = self.get_pc()
                    if self.jlink.breakpoint_find(pc):
                        self.log.debug("JLink Target hit breakpoint %d" % self.jlink.breakpoint_find(pc))
                        avatar_msg = BreakpointHitMessage(self.origin, self.jlink.breakpoint_find(pc), pc)
                        self.avatar.queue.put(avatar_msg)

                elif not is_halted and self.origin.state == TargetStates.STOPPED:
                    self.log.debug("About to resume target.")

# Path: avatar2/archs/arm.py
# class ARM(Architecture):
# 
#     get_qemu_executable = Architecture.resolve(QEMU)
#     get_panda_executable = Architecture.resolve(PANDA)
#     get_gdb_executable  = Architecture.resolve(GDB_MULTI)
#     get_oocd_executable = Architecture.resolve(OPENOCD)
# 
# 
# 
#     qemu_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, 'ip': 12,
#                  'sp': 13, 'lr': 14, 'pc': 15, 'cpsr': 25,
#                  }
#     unicorn_registers = {'r0': UC_ARM_REG_R0, 'r1': UC_ARM_REG_R1, 'r2': UC_ARM_REG_R2,
#                          'r3': UC_ARM_REG_R3, 'r4': UC_ARM_REG_R4, 'r5': UC_ARM_REG_R5,
#                          'r6': UC_ARM_REG_R6, 'r7': UC_ARM_REG_R7, 'r8': UC_ARM_REG_R8,
#                          'r9': UC_ARM_REG_R9, 'r10': UC_ARM_REG_R10, 'r11': UC_ARM_REG_R11,
#                          'r12': UC_ARM_REG_R12, 'sp': UC_ARM_REG_SP, 'lr': UC_ARM_REG_LR,
#                          'pc': UC_ARM_REG_PC, 'cpsr': UC_ARM_REG_CPSR}
#     pc_name = 'pc'
#     sr_name = 'cpsr'
#     unemulated_instructions = ['mcr', 'mrc']
#     capstone_arch = CS_ARCH_ARM
#     capstone_mode = CS_MODE_LITTLE_ENDIAN
#     keystone_arch = KS_ARCH_ARM
#     keystone_mode = KS_MODE_ARM
#     unicorn_arch = UC_ARCH_ARM
#     unicorn_mode = UC_MODE_ARM
# 
# Path: avatar2/targets/target.py
# class TargetStates(IntEnum):
#     """
#     A simple Enum for the different states a target can be in.
#     """
#     CREATED = 0x1
#     INITIALIZED = 0x2
#     STOPPED = 0x4
#     RUNNING = 0x8
#     SYNCING = 0x10
#     EXITED = 0x20
#     NOT_RUNNING = INITIALIZED | STOPPED
#     BREAKPOINT = SYNCING | STOPPED
# 
# Path: avatar2/message.py
# class AvatarMessage(object):
#     def __init__(self, origin):
#         self.origin = origin
# 
#     def __str__(self):
#         if self.origin:
#             return "%s from %s" % (self.__class__.__name__, self.origin.name)
#         else:
#             return "%s from unkown origin" % self.__class__.__name__
# 
# class UpdateStateMessage(AvatarMessage):
#     def __init__(self, origin, new_state):
#         super(UpdateStateMessage, self).__init__(origin)
#         self.state = new_state
# 
# class BreakpointHitMessage(UpdateStateMessage):
#     def __init__(self, origin, breakpoint_number, address):
#         super(BreakpointHitMessage, self).__init__(origin, TargetStates.BREAKPOINT)
#         self.breakpoint_number = breakpoint_number
#         self.address = address


# Path: avatar2/protocols/jlink.py
import sys
import pylink
import logging
import re
    import Queue as queue
    import queue
from time import sleep
from threading import Thread, Event, Condition
from avatar2.archs.arm import ARM
from avatar2.targets import TargetStates
from avatar2.message import AvatarMessage, UpdateStateMessage, BreakpointHitMessage


        if hasattr(self.origin, 'regs'):
            self.origin.regs._update(regs)

    def run(self):
        # Target state management thread
        # This thread needs to poll for the halted state
        # of the target
        # JLink is lame and doesn't let you do this asynch
        # Also, not all targets produce a "moe" (Mode of Entry) 
        # so we have to actually do that here.
        try:
            while not self._shutdown.is_set():
                is_halted = self.jlink.halted()
                if is_halted and self.origin.state == TargetStates.RUNNING:
                    # We just halted
                    # But did we hit a BP?
                    self.log.debug("JLink Target is halting...")
                    avatar_msg = UpdateStateMessage(self.origin, TargetStates.STOPPED)
                    self.avatar.fast_queue.put(avatar_msg)
                    self.origin.wait()
                    self.log.debug("JLink target has halted")
                    pc = self.get_pc()
                    if self.jlink.breakpoint_find(pc):
                        self.log.debug("JLink Target hit breakpoint %d" % self.jlink.breakpoint_find(pc))
                        avatar_msg = BreakpointHitMessage(self.origin, self.jlink.breakpoint_find(pc), pc)
                        self.avatar.queue.put(avatar_msg)

                elif not is_halted and self.origin.state == TargetStates.STOPPED:
                    self.log.debug("About to resume target.")

                    avatar_msg = UpdateStateMessage(self.origin, TargetStates.RUNNING)