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        self.step_out_destination = line_number(
74             'main.cpp', '// Expect to stop here after step-out.')
75
76    def step_out_single_thread_with_cmd(self):
77        self.step_out_with_cmd("this-thread")
78        self.expect(
79            "thread backtrace all",
80            "Thread location after step out is correct",
81            substrs=[
82                "main.cpp:%d" %
83                self.step_out_destination,
84                "main.cpp:%d" %
85                self.breakpoint])
86
87    def step_out_all_threads_with_cmd(self):
88        self.step_out_with_cmd("all-threads")
89        self.expect(
90            "thread backtrace all",
91            "Thread location after step out is correct",
92            substrs=[
93                "main.cpp:%d" %
94                self.step_out_destination])
95
96    def step_out_with_cmd(self, run_mode):
97        self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
98        self.runCmd("thread step-out -m %s" % run_mode)
99        self.expect("process status", "Expected stop reason to be step-out",
100                    substrs=["stop reason = step out"])
101
102        self.expect(
103            "thread list",
104            "Selected thread did not change during step-out",
105            substrs=[
106                "* thread #%d" %
107                self.step_out_thread.GetIndexID()])
108
109    def step_out_with_python(self):
110        self.step_out_thread.StepOut()
111
112        reason = self.step_out_thread.GetStopReason()
113        self.assertEqual(
114            lldb.eStopReasonPlanComplete,
115            reason,
116            "Expected thread stop reason 'plancomplete', but got '%s'" %
117            lldbutil.stop_reason_to_str(reason))
118
119        # Verify location after stepping out
120        frame = self.step_out_thread.GetFrameAtIndex(0)
121        desc = lldbutil.get_description(frame.GetLineEntry())
122        expect = "main.cpp:%d" % self.step_out_destination
123        self.assertTrue(
124            expect in desc, "Expected %s but thread stopped at %s" %
125            (expect, desc))
126
127    def step_out_test(self, step_out_func):
128        """Test single thread step out of a function."""
129        (self.inferior_target, self.inferior_process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
130            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
131
132        # We hit the breakpoint on at least one thread.  If we hit it on both threads
133        # simultaneously, we can try the step out.  Otherwise, suspend the thread
134        # that hit the breakpoint, and continue till the second thread hits
135        # the breakpoint:
136
137        (breakpoint_threads, other_threads) = ([], [])
138        lldbutil.sort_stopped_threads(self.inferior_process,
139                                      breakpoint_threads=breakpoint_threads,
140                                      other_threads=other_threads)
141        if len(breakpoint_threads) == 1:
142            success = thread.Suspend()
143            self.assertTrue(success, "Couldn't suspend a thread")
144            bkpt_threads = lldbutil.continue_to_breakpoint(self.inferior_process,
145                                                           bkpt)
146            self.assertEqual(len(bkpt_threads), 1, "Second thread stopped")
147            success = thread.Resume()
148            self.assertTrue(success, "Couldn't resume a thread")
149
150        self.step_out_thread = breakpoint_threads[0]
151
152        # Step out of thread stopped at breakpoint
153        step_out_func()
154