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