1134d7f9aSJim Inghamimport lldb
2134d7f9aSJim Inghamfrom lldbsuite.test.lldbtest import *
3134d7f9aSJim Inghamfrom lldbsuite.test import lldbutil
4134d7f9aSJim Inghamfrom lldbsuite.test.decorators import *
5134d7f9aSJim Ingham
6134d7f9aSJim Inghamclass TestProcessHandle(TestBase):
7134d7f9aSJim Ingham
8134d7f9aSJim Ingham    @no_debug_info_test
9134d7f9aSJim Ingham    @skipIfWindows
10134d7f9aSJim Ingham    def test_process_handle(self):
11134d7f9aSJim Ingham        """Test that calling process handle before we have a target, and before we
12134d7f9aSJim Ingham           have a process will affect the process.  Also that the signal settings
13134d7f9aSJim Ingham           are preserved on rerun."""
14134d7f9aSJim Ingham        self.build()
15134d7f9aSJim Ingham
16134d7f9aSJim Ingham        # Make sure we don't accept signal values by signo with no process - we don't know what the
17134d7f9aSJim Ingham        # mapping will be so we can't do the right thing with bare numbers:
18134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "9", "true", None, None, expect_success=False)
19134d7f9aSJim Ingham
20134d7f9aSJim Ingham        # First, I need a reference value so I can see whether changes actually took:
21134d7f9aSJim Ingham        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
22134d7f9aSJim Ingham        (default_pass, default_stop, default_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
23134d7f9aSJim Ingham
24134d7f9aSJim Ingham        # Let's change the value here, then exit and make sure the changed value sticks:
25134d7f9aSJim Ingham        new_value = "false"
26134d7f9aSJim Ingham        if default_pass == "true":
27134d7f9aSJim Ingham            new_value = "false"
28134d7f9aSJim Ingham
29134d7f9aSJim Ingham        # First make sure we get an error for bogus values when running:
30134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "NOTSIGSEGV", new_value, None, None, expect_success=False)
31134d7f9aSJim Ingham
32134d7f9aSJim Ingham        # Then set the one we intend to change.
33134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
34134d7f9aSJim Ingham
35134d7f9aSJim Ingham        process.Continue()
36134d7f9aSJim Ingham
37*1b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited)
38134d7f9aSJim Ingham        self.assertEqual(process.GetExitStatus(), 0)
39134d7f9aSJim Ingham
40134d7f9aSJim Ingham        # Check that we preserved the setting:
41134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True)
42134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
43134d7f9aSJim Ingham        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
44134d7f9aSJim Ingham        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
45134d7f9aSJim Ingham
46134d7f9aSJim Ingham        # Run again and make sure that we prime the new process with these settings:
47134d7f9aSJim Ingham        process = lldbutil.run_to_breakpoint_do_run(self, target, bkpt)
48134d7f9aSJim Ingham
49134d7f9aSJim Ingham        # We check the process settings now, to see what got copied into the process:
50134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
51134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
52134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
53134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
54134d7f9aSJim Ingham
55134d7f9aSJim Ingham        # Now kill this target, set the handling and make sure the values get copied from the dummy into the new target.
56134d7f9aSJim Ingham        success = self.dbg.DeleteTarget(target)
57134d7f9aSJim Ingham        self.assertTrue(success, "Deleted the target")
58134d7f9aSJim Ingham        self.assertEqual(self.dbg.GetNumTargets(), 0, "We did delete all the targets.")
59134d7f9aSJim Ingham
60134d7f9aSJim Ingham        # The signal settings should be back at their default - we were only setting this on the target:
61134d7f9aSJim Ingham        lldbutil.get_actions_for_signal(self, "SIGSEGV", from_target=True, expected_absent=True)
62134d7f9aSJim Ingham        # Set a valid one:
63134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
64134d7f9aSJim Ingham        # Set a bogus one - we don't have a way to check pre-run so this is allowed
65134d7f9aSJim Ingham        # but we should get an error message when launching:
66134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGNOTSIG", new_value, None, None)
67134d7f9aSJim Ingham
68134d7f9aSJim Ingham        out_filename = self.getBuildArtifact('output')
69134d7f9aSJim Ingham        success = True
70134d7f9aSJim Ingham        try:
71134d7f9aSJim Ingham            f = open(out_filename, 'w')
72134d7f9aSJim Ingham        except:
73134d7f9aSJim Ingham            success = False
74134d7f9aSJim Ingham
75134d7f9aSJim Ingham        if not success:
76134d7f9aSJim Ingham            self.fail("Couldn't open error output file for writing.")
77134d7f9aSJim Ingham
78134d7f9aSJim Ingham        self.dbg.SetErrorFileHandle(f, False)
79134d7f9aSJim Ingham        # Now make a new process and make sure the right values got copied into the new target
80134d7f9aSJim Ingham        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
81134d7f9aSJim Ingham        f.write("TESTPATTERN\n")
82134d7f9aSJim Ingham        f.flush()
83134d7f9aSJim Ingham        f.close()
84134d7f9aSJim Ingham
85134d7f9aSJim Ingham        try:
86134d7f9aSJim Ingham            f = open(out_filename, 'r')
87134d7f9aSJim Ingham        except:
88134d7f9aSJim Ingham            success = False
89134d7f9aSJim Ingham
90134d7f9aSJim Ingham        if not success:
91134d7f9aSJim Ingham            self.fail("Couldn't open error output file for reading")
92134d7f9aSJim Ingham        errors = f.read()
93134d7f9aSJim Ingham        f.close()
94134d7f9aSJim Ingham
95134d7f9aSJim Ingham        self.assertIn("SIGNOTSIG", errors, "We warned about the unset signal")
96134d7f9aSJim Ingham        # Also make sure we didn't accidentally add this bogus setting to the process.
97134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGNOTSIG", "true", "true", "true", expect_success=False)
98134d7f9aSJim Ingham
99134d7f9aSJim Ingham        # Check that they went into the target:
100134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True)
101134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
102134d7f9aSJim Ingham        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
103134d7f9aSJim Ingham        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
104134d7f9aSJim Ingham
105134d7f9aSJim Ingham        # And the process:
106134d7f9aSJim Ingham        # Check that they went into the target:
107134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
108134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
109134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
110134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
111134d7f9aSJim Ingham
112134d7f9aSJim Ingham        # Now clear the handling, and make sure that we get the right signal values again:
113134d7f9aSJim Ingham        self.runCmd("process handle -c SIGSEGV")
114134d7f9aSJim Ingham        # Check that there is no longer configuration for SIGSEGV in the target:
115134d7f9aSJim Ingham        lldbutil.get_actions_for_signal(self, "SIGSEGV",from_target=True, expected_absent=True)
116134d7f9aSJim Ingham        # Make a new process, to make sure we did indeed reset the values:
117134d7f9aSJim Ingham        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
118134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
119134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
120134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
121134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
122134d7f9aSJim Ingham
123134d7f9aSJim Ingham        # Finally remove this from the dummy target as well, and make sure it was cleared from there:
124134d7f9aSJim Ingham        self.runCmd("process handle -c -d SIGSEGV")
125134d7f9aSJim Ingham        error = process.Kill()
126134d7f9aSJim Ingham        self.assertSuccess(error, "Killed the process")
127134d7f9aSJim Ingham        success = self.dbg.DeleteTarget(target)
128134d7f9aSJim Ingham        self.assertTrue(success, "Destroyed the target.")
129134d7f9aSJim Ingham
130134d7f9aSJim Ingham        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(self, '// break here', lldb.SBFileSpec("main.cpp"))
131134d7f9aSJim Ingham        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(self, "SIGSEGV")
132134d7f9aSJim Ingham        self.assertEqual(curr_pass, default_pass, "Pass was set correctly")
133134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
134134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
135