]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/sigbpt.exp
Update copyright year range in header of all files managed by GDB
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / sigbpt.exp
CommitLineData
45a83408
AC
1# This testcase is part of GDB, the GNU debugger.
2
1d506c26 3# Copyright 2004-2024 Free Software Foundation, Inc.
45a83408
AC
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
e22f8b7c 7# the Free Software Foundation; either version 3 of the License, or
45a83408
AC
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
e22f8b7c 16# along with this program. If not, see <http://www.gnu.org/licenses/>.
45a83408
AC
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
450d26c8 31require {!target_info exists gdb,nosignals}
5f579bc5 32
45a83408 33
0ab77f5f
TT
34standard_testfile
35
5b362f04 36if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
b60f0898 37 return -1
45a83408
AC
38}
39
45a83408
AC
40#
41# Run to `main' where we begin our tests.
42#
43
65a33d75 44if {![runto_main]} {
c8ee3f04 45 return 0
45a83408
AC
46}
47
48# If we can examine what's at memory address 0, it is possible that we
49# could also execute it. This could probably make us run away,
50# executing random code, which could have all sorts of ill effects,
51# especially on targets without an MMU. Don't run the tests in that
52# case.
53
20c6f1e1 54if { [is_address_zero_readable] } {
bc6c7af4 55 untested "memory at address 0 is possibly executable"
20c6f1e1 56 return
45a83408
AC
57}
58
59gdb_test "break keeper"
60
61# Run to bowler, and then single step until there's a SIGSEGV. Record
62# the address of each single-step instruction (up to and including the
63# instruction that causes the SIGSEGV) in bowler_addrs, and the address
64# of the actual SIGSEGV in segv_addr.
aacd552b
TG
65# Note: this test detects which signal is received. Usually it is SIGSEGV
66# (and we use SIGSEGV in comments) but on Darwin it is SIGBUS.
45a83408
AC
67
68set bowler_addrs bowler
d12371a9 69set segv_addr none
45a83408 70gdb_test {display/i $pc}
591a12a1 71gdb_test "advance bowler" "bowler.*" "advance to the bowler"
aacd552b
TG
72set test "stepping to fault"
73set signame "SIGSEGV"
45a83408 74gdb_test_multiple "stepi" "$test" {
2b28d209 75 -re "Program received signal (SIGBUS|SIGSEGV).*pc(\r\n| *) *=> (0x\[0-9a-f\]*).*$gdb_prompt $" {
aacd552b
TG
76 set signame $expect_out(1,string)
77 set segv_addr $expect_out(3,string)
45a83408
AC
78 pass "$test"
79 }
2b28d209 80 -re " .*pc(\r\n| *)=> (0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" {
6a2eb474 81 set bowler_addrs [concat $expect_out(2,string) $bowler_addrs]
45a83408
AC
82 send_gdb "stepi\n"
83 exp_continue
84 }
85}
86
87# Now record the address of the instruction following the faulting
88# instruction in bowler_addrs.
89
90set test "get insn after fault"
91gdb_test_multiple {x/2i $pc} "$test" {
2b28d209 92 -re "=> (0x\[0-9a-f\]*).*bowler.*(0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" {
45a83408
AC
93 set bowler_addrs [concat $expect_out(2,string) $bowler_addrs]
94 pass "$test"
95 }
96}
97
98# Procedures for returning the address of the instruction before, at
99# and after, the faulting instruction.
100
101proc before_segv { } {
102 global bowler_addrs
103 return [lindex $bowler_addrs 2]
104}
105
106proc at_segv { } {
107 global bowler_addrs
108 return [lindex $bowler_addrs 1]
109}
110
111proc after_segv { } {
112 global bowler_addrs
113 return [lindex $bowler_addrs 0]
114}
115
116# Check that the address table and SIGSEGV correspond.
117
bc6c7af4 118set test "verify that ${signame} occurs at the last STEPI insn"
45a83408
AC
119if {[string compare $segv_addr [at_segv]] == 0} {
120 pass "$test"
121} else {
122 fail "$test ($segv_addr [at_segv])"
123}
124
125# Check that the inferior is correctly single stepped all the way back
126# to a faulting instruction.
127
128proc stepi_out { name args } {
129 global gdb_prompt
aacd552b 130 global signame
45a83408
AC
131
132 # Set SIGSEGV to pass+nostop and then run the inferior all the way
133 # through to the signal handler. With the handler is reached,
134 # disable SIGSEGV, ensuring that further signals stop the
135 # inferior. Stops a SIGSEGV infinite loop when a broke system
136 # keeps re-executing the faulting instruction.
c2b75043
LM
137 with_test_prefix $name {
138 rerun_to_main
139 }
f6978de9 140 gdb_test "handle ${signame} nostop print pass" ".*" "${name}; pass ${signame}"
1544280f 141 gdb_test "continue" "keeper.*" "${name}; continue to keeper"
f6978de9 142 gdb_test "handle ${signame} stop print nopass" ".*" "${name}; nopass ${signame}"
45a83408
AC
143
144 # Insert all the breakpoints. To avoid the need to step over
145 # these instructions, this is delayed until after the keeper has
146 # been reached.
147 for {set i 0} {$i < [llength $args]} {incr i} {
148 gdb_test "break [lindex $args $i]" "Breakpoint.*" \
1544280f 149 "${name}; set breakpoint $i of [llength $args]"
45a83408
AC
150 }
151
152 # Single step our way out of the keeper, through the signal
153 # trampoline, and back to the instruction that faulted.
1544280f 154 set test "${name}; stepi out of handler"
45a83408 155 gdb_test_multiple "stepi" "$test" {
8608915f 156 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
a5b6e449 157 setup_kfail gdb/8841 "sparc*-*-openbsd*"
8608915f
MK
158 fail "$test (could not insert single-step breakpoint)"
159 }
03346981
SL
160 -re "Cannot insert breakpoint.*Cannot access memory.*$gdb_prompt $" {
161 setup_kfail gdb/8841 "nios2*-*-linux*"
162 fail "$test (could not insert single-step breakpoint)"
163 }
45a83408
AC
164 -re "keeper.*$gdb_prompt $" {
165 send_gdb "stepi\n"
166 exp_continue
167 }
168 -re "signal handler.*$gdb_prompt $" {
169 send_gdb "stepi\n"
170 exp_continue
171 }
172 -re "Program received signal SIGSEGV.*$gdb_prompt $" {
a5b6e449 173 kfail gdb/8807 "$test (executed fault insn)"
45a83408 174 }
6a2eb474 175 -re "Breakpoint.*pc(\r\n| *)[at_segv] .*bowler.*$gdb_prompt $" {
45a83408
AC
176 pass "$test (at breakpoint)"
177 }
6a2eb474 178 -re "Breakpoint.*pc(\r\n| *)[after_segv] .*bowler.*$gdb_prompt $" {
a5b6e449 179 kfail gdb/8807 "$test (executed breakpoint)"
45a83408 180 }
6a2eb474 181 -re "pc(\r\n| *)[at_segv] .*bowler.*$gdb_prompt $" {
45a83408
AC
182 pass "$test"
183 }
6a2eb474 184 -re "pc(\r\n| *)[after_segv] .*bowler.*$gdb_prompt $" {
a5b6e449 185 kfail gdb/8807 "$test (skipped fault insn)"
45a83408 186 }
2b28d209 187 -re "pc(\r\n| *)=> 0x\[a-z0-9\]* .*bowler.*$gdb_prompt $" {
a5b6e449 188 kfail gdb/8807 "$test (corrupt pc)"
56401cd5 189 }
45a83408
AC
190 }
191
192 # Clear any breakpoints
193 for {set i 0} {$i < [llength $args]} {incr i} {
194 gdb_test "clear [lindex $args $i]" "Deleted .*" \
1544280f 195 "${name}; clear breakpoint $i of [llength $args]"
45a83408
AC
196 }
197}
198
199# Let a signal handler exit, returning to a breakpoint instruction
200# inserted at the original fault instruction. Check that the
201# breakpoint is hit, and that single stepping off that breakpoint
202# executes the underlying fault instruction causing a SIGSEGV.
203
204proc cont_out { name args } {
205 global gdb_prompt
aacd552b 206 global signame
45a83408
AC
207
208 # Set SIGSEGV to pass+nostop and then run the inferior all the way
209 # through to the signal handler. With the handler is reached,
210 # disable SIGSEGV, ensuring that further signals stop the
211 # inferior. Stops a SIGSEGV infinite loop when a broke system
212 # keeps re-executing the faulting instruction.
c2b75043
LM
213 with_test_prefix $name {
214 rerun_to_main
215 }
f6978de9 216 gdb_test "handle ${signame} nostop print pass" ".*" "${name}; pass ${signame}"
1544280f 217 gdb_test "continue" "keeper.*" "${name}; continue to keeper"
f6978de9 218 gdb_test "handle ${signame} stop print nopass" ".*" "${name}; nopass ${signame}"
45a83408
AC
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.*" \
1544280f 227 "${name}; set breakpoint $i of [llength $args]"
45a83408
AC
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.
2b28d209 234 gdb_test "continue" "Breakpoint.*pc(\r\n| *)=> [at_segv] .*" \
1544280f 235 "${name}; continue to breakpoint at fault"
45a83408
AC
236
237 # Now single step the faulted instrction at that breakpoint.
238 gdb_test "stepi" \
2b28d209 239 "Program received signal ${signame}.*pc(\r\n| *)=> [at_segv] .*" \
1544280f 240 "${name}; stepi fault"
45a83408
AC
241
242 # Clear any breakpoints
243 for {set i 0} {$i < [llength $args]} {incr i} {
244 gdb_test "clear [lindex $args $i]" "Deleted .*" \
1544280f 245 "${name}; clear breakpoint $i of [llength $args]"
45a83408
AC
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]"