xref: /llvm-project/lldb/test/API/functionalities/signal/raise/TestRaise.py (revision 9c2468821ec51defd09c246fea4a47886fff8c01)
1"""Test that we handle inferiors that send signals to themselves"""
2
3
4import lldb
5import re
6from lldbsuite.test.lldbplatformutil import getDarwinOSTriples
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12@skipIfWindows  # signals do not exist on Windows
13class RaiseTestCase(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    @skipIfNetBSD  # Hangs on NetBSD
17    def test_sigstop(self):
18        self.build()
19        self.signal_test("SIGSTOP", False)
20        # passing of SIGSTOP is not correctly handled, so not testing that
21        # scenario: https://llvm.org/bugs/show_bug.cgi?id=23574
22
23    @skipIfDarwin  # darwin does not support real time signals
24    @skipIfTargetAndroid()
25    def test_sigsigrtmin(self):
26        self.build()
27        self.signal_test("SIGRTMIN", True)
28
29    @skipIfNetBSD  # Hangs on NetBSD
30    def test_sigtrap(self):
31        self.build()
32        self.signal_test("SIGTRAP", True)
33
34    def launch(self, target, signal):
35        # launch the process, do not stop at entry point.
36        # If we have gotten the default for this signal, reset that as well.
37        if len(self.default_pass) != 0:
38            lldbutil.set_actions_for_signal(
39                self, signal, self.default_pass, self.default_stop, self.default_notify
40            )
41
42        process = target.LaunchSimple(
43            [signal], None, self.get_process_working_directory()
44        )
45        self.assertTrue(process, PROCESS_IS_VALID)
46        self.assertState(process.GetState(), lldb.eStateStopped)
47        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
48        self.assertTrue(
49            thread.IsValid(), "Thread should be stopped due to a breakpoint"
50        )
51        return process
52
53    def set_handle(self, signal, pass_signal, stop_at_signal, notify_signal):
54        return_obj = lldb.SBCommandReturnObject()
55        self.dbg.GetCommandInterpreter().HandleCommand(
56            "process handle %s -p %s -s %s -n %s"
57            % (signal, pass_signal, stop_at_signal, notify_signal),
58            return_obj,
59        )
60        self.assertTrue(return_obj.Succeeded(), "Setting signal handling failed")
61
62    def signal_test(self, signal, test_passing):
63        """Test that we handle inferior raising signals"""
64        exe = self.getBuildArtifact("a.out")
65
66        # Create a target by the debugger.
67        target = self.dbg.CreateTarget(exe)
68        self.assertTrue(target, VALID_TARGET)
69        lldbutil.run_break_set_by_symbol(self, "main")
70        self.default_pass = ""
71        self.default_stop = ""
72        self.default_notify = ""
73
74        # launch
75        process = self.launch(target, signal)
76        signo = process.GetUnixSignals().GetSignalNumberFromName(signal)
77
78        # retrieve default signal disposition
79        (
80            self.default_pass,
81            self.default_stop,
82            self.default_notify,
83        ) = lldbutil.get_actions_for_signal(self, signal)
84
85        # Make sure we stop at the signal
86        lldbutil.set_actions_for_signal(self, signal, "false", "true", "true")
87        process.Continue()
88        self.assertState(process.GetState(), lldb.eStateStopped)
89        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
90        self.assertTrue(thread.IsValid(), "Thread should be stopped due to a signal")
91        self.assertGreaterEqual(
92            thread.GetStopReasonDataCount(), 1, "There was data in the event."
93        )
94        self.assertEqual(
95            thread.GetStopReasonDataAtIndex(0), signo, "The stop signal was %s" % signal
96        )
97
98        # Continue until we exit.
99        process.Continue()
100        self.assertState(process.GetState(), lldb.eStateExited)
101        self.assertEqual(process.GetExitStatus(), 0)
102
103        process = self.launch(target, signal)
104
105        # Make sure we do not stop at the signal. We should still get the
106        # notification.
107        lldbutil.set_actions_for_signal(self, signal, "false", "false", "true")
108        self.expect("process continue", substrs=["stopped and restarted", signal])
109        self.assertState(process.GetState(), lldb.eStateExited)
110        self.assertEqual(process.GetExitStatus(), 0)
111
112        # launch again
113        process = self.launch(target, signal)
114
115        # Make sure we do not stop at the signal, and we do not get the
116        # notification.
117        lldbutil.set_actions_for_signal(self, signal, "false", "false", "false")
118        self.expect(
119            "process continue", substrs=["stopped and restarted"], matching=False
120        )
121        self.assertState(process.GetState(), lldb.eStateExited)
122        self.assertEqual(process.GetExitStatus(), 0)
123
124        if not test_passing:
125            # reset signal handling to default
126            lldbutil.set_actions_for_signal(
127                self, signal, self.default_pass, self.default_stop, self.default_notify
128            )
129            return
130
131        # launch again
132        process = self.launch(target, signal)
133
134        # Make sure we stop at the signal
135        lldbutil.set_actions_for_signal(self, signal, "true", "true", "true")
136        process.Continue()
137        self.assertState(process.GetState(), lldb.eStateStopped)
138        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonSignal)
139        self.assertTrue(thread.IsValid(), "Thread should be stopped due to a signal")
140        self.assertGreaterEqual(
141            thread.GetStopReasonDataCount(), 1, "There was data in the event."
142        )
143        self.assertEqual(
144            thread.GetStopReasonDataAtIndex(0),
145            process.GetUnixSignals().GetSignalNumberFromName(signal),
146            "The stop signal was %s" % signal,
147        )
148
149        # Continue until we exit. The process should receive the signal.
150        process.Continue()
151        self.assertState(process.GetState(), lldb.eStateExited)
152        self.assertEqual(process.GetExitStatus(), signo)
153
154        # launch again
155        process = self.launch(target, signal)
156
157        # Make sure we do not stop at the signal. We should still get the notification. Process
158        # should receive the signal.
159        lldbutil.set_actions_for_signal(self, signal, "true", "false", "true")
160        self.expect("process continue", substrs=["stopped and restarted", signal])
161        self.assertState(process.GetState(), lldb.eStateExited)
162        self.assertEqual(process.GetExitStatus(), signo)
163
164        # launch again
165        process = self.launch(target, signal)
166
167        # Make sure we do not stop at the signal, and we do not get the notification. Process
168        # should receive the signal.
169        lldbutil.set_actions_for_signal(self, signal, "true", "false", "false")
170        self.expect(
171            "process continue", substrs=["stopped and restarted"], matching=False
172        )
173        self.assertState(process.GetState(), lldb.eStateExited)
174        self.assertEqual(process.GetExitStatus(), signo)
175
176        # reset signal handling to default
177        lldbutil.set_actions_for_signal(
178            self, signal, self.default_pass, self.default_stop, self.default_notify
179        )
180