1 # Copyright 2004-2020 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 "failed to compile"
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 "Cannot insert breakpoint.*Cannot access memory.*$gdb_prompt $" {
113 setup_kfail gdb/8841 "nios2*-*-linux*"
114 fail "$test (could not insert single-step breakpoint)"
116 -re "done = 1;.*${gdb_prompt} $" {
117 send_gdb "$exit_cmd\n"
118 exp_continue -continue_timer
120 -re "\} .. handler .*${gdb_prompt} $" {
121 send_gdb "$exit_cmd\n"
122 exp_continue -continue_timer
124 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
125 setup_kfail gdb/8744 powerpc-*-*bsd*
126 fail "$test (program exited)"
128 -re "(while ..done|done = 0).*${gdb_prompt} $" {
129 # After stepping out of a function /r signal-handler, GDB will
130 # advance the inferior until it is at the first instruction of
131 # a code-line. While typically things return to the middle of
132 # the "while..." (and hence GDB advances the inferior to the
133 # "return..." line) it is also possible for the return to land
134 # on the first instruction of "while...". Accept both cases.
141 # Map of PREFIX => "things to do within the signal handler", for the
148 # Advance to the second location in handler.
149 gdb_test "si" "handler.*" "si in handler"
151 set test "advance in handler"
152 gdb_test_multiple "advance $other_handler_location" $test {
153 -re "Program received signal SIGTRAP.*$gdb_prompt $" {
154 # On some versions of Linux (observed on
155 # 3.16.4-200.fc20.x86_64), using PTRACE_SINGLESTEP+sig
156 # to step into a signal handler, and then issuing
157 # another PTRACE_SINGLESTEP within the handler ends up
158 # with $eflags.TF mistakenly set, which results in
159 # subsequent PTRACE_CONTINUEs trapping after each
161 if {$enter_cmd != "continue"} {
162 setup_xfail "i?86-*-linux*" gdb/17511
163 setup_xfail "x86_64-*-linux*" gdb/17511
165 fail "$test (spurious SIGTRAP)"
168 -re "other handler location.*$gdb_prompt $" {
175 # Check that we can step/next/continue, etc. our way in and out of a
176 # signal handler. Also test that we can step, and run to a breakpoint
177 # within the handler.
179 foreach enter_cmd { "stepi" "nexti" "step" "next" "continue" } {
180 if { $enter_cmd != "continue" && ![can_single_step_to_signal_handler] } {
184 foreach exit_cmd { "step" "next" "continue" } {
185 foreach {in_handler_prefix in_handler} $in_handler_map {
186 advance $enter_cmd $in_handler_prefix $in_handler $exit_cmd
191 proc advancei { cmd } {
192 global gdb_prompt inferior_exited_re
194 with_test_prefix "$cmd from handleri" {
197 # Get us into the handler.
198 gdb_test "break handler"
199 gdb_test "continue" ".* handler .*" "continue to handler"
202 set test "leave handler"
203 gdb_test_multiple "$cmd" "${test}" {
204 -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
205 # Some platforms use a special read-only page for signal
206 # trampolines. We can't set a breakpoint there, and we
207 # don't gracefully fall back to single-stepping.
208 setup_kfail gdb/8841 "i?86-*-linux*"
209 setup_kfail gdb/8841 "*-*-openbsd*"
210 setup_kfail gdb/8841 "nios2-*-linux*"
211 fail "$test (could not set breakpoint)"
214 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
215 setup_kfail gdb/8841 "sparc*-*-openbsd*"
216 fail "$test (could not insert single-step breakpoint)"
218 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
219 fail "$test (hit breakpoint again)"
221 -re "done = 1;.*${gdb_prompt} $" {
223 exp_continue -continue_timer
225 -re "\} .. handler .*${gdb_prompt} $" {
227 exp_continue -continue_timer
229 -re "signal handler called.*${gdb_prompt} $" {
232 -re "main .*${gdb_prompt} $" {
233 fail "$test (in main)"
235 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
236 fail "$test (program exited)"
239 -re "Make handler return now.*y or n. $" {
241 exp_continue -continue_timer
245 set test "leave signal trampoline"
246 gdb_test_multiple "$cmd" "${test}" {
247 -re "while .*${gdb_prompt} $" {
248 pass "$test (in main)"
250 -re "signal handler called.*${gdb_prompt} $" {
252 exp_continue -continue_timer
254 -re "return .*${gdb_prompt} $" {
255 fail "$test (stepped)"
257 -re "Make .*frame return now.*y or n. $" {
259 exp_continue -continue_timer
261 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
262 kfail gdb/8744 "$test (program exited)"
265 -re "The program is not being run.*${gdb_prompt} $" {
266 if { $program_exited } {
267 # Previously kfailed with an exit
268 pass "$test (the program is not being run)"
270 fail "$test (the program is not being run)"
277 # Check that we can step our way out of a signal handler, using
278 # commands that first step out to the signal trampoline, and then out
279 # to the mainline code.
281 foreach cmd {"stepi" "nexti" "finish" "return"} {
285 # Check that we can step/next our way into / over a signal handler.
287 # There are at least the following cases: breakpoint @pc VS breakpoint
288 # in handler VS step / next / continue.
291 # Try stepping when there's a signal pending, and a breakpoint at the
292 # handler. Should step into the signal handler.
294 proc skip_to_handler { cmd } {
297 with_test_prefix "$cmd to handler" {
299 # Use the real-time itimer, as otherwize the process never gets
300 # enough time to expire the timer.
301 gdb_test_no_output "set itimer = itimer_real"
303 # Advance to the infinite loop.
304 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
306 # Make the signal pending.
309 # Insert the handler breakpoint.
310 gdb_test "break handler" ".*"
312 # Step into the handler.
313 gdb_test "$cmd" " handler .*" "performing $cmd"
317 foreach cmd {"step" "next" "continue"} {
321 # Try stepping when there's a signal pending, and a breakpoint at the
322 # handler's entry-point. Should step into the signal handler stopping
323 # at the entry-point.
325 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
326 # signal, resume the process at the first instruction of the signal
327 # handler and not the first instruction of the signal trampoline. The
328 # stack is constructed such that the signal handler still appears to
329 # have been called by the trampoline code. This test checks that it
330 # is possible to stop the inferior, even at that first instruction.
332 proc skip_to_handler_entry { cmd } {
335 with_test_prefix "$cmd to handler entry" {
337 # Use the real-time itimer, as otherwize the process never gets
338 # enough time to expire the timer.
339 gdb_test_no_output "set itimer = itimer_real"
341 # Advance to the infinite loop.
342 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
344 # Make the signal pending.
347 # Insert / remove the handler breakpoint.
348 gdb_test "break *handler" ".*" "break handler"
349 gdb_test "$cmd" " handler .*" "performing $cmd"
353 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
354 skip_to_handler_entry $cmd
357 # Get the address of where a single-step should land.
359 proc get_next_pc {test} {
364 gdb_test_multiple "x/2i \$pc" $test {
365 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
366 set next $expect_out(1,string)
374 # Test that the command skipped over the handler.
376 proc test_skip_handler {cmd} {
377 if {$cmd == "stepi" || $cmd == "nexti"} {
378 set next_pc [get_next_pc "get next PC"]
379 gdb_test "$cmd" "dummy = 0.*" "performing $cmd"
380 gdb_test "p /x \$pc" " = $next_pc" "advanced"
382 gdb_test "$cmd" "done = 0.*" "performing $cmd"
386 # Try stepping when there's a signal pending but no breakpoints.
387 # Should skip the handler advancing to the next line.
389 proc skip_over_handler { cmd } {
393 with_test_prefix "$cmd over handler" {
395 # Use the real-time itimer, as otherwize the process never gets
396 # enough time to expire the timer.
397 gdb_test_no_output "set itimer = itimer_real"
399 gdb_test "break $clear_done" ".*" "break clear done"
401 # Advance to the infinite loop.
402 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
404 # Make the signal pending.
407 test_skip_handler $cmd
411 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
412 skip_over_handler $cmd
415 # Try stepping when there's a signal pending, a pre-existing
416 # breakpoint at the current instruction, and a breakpoint in the
417 # handler. Should advance to the signal handler. DISPLACED indicates
418 # whether to try with or without displaced stepping (to exercise the
419 # different techniques of stepping over the breakpoint at the current
422 proc breakpoint_to_handler { displaced cmd } {
425 with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler" {
428 gdb_test_no_output "set displaced-stepping $displaced"
430 # Use the real-time itimer, as otherwize the process never gets
431 # enough time to expire the timer.
432 gdb_test_no_output "set itimer = itimer_real"
434 gdb_test "break $infinite_loop" ".*" "break infinite loop"
435 gdb_test "break handler" ".*"
437 # Continue to the infinite loop.
438 gdb_test "continue" "while ..done.*" "continue to infinite loop"
440 # Make the signal pending.
443 gdb_test "$cmd" " handler .*" "performing $cmd"
445 # Make sure we the displaced stepping scratch pad isn't in the
447 gdb_test_sequence "bt" "backtrace" {
448 "\[\r\n\]+.0 \[^\r\n\]* handler "
449 "\[\r\n\]+.1 .signal handler called."
450 "\[\r\n\]+.2 \[^\r\n\]* main "
455 foreach displaced {"off" "on"} {
456 foreach cmd {"step" "next" "continue"} {
457 breakpoint_to_handler $displaced $cmd
461 # Try stepping when there's a signal pending, and a breakpoint at the
462 # handler's entry instruction and a breakpoint at the current
463 # instruction. Should step into the signal handler and breakpoint at
464 # that entry instruction.
466 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
467 # signal, resume the process at the first instruction of the signal
468 # handler and not the first instruction of the signal trampoline. The
469 # stack is constructed such that the signal handler still appears to
470 # have been called by the trampoline code. This test checks that it
471 # is possible to stop the inferior, even at that first instruction.
473 # DISPLACED indicates whether to try with or without displaced
474 # stepping (to exercise the different techniques of stepping over the
475 # breakpoint at the current instruction).
476 proc breakpoint_to_handler_entry { displaced cmd } {
479 with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler entry" {
482 gdb_test_no_output "set displaced-stepping $displaced"
484 # Use the real-time itimer, as otherwize the process never gets
485 # enough time to expire the timer.
486 gdb_test_no_output "set itimer = itimer_real"
488 gdb_test "break $infinite_loop" ".*" "break infinite loop"
489 gdb_test "break *handler" ".*" "break handler"
491 # Continue to the infinite loop.
492 gdb_test "continue" "while ..done.*" "continue to infinite loop"
494 # Make the signal pending.
497 gdb_test "$cmd" " handler .*" "performing $cmd"
499 # Make sure we the displaced stepping scratch pad isn't in the
501 gdb_test_sequence "bt" "backtrace" {
502 "\[\r\n\]+.0 \[^\r\n\]* handler "
503 "\[\r\n\]+.1 .signal handler called."
504 "\[\r\n\]+.2 \[^\r\n\]* main "
509 foreach displaced {"off" "on"} {
510 foreach cmd {"step" "next" "continue"} {
511 breakpoint_to_handler_entry $displaced $cmd
515 # Try stepping when there's a signal pending, and a pre-existing
516 # breakpoint at the current instruction, and no breakpoint in the
517 # handler. Should advance to the next line/instruction. DISPLACED
518 # indicates whether to try with or without displaced stepping (to
519 # exercise the different techniques of stepping over the breakpoint at
520 # the current instruction). If SW_WATCH is true, set a software
521 # watchpoint, which exercises stepping the breakpoint instruction
522 # while delivering a signal at the same time. If NO_HANDLER, arrange
523 # for the signal's handler be SIG_IGN, thus when the software
524 # watchpoint is also set, testing stepping a breakpoint instruction
525 # and immediately triggering the breakpoint (exercises
526 # adjust_pc_after_break logic).
528 proc breakpoint_over_handler { displaced cmd with_sw_watch no_handler } {
532 set prefix "$cmd on breakpoint, skip handler"
533 if { $with_sw_watch } {
534 append prefix ", with sw-watchpoint"
537 append prefix ", no handler"
540 with_test_prefix "displaced=$displaced: $prefix" {
543 gdb_test_no_output "set displaced-stepping $displaced"
545 # Use the real-time itimer, as otherwize the process never gets
546 # enough time to expire the timer.
547 gdb_test_no_output "set itimer = itimer_real"
550 gdb_test "print no_handler = 1" " = 1" \
554 gdb_test "break $infinite_loop" ".*" "break infinite loop"
556 gdb_test "break $clear_done" ".*" "break clear done"
558 # Continue to the infinite loop
559 gdb_test "continue" "while ..done.*" "continue to infinite loop"
561 # Make the signal pending
564 if { $with_sw_watch } {
565 # A watchpoint on a convenience variable is always a
566 # software watchpoint.
567 gdb_test "watch \$convenience" "Watchpoint .*: \\\$convenience"
571 # With no handler, we need to set the global ourselves
573 gdb_test "print done = 1" " = 1" "set done"
576 test_skip_handler $cmd
580 foreach displaced {"off" "on"} {
581 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
582 foreach with_sw_watch {0 1} {
583 foreach no_handler {0 1} {
584 breakpoint_over_handler $displaced $cmd $with_sw_watch $no_handler