c09c5590cf727f81259bd43449f21570df095444
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / multi-forks.exp
1 # Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
2
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.
7 #
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.
12 #
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/>.
15
16 if $tracelevel then {
17 strace $tracelevel
18 }
19
20 if { [is_remote target] || ![isnative] } then {
21 continue
22 }
23
24 # Until "set follow-fork-mode" and "catch fork" are implemented on
25 # other targets...
26 #
27 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-*-linux*"]} then {
28 continue
29 }
30
31 set prms_id 0
32 set bug_id 0
33
34 set testfile "multi-forks"
35 set srcfile ${testfile}.c
36 set binfile ${objdir}/${subdir}/${testfile}
37
38 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
39 untested multi-forks.exp
40 return -1
41 }
42
43 # Start with a fresh gdb
44
45 gdb_exit
46 gdb_start
47 gdb_reinitialize_dir $srcdir/$subdir
48 gdb_load ${binfile}
49
50 global gdb_prompt
51
52 # This is a test of gdb's ability to follow the parent, child or both
53 # parent and child of multiple Unix fork() system calls.
54
55 set exit_bp_loc [gdb_get_line_number "Set exit breakpoint here."]
56
57 # Insert a breakpoint at the location provided by the exit_bp_loc global
58 # and resume the execution until hitting that breakpoint. We also make
59 # sure to consume all the expected output from all processes as well,
60 # to make sure it doesn't cause trouble during a subsequent test.
61
62 proc continue_to_exit_bp_loc {} {
63 global exit_bp_loc decimal gdb_prompt
64
65 gdb_breakpoint $exit_bp_loc
66
67 send_gdb "continue\n"
68
69 # The output from the child processes can be interleaved arbitrarily
70 # with the output from GDB and the parent process. If we don't
71 # consume it all now, it can confuse later interactions.
72 set seen_done 0
73 set seen_break 0
74 set seen_prompt 0
75 set seen_timeout 0
76 while { ($seen_done < 16 || ! $seen_prompt) && ! $seen_timeout } {
77 # We don't know what order the interesting things will arrive in.
78 # Using a pattern of the form 'x|y|z' instead of -re x ... -re y
79 # ... -re z ensures that expect always chooses the match that
80 # occurs leftmost in the input, and not the pattern appearing
81 # first in the script that occurs anywhere in the input, so that
82 # we don't skip anything.
83 gdb_expect {
84 -re "($decimal done)|(Breakpoint)|($gdb_prompt)" {
85 if {[info exists expect_out(1,string)]} {
86 incr seen_done
87 } elseif {[info exists expect_out(2,string)]} {
88 set seen_break 1
89 } elseif {[info exists expect_out(3,string)]} {
90 set seen_prompt 1
91 }
92 array unset expect_out
93 }
94 timeout { set seen_timeout 1 }
95 }
96 }
97
98 if { $seen_timeout } {
99 fail "run to exit 2 (timeout)"
100 } elseif { ! $seen_prompt } {
101 fail "run to exit 2 (no prompt)"
102 } elseif { ! $seen_break } {
103 fail "run to exit 2 (no breakpoint hit)"
104 } elseif { $seen_done != 16 } {
105 fail "run to exit 2 (missing done messages)"
106 } else {
107 pass "run to exit 2"
108 }
109 }
110
111 # The inferior program builds a tree of processes by executing a loop
112 # four times, calling fork at each iteration. Thus, at each
113 # iteration, the total number of processes doubles; after four
114 # iterations, we have 16 processes. Each process saves the results
115 # from its 'fork' calls, so we can tell which leaf a given process is
116 # by looking at which forks returned zero and which returned a pid: a
117 # zero means to take the child's branch; a pid means to take the
118 # parent's branch.
119
120 # First set gdb to follow the child.
121 # The result should be that each of the 4 forks returns zero.
122
123 runto_main
124 gdb_test "set follow child"
125 continue_to_exit_bp_loc
126
127 gdb_test "print pids" "\\$.* = \\{0, 0, 0, 0\\}.*" "follow child, print pids"
128
129 # Now set gdb to follow the parent.
130 # Result should be that none of the 4 forks returns zero.
131
132 runto_main
133 gdb_test "set follow parent" "" ""
134 continue_to_exit_bp_loc
135
136 gdb_test "print pids\[0\]==0 || pids\[1\]==0 || pids\[2\]==0 || pids\[3\]==0" \
137 " = 0" "follow parent, print pids"
138
139 #
140 # Now test with detach-on-fork off.
141 #
142
143 # detach-on-fork isn't implemented on hpux.
144 #
145 if {![istarget "*-*-linux*"]} then {
146 continue
147 }
148
149 # Start with a fresh gdb
150
151 gdb_exit
152 gdb_start
153 gdb_reinitialize_dir $srcdir/$subdir
154 gdb_load ${binfile}
155
156 runto_main
157 gdb_breakpoint $exit_bp_loc
158
159 gdb_test "help set detach-on-fork" "whether gdb will detach the child.*" \
160 "help set detach"
161
162 gdb_test "show detach-on-fork" "on." "show detach default on"
163
164 gdb_test "set detach off" "" "set detach off"
165
166 #
167 # We will now run every fork up to the exit bp,
168 # eventually winding up with 16 inferiors.
169 #
170
171 for {set i 1} {$i <= 15} {incr i} {
172 gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit $i"
173 gdb_test "info inferior" " 5 .* 4 .* 3 .* 2 .*" "info inferior $i"
174 gdb_test "inferior $i + 1" "(_dl_sysinfo_int80|fork|__kernel_(v|)syscall).*" \
175 "inferior $i"
176 }
177
178 gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit 16"
179 gdb_test "info inferiors" " 5 .* 4 .* 3 .* 2 .*" "info inferior 16"
180 gdb_test "inferior 2" " main .*" "restart final"
181
182 #
183 # Now we should examine all the pids.
184 #
185
186 #
187 # Test detach inferior
188 #
189
190 # [assumes we're at #1]
191 gdb_test "detach inferior 2" "Detaching .*" "Detach 2"
192 gdb_test "detach inferior 3" "Detaching .*" "Detach 3"
193 gdb_test "detach inferior 4" "Detaching .*" "Detach 4"
194 gdb_test "detach inferior 5" "Detaching .*" "Detach 5"
195
196 #
197 # Test kill inferior
198 #
199
200 gdb_test "kill inferior 6" "" "Kill 6"
201 gdb_test "info inferior 6" "Inferior ID 6 not known." "Did kill 6"
202 gdb_test "kill inferior 7" "" "Kill 7"
203 gdb_test "info inferior 7" "Inferior ID 7 not known." "Did kill 7"
204 gdb_test "kill inferior 8" "" "Kill 8"
205 gdb_test "info inferior 8" "Inferior ID 8 not known." "Did kill 8"
206 gdb_test "kill inferior 9" "" "Kill 9"
207 gdb_test "info inferior 9" "Inferior ID 9 not known." "Did kill 9"
208 gdb_test "kill inferior 10" "" "Kill 10"
209 gdb_test "info inferior 10" "Inferior ID 10 not known." "Did kill 10"
210 gdb_test "kill inferior 11" "" "Kill 11"
211 gdb_test "info inferior 11" "Inferior ID 11 not known." "Did kill 11"
212 gdb_test "kill inferior 12" "" "Kill 12"
213 gdb_test "info inferior 12" "Inferior ID 12 not known." "Did kill 12"
214 gdb_test "kill inferior 13" "" "Kill 13"
215 gdb_test "info inferior 13" "Inferior ID 13 not known." "Did kill 13"
216 gdb_test "kill inferior 14" "" "Kill 14"
217 gdb_test "info inferior 14" "Inferior ID 14 not known." "Did kill 14"
218 gdb_test "kill inferior 15" "" "Kill 15"
219 gdb_test "info inferior 15" "Inferior ID 15 not known." "Did kill 15"
220 gdb_test "kill inferior 16" "" "Kill 16"
221 gdb_test "info inferior 16" "Inferior ID 16 not known." "Did kill 16"
222
223 return 0
This page took 0.035475 seconds and 3 git commands to generate.