]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/expression_command/call-restarts/TestCallThatRestarts.py
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / expression_command / call-restarts / TestCallThatRestarts.py
1 """
2 Test calling a function that hits a signal set to auto-restart, make sure the call completes.
3 """
4
5 from __future__ import print_function
6
7
8
9 import lldb
10 from lldbsuite.test.decorators import *
11 from lldbsuite.test.lldbtest import *
12 from lldbsuite.test import lldbutil
13
14 class ExprCommandThatRestartsTestCase(TestBase):
15
16     mydir = TestBase.compute_mydir(__file__)
17
18     def setUp(self):
19         # Call super's setUp().
20         TestBase.setUp(self)
21
22         self.main_source = "lotta-signals.c"
23         self.main_source_spec = lldb.SBFileSpec (self.main_source)
24
25     @skipIfFreeBSD # llvm.org/pr19246: intermittent failure
26     @skipIfDarwin # llvm.org/pr19246: intermittent failure
27     @skipIfWindows # Test relies on signals, unsupported on Windows
28     def test(self):
29         """Test calling function that hits a signal and restarts."""
30         self.build()
31         self.call_function()
32
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
37
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")
41
42     def call_function(self):
43         exe_name = "a.out"
44         exe = os.path.join(os.getcwd(), exe_name)
45
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)
51
52         # Launch the process, and do not stop at the entry point.
53         process = target.LaunchSimple (None, None, self.get_process_working_directory())
54
55         self.assertTrue(process, PROCESS_IS_VALID)
56
57         # Frame #0 should be at our breakpoint.
58         threads = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
59         
60         self.assertTrue(len(threads) == 1)
61         self.thread = threads[0]
62         
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")
67
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")
71
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")
74
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)
79
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()
83
84         num_sigchld = 30
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)
89
90         self.check_after_call(num_sigchld)
91
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)
98
99         value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
100
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)
104
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")
108
109         value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
110
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)
114
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)
118
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)
122
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:
125         
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")
128         
129         value = frame.EvaluateExpression("call_me (%d)"%(num_sigchld), options)
130         self.assertTrue (value.IsValid() and value.GetError().Success() == False)
131         
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")
135
136         error = process.Continue()
137         self.assertTrue (error.Success(), "Continuing after stopping for signal succeeds.")
138         
139         frame = self.thread.GetFrameAtIndex(0)
140         self.assertTrue (frame.GetPC() == self.orig_frame_pc, "Continuing returned to the place we started.")