xref: /llvm-project/lldb/test/API/functionalities/thread_plan/TestThreadPlanCommands.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test that thread plan listing, and deleting works.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9import lldbsuite.test.lldbutil as lldbutil
10from lldbsuite.test.lldbtest import *
11
12
13class TestThreadPlanCommands(TestBase):
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipIfWindows
18    def test_thread_plan_actions(self):
19        self.build()
20        self.main_source_file = lldb.SBFileSpec("main.c")
21        self.thread_plan_test()
22
23    def check_list_output(self, command, active_plans = [], completed_plans = [], discarded_plans = []):
24        # Check the "thread plan list" output against a list of active & completed and discarded plans.
25        # If all three check arrays are empty, that means the command is expected to fail.
26
27        interp = self.dbg.GetCommandInterpreter()
28        result = lldb.SBCommandReturnObject()
29
30        num_active = len(active_plans)
31        num_completed = len(completed_plans)
32        num_discarded = len(discarded_plans)
33
34        interp.HandleCommand(command, result)
35        print("Command: %s"%(command))
36        print(result.GetOutput())
37
38        if num_active == 0 and num_completed == 0 and num_discarded == 0:
39            self.assertFalse(result.Succeeded(), "command: '%s' succeeded when it should have failed: '%s'"%
40                             (command, result.GetError()))
41            return
42
43        self.assertTrue(result.Succeeded(), "command: '%s' failed: '%s'"%(command, result.GetError()))
44        result_arr = result.GetOutput().splitlines()
45        num_results = len(result_arr)
46
47        # Now iterate through the results array and pick out the results.
48        result_idx = 0
49        self.assertIn("thread #", result_arr[result_idx], "Found thread header") ; result_idx += 1
50        self.assertIn("Active plan stack", result_arr[result_idx], "Found active header") ; result_idx += 1
51        self.assertIn("Element 0: Base thread plan", result_arr[result_idx], "Found base plan") ; result_idx += 1
52
53        for text in active_plans:
54            self.assertIn(text, result_arr[result_idx], "Didn't find active plan: %s"%(text)) ; result_idx += 1
55
56
57        if len(completed_plans) > 0:
58            # First consume any remaining active plans:
59            while not "Completed plan stack:" in result_arr[result_idx]:
60                result_idx += 1
61                if result_idx == num_results:
62                    self.fail("There should have been completed plans, but I never saw the completed stack header")
63            # We are at the Completed header, skip it:
64            result_idx += 1
65            for text in completed_plans:
66                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
67
68        if len(discarded_plans) > 0:
69            # First consume any remaining completed plans:
70            while not "Discarded plan stack:" in result_arr[result_idx]:
71                result_idx += 1
72                if result_idx == num_results:
73                    self.fail("There should have been discarded plans, but I never saw the discarded stack header")
74
75            # We are at the Discarded header, skip it:
76            result_idx += 1
77            for text in discarded_plans:
78                self.assertIn(text, result_arr[result_idx], "Didn't find discarded plan: %s"%(text)) ; result_idx += 1
79
80
81    def thread_plan_test(self):
82        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
83                                   "Set a breakpoint here", self.main_source_file)
84
85        # We need to have an internal plan so we can test listing one.
86        # The most consistent way to do that is to use a scripted thread plan
87        # that uses a sub-plan.  Source that in now.
88        source_path = os.path.join(self.getSourceDir(), "wrap_step_over.py")
89        self.runCmd("command script import '%s'"%(source_path))
90
91        # Now set a breakpoint that we will hit by running our scripted step.
92        call_me_bkpt = target.BreakpointCreateBySourceRegex("Set another here", self.main_source_file)
93        self.assertTrue(call_me_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
94        thread.StepUsingScriptedThreadPlan("wrap_step_over.WrapStepOver")
95        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
96        self.assertEqual(len(threads), 1, "Hit my breakpoint while stepping over")
97
98        current_id = threads[0].GetIndexID()
99        current_tid = threads[0].GetThreadID()
100        # Run thread plan list without the -i flag:
101        command = "thread plan list %d"%(current_id)
102        self.check_list_output (command, ["wrap_step_over.WrapStepOver"], [])
103
104        # Run thread plan list with the -i flag:
105        command = "thread plan list -i %d"%(current_id)
106        self.check_list_output(command, ["WrapStepOver", "Stepping over line main.c"])
107
108        # Run thread plan list providing TID, output should be the same:
109        command = "thread plan list -t %d"%(current_tid)
110        self.check_list_output(command, ["wrap_step_over.WrapStepOver"])
111
112        # Provide both index & tid, and make sure we only print once:
113        command = "thread plan list -t %d %d"%(current_tid, current_id)
114        self.check_list_output(command, ["wrap_step_over.WrapStepOver"])
115
116        # Try a fake TID, and make sure that fails:
117        fake_tid = 0
118        for i in range(100, 10000, 100):
119            fake_tid = current_tid + i
120            thread = process.GetThreadByID(fake_tid)
121            if not thread:
122                break
123
124        command = "thread plan list -t %d"%(fake_tid)
125        self.check_list_output(command)
126
127        # Now continue, and make sure we printed the completed plan:
128        process.Continue()
129        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
130        self.assertEqual(len(threads), 1, "One thread completed a step")
131
132        # Run thread plan list - there aren't any private plans at this point:
133        command = "thread plan list %d"%(current_id)
134        self.check_list_output(command, [], ["wrap_step_over.WrapStepOver"])
135
136        # Set another breakpoint that we can run to, to try deleting thread plans.
137        second_step_bkpt = target.BreakpointCreateBySourceRegex("Run here to step over again",
138                                                                self.main_source_file)
139        self.assertTrue(second_step_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
140        final_bkpt = target.BreakpointCreateBySourceRegex("Make sure we get here on last continue",
141                                                          self.main_source_file)
142        self.assertTrue(final_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
143
144        threads = lldbutil.continue_to_breakpoint(process, second_step_bkpt)
145        self.assertEqual(len(threads), 1, "Hit the second step breakpoint")
146
147        threads[0].StepOver()
148        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
149
150        result = lldb.SBCommandReturnObject()
151        interp = self.dbg.GetCommandInterpreter()
152        interp.HandleCommand("thread plan discard 1", result)
153        self.assertTrue(result.Succeeded(), "Deleted the step over plan: %s"%(result.GetOutput()))
154
155        # Make sure the plan gets listed in the discarded plans:
156        command = "thread plan list %d"%(current_id)
157        self.check_list_output(command, [], [], ["Stepping over line main.c:"])
158
159        process.Continue()
160        threads = lldbutil.get_threads_stopped_at_breakpoint(process, final_bkpt)
161        self.assertEqual(len(threads), 1, "Ran to final breakpoint")
162        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
163        self.assertEqual(len(threads), 0, "Did NOT complete the step over plan")
164
165