]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/watchpoint.exp
* config/sh/tm-sh.h (BELIEVE_PCC_PROMOTION): Define, so that
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / watchpoint.exp
1 # Copyright (C) 1992, 1994, 1997 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
107 # Ensure that the watchpoint is disabled when we startup.
108
109 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] {
110 return 0;
111 }
112
113
114 # Run until we get to the first marker function.
115
116 gdb_run_cmd
117 set timeout 600
118 gdb_expect {
119 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
120 pass "run to marker1 in test_simple_watchpoint"
121 }
122 -re ".*$gdb_prompt $" {
123 fail "run to marker1 in test_simple_watchpoint"
124 return
125 }
126 timeout {
127 fail "run to marker1 in test_simple_watchpoint (timeout)"
128 return
129 }
130 }
131
132 # After reaching the marker function, enable the watchpoint.
133
134 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] {
135 return ;
136 }
137
138
139 gdb_test "break func1" "Breakpoint.*at.*"
140 gdb_test "set \$func1_breakpoint_number = \$bpnum" ""
141
142 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
143 "continue to breakpoint at func1"
144
145 # Continue until the first change, from -1 to 0
146
147 send_gdb "cont\n"
148 gdb_expect {
149 -re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
150 pass "watchpoint hit, first time"
151 }
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"
159 }
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 }
163 }
164
165 gdb_test "delete \$func1_breakpoint_number" ""
166
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"
169
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"
172
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"
175
176 # Continue until the next change, from 3 to 4.
177 # Note that this one is outside the loop.
178
179 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 3.*New value = 4.*ival3 = count; ival4 = count;.*" "watchpoint hit, fifth time"
180
181 # Continue until we hit the finishing marker function.
182 # Make sure we hit no more watchpoints.
183
184 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
185 "continue to marker2"
186
187 # Disable the watchpoint so we run at full speed until we exit.
188
189 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "watchpoint disabled" ] {
190 return ;
191 }
192
193
194 # Run until process exits.
195
196 if [target_info exists gdb,noresults] { return }
197
198 gdb_test "cont" "Continuing.*Program exited normally.*" \
199 "continue to exit in test_simple_watchpoint"
200 }
201
202 # Test disabling watchpoints.
203
204 proc test_disabling_watchpoints {} {
205 global gdb_prompt
206 global binfile
207 global srcfile
208 global decimal
209 global hex
210
211 # Ensure that the watchpoint is disabled when we startup.
212
213 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_disabling_watchpoints" ] {
214 return 0;
215 }
216
217
218 # Run until we get to the first marker function.
219
220 gdb_run_cmd
221 set timeout 600
222 gdb_expect {
223 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
224 pass "run to marker1 in test_disabling_watchpoints"
225 }
226 -re ".*$gdb_prompt $" {
227 fail "run to marker1 in test_disabling_watchpoints"
228 return
229 }
230 timeout {
231 fail "run to marker1 in test_disabling_watchpoints (timeout)"
232 return
233 }
234 }
235
236 # After reaching the marker function, enable the watchpoint.
237
238 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] {
239 return ;
240 }
241
242
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"
247
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"
250
251 # Disable the watchpoint but leave breakpoints
252
253 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] {
254 return 0;
255 }
256
257
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"
261
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"
266
267 if [target_info exists gdb,noresults] { return }
268
269 gdb_test "cont" "Continuing.*Program exited normally.*" \
270 "continue to exit in test_disabling_watchpoints"
271 }
272
273 # Test stepping and other mundane operations with watchpoints enabled
274 proc test_stepping {} {
275 global gdb_prompt
276
277 if [runto marker1] then {
278 gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
279
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" " = .*"
283
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.
287 setup_xfail "*-*-*"
288 fail "calling function with watchpoint enabled"
289 } else {
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.
294 setup_xfail "*-*-*"
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
297 # stops.
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"
311 }
312
313 #
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.
319 #
320
321 send_gdb "finish\n"
322 gdb_expect {
323 -re "Run.*exit from.*marker1.* at" { }
324 default { fail "finish from marker1" ; return }
325 }
326
327 gdb_expect {
328 -re "marker1 \\(\\);.*$gdb_prompt $" {
329 send_gdb "step\n"
330 exp_continue
331 }
332 -re "func1 \\(\\);.*$gdb_prompt $" {
333 pass "finish from marker1"
334 }
335 -re ".*$gdb_prompt $" {
336 fail "finish from marker1"
337 }
338 default { fail "finish from marker1" ; return }
339 }
340
341 gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
342
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.
351
352 gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
353 gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
354 send_gdb "until\n"
355 gdb_expect {
356 -re "(for \\(count = 0|\}).*$gdb_prompt $" {
357 gdb_test "until" "ival1 = count; /. Outside loop ./" \
358 "until out of loop"
359 }
360 -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
361 pass "until out of loop"
362 }
363 -re ".*$gdb_prompt $" {
364 fail "until out of loop"
365 }
366 default { fail "until out of loop" ; return }
367 }
368
369 gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
370 }
371 }
372
373 # Test stepping and other mundane operations with watchpoints enabled
374 proc test_watchpoint_triggered_in_syscall {} {
375 global gdb_prompt
376
377 if [target_info exists gdb,noinferiorio] {
378 verbose "Skipping test_watchpoint_triggered_in_syscall due to noinferiorio"
379 return
380 }
381 # Run until we get to the first marker function.
382 set x 0
383 set y 0
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.*"
392
393 gdb_test "set doread = 1" ""
394
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";
399 gdb_expect {
400 -re "Continuing\\.\r\ntype stuff for buf now:" {
401 pass "continue to read"
402 }
403 default {
404 fail "continue to read";
405 return ;
406 }
407 }
408
409 send_gdb "123\n"
410 gdb_expect {
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)" }
417 }
418
419 # Examine the values in buf to see how many watchpoints we
420 # should have printed.
421 send_gdb "print buf\[0\]\n"
422 gdb_expect {
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\]"}
427 }
428 send_gdb "print buf\[1\]\n"
429 gdb_expect {
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\]"}
434 }
435 send_gdb "print buf\[2\]\n"
436 gdb_expect {
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\]"}
441 }
442 send_gdb "print buf\[3\]\n"
443 gdb_expect {
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\]" }
448 }
449
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)"}
452
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"
457
458 # Disable everything so we can finish the program at full speed
459 gdb_test "disable" "" "disable in test_watchpoint_triggered_in_syscall"
460
461 if [target_info exists gdb,noresults] { return }
462
463 gdb_test "cont" "Continuing.*Program exited normally.*" \
464 "continue to exit in test_watchpoint_triggered_in_syscall"
465 }
466 }
467
468 # Do a simple test of of watching through a pointer when the pointer
469 # itself changes. Should add some more complicated stuff here.
470
471 proc test_complex_watchpoint {} {
472 global gdb_prompt
473
474 if [runto marker4] then {
475 gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
476 gdb_test "break marker5" ".*Breakpoint.*"
477
478 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
479
480 # Continue until we hit the marker5 function.
481 # Make sure we hit no more watchpoints.
482
483 gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
484 "did not trigger wrong watchpoint"
485
486 # Disable everything so we can finish the program at full speed
487 gdb_test "disable" "" "disable in test_complex_watchpoint"
488
489 if [target_info exists gdb,noresults] { return }
490
491
492 gdb_test "cont" "Continuing.*Program exited normally.*" \
493 "continue to exit in test_complex_watchpoint"
494 }
495 }
496
497 # Start with a fresh gdb.
498
499 gdb_start
500 gdb_reinitialize_dir $srcdir/$subdir
501 gdb_load $binfile
502 set timeout 600
503 verbose "Timeout now 600 sec.\n"
504
505 if [initialize] then {
506
507 test_simple_watchpoint
508
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 {
513 gdb_exit
514 gdb_start
515 gdb_reinitialize_dir $srcdir/$subdir
516 gdb_load $binfile
517 initialize
518 }
519
520 test_disabling_watchpoints
521
522 # See above.
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 if ![target_info exsts gdb,cannot_call_functions] {
532 test_stepping
533
534 # See above.
535 if [istarget "mips-idt-*"] then {
536 gdb_exit
537 gdb_start
538 gdb_reinitialize_dir $srcdir/$subdir
539 gdb_load $binfile
540 initialize
541 }
542 }
543
544 # Only enabled for some targets merely because it has not been tested
545 # elsewhere.
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
550 }
551
552 # See above.
553 if [istarget "mips-idt-*"] then {
554 gdb_exit
555 gdb_start
556 gdb_reinitialize_dir $srcdir/$subdir
557 gdb_load $binfile
558 initialize
559 }
560
561 # Only enabled for some targets merely because it has not been tested
562 # elsewhere.
563 if {[istarget "hppa*-*-*"] || \
564 [istarget "sparc*-*-sunos*"] || \
565 [istarget "m32r-*-*"]} then {
566 test_complex_watchpoint
567 }
568 }