1 # Copyright 2005-2019 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 # Until "set follow-fork-mode" and "catch fork" are implemented on
19 if { ![istarget "*-*-linux*"] } then {
26 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
27 untested "failed to compile"
31 # Start with a fresh gdb
33 clean_restart ${binfile}
37 # This is a test of gdb's ability to follow the parent, child or both
38 # parent and child of multiple Unix fork() system calls.
40 set exit_bp_loc [gdb_get_line_number "Set exit breakpoint here."]
42 # Insert a breakpoint at the location provided by the exit_bp_loc global
43 # and resume the execution until hitting that breakpoint. We also make
44 # sure to consume all the expected output from all processes as well,
45 # to make sure it doesn't cause trouble during a subsequent test.
47 proc continue_to_exit_bp_loc {} {
48 global exit_bp_loc decimal gdb_prompt
49 global inferior_spawn_id gdb_spawn_id
51 gdb_breakpoint $exit_bp_loc
55 # The output from the child processes can be interleaved arbitrarily
56 # with the output from GDB and the parent process. If we don't
57 # consume it all now, it can confuse later interactions.
62 while { ($seen_done < 16 || ! $seen_prompt) && ! $seen_timeout } {
63 # We don't know what order the interesting things will arrive in.
64 # Using a pattern of the form 'x|y|z' instead of -re x ... -re y
65 # ... -re z ensures that expect always chooses the match that
66 # occurs leftmost in the input, and not the pattern appearing
67 # first in the script that occurs anywhere in the input, so that
68 # we don't skip anything.
70 -i "$inferior_spawn_id $gdb_spawn_id"
71 -re "($decimal done)|(Breakpoint)|($gdb_prompt)" {
72 if {[info exists expect_out(1,string)]} {
74 } elseif {[info exists expect_out(2,string)]} {
76 } elseif {[info exists expect_out(3,string)]} {
79 array unset expect_out
81 timeout { set seen_timeout 1 }
85 if { $seen_timeout } {
86 fail "run to exit 2 (timeout)"
87 } elseif { ! $seen_prompt } {
88 fail "run to exit 2 (no prompt)"
89 } elseif { ! $seen_break } {
90 fail "run to exit 2 (no breakpoint hit)"
91 } elseif { $seen_done != 16 } {
92 fail "run to exit 2 (missing done messages)"
98 # The inferior program builds a tree of processes by executing a loop
99 # four times, calling fork at each iteration. Thus, at each
100 # iteration, the total number of processes doubles; after four
101 # iterations, we have 16 processes. Each process saves the results
102 # from its 'fork' calls, so we can tell which leaf a given process is
103 # by looking at which forks returned zero and which returned a pid: a
104 # zero means to take the child's branch; a pid means to take the
107 # First set gdb to follow the child.
108 # The result should be that each of the 4 forks returns zero.
110 clean_restart ${binfile}
112 gdb_test_no_output "set follow-fork child"
113 continue_to_exit_bp_loc
115 gdb_test "print pids" "\\$.* = \\{0, 0, 0, 0\\}.*" "follow child, print pids"
117 # Now set gdb to follow the parent.
118 # Result should be that none of the 4 forks returns zero.
120 clean_restart ${binfile}
122 gdb_test_no_output "set follow-fork parent" ""
123 continue_to_exit_bp_loc
125 gdb_test "print pids\[0\]==0 || pids\[1\]==0 || pids\[2\]==0 || pids\[3\]==0" \
126 " = 0" "follow parent, print pids"
129 # Now test with detach-on-fork off.
132 # Start with a fresh gdb
134 clean_restart ${binfile}
137 gdb_breakpoint $exit_bp_loc
139 gdb_test "help set detach-on-fork" "whether gdb will detach the child.*" \
142 gdb_test "show detach-on-fork" "on." "show detach default on"
144 gdb_test_no_output "set detach off" "set detach off"
147 # We will now run every fork up to the exit bp,
148 # eventually winding up with 16 inferiors.
151 for {set i 1} {$i <= 15} {incr i} {
152 gdb_test "continue" "Breakpoint .* main .*exit.*" "run to exit $i"
153 gdb_test "info inferior" " 2 .* 3 .* 4 .* 5 .*" "info inferior $i"
154 gdb_test "inferior $i + 1" "(_dl_sysinfo_int80|fork|__kernel_(v|)syscall).*" \
158 gdb_test "continue" "Breakpoint .* main .*exit.*" "run to exit 16"
159 gdb_test "info inferior" " 2 .* 3 .* 4 .* 5 .*" "info inferior 16"
160 gdb_test "inferior 2" " main .*" "restart final"
163 # Now we should examine all the pids.
167 # Test detach inferior
170 # [assumes we're at #1]
171 gdb_test "detach inferior 2" "Detaching .*" "detach 2"
172 gdb_test "detach inferior 3" "Detaching .*" "detach 3"
173 gdb_test "detach inferior 4" "Detaching .*" "detach 4"
174 gdb_test "detach inferior 5" "Detaching .*" "detach 5"
180 for {set i 6} { $i <= 16} {incr i} {
181 gdb_test_no_output "kill inferior $i" "kill $i"
182 gdb_test "info inferior $i" "<null>.*" "did kill $i"