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