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