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