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