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