1 # Copyright 2014-2020 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 if [gdb_debug_enabled] {
21 untested "debug is enabled"
27 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
35 # Delete all target-supplied memory regions.
40 # Probe for hardware stepping.
42 proc probe_target_hardware_step {} {
47 gdb_test_no_output "set debug target 1"
48 set test "probe target hardware step"
49 gdb_test_multiple "si" $test {
50 -re "resume \\(\[^\r\n\]+, step, .*$gdb_prompt $" {
58 gdb_test "set debug target 0" "->log_command.*\\).*"
62 # Get the bounds of a function, and write them to FUNC_LO (inclusive),
63 # FUNC_HI (exclusive). Return true on success and false on failure.
64 proc get_function_bounds {function func_lo func_hi} {
74 set test "get lo address of $function"
75 gdb_test_multiple "disassemble $function" $test {
76 -re "($hex) .*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
77 set lo $expect_out(1,string)
78 set size $expect_out(2,string)
83 if { $lo == "" || $size == "" } {
87 # Account for the size of the last instruction.
88 set test "get hi address of $function"
89 gdb_test_multiple "x/2i $function+$size" $test {
90 -re ".*$hex <$function\\+$size>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
91 set hi $expect_out(1,string)
100 # Remove unnecessary leading 0's (0x00000ADDR => 0xADDR) so we can
101 # easily do matches. Disassemble includes leading zeros, while
103 regsub -all "0x0\+" $lo "0x" lo
104 regsub -all "0x0\+" $hi "0x" hi
109 # Get the address where the thread is currently stopped.
110 proc get_curr_insn {} {
115 set test "get current insn"
116 gdb_test_multiple "p /x \$pc" $test {
117 -re " = ($hex)\r\n$gdb_prompt $" {
118 set pc $expect_out(1,string)
126 # Get the address of where a single-step should land.
127 proc get_next_insn {} {
132 set test "get next insn"
133 gdb_test_multiple "x/2i \$pc" $test {
134 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
135 set next $expect_out(1,string)
143 set hw_step [probe_target_hardware_step]
145 if ![get_function_bounds "main" main_lo main_hi] {
146 # Can't do the following tests if main's bounds are unknown.
150 # Manually create a read-only memory region that covers 'main'.
151 gdb_test_no_output "mem $main_lo $main_hi ro" \
152 "create read-only mem region covering main"
154 # So that we don't fail inserting breakpoints on addresses outside
155 # main, like the internal event breakpoints.
156 gdb_test_no_output "set mem inaccessible-by-default off"
158 # So we get an immediate warning/error without needing to resume the
160 gdb_test_no_output "set breakpoint always-inserted on"
162 # Disable the automatic fallback to HW breakpoints. We want a
163 # software breakpoint to be attempted, and to fail.
164 gdb_test_no_output "set breakpoint auto-hw off"
166 # Confirm manual writes to the read-only memory region fail.
167 gdb_test "p /x *(char *) $main_lo = 1" \
168 "Cannot access memory at address $main_lo" \
169 "writing to read-only memory fails"
171 # Ensure that inserting a software breakpoint in a known-read-only
173 gdb_test "break *$main_lo" \
174 "Cannot insert breakpoint .*Cannot set software breakpoint at read-only address $main_lo.*" \
175 "inserting software breakpoint in read-only memory fails"
179 set supports_hbreak 0
180 set test "probe hbreak support"
181 gdb_test_multiple "hbreak *$main_lo" $test {
182 -re "You may have requested too many.*$gdb_prompt $" {
183 pass "$test (no support)"
185 -re "No hardware breakpoint support.*$gdb_prompt $" {
186 pass "$test (no support)"
188 -re "$gdb_prompt $" {
189 pass "$test (support)"
190 set supports_hbreak 1
196 # Check that the "auto-hw on/off" setting affects single-step
197 # breakpoints as expected, by stepping through the read-only region.
198 # If the target does hardware stepping, we won't exercise that aspect,
199 # but we should be able to step through the region without seeing the
200 # hardware breakpoint or read-only address errors.
201 proc test_single_step { always_inserted auto_hw } {
205 global supports_hbreak
208 gdb_test_no_output "set breakpoint always-inserted $always_inserted"
209 gdb_test_no_output "set breakpoint auto-hw $auto_hw"
211 # Get the address of the current instruction so we know where SI is
213 set curr_insn [get_curr_insn]
215 # Get the address of the next instruction so we know where SI should
217 set next_insn [get_next_insn]
219 set test "step in ro region"
220 gdb_test_multiple "si" $test {
221 -re "Could not insert hardware breakpoints.*$gdb_prompt $" {
222 gdb_assert {!$hw_step && $auto_hw == "on" && !$supports_hbreak} \
223 "$test (cannot insert hw break)"
225 -re "Cannot set software breakpoint at read-only address $next_insn.*$gdb_prompt $" {
226 gdb_assert {!$hw_step && $auto_hw == "off"} \
227 "$test (cannot insert sw break)"
229 -re "^si\r\nNote: automatically using hardware breakpoints for read-only addresses\.\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
230 gdb_assert {!$hw_step && $auto_hw == "on" && $supports_hbreak} \
233 -re "^si\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
234 gdb_assert {$hw_step || ($auto_hw == "on" && $supports_hbreak)} \
239 gdb_test "maint info breakpoints 0" \
240 "No breakpoint or watchpoint matching '0'\." \
241 "single-step breakpoint is not left behind"
243 # Confirm the thread really advanced.
244 if {$hw_step || ($auto_hw == "on" && $supports_hbreak)} {
245 gdb_test "p /x \$pc" " = $next_insn" "thread advanced"
247 gdb_test "p /x \$pc" " = $curr_insn" "thread did not advance"
251 foreach always_inserted {"off" "on"} {
252 foreach auto_hw {"off" "on"} {
253 with_test_prefix "always-inserted $always_inserted: auto-hw $auto_hw" {
254 test_single_step $always_inserted $auto_hw