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