xref: /llvm-project/lldb/test/API/commands/process/handle/TestProcessHandle.py (revision 2238dcc39358353cac21df75c3c3286ab20b8f53)
1134d7f9aSJim Inghamimport lldb
2134d7f9aSJim Inghamfrom lldbsuite.test.lldbtest import *
3134d7f9aSJim Inghamfrom lldbsuite.test import lldbutil
4134d7f9aSJim Inghamfrom lldbsuite.test.decorators import *
5134d7f9aSJim Ingham
6134d7f9aSJim Ingham
7*2238dcc3SJonas Devlieghereclass TestProcessHandle(TestBase):
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:
18*2238dcc3SJonas Devlieghere        lldbutil.set_actions_for_signal(
19*2238dcc3SJonas Devlieghere            self, "9", "true", None, None, expect_success=False
20*2238dcc3SJonas Devlieghere        )
21134d7f9aSJim Ingham
22134d7f9aSJim Ingham        # First, I need a reference value so I can see whether changes actually took:
23*2238dcc3SJonas Devlieghere        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(
24*2238dcc3SJonas Devlieghere            self, "// break here", lldb.SBFileSpec("main.cpp")
25*2238dcc3SJonas Devlieghere        )
26*2238dcc3SJonas Devlieghere        (default_pass, default_stop, default_notify) = lldbutil.get_actions_for_signal(
27*2238dcc3SJonas Devlieghere            self, "SIGSEGV"
28*2238dcc3SJonas Devlieghere        )
29134d7f9aSJim Ingham
30134d7f9aSJim Ingham        # Let's change the value here, then exit and make sure the changed value sticks:
31134d7f9aSJim Ingham        new_value = "false"
32134d7f9aSJim Ingham        if default_pass == "true":
33134d7f9aSJim Ingham            new_value = "false"
34134d7f9aSJim Ingham
35134d7f9aSJim Ingham        # First make sure we get an error for bogus values when running:
36*2238dcc3SJonas Devlieghere        lldbutil.set_actions_for_signal(
37*2238dcc3SJonas Devlieghere            self, "NOTSIGSEGV", new_value, None, None, expect_success=False
38*2238dcc3SJonas Devlieghere        )
39134d7f9aSJim Ingham
40134d7f9aSJim Ingham        # Then set the one we intend to change.
41134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
42134d7f9aSJim Ingham
43134d7f9aSJim Ingham        process.Continue()
44134d7f9aSJim Ingham
451b8c7352SJonas Devlieghere        self.assertState(process.GetState(), lldb.eStateExited)
46134d7f9aSJim Ingham        self.assertEqual(process.GetExitStatus(), 0)
47134d7f9aSJim Ingham
48134d7f9aSJim Ingham        # Check that we preserved the setting:
49*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
50*2238dcc3SJonas Devlieghere            self, "SIGSEGV", from_target=True
51*2238dcc3SJonas Devlieghere        )
52134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
53134d7f9aSJim Ingham        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
54134d7f9aSJim Ingham        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
55134d7f9aSJim Ingham
56134d7f9aSJim Ingham        # Run again and make sure that we prime the new process with these settings:
57134d7f9aSJim Ingham        process = lldbutil.run_to_breakpoint_do_run(self, target, bkpt)
58134d7f9aSJim Ingham
59134d7f9aSJim Ingham        # We check the process settings now, to see what got copied into the process:
60*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
61*2238dcc3SJonas Devlieghere            self, "SIGSEGV"
62*2238dcc3SJonas Devlieghere        )
63134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
64134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
65134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
66134d7f9aSJim Ingham
67134d7f9aSJim Ingham        # Now kill this target, set the handling and make sure the values get copied from the dummy into the new target.
68134d7f9aSJim Ingham        success = self.dbg.DeleteTarget(target)
69134d7f9aSJim Ingham        self.assertTrue(success, "Deleted the target")
70134d7f9aSJim Ingham        self.assertEqual(self.dbg.GetNumTargets(), 0, "We did delete all the targets.")
71134d7f9aSJim Ingham
72134d7f9aSJim Ingham        # The signal settings should be back at their default - we were only setting this on the target:
73*2238dcc3SJonas Devlieghere        lldbutil.get_actions_for_signal(
74*2238dcc3SJonas Devlieghere            self, "SIGSEGV", from_target=True, expected_absent=True
75*2238dcc3SJonas Devlieghere        )
76134d7f9aSJim Ingham        # Set a valid one:
77134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGSEGV", new_value, None, None)
78134d7f9aSJim Ingham        # Set a bogus one - we don't have a way to check pre-run so this is allowed
79134d7f9aSJim Ingham        # but we should get an error message when launching:
80134d7f9aSJim Ingham        lldbutil.set_actions_for_signal(self, "SIGNOTSIG", new_value, None, None)
81134d7f9aSJim Ingham
82*2238dcc3SJonas Devlieghere        out_filename = self.getBuildArtifact("output")
83134d7f9aSJim Ingham        success = True
84134d7f9aSJim Ingham        try:
85*2238dcc3SJonas Devlieghere            f = open(out_filename, "w")
86134d7f9aSJim Ingham        except:
87134d7f9aSJim Ingham            success = False
88134d7f9aSJim Ingham
89134d7f9aSJim Ingham        if not success:
90134d7f9aSJim Ingham            self.fail("Couldn't open error output file for writing.")
91134d7f9aSJim Ingham
92134d7f9aSJim Ingham        self.dbg.SetErrorFileHandle(f, False)
93134d7f9aSJim Ingham        # Now make a new process and make sure the right values got copied into the new target
94*2238dcc3SJonas Devlieghere        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(
95*2238dcc3SJonas Devlieghere            self, "// break here", lldb.SBFileSpec("main.cpp")
96*2238dcc3SJonas Devlieghere        )
97134d7f9aSJim Ingham        f.write("TESTPATTERN\n")
98134d7f9aSJim Ingham        f.flush()
99134d7f9aSJim Ingham        f.close()
100134d7f9aSJim Ingham
101134d7f9aSJim Ingham        try:
102*2238dcc3SJonas Devlieghere            f = open(out_filename, "r")
103134d7f9aSJim Ingham        except:
104134d7f9aSJim Ingham            success = False
105134d7f9aSJim Ingham
106134d7f9aSJim Ingham        if not success:
107134d7f9aSJim Ingham            self.fail("Couldn't open error output file for reading")
108134d7f9aSJim Ingham        errors = f.read()
109134d7f9aSJim Ingham        f.close()
110134d7f9aSJim Ingham
111134d7f9aSJim Ingham        self.assertIn("SIGNOTSIG", errors, "We warned about the unset signal")
112134d7f9aSJim Ingham        # Also make sure we didn't accidentally add this bogus setting to the process.
113*2238dcc3SJonas Devlieghere        lldbutil.set_actions_for_signal(
114*2238dcc3SJonas Devlieghere            self, "SIGNOTSIG", "true", "true", "true", expect_success=False
115*2238dcc3SJonas Devlieghere        )
116134d7f9aSJim Ingham
117134d7f9aSJim Ingham        # Check that they went into the target:
118*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
119*2238dcc3SJonas Devlieghere            self, "SIGSEGV", from_target=True
120*2238dcc3SJonas Devlieghere        )
121134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
122134d7f9aSJim Ingham        self.assertEqual(curr_stop, "not set", "Stop was not set by us")
123134d7f9aSJim Ingham        self.assertEqual(curr_notify, "not set", "Notify was not set by us")
124134d7f9aSJim Ingham
125134d7f9aSJim Ingham        # And the process:
126134d7f9aSJim Ingham        # Check that they went into the target:
127*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
128*2238dcc3SJonas Devlieghere            self, "SIGSEGV"
129*2238dcc3SJonas Devlieghere        )
130134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
131134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
132134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
133134d7f9aSJim Ingham
134134d7f9aSJim Ingham        # Now clear the handling, and make sure that we get the right signal values again:
135134d7f9aSJim Ingham        self.runCmd("process handle -c SIGSEGV")
136134d7f9aSJim Ingham        # Check that there is no longer configuration for SIGSEGV in the target:
137*2238dcc3SJonas Devlieghere        lldbutil.get_actions_for_signal(
138*2238dcc3SJonas Devlieghere            self, "SIGSEGV", from_target=True, expected_absent=True
139*2238dcc3SJonas Devlieghere        )
140134d7f9aSJim Ingham        # Make a new process, to make sure we did indeed reset the values:
141*2238dcc3SJonas Devlieghere        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(
142*2238dcc3SJonas Devlieghere            self, "// break here", lldb.SBFileSpec("main.cpp")
143*2238dcc3SJonas Devlieghere        )
144*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
145*2238dcc3SJonas Devlieghere            self, "SIGSEGV"
146*2238dcc3SJonas Devlieghere        )
147134d7f9aSJim Ingham        self.assertEqual(curr_pass, new_value, "Pass was set correctly")
148134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
149134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
150134d7f9aSJim Ingham
151134d7f9aSJim Ingham        # Finally remove this from the dummy target as well, and make sure it was cleared from there:
152134d7f9aSJim Ingham        self.runCmd("process handle -c -d SIGSEGV")
153134d7f9aSJim Ingham        error = process.Kill()
154134d7f9aSJim Ingham        self.assertSuccess(error, "Killed the process")
155134d7f9aSJim Ingham        success = self.dbg.DeleteTarget(target)
156134d7f9aSJim Ingham        self.assertTrue(success, "Destroyed the target.")
157134d7f9aSJim Ingham
158*2238dcc3SJonas Devlieghere        (target, process, _, bkpt) = lldbutil.run_to_source_breakpoint(
159*2238dcc3SJonas Devlieghere            self, "// break here", lldb.SBFileSpec("main.cpp")
160*2238dcc3SJonas Devlieghere        )
161*2238dcc3SJonas Devlieghere        (curr_pass, curr_stop, curr_notify) = lldbutil.get_actions_for_signal(
162*2238dcc3SJonas Devlieghere            self, "SIGSEGV"
163*2238dcc3SJonas Devlieghere        )
164134d7f9aSJim Ingham        self.assertEqual(curr_pass, default_pass, "Pass was set correctly")
165134d7f9aSJim Ingham        self.assertEqual(curr_stop, default_stop, "Stop was its default value")
166134d7f9aSJim Ingham        self.assertEqual(curr_notify, default_notify, "Notify was its default value")
167