1#   Copyright 1988-2020 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
22if { [prepare_for_testing "failed to prepare" "hbreak2" {break.c break1.c} {debug nowarnings}] } {
23    return -1
24}
25set srcfile break.c
26set srcfile1 break1.c
27
28if ![runto_main] then { fail "break tests suppressed" }
29delete_breakpoints
30
31#
32# Test whether the target supports hardware breakpoints at all.
33#
34gdb_test_multiple "hbreak main" "hardware breakpoint support" {
35    -re "No hardware breakpoint support in the target.*$gdb_prompt $" {
36	unsupported "hardware breakpoints"
37	return
38    }
39    -re "Hardware breakpoints used exceeds limit.*$gdb_prompt $" {
40	unsupported "hardware breakpoints"
41	return
42    }
43    -re "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*$gdb_prompt $" {
44	pass "hardware breakpoint support"
45    }
46}
47gdb_run_cmd
48gdb_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"
51	return
52    }
53    -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:.*\[\r\n\]+.*\[\t \]+if .argc.* \{.*$gdb_prompt $" {
54	pass "hardware breakpoint insertion"
55    }
56}
57delete_breakpoints
58
59#
60# Test simple hardware breakpoint setting commands.
61#
62
63#
64# Test break at function.
65#
66gdb_test "hbreak main" \
67    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
68    "hardware breakpoint function"
69delete_breakpoints
70
71#
72# Test break at quoted function.
73#
74gdb_test "hbreak \"marker2\"" \
75    "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
76    "hardware breakpoint quoted function"
77delete_breakpoints
78
79#
80# Test break at function in file.
81#
82gdb_test "hbreak $srcfile:factorial" \
83    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
84    "hardware breakpoint function in file"
85delete_breakpoints
86
87set bp_location1 [gdb_get_line_number "set breakpoint 1 here"]
88
89#
90# Test break at line number.
91#
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.
97#
98gdb_test "list main" \
99    ".*main \\(int argc, char ..argv, char ..envp\\).*" \
100    "use `list' to establish default source file"
101gdb_test "hbreak $bp_location1" \
102    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
103    "hardware breakpoint line number"
104delete_breakpoints
105
106set bp_location2 [gdb_get_line_number "set breakpoint 2 here"]
107
108#
109# Test break at line number in file.
110#
111gdb_test "hbreak $srcfile:$bp_location2" \
112    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
113    "hardware breakpoint line number in file"
114delete_breakpoints
115
116set bp_location3 [gdb_get_line_number "set breakpoint 3 here"]
117set bp_location4 [gdb_get_line_number "set breakpoint 4 here"]
118
119#
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.
122#
123gdb_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"
126delete_breakpoints
127
128gdb_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"
131
132set bp_location6 [gdb_get_line_number "set breakpoint 6 here"]
133
134set main_line $bp_location6
135
136set bp_location7 [gdb_get_line_number "set breakpoint 7 here"]
137set bp_location8 [gdb_get_line_number "set breakpoint 8 here" $srcfile1]
138
139gdb_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"
143delete_breakpoints
144
145#
146# Run until the breakpoint at main is hit.  For non-stubs-using targets.
147#
148gdb_test "hbreak main" \
149    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
150    "hardware breakpoint function (2)"
151gdb_run_cmd
152gdb_test "" \
153    "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$srcfile:$bp_location6.*$bp_location6\[\t \]+if .argc.* \{.*" \
154    "run until function breakpoint"
155delete_breakpoints
156
157#
158# Run until the breakpoint at a line number.
159#
160gdb_test "hbreak $bp_location1" \
161    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1\\." \
162    "hardware breakpoint line number (2)"
163gdb_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"
166delete_breakpoints
167
168#
169# Run until the breakpoint set in a function in a file.
170#
171gdb_test "hbreak $srcfile:factorial" \
172    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
173    "hardware breakpoint function in file (2)"
174for {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"
178}
179delete_breakpoints
180
181#
182# Run until the breakpoint set at a quoted function.
183#
184gdb_test "hbreak \"marker2\"" \
185    "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
186    "hardware breakpoint quoted function (2)"
187gdb_test "continue" \
188    "Continuing\\..*Breakpoint \[0-9\]+, (0x\[0-9a-f\]+ in )?marker2 \\(a=43\\) at .*$srcfile1:$bp_location8.*" \
189    "run until quoted breakpoint"
190delete_breakpoints
191#
192# Run until the file:function breakpoint at a line number in a file.
193#
194gdb_test "hbreak $srcfile:$bp_location2" \
195    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2\\." \
196    "hardware breakpoint line number in file (2)"
197gdb_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"
200delete_breakpoints
201
202# Test break at offset +1.
203set bp_location10 [gdb_get_line_number "set breakpoint 10 here"]
204
205gdb_test "hbreak +1" \
206    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10\\." \
207    "hardware breakpoint offset +1"
208
209# Check to see if breakpoint is hit when stepped onto.
210
211gdb_test "step" \
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"
214delete_breakpoints
215
216# Check to see if breakpoint can be set on ending brace of function.
217set bp_location10a [gdb_get_line_number "set breakpoint 10a here"]
218
219gdb_test "hbreak $bp_location10a" \
220    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location10a\\." \
221    "setting hardware breakpoint at }"
222
223gdb_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 }"
226
227#
228# Delete all breakpoints so we can start over, course this can be a test too.
229#
230delete_breakpoints
231
232#
233# Test temporary breakpoint at function.
234#
235
236gdb_test "thbreak main" \
237    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
238    "temporary hardware breakpoint function"
239delete_breakpoints
240
241#
242# Test break at function in file.
243#
244
245gdb_test "thbreak $srcfile:factorial" \
246    "Hardware assisted breakpoint.*at.* file .*$srcfile, line.*" \
247    "temporary hardware breakpoint function in file"
248delete_breakpoints
249
250#
251# Test break at line number.
252#
253gdb_test "thbreak $bp_location1" \
254    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location1.*" \
255    "temporary hardware breakpoint line number #1"
256delete_breakpoints
257
258gdb_test "thbreak $bp_location6" \
259    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location6.*" \
260    "temporary hardware breakpoint line number #2"
261delete_breakpoints
262
263#
264# Test break at line number in file.
265#
266gdb_test "thbreak $srcfile:$bp_location2" \
267    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location2.*" \
268    "temporary hardware breakpoint line number in file #1"
269delete_breakpoints
270
271set bp_location11 [gdb_get_line_number "set breakpoint 11 here"]
272gdb_test "thbreak $srcfile:$bp_location11" \
273    "Hardware assisted breakpoint.*at.* file .*$srcfile, line $bp_location11.*" \
274    "temporary hardware breakpoint line number in file #2"
275
276#
277# Check to see what breakpoints are set (temporary this time).
278#
279gdb_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"
283
284
285#***********
286
287if ![runto_main] then { fail "break tests suppressed" }
288
289# Verify that GDB responds gracefully when asked to set a breakpoint
290# on a nonexistent source line.
291#
292gdb_test_no_output "set breakpoint pending off"
293gdb_test "hbreak 999" \
294    "No line 999 in the current file." \
295    "hardware break on non-existent source line"
296
297# Run to the desired default location.  If not positioned here, the
298# tests below don't work.
299#
300gdb_test "until $bp_location1" "main .* at .*:$bp_location1.*" \
301    "until bp_location1"
302
303
304# Verify that GDB allows one to just say "hbreak", which is treated
305# as the "default" breakpoint.
306#
307gdb_test "hbreak" "Hardware assisted breakpoint \[0-9\]*.*" \
308    "hardware break on default location"
309
310# Verify that a "silent" breakpoint can be set, and that GDB is indeed
311# "silent" about its triggering.
312#
313if ![runto_main] then { fail "break tests suppressed" }
314
315gdb_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"
319	}
320    }
321
322gdb_test "commands $expect_out(1,string)\nsilent\nend" ">end" "set silent break bp_location1"
323
324gdb_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"
327
328gdb_test "continue" "Continuing." \
329    "hit silent hardware break bp_location1"
330
331gdb_test "bt" "#0  main .* at .*:$bp_location1.*" \
332    "stopped for silent hardware break bp_location1"
333
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.)
338#
339set bp_location12 [gdb_get_line_number "set breakpoint 12 here"]
340gdb_test "hbreak $bp_location12 thread 999" "Unknown thread 999.*" \
341    "thread-specific hardware breakpoint on non-existent thread disallowed"
342
343gdb_test "hbreak $bp_location12 thread foo" \
344    "Invalid thread ID: foo" \
345    "thread-specific hardware breakpoint on bogus thread ID disallowed"
346
347# Verify that GDB responds gracefully to a breakpoint command with
348# trailing garbage.
349#
350gdb_test "hbreak $bp_location12 foo" \
351    "malformed linespec error: unexpected string, \"foo\".*" \
352    "hardware breakpoint with trailing garbage disallowed"
353
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.)
357#
358gdb_test "next" "marker1.*" "step over hardware breakpoint"
359
360gdb_test "clear 81" "No breakpoint at 81.*" \
361    "clear line has no breakpoint disallowed"
362
363gdb_test "clear" "No breakpoint at this line.*" \
364    "clear current line has no breakpoint disallowed"
365delete_breakpoints
366
367# Verify that a breakpoint can be set via a convenience variable.
368#
369gdb_test_no_output "set \$foo=$bp_location11" \
370    "set convenience variable \$foo to bp_location11"
371
372gdb_test "hbreak \$foo" \
373    "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location11.*" \
374    "set hardware breakpoint via convenience variable"
375delete_breakpoints
376
377# Verify that GDB responds gracefully to an attempt to set a
378# breakpoint via a convenience variable whose type is not integer.
379#
380gdb_test_no_output "set \$foo=81.5" \
381    "set convenience variable \$foo to 81.5"
382
383gdb_test "hbreak \$foo" \
384    "Convenience variables used in line specs must have integer values.*" \
385    "set hardware breakpoint via non-integer convenience variable disallowed"
386
387# Verify that we can set and trigger a breakpoint in a user-called function.
388#
389gdb_test "hbreak marker2" \
390    "Hardware assisted breakpoint (\[0-9\]*) at .*, line $bp_location8.*" \
391    "set hardware breakpoint on to-be-called function"
392
393gdb_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"
396
397# As long as we're stopped (breakpointed) in a called function,
398# verify that we can successfully backtrace & such from here.
399gdb_test "bt" \
400    "#0\[ \t\]*($hex in )?marker2.*:$bp_location8\r\n#1\[ \t\]*<function called from gdb>.*" \
401    "backtrace while in called function"
402
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.
406#
407gdb_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"
410    }
411    -re "Run till exit from .*marker2.* at .*$bp_location8\r\n.*Value returned.*$gdb_prompt $" {
412	pass "finish from called function"
413    }
414}
415
416#********
417
418
419#
420# Test "next" over recursive function call.
421#
422
423proc test_next_with_recursion {} {
424    global gdb_prompt
425    global decimal
426    global binfile
427
428    delete_breakpoints
429
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"
435
436    gdb_test "kill" "" "kill program" \
437	"Kill the program being debugged.*y or n. $" "y"
438
439    # Run until we call factorial with 6
440
441    gdb_run_cmd
442    if [gdb_test "" \
443	"Break.* factorial .value=6. .*" \
444	"run to factorial(6)"] then { gdb_suppress_tests }
445
446    # Continue until we call factorial recursively with 5.
447
448    if [gdb_test "continue" \
449	"Continuing.*Break.* factorial .value=5. .*" \
450	"continue to factorial(5)"] then { gdb_suppress_tests }
451
452    # Do a backtrace just to confirm how many levels deep we are.
453
454    if [gdb_test "backtrace" \
455	"#0\[ \t\]+ factorial .value=5..*" \
456	"backtrace from factorial(5)"] then { gdb_suppress_tests }
457
458    # Now a "next" should position us at the recursive call, which
459    # we will be performing with 4.
460
461    if [gdb_test "next" \
462	".* factorial .value - 1.;.*" \
463	"next to recursive call"] then { gdb_suppress_tests }
464
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.
470
471    delete_breakpoints
472
473    if [istarget "mips*tx39-*"] {
474	set timeout 60
475    }
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.
482    #
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.
488
489    gdb_test next "\[0-9\]*\[\t \]+return \\(value\\);.*" \
490	"next over recursive call"
491
492    # OK, we should be back in the same stack frame we started from.
493    # Do a backtrace just to confirm.
494
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 }
499
500    if [target_info exists gdb,noresults] { gdb_suppress_tests }
501    gdb_continue_to_end "recursive next test"
502    gdb_stop_suppressing_tests
503}
504
505test_next_with_recursion
506
507
508#********
509
510# Build a new file with optimization enabled so that we can try breakpoints
511# on targets with optimized prologues.
512
513if { [prepare_for_testing "failed to prepare" "hbreak2o2" {break.c break1.c} {debug nowarnings optimize=-O2}] } {
514    return -1
515}
516
517if ![runto_main] then { fail "break tests suppressed" }
518delete_breakpoints
519
520#
521# Test break at function.
522#
523gdb_test "hbreak main" \
524    "Hardware assisted breakpoint.*at.* file .*, line.*" \
525    "hardware breakpoint function, optimized file"
526
527#
528# Run until the breakpoint at main is hit.  For non-stubs-using targets.
529#
530gdb_run_cmd
531gdb_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"
534    }
535    -re "Breakpoint \[0-9\]+,.*main .*argc.*argv.* at .*$gdb_prompt $" {
536	pass "run until hardware function breakpoint, optimized file (code motion)"
537    }
538}
539delete_breakpoints
540
541#
542# Test break at function.
543#
544gdb_test "hbreak marker4" \
545    "Hardware assisted breakpoint.*at.* file .*$srcfile1, line.*" \
546    "hardware breakpoint small function, optimized file"
547
548#
549# Run until the breakpoint at a small function.
550#
551
552#
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.
560
561set bp_location14 [gdb_get_line_number "set breakpoint 14 here" $srcfile1]
562
563gdb_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)"
567	}
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-*"] } {
571		setup_xfail *-*-*
572	    }
573	    fail "run until hardware breakpoint set at small function, optimized file"
574	}
575}
576