2 Test calling a function that hits a signal set to auto-restart, make sure the call completes.
5 from __future__ import print_function
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
14 class ExprCommandThatRestartsTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
19 # Call super's setUp().
22 self.main_source = "lotta-signals.c"
23 self.main_source_spec = lldb.SBFileSpec (self.main_source)
25 @skipIfFreeBSD # llvm.org/pr19246: intermittent failure
26 @skipIfDarwin # llvm.org/pr19246: intermittent failure
27 @skipIfWindows # Test relies on signals, unsupported on Windows
29 """Test calling function that hits a signal and restarts."""
33 def check_after_call (self, num_sigchld):
34 after_call = self.sigchld_no.GetValueAsSigned(-1)
35 self.assertTrue (after_call - self.start_sigchld_no == num_sigchld, "Really got %d SIGCHLD signals through the call."%(num_sigchld))
36 self.start_sigchld_no = after_call
38 # Check that we are back where we were before:
39 frame = self.thread.GetFrameAtIndex(0)
40 self.assertTrue (self.orig_frame_pc == frame.GetPC(), "Restored the zeroth frame correctly")
42 def call_function(self):
44 exe = os.path.join(os.getcwd(), exe_name)
46 target = self.dbg.CreateTarget(exe)
47 self.assertTrue(target, VALID_TARGET)
48 empty = lldb.SBFileSpec()
49 breakpoint = target.BreakpointCreateBySourceRegex('Stop here in main.',self.main_source_spec)
50 self.assertTrue(breakpoint.GetNumLocations() > 0, VALID_BREAKPOINT)
52 # Launch the process, and do not stop at the entry point.
53 process = target.LaunchSimple (None, None, self.get_process_working_directory())
55 self.assertTrue(process, PROCESS_IS_VALID)
57 # Frame #0 should be at our breakpoint.
58 threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
60 self.assertTrue(len(threads) == 1)
61 self.thread = threads[0]
63 # Make sure the SIGCHLD behavior is pass/no-stop/no-notify:
64 return_obj = lldb.SBCommandReturnObject()
65 self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 0", return_obj)
66 self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop")
68 # The sigchld_no variable should be 0 at this point.
69 self.sigchld_no = target.FindFirstGlobalVariable("sigchld_no")
70 self.assertTrue (self.sigchld_no.IsValid(), "Got a value for sigchld_no")
72 self.start_sigchld_no = self.sigchld_no.GetValueAsSigned (-1)
73 self.assertTrue (self.start_sigchld_no != -1, "Got an actual value for sigchld_no")
75 options = lldb.SBExpressionOptions()
76 # processing 30 signals takes a while, increase the expression timeout a bit
77 options.SetTimeoutInMicroSeconds(3000000) # 3s
78 options.SetUnwindOnError(True)
80 frame = self.thread.GetFrameAtIndex(0)
81 # Store away the PC to check that the functions unwind to the right place after calls
82 self.orig_frame_pc = frame.GetPC()
85 value = frame.EvaluateExpression ("call_me (%d)"%(num_sigchld), options)
86 self.assertTrue (value.IsValid())
87 self.assertTrue (value.GetError().Success() == True)
88 self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld)
90 self.check_after_call(num_sigchld)
92 # Okay, now try with a breakpoint in the called code in the case where
93 # we are ignoring breakpoint hits.
94 handler_bkpt = target.BreakpointCreateBySourceRegex("Got sigchld %d.", self.main_source_spec)
95 self.assertTrue (handler_bkpt.GetNumLocations() > 0)
96 options.SetIgnoreBreakpoints(True)
97 options.SetUnwindOnError(True)
99 value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
101 self.assertTrue (value.IsValid() and value.GetError().Success() == True)
102 self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld)
103 self.check_after_call(num_sigchld)
105 # Now set the signal to print but not stop and make sure that calling still works:
106 self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 1", return_obj)
107 self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop, notify")
109 value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
111 self.assertTrue (value.IsValid() and value.GetError().Success() == True)
112 self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld)
113 self.check_after_call(num_sigchld)
115 # Now set this unwind on error to false, and make sure that we still complete the call:
116 options.SetUnwindOnError(False)
117 value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
119 self.assertTrue (value.IsValid() and value.GetError().Success() == True)
120 self.assertTrue (value.GetValueAsSigned(-1) == num_sigchld)
121 self.check_after_call(num_sigchld)
123 # Okay, now set UnwindOnError to true, and then make the signal behavior to stop
124 # and see that now we do stop at the signal point:
126 self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 1 -p 1 -n 1", return_obj)
127 self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, stop, notify")
129 value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
130 self.assertTrue (value.IsValid() and value.GetError().Success() == False)
132 # Set signal handling back to no-stop, and continue and we should end up back in out starting frame:
133 self.dbg.GetCommandInterpreter().HandleCommand("process handle SIGCHLD -s 0 -p 1 -n 1", return_obj)
134 self.assertTrue (return_obj.Succeeded() == True, "Set SIGCHLD to pass, no-stop, notify")
136 error = process.Continue()
137 self.assertTrue (error.Success(), "Continuing after stopping for signal succeeds.")
139 frame = self.thread.GetFrameAtIndex(0)
140 self.assertTrue (frame.GetPC() == self.orig_frame_pc, "Continuing returned to the place we started.")