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