1"""
2Test lldb breakpoint ids.
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 TestCPPBreakpointLocations(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
19    def test(self):
20        self.build()
21        self.breakpoint_id_tests()
22
23    def verify_breakpoint_locations(self, target, bp_dict):
24
25        name = bp_dict['name']
26        names = bp_dict['loc_names']
27        bp = target.BreakpointCreateByName(name)
28        self.assertEquals(
29            bp.GetNumLocations(),
30            len(names),
31            "Make sure we find the right number of breakpoint locations")
32
33        bp_loc_names = list()
34        for bp_loc in bp:
35            bp_loc_names.append(bp_loc.GetAddress().GetFunction().GetName())
36
37        for name in names:
38            found = name in bp_loc_names
39            if not found:
40                print("Didn't find '%s' in: %s" % (name, bp_loc_names))
41            self.assertTrue(found, "Make sure we find all required locations")
42
43    def breakpoint_id_tests(self):
44
45        # Create a target by the debugger.
46        exe = self.getBuildArtifact("a.out")
47        target = self.dbg.CreateTarget(exe)
48        self.assertTrue(target, VALID_TARGET)
49        bp_dicts = [
50            {'name': 'func1', 'loc_names': ['a::c::func1()', 'b::c::func1()']},
51            {'name': 'func2', 'loc_names': ['a::c::func2()', 'c::d::func2()']},
52            {'name': 'func3', 'loc_names': ['a::c::func3()', 'b::c::func3()', 'c::d::func3()']},
53            {'name': 'c::func1', 'loc_names': ['a::c::func1()', 'b::c::func1()']},
54            {'name': 'c::func2', 'loc_names': ['a::c::func2()']},
55            {'name': 'c::func3', 'loc_names': ['a::c::func3()', 'b::c::func3()']},
56            {'name': 'a::c::func1', 'loc_names': ['a::c::func1()']},
57            {'name': 'b::c::func1', 'loc_names': ['b::c::func1()']},
58            {'name': 'c::d::func2', 'loc_names': ['c::d::func2()']},
59            {'name': 'a::c::func1()', 'loc_names': ['a::c::func1()']},
60            {'name': 'b::c::func1()', 'loc_names': ['b::c::func1()']},
61            {'name': 'c::d::func2()', 'loc_names': ['c::d::func2()']},
62        ]
63
64        for bp_dict in bp_dicts:
65            self.verify_breakpoint_locations(target, bp_dict)
66
67    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24764")
68    def test_destructors(self):
69        self.build()
70        exe = self.getBuildArtifact("a.out")
71        target = self.dbg.CreateTarget(exe)
72
73        # Don't skip prologue, so we can check the breakpoint address more
74        # easily
75        self.runCmd("settings set target.skip-prologue false")
76        try:
77            names = ['~c', 'c::~c', 'c::~c()']
78            loc_names = {'a::c::~c()', 'b::c::~c()'}
79            # TODO: For windows targets we should put windows mangled names
80            # here
81            symbols = [
82                '_ZN1a1cD1Ev',
83                '_ZN1a1cD2Ev',
84                '_ZN1b1cD1Ev',
85                '_ZN1b1cD2Ev']
86
87            for name in names:
88                bp = target.BreakpointCreateByName(name)
89
90                bp_loc_names = {bp_loc.GetAddress().GetFunction().GetName()
91                                for bp_loc in bp}
92                self.assertEquals(
93                    bp_loc_names,
94                    loc_names,
95                    "Breakpoint set on the correct symbol")
96
97                bp_addresses = {bp_loc.GetLoadAddress() for bp_loc in bp}
98                symbol_addresses = set()
99                for symbol in symbols:
100                    sc_list = target.FindSymbols(symbol, lldb.eSymbolTypeCode)
101                    self.assertEquals(
102                        sc_list.GetSize(), 1, "Found symbol " + symbol)
103                    symbol = sc_list.GetContextAtIndex(0).GetSymbol()
104                    symbol_addresses.add(
105                        symbol.GetStartAddress().GetLoadAddress(target))
106
107                self.assertEquals(
108                    symbol_addresses,
109                    bp_addresses,
110                    "Breakpoint set on correct address")
111        finally:
112            self.runCmd("settings clear target.skip-prologue")
113