1"""
2Test that the language option for breakpoints works correctly
3with symbol load on-demand.
4"""
5
6
7import lldb
8from lldbsuite.test.lldbtest import *
9import lldbsuite.test.lldbutil as lldbutil
10
11
12class TestBreakpointLanguage(TestBase):
13
14    mydir = TestBase.compute_mydir(__file__)
15
16    def check_location_file(self, bp, loc, test_name):
17        bp_loc = bp.GetLocationAtIndex(loc)
18        addr = bp_loc.GetAddress()
19        comp_unit = addr.GetCompileUnit()
20        comp_name = comp_unit.GetFileSpec().GetFilename()
21        return comp_name == test_name
22
23    @skipIfWindows
24    def test_regex_breakpoint_language(self):
25        """Test that the name regex breakpoint commands obey the language filter."""
26
27        self.build()
28
29        # Load symbols on-demand
30        self.runCmd("settings set symbols.load-on-demand true")
31
32        # Create a target by the debugger.
33        exe = self.getBuildArtifact("a.out")
34        error = lldb.SBError()
35        # Don't read in dependencies so we don't come across false matches that
36        # add unwanted breakpoint hits.
37        self.target = self.dbg.CreateTarget(exe, None, None, False, error)
38        self.assertTrue(self.target, VALID_TARGET)
39
40        cpp_bp = self.target.BreakpointCreateByRegex(
41            "func_from",
42            lldb.eLanguageTypeC_plus_plus,
43            lldb.SBFileSpecList(),
44            lldb.SBFileSpecList())
45        self.assertEqual(
46            cpp_bp.GetNumLocations(), 1,
47            "Only one C++ symbol matches")
48        self.assertTrue(self.check_location_file(cpp_bp, 0, "cpp_lang.cpp"))
49
50        c_bp = self.target.BreakpointCreateByRegex(
51            "func_from",
52            lldb.eLanguageTypeC,
53            lldb.SBFileSpecList(),
54            lldb.SBFileSpecList())
55        self.assertEqual(
56            c_bp.GetNumLocations(), 1,
57            "Only one C symbol matches")
58        self.assertTrue(self.check_location_file(c_bp, 0, "c_lang.c"))
59
60        objc_bp = self.target.BreakpointCreateByRegex(
61            "func_from",
62            lldb.eLanguageTypeObjC,
63            lldb.SBFileSpecList(),
64            lldb.SBFileSpecList())
65        self.assertEqual(
66            objc_bp.GetNumLocations(), 0,
67            "No ObjC symbol matches")
68
69    @skipIfWindows
70    def test_by_name_breakpoint_language(self):
71        """Test that the name regex breakpoint commands obey the language filter."""
72
73        self.build()
74
75        # Load symbols on-demand
76        self.runCmd("settings set symbols.load-on-demand true")
77
78        # Create a target by the debugger.
79        exe = self.getBuildArtifact("a.out")
80        error = lldb.SBError()
81        # Don't read in dependencies so we don't come across false matches that
82        # add unwanted breakpoint hits.
83        self.target = self.dbg.CreateTarget(exe, None, None, False, error)
84        self.assertTrue(self.target, VALID_TARGET)
85
86        cpp_bp = self.target.BreakpointCreateByName(
87            "func_from_cpp",
88            lldb.eFunctionNameTypeAuto,
89            lldb.eLanguageTypeC_plus_plus,
90            lldb.SBFileSpecList(),
91            lldb.SBFileSpecList())
92        self.assertEqual(
93            cpp_bp.GetNumLocations(), 1,
94            "Only one C++ symbol matches")
95        self.assertTrue(self.check_location_file(cpp_bp, 0, "cpp_lang.cpp"))
96
97        no_cpp_bp = self.target.BreakpointCreateByName(
98            "func_from_c",
99            lldb.eFunctionNameTypeAuto,
100            lldb.eLanguageTypeC_plus_plus,
101            lldb.SBFileSpecList(),
102            lldb.SBFileSpecList())
103        self.assertEqual(
104            no_cpp_bp.GetNumLocations(), 0,
105            "And the C one doesn't match")
106
107        c_bp = self.target.BreakpointCreateByName(
108            "func_from_c",
109            lldb.eFunctionNameTypeAuto,
110            lldb.eLanguageTypeC,
111            lldb.SBFileSpecList(),
112            lldb.SBFileSpecList())
113        self.assertEqual(
114            c_bp.GetNumLocations(), 1,
115            "Only one C symbol matches")
116        self.assertTrue(self.check_location_file(c_bp, 0, "c_lang.c"))
117
118        no_c_bp = self.target.BreakpointCreateByName(
119            "func_from_cpp",
120            lldb.eFunctionNameTypeAuto,
121            lldb.eLanguageTypeC,
122            lldb.SBFileSpecList(),
123            lldb.SBFileSpecList())
124        self.assertEqual(
125            no_c_bp.GetNumLocations(), 0,
126            "And the C++ one doesn't match")
127
128        objc_bp = self.target.BreakpointCreateByName(
129            "func_from_cpp",
130            lldb.eFunctionNameTypeAuto,
131            lldb.eLanguageTypeObjC,
132            lldb.SBFileSpecList(),
133            lldb.SBFileSpecList())
134        self.assertEqual(
135            objc_bp.GetNumLocations(), 0,
136            "No ObjC symbol matches")
137