PR gdb/12623: non-stop crashes inferior, PC adjustment and 1-byte insns
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / sigstep.exp
1 # Copyright 2004-2014 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
7 #
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.
12 #
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/>.
15
16
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.
20
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.
23
24 if [target_info exists gdb,nosignals] {
25 verbose "Skipping sigstep.exp because of nosignals."
26 continue
27 }
28
29
30 standard_testfile
31
32 if {[build_executable $testfile.exp $testfile $srcfile debug]} {
33 untested $testfile.exp
34 return -1
35 }
36
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"]
40
41 # Restart GDB, set a display showing $PC, and run to main.
42
43 proc restart {} {
44 global binfile
45
46 clean_restart $binfile
47
48 gdb_test "display/i \$pc"
49
50 runto_main
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.
59
60 proc 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 }
72 }
73
74 validate_backtrace
75
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
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
83 set prefix "$enter_cmd to handler, $in_handler_prefix in handler, $exit_cmd from handler"
84
85 with_test_prefix $prefix {
86 restart
87
88 # Get us into the handler
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 }
105
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)"
111 }
112 -re "done = 1;.*${gdb_prompt} $" {
113 send_gdb "$exit_cmd\n"
114 exp_continue -continue_timer
115 }
116 -re "\} .. handler .*${gdb_prompt} $" {
117 send_gdb "$exit_cmd\n"
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 }
133 }
134 }
135 }
136
137 # Map of PREFIX => "things to do within the signal handler", for the
138 # advance tests.
139
140 set 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"} {
158 setup_xfail "x86_64-*-linux*" gdb/17511
159 }
160 fail "$test (spurious SIGTRAP)"
161 return
162 }
163 -re "other handler location.*$gdb_prompt $" {
164 pass $test
165 }
166 }
167 }
168 }
169
170 # Check that we can step/next/continue, etc. our way in and out of a
171 # signal handler. Also test that we can step, and run to a breakpoint
172 # within the handler.
173
174 foreach enter_cmd { "stepi" "nexti" "step" "next" "continue" } {
175 if { $enter_cmd != "continue" && ![can_single_step_to_signal_handler] } {
176 continue
177 }
178
179 foreach exit_cmd { "step" "next" "continue" } {
180 foreach {in_handler_prefix in_handler} $in_handler_map {
181 advance $enter_cmd $in_handler_prefix $in_handler $exit_cmd
182 }
183 }
184 }
185
186 proc advancei { cmd } {
187 global gdb_prompt inferior_exited_re
188
189 with_test_prefix "$cmd from handleri" {
190 restart
191
192 # Get us into the handler.
193 gdb_test "break handler"
194 gdb_test "continue" ".* handler .*" "continue to handler"
195
196 set program_exited 0
197 set test "leave handler"
198 gdb_test_multiple "$cmd" "${test}" {
199 -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
200 # Some platforms use a special read-only page for signal
201 # trampolines. We can't set a breakpoint there, and we
202 # don't gracefully fall back to single-stepping.
203 setup_kfail gdb/8841 "i?86-*-linux*"
204 setup_kfail gdb/8841 "*-*-openbsd*"
205 fail "$test (could not set breakpoint)"
206 return
207 }
208 -re "Could not insert single-step breakpoint.*$gdb_prompt $" {
209 setup_kfail gdb/8841 "sparc*-*-openbsd*"
210 fail "$test (could not insert single-step breakpoint)"
211 }
212 -re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
213 fail "$test (hit breakpoint again)"
214 }
215 -re "done = 1;.*${gdb_prompt} $" {
216 send_gdb "$cmd\n"
217 exp_continue -continue_timer
218 }
219 -re "\} .. handler .*${gdb_prompt} $" {
220 send_gdb "$cmd\n"
221 exp_continue -continue_timer
222 }
223 -re "signal handler called.*${gdb_prompt} $" {
224 pass "$test"
225 }
226 -re "main .*${gdb_prompt} $" {
227 fail "$test (in main)"
228 }
229 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
230 fail "$test (program exited)"
231 set program_exited 1
232 }
233 -re "Make handler return now.*y or n. $" {
234 send_gdb "y\n"
235 exp_continue -continue_timer
236 }
237 }
238
239 set test "leave signal trampoline"
240 gdb_test_multiple "$cmd" "${test}" {
241 -re "while .*${gdb_prompt} $" {
242 pass "$test (in main)"
243 }
244 -re "signal handler called.*${gdb_prompt} $" {
245 send_gdb "$cmd\n"
246 exp_continue -continue_timer
247 }
248 -re "return .*${gdb_prompt} $" {
249 fail "$test (stepped)"
250 }
251 -re "Make .*frame return now.*y or n. $" {
252 send_gdb "y\n"
253 exp_continue -continue_timer
254 }
255 -re "$inferior_exited_re normally.*${gdb_prompt} $" {
256 kfail gdb/8744 "$test (program exited)"
257 set program_exited 1
258 }
259 -re "The program is not being run.*${gdb_prompt} $" {
260 if { $program_exited } {
261 # Previously kfailed with an exit
262 pass "$test (the program is not being run)"
263 } else {
264 fail "$test (the program is not being run)"
265 }
266 }
267 }
268 }
269 }
270
271 # Check that we can step our way out of a signal handler, using
272 # commands that first step out to the signal trampoline, and then out
273 # to the mainline code.
274
275 foreach cmd {"stepi" "nexti" "finish" "return"} {
276 advancei $cmd
277 }
278
279 # Check that we can step/next our way into / over a signal handler.
280
281 # There are at least the following cases: breakpoint @pc VS breakpoint
282 # in handler VS step / next / continue.
283
284
285 # Try stepping when there's a signal pending, and a breakpoint at the
286 # handler. Should step into the signal handler.
287
288 proc skip_to_handler { cmd } {
289 global infinite_loop
290
291 with_test_prefix "$cmd to handler" {
292 restart
293 # Use the real-time itimer, as otherwize the process never gets
294 # enough time to expire the timer.
295 gdb_test_no_output "set itimer = itimer_real"
296
297 # Advance to the infinite loop.
298 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
299
300 # Make the signal pending.
301 sleep 1
302
303 # Insert the handler breakpoint.
304 gdb_test "break handler" ".*" "break handler"
305
306 # Step into the handler.
307 gdb_test "$cmd" " handler .*" "performing $cmd"
308 }
309 }
310
311 foreach cmd {"step" "next" "continue"} {
312 skip_to_handler $cmd
313 }
314
315 # Try stepping when there's a signal pending, and a breakpoint at the
316 # handler's entry-point. Should step into the signal handler stopping
317 # at the entry-point.
318
319 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
320 # signal, resume the process at the first instruction of the signal
321 # handler and not the first instruction of the signal trampoline. The
322 # stack is constructed such that the signal handler still appears to
323 # have been called by the trampoline code. This test checks that it
324 # is possible to stop the inferior, even at that first instruction.
325
326 proc skip_to_handler_entry { cmd } {
327 global infinite_loop
328
329 with_test_prefix "$cmd to handler entry" {
330 restart
331 # Use the real-time itimer, as otherwize the process never gets
332 # enough time to expire the timer.
333 gdb_test_no_output "set itimer = itimer_real"
334
335 # Advance to the infinite loop.
336 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
337
338 # Make the signal pending.
339 sleep 1
340
341 # Insert / remove the handler breakpoint.
342 gdb_test "break *handler" ".*" "break handler"
343 gdb_test "$cmd" " handler .*" "performing $cmd"
344 }
345 }
346
347 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
348 skip_to_handler_entry $cmd
349 }
350
351 # Get the address of where a single-step should land.
352
353 proc get_next_pc {test} {
354 global gdb_prompt
355 global hex
356
357 set next ""
358 gdb_test_multiple "x/2i \$pc" $test {
359 -re "$hex .*:\[^\r\n\]+\r\n\[ \]+($hex).*\.\r\n$gdb_prompt $" {
360 set next $expect_out(1,string)
361 pass $test
362 }
363 }
364
365 return $next
366 }
367
368 # Test that the command skipped over the handler.
369
370 proc test_skip_handler {cmd} {
371 if {$cmd == "stepi" || $cmd == "nexti"} {
372 set next_pc [get_next_pc "get next PC"]
373 gdb_test "$cmd" "dummy = 0.*" "performing $cmd"
374 gdb_test "p /x \$pc" " = $next_pc" "advanced"
375 } else {
376 gdb_test "$cmd" "done = 0.*" "performing $cmd"
377 }
378 }
379
380 # Try stepping when there's a signal pending but no breakpoints.
381 # Should skip the handler advancing to the next line.
382
383 proc skip_over_handler { cmd } {
384 global infinite_loop
385 global clear_done
386
387 with_test_prefix "$cmd over handler" {
388 restart
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"
392
393 gdb_test "break $clear_done" ".*" "break clear done"
394
395 # Advance to the infinite loop.
396 gdb_test "advance $infinite_loop" ".*" "advance to infinite loop"
397
398 # Make the signal pending.
399 sleep 1
400
401 test_skip_handler $cmd
402 }
403 }
404
405 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
406 skip_over_handler $cmd
407 }
408
409 # Try stepping when there's a signal pending, a pre-existing
410 # breakpoint at the current instruction, and a breakpoint in the
411 # handler. Should advance to the signal handler.
412
413 proc breakpoint_to_handler { cmd } {
414 global infinite_loop
415
416 with_test_prefix "$cmd on breakpoint, to handler" {
417 restart
418 # Use the real-time itimer, as otherwize the process never gets
419 # enough time to expire the timer.
420 gdb_test_no_output "set itimer = itimer_real"
421
422 gdb_test "break $infinite_loop" ".*" "break infinite loop"
423 gdb_test "break handler" ".*" "break handler"
424
425 # Continue to the infinite loop.
426 gdb_test "continue" "while ..done.*" "continue to infinite loop"
427
428 # Make the signal pending.
429 sleep 1
430
431 gdb_test "$cmd" " handler .*" "performing $cmd"
432 }
433 }
434
435 foreach cmd {"step" "next" "continue"} {
436 breakpoint_to_handler $cmd
437 }
438
439 # Try stepping when there's a signal pending, and a breakpoint at the
440 # handler's entry instruction and a breakpoint at the current
441 # instruction. Should step into the signal handler and breakpoint at
442 # that entry instruction.
443
444 # Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
445 # signal, resume the process at the first instruction of the signal
446 # handler and not the first instruction of the signal trampoline. The
447 # stack is constructed such that the signal handler still appears to
448 # have been called by the trampoline code. This test checks that it
449 # is possible to stop the inferior, even at that first instruction.
450
451 proc breakpoint_to_handler_entry { cmd } {
452 global infinite_loop
453
454 with_test_prefix "$cmd on breakpoint, to handler entry" {
455 restart
456 # Use the real-time itimer, as otherwize the process never gets
457 # enough time to expire the timer.
458 gdb_test_no_output "set itimer = itimer_real"
459
460 gdb_test "break $infinite_loop" ".*" "break infinite loop"
461 gdb_test "break *handler" ".*" "break handler"
462
463 # Continue to the infinite loop.
464 gdb_test "continue" "while ..done.*" "continue to infinite loop"
465
466 # Make the signal pending.
467 sleep 1
468
469 gdb_test "$cmd" " handler .*" "performing $cmd"
470 }
471 }
472
473 foreach cmd {"step" "next" "continue"} {
474 breakpoint_to_handler_entry $cmd
475 }
476
477 # Try stepping when there's a signal pending, and a pre-existing
478 # breakpoint at the current instruction, and no breakpoint in the
479 # handler. Should advance to the next line/instruction. If SW_WATCH
480 # is true, set a software watchpoint, which exercises stepping the
481 # breakpoint instruction while delivering a signal at the same time.
482 # If NO_HANDLER, arrange for the signal's handler be SIG_IGN, thus
483 # when the software watchpoint is also set, testing stepping a
484 # breakpoint instruction and immediately triggering the breakpoint
485 # (exercises adjust_pc_after_break logic).
486
487 proc breakpoint_over_handler { cmd with_sw_watch no_handler } {
488 global infinite_loop
489 global clear_done
490
491 set prefix "$cmd on breakpoint, skip handler"
492 if { $with_sw_watch } {
493 append prefix ", with sw-watchpoint"
494 }
495 if { $no_handler } {
496 append prefix ", no handler"
497 }
498
499 with_test_prefix "$prefix" {
500 restart
501 # Use the real-time itimer, as otherwize the process never gets
502 # enough time to expire the timer.
503 gdb_test_no_output "set itimer = itimer_real"
504
505 if {$no_handler} {
506 gdb_test "print no_handler = 1" " = 1" \
507 "set no_handler"
508 }
509
510 gdb_test "break $infinite_loop" ".*" "break infinite loop"
511
512 gdb_test "break $clear_done" ".*" "break clear done"
513
514 # Continue to the infinite loop
515 gdb_test "continue" "while ..done.*" "continue to infinite loop"
516
517 # Make the signal pending
518 sleep 1
519
520 if { $with_sw_watch } {
521 # A watchpoint on a convenience variable is always a
522 # software watchpoint.
523 gdb_test "watch \$convenience" "Watchpoint .*: \\\$convenience"
524 }
525
526 if {$no_handler} {
527 # With no handler, we need to set the global ourselves
528 # manually.
529 gdb_test "print done = 1" " = 1" "set done"
530 }
531
532 test_skip_handler $cmd
533 }
534 }
535
536 foreach cmd {"stepi" "nexti" "step" "next" "continue"} {
537 foreach with_sw_watch {0 1} {
538 foreach no_handler {0 1} {
539 breakpoint_over_handler $cmd $with_sw_watch $no_handler
540 }
541 }
542 }
This page took 0.041808 seconds and 4 git commands to generate.