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