예제 #1
0
파일: gdb_target.py 프로젝트: ufwt/avatar2
    def init(self):

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar_queue=self.avatar.queue,
                          origin=self)

        if not self._serial:
            if gdb.remote_connect(port=self.gdb_port):
                self.log.info("Connected to Target")
            else:
                self.log.warning("Connecting failed")
        else:
            if gdb.remote_connect_serial(device=self.gdb_serial_device,
                                         baud_rate=self.gdb_serial_baud_rate,
                                         parity=self.gdb_serial_parity):
                self.log.info("Connected to Target")
            else:
                self.log.warning("Connecting failed")

        self._exec_protocol = gdb
        self._memory_protocol = gdb
        self._register_protocol = gdb
        self._signal_protocol = gdb
        self._monitor_protocol = None

        self.wait()
예제 #2
0
    def init(self):
        openocd = OpenOCDProtocol(
            self.openocd_script,
            openocd_executable=self.executable,
            additional_args=self.additional_args,
            telnet_port=self.telnet_port,
            gdb_port=self.gdb_port,
            origin=self,
            output_directory=self.avatar.output_directory)

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar,
                          origin=self)

        time.sleep(.1)  # give openocd time to start. Find a better solution?

        if openocd.connect() and gdb.remote_connect(port=self.gdb_port):
            openocd.reset()
            self.log.info("Connected to Target")
        else:
            self.log.warning("Connecting failed")

        self.protocols.set_all(gdb)
        self.protocols.monitor = openocd

        self.wait()
예제 #3
0
    def init(self, cmd_line=None):
        """
        Spawns a Qemu process and connects to it
        """

        if self.cpu_model is None:
            if hasattr(self._arch, 'cpu_model'):
                self.cpu_model = self.avatar.arch.cpu_model
            else:
                self.log.warning('No cpu_model specified - are you sure?')

        if cmd_line is None:
            cmd_line = self.assemble_cmd_line()

        with open(
                "%s/%s" %
            (self.avatar.output_directory, self.QEMU_CONFIG_FILE),
                "w") as conf_file:
            conf_dict = self.generate_configuration()
            json.dump(conf_dict, conf_file)

        with open("%s/%s_out.txt" % (self.avatar.output_directory, self.name)
                , "wb") as out, \
                open("%s/%s_err.txt" % (self.avatar.output_directory, self.name)
                    , "wb") as err:
            self._process = Popen(cmd_line, stdout=out, stderr=err)
        self.log.debug("QEMU command line: %s" % ' '.join(cmd_line))
        self.log.info("QEMU process running")

        gdb = GDBProtocol(
            gdb_executable=self.gdb_executable,
            arch=self.avatar.arch,
            verbose=self.gdb_verbose,
            additional_args=self.gdb_additional_args,
            avatar=self.avatar,
            origin=self,
        )
        qmp = QMPProtocol(self.qmp_port, origin=self)  # TODO: Implement QMP

        if 'avatar-rmemory' in [
                i[2].qemu_name for i in self._memory_mapping.iter()
                if hasattr(i[2], 'qemu_name')
        ]:
            rmp = RemoteMemoryProtocol(self.rmem_tx_queue_name,
                                       self.rmem_rx_queue_name,
                                       self.avatar.queue, self)
        else:
            rmp = None

        self.protocols.set_all(gdb)
        self.protocols.monitor = qmp
        self.protocols.remote_memory = rmp

        if gdb.remote_connect(port=self.gdb_port) and qmp.connect():
            self.log.info("Connected to remote target")
        else:
            self.log.warning("Connection to remote target failed")
        if rmp:
            rmp.connect()
        self.wait()
