5 from __future__ import print_function
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
17 class BreakpointNames(TestBase):
19 mydir = TestBase.compute_mydir(__file__)
20 NO_DEBUG_INFO_TESTCASE = True
22 @add_test_categories(['pyapi'])
23 def test_setting_names(self):
24 """Use Python APIs to test that we can set breakpoint names."""
29 def test_illegal_names(self):
30 """Use Python APIs to test that we don't allow illegal names."""
33 self.do_check_illegal_names()
35 def test_using_names(self):
36 """Use Python APIs to test that operations on names works correctly."""
39 self.do_check_using_names()
41 def test_configuring_names(self):
42 """Use Python APIs to test that configuring options on breakpoint names works correctly."""
44 self.make_a_dummy_name()
46 self.do_check_configuring_names()
48 def test_configuring_permissions_sb(self):
49 """Use Python APIs to test that configuring permissions on names works correctly."""
52 self.do_check_configuring_permissions_sb()
54 def test_configuring_permissions_cli(self):
55 """Use Python APIs to test that configuring permissions on names works correctly."""
58 self.do_check_configuring_permissions_cli()
60 def setup_target(self):
61 exe = self.getBuildArtifact("a.out")
63 # Create a targets we are making breakpoint in and copying to:
64 self.target = self.dbg.CreateTarget(exe)
65 self.assertTrue(self.target, VALID_TARGET)
66 self.main_file_spec = lldb.SBFileSpec(os.path.join(self.getSourceDir(), "main.c"))
68 def check_name_in_target(self, bkpt_name):
69 name_list = lldb.SBStringList()
70 self.target.GetBreakpointNames(name_list)
72 for name in name_list:
76 self.assertTrue(found_it, "Didn't find the name %s in the target's name list:"%(bkpt_name))
79 # Call super's setUp().
82 # These are the settings we're going to be putting into names & breakpoints:
83 self.bp_name_string = "ABreakpoint"
84 self.is_one_shot = True
85 self.ignore_count = 1000
86 self.condition = "1 == 2"
87 self.auto_continue = True
90 self.thread_name = "Fooey"
91 self.queue_name = "Blooey"
92 self.cmd_list = lldb.SBStringList()
93 self.cmd_list.AppendString("frame var")
94 self.cmd_list.AppendString("bt")
95 self.help_string = "I do something interesting"
98 def do_check_names(self):
99 """Use Python APIs to check that we can set & retrieve breakpoint names"""
100 bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
101 bkpt_name = "ABreakpoint"
102 other_bkpt_name = "_AnotherBreakpoint"
104 # Add a name and make sure we match it:
105 success = bkpt.AddName(bkpt_name)
106 self.assertTrue(success, "We couldn't add a legal name to a breakpoint.")
108 matches = bkpt.MatchesName(bkpt_name)
109 self.assertTrue(matches, "We didn't match the name we just set")
111 # Make sure we don't match irrelevant names:
112 matches = bkpt.MatchesName("NotABreakpoint")
113 self.assertTrue(not matches, "We matched a name we didn't set.")
115 # Make sure the name is also in the target:
116 self.check_name_in_target(bkpt_name)
118 # Add another name, make sure that works too:
119 bkpt.AddName(other_bkpt_name)
121 matches = bkpt.MatchesName(bkpt_name)
122 self.assertTrue(matches, "Adding a name means we didn't match the name we just set")
123 self.check_name_in_target(other_bkpt_name)
125 # Remove the name and make sure we no longer match it:
126 bkpt.RemoveName(bkpt_name)
127 matches = bkpt.MatchesName(bkpt_name)
128 self.assertTrue(not matches,"We still match a name after removing it.")
130 # Make sure the name list has the remaining name:
131 name_list = lldb.SBStringList()
132 bkpt.GetNames(name_list)
133 num_names = name_list.GetSize()
134 self.assertTrue(num_names == 1, "Name list has %d items, expected 1."%(num_names))
136 name = name_list.GetStringAtIndex(0)
137 self.assertTrue(name == other_bkpt_name, "Remaining name was: %s expected %s."%(name, other_bkpt_name))
139 def do_check_illegal_names(self):
140 """Use Python APIs to check that we reject illegal names."""
141 bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
142 bad_names = ["-CantStartWithADash",
143 "1CantStartWithANumber",
144 "^CantStartWithNonAlpha",
147 for bad_name in bad_names:
148 success = bkpt.AddName(bad_name)
149 self.assertTrue(not success,"We allowed an illegal name: %s"%(bad_name))
150 bp_name = lldb.SBBreakpointName(self.target, bad_name)
151 self.assertFalse(bp_name.IsValid(), "We made a breakpoint name with an illegal name: %s"%(bad_name));
153 retval =lldb.SBCommandReturnObject()
154 self.dbg.GetCommandInterpreter().HandleCommand("break set -n whatever -N '%s'"%(bad_name), retval)
155 self.assertTrue(not retval.Succeeded(), "break set succeeded with: illegal name: %s"%(bad_name))
157 def do_check_using_names(self):
158 """Use Python APIs to check names work in place of breakpoint ID's."""
160 bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
161 bkpt_name = "ABreakpoint"
162 other_bkpt_name= "_AnotherBreakpoint"
164 # Add a name and make sure we match it:
165 success = bkpt.AddName(bkpt_name)
166 self.assertTrue(success, "We couldn't add a legal name to a breakpoint.")
168 bkpts = lldb.SBBreakpointList(self.target)
169 self.target.FindBreakpointsByName(bkpt_name, bkpts)
171 self.assertTrue(bkpts.GetSize() == 1, "One breakpoint matched.")
172 found_bkpt = bkpts.GetBreakpointAtIndex(0)
173 self.assertTrue(bkpt.GetID() == found_bkpt.GetID(),"The right breakpoint.")
175 retval = lldb.SBCommandReturnObject()
176 self.dbg.GetCommandInterpreter().HandleCommand("break disable %s"%(bkpt_name), retval)
177 self.assertTrue(retval.Succeeded(), "break disable failed with: %s."%(retval.GetError()))
178 self.assertTrue(not bkpt.IsEnabled(), "We didn't disable the breakpoint.")
180 # Also make sure we don't apply commands to non-matching names:
181 self.dbg.GetCommandInterpreter().HandleCommand("break modify --one-shot 1 %s"%(other_bkpt_name), retval)
182 self.assertTrue(retval.Succeeded(), "break modify failed with: %s."%(retval.GetError()))
183 self.assertTrue(not bkpt.IsOneShot(), "We applied one-shot to the wrong breakpoint.")
185 def check_option_values(self, bp_object):
186 self.assertEqual(bp_object.IsOneShot(), self.is_one_shot, "IsOneShot")
187 self.assertEqual(bp_object.GetIgnoreCount(), self.ignore_count, "IgnoreCount")
188 self.assertEqual(bp_object.GetCondition(), self.condition, "Condition")
189 self.assertEqual(bp_object.GetAutoContinue(), self.auto_continue, "AutoContinue")
190 self.assertEqual(bp_object.GetThreadID(), self.tid, "Thread ID")
191 self.assertEqual(bp_object.GetThreadIndex(), self.tidx, "Thread Index")
192 self.assertEqual(bp_object.GetThreadName(), self.thread_name, "Thread Name")
193 self.assertEqual(bp_object.GetQueueName(), self.queue_name, "Queue Name")
194 set_cmds = lldb.SBStringList()
195 bp_object.GetCommandLineCommands(set_cmds)
196 self.assertEqual(set_cmds.GetSize(), self.cmd_list.GetSize(), "Size of command line commands")
197 for idx in range(0, set_cmds.GetSize()):
198 self.assertEqual(self.cmd_list.GetStringAtIndex(idx), set_cmds.GetStringAtIndex(idx), "Command %d"%(idx))
200 def make_a_dummy_name(self):
201 "This makes a breakpoint name in the dummy target to make sure it gets copied over"
203 dummy_target = self.dbg.GetDummyTarget()
204 self.assertTrue(dummy_target.IsValid(), "Dummy target was not valid.")
207 self.dbg.GetDummyTarget().DeleteBreakpointName(self.bp_name_string)
209 # Execute the cleanup function during test case tear down.
210 self.addTearDownHook(cleanup)
212 # Now find it in the dummy target, and make sure these settings took:
213 bp_name = lldb.SBBreakpointName(dummy_target, self.bp_name_string)
214 # Make sure the name is right:
215 self.assertTrue (bp_name.GetName() == self.bp_name_string, "Wrong bp_name: %s"%(bp_name.GetName()))
216 bp_name.SetOneShot(self.is_one_shot)
217 bp_name.SetIgnoreCount(self.ignore_count)
218 bp_name.SetCondition(self.condition)
219 bp_name.SetAutoContinue(self.auto_continue)
220 bp_name.SetThreadID(self.tid)
221 bp_name.SetThreadIndex(self.tidx)
222 bp_name.SetThreadName(self.thread_name)
223 bp_name.SetQueueName(self.queue_name)
224 bp_name.SetCommandLineCommands(self.cmd_list)
226 # Now look it up again, and make sure it got set correctly.
227 bp_name = lldb.SBBreakpointName(dummy_target, self.bp_name_string)
228 self.assertTrue(bp_name.IsValid(), "Failed to make breakpoint name.")
229 self.check_option_values(bp_name)
231 def do_check_configuring_names(self):
232 """Use Python APIs to check that configuring breakpoint names works correctly."""
233 other_bp_name_string = "AnotherBreakpointName"
234 cl_bp_name_string = "CLBreakpointName"
236 # Now find the version copied in from the dummy target, and make sure these settings took:
237 bp_name = lldb.SBBreakpointName(self.target, self.bp_name_string)
238 self.assertTrue(bp_name.IsValid(), "Failed to make breakpoint name.")
239 self.check_option_values(bp_name)
241 # Now add this name to a breakpoint, and make sure it gets configured properly
242 bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
243 success = bkpt.AddName(self.bp_name_string)
244 self.assertTrue(success, "Couldn't add this name to the breakpoint")
245 self.check_option_values(bkpt)
247 # Now make a name from this breakpoint, and make sure the new name is properly configured:
248 new_name = lldb.SBBreakpointName(bkpt, other_bp_name_string)
249 self.assertTrue(new_name.IsValid(), "Couldn't make a valid bp_name from a breakpoint.")
250 self.check_option_values(bkpt)
252 # Now change the name's option and make sure it gets propagated to
254 new_auto_continue = not self.auto_continue
255 bp_name.SetAutoContinue(new_auto_continue)
256 self.assertEqual(bp_name.GetAutoContinue(), new_auto_continue, "Couldn't change auto-continue on the name")
257 self.assertEqual(bkpt.GetAutoContinue(), new_auto_continue, "Option didn't propagate to the breakpoint.")
259 # Now make this same breakpoint name - but from the command line
260 cmd_str = "breakpoint name configure %s -o %d -i %d -c '%s' -G %d -t %d -x %d -T '%s' -q '%s' -H '%s'"%(cl_bp_name_string,
270 for cmd in self.cmd_list:
271 cmd_str += " -C '%s'"%(cmd)
273 self.runCmd(cmd_str, check=True)
274 # Now look up this name again and check its options:
275 cl_name = lldb.SBBreakpointName(self.target, cl_bp_name_string)
276 self.check_option_values(cl_name)
277 # Also check the help string:
278 self.assertEqual(self.help_string, cl_name.GetHelpString(), "Help string didn't match")
279 # Change the name and make sure that works:
280 new_help = "I do something even more interesting"
281 cl_name.SetHelpString(new_help)
282 self.assertEqual(new_help, cl_name.GetHelpString(), "SetHelpString didn't")
284 # We should have three names now, make sure the target can list them:
285 name_list = lldb.SBStringList()
286 self.target.GetBreakpointNames(name_list)
287 for name_string in [self.bp_name_string, other_bp_name_string, cl_bp_name_string]:
288 self.assertTrue(name_string in name_list, "Didn't find %s in names"%(name_string))
290 # Delete the name from the current target. Make sure that works and deletes the
291 # name from the breakpoint as well:
292 self.target.DeleteBreakpointName(self.bp_name_string)
294 self.target.GetBreakpointNames(name_list)
295 self.assertTrue(self.bp_name_string not in name_list, "Didn't delete %s from a real target"%(self.bp_name_string))
296 # Also make sure the name got removed from breakpoints holding it:
297 self.assertFalse(bkpt.MatchesName(self.bp_name_string), "Didn't remove the name from the breakpoint.")
299 # Test that deleting the name we injected into the dummy target works (there's also a
300 # cleanup that will do this, but that won't test the result...
301 dummy_target = self.dbg.GetDummyTarget()
302 dummy_target.DeleteBreakpointName(self.bp_name_string)
304 dummy_target.GetBreakpointNames(name_list)
305 self.assertTrue(self.bp_name_string not in name_list, "Didn't delete %s from the dummy target"%(self.bp_name_string))
306 # Also make sure the name got removed from breakpoints holding it:
307 self.assertFalse(bkpt.MatchesName(self.bp_name_string), "Didn't remove the name from the breakpoint.")
309 def check_permission_results(self, bp_name):
310 self.assertEqual(bp_name.GetAllowDelete(), False, "Didn't set allow delete.")
311 protected_bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
312 protected_id = protected_bkpt.GetID()
314 unprotected_bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
315 unprotected_id = unprotected_bkpt.GetID()
317 success = protected_bkpt.AddName(self.bp_name_string)
318 self.assertTrue(success, "Couldn't add this name to the breakpoint")
320 self.target.DisableAllBreakpoints()
321 self.assertEqual(protected_bkpt.IsEnabled(), True, "Didnt' keep breakpoint from being disabled")
322 self.assertEqual(unprotected_bkpt.IsEnabled(), False, "Protected too many breakpoints from disabling.")
324 # Try from the command line too:
325 unprotected_bkpt.SetEnabled(True)
326 result = lldb.SBCommandReturnObject()
327 self.dbg.GetCommandInterpreter().HandleCommand("break disable", result)
328 self.assertTrue(result.Succeeded())
329 self.assertEqual(protected_bkpt.IsEnabled(), True, "Didnt' keep breakpoint from being disabled")
330 self.assertEqual(unprotected_bkpt.IsEnabled(), False, "Protected too many breakpoints from disabling.")
332 self.target.DeleteAllBreakpoints()
333 bkpt = self.target.FindBreakpointByID(protected_id)
334 self.assertTrue(bkpt.IsValid(), "Didn't keep the breakpoint from being deleted.")
335 bkpt = self.target.FindBreakpointByID(unprotected_id)
336 self.assertFalse(bkpt.IsValid(), "Protected too many breakpoints from deletion.")
338 # Remake the unprotected breakpoint and try again from the command line:
339 unprotected_bkpt = self.target.BreakpointCreateByLocation(self.main_file_spec, 10)
340 unprotected_id = unprotected_bkpt.GetID()
342 self.dbg.GetCommandInterpreter().HandleCommand("break delete -f", result)
343 self.assertTrue(result.Succeeded())
344 bkpt = self.target.FindBreakpointByID(protected_id)
345 self.assertTrue(bkpt.IsValid(), "Didn't keep the breakpoint from being deleted.")
346 bkpt = self.target.FindBreakpointByID(unprotected_id)
347 self.assertFalse(bkpt.IsValid(), "Protected too many breakpoints from deletion.")
349 def do_check_configuring_permissions_sb(self):
350 bp_name = lldb.SBBreakpointName(self.target, self.bp_name_string)
352 # Make a breakpoint name with delete disallowed:
353 bp_name = lldb.SBBreakpointName(self.target, self.bp_name_string)
354 self.assertTrue(bp_name.IsValid(), "Failed to make breakpoint name for valid name.")
356 bp_name.SetAllowDelete(False)
357 bp_name.SetAllowDisable(False)
358 bp_name.SetAllowList(False)
359 self.check_permission_results(bp_name)
361 def do_check_configuring_permissions_cli(self):
362 # Make the name with the right options using the command line:
363 self.runCmd("breakpoint name configure -L 0 -D 0 -A 0 %s"%(self.bp_name_string), check=True)
364 # Now look up the breakpoint we made, and check that it works.
365 bp_name = lldb.SBBreakpointName(self.target, self.bp_name_string)
366 self.assertTrue(bp_name.IsValid(), "Didn't make a breakpoint name we could find.")
367 self.check_permission_results(bp_name)