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