]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/hbreak2.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / hbreak2.exp
1 # Copyright 1988-2023 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 # Based on break.exp by Rob Savoye. (rob@cygnus.com)
17 # Hardware breakpoint support by Maciej W. Rozycki and Daniel Jacobowitz.
18 # Only one hardware breakpoint is set at a time as targets may limit
19 # the number available.
20
21
22 if { [prepare_for_testing "failed to prepare" "hbreak2" {break.c break1.c} {debug nowarnings}] } {
23 return -1
24 }
25 set srcfile break.c
26 set srcfile1 break1.c
27
28 if {![runto_main]} {
29 return
30 }
31
32 delete_breakpoints
33
34 #
35 # Test whether the target supports hardware breakpoints at all.
36 #
37 gdb_test_multiple "hbreak -q main" "hardware breakpoint support" {
38 -re "No hardware breakpoint support in the target.*$gdb_prompt $" {
39 unsupported "hardware breakpoints"
40 return
41 }
42 -re "Hardware breakpoints used exceeds limit.*$gdb_prompt $" {
43 unsupported "hardware breakpoints"
44 return
45 }
46 -re "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*$gdb_prompt $" {
47 pass "hardware breakpoint support"
48 }
49 }
50 gdb_run_cmd
51 gdb_test_multiple "" "hardware breakpoint insertion" {
52 -re "Warning:\[\r\n\]+Cannot insert hardware breakpoint \[0-9\]+\.\[\r\n\]+Could not insert hardware breakpoints:\[\r\n\]+You may have requested too many hardware breakpoints/watchpoints\.\[\r\n\]+.*$gdb_prompt $" {
53 unsupported "hardware breakpoint insertion"
54 return
55 }
56 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:.*\[\r\n\]+.*\[\t \]+if .argc.* \{.*$gdb_prompt $" {
57 pass "hardware breakpoint insertion"
58 }
59 }
60 delete_breakpoints
61
62 #
63 # Test simple hardware breakpoint setting commands.
64 #
65
66 #
67 # Test break at function.
68 #
69 gdb_test "hbreak -q main" \
70 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
71 "hardware breakpoint function"
72 delete_breakpoints
73
74 #
75 # Test break at quoted function.
76 #
77 gdb_test "hbreak \"marker2\"" \
78 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
79 "hardware breakpoint quoted function"
80 delete_breakpoints
81
82 #
83 # Test break at function in file.
84 #
85 gdb_test "hbreak $srcfile:factorial" \
86 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
87 "hardware breakpoint function in file"
88 delete_breakpoints
89
90 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
91
92 #
93 # Test break at line number.
94 #
95 # Note that the default source file is the last one whose source text
96 # was printed. For native debugging, before we've executed the
97 # program, this is the file containing main, but for remote debugging,
98 # it's wherever the processor was stopped when we connected to the
99 # board. So, to be sure, we do a list command.
100 #
101 gdb_test "list -q main" \
102 ".*main \\(int argc, char ..argv, char ..envp\\).*" \
103 "use `list' to establish default source file"
104 gdb_test "hbreak $bp_location1" \
105 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
106 "hardware breakpoint line number"
107 delete_breakpoints
108
109 set bp_location2 [gdb_get_line_number "set breakpoint 2 here"]
110
111 #
112 # Test break at line number in file.
113 #
114 gdb_test "hbreak $srcfile:$bp_location2" \
115 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
116 "hardware breakpoint line number in file"
117 delete_breakpoints
118
119 set bp_location3 [gdb_get_line_number "set breakpoint 3 here"]
120 set bp_location4 [gdb_get_line_number "set breakpoint 4 here"]
121
122 #
123 # Test putting a break at the start of a multi-line if conditional.
124 # Verify the breakpoint was put at the start of the conditional.
125 #
126 gdb_test "hbreak multi_line_if_conditional" \
127 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location3\\." \
128 "hardware breakpoint at start of multi line if conditional"
129 delete_breakpoints
130
131 gdb_test "hbreak multi_line_while_conditional" \
132 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location4\\." \
133 "hardware breakpoint at start of multi line while conditional"
134
135 set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]
136
137 set main_line $bp_location6
138
139 set bp_location7 [gdb_get_line_number "set breakpoint 7 here"]
140 set bp_location8 [gdb_get_line_number "set breakpoint 8 here" $srcfile1]
141
142 gdb_test "info break" \
143 "Num Type\[ \]+Disp Enb Address\[ \]+What.*
144 \[0-9\]+\[\t \]+hw breakpoint keep y.* in multi_line_while_conditional at .*$srcfile:$bp_location4" \
145 "hardware breakpoint info"
146 delete_breakpoints
147
148 #
149 # Run until the breakpoint at main is hit. For non-stubs-using targets.
150 #
151 gdb_test "hbreak -q main" \
152 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
153 "hardware breakpoint function (2)"
154 gdb_run_cmd
155 gdb_test "" \
156 "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*" \
157 "run until function breakpoint"
158 delete_breakpoints
159
160 #
161 # Run until the breakpoint at a line number.
162 #
163 gdb_test "hbreak $bp_location1" \
164 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
165 "hardware breakpoint line number (2)"
166 gdb_test "continue" \
167 "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location1.*$bp_location1\[\t \]+printf.*factorial.*" \
168 "run until breakpoint set at a line number"
169 delete_breakpoints
170
171 #
172 # Run until the breakpoint set in a function in a file.
173 #
174 gdb_test "hbreak $srcfile:factorial" \
175 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
176 "hardware breakpoint function in file (2)"
177 for {set i 6} {$i >= 1} {incr i -1} {
178 gdb_test "continue" \
179 "Continuing\\..*Breakpoint \[0-9\]+, factorial \\(value=$i\\) at .*$srcfile:$bp_location7.*$bp_location7\[\t \]+.*if .value > 1. \{.*" \
180 "run until file:function($i) breakpoint"
181 }
182 delete_breakpoints
183
184 #
185 # Run until the breakpoint set at a quoted function.
186 #
187 gdb_test "hbreak \"marker2\"" \
188 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
189 "hardware breakpoint quoted function (2)"
190 gdb_test "continue" \
191 "Continuing\\..*Breakpoint \[0-9\]+, (0x\[0-9a-f\]+ in )?marker2 \\(a=43\\) at .*$srcfile1:$bp_location8.*" \
192 "run until quoted breakpoint"
193 delete_breakpoints
194 #
195 # Run until the file:function breakpoint at a line number in a file.
196 #
197 gdb_test "hbreak $srcfile:$bp_location2" \
198 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
199 "hardware breakpoint line number in file (2)"
200 gdb_test "continue" \
201 "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location2.*$bp_location2\[\t \]+argc = \\(argc == 12345\\);.*" \
202 "run until file:linenum breakpoint"
203 delete_breakpoints
204
205 # Test break at offset +1.
206 set bp_location10 [gdb_get_line_number "set breakpoint 10 here"]
207
208 gdb_test "hbreak +1" \
209 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10\\." \
210 "hardware breakpoint offset +1"
211
212 # Check to see if breakpoint is hit when stepped onto.
213
214 gdb_test "step" \
215 ".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location10.*$bp_location10\[\t \]+return argc;.*breakpoint 10 here.*" \
216 "step onto hardware breakpoint"
217 delete_breakpoints
218
219 # Check to see if breakpoint can be set on ending brace of function.
220 set bp_location10a [gdb_get_line_number "set breakpoint 10a here"]
221
222 gdb_test "hbreak $bp_location10a" \
223 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10a\\." \
224 "setting hardware breakpoint at }"
225
226 gdb_test "continue" \
227 ".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location10a.*$bp_location10a\[\t \]+}.*breakpoint 10a here.*" \
228 "continue to hardware breakpoint at }"
229
230 #
231 # Delete all breakpoints so we can start over, course this can be a test too.
232 #
233 delete_breakpoints
234
235 #
236 # Test temporary breakpoint at function.
237 #
238
239 gdb_test "thbreak -q main" \
240 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
241 "temporary hardware breakpoint function"
242 delete_breakpoints
243
244 #
245 # Test break at function in file.
246 #
247
248 gdb_test "thbreak $srcfile:factorial" \
249 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
250 "temporary hardware breakpoint function in file"
251 delete_breakpoints
252
253 #
254 # Test break at line number.
255 #
256 gdb_test "thbreak $bp_location1" \
257 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1.*" \
258 "temporary hardware breakpoint line number #1"
259 delete_breakpoints
260
261 gdb_test "thbreak $bp_location6" \
262 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location6.*" \
263 "temporary hardware breakpoint line number #2"
264 delete_breakpoints
265
266 #
267 # Test break at line number in file.
268 #
269 gdb_test "thbreak $srcfile:$bp_location2" \
270 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2.*" \
271 "temporary hardware breakpoint line number in file #1"
272 delete_breakpoints
273
274 set bp_location11 [gdb_get_line_number "set breakpoint 11 here"]
275 gdb_test "thbreak $srcfile:$bp_location11" \
276 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location11.*" \
277 "temporary hardware breakpoint line number in file #2"
278
279 #
280 # Check to see what breakpoints are set (temporary this time).
281 #
282 gdb_test "info break" \
283 "Num Type.*Disp Enb Address.*What.*\[\r\n\]
284 \[0-9\]+\[\t \]+hw breakpoint del.*y.*in main at .*$srcfile:$bp_location11.*" \
285 "temporary hardware breakpoint info"
286
287
288 #***********
289
290 if {![runto_main]} {
291 return
292 }
293
294 # Verify that GDB responds gracefully when asked to set a breakpoint
295 # on a nonexistent source line.
296 #
297 gdb_test_no_output "set breakpoint pending off"
298 gdb_test "hbreak 999" \
299 "No line 999 in the current file." \
300 "hardware break on non-existent source line"
301
302 # Run to the desired default location. If not positioned here, the
303 # tests below don't work.
304 #
305 gdb_test "until $bp_location1" "main .* at .*:$bp_location1.*" \
306 "until bp_location1"
307
308
309 # Verify that GDB allows one to just say "hbreak", which is treated
310 # as the "default" breakpoint.
311 #
312 gdb_test "hbreak" "Hardware assisted breakpoint \[0-9\]*.*" \
313 "hardware break on default location"
314
315 # Verify that a "silent" breakpoint can be set, and that GDB is indeed
316 # "silent" about its triggering.
317 #
318 if {![runto_main]} {
319 return
320 }
321
322 gdb_test_multiple "hbreak $bp_location1" \
323 "set to-be-silent hardware break bp_location1" {
324 -re "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location1.*$gdb_prompt $" {
325 pass "set to-be-silent hardware break bp_location1"
326 }
327 }
328
329 gdb_test "commands $expect_out(1,string)\nsilent\nend" ">end" "set silent break bp_location1"
330
331 gdb_test "info break $expect_out(1,string)" \
332 "\[0-9\]*\[ \t\]*hw breakpoint.*:$bp_location1\r\n\[ \t\]*silent.*" \
333 "info silent hardware break bp_location1"
334
335 gdb_test "continue" "Continuing." \
336 "hit silent hardware break bp_location1"
337
338 gdb_test "bt" "#0 main .* at .*:$bp_location1.*" \
339 "stopped for silent hardware break bp_location1"
340
341 # Verify that GDB can at least parse a breakpoint with the
342 # "thread" keyword. (We won't attempt to test here that a
343 # thread-specific breakpoint really triggers appropriately.
344 # The gdb.threads subdirectory contains tests for that.)
345 #
346 set bp_location12 [gdb_get_line_number "set breakpoint 12 here"]
347 gdb_test "hbreak $bp_location12 thread 999" "Unknown thread 999.*" \
348 "thread-specific hardware breakpoint on non-existent thread disallowed"
349
350 gdb_test "hbreak $bp_location12 thread foo" \
351 "Invalid thread ID: foo" \
352 "thread-specific hardware breakpoint on bogus thread ID disallowed"
353
354 # Verify that GDB responds gracefully to a breakpoint command with
355 # trailing garbage.
356 #
357 gdb_test "hbreak $bp_location12 foo" \
358 "malformed linespec error: unexpected string, \"foo\".*" \
359 "hardware breakpoint with trailing garbage disallowed"
360
361 # Verify that GDB responds gracefully to a "clear" command that has
362 # no matching breakpoint. (First, get us off the current source line,
363 # which we know has a breakpoint.)
364 #
365 gdb_test "next" "marker1.*" "step over hardware breakpoint"
366
367 gdb_test "clear 81" "No breakpoint at 81.*" \
368 "clear line has no breakpoint disallowed"
369
370 gdb_test "clear" "No breakpoint at this line.*" \
371 "clear current line has no breakpoint disallowed"
372 delete_breakpoints
373
374 # Verify that a breakpoint can be set via a convenience variable.
375 #
376 gdb_test_no_output "set \$foo=$bp_location11" \
377 "set convenience variable \$foo to bp_location11"
378
379 gdb_test "hbreak \$foo" \
380 "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location11.*" \
381 "set hardware breakpoint via convenience variable"
382 delete_breakpoints
383
384 # Verify that GDB responds gracefully to an attempt to set a
385 # breakpoint via a convenience variable whose type is not integer.
386 #
387 gdb_test_no_output "set \$foo=81.5" \
388 "set convenience variable \$foo to 81.5"
389
390 gdb_test "hbreak \$foo" \
391 "Convenience variables used in line specs must have integer values.*" \
392 "set hardware breakpoint via non-integer convenience variable disallowed"
393
394 # Verify that we can set and trigger a breakpoint in a user-called function.
395 #
396 gdb_test "hbreak marker2" \
397 "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location8.*" \
398 "set hardware breakpoint on to-be-called function"
399
400 gdb_test "print marker2(99)" \
401 "The program being debugged stopped while in a function called from GDB.\r\nEvaluation of the expression containing the function\r\n.marker2. will be abandoned.\r\nWhen the function is done executing, GDB will silently stop.*" \
402 "hit hardware breakpoint on called function"
403
404 # As long as we're stopped (breakpointed) in a called function,
405 # verify that we can successfully backtrace & such from here.
406 gdb_test "bt" \
407 "#0\[ \t\]*($hex in )?marker2.*:$bp_location8\r\n#1\[ \t\]*<function called from gdb>.*" \
408 "backtrace while in called function"
409
410 # Return from the called function. For remote targets, it's important to do
411 # this before runto_main, which otherwise may silently stop on the dummy
412 # breakpoint inserted by GDB at the program's entry point.
413 #
414 gdb_test_multiple "finish" "finish from called function" {
415 -re "Run till exit from .*marker2.* at .*$bp_location8\r\n.*function called from gdb.*$gdb_prompt $" {
416 pass "finish from called function"
417 }
418 -re "Run till exit from .*marker2.* at .*$bp_location8\r\n.*Value returned.*$gdb_prompt $" {
419 pass "finish from called function"
420 }
421 }
422
423 #********
424
425
426 #
427 # Test "next" over recursive function call.
428 #
429
430 proc test_next_with_recursion {} {
431 global gdb_prompt
432 global decimal
433 global binfile
434
435 delete_breakpoints
436
437 # Can't set a hardware breakpoint without a live target, so do it now
438 # before it's killed below.
439 gdb_test "hbreak factorial" \
440 "Hardware assisted breakpoint $decimal at .*" \
441 "hardware break at factorial"
442
443 gdb_test "kill" "" "kill program" \
444 "Kill the program being debugged.*y or n. $" "y"
445
446 # Run until we call factorial with 6
447
448 gdb_run_cmd
449 gdb_test "" \
450 "Break.* factorial .value=6. .*" \
451 "run to factorial(6)"
452
453 # Continue until we call factorial recursively with 5.
454
455 gdb_test "continue" \
456 "Continuing.*Break.* factorial .value=5. .*" \
457 "continue to factorial(5)"
458
459 # Do a backtrace just to confirm how many levels deep we are.
460
461 gdb_test "backtrace" \
462 "#0\[ \t\]+ factorial .value=5..*" \
463 "backtrace from factorial(5)"
464
465 # Now a "next" should position us at the recursive call, which
466 # we will be performing with 4.
467
468 gdb_test "next" \
469 ".* factorial .value - 1.;.*" \
470 "next to recursive call"
471
472 # Disable the breakpoint at the entry to factorial by deleting them all.
473 # The "next" should run until we return to the next line from this
474 # recursive call to factorial with 4.
475 # Buggy versions of gdb will stop instead at the innermost frame on
476 # the line where we are trying to "next" to.
477
478 delete_breakpoints
479
480 if [istarget "mips*tx39-*"] {
481 set timeout 60
482 }
483 # We used to set timeout here for all other targets as well. This
484 # is almost certainly wrong. The proper timeout depends on the
485 # target system in use, and how we communicate with it, so there
486 # is no single value appropriate for all targets. The timeout
487 # should be established by the Dejagnu config file(s) for the
488 # board, and respected by the test suite.
489 #
490 # For example, if I'm running GDB over an SSH tunnel talking to a
491 # portmaster in California talking to an ancient 68k board running
492 # a crummy ROM monitor (a situation I can only wish were
493 # hypothetical), then I need a large timeout. But that's not the
494 # kind of knowledge that belongs in this file.
495
496 gdb_test next "\[0-9\]*\[\t \]+return \\(value\\);.*" \
497 "next over recursive call"
498
499 # OK, we should be back in the same stack frame we started from.
500 # Do a backtrace just to confirm.
501
502 gdb_test "backtrace" \
503 "#0\[ \t\]+ factorial .value=120.*\r\n#1\[ \t\]+ \[0-9a-fx\]+ in factorial .value=6..*" \
504 "backtrace from factorial(5.1)"
505
506 if { ![target_info exists gdb,noresults] } {
507 gdb_continue_to_end "recursive next test"
508 }
509 }
510
511 test_next_with_recursion
512
513
514 #********
515
516 # Build a new file with optimization enabled so that we can try breakpoints
517 # on targets with optimized prologues.
518
519 if { [prepare_for_testing "failed to prepare" "hbreak2o2" {break.c break1.c} {debug nowarnings optimize=-O2}] } {
520 return -1
521 }
522
523 if {![runto_main]} {
524 return
525 }
526
527 delete_breakpoints
528
529 #
530 # Test break at function.
531 #
532 gdb_test "hbreak -q main" \
533 "Hardware assisted breakpoint.*at.* file .*, line.*" \
534 "hardware breakpoint function, optimized file"
535
536 #
537 # Run until the breakpoint at main is hit. For non-stubs-using targets.
538 #
539 gdb_run_cmd
540 gdb_test_multiple "" "run until hardware function breakpoint, optimized file" {
541 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*$gdb_prompt $" {
542 pass "run until hardware function breakpoint, optimized file"
543 }
544 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $" {
545 pass "run until hardware function breakpoint, optimized file (code motion)"
546 }
547 }
548 delete_breakpoints
549
550 #
551 # Test break at function.
552 #
553 gdb_test "hbreak marker4" \
554 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
555 "hardware breakpoint small function, optimized file"
556
557 #
558 # Run until the breakpoint at a small function.
559 #
560
561 #
562 # Add a second pass pattern. The behavior differs here between stabs
563 # and dwarf for one-line functions. Stabs preserves two line symbols
564 # (one before the prologue and one after) with the same line number,
565 # but dwarf regards these as duplicates and discards one of them.
566 # Therefore the address after the prologue (where the breakpoint is)
567 # has no exactly matching line symbol, and GDB reports the breakpoint
568 # as if it were in the middle of a line rather than at the beginning.
569
570 set bp_location14 [gdb_get_line_number "set breakpoint 14 here" $srcfile1]
571
572 gdb_test_multiple "continue" \
573 "run until hardware breakpoint set at small function, optimized file" {
574 -re "Breakpoint $decimal, marker4 \\(d=(d@entry=)?177601976\\) at .*$srcfile1:$bp_location14\[\r\n\]+$bp_location14\[\t \]+void marker4.*" {
575 pass "run until hardware breakpoint set at small function, optimized file (line bp_location14)"
576 }
577 -re "Breakpoint $decimal, factorial \\(.*\\) .*\{\r\n$gdb_prompt" {
578 # GCC 4.3 emits bad line number information - see gcc/36748.
579 if { [test_compiler_info "gcc-4-3-*"] } {
580 setup_xfail *-*-*
581 }
582 fail "run until hardware breakpoint set at small function, optimized file"
583 }
584 }