1"""
2Test stepping out from a function in a multi-threaded program.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class ThreadStepOutTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @skipIfWindows # This test will hang on windows llvm.org/pr21753
18    @expectedFailureAll(oslist=["windows"])
19    @expectedFailureNetBSD
20    def test_step_single_thread(self):
21        """Test thread step out on one thread via command interpreter. """
22        self.build()
23        self.step_out_test(self.step_out_single_thread_with_cmd)
24
25    @skipIfWindows # This test will hang on windows llvm.org/pr21753
26    @expectedFailureAll(oslist=["windows"])
27    @expectedFailureAll(oslist=["watchos"], archs=['armv7k'], bugnumber="rdar://problem/34674488") # stop reason is trace when it should be step-out
28    @expectedFailureNetBSD
29    def test_step_all_threads(self):
30        """Test thread step out on all threads via command interpreter. """
31        self.build()
32        self.step_out_test(self.step_out_all_threads_with_cmd)
33
34    @skipIfWindows # This test will hang on windows llvm.org/pr21753
35    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24681")
36    @expectedFailureNetBSD
37    def test_python(self):
38        """Test thread step out on one thread via Python API (dwarf)."""
39        self.build()
40        self.step_out_test(self.step_out_with_python)
41
42    def setUp(self):
43        # Call super's setUp().
44        TestBase.setUp(self)
45        # Find the line number for our breakpoint.
46        self.bkpt_string = '// Set breakpoint here'
47        self.breakpoint = line_number('main.cpp', self.bkpt_string)
48        self.step_in_line = line_number('main.cpp', '// But we might still be here')
49        self.step_out_dest = line_number('main.cpp', '// Expect to stop here after step-out.')
50
51    def check_stepping_thread(self):
52        zeroth_frame = self.step_out_thread.frames[0]
53        line_entry = zeroth_frame.line_entry
54        self.assertTrue(line_entry.IsValid(), "Stopped at a valid line entry")
55        self.assertEqual("main.cpp", line_entry.file.basename, "Still in main.cpp")
56        # We can't really tell whether we stay on our line
57        # or get to the next line, it depends on whether there are any
58        # instructions between the call and the return.
59        line = line_entry.line
60        self.assertTrue(line == self.step_out_dest or line == self.step_in_line, "Stepped to the wrong line: {0}".format(line))
61
62    def step_out_single_thread_with_cmd(self):
63        other_threads = {}
64        for thread in self.process.threads:
65            if thread.GetIndexID() == self.step_out_thread.GetIndexID():
66                continue
67            other_threads[thread.GetIndexID()] = thread.frames[0].line_entry
68
69        # There should be other threads...
70        self.assertNotEqual(len(other_threads), 0)
71        self.step_out_with_cmd("this-thread")
72        # The other threads should not have made progress:
73        for thread in self.process.threads:
74            index_id = thread.GetIndexID()
75            line_entry = other_threads.get(index_id)
76            if line_entry:
77                self.assertEqual(thread.frames[0].line_entry.file.basename, line_entry.file.basename, "Thread {0} moved by file".format(index_id))
78                self.assertEqual(thread.frames[0].line_entry.line, line_entry.line, "Thread {0} moved by line".format(index_id))
79
80    def step_out_all_threads_with_cmd(self):
81        self.step_out_with_cmd("all-threads")
82
83    def step_out_with_cmd(self, run_mode):
84        self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
85        self.runCmd("thread step-out -m %s" % run_mode)
86        self.expect("process status", "Expected stop reason to be step-out",
87                    substrs=["stop reason = step out"])
88
89        selected_thread = self.process.GetSelectedThread()
90        self.assertEqual(selected_thread.GetIndexID(), self.step_out_thread.GetIndexID(), "Step out changed selected thread.")
91        self.check_stepping_thread()
92
93    def step_out_with_python(self):
94        self.step_out_thread.StepOut()
95
96        reason = self.step_out_thread.GetStopReason()
97        self.assertEqual(
98            lldb.eStopReasonPlanComplete,
99            reason,
100            "Expected thread stop reason 'plancomplete', but got '%s'" %
101            lldbutil.stop_reason_to_str(reason))
102        self.check_stepping_thread()
103
104
105    def step_out_test(self, step_out_func):
106        """Test single thread step out of a function."""
107        (self.inferior_target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
108            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
109
110        # We hit the breakpoint on at least one thread.  If we hit it on both threads
111        # simultaneously, we can try the step out.  Otherwise, suspend the thread
112        # that hit the breakpoint, and continue till the second thread hits
113        # the breakpoint:
114
115        (breakpoint_threads, other_threads) = ([], [])
116        lldbutil.sort_stopped_threads(self.process,
117                                      breakpoint_threads=breakpoint_threads,
118                                      other_threads=other_threads)
119        if len(breakpoint_threads) == 1:
120            success = thread.Suspend()
121            self.assertTrue(success, "Couldn't suspend a thread")
122            breakpoint_threads = lldbutil.continue_to_breakpoint(self.process,
123                                                           bkpt)
124            self.assertEqual(len(breakpoint_threads), 2, "Second thread stopped")
125            success = thread.Resume()
126            self.assertTrue(success, "Couldn't resume a thread")
127
128        self.step_out_thread = breakpoint_threads[0]
129
130        # Step out of thread stopped at breakpoint
131        step_out_func()
132