File: fork_testbase.py

package info (click to toggle)
llvm-toolchain-16 1%3A16.0.6-15~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,634,792 kB
  • sloc: cpp: 6,179,261; ansic: 1,216,205; asm: 741,319; python: 196,614; objc: 75,325; f90: 49,640; lisp: 32,396; pascal: 12,286; sh: 9,394; perl: 7,442; ml: 5,494; awk: 3,523; makefile: 2,723; javascript: 1,206; xml: 886; fortran: 581; cs: 573
file content (207 lines) | stat: -rw-r--r-- 8,883 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
import gdbremote_testcase


class GdbRemoteForkTestBase(gdbremote_testcase.GdbRemoteTestCaseBase):
    fork_regex = ("[$]T[0-9a-fA-F]{{2}}thread:p([0-9a-f]+)[.]([0-9a-f]+);.*"
                  "{}:p([0-9a-f]+)[.]([0-9a-f]+).*")
    fork_regex_nonstop = ("%Stop:T[0-9a-fA-F]{{2}}"
                          "thread:p([0-9a-f]+)[.]([0-9a-f]+);.*"
                          "{}:p([0-9a-f]+)[.]([0-9a-f]+).*")
    fork_capture = {1: "parent_pid", 2: "parent_tid",
                    3: "child_pid", 4: "child_tid"}
    stop_regex_base = "T[0-9a-fA-F]{{2}}thread:p{}.{};.*reason:signal.*"
    stop_regex = "^[$]" + stop_regex_base

    def start_fork_test(self, args, variant="fork", nonstop=False):
        self.build()
        self.prep_debug_monitor_and_inferior(inferior_args=args)
        self.add_qSupported_packets(["multiprocess+",
                                     "{}-events+".format(variant)])
        ret = self.expect_gdbremote_sequence()
        self.assertIn("{}-events+".format(variant), ret["qSupported_response"])
        self.reset_test_sequence()

        # continue and expect fork
        if nonstop:
            self.test_sequence.add_log_lines([
                "read packet: $QNonStop:1#00",
                "send packet: $OK#00",
                "read packet: $c#00",
                "send packet: $OK#00",
                {"direction": "send",
                 "regex": self.fork_regex_nonstop.format(variant),
                 "capture": self.fork_capture},
                "read packet: $vStopped#00",
                "send packet: $OK#00",
            ], True)
        else:
            self.test_sequence.add_log_lines([
                "read packet: $c#00",
                {"direction": "send", "regex": self.fork_regex.format(variant),
                 "capture": self.fork_capture},
            ], True)
        ret = self.expect_gdbremote_sequence()
        self.reset_test_sequence()

        return tuple(ret[x] for x in ("parent_pid", "parent_tid",
                                      "child_pid", "child_tid"))

    def fork_and_detach_test(self, variant, nonstop=False):
        parent_pid, parent_tid, child_pid, child_tid = (
            self.start_fork_test([variant], variant, nonstop=nonstop))

        # detach the forked child
        self.test_sequence.add_log_lines([
            "read packet: $D;{}#00".format(child_pid),
            "send packet: $OK#00",
            # verify that the current process is correct
            "read packet: $qC#00",
            "send packet: $QCp{}.{}#00".format(parent_pid, parent_tid),
            # verify that the correct processes are detached/available
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: $Eff#00",
            "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
            "send packet: $OK#00",
        ], True)
        self.expect_gdbremote_sequence()
        self.reset_test_sequence()
        return parent_pid, parent_tid

    def fork_and_follow_test(self, variant, nonstop=False):
        parent_pid, parent_tid, child_pid, child_tid = (
            self.start_fork_test([variant], variant, nonstop=nonstop))

        # switch to the forked child
        self.test_sequence.add_log_lines([
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: $OK#00",
            "read packet: $Hcp{}.{}#00".format(child_pid, child_tid),
            "send packet: $OK#00",
            # detach the parent
            "read packet: $D;{}#00".format(parent_pid),
            "send packet: $OK#00",
            # verify that the correct processes are detached/available
            "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
            "send packet: $Eff#00",
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: $OK#00",
            # then resume the child
            "read packet: $c#00",
        ], True)

        if nonstop:
            self.test_sequence.add_log_lines([
                "send packet: $OK#00",
                "send packet: %Stop:W00;process:{}#00".format(child_pid),
                "read packet: $vStopped#00",
                "send packet: $OK#00",
            ], True)
        else:
            self.test_sequence.add_log_lines([
                "send packet: $W00;process:{}#00".format(child_pid),
            ], True)
        self.expect_gdbremote_sequence()

    def detach_all_test(self, nonstop=False):
        parent_pid, parent_tid, child_pid, child_tid = (
            self.start_fork_test(["fork"], nonstop=nonstop))

        self.test_sequence.add_log_lines([
            # double-check our PIDs
            "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
            "send packet: $OK#00",
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: $OK#00",
            # detach all processes
            "read packet: $D#00",
            "send packet: $OK#00",
            # verify that both PIDs are invalid now
            "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
            "send packet: $Eff#00",
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: $Eff#00",
        ], True)
        self.expect_gdbremote_sequence()

    def vkill_test(self, kill_parent=False, kill_child=False, nonstop=False):
        assert kill_parent or kill_child
        parent_pid, parent_tid, child_pid, child_tid = (
            self.start_fork_test(["fork"], nonstop=nonstop))

        if kill_parent:
            self.test_sequence.add_log_lines([
                # kill the process
                "read packet: $vKill;{}#00".format(parent_pid),
                "send packet: $OK#00",
            ], True)
        if kill_child:
            self.test_sequence.add_log_lines([
                # kill the process
                "read packet: $vKill;{}#00".format(child_pid),
                "send packet: $OK#00",
            ], True)
        self.test_sequence.add_log_lines([
            # check child PID/TID
            "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
            "send packet: ${}#00".format("Eff" if kill_child else "OK"),
            # check parent PID/TID
            "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
            "send packet: ${}#00".format("Eff" if kill_parent else "OK"),
        ], True)
        self.expect_gdbremote_sequence()

    def resume_one_test(self, run_order, use_vCont=False, nonstop=False):
        parent_pid, parent_tid, child_pid, child_tid = (
            self.start_fork_test(["fork", "stop"], nonstop=nonstop))

        parent_expect = [
            self.stop_regex_base.format(parent_pid, parent_tid),
            "W00;process:{}#.*".format(parent_pid),
        ]
        child_expect = [
            self.stop_regex_base.format(child_pid, child_tid),
            "W00;process:{}#.*".format(child_pid),
        ]

        for x in run_order:
            if x == "parent":
                pidtid = (parent_pid, parent_tid)
                expect = parent_expect.pop(0)
            elif x == "child":
                pidtid = (child_pid, child_tid)
                expect = child_expect.pop(0)
            else:
                assert False, "unexpected x={}".format(x)

            if use_vCont:
                self.test_sequence.add_log_lines([
                    # continue the selected process
                    "read packet: $vCont;c:p{}.{}#00".format(*pidtid),
                ], True)
            else:
                self.test_sequence.add_log_lines([
                    # continue the selected process
                    "read packet: $Hcp{}.{}#00".format(*pidtid),
                    "send packet: $OK#00",
                    "read packet: $c#00",
                ], True)
            if nonstop:
                self.test_sequence.add_log_lines([
                    "send packet: $OK#00",
                    {"direction": "send", "regex": "%Stop:" + expect},
                    "read packet: $vStopped#00",
                    "send packet: $OK#00",
                ], True)
            else:
                self.test_sequence.add_log_lines([
                    {"direction": "send", "regex": "[$]" + expect},
                ], True)
            # if at least one process remained, check both PIDs
            if parent_expect or child_expect:
                self.test_sequence.add_log_lines([
                    "read packet: $Hgp{}.{}#00".format(parent_pid, parent_tid),
                    "send packet: ${}#00".format("OK" if parent_expect else "Eff"),
                    "read packet: $Hgp{}.{}#00".format(child_pid, child_tid),
                    "send packet: ${}#00".format("OK" if child_expect else "Eff"),
                ], True)
        self.expect_gdbremote_sequence()