xref: /llvm-project/lldb/test/API/python_api/run_locker/TestRunLocker.py (revision 83f8caeab476646eea21bdde619b0beb84ebd70b)
1"""
2Test that the run locker really does work to keep
3us from running SB API that should only be run
4while stopped.  This test is mostly concerned with
5what happens between launch and first stop.
6"""
7
8import lldb
9import lldbsuite.test.lldbutil as lldbutil
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12
13
14class TestRunLocker(TestBase):
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @expectedFailureAll(oslist=["windows"])
18    def test_run_locker(self):
19        """Test that the run locker is set correctly when we launch"""
20        self.build()
21        self.runlocker_test(False)
22
23    @expectedFailureAll(oslist=["windows"])
24    # Is flaky on Linux AArch64 buildbot.
25    @skipIf(oslist=["linux"], archs=["aarch64"])
26    def test_run_locker_stop_at_entry(self):
27        """Test that the run locker is set correctly when we launch"""
28        self.build()
29        self.runlocker_test(False)
30
31    def setUp(self):
32        # Call super's setUp().
33        TestBase.setUp(self)
34        self.main_source_file = lldb.SBFileSpec("main.c")
35
36    def runlocker_test(self, stop_at_entry):
37        """The code to stop at entry handles events slightly differently, so
38        we test both versions of process launch."""
39
40        target = lldbutil.run_to_breakpoint_make_target(self)
41
42        launch_info = target.GetLaunchInfo()
43        if stop_at_entry:
44            flags = launch_info.GetFlags()
45            launch_info.SetFlags(flags | lldb.eLaunchFlagStopAtEntry)
46
47        error = lldb.SBError()
48        # We are trying to do things when the process is running, so
49        # we have to run the debugger asynchronously.
50        self.dbg.SetAsync(True)
51
52        listener = lldb.SBListener("test-run-lock-listener")
53        launch_info.SetListener(listener)
54        process = target.Launch(launch_info, error)
55        self.assertSuccess(error, "Launched the process")
56
57        event = lldb.SBEvent()
58
59        event_result = listener.WaitForEvent(10, event)
60        self.assertTrue(event_result, "timed out waiting for launch")
61        state_type = lldb.SBProcess.GetStateFromEvent(event)
62        # We don't always see a launching...
63        if state_type == lldb.eStateLaunching:
64            event_result = listener.WaitForEvent(10, event)
65            self.assertTrue(
66                event_result, "Timed out waiting for running after launching"
67            )
68            state_type = lldb.SBProcess.GetStateFromEvent(event)
69
70        self.assertState(state_type, lldb.eStateRunning, "Didn't get a running event")
71
72        # We aren't checking the entry state, but just making sure
73        # the running state is set properly if we continue in this state.
74
75        if stop_at_entry:
76            event_result = listener.WaitForEvent(10, event)
77            self.assertTrue(event_result, "Timed out waiting for stop at entry stop")
78            state_type = lldb.SBProcess.GetStateFromEvent(event)
79            self.assertState(state_type, eStateStopped, "Stop at entry stopped")
80            process.Continue()
81
82        # Okay, now the process is running, make sure we can't do things
83        # you aren't supposed to do while running, and that we get some
84        # actual error:
85        val = target.EvaluateExpression("SomethingToCall()")
86        error = val.GetError()
87        self.assertTrue(error.Fail(), "Failed to run expression")
88        self.assertIn(
89            "can't evaluate expressions when the process is running",
90            error.GetCString(),
91            "Stopped by stop locker",
92        )
93
94        # This should also fail if we try to use the script interpreter directly:
95        interp = self.dbg.GetCommandInterpreter()
96        result = lldb.SBCommandReturnObject()
97        ret = interp.HandleCommand(
98            "script var = lldb.frame.EvaluateExpression('SomethingToCall()'); var.GetError().GetCString()",
99            result,
100        )
101        self.assertIn(
102            "can't evaluate expressions when the process is running", result.GetOutput()
103        )
104