Exemplo n.º 1
0
 def test_connect_multiple_clients(self):
     """
     Tests two or more connections to the same server raise an exception
     """
     self.log.info("Testing that multiple clients cannot connect at once")
     s = gdb.GDBServer()
     c1 = gdb.GDB()
     c1.connect(s.port)
     c2 = gdb.GDB()
     self.assertRaises(ValueError, c2.connect, s.port)
     s.exit()
Exemplo n.º 2
0
    def test_generate_core(self):
        """
        Load a file that will cause a segfault and produce a core dump
        """
        self.log.info("Testing that a core dump will be generated")

        g = gdb.GDB()
        file_cmd = "-file-exec-and-symbols %s" % self.segfault_binary_path
        r = g.cmd(file_cmd)
        self.assertEqual(r.result.class_, 'done')

        run_cmd = "-exec-run"
        r = g.cmd(run_cmd)
        self.assertEqual(r.result.class_, 'running')

        other_messages = g.read_until_break()
        core_path = None
        for msg in other_messages:
            parsed_msg = gdb.parse_mi(msg)
            if (hasattr(parsed_msg, 'class_')
                    and (parsed_msg.class_ == 'stopped')
                    and (parsed_msg.result.signal_name == 'SIGSEGV')):
                core_path = "%s.core" % self.segfault_binary_path
                gcore_cmd = 'gcore %s' % core_path
                gcore_cmd = gdb.encode_mi_cli(gcore_cmd)
                r = g.cmd(gcore_cmd)
                self.assertEqual(r.result.class_, 'done')

        self.assertTrue(os.path.exists(core_path))
        g.exit()
Exemplo n.º 3
0
    def __init__(self, cmd, verbose=True, allow_output_check='all',
                 shell=False, env=None):
        """
        Creates the subprocess object, stdout/err, reader threads and locks.

        :param cmd: Command line to run.
        :type cmd: str
        :param verbose: Whether to log the command run and stdout/stderr.
                        Currently unused and provided for compatibility only.
        :type verbose: bool
        :param allow_output_check: Whether to log the command stream outputs
                                   (stdout and stderr) in the test stream
                                   files. Valid values: 'stdout', for
                                   allowing only standard output, 'stderr',
                                   to allow only standard error, 'all',
                                   to allow both standard output and error
                                   (default), and 'none', to allow
                                   none to be recorded. Currently unused and
                                   provided for compatibility only.
        :type allow_output_check: str
        """

        self.cmd = cmd

        self.args = shlex.split(cmd)
        self.binary = self.args[0]
        self.binary_path = os.path.abspath(self.cmd)
        self.result = CmdResult(cmd)

        self.gdb_server = gdb.GDBServer(runtime.GDBSERVER_PATH)
        self.gdb = gdb.GDB(runtime.GDB_PATH)
        self.gdb.connect(self.gdb_server.port)
        self.gdb.set_file(self.binary)
Exemplo n.º 4
0
    def test_remote_exec(self):
        """
        Tests execution on a remote target
        """
        self.log.info("Testing execution on a remote target")
        hit_breakpoint = False

        s = gdb.GDBServer()
        g = gdb.GDB()

        cmd = '-file-exec-and-symbols %s' % self.return99_binary_path
        r = g.cmd(cmd)
        self.assertEqual(r.result.class_, 'done')

        cmd = 'set remote exec-file %s' % self.return99_binary_path
        r = g.cmd(cmd)
        self.assertEqual(r.result.class_, 'done')

        cmd = "-break-insert %s" % 'main'
        r = g.cmd(cmd)
        self.assertEqual(r.result.class_, 'done')

        r = g.cmd('-exec-run')

        other_messages = g.read_until_break()
        for msg in other_messages:
            parsed_msg = gdb.parse_mi(msg)
            if (hasattr(parsed_msg, 'class_')
                    and parsed_msg.class_ == 'stopped'
                    and parsed_msg.result.reason == 'breakpoint-hit'):
                hit_breakpoint = True

        self.assertTrue(hit_breakpoint)
        g.exit()
        s.exit()
