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