1 # Copyright 2014-2024 Free Software Foundation, Inc.
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.
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.
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/>.
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
25 # The allow_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
31 set allow_hw_watchpoint_tests_p [allow_hw_watchpoint_tests]
37 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
45 # The line we'll be stepping.
46 set srcline [gdb_get_line_number "stepi line"]
48 # The address the program is stopped at currently.
58 gdb_test_multiple "p /x \$pc" "$test" {
59 -re " = ($hex).*$gdb_prompt $" {
60 set addr $expect_out(1,string)
69 # Issue a stepi, and make sure the program advanced past the current
70 # instruction (stored in the CUR_ADDR global).
73 global hex gdb_prompt cur_addr
75 set srcline " for (i = 0; i < 100000; i++); /* stepi line */"
76 set test "stepi advanced"
77 gdb_test_multiple "stepi" $test {
78 -re -wrap "[string_to_regexp $srcline]" {
79 set addr [get_valueof "/x" "\$pc" "0"]
80 if {$addr != $cur_addr} {
90 gdb_breakpoint $srcline
91 gdb_continue_to_breakpoint "stepi line"
94 # The test tries various sequences of different types of watchpoints.
95 # Probe for support first.
96 proc build_cmds_list {} {
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"
104 # The list of supported commands. Below we'll probe for support and
105 # add elements to this list.
108 foreach cmd {"watch" "awatch" "rwatch"} {
110 gdb_test_multiple "$cmd buf.byte\[0\]" $test {
111 -re "You may have requested too many.*$gdb_prompt $" {
114 -re "Target does not support.*$gdb_prompt $" {
117 -re "Can't set read/access watchpoint when hardware watchpoints are disabled.*$gdb_prompt $" {
120 -re "$gdb_prompt $" {
130 gdb_test_multiple "hbreak -q main" $test {
131 -re "You may have requested too many.*$gdb_prompt $" {
134 -re "No hardware breakpoint support.*$gdb_prompt $" {
137 -re "$gdb_prompt $" {
139 lappend cmds "hbreak"
148 # Return true if the memory range [buf.byte + OFFSET, +WIDTH] can be
149 # monitored by CMD, otherwise return false.
151 proc valid_addr_p {cmd offset width} {
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 } {
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 } {
172 # Watchpoints can be of length 1, 2, 4 or 8 bytes.
173 if { [expr $width % 2] != 0 } {
178 if { [expr ($offset) % 8] == 0 && $width == 8 } {
179 # If WIDTH is 8 byte, the address should be 8-byte aligned.
181 } elseif { [expr ($offset) % 4] == 0 } {
183 } elseif { [expr ($offset) % 4] == 2 && $width == 2 } {
184 # Halfword watchpoints and breakpoints.
186 } elseif { [expr ($offset) % 4] == 1 && $width == 1 && $cmd != "hbreak" } {
187 # Single byte watchpoints.
197 # Watch WIDTH bytes at BASE + OFFSET. CMD specifices the specific
198 # type of watchpoint to use. If CMD is "hbreak", WIDTH is ignored.
199 # The HW_WP_P flag tells us if hardware watchpoints are enabled or
202 proc watch_command {cmd base offset width hw_wp_p} {
203 global srcfile srcline hex
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"
212 set wp_prefix "Watchpoint"
214 set wp_prefix "Hardware watchpoint"
217 gdb_test "$cmd $expr" \
218 "${wp_prefix} \[0-9\]+: [string_to_regexp $expr]"
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]"
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.
232 proc run_watchpoints_tests {hw_wp_p} {
234 set cmds [build_cmds_list]
236 foreach always_inserted {"off" "on" } {
237 gdb_test_no_output "set breakpoint always-inserted $always_inserted"
240 for {set width 1} {$width < 4} {incr width} {
242 if {$cmd1 == "hbreak" && $cmd2 == "hbreak" \
244 # hbreak ignores WIDTH, no use testing more than
249 for {set x 0} {$x < 4} {incr x} {
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.
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
265 gdb_test_no_output "delete \$bpnum"
267 with_test_prefix "base + 1" {
268 watch_command $cmd2 $x 1 $width $hw_wp_p
270 gdb_test_no_output "delete \$bpnum"
280 # Based on HW_WP_P set whether hardware watchpoints can be used or
281 # not, then call RUN_WATCHPOINTS_TESTS.
282 proc setup_and_run_watchpoints_tests { hw_wp_p } {
284 set prefix "hw-watch"
286 set prefix "sw-watch"
289 with_test_prefix $prefix {
290 gdb_test_no_output "set can-use-hw-watchpoints ${hw_wp_p}"
292 run_watchpoints_tests $hw_wp_p
296 # Run tests with hardware watchpoints disabled, then again with them
297 # enabled (if this target supports hardware watchpoints).
298 if { $allow_hw_watchpoint_tests_p } {
299 # Run test with H/W enabled.
300 setup_and_run_watchpoints_tests 1
303 # Run test with H/W disabled
304 setup_and_run_watchpoints_tests 0