1# Copyright 1992-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# This file was written by Fred Fish. (fnf@cygnus.com)
17
18# The skip_hw_watchpoint_tests checks if watchpoints are supported by the
19# processor.  On PowerPC, the check runs a small test program under gdb
20# to determine if the Power processor supports HW watchpoints.  The check
21# must be done before starting the test so as to not disrupt the execution
22# of the actual test.
23
24set skip_hw_watchpoint_tests_p [skip_hw_watchpoint_tests]
25
26standard_testfile
27
28if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
29     untested "failed to compile"
30     return -1
31}
32
33# True if we're forcing no hardware watchpoints.
34set no_hw 0
35
36# Prepare for watchpoint tests by setting up two breakpoints and one
37# watchpoint.
38#
39# We use breakpoints at marker functions to get past all the startup code,
40# so we can get to the watchpoints in a reasonable amount of time from a
41# known starting point.
42#
43# For simplicity, so we always know how to reference specific breakpoints or
44# watchpoints by number, we expect a particular ordering and numbering of
45# each in the combined breakpoint/watchpoint table, as follows:
46#
47#	Number		What		Where
48#	1		Breakpoint	marker1()
49#	2		Breakpoint	marker2()
50#	3		Watchpoint	ival3
51
52proc initialize {} {
53    global gdb_prompt
54    global hex
55    global decimal
56    global srcfile
57
58    if [gdb_test "break marker1" "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker1" ] {
59      return 0
60    }
61
62
63    if [gdb_test "break marker2" "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker2" ] {
64      return 0
65    }
66
67
68    if [gdb_test "info break" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*" "info break" ] {
69      return 0
70    }
71
72    gdb_test "watch ival3" ".*\[Ww\]atchpoint 3: ival3.*" "set watchpoint on ival3"
73
74    if [gdb_test "info watch" "3\[ \]*.*watchpoint.*ival3" "watchpoint found in watchpoint/breakpoint table" ] {
75      return 0
76    }
77
78
79    # After installing the watchpoint, we disable it until we are ready
80    # to use it.  This allows the test program to run at full speed until
81    # we get to the first marker function.
82
83    if [gdb_test "disable 3" "disable 3\[\r\n\]+" "disable watchpoint" ] {
84      return 0
85    }
86
87
88    return 1
89}
90
91#
92# Test simple watchpoint.
93#
94
95proc test_simple_watchpoint {} {
96    global gdb_prompt
97    global hex
98    global decimal
99
100    # Ensure that the watchpoint is disabled when we startup.
101
102    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] {
103      return 0
104    }
105
106    # Run until we get to the first marker function.
107
108    gdb_run_cmd
109    set timeout 600
110    set test "run to marker1 in test_simple_watchpoint"
111    set retcode [gdb_test_multiple "" $test {
112	-re "Breakpoint 1, marker1 .*$gdb_prompt $" {
113	    pass $test
114	}
115    }]
116
117    if { $retcode != 0 } {
118	return
119    }
120
121    # After reaching the marker function, enable the watchpoint.
122
123    if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] {
124      return
125    }
126
127
128    gdb_test "break func1" "Breakpoint.*at.*"
129    gdb_test_no_output "set \$func1_breakpoint_number = \$bpnum"
130
131    gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
132	"continue to breakpoint at func1"
133
134    # Continue until the first change, from -1 to 0
135
136    set test "watchpoint hit, first time"
137    gdb_test_multiple "cont" $test {
138	-re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
139	    pass $test
140	}
141	-re "Continuing.*Breakpoint.*func1.*$gdb_prompt $" {
142	    setup_xfail "m68*-*-*" 2597
143	    fail "thought it hit breakpoint at func1 twice"
144	    gdb_test_no_output "delete \$func1_breakpoint_number"
145	    gdb_test "continue" "\
146Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
147		$test
148	}
149    }
150
151    # Check that the hit count is reported correctly
152    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 1 time.*" "watchpoint hit count is 1"
153
154    gdb_test_no_output "delete \$func1_breakpoint_number"
155
156    # Continue until the next change, from 0 to 1.
157    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit, second time"
158
159    # Check that the hit count is reported correctly
160    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 2 times.*" "watchpoint hit count is 2"
161
162    # Continue until the next change, from 1 to 2.
163    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
164
165    # Check that the hit count is reported correctly
166    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 3 times.*" "watchpoint hit count is 3"
167
168    # Continue until the next change, from 2 to 3.
169    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
170
171    # Check that the hit count is reported correctly
172    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 4 times.*" "watchpoint hit count is 4"
173
174    # Continue until the next change, from 3 to 4.
175    # Note that this one is outside the loop.
176
177    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
178
179    # Check that the hit count is reported correctly
180    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 5 times.*" "watchpoint hit count is 5"
181
182    # Continue until we hit the finishing marker function.
183    # Make sure we hit no more watchpoints.
184
185    gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
186	"continue to marker2"
187
188    # Disable the watchpoint so we run at full speed until we exit.
189
190    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "watchpoint disabled" ] {
191      return
192    }
193
194
195    # Run until process exits.
196
197    if [target_info exists gdb,noresults] { return }
198
199    gdb_continue_to_end "continue to exit in test_simple_watchpoint"
200}
201
202# Test disabling watchpoints.
203
204proc test_disabling_watchpoints {} {
205    global gdb_prompt
206    global binfile
207    global srcfile
208    global decimal
209    global hex
210
211    gdb_test "info watch" "\[0-9]+\[ \]*.*watchpoint.*ival3.*" "watchpoints found in watchpoint/breakpoint table"
212
213    # Ensure that the watchpoint is disabled when we startup.
214
215    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_disabling_watchpoints" ] {
216      return 0
217    }
218
219
220    # Run until we get to the first marker function.
221
222    gdb_run_cmd
223    set timeout 600
224    set test "run to marker1 in test_disabling_watchpoints"
225    set retcode [gdb_test_multiple "" $test {
226	-re "Breakpoint 1, marker1 .*$gdb_prompt $" {
227	    pass $test
228	}
229    }]
230
231    if { $retcode != 0 } {
232	return
233    }
234
235    # After reaching the marker function, enable the watchpoint.
236
237    if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] {
238      return
239    }
240
241
242    # Continue until the first change, from -1 to 0
243    # Don't check the old value, because on VxWorks the variable value
244    # will not have been reinitialized.
245    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
246
247    # Continue until the next change, from 0 to 1.
248    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, second time"
249
250    # Disable the watchpoint but leave breakpoints
251
252    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] {
253      return 0
254    }
255
256
257    # Check watchpoint list, looking for the entry that confirms the
258    # watchpoint is disabled.
259    gdb_test "info watchpoints" "\[0-9]+\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
260
261    # Continue until we hit the finishing marker function.
262    # Make sure we hit no more watchpoints.
263    gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
264	"disabled watchpoint skipped"
265
266    if [target_info exists gdb,noresults] { return }
267
268    gdb_continue_to_end "continue to exit in test_disabling_watchpoints"
269}
270
271# Test stepping and other mundane operations with watchpoints enabled
272proc test_stepping {} {
273    global gdb_prompt
274
275    if {[runto marker1]} {
276	gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
277
278	# Well, let's not be too mundane.  It should be a *bit* of a challenge
279	gdb_test "break func2 if 0" "Breakpoint.*at.*"
280	gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
281
282	gdb_test "p func1 ()" "= 73" \
283	    "calling function with watchpoint enabled"
284
285	#
286	# "finish" brings us back to main.
287	# On some targets (e.g. alpha) gdb will stop from the finish in midline
288	# of the marker1 call. This is due to register restoring code on
289	# the alpha and might be caused by stack adjustment instructions
290	# on other targets. In this case we will step once more.
291	#
292
293	send_gdb "finish\n"
294	gdb_expect {
295	    -re "Run.*exit from.*marker1.* at" {
296		pass "finish from marker1"
297	    }
298	    default { fail "finish from marker1 (timeout)" ; return }
299	}
300
301	gdb_expect {
302	    -re "marker1 \\(\\);.*$gdb_prompt $" {
303		send_gdb "step\n"
304		exp_continue
305	    }
306	    -re "func1 \\(\\);.*$gdb_prompt $" {
307		pass "back at main from marker1"
308	    }
309	    -re ".*$gdb_prompt $" {
310		fail "back at main from marker1"
311	    }
312	    default { fail "back at main from marker1 (timeout)" ; return }
313	}
314
315	gdb_test "next" "for \\(count = 0.*" "next to `for'"
316
317	# Now test that "until" works.  It's a bit tricky to test
318	# "until", because compilers don't always arrange the code
319	# exactly the same way, and we might get slightly different
320	# sequences of statements.  But the following should be true
321	# (if not it is a compiler or a debugger bug): The user who
322	# does "until" at every statement of a loop should end up
323	# stepping through the loop once, and the debugger should not
324	# stop for any of the remaining iterations.
325
326	gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
327	gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
328	set test "until out of loop"
329	gdb_test_multiple "until" $test {
330	    -re "(for \\(count = 0|\}).*$gdb_prompt $" {
331		gdb_test "until" "ival1 = count; /. Outside loop ./" $test
332	    }
333	    -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
334		pass $test
335	    }
336	}
337
338	gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
339    }
340}
341
342# Test stepping and other mundane operations with watchpoints enabled
343proc test_watchpoint_triggered_in_syscall {} {
344    global gdb_prompt
345
346    # These tests won't work without printf support.
347    if [gdb_skip_stdio_test "watchpoints triggered in syscall"] {
348	return
349    }
350    # Run until we get to the first marker function.
351    set x 0
352    set y 0
353    set testname "watch buffer passed to read syscall"
354    if {[runto marker2]} {
355	gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
356	gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
357	gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
358	gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
359	gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
360	gdb_test "break marker4" ".*Breakpoint.*"
361
362	gdb_test_no_output "set doread = 1"
363
364	# If we send gdb "123\n" before gdb has switched the tty, then it goes
365	# to gdb, not the inferior, and we lose.  So that is why we have
366	# watchpoint.c prompt us, so we can wait for that prompt.
367
368	send_gdb "continue\n"
369	gdb_expect {
370	    -re "Continuing\\.\r\ntype stuff for buf now:" {
371		pass "continue to read"
372	    }
373	    default {
374		fail "continue to read"
375		return
376	    }
377	}
378
379	set test "sent 123"
380	gdb_test_multiple "123" $test {
381	    -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
382	    -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
383	    -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
384	    -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
385	    -re ".*$gdb_prompt $" { pass $test }
386	}
387
388	# Examine the values in buf to see how many watchpoints we
389	# should have printed.
390	set test "print buf\[0\]"
391	gdb_test_multiple $test $test {
392	    -re ".*= 49.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
393	    -re ".*= 0.*$gdb_prompt $" { $test }
394	}
395	set test "print buf\[1\]"
396	gdb_test_multiple $test $test {
397	    -re ".*= 50.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
398	    -re ".*= 0.*$gdb_prompt $" { pass $test }
399	}
400	set test "print buf\[2\]"
401	gdb_test_multiple $test $test {
402	    -re ".*= 51.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
403	    -re ".*= 0.*$gdb_prompt $" { pass $test }
404	}
405	set test "print buf\[3\]"
406	gdb_test_multiple $test $test {
407	    -re ".*= 10.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
408	    -re ".*= 0.*$gdb_prompt $" { pass $test }
409	}
410
411	# Did we find what we were looking for?  If not, flunk it.
412	if {[expr $x==$y]} { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
413
414	# Continue until we hit the finishing marker function.
415	# Make sure we hit no more watchpoints.
416	gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
417	    "continue to marker4"
418
419	# Disable everything so we can finish the program at full speed
420	gdb_test_no_output "disable" "disable in test_watchpoint_triggered_in_syscall"
421
422	if [target_info exists gdb,noresults] { return }
423
424	gdb_continue_to_end "continue to exit in test_watchpoint_triggered_in_syscall"
425    }
426}
427
428# Do a simple test of of watching through a pointer when the pointer
429# itself changes.  Should add some more complicated stuff here.
430
431proc test_complex_watchpoint {} {
432    global gdb_prompt
433
434    if {[runto marker4]} {
435	gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
436	gdb_test "break marker5" ".*Breakpoint.*"
437
438	gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "test complex watchpoint"
439
440	# Continue until we hit the marker5 function.
441	# Make sure we hit no more watchpoints.
442
443	gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
444	    "did not trigger wrong watchpoint"
445
446        # Test watches of things declared locally in a function.
447        # In particular, test that a watch of stack-based things
448        # is deleted when the stack-based things go out of scope.
449        #
450	gdb_test_no_output "disable" "disable in test_complex_watchpoint, first time"
451        gdb_test "break marker6" ".*Breakpoint.*"
452        gdb_test "cont" "Continuing.*Breakpoint.*marker6 \\(\\).*" \
453            "continue to marker6"
454	gdb_breakpoint [gdb_get_line_number "func2 breakpoint here"]
455	gdb_continue_to_breakpoint "func2 breakpoint here, first time"
456
457        # Test a watch of a single stack-based variable, whose scope
458        # is the function we're now in.  This should auto-delete when
459        # execution exits the scope of the watchpoint.
460        #
461        gdb_test "watch local_a" ".*\[Ww\]atchpoint \[0-9\]*: local_a" "set local watch"
462        gdb_test "cont" "\[Ww\]atchpoint.*local_a.*" "trigger local watch"
463
464	set test "self-delete local watch"
465        gdb_test_multiple "cont" $test {
466	    -re "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*\r\n$gdb_prompt $" {
467		pass $test
468	    }
469	    -re "can't compute CFA for this frame.*\r\n$gdb_prompt $" {
470		global no_hw
471
472		# GCC < 4.5.0 does not get LOCATIONS_VALID set by dwarf2read.c.
473		# Therefore epilogue unwinder gets applied which is
474		# incompatible with dwarf2_frame_cfa.
475		if {$no_hw && ([test_compiler_info {gcc-[0-3]-*}]
476			       || [test_compiler_info {gcc-4-[0-4]-*}])} {
477		    xfail "$test (old GCC has broken watchpoints in epilogues)"
478		    return
479		}
480		fail $test
481	    }
482	}
483
484	gdb_continue_to_breakpoint "func2 breakpoint here, second time"
485        # We should be in "func2" again now.  Test a watch of an
486        # expression which includes both a stack-based local and
487        # something whose scope is larger than this invocation
488        # of "func2".  This should also auto-delete.
489        #
490        gdb_test "watch local_a + ival5" ".*\[Ww\]atchpoint \[0-9\]*: local_a . ival5" \
491                 "set partially local watch"
492        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
493                 "trigger1 partially local watch"
494        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
495                 "trigger2 partially local watch"
496        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
497                 "self-delete partially local watch"
498
499        # We should be in "func2" again now.  Test a watch of a
500        # static (non-stack-based) local.  Since this has scope
501        # across any invocations of "func2", it should not auto-
502        # delete.
503        #
504	gdb_continue_to_breakpoint "func2 breakpoint here, third time"
505        gdb_test "watch static_b" ".*\[Ww\]atchpoint \[0-9\]*: static_b" \
506                 "set static local watch"
507        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: static_b.*" \
508                 "trigger static local watch"
509        gdb_test "cont" "Continuing.*marker6 \\(\\).*" \
510                 "continue after trigger static local watch"
511        gdb_test "info break" ".*watchpoint.*static_b.*" \
512                 "static local watch did not self-delete"
513
514        # We should be in "recurser" now.  Test a watch of a stack-
515        # based local.  Symbols mentioned in a watchpoint are bound
516        # at watchpoint-creation.  Thus, a watch of a stack-based
517        # local to a recursing function should be bound only to that
518        # one invocation, and should not trigger for other invocations.
519        #
520	with_test_prefix "local_x" {
521	    gdb_test "tbreak recurser" ".*breakpoint.*"
522	    gdb_test "cont" "Continuing.*recurser.*"
523	    gdb_test "next" "if \\(x > 0.*" "next past local_x initialization"
524	    gdb_test "watch local_x" ".*\[Ww\]atchpoint \[0-9\]*: local_x" \
525		"set local watch in recursive call"
526	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_x.*New value = 2.*" \
527		"trigger local watch in recursive call"
528	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
529		"self-delete local watch in recursive call"
530	}
531
532        # Repeat the preceding test, but this time use "recurser::local_x" as
533        # the variable to track.
534	with_test_prefix "recurser::local_x" {
535	    gdb_test "cont" "Continuing.*marker6.*" "continue to marker6"
536	    gdb_test "tbreak recurser" ".*breakpoint.*"
537	    gdb_test "cont" "Continuing.*recurser.*" "continue to recurser"
538	    gdb_test "next" "if \\(x > 0.*" "next past local_x initialization"
539	    gdb_test "watch recurser::local_x" ".*\[Ww\]atchpoint \[0-9\]*: recurser::local_x" \
540		"set local watch in recursive call with explicit scope"
541	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: recurser::local_x.*New value = 2.*" \
542		"trigger local watch with explicit scope in recursive call"
543	    gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
544		"self-delete local watch with explicit scope in recursive call (2)"
545	}
546
547	# Disable everything so we can finish the program at full speed
548	gdb_test_no_output "disable" "disable in test_complex_watchpoint, second time"
549
550	if [target_info exists gdb,noresults] { return }
551
552	gdb_continue_to_end "continue to exit in test_complex_watchpoint"
553    }
554}
555
556proc test_watchpoint_and_breakpoint {} {
557    global gdb_prompt
558
559    # This is a test for PR breakpoints/7143, which involves setting a
560    # watchpoint right after you've reached a breakpoint.
561
562    if {[runto func3]} {
563	gdb_breakpoint [gdb_get_line_number "second x assignment"]
564	gdb_continue_to_breakpoint "second x assignment"
565	gdb_test "watch x" ".*atchpoint \[0-9\]+: x"
566	gdb_test "next" \
567	    ".*atchpoint \[0-9\]+: x\r\n\r\nOld value = 0\r\nNew value = 1\r\n.*" \
568	    "next after watch x"
569
570	gdb_test_no_output "delete \$bpnum" "delete watch x"
571    }
572}
573
574proc test_constant_watchpoint {} {
575    gdb_test "watch 5" "Cannot watch constant value `5'." "number is constant"
576    gdb_test "watch (int *)5" "Cannot watch constant value `\\(int \\*\\)5'." \
577    "number with cast is constant"
578    gdb_test "watch marker1" "Cannot watch constant value `marker1'." \
579    "marker1 is constant"
580    gdb_test "watch count + 6" ".*atchpoint \[0-9\]+: count \\+ 6"
581    gdb_test_no_output "delete \$bpnum" "delete watchpoint `count + 6'"
582    gdb_test "watch 7 + count" ".*atchpoint \[0-9\]+: 7 \\+ count"
583    gdb_test_no_output "delete \$bpnum" "delete watchpoint `7 + count'"
584}
585
586proc test_disable_enable_software_watchpoint {} {
587    # This is regression test for a bug that caused `enable' to fail
588    # for software watchpoints.
589
590    # Watch something not memory to force a software watchpoint.
591    gdb_test {watch $pc} ".*atchpoint \[0-9\]+: .pc"
592
593    gdb_test_no_output "disable \$bpnum" "disable watchpoint `\$pc'"
594    gdb_test_no_output "enable \$bpnum" "reenable watchpoint `\$pc'"
595
596    gdb_test "info watchpoint \$bpnum" \
597	".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+.pc.*" \
598	"watchpoint `\$pc' is enabled"
599
600    gdb_test_no_output "delete \$bpnum" "delete watchpoint `\$pc'"
601}
602
603proc test_watch_location {} {
604    global gdb_prompt
605
606    gdb_breakpoint [gdb_get_line_number "func5 breakpoint here"]
607    gdb_continue_to_breakpoint "func5 breakpoint here"
608
609    # Check first if a null pointer can be dereferenced on the target.
610    gdb_test_multiple "p *null_ptr" "" {
611	-re "Cannot access memory at address 0x0.*$gdb_prompt $" {
612	    gdb_test "watch -location null_ptr->p->x" \
613		"Cannot access memory at address 0x0"
614	}
615	-re ".*$gdb_prompt $" {
616	    # Null pointer dereference is legitimate.
617	}
618    }
619
620    gdb_test "watch -location *x" "atchpoint .*: .*" "watch -location .x"
621
622    gdb_test "continue" \
623	"Continuing.*\[Ww\]atchpoint .*: .*New value = 27.*" \
624	"continue with watch -location"
625
626    gdb_test_no_output "delete \$bpnum" "delete watch -location"
627}
628
629# Tests watching areas larger than a word.
630
631proc test_wide_location_1 {} {
632    global no_hw
633    global gdb_prompt
634    global skip_hw_watchpoint_tests_p
635
636    # This test watches two words on most 32-bit ABIs, and one word on
637    # most 64-bit ABIs.
638
639    # Platforms where the target can't watch such a large region
640    # should clear hw_expected below.
641    if { $no_hw || $skip_hw_watchpoint_tests_p
642         || [istarget arm*-*-*]
643         || ([istarget powerpc*-*-*] && ![is_lp64_target])} {
644	set hw_expected 0
645    } else {
646	set hw_expected 1
647    }
648
649    gdb_breakpoint [gdb_get_line_number "func6 breakpoint here"]
650    gdb_continue_to_breakpoint "func6 breakpoint here"
651
652    if { $hw_expected } {
653	gdb_test "watch foo2" "Hardware watchpoint .*: .*"
654	gdb_test "continue" \
655	    "Continuing.*Hardware watchpoint .*: .*New value = \\\{val = \\\{0, 11\\\}\\\}.*" \
656	    "continue with watch foo2"
657    } else {
658	gdb_test "watch foo2" "atchpoint .*: .*"
659	set test "continue with watch foo2"
660	gdb_test_multiple "cont" $test {
661	    -re "Continuing.*\[Ww\]atchpoint .*: .*New value = \\\{val = \\\{0, 11\\\}\\\}.*$gdb_prompt $" {
662		pass $test
663	    }
664	    -re "Could not insert hardware breakpoints:.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
665		# This may happen with remote targets that support
666		# hardware watchpoints.  We only find out the
667		# watchpoint was too large, for example, at insert
668		# time.  If GDB is ever adjusted to downgrade the
669		# watchpoint automatically in this case, this match
670		# should be removed.
671		pass $test
672	    }
673	}
674    }
675
676    gdb_test_no_output "delete \$bpnum" "delete watch foo2"
677}
678
679proc test_wide_location_2 {} {
680    global no_hw
681    global gdb_prompt
682    global skip_hw_watchpoint_tests_p
683
684    # This test watches four words on most 32-bit ABIs, and two words
685    # on 64-bit ABIs.
686
687    # Platforms where the target can't watch such a large region
688    # should clear hw_expected below.
689    if { $no_hw || $skip_hw_watchpoint_tests_p
690         || [istarget arm*-*-*]
691         || [istarget powerpc*-*-*]} {
692	set hw_expected 0
693    } else {
694	set hw_expected 1
695    }
696
697    gdb_breakpoint [gdb_get_line_number "func7 breakpoint here"]
698    gdb_continue_to_breakpoint "func7 breakpoint here"
699
700    if { $hw_expected } {
701	gdb_test "watch foo4" "Hardware watchpoint .*: .*"
702	gdb_test "continue" \
703	    "Continuing.*Hardware watchpoint .*: .*New value = \\\{val = \\\{0, 0, 0, 33\\\}\\\}.*" \
704	    "continue with watch foo4"
705    } else {
706	gdb_test "watch foo4" "atchpoint .*: .*"
707	set test "continue with watch foo4"
708	gdb_test_multiple "cont" $test {
709	    -re "Continuing.*\[Ww\]atchpoint .*: .*New value = \\\{val = \\\{0, 0, 0, 33\\\}\\\}.*$gdb_prompt $" {
710		pass $test
711	    }
712	    -re "Could not insert hardware breakpoints:.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
713		# This may happen with remote targets that support
714		# hardware watchpoints.  We only find out the
715		# watchpoint was too large, for example, at insert
716		# time.  If GDB is ever adjusted to downgrade the
717		# watchpoint automatically in this case, this match
718		# should be removed.
719		pass $test
720	    }
721	}
722    }
723
724    gdb_test_no_output "delete \$bpnum" "delete watch foo4"
725}
726
727proc test_inaccessible_watchpoint {} {
728    global gdb_prompt
729
730    # This is a test for watchpoints on currently inaccessible (but later
731    # valid) memory.
732
733    if {[runto func4]} {
734	# Make sure we only allow memory access errors.
735	set msg "watchpoint refused to insert on nonexistent struct member"
736	gdb_test_multiple "watch struct1.nosuchmember" $msg {
737	    -re ".*atchpoint \[0-9\]+: struct1.nosuchmember.*$gdb_prompt $" {
738		# PR breakpoints/9681
739		fail $msg
740	    }
741	    -re "There is no member named nosuchmember\\..*$gdb_prompt $" {
742		pass $msg
743	    }
744	}
745
746	# See whether a watchpoint on a normal variable is a hardware
747	# watchpoint or not.  The watchpoints on NULL should be hardware
748	# iff this one is.
749	set watchpoint_msg "Watchpoint"
750	gdb_test_multiple "watch global_ptr" "watch global_ptr" {
751	    -re "Watchpoint \[0-9\]+: global_ptr\r\n.*$gdb_prompt $" {
752		pass "watch global_ptr"
753	    }
754	    -re "Hardware watchpoint \[0-9\]+: global_ptr\r\n.*$gdb_prompt $" {
755		set watchpoint_msg "Hardware watchpoint"
756		pass "watch global_ptr"
757	    }
758	}
759	delete_breakpoints
760
761	# Make sure that we can watch a constant address, and correctly
762	# use a HW watchpoint if supported.
763	gdb_test "watch *(int *) 0" \
764	    "$watchpoint_msg \[0-9\]+: \\*\\(int \\*\\) 0"
765	delete_breakpoints
766
767	# The same, but using -location through an indirection.
768	gdb_test "watch -location *global_ptr" \
769	    "$watchpoint_msg \[0-9\]+: \-location \\*global_ptr"
770	delete_breakpoints
771
772	# This step requires two HW watchpoints.  Since some platforms only
773	# have a single one, accept either SW or HW watchpoint in this case.
774	if {[skip_hw_watchpoint_multi_tests]} {
775	    set watchpoint_msg "(Watchpoint|Hardware watchpoint)"
776	}
777
778	gdb_test "watch *global_ptr" "$watchpoint_msg \[0-9\]+: \\\*global_ptr"
779	gdb_test "set \$global_ptr_breakpoint_number = \$bpnum" ""
780	gdb_test "next" ".*global_ptr = buf.*" "global_ptr next"
781	gdb_test_multiple "next" "next over ptr init" {
782	    -re ".*atchpoint \[0-9\]+: \\*global_ptr\r\n\r\nOld value = .*\r\nNew value = 3 .*\r\n.*$gdb_prompt $" {
783		# We can not test for <unknown> here because NULL may be readable.
784		# This test does rely on *NULL != 3.
785		pass "next over ptr init"
786	    }
787	}
788	gdb_test_multiple "next" "next over buffer set" {
789	    -re ".*atchpoint \[0-9\]+: \\*global_ptr\r\n\r\nOld value = 3 .*\r\nNew value = 7 .*\r\n.*$gdb_prompt $" {
790		pass "next over buffer set"
791	    }
792	}
793	gdb_test "delete \$global_ptr_breakpoint_number" ""
794	gdb_test "watch **global_ptr_ptr" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr"
795	gdb_test "set \$global_ptr_ptr_breakpoint_number = \$bpnum" ""
796	gdb_test "next" ".*global_ptr_ptr = &global_ptr.*" "global_ptr_ptr next"
797	gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = .*\r\nNew value = 7 .*" "next over global_ptr_ptr init"
798	gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = 7 .*\r\nNew value = 9 .*" "next over global_ptr_ptr buffer set"
799	gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = 9 .*\r\nNew value = 5 .*" "next over global_ptr_ptr pointer advance"
800	gdb_test_no_output "delete \$global_ptr_ptr_breakpoint_number"
801    }
802}
803
804proc test_no_hw_watchpoints {} {
805    global testfile
806    global skip_hw_watchpoint_tests_p
807
808    clean_restart $testfile
809
810    # Verify that a user can force GDB to use "slow" watchpoints.
811    # (This proves rather little on kernels that don't support
812    # fast watchpoints, but still...)
813    #
814    if {![runto_main]} {
815	return
816    }
817
818    gdb_test_no_output "set can-use-hw-watchpoints 0" "disable fast watches"
819
820    gdb_test "show can-use-hw-watchpoints" \
821	"Debugger's willingness to use watchpoint hardware is 0." \
822	"show disable fast watches"
823
824    gdb_test "watch ival3 if  count > 1" \
825	"Watchpoint \[0-9\]*: ival3.*" \
826	"set slow conditional watch"
827
828    gdb_test "continue" \
829	"Watchpoint \[0-9\]*: ival3.*Old value = 1.*New value = 2.*" \
830	"trigger slow conditional watch"
831
832    gdb_test_no_output "delete \$bpnum" "delete watch ival3"
833
834    gdb_test "watch ival3 if  count > 1  thread 1 " \
835         "Watchpoint \[0-9\]*: ival3.*" \
836         "set slow condition watch w/thread"
837
838    gdb_test_no_output "delete \$bpnum" "delete watch w/condition and thread"
839
840    # We've explicitly disabled hardware watches.  Verify that GDB
841    # refrains from using them.
842    #
843    gdb_test "rwatch ival3" \
844	"Can't set read/access watchpoint when hardware watchpoints are disabled." \
845	"rwatch disallowed when can-set-hw-watchpoints cleared"
846    gdb_test "awatch ival3" \
847	"Can't set read/access watchpoint when hardware watchpoints are disabled." \
848	"awatch disallowed when can-set-hw-watchpoints cleared"
849
850
851    # Re-enable hardware watchpoints if necessary.
852    if {!$skip_hw_watchpoint_tests_p} {
853        gdb_test_no_output "set can-use-hw-watchpoints 1" ""
854    }
855}
856
857proc test_watchpoint_in_big_blob {} {
858    global gdb_prompt
859
860    # On native targets where we do hardware resource accounting, this
861    # may end up as a software watchpoint.
862    set ok 0
863    set test "watch buf"
864    gdb_test_multiple "watch buf" $test {
865	-re "Hardware watchpoint \[0-9\]+: buf.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
866	    # This may happen with remote targets (where we don't do
867	    # resource accounting) that support hardware watchpoints,
868	    # when breakpoint always-inserted is on.  The watchpoint
869	    # was too large, for example.  If GDB is ever adjusted to
870	    # downgrade the watchpoint automatically in this case,
871	    # this match should be removed.  Note the breakpoint has
872	    # been created, and is in the list, so it needs deleting.
873	    pass $test
874	}
875	-re ".*atchpoint \[0-9\]+: buf.*$gdb_prompt $" {
876	    pass $test
877	    set ok 1
878	}
879    }
880
881    if { $ok } {
882	set test "watchpoint on buf hit"
883	gdb_test_multiple "cont" $test {
884	    -re "Continuing.*atchpoint \[0-9\]+: buf\r\n\r\nOld value = .*testte\".*$gdb_prompt $" {
885		pass $test
886	    }
887	    -re "Could not insert hardware breakpoints:.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
888		# This may happen with remote targets that support
889		# hardware watchpoints.  We only find out the
890		# watchpoint was too large, for example, at insert
891		# time.  If GDB is ever adjusted to downgrade the
892		# watchpoint automatically in this case, this match
893		# should be removed.
894		pass $test
895	    }
896	}
897    }
898
899    gdb_test_no_output "delete \$bpnum" "delete watch buf"
900}
901
902proc test_watch_register_location {} {
903    global no_hw
904    global skip_hw_watchpoint_tests_p
905
906    if {!$no_hw && !$skip_hw_watchpoint_tests_p} {
907	# Non-memory read/access watchpoints are not supported, they would
908	# require software read/access watchpoint support (which is not
909	# currently available).
910	gdb_test "rwatch \$pc" \
911	    "Expression cannot be implemented with read/access watchpoint..*" \
912	    "rwatch disallowed for register based expression"
913	gdb_test "awatch \$pc" \
914	    "Expression cannot be implemented with read/access watchpoint..*" \
915	    "awatch disallowed for register based expression"
916    }
917}
918
919# Start with a fresh gdb.
920
921set prev_timeout $timeout
922set timeout 600
923verbose "Timeout now 600 sec.\n"
924
925test_no_hw_watchpoints
926
927proc do_tests {} {
928    global testfile
929    global no_hw
930    global skip_hw_watchpoint_tests_p
931
932    clean_restart $testfile
933
934    if {$no_hw || $skip_hw_watchpoint_tests_p} {
935	gdb_test_no_output "set can-use-hw-watchpoints 0"\
936	    "disable fast watches, 1"
937    }
938
939    if {[initialize]} {
940
941	test_simple_watchpoint
942
943	test_disabling_watchpoints
944
945	if ![target_info exists gdb,cannot_call_functions] {
946	    test_stepping
947	}
948    }
949
950    # Tests below don't rely on the markers and watchpoint set by
951    # `initialize' anymore.
952    clean_restart $testfile
953
954    if {$no_hw || $skip_hw_watchpoint_tests_p} {
955	gdb_test_no_output "set can-use-hw-watchpoints 0" \
956	    "disable fast watches, 2"
957    }
958
959    # Only enabled for some targets merely because it has not been tested
960    # elsewhere.
961    # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4
962    # breakpoint before stopping for the watchpoint.  I don't know why.
963    if {[istarget "hppa*-*-*"]} {
964	test_watchpoint_triggered_in_syscall
965    }
966
967    test_complex_watchpoint
968
969    test_inaccessible_watchpoint
970
971    test_watchpoint_and_breakpoint
972
973    test_watchpoint_in_big_blob
974
975    test_constant_watchpoint
976
977    test_disable_enable_software_watchpoint
978
979    test_watch_location
980
981    test_wide_location_1
982    test_wide_location_2
983
984    test_watch_register_location
985}
986
987# On targets that can do hardware watchpoints, run the tests twice:
988# once with hardware watchpoints enabled; another with hardware
989# watchpoints force-disabled.
990
991do_tests
992if {!$skip_hw_watchpoint_tests_p} {
993    with_test_prefix "no-hw" {
994	set no_hw 1
995	do_tests
996    }
997}
998
999# Restore old timeout
1000set timeout $prev_timeout
1001verbose "Timeout now $timeout sec.\n"
1002