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    mydir = TestBase.compute_mydir(__file__)
161893065dSJim Ingham
171893065dSJim Ingham    NO_DEBUG_INFO_TESTCASE = True
181893065dSJim Ingham
192d658c56SJim Ingham    @skipIfWindows
201893065dSJim Ingham    def test_thread_plan_actions(self):
211893065dSJim Ingham        self.build()
221893065dSJim Ingham        self.main_source_file = lldb.SBFileSpec("main.c")
231893065dSJim Ingham        self.thread_plan_test()
241893065dSJim Ingham
251893065dSJim Ingham    def check_list_output(self, command, active_plans = [], completed_plans = [], discarded_plans = []):
261893065dSJim Ingham        # Check the "thread plan list" output against a list of active & completed and discarded plans.
271893065dSJim Ingham        # If all three check arrays are empty, that means the command is expected to fail.
281893065dSJim Ingham
291893065dSJim Ingham        interp = self.dbg.GetCommandInterpreter()
301893065dSJim Ingham        result = lldb.SBCommandReturnObject()
311893065dSJim Ingham
321893065dSJim Ingham        num_active = len(active_plans)
331893065dSJim Ingham        num_completed = len(completed_plans)
341893065dSJim Ingham        num_discarded = len(discarded_plans)
351893065dSJim Ingham
361893065dSJim Ingham        interp.HandleCommand(command, result)
371893065dSJim Ingham        print("Command: %s"%(command))
381893065dSJim Ingham        print(result.GetOutput())
391893065dSJim Ingham
401893065dSJim Ingham        if num_active == 0 and num_completed == 0 and num_discarded == 0:
411893065dSJim Ingham            self.assertFalse(result.Succeeded(), "command: '%s' succeeded when it should have failed: '%s'"%
421893065dSJim Ingham                             (command, result.GetError()))
431893065dSJim Ingham            return
441893065dSJim Ingham
451893065dSJim Ingham        self.assertTrue(result.Succeeded(), "command: '%s' failed: '%s'"%(command, result.GetError()))
461893065dSJim Ingham        result_arr = result.GetOutput().splitlines()
471893065dSJim Ingham        num_results = len(result_arr)
481893065dSJim Ingham
491893065dSJim Ingham        # Match the expected number of elements.
501893065dSJim Ingham        # Adjust the count for the number of header lines we aren't matching:
511893065dSJim Ingham        fudge = 0
521893065dSJim Ingham
531893065dSJim Ingham        if num_completed == 0 and num_discarded == 0:
541893065dSJim Ingham            # The fudge is 3: Thread header, Active Plan header and base plan
551893065dSJim Ingham            fudge = 3
561893065dSJim Ingham        elif num_completed == 0 or num_discarded == 0:
571893065dSJim Ingham            # The fudge is 4: The above plus either the Completed or Discarded Plan header:
581893065dSJim Ingham            fudge = 4
591893065dSJim Ingham        else:
601893065dSJim Ingham            # The fudge is 5 since we have both headers:
611893065dSJim Ingham            fudge = 5
621893065dSJim Ingham
631893065dSJim Ingham        self.assertEqual(num_results, num_active + num_completed + num_discarded + fudge,
64*48ba745eSJim Ingham                             "Too many elements in match arrays for: \n%s\n"%result.GetOutput())
651893065dSJim Ingham
661893065dSJim Ingham        # Now iterate through the results array and pick out the results.
671893065dSJim Ingham        result_idx = 0
681893065dSJim Ingham        self.assertIn("thread #", result_arr[result_idx], "Found thread header") ; result_idx += 1
691893065dSJim Ingham        self.assertIn("Active plan stack", result_arr[result_idx], "Found active header") ; result_idx += 1
701893065dSJim Ingham        self.assertIn("Element 0: Base thread plan", result_arr[result_idx], "Found base plan") ; result_idx += 1
711893065dSJim Ingham
721893065dSJim Ingham        for text in active_plans:
731893065dSJim Ingham            self.assertFalse("Completed plan stack" in result_arr[result_idx], "Found Completed header too early.")
741893065dSJim Ingham            self.assertIn(text, result_arr[result_idx], "Didn't find active plan: %s"%(text)) ; result_idx += 1
751893065dSJim Ingham
761893065dSJim Ingham        if len(completed_plans) > 0:
771893065dSJim Ingham            self.assertIn("Completed plan stack:", result_arr[result_idx], "Found completed plan stack header") ; result_idx += 1
781893065dSJim Ingham            for text in completed_plans:
791893065dSJim Ingham                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
801893065dSJim Ingham
811893065dSJim Ingham        if len(discarded_plans) > 0:
821893065dSJim Ingham            self.assertIn("Discarded plan stack:", result_arr[result_idx], "Found discarded plan stack header") ; result_idx += 1
831893065dSJim Ingham            for text in discarded_plans:
841893065dSJim Ingham                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
851893065dSJim Ingham
861893065dSJim Ingham
871893065dSJim Ingham    def thread_plan_test(self):
881893065dSJim Ingham        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
891893065dSJim Ingham                                   "Set a breakpoint here", self.main_source_file)
901893065dSJim Ingham
911893065dSJim Ingham        # Now set a breakpoint in call_me and step over.  We should have
921893065dSJim Ingham        # two public thread plans
931893065dSJim Ingham        call_me_bkpt = target.BreakpointCreateBySourceRegex("Set another here", self.main_source_file)
941893065dSJim Ingham        self.assertTrue(call_me_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
951893065dSJim Ingham        thread.StepOver()
961893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
971893065dSJim Ingham        self.assertEqual(len(threads), 1, "Hit my breakpoint while stepping over")
981893065dSJim Ingham
991893065dSJim Ingham        current_id = threads[0].GetIndexID()
1001893065dSJim Ingham        current_tid = threads[0].GetThreadID()
1011893065dSJim Ingham        # Run thread plan list without the -i flag:
1021893065dSJim Ingham        command = "thread plan list %d"%(current_id)
1031893065dSJim Ingham        self.check_list_output (command, ["Stepping over line main.c"], [])
1041893065dSJim Ingham
1051893065dSJim Ingham        # Run thread plan list with the -i flag:
1061893065dSJim Ingham        command = "thread plan list -i %d"%(current_id)
1071893065dSJim Ingham        self.check_list_output(command, ["Stepping over line main.c", "Stepping out from"])
1081893065dSJim Ingham
1091893065dSJim Ingham        # Run thread plan list providing TID, output should be the same:
1101893065dSJim Ingham        command = "thread plan list -t %d"%(current_tid)
1111893065dSJim Ingham        self.check_list_output(command, ["Stepping over line main.c"])
1121893065dSJim Ingham
1131893065dSJim Ingham        # Provide both index & tid, and make sure we only print once:
1141893065dSJim Ingham        command = "thread plan list -t %d %d"%(current_tid, current_id)
1151893065dSJim Ingham        self.check_list_output(command, ["Stepping over line main.c"])
1161893065dSJim Ingham
1171893065dSJim Ingham        # Try a fake TID, and make sure that fails:
1181893065dSJim Ingham        fake_tid = 0
1191893065dSJim Ingham        for i in range(100, 10000, 100):
1201893065dSJim Ingham            fake_tid = current_tid + i
1211893065dSJim Ingham            thread = process.GetThreadByID(fake_tid)
1221893065dSJim Ingham            if not thread:
1231893065dSJim Ingham                break
1241893065dSJim Ingham
1251893065dSJim Ingham        command = "thread plan list -t %d"%(fake_tid)
1261893065dSJim Ingham        self.check_list_output(command)
1271893065dSJim Ingham
1281893065dSJim Ingham        # Now continue, and make sure we printed the completed plan:
1291893065dSJim Ingham        process.Continue()
1301893065dSJim Ingham        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
1311893065dSJim Ingham        self.assertEqual(len(threads), 1, "One thread completed a step")
1321893065dSJim Ingham
1331893065dSJim Ingham        # Run thread plan list - there aren't any private plans at this point:
1341893065dSJim Ingham        command = "thread plan list %d"%(current_id)
1351893065dSJim Ingham        self.check_list_output(command, [], ["Stepping over line main.c"])
1361893065dSJim Ingham
1371893065dSJim Ingham        # Set another breakpoint that we can run to, to try deleting thread plans.
1381893065dSJim Ingham        second_step_bkpt = target.BreakpointCreateBySourceRegex("Run here to step over again",
1391893065dSJim Ingham                                                                self.main_source_file)
1401893065dSJim Ingham        self.assertTrue(second_step_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
1411893065dSJim Ingham        final_bkpt = target.BreakpointCreateBySourceRegex("Make sure we get here on last continue",
1421893065dSJim Ingham                                                          self.main_source_file)
1431893065dSJim Ingham        self.assertTrue(final_bkpt.GetNumLocations() > 0, "Set the breakpoint successfully")
1441893065dSJim Ingham
1451893065dSJim Ingham        threads = lldbutil.continue_to_breakpoint(process, second_step_bkpt)
1461893065dSJim Ingham        self.assertEqual(len(threads), 1, "Hit the second step breakpoint")
1471893065dSJim Ingham
1481893065dSJim Ingham        threads[0].StepOver()
1491893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, call_me_bkpt)
1501893065dSJim Ingham
1511893065dSJim Ingham        result = lldb.SBCommandReturnObject()
1521893065dSJim Ingham        interp = self.dbg.GetCommandInterpreter()
1531893065dSJim Ingham        interp.HandleCommand("thread plan discard 1", result)
1541893065dSJim Ingham        self.assertTrue(result.Succeeded(), "Deleted the step over plan: %s"%(result.GetOutput()))
1551893065dSJim Ingham
1561893065dSJim Ingham        # Make sure the plan gets listed in the discarded plans:
1571893065dSJim Ingham        command = "thread plan list %d"%(current_id)
1581893065dSJim Ingham        self.check_list_output(command, [], [], ["Stepping over line main.c:"])
1591893065dSJim Ingham
1601893065dSJim Ingham        process.Continue()
1611893065dSJim Ingham        threads = lldbutil.get_threads_stopped_at_breakpoint(process, final_bkpt)
1621893065dSJim Ingham        self.assertEqual(len(threads), 1, "Ran to final breakpoint")
1631893065dSJim Ingham        threads = lldbutil.get_stopped_threads(process, lldb.eStopReasonPlanComplete)
1641893065dSJim Ingham        self.assertEqual(len(threads), 0, "Did NOT complete the step over plan")
1651893065dSJim Ingham
166