xref: /llvm-project/lldb/test/API/functionalities/breakpoint/source_regexp/TestSourceRegexBreakpoints.py (revision 4cc8f2a017c76af25234afc7c380550e9c93135c)
1"""
2Test lldb breakpoint setting by source regular expression.
3This test just tests the source file & function restrictions.
4"""
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class TestSourceRegexBreakpoints(TestBase):
14
15    def test_location(self):
16        self.build()
17        self.source_regex_locations()
18
19    def test_restrictions(self):
20        self.build()
21        self.source_regex_restrictions()
22
23    def source_regex_locations(self):
24        """ Test that restricting source expressions to files & to functions. """
25        # Create a target by the debugger.
26        exe = self.getBuildArtifact("a.out")
27        target = self.dbg.CreateTarget(exe)
28        self.assertTrue(target, VALID_TARGET)
29
30        # First look just in main:
31        target_files = lldb.SBFileSpecList()
32        target_files.Append(lldb.SBFileSpec("a.c"))
33
34        func_names = lldb.SBStringList()
35        func_names.AppendString("a_func")
36
37        source_regex = "Set . breakpoint here"
38        main_break = target.BreakpointCreateBySourceRegex(
39            source_regex, lldb.SBFileSpecList(), target_files, func_names)
40        num_locations = main_break.GetNumLocations()
41        self.assertEqual(
42            num_locations, 1,
43            "a.c in a_func should give one breakpoint, got %d." %
44            (num_locations))
45
46        loc = main_break.GetLocationAtIndex(0)
47        self.assertTrue(loc.IsValid(), "Got a valid location.")
48        address = loc.GetAddress()
49        self.assertTrue(
50            address.IsValid(),
51            "Got a valid address from the location.")
52
53        a_func_line = line_number("a.c", "Set A breakpoint here")
54        line_entry = address.GetLineEntry()
55        self.assertTrue(line_entry.IsValid(), "Got a valid line entry.")
56        self.assertEquals(line_entry.line, a_func_line,
57                        "Our line number matches the one lldbtest found.")
58
59    def source_regex_restrictions(self):
60        """ Test that restricting source expressions to files & to functions. """
61        # Create a target by the debugger.
62        exe = self.getBuildArtifact("a.out")
63        target = self.dbg.CreateTarget(exe)
64        self.assertTrue(target, VALID_TARGET)
65
66        # First look just in main:
67        target_files = lldb.SBFileSpecList()
68        target_files.Append(lldb.SBFileSpec("main.c"))
69        source_regex = "Set . breakpoint here"
70        main_break = target.BreakpointCreateBySourceRegex(
71            source_regex, lldb.SBFileSpecList(), target_files, lldb.SBStringList())
72
73        num_locations = main_break.GetNumLocations()
74        self.assertEqual(
75            num_locations, 2,
76            "main.c should have 2 matches, got %d." %
77            (num_locations))
78
79        # Now look in both files:
80        target_files.Append(lldb.SBFileSpec("a.c"))
81
82        main_break = target.BreakpointCreateBySourceRegex(
83            source_regex, lldb.SBFileSpecList(), target_files, lldb.SBStringList())
84
85        num_locations = main_break.GetNumLocations()
86        self.assertEqual(
87            num_locations, 4,
88            "main.c and a.c should have 4 matches, got %d." %
89            (num_locations))
90
91        # Now restrict it to functions:
92        func_names = lldb.SBStringList()
93        func_names.AppendString("main_func")
94        main_break = target.BreakpointCreateBySourceRegex(
95            source_regex, lldb.SBFileSpecList(), target_files, func_names)
96
97        num_locations = main_break.GetNumLocations()
98        self.assertEqual(
99            num_locations, 2,
100            "main_func in main.c and a.c should have 2 matches, got %d." %
101            (num_locations))
102