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