1# Copyright 2004-2013 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
24if [target_info exists gdb,nosignals] {
25    verbose "Skipping sigstep.exp because of nosignals."
26    continue
27}
28
29
30set testfile sigstep
31set srcfile ${testfile}.c
32set binfile ${objdir}/${subdir}/${testfile}
33if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
34    untested "Couldn't compile ${srcfile}.c"
35    return -1
36}
37
38# get things started
39gdb_exit
40gdb_start
41gdb_reinitialize_dir $srcdir/$subdir
42gdb_load ${binfile}
43
44gdb_test "display/i \$pc"
45
46# Advance to main
47if { ![runto_main] } then {
48    gdb_suppress_tests;
49}
50
51# Pass all the alarms straight through (but verbosely)
52# gdb_test "handle SIGALRM print pass nostop"
53# gdb_test "handle SIGVTALRM print pass nostop"
54# gdb_test "handle SIGPROF print pass nostop"
55
56# Run to the signal handler, validate the backtrace.
57gdb_test "break handler"
58gdb_test "continue" ".* handler .*" "continue to stepi handler"
59gdb_test_sequence "bt" "backtrace for nexti" {
60    "\[\r\n\]+.0 \[^\r\n\]* handler "
61    "\[\r\n\]+.1  .signal handler called."
62    "\[\r\n\]+.2 \[^\r\n\]* main "
63}
64
65proc advance { i } {
66    global gdb_prompt inferior_exited_re
67    set prefix "$i from handler"
68
69    # Get us back into the handler
70    gdb_test "continue" ".* handler .*" "$prefix; continue to handler"
71
72    set test "$prefix; leave handler"
73    gdb_test_multiple "$i" "${test}" {
74	-re "Could not insert single-step breakpoint.*$gdb_prompt $" {
75	    setup_kfail gdb/1736 "sparc*-*-openbsd*"
76	    fail "$test (could not insert single-step breakpoint)"
77	}
78	-re "done = 1;.*${gdb_prompt} $" {
79	    send_gdb "$i\n"
80	    exp_continue -continue_timer
81	}
82	-re "\} .. handler .*${gdb_prompt} $" {
83	    send_gdb "$i\n"
84	    exp_continue -continue_timer
85	}
86	-re "$inferior_exited_re normally.*${gdb_prompt} $" {
87	    setup_kfail gdb/1639 powerpc-*-*bsd*
88	    fail "$test (program exited)"
89	}
90	-re "(while ..done|done = 0).*${gdb_prompt} $" {
91	    # After stepping out of a function /r signal-handler, GDB will
92	    # advance the inferior until it is at the first instruction of
93	    # a code-line.  While typically things return to the middle of
94	    # the "while..." (and hence GDB advances the inferior to the
95	    # "return..." line) it is also possible for the return to land
96	    # on the first instruction of "while...".  Accept both cases.
97	    pass "$test"
98	}
99    }
100}
101
102proc advancei { i } {
103    global gdb_prompt inferior_exited_re
104    set prefix "$i from handleri"
105    set program_exited 0
106
107    # Get us back into the handler
108    gdb_test "continue" ".* handler .*" "$prefix; continue to handler"
109
110    set test "$prefix; leave handler"
111    gdb_test_multiple "$i" "${test}" {
112        -re "Cannot insert breakpoint 0.*${gdb_prompt} $" {
113            # Some platforms use a special read-only page for signal
114            # trampolines.  We can't set a breakpoint there, and we
115            # don't gracefully fall back to single-stepping.
116            setup_kfail gdb/1736 "i?86-*-linux*"
117	    setup_kfail gdb/1736 "*-*-openbsd*"
118            fail "$test (could not set breakpoint)"
119	    return
120        }
121	-re "Could not insert single-step breakpoint.*$gdb_prompt $" {
122	    setup_kfail gdb/1736 "sparc*-*-openbsd*"
123	    fail "$test (could not insert single-step breakpoint)"
124	}
125	-re "Breakpoint \[0-9\]*, handler .*${gdb_prompt} $" {
126	    fail "$test (hit breakpoint again)"
127	}
128	-re "done = 1;.*${gdb_prompt} $" {
129	    send_gdb "$i\n"
130	    exp_continue -continue_timer
131	}
132	-re "\} .. handler .*${gdb_prompt} $" {
133	    send_gdb "$i\n"
134	    exp_continue -continue_timer
135	}
136	-re "signal handler called.*${gdb_prompt} $" {
137	    pass "$test"
138	}
139	-re "main .*${gdb_prompt} $" {
140	    fail "$test (in main)"
141	}
142	-re "$inferior_exited_re normally.*${gdb_prompt} $" {
143	    fail "$test (program exited)"
144	    set program_exited 1
145	}
146	-re "Make handler return now.*y or n. $" {
147	    send_gdb "y\n"
148	    exp_continue -continue_timer
149	}
150    }
151
152    set test "$prefix; leave signal trampoline"
153    gdb_test_multiple "$i" "${test}" {
154	-re "while .*${gdb_prompt} $" {
155	    pass "$test (in main)"
156	}
157	-re "signal handler called.*${gdb_prompt} $" {
158	    send_gdb "$i\n"
159	    exp_continue -continue_timer
160	}
161	-re "return .*${gdb_prompt} $" {
162	    fail "$test (stepped)"
163	}
164	-re "Make .*frame return now.*y or n. $" {
165	    send_gdb "y\n"
166	    exp_continue -continue_timer
167	}
168	-re "$inferior_exited_re normally.*${gdb_prompt} $" {
169	    kfail gdb/1639 "$test (program exited)"
170	    set program_exited 1
171	}
172	-re "The program is not being run.*${gdb_prompt} $" {
173	    if { $program_exited } {
174		# Previously kfailed with an exit
175		pass "$test (the program is not being run)"
176	    } else {
177		fail "$test (the program is not being run)"
178	    }
179	}
180    }
181}
182
183# Check that we can step/next our way out of a signal handler.
184
185advance step
186advancei stepi
187
188advance next
189advancei nexti
190
191advancei finish
192advancei return
193gdb_test_no_output "set done = 1" "Set done as return will have skipped it"
194
195
196# Check that we can step/next our way into / over a signal handler.
197
198# There are at least the following cases: breakpoint @pc VS breakpoint
199# in handler VS step / next / continue.
200
201# Use the real-time itimer, as otherwize the process never gets enough
202# time to expire the timer.
203
204delete_breakpoints
205set infinite_loop [gdb_get_line_number {while (!done)}]
206gdb_test_no_output "set itimer = itimer_real"
207gdb_test "break [gdb_get_line_number {done = 0}]"
208
209# Try stepping when there's a signal pending, and a breakpoint at the
210# handler.  Should step into the signal handler.
211
212proc skip_to_handler { i } {
213    global gdb_prompt
214    global infinite_loop
215    set prefix "$i to handler"
216
217    # Run around to the done
218    # You can add more patterns to this if you need them.
219    set test "$prefix; resync"
220    gdb_test_multiple "continue" "$test" {
221	-re "done = 0.*$gdb_prompt " {
222	    pass "$test"
223	}
224    }
225
226    # Advance to the infinite loop
227    gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
228
229    # Make the signal pending
230    sleep 1
231
232    # Insert / remove the handler breakpoint.
233    gdb_test "break handler" ".*" "$prefix; break handler"
234    gdb_test "$i" " handler .*" "$prefix; performing $i"
235    gdb_test "clear handler" ".*" "$prefix; clear handler"
236}
237
238skip_to_handler step
239skip_to_handler next
240skip_to_handler continue
241
242# Try stepping when there's a signal pending, and a breakpoint at the
243# handler's entry-point.  Should step into the signal handler stopping
244# at the entry-point.
245
246# Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
247# signal, resume the process at the first instruction of the signal
248# handler and not the first instruction of the signal trampoline.  The
249# stack is constructed such that the signal handler still appears to
250# have been called by the trampoline code.  This test checks that it
251# is possible to stop the inferior, even at that first instruction.
252
253proc skip_to_handler_entry { i } {
254    global gdb_prompt
255    global infinite_loop
256    set prefix "$i to handler entry"
257
258    # Run around to the done
259    # You can add more patterns to this if you need them.
260    set test "$prefix; resync"
261    gdb_test_multiple "continue" "$test" {
262	-re "done = 0.*$gdb_prompt " {
263	    pass "$test"
264	}
265    }
266
267    # Advance to the infinite loop
268    gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
269
270    # Make the signal pending
271    sleep 1
272
273    # Insert / remove the handler breakpoint.
274    gdb_test "break *handler" ".*" "$prefix; break handler"
275    gdb_test "$i" " handler .*" "$prefix; performing $i"
276    gdb_test "clear *handler" ".*" "$prefix; clear handler"
277}
278
279skip_to_handler_entry step
280skip_to_handler_entry next
281skip_to_handler_entry continue
282
283# Try stepping when there's a signal pending but no breakpoints.
284# Should skip the handler advancing to the next line.
285
286proc skip_over_handler { i } {
287    global gdb_prompt
288    global infinite_loop
289    set prefix "$i over handler"
290
291    # Run around to the done
292    # You can add more patterns to this if you need them.
293    set test "$prefix; resync"
294    gdb_test_multiple "continue" "$test" {
295	-re "done = 0.*$gdb_prompt " {
296	    pass "$test"
297	}
298    }
299
300    # Advance to the infinite loop
301    gdb_test "advance $infinite_loop" ".*" "$prefix; advance to infinite loop"
302
303    # Make the signal pending
304    sleep 1
305
306    gdb_test "$i" "done = 0.*" "$prefix; performing $i"
307}
308
309skip_over_handler step
310skip_over_handler next
311skip_over_handler continue
312
313# Try stepping when there's a signal pending, a pre-existing
314# breakpoint at the current instruction, and a breakpoint in the
315# handler.  Should advance to the signal handler.
316
317proc breakpoint_to_handler { i } {
318    global gdb_prompt
319    global infinite_loop
320    set prefix "$i on breakpoint, to handler"
321
322    # Run around to the done
323    # You can add more patterns to this if you need them.
324    set test "$prefix; resync"
325    gdb_test_multiple "continue" "$test" {
326	-re "done = 0.*$gdb_prompt " {
327	    pass "$test"
328	}
329    }
330
331    gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
332    gdb_test "break handler" ".*" "$prefix; break handler"
333
334    # Continue to the infinite loop
335    gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
336
337    # Make the signal pending
338    sleep 1
339
340    gdb_test "$i" " handler .*" "$prefix; performing $i"
341    gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
342    gdb_test "clear handler" ".*" "$prefix; clear handler"
343}
344
345breakpoint_to_handler step
346breakpoint_to_handler next
347breakpoint_to_handler continue
348
349# Try stepping when there's a signal pending, and a breakpoint at the
350# handler's entry instruction and a breakpoint at the current
351# instruction.  Should step into the signal handler and breakpoint at
352# that entry instruction.
353
354# Some systems (e.x., GNU/Linux as of 2004-08-30), when delivering a
355# signal, resume the process at the first instruction of the signal
356# handler and not the first instruction of the signal trampoline.  The
357# stack is constructed such that the signal handler still appears to
358# have been called by the trampoline code.  This test checks that it
359# is possible to stop the inferior, even at that first instruction.
360
361proc breakpoint_to_handler_entry { i } {
362    global gdb_prompt
363    global infinite_loop
364    set prefix "$i on breakpoint, to handler entry"
365
366    # Run around to the done
367    # You can add more patterns to this if you need them.
368    set test "$prefix; resync"
369    gdb_test_multiple "continue" "$test" {
370	-re "done = 0.*$gdb_prompt " {
371	    pass "$test"
372	}
373    }
374
375    gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
376    gdb_test "break *handler" ".*" "$prefix; break handler"
377
378    # Continue to the infinite loop
379    gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
380
381    # Make the signal pending
382    sleep 1
383
384    gdb_test "$i" " handler .*" "$prefix; performing $i"
385    gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
386    gdb_test "clear *handler" ".*" "$prefix; clear handler"
387}
388
389breakpoint_to_handler_entry step
390breakpoint_to_handler_entry next
391breakpoint_to_handler_entry continue
392
393# Try stepping when there's a signal pending, and a pre-existing
394# breakpoint at the current instruction, and no breakpoint in the
395# handler.  Should advance to the next line.
396
397proc breakpoint_over_handler { i } {
398    global gdb_prompt
399    global infinite_loop
400    set prefix "$i on breakpoint, skip handler"
401
402    # Run around to the done
403    # You can add more patterns to this if you need them.
404    set test "$prefix; resync"
405    gdb_test_multiple "continue" "$test" {
406	-re "done = 0.*$gdb_prompt " {
407	    pass "$test"
408	}
409    }
410
411    gdb_test "break $infinite_loop" ".*" "$prefix; break infinite loop"
412
413    # Continue to the infinite loop
414    gdb_test "continue" "while ..done.*" "$prefix; continue to infinite loop"
415
416    # Make the signal pending
417    sleep 1
418
419    gdb_test "$i" "done = 0.*" "$prefix; performing $i"
420    gdb_test "clear $infinite_loop" ".*" "$prefix; clear infinite loop"
421}
422
423breakpoint_over_handler step
424breakpoint_over_handler next
425breakpoint_over_handler continue
426