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