1 # Copyright (C) 2010-2016 Free Software Foundation, Inc.
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.
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.
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/>.
16 # This file is part of the GDB testsuite. It tests the mechanism
17 # exposing breakpoints to Python.
19 load_lib gdb-python.exp
23 if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } {
27 # Skip all tests if Python scripting is not enabled.
28 if { [skip_python_tests] } { continue }
30 proc test_bkpt_basic { } {
31 global srcfile testfile hex decimal
33 with_test_prefix "test_bkpt_basic" {
34 # Start with a fresh gdb.
35 clean_restart ${testfile}
37 # We should start with no breakpoints.
38 gdb_test "python print (gdb.breakpoints())" "\\(\\)"
40 if ![runto_main] then {
41 fail "cannot run to main."
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"
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.*"
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"
73 gdb_test "python print (blist\[1\].location)" \
74 "py-breakpoint\.c:${mult_line}*" \
75 "check breakpoint location @mult_line"
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"
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.*"
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"
118 proc test_bkpt_deletion { } {
119 global srcfile testfile hex decimal
121 with_test_prefix test_bkpt_deletion {
122 # Start with a fresh gdb.
123 clean_restart ${testfile}
125 if ![runto_main] then {
126 fail "cannot run to main."
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\")" \
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.*"
156 proc test_bkpt_cond_and_cmds { } {
157 global srcfile testfile hex decimal
159 with_test_prefix test_bkpt_cond_and_cmds {
160 # Start with a fresh gdb.
161 clean_restart ${testfile}
163 if ![runto_main] then {
164 fail "cannot run to main."
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\")" \
172 gdb_continue_to_breakpoint "Break at multiply" \
173 ".*Break at multiply.*"
174 gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" \
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.*"
181 "5" "Test conditional breakpoint stopped after five iterations"
182 gdb_py_test_silent_cmd "python bp1.condition = None" \
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.*"
189 "6" "Test breakpoint stopped after six iterations"
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 } }
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"
208 proc test_bkpt_invisible { } {
209 global srcfile testfile hex decimal
211 with_test_prefix test_bkpt_invisible {
212 # Start with a fresh gdb.
213 clean_restart ${testfile}
215 if ![runto_main] then {
216 fail "cannot run to main."
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"
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"
253 proc test_watchpoints { } {
254 global srcfile testfile hex decimal
256 with_test_prefix test_watchpoints {
257 # Start with a fresh gdb.
258 clean_restart ${testfile}
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" ""
265 if ![runto_main] then {
266 fail "cannot run to main."
270 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" \
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"
279 proc test_bkpt_internal { } {
280 global srcfile testfile hex decimal
282 with_test_prefix test_bkpt_internal {
283 # Start with a fresh gdb.
284 clean_restart ${testfile}
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" ""
290 if ![runto_main] then {
291 fail "cannot run to main."
295 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" \
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"
309 proc test_bkpt_eval_funcs { } {
310 global srcfile testfile hex decimal
312 with_test_prefix test_bkpt_eval_funcs {
313 # Start with a fresh gdb.
314 clean_restart ${testfile}
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" ""
320 if ![runto_main] then {
321 fail "cannot run to main."
326 gdb_py_test_multiple "Sub-class a breakpoint" \
328 "class bp_eval (gdb.Breakpoint):" "" \
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:" "" \
339 gdb_py_test_multiple "Sub-class a second breakpoint" \
341 "class bp_also_eval (gdb.Breakpoint):" "" \
343 " def stop (self):" "" \
344 " self.count = self.count + 1" "" \
345 " if self.count == 9:" "" \
350 gdb_py_test_multiple "Sub-class a third breakpoint" \
352 "class basic (gdb.Breakpoint):" "" \
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\")" \
360 gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" \
362 gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" \
364 gdb_continue_to_breakpoint "Break at multiply, i==3" \
365 ".*$srcfile:$bp_location2.*"
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."
376 set cond_bp [gdb_get_line_number "Break at multiply."]
377 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" \
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\")" \
385 gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" \
387 gdb_py_test_multiple "Construct an eval function" \
389 "def stop_func ():" "" \
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"
398 gdb_breakpoint [gdb_get_line_number "Break at multiply."]
399 gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" \
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"
408 gdb_py_test_multiple "Sub-class a watchpoint" \
410 "class wp_eval (gdb.Breakpoint):" "" \
411 " def stop (self):" "" \
412 " self.result = gdb.parse_and_eval(\"result\")" "" \
413 " if self.result == 788:" "" \
419 gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" \
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."
429 proc test_bkpt_temporary { } {
430 global srcfile testfile hex decimal
432 with_test_prefix test_bkpt_temporary {
433 # Start with a fresh gdb.
434 clean_restart ${testfile}
436 if ![runto_main] then {
437 fail "cannot run to main."
442 gdb_py_test_multiple "Sub-class and check temporary breakpoint" \
444 "class temp_bp (gdb.Breakpoint):" "" \
446 " def stop (self):" "" \
447 " self.count = self.count + 1" "" \
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"
471 # Test address locations.
473 proc test_bkpt_address {} {
474 global gdb_prompt decimal srcfile
476 # Delete all breakpoints
479 gdb_test "python gdb.Breakpoint(\"*main\")" \
480 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
482 gdb_py_test_silent_cmd \
483 "python main_loc = gdb.parse_and_eval(\"main\").address" \
484 "eval address of main" 0
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)" \
492 -re "$gdb_prompt $" {
493 pass "int value of main"
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)+\."
503 proc test_bkpt_pending {} {
505 gdb_breakpoint "nosuchfunction" allow-pending
506 gdb_test "python print (gdb.breakpoints()\[0\].pending)" "True" \
507 "Check pending status of pending breakpoint"
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"
518 # Helper proc to check that the most recently emitted breakpoint event
520 proc check_last_event {expected} {
521 gdb_test "python print (last_bp_event)" $expected \
522 "check for $expected event"
525 proc test_bkpt_events {} {
528 clean_restart ${testfile}
530 gdb_py_test_multiple "Create event handler" \
532 "def note_event(arg):" "" \
533 " global last_bp_event" "" \
534 " last_bp_event = arg" "" \
536 gdb_test_no_output "python last_bp_event = None"
538 connect_event breakpoint_created
539 connect_event breakpoint_modified
540 connect_event breakpoint_deleted
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
552 test_bkpt_cond_and_cmds