1 # Copyright 2004-2014 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."
32 if {[build_executable $testfile.exp $testfile $srcfile debug]} {
33 untested $testfile.exp
37 set clear_done [gdb_get_line_number {done = 0}]
38 set infinite_loop [gdb_get_line_number {while (!done)}]
40 # Restart GDB, set a display showing $PC, and run to main.
45 clean_restart $binfile
47 gdb_test "display/i \$pc"
52 # Pass all the alarms straight through (but verbosely)
53 # gdb_test "handle SIGALRM print pass nostop"
54 # gdb_test "handle SIGVTALRM print pass nostop"
55 # gdb_test "handle SIGPROF print pass nostop"
57 # Run to the signal handler, validate the backtrace.
59 proc validate_backtrace {} {
60 with_test_prefix "validate backtrace" {
63 gdb_test "break handler"
64 gdb_test "continue" ".* handler .*" "continue to stepi handler"
65 gdb_test_sequence "bt" "backtrace for nexti" {
66 "\[\r\n\]+.0 \[^\r\n\]* handler "
67 "\[\r\n\]+.1 .signal handler called."
68 "\[\r\n\]+.2 \[^\r\n\]* main "
75 proc advance { cmd } {
76 global gdb_prompt inferior_exited_re
78 with_test_prefix "$cmd from handler" {
81 gdb_test "break handler"
83 # Get us into the handler
84 gdb_test "continue" ".* handler .*" "continue to handler"
86 set test "leave handler"
87 gdb_test_multiple "$cmd" "${test}" {
88 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
89 setup_kfail gdb/8841 "sparc*-*-openbsd*"
90 fail "$test (could not insert single-step breakpoint)"
92 -re "done = 1;.*${gdb_prompt} $" {
94 exp_continue -continue_timer
96 -re "\} .. handler .*${gdb_prompt} $" {
98 exp_continue -continue_timer
100 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
101 setup_kfail gdb/8744 powerpc-*-*bsd*
102 fail "$test (program exited)"
104 -re "(while ..done|done = 0).*${gdb_prompt} $" {
105 # After stepping out of a function /r signal-handler, GDB will
106 # advance the inferior until it is at the first instruction of
107 # a code-line. While typically things return to the middle of
108 # the "while..." (and hence GDB advances the inferior to the
109 # "return..." line) it is also possible for the return to land
110 # on the first instruction of "while...". Accept both cases.
117 proc advancei { cmd } {
118 global gdb_prompt inferior_exited_re
120 with_test_prefix "$cmd from handleri" {
123 # Get us into the handler.
124 gdb_test "break handler"
125 gdb_test "continue" ".* handler .*" "continue to handler"
128 set test "leave handler"
129 gdb_test_multiple "$cmd" "${test}" {
130 -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
131 # Some platforms use a special read-only page for signal
132 # trampolines. We can't set a breakpoint there, and we
133 # don't gracefully fall back to single-stepping.
134 setup_kfail gdb/8841 "i?86-*-linux*"
135 setup_kfail gdb/8841 "*-*-openbsd*"
136 fail "$test (could not set breakpoint)"
139 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
140 setup_kfail gdb/8841 "sparc*-*-openbsd*"
141 fail "$test (could not insert single-step breakpoint)"
143 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
144 fail "$test (hit breakpoint again)"
146 -re "done = 1;.*${gdb_prompt} $" {
148 exp_continue -continue_timer
150 -re "\} .. handler .*${gdb_prompt} $" {
152 exp_continue -continue_timer
154 -re "signal handler called.*${gdb_prompt} $" {
157 -re "main .*${gdb_prompt} $" {
158 fail "$test (in main)"
160 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
161 fail "$test (program exited)"
164 -re "Make handler return now.*y or n. $" {
166 exp_continue -continue_timer
170 set test "leave signal trampoline"
171 gdb_test_multiple "$cmd" "${test}" {
172 -re "while .*${gdb_prompt} $" {
173 pass "$test (in main)"
175 -re "signal handler called.*${gdb_prompt} $" {
177 exp_continue -continue_timer
179 -re "return .*${gdb_prompt} $" {
180 fail "$test (stepped)"
182 -re "Make .*frame return now.*y or n. $" {
184 exp_continue -continue_timer
186 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
187 kfail gdb/8744 "$test (program exited)"
190 -re "The program is not being run.*${gdb_prompt} $" {
191 if { $program_exited } {
192 # Previously kfailed with an exit
193 pass "$test (the program is not being run)"
195 fail "$test (the program is not being run)"
202 # Check that we can step/next our way out of a signal handler.
204 foreach cmd {"step" "next"} {
208 foreach cmd {"stepi" "nexti" "finish" "return"} {
212 # Check that we can step/next our way into / over a signal handler.
214 # There are at least the following cases: breakpoint @pc VS breakpoint
215 # in handler VS step / next / continue.
218 # Try stepping when there's a signal pending, and a breakpoint at the
219 # handler. Should step into the signal handler.
221 proc skip_to_handler { cmd } {
224 with_test_prefix "$cmd to handler" {
226 # Use the real-time itimer, as otherwize the process never gets
227 # enough time to expire the timer.
228 gdb_test_no_output "set itimer = itimer_real"
230 # Advance to the infinite loop.
231 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
233 # Make the signal pending.
236 # Insert the handler breakpoint.
237 gdb_test "break handler" ".*" "break handler"
239 # Step into the handler.
240 gdb_test "$cmd" " handler .*" "performing $cmd"
244 foreach cmd {"step" "next" "continue"} {
248 # Try stepping when there's a signal pending, and a breakpoint at the
249 # handler's entry-point. Should step into the signal handler stopping
250 # at the entry-point.
252 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
253 # signal, resume the process at the first instruction of the signal
254 # handler and not the first instruction of the signal trampoline. The
255 # stack is constructed such that the signal handler still appears to
256 # have been called by the trampoline code. This test checks that it
257 # is possible to stop the inferior, even at that first instruction.
259 proc skip_to_handler_entry { cmd } {
262 with_test_prefix "$cmd to handler entry" {
264 # Use the real-time itimer, as otherwize the process never gets
265 # enough time to expire the timer.
266 gdb_test_no_output "set itimer = itimer_real"
268 # Advance to the infinite loop.
269 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
271 # Make the signal pending.
274 # Insert / remove the handler breakpoint.
275 gdb_test "break *handler" ".*" "break handler"
276 gdb_test "$cmd" " handler .*" "performing $cmd"
280 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
281 skip_to_handler_entry $cmd
284 # Get the address of where a single-step should land.
286 proc get_next_pc {test} {
291 gdb_test_multiple "x/2i \$pc" $test {
292 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
293 set next $expect_out(1,string)
301 # Test that the command skipped over the handler.
303 proc test_skip_handler {cmd} {
304 if {$cmd == "stepi" || $cmd == "nexti"} {
305 set next_pc [get_next_pc "get next PC"]
306 gdb_test "$cmd" "dummy = 0.*" "performing $cmd"
307 gdb_test "p /x \$pc" " = $next_pc" "advanced"
309 gdb_test "$cmd" "done = 0.*" "performing $cmd"
313 # Try stepping when there's a signal pending but no breakpoints.
314 # Should skip the handler advancing to the next line.
316 proc skip_over_handler { cmd } {
320 with_test_prefix "$cmd over handler" {
322 # Use the real-time itimer, as otherwize the process never gets
323 # enough time to expire the timer.
324 gdb_test_no_output "set itimer = itimer_real"
326 gdb_test "break $clear_done" ".*" "break clear done"
328 # Advance to the infinite loop.
329 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
331 # Make the signal pending.
334 test_skip_handler $cmd
338 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
339 skip_over_handler $cmd
342 # Try stepping when there's a signal pending, a pre-existing
343 # breakpoint at the current instruction, and a breakpoint in the
344 # handler. Should advance to the signal handler.
346 proc breakpoint_to_handler { cmd } {
349 with_test_prefix "$cmd on breakpoint, to handler" {
351 # Use the real-time itimer, as otherwize the process never gets
352 # enough time to expire the timer.
353 gdb_test_no_output "set itimer = itimer_real"
355 gdb_test "break $infinite_loop" ".*" "break infinite loop"
356 gdb_test "break handler" ".*" "break handler"
358 # Continue to the infinite loop.
359 gdb_test "continue" "while ..done.*" "continue to infinite loop"
361 # Make the signal pending.
364 gdb_test "$cmd" " handler .*" "performing $cmd"
368 foreach cmd {"step" "next" "continue"} {
369 breakpoint_to_handler $cmd
372 # Try stepping when there's a signal pending, and a breakpoint at the
373 # handler's entry instruction and a breakpoint at the current
374 # instruction. Should step into the signal handler and breakpoint at
375 # that entry instruction.
377 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
378 # signal, resume the process at the first instruction of the signal
379 # handler and not the first instruction of the signal trampoline. The
380 # stack is constructed such that the signal handler still appears to
381 # have been called by the trampoline code. This test checks that it
382 # is possible to stop the inferior, even at that first instruction.
384 proc breakpoint_to_handler_entry { cmd } {
387 with_test_prefix "$cmd on breakpoint, to handler entry" {
389 # Use the real-time itimer, as otherwize the process never gets
390 # enough time to expire the timer.
391 gdb_test_no_output "set itimer = itimer_real"
393 gdb_test "break $infinite_loop" ".*" "break infinite loop"
394 gdb_test "break *handler" ".*" "break handler"
396 # Continue to the infinite loop.
397 gdb_test "continue" "while ..done.*" "continue to infinite loop"
399 # Make the signal pending.
402 gdb_test "$cmd" " handler .*" "performing $cmd"
406 foreach cmd {"step" "next" "continue"} {
407 breakpoint_to_handler_entry $cmd
410 # Try stepping when there's a signal pending, and a pre-existing
411 # breakpoint at the current instruction, and no breakpoint in the
412 # handler. Should advance to the next line/instruction.
414 proc breakpoint_over_handler { cmd } {
418 with_test_prefix "$cmd on breakpoint, skip handler" {
420 # Use the real-time itimer, as otherwize the process never gets
421 # enough time to expire the timer.
422 gdb_test_no_output "set itimer = itimer_real"
424 gdb_test "break $infinite_loop" ".*" "break infinite loop"
426 gdb_test "break $clear_done" ".*" "break clear done"
428 # Continue to the infinite loop
429 gdb_test "continue" "while ..done.*" "continue to infinite loop"
431 # Make the signal pending
434 test_skip_handler $cmd
438 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
439 breakpoint_over_handler $cmd