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