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