xref: /llvm-project/lldb/test/API/functionalities/step_scripted/TestStepScripted.py (revision 6cb45aea92dc87974a0064c182600228c6e94329)
1"""
2Tests stepping with scripted thread plans.
3"""
4import threading
5import lldb
6import lldbsuite.test.lldbutil as lldbutil
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9
10
11class StepScriptedTestCase(TestBase):
12    NO_DEBUG_INFO_TESTCASE = True
13
14    def setUp(self):
15        TestBase.setUp(self)
16        self.main_source_file = lldb.SBFileSpec("main.c")
17        self.runCmd("command script import Steps.py")
18
19    def test_standard_step_out(self):
20        """Tests stepping with the scripted thread plan laying over a standard
21        thread plan for stepping out."""
22        self.build()
23        self.step_out_with_scripted_plan("Steps.StepOut")
24
25    def test_scripted_step_out(self):
26        """Tests stepping with the scripted thread plan laying over an another
27        scripted thread plan for stepping out."""
28        self.build()
29        self.step_out_with_scripted_plan("Steps.StepScripted")
30
31    def step_out_with_scripted_plan(self, name):
32        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
33            self, "Set a breakpoint here", self.main_source_file
34        )
35
36        frame = thread.GetFrameAtIndex(0)
37        self.assertEqual("foo", frame.GetFunctionName())
38
39        err = thread.StepUsingScriptedThreadPlan(name)
40        self.assertSuccess(err)
41
42        frame = thread.GetFrameAtIndex(0)
43        self.assertEqual("main", frame.GetFunctionName())
44        stop_desc = thread.GetStopDescription(1000)
45        self.assertIn("Stepping out from", stop_desc, "Got right description")
46
47    def test_misspelled_plan_name(self):
48        """Test that we get a useful error if we misspell the plan class name"""
49        self.build()
50        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
51            self, "Set a breakpoint here", self.main_source_file
52        )
53        stop_id = process.GetStopID()
54        # Pass a non-existent class for the plan class:
55        err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")
56
57        # Make sure we got a good error:
58        self.assertTrue(err.Fail(), "We got a failure state")
59        msg = err.GetCString()
60        self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class")
61
62        # Make sure we didn't let the process run:
63        self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")
64
65    def test_checking_variable(self):
66        """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""
67        self.do_test_checking_variable(False)
68
69    def test_checking_variable_cli(self):
70        """Test that we can call SBValue API's from a scripted thread plan - using cli to step"""
71        self.do_test_checking_variable(True)
72
73    def do_test_checking_variable(self, use_cli):
74        self.build()
75        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
76            self, "Set a breakpoint here", self.main_source_file
77        )
78
79        frame = thread.GetFrameAtIndex(0)
80        self.assertEqual("foo", frame.GetFunctionName())
81        foo_val = frame.FindVariable("foo")
82        self.assertSuccess(foo_val.GetError(), "Got the foo variable")
83        self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10")
84
85        if use_cli:
86            result = lldb.SBCommandReturnObject()
87            self.dbg.GetCommandInterpreter().HandleCommand(
88                "thread step-scripted -C Steps.StepUntil -k variable_name -v foo",
89                result,
90            )
91            self.assertTrue(result.Succeeded())
92        else:
93            args_data = lldb.SBStructuredData()
94            data = lldb.SBStream()
95            data.Print('{"variable_name" : "foo"}')
96            error = args_data.SetFromJSON(data)
97            self.assertSuccess(error, "Made the args_data correctly")
98
99            err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True)
100            self.assertSuccess(err)
101
102        # We should not have exited:
103        self.assertState(process.GetState(), lldb.eStateStopped, "We are stopped")
104
105        # We should still be in foo:
106        self.assertEqual("foo", frame.GetFunctionName())
107
108        # And foo should have changed:
109        self.assertTrue(foo_val.GetValueDidChange(), "Foo changed")
110
111        # And we should have a reasonable stop description:
112        desc = thread.GetStopDescription(1000)
113        self.assertIn("Stepped until foo changed", desc, "Got right stop description")
114
115    def test_stop_others_from_command(self):
116        """Test that the stop-others flag is set correctly by the command line.
117        Also test that the run-all-threads property overrides this."""
118        self.do_test_stop_others()
119
120    def run_step(self, stop_others_value, run_mode, token):
121        import Steps
122
123        interp = self.dbg.GetCommandInterpreter()
124        result = lldb.SBCommandReturnObject()
125
126        cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s" % (
127            token
128        )
129        if run_mode is not None:
130            cmd = cmd + " --run-mode %s" % (run_mode)
131        if self.TraceOn():
132            print(cmd)
133        interp.HandleCommand(cmd, result)
134        self.assertTrue(
135            result.Succeeded(), "Step scripted failed: %s." % (result.GetError())
136        )
137        if self.TraceOn():
138            print(Steps.StepReportsStopOthers.stop_mode_dict)
139        value = Steps.StepReportsStopOthers.stop_mode_dict[token]
140        self.assertEqual(value, stop_others_value, "Stop others has the correct value.")
141
142    def do_test_stop_others(self):
143        self.build()
144        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
145            self, "Set a breakpoint here", self.main_source_file
146        )
147        # First run with stop others false and see that we got that.
148        thread_id = str(threading.get_ident())
149
150        # all-threads should set stop others to False.
151        self.run_step(False, "all-threads", thread_id)
152
153        # this-thread should set stop others to True
154        self.run_step(True, "this-thread", thread_id)
155
156        # The default value should be stop others:
157        self.run_step(True, None, thread_id)
158
159        # The target.process.run-all-threads should override this:
160        interp = self.dbg.GetCommandInterpreter()
161        result = lldb.SBCommandReturnObject()
162
163        interp.HandleCommand("settings set target.process.run-all-threads true", result)
164        self.assertTrue(result.Succeeded(), "setting run-all-threads works.")
165
166        self.run_step(False, None, thread_id)
167