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