1"""
2Test SB API support for identifying artificial (tail call) frames.
3"""
4
5import lldb
6import lldbsuite.test.lldbutil as lldbutil
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9
10class TestTailCallFrameSBAPI(TestBase):
11
12    @skipIf(compiler="clang", compiler_version=['<', '10.0'])
13    @skipIf(dwarf_version=['<', '4'])
14    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr26265")
15    def test_tail_call_frame_sbapi(self):
16        self.build()
17        self.do_test()
18
19    def do_test(self):
20        exe = self.getBuildArtifact("a.out")
21
22        # Create a target by the debugger.
23        target = self.dbg.CreateTarget(exe)
24        self.assertTrue(target, VALID_TARGET)
25
26        breakpoint = target.BreakpointCreateBySourceRegex("break here",
27                lldb.SBFileSpec("main.cpp"))
28        self.assertTrue(breakpoint and
29                        breakpoint.GetNumLocations() == 1,
30                        VALID_BREAKPOINT)
31
32        error = lldb.SBError()
33        launch_info = target.GetLaunchInfo()
34        process = target.Launch(launch_info, error)
35        self.assertTrue(process, PROCESS_IS_VALID)
36
37        # Did we hit our breakpoint?
38        threads = lldbutil.get_threads_stopped_at_breakpoint(process,
39                breakpoint)
40        self.assertEqual(
41            len(threads), 1,
42            "There should be a thread stopped at our breakpoint")
43
44        self.assertEqual(breakpoint.GetHitCount(), 1)
45
46        thread = threads[0]
47
48        # Here's what we expect to see in the backtrace:
49        #   frame #0: ... a.out`sink() at main.cpp:13:4 [opt]
50        #   frame #1: ... a.out`func3() at main.cpp:14:1 [opt] [artificial]
51        #   frame #2: ... a.out`func2() at main.cpp:18:62 [opt]
52        #   frame #3: ... a.out`func1() at main.cpp:18:85 [opt] [artificial]
53        #   frame #4: ... a.out`main at main.cpp:23:3 [opt]
54        names = ["sink", "func3", "func2", "func1", "main"]
55        artificiality = [False, True, False, True, False]
56        for idx, (name, is_artificial) in enumerate(zip(names, artificiality)):
57            frame = thread.GetFrameAtIndex(idx)
58
59            # Use a relaxed substring check because function dislpay names are
60            # platform-dependent. E.g we see "void sink(void)" on Windows, but
61            # "sink()" on Darwin. This seems like a bug -- just work around it
62            # for now.
63            self.assertIn(name, frame.GetDisplayFunctionName())
64            self.assertEqual(frame.IsArtificial(), is_artificial)
65