xref: /llvm-project/lldb/test/API/python_api/run_locker/TestRunLocker.py (revision 588da01621a1f8efd139e677526b64c1e5ecf303)
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.lldbtest import *
11
12
13class TestRunLocker(TestBase):
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    def test_run_locker(self):
18        """Test that the run locker is set correctly when we launch"""
19        self.build()
20        self.runlocker_test(False)
21
22    def test_run_locker_stop_at_entry(self):
23        """Test that the run locker is set correctly when we launch"""
24        self.build()
25        self.runlocker_test(False)
26
27    def setUp(self):
28        # Call super's setUp().
29        TestBase.setUp(self)
30        self.main_source_file = lldb.SBFileSpec("main.c")
31
32    def runlocker_test(self, stop_at_entry):
33        """The code to stop at entry handles events slightly differently, so
34           we test both versions of process launch."""
35
36        target = lldbutil.run_to_breakpoint_make_target(self)
37
38        launch_info = target.GetLaunchInfo()
39        if stop_at_entry:
40            flags = launch_info.GetFlags()
41            launch_info.SetFlags(flags | lldb.eLaunchFlagStopAtEntry)
42
43        error = lldb.SBError()
44        # We are trying to do things when the process is running, so
45        # we have to run the debugger asynchronously.
46        self.dbg.SetAsync(True)
47
48        listener = lldb.SBListener("test-run-lock-listener")
49        launch_info.SetListener(listener)
50        process = target.Launch(launch_info, error)
51        self.assertSuccess(error, "Launched the process")
52
53        event = lldb.SBEvent()
54
55        event_result = listener.WaitForEvent(10, event)
56        self.assertTrue(event_result, "timed out waiting for launch")
57        state_type = lldb.SBProcess.GetStateFromEvent(event)
58        # We don't always see a launching...
59        if state_type == lldb.eStateLaunching:
60            event_result = listener.WaitForEvent(10, event)
61            self.assertTrue(event_result, "Timed out waiting for running after launching")
62            state_type = lldb.SBProcess.GetStateFromEvent(event)
63
64        self.assertState(state_type, lldb.eStateRunning, "Didn't get a running event")
65
66        # We aren't checking the entry state, but just making sure
67        # the running state is set properly if we continue in this state.
68
69        if stop_at_entry:
70            event_result = listener.WaitForEvent(10, event)
71            self.assertTrue(event_result, "Timed out waiting for stop at entry stop")
72            state_type = lldb.SBProcess.GetStateFromEvent(event)
73            self.assertState(state_type, eStateStopped, "Stop at entry stopped")
74            process.Continue()
75
76        # Okay, now the process is running, make sure we can't do things
77        # you aren't supposed to do while running, and that we get some
78        # actual error:
79        val = target.EvaluateExpression("SomethingToCall()")
80        error = val.GetError()
81        self.assertTrue(error.Fail(), "Failed to run expression")
82        print(f"Got Error: {error.GetCString()}")
83        self.assertIn("can't evaluate expressions when the process is running", error.GetCString(), "Stopped by stop locker")
84
85        # This should also fail if we try to use the script interpreter directly:
86        interp = self.dbg.GetCommandInterpreter()
87        result = lldb.SBCommandReturnObject()
88        ret = interp.HandleCommand("script var = lldb.frame.EvaluateExpression('SomethingToCall()'); var.GetError().GetCString()", result)
89        self.assertIn("can't evaluate expressions when the process is running", result.GetOutput())
90