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    @skipIfReproducer # FIXME: Unexpected packet during (passive) replay
26    def test(self):
27        """Exercise SBSymbolContext API extensively."""
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 name 'c'.
36        breakpoint = target.BreakpointCreateByName('c', exe)
37        self.assertTrue(breakpoint and breakpoint.GetNumLocations() == 1,
38                        VALID_BREAKPOINT)
39
40        # Now launch the process, and do not stop at entry point.
41        process = target.LaunchSimple(None, None,
42                                      self.get_process_working_directory())
43        self.assertTrue(process, PROCESS_IS_VALID)
44
45        # Frame #0 should be on self.line.
46        thread = lldbutil.get_stopped_thread(process,
47                                             lldb.eStopReasonBreakpoint)
48        self.assertTrue(thread.IsValid(),
49                        "There should be a thread stopped due to breakpoint")
50        frame0 = thread.GetFrameAtIndex(0)
51        self.assertEqual(frame0.GetLineEntry().GetLine(), self.line)
52
53        # Now get the SBSymbolContext from this frame.  We want everything. :-)
54        context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
55        self.assertTrue(context)
56
57        # Get the description of this module.
58        module = context.GetModule()
59        desc = lldbutil.get_description(module)
60        self.expect(desc, "The module should match", exe=False, substrs=[exe])
61
62        compileUnit = context.GetCompileUnit()
63        self.expect(str(compileUnit),
64                    "The compile unit should match",
65                    exe=False,
66                    substrs=[self.getSourcePath('main.c')])
67
68        function = context.GetFunction()
69        self.assertTrue(function)
70
71        block = context.GetBlock()
72        self.assertTrue(block)
73
74        lineEntry = context.GetLineEntry()
75        self.expect(lineEntry.GetFileSpec().GetDirectory(),
76                    "The line entry should have the correct directory",
77                    exe=False,
78                    substrs=[self.mydir])
79        self.expect(lineEntry.GetFileSpec().GetFilename(),
80                    "The line entry should have the correct filename",
81                    exe=False,
82                    substrs=['main.c'])
83        self.assertEqual(lineEntry.GetLine(), self.line,
84                        "The line entry's line number should match ")
85
86        symbol = context.GetSymbol()
87        self.assertTrue(
88            function.GetName() == symbol.GetName() and symbol.GetName() == 'c',
89            "The symbol name should be 'c'")
90
91        sc_list = lldb.SBSymbolContextList()
92        sc_list.Append(context)
93        self.assertEqual(len(sc_list), 1)
94        for sc in sc_list:
95            self.assertEqual(lineEntry, sc.GetLineEntry())
96