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