xref: /llvm-project/lldb/test/API/python_api/function_symbol/TestDisasmAPI.py (revision ab855530f797bbc113f75f833a4ca5711749d7c8)
1"""
2Test retrieval of SBAddress from function/symbol, disassembly, 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 DisasmAPITestCase(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.line1 = line_number(
17            "main.c", "// Find the line number for breakpoint 1 here."
18        )
19        self.line2 = line_number(
20            "main.c", "// Find the line number for breakpoint 2 here."
21        )
22
23    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr21765")
24    def test(self):
25        """Exercise getting SBAddress objects, disassembly, and SBAddress APIs."""
26        self.build()
27        exe = self.getBuildArtifact("a.out")
28
29        # Create a target by the debugger.
30        target = self.dbg.CreateTarget(exe)
31        self.assertTrue(target, VALID_TARGET)
32
33        # Now create the two breakpoints inside function 'a'.
34        breakpoint1 = target.BreakpointCreateByLocation("main.c", self.line1)
35        breakpoint2 = target.BreakpointCreateByLocation("main.c", self.line2)
36        self.trace("breakpoint1:", breakpoint1)
37        self.trace("breakpoint2:", breakpoint2)
38        self.assertTrue(
39            breakpoint1 and breakpoint1.GetNumLocations() == 1, VALID_BREAKPOINT
40        )
41        self.assertTrue(
42            breakpoint2 and breakpoint2.GetNumLocations() == 1, VALID_BREAKPOINT
43        )
44
45        # Now launch the process, and do not stop at entry point.
46        process = target.LaunchSimple(None, None, self.get_process_working_directory())
47        self.assertTrue(process, PROCESS_IS_VALID)
48
49        # Frame #0 should be on self.line1.
50        self.assertState(process.GetState(), lldb.eStateStopped)
51        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
52        self.assertTrue(
53            thread.IsValid(),
54            "There should be a thread stopped due to breakpoint condition",
55        )
56        frame0 = thread.GetFrameAtIndex(0)
57        lineEntry = frame0.GetLineEntry()
58        self.assertEqual(lineEntry.GetLine(), self.line1)
59
60        address1 = lineEntry.GetStartAddress()
61        self.trace("address1:", address1)
62
63        # Now call SBTarget.ResolveSymbolContextForAddress() with address1.
64        context1 = target.ResolveSymbolContextForAddress(
65            address1, lldb.eSymbolContextEverything
66        )
67
68        self.assertTrue(context1)
69        if self.TraceOn():
70            print("context1:", context1)
71
72        # Continue the inferior, the breakpoint 2 should be hit.
73        process.Continue()
74        self.assertState(process.GetState(), lldb.eStateStopped)
75        thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
76        self.assertTrue(
77            thread.IsValid(),
78            "There should be a thread stopped due to breakpoint condition",
79        )
80        frame0 = thread.GetFrameAtIndex(0)
81        lineEntry = frame0.GetLineEntry()
82        self.assertEqual(lineEntry.GetLine(), self.line2)
83
84        # Verify that the symbol and the function has the same address range
85        # per function 'a'.
86        symbol = context1.GetSymbol()
87        function = frame0.GetFunction()
88        self.assertTrue(symbol and function)
89
90        disasm_output = lldbutil.disassemble(target, symbol)
91        if self.TraceOn():
92            print("symbol:", symbol)
93            print("disassembly=>\n", disasm_output)
94
95        disasm_output = lldbutil.disassemble(target, function)
96        if self.TraceOn():
97            print("function:", function)
98            print("disassembly=>\n", disasm_output)
99
100        sa1 = symbol.GetStartAddress()
101        self.trace("sa1:", sa1)
102        self.trace("sa1.GetFileAddress():", hex(sa1.GetFileAddress()))
103        sa2 = function.GetStartAddress()
104        self.trace("sa2:", sa2)
105        self.trace("sa2.GetFileAddress():", hex(sa2.GetFileAddress()))
106        self.assertTrue(
107            sa1 and sa2 and sa1 == sa2, "The two starting addresses should be the same"
108        )
109
110        from lldbsuite.test.lldbutil import get_description
111
112        desc1 = get_description(sa1)
113        desc2 = get_description(sa2)
114        self.assertTrue(
115            desc1 and desc2 and desc1 == desc2,
116            "SBAddress.GetDescription() API of sa1 and sa2 should return the same string",
117        )
118