update copyright year range in GDB files
[external/binutils.git] / gdb / testsuite / gdb.base / breakpoint-in-ro-region.exp
1 # Copyright 2014-2017 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 # This file is part of the gdb testsuite
17
18 standard_testfile
19
20 if { [prepare_for_testing "failed to prepare" $testfile $srcfile] } {
21     return -1
22 }
23
24 if ![runto main] {
25     return -1
26 }
27
28 delete_breakpoints
29
30 # Probe for hardware stepping.
31
32 proc probe_target_hardware_step {} {
33     global gdb_prompt
34
35     set hw_step 0
36
37     gdb_test_no_output "set debug target 1"
38     set test "probe target hardware step"
39     gdb_test_multiple "si" $test {
40         -re "to_resume \\(\[^\r\n\]+, step, .*$gdb_prompt $" {
41             set hw_step 1
42             pass $test
43         }
44         -re "$gdb_prompt $" {
45             pass $test
46         }
47     }
48     gdb_test "set debug target 0" "->to_log_command.*\\).*"
49     return $hw_step
50 }
51
52 # Get the bounds of a function, and write them to FUNC_LO (inclusive),
53 # FUNC_HI (exclusive).  Return true on success and false on failure.
54 proc get_function_bounds {function func_lo func_hi} {
55     global gdb_prompt
56     global hex decimal
57
58     upvar $func_lo lo
59     upvar $func_hi hi
60
61     set lo ""
62     set size ""
63
64     set test "get lo address of $function"
65     gdb_test_multiple "disassemble $function" $test {
66         -re "($hex) .*$hex <\\+($decimal)>:\[^\r\n\]+\r\nEnd of assembler dump\.\r\n$gdb_prompt $" {
67             set lo $expect_out(1,string)
68             set size $expect_out(2,string)
69             pass $test
70         }
71     }
72
73     if { $lo == "" || $size == "" } {
74         return false
75     }
76
77     # Account for the size of the last instruction.
78     set test "get hi address of $function"
79     gdb_test_multiple "x/2i $function+$size" $test {
80         -re ".*$hex <$function\\+$size>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
81             set hi $expect_out(1,string)
82             pass $test
83         }
84     }
85
86     if { $hi == "" } {
87         return false
88     }
89
90     # Remove unnecessary leading 0's (0x00000ADDR => 0xADDR) so we can
91     # easily do matches.  Disassemble includes leading zeros, while
92     # x/i doesn't.
93     regsub -all "0x0\+" $lo "0x" lo
94     regsub -all "0x0\+" $hi "0x" hi
95
96     return true
97 }
98
99 # Get the address where the thread is currently stopped.
100 proc get_curr_insn {} {
101     global gdb_prompt
102     global hex
103
104     set pc ""
105     set test "get current insn"
106     gdb_test_multiple "p /x \$pc" $test {
107         -re " = ($hex)\r\n$gdb_prompt $" {
108             set pc $expect_out(1,string)
109             pass $test
110         }
111     }
112
113     return $pc
114 }
115
116 # Get the address of where a single-step should land.
117 proc get_next_insn {} {
118     global gdb_prompt
119     global hex
120
121     set next ""
122     set test "get next insn"
123     gdb_test_multiple "x/2i \$pc" $test {
124         -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
125             set next $expect_out(1,string)
126             pass $test
127         }
128     }
129
130     return $next
131 }
132
133 set hw_step [probe_target_hardware_step]
134
135 if ![get_function_bounds "main" main_lo main_hi] {
136     # Can't do the following tests if main's bounds are unknown.
137     return -1
138 }
139
140 # Manually create a read-only memory region that covers 'main'.
141 gdb_test_no_output "mem $main_lo $main_hi ro" \
142     "create read-only mem region covering main"
143
144 # So that we don't fail inserting breakpoints on addresses outside
145 # main, like the internal event breakpoints.
146 gdb_test_no_output "set mem inaccessible-by-default off"
147
148 # So we get an immediate warning/error without needing to resume the
149 # target.
150 gdb_test_no_output "set breakpoint always-inserted on"
151
152 # Disable the automatic fallback to HW breakpoints.  We want a
153 # software breakpoint to be attempted, and to fail.
154 gdb_test_no_output "set breakpoint auto-hw off"
155
156 # Confirm manual writes to the read-only memory region fail.
157 gdb_test "p /x *(char *) $main_lo = 1" \
158     "Cannot access memory at address $main_lo" \
159     "writing to read-only memory fails"
160
161 # Ensure that inserting a software breakpoint in a known-read-only
162 # region fails.
163 gdb_test "break *$main_lo" \
164     "Cannot insert breakpoint .*Cannot set software breakpoint at read-only address $main_lo.*" \
165     "inserting software breakpoint in read-only memory fails"
166
167 delete_breakpoints
168
169 set supports_hbreak 0
170 set test "probe hbreak support"
171 gdb_test_multiple "hbreak *$main_lo" $test {
172     -re "You may have requested too many.*$gdb_prompt $" {
173         pass "$test (no support)"
174     }
175     -re "No hardware breakpoint support.*$gdb_prompt $" {
176         pass "$test (no support)"
177     }
178     -re "$gdb_prompt $" {
179         pass "$test (support)"
180         set supports_hbreak 1
181     }
182 }
183
184 delete_breakpoints
185
186 # Check that the "auto-hw on/off" setting affects single-step
187 # breakpoints as expected, by stepping through the read-only region.
188 # If the target does hardware stepping, we won't exercise that aspect,
189 # but we should be able to step through the region without seeing the
190 # hardware breakpoint or read-only address errors.
191 proc test_single_step { always_inserted auto_hw } {
192     global gdb_prompt
193     global decimal
194     global hex
195     global supports_hbreak
196     global hw_step
197
198     gdb_test_no_output "set breakpoint always-inserted $always_inserted"
199     gdb_test_no_output "set breakpoint auto-hw $auto_hw"
200
201     # Get the address of the current instruction so we know where SI is
202     # starting from.
203     set curr_insn [get_curr_insn]
204
205     # Get the address of the next instruction so we know where SI should
206     # land.
207     set next_insn [get_next_insn]
208
209     set test "step in ro region"
210     gdb_test_multiple "si" $test {
211         -re "Could not insert hardware breakpoints.*$gdb_prompt $" {
212             gdb_assert {!$hw_step && $auto_hw == "on" && !$supports_hbreak} \
213                 "$test (cannot insert hw break)"
214         }
215         -re "Cannot set software breakpoint at read-only address $next_insn.*$gdb_prompt $" {
216             gdb_assert {!$hw_step && $auto_hw == "off"} \
217                 "$test (cannot insert sw break)"
218         }
219         -re "^si\r\nNote: automatically using hardware breakpoints for read-only addresses\.\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
220             gdb_assert {!$hw_step && $auto_hw == "on" && $supports_hbreak} \
221                 "$test (auto-hw)"
222         }
223         -re "^si\r\n\(\?\:${hex}\[ \t\]\)\?${decimal}\[ \t\]+i = 0;\r\n$gdb_prompt $" {
224             gdb_assert {$hw_step || ($auto_hw == "on" && $supports_hbreak)} \
225                 "$test (no error)"
226         }
227     }
228
229     gdb_test "maint info breakpoints 0" \
230         "No breakpoint or watchpoint matching '0'\." \
231         "single-step breakpoint is not left behind"
232
233     # Confirm the thread really advanced.
234     if {$hw_step || ($auto_hw == "on" && $supports_hbreak)} {
235         gdb_test "p /x \$pc" " = $next_insn" "thread advanced"
236     } else {
237         gdb_test "p /x \$pc" " = $curr_insn" "thread did not advance"
238     }
239 }
240
241 foreach always_inserted {"off" "on"} {
242     foreach auto_hw {"off" "on"} {
243         with_test_prefix "always-inserted $always_inserted: auto-hw $auto_hw" {
244             test_single_step $always_inserted $auto_hw
245         }
246     }
247 }