Exemplo n.º 5
0
    def test_load_set_breakpoint_run_exit_raw(self):
        """
        Test a common GDB cycle using raw commands: load, set break, run, exit
        """
        self.log.info("Testing that GDB loads a file and sets a breakpoint")
        g = gdb.GDB()

        file_cmd = "-file-exec-and-symbols %s" % self.return99_binary_path
        r = g.cmd(file_cmd)
        self.assertEqual(r.result.class_, 'done')

        break_cmd = "-break-insert 5"
        r = g.cmd(break_cmd)
        self.assertEqual(r.result.class_, 'done')
        self.assertEqual(r.result.result.bkpt.number, '1')
        self.assertEqual(r.result.result.bkpt.enabled, 'y')

        break_del_cmd = "-break-delete 1"
        r = g.cmd(break_del_cmd)
        self.assertEqual(r.result.class_, 'done')

        run_cmd = "-exec-run"
        r = g.cmd(run_cmd)
        self.assertEqual(r.result.class_, 'running')

        g.cmd("-gdb-exit")
        self.assertEqual(g.process.wait(), 0)
Exemplo n.º 6
0
    def test_disconnect_raw(self):
        """
        Connect/disconnect repeatedly from a remote debugger using raw commands
        """
        self.log.info("Testing connecting and disconnecting repeatedly using "
                      "raw commands")
        s = gdb.GDBServer()
        g = gdb.GDB()

        # Do 100 cycle of target (kind of connects) and disconnects
        for i in xrange(0, 100):
            cmd = '-target-select extended-remote :%s' % s.port
            r = g.cmd(cmd)
            self.assertEqual(r.result.class_, 'connected')
            r = g.cmd('-target-disconnect')
            self.assertEqual(r.result.class_, 'done')

        # manual server shutdown
        cmd = '-target-select extended-remote :%s' % s.port
        r = g.cmd(cmd)
        self.assertEqual(r.result.class_, 'connected')
        r = g.cli_cmd('monitor exit')
        self.assertEqual(r.result.class_, 'done')

        g.exit()
        s.exit()
Exemplo n.º 7
0
 def test_stream_messages(self):
     """
     Tests if the expected response appears in the result stream messages
     """
     self.log.info("Testing that messages appears in the result stream")
     g = gdb.GDB()
     r = g.cmd("-gdb-version")
     self.assertIn("GNU GPL version", r.get_stream_messages_text())
Exemplo n.º 8
0
 def test_set_multiple_break(self):
     """
     Tests that multiple breakpoints do not interfere with each other
     """
     self.log.info("Testing setting multiple breakpoints")
     g = gdb.GDB()
     g.set_file(self.return99_binary_path)
     g.set_break('empty')
     g.set_break('7')
     g.exit()
Exemplo n.º 9
0
 def test_load_set_breakpoint_run_exit(self):
     """
     Test a common GDB cycle: load, set break, del break, run, exit
     """
     self.log.info("Testing a common GDB cycle")
     g = gdb.GDB()
     g.set_file(self.return99_binary_path)
     g.set_break("5")
     g.del_break(1)
     g.run()
     g.exit()
Exemplo n.º 10
0
    def test_start_exit(self):
        """
        Tests execution of multiple GDB instances without any blocking or race
        """
        self.log.info("Testing execution of multiple GDB instances")
        process_count = 10
        gdb_instances = []
        for i in xrange(0, process_count):
            gdb_instances.append(gdb.GDB())

        for i in xrange(0, process_count):
            self.assertEqual(gdb_instances[i].exit(), 0)
