1"""Test that lldb command 'process signal SIGUSR1' to send a signal to the inferior works."""
2
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class SendSignalTestCase(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    def setUp(self):
16        # Call super's setUp().
17        TestBase.setUp(self)
18        # Find the line number to break inside main().
19        self.line = line_number('main.c', 'Put breakpoint here')
20
21    @expectedFailureNetBSD(bugnumber='llvm.org/pr43959')
22    @skipIfWindows  # Windows does not support signals
23    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
24    def test_with_run_command(self):
25        """Test that lldb command 'process signal SIGUSR1' sends a signal to the inferior process."""
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28
29        # Create a target by the debugger.
30        target = self.dbg.CreateTarget(exe)
31        self.assertTrue(target, VALID_TARGET)
32
33        # Now create a breakpoint on main.c by name 'c'.
34        breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
35        self.assertTrue(breakpoint and
36                        breakpoint.GetNumLocations() == 1,
37                        VALID_BREAKPOINT)
38
39        # Get the breakpoint location from breakpoint after we verified that,
40        # indeed, it has one location.
41        location = breakpoint.GetLocationAtIndex(0)
42        self.assertTrue(location and
43                        location.IsEnabled(),
44                        VALID_BREAKPOINT_LOCATION)
45
46        # Now launch the process, no arguments & do not stop at entry point.
47        launch_info = target.GetLaunchInfo()
48        launch_info.SetWorkingDirectory(self.get_process_working_directory())
49
50        process_listener = lldb.SBListener("signal_test_listener")
51        launch_info.SetListener(process_listener)
52        error = lldb.SBError()
53        process = target.Launch(launch_info, error)
54        self.assertTrue(process, PROCESS_IS_VALID)
55
56        self.runCmd("process handle -n False -p True -s True SIGUSR1")
57
58        thread = lldbutil.get_stopped_thread(
59            process, lldb.eStopReasonBreakpoint)
60        self.assertTrue(thread.IsValid(), "We hit the first breakpoint.")
61
62        # After resuming the process, send it a SIGUSR1 signal.
63
64        self.setAsync(True)
65
66        self.assertTrue(
67            process_listener.IsValid(),
68            "Got a good process listener")
69
70        # Disable our breakpoint, we don't want to hit it anymore...
71        breakpoint.SetEnabled(False)
72
73        # Now continue:
74        process.Continue()
75
76        # If running remote test, there should be a connected event
77        if lldb.remote_platform:
78            self.match_state(process_listener, lldb.eStateConnected)
79
80        self.match_state(process_listener, lldb.eStateRunning)
81
82        # Now signal the process, and make sure it stops:
83        process.Signal(lldbutil.get_signal_number('SIGUSR1'))
84
85        self.match_state(process_listener, lldb.eStateStopped)
86
87        # Now make sure the thread was stopped with a SIGUSR1:
88        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonSignal)
89        self.assertEquals(len(threads), 1, "One thread stopped for a signal.")
90        thread = threads[0]
91
92        self.assertTrue(
93            thread.GetStopReasonDataCount() >= 1,
94            "There was data in the event.")
95        self.assertEqual(
96            thread.GetStopReasonDataAtIndex(0), lldbutil.get_signal_number('SIGUSR1'),
97            "The stop signal was SIGUSR1")
98
99    def match_state(self, process_listener, expected_state):
100        num_seconds = 5
101        broadcaster = self.process().GetBroadcaster()
102        event_type_mask = lldb.SBProcess.eBroadcastBitStateChanged
103        event = lldb.SBEvent()
104        got_event = process_listener.WaitForEventForBroadcasterWithType(
105            num_seconds, broadcaster, event_type_mask, event)
106        self.assertTrue(got_event, "Got an event")
107        state = lldb.SBProcess.GetStateFromEvent(event)
108        self.assertEquals(state, expected_state,
109                        "It was the %s state." %
110                        lldb.SBDebugger_StateAsCString(expected_state))
111