1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test.decorators import *
4from lldbsuite.test.gdbclientutils import *
5from lldbsuite.test.lldbgdbclient import GDBRemoteTestBase
6
7
8@skipIfWindows
9class TestPty(GDBRemoteTestBase):
10    mydir = TestBase.compute_mydir(__file__)
11    server_socket_class = PtyServerSocket
12
13    def get_term_attrs(self):
14        import termios
15        return termios.tcgetattr(self.server._socket._secondary)
16
17    def setUp(self):
18        super().setUp()
19        self.orig_attr = self.get_term_attrs()
20
21    def assert_raw_mode(self, current_attr):
22        import termios
23        self.assertEqual(current_attr[0] & (termios.BRKINT |
24                                            termios.PARMRK |
25                                            termios.ISTRIP | termios.INLCR |
26                                            termios.IGNCR | termios.ICRNL |
27                                            termios.IXON),
28                         0)
29        self.assertEqual(current_attr[1] & termios.OPOST, 0)
30        self.assertEqual(current_attr[2] & termios.CSIZE, termios.CS8)
31        self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
32                                            termios.ISIG | termios.IEXTEN),
33                         0)
34        self.assertEqual(current_attr[6][termios.VMIN], 1)
35        self.assertEqual(current_attr[6][termios.VTIME], 0)
36
37    def get_parity_flags(self, attr):
38        import termios
39        return attr[2] & (termios.PARENB | termios.PARODD)
40
41    def get_stop_bit_flags(self, attr):
42        import termios
43        return attr[2] & termios.CSTOPB
44
45    def test_process_connect_sync(self):
46        """Test the process connect command in synchronous mode"""
47        try:
48            self.dbg.SetAsync(False)
49            self.expect("platform select remote-gdb-server",
50                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
51            self.expect("process connect " + self.server.get_connect_url(),
52                        substrs=['Process', 'stopped'])
53
54            current_attr = self.get_term_attrs()
55            # serial:// should set raw mode
56            self.assert_raw_mode(current_attr)
57            # other parameters should be unmodified
58            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
59            self.assertEqual(self.get_parity_flags(current_attr),
60                             self.get_parity_flags(self.orig_attr))
61            self.assertEqual(self.get_stop_bit_flags(current_attr),
62                             self.get_stop_bit_flags(self.orig_attr))
63        finally:
64            self.dbg.GetSelectedTarget().GetProcess().Kill()
65        # original mode should be restored on exit
66        self.assertEqual(self.get_term_attrs(), self.orig_attr)
67
68    def test_process_connect_async(self):
69        """Test the process connect command in asynchronous mode"""
70        try:
71            self.dbg.SetAsync(True)
72            self.expect("platform select remote-gdb-server",
73                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
74            self.expect("process connect " + self.server.get_connect_url(),
75                        matching=False,
76                        substrs=['Process', 'stopped'])
77            lldbutil.expect_state_changes(self, self.dbg.GetListener(),
78                                          self.process(), [lldb.eStateStopped])
79
80            current_attr = self.get_term_attrs()
81            # serial:// should set raw mode
82            self.assert_raw_mode(current_attr)
83            # other parameters should be unmodified
84            self.assertEqual(current_attr[4:6], self.orig_attr[4:6])
85            self.assertEqual(self.get_parity_flags(current_attr),
86                             self.get_parity_flags(self.orig_attr))
87            self.assertEqual(self.get_stop_bit_flags(current_attr),
88                             self.get_stop_bit_flags(self.orig_attr))
89        finally:
90            self.dbg.GetSelectedTarget().GetProcess().Kill()
91        lldbutil.expect_state_changes(self, self.dbg.GetListener(),
92                                      self.process(), [lldb.eStateExited])
93        # original mode should be restored on exit
94        self.assertEqual(self.get_term_attrs(), self.orig_attr)
95
96    def test_connect_via_file(self):
97        """Test connecting via the legacy file:// URL"""
98        import termios
99        try:
100            self.expect("platform select remote-gdb-server",
101                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
102            self.expect("process connect file://" +
103                        self.server.get_connect_address(),
104                        substrs=['Process', 'stopped'])
105
106            # file:// sets baud rate and some raw-related flags
107            current_attr = self.get_term_attrs()
108            self.assertEqual(current_attr[3] & (termios.ICANON | termios.ECHO |
109                                                termios.ECHOE | termios.ISIG),
110                             0)
111            self.assertEqual(current_attr[4], termios.B115200)
112            self.assertEqual(current_attr[5], termios.B115200)
113            self.assertEqual(current_attr[6][termios.VMIN], 1)
114            self.assertEqual(current_attr[6][termios.VTIME], 0)
115        finally:
116            self.dbg.GetSelectedTarget().GetProcess().Kill()
117
118    def test_process_connect_params(self):
119        """Test serial:// URL with parameters"""
120        import termios
121        try:
122            self.expect("platform select remote-gdb-server",
123                        substrs=['Platform: remote-gdb-server', 'Connected: no'])
124            self.expect("process connect " + self.server.get_connect_url() +
125                        "?baud=115200&stop-bits=2",
126                        substrs=['Process', 'stopped'])
127
128            current_attr = self.get_term_attrs()
129            self.assert_raw_mode(current_attr)
130            self.assertEqual(current_attr[4:6], 2 * [termios.B115200])
131            self.assertEqual(self.get_parity_flags(current_attr),
132                             self.get_parity_flags(self.orig_attr))
133            self.assertEqual(self.get_stop_bit_flags(current_attr),
134                             termios.CSTOPB)
135        finally:
136            self.dbg.GetSelectedTarget().GetProcess().Kill()
137        # original mode should be restored on exit
138        self.assertEqual(self.get_term_attrs(), self.orig_attr)
139