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