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    mydir = TestBase.compute_mydir(__file__)
16
17    def test_location(self):
18        self.build()
19        self.source_regex_locations()
20
21    def test_restrictions(self):
22        self.build()
23        self.source_regex_restrictions()
24
25    def source_regex_locations(self):
26        """ Test that restricting source expressions to files & to functions. """
27        # Create a target by the debugger.
28        exe = self.getBuildArtifact("a.out")
29        target = self.dbg.CreateTarget(exe)
30        self.assertTrue(target, VALID_TARGET)
31
32        # First look just in main:
33        target_files = lldb.SBFileSpecList()
34        target_files.Append(lldb.SBFileSpec("a.c"))
35
36        func_names = lldb.SBStringList()
37        func_names.AppendString("a_func")
38
39        source_regex = "Set . breakpoint here"
40        main_break = target.BreakpointCreateBySourceRegex(
41            source_regex, lldb.SBFileSpecList(), target_files, func_names)
42        num_locations = main_break.GetNumLocations()
43        self.assertTrue(
44            num_locations == 1,
45            "a.c in a_func should give one breakpoint, got %d." %
46            (num_locations))
47
48        loc = main_break.GetLocationAtIndex(0)
49        self.assertTrue(loc.IsValid(), "Got a valid location.")
50        address = loc.GetAddress()
51        self.assertTrue(
52            address.IsValid(),
53            "Got a valid address from the location.")
54
55        a_func_line = line_number("a.c", "Set A breakpoint here")
56        line_entry = address.GetLineEntry()
57        self.assertTrue(line_entry.IsValid(), "Got a valid line entry.")
58        self.assertEquals(line_entry.line, a_func_line,
59                        "Our line number matches the one lldbtest found.")
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        num_locations = main_break.GetNumLocations()
76        self.assertTrue(
77            num_locations == 2,
78            "main.c should have 2 matches, got %d." %
79            (num_locations))
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        num_locations = main_break.GetNumLocations()
88        self.assertTrue(
89            num_locations == 4,
90            "main.c and a.c should have 4 matches, got %d." %
91            (num_locations))
92
93        # Now restrict it to functions:
94        func_names = lldb.SBStringList()
95        func_names.AppendString("main_func")
96        main_break = target.BreakpointCreateBySourceRegex(
97            source_regex, lldb.SBFileSpecList(), target_files, func_names)
98
99        num_locations = main_break.GetNumLocations()
100        self.assertTrue(
101            num_locations == 2,
102            "main_func in main.c and a.c should have 2 matches, got %d." %
103            (num_locations))
104