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