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 " {
167 setup_kfail "gdb/14766" *-*-*
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 "Couldn't get registers.*$gdb_prompt " {
373 setup_kfail "gdb/14766" *-*-*
376 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
377 setup_kfail "gdb/14762" *-*-*
380 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
383 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
388 # The parent has been detached; allow time for any output it might
389 # generate to arrive, so that output doesn't get confused with
390 # any expected debugger output from a subsequent testpoint.
395 proc do_vfork_and_follow_parent_tests {} {
398 # Try following the parent process by stepping through a call to
399 # vfork. Do this without catchpoints.
400 vfork_parent_follow_through_step
402 # Try following the parent process by setting a breakpoint on the
403 # other side of a vfork, and running to that point. Do this
404 # without catchpoints.
405 vfork_parent_follow_to_bp
407 # Try catching a vfork, and stepping out to the parent.
409 tcatch_vfork_then_parent_follow
412 proc do_vfork_and_follow_child_tests_exec {} {
413 # Try following the child process by just continuing through the
414 # vfork, and letting the parent's breakpoint on "main" be auto-
415 # magically reset in the child.
417 vfork_and_exec_child_follow_to_main_bp
419 # Try following the child process by stepping through a call to
420 # vfork. The child also executes an exec. Since the child cannot
421 # be debugged until after it has exec'd, and since there's a bp on
422 # "main" in the parent, and since the bp's for the parent are
423 # recomputed in the exec'd child, the step through a vfork should
424 # land us in the "main" for the exec'd child, too.
426 vfork_and_exec_child_follow_through_step
428 # Try catching a vfork, and stepping out to the child.
430 tcatch_vfork_then_child_follow_exec
432 # Test the ability to follow both child and parent of a vfork. Do
433 # this without catchpoints.
434 # ??rehrauer: NYI. Will add testpoints here when implemented.
437 # Test the ability to have the debugger ask the user at vfork-time
438 # whether to follow the parent, child or both. Do this without
440 # ??rehrauer: NYI. Will add testpoints here when implemented.
444 proc do_vfork_and_follow_child_tests_exit {} {
445 # Try following the child process by just continuing through the
446 # vfork, and letting the child exit.
448 vfork_child_follow_to_exit
450 # Try catching a vfork, and stepping out to the child.
452 tcatch_vfork_then_child_follow_exit
455 with_test_prefix "check vfork support" {
456 # Check that vfork catchpoints are supported, as an indicator for
457 # whether vfork-following is supported.
458 check_vfork_catchpoints
461 # Follow parent and follow child vfork tests with a child that execs.
462 with_test_prefix "exec" {
463 # These are tests of gdb's ability to follow the parent of a Unix
464 # vfork system call. The child will subsequently call a variant
465 # of the Unix exec system call.
466 do_vfork_and_follow_parent_tests
468 # These are tests of gdb's ability to follow the child of a Unix
469 # vfork system call. The child will subsequently call a variant
470 # of a Unix exec system call.
472 do_vfork_and_follow_child_tests_exec
475 # Switch to test the case of the child exiting. We can't use
476 # standard_testfile here because we don't want to overwrite the binary
477 # of the previous tests.
478 set testfile "foll-vfork-exit"
479 set srcfile ${testfile}.c
480 set binfile [standard_output_file ${testfile}]
482 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
483 untested "failed to build $testfile"
487 # Follow parent and follow child vfork tests with a child that exits.
488 with_test_prefix "exit" {
489 # These are tests of gdb's ability to follow the parent of a Unix
490 # vfork system call. The child will subsequently exit.
491 do_vfork_and_follow_parent_tests
493 # These are tests of gdb's ability to follow the child of a Unix
494 # vfork system call. The child will subsequently exit.
496 do_vfork_and_follow_child_tests_exit
499 set timeout $oldtimeout