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