1# This testcase is part of GDB, the GNU debugger. 2 3# Copyright 2004-2014 Free Software Foundation, Inc. 4 5# This program is free software; you can redistribute it and/or modify 6# it under the terms of the GNU General Public License as published by 7# the Free Software Foundation; either version 3 of the License, or 8# (at your option) any later version. 9# 10# This program is distributed in the hope that it will be useful, 11# but WITHOUT ANY WARRANTY; without even the implied warranty of 12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13# GNU General Public License for more details. 14# 15# You should have received a copy of the GNU General Public License 16# along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18# Check that GDB can and only executes single instructions when 19# stepping through a sequence of breakpoints interleaved by a signal 20# handler. 21 22# This test is known to tickle the following problems: kernel letting 23# the inferior execute both the system call, and the instruction 24# following, when single-stepping a system call; kernel failing to 25# propogate the single-step state when single-stepping the sigreturn 26# system call, instead resuming the inferior at full speed; GDB 27# doesn't know how to software single-step across a sigreturn 28# instruction. Since the kernel problems can be "fixed" using 29# software single-step this is KFAILed rather than XFAILed. 30 31if [target_info exists gdb,nosignals] { 32 verbose "Skipping sigbpt.exp because of nosignals." 33 continue 34} 35 36 37standard_testfile 38 39if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} { 40 untested $testfile.exp 41 return -1 42} 43 44# 45# Run to `main' where we begin our tests. 46# 47 48if ![runto_main] then { 49 gdb_suppress_tests 50} 51 52# If we can examine what's at memory address 0, it is possible that we 53# could also execute it. This could probably make us run away, 54# executing random code, which could have all sorts of ill effects, 55# especially on targets without an MMU. Don't run the tests in that 56# case. 57 58gdb_test_multiple "x 0" "memory at address 0" { 59 -re "0x0:.*Cannot access memory at address 0x0.*$gdb_prompt $" { } 60 -re "0x0:.*Error accessing memory address 0x0.*$gdb_prompt $" { } 61 -re ".*$gdb_prompt $" { 62 untested "Memory at address 0 is possibly executable" 63 return 64 } 65} 66 67gdb_test "break keeper" 68 69# Run to bowler, and then single step until there's a SIGSEGV. Record 70# the address of each single-step instruction (up to and including the 71# instruction that causes the SIGSEGV) in bowler_addrs, and the address 72# of the actual SIGSEGV in segv_addr. 73# Note: this test detects which signal is received. Usually it is SIGSEGV 74# (and we use SIGSEGV in comments) but on Darwin it is SIGBUS. 75 76set bowler_addrs bowler 77set segv_addr none 78gdb_test {display/i $pc} 79gdb_test "advance *bowler" "bowler.*" "advance to the bowler" 80set test "stepping to fault" 81set signame "SIGSEGV" 82gdb_test_multiple "stepi" "$test" { 83 -re "Program received signal (SIGBUS|SIGSEGV).*pc(\r\n| *) *=> (0x\[0-9a-f\]*).*$gdb_prompt $" { 84 set signame $expect_out(1,string) 85 set segv_addr $expect_out(3,string) 86 pass "$test" 87 } 88 -re " .*pc(\r\n| *)=> (0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" { 89 set bowler_addrs [concat $expect_out(2,string) $bowler_addrs] 90 send_gdb "stepi\n" 91 exp_continue 92 } 93} 94 95# Now record the address of the instruction following the faulting 96# instruction in bowler_addrs. 97 98set test "get insn after fault" 99gdb_test_multiple {x/2i $pc} "$test" { 100 -re "=> (0x\[0-9a-f\]*).*bowler.*(0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" { 101 set bowler_addrs [concat $expect_out(2,string) $bowler_addrs] 102 pass "$test" 103 } 104} 105 106# Procedures for returning the address of the instruction before, at 107# and after, the faulting instruction. 108 109proc before_segv { } { 110 global bowler_addrs 111 return [lindex $bowler_addrs 2] 112} 113 114proc at_segv { } { 115 global bowler_addrs 116 return [lindex $bowler_addrs 1] 117} 118 119proc after_segv { } { 120 global bowler_addrs 121 return [lindex $bowler_addrs 0] 122} 123 124# Check that the address table and SIGSEGV correspond. 125 126set test "Verify that ${signame} occurs at the last STEPI insn" 127if {[string compare $segv_addr [at_segv]] == 0} { 128 pass "$test" 129} else { 130 fail "$test ($segv_addr [at_segv])" 131} 132 133# Check that the inferior is correctly single stepped all the way back 134# to a faulting instruction. 135 136proc stepi_out { name args } { 137 global gdb_prompt 138 global signame 139 140 # Set SIGSEGV to pass+nostop and then run the inferior all the way 141 # through to the signal handler. With the handler is reached, 142 # disable SIGSEGV, ensuring that further signals stop the 143 # inferior. Stops a SIGSEGV infinite loop when a broke system 144 # keeps re-executing the faulting instruction. 145 rerun_to_main 146 gdb_test "handle ${signame} nostop print pass" ".*" "${name}; pass ${signame}" 147 gdb_test "continue" "keeper.*" "${name}; continue to keeper" 148 gdb_test "handle ${signame} stop print nopass" ".*" "${name}; nopass ${signame}" 149 150 # Insert all the breakpoints. To avoid the need to step over 151 # these instructions, this is delayed until after the keeper has 152 # been reached. 153 for {set i 0} {$i < [llength $args]} {incr i} { 154 gdb_test "break [lindex $args $i]" "Breakpoint.*" \ 155 "${name}; set breakpoint $i of [llength $args]" 156 } 157 158 # Single step our way out of the keeper, through the signal 159 # trampoline, and back to the instruction that faulted. 160 set test "${name}; stepi out of handler" 161 gdb_test_multiple "stepi" "$test" { 162 -re "Could not insert single-step breakpoint.*$gdb_prompt $" { 163 setup_kfail gdb/1736 "sparc*-*-openbsd*" 164 fail "$test (could not insert single-step breakpoint)" 165 } 166 -re "keeper.*$gdb_prompt $" { 167 send_gdb "stepi\n" 168 exp_continue 169 } 170 -re "signal handler.*$gdb_prompt $" { 171 send_gdb "stepi\n" 172 exp_continue 173 } 174 -re "Program received signal SIGSEGV.*$gdb_prompt $" { 175 kfail gdb/1702 "$test (executed fault insn)" 176 } 177 -re "Breakpoint.*pc(\r\n| *)[at_segv] .*bowler.*$gdb_prompt $" { 178 pass "$test (at breakpoint)" 179 } 180 -re "Breakpoint.*pc(\r\n| *)[after_segv] .*bowler.*$gdb_prompt $" { 181 kfail gdb/1702 "$test (executed breakpoint)" 182 } 183 -re "pc(\r\n| *)[at_segv] .*bowler.*$gdb_prompt $" { 184 pass "$test" 185 } 186 -re "pc(\r\n| *)[after_segv] .*bowler.*$gdb_prompt $" { 187 kfail gdb/1702 "$test (skipped fault insn)" 188 } 189 -re "pc(\r\n| *)=> 0x\[a-z0-9\]* .*bowler.*$gdb_prompt $" { 190 kfail gdb/1702 "$test (corrupt pc)" 191 } 192 } 193 194 # Clear any breakpoints 195 for {set i 0} {$i < [llength $args]} {incr i} { 196 gdb_test "clear [lindex $args $i]" "Deleted .*" \ 197 "${name}; clear breakpoint $i of [llength $args]" 198 } 199} 200 201# Let a signal handler exit, returning to a breakpoint instruction 202# inserted at the original fault instruction. Check that the 203# breakpoint is hit, and that single stepping off that breakpoint 204# executes the underlying fault instruction causing a SIGSEGV. 205 206proc cont_out { name args } { 207 global gdb_prompt 208 global signame 209 210 # Set SIGSEGV to pass+nostop and then run the inferior all the way 211 # through to the signal handler. With the handler is reached, 212 # disable SIGSEGV, ensuring that further signals stop the 213 # inferior. Stops a SIGSEGV infinite loop when a broke system 214 # keeps re-executing the faulting instruction. 215 rerun_to_main 216 gdb_test "handle ${signame} nostop print pass" ".*" "${name}; pass ${signame}" 217 gdb_test "continue" "keeper.*" "${name}; continue to keeper" 218 gdb_test "handle ${signame} stop print nopass" ".*" "${name}; nopass ${signame}" 219 220 # Insert all the breakpoints. To avoid the need to step over 221 # these instructions, this is delayed until after the keeper has 222 # been reached. Always set a breakpoint at the signal trampoline 223 # instruction. 224 set args [concat $args "*[at_segv]"] 225 for {set i 0} {$i < [llength $args]} {incr i} { 226 gdb_test "break [lindex $args $i]" "Breakpoint.*" \ 227 "${name}; set breakpoint $i of [llength $args]" 228 } 229 230 # Let the handler return, it should "appear to hit" the breakpoint 231 # inserted at the faulting instruction. Note that the breakpoint 232 # instruction wasn't executed, rather the inferior was SIGTRAPed 233 # with the PC at the breakpoint. 234 gdb_test "continue" "Breakpoint.*pc(\r\n| *)=> [at_segv] .*" \ 235 "${name}; continue to breakpoint at fault" 236 237 # Now single step the faulted instrction at that breakpoint. 238 gdb_test "stepi" \ 239 "Program received signal ${signame}.*pc(\r\n| *)=> [at_segv] .*" \ 240 "${name}; stepi fault" 241 242 # Clear any breakpoints 243 for {set i 0} {$i < [llength $args]} {incr i} { 244 gdb_test "clear [lindex $args $i]" "Deleted .*" \ 245 "${name}; clear breakpoint $i of [llength $args]" 246 } 247 248} 249 250 251 252# Try to confuse DECR_PC_AFTER_BREAK architectures by scattering 253# breakpoints around the faulting address. In all cases the inferior 254# should single-step out of the signal trampoline halting (but not 255# executing) the fault instruction. 256 257stepi_out "stepi" 258stepi_out "stepi bp before segv" "*[before_segv]" 259stepi_out "stepi bp at segv" "*[at_segv]" 260stepi_out "stepi bp before and at segv" "*[at_segv]" "*[before_segv]" 261 262 263# Try to confuse DECR_PC_AFTER_BREAK architectures by scattering 264# breakpoints around the faulting address. In all cases the inferior 265# should exit the signal trampoline halting at the breakpoint that 266# replaced the fault instruction. 267cont_out "cont" 268cont_out "cont bp after segv" "*[before_segv]" 269cont_out "cont bp before and after segv" "*[before_segv]" "*[after_segv]" 270