1"""
2Make sure that we handle an expression on a thread, if
3the thread exits while the expression is running.
4"""
5
6import lldb
7from lldbsuite.test.decorators import *
8import lldbsuite.test.lldbutil as lldbutil
9from lldbsuite.test.lldbtest import *
10
11class TestExitDuringExpression(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    NO_DEBUG_INFO_TESTCASE = True
16
17    @skipIfWindows
18    @expectedFailureAll(oslist=["freebsd"])
19    def test_exit_before_one_thread_unwind(self):
20        """Test the case where we exit within the one thread timeout"""
21        self.exiting_expression_test(True, True)
22
23    @skipIfWindows
24    @expectedFailureAll(oslist=["freebsd"])
25    def test_exit_before_one_thread_no_unwind(self):
26        """Test the case where we exit within the one thread timeout"""
27        self.exiting_expression_test(True, False)
28
29    @skipIfWindows
30    def test_exit_after_one_thread_unwind(self):
31        """Test the case where we exit within the one thread timeout"""
32        self.exiting_expression_test(False, True)
33
34    @skipIfWindows
35    def test_exit_after_one_thread_no_unwind(self):
36        """Test the case where we exit within the one thread timeout"""
37        self.exiting_expression_test(False, False)
38
39    def setUp(self):
40        TestBase.setUp(self)
41        self.main_source_file = lldb.SBFileSpec("main.c")
42        self.build()
43
44    @skipIfReproducer # Timeouts are not currently modeled.
45    def exiting_expression_test(self, before_one_thread_timeout , unwind):
46        """function_to_call sleeps for g_timeout microseconds, then calls pthread_exit.
47           This test calls function_to_call with an overall timeout of 500
48           microseconds, and a one_thread_timeout as passed in.
49           It also sets unwind_on_exit for the call to the unwind passed in.
50           This allows you to have the thread exit either before the one thread
51           timeout is passed. """
52
53        (target, process, thread, bkpt) = lldbutil.run_to_source_breakpoint(self,
54                                   "Break here and cause the thread to exit", self.main_source_file)
55
56        # We'll continue to this breakpoint after running our expression:
57        return_bkpt = target.BreakpointCreateBySourceRegex("Break here to make sure the thread exited", self.main_source_file)
58        frame = thread.frames[0]
59        tid = thread.GetThreadID()
60        # Find the timeout:
61        var_options = lldb.SBVariablesOptions()
62        var_options.SetIncludeArguments(False)
63        var_options.SetIncludeLocals(False)
64        var_options.SetIncludeStatics(True)
65
66        value_list = frame.GetVariables(var_options)
67        g_timeout = value_list.GetFirstValueByName("g_timeout")
68        self.assertTrue(g_timeout.IsValid(), "Found g_timeout")
69
70        error = lldb.SBError()
71        timeout_value = g_timeout.GetValueAsUnsigned(error)
72        self.assertTrue(error.Success(), "Couldn't get timeout value: %s"%(error.GetCString()))
73
74        one_thread_timeout = 0
75        if (before_one_thread_timeout):
76            one_thread_timeout = timeout_value * 2
77        else:
78            one_thread_timeout = int(timeout_value / 2)
79
80        options = lldb.SBExpressionOptions()
81        options.SetUnwindOnError(unwind)
82        options.SetOneThreadTimeoutInMicroSeconds(one_thread_timeout)
83        options.SetTimeoutInMicroSeconds(4 * timeout_value)
84
85        result = frame.EvaluateExpression("function_to_call()", options)
86
87        # Make sure the thread actually exited:
88        thread = process.GetThreadByID(tid)
89        self.assertFalse(thread.IsValid(), "The thread exited")
90
91        # Make sure the expression failed:
92        self.assertFalse(result.GetError().Success(), "Expression failed.")
93
94        # Make sure we can keep going:
95        threads = lldbutil.continue_to_breakpoint(process, return_bkpt)
96        if not threads:
97            self.fail("didn't get any threads back after continuing")
98
99        self.assertEqual(len(threads), 1, "One thread hit our breakpoint")
100        thread = threads[0]
101        frame = thread.frames[0]
102        # Now get the return value, if we successfully caused the thread to exit
103        # it should be 10, not 20.
104        ret_val = frame.FindVariable("ret_val")
105        self.assertTrue(ret_val.GetError().Success(), "Found ret_val")
106        ret_val_value = ret_val.GetValueAsSigned(error)
107        self.assertTrue(error.Success(), "Got ret_val's value")
108        self.assertEqual(ret_val_value, 10, "We put the right value in ret_val")
109
110