]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-breakpoint.exp
Automatic Copyright Year update after running gdb/copyright.py
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-breakpoint.exp
1 # Copyright (C) 2010-2022 Free Software Foundation, Inc.
2 #
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12 #
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
15
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing breakpoints to Python.
18
19 load_lib gdb-python.exp
20
21 standard_testfile
22
23 set options {debug c++}
24
25 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile} ${options}] } {
26 return -1
27 }
28
29 # Skip all tests if Python scripting is not enabled.
30 if { [skip_python_tests] } { continue }
31
32 proc_with_prefix test_bkpt_basic { } {
33 global srcfile testfile hex decimal
34
35 # Start with a fresh gdb.
36 clean_restart ${testfile}
37
38 # We should start with no breakpoints.
39 gdb_test "python print (gdb.breakpoints())" "\\(\\)"
40
41 if ![runto_main] then {
42 return 0
43 }
44
45 # Now there should be one breakpoint: main.
46 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
47 "Get Breakpoint List" 0
48 gdb_test "python print (blist\[0\])" \
49 "<gdb.Breakpoint object at $hex>" "Check obj exists @main"
50 gdb_test "python print (blist\[0\].location)" \
51 "main." "Check breakpoint location @main"
52 gdb_test "python print (blist\[0\].pending)" "False" \
53 "Check pending status of main breakpoint"
54
55 set mult_line [gdb_get_line_number "Break at multiply."]
56 gdb_breakpoint ${mult_line}
57 gdb_continue_to_breakpoint "Break at multiply" \
58 ".*Break at multiply.*"
59
60 # Check that the Python breakpoint code noted the addition of a
61 # breakpoint "behind the scenes".
62 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
63 "Get Breakpoint List" 0
64 gdb_test "python print (len(blist))" \
65 "2" "Check for two breakpoints"
66 gdb_test "python print (blist\[0\])" \
67 "<gdb.Breakpoint object at $hex>" "Check obj exists @main 2"
68 gdb_test "python print (blist\[0\].location)" \
69 "main." "Check breakpoint location @main 2"
70 gdb_test "python print (blist\[1\])" \
71 "<gdb.Breakpoint object at $hex>" "Check obj exists @mult_line"
72
73 gdb_test "python print (blist\[1\].location)" \
74 "py-breakpoint\.c:${mult_line}*" \
75 "check breakpoint location @mult_line"
76
77 # Check hit and ignore counts.
78 gdb_test "python print (blist\[1\].hit_count)" \
79 "1" "Check breakpoint hit count @1"
80 gdb_py_test_silent_cmd "python blist\[1\].ignore_count = 4" \
81 "Set breakpoint hit count" 0
82 gdb_continue_to_breakpoint "Break at multiply @6" \
83 ".*Break at multiply.*"
84 gdb_test "python print (blist\[1\].hit_count)" \
85 "6" "Check breakpoint hit count @6"
86 gdb_test "print result" \
87 " = 545" "Check expected variable result after 6 iterations"
88
89 # Test breakpoint is enabled and disabled correctly..
90 gdb_breakpoint [gdb_get_line_number "Break at add."]
91 gdb_continue_to_breakpoint "Break at add 1" ".*Break at add.*"
92 gdb_test "python print (blist\[1\].enabled)" \
93 "True" "Check breakpoint enabled."
94 gdb_py_test_silent_cmd "python blist\[1\].enabled = False" \
95 "Set breakpoint disabled." 0
96 gdb_continue_to_breakpoint "Break at add 2" ".*Break at add.*"
97 gdb_py_test_silent_cmd "python blist\[1\].enabled = True" \
98 "Set breakpoint enabled." 0
99 gdb_continue_to_breakpoint "Break at multiply after re-enable" \
100 ".*Break at multiply.*"
101
102 # Test other getters and setters.
103 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
104 "Get Breakpoint List" 0
105 gdb_test "python print (blist\[1\].thread)" \
106 "None" "Check breakpoint thread"
107 gdb_test "python print (blist\[1\].type == gdb.BP_BREAKPOINT)" \
108 "True" "Check breakpoint type"
109 gdb_test "python print (blist\[0\].number)" \
110 "1" "Check breakpoint number 0"
111 gdb_test "python print (blist\[1\].number)" \
112 "2" "Check breakpoint number 1"
113 gdb_test "python print (blist\[2\].number)" \
114 "3" "Check breakpoint number 2"
115 }
116
117 proc_with_prefix test_bkpt_deletion { } {
118 global srcfile testfile hex decimal
119
120 # Start with a fresh gdb.
121 clean_restart ${testfile}
122
123 if ![runto_main] then {
124 return 0
125 }
126
127 # Test breakpoints are deleted correctly.
128 set deltst_location [gdb_get_line_number "Break at multiply."]
129 set end_location [gdb_get_line_number "Break at end."]
130 gdb_py_test_silent_cmd "python dp1 = gdb.Breakpoint (\"$deltst_location\")" \
131 "Set breakpoint" 0
132 gdb_breakpoint [gdb_get_line_number "Break at end."]
133 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
134 "Get Breakpoint List" 0
135 gdb_test "python print (len(del_list))" \
136 "3" "Number of breakpoints before delete"
137 gdb_continue_to_breakpoint "Break at multiply." \
138 ".*$srcfile:$deltst_location.*"
139 gdb_py_test_silent_cmd "python dp1.delete()" \
140 "Delete Breakpoint" 0
141 gdb_test "python print (dp1.number)" \
142 "RuntimeError: Breakpoint 2 is invalid.*" \
143 "Check breakpoint invalidated"
144 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
145 "Get Breakpoint List" 0
146 gdb_test "python print (len(del_list))" \
147 "2" "Number of breakpoints after delete"
148 gdb_continue_to_breakpoint "Break at end." \
149 ".*$srcfile:$end_location.*"
150 }
151
152 proc_with_prefix test_bkpt_cond_and_cmds { } {
153 global srcfile testfile hex decimal
154
155 # Start with a fresh gdb.
156 clean_restart ${testfile}
157
158 if ![runto_main] then {
159 return 0
160 }
161
162 # Test conditional setting.
163 set bp_location1 [gdb_get_line_number "Break at multiply."]
164 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"$bp_location1\")" \
165 "Set breakpoint" 0
166 gdb_continue_to_breakpoint "Break at multiply" \
167 ".*Break at multiply.*"
168 gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" \
169 "Set breakpoint" 0
170 gdb_test "python print (bp1.condition)" "i == 5" \
171 "Test conditional has been set"
172 gdb_continue_to_breakpoint "Break at multiply @5" \
173 ".*Break at multiply.*"
174 gdb_test "print i" \
175 "5" "Test conditional breakpoint stopped after five iterations"
176 gdb_py_test_silent_cmd "python bp1.condition = None" \
177 "Clear condition" 0
178 gdb_test "python print (bp1.condition)" \
179 "None" "Test conditional read"
180 gdb_continue_to_breakpoint "Break at multiply @6" \
181 ".*Break at multiply.*"
182 gdb_test "print i" \
183 "6" "Test breakpoint stopped after six iterations"
184
185 # Test commands.
186 gdb_breakpoint [gdb_get_line_number "Break at add."]
187 set test {commands $bpnum}
188 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
189 set test {print "Command for breakpoint has been executed."}
190 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
191 set test {print result}
192 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
193 gdb_test "end"
194
195 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
196 "Get Breakpoint List" 0
197 gdb_py_test_silent_cmd "python last_bp = blist\[len(blist)-1\]" \
198 "Find last breakpoint" 0
199 gdb_test "python print (last_bp.commands)" \
200 "print \"Command for breakpoint has been executed.\".*print result"
201
202 gdb_test_no_output "python last_bp.commands = 'echo hi\\necho there'" \
203 "set commands"
204 # Note the length is 3 because the string ends in a \n.
205 gdb_test "python print (len(last_bp.commands.split('\\n')))" "3" \
206 "check number of lines in commands"
207 }
208
209 proc_with_prefix test_bkpt_invisible { } {
210 global srcfile testfile hex decimal
211
212 # Start with a fresh gdb.
213 clean_restart ${testfile}
214
215 if ![runto_main] then {
216 return 0
217 }
218
219 delete_breakpoints
220 set ibp_location [gdb_get_line_number "Break at multiply."]
221 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=False)" \
222 "Set invisible breakpoint" 0
223 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
224 "Get Breakpoint List" 0
225 gdb_test "python print (ilist\[0\])" \
226 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 1"
227 gdb_test "python print (ilist\[0\].location)" \
228 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 1"
229 gdb_test "python print (ilist\[0\].visible)" \
230 "True" "Check breakpoint visibility 1"
231 gdb_test "info breakpoints" "py-breakpoint\.c:$ibp_location.*" \
232 "Check info breakpoints shows visible breakpoints"
233 delete_breakpoints
234 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=True)" \
235 "Set invisible breakpoint" 0
236 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
237 "Get Breakpoint List" 0
238 gdb_test "python print (ilist\[0\])" \
239 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 2"
240 gdb_test "python print (ilist\[0\].location)" \
241 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 2"
242 gdb_test "python print (ilist\[0\].visible)" \
243 "False" "Check breakpoint visibility 2"
244 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
245 "Check info breakpoints does not show invisible breakpoints"
246 gdb_test "maint info breakpoints" \
247 "py-breakpoint\.c:$ibp_location.*" \
248 "Check maint info breakpoints shows invisible breakpoints"
249 }
250
251 proc_with_prefix test_hardware_breakpoints { } {
252 global srcfile testfile hex decimal
253
254 # Skip these tests if the HW does not support hardware breakpoints.
255 if { [skip_hw_breakpoint_tests] } { return 0 }
256
257 # Start with a fresh gdb.
258 clean_restart ${testfile}
259
260 if ![runto_main] then {
261 return 0
262 }
263
264 delete_breakpoints
265
266 gdb_test "python hbp1 = gdb.Breakpoint(\"add\", type=gdb.BP_HARDWARE_BREAKPOINT)" \
267 ".*Hardware assisted breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\." \
268 "Set hardware breakpoint"
269 gdb_test "python print (gdb.breakpoints()\[0\].type == gdb.BP_HARDWARE_BREAKPOINT)" \
270 "True" "Check hardware breakpoint type"
271 gdb_test "continue" \
272 ".*Breakpoint ($decimal)+, add.*" \
273 "Test hardware breakpoint stop"
274 }
275
276 proc_with_prefix test_watchpoints { } {
277 global srcfile testfile hex decimal
278
279 # Start with a fresh gdb.
280 clean_restart ${testfile}
281
282 # Disable hardware watchpoints if necessary.
283 if [target_info exists gdb,no_hardware_watchpoints] {
284 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
285 }
286
287 if ![runto_main] then {
288 return 0
289 }
290
291 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" \
292 "Set watchpoint" 0
293 gdb_test "python print (wp1.pending)" "False"
294 gdb_test "continue" \
295 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" \
296 "Test watchpoint write"
297 }
298
299 proc_with_prefix test_bkpt_internal { } {
300 global srcfile testfile hex decimal
301
302 # Start with a fresh gdb.
303 clean_restart ${testfile}
304
305 # Disable hardware watchpoints if necessary.
306 if [target_info exists gdb,no_hardware_watchpoints] {
307 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
308 }
309 if ![runto_main] then {
310 return 0
311 }
312 delete_breakpoints
313 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" \
314 "Set watchpoint" 0
315 gdb_test "info breakpoints" \
316 "No breakpoints or watchpoints.*" \
317 "Check info breakpoints does not show invisible breakpoints"
318 gdb_test "maint info breakpoints" \
319 ".*watchpoint.*result.*" \
320 "Check maint info breakpoints shows invisible breakpoints"
321 gdb_test "continue" \
322 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*" \
323 "Test watchpoint write"
324 }
325
326 proc_with_prefix test_bkpt_eval_funcs { } {
327 global srcfile testfile hex decimal
328
329 # Start with a fresh gdb.
330 clean_restart ${testfile}
331
332 # Disable hardware watchpoints if necessary.
333 if [target_info exists gdb,no_hardware_watchpoints] {
334 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
335 }
336 if ![runto_main] then {
337 return 0
338 }
339 delete_breakpoints
340
341 gdb_test_multiline "Sub-class a breakpoint" \
342 "python" "" \
343 "class bp_eval (gdb.Breakpoint):" "" \
344 " inf_i = 0" "" \
345 " count = 0" "" \
346 " def stop (self):" "" \
347 " self.count = self.count + 1" "" \
348 " self.inf_i = gdb.parse_and_eval(\"i\")" "" \
349 " if self.inf_i == 3:" "" \
350 " return True" "" \
351 " return False" "" \
352 "end" ""
353
354 gdb_test_multiline "Sub-class a second breakpoint" \
355 "python" "" \
356 "class bp_also_eval (gdb.Breakpoint):" "" \
357 " count = 0" "" \
358 " def stop (self):" "" \
359 " self.count = self.count + 1" "" \
360 " if self.count == 9:" "" \
361 " return True" "" \
362 " return False" "" \
363 "end" ""
364
365 gdb_test_multiline "Sub-class a third breakpoint" \
366 "python" "" \
367 "class basic (gdb.Breakpoint):" "" \
368 " count = 0" "" \
369 "end" ""
370
371 set bp_location2 [gdb_get_line_number "Break at multiply."]
372 set end_location [gdb_get_line_number "Break at end."]
373 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$bp_location2\")" \
374 "Set breakpoint" 0
375 gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" \
376 "Set breakpoint" 0
377 gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" \
378 "Set breakpoint" 0
379 gdb_continue_to_breakpoint "Break at multiply, i==3" \
380 ".*$srcfile:$bp_location2.*"
381 gdb_test "print i" \
382 "3" "Check inferior value matches python accounting"
383 gdb_test "python print (eval_bp1.inf_i)" \
384 "3" "Check python accounting matches inferior"
385 gdb_test "python print (also_eval_bp1.count)" "4" \
386 "Check non firing same-location also_eval_bp1 function was also called at each stop."
387 gdb_test "python print (eval_bp1.count)" "4" \
388 "Check non firing same-location eval_bp1 function was also called at each stop."
389
390 delete_breakpoints
391 set cond_bp [gdb_get_line_number "Break at multiply."]
392 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" \
393 "Set breakpoint" 0
394 set test_cond {cond $bpnum}
395 gdb_test "$test_cond \"foo==3\"" \
396 "Only one stop condition allowed. There is currently a Python.*" \
397 "Check you cannot add a CLI condition to a Python breakpoint that has defined stop"
398 gdb_py_test_silent_cmd "python eval_bp2 = basic(\"$cond_bp\")" \
399 "Set breakpoint" 0
400 gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" \
401 "Set a condition" 0
402 gdb_test_multiline "Construct an eval function" \
403 "python" "" \
404 "def stop_func ():" "" \
405 " return True" "" \
406 "end" ""
407
408 gdb_test "python eval_bp2.stop = stop_func" \
409 "RuntimeError: Only one stop condition allowed. There is currently a GDB.*" \
410 "assign stop function to a breakpoint that has a condition"
411
412 delete_breakpoints
413 gdb_breakpoint [gdb_get_line_number "Break at multiply."]
414 gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" \
415 "Set breakpoint" 0
416 gdb_test "python print (check_eval.count)" "0" \
417 "Test that evaluate function has not been yet executed (ie count = 0)"
418 gdb_continue_to_breakpoint "Break at multiply, count==1" \
419 ".*$srcfile:$bp_location2.*"
420 gdb_test "python print (check_eval.count)" "1" \
421 "Test that evaluate function is run when location also has normal bp"
422
423 gdb_test_multiline "Sub-class a watchpoint" \
424 "python" "" \
425 "class wp_eval (gdb.Breakpoint):" "" \
426 " def stop (self):" "" \
427 " self.result = gdb.parse_and_eval(\"result\")" "" \
428 " if self.result == 788:" "" \
429 " return True" "" \
430 " return False" "" \
431 "end" ""
432
433 delete_breakpoints
434 gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" \
435 "Set watchpoint" 0
436 gdb_test "continue" \
437 ".*\[Ww\]atchpoint.*result.*Old value =.*New value = 788.*" \
438 "Test watchpoint write"
439 gdb_test "python print (never_eval_bp1.count)" "0" \
440 "Check that this unrelated breakpoints eval function was never called."
441 }
442
443 proc_with_prefix test_bkpt_temporary { } {
444 global srcfile testfile hex decimal
445
446 # Start with a fresh gdb.
447 clean_restart ${testfile}
448
449 if ![runto_main] then {
450 return 0
451 }
452 delete_breakpoints
453
454 gdb_test_multiline "Sub-class and check temporary breakpoint" \
455 "python" "" \
456 "class temp_bp (gdb.Breakpoint):" "" \
457 " count = 0" "" \
458 " def stop (self):" "" \
459 " self.count = self.count + 1" "" \
460 " return True" "" \
461 "end" ""
462 set ibp_location [gdb_get_line_number "Break at multiply."]
463 gdb_py_test_silent_cmd "python ibp = temp_bp(\"$ibp_location\", temporary=True)" \
464 "Set temporary breakpoint" 0
465 gdb_test "info breakpoints" \
466 "2.*breakpoint.*del.*py-breakpoint\.c:$ibp_location.*" \
467 "Check info breakpoints shows breakpoint with temporary status"
468 gdb_test "python print (ibp.location)" "py-breakpoint\.c:$ibp_location*" \
469 "Check temporary breakpoint location"
470 gdb_test "python print (ibp.temporary)" "True" \
471 "Check breakpoint temporary status"
472 gdb_continue_to_breakpoint "Break at multiply." \
473 ".*$srcfile:$ibp_location.*"
474 gdb_test "python print (ibp.count)" "1" \
475 "Check temporary stop callback executed before deletion."
476 gdb_test "python print (ibp.temporary)" "RuntimeError: Breakpoint 2 is invalid.*" \
477 "Check temporary breakpoint is deleted after being hit"
478 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
479 "Check info breakpoints shows temporary breakpoint is deleted"
480 }
481
482 # Test address locations.
483
484 proc_with_prefix test_bkpt_address {} {
485 global gdb_prompt decimal srcfile
486
487 # Delete all breakpoints
488 delete_breakpoints
489
490 gdb_test "python gdb.Breakpoint(\"*main\")" \
491 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
492
493 gdb_py_test_silent_cmd \
494 "python main_loc = gdb.parse_and_eval(\"main\").address" \
495 "eval address of main" 0
496
497 # Python 2 vs 3 ... Check `int' first. If that fails, try `long'.
498 gdb_test_multiple "python main_addr = int(main_loc)" "int value of main" {
499 -re "Traceback.*$gdb_prompt $" {
500 gdb_test_no_output "python main_addr = long(main_loc)" \
501 "long value of main"
502 }
503 -re "$gdb_prompt $" {
504 pass "int value of main"
505 }
506 }
507
508 # Include whitespace in the linespec to double-check proper
509 # grokking of argument to gdb.Breakpoint.
510 gdb_test "python gdb.Breakpoint(\" *{}\".format(str(main_addr)))" \
511 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
512 }
513
514 proc_with_prefix test_bkpt_pending {} {
515 delete_breakpoints
516 gdb_breakpoint "nosuchfunction" allow-pending
517 gdb_test "python print (gdb.breakpoints()\[0\].pending)" "True" \
518 "Check pending status of pending breakpoint"
519 }
520
521 # Helper proc to install an event listener for a given breakpoint
522 # event. NAME is the name of the event to listen for.
523 proc connect_event {name} {
524 set lambda "lambda x: note_event(\"$name\")"
525 gdb_test_no_output "python gdb.events.$name.connect($lambda)" \
526 "install $name event listener"
527 }
528
529 # Helper proc to check that the most recently emitted breakpoint event
530 # is EXPECTED.
531 proc check_last_event {expected} {
532 gdb_test "python print (last_bp_event)" $expected \
533 "check for $expected event"
534 }
535
536 proc_with_prefix test_bkpt_events {} {
537 global testfile
538
539 clean_restart ${testfile}
540
541 gdb_test_multiline "Create event handler" \
542 "python" "" \
543 "def note_event(arg):" "" \
544 " global last_bp_event" "" \
545 " last_bp_event = arg" "" \
546 "end" ""
547 gdb_test_no_output "python last_bp_event = None"
548
549 connect_event breakpoint_created
550 connect_event breakpoint_modified
551 connect_event breakpoint_deleted
552
553 gdb_breakpoint [gdb_get_line_number "Break at add."]
554 check_last_event breakpoint_created
555 gdb_test_no_output "disable 1"
556 check_last_event breakpoint_modified
557 gdb_test_no_output "delete 1"
558 check_last_event breakpoint_deleted
559 }
560
561 proc_with_prefix test_bkpt_explicit_loc {} {
562 global srcfile testfile
563
564 # Start with a fresh gdb.
565 clean_restart ${testfile}
566
567 if ![runto_main] then {
568 return 0
569 }
570
571 delete_breakpoints
572
573 set bp_location1 [gdb_get_line_number "Break at multiply."]
574 set bp_location2 [gdb_get_line_number "Break at add."]
575
576 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (line=$bp_location1)" \
577 "set explicit breakpoint by line" 0
578 gdb_continue_to_breakpoint "break at multiply for explicit line" \
579 ".*Break at multiply.*"
580
581 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (line=\"+1\")" \
582 "set explicit breakpoint by relative line" 0
583 gdb_continue_to_breakpoint "break at add for relative line" \
584 ".*Break at add.*"
585
586 delete_breakpoints
587 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (line=\"-1\")" \
588 "set explicit breakpoint by relative negative line" 0
589 gdb_continue_to_breakpoint "break at multiply for negative line" \
590 ".*Break at multiply.*"
591
592 delete_breakpoints
593 gdb_test "python bp1 = gdb.Breakpoint (line=bp1)" \
594 "RuntimeError: Line keyword should be an integer or a string.*" \
595 "set explicit breakpoint by invalid line type"
596
597 delete_breakpoints
598 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (function=\"add\")" \
599 "set explicit breakpoint by function" 0
600 gdb_continue_to_breakpoint "break at function add for function" \
601 ".*Break at function add.*"
602
603 delete_breakpoints
604 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (source=\"$srcfile\", function=\"add\")" \
605 "set explicit breakpoint by source file and function" 0
606 gdb_continue_to_breakpoint "break at function add for source and function" \
607 ".*Break at function add.*"
608
609 delete_breakpoints
610 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (source=\"$srcfile\", line=\"$bp_location2\")" \
611 "set explicit breakpoint by source file and line number" 0
612 gdb_continue_to_breakpoint "break at add for source and line" \
613 ".*Break at add.*"
614
615 delete_breakpoints
616 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"-source $srcfile -line $bp_location2\")" \
617 "set explicit breakpoint by source file and line number in spec" 0
618 gdb_continue_to_breakpoint "break at add for source and line in spec" \
619 ".*Break at add.*"
620
621 delete_breakpoints
622 gdb_test "python bp1 = gdb.Breakpoint (source=\"$srcfile\")" \
623 "RuntimeError: Specifying a source must also include a line, label or function.*" \
624 "set invalid explicit breakpoint by source only"
625
626 gdb_test "python bp1 = gdb.Breakpoint (source=\"foo.c\", line=\"5\")" \
627 "No source file named foo.*" \
628 "set invalid explicit breakpoint by missing source and line"
629 gdb_test "python bp1 = gdb.Breakpoint (source=\"$srcfile\", line=\"900\")" \
630 "No line 900 in file \"$srcfile\".*" \
631 "set invalid explicit breakpoint by source and invalid line"
632 gdb_test "python bp1 = gdb.Breakpoint (function=\"blah\")" \
633 "Function \"blah\" not defined.*" \
634 "set invalid explicit breakpoint by missing function"
635
636 delete_breakpoints
637 gdb_test "catch throw" "Catchpoint .* \\(throw\\)"
638 gdb_test "python print (gdb.breakpoints())" \
639 "\(\)" \
640 "catch throw is not a breakpoint"
641 }
642
643 proc_with_prefix test_bkpt_qualified {} {
644 global decimal hex testfile
645
646 # Start with a fresh gdb.
647 clean_restart ${testfile}
648
649 set one_location_re "Breakpoint $decimal at $hex:.*line $decimal."
650 set two_location_re "Breakpoint $decimal at $hex:.*2 locations."
651
652 if ![runto_main] then {
653 return 0
654 }
655
656 # Test the default value of "qualified".
657 delete_breakpoints
658 gdb_test \
659 "python gdb.Breakpoint(\"multiply\")" \
660 $two_location_re \
661 "qualified implicitly false"
662
663 # Test qualified=False.
664 delete_breakpoints
665 gdb_test \
666 "python gdb.Breakpoint(\"multiply\", qualified=False)" \
667 $two_location_re \
668 "qualified false"
669
670 # Test qualified=True.
671 delete_breakpoints
672 gdb_test \
673 "python gdb.Breakpoint(\"multiply\", qualified=True)" \
674 $one_location_re \
675 "qualified true"
676
677 # Test qualified=True with an explicit function.
678 delete_breakpoints
679 gdb_test \
680 "python gdb.Breakpoint(function=\"multiply\", qualified=True)" \
681 $one_location_re \
682 "qualified true and explicit"
683
684 # Test qualified=False with an explicit function.
685 delete_breakpoints
686 gdb_test \
687 "python gdb.Breakpoint(function=\"multiply\", qualified=False)" \
688 $two_location_re \
689 "qualified false and explicit"
690
691 # Test -q in the spec string.
692 delete_breakpoints
693 gdb_test \
694 "python gdb.Breakpoint(\"-q multiply\")" \
695 $one_location_re \
696 "-q in spec string"
697
698 # Test -q in the spec string with explicit location.
699 delete_breakpoints
700 gdb_test \
701 "python gdb.Breakpoint(\"-q -function multiply\")" \
702 $one_location_re \
703 "-q in spec string with explicit location"
704
705 # Test -q in the spec string and qualified=False (-q should win).
706 delete_breakpoints
707 gdb_test \
708 "python gdb.Breakpoint(\"-q multiply\", qualified=False)" \
709 $one_location_re \
710 "-q in spec string and qualified false"
711 }
712
713 proc_with_prefix test_bkpt_probe {} {
714 global decimal hex testfile srcfile
715
716 if { [prepare_for_testing "failed to prepare" ${testfile}-probes \
717 ${srcfile} {debug c++ additional_flags=-DUSE_PROBES}] } {
718 return -1
719 }
720
721 if ![runto_main] then {
722 return 0
723 }
724
725 gdb_test \
726 "python gdb.Breakpoint(\"-probe test:result_updated\")" \
727 "Breakpoint $decimal at $hex" \
728 "-probe in spec string"
729 }
730
731 proc_with_prefix test_catchpoints {} {
732 global srcfile testfile
733 global gdb_prompt decimal
734
735 # Start with a fresh gdb.
736 clean_restart ${testfile}
737
738 if ![runto_main] then {
739 return 0
740 }
741
742 # Try to create a catchpoint, currently this isn't supported via
743 # the python api.
744 gdb_test "python gdb.Breakpoint (\"syscall\", type=gdb.BP_CATCHPOINT)" \
745 [multi_line \
746 "gdb.error: BP_CATCHPOINT not supported" \
747 "Error while executing Python code\\."] \
748 "create a catchpoint via the api"
749
750 # Setup a catchpoint.
751 set num "XXX"
752 gdb_test_multiple "catch throw" "" {
753 -re "The feature \'catch throw\' is not supported.*\r\n$gdb_prompt $" {
754 unsupported "catch syscall isn't supported"
755 return -1
756 }
757 -re "Catchpoint ($decimal) \\(throw\\)\r\n$gdb_prompt $" {
758 set num $expect_out(1,string)
759 pass $gdb_test_name
760 }
761 }
762
763 # Look for the catchpoint in the breakpoint list.
764 gdb_test_multiline "scan breakpoint list for BP_CATCHPOINT" \
765 "python" "" \
766 "def scan_bp_list ():" "" \
767 " for b in gdb.breakpoints():" "" \
768 " if b.type == gdb.BP_CATCHPOINT:" "" \
769 " print(\"breakpoint #%d, type BP_CATCHPOINT\" % b.number)" "" \
770 "end" ""
771 gdb_test "python scan_bp_list ()" \
772 "breakpoint #${num}, type BP_CATCHPOINT" \
773 "scan breakpoint for BP_CATCHPOINT"
774
775 # Arrange to print something when GDB stops, then continue to the
776 # catchpoint and check we get the expected event.
777 gdb_test_multiline "setup stop event handler" \
778 "python" "" \
779 "def stop_handler (event):" "" \
780 " if (isinstance (event, gdb.BreakpointEvent)" "" \
781 " and isinstance (event.breakpoint, gdb.Breakpoint)" "" \
782 " and event.breakpoint.type == gdb.BP_CATCHPOINT):" "" \
783 " print (\"Stopped at catchpoint event: %d\" % event.breakpoint.number)" "" \
784 "end" "" \
785 "python gdb.events.stop.connect (stop_handler)" ""
786 gdb_test "continue" "Stopped at catchpoint event: ${num}"
787 }
788
789 # Test auto-disable is effective when notifying breakpoint_modified.
790
791 proc_with_prefix test_bkpt_auto_disable { } {
792 global srcfile testfile hex decimal
793
794 # Start with a fresh gdb.
795 clean_restart ${testfile}
796
797 if ![runto_main] then {
798 return 0
799 }
800 delete_breakpoints
801
802 set mult_line [gdb_get_line_number "Break at multiply."]
803 gdb_breakpoint ${mult_line}
804 gdb_test_no_output "enable count 1 2" "one shot enable"
805 # Python 2 doesn't support print in lambda function, so use a named
806 # function instead.
807 gdb_test_multiline "Define print_bp_enabled" \
808 "python" "" \
809 "def print_bp_enabled (bp):" "" \
810 " print (bp.enabled)" "" \
811 "end" ""
812 gdb_test_no_output \
813 "python gdb.events.breakpoint_modified.connect(print_bp_enabled)" \
814 "trap breakpoint_modified event"
815 gdb_test "continue" "False.*" "auto-disabling after enable count reached"
816 }
817
818 test_bkpt_basic
819 test_bkpt_deletion
820 test_bkpt_cond_and_cmds
821 test_bkpt_invisible
822 test_hardware_breakpoints
823 test_catchpoints
824 test_watchpoints
825 test_bkpt_internal
826 test_bkpt_eval_funcs
827 test_bkpt_temporary
828 test_bkpt_address
829 test_bkpt_pending
830 test_bkpt_events
831 test_bkpt_explicit_loc
832 test_bkpt_qualified
833 test_bkpt_probe
834 test_bkpt_auto_disable