199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest stepping out from a function in a multi-threaded program.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprecht
699451b44SJordan Rupprecht
799451b44SJordan Rupprechtimport lldb
899451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
999451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
1099451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
1199451b44SJordan Rupprecht
1299451b44SJordan Rupprecht
1399451b44SJordan Rupprechtclass ThreadStepOutTestCase(TestBase):
1499451b44SJordan Rupprecht
1599451b44SJordan Rupprecht    mydir = TestBase.compute_mydir(__file__)
1699451b44SJordan Rupprecht
1799451b44SJordan Rupprecht    # Test occasionally times out on the Linux build bot
1899451b44SJordan Rupprecht    @skipIfLinux
1999451b44SJordan Rupprecht    @expectedFailureAll(
2099451b44SJordan Rupprecht        oslist=["linux"],
2199451b44SJordan Rupprecht        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
2299451b44SJordan Rupprecht    @expectedFailureAll(
2399451b44SJordan Rupprecht        oslist=["freebsd"],
2499451b44SJordan Rupprecht        bugnumber="llvm.org/pr18066 inferior does not exit")
2599451b44SJordan Rupprecht    @skipIfWindows # This test will hang on windows llvm.org/pr21753
2699451b44SJordan Rupprecht    @expectedFailureAll(oslist=["windows"])
2799451b44SJordan Rupprecht    @expectedFailureNetBSD
2899451b44SJordan Rupprecht    def test_step_single_thread(self):
2999451b44SJordan Rupprecht        """Test thread step out on one thread via command interpreter. """
30*d7dbe2c4SPavel Labath        self.build()
3199451b44SJordan Rupprecht        self.step_out_test(self.step_out_single_thread_with_cmd)
3299451b44SJordan Rupprecht
3399451b44SJordan Rupprecht    # Test occasionally times out on the Linux build bot
3499451b44SJordan Rupprecht    @skipIfLinux
3599451b44SJordan Rupprecht    @expectedFailureAll(
3699451b44SJordan Rupprecht        oslist=["linux"],
3799451b44SJordan Rupprecht        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
3899451b44SJordan Rupprecht    @expectedFailureAll(
3999451b44SJordan Rupprecht        oslist=["freebsd"],
4099451b44SJordan Rupprecht        bugnumber="llvm.org/pr19347 2nd thread stops at breakpoint")
4199451b44SJordan Rupprecht    @skipIfWindows # This test will hang on windows llvm.org/pr21753
4299451b44SJordan Rupprecht    @expectedFailureAll(oslist=["windows"])
4399451b44SJordan Rupprecht    @expectedFailureAll(oslist=["watchos"], archs=['armv7k'], bugnumber="rdar://problem/34674488") # stop reason is trace when it should be step-out
4499451b44SJordan Rupprecht    @expectedFailureNetBSD
4599451b44SJordan Rupprecht    def test_step_all_threads(self):
4699451b44SJordan Rupprecht        """Test thread step out on all threads via command interpreter. """
47*d7dbe2c4SPavel Labath        self.build()
4899451b44SJordan Rupprecht        self.step_out_test(self.step_out_all_threads_with_cmd)
4999451b44SJordan Rupprecht
5099451b44SJordan Rupprecht    # Test occasionally times out on the Linux build bot
5199451b44SJordan Rupprecht    @skipIfLinux
5299451b44SJordan Rupprecht    @expectedFailureAll(
5399451b44SJordan Rupprecht        oslist=["linux"],
5499451b44SJordan Rupprecht        bugnumber="llvm.org/pr23477 Test occasionally times out on the Linux build bot")
5599451b44SJordan Rupprecht    @expectedFailureAll(
5699451b44SJordan Rupprecht        oslist=["freebsd"],
5799451b44SJordan Rupprecht        bugnumber="llvm.org/pr19347 2nd thread stops at breakpoint")
5899451b44SJordan Rupprecht    @skipIfWindows # This test will hang on windows llvm.org/pr21753
5999451b44SJordan Rupprecht    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24681")
6099451b44SJordan Rupprecht    @expectedFailureNetBSD
6199451b44SJordan Rupprecht    def test_python(self):
6299451b44SJordan Rupprecht        """Test thread step out on one thread via Python API (dwarf)."""
63*d7dbe2c4SPavel Labath        self.build()
6499451b44SJordan Rupprecht        self.step_out_test(self.step_out_with_python)
6599451b44SJordan Rupprecht
6699451b44SJordan Rupprecht    def setUp(self):
6799451b44SJordan Rupprecht        # Call super's setUp().
6899451b44SJordan Rupprecht        TestBase.setUp(self)
6999451b44SJordan Rupprecht        # Find the line number for our breakpoint.
7099451b44SJordan Rupprecht        self.bkpt_string = '// Set breakpoint here'
7199451b44SJordan Rupprecht        self.breakpoint = line_number('main.cpp', self.bkpt_string)
7271c4da83SJim Ingham        self.step_in_line = line_number('main.cpp', '// But we might still be here')
7371c4da83SJim Ingham        self.step_out_dest = line_number('main.cpp', '// Expect to stop here after step-out.')
7499451b44SJordan Rupprecht
7571c4da83SJim Ingham    def check_stepping_thread(self):
7671c4da83SJim Ingham        zeroth_frame = self.step_out_thread.frames[0]
7771c4da83SJim Ingham        line_entry = zeroth_frame.line_entry
7871c4da83SJim Ingham        self.assertTrue(line_entry.IsValid(), "Stopped at a valid line entry")
7971c4da83SJim Ingham        self.assertEqual("main.cpp", line_entry.file.basename, "Still in main.cpp")
8071c4da83SJim Ingham        # We can't really tell whether we stay on our line
8171c4da83SJim Ingham        # or get to the next line, it depends on whether there are any
8271c4da83SJim Ingham        # instructions between the call and the return.
8371c4da83SJim Ingham        line = line_entry.line
8471c4da83SJim Ingham        self.assertTrue(line == self.step_out_dest or line == self.step_in_line, "Stepped to the wrong line: {0}".format(line))
8599451b44SJordan Rupprecht
8699451b44SJordan Rupprecht    def step_out_single_thread_with_cmd(self):
8771c4da83SJim Ingham        other_threads = {}
8871c4da83SJim Ingham        for thread in self.process.threads:
8971c4da83SJim Ingham            if thread.GetIndexID() == self.step_out_thread.GetIndexID():
9071c4da83SJim Ingham                continue
9171c4da83SJim Ingham            other_threads[thread.GetIndexID()] = thread.frames[0].line_entry
9271c4da83SJim Ingham
9371c4da83SJim Ingham        # There should be other threads...
9471c4da83SJim Ingham        self.assertNotEqual(len(other_threads), 0)
9599451b44SJordan Rupprecht        self.step_out_with_cmd("this-thread")
9671c4da83SJim Ingham        # The other threads should not have made progress:
9771c4da83SJim Ingham        for thread in self.process.threads:
9871c4da83SJim Ingham            index_id = thread.GetIndexID()
9971c4da83SJim Ingham            line_entry = other_threads.get(index_id)
10071c4da83SJim Ingham            if line_entry:
10171c4da83SJim Ingham                self.assertEqual(thread.frames[0].line_entry.file.basename, line_entry.file.basename, "Thread {0} moved by file".format(index_id))
10271c4da83SJim Ingham                self.assertEqual(thread.frames[0].line_entry.line, line_entry.line, "Thread {0} moved by line".format(index_id))
10399451b44SJordan Rupprecht
10499451b44SJordan Rupprecht    def step_out_all_threads_with_cmd(self):
10599451b44SJordan Rupprecht        self.step_out_with_cmd("all-threads")
10699451b44SJordan Rupprecht
10799451b44SJordan Rupprecht    def step_out_with_cmd(self, run_mode):
10899451b44SJordan Rupprecht        self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
10999451b44SJordan Rupprecht        self.runCmd("thread step-out -m %s" % run_mode)
11099451b44SJordan Rupprecht        self.expect("process status", "Expected stop reason to be step-out",
11199451b44SJordan Rupprecht                    substrs=["stop reason = step out"])
11299451b44SJordan Rupprecht
11371c4da83SJim Ingham        selected_thread = self.process.GetSelectedThread()
11471c4da83SJim Ingham        self.assertEqual(selected_thread.GetIndexID(), self.step_out_thread.GetIndexID(), "Step out changed selected thread.")
11571c4da83SJim Ingham        self.check_stepping_thread()
11699451b44SJordan Rupprecht
11799451b44SJordan Rupprecht    def step_out_with_python(self):
11899451b44SJordan Rupprecht        self.step_out_thread.StepOut()
11999451b44SJordan Rupprecht
12099451b44SJordan Rupprecht        reason = self.step_out_thread.GetStopReason()
12199451b44SJordan Rupprecht        self.assertEqual(
12299451b44SJordan Rupprecht            lldb.eStopReasonPlanComplete,
12399451b44SJordan Rupprecht            reason,
12499451b44SJordan Rupprecht            "Expected thread stop reason 'plancomplete', but got '%s'" %
12599451b44SJordan Rupprecht            lldbutil.stop_reason_to_str(reason))
12671c4da83SJim Ingham        self.check_stepping_thread()
12799451b44SJordan Rupprecht
12899451b44SJordan Rupprecht
12999451b44SJordan Rupprecht    def step_out_test(self, step_out_func):
13099451b44SJordan Rupprecht        """Test single thread step out of a function."""
13171c4da83SJim Ingham        (self.inferior_target, self.process, thread, bkpt) = lldbutil.run_to_source_breakpoint(
13299451b44SJordan Rupprecht            self, self.bkpt_string, lldb.SBFileSpec('main.cpp'), only_one_thread = False)
13399451b44SJordan Rupprecht
13499451b44SJordan Rupprecht        # We hit the breakpoint on at least one thread.  If we hit it on both threads
13599451b44SJordan Rupprecht        # simultaneously, we can try the step out.  Otherwise, suspend the thread
13699451b44SJordan Rupprecht        # that hit the breakpoint, and continue till the second thread hits
13799451b44SJordan Rupprecht        # the breakpoint:
13899451b44SJordan Rupprecht
13999451b44SJordan Rupprecht        (breakpoint_threads, other_threads) = ([], [])
14071c4da83SJim Ingham        lldbutil.sort_stopped_threads(self.process,
14199451b44SJordan Rupprecht                                      breakpoint_threads=breakpoint_threads,
14299451b44SJordan Rupprecht                                      other_threads=other_threads)
14399451b44SJordan Rupprecht        if len(breakpoint_threads) == 1:
14499451b44SJordan Rupprecht            success = thread.Suspend()
14599451b44SJordan Rupprecht            self.assertTrue(success, "Couldn't suspend a thread")
14671c4da83SJim Ingham            bkpt_threads = lldbutil.continue_to_breakpoint(self.process,
14777f0ea4bSMichał Górny                                                           bkpt)
14899451b44SJordan Rupprecht            self.assertEqual(len(bkpt_threads), 1, "Second thread stopped")
14999451b44SJordan Rupprecht            success = thread.Resume()
15099451b44SJordan Rupprecht            self.assertTrue(success, "Couldn't resume a thread")
15199451b44SJordan Rupprecht
15299451b44SJordan Rupprecht        self.step_out_thread = breakpoint_threads[0]
15399451b44SJordan Rupprecht
15499451b44SJordan Rupprecht        # Step out of thread stopped at breakpoint
15599451b44SJordan Rupprecht        step_out_func()
156