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