예제 #4
0
    def init(self):
        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar, origin=self)

        simavr = SimAvrProtocol(self.firmware,
                                simavr_executable=self.simavr_executable,
                                flash=self.flash, eeprom=self.eeprom,
                                additional_args=self.additional_args,
                                verbose_level=self.verbose_level,
                                serial_terminator=self.serial_terminator,
                                pty_number=self.pty_number,
                                serial_baud_rate=self.serial_baud_rate,
                                core_frequency=self.core_frequency,
                                core=self.core,
                                origin=self,
                                output_dir=self.avatar.output_directory)

        if simavr.is_running() and gdb.remote_connect(port=self.gdb_port):
            self.log.info("Connected to target")
        else:
            self.log.warning("Connection failed")

        self.protocols.set_all(gdb)
        self.protocols.monitor = simavr

        self.wait()
예제 #5
0
def setup_inf_loop():
    global p, g
    p = subprocess.Popen(
        'gdbserver --once 127.0.0.1:%d %s/tests/binaries/infinite_loop' %
        (port, os.getcwd()),
        shell=True)
    g = GDBProtocol(arch=avatar2.archs.X86)
    g.remote_connect(port=port)
예제 #6
0
    def _connect_protocols(self):
        """
        Internal routine to connect the various protocols to a running qemu
        """

        gdb = GDBProtocol(
            gdb_executable=self.gdb_executable,
            arch=self.avatar.arch,
            verbose=self.gdb_verbose,
            additional_args=self.gdb_additional_args,
            avatar=self.avatar,
            origin=self,
        )
        qmp = QMPProtocol(self.qmp_port, origin=self)

        if "avatar-rmemory" in [
                i[2].qemu_name for i in self._memory_mapping.iter()
                if hasattr(i[2], "qemu_name")
        ] and RemoteMemoryProtocol is not None:
            rmp = RemoteMemoryProtocol(
                self._rmem_tx_queue_name,
                self._rmem_rx_queue_name,
                self.avatar.queue,
                self,
            )
        else:
            rmp = None

        self.protocols.set_all(gdb)
        self.protocols.monitor = qmp
        self.protocols.remote_memory = rmp

        connect_success = True

        if self.gdb_unix_socket_path:
            connect_success = connect_success and gdb.remote_connect_unix(
                self.gdb_unix_socket_path)
        else:
            connect_success = connect_success and gdb.remote_connect(
                port=self.gdb_port)

        connect_success = connect_success and qmp.connect()

        if connect_success:
            self.log.info("Connected to remote target")
        else:
            self.log.warning("Connection to remote target failed")

        if rmp:
            rmp.connect()
        self.wait()
예제 #7
0
def setup_inf_loop():
    global p, g, port

    binary = '%s/tests/binaries/infinite_loop' % os.getcwd()
    p = subprocess.Popen(['gdbserver', '--once', '127.0.0.1:%d' % port, binary],
                        stderr=subprocess.PIPE)

    out = str(p.stderr.readline())
    assert_equal(binary in out, True)
    out = str(p.stderr.readline())
    assert_equal(str(port) in out, True)

    g = GDBProtocol(arch=avatar2.archs.X86_64)
    g.remote_connect(port=port)
예제 #8
0
class GdbPluginTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def setup_avatar_gdb_server(self):

        self.avatar = avatar2.Avatar(arch=avatar2.archs.X86_64)
        self.gdb_target = self.avatar.add_target(avatar2.GDBTarget,
                                                 gdb_port=AV_GDB_PORT)

        self.avatar.init_targets()
        self.avatar.load_plugin('gdbserver')
        self.sk = self.avatar.spawn_gdb_server(self.gdb_target, PORT, True,
                                               XML_PATH)

    def setup_env(self, binary):

        self.process = subprocess.Popen(
            ['gdbserver', '--once',
             '127.0.0.1:%d' % AV_GDB_PORT, binary],
            stderr=subprocess.PIPE)

        out = str(self.process.stderr.readline())
        self.assertEqual(binary in out, True, out)
        out = str(self.process.stderr.readline())
        self.assertEqual(str(AV_GDB_PORT) in out, True, out)

        # create avatar instance offering the gdbserver
        self.setup_avatar_gdb_server()

        self.gdb = GDBProtocol(arch=avatar2.archs.X86_64)
        self.gdb.remote_connect(port=PORT)

    def wait_stopped(self):
        # As we do not have access to avatar synchronizing target states
        # on this level, we apply this little hack to synchronize the target
        while True:
            ret, out = self.gdb.console_command('info program')
            if 'Program stopped' in out:
                break
            time.sleep(SLEEP_TIME)

    def tearDown(self):
        self.sk.shutdown()
        self.avatar.shutdown()
        self.gdb.shutdown()
        self.process.terminate()
