1"""
2Test that we page getting a long backtrace on more than one thread
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestThreadBacktracePage(TestBase):
14    NO_DEBUG_INFO_TESTCASE = True
15
16    def test_thread_backtrace_one_thread(self):
17        """Run a simplified version of the test that just hits one breakpoint and
18           doesn't care about synchronizing the two threads - hopefully this will
19           run on more systems."""
20
21    def test_thread_backtrace_one_thread(self):
22        self.build()
23        (self.inferior_target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
24            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
25
26        # We hit the breakpoint on at least one thread.  If we hit it on both threads
27        # simultaneously, we are ready to run our tests.  Otherwise, suspend the thread
28        # that hit the breakpoint, and continue till the second thread hits
29        # the breakpoint:
30
31        (breakpoint_threads, other_threads) = ([], [])
32        lldbutil.sort_stopped_threads(self.process,
33                                      breakpoint_threads=breakpoint_threads,
34                                      other_threads=other_threads)
35        self.assertGreater(len(breakpoint_threads), 0, "We hit at least one breakpoint thread")
36        self.assertGreater(len(breakpoint_threads[0].frames), 2, "I can go up")
37        thread_id = breakpoint_threads[0].idx
38        name = breakpoint_threads[0].frame[1].name.split("(")[0]
39        self.check_one_thread(thread_id, name)
40
41    def setUp(self):
42        # Call super's setUp().
43        TestBase.setUp(self)
44        # Find the line number for our breakpoint.
45        self.bkpt_string = '// Set breakpoint here'
46
47    def check_one_thread(self, thread_id, func_name):
48        # Now issue some thread backtrace commands and make sure they
49        # get the right answer back.
50        interp = self.dbg.GetCommandInterpreter()
51        result = lldb.SBCommandReturnObject()
52
53        # Run the real backtrace, remember to pass True for add_to_history since
54        # we don't generate repeat commands for commands that aren't going into the history.
55        interp.HandleCommand("thread backtrace --count 10 {0}".format(thread_id), result, True)
56        self.assertTrue(result.Succeeded(), "bt with count succeeded")
57        # There should be 11 lines:
58        lines = result.GetOutput().splitlines()
59        self.assertEqual(len(lines), 11, "Got the right number of lines")
60        # First frame is stop_here:
61        self.assertNotEqual(lines[1].find("stop_here"), -1, "Found Stop Here")
62        for line in lines[2:10]:
63            self.assertNotEqual(line.find(func_name), -1, "Name {0} not found in line: {1}".format(func_name, line))
64        # The last entry should be 43:
65        self.assertNotEqual(lines[10].find("count=43"), -1, "First show ends at 43")
66
67        # Now try a repeat, and make sure we get 10 more on this thread:
68        #import pdb; pdb.set_trace()
69        interp.HandleCommand("", result, True)
70        self.assertTrue(result.Succeeded(), "repeat command failed: {0}".format(result.GetError()))
71        lines = result.GetOutput().splitlines()
72        self.assertEqual(len(lines), 11, "Repeat got 11 lines")
73        # Every line should now be the recurse function:
74        for line in lines[1:10]:
75            self.assertNotEqual(line.find(func_name), -1, "Name in every line")
76        self.assertNotEqual(lines[10].find("count=33"), -1, "Last one is now 33")
77
78    def check_two_threads(self, result_str, thread_id_1, name_1, thread_id_2, name_2, start_idx, end_idx):
79        # We should have 2 occurrences ot the thread header:
80        self.assertEqual(result_str.count("thread #{0}".format(thread_id_1)), 1, "One for thread 1")
81        self.assertEqual(result_str.count("thread #{0}".format(thread_id_2)), 1, "One for thread 2")
82        # We should have 10 occurrences of each name:
83        self.assertEqual(result_str.count(name_1), 10, "Found 10 of {0}".format(name_1))
84        self.assertEqual(result_str.count(name_2), 10, "Found 10 of {0}".format(name_1))
85        # There should be two instances of count=<start_idx> and none of count=<start-1>:
86        self.assertEqual(result_str.count("count={0}".format(start_idx)), 2, "Two instances of start_idx")
87        self.assertEqual(result_str.count("count={0}".format(start_idx-1)), 0, "No instances of start_idx - 1")
88        # There should be two instances of count=<end_idx> and none of count=<end_idx+1>:
89        self.assertEqual(result_str.count("count={0}".format(end_idx)), 2, "Two instances of end_idx")
90        self.assertEqual(result_str.count("count={0}".format(end_idx+1)), 0, "No instances after end idx")
91
92    # The setup of this test was copied from the step-out test, and I can't tell from
93    # the comments whether it was getting two threads to the same breakpoint that was
94    # problematic, or the step-out part.  This test stops at the rendevous point so I'm
95    # removing the skipIfLinux to see if we see any flakiness in just this part of the test.
96    @skipIfWindows # This test will hang on windows llvm.org/pr21753
97    @expectedFailureAll(oslist=["windows"])
98    @expectedFailureNetBSD
99    def test_thread_backtrace_two_threads(self):
100        """Test that repeat works even when backtracing on more than one thread."""
101        self.build()
102        (self.inferior_target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
103            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
104
105        # We hit the breakpoint on at least one thread.  If we hit it on both threads
106        # simultaneously, we are ready to run our tests.  Otherwise, suspend the thread
107        # that hit the breakpoint, and continue till the second thread hits
108        # the breakpoint:
109
110        (breakpoint_threads, other_threads) = ([], [])
111        lldbutil.sort_stopped_threads(self.process,
112                                      breakpoint_threads=breakpoint_threads,
113                                      other_threads=other_threads)
114        if len(breakpoint_threads) == 1:
115            success = thread.Suspend()
116            self.assertTrue(success, "Couldn't suspend a thread")
117            breakpoint_threads = lldbutil.continue_to_breakpoint(self.process,
118                                                           bkpt)
119            self.assertEqual(len(breakpoint_threads), 2, "Second thread stopped")
120
121        # Figure out which thread is which:
122        thread_id_1 = breakpoint_threads[0].idx
123        self.assertGreater(len(breakpoint_threads[0].frames), 2, "I can go up")
124        name_1 = breakpoint_threads[0].frame[1].name.split("(")[0]
125
126        thread_id_2 = breakpoint_threads[1].idx
127        self.assertGreater(len(breakpoint_threads[1].frames), 2, "I can go up")
128        name_2 = breakpoint_threads[1].frame[1].name.split("(")[0]
129
130        # Check that backtrace and repeat works on one thread, then works on the second
131        # when we switch to it:
132        self.check_one_thread(thread_id_1, name_1)
133        self.check_one_thread(thread_id_2, name_2)
134
135        # The output is looking right at this point, let's just do a couple more quick checks
136        # to see we handle two threads and a start count:
137        interp = self.dbg.GetCommandInterpreter()
138        result = lldb.SBCommandReturnObject()
139
140        interp.HandleCommand("thread backtrace --count 10 --start 10 {0} {1}".format(thread_id_1, thread_id_2), result, True)
141        self.assertTrue(result.Succeeded(), "command succeeded for two threads")
142
143        result.Clear()
144        interp.HandleCommand("", result, True)
145        self.assertTrue(result.Succeeded(), "repeat command succeeded for two threads")
146        result_str = result.GetOutput()
147        self.check_two_threads(result_str, thread_id_1, name_1, thread_id_2, name_2, 23, 32)
148
149        # Finally make sure the repeat repeats:
150        result.Clear()
151        interp.HandleCommand("", result, True)
152        self.assertTrue(result.Succeeded(), "repeat command succeeded for two threads")
153        result_str = result.GetOutput()
154        self.check_two_threads(result_str, thread_id_1, name_1, thread_id_2, name_2, 13, 22)
155