1"""
2Test breakpoint commands for a breakpoint ID with multiple locations.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class BreakpointLocationsTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24528")
18    def test_enable(self):
19        """Test breakpoint enable/disable for a breakpoint ID with multiple locations."""
20        self.build()
21        self.breakpoint_locations_test()
22
23    def test_shadowed_cond_options(self):
24        """Test that options set on the breakpoint and location behave correctly."""
25        self.build()
26        self.shadowed_bkpt_cond_test()
27
28    def test_shadowed_command_options(self):
29        """Test that options set on the breakpoint and location behave correctly."""
30        self.build()
31        self.shadowed_bkpt_command_test()
32
33    def setUp(self):
34        # Call super's setUp().
35        TestBase.setUp(self)
36        # Find the line number to break inside main().
37        self.line = line_number('main.c', '// Set break point at this line.')
38
39    def set_breakpoint (self):
40        exe = self.getBuildArtifact("a.out")
41        target = self.dbg.CreateTarget(exe)
42        self.assertTrue(target, "Target %s is not valid"%(exe))
43
44        # This should create a breakpoint with 3 locations.
45
46        bkpt = target.BreakpointCreateByLocation("main.c", self.line)
47
48        # The breakpoint list should show 3 locations.
49        self.assertEqual(bkpt.GetNumLocations(), 3, "Wrong number of locations")
50
51        self.expect(
52            "breakpoint list -f",
53            "Breakpoint locations shown correctly",
54            substrs=[
55                "1: file = 'main.c', line = %d, exact_match = 0, locations = 3" %
56                self.line],
57            patterns=[
58                "where = a.out`func_inlined .+unresolved, hit count = 0",
59                "where = a.out`main .+\[inlined\].+unresolved, hit count = 0"])
60
61        return bkpt
62
63    def shadowed_bkpt_cond_test(self):
64        """Test that options set on the breakpoint and location behave correctly."""
65        # Breakpoint option propagation from bkpt to loc used to be done the first time
66        # a breakpoint location option was specifically set.  After that the other options
67        # on that location would stop tracking the breakpoint.  That got fixed, and this test
68        # makes sure only the option touched is affected.
69
70        bkpt = self.set_breakpoint()
71        bkpt_cond = "1 == 0"
72        bkpt.SetCondition(bkpt_cond)
73        self.assertEqual(bkpt.GetCondition(), bkpt_cond,"Successfully set condition")
74        self.assertEquals(bkpt.location[0].GetCondition(), bkpt.GetCondition(), "Conditions are the same")
75
76        # Now set a condition on the locations, make sure that this doesn't effect the bkpt:
77        bkpt_loc_1_cond = "1 == 1"
78        bkpt.location[0].SetCondition(bkpt_loc_1_cond)
79        self.assertEqual(bkpt.location[0].GetCondition(), bkpt_loc_1_cond, "Successfully changed location condition")
80        self.assertNotEqual(bkpt.GetCondition(), bkpt_loc_1_cond, "Changed location changed Breakpoint condition")
81        self.assertEqual(bkpt.location[1].GetCondition(), bkpt_cond, "Changed another location's condition")
82
83        # Now make sure that setting one options doesn't fix the value of another:
84        bkpt.SetIgnoreCount(10)
85        self.assertEqual(bkpt.GetIgnoreCount(), 10, "Set the ignore count successfully")
86        self.assertEqual(bkpt.location[0].GetIgnoreCount(), 10, "Location doesn't track top-level bkpt.")
87
88        # Now make sure resetting the condition to "" resets the tracking:
89        bkpt.location[0].SetCondition("")
90        bkpt_new_cond = "1 == 3"
91        bkpt.SetCondition(bkpt_new_cond)
92        self.assertEqual(bkpt.location[0].GetCondition(), bkpt_new_cond, "Didn't go back to tracking condition")
93
94    def shadowed_bkpt_command_test(self):
95        """Test that options set on the breakpoint and location behave correctly."""
96        # Breakpoint option propagation from bkpt to loc used to be done the first time
97        # a breakpoint location option was specifically set.  After that the other options
98        # on that location would stop tracking the breakpoint.  That got fixed, and this test
99        # makes sure only the option touched is affected.
100
101        bkpt = self.set_breakpoint()
102        commands = ["AAAAAA", "BBBBBB", "CCCCCC"]
103        str_list = lldb.SBStringList()
104        str_list.AppendList(commands, len(commands))
105
106        bkpt.SetCommandLineCommands(str_list)
107        cmd_list = lldb.SBStringList()
108        bkpt.GetCommandLineCommands(cmd_list)
109        list_size = str_list.GetSize()
110        self.assertEqual(cmd_list.GetSize() , list_size, "Added the right number of commands")
111        for i in range(0,list_size):
112            self.assertEqual(str_list.GetStringAtIndex(i), cmd_list.GetStringAtIndex(i), "Mismatched commands.")
113
114        commands = ["DDDDDD", "EEEEEE", "FFFFFF", "GGGGGG"]
115        loc_list = lldb.SBStringList()
116        loc_list.AppendList(commands, len(commands))
117        bkpt.location[1].SetCommandLineCommands(loc_list)
118        loc_cmd_list = lldb.SBStringList()
119        bkpt.location[1].GetCommandLineCommands(loc_cmd_list)
120
121        loc_list_size = loc_list.GetSize()
122
123        # Check that the location has the right commands:
124        self.assertEqual(loc_cmd_list.GetSize() , loc_list_size, "Added the right number of commands to location")
125        for i in range(0,loc_list_size):
126            self.assertEqual(loc_list.GetStringAtIndex(i), loc_cmd_list.GetStringAtIndex(i), "Mismatched commands.")
127
128        # Check that we didn't mess up the breakpoint level commands:
129        self.assertEqual(cmd_list.GetSize() , list_size, "Added the right number of commands")
130        for i in range(0,list_size):
131            self.assertEqual(str_list.GetStringAtIndex(i), cmd_list.GetStringAtIndex(i), "Mismatched commands.")
132
133        # And check we didn't mess up another location:
134        untouched_loc_cmds = lldb.SBStringList()
135        bkpt.location[0].GetCommandLineCommands(untouched_loc_cmds)
136        self.assertEqual(untouched_loc_cmds.GetSize() , 0, "Changed the wrong location")
137
138    def breakpoint_locations_test(self):
139        """Test breakpoint enable/disable for a breakpoint ID with multiple locations."""
140        self.set_breakpoint()
141
142        # The 'breakpoint disable 3.*' command should fail gracefully.
143        self.expect("breakpoint disable 3.*",
144                    "Disabling an invalid breakpoint should fail gracefully",
145                    error=True,
146                    startstr="error: '3' is not a valid breakpoint ID.")
147
148        # The 'breakpoint disable 1.*' command should disable all 3 locations.
149        self.expect(
150            "breakpoint disable 1.*",
151            "All 3 breakpoint locatons disabled correctly",
152            startstr="3 breakpoints disabled.")
153
154        # Run the program.
155        self.runCmd("run", RUN_SUCCEEDED)
156
157        # We should not stopped on any breakpoint at all.
158        self.expect("process status", "No stopping on any disabled breakpoint",
159                    patterns=["^Process [0-9]+ exited with status = 0"])
160
161        # The 'breakpoint enable 1.*' command should enable all 3 breakpoints.
162        self.expect(
163            "breakpoint enable 1.*",
164            "All 3 breakpoint locatons enabled correctly",
165            startstr="3 breakpoints enabled.")
166
167        # The 'breakpoint disable 1.1' command should disable 1 location.
168        self.expect(
169            "breakpoint disable 1.1",
170            "1 breakpoint locatons disabled correctly",
171            startstr="1 breakpoints disabled.")
172
173        # Run the program again.  We should stop on the two breakpoint
174        # locations.
175        self.runCmd("run", RUN_SUCCEEDED)
176
177        # Stopped once.
178        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
179                    substrs=["stop reason = breakpoint 1."])
180
181        # Continue the program, there should be another stop.
182        self.runCmd("process continue")
183
184        # Stopped again.
185        self.expect("thread backtrace", STOPPED_DUE_TO_BREAKPOINT,
186                    substrs=["stop reason = breakpoint 1."])
187
188        # At this point, 1.1 has a hit count of 0 and the other a hit count of
189        # 1".
190        lldbutil.check_breakpoint(self, bpno = 1, expected_locations = 3, expected_resolved_count = 2, expected_hit_count = 2)
191        lldbutil.check_breakpoint(self, bpno = 1, location_id = 1,  expected_location_resolved = False, expected_location_hit_count = 0)
192        lldbutil.check_breakpoint(self, bpno = 1, location_id = 2, expected_location_resolved = True, expected_location_hit_count = 1)
193        lldbutil.check_breakpoint(self, bpno = 1, location_id = 3, expected_location_resolved = True, expected_location_hit_count = 1)
194