1 # Copyright 1997-2016 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 continue_to_vfork {} {
218 # A vfork catchpoint may stop in either "vfork" or "_vfork".
219 set test "continue to vfork"
220 gdb_test_multiple "continue" $test {
221 -re "vfork \\(\\) at .*$gdb_prompt $" {
224 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
230 proc tcatch_vfork_then_parent_follow {} {
231 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
237 gdb_test_no_output "set follow-fork parent"
239 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
243 set linenum [gdb_get_line_number "pid = vfork ();"]
245 gdb_test_multiple "finish" $test {
246 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
249 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
254 # The child has been detached; allow time for any output it might
255 # generate to arrive, so that output doesn't get confused with
256 # any expected debugger output from a subsequent testpoint.
261 proc tcatch_vfork_then_child_follow_exec {} {
262 with_test_prefix "vfork child follow, finish after tcatch vfork" {
269 gdb_test_no_output "set follow-fork child"
271 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
275 set linenum1 [gdb_get_line_number "pid = vfork ();"]
276 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
279 gdb_test_multiple "finish" $test {
280 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
283 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
287 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
288 pass "$test (followed exec)"
291 # The parent has been detached; allow time for any output it might
292 # generate to arrive, so that output doesn't get confused with
293 # any expected debugger output from a subsequent testpoint.
298 proc tcatch_vfork_then_child_follow_exit {} {
299 with_test_prefix "vfork child follow, finish after tcatch vfork" {
305 gdb_test_no_output "set follow-fork child"
307 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
312 gdb_test_multiple "finish" $test {
313 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
314 setup_kfail "gdb/14762" *-*-*
317 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
320 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
325 # The parent has been detached; allow time for any output it might
326 # generate to arrive, so that output doesn't get confused with
327 # any expected debugger output from a subsequent testpoint.
332 proc vfork_relations_in_info_inferiors { variant } {
333 with_test_prefix "vfork relations in info inferiors" {
338 gdb_test_no_output "set follow-fork child"
340 set test "step over vfork"
341 gdb_test_multiple "next" $test {
342 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
347 gdb_test "info inferiors" \
348 ".*is vfork child of inferior 1.*is vfork parent of inferior 2" \
349 "info inferiors shows vfork parent/child relation"
351 if { $variant == "exec" } {
354 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
355 set test "continue to bp"
356 gdb_test_multiple "continue" $test {
357 -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
362 set test "continue to child exit"
363 gdb_test_multiple "continue" $test {
364 -re "exited normally.*$gdb_prompt " {
370 set test "vfork relation no longer appears in info inferiors"
371 gdb_test_multiple "info inferiors" $test {
372 -re "is vfork child of inferior 1.*$gdb_prompt $" {
375 -re "is vfork parent of inferior 2.*$gdb_prompt $" {
378 -re "$gdb_prompt $" {
384 proc do_vfork_and_follow_parent_tests {} {
387 # Try following the parent process by stepping through a call to
388 # vfork. Do this without catchpoints.
389 vfork_parent_follow_through_step
391 # Try following the parent process by setting a breakpoint on the
392 # other side of a vfork, and running to that point. Do this
393 # without catchpoints.
394 vfork_parent_follow_to_bp
396 # Try catching a vfork, and stepping out to the parent.
398 tcatch_vfork_then_parent_follow
401 proc do_vfork_and_follow_child_tests_exec {} {
402 # Try following the child process by just continuing through the
403 # vfork, and letting the parent's breakpoint on "main" be auto-
404 # magically reset in the child.
406 vfork_and_exec_child_follow_to_main_bp
408 # Try following the child process by stepping through a call to
409 # vfork. The child also executes an exec. Since the child cannot
410 # be debugged until after it has exec'd, and since there's a bp on
411 # "main" in the parent, and since the bp's for the parent are
412 # recomputed in the exec'd child, the step through a vfork should
413 # land us in the "main" for the exec'd child, too.
415 vfork_and_exec_child_follow_through_step
417 # Try catching a vfork, and stepping out to the child.
419 tcatch_vfork_then_child_follow_exec
421 # Test the ability to follow both child and parent of a vfork. Do
422 # this without catchpoints.
423 # ??rehrauer: NYI. Will add testpoints here when implemented.
426 # Test the ability to have the debugger ask the user at vfork-time
427 # whether to follow the parent, child or both. Do this without
429 # ??rehrauer: NYI. Will add testpoints here when implemented.
432 # Step over a vfork in the child, do "info inferiors" and check the
433 # parent/child relation is displayed. Run the child over the exec,
434 # and confirm the relation is no longer displayed in "info
437 vfork_relations_in_info_inferiors "exec"
440 proc do_vfork_and_follow_child_tests_exit {} {
441 # Try following the child process by just continuing through the
442 # vfork, and letting the child exit.
444 vfork_child_follow_to_exit
446 # Try catching a vfork, and stepping out to the child.
448 tcatch_vfork_then_child_follow_exit
450 # Step over a vfork in the child, do "info inferiors" and check the
451 # parent/child relation is displayed. Run the child to completion,
452 # and confirm the relation is no longer displayed in "info
455 vfork_relations_in_info_inferiors "exit"
458 with_test_prefix "check vfork support" {
459 # Check that vfork catchpoints are supported, as an indicator for
460 # whether vfork-following is supported.
461 check_vfork_catchpoints
464 # Follow parent and follow child vfork tests with a child that execs.
465 with_test_prefix "exec" {
466 # These are tests of gdb's ability to follow the parent of a Unix
467 # vfork system call. The child will subsequently call a variant
468 # of the Unix exec system call.
469 do_vfork_and_follow_parent_tests
471 # These are tests of gdb's ability to follow the child of a Unix
472 # vfork system call. The child will subsequently call a variant
473 # of a Unix exec system call.
475 do_vfork_and_follow_child_tests_exec
478 # Switch to test the case of the child exiting. We can't use
479 # standard_testfile here because we don't want to overwrite the binary
480 # of the previous tests.
481 set testfile "foll-vfork-exit"
482 set srcfile ${testfile}.c
483 set binfile [standard_output_file ${testfile}]
485 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
486 untested "failed to build $testfile"
490 # Follow parent and follow child vfork tests with a child that exits.
491 with_test_prefix "exit" {
492 # These are tests of gdb's ability to follow the parent of a Unix
493 # vfork system call. The child will subsequently exit.
494 do_vfork_and_follow_parent_tests
496 # These are tests of gdb's ability to follow the child of a Unix
497 # vfork system call. The child will subsequently exit.
499 do_vfork_and_follow_child_tests_exit
502 set timeout $oldtimeout