# Copyright 1997, 1999, 2007-2012 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 # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . if { [is_remote target] || ![isnative] } then { continue } # Until "set follow-fork-mode" and "catch vfork" are implemented on # other targets... # if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then { continue } # Test to see if we are on an HP-UX 10.20 and if so, # do not run these tests as catching vfork is disabled for # 10.20. if [istarget "hppa*-hp-hpux10.20"] then { return 0 } # NOTE drow/2002-12-06: I don't know what the referenced kernel problem # is, but it appears to be fixed in recent HP/UX versions. ##if [istarget "hppa2.0w-hp-hpux*"] { ## warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed." ## return 0 ##} standard_testfile if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} { untested "failed to compile $testfile" return -1 } set testfile2 "vforked-prog" set srcfile2 ${testfile2}.c if {[build_executable $testfile.exp $testfile2 $srcfile2 {debug}] == -1} { untested "failed to compile $testfile2" return -1 } # A few of these tests require a little more time than the standard # timeout allows. set oldtimeout $timeout set timeout [expr "$timeout + 10"] proc check_vfork_catchpoints {} { global gdb_prompt global has_vfork_catchpoints # Verify that the system supports "catch vfork". gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint" set has_vfork_catchpoints 0 gdb_test_multiple "continue" "continue to first vfork catchpoint" { -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { unsupported "continue to first vfork catchpoint" } -re ".*Catchpoint.*$gdb_prompt $" { set has_vfork_catchpoints 1 pass "continue to first vfork catchpoint" } } if {$has_vfork_catchpoints == 0} { unsupported "vfork catchpoints" return -code return } } proc vfork_parent_follow_through_step {} { global gdb_prompt gdb_test_no_output "set follow-fork parent" \ "set follow-fork parent, vfork through step" set test "vfork parent follow, through step" gdb_test_multiple "next" $test { -re "Detaching after fork from.*if \\(pid == 0\\).*$gdb_prompt " { pass "vfork parent follow, through step" } } # 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 } proc vfork_parent_follow_to_bp {} { global gdb_prompt global srcfile gdb_test_no_output "set follow-fork parent" \ "set follow-fork parent, vfork to bp" set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"] gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp" set test "vfork parent follow, to bp" gdb_test_multiple "continue" $test { -re ".*Detaching after fork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " { pass $test } } # 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 } # Kill child and reload symbols. proc kill_child {} { global binfile global gdb_prompt set test "killing inferior" gdb_test_multiple "kill" $test { -re ".*Kill the program being debugged.*y or n. $" { gdb_test_no_output "y" "" set test2 "file $binfile" gdb_test_multiple "file $binfile" $test2 { -re ".*Load new symbol table from.*y or n. $" { send_gdb "y\n" gdb_test_multiple "" "loading symbols" { -re "Reading symbols from.*$gdb_prompt $" { } } } -re ".*gdb_prompt $" { } } } -re ".*$gdb_prompt $" { } } } proc vfork_and_exec_child_follow_to_main_bp {} { global gdb_prompt global srcfile2 gdb_test_no_output \ "set follow-fork child" \ "set follow-fork child, vfork and exec to main bp" set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] set test "vfork and exec child follow, to main bp" gdb_test_multiple "continue" $test { -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " { pass $test } } # The parent 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 # Explicitly kill this child, or a subsequent rerun actually runs # the exec'd child, not the original program... kill_child } proc vfork_and_exec_child_follow_through_step {} { global gdb_prompt global srcfile2 if { [istarget "hppa*-*-hpux*"] && ![istarget "hppa*-*-hpux11.*"] } { # This test cannot be performed prior to HP-UX 10.30, because # ptrace-based debugging of a vforking program basically doesn't # allow the child to do things like hit a breakpoint between a # vfork and exec. This means that saying "set follow-fork # child; next" at a vfork() call won't work, because the # implementation of "next" sets a "step resume" breakpoint at # the return from the vfork(), which the child will hit on its # way to exec'ing. # verbose "vfork child-following next test ignored for pre-HP/UX-10.30 targets." return 0 } gdb_test_no_output \ "set follow-fork child" \ "set follow-fork child, vfork and exec through step" set test "vfork and exec child follow, through step" if { [istarget "hppa*-*-hpux*"]} { # Since the child cannot be debugged until after it has exec'd, # and since there's a bp on "main" in the parent, and since the # bp's for the parent are recomputed in the exec'd child, the # step through a vfork should land us in the "main" for the # exec'd child, too. # set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] gdb_test_multiple "next" $test { -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " { pass "$test" } } } else { # The ideal support is to be able to debug the child even # before it execs. Thus, "next" lands on the next line after # the vfork. gdb_test_multiple "next" $test { -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " { pass "$test" } } } # The parent 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 # Explicitly kill this child, or a subsequent rerun actually runs # the exec'd child, not the original program... kill_child } proc tcatch_vfork_then_parent_follow {} { global gdb_prompt global srcfile gdb_test_no_output \ "set follow-fork parent" \ "set follow-fork parent, tcatch vfork" gdb_test "tcatch vfork" \ "Catchpoint .*(vfork).*" \ "vfork parent follow, set tcatch vfork" # HP-UX 10.20 seems to stop you in "vfork", while more recent # HP-UXs stop you in "_vfork". set test "vfork parent follow, tcatch vfork" gdb_test_multiple "continue" $test { -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " { pass $test } -re "vfork \\(\\) at.*$gdb_prompt " { pass $test } } set linenum [gdb_get_line_number "pid = vfork ();"] set test "vfork parent follow, finish after tcatch vfork" gdb_test_multiple "finish" $test { -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " { pass $test } -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " { send_gdb "finish\n" exp_continue } } # 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 } proc tcatch_vfork_then_child_follow {} { global gdb_prompt global srcfile global srcfile2 gdb_test_no_output \ "set follow-fork child" \ "set follow-fork child, tcatch vfork" gdb_test "tcatch vfork" \ "Catchpoint .*(vfork).*" \ "vfork child follow, set tcatch vfork" # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs # stop you in "_vfork". set test "vfork child follow, tcatch vfork" gdb_test_multiple "continue" $test { -re "vfork \\(\\) at .*$gdb_prompt $" { pass $test } -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " { pass $test } } set linenum1 [gdb_get_line_number "pid = vfork ();"] set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] set test "vfork child follow, finish after tcatch vfork" gdb_test_multiple "finish" $test { -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " { pass $test } -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " { send_gdb "finish\n" exp_continue } -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " { pass "$test (followed exec)" } } # The parent 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 } proc do_vfork_and_exec_tests {} { global gdb_prompt # Check that vfork catchpoints are supported, as an indicator for whether # vfork-following is supported. if [runto_main] then { check_vfork_catchpoints } # Try following the parent process by stepping through a call to # vfork. Do this without catchpoints. if [runto_main] then { vfork_parent_follow_through_step } # Try following the parent process by setting a breakpoint on the # other side of a vfork, and running to that point. Do this # without catchpoints. if [runto_main] then { vfork_parent_follow_to_bp } # Try following the child process by just continuing through the # vfork, and letting the parent's breakpoint on "main" be auto- # magically reset in the child. # if [runto_main] then { vfork_and_exec_child_follow_to_main_bp } # Try following the child process by stepping through a call to # vfork. The child also executes an exec. Since the child cannot # be debugged until after it has exec'd, and since there's a bp on # "main" in the parent, and since the bp's for the parent are # recomputed in the exec'd child, the step through a vfork should # land us in the "main" for the exec'd child, too. # if [runto_main] then { vfork_and_exec_child_follow_through_step } # Try catching a vfork, and stepping out to the parent. # if [runto_main] then { tcatch_vfork_then_parent_follow } # Try catching a vfork, and stepping out to the child. # if [runto_main] then { tcatch_vfork_then_child_follow } # Test the ability to follow both child and parent of a vfork. Do # this without catchpoints. # ??rehrauer: NYI. Will add testpoints here when implemented. # # Test the ability to have the debugger ask the user at vfork-time # whether to follow the parent, child or both. Do this without # catchpoints. # ??rehrauer: NYI. Will add testpoints here when implemented. # } # Start with a fresh gdb clean_restart $testfile # The "Detaching..." and "Attaching..." messages may be hidden by # default. gdb_test_no_output "set verbose" # This is a test of gdb's ability to follow the parent or child # of a Unix vfork() system call. (The child will subsequently # call a variant of a Unix exec() system call.) # do_vfork_and_exec_tests set timeout $oldtimeout return 0