1 # Copyright 1997-2015 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 # Until "set follow-fork-mode" and "catch vfork" are implemented on
24 if {![istarget "*-linux*"]} then {
30 set compile_options debug
31 set dirname [relative_filename [pwd] [file dirname $binfile]]
32 lappend compile_options "additional_flags=-DBASEDIR=\"$dirname\""
34 if {[build_executable $testfile.exp $testfile $srcfile $compile_options] == -1} {
35 untested "failed to compile $testfile"
39 set testfile2 "vforked-prog"
40 set srcfile2 ${testfile2}.c
42 if {[build_executable $testfile.exp $testfile2 $srcfile2 $compile_options] == -1} {
43 untested "failed to compile $testfile2"
47 # A few of these tests require a little more time than the standard
49 set oldtimeout $timeout
50 set timeout [expr "$timeout + 10"]
52 # Start with a fresh GDB, with verbosity enabled, and run to main. On
53 # error, behave as "return", so we don't try to continue testing with
58 clean_restart $testfile
60 # The "Detaching..." and "Attaching..." messages may be hidden by
62 gdb_test_no_output "set verbose"
69 proc check_vfork_catchpoints {} {
71 global has_vfork_catchpoints
75 # Verify that the system supports "catch vfork".
76 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
77 set has_vfork_catchpoints 0
78 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
79 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
80 unsupported "continue to first vfork catchpoint"
82 -re ".*Catchpoint.*$gdb_prompt $" {
83 set has_vfork_catchpoints 1
84 pass "continue to first vfork catchpoint"
88 if {$has_vfork_catchpoints == 0} {
89 unsupported "vfork catchpoints"
94 proc vfork_parent_follow_through_step {} {
95 with_test_prefix "vfork parent follow, through step" {
100 gdb_test_no_output "set follow-fork parent"
103 gdb_test_multiple "next" $test {
104 -re "Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " {
108 # The child has been detached; allow time for any output it might
109 # generate to arrive, so that output doesn't get confused with
110 # any gdb_expected debugger output from a subsequent testpoint.
115 proc vfork_parent_follow_to_bp {} {
116 with_test_prefix "vfork parent follow, to bp" {
122 gdb_test_no_output "set follow-fork parent"
124 set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"]
125 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
127 set test "continue to bp"
128 gdb_test_multiple "continue" $test {
129 -re ".*Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
133 # The child has been detached; allow time for any output it might
134 # generate to arrive, so that output doesn't get confused with
135 # any expected debugger output from a subsequent testpoint.
140 proc vfork_child_follow_to_exit {} {
141 with_test_prefix "vfork child follow, to exit" {
146 gdb_test_no_output "set follow-fork child"
148 set test "continue to child exit"
149 gdb_test_multiple "continue" $test {
150 -re "Couldn't get registers.*$gdb_prompt " {
154 -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
158 # The parent has been detached; allow time for any output it might
159 # generate to arrive, so that output doesn't get confused with
160 # any gdb_expected debugger output from a subsequent testpoint.
165 proc vfork_and_exec_child_follow_to_main_bp {} {
166 with_test_prefix "vfork and exec child follow, to main bp" {
172 gdb_test_no_output "set follow-fork child"
174 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
176 set test "continue to bp"
177 gdb_test_multiple "continue" $test {
178 -re "Attaching after.* vfork to.*Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
182 # The parent has been detached; allow time for any output it might
183 # generate to arrive, so that output doesn't get confused with
184 # any gdb_expected debugger output from a subsequent testpoint.
189 proc vfork_and_exec_child_follow_through_step {} {
190 with_test_prefix "vfork and exec child follow, through step" {
196 gdb_test_no_output "set follow-fork child"
198 set test "step over vfork"
200 # The ideal support is to be able to debug the child even
201 # before it execs. Thus, "next" lands on the next line after
203 gdb_test_multiple "next" $test {
204 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
208 # The parent has been detached; allow time for any output it might
209 # generate to arrive, so that output doesn't get confused with
210 # any expected debugger output from a subsequent testpoint.
215 proc tcatch_vfork_then_parent_follow {} {
216 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
222 gdb_test_no_output "set follow-fork parent"
224 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
226 # HP-UX 10.20 seems to stop you in "vfork", while more recent
227 # HP-UXs stop you in "_vfork".
228 set test "continue to vfork"
229 gdb_test_multiple "continue" $test {
230 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
233 -re "vfork \\(\\) at.*$gdb_prompt " {
238 set linenum [gdb_get_line_number "pid = vfork ();"]
240 gdb_test_multiple "finish" $test {
241 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
244 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
249 # The child has been detached; allow time for any output it might
250 # generate to arrive, so that output doesn't get confused with
251 # any expected debugger output from a subsequent testpoint.
256 proc tcatch_vfork_then_child_follow_exec {} {
257 with_test_prefix "vfork child follow, finish after tcatch vfork" {
264 gdb_test_no_output "set follow-fork child"
266 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
268 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
269 # stop you in "_vfork".
270 set test "continue to vfork"
271 gdb_test_multiple "continue" $test {
272 -re "vfork \\(\\) at .*$gdb_prompt $" {
275 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
280 set linenum1 [gdb_get_line_number "pid = vfork ();"]
281 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
284 gdb_test_multiple "finish" $test {
285 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
288 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
292 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
293 pass "$test (followed exec)"
296 # The parent has been detached; allow time for any output it might
297 # generate to arrive, so that output doesn't get confused with
298 # any expected debugger output from a subsequent testpoint.
303 proc tcatch_vfork_then_child_follow_exit {} {
304 with_test_prefix "vfork child follow, finish after tcatch vfork" {
310 gdb_test_no_output "set follow-fork child"
312 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
314 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
315 # stop you in "_vfork".
316 set test "continue to vfork"
317 gdb_test_multiple "continue" $test {
318 -re "vfork \\(\\) at .*$gdb_prompt $" {
321 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
327 gdb_test_multiple "finish" $test {
328 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
329 setup_kfail "gdb/14762" *-*-*
332 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
335 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
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 vfork_relations_in_info_inferiors { variant } {
348 with_test_prefix "vfork relations in info inferiors" {
353 gdb_test_no_output "set follow-fork child"
355 set test "step over vfork"
356 gdb_test_multiple "next" $test {
357 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
362 gdb_test "info inferiors" \
363 ".*is vfork parent of inferior 2.*is vfork child of inferior 1" \
364 "info inferiors shows vfork parent/child relation"
366 if { $variant == "exec" } {
369 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
370 set test "continue to bp"
371 gdb_test_multiple "continue" $test {
372 -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
377 set test "continue to child exit"
378 gdb_test_multiple "continue" $test {
379 -re "exited normally.*$gdb_prompt " {
385 set test "vfork relation no longer appears in info inferiors"
386 gdb_test_multiple "info inferiors" $test {
387 -re "is vfork child of inferior 1.*$gdb_prompt $" {
390 -re "is vfork parent of inferior 2.*$gdb_prompt $" {
393 -re "$gdb_prompt $" {
399 proc do_vfork_and_follow_parent_tests {} {
402 # Try following the parent process by stepping through a call to
403 # vfork. Do this without catchpoints.
404 vfork_parent_follow_through_step
406 # Try following the parent process by setting a breakpoint on the
407 # other side of a vfork, and running to that point. Do this
408 # without catchpoints.
409 vfork_parent_follow_to_bp
411 # Try catching a vfork, and stepping out to the parent.
413 tcatch_vfork_then_parent_follow
416 proc do_vfork_and_follow_child_tests_exec {} {
417 # Try following the child process by just continuing through the
418 # vfork, and letting the parent's breakpoint on "main" be auto-
419 # magically reset in the child.
421 vfork_and_exec_child_follow_to_main_bp
423 # Try following the child process by stepping through a call to
424 # vfork. The child also executes an exec. Since the child cannot
425 # be debugged until after it has exec'd, and since there's a bp on
426 # "main" in the parent, and since the bp's for the parent are
427 # recomputed in the exec'd child, the step through a vfork should
428 # land us in the "main" for the exec'd child, too.
430 vfork_and_exec_child_follow_through_step
432 # Try catching a vfork, and stepping out to the child.
434 tcatch_vfork_then_child_follow_exec
436 # Test the ability to follow both child and parent of a vfork. Do
437 # this without catchpoints.
438 # ??rehrauer: NYI. Will add testpoints here when implemented.
441 # Test the ability to have the debugger ask the user at vfork-time
442 # whether to follow the parent, child or both. Do this without
444 # ??rehrauer: NYI. Will add testpoints here when implemented.
447 # Step over a vfork in the child, do "info inferiors" and check the
448 # parent/child relation is displayed. Run the child over the exec,
449 # and confirm the relation is no longer displayed in "info
452 vfork_relations_in_info_inferiors "exec"
455 proc do_vfork_and_follow_child_tests_exit {} {
456 # Try following the child process by just continuing through the
457 # vfork, and letting the child exit.
459 vfork_child_follow_to_exit
461 # Try catching a vfork, and stepping out to the child.
463 tcatch_vfork_then_child_follow_exit
465 # Step over a vfork in the child, do "info inferiors" and check the
466 # parent/child relation is displayed. Run the child to completion,
467 # and confirm the relation is no longer displayed in "info
470 vfork_relations_in_info_inferiors "exit"
473 with_test_prefix "check vfork support" {
474 # Check that vfork catchpoints are supported, as an indicator for
475 # whether vfork-following is supported.
476 check_vfork_catchpoints
479 # Follow parent and follow child vfork tests with a child that execs.
480 with_test_prefix "exec" {
481 # These are tests of gdb's ability to follow the parent of a Unix
482 # vfork system call. The child will subsequently call a variant
483 # of the Unix exec system call.
484 do_vfork_and_follow_parent_tests
486 # These are tests of gdb's ability to follow the child of a Unix
487 # vfork system call. The child will subsequently call a variant
488 # of a Unix exec system call.
490 do_vfork_and_follow_child_tests_exec
493 # Switch to test the case of the child exiting. We can't use
494 # standard_testfile here because we don't want to overwrite the binary
495 # of the previous tests.
496 set testfile "foll-vfork-exit"
497 set srcfile ${testfile}.c
498 set binfile [standard_output_file ${testfile}]
500 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
501 untested "failed to build $testfile"
505 # Follow parent and follow child vfork tests with a child that exits.
506 with_test_prefix "exit" {
507 # These are tests of gdb's ability to follow the parent of a Unix
508 # vfork system call. The child will subsequently exit.
509 do_vfork_and_follow_parent_tests
511 # These are tests of gdb's ability to follow the child of a Unix
512 # vfork system call. The child will subsequently exit.
514 do_vfork_and_follow_child_tests_exit
517 set timeout $oldtimeout