11893065dSJim Ingham"""
21893065dSJim InghamTest that thread plan listing, and deleting works.
31893065dSJim Ingham"""
41893065dSJim Ingham
51893065dSJim Ingham
61893065dSJim Ingham
71893065dSJim Inghamimport lldb
82d658c56SJim Inghamfrom lldbsuite.test.decorators import *
91893065dSJim Inghamimport lldbsuite.test.lldbutil as lldbutil
101893065dSJim Inghamfrom lldbsuite.test.lldbtest import *
111893065dSJim Ingham
121893065dSJim Ingham
131893065dSJim Inghamclass TestThreadPlanCommands(TestBase):
141893065dSJim Ingham
151893065dSJim Ingham    NO_DEBUG_INFO_TESTCASE = True
161893065dSJim Ingham
172d658c56SJim Ingham    @skipIfWindows
181893065dSJim Ingham    def test_thread_plan_actions(self):
191893065dSJim Ingham        self.build()
201893065dSJim Ingham        self.main_source_file = lldb.SBFileSpec("main.c")
211893065dSJim Ingham        self.thread_plan_test()
221893065dSJim Ingham
231893065dSJim Ingham    def check_list_output(self, command, active_plans = [], completed_plans = [], discarded_plans = []):
241893065dSJim Ingham        # Check the "thread plan list" output against a list of active & completed and discarded plans.
251893065dSJim Ingham        # If all three check arrays are empty, that means the command is expected to fail.
261893065dSJim Ingham
271893065dSJim Ingham        interp = self.dbg.GetCommandInterpreter()
281893065dSJim Ingham        result = lldb.SBCommandReturnObject()
291893065dSJim Ingham
301893065dSJim Ingham        num_active = len(active_plans)
311893065dSJim Ingham        num_completed = len(completed_plans)
321893065dSJim Ingham        num_discarded = len(discarded_plans)
331893065dSJim Ingham
341893065dSJim Ingham        interp.HandleCommand(command, result)
351893065dSJim Ingham        print("Command: %s"%(command))
361893065dSJim Ingham        print(result.GetOutput())
371893065dSJim Ingham
381893065dSJim Ingham        if num_active == 0 and num_completed == 0 and num_discarded == 0:
391893065dSJim Ingham            self.assertFalse(result.Succeeded(), "command: '%s' succeeded when it should have failed: '%s'"%
401893065dSJim Ingham                             (command, result.GetError()))
411893065dSJim Ingham            return
421893065dSJim Ingham
431893065dSJim Ingham        self.assertTrue(result.Succeeded(), "command: '%s' failed: '%s'"%(command, result.GetError()))
441893065dSJim Ingham        result_arr = result.GetOutput().splitlines()
451893065dSJim Ingham        num_results = len(result_arr)
461893065dSJim Ingham
471893065dSJim Ingham        # Now iterate through the results array and pick out the results.
481893065dSJim Ingham        result_idx = 0
491893065dSJim Ingham        self.assertIn("thread #", result_arr[result_idx], "Found thread header") ; result_idx += 1
501893065dSJim Ingham        self.assertIn("Active plan stack", result_arr[result_idx], "Found active header") ; result_idx += 1
511893065dSJim Ingham        self.assertIn("Element 0: Base thread plan", result_arr[result_idx], "Found base plan") ; result_idx += 1
521893065dSJim Ingham
531893065dSJim Ingham        for text in active_plans:
541893065dSJim Ingham            self.assertIn(text, result_arr[result_idx], "Didn't find active plan: %s"%(text)) ; result_idx += 1
551893065dSJim Ingham
56*aa4b37b2SJim Ingham
571893065dSJim Ingham        if len(completed_plans) > 0:
58*aa4b37b2SJim Ingham            # First consume any remaining active plans:
59*aa4b37b2SJim Ingham            while not "Completed plan stack:" in result_arr[result_idx]:
60*aa4b37b2SJim Ingham                result_idx += 1
61*aa4b37b2SJim Ingham                if result_idx == num_results:
62*aa4b37b2SJim Ingham                    self.fail("There should have been completed plans, but I never saw the completed stack header")
63*aa4b37b2SJim Ingham            # We are at the Completed header, skip it:
64*aa4b37b2SJim Ingham            result_idx += 1
651893065dSJim Ingham            for text in completed_plans:
661893065dSJim Ingham                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
671893065dSJim Ingham
681893065dSJim Ingham        if len(discarded_plans) > 0:
69*aa4b37b2SJim Ingham            # First consume any remaining completed plans:
70*aa4b37b2SJim Ingham            while not "Discarded plan stack:" in result_arr[result_idx]:
71*aa4b37b2SJim Ingham                result_idx += 1
72*aa4b37b2SJim Ingham                if result_idx == num_results:
73*aa4b37b2SJim Ingham                    self.fail("There should have been discarded plans, but I never saw the discarded stack header")
74*aa4b37b2SJim Ingham
75*aa4b37b2SJim Ingham            # We are at the Discarded header, skip it:
76*aa4b37b2SJim Ingham            result_idx += 1
771893065dSJim Ingham            for text in discarded_plans:
78*aa4b37b2SJim Ingham                self.assertIn(text, result_arr[result_idx], "Didn't find discarded plan: %s"%(text)) ; result_idx += 1
791893065dSJim Ingham
801893065dSJim Ingham
811893065dSJim Ingham    def thread_plan_test(self):
821893065dSJim Ingham        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
831893065dSJim Ingham                                   "Set a breakpoint here", self.main_source_file)
841893065dSJim Ingham
85*aa4b37b2SJim Ingham        # We need to have an internal plan so we can test listing one.
86*aa4b37b2SJim Ingham        # The most consistent way to do that is to use a scripted thread plan
87*aa4b37b2SJim Ingham        # that uses a sub-plan.  Source that in now.
88*aa4b37b2SJim Ingham        source_path = os.path.join(self.getSourceDir(), "wrap_step_over.py")
89*aa4b37b2SJim Ingham        self.runCmd("command script import '%s'"%(source_path))
90*aa4b37b2SJim Ingham
91*aa4b37b2SJim Ingham        # Now set a breakpoint that we will hit by running our scripted step.
921893065dSJim Ingham        call_me_bkpt = target.BreakpointCreateBySourceRegex("Set another here", self.main_source_file)
931893065dSJim Ingham        self.assertTrue(call_me_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
94*aa4b37b2SJim Ingham        thread.StepUsingScriptedThreadPlan("wrap_step_over.WrapStepOver")
951893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
961893065dSJim Ingham        self.assertEqual(len(threads), 1, "Hit my breakpoint while stepping over")
971893065dSJim Ingham
981893065dSJim Ingham        current_id = threads[0].GetIndexID()
991893065dSJim Ingham        current_tid = threads[0].GetThreadID()
1001893065dSJim Ingham        # Run thread plan list without the -i flag:
1011893065dSJim Ingham        command = "thread plan list %d"%(current_id)
102*aa4b37b2SJim Ingham        self.check_list_output (command, ["wrap_step_over.WrapStepOver"], [])
1031893065dSJim Ingham
1041893065dSJim Ingham        # Run thread plan list with the -i flag:
1051893065dSJim Ingham        command = "thread plan list -i %d"%(current_id)
106*aa4b37b2SJim Ingham        self.check_list_output(command, ["WrapStepOver", "Stepping over line main.c"])
1071893065dSJim Ingham
1081893065dSJim Ingham        # Run thread plan list providing TID, output should be the same:
1091893065dSJim Ingham        command = "thread plan list -t %d"%(current_tid)
110*aa4b37b2SJim Ingham        self.check_list_output(command, ["wrap_step_over.WrapStepOver"])
1111893065dSJim Ingham
1121893065dSJim Ingham        # Provide both index & tid, and make sure we only print once:
1131893065dSJim Ingham        command = "thread plan list -t %d %d"%(current_tid, current_id)
114*aa4b37b2SJim Ingham        self.check_list_output(command, ["wrap_step_over.WrapStepOver"])
1151893065dSJim Ingham
1161893065dSJim Ingham        # Try a fake TID, and make sure that fails:
1171893065dSJim Ingham        fake_tid = 0
1181893065dSJim Ingham        for i in range(100, 10000, 100):
1191893065dSJim Ingham            fake_tid = current_tid + i
1201893065dSJim Ingham            thread = process.GetThreadByID(fake_tid)
1211893065dSJim Ingham            if not thread:
1221893065dSJim Ingham                break
1231893065dSJim Ingham
1241893065dSJim Ingham        command = "thread plan list -t %d"%(fake_tid)
1251893065dSJim Ingham        self.check_list_output(command)
1261893065dSJim Ingham
1271893065dSJim Ingham        # Now continue, and make sure we printed the completed plan:
1281893065dSJim Ingham        process.Continue()
1291893065dSJim Ingham        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
1301893065dSJim Ingham        self.assertEqual(len(threads), 1, "One thread completed a step")
1311893065dSJim Ingham
1321893065dSJim Ingham        # Run thread plan list - there aren't any private plans at this point:
1331893065dSJim Ingham        command = "thread plan list %d"%(current_id)
134*aa4b37b2SJim Ingham        self.check_list_output(command, [], ["wrap_step_over.WrapStepOver"])
1351893065dSJim Ingham
1361893065dSJim Ingham        # Set another breakpoint that we can run to, to try deleting thread plans.
1371893065dSJim Ingham        second_step_bkpt = target.BreakpointCreateBySourceRegex("Run here to step over again",
1381893065dSJim Ingham                                                                self.main_source_file)
1391893065dSJim Ingham        self.assertTrue(second_step_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
1401893065dSJim Ingham        final_bkpt = target.BreakpointCreateBySourceRegex("Make sure we get here on last continue",
1411893065dSJim Ingham                                                          self.main_source_file)
1421893065dSJim Ingham        self.assertTrue(final_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
1431893065dSJim Ingham
1441893065dSJim Ingham        threads = lldbutil.continue_to_breakpoint(process, second_step_bkpt)
1451893065dSJim Ingham        self.assertEqual(len(threads), 1, "Hit the second step breakpoint")
1461893065dSJim Ingham
1471893065dSJim Ingham        threads[0].StepOver()
1481893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
1491893065dSJim Ingham
1501893065dSJim Ingham        result = lldb.SBCommandReturnObject()
1511893065dSJim Ingham        interp = self.dbg.GetCommandInterpreter()
1521893065dSJim Ingham        interp.HandleCommand("thread plan discard 1", result)
1531893065dSJim Ingham        self.assertTrue(result.Succeeded(), "Deleted the step over plan: %s"%(result.GetOutput()))
1541893065dSJim Ingham
1551893065dSJim Ingham        # Make sure the plan gets listed in the discarded plans:
1561893065dSJim Ingham        command = "thread plan list %d"%(current_id)
1571893065dSJim Ingham        self.check_list_output(command, [], [], ["Stepping over line main.c:"])
1581893065dSJim Ingham
1591893065dSJim Ingham        process.Continue()
1601893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, final_bkpt)
1611893065dSJim Ingham        self.assertEqual(len(threads), 1, "Ran to final breakpoint")
1621893065dSJim Ingham        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
1631893065dSJim Ingham        self.assertEqual(len(threads), 0, "Did NOT complete the step over plan")
1641893065dSJim Ingham
165