2004-07-06 Andrew Cagney <cagney@gnu.org>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / sigbpt.exp
1 # This testcase is part of GDB, the GNU debugger.
2
3 # Copyright 2004 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 2 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, write to the Free Software
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
19 # Check that GDB can and only executes single instructions when
20 # stepping through a sequence of breakpoints interleaved by a signal
21 # handler.
22
23 # This test is known to tickle the following problems: kernel letting
24 # the inferior execute both the system call, and the instruction
25 # following, when single-stepping a system call; kernel failing to
26 # propogate the single-step state when single-stepping the sigreturn
27 # system call, instead resuming the inferior at full speed; GDB
28 # doesn't know how to software single-step across a sigreturn
29 # instruction. Since the kernel problems can be "fixed" using
30 # software single-step this is KFAILed rather than XFAILed.
31
32 if $tracelevel {
33 strace $tracelevel
34 }
35
36 set prms_id 0
37 set bug_id 0
38
39 set testfile "sigbpt"
40 set srcfile ${testfile}.c
41 set binfile ${objdir}/${subdir}/${testfile}
42 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
43 gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
44 }
45
46 gdb_exit
47 gdb_start
48 gdb_reinitialize_dir $srcdir/$subdir
49 gdb_load ${binfile}
50
51 #
52 # Run to `main' where we begin our tests.
53 #
54
55 if ![runto_main] then {
56 gdb_suppress_tests
57 }
58
59 # If we can examine what's at memory address 0, it is possible that we
60 # could also execute it. This could probably make us run away,
61 # executing random code, which could have all sorts of ill effects,
62 # especially on targets without an MMU. Don't run the tests in that
63 # case.
64
65 send_gdb "x 0\n"
66 gdb_expect {
67 -re "0x0:.*Cannot access memory at address 0x0.*$gdb_prompt $" { }
68 -re "0x0:.*Error accessing memory address 0x0.*$gdb_prompt $" { }
69 -re ".*$gdb_prompt $" {
70 untested "Memory at address 0 is possibly executable"
71 return
72 }
73 }
74
75 gdb_test "break keeper"
76
77 # Run to bowler, and then single step until there's a SIGSEGV. Record
78 # the address of each single-step instruction (up to and including the
79 # instruction that causes the SIGSEGV) in bowler_addrs, and the address
80 # of the actual SIGSEGV in segv_addr.
81
82 set bowler_addrs bowler
83 gdb_test {display/i $pc}
84 gdb_test "advance *bowler" "bowler.*" "advance to the bowler"
85 set test "stepping to SIGSEGV"
86 gdb_test_multiple "stepi" "$test" {
87 -re "Program received signal SIGSEGV.*pc *(0x\[0-9a-f\]*).*$gdb_prompt $" {
88 set segv_addr $expect_out(1,string)
89 pass "$test"
90 }
91 -re " .*pc *(0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" {
92 set bowler_addrs [concat $expect_out(1,string) $bowler_addrs]
93 send_gdb "stepi\n"
94 exp_continue
95 }
96 }
97
98 # Now record the address of the instruction following the faulting
99 # instruction in bowler_addrs.
100
101 set test "get insn after fault"
102 gdb_test_multiple {x/2i $pc} "$test" {
103 -re "(0x\[0-9a-f\]*).*bowler.*(0x\[0-9a-f\]*).*bowler.*$gdb_prompt $" {
104 set bowler_addrs [concat $expect_out(2,string) $bowler_addrs]
105 pass "$test"
106 }
107 }
108
109 # Procedures for returning the address of the instruction before, at
110 # and after, the faulting instruction.
111
112 proc before_segv { } {
113 global bowler_addrs
114 return [lindex $bowler_addrs 2]
115 }
116
117 proc at_segv { } {
118 global bowler_addrs
119 return [lindex $bowler_addrs 1]
120 }
121
122 proc after_segv { } {
123 global bowler_addrs
124 return [lindex $bowler_addrs 0]
125 }
126
127 # Check that the address table and SIGSEGV correspond.
128
129 set test "Verify that SIGSEGV occurs at the last STEPI insn"
130 if {[string compare $segv_addr [at_segv]] == 0} {
131 pass "$test"
132 } else {
133 fail "$test ($segv_addr [at_segv])"
134 }
135
136 # Check that the inferior is correctly single stepped all the way back
137 # to a faulting instruction.
138
139 proc stepi_out { name args } {
140 global gdb_prompt
141
142 # Set SIGSEGV to pass+nostop and then run the inferior all the way
143 # through to the signal handler. With the handler is reached,
144 # disable SIGSEGV, ensuring that further signals stop the
145 # inferior. Stops a SIGSEGV infinite loop when a broke system
146 # keeps re-executing the faulting instruction.
147 rerun_to_main
148 gdb_test "handle SIGSEGV nostop print pass" "" "pass SIGSEGV; $name"
149 gdb_test "continue" "keeper.*" "continue to keeper; $name"
150 gdb_test "handle SIGSEGV stop print nopass" "" "nopass SIGSEGV; $name"
151
152 # Insert all the breakpoints. To avoid the need to step over
153 # these instructions, this is delayed until after the keeper has
154 # been reached.
155 for {set i 0} {$i < [llength $args]} {incr i} {
156 gdb_test "break [lindex $args $i]" "Breakpoint.*" \
157 "set breakpoint $i of [llength $args]; $name"
158 }
159
160 # Single step our way out of the keeper, through the signal
161 # trampoline, and back to the instruction that faulted.
162 set test "stepi out of handler; $name"
163 gdb_test_multiple "stepi" "$test" {
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 $" {
173 kfail gdb/1702 "$test (executed fault insn)"
174 }
175 -re "Breakpoint.*pc *[at_segv] .*bowler.*$gdb_prompt $" {
176 pass "$test (at breakpoint)"
177 }
178 -re "Breakpoint.*pc *[after_segv] .*bowler.*$gdb_prompt $" {
179 kfail gdb/1702 "$test (executed breakpoint)"
180 }
181 -re "pc *[at_segv] .*bowler.*$gdb_prompt $" {
182 pass "$test"
183 }
184 -re "pc *[after_segv] .*bowler.*$gdb_prompt $" {
185 kfail gdb/1702 "$test (skipped fault insn)"
186 }
187 }
188
189 # Clear any breakpoints
190 for {set i 0} {$i < [llength $args]} {incr i} {
191 gdb_test "clear [lindex $args $i]" "Deleted .*" \
192 "clear breakpoint $i of [llength $args]; $name"
193 }
194 }
195
196 # Let a signal handler exit, returning to a breakpoint instruction
197 # inserted at the original fault instruction. Check that the
198 # breakpoint is hit, and that single stepping off that breakpoint
199 # executes the underlying fault instruction causing a SIGSEGV.
200
201 proc cont_out { name args } {
202 global gdb_prompt
203
204 # Set SIGSEGV to pass+nostop and then run the inferior all the way
205 # through to the signal handler. With the handler is reached,
206 # disable SIGSEGV, ensuring that further signals stop the
207 # inferior. Stops a SIGSEGV infinite loop when a broke system
208 # keeps re-executing the faulting instruction.
209 rerun_to_main
210 gdb_test "handle SIGSEGV nostop print pass" "" "pass SIGSEGV; $name"
211 gdb_test "continue" "keeper.*" "continue to keeper; $name"
212 gdb_test "handle SIGSEGV stop print nopass" "" "nopass SIGSEGV; $name"
213
214 # Insert all the breakpoints. To avoid the need to step over
215 # these instructions, this is delayed until after the keeper has
216 # been reached. Always set a breakpoint at the signal trampoline
217 # instruction.
218 set args [concat $args "*[at_segv]"]
219 for {set i 0} {$i < [llength $args]} {incr i} {
220 gdb_test "break [lindex $args $i]" "Breakpoint.*" \
221 "set breakpoint $i of [llength $args]; $name"
222 }
223
224 # Let the handler return, it should "appear to hit" the breakpoint
225 # inserted at the faulting instruction. Note that the breakpoint
226 # instruction wasn't executed, rather the inferior was SIGTRAPed
227 # with the PC at the breakpoint.
228 gdb_test "continue" "Breakpoint.*pc *[at_segv] .*" \
229 "continue to breakpoint at fault; $name"
230
231 # Now single step the faulted instrction at that breakpoint.
232 gdb_test "stepi" \
233 "Program received signal SIGSEGV.*pc *[at_segv] .*" \
234 "stepi fault; $name"
235
236 # Clear any breakpoints
237 for {set i 0} {$i < [llength $args]} {incr i} {
238 gdb_test "clear [lindex $args $i]" "Deleted .*" \
239 "clear breakpoint $i of [llength $args]; $name"
240 }
241
242 }
243
244
245
246 # Try to confuse DECR_PC_AFTER_BREAK architectures by scattering
247 # breakpoints around the faulting address. In all cases the inferior
248 # should single-step out of the signal trampoline halting (but not
249 # executing) the fault instruction.
250
251 stepi_out "stepi"
252 stepi_out "stepi bp before segv" "*[before_segv]"
253 stepi_out "stepi bp at segv" "*[at_segv]"
254 stepi_out "stepi bp before and at segv" "*[at_segv]" "*[before_segv]"
255
256
257 # Try to confuse DECR_PC_AFTER_BREAK architectures by scattering
258 # breakpoints around the faulting address. In all cases the inferior
259 # should exit the signal trampoline halting at the breakpoint that
260 # replaced the fault instruction.
261 cont_out "cont"
262 cont_out "cont bp after segv" "*[before_segv]"
263 cont_out "cont bp before and after segv" "*[before_segv]" "*[after_segv]"
This page took 0.037044 seconds and 5 git commands to generate.