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