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