예제 #9
0
    def setup_env(self, binary):

        self.process = subprocess.Popen(
            ['gdbserver', '--once',
             '127.0.0.1:%d' % AV_GDB_PORT, binary],
            stderr=subprocess.PIPE)

        out = str(self.process.stderr.readline())
        self.assertEqual(binary in out, True, out)
        out = str(self.process.stderr.readline())
        self.assertEqual(str(AV_GDB_PORT) in out, True, out)

        # create avatar instance offering the gdbserver
        self.setup_avatar_gdb_server()

        self.gdb = GDBProtocol(arch=avatar2.archs.X86_64)
        self.gdb.remote_connect(port=PORT)
예제 #10
0
def setup_helloworld():
    global p, g, port

    binary = '%s/tests/binaries/hello_world' % os.getcwd()
    p = subprocess.Popen(
        ['gdbserver', '--once',
         '127.0.0.1:%d' % avatar_gdb_port, binary],
        stderr=subprocess.PIPE)

    out = str(p.stderr.readline())
    assert_equal(binary in out, True)
    out = str(p.stderr.readline())
    assert_equal(str(avatar_gdb_port) in out, True)

    # create avatar instance offering the gdbserver
    setup_avatar_gdb_server()

    g = GDBProtocol(arch=avatar2.archs.X86_64)
    g.remote_connect(port=port)
예제 #11
0
    def init(self):

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar,
                          origin=self,
                          enable_init_files=self._enable_init_files,
                          binary=self._local_binary,
                          local_arguments=self._arguments,
                          verbose=self._verbose_gdbmi)

        # If we are debugging a program locally,
        # we do not need to establish any connections
        if not self._local_binary:
            if self.gdb_unix_socket_path is not None:
                if gdb.remote_connect_unix(self.gdb_unix_socket_path):
                    self.log.info("Connected to Target")
                else:
                    self.log.warning("Connecting failed")
            elif not self._serial:
                if gdb.remote_connect(ip=self.gdb_ip, port=self.gdb_port):
                    self.log.info("Connected to Target")
                else:
                    self.log.warning("Connecting failed")
            else:
                if gdb.remote_connect_serial(
                        device=self.gdb_serial_device,
                        baud_rate=self.gdb_serial_baud_rate,
                        parity=self.gdb_serial_parity):
                    self.log.info("Connected to Target")
                else:
                    self.log.warning("Connecting failed")
        else:
            self.update_state(TargetStates.INITIALIZED)

        self.protocols.set_all(gdb)

        if self._local_binary:
            self.wait(state=TargetStates.INITIALIZED)
        else:
            self.wait()
예제 #12
0
    def init(self):
        openocd = OpenOCDProtocol(
            self.avatar,
            self,
            self.openocd_script,
            openocd_executable=self.executable,
            additional_args=self.additional_args,
            tcl_port=self.tcl_port,
            gdb_port=self.gdb_port,
            output_directory=self.avatar.output_directory)
        time.sleep(.1)  # give openocd time to start. Find a better solution?
        self.log.debug("Connecting to OpenOCD telnet port")
        ocd_connected = openocd.connect()

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar,
                          origin=self)
        self.log.debug("Connecting to OpenOCD GDB port")
        gdb_connected = gdb.remote_connect(port=self.gdb_port)
        script_has_reset = False
        if self.openocd_script:
            with open(self.openocd_script) as f:
                script = f.read()
            if "reset halt" in script:
                self.log.debug(
                    "Not resetting target, script may have done it already")
                script_has_reset = True
        if ocd_connected:
            self.log.info("Successfully connected to OpenOCD target!")
        else:
            self.log.error("Failed to connect to OpenOCD target!")
        if ocd_connected and not script_has_reset:
            self.log.debug("Resetting target...")
            openocd.reset()

        self.protocols.set_all(gdb)
        self.protocols.monitor = openocd
        self.wait()
