1"""
2Test newly added SBSymbol 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 SymbolAPITestCase(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 some SBSymbol 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        symbol_line1 = frame0.GetSymbol()
61        # We should have a symbol type of code.
62        self.assertEqual(symbol_line1.GetType(), lldb.eSymbolTypeCode)
63        addr_line1 = symbol_line1.GetStartAddress()
64        # And a section type of code, too.
65        self.assertTrue(addr_line1.GetSection().GetSectionType()
66                        == lldb.eSectionTypeCode)
67
68        # Continue the inferior, the breakpoint 2 should be hit.
69        process.Continue()
70        self.assertState(process.GetState(), lldb.eStateStopped)
71        thread = lldbutil.get_stopped_thread(
72            process, lldb.eStopReasonBreakpoint)
73        self.assertTrue(
74            thread.IsValid(),
75            "There should be a thread stopped due to breakpoint condition")
76        frame0 = thread.GetFrameAtIndex(0)
77        symbol_line2 = frame0.GetSymbol()
78        # We should have a symbol type of code.
79        self.assertEqual(symbol_line2.GetType(), lldb.eSymbolTypeCode)
80        addr_line2 = symbol_line2.GetStartAddress()
81        # And a section type of code, too.
82        self.assertTrue(addr_line2.GetSection().GetSectionType()
83                        == lldb.eSectionTypeCode)
84
85        # Now verify that both addresses point to the same module.
86        if self.TraceOn():
87            print("UUID:", addr_line1.GetModule().GetUUIDString())
88        self.assertTrue(addr_line1.GetModule().GetUUIDString()
89                        == addr_line2.GetModule().GetUUIDString())
90