1"""
2Test SBSymbolContext APIs.
3"""
4
5from __future__ import print_function
6
7
8
9import lldb
10from lldbsuite.test.decorators import *
11from lldbsuite.test.lldbtest import *
12from lldbsuite.test import lldbutil
13
14
15class SymbolContextAPITestCase(TestBase):
16
17    mydir = TestBase.compute_mydir(__file__)
18
19    def setUp(self):
20        # Call super's setUp().
21        TestBase.setUp(self)
22        # Find the line number to of function 'c'.
23        self.line = line_number(
24            'main.c', '// Find the line number of function "c" here.')
25
26    @add_test_categories(['pyapi'])
27    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
28    def test(self):
29        """Exercise SBSymbolContext API extensively."""
30        self.build()
31        exe = self.getBuildArtifact("a.out")
32
33        # Create a target by the debugger.
34        target = self.dbg.CreateTarget(exe)
35        self.assertTrue(target, VALID_TARGET)
36
37        # Now create a breakpoint on main.c by name 'c'.
38        breakpoint = target.BreakpointCreateByName('c', 'a.out')
39        #print("breakpoint:", breakpoint)
40        self.assertTrue(breakpoint and
41                        breakpoint.GetNumLocations() == 1,
42                        VALID_BREAKPOINT)
43
44        # Now launch the process, and do not stop at entry point.
45        process = target.LaunchSimple(
46            None, None, self.get_process_working_directory())
47        self.assertTrue(process, PROCESS_IS_VALID)
48
49        # Frame #0 should be on self.line.
50        from lldbsuite.test.lldbutil import get_stopped_thread
51        thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
52        self.assertTrue(
53            thread.IsValid(),
54            "There should be a thread stopped due to breakpoint")
55        frame0 = thread.GetFrameAtIndex(0)
56        self.assertTrue(frame0.GetLineEntry().GetLine() == self.line)
57
58        # Now get the SBSymbolContext from this frame.  We want everything. :-)
59        context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
60        self.assertTrue(context)
61
62        # Get the description of this module.
63        module = context.GetModule()
64        desc = lldbutil.get_description(module)
65        self.expect(desc, "The module should match", exe=False,
66                    substrs=[self.getBuildArtifact("a.out")])
67
68        compileUnit = context.GetCompileUnit()
69        self.expect(
70            str(compileUnit),
71            "The compile unit should match",
72            exe=False,
73            substrs=[self.getSourcePath('main.c')])
74
75        function = context.GetFunction()
76        self.assertTrue(function)
77        #print("function:", function)
78
79        block = context.GetBlock()
80        self.assertTrue(block)
81        #print("block:", block)
82
83        lineEntry = context.GetLineEntry()
84        #print("line entry:", lineEntry)
85        self.expect(
86            lineEntry.GetFileSpec().GetDirectory(),
87            "The line entry should have the correct directory",
88            exe=False,
89            substrs=[self.mydir])
90        self.expect(
91            lineEntry.GetFileSpec().GetFilename(),
92            "The line entry should have the correct filename",
93            exe=False,
94            substrs=['main.c'])
95        self.assertTrue(lineEntry.GetLine() == self.line,
96                        "The line entry's line number should match ")
97
98        symbol = context.GetSymbol()
99        self.assertTrue(
100            function.GetName() == symbol.GetName() and symbol.GetName() == 'c',
101            "The symbol name should be 'c'")
102
103        sc_list = lldb.SBSymbolContextList()
104        sc_list.Append(context)
105        self.assertEqual(len(sc_list), 1)
106        for sc in sc_list:
107            self.assertEqual(lineEntry, sc.GetLineEntry())
108