1 # Copyright 1988-2020 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 # 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.
22 if { [prepare_for_testing "failed to prepare" "hbreak2" {break.c break1.c} {debug nowarnings}] } {
28 if ![runto_main] then { fail "break tests suppressed" }
32 # Test whether the target supports hardware breakpoints at all.
34 gdb_test_multiple "hbreak main" "hardware breakpoint support" {
35 -re "No hardware breakpoint support in the target.*$gdb_prompt $" {
36 unsupported "hardware breakpoints"
39 -re "Hardware breakpoints used exceeds limit.*$gdb_prompt $" {
40 unsupported "hardware breakpoints"
43 -re "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*$gdb_prompt $" {
44 pass "hardware breakpoint support"
48 gdb_test_multiple "" "hardware breakpoint insertion" {
49 -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 $" {
50 unsupported "hardware breakpoint insertion"
53 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:.*\[\r\n\]+.*\[\t \]+if .argc.* \{.*$gdb_prompt $" {
54 pass "hardware breakpoint insertion"
60 # Test simple hardware breakpoint setting commands.
64 # Test break at function.
66 gdb_test "hbreak main" \
67 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
68 "hardware breakpoint function"
72 # Test break at quoted function.
74 gdb_test "hbreak \"marker2\"" \
75 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
76 "hardware breakpoint quoted function"
80 # Test break at function in file.
82 gdb_test "hbreak $srcfile:factorial" \
83 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
84 "hardware breakpoint function in file"
87 set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
90 # Test break at line number.
92 # Note that the default source file is the last one whose source text
93 # was printed. For native debugging, before we've executed the
94 # program, this is the file containing main, but for remote debugging,
95 # it's wherever the processor was stopped when we connected to the
96 # board. So, to be sure, we do a list command.
98 gdb_test "list main" \
99 ".*main \\(int argc, char ..argv, char ..envp\\).*" \
100 "use `list' to establish default source file"
101 gdb_test "hbreak $bp_location1" \
102 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
103 "hardware breakpoint line number"
106 set bp_location2 [gdb_get_line_number "set breakpoint 2 here"]
109 # Test break at line number in file.
111 gdb_test "hbreak $srcfile:$bp_location2" \
112 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
113 "hardware breakpoint line number in file"
116 set bp_location3 [gdb_get_line_number "set breakpoint 3 here"]
117 set bp_location4 [gdb_get_line_number "set breakpoint 4 here"]
120 # Test putting a break at the start of a multi-line if conditional.
121 # Verify the breakpoint was put at the start of the conditional.
123 gdb_test "hbreak multi_line_if_conditional" \
124 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location3\\." \
125 "hardware breakpoint at start of multi line if conditional"
128 gdb_test "hbreak multi_line_while_conditional" \
129 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location4\\." \
130 "hardware breakpoint at start of multi line while conditional"
132 set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]
134 set main_line $bp_location6
136 set bp_location7 [gdb_get_line_number "set breakpoint 7 here"]
137 set bp_location8 [gdb_get_line_number "set breakpoint 8 here" $srcfile1]
139 gdb_test "info break" \
140 "Num Type\[ \]+Disp Enb Address\[ \]+What.*
141 \[0-9\]+\[\t \]+hw breakpoint keep y.* in multi_line_while_conditional at .*$srcfile:$bp_location4" \
142 "hardware breakpoint info"
146 # Run until the breakpoint at main is hit. For non-stubs-using targets.
148 gdb_test "hbreak main" \
149 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
150 "hardware breakpoint function (2)"
153 "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*" \
154 "run until function breakpoint"
158 # Run until the breakpoint at a line number.
160 gdb_test "hbreak $bp_location1" \
161 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
162 "hardware breakpoint line number (2)"
163 gdb_test "continue" \
164 "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location1.*$bp_location1\[\t \]+printf.*factorial.*" \
165 "run until breakpoint set at a line number"
169 # Run until the breakpoint set in a function in a file.
171 gdb_test "hbreak $srcfile:factorial" \
172 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
173 "hardware breakpoint function in file (2)"
174 for {set i 6} {$i >= 1} {incr i -1} {
175 gdb_test "continue" \
176 "Continuing\\..*Breakpoint \[0-9\]+, factorial \\(value=$i\\) at .*$srcfile:$bp_location7.*$bp_location7\[\t \]+.*if .value > 1. \{.*" \
177 "run until file:function($i) breakpoint"
182 # Run until the breakpoint set at a quoted function.
184 gdb_test "hbreak \"marker2\"" \
185 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
186 "hardware breakpoint quoted function (2)"
187 gdb_test "continue" \
188 "Continuing\\..*Breakpoint \[0-9\]+, (0x\[0-9a-f\]+ in )?marker2 \\(a=43\\) at .*$srcfile1:$bp_location8.*" \
189 "run until quoted breakpoint"
192 # Run until the file:function breakpoint at a line number in a file.
194 gdb_test "hbreak $srcfile:$bp_location2" \
195 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
196 "hardware breakpoint line number in file (2)"
197 gdb_test "continue" \
198 "Continuing\\..*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location2.*$bp_location2\[\t \]+argc = \\(argc == 12345\\);.*" \
199 "run until file:linenum breakpoint"
202 # Test break at offset +1.
203 set bp_location10 [gdb_get_line_number "set breakpoint 10 here"]
205 gdb_test "hbreak +1" \
206 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10\\." \
207 "hardware breakpoint offset +1"
209 # Check to see if breakpoint is hit when stepped onto.
212 ".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location10.*$bp_location10\[\t \]+return argc;.*breakpoint 10 here.*" \
213 "step onto hardware breakpoint"
216 # Check to see if breakpoint can be set on ending brace of function.
217 set bp_location10a [gdb_get_line_number "set breakpoint 10a here"]
219 gdb_test "hbreak $bp_location10a" \
220 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10a\\." \
221 "setting hardware breakpoint at }"
223 gdb_test "continue" \
224 ".*Breakpoint \[0-9\]+, main \\(argc=.*, argv=.*, envp=.*\\) at .*$srcfile:$bp_location10a.*$bp_location10a\[\t \]+}.*breakpoint 10a here.*" \
225 "continue to hardware breakpoint at }"
228 # Delete all breakpoints so we can start over, course this can be a test too.
233 # Test temporary breakpoint at function.
236 gdb_test "thbreak main" \
237 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
238 "temporary hardware breakpoint function"
242 # Test break at function in file.
245 gdb_test "thbreak $srcfile:factorial" \
246 "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
247 "temporary hardware breakpoint function in file"
251 # Test break at line number.
253 gdb_test "thbreak $bp_location1" \
254 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1.*" \
255 "temporary hardware breakpoint line number #1"
258 gdb_test "thbreak $bp_location6" \
259 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location6.*" \
260 "temporary hardware breakpoint line number #2"
264 # Test break at line number in file.
266 gdb_test "thbreak $srcfile:$bp_location2" \
267 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2.*" \
268 "temporary hardware breakpoint line number in file #1"
271 set bp_location11 [gdb_get_line_number "set breakpoint 11 here"]
272 gdb_test "thbreak $srcfile:$bp_location11" \
273 "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location11.*" \
274 "temporary hardware breakpoint line number in file #2"
277 # Check to see what breakpoints are set (temporary this time).
279 gdb_test "info break" \
280 "Num Type.*Disp Enb Address.*What.*\[\r\n\]
281 \[0-9\]+\[\t \]+hw breakpoint del.*y.*in main at .*$srcfile:$bp_location11.*" \
282 "temporary hardware breakpoint info"
287 if ![runto_main] then { fail "break tests suppressed" }
289 # Verify that GDB responds gracefully when asked to set a breakpoint
290 # on a nonexistent source line.
292 gdb_test_no_output "set breakpoint pending off"
293 gdb_test "hbreak 999" \
294 "No line 999 in the current file." \
295 "hardware break on non-existent source line"
297 # Run to the desired default location. If not positioned here, the
298 # tests below don't work.
300 gdb_test "until $bp_location1" "main .* at .*:$bp_location1.*" \
304 # Verify that GDB allows one to just say "hbreak", which is treated
305 # as the "default" breakpoint.
307 gdb_test "hbreak" "Hardware assisted breakpoint \[0-9\]*.*" \
308 "hardware break on default location"
310 # Verify that a "silent" breakpoint can be set, and that GDB is indeed
311 # "silent" about its triggering.
313 if ![runto_main] then { fail "break tests suppressed" }
315 gdb_test_multiple "hbreak $bp_location1" \
316 "set to-be-silent hardware break bp_location1" {
317 -re "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location1.*$gdb_prompt $" {
318 pass "set to-be-silent hardware break bp_location1"
322 gdb_test "commands $expect_out(1,string)\nsilent\nend" ">end" "set silent break bp_location1"
324 gdb_test "info break $expect_out(1,string)" \
325 "\[0-9\]*\[ \t\]*hw breakpoint.*:$bp_location1\r\n\[ \t\]*silent.*" \
326 "info silent hardware break bp_location1"
328 gdb_test "continue" "Continuing." \
329 "hit silent hardware break bp_location1"
331 gdb_test "bt" "#0 main .* at .*:$bp_location1.*" \
332 "stopped for silent hardware break bp_location1"
334 # Verify that GDB can at least parse a breakpoint with the
335 # "thread" keyword. (We won't attempt to test here that a
336 # thread-specific breakpoint really triggers appropriately.
337 # The gdb.threads subdirectory contains tests for that.)
339 set bp_location12 [gdb_get_line_number "set breakpoint 12 here"]
340 gdb_test "hbreak $bp_location12 thread 999" "Unknown thread 999.*" \
341 "thread-specific hardware breakpoint on non-existent thread disallowed"
343 gdb_test "hbreak $bp_location12 thread foo" \
344 "Invalid thread ID: foo" \
345 "thread-specific hardware breakpoint on bogus thread ID disallowed"
347 # Verify that GDB responds gracefully to a breakpoint command with
350 gdb_test "hbreak $bp_location12 foo" \
351 "malformed linespec error: unexpected string, \"foo\".*" \
352 "hardware breakpoint with trailing garbage disallowed"
354 # Verify that GDB responds gracefully to a "clear" command that has
355 # no matching breakpoint. (First, get us off the current source line,
356 # which we know has a breakpoint.)
358 gdb_test "next" "marker1.*" "step over hardware breakpoint"
360 gdb_test "clear 81" "No breakpoint at 81.*" \
361 "clear line has no breakpoint disallowed"
363 gdb_test "clear" "No breakpoint at this line.*" \
364 "clear current line has no breakpoint disallowed"
367 # Verify that a breakpoint can be set via a convenience variable.
369 gdb_test_no_output "set \$foo=$bp_location11" \
370 "set convenience variable \$foo to bp_location11"
372 gdb_test "hbreak \$foo" \
373 "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location11.*" \
374 "set hardware breakpoint via convenience variable"
377 # Verify that GDB responds gracefully to an attempt to set a
378 # breakpoint via a convenience variable whose type is not integer.
380 gdb_test_no_output "set \$foo=81.5" \
381 "set convenience variable \$foo to 81.5"
383 gdb_test "hbreak \$foo" \
384 "Convenience variables used in line specs must have integer values.*" \
385 "set hardware breakpoint via non-integer convenience variable disallowed"
387 # Verify that we can set and trigger a breakpoint in a user-called function.
389 gdb_test "hbreak marker2" \
390 "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location8.*" \
391 "set hardware breakpoint on to-be-called function"
393 gdb_test "print marker2(99)" \
394 "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.*" \
395 "hit hardware breakpoint on called function"
397 # As long as we're stopped (breakpointed) in a called function,
398 # verify that we can successfully backtrace & such from here.
400 "#0\[ \t\]*($hex in )?marker2.*:$bp_location8\r\n#1\[ \t\]*<function called from gdb>.*" \
401 "backtrace while in called function"
403 # Return from the called function. For remote targets, it's important to do
404 # this before runto_main, which otherwise may silently stop on the dummy
405 # breakpoint inserted by GDB at the program's entry point.
407 gdb_test_multiple "finish" "finish from called function" {
408 -re "Run till exit from .*marker2.* at .*$bp_location8\r\n.*function called from gdb.*$gdb_prompt $" {
409 pass "finish from called function"
411 -re "Run till exit from .*marker2.* at .*$bp_location8\r\n.*Value returned.*$gdb_prompt $" {
412 pass "finish from called function"
420 # Test "next" over recursive function call.
423 proc test_next_with_recursion {} {
430 # Can't set a hardware breakpoint without a live target, so do it now
431 # before it's killed below.
432 gdb_test "hbreak factorial" \
433 "Hardware assisted breakpoint $decimal at .*" \
434 "hardware break at factorial"
436 gdb_test "kill" "" "kill program" \
437 "Kill the program being debugged.*y or n. $" "y"
439 # Run until we call factorial with 6
443 "Break.* factorial .value=6. .*" \
444 "run to factorial(6)"] then { gdb_suppress_tests }
446 # Continue until we call factorial recursively with 5.
448 if [gdb_test "continue" \
449 "Continuing.*Break.* factorial .value=5. .*" \
450 "continue to factorial(5)"] then { gdb_suppress_tests }
452 # Do a backtrace just to confirm how many levels deep we are.
454 if [gdb_test "backtrace" \
455 "#0\[ \t\]+ factorial .value=5..*" \
456 "backtrace from factorial(5)"] then { gdb_suppress_tests }
458 # Now a "next" should position us at the recursive call, which
459 # we will be performing with 4.
461 if [gdb_test "next" \
462 ".* factorial .value - 1.;.*" \
463 "next to recursive call"] then { gdb_suppress_tests }
465 # Disable the breakpoint at the entry to factorial by deleting them all.
466 # The "next" should run until we return to the next line from this
467 # recursive call to factorial with 4.
468 # Buggy versions of gdb will stop instead at the innermost frame on
469 # the line where we are trying to "next" to.
473 if [istarget "mips*tx39-*"] {
476 # We used to set timeout here for all other targets as well. This
477 # is almost certainly wrong. The proper timeout depends on the
478 # target system in use, and how we communicate with it, so there
479 # is no single value appropriate for all targets. The timeout
480 # should be established by the Dejagnu config file(s) for the
481 # board, and respected by the test suite.
483 # For example, if I'm running GDB over an SSH tunnel talking to a
484 # portmaster in California talking to an ancient 68k board running
485 # a crummy ROM monitor (a situation I can only wish were
486 # hypothetical), then I need a large timeout. But that's not the
487 # kind of knowledge that belongs in this file.
489 gdb_test next "\[0-9\]*\[\t \]+return \\(value\\);.*" \
490 "next over recursive call"
492 # OK, we should be back in the same stack frame we started from.
493 # Do a backtrace just to confirm.
495 set result [gdb_test "backtrace" \
496 "#0\[ \t\]+ factorial .value=120.*\r\n#1\[ \t\]+ \[0-9a-fx\]+ in factorial .value=6..*" \
497 "backtrace from factorial(5.1)"]
498 if { $result != 0 } { gdb_suppress_tests }
500 if [target_info exists gdb,noresults] { gdb_suppress_tests }
501 gdb_continue_to_end "recursive next test"
502 gdb_stop_suppressing_tests
505 test_next_with_recursion
510 # Build a new file with optimization enabled so that we can try breakpoints
511 # on targets with optimized prologues.
513 if { [prepare_for_testing "failed to prepare" "hbreak2o2" {break.c break1.c} {debug nowarnings optimize=-O2}] } {
517 if ![runto_main] then { fail "break tests suppressed" }
521 # Test break at function.
523 gdb_test "hbreak main" \
524 "Hardware assisted breakpoint.*at.* file .*, line.*" \
525 "hardware breakpoint function, optimized file"
528 # Run until the breakpoint at main is hit. For non-stubs-using targets.
531 gdb_test_multiple "" "run until hardware function breakpoint, optimized file" {
532 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*$gdb_prompt $" {
533 pass "run until hardware function breakpoint, optimized file"
535 -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $" {
536 pass "run until hardware function breakpoint, optimized file (code motion)"
542 # Test break at function.
544 gdb_test "hbreak marker4" \
545 "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
546 "hardware breakpoint small function, optimized file"
549 # Run until the breakpoint at a small function.
553 # Add a second pass pattern. The behavior differs here between stabs
554 # and dwarf for one-line functions. Stabs preserves two line symbols
555 # (one before the prologue and one after) with the same line number,
556 # but dwarf regards these as duplicates and discards one of them.
557 # Therefore the address after the prologue (where the breakpoint is)
558 # has no exactly matching line symbol, and GDB reports the breakpoint
559 # as if it were in the middle of a line rather than at the beginning.
561 set bp_location14 [gdb_get_line_number "set breakpoint 14 here" $srcfile1]
563 gdb_test_multiple "continue" \
564 "run until hardware breakpoint set at small function, optimized file" {
565 -re "Breakpoint $decimal, marker4 \\(d=(d@entry=)?177601976\\) at .*$srcfile1:$bp_location14\[\r\n\]+$bp_location14\[\t \]+void marker4.*" {
566 pass "run until hardware breakpoint set at small function, optimized file (line bp_location14)"
568 -re "Breakpoint $decimal, factorial \\(.*\\) .*\{\r\n$gdb_prompt" {
569 # GCC 4.3 emits bad line number information - see gcc/36748.
570 if { [test_compiler_info "gcc-4-3-*"] } {
573 fail "run until hardware breakpoint set at small function, optimized file"