xref: /llvm-project/lldb/test/API/tools/lldb-server/TestGdbRemoteLaunch.py (revision 5a658ee933065d0e4ef1a65d9a6ddfba2874ee98)
1681d0d9eSPavel Labathimport itertools
2681d0d9eSPavel Labath
3681d0d9eSPavel Labathimport gdbremote_testcase
4681d0d9eSPavel Labathimport lldbgdbserverutils
5681d0d9eSPavel Labathfrom lldbsuite.support import seven
6681d0d9eSPavel Labathfrom lldbsuite.test.decorators import *
7681d0d9eSPavel Labathfrom lldbsuite.test.lldbtest import *
8681d0d9eSPavel Labath
9681d0d9eSPavel Labath
10681d0d9eSPavel Labathclass GdbRemoteLaunchTestCase(gdbremote_testcase.GdbRemoteTestCaseBase):
11681d0d9eSPavel Labath    @skipIfWindows  # No pty support to test any inferior output
12681d0d9eSPavel Labath    @add_test_categories(["llgs"])
13681d0d9eSPavel Labath    def test_launch_via_A(self):
14681d0d9eSPavel Labath        self.build()
15681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
16681d0d9eSPavel Labath        self.assertIsNotNone(server)
17681d0d9eSPavel Labath        self.do_handshake()
18e2f079ccSDmitry Vasilyev        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
19e2f079ccSDmitry Vasilyev        args = [exe_path, "stderr:arg1", "stderr:arg2", "stderr:arg3"]
20e2f079ccSDmitry Vasilyev        hex_args = [seven.hexlify(x) for x in args]
21e2f079ccSDmitry Vasilyev
22681d0d9eSPavel Labath        # NB: strictly speaking we should use %x here but this packet
23681d0d9eSPavel Labath        # is deprecated, so no point in changing lldb-server's expectations
24681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
252238dcc3SJonas Devlieghere            [
262238dcc3SJonas Devlieghere                "read packet: $A %d,0,%s,%d,1,%s,%d,2,%s,%d,3,%s#00"
272238dcc3SJonas Devlieghere                % tuple(itertools.chain.from_iterable([(len(x), x) for x in hex_args])),
28681d0d9eSPavel Labath                "send packet: $OK#00",
29681d0d9eSPavel Labath                "read packet: $c#00",
302238dcc3SJonas Devlieghere                "send packet: $W00#00",
312238dcc3SJonas Devlieghere            ],
322238dcc3SJonas Devlieghere            True,
332238dcc3SJonas Devlieghere        )
34681d0d9eSPavel Labath        context = self.expect_gdbremote_sequence()
352238dcc3SJonas Devlieghere        self.assertEqual(context["O_content"], b"arg1\r\narg2\r\narg3\r\n")
36681d0d9eSPavel Labath
37681d0d9eSPavel Labath    @skipIfWindows  # No pty support to test any inferior output
38681d0d9eSPavel Labath    @add_test_categories(["llgs"])
39681d0d9eSPavel Labath    def test_launch_via_vRun(self):
40681d0d9eSPavel Labath        self.build()
41681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
42681d0d9eSPavel Labath        self.assertIsNotNone(server)
43681d0d9eSPavel Labath        self.do_handshake()
44e2f079ccSDmitry Vasilyev        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
45e2f079ccSDmitry Vasilyev        args = [exe_path, "stderr:arg1", "stderr:arg2", "stderr:arg3"]
46e2f079ccSDmitry Vasilyev        hex_args = [seven.hexlify(x) for x in args]
47e2f079ccSDmitry Vasilyev
48681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
492238dcc3SJonas Devlieghere            [
502238dcc3SJonas Devlieghere                "read packet: $vRun;%s;%s;%s;%s#00" % tuple(hex_args),
512238dcc3SJonas Devlieghere                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
52681d0d9eSPavel Labath                "read packet: $c#00",
532238dcc3SJonas Devlieghere                "send packet: $W00#00",
542238dcc3SJonas Devlieghere            ],
552238dcc3SJonas Devlieghere            True,
562238dcc3SJonas Devlieghere        )
57681d0d9eSPavel Labath        context = self.expect_gdbremote_sequence()
582238dcc3SJonas Devlieghere        self.assertEqual(context["O_content"], b"arg1\r\narg2\r\narg3\r\n")
59681d0d9eSPavel Labath
60681d0d9eSPavel Labath    @add_test_categories(["llgs"])
61*5a658ee9SDavid Spickett    @skipIfWindows  # Sometimes returns '$E1f'.
62681d0d9eSPavel Labath    def test_launch_via_vRun_no_args(self):
63681d0d9eSPavel Labath        self.build()
64681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
65681d0d9eSPavel Labath        self.assertIsNotNone(server)
66681d0d9eSPavel Labath        self.do_handshake()
67e2f079ccSDmitry Vasilyev        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
68e2f079ccSDmitry Vasilyev        hex_path = seven.hexlify(exe_path)
69e2f079ccSDmitry Vasilyev
70681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
712238dcc3SJonas Devlieghere            [
722238dcc3SJonas Devlieghere                "read packet: $vRun;%s#00" % (hex_path,),
732238dcc3SJonas Devlieghere                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
74681d0d9eSPavel Labath                "read packet: $c#00",
752238dcc3SJonas Devlieghere                "send packet: $W00#00",
762238dcc3SJonas Devlieghere            ],
772238dcc3SJonas Devlieghere            True,
782238dcc3SJonas Devlieghere        )
79681d0d9eSPavel Labath        self.expect_gdbremote_sequence()
80681d0d9eSPavel Labath
81681d0d9eSPavel Labath    @add_test_categories(["llgs"])
82e2f079ccSDmitry Vasilyev    @skipIfRemote
83681d0d9eSPavel Labath    def test_launch_failure_via_vRun(self):
84681d0d9eSPavel Labath        self.build()
85681d0d9eSPavel Labath        exe_path = self.getBuildArtifact("a.out")
86681d0d9eSPavel Labath        hex_path = seven.hexlify(exe_path)
87681d0d9eSPavel Labath
88681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
89681d0d9eSPavel Labath        self.assertIsNotNone(server)
90681d0d9eSPavel Labath        self.do_handshake()
91681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
922238dcc3SJonas Devlieghere            [
932238dcc3SJonas Devlieghere                "read packet: $QEnableErrorStrings#00",
94681d0d9eSPavel Labath                "send packet: $OK#00",
95681d0d9eSPavel Labath                "read packet: $vRun;%s#00" % hex_path,
962238dcc3SJonas Devlieghere                {
972238dcc3SJonas Devlieghere                    "direction": "send",
982238dcc3SJonas Devlieghere                    "regex": r"^\$E..;([0-9a-fA-F]+)#",
99681d0d9eSPavel Labath                    "capture": {1: "msg"},
1002238dcc3SJonas Devlieghere                },
1012238dcc3SJonas Devlieghere            ],
1022238dcc3SJonas Devlieghere            True,
1032238dcc3SJonas Devlieghere        )
104681d0d9eSPavel Labath        with open(exe_path, "ab") as exe_for_writing:
105681d0d9eSPavel Labath            context = self.expect_gdbremote_sequence()
106737bc9f7SJonas Devlieghere        self.assertRegex(
1072238dcc3SJonas Devlieghere            seven.unhexlify(context.get("msg")),
1082238dcc3SJonas Devlieghere            r"(execve failed: Text file busy|The process cannot access the file because it is being used by another process.)",
1092238dcc3SJonas Devlieghere        )
110681d0d9eSPavel Labath
111681d0d9eSPavel Labath    @skipIfWindows  # No pty support to test any inferior output
112681d0d9eSPavel Labath    @add_test_categories(["llgs"])
113681d0d9eSPavel Labath    def test_QEnvironment(self):
114681d0d9eSPavel Labath        self.build()
115681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
116681d0d9eSPavel Labath        self.assertIsNotNone(server)
117681d0d9eSPavel Labath        self.do_handshake()
118e2f079ccSDmitry Vasilyev        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
119e2f079ccSDmitry Vasilyev        env = {"FOO": "test", "BAR": "a=z"}
120e2f079ccSDmitry Vasilyev        args = [exe_path, "print-env:FOO", "print-env:BAR"]
121e2f079ccSDmitry Vasilyev        hex_args = [seven.hexlify(x) for x in args]
122681d0d9eSPavel Labath
123681d0d9eSPavel Labath        for key, value in env.items():
124681d0d9eSPavel Labath            self.test_sequence.add_log_lines(
1252238dcc3SJonas Devlieghere                [
1262238dcc3SJonas Devlieghere                    "read packet: $QEnvironment:%s=%s#00" % (key, value),
1272238dcc3SJonas Devlieghere                    "send packet: $OK#00",
1282238dcc3SJonas Devlieghere                ],
1292238dcc3SJonas Devlieghere                True,
1302238dcc3SJonas Devlieghere            )
131681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
1322238dcc3SJonas Devlieghere            [
1332238dcc3SJonas Devlieghere                "read packet: $vRun;%s#00" % (";".join(hex_args),),
1342238dcc3SJonas Devlieghere                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
135681d0d9eSPavel Labath                "read packet: $c#00",
1362238dcc3SJonas Devlieghere                "send packet: $W00#00",
1372238dcc3SJonas Devlieghere            ],
1382238dcc3SJonas Devlieghere            True,
1392238dcc3SJonas Devlieghere        )
140681d0d9eSPavel Labath        context = self.expect_gdbremote_sequence()
141681d0d9eSPavel Labath        self.assertEqual(context["O_content"], b"test\r\na=z\r\n")
142681d0d9eSPavel Labath
143681d0d9eSPavel Labath    @skipIfWindows  # No pty support to test any inferior output
144681d0d9eSPavel Labath    @add_test_categories(["llgs"])
145681d0d9eSPavel Labath    def test_QEnvironmentHexEncoded(self):
146681d0d9eSPavel Labath        self.build()
147681d0d9eSPavel Labath        server = self.connect_to_debug_monitor()
148681d0d9eSPavel Labath        self.assertIsNotNone(server)
149681d0d9eSPavel Labath        self.do_handshake()
150e2f079ccSDmitry Vasilyev        exe_path = lldbutil.install_to_target(self, self.getBuildArtifact("a.out"))
151e2f079ccSDmitry Vasilyev        env = {"FOO": "test", "BAR": "a=z", "BAZ": "a*}#z"}
152e2f079ccSDmitry Vasilyev        args = [exe_path, "print-env:FOO", "print-env:BAR", "print-env:BAZ"]
153e2f079ccSDmitry Vasilyev        hex_args = [seven.hexlify(x) for x in args]
154681d0d9eSPavel Labath
155681d0d9eSPavel Labath        for key, value in env.items():
156681d0d9eSPavel Labath            hex_enc = seven.hexlify("%s=%s" % (key, value))
157681d0d9eSPavel Labath            self.test_sequence.add_log_lines(
1582238dcc3SJonas Devlieghere                [
1592238dcc3SJonas Devlieghere                    "read packet: $QEnvironmentHexEncoded:%s#00" % (hex_enc,),
1602238dcc3SJonas Devlieghere                    "send packet: $OK#00",
1612238dcc3SJonas Devlieghere                ],
1622238dcc3SJonas Devlieghere                True,
1632238dcc3SJonas Devlieghere            )
164681d0d9eSPavel Labath        self.test_sequence.add_log_lines(
1652238dcc3SJonas Devlieghere            [
1662238dcc3SJonas Devlieghere                "read packet: $vRun;%s#00" % (";".join(hex_args),),
1672238dcc3SJonas Devlieghere                {"direction": "send", "regex": r"^\$T([0-9a-fA-F]+)"},
168681d0d9eSPavel Labath                "read packet: $c#00",
1692238dcc3SJonas Devlieghere                "send packet: $W00#00",
1702238dcc3SJonas Devlieghere            ],
1712238dcc3SJonas Devlieghere            True,
1722238dcc3SJonas Devlieghere        )
173681d0d9eSPavel Labath        context = self.expect_gdbremote_sequence()
174681d0d9eSPavel Labath        self.assertEqual(context["O_content"], b"test\r\na=z\r\na*}#z\r\n")
175