1""" 2Test that thread plan listing, and deleting works. 3""" 4 5 6 7import lldb 8import lldbsuite.test.lldbutil as lldbutil 9from lldbsuite.test.lldbtest import * 10 11 12class TestThreadPlanCommands(TestBase): 13 14 mydir = TestBase.compute_mydir(__file__) 15 16 NO_DEBUG_INFO_TESTCASE = True 17 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 if self.TraceOn(): 36 print("Command: %s"%(command)) 37 print(result.GetOutput()) 38 39 if num_active == 0 and num_completed == 0 and num_discarded == 0: 40 self.assertFalse(result.Succeeded(), "command: '%s' succeeded when it should have failed: '%s'"% 41 (command, result.GetError())) 42 return 43 44 self.assertTrue(result.Succeeded(), "command: '%s' failed: '%s'"%(command, result.GetError())) 45 result_arr = result.GetOutput().splitlines() 46 num_results = len(result_arr) 47 48 # Match the expected number of elements. 49 # Adjust the count for the number of header lines we aren't matching: 50 fudge = 0 51 52 if num_completed == 0 and num_discarded == 0: 53 # The fudge is 3: Thread header, Active Plan header and base plan 54 fudge = 3 55 elif num_completed == 0 or num_discarded == 0: 56 # The fudge is 4: The above plus either the Completed or Discarded Plan header: 57 fudge = 4 58 else: 59 # The fudge is 5 since we have both headers: 60 fudge = 5 61 62 self.assertEqual(num_results, num_active + num_completed + num_discarded + fudge, 63 "Too many elements in match arrays") 64 65 # Now iterate through the results array and pick out the results. 66 result_idx = 0 67 self.assertIn("thread #", result_arr[result_idx], "Found thread header") ; result_idx += 1 68 self.assertIn("Active plan stack", result_arr[result_idx], "Found active header") ; result_idx += 1 69 self.assertIn("Element 0: Base thread plan", result_arr[result_idx], "Found base plan") ; result_idx += 1 70 71 for text in active_plans: 72 self.assertFalse("Completed plan stack" in result_arr[result_idx], "Found Completed header too early.") 73 self.assertIn(text, result_arr[result_idx], "Didn't find active plan: %s"%(text)) ; result_idx += 1 74 75 if len(completed_plans) > 0: 76 self.assertIn("Completed plan stack:", result_arr[result_idx], "Found completed plan stack header") ; result_idx += 1 77 for text in completed_plans: 78 self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1 79 80 if len(discarded_plans) > 0: 81 self.assertIn("Discarded plan stack:", result_arr[result_idx], "Found discarded plan stack header") ; result_idx += 1 82 for text in discarded_plans: 83 self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1 84 85 86 def thread_plan_test(self): 87 (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self, 88 "Set a breakpoint here", self.main_source_file) 89 90 # Now set a breakpoint in call_me and step over. We should have 91 # two public thread plans 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.StepOver() 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, ["Stepping over line main.c"], []) 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, ["Stepping over line main.c", "Stepping out from"]) 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, ["Stepping over line main.c"]) 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, ["Stepping over line main.c"]) 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, [], ["Stepping over line main.c"]) 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