xref: /llvm-project/lldb/test/API/tools/lldb-server/TestGdbRemoteForkNonStop.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
17fc12da8SMichał Górnyfrom lldbsuite.test.decorators import *
27fc12da8SMichał Górnyfrom lldbsuite.test.lldbtest import *
37fc12da8SMichał Górny
47fc12da8SMichał Górnyfrom fork_testbase import GdbRemoteForkTestBase
57fc12da8SMichał Górny
67fc12da8SMichał Górny
77fc12da8SMichał Górnyclass TestGdbRemoteForkNonStop(GdbRemoteForkTestBase):
8ba14e4d6SMuhammad Omair Javaid    def setUp(self):
9ba14e4d6SMuhammad Omair Javaid        GdbRemoteForkTestBase.setUp(self)
10*2238dcc3SJonas Devlieghere        if self.getPlatform() == "linux" and self.getArchitecture() in [
11*2238dcc3SJonas Devlieghere            "arm",
12*2238dcc3SJonas Devlieghere            "aarch64",
13*2238dcc3SJonas Devlieghere        ]:
14ba14e4d6SMuhammad Omair Javaid            self.skipTest("Unsupported for Arm/AArch64 Linux")
15ba14e4d6SMuhammad Omair Javaid
167fc12da8SMichał Górny    @add_test_categories(["fork"])
177fc12da8SMichał Górny    def test_vfork_nonstop(self):
18*2238dcc3SJonas Devlieghere        parent_pid, parent_tid = self.fork_and_detach_test("vfork", nonstop=True)
197fc12da8SMichał Górny
207fc12da8SMichał Górny        # resume the parent
21*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
22*2238dcc3SJonas Devlieghere            [
237fc12da8SMichał Górny                "read packet: $c#00",
247fc12da8SMichał Górny                "send packet: $OK#00",
25*2238dcc3SJonas Devlieghere                {
26*2238dcc3SJonas Devlieghere                    "direction": "send",
27*2238dcc3SJonas Devlieghere                    "regex": r"%Stop:T[0-9a-fA-F]{{2}}thread:p{}[.]{}.*vforkdone.*".format(
28*2238dcc3SJonas Devlieghere                        parent_pid, parent_tid
29*2238dcc3SJonas Devlieghere                    ),
307fc12da8SMichał Górny                },
317fc12da8SMichał Górny                "read packet: $vStopped#00",
327fc12da8SMichał Górny                "send packet: $OK#00",
337fc12da8SMichał Górny                "read packet: $c#00",
347fc12da8SMichał Górny                "send packet: $OK#00",
357fc12da8SMichał Górny                "send packet: %Stop:W00;process:{}#00".format(parent_pid),
367fc12da8SMichał Górny                "read packet: $vStopped#00",
377fc12da8SMichał Górny                "send packet: $OK#00",
38*2238dcc3SJonas Devlieghere            ],
39*2238dcc3SJonas Devlieghere            True,
40*2238dcc3SJonas Devlieghere        )
417fc12da8SMichał Górny        self.expect_gdbremote_sequence()
427fc12da8SMichał Górny
437fc12da8SMichał Górny    @add_test_categories(["fork"])
447fc12da8SMichał Górny    def test_fork_nonstop(self):
457fc12da8SMichał Górny        parent_pid, _ = self.fork_and_detach_test("fork", nonstop=True)
467fc12da8SMichał Górny
477fc12da8SMichał Górny        # resume the parent
48*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
49*2238dcc3SJonas Devlieghere            [
507fc12da8SMichał Górny                "read packet: $c#00",
517fc12da8SMichał Górny                "send packet: $OK#00",
527fc12da8SMichał Górny                "send packet: %Stop:W00;process:{}#00".format(parent_pid),
537fc12da8SMichał Górny                "read packet: $vStopped#00",
547fc12da8SMichał Górny                "send packet: $OK#00",
55*2238dcc3SJonas Devlieghere            ],
56*2238dcc3SJonas Devlieghere            True,
57*2238dcc3SJonas Devlieghere        )
587fc12da8SMichał Górny        self.expect_gdbremote_sequence()
597fc12da8SMichał Górny
607fc12da8SMichał Górny    @add_test_categories(["fork"])
617fc12da8SMichał Górny    def test_fork_follow_nonstop(self):
627fc12da8SMichał Górny        self.fork_and_follow_test("fork", nonstop=True)
637fc12da8SMichał Górny
647fc12da8SMichał Górny    @add_test_categories(["fork"])
657fc12da8SMichał Górny    def test_vfork_follow_nonstop(self):
667fc12da8SMichał Górny        self.fork_and_follow_test("vfork", nonstop=True)
677fc12da8SMichał Górny
687fc12da8SMichał Górny    @add_test_categories(["fork"])
697fc12da8SMichał Górny    def test_detach_all_nonstop(self):
707fc12da8SMichał Górny        self.detach_all_test(nonstop=True)
717fc12da8SMichał Górny
727fc12da8SMichał Górny    @add_test_categories(["fork"])
737fc12da8SMichał Górny    def test_kill_all_nonstop(self):
74*2238dcc3SJonas Devlieghere        parent_pid, _, child_pid, _ = self.start_fork_test(["fork"], nonstop=True)
757fc12da8SMichał Górny
767fc12da8SMichał Górny        exit_regex = "X09;process:([0-9a-f]+)"
777fc12da8SMichał Górny        # Depending on a potential race, the second kill may make it into
787fc12da8SMichał Górny        # the async queue before we issue vStopped or after.  In the former
797fc12da8SMichał Górny        # case, we should expect the exit status in reply to vStopped.
807fc12da8SMichał Górny        # In the latter, we should expect an OK response (queue empty),
817fc12da8SMichał Górny        # followed by another async notification.
827fc12da8SMichał Górny        vstop_regex = "[$](OK|{})#.*".format(exit_regex)
83*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
84*2238dcc3SJonas Devlieghere            [
857fc12da8SMichał Górny                # kill all processes
867fc12da8SMichał Górny                "read packet: $k#00",
877fc12da8SMichał Górny                "send packet: $OK#00",
88*2238dcc3SJonas Devlieghere                {
89*2238dcc3SJonas Devlieghere                    "direction": "send",
90*2238dcc3SJonas Devlieghere                    "regex": "%Stop:{}#.*".format(exit_regex),
91*2238dcc3SJonas Devlieghere                    "capture": {1: "pid1"},
92*2238dcc3SJonas Devlieghere                },
937fc12da8SMichał Górny                "read packet: $vStopped#00",
94*2238dcc3SJonas Devlieghere                {
95*2238dcc3SJonas Devlieghere                    "direction": "send",
96*2238dcc3SJonas Devlieghere                    "regex": vstop_regex,
97*2238dcc3SJonas Devlieghere                    "capture": {1: "vstop_reply", 2: "pid2"},
98*2238dcc3SJonas Devlieghere                },
99*2238dcc3SJonas Devlieghere            ],
100*2238dcc3SJonas Devlieghere            True,
101*2238dcc3SJonas Devlieghere        )
1027fc12da8SMichał Górny        ret = self.expect_gdbremote_sequence()
1037fc12da8SMichał Górny        pid1 = ret["pid1"]
1047fc12da8SMichał Górny        if ret["vstop_reply"] == "OK":
1057fc12da8SMichał Górny            self.reset_test_sequence()
106*2238dcc3SJonas Devlieghere            self.test_sequence.add_log_lines(
107*2238dcc3SJonas Devlieghere                [
108*2238dcc3SJonas Devlieghere                    {
109*2238dcc3SJonas Devlieghere                        "direction": "send",
110*2238dcc3SJonas Devlieghere                        "regex": "%Stop:{}#.*".format(exit_regex),
111*2238dcc3SJonas Devlieghere                        "capture": {1: "pid2"},
112*2238dcc3SJonas Devlieghere                    },
113*2238dcc3SJonas Devlieghere                ],
114*2238dcc3SJonas Devlieghere                True,
115*2238dcc3SJonas Devlieghere            )
1167fc12da8SMichał Górny            ret = self.expect_gdbremote_sequence()
1177fc12da8SMichał Górny        pid2 = ret["pid2"]
1187fc12da8SMichał Górny        self.reset_test_sequence()
119*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
120*2238dcc3SJonas Devlieghere            [
1217fc12da8SMichał Górny                "read packet: $vStopped#00",
1227fc12da8SMichał Górny                "send packet: $OK#00",
123*2238dcc3SJonas Devlieghere            ],
124*2238dcc3SJonas Devlieghere            True,
125*2238dcc3SJonas Devlieghere        )
1267fc12da8SMichał Górny        self.expect_gdbremote_sequence()
1273c16fb3aSMichał Górny        self.assertEqual(set([pid1, pid2]), set([parent_pid, child_pid]))
1287fc12da8SMichał Górny
1297fc12da8SMichał Górny    @add_test_categories(["fork"])
1307fc12da8SMichał Górny    def test_vkill_both_nonstop(self):
1317fc12da8SMichał Górny        self.vkill_test(kill_parent=True, kill_child=True, nonstop=True)
1327fc12da8SMichał Górny
1337fc12da8SMichał Górny    @add_test_categories(["fork"])
1347fc12da8SMichał Górny    def test_c_interspersed_nonstop(self):
135*2238dcc3SJonas Devlieghere        self.resume_one_test(
136*2238dcc3SJonas Devlieghere            run_order=["parent", "child", "parent", "child"], nonstop=True
137*2238dcc3SJonas Devlieghere        )
1387fc12da8SMichał Górny
1397fc12da8SMichał Górny    @add_test_categories(["fork"])
1407fc12da8SMichał Górny    def test_vCont_interspersed_nonstop(self):
141*2238dcc3SJonas Devlieghere        self.resume_one_test(
142*2238dcc3SJonas Devlieghere            run_order=["parent", "child", "parent", "child"],
143*2238dcc3SJonas Devlieghere            use_vCont=True,
144*2238dcc3SJonas Devlieghere            nonstop=True,
145*2238dcc3SJonas Devlieghere        )
14609531edeSMichał Górny
14708069279SMichał Górny    def get_all_output_via_vStdio(self, output_test):
14808069279SMichał Górny        # The output may be split into an arbitrary number of messages.
14908069279SMichał Górny        # Loop until we have everything. The first message is waiting for us
15008069279SMichał Górny        # in the packet queue.
15108069279SMichał Górny        output = self._server.get_raw_output_packet()
15208069279SMichał Górny        while not output_test(output):
15308069279SMichał Górny            self._server.send_packet(b"vStdio")
15408069279SMichał Górny            output += self._server.get_raw_output_packet()
15508069279SMichał Górny        return output
15608069279SMichał Górny
15709531edeSMichał Górny    @add_test_categories(["fork"])
15809531edeSMichał Górny    def test_c_both_nonstop(self):
15909531edeSMichał Górny        lock1 = self.getBuildArtifact("lock1")
16009531edeSMichał Górny        lock2 = self.getBuildArtifact("lock2")
161*2238dcc3SJonas Devlieghere        parent_pid, parent_tid, child_pid, child_tid = self.start_fork_test(
162*2238dcc3SJonas Devlieghere            [
163*2238dcc3SJonas Devlieghere                "fork",
164*2238dcc3SJonas Devlieghere                "process:sync:" + lock1,
165*2238dcc3SJonas Devlieghere                "print-pid",
166*2238dcc3SJonas Devlieghere                "process:sync:" + lock2,
167*2238dcc3SJonas Devlieghere                "stop",
168*2238dcc3SJonas Devlieghere            ],
169*2238dcc3SJonas Devlieghere            nonstop=True,
170*2238dcc3SJonas Devlieghere        )
17109531edeSMichał Górny
172*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
173*2238dcc3SJonas Devlieghere            [
17409531edeSMichał Górny                "read packet: $Hcp{}.{}#00".format(parent_pid, parent_tid),
17509531edeSMichał Górny                "send packet: $OK#00",
17609531edeSMichał Górny                "read packet: $c#00",
17709531edeSMichał Górny                "send packet: $OK#00",
17809531edeSMichał Górny                "read packet: $Hcp{}.{}#00".format(child_pid, child_tid),
17909531edeSMichał Górny                "send packet: $OK#00",
18009531edeSMichał Górny                "read packet: $c#00",
18109531edeSMichał Górny                "send packet: $OK#00",
18209531edeSMichał Górny                {"direction": "send", "regex": "%Stop:T.*"},
183*2238dcc3SJonas Devlieghere            ],
184*2238dcc3SJonas Devlieghere            True,
185*2238dcc3SJonas Devlieghere        )
18608069279SMichał Górny        self.expect_gdbremote_sequence()
18708069279SMichał Górny
18808069279SMichał Górny        output = self.get_all_output_via_vStdio(
189*2238dcc3SJonas Devlieghere            lambda output: output.count(b"PID: ") >= 2
190*2238dcc3SJonas Devlieghere        )
19108069279SMichał Górny        self.assertEqual(output.count(b"PID: "), 2)
19208069279SMichał Górny        self.assertIn("PID: {}".format(int(parent_pid, 16)).encode(), output)
19308069279SMichał Górny        self.assertIn("PID: {}".format(int(child_pid, 16)).encode(), output)
194f8603c1fSMichał Górny
195f8603c1fSMichał Górny    @add_test_categories(["fork"])
196f8603c1fSMichał Górny    def test_vCont_both_nonstop(self):
197f8603c1fSMichał Górny        lock1 = self.getBuildArtifact("lock1")
198f8603c1fSMichał Górny        lock2 = self.getBuildArtifact("lock2")
199*2238dcc3SJonas Devlieghere        parent_pid, parent_tid, child_pid, child_tid = self.start_fork_test(
200*2238dcc3SJonas Devlieghere            [
201*2238dcc3SJonas Devlieghere                "fork",
202*2238dcc3SJonas Devlieghere                "process:sync:" + lock1,
203*2238dcc3SJonas Devlieghere                "print-pid",
204*2238dcc3SJonas Devlieghere                "process:sync:" + lock2,
205*2238dcc3SJonas Devlieghere                "stop",
206*2238dcc3SJonas Devlieghere            ],
207*2238dcc3SJonas Devlieghere            nonstop=True,
208*2238dcc3SJonas Devlieghere        )
209f8603c1fSMichał Górny
210*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
211*2238dcc3SJonas Devlieghere            [
212f8603c1fSMichał Górny                "read packet: $vCont;c:p{}.{};c:p{}.{}#00".format(
213*2238dcc3SJonas Devlieghere                    parent_pid, parent_tid, child_pid, child_tid
214*2238dcc3SJonas Devlieghere                ),
215f8603c1fSMichał Górny                "send packet: $OK#00",
216f8603c1fSMichał Górny                {"direction": "send", "regex": "%Stop:T.*"},
217*2238dcc3SJonas Devlieghere            ],
218*2238dcc3SJonas Devlieghere            True,
219*2238dcc3SJonas Devlieghere        )
220f8603c1fSMichał Górny        self.expect_gdbremote_sequence()
221f8603c1fSMichał Górny
222f8603c1fSMichał Górny        output = self.get_all_output_via_vStdio(
223*2238dcc3SJonas Devlieghere            lambda output: output.count(b"PID: ") >= 2
224*2238dcc3SJonas Devlieghere        )
225f8603c1fSMichał Górny        self.assertEqual(output.count(b"PID: "), 2)
226f8603c1fSMichał Górny        self.assertIn("PID: {}".format(int(parent_pid, 16)).encode(), output)
227f8603c1fSMichał Górny        self.assertIn("PID: {}".format(int(child_pid, 16)).encode(), output)
228f8603c1fSMichał Górny
229f8603c1fSMichał Górny    def vCont_both_nonstop_test(self, vCont_packet):
230f8603c1fSMichał Górny        lock1 = self.getBuildArtifact("lock1")
231f8603c1fSMichał Górny        lock2 = self.getBuildArtifact("lock2")
232*2238dcc3SJonas Devlieghere        parent_pid, parent_tid, child_pid, child_tid = self.start_fork_test(
233*2238dcc3SJonas Devlieghere            [
234*2238dcc3SJonas Devlieghere                "fork",
235*2238dcc3SJonas Devlieghere                "process:sync:" + lock1,
236*2238dcc3SJonas Devlieghere                "print-pid",
237*2238dcc3SJonas Devlieghere                "process:sync:" + lock2,
238*2238dcc3SJonas Devlieghere                "stop",
239*2238dcc3SJonas Devlieghere            ],
240*2238dcc3SJonas Devlieghere            nonstop=True,
241*2238dcc3SJonas Devlieghere        )
242f8603c1fSMichał Górny
243*2238dcc3SJonas Devlieghere        self.test_sequence.add_log_lines(
244*2238dcc3SJonas Devlieghere            [
245f8603c1fSMichał Górny                "read packet: ${}#00".format(vCont_packet),
246f8603c1fSMichał Górny                "send packet: $OK#00",
247f8603c1fSMichał Górny                {"direction": "send", "regex": "%Stop:T.*"},
248*2238dcc3SJonas Devlieghere            ],
249*2238dcc3SJonas Devlieghere            True,
250*2238dcc3SJonas Devlieghere        )
251f8603c1fSMichał Górny        self.expect_gdbremote_sequence()
252f8603c1fSMichał Górny
253f8603c1fSMichał Górny        output = self.get_all_output_via_vStdio(
254*2238dcc3SJonas Devlieghere            lambda output: output.count(b"PID: ") >= 2
255*2238dcc3SJonas Devlieghere        )
256f8603c1fSMichał Górny        self.assertEqual(output.count(b"PID: "), 2)
257f8603c1fSMichał Górny        self.assertIn("PID: {}".format(int(parent_pid, 16)).encode(), output)
258f8603c1fSMichał Górny        self.assertIn("PID: {}".format(int(child_pid, 16)).encode(), output)
259f8603c1fSMichał Górny
260f8603c1fSMichał Górny    @add_test_categories(["fork"])
261f8603c1fSMichał Górny    def test_vCont_both_implicit_nonstop(self):
262f8603c1fSMichał Górny        self.vCont_both_nonstop_test("vCont;c")
263f8603c1fSMichał Górny
264f8603c1fSMichał Górny    @add_test_categories(["fork"])
265f8603c1fSMichał Górny    def test_vCont_both_minus_one_nonstop(self):
266f8603c1fSMichał Górny        self.vCont_both_nonstop_test("vCont;c:p-1.-1")
267