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