2013-09-20 Chung-Lin Tang <cltang@codesourcery.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-vfork.exp
1 # Copyright 1997-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 # Various tests of gdb's ability to follow the parent or child of a
17 # Unix vfork system call. A vfork parent is blocked until the child
18 # either execs or exits --- since those events take somewhat different
19 # code paths in GDB, both variants are exercised.
20
21 if { [is_remote target] || ![isnative] } then {
22 continue
23 }
24
25 # Until "set follow-fork-mode" and "catch vfork" are implemented on
26 # other targets...
27 #
28 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
29 continue
30 }
31
32 # Test to see if we are on an HP-UX 10.20 and if so,
33 # do not run these tests as catching vfork is disabled for
34 # 10.20.
35
36 if [istarget "hppa*-hp-hpux10.20"] then {
37 return 0
38 }
39
40 # NOTE drow/2002-12-06: I don't know what the referenced kernel problem
41 # is, but it appears to be fixed in recent HP/UX versions.
42
43 ##if [istarget "hppa2.0w-hp-hpux*"] {
44 ## warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
45 ## return 0
46 ##}
47
48 standard_testfile
49
50 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
51 untested "failed to compile $testfile"
52 return -1
53 }
54
55 set testfile2 "vforked-prog"
56 set srcfile2 ${testfile2}.c
57
58 if {[build_executable $testfile.exp $testfile2 $srcfile2 {debug}] == -1} {
59 untested "failed to compile $testfile2"
60 return -1
61 }
62
63 # A few of these tests require a little more time than the standard
64 # timeout allows.
65 set oldtimeout $timeout
66 set timeout [expr "$timeout + 10"]
67
68 # Start with a fresh GDB, with verbosity enabled, and run to main. On
69 # error, behave as "return", so we don't try to continue testing with
70 # a borked session.
71 proc setup_gdb {} {
72 global testfile
73
74 clean_restart $testfile
75
76 # The "Detaching..." and "Attaching..." messages may be hidden by
77 # default.
78 gdb_test_no_output "set verbose"
79
80 if ![runto_main] {
81 return -code return
82 }
83 }
84
85 proc check_vfork_catchpoints {} {
86 global gdb_prompt
87 global has_vfork_catchpoints
88
89 setup_gdb
90
91 # Verify that the system supports "catch vfork".
92 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
93 set has_vfork_catchpoints 0
94 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
95 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
96 unsupported "continue to first vfork catchpoint"
97 }
98 -re ".*Catchpoint.*$gdb_prompt $" {
99 set has_vfork_catchpoints 1
100 pass "continue to first vfork catchpoint"
101 }
102 }
103
104 if {$has_vfork_catchpoints == 0} {
105 unsupported "vfork catchpoints"
106 return -code return
107 }
108 }
109
110 proc vfork_parent_follow_through_step {} {
111 with_test_prefix "vfork parent follow, through step" {
112 global gdb_prompt
113
114 setup_gdb
115
116 gdb_test_no_output "set follow-fork parent"
117
118 set test "step"
119 gdb_test_multiple "next" $test {
120 -re "Detaching after fork from.*if \\(pid == 0\\).*$gdb_prompt " {
121 pass $test
122 }
123 }
124 # The child has been detached; allow time for any output it might
125 # generate to arrive, so that output doesn't get confused with
126 # any gdb_expected debugger output from a subsequent testpoint.
127 #
128 exec sleep 1
129 }}
130
131 proc vfork_parent_follow_to_bp {} {
132 with_test_prefix "vfork parent follow, to bp" {
133 global gdb_prompt
134 global srcfile
135
136 setup_gdb
137
138 gdb_test_no_output "set follow-fork parent"
139
140 set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"]
141 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
142
143 set test "continue to bp"
144 gdb_test_multiple "continue" $test {
145 -re ".*Detaching after fork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
146 pass $test
147 }
148 }
149 # The child has been detached; allow time for any output it might
150 # generate to arrive, so that output doesn't get confused with
151 # any expected debugger output from a subsequent testpoint.
152 #
153 exec sleep 1
154 }}
155
156 proc vfork_child_follow_to_exit {} {
157 with_test_prefix "vfork child follow, to exit" {
158 global gdb_prompt
159
160 setup_gdb
161
162 gdb_test_no_output "set follow-fork child"
163
164 set test "continue to child exit"
165 gdb_test_multiple "continue" $test {
166 -re "Couldn't get registers.*$gdb_prompt " {
167 # PR gdb/14766
168 fail "$test"
169 }
170 -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
171 pass $test
172 }
173 }
174 # The parent has been detached; allow time for any output it might
175 # generate to arrive, so that output doesn't get confused with
176 # any gdb_expected debugger output from a subsequent testpoint.
177 #
178 exec sleep 1
179 }}
180
181 proc vfork_and_exec_child_follow_to_main_bp {} {
182 with_test_prefix "vfork and exec child follow, to main bp" {
183 global gdb_prompt
184 global srcfile2
185
186 setup_gdb
187
188 gdb_test_no_output "set follow-fork child"
189
190 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
191
192 set test "continue to bp"
193 gdb_test_multiple "continue" $test {
194 -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
195 pass $test
196 }
197 }
198 # The parent has been detached; allow time for any output it might
199 # generate to arrive, so that output doesn't get confused with
200 # any gdb_expected debugger output from a subsequent testpoint.
201 #
202 exec sleep 1
203 }}
204
205 proc vfork_and_exec_child_follow_through_step {} {
206 with_test_prefix "vfork and exec child follow, through step" {
207 global gdb_prompt
208 global srcfile2
209
210 if { [istarget "hppa*-*-hpux*"] && ![istarget "hppa*-*-hpux11.*"] } {
211 # This test cannot be performed prior to HP-UX 10.30, because
212 # ptrace-based debugging of a vforking program basically doesn't
213 # allow the child to do things like hit a breakpoint between a
214 # vfork and exec. This means that saying "set follow-fork
215 # child; next" at a vfork() call won't work, because the
216 # implementation of "next" sets a "step resume" breakpoint at
217 # the return from the vfork(), which the child will hit on its
218 # way to exec'ing.
219 #
220 verbose "vfork child-following next test ignored for pre-HP/UX-10.30 targets."
221 return 0
222 }
223
224 setup_gdb
225
226 gdb_test_no_output "set follow-fork child"
227
228 set test "step over vfork"
229 if { [istarget "hppa*-*-hpux*"]} {
230 # Since the child cannot be debugged until after it has exec'd,
231 # and since there's a bp on "main" in the parent, and since the
232 # bp's for the parent are recomputed in the exec'd child, the
233 # step through a vfork should land us in the "main" for the
234 # exec'd child, too.
235 #
236 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
237 gdb_test_multiple "next" $test {
238 -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
239 pass "$test"
240 }
241 }
242 } else {
243 # The ideal support is to be able to debug the child even
244 # before it execs. Thus, "next" lands on the next line after
245 # the vfork.
246 gdb_test_multiple "next" $test {
247 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
248 pass "$test"
249 }
250 }
251 }
252 # The parent has been detached; allow time for any output it might
253 # generate to arrive, so that output doesn't get confused with
254 # any expected debugger output from a subsequent testpoint.
255 #
256 exec sleep 1
257 }}
258
259 proc tcatch_vfork_then_parent_follow {} {
260 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
261 global gdb_prompt
262 global srcfile
263
264 setup_gdb
265
266 gdb_test_no_output "set follow-fork parent"
267
268 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
269
270 # HP-UX 10.20 seems to stop you in "vfork", while more recent
271 # HP-UXs stop you in "_vfork".
272 set test "continue to vfork"
273 gdb_test_multiple "continue" $test {
274 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
275 pass $test
276 }
277 -re "vfork \\(\\) at.*$gdb_prompt " {
278 pass $test
279 }
280 }
281
282 set linenum [gdb_get_line_number "pid = vfork ();"]
283 set test "finish"
284 gdb_test_multiple "finish" $test {
285 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
286 pass $test
287 }
288 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
289 send_gdb "finish\n"
290 exp_continue
291 }
292 }
293 # The child has been detached; allow time for any output it might
294 # generate to arrive, so that output doesn't get confused with
295 # any expected debugger output from a subsequent testpoint.
296 #
297 exec sleep 1
298 }}
299
300 proc tcatch_vfork_then_child_follow_exec {} {
301 with_test_prefix "vfork child follow, finish after tcatch vfork" {
302 global gdb_prompt
303 global srcfile
304 global srcfile2
305
306 setup_gdb
307
308 gdb_test_no_output "set follow-fork child"
309
310 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
311
312 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
313 # stop you in "_vfork".
314 set test "continue to vfork"
315 gdb_test_multiple "continue" $test {
316 -re "vfork \\(\\) at .*$gdb_prompt $" {
317 pass $test
318 }
319 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
320 pass $test
321 }
322 }
323
324 set linenum1 [gdb_get_line_number "pid = vfork ();"]
325 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
326
327 set test "finish"
328 gdb_test_multiple "finish" $test {
329 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
330 pass $test
331 }
332 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
333 send_gdb "finish\n"
334 exp_continue
335 }
336 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
337 pass "$test (followed exec)"
338 }
339 }
340 # The parent has been detached; allow time for any output it might
341 # generate to arrive, so that output doesn't get confused with
342 # any expected debugger output from a subsequent testpoint.
343 #
344 exec sleep 1
345 }}
346
347 proc tcatch_vfork_then_child_follow_exit {} {
348 with_test_prefix "vfork child follow, finish after tcatch vfork" {
349 global gdb_prompt
350 global srcfile
351
352 setup_gdb
353
354 gdb_test_no_output "set follow-fork child"
355
356 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
357
358 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
359 # stop you in "_vfork".
360 set test "continue to vfork"
361 gdb_test_multiple "continue" $test {
362 -re "vfork \\(\\) at .*$gdb_prompt $" {
363 pass $test
364 }
365 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
366 pass $test
367 }
368 }
369
370 set test "finish"
371 gdb_test_multiple "finish" $test {
372 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
373 setup_kfail "gdb/14762" *-*-*
374 fail $test
375 }
376 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
377 pass $test
378 }
379 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
380 send_gdb "finish\n"
381 exp_continue
382 }
383 }
384 # The parent has been detached; allow time for any output it might
385 # generate to arrive, so that output doesn't get confused with
386 # any expected debugger output from a subsequent testpoint.
387 #
388 exec sleep 1
389 }}
390
391 proc vfork_relations_in_info_inferiors { variant } {
392 with_test_prefix "vfork relations in info inferiors" {
393 global gdb_prompt
394
395 setup_gdb
396
397 gdb_test_no_output "set follow-fork child"
398
399 set test "step over vfork"
400 gdb_test_multiple "next" $test {
401 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
402 pass "$test"
403 }
404 }
405
406 gdb_test "info inferiors" \
407 ".*is vfork child of inferior 1.*is vfork parent of inferior 2" \
408 "info inferiors shows vfork parent/child relation"
409
410 if { $variant == "exec" } {
411 global srcfile2
412
413 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
414 set test "continue to bp"
415 gdb_test_multiple "continue" $test {
416 -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
417 pass $test
418 }
419 }
420 } else {
421 set test "continue to child exit"
422 gdb_test_multiple "continue" $test {
423 -re "exited normally.*$gdb_prompt " {
424 pass $test
425 }
426 }
427 }
428
429 set test "vfork relation no longer appears in info inferiors"
430 gdb_test_multiple "info inferiors" $test {
431 -re "is vfork child of inferior 1.*$gdb_prompt $" {
432 fail $test
433 }
434 -re "is vfork parent of inferior 2.*$gdb_prompt $" {
435 fail $test
436 }
437 -re "$gdb_prompt $" {
438 pass $test
439 }
440 }
441 }}
442
443 proc do_vfork_and_follow_parent_tests {} {
444 global gdb_prompt
445
446 # Try following the parent process by stepping through a call to
447 # vfork. Do this without catchpoints.
448 vfork_parent_follow_through_step
449
450 # Try following the parent process by setting a breakpoint on the
451 # other side of a vfork, and running to that point. Do this
452 # without catchpoints.
453 vfork_parent_follow_to_bp
454
455 # Try catching a vfork, and stepping out to the parent.
456 #
457 tcatch_vfork_then_parent_follow
458 }
459
460 proc do_vfork_and_follow_child_tests_exec {} {
461 # Try following the child process by just continuing through the
462 # vfork, and letting the parent's breakpoint on "main" be auto-
463 # magically reset in the child.
464 #
465 vfork_and_exec_child_follow_to_main_bp
466
467 # Try following the child process by stepping through a call to
468 # vfork. The child also executes an exec. Since the child cannot
469 # be debugged until after it has exec'd, and since there's a bp on
470 # "main" in the parent, and since the bp's for the parent are
471 # recomputed in the exec'd child, the step through a vfork should
472 # land us in the "main" for the exec'd child, too.
473 #
474 vfork_and_exec_child_follow_through_step
475
476 # Try catching a vfork, and stepping out to the child.
477 #
478 tcatch_vfork_then_child_follow_exec
479
480 # Test the ability to follow both child and parent of a vfork. Do
481 # this without catchpoints.
482 # ??rehrauer: NYI. Will add testpoints here when implemented.
483 #
484
485 # Test the ability to have the debugger ask the user at vfork-time
486 # whether to follow the parent, child or both. Do this without
487 # catchpoints.
488 # ??rehrauer: NYI. Will add testpoints here when implemented.
489 #
490
491 # Step over a vfork in the child, do "info inferiors" and check the
492 # parent/child relation is displayed. Run the child over the exec,
493 # and confirm the relation is no longer displayed in "info
494 # inferiors".
495 #
496 vfork_relations_in_info_inferiors "exec"
497 }
498
499 proc do_vfork_and_follow_child_tests_exit {} {
500 # Try following the child process by just continuing through the
501 # vfork, and letting the child exit.
502 #
503 vfork_child_follow_to_exit
504
505 # Try catching a vfork, and stepping out to the child.
506 #
507 tcatch_vfork_then_child_follow_exit
508
509 # Step over a vfork in the child, do "info inferiors" and check the
510 # parent/child relation is displayed. Run the child to completion,
511 # and confirm the relation is no longer displayed in "info
512 # inferiors".
513 #
514 vfork_relations_in_info_inferiors "exit"
515 }
516
517 with_test_prefix "check vfork support" {
518 # Check that vfork catchpoints are supported, as an indicator for
519 # whether vfork-following is supported.
520 check_vfork_catchpoints
521 }
522
523 # Follow parent and follow child vfork tests with a child that execs.
524 with_test_prefix "exec" {
525 # These are tests of gdb's ability to follow the parent of a Unix
526 # vfork system call. The child will subsequently call a variant
527 # of the Unix exec system call.
528 do_vfork_and_follow_parent_tests
529
530 # These are tests of gdb's ability to follow the child of a Unix
531 # vfork system call. The child will subsequently call a variant
532 # of a Unix exec system call.
533 #
534 do_vfork_and_follow_child_tests_exec
535 }
536
537 # Switch to test the case of the child exiting. We can't use
538 # standard_testfile here because we don't want to overwrite the binary
539 # of the previous tests.
540 set testfile "foll-vfork-exit"
541 set srcfile ${testfile}.c
542 set binfile [standard_output_file ${testfile}]
543
544 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
545 untested "failed to build $testfile"
546 return
547 }
548
549 # Follow parent and follow child vfork tests with a child that exits.
550 with_test_prefix "exit" {
551 # These are tests of gdb's ability to follow the parent of a Unix
552 # vfork system call. The child will subsequently exit.
553 do_vfork_and_follow_parent_tests
554
555 # These are tests of gdb's ability to follow the child of a Unix
556 # vfork system call. The child will subsequently exit.
557 #
558 do_vfork_and_follow_child_tests_exit
559 }
560
561 set timeout $oldtimeout
562 return 0
This page took 0.040483 seconds and 4 git commands to generate.