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