1"""Test that backtraces can follow cross-object tail calls"""
2
3
4import lldb
5from lldbsuite.test.decorators import *
6from lldbsuite.test.lldbtest import *
7from lldbsuite.test import lldbutil
8
9
10class TestCrossObjectTailCalls(TestBase):
11    @skipIf(compiler="clang", compiler_version=["<", "10.0"])
12    @skipIf(dwarf_version=["<", "4"])
13    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr26265")
14    @expectedFailureAll(archs=["arm", "aarch64"], bugnumber="llvm.org/PR44561")
15    def test_cross_object_tail_calls(self):
16        self.build()
17        exe = self.getBuildArtifact("a.out")
18        target = self.dbg.CreateTarget(exe)
19        self.assertTrue(target, VALID_TARGET)
20
21        lldbutil.run_break_set_by_source_regexp(
22            self, "// break here", extra_options="-f Two.c"
23        )
24
25        process = target.LaunchSimple(None, None, self.get_process_working_directory())
26        self.assertTrue(process, PROCESS_IS_VALID)
27
28        # We should be stopped in the second dylib.
29        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
30
31        # Debug helper:
32        # self.runCmd("log enable -f /tmp/lldb.log lldb step")
33        # self.runCmd("bt")
34
35        # Check that the backtrace is what we expect:
36        #  frame #0: 0x000000010be73f94 a.out`tail_called_in_b_from_b at Two.c:7:3 [opt]
37        #  frame #1: 0x000000010be73fa0 a.out`tail_called_in_b_from_a at Two.c:8:1 [opt] [artificial]
38        #  frame #2: 0x000000010be73f80 a.out`helper_in_a at One.c:11:1 [opt] [artificial]
39        #  frame #3: 0x000000010be73f79 a.out`tail_called_in_a_from_main at One.c:10:3 [opt]
40        #  frame #4: 0x000000010be73f60 a.out`helper at main.c:11:3 [opt] [artificial]
41        #  frame #5: 0x000000010be73f59 a.out`main at main.c:10:3 [opt]
42        expected_frames = [
43            ("tail_called_in_b_from_b", False),
44            ("tail_called_in_b_from_a", True),
45            ("helper_in_a", True),
46            ("tail_called_in_a_from_main", False),
47            ("helper", True),
48            ("main", False),
49        ]
50        for idx, (name, is_artificial) in enumerate(expected_frames):
51            frame = thread.GetFrameAtIndex(idx)
52            self.assertIn(name, frame.GetDisplayFunctionName())
53            self.assertEqual(frame.IsArtificial(), is_artificial)
54