1 # Copyright 1997-2023 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*"]} {
30 set compile_options debug
32 if {[build_executable $testfile.exp $testfile $srcfile $compile_options] == -1} {
33 untested "failed to compile main testcase"
37 set testfile2 "vforked-prog"
38 set srcfile2 ${testfile2}.c
40 if {[build_executable $testfile.exp $testfile2 $srcfile2 $compile_options] == -1} {
41 untested "failed to compile secondary testcase"
45 if { [is_remote target] } {
46 gdb_remote_download target [standard_output_file $testfile2]
49 # A few of these tests require a little more time than the standard
51 set oldtimeout $timeout
52 set timeout [expr "$timeout + 10"]
54 # Start with a fresh GDB, with verbosity enabled, and run to main. On
55 # error, behave as "return", so we don't try to continue testing with
58 global testfile srcfile
60 clean_restart $testfile
66 set tbreak_line [gdb_get_line_number " VFORK " $srcfile]
67 gdb_test "tbreak ${tbreak_line}"
68 gdb_continue_to_breakpoint ".*"
71 proc check_vfork_catchpoints {} {
73 global has_vfork_catchpoints
77 # Verify that the system supports "catch vfork".
78 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
79 set has_vfork_catchpoints 0
80 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
81 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
82 unsupported "continue to first vfork catchpoint"
84 -re ".*Catchpoint.*$gdb_prompt $" {
85 set has_vfork_catchpoints 1
86 pass "continue to first vfork catchpoint"
90 if {$has_vfork_catchpoints == 0} {
91 unsupported "vfork catchpoints"
96 proc vfork_parent_follow_through_step {} {
97 with_test_prefix "vfork parent follow, through step" {
102 gdb_test_no_output "set follow-fork parent"
105 gdb_test_multiple "next" $test {
106 -re "\\\[Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " {
110 # The child has been detached; allow time for any output it might
111 # generate to arrive, so that output doesn't get confused with
112 # any gdb_expected debugger output from a subsequent testpoint.
117 proc vfork_parent_follow_to_bp {} {
118 with_test_prefix "vfork parent follow, to bp" {
124 gdb_test_no_output "set follow-fork parent"
126 set bp_location [gdb_get_line_number "I'm the proud parent of child"]
127 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
129 set test "continue to bp"
130 gdb_test_multiple "continue" $test {
131 -re ".*\\\[Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
135 # The child has been detached; allow time for any output it might
136 # generate to arrive, so that output doesn't get confused with
137 # any expected debugger output from a subsequent testpoint.
142 proc vfork_child_follow_to_exit {} {
143 with_test_prefix "vfork child follow, to exit" {
148 gdb_test_no_output "set follow-fork child"
150 set test "continue to child exit"
151 gdb_test_multiple "continue" $test {
152 -re "Couldn't get registers.*$gdb_prompt " {
156 -re "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent .* after child exit.*$gdb_prompt " {
160 # The parent has been detached; allow time for any output it might
161 # generate to arrive, so that output doesn't get confused with
162 # any gdb_expected debugger output from a subsequent testpoint.
167 proc vfork_and_exec_child_follow_to_main_bp {} {
168 with_test_prefix "vfork and exec child follow, to main bp" {
174 gdb_test_no_output "set follow-fork child"
176 set linenum [gdb_get_line_number "Hello from vforked-prog" ${srcfile2}]
178 set test "continue to bp"
179 gdb_test_multiple "continue" $test {
180 -re "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
184 # The parent has been detached; allow time for any output it might
185 # generate to arrive, so that output doesn't get confused with
186 # any gdb_expected debugger output from a subsequent testpoint.
191 proc vfork_and_exec_child_follow_through_step {} {
192 with_test_prefix "vfork and exec child follow, through step" {
198 gdb_test_no_output "set follow-fork child"
200 set test "step over vfork"
202 # The ideal support is to be able to debug the child even
203 # before it execs. Thus, "next" lands on the next line after
205 gdb_test_multiple "next" $test {
206 -re "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
210 # The parent has been detached; allow time for any output it might
211 # generate to arrive, so that output doesn't get confused with
212 # any expected debugger output from a subsequent testpoint.
217 proc continue_to_vfork {} {
220 # A vfork catchpoint may stop in either "vfork" or "_vfork".
221 set test "continue to vfork"
222 gdb_test_multiple "continue" $test {
223 -re "vfork \\(\\) at .*$gdb_prompt $" {
226 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
232 proc tcatch_vfork_then_parent_follow {} {
233 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
239 gdb_test_no_output "set follow-fork parent"
241 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
245 set linenum [gdb_get_line_number "pid = vfork ();"]
247 gdb_test_multiple "finish" $test {
248 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
251 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
256 # The child has been detached; allow time for any output it might
257 # generate to arrive, so that output doesn't get confused with
258 # any expected debugger output from a subsequent testpoint.
263 proc tcatch_vfork_then_child_follow_exec {} {
264 with_test_prefix "vfork child follow, finish after tcatch vfork" {
271 gdb_test_no_output "set follow-fork child"
273 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
277 set linenum1 [gdb_get_line_number "pid = vfork ();"]
278 set linenum2 [gdb_get_line_number "Hello from vforked-prog" ${srcfile2}]
281 gdb_test_multiple "finish" $test {
282 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
285 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
289 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
290 pass "$test (followed exec)"
293 # The parent 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_exit {} {
301 with_test_prefix "vfork child follow, finish after tcatch vfork" {
307 gdb_test_no_output "set follow-fork child"
309 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
314 gdb_test_multiple "finish" $test {
315 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
316 setup_kfail "gdb/14762" *-*-*
319 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
322 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
327 # The parent has been detached; allow time for any output it might
328 # generate to arrive, so that output doesn't get confused with
329 # any expected debugger output from a subsequent testpoint.
334 proc vfork_relations_in_info_inferiors { variant } {
335 with_test_prefix "vfork relations in info inferiors" {
340 gdb_test_no_output "set follow-fork child"
342 set test "step over vfork"
343 gdb_test_multiple "next" $test {
344 -re "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
349 gdb_test "info inferiors" \
350 ".*is vfork parent of inferior 2.*is vfork child of inferior 1" \
351 "info inferiors shows vfork parent/child relation"
353 if { $variant == "exec" } {
356 set linenum [gdb_get_line_number "Hello from vforked-prog" ${srcfile2}]
357 set test "continue to bp"
358 gdb_test_multiple "continue" $test {
359 -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
364 set test "continue to child exit"
365 gdb_test_multiple "continue" $test {
366 -re "exited normally.*$gdb_prompt " {
372 set test "vfork relation no longer appears in info inferiors"
373 gdb_test_multiple "info inferiors" $test {
374 -re -wrap "is vfork child of inferior 1.*" {
377 -re -wrap "is vfork parent of inferior 2.*" {
386 proc do_vfork_and_follow_parent_tests {} {
389 # Try following the parent process by stepping through a call to
390 # vfork. Do this without catchpoints.
391 vfork_parent_follow_through_step
393 # Try following the parent process by setting a breakpoint on the
394 # other side of a vfork, and running to that point. Do this
395 # without catchpoints.
396 vfork_parent_follow_to_bp
398 # Try catching a vfork, and stepping out to the parent.
400 tcatch_vfork_then_parent_follow
403 proc do_vfork_and_follow_child_tests_exec {} {
404 # Try following the child process by just continuing through the
405 # vfork, and letting the parent's breakpoint on "main" be auto-
406 # magically reset in the child.
408 vfork_and_exec_child_follow_to_main_bp
410 # Try following the child process by stepping through a call to
411 # vfork. The child also executes an exec. Since the child cannot
412 # be debugged until after it has exec'd, and since there's a bp on
413 # "main" in the parent, and since the bp's for the parent are
414 # recomputed in the exec'd child, the step through a vfork should
415 # land us in the "main" for the exec'd child, too.
417 vfork_and_exec_child_follow_through_step
419 # Try catching a vfork, and stepping out to the child.
421 tcatch_vfork_then_child_follow_exec
423 # Test the ability to follow both child and parent of a vfork. Do
424 # this without catchpoints.
425 # ??rehrauer: NYI. Will add testpoints here when implemented.
428 # Test the ability to have the debugger ask the user at vfork-time
429 # whether to follow the parent, child or both. Do this without
431 # ??rehrauer: NYI. Will add testpoints here when implemented.
434 # Step over a vfork in the child, do "info inferiors" and check the
435 # parent/child relation is displayed. Run the child over the exec,
436 # and confirm the relation is no longer displayed in "info
439 vfork_relations_in_info_inferiors "exec"
442 proc do_vfork_and_follow_child_tests_exit {} {
443 # Try following the child process by just continuing through the
444 # vfork, and letting the child exit.
446 vfork_child_follow_to_exit
448 # Try catching a vfork, and stepping out to the child.
450 tcatch_vfork_then_child_follow_exit
452 # Step over a vfork in the child, do "info inferiors" and check the
453 # parent/child relation is displayed. Run the child to completion,
454 # and confirm the relation is no longer displayed in "info
457 vfork_relations_in_info_inferiors "exit"
460 with_test_prefix "check vfork support" {
461 # Check that vfork catchpoints are supported, as an indicator for
462 # whether vfork-following is supported.
463 check_vfork_catchpoints
466 # Follow parent and follow child vfork tests with a child that execs.
467 with_test_prefix "exec" {
468 # These are tests of gdb's ability to follow the parent of a Unix
469 # vfork system call. The child will subsequently call a variant
470 # of the Unix exec system call.
471 do_vfork_and_follow_parent_tests
473 # These are tests of gdb's ability to follow the child of a Unix
474 # vfork system call. The child will subsequently call a variant
475 # of a Unix exec system call.
477 do_vfork_and_follow_child_tests_exec
480 # Switch to test the case of the child exiting. We can't use
481 # standard_testfile here because we don't want to overwrite the binary
482 # of the previous tests.
483 set testfile "foll-vfork-exit"
484 set srcfile ${testfile}.c
485 set binfile [standard_output_file ${testfile}]
487 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
488 untested "failed to build $testfile"
492 # Follow parent and follow child vfork tests with a child that exits.
493 with_test_prefix "exit" {
494 # These are tests of gdb's ability to follow the parent of a Unix
495 # vfork system call. The child will subsequently exit.
496 do_vfork_and_follow_parent_tests
498 # These are tests of gdb's ability to follow the child of a Unix
499 # vfork system call. The child will subsequently exit.
501 do_vfork_and_follow_child_tests_exit
504 set timeout $oldtimeout