Fix PR python/17981
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.python / py-breakpoint.exp
1 # Copyright (C) 2010-2016 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 ${testfile}.exp ${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
53 set mult_line [gdb_get_line_number "Break at multiply."]
54 gdb_breakpoint ${mult_line}
55 gdb_continue_to_breakpoint "Break at multiply" \
56 ".*Break at multiply.*"
57
58 # Check that the Python breakpoint code noted the addition of a
59 # breakpoint "behind the scenes".
60 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
61 "Get Breakpoint List" 0
62 gdb_test "python print (len(blist))" \
63 "2" "Check for two breakpoints"
64 gdb_test "python print (blist\[0\])" \
65 "<gdb.Breakpoint object at $hex>" "Check obj exists @main 2"
66 gdb_test "python print (blist\[0\].location)" \
67 "main." "Check breakpoint location @main 2"
68 gdb_test "python print (blist\[1\])" \
69 "<gdb.Breakpoint object at $hex>" "Check obj exists @mult_line"
70
71 gdb_test "python print (blist\[1\].location)" \
72 "py-breakpoint\.c:${mult_line}*" \
73 "Check breakpoint location @mult_line"
74
75 # Check hit and ignore counts.
76 gdb_test "python print (blist\[1\].hit_count)" \
77 "1" "Check breakpoint hit count @1"
78 gdb_py_test_silent_cmd "python blist\[1\].ignore_count = 4" \
79 "Set breakpoint hit count" 0
80 gdb_continue_to_breakpoint "Break at multiply @6" \
81 ".*Break at multiply.*"
82 gdb_test "python print (blist\[1\].hit_count)" \
83 "6" "Check breakpoint hit count @6"
84 gdb_test "print result" \
85 " = 545" "Check expected variable result after 6 iterations"
86
87 # Test breakpoint is enabled and disabled correctly..
88 gdb_breakpoint [gdb_get_line_number "Break at add."]
89 gdb_continue_to_breakpoint "Break at add 1" ".*Break at add.*"
90 gdb_test "python print (blist\[1\].enabled)" \
91 "True" "Check breakpoint enabled."
92 gdb_py_test_silent_cmd "python blist\[1\].enabled = False" \
93 "Set breakpoint disabled." 0
94 gdb_continue_to_breakpoint "Break at add 2" ".*Break at add.*"
95 gdb_py_test_silent_cmd "python blist\[1\].enabled = True" \
96 "Set breakpoint enabled." 0
97 gdb_continue_to_breakpoint "Break at multiply after re-enable" \
98 ".*Break at multiply.*"
99
100 # Test other getters and setters.
101 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
102 "Get Breakpoint List" 0
103 gdb_test "python print (blist\[1\].thread)" \
104 "None" "Check breakpoint thread"
105 gdb_test "python print (blist\[1\].type == gdb.BP_BREAKPOINT)" \
106 "True" "Check breakpoint type"
107 gdb_test "python print (blist\[0\].number)" \
108 "1" "Check breakpoint number 0"
109 gdb_test "python print (blist\[1\].number)" \
110 "2" "Check breakpoint number 1"
111 gdb_test "python print (blist\[2\].number)" \
112 "3" "Check breakpoint number 2"
113 }
114 }
115
116 proc test_bkpt_deletion { } {
117 global srcfile testfile hex decimal
118
119 with_test_prefix test_bkpt_deletion {
120 # Start with a fresh gdb.
121 clean_restart ${testfile}
122
123 if ![runto_main] then {
124 fail "Cannot run to main."
125 return 0
126 }
127
128 # Test breakpoints are deleted correctly.
129 set deltst_location [gdb_get_line_number "Break at multiply."]
130 set end_location [gdb_get_line_number "Break at end."]
131 gdb_py_test_silent_cmd "python dp1 = gdb.Breakpoint (\"$deltst_location\")" \
132 "Set breakpoint" 0
133 gdb_breakpoint [gdb_get_line_number "Break at end."]
134 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
135 "Get Breakpoint List" 0
136 gdb_test "python print (len(del_list))" \
137 "3" "Number of breakpoints before delete"
138 gdb_continue_to_breakpoint "Break at multiply." \
139 ".*$srcfile:$deltst_location.*"
140 gdb_py_test_silent_cmd "python dp1.delete()" \
141 "Delete Breakpoint" 0
142 gdb_test "python print (dp1.number)" \
143 "RuntimeError: Breakpoint 2 is invalid.*" \
144 "Check breakpoint invalidated"
145 gdb_py_test_silent_cmd "python del_list = gdb.breakpoints()" \
146 "Get Breakpoint List" 0
147 gdb_test "python print (len(del_list))" \
148 "2" "Number of breakpoints after delete"
149 gdb_continue_to_breakpoint "Break at end." \
150 ".*$srcfile:$end_location.*"
151 }
152 }
153
154 proc test_bkpt_cond_and_cmds { } {
155 global srcfile testfile hex decimal
156
157 with_test_prefix test_bkpt_cond_and_cmds {
158 # Start with a fresh gdb.
159 clean_restart ${testfile}
160
161 if ![runto_main] then {
162 fail "Cannot run to main."
163 return 0
164 }
165
166 # Test conditional setting.
167 set bp_location1 [gdb_get_line_number "Break at multiply."]
168 gdb_py_test_silent_cmd "python bp1 = gdb.Breakpoint (\"$bp_location1\")" \
169 "Set breakpoint" 0
170 gdb_continue_to_breakpoint "Break at multiply" \
171 ".*Break at multiply.*"
172 gdb_py_test_silent_cmd "python bp1.condition = \"i == 5\"" \
173 "Set breakpoint" 0
174 gdb_test "python print (bp1.condition)" "i == 5" \
175 "Test conditional has been set"
176 gdb_continue_to_breakpoint "Break at multiply @5" \
177 ".*Break at multiply.*"
178 gdb_test "print i" \
179 "5" "Test conditional breakpoint stopped after five iterations"
180 gdb_py_test_silent_cmd "python bp1.condition = None" \
181 "Clear condition" 0
182 gdb_test "python print (bp1.condition)" \
183 "None" "Test conditional read"
184 gdb_continue_to_breakpoint "Break at multiply @6" \
185 ".*Break at multiply.*"
186 gdb_test "print i" \
187 "6" "Test breakpoint stopped after six iterations"
188
189 # Test commands.
190 gdb_breakpoint [gdb_get_line_number "Break at add."]
191 set test {commands $bpnum}
192 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
193 set test {print "Command for breakpoint has been executed."}
194 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
195 set test {print result}
196 gdb_test_multiple $test $test { -re "\r\n>$" { pass $test } }
197 gdb_test "end"
198
199 gdb_py_test_silent_cmd "python blist = gdb.breakpoints()" \
200 "Get Breakpoint List" 0
201 gdb_test "python print (blist\[len(blist)-1\].commands)" \
202 "print \"Command for breakpoint has been executed.\".*print result"
203 }
204 }
205
206 proc test_bkpt_invisible { } {
207 global srcfile testfile hex decimal
208
209 with_test_prefix test_bkpt_invisible {
210 # Start with a fresh gdb.
211 clean_restart ${testfile}
212
213 if ![runto_main] then {
214 fail "Cannot run to main."
215 return 0
216 }
217
218 delete_breakpoints
219 set ibp_location [gdb_get_line_number "Break at multiply."]
220 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=False)" \
221 "Set invisible breakpoint" 0
222 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
223 "Get Breakpoint List" 0
224 gdb_test "python print (ilist\[0\])" \
225 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 1"
226 gdb_test "python print (ilist\[0\].location)" \
227 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 1"
228 gdb_test "python print (ilist\[0\].visible)" \
229 "True" "Check breakpoint visibility 1"
230 gdb_test "info breakpoints" "py-breakpoint\.c:$ibp_location.*" \
231 "Check info breakpoints shows visible breakpoints"
232 delete_breakpoints
233 gdb_py_test_silent_cmd "python ibp = gdb.Breakpoint(\"$ibp_location\", internal=True)" \
234 "Set invisible breakpoint" 0
235 gdb_py_test_silent_cmd "python ilist = gdb.breakpoints()" \
236 "Get Breakpoint List" 0
237 gdb_test "python print (ilist\[0\])" \
238 "<gdb.Breakpoint object at $hex>" "Check invisible bp obj exists 2"
239 gdb_test "python print (ilist\[0\].location)" \
240 "py-breakpoint\.c:$ibp_location*" "Check breakpoint location 2"
241 gdb_test "python print (ilist\[0\].visible)" \
242 "False" "Check breakpoint visibility 2"
243 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
244 "Check info breakpoints does not show invisible breakpoints"
245 gdb_test "maint info breakpoints" \
246 "py-breakpoint\.c:$ibp_location.*" \
247 "Check maint info breakpoints shows invisible breakpoints"
248 }
249 }
250
251 proc test_watchpoints { } {
252 global srcfile testfile hex decimal
253
254 with_test_prefix test_watchpoints {
255 # Start with a fresh gdb.
256 clean_restart ${testfile}
257
258 # Disable hardware watchpoints if necessary.
259 if [target_info exists gdb,no_hardware_watchpoints] {
260 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
261 }
262
263 if ![runto_main] then {
264 fail "Cannot run to main."
265 return 0
266 }
267
268 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE )" \
269 "Set watchpoint" 0
270 gdb_test "continue" \
271 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*main.*" \
272 "Test watchpoint write"
273 }
274 }
275
276 proc test_bkpt_internal { } {
277 global srcfile testfile hex decimal
278
279 with_test_prefix test_bkpt_internal {
280 # Start with a fresh gdb.
281 clean_restart ${testfile}
282
283 # Disable hardware watchpoints if necessary.
284 if [target_info exists gdb,no_hardware_watchpoints] {
285 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
286 }
287 if ![runto_main] then {
288 fail "Cannot run to main."
289 return 0
290 }
291 delete_breakpoints
292 gdb_py_test_silent_cmd "python wp1 = gdb.Breakpoint (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE, internal=True )" \
293 "Set watchpoint" 0
294 gdb_test "info breakpoints" \
295 "No breakpoints or watchpoints.*" \
296 "Check info breakpoints does not show invisible breakpoints"
297 gdb_test "maint info breakpoints" \
298 ".*watchpoint.*result.*" \
299 "Check maint info breakpoints shows invisible breakpoints"
300 gdb_test "continue" \
301 ".*\[Ww\]atchpoint.*result.*Old value = 0.*New value = 25.*" \
302 "Test watchpoint write"
303 }
304 }
305
306 proc test_bkpt_eval_funcs { } {
307 global srcfile testfile hex decimal
308
309 with_test_prefix test_bkpt_eval_funcs {
310 # Start with a fresh gdb.
311 clean_restart ${testfile}
312
313 # Disable hardware watchpoints if necessary.
314 if [target_info exists gdb,no_hardware_watchpoints] {
315 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
316 }
317 if ![runto_main] then {
318 fail "Cannot run to main."
319 return 0
320 }
321 delete_breakpoints
322
323 gdb_py_test_multiple "Sub-class a breakpoint" \
324 "python" "" \
325 "class bp_eval (gdb.Breakpoint):" "" \
326 " inf_i = 0" "" \
327 " count = 0" "" \
328 " def stop (self):" "" \
329 " self.count = self.count + 1" "" \
330 " self.inf_i = gdb.parse_and_eval(\"i\")" "" \
331 " if self.inf_i == 3:" "" \
332 " return True" "" \
333 " return False" "" \
334 "end" ""
335
336 gdb_py_test_multiple "Sub-class a second breakpoint" \
337 "python" "" \
338 "class bp_also_eval (gdb.Breakpoint):" "" \
339 " count = 0" "" \
340 " def stop (self):" "" \
341 " self.count = self.count + 1" "" \
342 " if self.count == 9:" "" \
343 " return True" "" \
344 " return False" "" \
345 "end" ""
346
347 gdb_py_test_multiple "Sub-class a third breakpoint" \
348 "python" "" \
349 "class basic (gdb.Breakpoint):" "" \
350 " count = 0" "" \
351 "end" ""
352
353 set bp_location2 [gdb_get_line_number "Break at multiply."]
354 set end_location [gdb_get_line_number "Break at end."]
355 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$bp_location2\")" \
356 "Set breakpoint" 0
357 gdb_py_test_silent_cmd "python also_eval_bp1 = bp_also_eval(\"$bp_location2\")" \
358 "Set breakpoint" 0
359 gdb_py_test_silent_cmd "python never_eval_bp1 = bp_also_eval(\"$end_location\")" \
360 "Set breakpoint" 0
361 gdb_continue_to_breakpoint "Break at multiply, i==3" \
362 ".*$srcfile:$bp_location2.*"
363 gdb_test "print i" \
364 "3" "Check inferior value matches python accounting"
365 gdb_test "python print (eval_bp1.inf_i)" \
366 "3" "Check python accounting matches inferior"
367 gdb_test "python print (also_eval_bp1.count)" "4" \
368 "Check non firing same-location also_eval_bp1 function was also called at each stop."
369 gdb_test "python print (eval_bp1.count)" "4" \
370 "Check non firing same-location eval_bp1 function was also called at each stop."
371
372 delete_breakpoints
373 set cond_bp [gdb_get_line_number "Break at multiply."]
374 gdb_py_test_silent_cmd "python eval_bp1 = bp_eval(\"$cond_bp\")" \
375 "Set breakpoint" 0
376 set test_cond {cond $bpnum}
377 gdb_test "$test_cond \"foo==3\"" \
378 "Only one stop condition allowed. There is currently a Python.*" \
379 "Check you cannot add a CLI condition to a Python breakpoint that has defined stop"
380 gdb_py_test_silent_cmd "python eval_bp2 = basic(\"$cond_bp\")" \
381 "Set breakpoint" 0
382 gdb_py_test_silent_cmd "python eval_bp2.condition = \"1==1\"" \
383 "Set a condition" 0
384 gdb_py_test_multiple "Construct an eval function" \
385 "python" "" \
386 "def stop_func ():" "" \
387 " return True" "" \
388 "end" ""
389
390 gdb_test "python eval_bp2.stop = stop_func" \
391 "RuntimeError: Only one stop condition allowed. There is currently a GDB.*" \
392 "Assign stop function to a breakpoint that has a condition"
393
394 delete_breakpoints
395 gdb_breakpoint [gdb_get_line_number "Break at multiply."]
396 gdb_py_test_silent_cmd "python check_eval = bp_eval(\"$bp_location2\")" \
397 "Set breakpoint" 0
398 gdb_test "python print (check_eval.count)" "0" \
399 "Test that evaluate function has not been yet executed (ie count = 0)"
400 gdb_continue_to_breakpoint "Break at multiply, count==1" \
401 ".*$srcfile:$bp_location2.*"
402 gdb_test "python print (check_eval.count)" "1" \
403 "Test that evaluate function is run when location also has normal bp"
404
405 gdb_py_test_multiple "Sub-class a watchpoint" \
406 "python" "" \
407 "class wp_eval (gdb.Breakpoint):" "" \
408 " def stop (self):" "" \
409 " self.result = gdb.parse_and_eval(\"result\")" "" \
410 " if self.result == 788:" "" \
411 " return True" "" \
412 " return False" "" \
413 "end" ""
414
415 delete_breakpoints
416 gdb_py_test_silent_cmd "python wp1 = wp_eval (\"result\", type=gdb.BP_WATCHPOINT, wp_class=gdb.WP_WRITE)" \
417 "Set watchpoint" 0
418 gdb_test "continue" \
419 ".*\[Ww\]atchpoint.*result.*Old value =.*New value = 788.*" \
420 "Test watchpoint write"
421 gdb_test "python print (never_eval_bp1.count)" "0" \
422 "Check that this unrelated breakpoints eval function was never called."
423 }
424 }
425
426 proc test_bkpt_temporary { } {
427 global srcfile testfile hex decimal
428
429 with_test_prefix test_bkpt_temporary {
430 # Start with a fresh gdb.
431 clean_restart ${testfile}
432
433 if ![runto_main] then {
434 fail "Cannot run to main."
435 return 0
436 }
437 delete_breakpoints
438
439 gdb_py_test_multiple "Sub-class and check temporary breakpoint" \
440 "python" "" \
441 "class temp_bp (gdb.Breakpoint):" "" \
442 " count = 0" "" \
443 " def stop (self):" "" \
444 " self.count = self.count + 1" "" \
445 " return True" "" \
446 "end" ""
447 set ibp_location [gdb_get_line_number "Break at multiply."]
448 gdb_py_test_silent_cmd "python ibp = temp_bp(\"$ibp_location\", temporary=True)" \
449 "Set temporary breakpoint" 0
450 gdb_test "info breakpoints" \
451 "2.*breakpoint.*del.*py-breakpoint\.c:$ibp_location.*" \
452 "Check info breakpoints shows breakpoint with temporary status"
453 gdb_test "python print (ibp.location)" "py-breakpoint\.c:$ibp_location*" \
454 "Check temporary breakpoint location"
455 gdb_test "python print (ibp.temporary)" "True" \
456 "Check breakpoint temporary status"
457 gdb_continue_to_breakpoint "Break at multiply." \
458 ".*$srcfile:$ibp_location.*"
459 gdb_test "python print (ibp.count)" "1" \
460 "Check temporary stop callback executed before deletion."
461 gdb_test "python print (ibp.temporary)" "RuntimeError: Breakpoint 2 is invalid.*" \
462 "Check temporary breakpoint is deleted after being hit"
463 gdb_test "info breakpoints" "No breakpoints or watchpoints.*" \
464 "Check info breakpoints shows temporary breakpoint is deleted"
465 }
466 }
467
468 # Test address locations.
469
470 proc test_bkpt_address {} {
471 global gdb_prompt decimal srcfile
472
473 # Delete all breakpoints
474 delete_breakpoints
475
476 gdb_test "python gdb.Breakpoint(\"*main\")" \
477 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
478
479 gdb_py_test_silent_cmd \
480 "python main_loc = gdb.parse_and_eval(\"main\").address" \
481 "eval address of main" 0
482
483 # Python 2 vs 3 ... Check `int' first. If that fails, try `long'.
484 gdb_test_multiple "python main_addr = int(main_loc)" "int value of main" {
485 -re "Traceback.*$gdb_prompt $" {
486 gdb_test_no_output "python main_addr = long(main_loc)" \
487 "long value of main"
488 }
489 -re "$gdb_prompt $" {
490 pass "int value of main"
491 }
492 }
493
494 # Include whitespace in the linespec to double-check proper
495 # grokking of argument to gdb.Breakpoint.
496 gdb_test "python gdb.Breakpoint(\" *{}\".format(str(main_addr)))" \
497 ".*Breakpoint ($decimal)+ at .*$srcfile, line ($decimal)+\."
498 }
499
500 test_bkpt_basic
501 test_bkpt_deletion
502 test_bkpt_cond_and_cmds
503 test_bkpt_invisible
504 test_watchpoints
505 test_bkpt_internal
506 test_bkpt_eval_funcs
507 test_bkpt_temporary
508 test_bkpt_address
This page took 0.041998 seconds and 4 git commands to generate.