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 # This file is part of the gdb testsuite
18 # Test relies on checking gdb debug output. Do not run if gdb debug is
19 # enabled as any debug will be redirected to the log.
20 require !gdb_debug_enabled
24 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
32 # Delete all target-supplied memory regions.
37 # Probe for hardware stepping.
39 proc probe_target_hardware_step {} {
44 gdb_test_no_output "set debug target 1"
45 set test "probe target hardware step"
46 gdb_test_multiple "pipe si | grep resume" $test {
47 -re "resume \\(\[^\r\n\]+, step, .*$gdb_prompt $" {
55 gdb_test "set debug target 0" "->log_command.*\\).*"
59 # Get the bounds of a function, and write them to FUNC_LO (inclusive),
60 # FUNC_HI (exclusive). Return true on success and false on failure.
61 proc get_function_bounds {function func_lo func_hi} {
71 set test "get lo address of $function"
72 gdb_test_multiple "disassemble $function" $test {
73 -re "($hex) .*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
74 set lo $expect_out(1,string)
75 set size $expect_out(2,string)
80 if { $lo == "" || $size == "" } {
84 # Account for the size of the last instruction.
85 set test "get hi address of $function"
86 gdb_test_multiple "x/2i $function+$size" $test {
87 -re ".*$hex <$function\\+$size>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
88 set hi $expect_out(1,string)
97 # Remove unnecessary leading 0's (0x00000ADDR => 0xADDR) so we can
98 # easily do matches. Disassemble includes leading zeros, while
100 regsub -all "0x0\+" $lo "0x" lo
101 regsub -all "0x0\+" $hi "0x" hi
106 # Get the address where the thread is currently stopped.
107 proc get_curr_insn {} {
112 set test "get current insn"
113 gdb_test_multiple "p /x \$pc" $test {
114 -re " = ($hex)\r\n$gdb_prompt $" {
115 set pc $expect_out(1,string)
123 # Get the address of where a single-step should land.
124 proc get_next_insn {} {
129 set test "get next insn"
130 gdb_test_multiple "x/2i \$pc" $test {
131 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
132 set next $expect_out(1,string)
140 set hw_step [probe_target_hardware_step]
142 if ![get_function_bounds "main" main_lo main_hi] {
143 # Can't do the following tests if main's bounds are unknown.
147 # Manually create a read-only memory region that covers 'main'.
148 gdb_test_no_output "mem $main_lo $main_hi ro" \
149 "create read-only mem region covering main"
151 # So that we don't fail inserting breakpoints on addresses outside
152 # main, like the internal event breakpoints.
153 gdb_test_no_output "set mem inaccessible-by-default off"
155 # So we get an immediate warning/error without needing to resume the
157 gdb_test_no_output "set breakpoint always-inserted on"
159 # Disable the automatic fallback to HW breakpoints. We want a
160 # software breakpoint to be attempted, and to fail.
161 gdb_test_no_output "set breakpoint auto-hw off"
163 # Confirm manual writes to the read-only memory region fail.
164 gdb_test "p /x *(char *) $main_lo = 1" \
165 "Cannot access memory at address $main_lo" \
166 "writing to read-only memory fails"
168 # Ensure that inserting a software breakpoint in a known-read-only
170 gdb_test "break *$main_lo" \
171 "Cannot insert breakpoint .*Cannot set software breakpoint at read-only address $main_lo.*" \
172 "inserting software breakpoint in read-only memory fails"
176 set supports_hbreak 0
177 set test "probe hbreak support"
178 gdb_test_multiple "hbreak *$main_lo" $test {
179 -re "You may have requested too many.*$gdb_prompt $" {
180 pass "$test (no support)"
182 -re "No hardware breakpoint support.*$gdb_prompt $" {
183 pass "$test (no support)"
185 -re "$gdb_prompt $" {
186 pass "$test (support)"
187 set supports_hbreak 1
193 # Check that the "auto-hw on/off" setting affects single-step
194 # breakpoints as expected, by stepping through the read-only region.
195 # If the target does hardware stepping, we won't exercise that aspect,
196 # but we should be able to step through the region without seeing the
197 # hardware breakpoint or read-only address errors.
198 proc test_single_step { always_inserted auto_hw } {
202 global supports_hbreak
205 gdb_test_no_output "set breakpoint always-inserted $always_inserted"
206 gdb_test_no_output "set breakpoint auto-hw $auto_hw"
208 # Get the address of the current instruction so we know where SI is
210 set curr_insn [get_curr_insn]
212 # Get the address of the next instruction so we know where SI should
214 set next_insn [get_next_insn]
216 set test "step in ro region"
217 gdb_test_multiple "si" $test {
218 -re "Could not insert hardware breakpoints.*$gdb_prompt $" {
219 gdb_assert {!$hw_step && $auto_hw == "on" && !$supports_hbreak} \
220 "$test (cannot insert hw break)"
222 -re "Cannot set software breakpoint at read-only address $next_insn.*$gdb_prompt $" {
223 gdb_assert {!$hw_step && $auto_hw == "off"} \
224 "$test (cannot insert sw break)"
226 -re "^si\r\nNote: automatically using hardware breakpoints for read-only addresses\.\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
227 gdb_assert {!$hw_step && $auto_hw == "on" && $supports_hbreak} \
230 -re "^si\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
231 gdb_assert {$hw_step || ($auto_hw == "on" && $supports_hbreak)} \
236 gdb_test "maint info breakpoints 0" \
237 "No breakpoint, watchpoint, tracepoint, or catchpoint matching '0'\." \
238 "single-step breakpoint is not left behind"
240 # Confirm the thread really advanced.
241 if {$hw_step || ($auto_hw == "on" && $supports_hbreak)} {
242 gdb_test "p /x \$pc" " = $next_insn" "thread advanced"
244 gdb_test "p /x \$pc" " = $curr_insn" "thread did not advance"
248 foreach always_inserted {"off" "on"} {
249 foreach auto_hw {"off" "on"} {
250 with_test_prefix "always-inserted $always_inserted: auto-hw $auto_hw" {
251 test_single_step $always_inserted $auto_hw