xref: /llvm-project/lldb/test/API/tools/lldb-server/vCont-threads/TestSignal.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1import re
2
3import gdbremote_testcase
4from lldbsuite.test.decorators import *
5from lldbsuite.test.lldbtest import *
6from lldbsuite.test import lldbutil
7
8
9class TestSignal(gdbremote_testcase.GdbRemoteTestCaseBase):
10    def start_threads(self, num):
11        procs = self.prep_debug_monitor_and_inferior(inferior_args=[str(num)])
12        self.test_sequence.add_log_lines(
13            [
14                "read packet: $c#63",
15                {"direction": "send", "regex": "[$]T.*;reason:signal.*"},
16            ],
17            True,
18        )
19        self.add_threadinfo_collection_packets()
20
21        context = self.expect_gdbremote_sequence()
22        self.assertIsNotNone(context)
23        threads = self.parse_threadinfo_packets(context)
24        self.assertIsNotNone(threads)
25        self.assertEqual(len(threads), num + 1)
26
27        self.reset_test_sequence()
28        return threads
29
30    SIGNAL_MATCH_RE = re.compile(r"received SIGUSR1 on thread id: ([0-9a-f]+)")
31
32    def send_and_check_signal(self, vCont_data, threads):
33        self.test_sequence.add_log_lines(
34            [
35                "read packet: $vCont;{0}#00".format(vCont_data),
36                "send packet: $W00#00",
37            ],
38            True,
39        )
40        exp = self.expect_gdbremote_sequence()
41        self.reset_test_sequence()
42        tids = []
43        for line in exp["O_content"].decode().splitlines():
44            m = self.SIGNAL_MATCH_RE.match(line)
45            if m is not None:
46                tids.append(int(m.group(1), 16))
47        self.assertEqual(sorted(tids), sorted(threads))
48
49    def get_pid(self):
50        self.add_process_info_collection_packets()
51        context = self.expect_gdbremote_sequence()
52        self.assertIsNotNone(context)
53        procinfo = self.parse_process_info_response(context)
54        return int(procinfo["pid"], 16)
55
56    @skipIfWindows
57    @skipIfDarwin
58    @expectedFailureNetBSD
59    @expectedFailureAll(
60        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
61    )
62    @skipIfAsan  # Times out under asan
63    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
64    def test_signal_process_without_tid(self):
65        self.build()
66        self.set_inferior_startup_launch()
67
68        threads = self.start_threads(1)
69        self.send_and_check_signal(
70            "C{0:x}".format(lldbutil.get_signal_number("SIGUSR1")), threads
71        )
72
73    @skipUnlessPlatform(["netbsd"])
74    @expectedFailureNetBSD
75    @skipIfAsan  # Times out under asan
76    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
77    def test_signal_one_thread(self):
78        self.build()
79        self.set_inferior_startup_launch()
80
81        threads = self.start_threads(1)
82        # try sending a signal to one of the two threads
83        self.send_and_check_signal(
84            "C{0:x}:{1:x};c".format(lldbutil.get_signal_number("SIGUSR1")), threads[:1]
85        )
86
87    @skipIfWindows
88    @skipIfDarwin
89    @expectedFailureNetBSD
90    @expectedFailureAll(
91        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
92    )
93    @skipIfAsan  # Times out under asan
94    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
95    def test_signal_all_threads(self):
96        self.build()
97        self.set_inferior_startup_launch()
98
99        threads = self.start_threads(1)
100        # try sending a signal to two threads (= the process)
101        self.send_and_check_signal(
102            "C{0:x}:{1:x};C{0:x}:{2:x}".format(
103                lldbutil.get_signal_number("SIGUSR1"), *threads
104            ),
105            threads,
106        )
107
108    @skipIfWindows
109    @expectedFailureNetBSD
110    @expectedFailureAll(
111        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
112    )
113    @add_test_categories(["llgs"])
114    @skipIfAsan  # Times out under asan
115    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
116    def test_signal_process_by_pid(self):
117        self.build()
118        self.set_inferior_startup_launch()
119
120        threads = self.start_threads(1)
121        self.send_and_check_signal(
122            "C{0:x}:p{1:x}".format(
123                lldbutil.get_signal_number("SIGUSR1"), self.get_pid()
124            ),
125            threads,
126        )
127
128    @skipIfWindows
129    @expectedFailureNetBSD
130    @expectedFailureAll(
131        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
132    )
133    @add_test_categories(["llgs"])
134    @skipIfAsan  # Times out under asan
135    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
136    def test_signal_process_minus_one(self):
137        self.build()
138        self.set_inferior_startup_launch()
139
140        threads = self.start_threads(1)
141        self.send_and_check_signal(
142            "C{0:x}:p-1".format(lldbutil.get_signal_number("SIGUSR1")), threads
143        )
144
145    @skipIfWindows
146    @expectedFailureNetBSD
147    @expectedFailureAll(
148        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
149    )
150    @add_test_categories(["llgs"])
151    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
152    def test_signal_minus_one(self):
153        self.build()
154        self.set_inferior_startup_launch()
155
156        threads = self.start_threads(1)
157        self.send_and_check_signal(
158            "C{0:x}:-1".format(lldbutil.get_signal_number("SIGUSR1")), threads
159        )
160
161    @skipIfWindows
162    @expectedFailureNetBSD
163    @expectedFailureAll(
164        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
165    )
166    @add_test_categories(["llgs"])
167    @skipIfAsan  # Times out under asan
168    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
169    def test_signal_all_threads_by_pid(self):
170        self.build()
171        self.set_inferior_startup_launch()
172
173        threads = self.start_threads(1)
174        # try sending a signal to two threads (= the process)
175        self.send_and_check_signal(
176            "C{0:x}:p{1:x}.{2:x};C{0:x}:p{1:x}.{3:x}".format(
177                lldbutil.get_signal_number("SIGUSR1"), self.get_pid(), *threads
178            ),
179            threads,
180        )
181
182    @skipIfWindows
183    @expectedFailureNetBSD
184    @expectedFailureAll(
185        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
186    )
187    @add_test_categories(["llgs"])
188    @skipIfAsan  # Times out under asan
189    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
190    def test_signal_minus_one_by_pid(self):
191        self.build()
192        self.set_inferior_startup_launch()
193
194        threads = self.start_threads(1)
195        self.send_and_check_signal(
196            "C{0:x}:p{1:x}.-1".format(
197                lldbutil.get_signal_number("SIGUSR1"), self.get_pid()
198            ),
199            threads,
200        )
201
202    @skipIfWindows
203    @expectedFailureNetBSD
204    @expectedFailureAll(
205        oslist=["freebsd"], bugnumber="github.com/llvm/llvm-project/issues/56086"
206    )
207    @add_test_categories(["llgs"])
208    @skipIfAsan  # Times out under asan
209    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
210    def test_signal_minus_one_by_minus_one(self):
211        self.build()
212        self.set_inferior_startup_launch()
213
214        threads = self.start_threads(1)
215        self.send_and_check_signal(
216            "C{0:x}:p-1.-1".format(lldbutil.get_signal_number("SIGUSR1")), threads
217        )
218
219    @skipUnlessPlatform(["netbsd"])
220    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
221    def test_signal_two_of_three_threads(self):
222        self.build()
223        self.set_inferior_startup_launch()
224
225        threads = self.start_threads(2)
226        # try sending a signal to 2 out of 3 threads
227        self.test_sequence.add_log_lines(
228            [
229                "read packet: $vCont;C{0:x}:{1:x};C{0:x}:{2:x};c#00".format(
230                    lldbutil.get_signal_number("SIGUSR1"), threads[1], threads[2]
231                ),
232                {"direction": "send", "regex": r"^\$E1e#db$"},
233            ],
234            True,
235        )
236
237        context = self.expect_gdbremote_sequence()
238        self.assertIsNotNone(context)
239
240    @skipUnlessPlatform(["netbsd"])
241    @skipIf(oslist=["linux"], archs=["arm", "aarch64"])  # Randomly fails on buildbot
242    def test_signal_two_signals(self):
243        self.build()
244        self.set_inferior_startup_launch()
245
246        threads = self.start_threads(1)
247        # try sending two different signals to two threads
248        self.test_sequence.add_log_lines(
249            [
250                "read packet: $vCont;C{0:x}:{1:x};C{2:x}:{3:x}#00".format(
251                    lldbutil.get_signal_number("SIGUSR1"),
252                    threads[0],
253                    lldbutil.get_signal_number("SIGUSR2"),
254                    threads[1],
255                ),
256                {"direction": "send", "regex": r"^\$E1e#db$"},
257            ],
258            True,
259        )
260
261        context = self.expect_gdbremote_sequence()
262        self.assertIsNotNone(context)
263