예제 #13
0
    def init(self):

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self._arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar, origin=self)

        if not self._serial:
            if gdb.remote_connect(ip=self.gdb_ip, port=self.gdb_port):
                self.log.info("Connected to Target")
            else:
                self.log.warning("Connecting failed")
        else:
            if gdb.remote_connect_serial(device=self.gdb_serial_device,
                                         baud_rate=self.gdb_serial_baud_rate,
                                         parity=self.gdb_serial_parity):
                self.log.info("Connected to Target")
            else:
                self.log.warning("Connecting failed")

        self.protocols.set_all(gdb)

        self.wait()
예제 #14
0
    def setup_env(self, binary, unix_socket=False):

        self.process = subprocess.Popen(
            ['gdbserver', '--once',
             '127.0.0.1:%d' % PORT, binary],
            stderr=subprocess.PIPE)

        out = str(self.process.stderr.readline())
        self.assertEqual(binary in out, True, out)
        out = str(self.process.stderr.readline())
        self.assertEqual(str(PORT) in out, True, out)

        self.gdb = GDBProtocol(arch=avatar2.archs.X86_64)

        if unix_socket is True:
            socket_path = '/tmp/test_socket'
            unix2tcp(socket_path, "127.0.0.1", PORT)
            self.gdb.remote_connect_unix(socket_path)

        else:
            self.gdb.remote_connect(port=PORT)

        # Base addresses can change across kernel versions due to PIE binaries
        self.base_address = self.gdb.get_symbol("main")[1] & ~0xfff
예제 #15
0
class GdbProtocolTestCase(unittest.TestCase):
    def setUp(self):
        pass

    def setup_env(self, binary, unix_socket=False):

        self.process = subprocess.Popen(
            ['gdbserver', '--once',
             '127.0.0.1:%d' % PORT, binary],
            stderr=subprocess.PIPE)

        out = str(self.process.stderr.readline())
        self.assertEqual(binary in out, True, out)
        out = str(self.process.stderr.readline())
        self.assertEqual(str(PORT) in out, True, out)

        self.gdb = GDBProtocol(arch=avatar2.archs.X86_64)

        if unix_socket is True:
            socket_path = '/tmp/test_socket'
            unix2tcp(socket_path, "127.0.0.1", PORT)
            self.gdb.remote_connect_unix(socket_path)

        else:
            self.gdb.remote_connect(port=PORT)

        # Base addresses can change across kernel versions due to PIE binaries
        self.base_address = self.gdb.get_symbol("main")[1] & ~0xfff

    def wait_stopped(self):
        # As we do not have access to avatar synchronizing target states
        # on this level, we apply this little hack to synchronize the target
        while True:
            ret, out = self.gdb.console_command('info program')
            if 'Program stopped' in out:
                break
            time.sleep(SLEEP_TIME)

    def tearDown(self):
        self.gdb.shutdown()
        self.process.terminate()
