OSDN Git Service

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