xref: /llvm-project/lldb/test/API/functionalities/step_scripted/TestStepScripted.py (revision 0e9af88b7099fa0588661b5b8d5024b2f25f68d8)
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    mydir = TestBase.compute_mydir(__file__)
13
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def setUp(self):
17        TestBase.setUp(self)
18        self.main_source_file = lldb.SBFileSpec("main.c")
19        self.runCmd("command script import Steps.py")
20
21    def test_standard_step_out(self):
22        """Tests stepping with the scripted thread plan laying over a standard
23        thread plan for stepping out."""
24        self.build()
25        self.step_out_with_scripted_plan("Steps.StepOut")
26
27    def test_scripted_step_out(self):
28        """Tests stepping with the scripted thread plan laying over an another
29        scripted thread plan for stepping out."""
30        self.build()
31        self.step_out_with_scripted_plan("Steps.StepScripted")
32
33    def step_out_with_scripted_plan(self, name):
34        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
35                                                                            "Set a breakpoint here",
36                                                                            self.main_source_file)
37
38        frame = thread.GetFrameAtIndex(0)
39        self.assertEqual("foo", frame.GetFunctionName())
40
41        err = thread.StepUsingScriptedThreadPlan(name)
42        self.assertSuccess(err)
43
44        frame = thread.GetFrameAtIndex(0)
45        self.assertEqual("main", frame.GetFunctionName())
46
47
48    def test_misspelled_plan_name(self):
49        """Test that we get a useful error if we misspell the plan class name"""
50        self.build()
51        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
52                                                                            "Set a breakpoint here",
53                                                                            self.main_source_file)
54        stop_id = process.GetStopID()
55        # Pass a non-existent class for the plan class:
56        err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")
57
58        # Make sure we got a good error:
59        self.assertTrue(err.Fail(), "We got a failure state")
60        msg = err.GetCString()
61        self.assertIn("NoSuchModule.NoSuchPlan", msg, "Mentioned missing class")
62
63        # Make sure we didn't let the process run:
64        self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")
65
66    def test_checking_variable(self):
67        """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""
68        self.do_test_checking_variable(False)
69
70    def test_checking_variable_cli(self):
71        """Test that we can call SBValue API's from a scripted thread plan - using cli to step"""
72        self.do_test_checking_variable(True)
73
74    def do_test_checking_variable(self, use_cli):
75        self.build()
76        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
77                                                                            "Set a breakpoint here",
78                                                                            self.main_source_file)
79
80        frame = thread.GetFrameAtIndex(0)
81        self.assertEqual("foo", frame.GetFunctionName())
82        foo_val = frame.FindVariable("foo")
83        self.assertSuccess(foo_val.GetError(), "Got the foo variable")
84        self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10")
85
86        if use_cli:
87            result = lldb.SBCommandReturnObject()
88            self.dbg.GetCommandInterpreter().HandleCommand(
89                "thread step-scripted -C Steps.StepUntil -k variable_name -v foo",
90                result)
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.assertEqual(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    def test_stop_others_from_command(self):
112        """Test that the stop-others flag is set correctly by the command line.
113           Also test that the run-all-threads property overrides this."""
114        self.do_test_stop_others()
115
116    def run_step(self, stop_others_value, run_mode, token):
117        import Steps
118        interp = self.dbg.GetCommandInterpreter()
119        result = lldb.SBCommandReturnObject()
120
121        cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s"%(token)
122        if run_mode != None:
123            cmd = cmd + " --run-mode %s"%(run_mode)
124        print(cmd)
125        interp.HandleCommand(cmd, result)
126        self.assertTrue(result.Succeeded(), "Step scripted failed: %s."%(result.GetError()))
127        print(Steps.StepReportsStopOthers.stop_mode_dict)
128        value = Steps.StepReportsStopOthers.stop_mode_dict[token]
129        self.assertEqual(value, stop_others_value, "Stop others has the correct value.")
130
131    def do_test_stop_others(self):
132        self.build()
133        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
134                                                                            "Set a breakpoint here",
135                                                                            self.main_source_file)
136        # First run with stop others false and see that we got that.
137        thread_id = str(threading.get_ident())
138
139        # all-threads should set stop others to False.
140        self.run_step(False, "all-threads", thread_id)
141
142        # this-thread should set stop others to True
143        self.run_step(True, "this-thread", thread_id)
144
145        # The default value should be stop others:
146        self.run_step(True, None, thread_id)
147
148        # The target.process.run-all-threads should override this:
149        interp = self.dbg.GetCommandInterpreter()
150        result = lldb.SBCommandReturnObject()
151
152        interp.HandleCommand("settings set target.process.run-all-threads true", result)
153        self.assertTrue(result.Succeeded, "setting run-all-threads works.")
154
155        self.run_step(False, None, thread_id)
156