xref: /llvm-project/lldb/test/API/functionalities/step_scripted/TestStepScripted.py (revision d3dfd8cec44072302818c34193d898903dbaef8f)
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    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
22    def test_standard_step_out(self):
23        """Tests stepping with the scripted thread plan laying over a standard
24        thread plan for stepping out."""
25        self.build()
26        self.step_out_with_scripted_plan("Steps.StepOut")
27
28    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
29    def test_scripted_step_out(self):
30        """Tests stepping with the scripted thread plan laying over an another
31        scripted thread plan for stepping out."""
32        self.build()
33        self.step_out_with_scripted_plan("Steps.StepScripted")
34
35    def step_out_with_scripted_plan(self, name):
36        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
37                                                                            "Set a breakpoint here",
38                                                                            self.main_source_file)
39
40        frame = thread.GetFrameAtIndex(0)
41        self.assertEqual("foo", frame.GetFunctionName())
42
43        err = thread.StepUsingScriptedThreadPlan(name)
44        self.assertTrue(err.Success(), err.GetCString())
45
46        frame = thread.GetFrameAtIndex(0)
47        self.assertEqual("main", frame.GetFunctionName())
48
49
50    def test_misspelled_plan_name(self):
51        """Test that we get a useful error if we misspell the plan class name"""
52        self.build()
53        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
54                                                                            "Set a breakpoint here",
55                                                                            self.main_source_file)
56        stop_id = process.GetStopID()
57        # Pass a non-existent class for the plan class:
58        err = thread.StepUsingScriptedThreadPlan("NoSuchModule.NoSuchPlan")
59
60        # Make sure we got a good error:
61        self.assertTrue(err.Fail(), "We got a failure state")
62        msg = err.GetCString()
63        self.assertTrue("NoSuchModule.NoSuchPlan" in msg, "Mentioned missing class")
64
65        # Make sure we didn't let the process run:
66        self.assertEqual(stop_id, process.GetStopID(), "Process didn't run")
67
68    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
69    def test_checking_variable(self):
70        """Test that we can call SBValue API's from a scripted thread plan - using SBAPI's to step"""
71        self.do_test_checking_variable(False)
72
73    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
74    def test_checking_variable_cli(self):
75        """Test that we can call SBValue API's from a scripted thread plan - using cli to step"""
76        self.do_test_checking_variable(True)
77
78    def do_test_checking_variable(self, use_cli):
79        self.build()
80        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
81                                                                            "Set a breakpoint here",
82                                                                            self.main_source_file)
83
84        frame = thread.GetFrameAtIndex(0)
85        self.assertEqual("foo", frame.GetFunctionName())
86        foo_val = frame.FindVariable("foo")
87        self.assertTrue(foo_val.GetError().Success(), "Got the foo variable")
88        self.assertEqual(foo_val.GetValueAsUnsigned(), 10, "foo starts at 10")
89
90        if use_cli:
91            result = lldb.SBCommandReturnObject()
92            self.dbg.GetCommandInterpreter().HandleCommand(
93                "thread step-scripted -C Steps.StepUntil -k variable_name -v foo",
94                result)
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.assertTrue(error.Success(), "Made the args_data correctly")
102
103            err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True)
104            self.assertTrue(err.Success(), err.GetCString())
105
106        # We should not have exited:
107        self.assertEqual(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    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        interp = self.dbg.GetCommandInterpreter()
123        result = lldb.SBCommandReturnObject()
124
125        cmd = "thread step-scripted -C Steps.StepReportsStopOthers -k token -v %s"%(token)
126        if run_mode != None:
127            cmd = cmd + " --run-mode %s"%(run_mode)
128        print(cmd)
129        interp.HandleCommand(cmd, result)
130        self.assertTrue(result.Succeeded(), "Step scripted failed: %s."%(result.GetError()))
131        print(Steps.StepReportsStopOthers.stop_mode_dict)
132        value = Steps.StepReportsStopOthers.stop_mode_dict[token]
133        self.assertEqual(value, stop_others_value, "Stop others has the correct value.")
134
135    def do_test_stop_others(self):
136        self.build()
137        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
138                                                                            "Set a breakpoint here",
139                                                                            self.main_source_file)
140        # First run with stop others false and see that we got that.
141        thread_id = ""
142        if sys.version_info.major == 2:
143            thread_id = str(threading._get_ident())
144        else:
145            thread_id = str(threading.get_ident())
146
147        # all-threads should set stop others to False.
148        self.run_step(False, "all-threads", thread_id)
149
150        # this-thread should set stop others to True
151        self.run_step(True, "this-thread", thread_id)
152
153        # The default value should be stop others:
154        self.run_step(True, None, thread_id)
155
156        # The target.process.run-all-threads should override this:
157        interp = self.dbg.GetCommandInterpreter()
158        result = lldb.SBCommandReturnObject()
159
160        interp.HandleCommand("settings set target.process.run-all-threads true", result)
161        self.assertTrue(result.Succeeded, "setting run-all-threads works.")
162
163        self.run_step(False, None, thread_id)
164
165
166
167
168
169