1"""Test that backtraces can follow cross-DSO tail calls"""
2
3
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class TestCrossDSOTailCalls(TestBase):
12
13    mydir = TestBase.compute_mydir(__file__)
14
15    def setUp(self):
16        TestBase.setUp(self)
17
18    @skipIf(compiler="clang", compiler_version=['<', '10.0'])
19    @skipIf(dwarf_version=['<', '4'])
20    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr26265")
21    @expectedFailureAll(archs=['arm', 'aarch64'], bugnumber="llvm.org/PR44561")
22    def test_cross_dso_tail_calls(self):
23        self.build()
24        exe = self.getBuildArtifact("a.out")
25        target = self.dbg.CreateTarget(exe)
26        self.assertTrue(target, VALID_TARGET)
27
28        # Register our shared libraries for remote targets so they get
29        # automatically uploaded
30        environment = self.registerSharedLibrariesWithTarget(
31            target, ['One', 'Two'])
32
33        lldbutil.run_break_set_by_source_regexp(self, '// break here',
34                extra_options='-f Two.c')
35
36        process = target.LaunchSimple(
37            None, environment, self.get_process_working_directory())
38        self.assertTrue(process, PROCESS_IS_VALID)
39
40        # We should be stopped in the second dylib.
41        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
42
43        # Debug helper:
44        # self.runCmd("log enable -f /tmp/lldb.log lldb step")
45        # self.runCmd("bt")
46
47        # Check that the backtrace is what we expect:
48        #  frame #0: 0x000000010d5e5f94 libTwo.dylib`tail_called_in_b_from_b at Two.c:7:3 [opt]
49        #  frame #1: 0x000000010d5e5fa0 libTwo.dylib`tail_called_in_b_from_a [opt] [artificial]
50        #  frame #2: 0x000000010d5dcf80 libOne.dylib`helper_in_a [opt] [artificial]
51        #  frame #3: 0x000000010d5dcf79 libOne.dylib`tail_called_in_a_from_main at One.c:10:3 [opt]
52        #  frame #4: 0x000000010d5d3f80 a.out`helper [opt] [artificial]
53        #  frame #5: 0x000000010d5d3f79 a.out`main at main.c:10:3 [opt]
54        expected_frames = [
55                ("tail_called_in_b_from_b", False),
56                ("tail_called_in_b_from_a", True),
57                ("helper_in_a", True),
58                ("tail_called_in_a_from_main", False),
59                ("helper", True),
60                ("main", False)
61        ]
62        for idx, (name, is_artificial) in enumerate(expected_frames):
63            frame = thread.GetFrameAtIndex(idx)
64            self.assertIn(name, frame.GetDisplayFunctionName())
65            self.assertEqual(frame.IsArtificial(), is_artificial)
66