xref: /llvm-project/lldb/test/API/functionalities/gdb_remote_client/TestProcessConnect.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import lldb
2import binascii
3import os
4from lldbsuite.test.lldbtest import *
5from lldbsuite.test.decorators import *
6from lldbsuite.test.gdbclientutils import *
7from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
8
9
10@skipIfRemote
11class TestProcessConnect(GDBRemoteTestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def test_gdb_remote_sync(self):
15        """Test the gdb-remote command in synchronous mode"""
16        try:
17            self.dbg.SetAsync(False)
18            self.expect(
19                "gdb-remote " + self.server.get_connect_address(),
20                substrs=["Process", "stopped"],
21            )
22        finally:
23            self.dbg.GetSelectedTarget().GetProcess().Kill()
24
25    def test_gdb_remote_async(self):
26        """Test the gdb-remote command in asynchronous mode"""
27        try:
28            self.dbg.SetAsync(True)
29            self.expect(
30                "gdb-remote " + self.server.get_connect_address(),
31                matching=False,
32                substrs=["Process", "stopped"],
33            )
34            lldbutil.expect_state_changes(
35                self, self.dbg.GetListener(), self.process(), [lldb.eStateStopped]
36            )
37        finally:
38            self.dbg.GetSelectedTarget().GetProcess().Kill()
39        lldbutil.expect_state_changes(
40            self, self.dbg.GetListener(), self.process(), [lldb.eStateExited]
41        )
42
43    @skipIfWindows
44    def test_process_connect_sync(self):
45        """Test the gdb-remote command in synchronous mode"""
46        try:
47            self.dbg.SetAsync(False)
48            self.expect(
49                "platform select remote-gdb-server",
50                substrs=["Platform: remote-gdb-server", "Connected: no"],
51            )
52            self.expect(
53                "process connect " + self.server.get_connect_url(),
54                substrs=["Process", "stopped"],
55            )
56        finally:
57            self.dbg.GetSelectedTarget().GetProcess().Kill()
58
59    @skipIfWindows
60    def test_process_connect_async(self):
61        """Test the gdb-remote command in asynchronous mode"""
62        try:
63            self.dbg.SetAsync(True)
64            self.expect(
65                "platform select remote-gdb-server",
66                substrs=["Platform: remote-gdb-server", "Connected: no"],
67            )
68            self.expect(
69                "process connect " + self.server.get_connect_url(),
70                matching=False,
71                substrs=["Process", "stopped"],
72            )
73            lldbutil.expect_state_changes(
74                self, self.dbg.GetListener(), self.process(), [lldb.eStateStopped]
75            )
76        finally:
77            self.dbg.GetSelectedTarget().GetProcess().Kill()
78        lldbutil.expect_state_changes(
79            self, self.dbg.GetListener(), self.process(), [lldb.eStateExited]
80        )
81
82    def test_breakpoint_count(self):
83        """
84        Test that breakpoint count gets reset for each new connection.
85        """
86
87        class MyResponder(MockGDBServerResponder):
88            def __init__(self):
89                super().__init__()
90                self.continued = False
91
92            def qfThreadInfo(self):
93                return "m47"
94
95            def qsThreadInfo(self):
96                return "l"
97
98            def setBreakpoint(self, packet):
99                return "OK"
100
101            def readRegister(self, reg):
102                # Pretend we're at the breakpoint after we've been resumed.
103                return "3412000000000000" if self.continued else "4747000000000000"
104
105            def cont(self):
106                self.continued = True
107                return "T05thread=47;reason:breakpoint"
108
109        # Connect to the first process and set our breakpoint.
110        self.server.responder = MyResponder()
111        target = self.createTarget("a.yaml")
112        process = self.connect(target)
113
114        bkpt = target.BreakpointCreateByAddress(0x1234)
115        self.assertTrue(bkpt.IsValid())
116        self.assertEqual(bkpt.GetNumLocations(), 1)
117
118        # "continue" the process. It should hit our breakpoint.
119        process.Continue()
120        self.assertState(process.GetState(), lldb.eStateStopped)
121        self.assertEqual(bkpt.GetHitCount(), 1)
122
123        # Now kill it. The breakpoint should still show a hit count of one.
124        process.Kill()
125        self.server.stop()
126        self.assertEqual(bkpt.GetHitCount(), 1)
127
128        # Start over, and reconnect.
129        self.server = MockGDBServer(self.server_socket_class())
130        self.server.start()
131
132        process = self.connect(target)
133
134        # The hit count should be reset.
135        self.assertEqual(bkpt.GetHitCount(), 0)
136