1"""
2Testlldb Python SBFrame APIs IsInlined() and GetFunctionName().
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class InlinedFrameAPITestCase(TestBase):
15
16    def setUp(self):
17        # Call super's setUp().
18        TestBase.setUp(self)
19        # Find the line number to of function 'c'.
20        self.source = 'inlines.c'
21        self.first_stop = line_number(
22            self.source, '// This should correspond to the first break stop.')
23        self.second_stop = line_number(
24            self.source, '// This should correspond to the second break stop.')
25
26    def test_stop_at_outer_inline(self):
27        """Exercise SBFrame.IsInlined() and SBFrame.GetFunctionName()."""
28        self.build()
29        exe = self.getBuildArtifact("a.out")
30
31        # Create a target by the debugger.
32        target = self.dbg.CreateTarget(exe)
33        self.assertTrue(target, VALID_TARGET)
34
35        # Now create a breakpoint on main.c by the name of 'inner_inline'.
36        breakpoint = target.BreakpointCreateByName('inner_inline', 'a.out')
37        self.trace("breakpoint:", breakpoint)
38        self.assertTrue(breakpoint and
39                        breakpoint.GetNumLocations() > 1,
40                        VALID_BREAKPOINT)
41
42        # Now launch the process, and do not stop at the entry point.
43        process = target.LaunchSimple(
44            None, None, self.get_process_working_directory())
45
46        process = target.GetProcess()
47        self.assertState(process.GetState(), lldb.eStateStopped,
48                         PROCESS_STOPPED)
49
50        import lldbsuite.test.lldbutil as lldbutil
51        stack_traces1 = lldbutil.print_stacktraces(process, string_buffer=True)
52        if self.TraceOn():
53            print(
54                "Full stack traces when first stopped on the breakpoint 'inner_inline':")
55            print(stack_traces1)
56
57        # The first breakpoint should correspond to an inlined call frame.
58        # If it's an inlined call frame, expect to find, in the stack trace,
59        # that there is a frame which corresponds to the following call site:
60        #
61        #     outer_inline (argc);
62        #
63        thread = lldbutil.get_stopped_thread(
64            process, lldb.eStopReasonBreakpoint)
65        self.assertIsNotNone(thread)
66
67        frame0 = thread.GetFrameAtIndex(0)
68        if frame0.IsInlined():
69            filename = frame0.GetLineEntry().GetFileSpec().GetFilename()
70            self.assertEqual(filename, self.source)
71            self.expect(
72                stack_traces1, "First stop at %s:%d" %
73                (self.source, self.first_stop), exe=False, substrs=[
74                    '%s:%d' %
75                    (self.source, self.first_stop)])
76
77            # Expect to break again for the second time.
78            process.Continue()
79            self.assertState(process.GetState(), lldb.eStateStopped,
80                             PROCESS_STOPPED)
81            stack_traces2 = lldbutil.print_stacktraces(
82                process, string_buffer=True)
83            if self.TraceOn():
84                print(
85                    "Full stack traces when stopped on the breakpoint 'inner_inline' for the second time:")
86                print(stack_traces2)
87                self.expect(
88                    stack_traces2, "Second stop at %s:%d" %
89                    (self.source, self.second_stop), exe=False, substrs=[
90                        '%s:%d' %
91                        (self.source, self.second_stop)])
92