1import lldb
2from lldbsuite.test.lldbtest import *
3from lldbsuite.test import lldbutil
4from lldbsuite.test.decorators import *
5
6class TestProcessHandle(TestBase):
7
8    @no_debug_info_test
9    @skipIfWindows
10    def test_process_handle(self):
11        """Test that calling process handle before we have a target, and before we
12           have a process will affect the process.  Also that the signal settings
13           are preserved on rerun."""
14        self.build()
15
16        # Make sure we don't accept signal values by signo with no process - we don't know what the
17        # mapping will be so we can't do the right thing with bare numbers:
18        lldbutil.set_actions_for_signal(self, "9", "true", None, None, expect_success=False)
19
20        # First, I need a reference value so I can see whether changes actually took:
21        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
22        (default_pass, default_stop, default_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
23
24        # Let's change the value here, then exit and make sure the changed value sticks:
25        new_value = "false"
26        if default_pass == "true":
27            new_value = "false"
28
29        # First make sure we get an error for bogus values when running:
30        lldbutil.set_actions_for_signal(self, "NOTSIGSEGV", new_value, None, None, expect_success=False)
31
32        # Then set the one we intend to change.
33        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
34
35        process.Continue()
36
37        self.assertState(process.GetState(), lldb.eStateExited)
38        self.assertEqual(process.GetExitStatus(), 0)
39
40        # Check that we preserved the setting:
41        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True)
42        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
43        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
44        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
45
46        # Run again and make sure that we prime the new process with these settings:
47        process = lldbutil.run_to_breakpoint_do_run(self, target, bkpt)
48
49        # We check the process settings now, to see what got copied into the process:
50        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
51        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
52        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
53        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
54
55        # Now kill this target, set the handling and make sure the values get copied from the dummy into the new target.
56        success = self.dbg.DeleteTarget(target)
57        self.assertTrue(success, "Deleted the target")
58        self.assertEqual(self.dbg.GetNumTargets(), 0, "We did delete all the targets.")
59
60        # The signal settings should be back at their default - we were only setting this on the target:
61        lldbutil.get_actions_for_signal(self, "SIGSEGV", from_target=True, expected_absent=True)
62        # Set a valid one:
63        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
64        # Set a bogus one - we don't have a way to check pre-run so this is allowed
65        # but we should get an error message when launching:
66        lldbutil.set_actions_for_signal(self, "SIGNOTSIG", new_value, None, None)
67
68        out_filename = self.getBuildArtifact('output')
69        success = True
70        try:
71            f = open(out_filename, 'w')
72        except:
73            success = False
74
75        if not success:
76            self.fail("Couldn't open error output file for writing.")
77
78        self.dbg.SetErrorFileHandle(f, False)
79        # Now make a new process and make sure the right values got copied into the new target
80        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
81        f.write("TESTPATTERN\n")
82        f.flush()
83        f.close()
84
85        try:
86            f = open(out_filename, 'r')
87        except:
88            success = False
89
90        if not success:
91            self.fail("Couldn't open error output file for reading")
92        errors = f.read()
93        f.close()
94
95        self.assertIn("SIGNOTSIG", errors, "We warned about the unset signal")
96        # Also make sure we didn't accidentally add this bogus setting to the process.
97        lldbutil.set_actions_for_signal(self, "SIGNOTSIG", "true", "true", "true", expect_success=False)
98
99        # Check that they went into the target:
100        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True)
101        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
102        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
103        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
104
105        # And the process:
106        # Check that they went into the target:
107        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
108        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
109        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
110        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
111
112        # Now clear the handling, and make sure that we get the right signal values again:
113        self.runCmd("process handle -c SIGSEGV")
114        # Check that there is no longer configuration for SIGSEGV in the target:
115        lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True, expected_absent=True)
116        # Make a new process, to make sure we did indeed reset the values:
117        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
118        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
119        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
120        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
121        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
122
123        # Finally remove this from the dummy target as well, and make sure it was cleared from there:
124        self.runCmd("process handle -c -d SIGSEGV")
125        error = process.Kill()
126        self.assertSuccess(error, "Killed the process")
127        success = self.dbg.DeleteTarget(target)
128        self.assertTrue(success, "Destroyed the target.")
129
130        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
131        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
132        self.assertEqual(curr_pass, default_pass, "Pass was set correctly")
133        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
134        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
135