TestSendSignal.py revision d7a4eb073d08bf632154b74189e36a28726cefaa
1d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen"""Test that lldb command 'process signal SIGUSR1' to send a signal to the inferior works."""
2d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
3d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenimport os, time, signal
4d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenimport unittest2
5d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenimport lldb
6d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenfrom lldbtest import *
7d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
8d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenclass SendSignalTestCase(TestBase):
9d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
10d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    mydir = "signal"
11d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
12d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
13d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    def test_with_dsym_and_run_command(self):
14d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        """Test that lldb command 'process signal SIGUSR1' sends a signal to the inferior process."""
15d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.buildDsym()
16d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.send_signal()
17d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
18d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    def test_with_dwarf_and_run_command(self):
19d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        """Test that lldb command 'process signal SIGUSR1' sends a signal to the inferior process."""
20d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.buildDwarf()
21d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.send_signal()
22d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
23d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    def setUp(self):
24d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        super(SendSignalTestCase, self).setUp()
25d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        # Find the line number to break inside main().
26d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.line = line_number('main.c', 'Put breakpoint here')
27d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
28d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    def send_signal(self):
29d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        """Test that lldb command 'process signal SIGUSR1' sends a signal to the inferior process."""
30d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
31d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        exe = os.path.join(os.getcwd(), "a.out")
32d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
33d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
34d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        # Break inside the main() function and immediately send a signal to the inferior after resuming.
35d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.expect("breakpoint set -f main.c -l %d" % self.line,
36d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen                    BREAKPOINT_CREATED,
37d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            startstr = "Breakpoint created: 1: file ='main.c', line = %d, locations = 1" %
38d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen                        self.line)
39d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
40d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("run", RUN_SUCCEEDED)
41d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("thread backtrac")
42d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
43d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        # The stop reason of the thread should be breakpoint.
44d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
45d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            substrs = ['state is Stopped',
46d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen                       'stop reason = breakpoint'])
47d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
48d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        # The breakpoint should have a hit count of 1.
49d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.expect("breakpoint list", BREAKPOINT_HIT_ONCE,
50d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            substrs = [' resolved, hit count = 1'])
51d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
52d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("process status")
53d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        output = self.res.GetOutput()
54d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        pid = re.match("Process (.*) Stopped", output).group(1)
55d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
56d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        # After resuming the process, immediately send a SIGUSR1 signal.
57d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.dbg.SetAsync(True)
58d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("process continue")
59d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("process handle -n False -p True -s True SIGUSR1")
60d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        #os.kill(int(pid), signal.SIGUSR1)
61d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.runCmd("process signal SIGUSR1")
62d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
63d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        time.sleep(1)
64d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.dbg.SetAsync(False)
65d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.expect("process status", STOPPED_DUE_TO_SIGNAL,
66d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            startstr = "Process %s Stopped" % pid,
67d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            substrs = ['stop reason = signal SIGUSR1'])
68d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen        self.expect("thread backtrace", STOPPED_DUE_TO_SIGNAL,
69d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen            substrs = ['stop reason = signal SIGUSR1'])
70d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
71d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen
72d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chenif __name__ == '__main__':
73d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    import atexit
74d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    lldb.SBDebugger.Initialize()
75d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    atexit.register(lambda: lldb.SBDebugger.Terminate())
76d7a4eb073d08bf632154b74189e36a28726cefaaJohnny Chen    unittest2.main()
77