1 # Copyright 1997, 1999, 2007-2012 Free Software Foundation, Inc.
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.
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.
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/>.
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.
21 if { [is_remote target] || ![isnative] } then {
25 # Until "set follow-fork-mode" and "catch vfork" are implemented on
28 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
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
36 if [istarget "hppa*-hp-hpux10.20"] then {
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.
43 ##if [istarget "hppa2.0w-hp-hpux*"] {
44 ## warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
50 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
51 untested "failed to compile $testfile"
55 set testfile2 "vforked-prog"
56 set srcfile2 ${testfile2}.c
58 if {[build_executable $testfile.exp $testfile2 $srcfile2 {debug}] == -1} {
59 untested "failed to compile $testfile2"
63 # A few of these tests require a little more time than the standard
65 set oldtimeout $timeout
66 set timeout [expr "$timeout + 10"]
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
74 clean_restart $testfile
76 # The "Detaching..." and "Attaching..." messages may be hidden by
78 gdb_test_no_output "set verbose"
85 proc check_vfork_catchpoints {} {
87 global has_vfork_catchpoints
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"
98 -re ".*Catchpoint.*$gdb_prompt $" {
99 set has_vfork_catchpoints 1
100 pass "continue to first vfork catchpoint"
104 if {$has_vfork_catchpoints == 0} {
105 unsupported "vfork catchpoints"
110 proc vfork_parent_follow_through_step {} {
111 with_test_prefix "vfork parent follow, through step" {
116 gdb_test_no_output "set follow-fork parent"
119 gdb_test_multiple "next" $test {
120 -re "Detaching after fork from.*if \\(pid == 0\\).*$gdb_prompt " {
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.
131 proc vfork_parent_follow_to_bp {} {
132 with_test_prefix "vfork parent follow, to bp" {
138 gdb_test_no_output "set follow-fork parent"
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"
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 " {
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.
156 proc vfork_child_follow_to_exit {} {
157 with_test_prefix "vfork child follow, to exit" {
162 gdb_test_no_output "set follow-fork child"
164 set test "continue to child exit"
165 gdb_test_multiple "continue" $test {
166 -re "Couldn't get registers.*$gdb_prompt " {
170 -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
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.
181 proc vfork_and_exec_child_follow_to_main_bp {} {
182 with_test_prefix "vfork and exec child follow, to main bp" {
188 gdb_test_no_output "set follow-fork child"
190 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
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 " {
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.
205 proc vfork_and_exec_child_follow_through_step {} {
206 with_test_prefix "vfork and exec child follow, through step" {
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
220 verbose "vfork child-following next test ignored for pre-HP/UX-10.30 targets."
226 gdb_test_no_output "set follow-fork child"
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
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 " {
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
246 gdb_test_multiple "next" $test {
247 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
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.
259 proc tcatch_vfork_then_parent_follow {} {
260 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
266 gdb_test_no_output "set follow-fork parent"
268 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
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 " {
277 -re "vfork \\(\\) at.*$gdb_prompt " {
282 set linenum [gdb_get_line_number "pid = vfork ();"]
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 " {
288 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
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.
300 proc tcatch_vfork_then_child_follow_exec {} {
301 with_test_prefix "vfork child follow, finish after tcatch vfork" {
308 gdb_test_no_output "set follow-fork child"
310 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
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 $" {
319 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
324 set linenum1 [gdb_get_line_number "pid = vfork ();"]
325 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
328 gdb_test_multiple "finish" $test {
329 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
332 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
336 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
337 pass "$test (followed exec)"
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.
347 proc tcatch_vfork_then_child_follow_exit {} {
348 with_test_prefix "vfork child follow, finish after tcatch vfork" {
354 gdb_test_no_output "set follow-fork child"
356 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
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 $" {
365 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
371 gdb_test_multiple "finish" $test {
372 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
373 setup_kfail "gdb/14762" *-*-*
376 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
379 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
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.
391 proc vfork_relations_in_info_inferiors { variant } {
392 with_test_prefix "vfork relations in info inferiors" {
397 gdb_test_no_output "set follow-fork child"
399 set test "step over vfork"
400 gdb_test_multiple "next" $test {
401 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
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"
410 if { $variant == "exec" } {
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 " {
421 set test "continue to child exit"
422 gdb_test_multiple "continue" $test {
423 -re "exited normally.*$gdb_prompt " {
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 $" {
434 -re "is vfork parent of inferior 2.*$gdb_prompt $" {
437 -re "$gdb_prompt $" {
443 proc do_vfork_and_follow_parent_tests {} {
446 # Try following the parent process by stepping through a call to
447 # vfork. Do this without catchpoints.
448 vfork_parent_follow_through_step
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
455 # Try catching a vfork, and stepping out to the parent.
457 tcatch_vfork_then_parent_follow
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.
465 vfork_and_exec_child_follow_to_main_bp
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.
474 vfork_and_exec_child_follow_through_step
476 # Try catching a vfork, and stepping out to the child.
478 tcatch_vfork_then_child_follow_exec
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.
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
488 # ??rehrauer: NYI. Will add testpoints here when implemented.
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
496 vfork_relations_in_info_inferiors "exec"
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.
503 vfork_child_follow_to_exit
505 # Try catching a vfork, and stepping out to the child.
507 tcatch_vfork_then_child_follow_exit
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
514 vfork_relations_in_info_inferiors "exit"
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
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
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.
534 do_vfork_and_follow_child_tests_exec
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}]
544 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
545 untested "failed to build $testfile"
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
555 # These are tests of gdb's ability to follow the child of a Unix
556 # vfork system call. The child will subsequently exit.
558 do_vfork_and_follow_child_tests_exit
561 set timeout $oldtimeout