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