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