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