xref: /openbsd-src/gnu/llvm/lldb/packages/Python/lldbsuite/test/lldbgdbclient.py (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1*f6aab3d8Srobertimport os
2*f6aab3d8Srobertimport os.path
3*f6aab3d8Srobertimport lldb
4*f6aab3d8Srobertfrom lldbsuite.test.lldbtest import *
5*f6aab3d8Srobertfrom lldbsuite.test.gdbclientutils import *
6*f6aab3d8Srobert
7*f6aab3d8Srobertclass GDBRemoteTestBase(TestBase):
8*f6aab3d8Srobert    """
9*f6aab3d8Srobert    Base class for GDB client tests.
10*f6aab3d8Srobert
11*f6aab3d8Srobert    This class will setup and start a mock GDB server for the test to use.
12*f6aab3d8Srobert    It also provides assertPacketLogContains, which simplifies the checking
13*f6aab3d8Srobert    of packets sent by the client.
14*f6aab3d8Srobert    """
15*f6aab3d8Srobert
16*f6aab3d8Srobert    NO_DEBUG_INFO_TESTCASE = True
17*f6aab3d8Srobert    server = None
18*f6aab3d8Srobert    server_socket_class = TCPServerSocket
19*f6aab3d8Srobert
20*f6aab3d8Srobert    def setUp(self):
21*f6aab3d8Srobert        TestBase.setUp(self)
22*f6aab3d8Srobert        self.server = MockGDBServer(self.server_socket_class())
23*f6aab3d8Srobert        self.server.start()
24*f6aab3d8Srobert
25*f6aab3d8Srobert    def tearDown(self):
26*f6aab3d8Srobert        # TestBase.tearDown will kill the process, but we need to kill it early
27*f6aab3d8Srobert        # so its client connection closes and we can stop the server before
28*f6aab3d8Srobert        # finally calling the base tearDown.
29*f6aab3d8Srobert        if self.process() is not None:
30*f6aab3d8Srobert            self.process().Kill()
31*f6aab3d8Srobert        self.server.stop()
32*f6aab3d8Srobert        TestBase.tearDown(self)
33*f6aab3d8Srobert
34*f6aab3d8Srobert    def createTarget(self, yaml_path):
35*f6aab3d8Srobert        """
36*f6aab3d8Srobert        Create a target by auto-generating the object based on the given yaml
37*f6aab3d8Srobert        instructions.
38*f6aab3d8Srobert
39*f6aab3d8Srobert        This will track the generated object so it can be automatically removed
40*f6aab3d8Srobert        during tearDown.
41*f6aab3d8Srobert        """
42*f6aab3d8Srobert        yaml_base, ext = os.path.splitext(yaml_path)
43*f6aab3d8Srobert        obj_path = self.getBuildArtifact(yaml_base)
44*f6aab3d8Srobert        self.yaml2obj(yaml_path, obj_path)
45*f6aab3d8Srobert        return self.dbg.CreateTarget(obj_path)
46*f6aab3d8Srobert
47*f6aab3d8Srobert    def connect(self, target):
48*f6aab3d8Srobert        """
49*f6aab3d8Srobert        Create a process by connecting to the mock GDB server.
50*f6aab3d8Srobert
51*f6aab3d8Srobert        Includes assertions that the process was successfully created.
52*f6aab3d8Srobert        """
53*f6aab3d8Srobert        listener = self.dbg.GetListener()
54*f6aab3d8Srobert        error = lldb.SBError()
55*f6aab3d8Srobert        process = target.ConnectRemote(listener,
56*f6aab3d8Srobert                self.server.get_connect_url(), "gdb-remote", error)
57*f6aab3d8Srobert        self.assertTrue(error.Success(), error.description)
58*f6aab3d8Srobert        self.assertTrue(process, PROCESS_IS_VALID)
59*f6aab3d8Srobert        return process
60*f6aab3d8Srobert
61*f6aab3d8Srobert    def assertPacketLogContains(self, packets, log=None):
62*f6aab3d8Srobert        """
63*f6aab3d8Srobert        Assert that the mock server's packet log contains the given packets.
64*f6aab3d8Srobert
65*f6aab3d8Srobert        The packet log includes all packets sent by the client and received
66*f6aab3d8Srobert        by the server.  This fuction makes it easy to verify that the client
67*f6aab3d8Srobert        sent the expected packets to the server.
68*f6aab3d8Srobert
69*f6aab3d8Srobert        The check does not require that the packets be consecutive, but does
70*f6aab3d8Srobert        require that they are ordered in the log as they ordered in the arg.
71*f6aab3d8Srobert        """
72*f6aab3d8Srobert        if log is None:
73*f6aab3d8Srobert            log = self.server.responder.packetLog
74*f6aab3d8Srobert        i = 0
75*f6aab3d8Srobert        j = 0
76*f6aab3d8Srobert
77*f6aab3d8Srobert        while i < len(packets) and j < len(log):
78*f6aab3d8Srobert            if log[j] == packets[i]:
79*f6aab3d8Srobert                i += 1
80*f6aab3d8Srobert            j += 1
81*f6aab3d8Srobert        if i < len(packets):
82*f6aab3d8Srobert            self.fail(u"Did not receive: %s\nLast 10 packets:\n\t%s" %
83*f6aab3d8Srobert                    (packets[i], u'\n\t'.join(log)))
84*f6aab3d8Srobert
85*f6aab3d8Srobert
86*f6aab3d8Srobertclass GDBPlatformClientTestBase(GDBRemoteTestBase):
87*f6aab3d8Srobert    """
88*f6aab3d8Srobert    Base class for platform server clients.
89*f6aab3d8Srobert
90*f6aab3d8Srobert    This class extends GDBRemoteTestBase by automatically connecting
91*f6aab3d8Srobert    via "platform connect" in the setUp() method.
92*f6aab3d8Srobert    """
93*f6aab3d8Srobert
94*f6aab3d8Srobert    def setUp(self):
95*f6aab3d8Srobert        super().setUp()
96*f6aab3d8Srobert        self.runCmd("platform select remote-gdb-server")
97*f6aab3d8Srobert        self.runCmd("platform connect " + self.server.get_connect_url())
98*f6aab3d8Srobert        self.assertTrue(self.dbg.GetSelectedPlatform().IsConnected())
99*f6aab3d8Srobert
100*f6aab3d8Srobert    def tearDown(self):
101*f6aab3d8Srobert        self.dbg.GetSelectedPlatform().DisconnectRemote()
102*f6aab3d8Srobert        super().tearDown()
103