From 6ad180cf355f98798c3d06457516f8474b3d5e2d Mon Sep 17 00:00:00 2001 From: Jim Ingham Date: Mon, 11 Feb 2013 19:05:29 +0000 Subject: [PATCH] Forgot to add the testsuite for the changes I checked in on Friday. llvm-svn: 174897 --- .../test/expression_command/call-restarts/Makefile | 5 + .../call-restarts/TestCallThatRestarts.py | 148 +++++++++++++++++++++ .../call-restarts/lotta-signals.c | 59 ++++++++ 3 files changed, 212 insertions(+) create mode 100644 lldb/test/expression_command/call-restarts/Makefile create mode 100644 lldb/test/expression_command/call-restarts/TestCallThatRestarts.py create mode 100644 lldb/test/expression_command/call-restarts/lotta-signals.c diff --git a/lldb/test/expression_command/call-restarts/Makefile b/lldb/test/expression_command/call-restarts/Makefile new file mode 100644 index 0000000..1c93ae1 --- /dev/null +++ b/lldb/test/expression_command/call-restarts/Makefile @@ -0,0 +1,5 @@ +LEVEL = ../../make + +C_SOURCES := lotta-signals.c + +include $(LEVEL)/Makefile.rules diff --git a/lldb/test/expression_command/call-restarts/TestCallThatRestarts.py b/lldb/test/expression_command/call-restarts/TestCallThatRestarts.py new file mode 100644 index 0000000..8fd9bb8 --- /dev/null +++ b/lldb/test/expression_command/call-restarts/TestCallThatRestarts.py @@ -0,0 +1,148 @@ +""" +Test calling a function that hits a signal set to auto-restart, make sure the call completes. +""" + +import unittest2 +import lldb +import lldbutil +from lldbtest import * + +class ExprCommandWithTimeoutsTestCase(TestBase): + + mydir = os.path.join("expression_command", "call-restarts") + + def setUp(self): + # Call super's setUp(). + TestBase.setUp(self) + + self.main_source = "lotta-signals.c" + self.main_source_spec = lldb.SBFileSpec (self.main_source) + + + @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin") + @dsym_test + def test_with_dsym(self): + """Test calling std::String member function.""" + self.buildDsym() + self.call_function() + + @dwarf_test + def test_with_dwarf(self): + """Test calling std::String member function.""" + self.buildDwarf() + self.call_function() + + def check_after_call (self, num_sigchld): + after_call = self.sigchld_no.GetValueAsSigned(-1) + self.assertTrue (after_call - self.start_sigchld_no == num_sigchld, "Really got %d SIGCHLD signals through the call."%(num_sigchld)) + self.start_sigchld_no = after_call + + # Check that we are back where we were before: + frame = self.thread.GetFrameAtIndex(0) + self.assertTrue (self.orig_frame_pc == frame.GetPC(), "Restored the zeroth frame correctly") + + + def call_function(self): + """Test calling function with timeout.""" + exe_name = "a.out" + exe = os.path.join(os.getcwd(), exe_name) + + target = self.dbg.CreateTarget(exe) + self.assertTrue(target, VALID_TARGET) + empty = lldb.SBFileSpec() + breakpoint = target.BreakpointCreateBySourceRegex('Stop here in main.',self.main_source_spec) + self.assertTrue(breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT) + + # Launch the process, and do not stop at the entry point. + process = target.LaunchSimple(None, None, os.getcwd()) + + self.assertTrue(process, PROCESS_IS_VALID) + + # Frame #0 should be at our breakpoint. + threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint) + + self.assertTrue(len(threads) == 1) + self.thread = threads[0] + + # Make sure the SIGCHLD behavior is pass/no-stop/no-notify: + return_obj = lldb.SBCommandReturnObject() + self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 0", return_obj) + self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop") + + # The sigchld_no variable should be 0 at this point. + self.sigchld_no = target.FindFirstGlobalVariable("sigchld_no") + self.assertTrue (self.sigchld_no.IsValid(), "Got a value for sigchld_no") + + self.start_sigchld_no = self.sigchld_no.GetValueAsSigned (-1) + self.assertTrue (self.start_sigchld_no != -1, "Got an actual value for sigchld_no") + + options = lldb.SBExpressionOptions() + options.SetUnwindOnError(True) + + frame = self.thread.GetFrameAtIndex(0) + # Store away the PC to check that the functions unwind to the right place after calls + self.orig_frame_pc = frame.GetPC() + + num_sigchld = 30 + value = frame.EvaluateExpression ("call_me (%d)"%(num_sigchld), options) + self.assertTrue (value.IsValid()) + self.assertTrue (value.GetError().Success() == True) + self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld) + + self.check_after_call(num_sigchld) + + # Okay, now try with a breakpoint in the called code in the case where + # we are ignoring breakpoint hits. + handler_bkpt = target.BreakpointCreateBySourceRegex("Got sigchld %d.", self.main_source_spec) + self.assertTrue (handler_bkpt.GetNumLocations() > 0) + options.SetIgnoreBreakpoints(True) + options.SetUnwindOnError(True) + + value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options) + + self.assertTrue (value.IsValid() and value.GetError().Success() == True) + self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld) + self.check_after_call(num_sigchld) + + # Now set the signal to print but not stop and make sure that calling still works: + self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 1", return_obj) + self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop, notify") + + value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options) + + self.assertTrue (value.IsValid() and value.GetError().Success() == True) + self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld) + self.check_after_call(num_sigchld) + + # Now set this unwind on error to false, and make sure that we still complete the call: + options.SetUnwindOnError(False) + value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options) + + self.assertTrue (value.IsValid() and value.GetError().Success() == True) + self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld) + self.check_after_call(num_sigchld) + + # Okay, now set UnwindOnError to true, and then make the signal behavior to stop + # and see that now we do stop at the signal point: + + self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 1 -p 1 -n 1", return_obj) + self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, stop, notify") + + value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options) + self.assertTrue (value.IsValid() and value.GetError().Success() == False) + + # Set signal handling back to no-stop, and continue and we should end up back in out starting frame: + self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 1", return_obj) + self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop, notify") + + error = process.Continue() + self.assertTrue (error.Success(), "Continuing after stopping for signal succeeds.") + + frame = self.thread.GetFrameAtIndex(0) + self.assertTrue (frame.GetPC() == self.orig_frame_pc, "Continuing returned to the place we started.") + +if __name__ == '__main__': + import atexit + lldb.SBDebugger.Initialize() + atexit.register(lambda: lldb.SBDebugger.Terminate()) + unittest2.main() diff --git a/lldb/test/expression_command/call-restarts/lotta-signals.c b/lldb/test/expression_command/call-restarts/lotta-signals.c new file mode 100644 index 0000000..ac487cb --- /dev/null +++ b/lldb/test/expression_command/call-restarts/lotta-signals.c @@ -0,0 +1,59 @@ +#include +#include +#include + +static int sigchld_no; +static int nosig_no; +static int weird_value; + +void +sigchld_handler (int signo) +{ + sigchld_no++; + printf ("Got sigchld %d.\n", sigchld_no); +} + +int +call_me (int some_value) +{ + int ret_val = 0; + for (int i = 0; i < some_value; i++) + { + int result = 0; + if (i%2 == 0) + result = kill (getpid(), SIGCHLD); + else + sigchld_no++; + + usleep(1000); + if (result == 0) + ret_val++; + } + usleep (10000); + return ret_val; +} + +int +call_me_nosig (int some_value) +{ + int ret_val = 0; + for (int i = 0; i < some_value; i++) + weird_value += i % 4; + + nosig_no += some_value; + return some_value; +} + +int +main () +{ + int ret_val; + signal (SIGCHLD, sigchld_handler); + + ret_val = call_me (2); // Stop here in main. + + ret_val = call_me_nosig (10); + + return 0; + +} -- 2.7.4