-# Copyright 1997, 1999, 2007, 2008, 2009, 2010
-# Free Software Foundation, Inc.
+# Copyright 1997-2017 Free Software Foundation, Inc.
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-if { [is_remote target] || ![isnative] } then {
- continue
-}
-
-
-global srcfile
-set testfile "foll-fork"
-set srcfile ${testfile}.c
-set binfile ${objdir}/${subdir}/${testfile}
-
-if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
- untested foll-fork.exp
- return -1
-}
-
-
-
# Until "set follow-fork-mode" and "catch fork" are implemented on
# other targets...
#
-if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
+if { ![istarget "*-*-linux*"] && ![istarget "*-*-openbsd*"] } then {
continue
}
+standard_testfile
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+ return -1
+}
+
proc check_fork_catchpoints {} {
global gdb_prompt
gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
set has_fork_catchpoints 0
gdb_test_multiple "continue" "continue to first fork catchpoint" {
- -re ".*Your system does not support fork catchpoints.*$gdb_prompt $" {
+ -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
unsupported "continue to first fork catchpoint"
}
-re ".*Catchpoint.*$gdb_prompt $" {
}
}
-proc default_fork_parent_follow {} {
+# Test follow-fork to ensure that the correct process is followed, that
+# the followed process stops where it is expected to stop, that processes
+# are detached (or not) as expected, and that the inferior list has the
+# expected contents after following the fork. WHO is the argument to
+# the 'set follow-fork-mode' command, DETACH is the argument to the
+# 'set detach-on-fork' command, and CMD is the GDB command used to
+# execute the program past the fork. If the value of WHO or DETACH is
+# 'default', the corresponding GDB command is skipped for that test.
+# The value of CMD must be either 'next 2' or 'continue'.
+proc test_follow_fork { who detach cmd } {
global gdb_prompt
+ global srcfile
+ global testfile
- gdb_test "show follow-fork" \
- "Debugger response to a program call of fork or vfork is \"parent\".*" \
- "default show parent follow, no catchpoints"
+ with_test_prefix "follow $who, detach $detach, command \"$cmd\"" {
- gdb_test "next 2" \
- "Detaching after fork from.*" \
- "default parent follow, no catchpoints"
+ # Start a new debugger session each time so defaults are legitimate.
+ clean_restart $testfile
- # The child has been detached; allow time for any output it might
- # generate to arrive, so that output doesn't get confused with
- # any expected debugger output from a subsequent testpoint.
- #
- exec sleep 1
-}
+ if ![runto_main] {
+ untested "could not run to main"
+ return -1
+ }
-proc explicit_fork_parent_follow {} {
- global gdb_prompt
+ # The "Detaching..." and "Attaching..." messages may be hidden by
+ # default.
+ gdb_test_no_output "set verbose"
- gdb_test_no_output "set follow-fork parent"
+ # Set follow-fork-mode if we aren't using the default.
+ if {$who == "default"} {
+ set who "parent"
+ } else {
+ gdb_test_no_output "set follow-fork $who"
+ }
- gdb_test "show follow-fork" \
- "Debugger response to a program call of fork or vfork is \"parent\"." \
- "explicit show parent follow, no catchpoints"
+ gdb_test "show follow-fork" \
+ "Debugger response to a program call of fork or vfork is \"$who\"." \
+ "show follow-fork"
- gdb_test "next 2" "Detaching after fork from.*" \
- "explicit parent follow, no catchpoints"
+ # Set detach-on-fork mode if we aren't using the default.
+ if {$detach == "default"} {
+ set detach "on"
+ } else {
+ gdb_test_no_output "set detach-on-fork $detach"
+ }
- # The child has been detached; allow time for any output it might
- # generate to arrive, so that output doesn't get confused with
- # any expected debugger output from a subsequent testpoint.
- #
- exec sleep 1
-}
+ gdb_test "show detach-on-fork" \
+ "Whether gdb will detach.* fork is $detach." \
+ "show detach-on-fork"
+
+ # Set a breakpoint after the fork if we aren't single-stepping
+ # past the fork.
+ if {$cmd == "continue"} {
+ set bp_after_fork [gdb_get_line_number "set breakpoint here"]
+ gdb_test "break ${srcfile}:$bp_after_fork" \
+ "Breakpoint.*, line $bp_after_fork.*" \
+ "set breakpoint after fork"
+ }
-proc explicit_fork_child_follow {} {
- global gdb_prompt
+ # Set up the output we expect to see after we run.
+ set expected_re ""
+ if {$who == "child"} {
+ set expected_re "Attaching after.* fork to.*"
+ if {$detach == "on"} {
+ append expected_re "Detaching after fork from .*"
+ }
+ append expected_re "set breakpoint here.*"
+ } elseif {$who == "parent" && $detach == "on"} {
+ set expected_re "Detaching after fork from .*set breakpoint here.*"
+ } else {
+ set expected_re ".*set breakpoint here.*"
+ }
- gdb_test_no_output "set follow-fork child"
+ # Test running past and following the fork, using the parameters
+ # set above.
+ gdb_test $cmd $expected_re "$cmd past fork"
- gdb_test "show follow-fork" \
- "Debugger response to a program call of fork or vfork is \"child\"." \
- "explicit show child follow, no catchpoints"
+ # Check that we have the inferiors arranged correctly after
+ # following the fork.
+ set resume_unfollowed 0
+ if {$who == "parent" && $detach == "on"} {
- gdb_test "next 2" "Attaching after.* fork to.*" \
- "explicit child follow, no catchpoints"
+ # Follow parent / detach child: the only inferior is the parent.
+ gdb_test "info inferiors" "\\* 1 .* process.*" \
+ "info inferiors"
- # The child has been detached; allow time for any output it might
- # generate to arrive, so that output doesn't get confused with
- # any gdb_expected debugger output from a subsequent testpoint.
- #
- exec sleep 1
+ } elseif {$who == "parent" && $detach == "off"} {
+
+ # Follow parent / keep child: two inferiors under debug, the
+ # parent is the current inferior.
+ gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*" \
+ "info inferiors"
+
+ gdb_test "inferior 2" "Switching to inferior 2 .*"
+ set resume_unfollowed 1
+
+ } elseif {$who == "child" && $detach == "on"} {
+
+ # Follow child / detach parent: the child is under debug and is
+ # the current inferior. The parent is listed but is not under
+ # debug.
+ gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*" \
+ "info inferiors"
+
+ } elseif {$who == "child" && $detach == "off"} {
+
+ # Follow child / keep parent: two inferiors under debug, the
+ # child is the current inferior.
+ gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*" \
+ "info inferiors"
+
+ gdb_test "inferior 1" "Switching to inferior 1 .*"
+ set resume_unfollowed 1
+ }
+
+ if {$resume_unfollowed == 1} {
+ if {$cmd == "next 2"} {
+
+ gdb_continue_to_end "continue unfollowed inferior to end"
+
+ } elseif {$cmd == "continue"} {
+
+ gdb_continue_to_breakpoint \
+ "continue unfollowed inferior to bp" \
+ ".* set breakpoint here.*"
+ }
+ }
+ }
}
proc catch_fork_child_follow {} {
set bp_after_fork [gdb_get_line_number "set breakpoint here"]
- gdb_test "catch fork" "Catchpoint .*(fork).*" \
+ gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
"explicit child follow, set catch fork"
# Verify that the catchpoint is mentioned in an "info breakpoints",
}
gdb_test "continue" \
- "Catchpoint.*(forked process.*),.*in .*(fork|__kernel_v?syscall).*" \
+ "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
"explicit child follow, catch fork"
# Verify that the catchpoint is mentioned in an "info breakpoints",
"Temporary breakpoint.*, line $bp_after_fork.*" \
"set follow-fork child, tbreak"
- gdb_test "continue" \
- "Attaching after.* fork to.* at .*$bp_after_fork.*" \
- "set follow-fork child, hit tbreak"
+ set expected_re "Attaching after.* fork to.*Detaching after fork from"
+ append expected_re ".* at .*$bp_after_fork.*"
+ gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
# The parent has been detached; allow time for any output it might
# generate to arrive, so that output doesn't get confused with
gdb_test "delete breakpoints" \
"" \
"set follow-fork child, cleanup" \
- "Delete all breakpoints.*$" \
+ "Delete all breakpoints. \\(y or n\\) $" \
"y"
}
"unpatch child, set catch fork"
gdb_test "continue" \
- "Catchpoint.*\\(forked process.*\\).*,.*in .*(fork|__kernel_v?syscall).*" \
+ "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
"unpatch child, catch fork"
# Delete all breakpoints and catchpoints.
set bp_after_fork [gdb_get_line_number "set breakpoint here"]
- gdb_test "catch fork" "Catchpoint .*(fork).*" \
+ gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
"explicit parent follow, set tcatch fork"
# ??rehrauer: I don't yet know how to get the id of the tcatch
# via this script, so that I can add a -do list to it. For now,
# do the follow stuff after the catch happens.
- gdb_test "continue" "in .*(fork|__kernel_v?syscall).*" \
+ gdb_test "continue" \
+ "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
"explicit parent follow, tcatch fork"
gdb_test_no_output "set follow-fork parent"
gdb_test "delete breakpoints" \
"" \
"set follow-fork parent, cleanup" \
- "Delete all breakpoints.*$" \
+ "Delete all breakpoints. \\(y or n\\) $" \
"y"
}
proc do_fork_tests {} {
global gdb_prompt
+ global testfile
# Verify that help is available for "set follow-fork-mode".
#
# fork-following is supported.
if [runto_main] then { check_fork_catchpoints }
- # Test the default behaviour, which is to follow the parent of a
- # fork, and detach from the child. Do this without catchpoints.
- #
- if [runto_main] then { default_fork_parent_follow }
-
- # Test the ability to explicitly follow the parent of a fork, and
- # detach from the child. Do this without catchpoints.
+ # Test the basic follow-fork functionality using all combinations of
+ # values for follow-fork-mode and detach-on-fork, using either a
+ # breakpoint or single-step to execute past the fork.
#
- if [runto_main] then { explicit_fork_parent_follow }
+ # The first loop should be sufficient to test the defaults. There
+ # is no need to test using the defaults in other permutations (e.g.
+ # "default" "on", "parent" "default", etc.).
+ foreach cmd {"next 2" "continue"} {
+ test_follow_fork "default" "default" $cmd
+ }
- # Test the ability to follow the child of a fork, and detach from
- # the parent. Do this without catchpoints.
- #
- if [runto_main] then { explicit_fork_child_follow }
+ # Now test all explicit permutations.
+ foreach who {"parent" "child"} {
+ foreach detach {"on" "off"} {
+ foreach cmd {"next 2" "continue"} {
+ test_follow_fork $who $detach $cmd
+ }
+ }
+ }
- # Test the ability to follow both child and parent of a fork. Do
- # this without catchpoints.
- # ??rehrauer: NYI. Will add testpoints here when implemented.
- #
+ # Catchpoint tests.
- # Test the ability to have the debugger ask the user at fork-time
- # whether to follow the parent, child or both. Do this without
- # catchpoints.
- # ??rehrauer: NYI. Will add testpoints here when implemented.
- #
+ # Restart to eliminate any effects of the follow-fork tests.
+ clean_restart $testfile
+ gdb_test_no_output "set verbose"
# Test the ability to catch a fork, specify that the child be
# followed, and continue. Make the catchpoint permanent.
if [runto_main] then { tcatch_fork_parent_follow }
}
-# Start with a fresh gdb
-
-gdb_exit
-gdb_start
-gdb_reinitialize_dir $srcdir/$subdir
-gdb_load ${binfile}
-
# The "Detaching..." and "Attaching..." messages may be hidden by
# default.
gdb_test_no_output "set verbose"