]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - packages/Python/lldbsuite/test/functionalities/inline-stepping/TestInlineStepping.py
Vendor import of lldb release_39 branch r276489:
[FreeBSD/FreeBSD.git] / packages / Python / lldbsuite / test / functionalities / inline-stepping / TestInlineStepping.py
1 """Test stepping over and into inlined functions."""
2
3 from __future__ import print_function
4
5
6
7 import os, time, sys
8 import lldb
9 from lldbsuite.test.decorators import *
10 from lldbsuite.test.lldbtest import *
11 from lldbsuite.test import lldbutil
12
13 class TestInlineStepping(TestBase):
14
15     mydir = TestBase.compute_mydir(__file__)
16
17     @add_test_categories(['pyapi'])
18     @expectedFailureAll(compiler="icc", bugnumber="# Not really a bug.  ICC combines two inlined functions.")
19     def test_with_python_api(self):
20         """Test stepping over and into inlined functions."""
21         self.build()
22         self.inline_stepping()
23
24     @add_test_categories(['pyapi'])
25     def test_step_over_with_python_api(self):
26         """Test stepping over and into inlined functions."""
27         self.build()
28         self.inline_stepping_step_over()
29     
30     @add_test_categories(['pyapi'])
31     def test_step_in_template_with_python_api(self):
32         """Test stepping in to templated functions."""
33         self.build()
34         self.step_in_template()
35
36     def setUp(self):
37         # Call super's setUp().
38         TestBase.setUp(self)
39         # Find the line numbers that we will step to in main:
40         self.main_source = "calling.cpp"
41         self.source_lines = {}
42         functions = ['caller_ref_1', 'caller_ref_2', 'inline_ref_1', 'inline_ref_2', 'called_by_inline_ref', 'caller_trivial_1', 'caller_trivial_2', 'inline_trivial_1', 'inline_trivial_2', 'called_by_inline_trivial' ]
43         for name in functions:
44             self.source_lines[name] = line_number(self.main_source, "// In " + name + ".")
45         self.main_source_spec = lldb.SBFileSpec (self.main_source)
46
47     def do_step(self, step_type, destination_line_entry, test_stack_depth):
48         expected_stack_depth = self.thread.GetNumFrames()
49         if step_type == "into":
50             expected_stack_depth += 1
51             self.thread.StepInto()
52         elif step_type == "out":
53             expected_stack_depth -= 1
54             self.thread.StepOut()
55         elif step_type == "over":
56             self.thread.StepOver()
57         else:
58             self.fail ("Unrecognized step type: " + step_type)
59
60         threads = lldbutil.get_stopped_threads (self.process, lldb.eStopReasonPlanComplete)
61         if len(threads) != 1:
62             destination_description = lldb.SBStream()
63             destination_line_entry.GetDescription(destination_description)
64             self.fail ("Failed to stop due to step " + step_type + " operation stepping to: " + destination_description.GetData())
65
66         self.thread = threads[0]
67
68         stop_line_entry = self.thread.GetFrameAtIndex(0).GetLineEntry()
69         self.assertTrue (stop_line_entry.IsValid(), "Stop line entry was not valid.")
70
71         # Don't use the line entry equal operator because we don't care about the column number.
72         stop_at_right_place = (stop_line_entry.GetFileSpec() == destination_line_entry.GetFileSpec() and stop_line_entry.GetLine() == destination_line_entry.GetLine())
73         if stop_at_right_place == False:
74             destination_description = lldb.SBStream()
75             destination_line_entry.GetDescription(destination_description)
76
77             actual_description = lldb.SBStream()
78             stop_line_entry.GetDescription(actual_description)
79
80             self.fail ("Step " + step_type + " stopped at wrong place: expected: " + destination_description.GetData() + " got: " + actual_description.GetData() + ".")
81
82         real_stack_depth = self.thread.GetNumFrames()
83
84         if test_stack_depth and real_stack_depth != expected_stack_depth:
85             destination_description = lldb.SBStream()
86             destination_line_entry.GetDescription(destination_description)
87             self.fail ("Step %s to %s got wrong number of frames, should be: %d was: %d."%(step_type, destination_description.GetData(), expected_stack_depth, real_stack_depth))
88             
89     def run_step_sequence(self, step_sequence):
90         """This function takes a list of duples instructing how to run the program.  The first element in each duple is
91            a source pattern for the target location, and the second is the operation that will take you from the current
92            source location to the target location.  It will then run all the steps in the sequence.
93            It will check that you arrived at the expected source location at each step, and that the stack depth changed 
94            correctly for the operation in the sequence."""
95
96         target_line_entry = lldb.SBLineEntry()
97         target_line_entry.SetFileSpec(self.main_source_spec)
98
99         test_stack_depth = True
100         # Work around for <rdar://problem/16363195>, the darwin unwinder seems flakey about whether it duplicates the first frame 
101         # or not, which makes counting stack depth unreliable.
102         if self.platformIsDarwin():
103             test_stack_depth = False
104
105         for step_pattern in step_sequence:
106             step_stop_line = line_number (self.main_source, step_pattern[0])
107             target_line_entry.SetLine(step_stop_line)
108             self.do_step (step_pattern[1], target_line_entry, test_stack_depth)
109         
110     def inline_stepping(self):
111         """Use Python APIs to test stepping over and hitting breakpoints."""
112         exe = os.path.join(os.getcwd(), "a.out")
113
114         target = self.dbg.CreateTarget(exe)
115         self.assertTrue(target, VALID_TARGET)
116
117         break_1_in_main = target.BreakpointCreateBySourceRegex ('// Stop here and step over to set up stepping over.', self.main_source_spec)
118         self.assertTrue(break_1_in_main, VALID_BREAKPOINT)
119
120         # Now launch the process, and do not stop at entry point.
121         self.process = target.LaunchSimple (None, None, self.get_process_working_directory())
122
123         self.assertTrue(self.process, PROCESS_IS_VALID)
124
125         # The stop reason of the thread should be breakpoint.
126         threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, break_1_in_main)
127
128         if len(threads) != 1:
129             self.fail ("Failed to stop at first breakpoint in main.")
130
131         self.thread = threads[0]
132
133         # Step over the inline_value = 0 line to get us to inline_trivial_1 called from main.  Doing it this way works
134         # around a bug in lldb where the breakpoint on the containing line of an inlined function with no return value
135         # gets set past the insertion line in the function.
136         # Then test stepping over a simple inlined function.  Note, to test all the parts of the inlined stepping
137         # the calls inline_stepping_1 and inline_stepping_2 should line up at the same address, that way we will test
138         # the "virtual" stepping.  
139         # FIXME: Put in a check to see if that is true and warn if it is not.
140
141         step_sequence = [["// At inline_trivial_1 called from main.", "over"], 
142                          ["// At first call of caller_trivial_1 in main.", "over"]]
143         self.run_step_sequence(step_sequence)
144            
145         # Now step from caller_ref_1 all the way into called_by_inline_trivial
146
147         step_sequence = [["// In caller_trivial_1.", "into"], 
148                          ["// In caller_trivial_2.", "into"], 
149                          ["// In inline_trivial_1.", "into"],
150                          ["// In inline_trivial_2.", "into"],
151                          ["// At caller_by_inline_trivial in inline_trivial_2.", "over"],
152                          ["// In called_by_inline_trivial.", "into"]]
153         self.run_step_sequence(step_sequence)
154
155         # Now run to the inline_trivial_1 just before the immediate step into inline_trivial_2:
156
157         break_2_in_main = target.BreakpointCreateBySourceRegex ('// At second call of caller_trivial_1 in main.', self.main_source_spec)
158         self.assertTrue(break_2_in_main, VALID_BREAKPOINT)
159
160         threads = lldbutil.continue_to_breakpoint (self.process, break_2_in_main)
161         self.assertTrue (len(threads) == 1, "Successfully ran to call site of second caller_trivial_1 call.")
162         self.thread = threads[0]
163         
164         step_sequence = [["// In caller_trivial_1.", "into"], 
165                          ["// In caller_trivial_2.", "into"], 
166                          ["// In inline_trivial_1.", "into"]]
167         self.run_step_sequence(step_sequence)
168
169         # Then call some trivial function, and make sure we end up back where we were in the inlined call stack:
170         
171         frame = self.thread.GetFrameAtIndex(0)
172         before_line_entry = frame.GetLineEntry()
173         value = frame.EvaluateExpression ("function_to_call()")
174         after_line_entry = frame.GetLineEntry()
175
176         self.assertTrue (before_line_entry.GetLine() == after_line_entry.GetLine(), "Line entry before and after function calls are the same.")
177
178         # Now make sure stepping OVER in the middle of the stack works, and then check finish from the inlined frame:
179
180         step_sequence = [["// At increment in inline_trivial_1.", "over"],
181                          ["// At increment in caller_trivial_2.", "out"]]
182         self.run_step_sequence(step_sequence)
183
184
185         # Now run to the place in main just before the first call to caller_ref_1:
186
187         break_3_in_main = target.BreakpointCreateBySourceRegex ('// At first call of caller_ref_1 in main.', self.main_source_spec)
188         self.assertTrue(break_3_in_main, VALID_BREAKPOINT)
189
190         threads = lldbutil.continue_to_breakpoint (self.process, break_3_in_main)
191         self.assertTrue (len(threads) == 1, "Successfully ran to call site of first caller_ref_1 call.")
192         self.thread = threads[0]
193
194         step_sequence = [["// In caller_ref_1.", "into"],
195                          ["// In caller_ref_2.", "into"],
196                          ["// In inline_ref_1.", "into"],
197                          ["// In inline_ref_2.", "into"],
198                          ["// In called_by_inline_ref.", "into"],
199                          ["// In inline_ref_2.", "out"],
200                          ["// In inline_ref_1.", "out"],
201                          ["// At increment in inline_ref_1.", "over"],
202                          ["// In caller_ref_2.", "out"],
203                          ["// At increment in caller_ref_2.", "over"]]
204         self.run_step_sequence (step_sequence)
205
206     def inline_stepping_step_over(self):
207         """Use Python APIs to test stepping over and hitting breakpoints."""
208         exe = os.path.join(os.getcwd(), "a.out")
209
210         target = self.dbg.CreateTarget(exe)
211         self.assertTrue(target, VALID_TARGET)
212
213         break_1_in_main = target.BreakpointCreateBySourceRegex ('// At second call of caller_ref_1 in main.', self.main_source_spec)
214         self.assertTrue(break_1_in_main, VALID_BREAKPOINT)
215
216         # Now launch the process, and do not stop at entry point.
217         self.process = target.LaunchSimple (None, None, self.get_process_working_directory())
218
219         self.assertTrue(self.process, PROCESS_IS_VALID)
220
221         # The stop reason of the thread should be breakpoint.
222         threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, break_1_in_main)
223
224         if len(threads) != 1:
225             self.fail ("Failed to stop at first breakpoint in main.")
226
227         self.thread = threads[0]
228
229         step_sequence = [["// In caller_ref_1.", "into"],
230                          ["// In caller_ref_2.", "into"],
231                          ["// At increment in caller_ref_2.", "over"]]
232         self.run_step_sequence (step_sequence)
233
234     def step_in_template(self):
235         """Use Python APIs to test stepping in to templated functions."""
236         exe = os.path.join(os.getcwd(), "a.out")
237
238         target = self.dbg.CreateTarget(exe)
239         self.assertTrue(target, VALID_TARGET)
240
241         break_1_in_main = target.BreakpointCreateBySourceRegex ('// Call max_value template', self.main_source_spec)
242         self.assertTrue(break_1_in_main, VALID_BREAKPOINT)
243         
244         break_2_in_main = target.BreakpointCreateBySourceRegex ('// Call max_value specialized', self.main_source_spec)
245         self.assertTrue(break_2_in_main, VALID_BREAKPOINT)
246
247         # Now launch the process, and do not stop at entry point.
248         self.process = target.LaunchSimple (None, None, self.get_process_working_directory())
249         self.assertTrue(self.process, PROCESS_IS_VALID)
250
251         # The stop reason of the thread should be breakpoint.
252         threads = lldbutil.get_threads_stopped_at_breakpoint (self.process, break_1_in_main)
253
254         if len(threads) != 1:
255             self.fail ("Failed to stop at first breakpoint in main.")
256
257         self.thread = threads[0]
258
259         step_sequence = [["// In max_value template", "into"]]
260         self.run_step_sequence(step_sequence)
261         
262         threads = lldbutil.continue_to_breakpoint (self.process, break_2_in_main)
263         self.assertEqual(len(threads), 1, "Successfully ran to call site of second caller_trivial_1 call.")
264         self.thread = threads[0]
265         
266         step_sequence = [["// In max_value specialized", "into"]]
267         self.run_step_sequence(step_sequence)