예제 #16
0
    def init(self):
        """
        Spawns a Qemu process and connects to it
        """

        if self.cpu_model is None:
            if hasattr(self._arch, 'cpu_model'):
                self.cpu_model = self.avatar.arch.cpu_model
            else:
                self.log.warning('No cpu_model specified - are you sure?')

        cmd_line = self.assemble_cmd_line()

        with open("%s/%s" % (self.avatar.output_directory,
                             self.QEMU_CONFIG_FILE), "w") as conf_file:
            conf_dict = self.generate_configuration()
            json.dump(conf_dict, conf_file)

        with open("%s/%s_out.txt" % (self.avatar.output_directory, self.name)
                , "wb") as out, \
                open("%s/%s_err.txt" % (self.avatar.output_directory, self.name)
                    , "wb") as err:
            self._process = Popen(cmd_line, stdout=out, stderr=err)
        self.log.debug("QEMU command line: %s" % ' '.join(cmd_line))
        self.log.info("QEMU process running")

        gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                          arch=self.avatar.arch,
                          additional_args=self.gdb_additional_args,
                          avatar=self.avatar, origin=self)
        qmp = QMPProtocol(self.qmp_port, origin=self)  # TODO: Implement QMP

        if 'avatar-rmemory' in [i[2].qemu_name for i in
                                self._memory_mapping.iter() if
                                hasattr(i[2], 'qemu_name')]:
            rmp = RemoteMemoryProtocol(self.rmem_tx_queue_name,
                                       self.rmem_rx_queue_name,
                                       self.avatar.queue, self)
        else:
            rmp = None

        self.protocols.set_all(gdb)
        self.protocols.gdb = gdb
        self.protocols.monitor = qmp
        self.protocols.remote_memory = rmp

        if gdb.remote_connect(port=self.gdb_port) and qmp.connect():
            self.log.info("Connected to remote target")
        else:
            self.log.warning("Connection to remote target failed")
        if rmp:
            rmp.connect()


        thread = None
        # Save a vm snapshot and get register values
        # thread = Thread(target=self.saveVMSnapshot, args=(qmp,gdb,))

        if self.interval == 10:
            # start a mutually exclusive thread with RMP
            thread = Thread(target=self.interrupt_injection, args=(qmp,self.interval))

        if thread:
            thread.daemon = True
            thread.start()




        self.wait()
예제 #17
0
    def init(self):

        if self.processor == 'cortex-m3':
            dmon = XSDBProtocol(avatar=self.avatar,
                                origin=self,
                                output_directory=self.avatar.output_directory)
            gdb = GDBProtocol(gdb_executable=self.gdb_executable,
                              arch=self._arch,
                              additional_args=self.gdb_additional_args,
                              avatar=self.avatar,
                              origin=self,
                              enable_init_files=self._enable_init_files,
                              local_arguments=self._arguments,
                              verbose=self._verbose_gdbmi)
        else:
            dmon = None
            self.log.warning("Target board not implemented")
            raise Exception("Target board not implemented")

        if dmon.connect():
            #dmon.execute_command("connect -url tcp:127.0.0.1:3121")
            dmon.execute_command("source %s" % self._ps7_init_file)
            dmon.execute_command(
                "targets -set -nocase -filter {name =~\"APU*\" && jtag_cable_name =~ \"Digilent Zed 210248A398A9\"} -index 0"
            )
            dmon.execute_command(
                "loadhw -hw %s -mem-ranges [list {0x40000000 0xbfffffff}]" %
                self._hdf_file)
            dmon.execute_command("configparams force-mem-access 1")
            dmon.execute_command(
                "targets -set -nocase -filter {name =~\"APU*\" && jtag_cable_name =~ \"Digilent Zed 210248A398A9\"} -index 0"
            )
            dmon.execute_command("stop")
            dmon.execute_command("ps7_init")
            dmon.execute_command("ps7_post_config")
            dmon.execute_command(
                "targets -set -nocase -filter {name =~ \"ARM*#0\" && jtag_cable_name =~ \"Digilent Zed 210248A398A9\"} -index 0"
            )
            dmon.execute_command("rst -processor")
            dmon.execute_command(
                "targets -set -nocase -filter {name =~ \"ARM*#0\" && jtag_cable_name =~ \"Digilent Zed 210248A398A9\"} -index 0"
            )
            dmon.execute_command("dow %s" % self._firmware)
            dmon.execute_command("configparams force-mem-access 0")
            #dmon.reset()
            dmon.execute_command("con")
            self.log.info("Connected to Target")
        else:
            self.log.warning("Connecting failed")
            raise Exception("Connecting to target failed")

        if gdb.remote_connect(ip=self.gdb_ip, port=self.gdb_port):
            self.log.info("Connected to Target")
        else:
            self.log.warning("Connecting failed")
            raise Exception("Connecting to target failed")

        self.update_state(TargetStates.STOPPED)

        #if dmon.stop():
        #   self.update_state(TargetStates.STOPPED)

        #self.protocols.set_all(dmon)
        self.protocols.set_all(gdb)
        self.protocols.monitor = gdb