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    # Test occasionally times out on the Linux build bot
18    @skipIfLinux
19    @expectedFailureAll(
20        oslist=["linux"],
21        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
22    @expectedFailureAll(
23        oslist=["freebsd"],
24        bugnumber="llvm.org/pr18066 inferior does not exit")
25    @skipIfWindows # This test will hang on windows llvm.org/pr21753
26    @expectedFailureAll(oslist=["windows"])
27    @expectedFailureNetBSD
28    def test_step_single_thread(self):
29        """Test thread step out on one thread via command interpreter. """
30        self.build(dictionary=self.getBuildFlags())
31        self.step_out_test(self.step_out_single_thread_with_cmd)
32
33    # Test occasionally times out on the Linux build bot
34    @skipIfLinux
35    @expectedFailureAll(
36        oslist=["linux"],
37        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
38    @expectedFailureAll(
39        oslist=["freebsd"],
40        bugnumber="llvm.org/pr19347 2nd thread stops at breakpoint")
41    @skipIfWindows # This test will hang on windows llvm.org/pr21753
42    @expectedFailureAll(oslist=["windows"])
43    @expectedFailureAll(oslist=["watchos"], archs=['armv7k'], bugnumber="rdar://problem/34674488") # stop reason is trace when it should be step-out
44    @expectedFailureNetBSD
45    def test_step_all_threads(self):
46        """Test thread step out on all threads via command interpreter. """
47        self.build(dictionary=self.getBuildFlags())
48        self.step_out_test(self.step_out_all_threads_with_cmd)
49
50    # Test occasionally times out on the Linux build bot
51    @skipIfLinux
52    @expectedFailureAll(
53        oslist=["linux"],
54        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
55    @expectedFailureAll(
56        oslist=["freebsd"],
57        bugnumber="llvm.org/pr19347 2nd thread stops at breakpoint")
58    @skipIfWindows # This test will hang on windows llvm.org/pr21753
59    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24681")
60    @expectedFailureNetBSD
61    def test_python(self):
62        """Test thread step out on one thread via Python API (dwarf)."""
63        self.build(dictionary=self.getBuildFlags())
64        self.step_out_test(self.step_out_with_python)
65
66    def setUp(self):
67        # Call super's setUp().
68        TestBase.setUp(self)
69        # Find the line number for our breakpoint.
70        self.bkpt_string = '// Set breakpoint here'
71        self.breakpoint = line_number('main.cpp', self.bkpt_string)
72
73        if "gcc" in self.getCompiler() or self.isIntelCompiler():
74            self.step_out_destination = line_number(
75                'main.cpp', '// Expect to stop here after step-out (icc and gcc)')
76        else:
77            self.step_out_destination = line_number(
78                'main.cpp', '// Expect to stop here after step-out (clang)')
79
80    def step_out_single_thread_with_cmd(self):
81        self.step_out_with_cmd("this-thread")
82        self.expect(
83            "thread backtrace all",
84            "Thread location after step out is correct",
85            substrs=[
86                "main.cpp:%d" %
87                self.step_out_destination,
88                "main.cpp:%d" %
89                self.breakpoint])
90
91    def step_out_all_threads_with_cmd(self):
92        self.step_out_with_cmd("all-threads")
93        self.expect(
94            "thread backtrace all",
95            "Thread location after step out is correct",
96            substrs=[
97                "main.cpp:%d" %
98                self.step_out_destination])
99
100    def step_out_with_cmd(self, run_mode):
101        self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
102        self.runCmd("thread step-out -m %s" % run_mode)
103        self.expect("process status", "Expected stop reason to be step-out",
104                    substrs=["stop reason = step out"])
105
106        self.expect(
107            "thread list",
108            "Selected thread did not change during step-out",
109            substrs=[
110                "* thread #%d" %
111                self.step_out_thread.GetIndexID()])
112
113    def step_out_with_python(self):
114        self.step_out_thread.StepOut()
115
116        reason = self.step_out_thread.GetStopReason()
117        self.assertEqual(
118            lldb.eStopReasonPlanComplete,
119            reason,
120            "Expected thread stop reason 'plancomplete', but got '%s'" %
121            lldbutil.stop_reason_to_str(reason))
122
123        # Verify location after stepping out
124        frame = self.step_out_thread.GetFrameAtIndex(0)
125        desc = lldbutil.get_description(frame.GetLineEntry())
126        expect = "main.cpp:%d" % self.step_out_destination
127        self.assertTrue(
128            expect in desc, "Expected %s but thread stopped at %s" %
129            (expect, desc))
130
131    def step_out_test(self, step_out_func):
132        """Test single thread step out of a function."""
133        (self.inferior_target, self.inferior_process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
134            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
135
136        # We hit the breakpoint on at least one thread.  If we hit it on both threads
137        # simultaneously, we can try the step out.  Otherwise, suspend the thread
138        # that hit the breakpoint, and continue till the second thread hits
139        # the breakpoint:
140
141        (breakpoint_threads, other_threads) = ([], [])
142        lldbutil.sort_stopped_threads(self.inferior_process,
143                                      breakpoint_threads=breakpoint_threads,
144                                      other_threads=other_threads)
145        if len(breakpoint_threads) == 1:
146            success = thread.Suspend()
147            self.assertTrue(success, "Couldn't suspend a thread")
148            bkpt_threads = lldbutil.continue_to_breakpoint(bkpt)
149            self.assertEqual(len(bkpt_threads), 1, "Second thread stopped")
150            success = thread.Resume()
151            self.assertTrue(success, "Couldn't resume a thread")
152
153        self.step_out_thread = breakpoint_threads[0]
154
155        # Step out of thread stopped at breakpoint
156        step_out_func()
157