xref: /llvm-project/lldb/test/API/python_api/symbol-context/TestSymbolContext.py (revision ab05d9134d18db34501985a01fbfc02609767587)
199451b44SJordan Rupprecht"""
299451b44SJordan RupprechtTest SBSymbolContext APIs.
399451b44SJordan Rupprecht"""
499451b44SJordan Rupprecht
599451b44SJordan Rupprechtimport lldb
699451b44SJordan Rupprechtfrom lldbsuite.test.decorators import *
799451b44SJordan Rupprechtfrom lldbsuite.test.lldbtest import *
899451b44SJordan Rupprechtfrom lldbsuite.test import lldbutil
999451b44SJordan Rupprecht
1099451b44SJordan Rupprecht
1199451b44SJordan Rupprechtclass SymbolContextAPITestCase(TestBase):
1299451b44SJordan Rupprecht    def setUp(self):
1399451b44SJordan Rupprecht        # Call super's setUp().
1499451b44SJordan Rupprecht        TestBase.setUp(self)
1599451b44SJordan Rupprecht        # Find the line number to of function 'c'.
1699451b44SJordan Rupprecht        self.line = line_number(
172238dcc3SJonas Devlieghere            "main.c", '// Find the line number of function "c" here.'
182238dcc3SJonas Devlieghere        )
1999451b44SJordan Rupprecht
20*ab05d913Stcwg    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24778")
2199451b44SJordan Rupprecht    def test(self):
2299451b44SJordan Rupprecht        """Exercise SBSymbolContext API extensively."""
2399451b44SJordan Rupprecht        self.build()
2499451b44SJordan Rupprecht        exe = self.getBuildArtifact("a.out")
2599451b44SJordan Rupprecht
2699451b44SJordan Rupprecht        # Create a target by the debugger.
2799451b44SJordan Rupprecht        target = self.dbg.CreateTarget(exe)
2899451b44SJordan Rupprecht        self.assertTrue(target, VALID_TARGET)
2999451b44SJordan Rupprecht
3099451b44SJordan Rupprecht        # Now create a breakpoint on main.c by name 'c'.
312238dcc3SJonas Devlieghere        breakpoint = target.BreakpointCreateByName("c", exe)
322238dcc3SJonas Devlieghere        self.assertTrue(
332238dcc3SJonas Devlieghere            breakpoint and breakpoint.GetNumLocations() == 1, VALID_BREAKPOINT
342238dcc3SJonas Devlieghere        )
3599451b44SJordan Rupprecht
3699451b44SJordan Rupprecht        # Now launch the process, and do not stop at entry point.
372238dcc3SJonas Devlieghere        process = target.LaunchSimple(None, None, self.get_process_working_directory())
3899451b44SJordan Rupprecht        self.assertTrue(process, PROCESS_IS_VALID)
3999451b44SJordan Rupprecht
4099451b44SJordan Rupprecht        # Frame #0 should be on self.line.
412238dcc3SJonas Devlieghere        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
422238dcc3SJonas Devlieghere        self.assertTrue(
432238dcc3SJonas Devlieghere            thread.IsValid(), "There should be a thread stopped due to breakpoint"
442238dcc3SJonas Devlieghere        )
4599451b44SJordan Rupprecht        frame0 = thread.GetFrameAtIndex(0)
46619e2e09SDave Lee        self.assertEqual(frame0.GetLineEntry().GetLine(), self.line)
4799451b44SJordan Rupprecht
4899451b44SJordan Rupprecht        # Now get the SBSymbolContext from this frame.  We want everything. :-)
4999451b44SJordan Rupprecht        context = frame0.GetSymbolContext(lldb.eSymbolContextEverything)
5099451b44SJordan Rupprecht        self.assertTrue(context)
5199451b44SJordan Rupprecht
5299451b44SJordan Rupprecht        # Get the description of this module.
5399451b44SJordan Rupprecht        module = context.GetModule()
5499451b44SJordan Rupprecht        desc = lldbutil.get_description(module)
559e391d4fSJonas Devlieghere        self.expect(desc, "The module should match", exe=False, substrs=[exe])
5699451b44SJordan Rupprecht
5799451b44SJordan Rupprecht        compileUnit = context.GetCompileUnit()
582238dcc3SJonas Devlieghere        self.expect(
592238dcc3SJonas Devlieghere            str(compileUnit),
6099451b44SJordan Rupprecht            "The compile unit should match",
6199451b44SJordan Rupprecht            exe=False,
622238dcc3SJonas Devlieghere            substrs=[self.getSourcePath("main.c")],
632238dcc3SJonas Devlieghere        )
6499451b44SJordan Rupprecht
6599451b44SJordan Rupprecht        function = context.GetFunction()
6699451b44SJordan Rupprecht        self.assertTrue(function)
6799451b44SJordan Rupprecht
6899451b44SJordan Rupprecht        block = context.GetBlock()
6999451b44SJordan Rupprecht        self.assertTrue(block)
7099451b44SJordan Rupprecht
7199451b44SJordan Rupprecht        lineEntry = context.GetLineEntry()
722238dcc3SJonas Devlieghere        self.expect(
732238dcc3SJonas Devlieghere            lineEntry.GetFileSpec().GetDirectory(),
7499451b44SJordan Rupprecht            "The line entry should have the correct directory",
7599451b44SJordan Rupprecht            exe=False,
762238dcc3SJonas Devlieghere            substrs=[self.mydir],
772238dcc3SJonas Devlieghere        )
782238dcc3SJonas Devlieghere        self.expect(
792238dcc3SJonas Devlieghere            lineEntry.GetFileSpec().GetFilename(),
8099451b44SJordan Rupprecht            "The line entry should have the correct filename",
8199451b44SJordan Rupprecht            exe=False,
822238dcc3SJonas Devlieghere            substrs=["main.c"],
832238dcc3SJonas Devlieghere        )
842238dcc3SJonas Devlieghere        self.assertEqual(
852238dcc3SJonas Devlieghere            lineEntry.GetLine(), self.line, "The line entry's line number should match "
862238dcc3SJonas Devlieghere        )
8799451b44SJordan Rupprecht
8899451b44SJordan Rupprecht        symbol = context.GetSymbol()
8999451b44SJordan Rupprecht        self.assertTrue(
902238dcc3SJonas Devlieghere            function.GetName() == symbol.GetName() and symbol.GetName() == "c",
912238dcc3SJonas Devlieghere            "The symbol name should be 'c'",
922238dcc3SJonas Devlieghere        )
9399451b44SJordan Rupprecht
9499451b44SJordan Rupprecht        sc_list = lldb.SBSymbolContextList()
9599451b44SJordan Rupprecht        sc_list.Append(context)
9699451b44SJordan Rupprecht        self.assertEqual(len(sc_list), 1)
9799451b44SJordan Rupprecht        for sc in sc_list:
9899451b44SJordan Rupprecht            self.assertEqual(lineEntry, sc.GetLineEntry())
99