xref: /llvm-project/lldb/test/API/functionalities/step_scripted/TestStepScripted.py (revision 99451b4453688a94c6014cac233d371ab4cc342d)
1"""
2Tests stepping with scripted thread plans.
3"""
4
5import lldb
6import lldbsuite.test.lldbutil as lldbutil
7from lldbsuite.test.lldbtest import *
8
9class StepScriptedTestCase(TestBase):
10
11    mydir = TestBase.compute_mydir(__file__)
12
13    NO_DEBUG_INFO_TESTCASE = True
14
15    def setUp(self):
16        TestBase.setUp(self)
17        self.main_source_file = lldb.SBFileSpec("main.c")
18        self.runCmd("command script import Steps.py")
19
20    def test_standard_step_out(self):
21        """Tests stepping with the scripted thread plan laying over a standard
22        thread plan for stepping out."""
23        self.build()
24        self.step_out_with_scripted_plan("Steps.StepOut")
25
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(self,
34                                                                            "Set a breakpoint here",
35                                                                            self.main_source_file)
36
37        frame = thread.GetFrameAtIndex(0)
38        self.assertEqual("foo", frame.GetFunctionName())
39
40        err = thread.StepUsingScriptedThreadPlan(name)
41        self.assertTrue(err.Success(), err.GetCString())
42
43        frame = thread.GetFrameAtIndex(0)
44        self.assertEqual("main", frame.GetFunctionName())
45
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(self,
51                                                                            "Set a breakpoint here",
52                                                                            self.main_source_file)
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.assertTrue("NoSuchModule.NoSuchPlan" in 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(self,
76                                                                            "Set a breakpoint here",
77                                                                            self.main_source_file)
78
79        frame = thread.GetFrameAtIndex(0)
80        self.assertEqual("foo", frame.GetFunctionName())
81        foo_val = frame.FindVariable("foo")
82        self.assertTrue(foo_val.GetError().Success(), "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            self.assertTrue(result.Succeeded())
91        else:
92            args_data = lldb.SBStructuredData()
93            data = lldb.SBStream()
94            data.Print('{"variable_name" : "foo"}')
95            error = args_data.SetFromJSON(data)
96            self.assertTrue(error.Success(), "Made the args_data correctly")
97
98            err = thread.StepUsingScriptedThreadPlan("Steps.StepUntil", args_data, True)
99            self.assertTrue(err.Success(), err.GetCString())
100
101        # We should not have exited:
102        self.assertEqual(process.GetState(), lldb.eStateStopped, "We are stopped")
103
104        # We should still be in foo:
105        self.assertEqual("foo", frame.GetFunctionName())
106
107        # And foo should have changed:
108        self.assertTrue(foo_val.GetValueDidChange(), "Foo changed")
109