xref: /llvm-project/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1706cccb8SJonas Devlieghereimport lldb
2706cccb8SJonas Devlieghereimport binascii
3706cccb8SJonas Devlieghereimport os
4706cccb8SJonas Devliegherefrom lldbsuite.test.lldbtest import *
5706cccb8SJonas Devliegherefrom lldbsuite.test.decorators import *
633c0f93fSPavel Labathfrom lldbsuite.test.gdbclientutils import *
733c0f93fSPavel Labathfrom lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
8706cccb8SJonas Devlieghere
9706cccb8SJonas Devlieghere
1031137b87SJonas Devlieghere@skipIfRemote
11706cccb8SJonas Devlieghereclass TestProcessConnect(GDBRemoteTestBase):
12f5f15aceSJonas Devlieghere    NO_DEBUG_INFO_TESTCASE = True
13f5f15aceSJonas Devlieghere
14706cccb8SJonas Devlieghere    def test_gdb_remote_sync(self):
15706cccb8SJonas Devlieghere        """Test the gdb-remote command in synchronous mode"""
16706cccb8SJonas Devlieghere        try:
17706cccb8SJonas Devlieghere            self.dbg.SetAsync(False)
18*2238dcc3SJonas Devlieghere            self.expect(
19*2238dcc3SJonas Devlieghere                "gdb-remote " + self.server.get_connect_address(),
20*2238dcc3SJonas Devlieghere                substrs=["Process", "stopped"],
21*2238dcc3SJonas Devlieghere            )
22706cccb8SJonas Devlieghere        finally:
2345f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
24706cccb8SJonas Devlieghere
25706cccb8SJonas Devlieghere    def test_gdb_remote_async(self):
26706cccb8SJonas Devlieghere        """Test the gdb-remote command in asynchronous mode"""
27706cccb8SJonas Devlieghere        try:
28706cccb8SJonas Devlieghere            self.dbg.SetAsync(True)
29*2238dcc3SJonas Devlieghere            self.expect(
30*2238dcc3SJonas Devlieghere                "gdb-remote " + self.server.get_connect_address(),
31706cccb8SJonas Devlieghere                matching=False,
32*2238dcc3SJonas Devlieghere                substrs=["Process", "stopped"],
33*2238dcc3SJonas Devlieghere            )
34*2238dcc3SJonas Devlieghere            lldbutil.expect_state_changes(
35*2238dcc3SJonas Devlieghere                self, self.dbg.GetListener(), self.process(), [lldb.eStateStopped]
36*2238dcc3SJonas Devlieghere            )
37706cccb8SJonas Devlieghere        finally:
3845f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
39*2238dcc3SJonas Devlieghere        lldbutil.expect_state_changes(
40*2238dcc3SJonas Devlieghere            self, self.dbg.GetListener(), self.process(), [lldb.eStateExited]
41*2238dcc3SJonas Devlieghere        )
42706cccb8SJonas Devlieghere
43a8f6f4e8SMichał Górny    @skipIfWindows
44706cccb8SJonas Devlieghere    def test_process_connect_sync(self):
45706cccb8SJonas Devlieghere        """Test the gdb-remote command in synchronous mode"""
46706cccb8SJonas Devlieghere        try:
47706cccb8SJonas Devlieghere            self.dbg.SetAsync(False)
48*2238dcc3SJonas Devlieghere            self.expect(
49*2238dcc3SJonas Devlieghere                "platform select remote-gdb-server",
50*2238dcc3SJonas Devlieghere                substrs=["Platform: remote-gdb-server", "Connected: no"],
51*2238dcc3SJonas Devlieghere            )
52*2238dcc3SJonas Devlieghere            self.expect(
53*2238dcc3SJonas Devlieghere                "process connect " + self.server.get_connect_url(),
54*2238dcc3SJonas Devlieghere                substrs=["Process", "stopped"],
55*2238dcc3SJonas Devlieghere            )
56706cccb8SJonas Devlieghere        finally:
5745f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
58706cccb8SJonas Devlieghere
59a8f6f4e8SMichał Górny    @skipIfWindows
60706cccb8SJonas Devlieghere    def test_process_connect_async(self):
61706cccb8SJonas Devlieghere        """Test the gdb-remote command in asynchronous mode"""
62706cccb8SJonas Devlieghere        try:
63706cccb8SJonas Devlieghere            self.dbg.SetAsync(True)
64*2238dcc3SJonas Devlieghere            self.expect(
65*2238dcc3SJonas Devlieghere                "platform select remote-gdb-server",
66*2238dcc3SJonas Devlieghere                substrs=["Platform: remote-gdb-server", "Connected: no"],
67*2238dcc3SJonas Devlieghere            )
68*2238dcc3SJonas Devlieghere            self.expect(
69*2238dcc3SJonas Devlieghere                "process connect " + self.server.get_connect_url(),
70706cccb8SJonas Devlieghere                matching=False,
71*2238dcc3SJonas Devlieghere                substrs=["Process", "stopped"],
72*2238dcc3SJonas Devlieghere            )
73*2238dcc3SJonas Devlieghere            lldbutil.expect_state_changes(
74*2238dcc3SJonas Devlieghere                self, self.dbg.GetListener(), self.process(), [lldb.eStateStopped]
75*2238dcc3SJonas Devlieghere            )
76706cccb8SJonas Devlieghere        finally:
7745f97950SMichał Górny            self.dbg.GetSelectedTarget().GetProcess().Kill()
78*2238dcc3SJonas Devlieghere        lldbutil.expect_state_changes(
79*2238dcc3SJonas Devlieghere            self, self.dbg.GetListener(), self.process(), [lldb.eStateExited]
80*2238dcc3SJonas Devlieghere        )
81*2238dcc3SJonas Devlieghere
8208c4a679SPavel Labath    def test_breakpoint_count(self):
8308c4a679SPavel Labath        """
8408c4a679SPavel Labath        Test that breakpoint count gets reset for each new connection.
8508c4a679SPavel Labath        """
8608c4a679SPavel Labath
87*2238dcc3SJonas Devlieghere        class MyResponder(MockGDBServerResponder):
8808c4a679SPavel Labath            def __init__(self):
8908c4a679SPavel Labath                super().__init__()
9008c4a679SPavel Labath                self.continued = False
9108c4a679SPavel Labath
9208c4a679SPavel Labath            def qfThreadInfo(self):
9308c4a679SPavel Labath                return "m47"
9408c4a679SPavel Labath
9508c4a679SPavel Labath            def qsThreadInfo(self):
9608c4a679SPavel Labath                return "l"
9708c4a679SPavel Labath
9808c4a679SPavel Labath            def setBreakpoint(self, packet):
9908c4a679SPavel Labath                return "OK"
10008c4a679SPavel Labath
10108c4a679SPavel Labath            def readRegister(self, reg):
10208c4a679SPavel Labath                # Pretend we're at the breakpoint after we've been resumed.
10308c4a679SPavel Labath                return "3412000000000000" if self.continued else "4747000000000000"
10408c4a679SPavel Labath
10508c4a679SPavel Labath            def cont(self):
10608c4a679SPavel Labath                self.continued = True
10708c4a679SPavel Labath                return "T05thread=47;reason:breakpoint"
10808c4a679SPavel Labath
10908c4a679SPavel Labath        # Connect to the first process and set our breakpoint.
11008c4a679SPavel Labath        self.server.responder = MyResponder()
11108c4a679SPavel Labath        target = self.createTarget("a.yaml")
11208c4a679SPavel Labath        process = self.connect(target)
11308c4a679SPavel Labath
11408c4a679SPavel Labath        bkpt = target.BreakpointCreateByAddress(0x1234)
11508c4a679SPavel Labath        self.assertTrue(bkpt.IsValid())
11608c4a679SPavel Labath        self.assertEqual(bkpt.GetNumLocations(), 1)
11708c4a679SPavel Labath
11808c4a679SPavel Labath        # "continue" the process. It should hit our breakpoint.
11908c4a679SPavel Labath        process.Continue()
12008c4a679SPavel Labath        self.assertState(process.GetState(), lldb.eStateStopped)
12108c4a679SPavel Labath        self.assertEqual(bkpt.GetHitCount(), 1)
12208c4a679SPavel Labath
12308c4a679SPavel Labath        # Now kill it. The breakpoint should still show a hit count of one.
12408c4a679SPavel Labath        process.Kill()
12508c4a679SPavel Labath        self.server.stop()
12608c4a679SPavel Labath        self.assertEqual(bkpt.GetHitCount(), 1)
12708c4a679SPavel Labath
12808c4a679SPavel Labath        # Start over, and reconnect.
12908c4a679SPavel Labath        self.server = MockGDBServer(self.server_socket_class())
13008c4a679SPavel Labath        self.server.start()
13108c4a679SPavel Labath
13208c4a679SPavel Labath        process = self.connect(target)
13308c4a679SPavel Labath
13408c4a679SPavel Labath        # The hit count should be reset.
13508c4a679SPavel Labath        self.assertEqual(bkpt.GetHitCount(), 0)
136