]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.python/py-breakpoint.exp
bd138ac3d2f4a7393ef1a225e58c07cb1a102a14
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.python / py-breakpoint.exp
1 # Copyright (C) 2010-2017 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 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
24 return -1
25 }
26
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
29
30 proc test_bkpt_basic { } {
31 global srcfile testfile hex decimal
32
33 with_test_prefix "test_bkpt_basic" {
34 # Start with a fresh gdb.
35 clean_restart ${testfile}
36
37 # We should start with no breakpoints.
38 gdb_test "python print (gdb.breakpoints())" "\\(\\)"
39
40 if ![runto_main] then {
41 fail "cannot run to main."
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
118 proc test_bkpt_deletion { } {
119 global srcfile testfile hex decimal
120
121 with_test_prefix test_bkpt_deletion {
122 # Start with a fresh gdb.
123 clean_restart ${testfile}
124
125 if ![runto_main] then {
126 fail "cannot run to main."
127 return 0
128 }
129
130 # Test breakpoints are deleted correctly.
131 set deltst_location [gdb_get_line_number "Break at multiply."]
132 set end_location [gdb_get_line_number "Break at end."]
133 gdb_py_test_silent_cmd "python dp1 = gdb.Breakpoint (\"$deltst_location\")" \
134 "Set breakpoint" 0
135 gdb_breakpoint [gdb_get_line_number "Break at end."]
136 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
137 "Get Breakpoint List" 0
138 gdb_test "python print (len(del_list))" \
139 "3" "Number of breakpoints before delete"
140 gdb_continue_to_breakpoint "Break at multiply." \
141 ".*$srcfile:$deltst_location.*"
142 gdb_py_test_silent_cmd "python dp1.delete()" \
143 "Delete Breakpoint" 0
144 gdb_test "python print (dp1.number)" \
145 "RuntimeError: Breakpoint 2 is invalid.*" \
146 "Check breakpoint invalidated"
147 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
148 "Get Breakpoint List" 0
149 gdb_test "python print (len(del_list))" \
150 "2" "Number of breakpoints after delete"
151 gdb_continue_to_breakpoint "Break at end." \
152 ".*$srcfile:$end_location.*"
153 }
154 }
155
156 proc test_bkpt_cond_and_cmds { } {
157 global srcfile testfile hex decimal
158
159 with_test_prefix test_bkpt_cond_and_cmds {
160 # Start with a fresh gdb.
161 clean_restart ${testfile}
162
163 if ![runto_main] then {
164 fail "cannot run to main."
165 return 0
166 }
167
168 # Test conditional setting.
169 set bp_location1 [gdb_get_line_number "Break at multiply."]
170 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"$bp_location1\")" \
171 "Set breakpoint" 0
172 gdb_continue_to_breakpoint "Break at multiply" \
173 ".*Break at multiply.*"
174 gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" \
175 "Set breakpoint" 0
176 gdb_test "python print (bp1.condition)" "i == 5" \
177 "Test conditional has been set"
178 gdb_continue_to_breakpoint "Break at multiply @5" \
179 ".*Break at multiply.*"
180 gdb_test "print i" \
181 "5" "Test conditional breakpoint stopped after five iterations"
182 gdb_py_test_silent_cmd "python bp1.condition = None" \
183 "Clear condition" 0
184 gdb_test "python print (bp1.condition)" \
185 "None" "Test conditional read"
186 gdb_continue_to_breakpoint "Break at multiply @6" \
187 ".*Break at multiply.*"
188 gdb_test "print i" \
189 "6" "Test breakpoint stopped after six iterations"
190
191 # Test commands.
192 gdb_breakpoint [gdb_get_line_number "Break at add."]
193 set test {commands $bpnum}
194 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
195 set test {print "Command for breakpoint has been executed."}
196 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
197 set test {print result}
198 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
199 gdb_test "end"
200
201 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
202 "Get Breakpoint List" 0
203 gdb_test "python print (blist\[len(blist)-1\].commands)" \
204 "print \"Command for breakpoint has been executed.\".*print result"
205 }
206 }
207
208 proc test_bkpt_invisible { } {
209 global srcfile testfile hex decimal
210
211 with_test_prefix test_bkpt_invisible {
212 # Start with a fresh gdb.
213 clean_restart ${testfile}
214
215 if ![runto_main] then {
216 fail "cannot run to main."
217 return 0
218 }
219
220 delete_breakpoints
221 set ibp_location [gdb_get_line_number "Break at multiply."]
222 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=False)" \
223 "Set invisible breakpoint" 0
224 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
225 "Get Breakpoint List" 0
226 gdb_test "python print (ilist\[0\])" \
227 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 1"
228 gdb_test "python print (ilist\[0\].location)" \
229 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 1"
230 gdb_test "python print (ilist\[0\].visible)" \
231 "True" "Check breakpoint visibility 1"
232 gdb_test "info breakpoints" "py-breakpoint\.c:$ibp_location.*" \
233 "Check info breakpoints shows visible breakpoints"
234 delete_breakpoints
235 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=True)" \
236 "Set invisible breakpoint" 0
237 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
238 "Get Breakpoint List" 0
239 gdb_test "python print (ilist\[0\])" \
240 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 2"
241 gdb_test "python print (ilist\[0\].location)" \
242 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 2"
243 gdb_test "python print (ilist\[0\].visible)" \
244 "False" "Check breakpoint visibility 2"
245 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
246 "Check info breakpoints does not show invisible breakpoints"
247 gdb_test "maint info breakpoints" \
248 "py-breakpoint\.c:$ibp_location.*" \
249 "Check maint info breakpoints shows invisible breakpoints"
250 }
251 }
252
253 proc test_watchpoints { } {
254 global srcfile testfile hex decimal
255
256 with_test_prefix test_watchpoints {
257 # Start with a fresh gdb.
258 clean_restart ${testfile}
259
260 # Disable hardware watchpoints if necessary.
261 if [target_info exists gdb,no_hardware_watchpoints] {
262 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
263 }
264
265 if ![runto_main] then {
266 fail "cannot run to main."
267 return 0
268 }
269
270 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" \
271 "Set watchpoint" 0
272 gdb_test "python print (wp1.pending)" "False"
273 gdb_test "continue" \
274 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" \
275 "Test watchpoint write"
276 }
277 }
278
279 proc test_bkpt_internal { } {
280 global srcfile testfile hex decimal
281
282 with_test_prefix test_bkpt_internal {
283 # Start with a fresh gdb.
284 clean_restart ${testfile}
285
286 # Disable hardware watchpoints if necessary.
287 if [target_info exists gdb,no_hardware_watchpoints] {
288 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
289 }
290 if ![runto_main] then {
291 fail "cannot run to main."
292 return 0
293 }
294 delete_breakpoints
295 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" \
296 "Set watchpoint" 0
297 gdb_test "info breakpoints" \
298 "No breakpoints or watchpoints.*" \
299 "Check info breakpoints does not show invisible breakpoints"
300 gdb_test "maint info breakpoints" \
301 ".*watchpoint.*result.*" \
302 "Check maint info breakpoints shows invisible breakpoints"
303 gdb_test "continue" \
304 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*" \
305 "Test watchpoint write"
306 }
307 }
308
309 proc test_bkpt_eval_funcs { } {
310 global srcfile testfile hex decimal
311
312 with_test_prefix test_bkpt_eval_funcs {
313 # Start with a fresh gdb.
314 clean_restart ${testfile}
315
316 # Disable hardware watchpoints if necessary.
317 if [target_info exists gdb,no_hardware_watchpoints] {
318 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
319 }
320 if ![runto_main] then {
321 fail "cannot run to main."
322 return 0
323 }
324 delete_breakpoints
325
326 gdb_py_test_multiple "Sub-class a breakpoint" \
327 "python" "" \
328 "class bp_eval (gdb.Breakpoint):" "" \
329 " inf_i = 0" "" \
330 " count = 0" "" \
331 " def stop (self):" "" \
332 " self.count = self.count + 1" "" \
333 " self.inf_i = gdb.parse_and_eval(\"i\")" "" \
334 " if self.inf_i == 3:" "" \
335 " return True" "" \
336 " return False" "" \
337 "end" ""
338
339 gdb_py_test_multiple "Sub-class a second breakpoint" \
340 "python" "" \
341 "class bp_also_eval (gdb.Breakpoint):" "" \
342 " count = 0" "" \
343 " def stop (self):" "" \
344 " self.count = self.count + 1" "" \
345 " if self.count == 9:" "" \
346 " return True" "" \
347 " return False" "" \
348 "end" ""
349
350 gdb_py_test_multiple "Sub-class a third breakpoint" \
351 "python" "" \
352 "class basic (gdb.Breakpoint):" "" \
353 " count = 0" "" \
354 "end" ""
355
356 set bp_location2 [gdb_get_line_number "Break at multiply."]
357 set end_location [gdb_get_line_number "Break at end."]
358 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$bp_location2\")" \
359 "Set breakpoint" 0
360 gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" \
361 "Set breakpoint" 0
362 gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" \
363 "Set breakpoint" 0
364 gdb_continue_to_breakpoint "Break at multiply, i==3" \
365 ".*$srcfile:$bp_location2.*"
366 gdb_test "print i" \
367 "3" "Check inferior value matches python accounting"
368 gdb_test "python print (eval_bp1.inf_i)" \
369 "3" "Check python accounting matches inferior"
370 gdb_test "python print (also_eval_bp1.count)" "4" \
371 "Check non firing same-location also_eval_bp1 function was also called at each stop."
372 gdb_test "python print (eval_bp1.count)" "4" \
373 "Check non firing same-location eval_bp1 function was also called at each stop."
374
375 delete_breakpoints
376 set cond_bp [gdb_get_line_number "Break at multiply."]
377 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" \
378 "Set breakpoint" 0
379 set test_cond {cond $bpnum}
380 gdb_test "$test_cond \"foo==3\"" \
381 "Only one stop condition allowed. There is currently a Python.*" \
382 "Check you cannot add a CLI condition to a Python breakpoint that has defined stop"
383 gdb_py_test_silent_cmd "python eval_bp2 = basic(\"$cond_bp\")" \
384 "Set breakpoint" 0
385 gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" \
386 "Set a condition" 0
387 gdb_py_test_multiple "Construct an eval function" \
388 "python" "" \
389 "def stop_func ():" "" \
390 " return True" "" \
391 "end" ""
392
393 gdb_test "python eval_bp2.stop = stop_func" \
394 "RuntimeError: Only one stop condition allowed. There is currently a GDB.*" \
395 "assign stop function to a breakpoint that has a condition"
396
397 delete_breakpoints
398 gdb_breakpoint [gdb_get_line_number "Break at multiply."]
399 gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" \
400 "Set breakpoint" 0
401 gdb_test "python print (check_eval.count)" "0" \
402 "Test that evaluate function has not been yet executed (ie count = 0)"
403 gdb_continue_to_breakpoint "Break at multiply, count==1" \
404 ".*$srcfile:$bp_location2.*"
405 gdb_test "python print (check_eval.count)" "1" \
406 "Test that evaluate function is run when location also has normal bp"
407
408 gdb_py_test_multiple "Sub-class a watchpoint" \
409 "python" "" \
410 "class wp_eval (gdb.Breakpoint):" "" \
411 " def stop (self):" "" \
412 " self.result = gdb.parse_and_eval(\"result\")" "" \
413 " if self.result == 788:" "" \
414 " return True" "" \
415 " return False" "" \
416 "end" ""
417
418 delete_breakpoints
419 gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" \
420 "Set watchpoint" 0
421 gdb_test "continue" \
422 ".*\[Ww\]atchpoint.*result.*Old value =.*New value = 788.*" \
423 "Test watchpoint write"
424 gdb_test "python print (never_eval_bp1.count)" "0" \
425 "Check that this unrelated breakpoints eval function was never called."
426 }
427 }
428
429 proc test_bkpt_temporary { } {
430 global srcfile testfile hex decimal
431
432 with_test_prefix test_bkpt_temporary {
433 # Start with a fresh gdb.
434 clean_restart ${testfile}
435
436 if ![runto_main] then {
437 fail "cannot run to main."
438 return 0
439 }
440 delete_breakpoints
441
442 gdb_py_test_multiple "Sub-class and check temporary breakpoint" \
443 "python" "" \
444 "class temp_bp (gdb.Breakpoint):" "" \
445 " count = 0" "" \
446 " def stop (self):" "" \
447 " self.count = self.count + 1" "" \
448 " return True" "" \
449 "end" ""
450 set ibp_location [gdb_get_line_number "Break at multiply."]
451 gdb_py_test_silent_cmd "python ibp = temp_bp(\"$ibp_location\", temporary=True)" \
452 "Set temporary breakpoint" 0
453 gdb_test "info breakpoints" \
454 "2.*breakpoint.*del.*py-breakpoint\.c:$ibp_location.*" \
455 "Check info breakpoints shows breakpoint with temporary status"
456 gdb_test "python print (ibp.location)" "py-breakpoint\.c:$ibp_location*" \
457 "Check temporary breakpoint location"
458 gdb_test "python print (ibp.temporary)" "True" \
459 "Check breakpoint temporary status"
460 gdb_continue_to_breakpoint "Break at multiply." \
461 ".*$srcfile:$ibp_location.*"
462 gdb_test "python print (ibp.count)" "1" \
463 "Check temporary stop callback executed before deletion."
464 gdb_test "python print (ibp.temporary)" "RuntimeError: Breakpoint 2 is invalid.*" \
465 "Check temporary breakpoint is deleted after being hit"
466 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
467 "Check info breakpoints shows temporary breakpoint is deleted"
468 }
469 }
470
471 # Test address locations.
472
473 proc test_bkpt_address {} {
474 global gdb_prompt decimal srcfile
475
476 # Delete all breakpoints
477 delete_breakpoints
478
479 gdb_test "python gdb.Breakpoint(\"*main\")" \
480 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
481
482 gdb_py_test_silent_cmd \
483 "python main_loc = gdb.parse_and_eval(\"main\").address" \
484 "eval address of main" 0
485
486 # Python 2 vs 3 ... Check `int' first. If that fails, try `long'.
487 gdb_test_multiple "python main_addr = int(main_loc)" "int value of main" {
488 -re "Traceback.*$gdb_prompt $" {
489 gdb_test_no_output "python main_addr = long(main_loc)" \
490 "long value of main"
491 }
492 -re "$gdb_prompt $" {
493 pass "int value of main"
494 }
495 }
496
497 # Include whitespace in the linespec to double-check proper
498 # grokking of argument to gdb.Breakpoint.
499 gdb_test "python gdb.Breakpoint(\" *{}\".format(str(main_addr)))" \
500 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
501 }
502
503 proc test_bkpt_pending {} {
504 delete_breakpoints
505 gdb_breakpoint "nosuchfunction" allow-pending
506 gdb_test "python print (gdb.breakpoints()\[0\].pending)" "True" \
507 "Check pending status of pending breakpoint"
508 }
509
510 # Helper proc to install an event listener for a given breakpoint
511 # event. NAME is the name of the event to listen for.
512 proc connect_event {name} {
513 set lambda "lambda x: note_event(\"$name\")"
514 gdb_test_no_output "python gdb.events.$name.connect($lambda)" \
515 "install $name event listener"
516 }
517
518 # Helper proc to check that the most recently emitted breakpoint event
519 # is EXPECTED.
520 proc check_last_event {expected} {
521 gdb_test "python print (last_bp_event)" $expected \
522 "check for $expected event"
523 }
524
525 proc test_bkpt_events {} {
526 global testfile
527
528 clean_restart ${testfile}
529
530 gdb_py_test_multiple "Create event handler" \
531 "python" "" \
532 "def note_event(arg):" "" \
533 " global last_bp_event" "" \
534 " last_bp_event = arg" "" \
535 "end" ""
536 gdb_test_no_output "python last_bp_event = None"
537
538 connect_event breakpoint_created
539 connect_event breakpoint_modified
540 connect_event breakpoint_deleted
541
542 gdb_breakpoint [gdb_get_line_number "Break at add."]
543 check_last_event breakpoint_created
544 gdb_test_no_output "disable 1"
545 check_last_event breakpoint_modified
546 gdb_test_no_output "delete 1"
547 check_last_event breakpoint_deleted
548 }
549
550 test_bkpt_basic
551 test_bkpt_deletion
552 test_bkpt_cond_and_cmds
553 test_bkpt_invisible
554 test_watchpoints
555 test_bkpt_internal
556 test_bkpt_eval_funcs
557 test_bkpt_temporary
558 test_bkpt_address
559 test_bkpt_pending
560 test_bkpt_events