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    @expectedFailureAll(oslist=["windows"], bugnumber='llvm.org/pr21765')
28    def test(self):
29        """Exercise getting SBAddress objects, disassembly, 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.assertEqual(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        lineEntry = frame0.GetLineEntry()
63        self.assertEqual(lineEntry.GetLine(), self.line1)
64
65        address1 = lineEntry.GetStartAddress()
66        self.trace("address1:", address1)
67
68        # Now call SBTarget.ResolveSymbolContextForAddress() with address1.
69        context1 = target.ResolveSymbolContextForAddress(
70            address1, lldb.eSymbolContextEverything)
71
72        self.assertTrue(context1)
73        if self.TraceOn():
74            print("context1:", context1)
75
76        # Continue the inferior, the breakpoint 2 should be hit.
77        process.Continue()
78        self.assertEqual(process.GetState(), lldb.eStateStopped)
79        thread = lldbutil.get_stopped_thread(
80            process, lldb.eStopReasonBreakpoint)
81        self.assertTrue(
82            thread.IsValid(),
83            "There should be a thread stopped due to breakpoint condition")
84        frame0 = thread.GetFrameAtIndex(0)
85        lineEntry = frame0.GetLineEntry()
86        self.assertEqual(lineEntry.GetLine(), self.line2)
87
88        # Verify that the symbol and the function has the same address range
89        # per function 'a'.
90        symbol = context1.GetSymbol()
91        function = frame0.GetFunction()
92        self.assertTrue(symbol and function)
93
94        disasm_output = lldbutil.disassemble(target, symbol)
95        if self.TraceOn():
96            print("symbol:", symbol)
97            print("disassembly=>\n", disasm_output)
98
99        disasm_output = lldbutil.disassemble(target, function)
100        if self.TraceOn():
101            print("function:", function)
102            print("disassembly=>\n", disasm_output)
103
104        sa1 = symbol.GetStartAddress()
105        self.trace("sa1:", sa1)
106        self.trace("sa1.GetFileAddress():", hex(sa1.GetFileAddress()))
107        sa2 = function.GetStartAddress()
108        self.trace("sa2:", sa2)
109        self.trace("sa2.GetFileAddress():", hex(sa2.GetFileAddress()))
110        self.assertTrue(sa1 and sa2 and sa1 == sa2,
111                        "The two starting addresses should be the same")
112
113        from lldbsuite.test.lldbutil import get_description
114        desc1 = get_description(sa1)
115        desc2 = get_description(sa2)
116        self.assertTrue(
117            desc1 and desc2 and desc1 == desc2,
118            "SBAddress.GetDescription() API of sa1 and sa2 should return the same string")
119