* lib/gdb.exp(gdb_test): Surround the result pattern with
[external/binutils.git] / gdb / testsuite / gdb.base / watchpoint.exp
1 # Copyright (C) 1992, 1994 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 2 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, write to the Free Software
15 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
16
17 # Please email any bugs, comments, and/or additions to this file to:
18 # bug-gdb@prep.ai.mit.edu
19
20 # This file was written by Fred Fish. (fnf@cygnus.com)
21
22 if $tracelevel then {
23     strace $tracelevel
24 }
25
26 set prms_id 0
27 set bug_id 0
28
29 set testfile "watchpoint"
30 set srcfile ${testfile}.c
31 set binfile ${objdir}/${subdir}/${testfile}
32 if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33     perror "Couldn't compile ${srcfile}"
34     return -1
35 }
36
37 # Prepare for watchpoint tests by setting up two breakpoints and one
38 # watchpoint.
39 #
40 # We use breakpoints at marker functions to get past all the startup code,
41 # so we can get to the watchpoints in a reasonable amount of time from a
42 # known starting point.
43 #
44 # For simplicity, so we always know how to reference specific breakpoints or
45 # watchpoints by number, we expect a particular ordering and numbering of
46 # each in the combined breakpoint/watchpoint table, as follows:
47 #
48 #       Number          What            Where
49 #       1               Breakpoint      marker1()
50 #       2               Breakpoint      marker2()
51 #       3               Watchpoint      ival3
52
53 proc initialize {} {
54     global gdb_prompt
55     global hex
56     global decimal
57     global srcfile
58
59     if [gdb_test "break marker1" "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker1" ] { 
60       return 0; 
61     }
62
63
64     if [gdb_test "break marker2" "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker2" ] { 
65       return 0; 
66     }
67
68
69     if [gdb_test "info break" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*" "info break in watchpoint.exp" ] { 
70       return 0; 
71     }
72
73
74     if [gdb_test "watch ival3" ".*\[Ww\]atchpoint 3: ival3" "set watchpoint on ival3" ] { 
75       return 0; 
76     }
77
78
79     # "info watch" is the same as "info break"
80
81     if [gdb_test "info watch" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*\r\n3\[ \]*.*watchpoint.*ival3" "watchpoint found in watchpoint/breakpoint table" ] { 
82       return 0; 
83     }
84
85
86     # After installing the watchpoint, we disable it until we are ready
87     # to use it.  This allows the test program to run at full speed until
88     # we get to the first marker function.
89
90     if [gdb_test "disable 3" "disable 3\[\r\n\]+" "disable watchpoint" ] { 
91       return 0; 
92     }
93
94
95     return 1
96 }
97
98 #
99 # Test simple watchpoint.
100 #
101
102 proc test_simple_watchpoint {} {
103     global gdb_prompt
104     global hex
105     global decimal
106     global noresults
107
108     # Ensure that the watchpoint is disabled when we startup.
109
110     if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] { 
111       return 0; 
112     }
113
114
115     # Run until we get to the first marker function.
116
117     gdb_run_cmd
118     set timeout 600
119     expect {
120         -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
121             pass "run to marker1 in test_simple_watchpoint"
122         }
123         -re ".*$gdb_prompt $" {
124             fail "run to marker1 in test_simple_watchpoint"
125             return
126         }
127         timeout {
128             fail "run to marker1 in test_simple_watchpoint (timeout)"
129             return
130         }
131     }
132
133     # After reaching the marker function, enable the watchpoint.
134
135     if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] { 
136       return ; 
137     }
138
139
140     gdb_test "break func1" "Breakpoint.*at.*"
141     gdb_test "set \$func1_breakpoint_number = \$bpnum" ""
142
143     gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
144         "continue to breakpoint at func1"
145
146     # Continue until the first change, from -1 to 0
147
148     send_gdb "cont\n"
149     expect {
150         -re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
151             pass "watchpoint hit, first time"
152         }
153         -re "Continuing.*Breakpoint.*func1.*$gdb_prompt $" {
154             setup_xfail "m68*-*-*" 2597
155             fail "thought it hit breakpoint at func1 twice"
156             gdb_test "delete \$func1_breakpoint_number" ""
157             gdb_test "continue" "\
158 Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
159                 "watchpoint hit, first time"
160         }
161         -re ".*$gdb_prompt $" { fail "watchpoint hit, first time" ; return }
162         timeout { fail "watchpoint hit, first time (timeout)" ; return }
163         eof { fail "watchpoint hit, first time (eof)" ; return }
164     }
165
166     gdb_test "delete \$func1_breakpoint_number" ""
167
168     # Continue until the next change, from 0 to 1.
169     gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit, second time"
170
171     # Continue until the next change, from 1 to 2.
172     gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
173     
174     # Continue until the next change, from 2 to 3.
175     gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
176
177     # Continue until the next change, from 3 to 4.
178     # Note that this one is outside the loop.
179
180     gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
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 $noresults==1 then { return }
198
199     gdb_test "cont" "Continuing.*Program exited normally.*" \
200         "continue to exit in test_simple_watchpoint"
201 }
202
203 # Test disabling watchpoints.
204
205 proc test_disabling_watchpoints {} {
206     global gdb_prompt
207     global binfile
208     global srcfile
209     global decimal
210     global hex
211     global noresults
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     expect {
225         -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
226             pass "run to marker1 in test_disabling_watchpoints"
227         }
228         -re ".*$gdb_prompt $" {
229             fail "run to marker1 in test_disabling_watchpoints"
230             return
231         }
232         timeout {
233             fail "run to marker1 in test_disabling_watchpoints (timeout)"
234             return
235         }
236     }
237
238     # After reaching the marker function, enable the watchpoint.
239
240     if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] { 
241       return ; 
242     }
243
244
245     # Continue until the first change, from -1 to 0
246     # Don't check the old value, because on VxWorks the variable value
247     # will not have been reinitialized.
248     gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
249     
250     # Continue until the next change, from 0 to 1.
251     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"
252     
253     # Disable the watchpoint but leave breakpoints
254
255     if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] { 
256       return 0; 
257     }
258
259
260     # Check watchpoint list, looking for the entry that confirms the
261     # watchpoint is disabled.
262     gdb_test "info watchpoints" "3\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
263
264     # Continue until we hit the finishing marker function.
265     # Make sure we hit no more watchpoints.
266     gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
267         "disabled watchpoint skipped"
268     
269     if $noresults==1 then { return }
270
271     gdb_test "cont" "Continuing.*Program exited normally.*" \
272         "continue to exit in test_disabling_watchpoints"
273 }
274
275 # Test stepping and other mundane operations with watchpoints enabled
276 proc test_stepping {} {
277     global gdb_prompt
278
279     if [runto marker1] then {
280         gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
281
282         # Well, let's not be too mundane.  It should be a *bit* of a challenge
283         gdb_test "break func2 if 0" "Breakpoint.*at.*"
284         gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
285
286         # The HPPA has a problem here if it's not using hardware watchpoints
287         if {[ istarget "hppa*-*-*" ] && ![ istarget "hppa*-*-*bsd*" ]} then {
288             # Don't actually try doing the call, if we do we can't continue.
289             setup_xfail "*-*-*"
290             fail "calling function with watchpoint enabled"
291         } else {
292             # The problem is that GDB confuses stepping through the call
293             # dummy with hitting the breakpoint at the end of the call dummy.
294             # Will be fixed once all architectures define 
295             # CALL_DUMMY_BREAKPOINT_OFFSET.
296             setup_xfail "*-*-*"
297             # This doesn't occur if the call dummy starts with a call,
298             # because we are out of the dummy by the first time the inferior
299             # stops.
300             clear_xfail "m68*-*-*"
301             clear_xfail "i*86*-*-*"
302             clear_xfail "vax-*-*"
303             # The following architectures define CALL_DUMMY_BREAKPOINT_OFFSET.
304             clear_xfail "alpha-*-*"
305             clear_xfail "mips*-*-*"
306             clear_xfail "sparc-*-*"
307             clear_xfail "hppa*-*-*bsd*"
308             gdb_test "p func1 ()" "= 73" \
309                 "calling function with watchpoint enabled"
310         }
311
312         # 
313         # "finish" brings us back to main.
314         # On some targets (e.g. alpha) gdb will stop from the finish in midline
315         # of the marker1 call. This is due to register restoring code on
316         # the alpha and might be caused by stack adjustment instructions
317         # on other targets. In this case we will step once more.
318         #
319
320         send_gdb "finish\n"
321         expect {
322             -re "Run.*exit from.*marker1.* at" { }
323             default { fail "finish from marker1" ; return }
324         }
325
326         expect {
327             -re "marker1 \\(\\);.*$gdb_prompt $" {
328                 send_gdb "step\n"
329                 exp_continue
330             }
331             -re "func1 \\(\\);.*$gdb_prompt $" {
332                 pass "finish from marker1"
333             }
334             -re ".*$gdb_prompt $" {
335                 fail "finish from marker1"
336             }
337             default { fail "finish from marker1" ; return }
338         }
339
340         gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
341
342         # Now test that "until" works.  It's a bit tricky to test
343         # "until", because compilers don't always arrange the code
344         # exactly the same way, and we might get slightly different
345         # sequences of statements.  But the following should be true
346         # (if not it is a compiler or a debugger bug): The user who
347         # does "until" at every statement of a loop should end up
348         # stepping through the loop once, and the debugger should not
349         # stop for any of the remaining iterations.
350
351         gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
352         gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
353         send_gdb "until\n"
354         expect {
355             -re "(for \\(count = 0|\}).*$gdb_prompt $" {
356                 gdb_test "until" "ival1 = count; /. Outside loop ./" \
357                     "until out of loop"
358             }
359             -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
360                 pass "until out of loop"
361             }
362             -re ".*$gdb_prompt $" {
363                 fail "until out of loop"
364             }
365             default { fail "until out of loop" ; return }
366         }
367
368         gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
369     }
370 }
371
372 # Test stepping and other mundane operations with watchpoints enabled
373 proc test_watchpoint_triggered_in_syscall {} {
374     global gdb_prompt
375     global noinferiorio
376     global noresults
377
378     if $noinferiorio {
379         verbose "Skipping test_watchpoint_triggered_in_syscall due to noinferiorio"
380         return
381     }
382     # Run until we get to the first marker function.
383     set x 0
384     set y 0
385     set testname "Watch buffer passed to read syscall"
386     if [runto marker2] then {
387         gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
388         gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
389         gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
390         gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
391         gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
392         gdb_test "break marker4" ".*Breakpoint.*"
393
394         gdb_test "set doread = 1" ""
395
396         # If we send_gdb "123\n" before gdb has switched the tty, then it goes
397         # to gdb, not the inferior, and we lose.  So that is why we have
398         # watchpoint.c prompt us, so we can wait for that prompt.
399         if [gdb_test "continue" "Continuing\\.\r\ntype stuff for buf now:" "continue to read" ] { 
400           return ; 
401         }
402
403         send_gdb "123\n"
404         expect {
405             -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
406             -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
407             -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
408             -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
409             -re ".*$gdb_prompt $" { pass "sent 123" }
410             timeout { fail "sent 123 (timeout)" }
411         }
412
413         # Examine the values in buf to see how many watchpoints we
414         # should have printed.
415         send_gdb "print buf\[0\]\n"
416         expect {
417             -re ".*= 49.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[0\]"}
418             -re ".*= 0.*$gdb_prompt $" { pass "print buf\[0\]"}
419             -re ".*$gdb_prompt $" { fail "print buf\[0\]"}
420             default { fail "print buf\[0\]"}
421         }
422         send_gdb "print buf\[1\]\n"
423         expect {
424             -re ".*= 50.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[1\]"}
425             -re ".*= 0.*$gdb_prompt $" { pass "print buf\[1\]"}
426             -re ".*$gdb_prompt $" { fail "print buf\[1\]"}
427             default { fail "print buf\[1\]"}
428         }
429         send_gdb "print buf\[2\]\n"
430         expect {
431             -re ".*= 51.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[2\]"}
432             -re ".*= 0.*$gdb_prompt $" { pass "print buf\[2\]"}
433             -re ".*$gdb_prompt $" { fail "print buf\[2\]"}
434             default { fail "print buf\[2\]"}
435         }
436         send_gdb "print buf\[3\]\n"
437         expect {
438             -re ".*= 10.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[3\]"}
439             -re ".*= 0.*$gdb_prompt $" { pass "print buf\[3\]"}
440             -re ".*$gdb_prompt $" { fail "print buf\[3\]" }
441             default { fail "print buf\[3\]" }
442         }
443
444         # Did we find what we were looking for?  If not, flunk it.
445         if [expr $x==$y] then { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
446
447         # Continue until we hit the finishing marker function.
448         # Make sure we hit no more watchpoints.
449         gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
450             "continue to marker4"
451
452         # Disable everything so we can finish the program at full speed
453         gdb_test "disable" "" "disable in test_watchpoint_triggered_in_syscall"
454
455         if $noresults==1 then { return }
456
457         gdb_test "cont" "Continuing.*Program exited normally.*" \
458             "continue to exit in test_watchpoint_triggered_in_syscall"
459     }
460 }
461
462 # Do a simple test of of watching through a pointer when the pointer
463 # itself changes.  Should add some more complicated stuff here.
464
465 proc test_complex_watchpoint {} {
466     global gdb_prompt
467     global noresults
468
469     if [runto marker4] then {
470         gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
471         gdb_test "break marker5" ".*Breakpoint.*"
472
473         gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
474
475         # Continue until we hit the marker5 function.
476         # Make sure we hit no more watchpoints.
477
478         gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
479             "did not trigger wrong watchpoint"
480
481         # Disable everything so we can finish the program at full speed
482         gdb_test "disable" "" "disable in test_complex_watchpoint"
483
484         if $noresults==1 then { return }
485
486         gdb_test "cont" "Continuing.*Program exited normally.*" \
487             "continue to exit in test_complex_watchpoint"
488     }
489 }
490
491 # Start with a fresh gdb.
492
493 gdb_start
494 gdb_reinitialize_dir $srcdir/$subdir
495 gdb_load $binfile
496 set timeout 600 
497 verbose "Timeout now 600 sec.\n"
498
499 if [initialize] then {
500
501     test_simple_watchpoint
502
503     # The IDT/sim monitor only has 8 (!) open files, of which it uses
504     # 4 (!).  So we have to make sure one program exits before
505     # starting another one.
506     if [istarget "mips-idt-*"] then {
507         gdb_exit
508         gdb_start
509         gdb_reinitialize_dir $srcdir/$subdir
510         gdb_load $binfile
511         initialize
512     }
513
514     test_disabling_watchpoints
515
516     # See above.
517     if [istarget "mips-idt-*"] then {
518         gdb_exit
519         gdb_start
520         gdb_reinitialize_dir $srcdir/$subdir
521         gdb_load $binfile
522         initialize
523     }
524
525     test_stepping
526
527     # See above.
528     if [istarget "mips-idt-*"] then {
529         gdb_exit
530         gdb_start
531         gdb_reinitialize_dir $srcdir/$subdir
532         gdb_load $binfile
533         initialize
534     }
535
536     # Only enabled for some targets merely because it has not been tested 
537     # elsewhere.
538     # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4 
539     # breakpoint before stopping for the watchpoint.  I don't know why.
540     if {[istarget "hppa*-*-*"]} then {
541         test_watchpoint_triggered_in_syscall
542     }
543
544     # See above.
545     if [istarget "mips-idt-*"] then {
546         gdb_exit
547         gdb_start
548         gdb_reinitialize_dir $srcdir/$subdir
549         gdb_load $binfile
550         initialize
551     }
552
553     # Only enabled for some targets merely because it has not been tested 
554     # elsewhere.
555     if {[istarget "hppa*-*-*"] || [istarget "sparc*-*-sunos*"]} then {
556         test_complex_watchpoint
557     }
558 }