xref: /llvm-project/lldb/test/API/functionalities/thread_plan/TestThreadPlanCommands.py (revision 327074812707d41f492a0e23940430422a9cd237)
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    @expectedFailureAll(oslist=["Linux"])
21    def test_thread_plan_actions(self):
22        self.build()
23        self.main_source_file = lldb.SBFileSpec("main.c")
24        self.thread_plan_test()
25
26    def check_list_output(self, command, active_plans = [], completed_plans = [], discarded_plans = []):
27        # Check the "thread plan list" output against a list of active & completed and discarded plans.
28        # If all three check arrays are empty, that means the command is expected to fail.
29
30        interp = self.dbg.GetCommandInterpreter()
31        result = lldb.SBCommandReturnObject()
32
33        num_active = len(active_plans)
34        num_completed = len(completed_plans)
35        num_discarded = len(discarded_plans)
36
37        interp.HandleCommand(command, result)
38        print("Command: %s"%(command))
39        print(result.GetOutput())
40
41        if num_active == 0 and num_completed == 0 and num_discarded == 0:
42            self.assertFalse(result.Succeeded(), "command: '%s' succeeded when it should have failed: '%s'"%
43                             (command, result.GetError()))
44            return
45
46        self.assertTrue(result.Succeeded(), "command: '%s' failed: '%s'"%(command, result.GetError()))
47        result_arr = result.GetOutput().splitlines()
48        num_results = len(result_arr)
49
50        # Match the expected number of elements.
51        # Adjust the count for the number of header lines we aren't matching:
52        fudge = 0
53
54        if num_completed == 0 and num_discarded == 0:
55            # The fudge is 3: Thread header, Active Plan header and base plan
56            fudge = 3
57        elif num_completed == 0 or num_discarded == 0:
58            # The fudge is 4: The above plus either the Completed or Discarded Plan header:
59            fudge = 4
60        else:
61            # The fudge is 5 since we have both headers:
62            fudge = 5
63
64        self.assertEqual(num_results, num_active + num_completed + num_discarded + fudge,
65                             "Too many elements in match arrays for: \n%s\n"%result.GetOutput())
66
67        # Now iterate through the results array and pick out the results.
68        result_idx = 0
69        self.assertIn("thread #", result_arr[result_idx], "Found thread header") ; result_idx += 1
70        self.assertIn("Active plan stack", result_arr[result_idx], "Found active header") ; result_idx += 1
71        self.assertIn("Element 0: Base thread plan", result_arr[result_idx], "Found base plan") ; result_idx += 1
72
73        for text in active_plans:
74            self.assertFalse("Completed plan stack" in result_arr[result_idx], "Found Completed header too early.")
75            self.assertIn(text, result_arr[result_idx], "Didn't find active plan: %s"%(text)) ; result_idx += 1
76
77        if len(completed_plans) > 0:
78            self.assertIn("Completed plan stack:", result_arr[result_idx], "Found completed plan stack header") ; result_idx += 1
79            for text in completed_plans:
80                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
81
82        if len(discarded_plans) > 0:
83            self.assertIn("Discarded plan stack:", result_arr[result_idx], "Found discarded plan stack header") ; result_idx += 1
84            for text in discarded_plans:
85                self.assertIn(text, result_arr[result_idx], "Didn't find completed plan: %s"%(text)) ; result_idx += 1
86
87
88    def thread_plan_test(self):
89        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
90                                   "Set a breakpoint here", self.main_source_file)
91
92        # Now set a breakpoint in call_me and step over.  We should have
93        # two public thread plans
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.StepOver()
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, ["Stepping over line main.c"], [])
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, ["Stepping over line main.c", "Stepping out from"])
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, ["Stepping over line main.c"])
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, ["Stepping over line main.c"])
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, [], ["Stepping over line main.c"])
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