xref: /llvm-project/lldb/test/API/python_api/function_symbol/TestSymbolAPI.py (revision 6ea1a0d4fc3823de143a288df2059b48dc01cf72)
1"""
2Test newly added SBSymbol and SBAddress APIs.
3"""
4
5import lldb
6from lldbsuite.test.decorators import *
7from lldbsuite.test.lldbtest import *
8from lldbsuite.test import lldbutil
9
10
11class SymbolAPITestCase(TestBase):
12
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number to of function 'c'.
17        self.line1 = line_number(
18            'main.c', '// Find the line number for breakpoint 1 here.')
19        self.line2 = line_number(
20            'main.c', '// Find the line number for breakpoint 2 here.')
21
22    def test(self):
23        """Exercise some SBSymbol and SBAddress APIs."""
24        self.build()
25        exe = self.getBuildArtifact("a.out")
26
27        # Create a target by the debugger.
28        target = self.dbg.CreateTarget(exe)
29        self.assertTrue(target, VALID_TARGET)
30
31        # Now create the two breakpoints inside function 'a'.
32        breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
33        breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
34        self.trace("breakpoint1:", breakpoint1)
35        self.trace("breakpoint2:", breakpoint2)
36        self.assertTrue(breakpoint1 and
37                        breakpoint1.GetNumLocations() == 1,
38                        VALID_BREAKPOINT)
39        self.assertTrue(breakpoint2 and
40                        breakpoint2.GetNumLocations() == 1,
41                        VALID_BREAKPOINT)
42
43        # Now launch the process, and do not stop at entry point.
44        process = target.LaunchSimple(
45            None, None, self.get_process_working_directory())
46        self.assertTrue(process, PROCESS_IS_VALID)
47
48        # Frame #0 should be on self.line1.
49        self.assertState(process.GetState(), lldb.eStateStopped)
50        thread = lldbutil.get_stopped_thread(
51            process, lldb.eStopReasonBreakpoint)
52        self.assertTrue(
53            thread.IsValid(),
54            "There should be a thread stopped due to breakpoint condition")
55        frame0 = thread.GetFrameAtIndex(0)
56        symbol_line1 = frame0.GetSymbol()
57        # We should have a symbol type of code.
58        self.assertEqual(symbol_line1.GetType(), lldb.eSymbolTypeCode)
59        addr_line1 = symbol_line1.GetStartAddress()
60        # And a section type of code, too.
61        self.assertEqual(addr_line1.GetSection().GetSectionType(),
62                         lldb.eSectionTypeCode)
63
64        # Continue the inferior, the breakpoint 2 should be hit.
65        process.Continue()
66        self.assertState(process.GetState(), lldb.eStateStopped)
67        thread = lldbutil.get_stopped_thread(
68            process, lldb.eStopReasonBreakpoint)
69        self.assertTrue(
70            thread.IsValid(),
71            "There should be a thread stopped due to breakpoint condition")
72        frame0 = thread.GetFrameAtIndex(0)
73        symbol_line2 = frame0.GetSymbol()
74        # We should have a symbol type of code.
75        self.assertEqual(symbol_line2.GetType(), lldb.eSymbolTypeCode)
76        addr_line2 = symbol_line2.GetStartAddress()
77        # And a section type of code, too.
78        self.assertEqual(addr_line2.GetSection().GetSectionType(),
79                         lldb.eSectionTypeCode)
80
81        # Now verify that both addresses point to the same module.
82        if self.TraceOn():
83            print("UUID:", addr_line1.GetModule().GetUUIDString())
84        self.assertEqual(addr_line1.GetModule().GetUUIDString(),
85                         addr_line2.GetModule().GetUUIDString())
86