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