Exemplo n.º 11
0
    def test_existing_commands(self):
        g = gdb.GDB()

        self.log.info("Testing existing (valid) GDB commands using utility "
                      "methods")
        for cmd in self.VALID_CMDS:
            self.assertTrue(g.cmd_exists(cmd))
            g.cmd(cmd)

        self.log.info("Testing non-existing (invalid) GDB commands using "
                      "utility methods")
        for cmd in self.INVALID_CMDS:
            self.assertFalse(g.cmd_exists(cmd))
Exemplo n.º 12
0
    def test_server_stdout(self):
        self.log.info('Testing server stdout/stderr collection')
        s = gdb.GDBServer()
        c = gdb.GDB()
        c.connect(s.port)
        c.set_file(self.return99_binary_path)
        c.run()
        s.exit()

        self.assertTrue(os.path.exists(s.stdout_path))
        self.assertTrue(os.path.exists(s.stderr_path))

        stdout_lines = open(s.stdout_path, 'r').readlines()
        self.assertIn("return 99\n", stdout_lines)
Exemplo n.º 13
0
 def action(self):
     """
     Execute 'print_variable'.
     """
     path = os.path.join(self.srcdir, self.__binary)
     app = gdb.GDB()
     app.set_file(path)
     app.set_break(6)
     app.run()
     self.log.info("\n".join(app.read_until_break()))
     app.cmd("set variable a = 0xff")
     app.cmd("c")
     out = "\n".join(app.read_until_break())
     self.log.info(out)
     app.exit()
     self.assertIn("MY VARIABLE 'A' IS: ff", out)
Exemplo n.º 14
0
    def test_server_stderr_redirected(self):
        s = gdb.GDBServer()
        if not s.redirected_stderr:
            s.exit()
            return

        self.log.info('Testing server stderr redirection')
        c = gdb.GDB()
        c.connect(s.port)
        c.set_file(self.return99_binary_path)
        c.run()
        s.exit()

        # only thing in stderr should be the output generated by return99
        stderr = open(s.stderr_path, 'r').read()
        self.assertEqual("testing output to stderr\n", stderr)
Exemplo n.º 15
0
    def test_disconnect(self):
        """
        Connect/disconnect repeatedly from a remote debugger using utilities
        """
        self.log.info("Testing connecting and disconnecting repeatedly")
        s = gdb.GDBServer()
        g = gdb.GDB()

        for i in xrange(0, 100):
            r = g.connect(s.port)
            self.assertEqual(r.result.class_, 'connected')
            r = g.disconnect()
            self.assertEqual(r.result.class_, 'done')

        g.exit()
        s.exit()
Exemplo n.º 16
0
    def test_existing_commands_raw(self):
        """
        Tests the GDB response to commands that exist and to those that do not
        """
        g = gdb.GDB()
        self.log.info(
            "Testing existing (valid) GDB commands using raw commands")
        for cmd in self.VALID_CMDS:
            info_cmd = "-info-gdb-mi-command %s" % cmd[1:]
            r = g.cmd(info_cmd)
            self.assertEqual(r.result.result.command.exists, 'true')

        self.log.info("Testing non-existing (invalid) GDB commands using raw "
                      "commands")
        for cmd in self.INVALID_CMDS:
            info_cmd = "-info-gdb-mi-command %s" % cmd[1:]
            r = g.cmd(info_cmd)
            self.assertEqual(r.result.result.command.exists, 'false')
Exemplo n.º 17
0
    def test_multiple_servers(self):
        """
        Tests multiple server instances without any blocking or race condition
        """
        self.log.info("Testing execution of multiple GDB server instances")
        process_count = 10
        server_instances = []
        for i in xrange(0, process_count):
            s = gdb.GDBServer()
            c = gdb.GDB()
            c.connect(s.port)
            c.cmd('show-version')
            c.disconnect()
            server_instances.append(s)

        for i in xrange(0, process_count):
            self.assertTrue(self.is_process_alive(server_instances[i].process))
            server_instances[i].exit()
            self.assertFalse(self.is_process_alive(
                server_instances[i].process))