def step_out_test(self, step_out_func):
        """Test single thread step out of a function."""
        (self.inferior_target, self.inferior_process, thread,
         bkpt) = lldbutil.run_to_source_breakpoint(self,
                                                   self.bkpt_string,
                                                   lldb.SBFileSpec('main.cpp'),
                                                   only_one_thread=False)

        # We hit the breakpoint on at least one thread.  If we hit it on both threads
        # simultaneously, we can try the step out.  Otherwise, suspend the thread
        # that hit the breakpoint, and continue till the second thread hits
        # the breakpoint:

        (breakpoint_threads, other_threads) = ([], [])
        lldbutil.sort_stopped_threads(self.inferior_process,
                                      breakpoint_threads=breakpoint_threads,
                                      other_threads=other_threads)
        if len(breakpoint_threads) == 1:
            success = thread.Suspend()
            self.assertTrue(success, "Couldn't suspend a thread")
            bkpt_threads = lldbutil.continue_to_breakpoint(bkpt)
            self.assertEqual(len(bkpt_threads), 1, "Second thread stopped")
            success = thread.Resume()
            self.assertTrue(success, "Couldn't resume a thread")

        self.step_out_thread = breakpoint_threads[0]

        # Step out of thread stopped at breakpoint
        step_out_func()
    def step_out_test(self, step_out_func):
        """Test single thread step out of a function."""
        exe = self.getBuildArtifact("a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # This should create a breakpoint in the main thread.
        lldbutil.run_break_set_by_file_and_line(self,
                                                "main.cpp",
                                                self.breakpoint,
                                                num_expected_locations=1)

        # The breakpoint list should show 1 location.
        self.expect(
            "breakpoint list -f",
            "Breakpoint location shown correctly",
            substrs=[
                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1"
                % self.breakpoint
            ])

        # Run the program.
        self.runCmd("run", RUN_SUCCEEDED)

        # Get the target process
        self.inferior_target = self.dbg.GetSelectedTarget()
        self.inferior_process = self.inferior_target.GetProcess()

        # Get the number of threads, ensure we see all three.
        num_threads = self.inferior_process.GetNumThreads()
        self.assertEqual(
            num_threads, 3,
            'Number of expected threads and actual threads do not match.')

        (breakpoint_threads, other_threads) = ([], [])
        lldbutil.sort_stopped_threads(self.inferior_process,
                                      breakpoint_threads=breakpoint_threads,
                                      other_threads=other_threads)

        while len(breakpoint_threads) < 2:
            self.runCmd("thread continue %s" %
                        " ".join([str(x.GetIndexID()) for x in other_threads]))
            lldbutil.sort_stopped_threads(
                self.inferior_process,
                breakpoint_threads=breakpoint_threads,
                other_threads=other_threads)

        self.step_out_thread = breakpoint_threads[0]

        # Step out of thread stopped at breakpoint
        step_out_func()

        # Run to completion
        self.runCmd("continue")

        # At this point, the inferior process should have exited.
        self.assertTrue(self.inferior_process.GetState() == lldb.eStateExited,
                        PROCESS_EXITED)
    def step_out_test(self, step_out_func):
        """Test single thread step out of a function."""
        exe = os.path.join(os.getcwd(), "a.out")
        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)

        # This should create a breakpoint in the main thread.
        lldbutil.run_break_set_by_file_and_line(self, "main.cpp", self.breakpoint, num_expected_locations=1)

        # The breakpoint list should show 1 location.
        self.expect(
            "breakpoint list -f",
            "Breakpoint location shown correctly",
            substrs=["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.breakpoint],
        )

        # Run the program.
        self.runCmd("run", RUN_SUCCEEDED)

        # Get the target process
        self.inferior_target = self.dbg.GetSelectedTarget()
        self.inferior_process = self.inferior_target.GetProcess()

        # Get the number of threads, ensure we see all three.
        num_threads = self.inferior_process.GetNumThreads()
        self.assertEqual(num_threads, 3, "Number of expected threads and actual threads do not match.")

        (breakpoint_threads, other_threads) = ([], [])
        lldbutil.sort_stopped_threads(
            self.inferior_process, breakpoint_threads=breakpoint_threads, other_threads=other_threads
        )

        while len(breakpoint_threads) < 2:
            self.runCmd("thread continue %s" % " ".join([str(x.GetIndexID()) for x in other_threads]))
            lldbutil.sort_stopped_threads(
                self.inferior_process, breakpoint_threads=breakpoint_threads, other_threads=other_threads
            )

        self.step_out_thread = breakpoint_threads[0]

        # Step out of thread stopped at breakpoint
        step_out_func()

        # Run to completion
        self.runCmd("continue")

        # At this point, the inferior process should have exited.
        self.assertTrue(self.inferior_process.GetState() == lldb.eStateExited, PROCESS_EXITED)
    def test_thread_backtrace_one_thread(self):
        self.build()
        (self.inferior_target, self.process, thread,
         bkpt) = lldbutil.run_to_source_breakpoint(self,
                                                   self.bkpt_string,
                                                   lldb.SBFileSpec('main.cpp'),
                                                   only_one_thread=False)

        # We hit the breakpoint on at least one thread.  If we hit it on both threads
        # simultaneously, we are ready to run our tests.  Otherwise, suspend the thread
        # that hit the breakpoint, and continue till the second thread hits
        # the breakpoint:

        (breakpoint_threads, other_threads) = ([], [])
        lldbutil.sort_stopped_threads(self.process,
                                      breakpoint_threads=breakpoint_threads,
                                      other_threads=other_threads)
        self.assertGreater(len(breakpoint_threads), 0,
                           "We hit at least one breakpoint thread")
        self.assertGreater(len(breakpoint_threads[0].frames), 2, "I can go up")
        thread_id = breakpoint_threads[0].idx
        name = breakpoint_threads[0].frame[1].name.split("(")[0]
        self.check_one_thread(thread_id, name)
    def test_thread_backtrace_two_threads(self):
        """Test that repeat works even when backtracing on more than one thread."""
        self.build()
        (self.inferior_target, self.process, thread,
         bkpt) = lldbutil.run_to_source_breakpoint(self,
                                                   self.bkpt_string,
                                                   lldb.SBFileSpec('main.cpp'),
                                                   only_one_thread=False)

        # We hit the breakpoint on at least one thread.  If we hit it on both threads
        # simultaneously, we are ready to run our tests.  Otherwise, suspend the thread
        # that hit the breakpoint, and continue till the second thread hits
        # the breakpoint:

        (breakpoint_threads, other_threads) = ([], [])
        lldbutil.sort_stopped_threads(self.process,
                                      breakpoint_threads=breakpoint_threads,
                                      other_threads=other_threads)
        if len(breakpoint_threads) == 1:
            success = thread.Suspend()
            self.assertTrue(success, "Couldn't suspend a thread")
            breakpoint_threads = lldbutil.continue_to_breakpoint(
                self.process, bkpt)
            self.assertEqual(len(breakpoint_threads), 2,
                             "Second thread stopped")

        # Figure out which thread is which:
        thread_id_1 = breakpoint_threads[0].idx
        self.assertGreater(len(breakpoint_threads[0].frames), 2, "I can go up")
        name_1 = breakpoint_threads[0].frame[1].name.split("(")[0]

        thread_id_2 = breakpoint_threads[1].idx
        self.assertGreater(len(breakpoint_threads[1].frames), 2, "I can go up")
        name_2 = breakpoint_threads[1].frame[1].name.split("(")[0]

        # Check that backtrace and repeat works on one thread, then works on the second
        # when we switch to it:
        self.check_one_thread(thread_id_1, name_1)
        self.check_one_thread(thread_id_2, name_2)

        # The output is looking right at this point, let's just do a couple more quick checks
        # to see we handle two threads and a start count:
        interp = self.dbg.GetCommandInterpreter()
        result = lldb.SBCommandReturnObject()

        interp.HandleCommand(
            "thread backtrace --count 10 --start 10 {0} {1}".format(
                thread_id_1, thread_id_2), result, True)
        self.assertTrue(result.Succeeded(),
                        "command succeeded for two threads")

        result.Clear()
        interp.HandleCommand("", result, True)
        self.assertTrue(result.Succeeded(),
                        "repeat command succeeded for two threads")
        result_str = result.GetOutput()
        self.check_two_threads(result_str, thread_id_1, name_1, thread_id_2,
                               name_2, 23, 32)

        # Finally make sure the repeat repeats:
        result.Clear()
        interp.HandleCommand("", result, True)
        self.assertTrue(result.Succeeded(),
                        "repeat command succeeded for two threads")
        result_str = result.GetOutput()
        self.check_two_threads(result_str, thread_id_1, name_1, thread_id_2,
                               name_2, 13, 22)