Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.base / sigstep.exp
blob54cdfda5f29c8ddae4d6ef9a01710e80dd10e23b
1 # Copyright 2004-2023 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."
26     return
30 standard_testfile
32 if {[build_executable $testfile.exp $testfile $srcfile debug]} {
33     untested "failed to compile"
34     return -1
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.
43 proc restart {} {
44     global binfile
46     clean_restart $binfile
48     gdb_test "display/i \$pc"
50     runto_main
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" {
62         restart
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 "
70         }
71     }
74 validate_backtrace
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
82     global decimal
84     set prefix "$enter_cmd to handler, $in_handler_prefix in handler, $exit_cmd from handler"
86     with_test_prefix $prefix {
87         restart
89         # Get us into the handler
90         if { $enter_cmd == "continue" } {
91             gdb_test "break handler"
92         } else {
93             gdb_test "handle SIGALRM print pass stop"
94             gdb_test "handle SIGVTALRM print pass stop"
95             gdb_test "continue" "Program received signal.*" "continue to signal"
96         }
98         gdb_test_multiple "$enter_cmd" "$enter_cmd to handler" {
99             -re -wrap "\r\n<signal handler called>.*" {
100                 send_gdb "$enter_cmd\n"
101                 exp_continue
102             }
103             -re -wrap "\r\n(Breakpoint $decimal, )?handler \\(sig=.*" {
104                 pass $gdb_test_name
105             }
106         }
108         delete_breakpoints
110         uplevel 1 $in_handler
112         if { $exit_cmd == "continue" } {
113             gdb_test "break $clear_done" ".*" "break clear done"
114         }
116         set test "leave handler"
117         gdb_test_multiple "$exit_cmd" "${test}" {
118             -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
119                 setup_kfail gdb/8841 "sparc*-*-openbsd*"
120                 fail "$test (could not insert single-step breakpoint)"
121             }
122             -re "Cannot insert breakpoint.*Cannot access memory.*$gdb_prompt $" {
123                 setup_kfail gdb/8841 "nios2*-*-linux*"
124                 fail "$test (could not insert single-step breakpoint)"
125             }
126             -re "done = 1;.*${gdb_prompt} $" {
127                 send_gdb "$exit_cmd\n"
128                 exp_continue -continue_timer
129             }
130             -re "\} .. handler .*${gdb_prompt} $" {
131                 send_gdb "$exit_cmd\n"
132                 exp_continue -continue_timer
133             }
134             -re "$inferior_exited_re normally.*${gdb_prompt} $" {
135                 setup_kfail gdb/8744 powerpc-*-*bsd*
136                 fail "$test (program exited)"
137             }
138             -re "(while ..done|done = 0).*${gdb_prompt} $" {
139                 # After stepping out of a function /r signal-handler, GDB will
140                 # advance the inferior until it is at the first instruction of
141                 # a code-line.  While typically things return to the middle of
142                 # the "while..." (and hence GDB advances the inferior to the
143                 # "return..." line) it is also possible for the return to land
144                 # on the first instruction of "while...".  Accept both cases.
145                 pass "$test"
146             }
147         }
148     }
151 # Map of PREFIX => "things to do within the signal handler", for the
152 # advance tests.
154 set in_handler_map {
155     "nothing" {
156     }
157     "si+advance" {
158         # Advance to the second location in handler.
159         gdb_test "si" "handler.*" "si in handler"
161         set test "advance in handler"
162         gdb_test_multiple "advance $other_handler_location" $test {
163             -re "Program received signal SIGTRAP.*$gdb_prompt $" {
164                 # On some versions of Linux (observed on
165                 # 3.16.4-200.fc20.x86_64), using PTRACE_SINGLESTEP+sig
166                 # to step into a signal handler, and then issuing
167                 # another PTRACE_SINGLESTEP within the handler ends up
168                 # with $eflags.TF mistakenly set, which results in
169                 # subsequent PTRACE_CONTINUEs trapping after each
170                 # insn.
171                 if {$enter_cmd != "continue"} {
172                     setup_xfail "i?86-*-linux*" gdb/17511
173                     setup_xfail "x86_64-*-linux*" gdb/17511
174                 }
175                 fail "$test (spurious SIGTRAP)"
176                 return
177             }
178             -re "other handler location.*$gdb_prompt $" {
179                 pass $test
180             }
181         }
182     }
185 # Check that we can step/next/continue, etc. our way in and out of a
186 # signal handler.  Also test that we can step, and run to a breakpoint
187 # within the handler.
189 foreach enter_cmd { "stepi" "nexti" "step" "next" "continue" } {
190     if { $enter_cmd != "continue" && ![can_single_step_to_signal_handler] } {
191         continue
192     }
194     foreach exit_cmd { "step" "next" "continue" } {
195         foreach {in_handler_prefix in_handler} $in_handler_map {
196             advance $enter_cmd $in_handler_prefix $in_handler $exit_cmd
197         }
198     }
201 proc advancei { cmd } {
202     global gdb_prompt inferior_exited_re
204     with_test_prefix "$cmd from handleri" {
205         restart
207         # Get us into the handler.
208         gdb_test "break handler"
209         gdb_test "continue" ".* handler .*" "continue to handler"
211         set program_exited 0
212         set test "leave handler"
213         gdb_test_multiple "$cmd" "${test}" {
214             -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
215                 # Some platforms use a special read-only page for signal
216                 # trampolines.  We can't set a breakpoint there, and we
217                 # don't gracefully fall back to single-stepping.
218                 setup_kfail gdb/8841 "i?86-*-linux*"
219                 setup_kfail gdb/8841 "*-*-openbsd*"
220                 setup_kfail gdb/8841 "nios2-*-linux*"
221                 fail "$test (could not set breakpoint)"
222                 return
223             }
224             -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
225                 setup_kfail gdb/8841 "sparc*-*-openbsd*"
226                 fail "$test (could not insert single-step breakpoint)"
227             }
228             -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
229                 fail "$test (hit breakpoint again)"
230             }
231             -re "done = 1;.*${gdb_prompt} $" {
232                 send_gdb "$cmd\n"
233                 exp_continue -continue_timer
234             }
235             -re "\} .. handler .*${gdb_prompt} $" {
236                 send_gdb "$cmd\n"
237                 exp_continue -continue_timer
238             }
239             -re "signal handler called.*${gdb_prompt} $" {
240                 pass "$test"
241             }
242             -re "main .*${gdb_prompt} $" {
243                 fail "$test (in main)"
244             }
245             -re "$inferior_exited_re normally.*${gdb_prompt} $" {
246                 fail "$test (program exited)"
247                 set program_exited 1
248             }
249             -re "Make handler return now.*y or n. $" {
250                 send_gdb "y\n"
251                 exp_continue -continue_timer
252             }
253         }
255         set test "leave signal trampoline"
256         gdb_test_multiple "$cmd" "${test}" {
257             -re "while .*${gdb_prompt} $" {
258                 pass "$test (in main)"
259             }
260             -re "signal handler called.*${gdb_prompt} $" {
261                 send_gdb "$cmd\n"
262                 exp_continue -continue_timer
263             }
264             -re "return .*${gdb_prompt} $" {
265                 fail "$test (stepped)"
266             }
267             -re "Make .*frame return now.*y or n. $" {
268                 send_gdb "y\n"
269                 exp_continue -continue_timer
270             }
271             -re "$inferior_exited_re normally.*${gdb_prompt} $" {
272                 kfail gdb/8744 "$test (program exited)"
273                 set program_exited 1
274             }
275             -re "The program is not being run.*${gdb_prompt} $" {
276                 if { $program_exited } {
277                     # Previously kfailed with an exit
278                     pass "$test (the program is not being run)"
279                 } else {
280                     fail "$test (the program is not being run)"
281                 }
282             }
283         }
284     }
287 # Check that we can step our way out of a signal handler, using
288 # commands that first step out to the signal trampoline, and then out
289 # to the mainline code.
291 foreach cmd {"stepi" "nexti" "finish" "return"} {
292     advancei $cmd
295 # Check that we can step/next our way into / over a signal handler.
297 # There are at least the following cases: breakpoint @pc VS breakpoint
298 # in handler VS step / next / continue.
301 # Try stepping when there's a signal pending, and a breakpoint at the
302 # handler.  Should step into the signal handler.
304 proc skip_to_handler { cmd } {
305     global infinite_loop
307     with_test_prefix "$cmd to handler" {
308         restart
309         # Use the real-time itimer, as otherwize the process never gets
310         # enough time to expire the timer.
311         gdb_test_no_output "set itimer = itimer_real"
313         # Advance to the infinite loop.
314         gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
316         # Make the signal pending.
317         sleep 1
319         # Insert the handler breakpoint.
320         gdb_test "break handler" ".*"
322         # Step into the handler.
323         gdb_test "$cmd" " handler .*" "performing $cmd"
324     }
327 foreach cmd {"step" "next" "continue"} {
328     skip_to_handler $cmd
331 # Try stepping when there's a signal pending, and a breakpoint at the
332 # handler's entry-point.  Should step into the signal handler stopping
333 # at the entry-point.
335 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
336 # signal, resume the process at the first instruction of the signal
337 # handler and not the first instruction of the signal trampoline.  The
338 # stack is constructed such that the signal handler still appears to
339 # have been called by the trampoline code.  This test checks that it
340 # is possible to stop the inferior, even at that first instruction.
342 proc skip_to_handler_entry { cmd } {
343     global infinite_loop
345     with_test_prefix "$cmd to handler entry" {
346         restart
347         # Use the real-time itimer, as otherwize the process never gets
348         # enough time to expire the timer.
349         gdb_test_no_output "set itimer = itimer_real"
351         # Advance to the infinite loop.
352         gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
354         # Make the signal pending.
355         sleep 1
357         # Insert / remove the handler breakpoint.
358         gdb_test "break *handler" ".*" "break handler"
359         gdb_test "$cmd" " handler .*" "performing $cmd"
360     }
363 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
364     skip_to_handler_entry $cmd
367 # Get the address of where a single-step should land.
369 proc get_next_pc {test} {
370     global gdb_prompt
371     global hex
373     set next ""
374     gdb_test_multiple "x/2i \$pc" $test {
375         -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
376             set next $expect_out(1,string)
377             pass $test
378         }
379     }
381     return $next
384 # Test that the command skipped over the handler.
386 proc test_skip_handler {cmd} {
387     if {$cmd == "stepi" || $cmd == "nexti"} {
388         set next_pc [get_next_pc "get next PC"]
389         gdb_test "$cmd" "dummy = 0.*" "performing $cmd"
390         gdb_test "p /x \$pc" " = $next_pc" "advanced"
391     } else {
392         gdb_test "$cmd" "done = 0.*" "performing $cmd"
393     }
396 # Try stepping when there's a signal pending but no breakpoints.
397 # Should skip the handler advancing to the next line.
399 proc skip_over_handler { cmd } {
400     global infinite_loop
401     global clear_done
403     with_test_prefix "$cmd over handler" {
404         restart
405         # Use the real-time itimer, as otherwize the process never gets
406         # enough time to expire the timer.
407         gdb_test_no_output "set itimer = itimer_real"
409         gdb_test "break $clear_done" ".*" "break clear done"
411         # Advance to the infinite loop.
412         gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
414         # Make the signal pending.
415         sleep 1
417         test_skip_handler $cmd
418     }
421 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
422     skip_over_handler $cmd
425 # Try stepping when there's a signal pending, a pre-existing
426 # breakpoint at the current instruction, and a breakpoint in the
427 # handler.  Should advance to the signal handler.  DISPLACED indicates
428 # whether to try with or without displaced stepping (to exercise the
429 # different techniques of stepping over the breakpoint at the current
430 # instruction).
432 proc breakpoint_to_handler { displaced cmd } {
433     global infinite_loop
435     with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler" {
436         restart
438         gdb_test_no_output "set displaced-stepping $displaced"
440         # Use the real-time itimer, as otherwize the process never gets
441         # enough time to expire the timer.
442         gdb_test_no_output "set itimer = itimer_real"
444         gdb_test "break $infinite_loop" ".*" "break infinite loop"
445         gdb_test "break handler" ".*"
447         # Continue to the infinite loop.
448         gdb_test "continue" "while ..done.*" "continue to infinite loop"
450         # Make the signal pending.
451         sleep 1
453         gdb_test "$cmd" " handler .*" "performing $cmd"
455         # Make sure we the displaced stepping scratch pad isn't in the
456         # backtrace.
457         gdb_test_sequence "bt" "backtrace" {
458             "\[\r\n\]+.0 \[^\r\n\]* handler "
459             "\[\r\n\]+.1  .signal handler called."
460             "\[\r\n\]+.2 \[^\r\n\]* main "
461         }
462     }
465 foreach displaced {"off" "on"} {
466     foreach cmd {"step" "next" "continue"} {
467         breakpoint_to_handler $displaced $cmd
468     }
471 # Try stepping when there's a signal pending, and a breakpoint at the
472 # handler's entry instruction and a breakpoint at the current
473 # instruction.  Should step into the signal handler and breakpoint at
474 # that entry instruction.
476 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
477 # signal, resume the process at the first instruction of the signal
478 # handler and not the first instruction of the signal trampoline.  The
479 # stack is constructed such that the signal handler still appears to
480 # have been called by the trampoline code.  This test checks that it
481 # is possible to stop the inferior, even at that first instruction.
483 # DISPLACED indicates whether to try with or without displaced
484 # stepping (to exercise the different techniques of stepping over the
485 # breakpoint at the current instruction).
486 proc breakpoint_to_handler_entry { displaced cmd } {
487     global infinite_loop
489     with_test_prefix "displaced=$displaced: $cmd on breakpoint, to handler entry" {
490         restart
492         gdb_test_no_output "set displaced-stepping $displaced"
494         # Use the real-time itimer, as otherwize the process never gets
495         # enough time to expire the timer.
496         gdb_test_no_output "set itimer = itimer_real"
498         gdb_test "break $infinite_loop" ".*" "break infinite loop"
499         gdb_test "break *handler" ".*" "break handler"
501         # Continue to the infinite loop.
502         gdb_test "continue" "while ..done.*" "continue to infinite loop"
504         # Make the signal pending.
505         sleep 1
507         gdb_test "$cmd" " handler .*" "performing $cmd"
509         # Make sure we the displaced stepping scratch pad isn't in the
510         # backtrace.
511         gdb_test_sequence "bt" "backtrace" {
512             "\[\r\n\]+.0 \[^\r\n\]* handler "
513             "\[\r\n\]+.1  .signal handler called."
514             "\[\r\n\]+.2 \[^\r\n\]* main "
515         }
516     }
519 foreach displaced {"off" "on"} {
520     foreach cmd {"step" "next" "continue"} {
521         breakpoint_to_handler_entry $displaced $cmd
522     }
525 # Try stepping when there's a signal pending, and a pre-existing
526 # breakpoint at the current instruction, and no breakpoint in the
527 # handler.  Should advance to the next line/instruction.  DISPLACED
528 # indicates whether to try with or without displaced stepping (to
529 # exercise the different techniques of stepping over the breakpoint at
530 # the current instruction).  If SW_WATCH is true, set a software
531 # watchpoint, which exercises stepping the breakpoint instruction
532 # while delivering a signal at the same time.  If NO_HANDLER, arrange
533 # for the signal's handler be SIG_IGN, thus when the software
534 # watchpoint is also set, testing stepping a breakpoint instruction
535 # and immediately triggering the breakpoint (exercises
536 # adjust_pc_after_break logic).
538 proc breakpoint_over_handler { displaced cmd with_sw_watch no_handler } {
539     global infinite_loop
540     global clear_done
542     set prefix "$cmd on breakpoint, skip handler"
543     if { $with_sw_watch } {
544         append prefix ", with sw-watchpoint"
545     }
546     if { $no_handler } {
547         append prefix ", no handler"
548     }
550     with_test_prefix "displaced=$displaced: $prefix" {
551         restart
553         gdb_test_no_output "set displaced-stepping $displaced"
555         # Use the real-time itimer, as otherwize the process never gets
556         # enough time to expire the timer.
557         gdb_test_no_output "set itimer = itimer_real"
559         if {$no_handler} {
560             gdb_test "print no_handler = 1" " = 1" \
561                 "set no_handler"
562         }
564         gdb_test "break $infinite_loop" ".*" "break infinite loop"
566         gdb_test "break $clear_done" ".*" "break clear done"
568         # Continue to the infinite loop
569         gdb_test "continue" "while ..done.*" "continue to infinite loop"
571         # Make the signal pending
572         sleep 1
574         if { $with_sw_watch } {
575             # A watchpoint on a convenience variable is always a
576             # software watchpoint.
577             gdb_test "watch \$convenience" "Watchpoint .*: \\\$convenience"
578         }
580         if {$no_handler} {
581             # With no handler, we need to set the global ourselves
582             # manually.
583             gdb_test "print done = 1" " = 1" "set done"
584         }
586         test_skip_handler $cmd
587     }
590 foreach displaced {"off" "on"} {
591     foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
592         foreach with_sw_watch {0 1} {
593             foreach no_handler {0 1} {
594                 breakpoint_over_handler $displaced $cmd $with_sw_watch $no_handler
595             }
596         }
597     }