1 # Copyright 2004-2013 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # The program sigstep.c creates a very simple backtrace containing one
18 # signal handler and signal trampoline. A flag is set and then the
19 # handler returns. This is repeated at infinitum.
21 # This test runs the program up to the signal handler, and then
22 # attempts to step/next out of the handler and back into main.
24 if [target_info exists gdb,nosignals] {
25 verbose "Skipping sigstep.exp because of nosignals."
31 set srcfile ${testfile}.c
32 set binfile ${objdir}/${subdir}/${testfile}
33 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34 untested "Couldn't compile ${srcfile}.c"
41 gdb_reinitialize_dir $srcdir/$subdir
44 gdb_test "display/i \$pc"
47 if { ![runto_main] } then {
51 # Pass all the alarms straight through (but verbosely)
52 # gdb_test "handle SIGALRM print pass nostop"
53 # gdb_test "handle SIGVTALRM print pass nostop"
54 # gdb_test "handle SIGPROF print pass nostop"
56 # Run to the signal handler, validate the backtrace.
57 gdb_test "break handler"
58 gdb_test "continue" ".* handler .*" "continue to stepi handler"
59 gdb_test_sequence "bt" "backtrace for nexti" {
60 "\[\r\n\]+.0 \[^\r\n\]* handler "
61 "\[\r\n\]+.1 .signal handler called."
62 "\[\r\n\]+.2 \[^\r\n\]* main "
66 global gdb_prompt inferior_exited_re
67 set prefix "$i from handler"
69 # Get us back into the handler
70 gdb_test "continue" ".* handler .*" "$prefix; continue to handler"
72 set test "$prefix; leave handler"
73 gdb_test_multiple "$i" "${test}" {
74 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
75 setup_kfail gdb/1736 "sparc*-*-openbsd*"
76 fail "$test (could not insert single-step breakpoint)"
78 -re "done = 1;.*${gdb_prompt} $" {
80 exp_continue -continue_timer
82 -re "\} .. handler .*${gdb_prompt} $" {
84 exp_continue -continue_timer
86 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
87 setup_kfail gdb/1639 powerpc-*-*bsd*
88 fail "$test (program exited)"
90 -re "(while ..done|done = 0).*${gdb_prompt} $" {
91 # After stepping out of a function /r signal-handler, GDB will
92 # advance the inferior until it is at the first instruction of
93 # a code-line. While typically things return to the middle of
94 # the "while..." (and hence GDB advances the inferior to the
95 # "return..." line) it is also possible for the return to land
96 # on the first instruction of "while...". Accept both cases.
102 proc advancei { i } {
103 global gdb_prompt inferior_exited_re
104 set prefix "$i from handleri"
107 # Get us back into the handler
108 gdb_test "continue" ".* handler .*" "$prefix; continue to handler"
110 set test "$prefix; leave handler"
111 gdb_test_multiple "$i" "${test}" {
112 -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
113 # Some platforms use a special read-only page for signal
114 # trampolines. We can't set a breakpoint there, and we
115 # don't gracefully fall back to single-stepping.
116 setup_kfail gdb/1736 "i?86-*-linux*"
117 setup_kfail gdb/1736 "*-*-openbsd*"
118 fail "$test (could not set breakpoint)"
121 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
122 setup_kfail gdb/1736 "sparc*-*-openbsd*"
123 fail "$test (could not insert single-step breakpoint)"
125 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
126 fail "$test (hit breakpoint again)"
128 -re "done = 1;.*${gdb_prompt} $" {
130 exp_continue -continue_timer
132 -re "\} .. handler .*${gdb_prompt} $" {
134 exp_continue -continue_timer
136 -re "signal handler called.*${gdb_prompt} $" {
139 -re "main .*${gdb_prompt} $" {
140 fail "$test (in main)"
142 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
143 fail "$test (program exited)"
146 -re "Make handler return now.*y or n. $" {
148 exp_continue -continue_timer
152 set test "$prefix; leave signal trampoline"
153 gdb_test_multiple "$i" "${test}" {
154 -re "while .*${gdb_prompt} $" {
155 pass "$test (in main)"
157 -re "signal handler called.*${gdb_prompt} $" {
159 exp_continue -continue_timer
161 -re "return .*${gdb_prompt} $" {
162 fail "$test (stepped)"
164 -re "Make .*frame return now.*y or n. $" {
166 exp_continue -continue_timer
168 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
169 kfail gdb/1639 "$test (program exited)"
172 -re "The program is not being run.*${gdb_prompt} $" {
173 if { $program_exited } {
174 # Previously kfailed with an exit
175 pass "$test (the program is not being run)"
177 fail "$test (the program is not being run)"
183 # Check that we can step/next our way out of a signal handler.
193 gdb_test_no_output "set done = 1" "Set done as return will have skipped it"
196 # Check that we can step/next our way into / over a signal handler.
198 # There are at least the following cases: breakpoint @pc VS breakpoint
199 # in handler VS step / next / continue.
201 # Use the real-time itimer, as otherwize the process never gets enough
202 # time to expire the timer.
205 set infinite_loop [gdb_get_line_number {while (!done)}]
206 gdb_test_no_output "set itimer = itimer_real"
207 gdb_test "break [gdb_get_line_number {done = 0}]"
209 # Try stepping when there's a signal pending, and a breakpoint at the
210 # handler. Should step into the signal handler.
212 proc skip_to_handler { i } {
215 set prefix "$i to handler"
217 # Run around to the done
218 # You can add more patterns to this if you need them.
219 set test "$prefix; resync"
220 gdb_test_multiple "continue" "$test" {
221 -re "done = 0.*$gdb_prompt " {
226 # Advance to the infinite loop
227 gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
229 # Make the signal pending
232 # Insert / remove the handler breakpoint.
233 gdb_test "break handler" ".*" "$prefix; break handler"
234 gdb_test "$i" " handler .*" "$prefix; performing $i"
235 gdb_test "clear handler" ".*" "$prefix; clear handler"
240 skip_to_handler continue
242 # Try stepping when there's a signal pending, and a breakpoint at the
243 # handler's entry-point. Should step into the signal handler stopping
244 # at the entry-point.
246 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
247 # signal, resume the process at the first instruction of the signal
248 # handler and not the first instruction of the signal trampoline. The
249 # stack is constructed such that the signal handler still appears to
250 # have been called by the trampoline code. This test checks that it
251 # is possible to stop the inferior, even at that first instruction.
253 proc skip_to_handler_entry { i } {
256 set prefix "$i to handler entry"
258 # Run around to the done
259 # You can add more patterns to this if you need them.
260 set test "$prefix; resync"
261 gdb_test_multiple "continue" "$test" {
262 -re "done = 0.*$gdb_prompt " {
267 # Advance to the infinite loop
268 gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
270 # Make the signal pending
273 # Insert / remove the handler breakpoint.
274 gdb_test "break *handler" ".*" "$prefix; break handler"
275 gdb_test "$i" " handler .*" "$prefix; performing $i"
276 gdb_test "clear *handler" ".*" "$prefix; clear handler"
279 skip_to_handler_entry step
280 skip_to_handler_entry next
281 skip_to_handler_entry continue
283 # Try stepping when there's a signal pending but no breakpoints.
284 # Should skip the handler advancing to the next line.
286 proc skip_over_handler { i } {
289 set prefix "$i over handler"
291 # Run around to the done
292 # You can add more patterns to this if you need them.
293 set test "$prefix; resync"
294 gdb_test_multiple "continue" "$test" {
295 -re "done = 0.*$gdb_prompt " {
300 # Advance to the infinite loop
301 gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
303 # Make the signal pending
306 gdb_test "$i" "done = 0.*" "$prefix; performing $i"
309 skip_over_handler step
310 skip_over_handler next
311 skip_over_handler continue
313 # Try stepping when there's a signal pending, a pre-existing
314 # breakpoint at the current instruction, and a breakpoint in the
315 # handler. Should advance to the signal handler.
317 proc breakpoint_to_handler { i } {
320 set prefix "$i on breakpoint, to handler"
322 # Run around to the done
323 # You can add more patterns to this if you need them.
324 set test "$prefix; resync"
325 gdb_test_multiple "continue" "$test" {
326 -re "done = 0.*$gdb_prompt " {
331 gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
332 gdb_test "break handler" ".*" "$prefix; break handler"
334 # Continue to the infinite loop
335 gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
337 # Make the signal pending
340 gdb_test "$i" " handler .*" "$prefix; performing $i"
341 gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
342 gdb_test "clear handler" ".*" "$prefix; clear handler"
345 breakpoint_to_handler step
346 breakpoint_to_handler next
347 breakpoint_to_handler continue
349 # Try stepping when there's a signal pending, and a breakpoint at the
350 # handler's entry instruction and a breakpoint at the current
351 # instruction. Should step into the signal handler and breakpoint at
352 # that entry instruction.
354 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
355 # signal, resume the process at the first instruction of the signal
356 # handler and not the first instruction of the signal trampoline. The
357 # stack is constructed such that the signal handler still appears to
358 # have been called by the trampoline code. This test checks that it
359 # is possible to stop the inferior, even at that first instruction.
361 proc breakpoint_to_handler_entry { i } {
364 set prefix "$i on breakpoint, to handler entry"
366 # Run around to the done
367 # You can add more patterns to this if you need them.
368 set test "$prefix; resync"
369 gdb_test_multiple "continue" "$test" {
370 -re "done = 0.*$gdb_prompt " {
375 gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
376 gdb_test "break *handler" ".*" "$prefix; break handler"
378 # Continue to the infinite loop
379 gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
381 # Make the signal pending
384 gdb_test "$i" " handler .*" "$prefix; performing $i"
385 gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
386 gdb_test "clear *handler" ".*" "$prefix; clear handler"
389 breakpoint_to_handler_entry step
390 breakpoint_to_handler_entry next
391 breakpoint_to_handler_entry continue
393 # Try stepping when there's a signal pending, and a pre-existing
394 # breakpoint at the current instruction, and no breakpoint in the
395 # handler. Should advance to the next line.
397 proc breakpoint_over_handler { i } {
400 set prefix "$i on breakpoint, skip handler"
402 # Run around to the done
403 # You can add more patterns to this if you need them.
404 set test "$prefix; resync"
405 gdb_test_multiple "continue" "$test" {
406 -re "done = 0.*$gdb_prompt " {
411 gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
413 # Continue to the infinite loop
414 gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
416 # Make the signal pending
419 gdb_test "$i" "done = 0.*" "$prefix; performing $i"
420 gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
423 breakpoint_over_handler step
424 breakpoint_over_handler next
425 breakpoint_over_handler continue