1 # Copyright 2004-2015 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)}]
39 set other_handler_location [gdb_get_line_number "other handler location"]
41 # Restart GDB, set a display showing $PC, and run to main.
46 clean_restart $binfile
48 gdb_test "display/i \$pc"
53 # Pass all the alarms straight through (but verbosely)
54 # gdb_test "handle SIGALRM print pass nostop"
55 # gdb_test "handle SIGVTALRM print pass nostop"
56 # gdb_test "handle SIGPROF print pass nostop"
58 # Run to the signal handler, validate the backtrace.
60 proc validate_backtrace {} {
61 with_test_prefix "validate backtrace" {
64 gdb_test "break handler"
65 gdb_test "continue" ".* handler .*" "continue to stepi handler"
66 gdb_test_sequence "bt" "backtrace for nexti" {
67 "\[\r\n\]+.0 \[^\r\n\]* handler "
68 "\[\r\n\]+.1 .signal handler called."
69 "\[\r\n\]+.2 \[^\r\n\]* main "
76 # Goes to handler using ENTER_CMD, runs IN_HANDLER while in the signal
77 # hander, and then steps out of the signal handler using EXIT_CMD.
79 proc advance { enter_cmd in_handler_prefix in_handler exit_cmd } {
80 global gdb_prompt inferior_exited_re
81 global clear_done other_handler_location
83 set prefix "$enter_cmd to handler, $in_handler_prefix in handler, $exit_cmd from handler"
85 with_test_prefix $prefix {
88 # Get us into the handler
89 if { $enter_cmd == "continue" } {
90 gdb_test "break handler"
92 gdb_test "handle SIGALRM print pass stop"
93 gdb_test "handle SIGVTALRM print pass stop"
94 gdb_test "continue" "Program received signal.*" "continue to signal"
96 gdb_test "$enter_cmd" ".*handler .*" "$enter_cmd to handler"
100 uplevel 1 $in_handler
102 if { $exit_cmd == "continue" } {
103 gdb_test "break $clear_done" ".*" "break clear done"
106 set test "leave handler"
107 gdb_test_multiple "$exit_cmd" "${test}" {
108 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
109 setup_kfail gdb/8841 "sparc*-*-openbsd*"
110 fail "$test (could not insert single-step breakpoint)"
112 -re "done = 1;.*${gdb_prompt} $" {
113 send_gdb "$exit_cmd\n"
114 exp_continue -continue_timer
116 -re "\} .. handler .*${gdb_prompt} $" {
117 send_gdb "$exit_cmd\n"
118 exp_continue -continue_timer
120 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
121 setup_kfail gdb/8744 powerpc-*-*bsd*
122 fail "$test (program exited)"
124 -re "(while ..done|done = 0).*${gdb_prompt} $" {
125 # After stepping out of a function /r signal-handler, GDB will
126 # advance the inferior until it is at the first instruction of
127 # a code-line. While typically things return to the middle of
128 # the "while..." (and hence GDB advances the inferior to the
129 # "return..." line) it is also possible for the return to land
130 # on the first instruction of "while...". Accept both cases.
137 # Map of PREFIX => "things to do within the signal handler", for the
144 # Advance to the second location in handler.
145 gdb_test "si" "handler.*" "si in handler"
147 set test "advance in handler"
148 gdb_test_multiple "advance $other_handler_location" $test {
149 -re "Program received signal SIGTRAP.*$gdb_prompt $" {
150 # On some versions of Linux (observed on
151 # 3.16.4-200.fc20.x86_64), using PTRACE_SINGLESTEP+sig
152 # to step into a signal handler, and then issuing
153 # another PTRACE_SINGLESTEP within the handler ends up
154 # with $eflags.TF mistakenly set, which results in
155 # subsequent PTRACE_CONTINUEs trapping after each
157 if {$enter_cmd != "continue"} {
158 setup_xfail "i?86-*-linux*" gdb/17511
159 setup_xfail "x86_64-*-linux*" gdb/17511
161 fail "$test (spurious SIGTRAP)"
164 -re "other handler location.*$gdb_prompt $" {
171 # Check that we can step/next/continue, etc. our way in and out of a
172 # signal handler. Also test that we can step, and run to a breakpoint
173 # within the handler.
175 foreach enter_cmd { "stepi" "nexti" "step" "next" "continue" } {
176 if { $enter_cmd != "continue" && ![can_single_step_to_signal_handler] } {
180 foreach exit_cmd { "step" "next" "continue" } {
181 foreach {in_handler_prefix in_handler} $in_handler_map {
182 advance $enter_cmd $in_handler_prefix $in_handler $exit_cmd
187 proc advancei { cmd } {
188 global gdb_prompt inferior_exited_re
190 with_test_prefix "$cmd from handleri" {
193 # Get us into the handler.
194 gdb_test "break handler"
195 gdb_test "continue" ".* handler .*" "continue to handler"
198 set test "leave handler"
199 gdb_test_multiple "$cmd" "${test}" {
200 -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
201 # Some platforms use a special read-only page for signal
202 # trampolines. We can't set a breakpoint there, and we
203 # don't gracefully fall back to single-stepping.
204 setup_kfail gdb/8841 "i?86-*-linux*"
205 setup_kfail gdb/8841 "*-*-openbsd*"
206 fail "$test (could not set breakpoint)"
209 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
210 setup_kfail gdb/8841 "sparc*-*-openbsd*"
211 fail "$test (could not insert single-step breakpoint)"
213 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
214 fail "$test (hit breakpoint again)"
216 -re "done = 1;.*${gdb_prompt} $" {
218 exp_continue -continue_timer
220 -re "\} .. handler .*${gdb_prompt} $" {
222 exp_continue -continue_timer
224 -re "signal handler called.*${gdb_prompt} $" {
227 -re "main .*${gdb_prompt} $" {
228 fail "$test (in main)"
230 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
231 fail "$test (program exited)"
234 -re "Make handler return now.*y or n. $" {
236 exp_continue -continue_timer
240 set test "leave signal trampoline"
241 gdb_test_multiple "$cmd" "${test}" {
242 -re "while .*${gdb_prompt} $" {
243 pass "$test (in main)"
245 -re "signal handler called.*${gdb_prompt} $" {
247 exp_continue -continue_timer
249 -re "return .*${gdb_prompt} $" {
250 fail "$test (stepped)"
252 -re "Make .*frame return now.*y or n. $" {
254 exp_continue -continue_timer
256 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
257 kfail gdb/8744 "$test (program exited)"
260 -re "The program is not being run.*${gdb_prompt} $" {
261 if { $program_exited } {
262 # Previously kfailed with an exit
263 pass "$test (the program is not being run)"
265 fail "$test (the program is not being run)"
272 # Check that we can step our way out of a signal handler, using
273 # commands that first step out to the signal trampoline, and then out
274 # to the mainline code.
276 foreach cmd {"stepi" "nexti" "finish" "return"} {
280 # Check that we can step/next our way into / over a signal handler.
282 # There are at least the following cases: breakpoint @pc VS breakpoint
283 # in handler VS step / next / continue.
286 # Try stepping when there's a signal pending, and a breakpoint at the
287 # handler. Should step into the signal handler.
289 proc skip_to_handler { cmd } {
292 with_test_prefix "$cmd to handler" {
294 # Use the real-time itimer, as otherwize the process never gets
295 # enough time to expire the timer.
296 gdb_test_no_output "set itimer = itimer_real"
298 # Advance to the infinite loop.
299 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
301 # Make the signal pending.
304 # Insert the handler breakpoint.
305 gdb_test "break handler" ".*" "break handler"
307 # Step into the handler.
308 gdb_test "$cmd" " handler .*" "performing $cmd"
312 foreach cmd {"step" "next" "continue"} {
316 # Try stepping when there's a signal pending, and a breakpoint at the
317 # handler's entry-point. Should step into the signal handler stopping
318 # at the entry-point.
320 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
321 # signal, resume the process at the first instruction of the signal
322 # handler and not the first instruction of the signal trampoline. The
323 # stack is constructed such that the signal handler still appears to
324 # have been called by the trampoline code. This test checks that it
325 # is possible to stop the inferior, even at that first instruction.
327 proc skip_to_handler_entry { cmd } {
330 with_test_prefix "$cmd to handler entry" {
332 # Use the real-time itimer, as otherwize the process never gets
333 # enough time to expire the timer.
334 gdb_test_no_output "set itimer = itimer_real"
336 # Advance to the infinite loop.
337 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
339 # Make the signal pending.
342 # Insert / remove the handler breakpoint.
343 gdb_test "break *handler" ".*" "break handler"
344 gdb_test "$cmd" " handler .*" "performing $cmd"
348 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
349 skip_to_handler_entry $cmd
352 # Get the address of where a single-step should land.
354 proc get_next_pc {test} {
359 gdb_test_multiple "x/2i \$pc" $test {
360 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
361 set next $expect_out(1,string)
369 # Test that the command skipped over the handler.
371 proc test_skip_handler {cmd} {
372 if {$cmd == "stepi" || $cmd == "nexti"} {
373 set next_pc [get_next_pc "get next PC"]
374 gdb_test "$cmd" "dummy = 0.*" "performing $cmd"
375 gdb_test "p /x \$pc" " = $next_pc" "advanced"
377 gdb_test "$cmd" "done = 0.*" "performing $cmd"
381 # Try stepping when there's a signal pending but no breakpoints.
382 # Should skip the handler advancing to the next line.
384 proc skip_over_handler { cmd } {
388 with_test_prefix "$cmd over handler" {
390 # Use the real-time itimer, as otherwize the process never gets
391 # enough time to expire the timer.
392 gdb_test_no_output "set itimer = itimer_real"
394 gdb_test "break $clear_done" ".*" "break clear done"
396 # Advance to the infinite loop.
397 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
399 # Make the signal pending.
402 test_skip_handler $cmd
406 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
407 skip_over_handler $cmd
410 # Try stepping when there's a signal pending, a pre-existing
411 # breakpoint at the current instruction, and a breakpoint in the
412 # handler. Should advance to the signal handler. DISPLACED indicates
413 # whether to try with or without displaced stepping (to exercise the
414 # different techniques of stepping over the breakpoint at the current
417 proc breakpoint_to_handler { displaced cmd } {
420 with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler" {
423 gdb_test_no_output "set displaced-stepping $displaced"
425 # Use the real-time itimer, as otherwize the process never gets
426 # enough time to expire the timer.
427 gdb_test_no_output "set itimer = itimer_real"
429 gdb_test "break $infinite_loop" ".*" "break infinite loop"
430 gdb_test "break handler" ".*" "break handler"
432 # Continue to the infinite loop.
433 gdb_test "continue" "while ..done.*" "continue to infinite loop"
435 # Make the signal pending.
438 gdb_test "$cmd" " handler .*" "performing $cmd"
440 # Make sure we the displaced stepping scratch pad isn't in the
442 gdb_test_sequence "bt" "backtrace" {
443 "\[\r\n\]+.0 \[^\r\n\]* handler "
444 "\[\r\n\]+.1 .signal handler called."
445 "\[\r\n\]+.2 \[^\r\n\]* main "
450 foreach displaced {"off" "on"} {
451 foreach cmd {"step" "next" "continue"} {
452 breakpoint_to_handler $displaced $cmd
456 # Try stepping when there's a signal pending, and a breakpoint at the
457 # handler's entry instruction and a breakpoint at the current
458 # instruction. Should step into the signal handler and breakpoint at
459 # that entry instruction.
461 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
462 # signal, resume the process at the first instruction of the signal
463 # handler and not the first instruction of the signal trampoline. The
464 # stack is constructed such that the signal handler still appears to
465 # have been called by the trampoline code. This test checks that it
466 # is possible to stop the inferior, even at that first instruction.
468 # DISPLACED indicates whether to try with or without displaced
469 # stepping (to exercise the different techniques of stepping over the
470 # breakpoint at the current instruction).
471 proc breakpoint_to_handler_entry { displaced cmd } {
474 with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler entry" {
477 gdb_test_no_output "set displaced-stepping $displaced"
479 # Use the real-time itimer, as otherwize the process never gets
480 # enough time to expire the timer.
481 gdb_test_no_output "set itimer = itimer_real"
483 gdb_test "break $infinite_loop" ".*" "break infinite loop"
484 gdb_test "break *handler" ".*" "break handler"
486 # Continue to the infinite loop.
487 gdb_test "continue" "while ..done.*" "continue to infinite loop"
489 # Make the signal pending.
492 gdb_test "$cmd" " handler .*" "performing $cmd"
494 # Make sure we the displaced stepping scratch pad isn't in the
496 gdb_test_sequence "bt" "backtrace" {
497 "\[\r\n\]+.0 \[^\r\n\]* handler "
498 "\[\r\n\]+.1 .signal handler called."
499 "\[\r\n\]+.2 \[^\r\n\]* main "
504 foreach displaced {"off" "on"} {
505 foreach cmd {"step" "next" "continue"} {
506 breakpoint_to_handler_entry $displaced $cmd
510 # Try stepping when there's a signal pending, and a pre-existing
511 # breakpoint at the current instruction, and no breakpoint in the
512 # handler. Should advance to the next line/instruction. DISPLACED
513 # indicates whether to try with or without displaced stepping (to
514 # exercise the different techniques of stepping over the breakpoint at
515 # the current instruction). If SW_WATCH is true, set a software
516 # watchpoint, which exercises stepping the breakpoint instruction
517 # while delivering a signal at the same time. If NO_HANDLER, arrange
518 # for the signal's handler be SIG_IGN, thus when the software
519 # watchpoint is also set, testing stepping a breakpoint instruction
520 # and immediately triggering the breakpoint (exercises
521 # adjust_pc_after_break logic).
523 proc breakpoint_over_handler { displaced cmd with_sw_watch no_handler } {
527 set prefix "$cmd on breakpoint, skip handler"
528 if { $with_sw_watch } {
529 append prefix ", with sw-watchpoint"
532 append prefix ", no handler"
535 with_test_prefix "displaced=$displaced: $prefix" {
538 gdb_test_no_output "set displaced-stepping $displaced"
540 # Use the real-time itimer, as otherwize the process never gets
541 # enough time to expire the timer.
542 gdb_test_no_output "set itimer = itimer_real"
545 gdb_test "print no_handler = 1" " = 1" \
549 gdb_test "break $infinite_loop" ".*" "break infinite loop"
551 gdb_test "break $clear_done" ".*" "break clear done"
553 # Continue to the infinite loop
554 gdb_test "continue" "while ..done.*" "continue to infinite loop"
556 # Make the signal pending
559 if { $with_sw_watch } {
560 # A watchpoint on a convenience variable is always a
561 # software watchpoint.
562 gdb_test "watch \$convenience" "Watchpoint .*: \\\$convenience"
566 # With no handler, we need to set the global ourselves
568 gdb_test "print done = 1" " = 1" "set done"
571 test_skip_handler $cmd
575 foreach displaced {"off" "on"} {
576 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
577 foreach with_sw_watch {0 1} {
578 foreach no_handler {0 1} {
579 breakpoint_over_handler $displaced $cmd $with_sw_watch $no_handler