xref: /llvm-project/lldb/test/API/functionalities/step_scripted/TestStepScripted.py (revision 6cb45aea92dc87974a0064c182600228c6e94329)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTests stepping with scripted thread plans.
399451b44SJordan Rupprecht"""
4d3dfd8ceSJim Inghamimport threading
599451b44SJordan Rupprechtimport lldb
699451b44SJordan Rupprechtimport lldbsuite.test.lldbutil as lldbutil
71e566f6bSJonas Devliegherefrom lldbsuite.test.decorators import *
899451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
999451b44SJordan Rupprecht
10*6cb45aeaSMed Ismail Bennani
112238dcc3SJonas Devlieghereclass StepScriptedTestCase(TestBase):
1299451b44SJordan Rupprecht    NO_DEBUG_INFO_TESTCASE = True
1399451b44SJordan Rupprecht
1499451b44SJordan Rupprecht    def setUp(self):
1599451b44SJordan Rupprecht        TestBase.setUp(self)
1699451b44SJordan Rupprecht        self.main_source_file = lldb.SBFileSpec("main.c")
1799451b44SJordan Rupprecht        self.runCmd("command script import Steps.py")
1899451b44SJordan Rupprecht
1999451b44SJordan Rupprecht    def test_standard_step_out(self):
2099451b44SJordan Rupprecht        """Tests stepping with the scripted thread plan laying over a standard
2199451b44SJordan Rupprecht        thread plan for stepping out."""
2299451b44SJordan Rupprecht        self.build()
2399451b44SJordan Rupprecht        self.step_out_with_scripted_plan("Steps.StepOut")
2499451b44SJordan Rupprecht
2599451b44SJordan Rupprecht    def test_scripted_step_out(self):
2699451b44SJordan Rupprecht        """Tests stepping with the scripted thread plan laying over an another
2799451b44SJordan Rupprecht        scripted thread plan for stepping out."""
2899451b44SJordan Rupprecht        self.build()
2999451b44SJordan Rupprecht        self.step_out_with_scripted_plan("Steps.StepScripted")
3099451b44SJordan Rupprecht
3199451b44SJordan Rupprecht    def step_out_with_scripted_plan(self, name):
322238dcc3SJonas Devlieghere        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
332238dcc3SJonas Devlieghere            self, "Set a breakpoint here", self.main_source_file
342238dcc3SJonas Devlieghere        )
3599451b44SJordan Rupprecht
3699451b44SJordan Rupprecht        frame = thread.GetFrameAtIndex(0)
3799451b44SJordan Rupprecht        self.assertEqual("foo", frame.GetFunctionName())
3899451b44SJordan Rupprecht
3999451b44SJordan Rupprecht        err = thread.StepUsingScriptedThreadPlan(name)
40779bbbf2SDave Lee        self.assertSuccess(err)
4199451b44SJordan Rupprecht
4299451b44SJordan Rupprecht        frame = thread.GetFrameAtIndex(0)
4399451b44SJordan Rupprecht        self.assertEqual("main", frame.GetFunctionName())
44c2be7021SJim Ingham        stop_desc = thread.GetStopDescription(1000)
45c2be7021SJim Ingham        self.assertIn("Stepping out from", stop_desc, "Got right description")
4699451b44SJordan Rupprecht
4799451b44SJordan Rupprecht    def test_misspelled_plan_name(self):
4899451b44SJordan Rupprecht        """Test that we get a useful error if we misspell the plan class name"""
4999451b44SJordan Rupprecht        self.build()
502238dcc3SJonas Devlieghere        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
512238dcc3SJonas Devlieghere            self, "Set a breakpoint here", self.main_source_file
522238dcc3SJonas Devlieghere        )
5399451b44SJordan Rupprecht        stop_id = process.GetStopID()
5499451b44SJordan Rupprecht        # Pass a non-existent class for the plan class:
5599451b44SJordan Rupprecht        err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")
5699451b44SJordan Rupprecht
5799451b44SJordan Rupprecht        # Make sure we got a good error:
5899451b44SJordan Rupprecht        self.assertTrue(err.Fail(), "We got a failure state")
5999451b44SJordan Rupprecht        msg = err.GetCString()
603cc37622SDave Lee        self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class")
6199451b44SJordan Rupprecht
6299451b44SJordan Rupprecht        # Make sure we didn't let the process run:
6399451b44SJordan Rupprecht        self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht    def test_checking_variable(self):
6699451b44SJordan Rupprecht        """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""
6799451b44SJordan Rupprecht        self.do_test_checking_variable(False)
6899451b44SJordan Rupprecht
6999451b44SJordan Rupprecht    def test_checking_variable_cli(self):
7099451b44SJordan Rupprecht        """Test that we can call SBValue API's from a scripted thread plan - using cli to step"""
7199451b44SJordan Rupprecht        self.do_test_checking_variable(True)
7299451b44SJordan Rupprecht
7399451b44SJordan Rupprecht    def do_test_checking_variable(self, use_cli):
7499451b44SJordan Rupprecht        self.build()
752238dcc3SJonas Devlieghere        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
762238dcc3SJonas Devlieghere            self, "Set a breakpoint here", self.main_source_file
772238dcc3SJonas Devlieghere        )
7899451b44SJordan Rupprecht
7999451b44SJordan Rupprecht        frame = thread.GetFrameAtIndex(0)
8099451b44SJordan Rupprecht        self.assertEqual("foo", frame.GetFunctionName())
8199451b44SJordan Rupprecht        foo_val = frame.FindVariable("foo")
82779bbbf2SDave Lee        self.assertSuccess(foo_val.GetError(), "Got the foo variable")
8399451b44SJordan Rupprecht        self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10")
8499451b44SJordan Rupprecht
8599451b44SJordan Rupprecht        if use_cli:
8699451b44SJordan Rupprecht            result = lldb.SBCommandReturnObject()
8799451b44SJordan Rupprecht            self.dbg.GetCommandInterpreter().HandleCommand(
8899451b44SJordan Rupprecht                "thread step-scripted -C Steps.StepUntil -k variable_name -v foo",
892238dcc3SJonas Devlieghere                result,
902238dcc3SJonas Devlieghere            )
9199451b44SJordan Rupprecht            self.assertTrue(result.Succeeded())
9299451b44SJordan Rupprecht        else:
9399451b44SJordan Rupprecht            args_data = lldb.SBStructuredData()
9499451b44SJordan Rupprecht            data = lldb.SBStream()
9599451b44SJordan Rupprecht            data.Print('{"variable_name" : "foo"}')
9699451b44SJordan Rupprecht            error = args_data.SetFromJSON(data)
97779bbbf2SDave Lee            self.assertSuccess(error, "Made the args_data correctly")
9899451b44SJordan Rupprecht
9999451b44SJordan Rupprecht            err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True)
100779bbbf2SDave Lee            self.assertSuccess(err)
10199451b44SJordan Rupprecht
10299451b44SJordan Rupprecht        # We should not have exited:
10347c4c6a7SDave Lee        self.assertState(process.GetState(), lldb.eStateStopped, "We are stopped")
10499451b44SJordan Rupprecht
10599451b44SJordan Rupprecht        # We should still be in foo:
10699451b44SJordan Rupprecht        self.assertEqual("foo", frame.GetFunctionName())
10799451b44SJordan Rupprecht
10899451b44SJordan Rupprecht        # And foo should have changed:
10999451b44SJordan Rupprecht        self.assertTrue(foo_val.GetValueDidChange(), "Foo changed")
110d3dfd8ceSJim Ingham
111c2be7021SJim Ingham        # And we should have a reasonable stop description:
112c2be7021SJim Ingham        desc = thread.GetStopDescription(1000)
113c2be7021SJim Ingham        self.assertIn("Stepped until foo changed", desc, "Got right stop description")
114c2be7021SJim Ingham
115d3dfd8ceSJim Ingham    def test_stop_others_from_command(self):
116d3dfd8ceSJim Ingham        """Test that the stop-others flag is set correctly by the command line.
117d3dfd8ceSJim Ingham        Also test that the run-all-threads property overrides this."""
118d3dfd8ceSJim Ingham        self.do_test_stop_others()
119d3dfd8ceSJim Ingham
120d3dfd8ceSJim Ingham    def run_step(self, stop_others_value, run_mode, token):
121d3dfd8ceSJim Ingham        import Steps
1222238dcc3SJonas Devlieghere
123d3dfd8ceSJim Ingham        interp = self.dbg.GetCommandInterpreter()
124d3dfd8ceSJim Ingham        result = lldb.SBCommandReturnObject()
125d3dfd8ceSJim Ingham
1262238dcc3SJonas Devlieghere        cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s" % (
1272238dcc3SJonas Devlieghere            token
1282238dcc3SJonas Devlieghere        )
12958611451SEisuke Kawashima        if run_mode is not None:
130d3dfd8ceSJim Ingham            cmd = cmd + " --run-mode %s" % (run_mode)
131c2be7021SJim Ingham        if self.TraceOn():
132d3dfd8ceSJim Ingham            print(cmd)
133d3dfd8ceSJim Ingham        interp.HandleCommand(cmd, result)
1342238dcc3SJonas Devlieghere        self.assertTrue(
1352238dcc3SJonas Devlieghere            result.Succeeded(), "Step scripted failed: %s." % (result.GetError())
1362238dcc3SJonas Devlieghere        )
137c2be7021SJim Ingham        if self.TraceOn():
138d3dfd8ceSJim Ingham            print(Steps.StepReportsStopOthers.stop_mode_dict)
139d3dfd8ceSJim Ingham        value = Steps.StepReportsStopOthers.stop_mode_dict[token]
140d3dfd8ceSJim Ingham        self.assertEqual(value, stop_others_value, "Stop others has the correct value.")
141d3dfd8ceSJim Ingham
142d3dfd8ceSJim Ingham    def do_test_stop_others(self):
143d3dfd8ceSJim Ingham        self.build()
1442238dcc3SJonas Devlieghere        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
1452238dcc3SJonas Devlieghere            self, "Set a breakpoint here", self.main_source_file
1462238dcc3SJonas Devlieghere        )
147d3dfd8ceSJim Ingham        # First run with stop others false and see that we got that.
148d3dfd8ceSJim Ingham        thread_id = str(threading.get_ident())
149d3dfd8ceSJim Ingham
150d3dfd8ceSJim Ingham        # all-threads should set stop others to False.
151d3dfd8ceSJim Ingham        self.run_step(False, "all-threads", thread_id)
152d3dfd8ceSJim Ingham
153d3dfd8ceSJim Ingham        # this-thread should set stop others to True
154d3dfd8ceSJim Ingham        self.run_step(True, "this-thread", thread_id)
155d3dfd8ceSJim Ingham
156d3dfd8ceSJim Ingham        # The default value should be stop others:
157d3dfd8ceSJim Ingham        self.run_step(True, None, thread_id)
158d3dfd8ceSJim Ingham
159d3dfd8ceSJim Ingham        # The target.process.run-all-threads should override this:
160d3dfd8ceSJim Ingham        interp = self.dbg.GetCommandInterpreter()
161d3dfd8ceSJim Ingham        result = lldb.SBCommandReturnObject()
162d3dfd8ceSJim Ingham
163d3dfd8ceSJim Ingham        interp.HandleCommand("settings set target.process.run-all-threads true", result)
164785009e1SDave Lee        self.assertTrue(result.Succeeded(), "setting run-all-threads works.")
165d3dfd8ceSJim Ingham
166d3dfd8ceSJim Ingham        self.run_step(False, None, thread_id)
167