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