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