xref: /llvm-project/lldb/test/API/tools/lldb-server/TestGdbRemoteLaunch.py (revision 5a658ee933065d0e4ef1a65d9a6ddfba2874ee98)
1import itertools
2
3import gdbremote_testcase
4import lldbgdbserverutils
5from lldbsuite.support import seven
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8
9
10class GdbRemoteLaunchTestCase(gdbremote_testcase.GdbRemoteTestCaseBase):
11    @skipIfWindows  # No pty support to test any inferior output
12    @add_test_categories(["llgs"])
13    def test_launch_via_A(self):
14        self.build()
15        server = self.connect_to_debug_monitor()
16        self.assertIsNotNone(server)
17        self.do_handshake()
18        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
19        args = [exe_path, "stderr:arg1", "stderr:arg2", "stderr:arg3"]
20        hex_args = [seven.hexlify(x) for x in args]
21
22        # NB: strictly speaking we should use %x here but this packet
23        # is deprecated, so no point in changing lldb-server's expectations
24        self.test_sequence.add_log_lines(
25            [
26                "read packet: $A %d,0,%s,%d,1,%s,%d,2,%s,%d,3,%s#00"
27                % tuple(itertools.chain.from_iterable([(len(x), x) for x in hex_args])),
28                "send packet: $OK#00",
29                "read packet: $c#00",
30                "send packet: $W00#00",
31            ],
32            True,
33        )
34        context = self.expect_gdbremote_sequence()
35        self.assertEqual(context["O_content"], b"arg1\r\narg2\r\narg3\r\n")
36
37    @skipIfWindows  # No pty support to test any inferior output
38    @add_test_categories(["llgs"])
39    def test_launch_via_vRun(self):
40        self.build()
41        server = self.connect_to_debug_monitor()
42        self.assertIsNotNone(server)
43        self.do_handshake()
44        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
45        args = [exe_path, "stderr:arg1", "stderr:arg2", "stderr:arg3"]
46        hex_args = [seven.hexlify(x) for x in args]
47
48        self.test_sequence.add_log_lines(
49            [
50                "read packet: $vRun;%s;%s;%s;%s#00" % tuple(hex_args),
51                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
52                "read packet: $c#00",
53                "send packet: $W00#00",
54            ],
55            True,
56        )
57        context = self.expect_gdbremote_sequence()
58        self.assertEqual(context["O_content"], b"arg1\r\narg2\r\narg3\r\n")
59
60    @add_test_categories(["llgs"])
61    @skipIfWindows  # Sometimes returns '$E1f'.
62    def test_launch_via_vRun_no_args(self):
63        self.build()
64        server = self.connect_to_debug_monitor()
65        self.assertIsNotNone(server)
66        self.do_handshake()
67        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
68        hex_path = seven.hexlify(exe_path)
69
70        self.test_sequence.add_log_lines(
71            [
72                "read packet: $vRun;%s#00" % (hex_path,),
73                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
74                "read packet: $c#00",
75                "send packet: $W00#00",
76            ],
77            True,
78        )
79        self.expect_gdbremote_sequence()
80
81    @add_test_categories(["llgs"])
82    @skipIfRemote
83    def test_launch_failure_via_vRun(self):
84        self.build()
85        exe_path = self.getBuildArtifact("a.out")
86        hex_path = seven.hexlify(exe_path)
87
88        server = self.connect_to_debug_monitor()
89        self.assertIsNotNone(server)
90        self.do_handshake()
91        self.test_sequence.add_log_lines(
92            [
93                "read packet: $QEnableErrorStrings#00",
94                "send packet: $OK#00",
95                "read packet: $vRun;%s#00" % hex_path,
96                {
97                    "direction": "send",
98                    "regex": r"^\$E..;([0-9a-fA-F]+)#",
99                    "capture": {1: "msg"},
100                },
101            ],
102            True,
103        )
104        with open(exe_path, "ab") as exe_for_writing:
105            context = self.expect_gdbremote_sequence()
106        self.assertRegex(
107            seven.unhexlify(context.get("msg")),
108            r"(execve failed: Text file busy|The process cannot access the file because it is being used by another process.)",
109        )
110
111    @skipIfWindows  # No pty support to test any inferior output
112    @add_test_categories(["llgs"])
113    def test_QEnvironment(self):
114        self.build()
115        server = self.connect_to_debug_monitor()
116        self.assertIsNotNone(server)
117        self.do_handshake()
118        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
119        env = {"FOO": "test", "BAR": "a=z"}
120        args = [exe_path, "print-env:FOO", "print-env:BAR"]
121        hex_args = [seven.hexlify(x) for x in args]
122
123        for key, value in env.items():
124            self.test_sequence.add_log_lines(
125                [
126                    "read packet: $QEnvironment:%s=%s#00" % (key, value),
127                    "send packet: $OK#00",
128                ],
129                True,
130            )
131        self.test_sequence.add_log_lines(
132            [
133                "read packet: $vRun;%s#00" % (";".join(hex_args),),
134                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
135                "read packet: $c#00",
136                "send packet: $W00#00",
137            ],
138            True,
139        )
140        context = self.expect_gdbremote_sequence()
141        self.assertEqual(context["O_content"], b"test\r\na=z\r\n")
142
143    @skipIfWindows  # No pty support to test any inferior output
144    @add_test_categories(["llgs"])
145    def test_QEnvironmentHexEncoded(self):
146        self.build()
147        server = self.connect_to_debug_monitor()
148        self.assertIsNotNone(server)
149        self.do_handshake()
150        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
151        env = {"FOO": "test", "BAR": "a=z", "BAZ": "a*}#z"}
152        args = [exe_path, "print-env:FOO", "print-env:BAR", "print-env:BAZ"]
153        hex_args = [seven.hexlify(x) for x in args]
154
155        for key, value in env.items():
156            hex_enc = seven.hexlify("%s=%s" % (key, value))
157            self.test_sequence.add_log_lines(
158                [
159                    "read packet: $QEnvironmentHexEncoded:%s#00" % (hex_enc,),
160                    "send packet: $OK#00",
161                ],
162                True,
163            )
164        self.test_sequence.add_log_lines(
165            [
166                "read packet: $vRun;%s#00" % (";".join(hex_args),),
167                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
168                "read packet: $c#00",
169                "send packet: $W00#00",
170            ],
171            True,
172        )
173        context = self.expect_gdbremote_sequence()
174        self.assertEqual(context["O_content"], b"test\r\na=z\r\na*}#z\r\n")
175