1 # Copyright (C) 2014-2015 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 stepping over permanent breakpoints.
23 if { ![target_info exists gdb,nosignals] } {
24 lappend options "additional_flags=-DSIGNALS"
27 if {[build_executable "failed to prepare" $testfile $srcfile $options]} {
31 set line_bp [gdb_get_line_number "write permanent bp"]
33 # The test proper. ALWAYS_INSERTED indicates whether testing in
34 # "breakpoint always-inserted" mode. If SW_WATCHPOINT is true, set a
35 # software watchpoint, which forces constantly single-stepping, and
36 # exercises stepping the permanent breakpoint while delivering a
37 # signal at the same time.
39 proc test {always_inserted sw_watchpoint} {
43 global srcfile binfile
45 clean_restart $binfile
47 if ![runto_main] then {
51 gdb_test "set breakpoint always-inserted $always_inserted"
54 # Watching a convenience variable forces a software
56 gdb_test "watch \$dummy_convenience" "Watchpoint .*"
60 set address_after_bp ""
62 with_test_prefix "setup" {
64 # Set a breakpoint where we'll manually plant a permanent
66 set test "set probe breakpoint"
67 gdb_test_multiple "break $line_bp" $test {
68 -re "Breakpoint .* at ($hex).*$gdb_prompt $" {
69 set address_bp $expect_out(1,string)
73 if {$address_bp == ""} {
77 # Get the size of the instruction where the breakpoint will
79 set test "get size of instruction"
80 gdb_test_multiple "x/2i $address_bp" $test {
81 -re ".*$hex <test\\+$decimal>:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
82 set address_after_bp $expect_out(1,string)
86 if {$address_after_bp == ""} {
90 # Write address range where the breakpoint is inserted to the
91 # corresponding variables in the inferior.
92 gdb_test "p /x addr_bp = $address_bp" " = $address_bp" \
94 gdb_test "p /x addr_after_bp = $address_after_bp" " = $address_after_bp" \
97 # Run the "setup" function in the inferior. This memcpy's the
98 # breakpoint instruction to a buffer in the inferior.
99 gdb_test "next" "test_basics \\(\\).*" "next over setup"
103 # We now have the breakpoint instruction stored in 'buffer'. Poke it
104 # to memory manually.
105 set count [expr $address_after_bp - $address_bp]
106 for {set i 0} {$i < $count} {incr i} {
107 set test "p /x addr_bp\[$i\] = buffer\[$i\]"
108 gdb_test_multiple $test $test {
109 -re "Cannot access memory at address $hex.*$gdb_prompt $" {
110 # Some targets (QEMU for one) will disallow writes to the
111 # .text section under certain circumstances. It is no use
112 # continuing with the test at this point. Just return.
113 unsupported "Cannot modify memory"
116 -re " = .*$gdb_prompt $" {
123 with_test_prefix "basics" {
124 # Run to the permanent breakpoint, just to make sure we've inserted it
126 gdb_test "continue" "Program received signal SIGTRAP.*" \
127 "permanent breakpoint causes random signal"
129 # Now set a breakpoint on top, thus creating a permanent breakpoint.
130 gdb_breakpoint "$line_bp"
132 # Depending on whether this is a decr_pc_after_break arch, the PC will
133 # be either pointing at the permanent breakpoint address, or just
134 # after. Set the GDB breakpoint on top, and continue, twice. At
135 # least once, GDB will need to step-over the permanent breakpoint.
137 gdb_test "continue" "Breakpoint .*" "stop at permanent breakpoint"
139 gdb_test "p \$prev_counter = counter" " = $decimal"
141 gdb_test "continue" "Breakpoint .*" "stop at permanent breakpoint twice"
143 # Check that indeed the continue made progress, instead of re-trapping
145 gdb_test "p counter - \$prev_counter" " = 1"
147 gdb_test "info breakpoints" \
148 "breakpoint.*keep.*y.*$hex.*in test at .*$srcfile:$line_bp.*already hit 2 times.*" \
149 "info breakpoints show enabled breakpoint"
151 gdb_test "disable \$bpnum"
153 gdb_test "commands\nset \$commands_ran = 1\nend" "" \
154 "set breakpoint commands"
156 gdb_test "info breakpoints" \
157 "breakpoint.*keep.*n.*$hex.*in test at .*$srcfile:$line_bp.*already hit 2 times.*" \
158 "info breakpoints shows disabled breakpoint"
160 # Run to the permanent breakpoint again. This time, since it's
161 # disabled, it should act as if we hadn't created it in the first
162 # place. IOW, we should get a random signal, and, the breakpoint's
163 # command should not run.
164 gdb_test "continue" "Program received signal SIGTRAP.*" \
165 "disabled permanent breakpoint doesn't explain stop"
167 gdb_test "info breakpoints" \
168 "breakpoint.*keep.*n.*$hex.*in test at .*$srcfile:$line_bp.*already hit 2 times.*" \
169 "info breakpoints still shows same number of hits"
171 gdb_test "print \$commands_ran" " = void" \
172 "breakpoint commands didn't run"
174 # Reenable the breakpoint, and check that it gets hit and accounted
176 gdb_test "enable \$bpnum" "" "reenable breakpoint"
178 gdb_test "continue" "Breakpoint .*" \
179 "stop at permanent breakpoint thrice"
181 gdb_test "info breakpoints" \
182 "breakpoint.*keep.*y.*$hex.*in test at .*$srcfile:$line_bp.*already hit 3 times.*" \
183 "info breakpoints shows one more hit"
185 gdb_test "print \$commands_ran" " = 1" "breakpoint commands ran"
187 # Check that stepi advances only past the permanent breakpoint, and
188 # not a single instruction more.
189 gdb_test "stepi" "after permanent bp .*" \
190 "single-step past permanent breakpoint"
193 with_test_prefix "next trips on permanent bp" {
196 gdb_breakpoint "test_next"
197 gdb_continue_to_breakpoint "test_next"
199 gdb_breakpoint "$line_bp"
200 gdb_test "condition \$bpnum 0"
202 gdb_test "next" "after next .*"
205 if ![target_info exists gdb,nosignals] {
207 with_test_prefix "continue trips on nested permanent bp" {
210 gdb_breakpoint "test_signal_nested"
211 gdb_continue_to_breakpoint "test_signal_nested"
213 gdb_breakpoint "$line_bp"
214 gdb_continue_to_breakpoint "permanent bp"
215 gdb_test "condition \$bpnum 0"
217 # Let SIGALRM trigger.
220 # We're now stopped at a permanent breakpoint, with a
222 gdb_breakpoint "test_signal_nested_done"
223 gdb_continue_to_breakpoint "test_signal_nested_done"
225 # Ensure that the handler did run. There's one call to
226 # test in the mainline code, and another in the signal
228 gdb_test "p counter" " = 2"
231 if [can_single_step_to_signal_handler] {
233 with_test_prefix "stepi signal with handler" {
236 gdb_breakpoint "test_signal_with_handler"
237 gdb_continue_to_breakpoint "test_signal_with_handler"
239 gdb_breakpoint "$line_bp"
241 gdb_test "continue" "Breakpoint .*" "stop at permanent breakpoint"
243 gdb_test "queue-signal SIGUSR1"
245 set test "single-step to handler"
246 gdb_test_multiple "stepi" $test {
247 -re "Program received signal SIGTRAP.*$gdb_prompt $" {
250 -re "handler .*$gdb_prompt $" {
255 # Check that the mainline PC points at the permanent
257 gdb_test "up 2" "test .*" "up to mainline code"
259 gdb_test "p /x \$pc" " = $address_bp" \
260 "mainline pc points at permanent breakpoint"
262 gdb_test "continue" "Breakpoint .*" \
263 "stop at permanent breakpoint, out of handler"
266 with_test_prefix "stepi signal with no handler" {
267 gdb_breakpoint "test_signal_no_handler"
268 gdb_continue_to_breakpoint "test_signal_no_handler"
270 gdb_test "continue" "Breakpoint .*" "stop at permanent breakpoint"
272 gdb_test "queue-signal SIGUSR1"
274 gdb_test "stepi" "after permanent bp .*" \
275 "single-step past permanent breakpoint"
281 foreach always_inserted {off on} {
282 foreach sw_watchpoint {0 1} {
283 with_test_prefix "always_inserted=$always_inserted, sw_watchpoint=$sw_watchpoint" {
284 test $always_inserted $sw_watchpoint