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