1# Copyright 1992, 1994, 1995, 1996, 1997, 1998, 1999, 2000
2# Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 2 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program; if not, write to the Free Software
16# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17
18# Please email any bugs, comments, and/or additions to this file to:
19# bug-gdb@prep.ai.mit.edu
20
21# This file was written by Fred Fish. (fnf@cygnus.com)
22
23if $tracelevel then {
24    strace $tracelevel
25}
26
27set prms_id 0
28set bug_id 0
29
30set testfile "watchpoint"
31set srcfile ${testfile}.c
32set binfile ${objdir}/${subdir}/${testfile}
33
34set wp_set 1
35
36if [get_compiler_info ${binfile}] {
37    return -1
38}
39
40if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
41     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
42}
43
44# Prepare for watchpoint tests by setting up two breakpoints and one
45# watchpoint.
46#
47# We use breakpoints at marker functions to get past all the startup code,
48# so we can get to the watchpoints in a reasonable amount of time from a
49# known starting point.
50#
51# For simplicity, so we always know how to reference specific breakpoints or
52# watchpoints by number, we expect a particular ordering and numbering of
53# each in the combined breakpoint/watchpoint table, as follows:
54#
55#	Number		What		Where
56#	1		Breakpoint	marker1()
57#	2		Breakpoint	marker2()
58#	3		Watchpoint	ival3
59
60proc initialize {} {
61    global gdb_prompt
62    global hex
63    global decimal
64    global srcfile
65    global wp_set
66
67    # Disable hardware watchpoints if necessary.
68    if [target_info exists gdb,no_hardware_watchpoints] {
69	gdb_test "set can-use-hw-watchpoints 0" "" ""
70    }
71
72    if [gdb_test "break marker1" "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker1" ] {
73      return 0;
74    }
75
76
77    if [gdb_test "break marker2" "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker2" ] {
78      return 0;
79    }
80
81
82    if [gdb_test "info break" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*" "info break in watchpoint.exp" ] {
83      return 0;
84    }
85
86
87    # ??rehrauer: To fix DTS #CHFts23014, in which setting a watchpoint
88    # before running can cause the inferior to croak on HP-UX 11.0 for
89    # reasons yet unknown, we've disabled the ability to set watches
90    # without a running inferior.  Verify the restriction.
91    #
92    send_gdb "watch ival3\n"
93    gdb_expect {
94        -re ".*\[Ww\]atchpoint 3: ival3.*$gdb_prompt $" {
95            pass "set watchpoint on ival3"
96        }
97        -re "warning: can't do that without a running program; try \"break main\", \"run\" first.*$gdb_prompt $" {
98            pass "set watchpoint on ival3"
99            set wp_set 0
100            return 1
101        }
102        timeout {
103            fail "(timeout) set watchpoint on ival3"
104            return 0
105        }
106    }
107
108    # "info watch" is the same as "info break"
109
110    if [gdb_test "info watch" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*\r\n3\[ \]*.*watchpoint.*ival3" "watchpoint found in watchpoint/breakpoint table" ] {
111      return 0;
112    }
113
114
115    # After installing the watchpoint, we disable it until we are ready
116    # to use it.  This allows the test program to run at full speed until
117    # we get to the first marker function.
118
119    if [gdb_test "disable 3" "disable 3\[\r\n\]+" "disable watchpoint" ] {
120      return 0;
121    }
122
123
124    return 1
125}
126
127#
128# Test simple watchpoint.
129#
130
131proc test_simple_watchpoint {} {
132    global gdb_prompt
133    global hex
134    global decimal
135    global wp_set
136
137    # Ensure that the watchpoint is disabled when we startup.
138
139    if { $wp_set } {
140        if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] {
141	    return 0;
142	}
143    }
144
145
146    # Run until we get to the first marker function.
147
148    gdb_run_cmd
149    set timeout 600
150    gdb_expect {
151	-re "Breakpoint 1, marker1 .*$gdb_prompt $" {
152	    pass "run to marker1 in test_simple_watchpoint"
153	}
154	-re ".*$gdb_prompt $" {
155	    fail "run to marker1 in test_simple_watchpoint"
156	    return
157	}
158	timeout {
159	    fail "run to marker1 in test_simple_watchpoint (timeout)"
160	    return
161	}
162    }
163
164    if { !$wp_set } {
165	# ??rehrauer: To fix DTS #CHFts23014, in which setting a watchpoint
166	# before running can cause the inferior to croak on HP-UX 11.0
167	# for reasons yet unknown, we've disabled the ability to set
168	# watches without a running inferior.  The following testpoints used
169	# to be in [initialize].
170	#
171	send_gdb "watch ival3\n"
172	gdb_expect {
173	    -re ".*\[Ww\]atchpoint 3: ival3\r\n$gdb_prompt $" {
174	        pass "set watchpoint on ival3"
175	    }
176	    -re ".*$gdb_prompt $" { fail "set watchpoint on ival3"  }
177	    timeout { fail "set watchpoint on ival3 (timeout)"  }
178	}
179
180        set wp_set 1
181
182	# "info watch" is the same as "info break"
183
184	send_gdb "info watch\n"
185	gdb_expect {
186	    -re "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*\r\n3\[ \]*.*watchpoint.*ival3\r\n$gdb_prompt $" {
187	        pass "watchpoint found in watchpoint/breakpoint table"
188	    }
189	    -re ".*$gdb_prompt $" {
190	        fail "watchpoint found in watchpoint/breakpoint table"
191	    }
192	    timeout {
193	       fail "watchpoint found in watchpoint/breakpoint table"
194	    }
195	}
196
197	# After installing the watchpoint, we disable it until we are ready
198	# to use it.  This allows the test program to run at full speed until
199	# we get to the first marker function.
200
201	send_gdb "disable 3\n"
202	gdb_expect {
203	    -re "disable 3\[\r\n\]+$gdb_prompt $" { pass "disable watchpoint" }
204	    -re ".*$gdb_prompt $" { fail "disable watchpoint"  }
205	    timeout { fail "disable watchpoint (timeout)"  }
206	}
207    }
208
209    # After reaching the marker function, enable the watchpoint.
210
211    if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] {
212      return ;
213    }
214
215
216    gdb_test "break func1" "Breakpoint.*at.*"
217    gdb_test "set \$func1_breakpoint_number = \$bpnum" ""
218
219    gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
220	"continue to breakpoint at func1"
221
222    # Continue until the first change, from -1 to 0
223
224    send_gdb "cont\n"
225    gdb_expect {
226	-re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
227	    pass "watchpoint hit, first time"
228	}
229	-re "Continuing.*Breakpoint.*func1.*$gdb_prompt $" {
230	    setup_xfail "m68*-*-*" 2597
231	    fail "thought it hit breakpoint at func1 twice"
232	    gdb_test "delete \$func1_breakpoint_number" ""
233	    gdb_test "continue" "\
234Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
235		"watchpoint hit, first time"
236	}
237	-re ".*$gdb_prompt $" { fail "watchpoint hit, first time" ; return }
238	timeout { fail "watchpoint hit, first time (timeout)" ; return }
239	eof { fail "watchpoint hit, first time (eof)" ; return }
240    }
241
242    # Check that the hit count is reported correctly
243    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 1 time.*" "Watchpoint hit count is 1"
244
245    gdb_test "delete \$func1_breakpoint_number" ""
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, second time"
249
250    # Check that the hit count is reported correctly
251    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 2 times.*" "Watchpoint hit count is 2"
252
253    # Continue until the next change, from 1 to 2.
254    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
255
256    # Check that the hit count is reported correctly
257    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 3 times.*" "Watchpoint hit count is 3"
258
259    # Continue until the next change, from 2 to 3.
260    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
261
262    # Check that the hit count is reported correctly
263    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 4 times.*" "Watchpoint hit count is 4"
264
265    # Continue until the next change, from 3 to 4.
266    # Note that this one is outside the loop.
267
268    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
269
270    # Check that the hit count is reported correctly
271    gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 5 times.*" "Watchpoint hit count is 5"
272
273    # Continue until we hit the finishing marker function.
274    # Make sure we hit no more watchpoints.
275
276    gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
277	"continue to marker2"
278
279    # Disable the watchpoint so we run at full speed until we exit.
280
281    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "watchpoint disabled" ] {
282      return ;
283    }
284
285
286    # Run until process exits.
287
288    if [target_info exists gdb,noresults] { return }
289
290    gdb_continue_to_end "continue to exit in test_simple_watchpoint"
291}
292
293# Test disabling watchpoints.
294
295proc test_disabling_watchpoints {} {
296    global gdb_prompt
297    global binfile
298    global srcfile
299    global decimal
300    global hex
301
302    # "info watch" is the same as "info break"
303    gdb_test "info watch" "\[0-9\]+\[ \]*breakpoint.*marker1.*\r\n\[0-9\]+\[ \]*breakpoint.*marker2.*\r\n\[0-9]+\[ \]*.*watchpoint.*ival3\r\n\.*\[0-9\]+ times.*" "watchpoints found in watchpoint/breakpoint table"
304
305    # Ensure that the watchpoint is disabled when we startup.
306
307    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_disabling_watchpoints" ] {
308      return 0;
309    }
310
311
312    # Run until we get to the first marker function.
313
314    gdb_run_cmd
315    set timeout 600
316    gdb_expect {
317	-re "Breakpoint 1, marker1 .*$gdb_prompt $" {
318	    pass "run to marker1 in test_disabling_watchpoints"
319	}
320	-re ".*$gdb_prompt $" {
321	    fail "run to marker1 in test_disabling_watchpoints"
322	    return
323	}
324	timeout {
325	    fail "run to marker1 in test_disabling_watchpoints (timeout)"
326	    return
327	}
328    }
329
330    # After reaching the marker function, enable the watchpoint.
331
332    if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] {
333      return ;
334    }
335
336
337    # Continue until the first change, from -1 to 0
338    # Don't check the old value, because on VxWorks the variable value
339    # will not have been reinitialized.
340    gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
341
342    # Continue until the next change, from 0 to 1.
343    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"
344
345    # Disable the watchpoint but leave breakpoints
346
347    if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] {
348      return 0;
349    }
350
351
352    # Check watchpoint list, looking for the entry that confirms the
353    # watchpoint is disabled.
354    gdb_test "info watchpoints" "\[0-9]+\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
355
356    # Continue until we hit the finishing marker function.
357    # Make sure we hit no more watchpoints.
358    gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
359	"disabled watchpoint skipped"
360
361    if [target_info exists gdb,noresults] { return }
362
363    gdb_continue_to_end "continue to exit in test_disabling_watchpoints"
364}
365
366# Test stepping and other mundane operations with watchpoints enabled
367proc test_stepping {} {
368    global gdb_prompt
369
370    if [runto marker1] then {
371	gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
372
373	# Well, let's not be too mundane.  It should be a *bit* of a challenge
374	gdb_test "break func2 if 0" "Breakpoint.*at.*"
375	gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
376
377	gdb_test "p func1 ()" "= 73" \
378	    "calling function with watchpoint enabled"
379
380	#
381	# "finish" brings us back to main.
382	# On some targets (e.g. alpha) gdb will stop from the finish in midline
383	# of the marker1 call. This is due to register restoring code on
384	# the alpha and might be caused by stack adjustment instructions
385	# on other targets. In this case we will step once more.
386	#
387
388	send_gdb "finish\n"
389	gdb_expect {
390	    -re "Run.*exit from.*marker1.* at" {
391		pass "finish from marker1"
392	    }
393	    default { fail "finish from marker1 (timeout)" ; return }
394	}
395
396	gdb_expect {
397	    -re "marker1 \\(\\);.*$gdb_prompt $" {
398		send_gdb "step\n"
399		exp_continue
400	    }
401	    -re "func1 \\(\\);.*$gdb_prompt $" {
402		pass "back at main from marker1"
403	    }
404	    -re ".*$gdb_prompt $" {
405		fail "back at main from marker1"
406	    }
407	    default { fail "back at main from marker1 (timeout)" ; return }
408	}
409
410	gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
411
412	# Now test that "until" works.  It's a bit tricky to test
413	# "until", because compilers don't always arrange the code
414	# exactly the same way, and we might get slightly different
415	# sequences of statements.  But the following should be true
416	# (if not it is a compiler or a debugger bug): The user who
417	# does "until" at every statement of a loop should end up
418	# stepping through the loop once, and the debugger should not
419	# stop for any of the remaining iterations.
420
421	gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
422	gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
423	send_gdb "until\n"
424	gdb_expect {
425	    -re "(for \\(count = 0|\}).*$gdb_prompt $" {
426		gdb_test "until" "ival1 = count; /. Outside loop ./" \
427		    "until out of loop"
428	    }
429	    -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
430		pass "until out of loop"
431	    }
432	    -re ".*$gdb_prompt $" {
433		fail "until out of loop"
434	    }
435	    default { fail "until out of loop (timeout)" ; return }
436	}
437
438	gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
439    }
440}
441
442# Test stepping and other mundane operations with watchpoints enabled
443proc test_watchpoint_triggered_in_syscall {} {
444    global gdb_prompt
445
446    # These tests won't work without printf support.
447    if [gdb_skip_stdio_test "watchpoints triggered in syscall"] {
448	return;
449    }
450    # Run until we get to the first marker function.
451    set x 0
452    set y 0
453    set testname "Watch buffer passed to read syscall"
454    if [runto marker2] then {
455	gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
456	gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
457	gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
458	gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
459	gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
460	gdb_test "break marker4" ".*Breakpoint.*"
461
462	gdb_test "set doread = 1" ""
463
464	# If we send_gdb "123\n" before gdb has switched the tty, then it goes
465	# to gdb, not the inferior, and we lose.  So that is why we have
466	# watchpoint.c prompt us, so we can wait for that prompt.
467	send_gdb "continue\n";
468	gdb_expect {
469	    -re "Continuing\\.\r\ntype stuff for buf now:" {
470		pass "continue to read"
471	    }
472	    default {
473		fail "continue to read";
474		return ;
475	    }
476	}
477
478	send_gdb "123\n"
479	gdb_expect {
480	    -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
481	    -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
482	    -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
483	    -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
484	    -re ".*$gdb_prompt $" { pass "sent 123" }
485	    timeout { fail "sent 123 (timeout)" }
486	}
487
488	# Examine the values in buf to see how many watchpoints we
489	# should have printed.
490	send_gdb "print buf\[0\]\n"
491	gdb_expect {
492	    -re ".*= 49.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[0\]"}
493	    -re ".*= 0.*$gdb_prompt $" { pass "print buf\[0\]"}
494	    -re ".*$gdb_prompt $" { fail "print buf\[0\]"}
495	    default { fail "print buf\[0\]"}
496	}
497	send_gdb "print buf\[1\]\n"
498	gdb_expect {
499	    -re ".*= 50.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[1\]"}
500	    -re ".*= 0.*$gdb_prompt $" { pass "print buf\[1\]"}
501	    -re ".*$gdb_prompt $" { fail "print buf\[1\]"}
502	    default { fail "print buf\[1\]"}
503	}
504	send_gdb "print buf\[2\]\n"
505	gdb_expect {
506	    -re ".*= 51.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[2\]"}
507	    -re ".*= 0.*$gdb_prompt $" { pass "print buf\[2\]"}
508	    -re ".*$gdb_prompt $" { fail "print buf\[2\]"}
509	    default { fail "print buf\[2\]"}
510	}
511	send_gdb "print buf\[3\]\n"
512	gdb_expect {
513	    -re ".*= 10.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[3\]"}
514	    -re ".*= 0.*$gdb_prompt $" { pass "print buf\[3\]"}
515	    -re ".*$gdb_prompt $" { fail "print buf\[3\]" }
516	    default { fail "print buf\[3\]" }
517	}
518
519	# Did we find what we were looking for?  If not, flunk it.
520	if [expr $x==$y] then { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
521
522	# Continue until we hit the finishing marker function.
523	# Make sure we hit no more watchpoints.
524	gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
525	    "continue to marker4"
526
527	# Disable everything so we can finish the program at full speed
528	gdb_test "disable" "" "disable in test_watchpoint_triggered_in_syscall"
529
530	if [target_info exists gdb,noresults] { return }
531
532	gdb_continue_to_end "continue to exit in test_watchpoint_triggered_in_syscall"
533    }
534}
535
536# Do a simple test of of watching through a pointer when the pointer
537# itself changes.  Should add some more complicated stuff here.
538
539proc test_complex_watchpoint {} {
540    global gdb_prompt
541
542    if [runto marker4] then {
543	gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
544	gdb_test "break marker5" ".*Breakpoint.*"
545
546	gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
547
548	# Continue until we hit the marker5 function.
549	# Make sure we hit no more watchpoints.
550
551	gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
552	    "did not trigger wrong watchpoint"
553
554        # Test watches of things declared locally in a function.
555        # In particular, test that a watch of stack-based things
556        # is deleted when the stack-based things go out of scope.
557        #
558	gdb_test "disable" "" "disable in test_complex_watchpoint"
559        gdb_test "break marker6" ".*Breakpoint.*"
560        gdb_test "cont" "Continuing.*Breakpoint.*marker6 \\(\\).*" \
561            "continue to marker6"
562        gdb_test "break func2" ".*Breakpoint.*"
563        gdb_test "cont" "Continuing.*func2.*"
564
565        # Test a watch of a single stack-based variable, whose scope
566        # is the function we're now in.  This should auto-delete when
567        # execution exits the scope of the watchpoint.
568        #
569        gdb_test "watch local_a" ".*\[Ww\]atchpoint \[0-9\]*: local_a" "set local watch"
570        gdb_test "cont" "\[Ww\]atchpoint.*local_a.*" "trigger local watch"
571        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" "self-delete local watch"
572
573        gdb_test "cont" "Continuing.*func2.*"
574        # We should be in "func2" again now.  Test a watch of an
575        # expression which includes both a stack-based local and
576        # something whose scope is larger than this invocation
577        # of "func2".  This should also auto-delete.
578        #
579        gdb_test "watch local_a + ival5" ".*\[Ww\]atchpoint \[0-9\]*: local_a . ival5" \
580                 "set partially local watch"
581        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
582                 "trigger1 partially local watch"
583        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
584                 "trigger2 partially local watch"
585        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
586                 "self-delete partially local watch"
587
588        # We should be in "func2" again now.  Test a watch of a
589        # static (non-stack-based) local.  Since this has scope
590        # across any invocations of "func2", it should not auto-
591        # delete.
592        #
593        gdb_test "cont" "Continuing.*func2.*"
594        gdb_test "watch static_b" ".*\[Ww\]atchpoint \[0-9\]*: static_b" \
595                 "set static local watch"
596        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: static_b.*" \
597                 "trigger static local watch"
598        gdb_test "cont" "Continuing.*marker6 \\(\\).*" \
599                 "continue after trigger static local watch"
600        gdb_test "info break" ".*watchpoint.*static_b.*" \
601                 "static local watch did not self-delete"
602
603        # We should be in "recurser" now.  Test a watch of a stack-
604        # based local.  Symbols mentioned in a watchpoint are bound
605        # at watchpoint-creation.  Thus, a watch of a stack-based
606        # local to a recursing function should be bound only to that
607        # one invocation, and should not trigger for other invocations.
608        #
609        gdb_test "tbreak recurser" ".*Breakpoint.*"
610        gdb_test "cont" "Continuing.*recurser.*"
611        gdb_test "watch local_x" ".*\[Ww\]atchpoint \[0-9\]*: local_x" \
612                 "set local watch in recursive call"
613        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_x.*New value = 2.*" \
614                 "trigger local watch in recursive call"
615        gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
616                 "self-delete local watch in recursive call"
617
618	# Disable everything so we can finish the program at full speed
619	gdb_test "disable" "" "disable in test_complex_watchpoint"
620
621	if [target_info exists gdb,noresults] { return }
622
623	gdb_continue_to_end "continue to exit in test_complex_watchpoint"
624    }
625}
626
627proc test_watchpoint_and_breakpoint {} {
628    global gdb_prompt
629
630    # This is a test for PR gdb/38, which involves setting a
631    # watchpoint right after you've reached a breakpoint.
632
633    if [runto func3] then {
634	gdb_breakpoint [gdb_get_line_number "second x assignment"]
635	gdb_continue_to_breakpoint "second x assignment"
636	gdb_test "watch x" ".*atchpoint \[0-9\]+: x"
637	gdb_test_multiple "next" "next after watch x" {
638	    -re ".*atchpoint \[0-9\]+: x\r\n\r\nOld value = 0\r\nNew value = 1\r\n.*$gdb_prompt $" {
639		pass "next after watch x"
640	    }
641	    -re "\[0-9\]+\[\t \]+y = 1;\r\n$gdb_prompt $" {
642		kfail "gdb/38" "next after watch x"
643	    }
644	}
645    }
646}
647
648# Start with a fresh gdb.
649
650gdb_exit
651gdb_start
652gdb_reinitialize_dir $srcdir/$subdir
653gdb_load $binfile
654set prev_timeout $timeout
655set timeout 600
656verbose "Timeout now 600 sec.\n"
657
658if [initialize] then {
659
660    test_simple_watchpoint
661
662    # The IDT/sim monitor only has 8 (!) open files, of which it uses
663    # 4 (!).  So we have to make sure one program exits before
664    # starting another one.
665    if [istarget "mips-idt-*"] then {
666	gdb_exit
667	gdb_start
668	gdb_reinitialize_dir $srcdir/$subdir
669	gdb_load $binfile
670	initialize
671    }
672
673    test_disabling_watchpoints
674
675    # See above.
676    if [istarget "mips-idt-*"] then {
677	gdb_exit
678	gdb_start
679	gdb_reinitialize_dir $srcdir/$subdir
680	gdb_load $binfile
681	initialize
682    }
683
684    if ![target_info exists gdb,cannot_call_functions] {
685	test_stepping
686
687	# See above.
688	if [istarget "mips-idt-*"] then {
689	    gdb_exit
690	    gdb_start
691	    gdb_reinitialize_dir $srcdir/$subdir
692	    gdb_load $binfile
693	    initialize
694	}
695    }
696
697    # Only enabled for some targets merely because it has not been tested
698    # elsewhere.
699    # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4
700    # breakpoint before stopping for the watchpoint.  I don't know why.
701    if {[istarget "hppa*-*-*"]} then {
702	test_watchpoint_triggered_in_syscall
703    }
704
705    # See above.
706    if [istarget "mips-idt-*"] then {
707	gdb_exit
708	gdb_start
709	gdb_reinitialize_dir $srcdir/$subdir
710	gdb_load $binfile
711	initialize
712    }
713
714    # Only enabled for some targets merely because it has not been tested
715    # elsewhere.
716    if {[istarget "hppa*-*-*"] || \
717	    [istarget "sparc*-*-sunos*"] || \
718	    [istarget "m32r-*-*"]} then {
719	test_complex_watchpoint
720    }
721
722    # Verify that a user can force GDB to use "slow" watchpoints.
723    # (This proves rather little on kernels that don't support
724    # fast watchpoints, but still...)
725    #
726    if ![runto_main] then { fail "watch tests suppressed" }
727
728    send_gdb "set can-use-hw-watchpoints 0\n"
729    gdb_expect {
730      -re "$gdb_prompt $"\
731              {pass "disable fast watches"}
732      timeout {fail "(timeout) disable fast watches"}
733    }
734    send_gdb "show can-use-hw-watchpoints\n"
735    gdb_expect {
736      -re "Debugger's willingness to use watchpoint hardware is 0.*$gdb_prompt $"\
737              {pass "show disable fast watches"}
738      -re "$gdb_prompt $"\
739              {fail "show disable fast watches"}
740      timeout {fail "(timeout) show disable fast watches"}
741    }
742    send_gdb "watch ival3 if  count > 1\n"
743    gdb_expect {
744      -re "Watchpoint \[0-9\]*: ival3.*$gdb_prompt $"\
745              {pass "set slow conditional watch"}
746      -re "$gdb_prompt $"\
747              {fail "set slow conditional watch"}
748      timeout {fail "(timeout) set slow conditional watch"}
749    }
750    send_gdb "continue\n"
751    gdb_expect {
752      -re "Watchpoint \[0-9\]*: ival3.*Old value = 1.*New value = 2.*$gdb_prompt $"\
753              {pass "trigger slow conditional watch"}
754      -re "$gdb_prompt $"\
755              {fail "trigger slow conditional watch"}
756      timeout {fail "(timeout) trigger slow conditional watch"}
757    }
758
759    # We've explicitly disabled hardware watches.  Verify that GDB
760    #
761    #
762    send_gdb "rwatch ival3\n"
763    gdb_expect {
764      -re "Expression cannot be implemented with read/access watchpoint..*$gdb_prompt $"\
765              {pass "rwatch disallowed when can-set-hw-watchpoints cleared"}
766      -re "$gdb_prompt $"\
767              {fail "rwatch disallowed when can-set-hw-watchpoints cleared"}
768      timeout {fail "(timeout) rwatch disallowed when can-use-hw-watchpoints cleared"}
769    }
770
771    # Read- and access watchpoints are unsupported on HP-UX.  Verify
772    # that GDB gracefully responds to requests to create them.
773    #
774    if [istarget "hppa*-*-hpux*"] then {
775      send_gdb "set can-use-hw-watchpoints 1\n"
776      gdb_expect {
777        -re "$gdb_prompt $"\
778                {pass "enable fast watches"}
779        timeout {fail "(timeout) enable fast watches"}
780      }
781      send_gdb "rwatch ival3\n"
782      gdb_expect {
783        -re "Target does not have this type of hardware watchpoint support.*$gdb_prompt $"\
784                {pass "read watches disallowed"}
785        -re "$gdb_prompt $"\
786                {fail "read watches disallowed"}
787        timeout {fail "(timeout) read watches disallowed"}
788      }
789
790      send_gdb "awatch ival3\n"
791      gdb_expect {
792        -re "Target does not have this type of hardware watchpoint support.*$gdb_prompt $"\
793                {pass "access watches disallowed"}
794        -re "$gdb_prompt $"\
795                {fail "access watches disallowed"}
796        timeout {fail "(timeout) access watches disallowed"}
797      }
798    }
799
800    # See above.
801    if [istarget "mips-idt-*"] then {
802	gdb_exit
803	gdb_start
804	gdb_reinitialize_dir $srcdir/$subdir
805	gdb_load $binfile
806	initialize
807    }
808
809    test_watchpoint_and_breakpoint
810}
811
812# Restore old timeout
813set timeout $prev_timeout
814verbose "Timeout now $timeout sec.\n"
815