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