]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/watchpoint-reuse-slot.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / watchpoint-reuse-slot.exp
CommitLineData
213516ef 1# Copyright 2014-2023 Free Software Foundation, Inc.
8e9db26e
PA
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# Test alternating between watchpoint types, watching a sliding window
17# of addresses (thus alternating between aligned and unaligned
18# addresses). Only a single watchpoint exists at any given time. On
19# targets that only update the debug registers on resume, this
20# stresses the debug register setup code, both in GDB and in the
21# target/kernel as one watchpoint replaces the other in a single
22# operation. (Note that we don't have any of these watchpoints
23# trigger.)
24
a47a2d45
CL
25# The skip_hw_watchpoint_tests checks if watchpoints are supported by the
26# processor. On PowerPC, the check runs a small test program under gdb
27# to determine if the Power processor supports HW watchpoints. The check
28# must be done before starting the test so as to not disrupt the execution
29# of the actual test.
30
31set skip_hw_watchpoint_tests_p [skip_hw_watchpoint_tests]
32
33# starting the test.
34
8e9db26e
PA
35standard_testfile
36
37if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
38 return -1
39}
40
65a33d75 41if {![runto_main]} {
8e9db26e
PA
42 return 0
43}
44
45# The line we'll be stepping.
46set srcline [gdb_get_line_number "stepi line"]
47
48# The address the program is stopped at currently.
49set cur_addr ""
50
51# Get the current PC.
52
53proc get_pc {} {
54 global hex gdb_prompt
55
56 set addr ""
57 set test "get PC"
58 gdb_test_multiple "p /x \$pc" "$test" {
59 -re " = ($hex).*$gdb_prompt $" {
60 set addr $expect_out(1,string)
61 pass "$test"
62 }
63 }
64
65 return $addr
66}
67
68
69# Issue a stepi, and make sure the program advanced past the current
70# instruction (stored in the CUR_ADDR global).
71
72proc stepi {} {
73 global hex gdb_prompt cur_addr
74
75 set srcline " for (i = 0; i < 100000; i++); /* stepi line */"
76 set test "stepi advanced"
77 gdb_test_multiple "stepi" $test {
0d8683a3
TV
78 -re -wrap "[string_to_regexp $srcline]" {
79 set addr [get_valueof "/x" "\$pc" "0"]
8e9db26e
PA
80 if {$addr != $cur_addr} {
81 pass $test
82 } else {
83 fail $test
84 }
b8983c46 85 set cur_addr $addr
8e9db26e
PA
86 }
87 }
88}
89
90gdb_breakpoint $srcline
91gdb_continue_to_breakpoint "stepi line"
3541979f 92set cur_addr [get_pc]
8e9db26e
PA
93
94# The test tries various sequences of different types of watchpoints.
95# Probe for support first.
3541979f
AB
96proc build_cmds_list {} {
97 global gdb_prompt
98
99 # So we get an immediate warning/error if the target doesn't support a
100 # given watchpoint type.
101 gdb_test_no_output "set breakpoint always-inserted on" \
102 "Set breakpoints always inserted while building cmds list"
103
104 # The list of supported commands. Below we'll probe for support and
105 # add elements to this list.
106 set cmds {}
107
108 foreach cmd {"watch" "awatch" "rwatch"} {
109 set test $cmd
110 gdb_test_multiple "$cmd buf.byte\[0\]" $test {
111 -re "You may have requested too many.*$gdb_prompt $" {
112 unsupported $test
113 }
114 -re "Target does not support.*$gdb_prompt $" {
115 unsupported $test
116 }
117 -re "Can't set read/access watchpoint when hardware watchpoints are disabled.*$gdb_prompt $" {
118 unsupported $test
119 }
120 -re "$gdb_prompt $" {
121 pass $test
122 lappend cmds $cmd
123 }
124 }
8e9db26e 125
3541979f
AB
126 delete_breakpoints
127 }
8e9db26e 128
3541979f 129 set test "hbreak"
e777225b 130 gdb_test_multiple "hbreak -q main" $test {
8e9db26e
PA
131 -re "You may have requested too many.*$gdb_prompt $" {
132 unsupported $test
133 }
3541979f 134 -re "No hardware breakpoint support.*$gdb_prompt $" {
aebf9d24
AA
135 unsupported $test
136 }
8e9db26e
PA
137 -re "$gdb_prompt $" {
138 pass $test
3541979f 139 lappend cmds "hbreak"
8e9db26e
PA
140 }
141 }
142
3541979f 143 delete_breakpoints
8e9db26e 144
3541979f 145 return $cmds
8e9db26e
PA
146}
147
ca5fd19b
YQ
148# Return true if the memory range [buf.byte + OFFSET, +WIDTH] can be
149# monitored by CMD, otherwise return false.
150
151proc valid_addr_p {cmd offset width} {
152
153 if { [istarget "aarch64*-*-linux*"] } {
154 # The aarch64 Linux kernel port only accepts 4-byte aligned addresses
155 # for hardware breakpoints and 8-byte aligned addresses for hardware
156 # watchpoints. However, both GDB and GDBserver support unaligned
157 # watchpoints by using more than one properly aligned watchpoint
158 # registers to represent the whole unaligned region. Breakpoint
159 # addresses must still be aligned though.
160 if {$cmd == "hbreak" } {
161 if { [expr ($offset) % 4] != 0 } {
162 return 0
163 }
164 }
de3db44c
YQ
165 } elseif { [istarget "arm*-*-linux*"] } {
166 if { $cmd == "hbreak" } {
167 # Breakpoints must be of length 2 (thumb) or 4 (ARM) bytes.
168 if { $width != 2 && $width != 4 } {
169 return 0
170 }
171 } else {
172 # Watchpoints can be of length 1, 2, 4 or 8 bytes.
173 if { [expr $width % 2] != 0 } {
174 return 0
175 }
176 }
177
178 if { [expr ($offset) % 8] == 0 && $width == 8 } {
179 # If WIDTH is 8 byte, the address should be 8-byte aligned.
180 return 1
181 } elseif { [expr ($offset) % 4] == 0 } {
182 return 1
183 } elseif { [expr ($offset) % 4] == 2 && $width == 2 } {
184 # Halfword watchpoints and breakpoints.
185 return 1
186 } elseif { [expr ($offset) % 4] == 1 && $width == 1 && $cmd != "hbreak" } {
187 # Single byte watchpoints.
188 return 1
189 } else {
190 return 0
191 }
ca5fd19b
YQ
192 }
193
194 return 1
195}
196
8e9db26e
PA
197# Watch WIDTH bytes at BASE + OFFSET. CMD specifices the specific
198# type of watchpoint to use. If CMD is "hbreak", WIDTH is ignored.
3541979f
AB
199# The HW_WP_P flag tells us if hardware watchpoints are enabled or
200# not.
8e9db26e 201
3541979f 202proc watch_command {cmd base offset width hw_wp_p} {
8e9db26e
PA
203 global srcfile srcline hex
204
205 if {$cmd == "hbreak"} {
206 set expr "*(buf.byte + $base + $offset)"
207 gdb_test "hbreak $expr" "Hardware assisted breakpoint \[0-9\]+ at $hex"
208 } elseif {$cmd == "watch"} {
209 set expr "*(buf.byte + $base + $offset)@$width"
3541979f
AB
210
211 if { ! $hw_wp_p } {
212 set wp_prefix "Watchpoint"
213 } else {
214 set wp_prefix "Hardware watchpoint"
215 }
216
8e9db26e 217 gdb_test "$cmd $expr" \
3541979f 218 "${wp_prefix} \[0-9\]+: [string_to_regexp $expr]"
8e9db26e
PA
219 } elseif {$cmd == "awatch"} {
220 set expr "*(buf.byte + $base + $offset)@$width"
221 gdb_test "$cmd $expr" \
222 "Hardware access \\(read/write\\) watchpoint \[0-9\]+: [string_to_regexp $expr]"
223 } elseif {$cmd == "rwatch"} {
224 set expr "*(buf.byte + $base + $offset)@$width"
225 gdb_test "$cmd $expr" \
226 "Hardware read watchpoint \[0-9\]+: [string_to_regexp $expr]"
227 }
228}
229
3541979f
AB
230# Run the watchpoint tests (see the description at the top for details), the
231# HW_WP_P flag tells us if hardware watchpoints are enabled or not.
232proc run_watchpoints_tests {hw_wp_p} {
8e9db26e 233
3541979f 234 set cmds [build_cmds_list]
8e9db26e 235
3541979f
AB
236 foreach always_inserted {"off" "on" } {
237 gdb_test_no_output "set breakpoint always-inserted $always_inserted"
238 foreach cmd1 $cmds {
239 foreach cmd2 $cmds {
240 for {set width 1} {$width < 4} {incr width} {
8e9db26e 241
3541979f
AB
242 if {$cmd1 == "hbreak" && $cmd2 == "hbreak" \
243 && $width > 1} {
244 # hbreak ignores WIDTH, no use testing more than
245 # once.
ca5fd19b
YQ
246 continue
247 }
248
3541979f
AB
249 for {set x 0} {$x < 4} {incr x} {
250
251 if { ![valid_addr_p $cmd1 $x $width]
252 || ![valid_addr_p $cmd2 $x+1 $width] } {
253 # Skip tests if requested address or length
254 # of breakpoint or watchpoint don't meet
255 # target or kernel requirements.
256 continue
8e9db26e 257 }
3541979f
AB
258
259 set prefix "always-inserted $always_inserted: "
260 append prefix "$cmd1 x $cmd2: "
261 with_test_prefix "$prefix: width $width, iter $x" {
262 with_test_prefix "base + 0" {
263 watch_command $cmd1 $x 0 $width $hw_wp_p
264 stepi
265 gdb_test_no_output "delete \$bpnum"
266 }
267 with_test_prefix "base + 1" {
268 watch_command $cmd2 $x 1 $width $hw_wp_p
269 stepi
270 gdb_test_no_output "delete \$bpnum"
271 }
8e9db26e
PA
272 }
273 }
274 }
275 }
276 }
277 }
278}
3541979f
AB
279
280# Based on HW_WP_P set whether hardware watchpoints can be used or
281# not, then call RUN_WATCHPOINTS_TESTS.
282proc setup_and_run_watchpoints_tests { hw_wp_p } {
283 if {$hw_wp_p} {
284 set prefix "hw-watch"
285 } else {
286 set prefix "sw-watch"
287 }
288
289 with_test_prefix $prefix {
290 gdb_test_no_output "set can-use-hw-watchpoints ${hw_wp_p}"
291
292 run_watchpoints_tests $hw_wp_p
293 }
294}
295
296# Run tests with hardware watchpoints disabled, then again with them
297# enabled (if this target supports hardware watchpoints).
a47a2d45 298if { !$skip_hw_watchpoint_tests_p } {
3541979f
AB
299 # Run test with H/W enabled.
300 setup_and_run_watchpoints_tests 1
301}
302
303# Run test with H/W disabled
304setup_and_run_watchpoints_tests 0