1 # Copyright (C) 1992, 1994 Free Software Foundation, Inc.
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.
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.
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. */
17 # Please email any bugs, comments, and/or additions to this file to:
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}"
37 # Prepare for watchpoint tests by setting up two breakpoints and one
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.
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:
49 # 1 Breakpoint marker1()
50 # 2 Breakpoint marker2()
59 send_gdb "break marker1\n"
61 -re "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*$prompt $" {
62 pass "set breakpoint at marker1"
64 -re ".*$prompt $" { fail "set breakpoint at marker1" ; return 0 }
65 timeout { fail "set breakpoint at marker1 (timeout)" ; return 0 }
68 send_gdb "break marker2\n"
70 -re "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*$prompt $" {
71 pass "set breakpoint at marker2"
73 -re ".*$prompt $" { fail "set breakpoint at marker2" ; return 0 }
74 timeout { fail "set breakpoint at marker2 (timeout)" ; return 0 }
77 send_gdb "info break\n"
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 }
84 send_gdb "watch ival3\n"
86 -re ".*\[Ww\]atchpoint 3: ival3\r\n$prompt $" {
87 pass "set watchpoint on ival3"
89 -re ".*$prompt $" { fail "set watchpoint on ival3" ; return 0 }
90 timeout { fail "set watchpoint on ival3 (timeout)" ; return 0 }
93 # "info watch" is the same as "info break"
95 send_gdb "info watch\n"
97 -re "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*\r\n3\[ \]*.*watchpoint.*ival3\r\n$prompt $" {
98 pass "watchpoint found in watchpoint/breakpoint table"
101 fail "watchpoint found in watchpoint/breakpoint table" ; return 0
104 fail "watchpoint found in watchpoint/breakpoint table (timeout)" ; return 0
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.
112 send_gdb "disable 3\n"
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 }
123 # Test simple watchpoint.
126 proc test_simple_watchpoint {} {
132 # Ensure that the watchpoint is disabled when we startup.
134 send_gdb "disable 3\n"
136 -re "^disable 3\[\r\n\]+$prompt $" {
137 pass "disable watchpoint in test_simple_watchpoint"
140 fail "disable watchpoint in test_simple_watchpoint"
144 fail "disable watchpoint in test_simple_watchpoint (timeout)"
149 # Run until we get to the first marker function.
154 -re "Breakpoint 1, marker1 .*$prompt $" {
155 pass "run to marker1 in test_simple_watchpoint"
158 fail "run to marker1 in test_simple_watchpoint"
162 fail "run to marker1 in test_simple_watchpoint (timeout)"
167 # After reaching the marker function, enable the watchpoint.
169 send_gdb "enable 3\n"
171 -re "^enable 3\[\r\n\]+$prompt $" { pass "enable watchpoint" }
172 -re ".*$prompt $" { fail "enable watchpoint" ; return }
173 timeout { fail "enable watchpoint (timeout)" ; return }
176 gdb_test "break func1" "Breakpoint.*at.*"
177 gdb_test "set \$func1_breakpoint_number = \$bpnum" ""
179 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
180 "continue to breakpoint at func1"
182 # Continue until the first change, from -1 to 0
186 -re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$prompt $" {
187 pass "watchpoint hit, first time"
189 -re "Continuing.*Breakpoint.*func1.*$prompt $" {
190 setup_xfail "m68*-*-*" 2597
191 fail "thought it hit breakpoint at func1 twice"
192 gdb_test "delete \$func1_breakpoint_number" ""
193 gdb_test "continue" "\
194 Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
195 "watchpoint hit, first time"
197 -re ".*$prompt $" { fail "watchpoint hit, first time" ; return }
198 timeout { fail "watchpoint hit, first time (timeout)" ; return }
199 eof { fail "watchpoint hit, first time (eof)" ; return }
202 gdb_test "delete \$func1_breakpoint_number" ""
204 # Continue until the next change, from 0 to 1.
205 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit, second time"
207 # Continue until the next change, from 1 to 2.
208 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
210 # Continue until the next change, from 2 to 3.
211 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
213 # Continue until the next change, from 3 to 4.
214 # Note that this one is outside the loop.
216 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
218 # Continue until we hit the finishing marker function.
219 # Make sure we hit no more watchpoints.
221 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
222 "continue to marker2"
224 # Disable the watchpoint so we run at full speed until we exit.
226 send_gdb "disable 3\n"
228 -re "^disable 3\[\r\n\]+$prompt $" { pass "watchpoint disabled" }
229 -re ".*$prompt $" { fail "watchpoint disabled" ; return }
230 timeout { fail "watchpoint disabled (timeout)" ; return }
233 # Run until process exits.
235 if $noresults==1 then { return }
237 gdb_test "cont" "Continuing.*Program exited normally.*" \
238 "continue to exit in test_simple_watchpoint"
241 # Test disabling watchpoints.
243 proc test_disabling_watchpoints {} {
251 # Ensure that the watchpoint is disabled when we startup.
253 send_gdb "disable 3\n"
255 -re "^disable 3\[\r\n\]+$prompt $" {
256 pass "disable watchpoint in test_disabling_watchpoints"
259 fail "disable watchpoint in test_disabling_watchpoints"
263 fail "disable watchpoint in test_disabling_watchpoints (timeout)"
268 # Run until we get to the first marker function.
273 -re "Breakpoint 1, marker1 .*$prompt $" {
274 pass "run to marker1 in test_disabling_watchpoints"
277 fail "run to marker1 in test_disabling_watchpoints"
281 fail "run to marker1 in test_disabling_watchpoints (timeout)"
286 # After reaching the marker function, enable the watchpoint.
288 send_gdb "enable 3\n"
290 -re "^enable 3\[\r\n\]+$prompt $" { pass "watchpoint enabled" }
291 -re ".*$prompt $" { fail "watchpoint enabled" ; return }
292 timeout { fail "watchpoint enabled (timeout)" ; return }
295 # Continue until the first change, from -1 to 0
296 # Don't check the old value, because on VxWorks the variable value
297 # will not have been reinitialized.
298 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
300 # Continue until the next change, from 0 to 1.
301 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"
303 # Disable the watchpoint but leave breakpoints
305 send_gdb "disable 3\n"
307 -re "^disable 3\[\r\n\]+$prompt $" {
308 pass "disable watchpoint #2 in test_disabling_watchpoints"
311 "disable watchpoint #2 in test_disabling_watchpoints"
315 "disable watchpoint #2 in test_disabling_watchpoints (timeout)"
320 # Check watchpoint list, looking for the entry that confirms the
321 # watchpoint is disabled.
322 gdb_test "info watchpoints" "3\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
324 # Continue until we hit the finishing marker function.
325 # Make sure we hit no more watchpoints.
326 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
327 "disabled watchpoint skipped"
329 if $noresults==1 then { return }
331 gdb_test "cont" "Continuing.*Program exited normally.*" \
332 "continue to exit in test_disabling_watchpoints"
335 # Test stepping and other mundane operations with watchpoints enabled
336 proc test_stepping {} {
339 if [runto marker1] then {
340 gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
342 # Well, let's not be too mundane. It should be a *bit* of a challenge
343 gdb_test "break func2 if 0" "Breakpoint.*at.*"
344 gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
346 # The HPPA has a problem here if it's not using hardware watchpoints
347 if {[ istarget "hppa*-*-*" ] && ![ istarget "hppa*-*-*bsd*" ]} then {
348 # Don't actually try doing the call, if we do we can't continue.
350 fail "calling function with watchpoint enabled"
352 # The problem is that GDB confuses stepping through the call
353 # dummy with hitting the breakpoint at the end of the call dummy.
354 # Will be fixed once all architectures define
355 # CALL_DUMMY_BREAKPOINT_OFFSET.
357 # This doesn't occur if the call dummy starts with a call,
358 # because we are out of the dummy by the first time the inferior
360 clear_xfail "m68*-*-*"
361 clear_xfail "i*86*-*-*"
362 clear_xfail "vax-*-*"
363 # The following architectures define CALL_DUMMY_BREAKPOINT_OFFSET.
364 clear_xfail "alpha-*-*"
365 clear_xfail "mips*-*-*"
366 clear_xfail "sparc-*-*"
367 clear_xfail "hppa*-*-*bsd*"
368 gdb_test "p func1 ()" "= 73" \
369 "calling function with watchpoint enabled"
373 # "finish" brings us back to main.
374 # On some targets (e.g. alpha) gdb will stop from the finish in midline
375 # of the marker1 call. This is due to register restoring code on
376 # the alpha and might be caused by stack adjustment instructions
377 # on other targets. In this case we will step once more.
382 -re "Run.*exit from.*marker1.* at" { }
383 default { fail "finish from marker1" ; return }
386 -re "marker1 \\(\\);.*$prompt $" {
390 -re "func1 \\(\\);.*$prompt $" {
391 pass "finish from marker1"
394 fail "finish from marker1"
396 default { fail "finish from marker1" ; return }
399 gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
401 # Now test that "until" works. It's a bit tricky to test
402 # "until", because compilers don't always arrange the code
403 # exactly the same way, and we might get slightly different
404 # sequences of statements. But the following should be true
405 # (if not it is a compiler or a debugger bug): The user who
406 # does "until" at every statement of a loop should end up
407 # stepping through the loop once, and the debugger should not
408 # stop for any of the remaining iterations.
410 gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
411 gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
414 -re "(for \\(count = 0|\}).*$prompt $" {
415 gdb_test "until" "ival1 = count; /. Outside loop ./" \
418 -re "ival1 = count; /. Outside loop ./.*$prompt $" {
419 pass "until out of loop"
422 fail "until out of loop"
424 default { fail "until out of loop" ; return }
427 gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
431 # Test stepping and other mundane operations with watchpoints enabled
432 proc test_watchpoint_triggered_in_syscall {} {
438 verbose "Skipping test_watchpoint_triggered_in_syscall due to noinferiorio"
441 # Run until we get to the first marker function.
444 set testname "Watch buffer passed to read syscall"
445 if [runto marker2] then {
446 gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
447 gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
448 gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
449 gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
450 gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
451 gdb_test "break marker4" ".*Breakpoint.*"
453 gdb_test "set doread = 1" ""
455 # If we send_gdb "123\n" before gdb has switched the tty, then it goes
456 # to gdb, not the inferior, and we lose. So that is why we have
457 # watchpoint.c prompt us, so we can wait for that prompt.
458 send_gdb "continue\n"
460 -re "Continuing\\.\r\ntype stuff for buf now:" {
461 pass "continue to read"
463 default { fail "continue to read"; return }
467 -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
468 -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
469 -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
470 -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
471 -re ".*$prompt $" { pass "sent 123" }
472 timeout { fail "sent 123 (timeout)" }
475 # Examine the values in buf to see how many watchpoints we
476 # should have printed.
477 send_gdb "print buf\[0\]\n"
479 -re ".*= 49.*$prompt $" { set y [expr $y+1]; pass "print buf\[0\]"}
480 -re ".*= 0.*$prompt $" { pass "print buf\[0\]"}
481 -re ".*$prompt $" { fail "print buf\[0\]"}
482 default { fail "print buf\[0\]"}
484 send_gdb "print buf\[1\]\n"
486 -re ".*= 50.*$prompt $" { set y [expr $y+1]; pass "print buf\[1\]"}
487 -re ".*= 0.*$prompt $" { pass "print buf\[1\]"}
488 -re ".*$prompt $" { fail "print buf\[1\]"}
489 default { fail "print buf\[1\]"}
491 send_gdb "print buf\[2\]\n"
493 -re ".*= 51.*$prompt $" { set y [expr $y+1]; pass "print buf\[2\]"}
494 -re ".*= 0.*$prompt $" { pass "print buf\[2\]"}
495 -re ".*$prompt $" { fail "print buf\[2\]"}
496 default { fail "print buf\[2\]"}
498 send_gdb "print buf\[3\]\n"
500 -re ".*= 10.*$prompt $" { set y [expr $y+1]; pass "print buf\[3\]"}
501 -re ".*= 0.*$prompt $" { pass "print buf\[3\]"}
502 -re ".*$prompt $" { fail "print buf\[3\]" }
503 default { fail "print buf\[3\]" }
506 # Did we find what we were looking for? If not, flunk it.
507 if [expr $x==$y] then { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
509 # Continue until we hit the finishing marker function.
510 # Make sure we hit no more watchpoints.
511 gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
512 "continue to marker4"
514 # Disable everything so we can finish the program at full speed
515 gdb_test "disable" "" "disable in test_watchpoint_triggered_in_syscall"
517 if $noresults==1 then { return }
519 gdb_test "cont" "Continuing.*Program exited normally.*" \
520 "continue to exit in test_watchpoint_triggered_in_syscall"
524 # Do a simple test of of watching through a pointer when the pointer
525 # itself changes. Should add some more complicated stuff here.
527 proc test_complex_watchpoint {} {
531 if [runto marker4] then {
532 gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
533 gdb_test "break marker5" ".*Breakpoint.*"
535 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
537 # Continue until we hit the marker5 function.
538 # Make sure we hit no more watchpoints.
540 gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
541 "did not trigger wrong watchpoint"
543 # Disable everything so we can finish the program at full speed
544 gdb_test "disable" "" "disable in test_complex_watchpoint"
546 if $noresults==1 then { return }
548 gdb_test "cont" "Continuing.*Program exited normally.*" \
549 "continue to exit in test_complex_watchpoint"
553 # Start with a fresh gdb.
557 gdb_reinitialize_dir $srcdir/$subdir
560 verbose "Timeout now 600 sec.\n"
562 if [initialize] then {
564 test_simple_watchpoint
566 # The IDT/sim monitor only has 8 (!) open files, of which it uses
567 # 4 (!). So we have to make sure one program exits before
568 # starting another one.
569 if [istarget "mips-idt-*"] then {
572 gdb_reinitialize_dir $srcdir/$subdir
577 test_disabling_watchpoints
580 if [istarget "mips-idt-*"] then {
583 gdb_reinitialize_dir $srcdir/$subdir
591 if [istarget "mips-idt-*"] then {
594 gdb_reinitialize_dir $srcdir/$subdir
599 # Only enabled for some targets merely because it has not been tested
601 # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4
602 # breakpoint before stopping for the watchpoint. I don't know why.
603 if {[istarget "hppa*-*-*"]} then {
604 test_watchpoint_triggered_in_syscall
608 if [istarget "mips-idt-*"] then {
611 gdb_reinitialize_dir $srcdir/$subdir
616 # Only enabled for some targets merely because it has not been tested
618 if {[istarget "hppa*-*-*"] || [istarget "sparc*-*-sunos*"]} then {
619 test_complex_watchpoint