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