Add copyright headers.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / sigstep.exp
CommitLineData
ecd75fc8 1# Copyright 2004-2014 Free Software Foundation, Inc.
f0fd9238
AC
2
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
f0fd9238 6# (at your option) any later version.
e22f8b7c 7#
f0fd9238
AC
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.
e22f8b7c 12#
f0fd9238 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
f0fd9238
AC
15
16
17# The program sigstep.c creates a very simple backtrace containing one
b04e311d
AC
18# signal handler and signal trampoline. A flag is set and then the
19# handler returns. This is repeated at infinitum.
f0fd9238
AC
20
21# This test runs the program up to the signal handler, and then
b04e311d 22# attempts to step/next out of the handler and back into main.
f0fd9238
AC
23
24if [target_info exists gdb,nosignals] {
25 verbose "Skipping sigstep.exp because of nosignals."
26 continue
27}
28
f0fd9238 29
0ab77f5f
TT
30standard_testfile
31
1df4399f 32if {[build_executable $testfile.exp $testfile $srcfile debug]} {
0ab77f5f 33 untested $testfile.exp
f0fd9238
AC
34 return -1
35}
36
1df4399f
PA
37set clear_done [gdb_get_line_number {done = 0}]
38set infinite_loop [gdb_get_line_number {while (!done)}]
abbdbd03 39set other_handler_location [gdb_get_line_number "other handler location"]
1df4399f
PA
40
41# Restart GDB, set a display showing $PC, and run to main.
42
43proc restart {} {
44 global binfile
45
46 clean_restart $binfile
f0fd9238 47
1df4399f
PA
48 gdb_test "display/i \$pc"
49
50 runto_main
f0fd9238
AC
51}
52
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"
57
58# Run to the signal handler, validate the backtrace.
1df4399f
PA
59
60proc validate_backtrace {} {
61 with_test_prefix "validate backtrace" {
62 restart
63
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 }
f0fd9238
AC
72}
73
1df4399f
PA
74validate_backtrace
75
abbdbd03
PA
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.
78
79proc advance { enter_cmd in_handler_prefix in_handler exit_cmd } {
fda326dd 80 global gdb_prompt inferior_exited_re
abbdbd03 81 global clear_done other_handler_location
f0fd9238 82
abbdbd03 83 set prefix "$enter_cmd to handler, $in_handler_prefix in handler, $exit_cmd from handler"
f0fd9238 84
abbdbd03
PA
85 with_test_prefix $prefix {
86 restart
1df4399f
PA
87
88 # Get us into the handler
abbdbd03
PA
89 if { $enter_cmd == "continue" } {
90 gdb_test "break handler"
91 } else {
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"
95 }
96 gdb_test "$enter_cmd" ".*handler .*" "$enter_cmd to handler"
97
98 delete_breakpoints
99
100 uplevel 1 $in_handler
101
102 if { $exit_cmd == "continue" } {
103 gdb_test "break $clear_done" ".*" "break clear done"
104 }
1df4399f
PA
105
106 set test "leave handler"
abbdbd03 107 gdb_test_multiple "$exit_cmd" "${test}" {
1df4399f
PA
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)"
111 }
112 -re "done = 1;.*${gdb_prompt} $" {
abbdbd03 113 send_gdb "$exit_cmd\n"
1df4399f
PA
114 exp_continue -continue_timer
115 }
116 -re "\} .. handler .*${gdb_prompt} $" {
abbdbd03 117 send_gdb "$exit_cmd\n"
1df4399f
PA
118 exp_continue -continue_timer
119 }
120 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
121 setup_kfail gdb/8744 powerpc-*-*bsd*
122 fail "$test (program exited)"
123 }
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.
131 pass "$test"
132 }
f0fd9238
AC
133 }
134 }
135}
136
abbdbd03
PA
137# Map of PREFIX => "things to do within the signal handler", for the
138# advance tests.
139
140set in_handler_map {
141 "nothing" {
142 }
143 "si+advance" {
144 # Advance to the second location in handler.
145 gdb_test "si" "handler.*" "si in handler"
146
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
156 # insn.
157 if {$enter_cmd != "continue"} {
9de00a4a 158 setup_xfail "i?86-*-linux*" gdb/17511
abbdbd03
PA
159 setup_xfail "x86_64-*-linux*" gdb/17511
160 }
161 fail "$test (spurious SIGTRAP)"
162 return
163 }
164 -re "other handler location.*$gdb_prompt $" {
165 pass $test
166 }
167 }
168 }
169}
170
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.
174
175foreach enter_cmd { "stepi" "nexti" "step" "next" "continue" } {
176 if { $enter_cmd != "continue" && ![can_single_step_to_signal_handler] } {
177 continue
178 }
179
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
183 }
184 }
185}
186
1df4399f 187proc advancei { cmd } {
fda326dd 188 global gdb_prompt inferior_exited_re
f0fd9238 189
1df4399f
PA
190 with_test_prefix "$cmd from handleri" {
191 restart
192
193 # Get us into the handler.
194 gdb_test "break handler"
195 gdb_test "continue" ".* handler .*" "continue to handler"
196
197 set program_exited 0
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)"
207 return
208 }
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)"
212 }
213 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
214 fail "$test (hit breakpoint again)"
215 }
216 -re "done = 1;.*${gdb_prompt} $" {
217 send_gdb "$cmd\n"
218 exp_continue -continue_timer
219 }
220 -re "\} .. handler .*${gdb_prompt} $" {
221 send_gdb "$cmd\n"
222 exp_continue -continue_timer
223 }
224 -re "signal handler called.*${gdb_prompt} $" {
225 pass "$test"
226 }
227 -re "main .*${gdb_prompt} $" {
228 fail "$test (in main)"
229 }
230 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
231 fail "$test (program exited)"
232 set program_exited 1
233 }
234 -re "Make handler return now.*y or n. $" {
235 send_gdb "y\n"
236 exp_continue -continue_timer
237 }
42edda50 238 }
1df4399f
PA
239
240 set test "leave signal trampoline"
241 gdb_test_multiple "$cmd" "${test}" {
242 -re "while .*${gdb_prompt} $" {
243 pass "$test (in main)"
244 }
245 -re "signal handler called.*${gdb_prompt} $" {
246 send_gdb "$cmd\n"
247 exp_continue -continue_timer
248 }
249 -re "return .*${gdb_prompt} $" {
250 fail "$test (stepped)"
251 }
252 -re "Make .*frame return now.*y or n. $" {
253 send_gdb "y\n"
254 exp_continue -continue_timer
255 }
256 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
257 kfail gdb/8744 "$test (program exited)"
258 set program_exited 1
259 }
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)"
264 } else {
265 fail "$test (the program is not being run)"
266 }
f0fd9238
AC
267 }
268 }
269 }
270}
271
abbdbd03
PA
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.
b04e311d 275
1df4399f
PA
276foreach cmd {"stepi" "nexti" "finish" "return"} {
277 advancei $cmd
278}
b04e311d
AC
279
280# Check that we can step/next our way into / over a signal handler.
281
282# There are at least the following cases: breakpoint @pc VS breakpoint
283# in handler VS step / next / continue.
284
b04e311d
AC
285
286# Try stepping when there's a signal pending, and a breakpoint at the
287# handler. Should step into the signal handler.
288
1df4399f 289proc skip_to_handler { cmd } {
b04e311d 290 global infinite_loop
1df4399f
PA
291
292 with_test_prefix "$cmd to handler" {
293 restart
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"
297
298 # Advance to the infinite loop.
299 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
300
301 # Make the signal pending.
302 sleep 1
303
304 # Insert the handler breakpoint.
305 gdb_test "break handler" ".*" "break handler"
306
307 # Step into the handler.
308 gdb_test "$cmd" " handler .*" "performing $cmd"
b04e311d 309 }
b04e311d
AC
310}
311
1df4399f
PA
312foreach cmd {"step" "next" "continue"} {
313 skip_to_handler $cmd
314}
b04e311d 315
2dedb159
AC
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.
319
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.
326
1df4399f 327proc skip_to_handler_entry { cmd } {
2dedb159 328 global infinite_loop
1df4399f
PA
329
330 with_test_prefix "$cmd to handler entry" {
331 restart
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"
335
336 # Advance to the infinite loop.
337 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
338
339 # Make the signal pending.
340 sleep 1
341
342 # Insert / remove the handler breakpoint.
343 gdb_test "break *handler" ".*" "break handler"
344 gdb_test "$cmd" " handler .*" "performing $cmd"
2dedb159 345 }
2dedb159
AC
346}
347
e5f8a7cc
PA
348foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
349 skip_to_handler_entry $cmd
350}
351
352# Get the address of where a single-step should land.
353
354proc get_next_pc {test} {
355 global gdb_prompt
356 global hex
357
358 set next ""
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)
362 pass $test
363 }
364 }
365
366 return $next
367}
368
369# Test that the command skipped over the handler.
370
1df4399f
PA
371proc 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"
e5f8a7cc 376 } else {
1df4399f 377 gdb_test "$cmd" "done = 0.*" "performing $cmd"
e5f8a7cc
PA
378 }
379}
2dedb159 380
b04e311d
AC
381# Try stepping when there's a signal pending but no breakpoints.
382# Should skip the handler advancing to the next line.
383
1df4399f 384proc skip_over_handler { cmd } {
b04e311d 385 global infinite_loop
1df4399f
PA
386 global clear_done
387
388 with_test_prefix "$cmd over handler" {
389 restart
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"
393
394 gdb_test "break $clear_done" ".*" "break clear done"
395
396 # Advance to the infinite loop.
397 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
b04e311d 398
1df4399f
PA
399 # Make the signal pending.
400 sleep 1
e5f8a7cc 401
1df4399f
PA
402 test_skip_handler $cmd
403 }
b04e311d
AC
404}
405
e5f8a7cc
PA
406foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
407 skip_over_handler $cmd
408}
b04e311d
AC
409
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.
413
1df4399f 414proc breakpoint_to_handler { cmd } {
b04e311d 415 global infinite_loop
1df4399f
PA
416
417 with_test_prefix "$cmd on breakpoint, to handler" {
418 restart
419 # Use the real-time itimer, as otherwize the process never gets
420 # enough time to expire the timer.
421 gdb_test_no_output "set itimer = itimer_real"
422
423 gdb_test "break $infinite_loop" ".*" "break infinite loop"
424 gdb_test "break handler" ".*" "break handler"
425
426 # Continue to the infinite loop.
427 gdb_test "continue" "while ..done.*" "continue to infinite loop"
428
429 # Make the signal pending.
430 sleep 1
431
432 gdb_test "$cmd" " handler .*" "performing $cmd"
b04e311d 433 }
b04e311d
AC
434}
435
1df4399f
PA
436foreach cmd {"step" "next" "continue"} {
437 breakpoint_to_handler $cmd
438}
b04e311d 439
2dedb159
AC
440# Try stepping when there's a signal pending, and a breakpoint at the
441# handler's entry instruction and a breakpoint at the current
442# instruction. Should step into the signal handler and breakpoint at
443# that entry instruction.
444
445# Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
446# signal, resume the process at the first instruction of the signal
447# handler and not the first instruction of the signal trampoline. The
448# stack is constructed such that the signal handler still appears to
449# have been called by the trampoline code. This test checks that it
450# is possible to stop the inferior, even at that first instruction.
451
1df4399f 452proc breakpoint_to_handler_entry { cmd } {
2dedb159 453 global infinite_loop
1df4399f
PA
454
455 with_test_prefix "$cmd on breakpoint, to handler entry" {
456 restart
457 # Use the real-time itimer, as otherwize the process never gets
458 # enough time to expire the timer.
459 gdb_test_no_output "set itimer = itimer_real"
460
461 gdb_test "break $infinite_loop" ".*" "break infinite loop"
462 gdb_test "break *handler" ".*" "break handler"
463
464 # Continue to the infinite loop.
465 gdb_test "continue" "while ..done.*" "continue to infinite loop"
466
467 # Make the signal pending.
468 sleep 1
469
470 gdb_test "$cmd" " handler .*" "performing $cmd"
2dedb159 471 }
2dedb159
AC
472}
473
1df4399f
PA
474foreach cmd {"step" "next" "continue"} {
475 breakpoint_to_handler_entry $cmd
476}
2dedb159 477
b04e311d
AC
478# Try stepping when there's a signal pending, and a pre-existing
479# breakpoint at the current instruction, and no breakpoint in the
7f5ef605
PA
480# handler. Should advance to the next line/instruction. If SW_WATCH
481# is true, set a software watchpoint, which exercises stepping the
482# breakpoint instruction while delivering a signal at the same time.
483# If NO_HANDLER, arrange for the signal's handler be SIG_IGN, thus
484# when the software watchpoint is also set, testing stepping a
485# breakpoint instruction and immediately triggering the breakpoint
486# (exercises adjust_pc_after_break logic).
487
488proc breakpoint_over_handler { cmd with_sw_watch no_handler } {
b04e311d 489 global infinite_loop
1df4399f
PA
490 global clear_done
491
7f5ef605
PA
492 set prefix "$cmd on breakpoint, skip handler"
493 if { $with_sw_watch } {
494 append prefix ", with sw-watchpoint"
495 }
496 if { $no_handler } {
497 append prefix ", no handler"
498 }
499
500 with_test_prefix "$prefix" {
1df4399f
PA
501 restart
502 # Use the real-time itimer, as otherwize the process never gets
503 # enough time to expire the timer.
504 gdb_test_no_output "set itimer = itimer_real"
505
7f5ef605
PA
506 if {$no_handler} {
507 gdb_test "print no_handler = 1" " = 1" \
508 "set no_handler"
509 }
510
1df4399f 511 gdb_test "break $infinite_loop" ".*" "break infinite loop"
b04e311d 512
1df4399f 513 gdb_test "break $clear_done" ".*" "break clear done"
b04e311d 514
1df4399f
PA
515 # Continue to the infinite loop
516 gdb_test "continue" "while ..done.*" "continue to infinite loop"
517
518 # Make the signal pending
519 sleep 1
520
7f5ef605
PA
521 if { $with_sw_watch } {
522 # A watchpoint on a convenience variable is always a
523 # software watchpoint.
524 gdb_test "watch \$convenience" "Watchpoint .*: \\\$convenience"
525 }
526
527 if {$no_handler} {
528 # With no handler, we need to set the global ourselves
529 # manually.
530 gdb_test "print done = 1" " = 1" "set done"
531 }
532
1df4399f
PA
533 test_skip_handler $cmd
534 }
b04e311d
AC
535}
536
e5f8a7cc 537foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
7f5ef605
PA
538 foreach with_sw_watch {0 1} {
539 foreach no_handler {0 1} {
540 breakpoint_over_handler $cmd $with_sw_watch $no_handler
541 }
542 }
e5f8a7cc 543}
This page took 1.262921 seconds and 4 git commands to generate.