1 # Copyright (C) 1992, 1994, 1997 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:
18 # bug-gdb@prep.ai.mit.edu
20 # This file was written by Fred Fish. (fnf@cygnus.com)
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 if [gdb_test "break marker1" "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker1" ] {
64 if [gdb_test "break marker2" "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker2" ] {
69 if [gdb_test "info break" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*" "info break in watchpoint.exp" ] {
74 if [gdb_test "watch ival3" ".*\[Ww\]atchpoint 3: ival3" "set watchpoint on ival3" ] {
79 # "info watch" is the same as "info break"
81 if [gdb_test "info watch" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*\r\n3\[ \]*.*watchpoint.*ival3" "watchpoint found in watchpoint/breakpoint table" ] {
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.
90 if [gdb_test "disable 3" "disable 3\[\r\n\]+" "disable watchpoint" ] {
99 # Test simple watchpoint.
102 proc test_simple_watchpoint {} {
107 # Ensure that the watchpoint is disabled when we startup.
109 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] {
114 # Run until we get to the first marker function.
119 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
120 pass "run to marker1 in test_simple_watchpoint"
122 -re ".*$gdb_prompt $" {
123 fail "run to marker1 in test_simple_watchpoint"
127 fail "run to marker1 in test_simple_watchpoint (timeout)"
132 # After reaching the marker function, enable the watchpoint.
134 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] {
139 gdb_test "break func1" "Breakpoint.*at.*"
140 gdb_test "set \$func1_breakpoint_number = \$bpnum" ""
142 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
143 "continue to breakpoint at func1"
145 # Continue until the first change, from -1 to 0
149 -re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
150 pass "watchpoint hit, first time"
152 -re "Continuing.*Breakpoint.*func1.*$gdb_prompt $" {
153 setup_xfail "m68*-*-*" 2597
154 fail "thought it hit breakpoint at func1 twice"
155 gdb_test "delete \$func1_breakpoint_number" ""
156 gdb_test "continue" "\
157 Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
158 "watchpoint hit, first time"
160 -re ".*$gdb_prompt $" { fail "watchpoint hit, first time" ; return }
161 timeout { fail "watchpoint hit, first time (timeout)" ; return }
162 eof { fail "watchpoint hit, first time (eof)" ; return }
165 gdb_test "delete \$func1_breakpoint_number" ""
167 # Continue until the next change, from 0 to 1.
168 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit, second time"
170 # Continue until the next change, from 1 to 2.
171 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
173 # Continue until the next change, from 2 to 3.
174 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
176 # Continue until the next change, from 3 to 4.
177 # Note that this one is outside the loop.
179 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
181 # Continue until we hit the finishing marker function.
182 # Make sure we hit no more watchpoints.
184 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
185 "continue to marker2"
187 # Disable the watchpoint so we run at full speed until we exit.
189 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "watchpoint disabled" ] {
194 # Run until process exits.
196 if [target_info exists gdb,noresults] { return }
198 gdb_test "cont" "Continuing.*Program exited normally.*" \
199 "continue to exit in test_simple_watchpoint"
202 # Test disabling watchpoints.
204 proc test_disabling_watchpoints {} {
211 # Ensure that the watchpoint is disabled when we startup.
213 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_disabling_watchpoints" ] {
218 # Run until we get to the first marker function.
223 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
224 pass "run to marker1 in test_disabling_watchpoints"
226 -re ".*$gdb_prompt $" {
227 fail "run to marker1 in test_disabling_watchpoints"
231 fail "run to marker1 in test_disabling_watchpoints (timeout)"
236 # After reaching the marker function, enable the watchpoint.
238 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] {
243 # Continue until the first change, from -1 to 0
244 # Don't check the old value, because on VxWorks the variable value
245 # will not have been reinitialized.
246 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
248 # Continue until the next change, from 0 to 1.
249 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"
251 # Disable the watchpoint but leave breakpoints
253 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] {
258 # Check watchpoint list, looking for the entry that confirms the
259 # watchpoint is disabled.
260 gdb_test "info watchpoints" "3\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
262 # Continue until we hit the finishing marker function.
263 # Make sure we hit no more watchpoints.
264 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
265 "disabled watchpoint skipped"
267 if [target_info exists gdb,noresults] { return }
269 gdb_test "cont" "Continuing.*Program exited normally.*" \
270 "continue to exit in test_disabling_watchpoints"
273 # Test stepping and other mundane operations with watchpoints enabled
274 proc test_stepping {} {
277 if [runto marker1] then {
278 gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
280 # Well, let's not be too mundane. It should be a *bit* of a challenge
281 gdb_test "break func2 if 0" "Breakpoint.*at.*"
282 gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
284 # The HPPA has a problem here if it's not using hardware watchpoints
285 if {[ istarget "hppa*-*-*" ] && ![ istarget "hppa*-*-*bsd*" ]} then {
286 # Don't actually try doing the call, if we do we can't continue.
288 fail "calling function with watchpoint enabled"
290 # The problem is that GDB confuses stepping through the call
291 # dummy with hitting the breakpoint at the end of the call dummy.
292 # Will be fixed once all architectures define
293 # CALL_DUMMY_BREAKPOINT_OFFSET.
295 # This doesn't occur if the call dummy starts with a call,
296 # because we are out of the dummy by the first time the inferior
298 clear_xfail "m68*-*-*"
299 clear_xfail "i*86*-*-*"
300 clear_xfail "vax-*-*"
301 # The following architectures define CALL_DUMMY_BREAKPOINT_OFFSET.
302 clear_xfail "alpha-*-*"
303 clear_xfail "mips*-*-*"
304 clear_xfail "sparc-*-*"
305 clear_xfail "hppa*-*-*bsd*"
306 # It works with the generic inferior function calling code too.
307 clear_xfail "mn10200*-*-*"
308 clear_xfail "mn10300*-*-*"
309 gdb_test "p func1 ()" "= 73" \
310 "calling function with watchpoint enabled"
314 # "finish" brings us back to main.
315 # On some targets (e.g. alpha) gdb will stop from the finish in midline
316 # of the marker1 call. This is due to register restoring code on
317 # the alpha and might be caused by stack adjustment instructions
318 # on other targets. In this case we will step once more.
323 -re "Run.*exit from.*marker1.* at" { }
324 default { fail "finish from marker1" ; return }
328 -re "marker1 \\(\\);.*$gdb_prompt $" {
332 -re "func1 \\(\\);.*$gdb_prompt $" {
333 pass "finish from marker1"
335 -re ".*$gdb_prompt $" {
336 fail "finish from marker1"
338 default { fail "finish from marker1" ; return }
341 gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
343 # Now test that "until" works. It's a bit tricky to test
344 # "until", because compilers don't always arrange the code
345 # exactly the same way, and we might get slightly different
346 # sequences of statements. But the following should be true
347 # (if not it is a compiler or a debugger bug): The user who
348 # does "until" at every statement of a loop should end up
349 # stepping through the loop once, and the debugger should not
350 # stop for any of the remaining iterations.
352 gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
353 gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
356 -re "(for \\(count = 0|\}).*$gdb_prompt $" {
357 gdb_test "until" "ival1 = count; /. Outside loop ./" \
360 -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
361 pass "until out of loop"
363 -re ".*$gdb_prompt $" {
364 fail "until out of loop"
366 default { fail "until out of loop" ; return }
369 gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
373 # Test stepping and other mundane operations with watchpoints enabled
374 proc test_watchpoint_triggered_in_syscall {} {
377 if [target_info exists gdb,noinferiorio] {
378 verbose "Skipping test_watchpoint_triggered_in_syscall due to noinferiorio"
381 # Run until we get to the first marker function.
384 set testname "Watch buffer passed to read syscall"
385 if [runto marker2] then {
386 gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
387 gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
388 gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
389 gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
390 gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
391 gdb_test "break marker4" ".*Breakpoint.*"
393 gdb_test "set doread = 1" ""
395 # If we send_gdb "123\n" before gdb has switched the tty, then it goes
396 # to gdb, not the inferior, and we lose. So that is why we have
397 # watchpoint.c prompt us, so we can wait for that prompt.
398 send_gdb "continue\n";
400 -re "Continuing\\.\r\ntype stuff for buf now:" {
401 pass "continue to read"
404 fail "continue to read";
411 -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
412 -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
413 -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
414 -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
415 -re ".*$gdb_prompt $" { pass "sent 123" }
416 timeout { fail "sent 123 (timeout)" }
419 # Examine the values in buf to see how many watchpoints we
420 # should have printed.
421 send_gdb "print buf\[0\]\n"
423 -re ".*= 49.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[0\]"}
424 -re ".*= 0.*$gdb_prompt $" { pass "print buf\[0\]"}
425 -re ".*$gdb_prompt $" { fail "print buf\[0\]"}
426 default { fail "print buf\[0\]"}
428 send_gdb "print buf\[1\]\n"
430 -re ".*= 50.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[1\]"}
431 -re ".*= 0.*$gdb_prompt $" { pass "print buf\[1\]"}
432 -re ".*$gdb_prompt $" { fail "print buf\[1\]"}
433 default { fail "print buf\[1\]"}
435 send_gdb "print buf\[2\]\n"
437 -re ".*= 51.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[2\]"}
438 -re ".*= 0.*$gdb_prompt $" { pass "print buf\[2\]"}
439 -re ".*$gdb_prompt $" { fail "print buf\[2\]"}
440 default { fail "print buf\[2\]"}
442 send_gdb "print buf\[3\]\n"
444 -re ".*= 10.*$gdb_prompt $" { set y [expr $y+1]; pass "print buf\[3\]"}
445 -re ".*= 0.*$gdb_prompt $" { pass "print buf\[3\]"}
446 -re ".*$gdb_prompt $" { fail "print buf\[3\]" }
447 default { fail "print buf\[3\]" }
450 # Did we find what we were looking for? If not, flunk it.
451 if [expr $x==$y] then { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
453 # Continue until we hit the finishing marker function.
454 # Make sure we hit no more watchpoints.
455 gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
456 "continue to marker4"
458 # Disable everything so we can finish the program at full speed
459 gdb_test "disable" "" "disable in test_watchpoint_triggered_in_syscall"
461 if [target_info exists gdb,noresults] { return }
463 gdb_test "cont" "Continuing.*Program exited normally.*" \
464 "continue to exit in test_watchpoint_triggered_in_syscall"
468 # Do a simple test of of watching through a pointer when the pointer
469 # itself changes. Should add some more complicated stuff here.
471 proc test_complex_watchpoint {} {
474 if [runto marker4] then {
475 gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
476 gdb_test "break marker5" ".*Breakpoint.*"
478 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
480 # Continue until we hit the marker5 function.
481 # Make sure we hit no more watchpoints.
483 gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
484 "did not trigger wrong watchpoint"
486 # Disable everything so we can finish the program at full speed
487 gdb_test "disable" "" "disable in test_complex_watchpoint"
489 if [target_info exists gdb,noresults] { return }
492 gdb_test "cont" "Continuing.*Program exited normally.*" \
493 "continue to exit in test_complex_watchpoint"
497 # Start with a fresh gdb.
500 gdb_reinitialize_dir $srcdir/$subdir
503 verbose "Timeout now 600 sec.\n"
505 if [initialize] then {
507 test_simple_watchpoint
509 # The IDT/sim monitor only has 8 (!) open files, of which it uses
510 # 4 (!). So we have to make sure one program exits before
511 # starting another one.
512 if [istarget "mips-idt-*"] then {
515 gdb_reinitialize_dir $srcdir/$subdir
520 test_disabling_watchpoints
523 if [istarget "mips-idt-*"] then {
526 gdb_reinitialize_dir $srcdir/$subdir
531 if ![target_info exsts gdb,cannot_call_functions] {
535 if [istarget "mips-idt-*"] then {
538 gdb_reinitialize_dir $srcdir/$subdir
544 # Only enabled for some targets merely because it has not been tested
546 # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4
547 # breakpoint before stopping for the watchpoint. I don't know why.
548 if {[istarget "hppa*-*-*"]} then {
549 test_watchpoint_triggered_in_syscall
553 if [istarget "mips-idt-*"] then {
556 gdb_reinitialize_dir $srcdir/$subdir
561 # Only enabled for some targets merely because it has not been tested
563 if {[istarget "hppa*-*-*"] || \
564 [istarget "sparc*-*-sunos*"] || \
565 [istarget "m32r-*-*"]} then {
566 test_complex_watchpoint