2012-01-27 Pedro Alves <palves@redhat.com>
[binutils-gdb.git] / gdb / testsuite / gdb.base / watchpoint.exp
1 # Copyright 1992, 1994-2000, 2007-2012 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 3 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, see <http://www.gnu.org/licenses/>.
15
16 # This file was written by Fred Fish. (fnf@cygnus.com)
17
18
19 set testfile "watchpoint"
20 set srcfile ${testfile}.c
21 set binfile ${objdir}/${subdir}/${testfile}
22
23 if [get_compiler_info ${binfile}] {
24 return -1
25 }
26
27 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
28 untested watchpoint.exp
29 return -1
30 }
31
32 # True if we're forcing no hardware watchpoints.
33 set no_hw 0
34
35 # Prepare for watchpoint tests by setting up two breakpoints and one
36 # watchpoint.
37 #
38 # We use breakpoints at marker functions to get past all the startup code,
39 # so we can get to the watchpoints in a reasonable amount of time from a
40 # known starting point.
41 #
42 # For simplicity, so we always know how to reference specific breakpoints or
43 # watchpoints by number, we expect a particular ordering and numbering of
44 # each in the combined breakpoint/watchpoint table, as follows:
45 #
46 # Number What Where
47 # 1 Breakpoint marker1()
48 # 2 Breakpoint marker2()
49 # 3 Watchpoint ival3
50
51 proc initialize {} {
52 global gdb_prompt
53 global hex
54 global decimal
55 global srcfile
56
57 # Disable hardware watchpoints if necessary.
58 if [target_info exists gdb,no_hardware_watchpoints] {
59 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
60 }
61
62 if [gdb_test "break marker1" "Breakpoint 1 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker1" ] {
63 return 0;
64 }
65
66
67 if [gdb_test "break marker2" "Breakpoint 2 at $hex: file .*$srcfile, line $decimal.*" "set breakpoint at marker2" ] {
68 return 0;
69 }
70
71
72 if [gdb_test "info break" "1\[ \]*breakpoint.*marker1.*\r\n2\[ \]*breakpoint.*marker2.*" "info break in watchpoint.exp" ] {
73 return 0;
74 }
75
76 gdb_test "watch ival3" ".*\[Ww\]atchpoint 3: ival3.*" "set watchpoint on ival3"
77
78 if [gdb_test "info watch" "3\[ \]*.*watchpoint.*ival3" "watchpoint found in watchpoint/breakpoint table" ] {
79 return 0;
80 }
81
82
83 # After installing the watchpoint, we disable it until we are ready
84 # to use it. This allows the test program to run at full speed until
85 # we get to the first marker function.
86
87 if [gdb_test "disable 3" "disable 3\[\r\n\]+" "disable watchpoint" ] {
88 return 0;
89 }
90
91
92 return 1
93 }
94
95 proc maybe_clean_restart { } {
96 # The IDT/sim monitor only has 8 (!) open files, of which it uses
97 # 4 (!). So we have to make sure one program exits before
98 # starting another one.
99 if [istarget "mips-idt-*"] then {
100 global testfile
101 global no_hw
102
103 clean_restart $testfile
104
105 if {$no_hw} {
106 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
107 }
108 }
109 }
110
111 proc maybe_reinitialize { } {
112 # The IDT/sim monitor only has 8 (!) open files, of which it uses
113 # 4 (!). So we have to make sure one program exits before
114 # starting another one.
115 if [istarget "mips-idt-*"] then {
116 global testfile
117 global no_hw
118
119 clean_restart $testfile
120
121 if {$no_hw} {
122 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
123 }
124
125 initialize
126 }
127 }
128
129 #
130 # Test simple watchpoint.
131 #
132
133 proc test_simple_watchpoint {} {
134 global gdb_prompt
135 global hex
136 global decimal
137
138 # Ensure that the watchpoint is disabled when we startup.
139
140 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_simple_watchpoint" ] {
141 return 0;
142 }
143
144 # Run until we get to the first marker function.
145
146 gdb_run_cmd
147 set timeout 600
148 set test "run to marker1 in test_simple_watchpoint"
149 set retcode [gdb_test_multiple "" $test {
150 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
151 pass $test
152 }
153 }]
154
155 if { $retcode != 0 } {
156 return
157 }
158
159 # After reaching the marker function, enable the watchpoint.
160
161 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "enable watchpoint" ] {
162 return ;
163 }
164
165
166 gdb_test "break func1" "Breakpoint.*at.*"
167 gdb_test_no_output "set \$func1_breakpoint_number = \$bpnum"
168
169 gdb_test "continue" "Continuing.*Breakpoint \[0-9\]*, func1.*" \
170 "continue to breakpoint at func1"
171
172 # Continue until the first change, from -1 to 0
173
174 set test "watchpoint hit, first time"
175 gdb_test_multiple "cont" $test {
176 -re "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count; ival4 = count;.*$gdb_prompt $" {
177 pass $test
178 }
179 -re "Continuing.*Breakpoint.*func1.*$gdb_prompt $" {
180 setup_xfail "m68*-*-*" 2597
181 fail "thought it hit breakpoint at func1 twice"
182 gdb_test_no_output "delete \$func1_breakpoint_number"
183 gdb_test "continue" "\
184 Continuing.*\[Ww\]atchpoint.*ival3.*Old value = -1.*New value = 0.*ival3 = count;" \
185 $test
186 }
187 }
188
189 # Check that the hit count is reported correctly
190 gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 1 time.*" "Watchpoint hit count is 1"
191
192 gdb_test_no_output "delete \$func1_breakpoint_number"
193
194 # Continue until the next change, from 0 to 1.
195 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 0.*New value = 1.*ival3 = count; ival4 = count;.*" "watchpoint hit, second time"
196
197 # Check that the hit count is reported correctly
198 gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 2 times.*" "Watchpoint hit count is 2"
199
200 # Continue until the next change, from 1 to 2.
201 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 1.*New value = 2.*ival3 = count; ival4 = count;.*" "watchpoint hit, third time"
202
203 # Check that the hit count is reported correctly
204 gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 3 times.*" "Watchpoint hit count is 3"
205
206 # Continue until the next change, from 2 to 3.
207 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = 2.*New value = 3.*ival3 = count; ival4 = count;.*" "watchpoint hit, fourth time"
208
209 # Check that the hit count is reported correctly
210 gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 4 times.*" "Watchpoint hit count is 4"
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 # Check that the hit count is reported correctly
218 gdb_test "info break" ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+ival3\r\n\[ \t]+breakpoint already hit 5 times.*" "Watchpoint hit count is 5"
219
220 # Continue until we hit the finishing marker function.
221 # Make sure we hit no more watchpoints.
222
223 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \(\).*" \
224 "continue to marker2"
225
226 # Disable the watchpoint so we run at full speed until we exit.
227
228 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "watchpoint disabled" ] {
229 return ;
230 }
231
232
233 # Run until process exits.
234
235 if [target_info exists gdb,noresults] { return }
236
237 gdb_continue_to_end "continue to exit in test_simple_watchpoint"
238 }
239
240 # Test disabling watchpoints.
241
242 proc test_disabling_watchpoints {} {
243 global gdb_prompt
244 global binfile
245 global srcfile
246 global decimal
247 global hex
248
249 maybe_reinitialize
250
251 gdb_test "info watch" "\[0-9]+\[ \]*.*watchpoint.*ival3.*" "watchpoints found in watchpoint/breakpoint table"
252
253 # Ensure that the watchpoint is disabled when we startup.
254
255 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint in test_disabling_watchpoints" ] {
256 return 0;
257 }
258
259
260 # Run until we get to the first marker function.
261
262 gdb_run_cmd
263 set timeout 600
264 set test "run to marker1 in test_disabling_watchpoints"
265 set retcode [gdb_test_multiple "" $test {
266 -re "Breakpoint 1, marker1 .*$gdb_prompt $" {
267 pass $test
268 }
269 }]
270
271 if { $retcode != 0 } {
272 return
273 }
274
275 # After reaching the marker function, enable the watchpoint.
276
277 if [gdb_test "enable 3" "^enable 3\[\r\n\]+" "watchpoint enabled" ] {
278 return ;
279 }
280
281
282 # Continue until the first change, from -1 to 0
283 # Don't check the old value, because on VxWorks the variable value
284 # will not have been reinitialized.
285 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ival3.*Old value = .*New value = 0.*ival3 = count; ival4 = count;.*" "watchpoint hit in test_disabling_watchpoints, first time"
286
287 # Continue until the next change, from 0 to 1.
288 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"
289
290 # Disable the watchpoint but leave breakpoints
291
292 if [gdb_test "disable 3" "^disable 3\[\r\n\]+" "disable watchpoint #2 in test_disabling_watchpoints" ] {
293 return 0;
294 }
295
296
297 # Check watchpoint list, looking for the entry that confirms the
298 # watchpoint is disabled.
299 gdb_test "info watchpoints" "\[0-9]+\[ \]*.*watchpoint\[ \]*keep\[ \]*n\[ \]*ival3\r\n.*" "watchpoint disabled in table"
300
301 # Continue until we hit the finishing marker function.
302 # Make sure we hit no more watchpoints.
303 gdb_test "cont" "Continuing.*Breakpoint.*marker2 \\(\\).*" \
304 "disabled watchpoint skipped"
305
306 if [target_info exists gdb,noresults] { return }
307
308 gdb_continue_to_end "continue to exit in test_disabling_watchpoints"
309 }
310
311 # Test stepping and other mundane operations with watchpoints enabled
312 proc test_stepping {} {
313 global gdb_prompt
314
315 maybe_clean_restart
316
317 if [runto marker1] then {
318 gdb_test "watch ival2" ".*\[Ww\]atchpoint \[0-9\]*: ival2"
319
320 # Well, let's not be too mundane. It should be a *bit* of a challenge
321 gdb_test "break func2 if 0" "Breakpoint.*at.*"
322 gdb_test "p \$func2_breakpoint_number = \$bpnum" " = .*"
323
324 gdb_test "p func1 ()" "= 73" \
325 "calling function with watchpoint enabled"
326
327 #
328 # "finish" brings us back to main.
329 # On some targets (e.g. alpha) gdb will stop from the finish in midline
330 # of the marker1 call. This is due to register restoring code on
331 # the alpha and might be caused by stack adjustment instructions
332 # on other targets. In this case we will step once more.
333 #
334
335 send_gdb "finish\n"
336 gdb_expect {
337 -re "Run.*exit from.*marker1.* at" {
338 pass "finish from marker1"
339 }
340 default { fail "finish from marker1 (timeout)" ; return }
341 }
342
343 gdb_expect {
344 -re "marker1 \\(\\);.*$gdb_prompt $" {
345 send_gdb "step\n"
346 exp_continue
347 }
348 -re "func1 \\(\\);.*$gdb_prompt $" {
349 pass "back at main from marker1"
350 }
351 -re ".*$gdb_prompt $" {
352 fail "back at main from marker1"
353 }
354 default { fail "back at main from marker1 (timeout)" ; return }
355 }
356
357 gdb_test "next" "for \\(count = 0.*" "next to `for' in watchpoint.exp"
358
359 # Now test that "until" works. It's a bit tricky to test
360 # "until", because compilers don't always arrange the code
361 # exactly the same way, and we might get slightly different
362 # sequences of statements. But the following should be true
363 # (if not it is a compiler or a debugger bug): The user who
364 # does "until" at every statement of a loop should end up
365 # stepping through the loop once, and the debugger should not
366 # stop for any of the remaining iterations.
367
368 gdb_test "until" "ival1 = count.*" "until to ival1 assignment"
369 gdb_test "until" "ival3 = count.*" "until to ival3 assignment"
370 set test "until out of loop"
371 gdb_test_multiple "until" $test {
372 -re "(for \\(count = 0|\}).*$gdb_prompt $" {
373 gdb_test "until" "ival1 = count; /. Outside loop ./" $test
374 }
375 -re "ival1 = count; /. Outside loop ./.*$gdb_prompt $" {
376 pass $test
377 }
378 }
379
380 gdb_test "step" "ival2 = count.*" "step to ival2 assignment"
381 }
382 }
383
384 # Test stepping and other mundane operations with watchpoints enabled
385 proc test_watchpoint_triggered_in_syscall {} {
386 global gdb_prompt
387
388 # These tests won't work without printf support.
389 if [gdb_skip_stdio_test "watchpoints triggered in syscall"] {
390 return;
391 }
392 # Run until we get to the first marker function.
393 set x 0
394 set y 0
395 set testname "Watch buffer passed to read syscall"
396 if [runto marker2] then {
397 gdb_test "watch buf\[0\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[0\\\]"
398 gdb_test "watch buf\[1\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[1\\\]"
399 gdb_test "watch buf\[2\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[2\\\]"
400 gdb_test "watch buf\[3\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[3\\\]"
401 gdb_test "watch buf\[4\]" ".*\[Ww\]atchpoint \[0-9\]*: buf\\\[4\\\]"
402 gdb_test "break marker4" ".*Breakpoint.*"
403
404 gdb_test_no_output "set doread = 1"
405
406 # If we send gdb "123\n" before gdb has switched the tty, then it goes
407 # to gdb, not the inferior, and we lose. So that is why we have
408 # watchpoint.c prompt us, so we can wait for that prompt.
409
410 send_gdb "continue\n";
411 gdb_expect {
412 -re "Continuing\\.\r\ntype stuff for buf now:" {
413 pass "continue to read"
414 }
415 default {
416 fail "continue to read";
417 return ;
418 }
419 }
420
421 set test "sent 123"
422 gdb_test_multiple "123" $test {
423 -re ".*\[Ww\]atchpoint.*buf\\\[0\\\].*Old value = 0.*New value = 49\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
424 -re ".*\[Ww\]atchpoint.*buf\\\[1\\\].*Old value = 0.*New value = 50\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
425 -re ".*\[Ww\]atchpoint.*buf\\\[2\\\].*Old value = 0.*New value = 51\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
426 -re ".*\[Ww\]atchpoint.*buf\\\[3\\\].*Old value = 0.*New value = 10\[^\n\]*\n" { set x [expr $x+1] ; exp_continue }
427 -re ".*$gdb_prompt $" { pass $test }
428 }
429
430 # Examine the values in buf to see how many watchpoints we
431 # should have printed.
432 set test "print buf\[0\]"
433 gdb_test_multiple $test $test {
434 -re ".*= 49.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
435 -re ".*= 0.*$gdb_prompt $" { $test }
436 }
437 set test "print buf\[1\]"
438 gdb_test_multiple $test $test {
439 -re ".*= 50.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
440 -re ".*= 0.*$gdb_prompt $" { pass $test }
441 }
442 set test "print buf\[2\]"
443 gdb_test_multiple $test $test {
444 -re ".*= 51.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
445 -re ".*= 0.*$gdb_prompt $" { pass $test }
446 }
447 set test "print buf\[3\]"
448 gdb_test_multiple $test $test {
449 -re ".*= 10.*$gdb_prompt $" { set y [expr $y+1]; pass $test }
450 -re ".*= 0.*$gdb_prompt $" { pass $test }
451 }
452
453 # Did we find what we were looking for? If not, flunk it.
454 if [expr $x==$y] then { pass $testname } else { fail "$testname (only triggered $x watchpoints, expected $y)"}
455
456 # Continue until we hit the finishing marker function.
457 # Make sure we hit no more watchpoints.
458 gdb_test "cont" "Continuing.*Breakpoint.*marker4 \\(\\).*" \
459 "continue to marker4"
460
461 # Disable everything so we can finish the program at full speed
462 gdb_test_no_output "disable" "disable in test_watchpoint_triggered_in_syscall"
463
464 if [target_info exists gdb,noresults] { return }
465
466 gdb_continue_to_end "continue to exit in test_watchpoint_triggered_in_syscall"
467 }
468 }
469
470 # Do a simple test of of watching through a pointer when the pointer
471 # itself changes. Should add some more complicated stuff here.
472
473 proc test_complex_watchpoint {} {
474 global gdb_prompt
475
476 maybe_clean_restart
477
478 if [runto marker4] then {
479 gdb_test "watch ptr1->val" ".*\[Ww\]atchpoint \[0-9\]*: ptr1->val"
480 gdb_test "break marker5" ".*Breakpoint.*"
481
482 gdb_test "cont" "Continuing.*\[Ww\]atchpoint.*ptr1->val.*Old value = 1.*New value = 2.*" "Test complex watchpoint"
483
484 # Continue until we hit the marker5 function.
485 # Make sure we hit no more watchpoints.
486
487 gdb_test "cont" "Continuing.*Breakpoint.*marker5 \\(\\).*" \
488 "did not trigger wrong watchpoint"
489
490 # Test watches of things declared locally in a function.
491 # In particular, test that a watch of stack-based things
492 # is deleted when the stack-based things go out of scope.
493 #
494 gdb_test_no_output "disable" "disable in test_complex_watchpoint"
495 gdb_test "break marker6" ".*Breakpoint.*"
496 gdb_test "cont" "Continuing.*Breakpoint.*marker6 \\(\\).*" \
497 "continue to marker6"
498 gdb_test "break func2" ".*Breakpoint.*"
499 gdb_test "cont" "Continuing.*func2.*"
500
501 # Test a watch of a single stack-based variable, whose scope
502 # is the function we're now in. This should auto-delete when
503 # execution exits the scope of the watchpoint.
504 #
505 gdb_test "watch local_a" ".*\[Ww\]atchpoint \[0-9\]*: local_a" "set local watch"
506 gdb_test "cont" "\[Ww\]atchpoint.*local_a.*" "trigger local watch"
507 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" "self-delete local watch"
508
509 gdb_test "cont" "Continuing.*func2.*"
510 # We should be in "func2" again now. Test a watch of an
511 # expression which includes both a stack-based local and
512 # something whose scope is larger than this invocation
513 # of "func2". This should also auto-delete.
514 #
515 gdb_test "watch local_a + ival5" ".*\[Ww\]atchpoint \[0-9\]*: local_a . ival5" \
516 "set partially local watch"
517 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
518 "trigger1 partially local watch"
519 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_a . ival5.*" \
520 "trigger2 partially local watch"
521 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
522 "self-delete partially local watch"
523
524 # We should be in "func2" again now. Test a watch of a
525 # static (non-stack-based) local. Since this has scope
526 # across any invocations of "func2", it should not auto-
527 # delete.
528 #
529 gdb_test "cont" "Continuing.*func2.*"
530 gdb_test "watch static_b" ".*\[Ww\]atchpoint \[0-9\]*: static_b" \
531 "set static local watch"
532 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: static_b.*" \
533 "trigger static local watch"
534 gdb_test "cont" "Continuing.*marker6 \\(\\).*" \
535 "continue after trigger static local watch"
536 gdb_test "info break" ".*watchpoint.*static_b.*" \
537 "static local watch did not self-delete"
538
539 # We should be in "recurser" now. Test a watch of a stack-
540 # based local. Symbols mentioned in a watchpoint are bound
541 # at watchpoint-creation. Thus, a watch of a stack-based
542 # local to a recursing function should be bound only to that
543 # one invocation, and should not trigger for other invocations.
544 #
545 gdb_test "tbreak recurser" ".*breakpoint.*"
546 gdb_test "cont" "Continuing.*recurser.*"
547 gdb_test "next" "if \\(x > 0.*" "next past local_x initialization"
548 gdb_test "watch local_x" ".*\[Ww\]atchpoint \[0-9\]*: local_x" \
549 "set local watch in recursive call"
550 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: local_x.*New value = 2.*" \
551 "trigger local watch in recursive call"
552 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
553 "self-delete local watch in recursive call"
554
555 # Repeat the preceding test, but this time use "recurser::local_x" as
556 # the variable to track.
557 gdb_test "cont" "Continuing.*marker6.*"
558 gdb_test "tbreak recurser" ".*breakpoint.*"
559 gdb_test "cont" "Continuing.*recurser.*"
560 gdb_test "next" "if \\(x > 0.*" "next past local_x initialization"
561 gdb_test "watch recurser::local_x" ".*\[Ww\]atchpoint \[0-9\]*: recurser::local_x" \
562 "set local watch in recursive call with explicit scope"
563 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .*: recurser::local_x.*New value = 2.*" \
564 "trigger local watch with explicit scope in recursive call"
565 gdb_test "cont" "Continuing.*\[Ww\]atchpoint .* deleted because the program has left the block in.*which its expression is valid.*" \
566 "self-delete local watch with explicit scope in recursive call (2)"
567
568 # Disable everything so we can finish the program at full speed
569 gdb_test_no_output "disable" "disable in test_complex_watchpoint"
570
571 if [target_info exists gdb,noresults] { return }
572
573 gdb_continue_to_end "continue to exit in test_complex_watchpoint"
574 }
575 }
576
577 proc test_watchpoint_and_breakpoint {} {
578 global gdb_prompt
579
580 maybe_clean_restart
581
582 # This is a test for PR gdb/38, which involves setting a
583 # watchpoint right after you've reached a breakpoint.
584
585 if [runto func3] then {
586 gdb_breakpoint [gdb_get_line_number "second x assignment"]
587 gdb_continue_to_breakpoint "second x assignment"
588 gdb_test "watch x" ".*atchpoint \[0-9\]+: x"
589 gdb_test_multiple "next" "next after watch x" {
590 -re ".*atchpoint \[0-9\]+: x\r\n\r\nOld value = 0\r\nNew value = 1\r\n.*$gdb_prompt $" {
591 pass "next after watch x"
592 }
593 -re "\[0-9\]+\[\t \]+y = 1;\r\n$gdb_prompt $" {
594 kfail "gdb/38" "next after watch x"
595 }
596 }
597
598 gdb_test_no_output "delete \$bpnum" "delete watch x"
599 }
600 }
601
602 proc test_constant_watchpoint {} {
603 gdb_test "watch 5" "Cannot watch constant value `5'." "number is constant"
604 gdb_test "watch marker1" "Cannot watch constant value `marker1'." \
605 "marker1 is constant"
606 gdb_test "watch count + 6" ".*atchpoint \[0-9\]+: count \\+ 6"
607 gdb_test_no_output "delete \$bpnum" "delete watchpoint `count + 6'"
608 gdb_test "watch 7 + count" ".*atchpoint \[0-9\]+: 7 \\+ count"
609 gdb_test_no_output "delete \$bpnum" "delete watchpoint `7 + count'"
610 }
611
612 proc test_disable_enable_software_watchpoint {} {
613 # This is regression test for a bug that caused `enable' to fail
614 # for software watchpoints.
615
616 # Watch something not memory to force a software watchpoint.
617 gdb_test {watch $pc} ".*atchpoint \[0-9\]+: .pc"
618
619 gdb_test_no_output "disable \$bpnum" "disable watchpoint `\$pc'"
620 gdb_test_no_output "enable \$bpnum" "reenable watchpoint `\$pc'"
621
622 gdb_test "info watchpoint \$bpnum" \
623 ".*watchpoint\[ \t\]+keep\[ \t\]+y\[ \t\]+.pc.*" \
624 "watchpoint `\$pc' is enabled"
625
626 gdb_test_no_output "delete \$bpnum" "delete watchpoint `\$pc'"
627 }
628
629 proc test_watch_location {} {
630 gdb_breakpoint [gdb_get_line_number "func5 breakpoint here"]
631 gdb_continue_to_breakpoint "func5 breakpoint here"
632
633 gdb_test "watch -location *x" "atchpoint .*: .*" "watch -location .x"
634
635 gdb_test "continue" \
636 "Continuing.*\[Ww\]atchpoint .*: .*New value = 27.*" \
637 "continue with watch -location"
638
639 gdb_test_no_output "delete \$bpnum" "delete watch -location"
640 }
641
642 # Tests watching areas larger than a word.
643
644 proc test_wide_location_1 {} {
645 global no_hw
646
647 # This test watches two words on most 32-bit ABIs, and one word on
648 # most 64-bit ABIs.
649
650 # Platforms where the target can't watch such a large region
651 # should clear hw_expected below.
652 if { $no_hw || [target_info exists gdb,no_hardware_watchpoints] } {
653 set hw_expected 0
654 } else {
655 set hw_expected 1
656 }
657
658 gdb_breakpoint [gdb_get_line_number "func6 breakpoint here"]
659 gdb_continue_to_breakpoint "func6 breakpoint here"
660
661 if { $hw_expected } {
662 gdb_test "watch foo2" "Hardware watchpoint .*: .*" "watch foo2"
663 gdb_test "continue" \
664 "Continuing.*Hardware watchpoint .*: .*New value = \\\{val = \\\{0, 11\\\}\\\}.*" \
665 "continue with watch foo2"
666 } else {
667 gdb_test "watch foo2" "atchpoint .*: .*" "watch foo2"
668 gdb_test "continue" \
669 "Continuing.*\[Ww\]atchpoint .*: .*New value = \\\{val = \\\{0, 11\\\}\\\}.*" \
670 "continue with watch foo2"
671 }
672
673 gdb_test_no_output "delete \$bpnum" "delete watch foo2"
674 }
675
676 proc test_wide_location_2 {} {
677 global no_hw
678
679 # This test watches four words on most 32-bit ABIs, and two words
680 # on 64-bit ABIs.
681
682 # Platforms where the target can't watch such a large region
683 # should clear hw_expected below.
684 if { $no_hw || [target_info exists gdb,no_hardware_watchpoints] } {
685 set hw_expected 0
686 } else {
687 set hw_expected 1
688 }
689
690 gdb_breakpoint [gdb_get_line_number "func7 breakpoint here"]
691 gdb_continue_to_breakpoint "func7 breakpoint here"
692
693 if { $hw_expected } {
694 gdb_test "watch foo4" "Hardware watchpoint .*: .*" "watch foo4"
695 gdb_test "continue" \
696 "Continuing.*Hardware watchpoint .*: .*New value = \\\{val = \\\{0, 0, 0, 33\\\}\\\}.*" \
697 "continue with watch foo4"
698 } else {
699 gdb_test "watch foo4" "atchpoint .*: .*" "watch foo4"
700 gdb_test "continue" \
701 "Continuing.*\[Ww\]atchpoint .*: .*New value = \\\{val = \\\{0, 0, 0, 33\\\}\\\}.*" \
702 "continue with watch foo4"
703 }
704
705 gdb_test_no_output "delete \$bpnum" "delete watch foo4"
706 }
707
708 proc test_inaccessible_watchpoint {} {
709 global gdb_prompt
710
711 maybe_clean_restart
712
713 # This is a test for watchpoints on currently inaccessible (but later
714 # valid) memory.
715
716 if [runto func4] then {
717 # Make sure we only allow memory access errors.
718 set msg "watchpoint refused to insert on nonexistent struct member"
719 gdb_test_multiple "watch struct1.nosuchmember" $msg {
720 -re ".*atchpoint \[0-9\]+: struct1.nosuchmember.*$gdb_prompt $" {
721 # PR breakpoints/9681
722 fail $msg
723 }
724 -re "There is no member named nosuchmember\\..*$gdb_prompt $" {
725 pass $msg
726 }
727 }
728
729 # See whether a watchpoint on a normal variable is a hardware
730 # watchpoint or not. The watchpoints on NULL should be hardware
731 # iff this one is.
732 set watchpoint_msg "Watchpoint"
733 gdb_test_multiple "watch global_ptr" "watch global_ptr" {
734 -re "Watchpoint \[0-9\]+: global_ptr\r\n.*$gdb_prompt $" {
735 pass "watch global_ptr"
736 }
737 -re "Hardware watchpoint \[0-9\]+: global_ptr\r\n.*$gdb_prompt $" {
738 set watchpoint_msg "Hardware watchpoint"
739 pass "watch global_ptr"
740 }
741 }
742 delete_breakpoints
743
744 # Make sure that we can watch a constant address, and correctly
745 # use a HW watchpoint if supported.
746 gdb_test "watch *(int *) 0" \
747 "$watchpoint_msg \[0-9\]+: \\*\\(int \\*\\) 0"
748 delete_breakpoints
749
750 # The same, but using -location through an indirection.
751 gdb_test "watch -location *global_ptr" \
752 "$watchpoint_msg \[0-9\]+: \-location \\*global_ptr"
753 delete_breakpoints
754
755 # This step requires two HW watchpoints. Since some platforms only
756 # have a single one, accept either SW or HW watchpoint in this case.
757 if {[skip_hw_watchpoint_multi_tests]} {
758 set watchpoint_msg "(Watchpoint|Hardware watchpoint)"
759 }
760
761 gdb_test "watch *global_ptr" "$watchpoint_msg \[0-9\]+: \\\*global_ptr"
762 gdb_test "set \$global_ptr_breakpoint_number = \$bpnum" ""
763 gdb_test "next" ".*global_ptr = buf.*" "global_ptr next"
764 gdb_test_multiple "next" "next over ptr init" {
765 -re ".*atchpoint \[0-9\]+: \\*global_ptr\r\n\r\nOld value = .*\r\nNew value = 3 .*\r\n.*$gdb_prompt $" {
766 # We can not test for <unknown> here because NULL may be readable.
767 # This test does rely on *NULL != 3.
768 pass "next over ptr init"
769 }
770 }
771 gdb_test_multiple "next" "next over buffer set" {
772 -re ".*atchpoint \[0-9\]+: \\*global_ptr\r\n\r\nOld value = 3 .*\r\nNew value = 7 .*\r\n.*$gdb_prompt $" {
773 pass "next over buffer set"
774 }
775 }
776 gdb_test "delete \$global_ptr_breakpoint_number" ""
777 gdb_test "watch **global_ptr_ptr" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr"
778 gdb_test "set \$global_ptr_ptr_breakpoint_number = \$bpnum" ""
779 gdb_test "next" ".*global_ptr_ptr = &global_ptr.*" "global_ptr_ptr next"
780 gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = .*\r\nNew value = 7 .*" "next over global_ptr_ptr init"
781 gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = 7 .*\r\nNew value = 9 .*" "next over global_ptr_ptr buffer set"
782 gdb_test "next" ".*atchpoint \[0-9\]+: \\*\\*global_ptr_ptr\[\r\n\]+Old value = 9 .*\r\nNew value = 5 .*" "next over global_ptr_ptr pointer advance"
783 gdb_test_no_output "delete \$global_ptr_ptr_breakpoint_number"
784 }
785 }
786
787 proc test_no_hw_watchpoints {} {
788 global testfile
789
790 clean_restart $testfile
791
792 # Verify that a user can force GDB to use "slow" watchpoints.
793 # (This proves rather little on kernels that don't support
794 # fast watchpoints, but still...)
795 #
796 if ![runto_main] then { fail "watch tests suppressed" }
797
798 gdb_test_no_output "set can-use-hw-watchpoints 0" "disable fast watches"
799
800 gdb_test "show can-use-hw-watchpoints" \
801 "Debugger's willingness to use watchpoint hardware is 0." \
802 "show disable fast watches"
803
804 gdb_test "watch ival3 if count > 1" \
805 "Watchpoint \[0-9\]*: ival3.*" \
806 "set slow conditional watch"
807
808 gdb_test "continue" \
809 "Watchpoint \[0-9\]*: ival3.*Old value = 1.*New value = 2.*" \
810 "trigger slow conditional watch"
811
812 gdb_test_no_output "delete \$bpnum" "delete watch ival3"
813
814 # We've explicitly disabled hardware watches. Verify that GDB
815 # refrains from using them.
816 #
817 gdb_test "rwatch ival3" \
818 "Expression cannot be implemented with read/access watchpoint..*" \
819 "rwatch disallowed when can-set-hw-watchpoints cleared"
820
821 # Re-enable hardware watchpoints if necessary.
822 if ![target_info exists gdb,no_hardware_watchpoints] {
823 gdb_test_no_output "set can-use-hw-watchpoints 1" ""
824 }
825 }
826
827 proc test_watchpoint_in_big_blob {} {
828 global gdb_prompt
829
830 # On native targets where we do hardware resource accounting, this
831 # may end up as a software watchpoint.
832 set ok 0
833 set test "watch buf"
834 gdb_test_multiple "watch buf" $test {
835 -re "Hardware watchpoint \[0-9\]+: buf.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
836 # This may happen with remote targets (where we don't do
837 # resource accounting) that support hardware watchpoints,
838 # when breakpoint always-inserted is on. The watchpoint
839 # was too large, for example. If GDB is ever adjusted to
840 # downgrade the watchpoint automatically in this case,
841 # this match should be removed. Note the breakpoint has
842 # been created, and is in the list, so it needs deleting.
843 pass $test
844 }
845 -re ".*atchpoint \[0-9\]+: buf.*$gdb_prompt $" {
846 pass $test
847 set ok 1
848 }
849 }
850
851 if { $ok } {
852 set test "watchpoint on buf hit"
853 gdb_test_multiple "cont" $test {
854 -re "Continuing.*atchpoint \[0-9\]+: buf\r\n\r\nOld value = .*testte\".*$gdb_prompt $" {
855 pass $test
856 }
857 -re "Could not insert hardware breakpoints:.*You may have requested too many hardware breakpoints/watchpoints.*$gdb_prompt $" {
858 # This may happen with remote targets that support
859 # hardware watchpoints. We only find out the
860 # watchpoint was too large, for example, at insert
861 # time. If GDB is ever adjusted to downgrade the
862 # watchpoint automatically in this case, this match
863 # should be removed.
864 pass $test
865 }
866 }
867 }
868
869 gdb_test_no_output "delete \$bpnum" "delete watch buf"
870 }
871
872 # Start with a fresh gdb.
873
874 set prev_timeout $timeout
875 set timeout 600
876 verbose "Timeout now 600 sec.\n"
877
878 test_no_hw_watchpoints
879
880 proc do_tests {} {
881 global testfile
882 global no_hw
883
884 clean_restart $testfile
885
886 if {$no_hw} {
887 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
888 }
889
890 if [initialize] then {
891
892 test_simple_watchpoint
893
894 test_disabling_watchpoints
895
896 if ![target_info exists gdb,cannot_call_functions] {
897 test_stepping
898 }
899 }
900
901 # Tests below don't rely on the markers and watchpoint set by
902 # `initialize' anymore.
903 clean_restart $testfile
904
905 if {$no_hw} {
906 gdb_test_no_output "set can-use-hw-watchpoints 0" ""
907 }
908
909 # Only enabled for some targets merely because it has not been tested
910 # elsewhere.
911 # On sparc-sun-sunos4.1.3, GDB was running all the way to the marker4
912 # breakpoint before stopping for the watchpoint. I don't know why.
913 if {[istarget "hppa*-*-*"]} then {
914 test_watchpoint_triggered_in_syscall
915 }
916
917 test_complex_watchpoint
918
919 test_inaccessible_watchpoint
920
921 test_watchpoint_and_breakpoint
922
923 test_watchpoint_in_big_blob
924
925 test_constant_watchpoint
926
927 test_disable_enable_software_watchpoint
928
929 test_watch_location
930
931 test_wide_location_1
932 test_wide_location_2
933 }
934
935 # On targets that can do hardware watchpoints, run the tests twice:
936 # once with hardware watchpoints enabled; another with hardware
937 # watchpoints force-disabled.
938
939 do_tests
940 if ![target_info exists gdb,no_hardware_watchpoints] {
941 set save_pf_prefix $pf_prefix
942 lappend pf_prefix "no-hw:"
943
944 set no_hw 1
945 do_tests
946 set pf_prefix $save_pf_prefix
947 }
948
949 # Restore old timeout
950 set timeout $prev_timeout
951 verbose "Timeout now $timeout sec.\n"