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