* gdb.base/exe-lock.exp (binfile): Add $EXEEXT suffix to fix
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / multi-forks.exp
CommitLineData
0fb0cc75 1# Copyright 2005, 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
099ac3dd
MS
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
099ac3dd 6# (at your option) any later version.
e22f8b7c 7#
099ac3dd
MS
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.
e22f8b7c 12#
099ac3dd 13# You should have received a copy of the GNU General Public License
e22f8b7c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
099ac3dd
MS
15
16if $tracelevel then {
17 strace $tracelevel
18 }
19
db9d7fc5 20if { [is_remote target] || ![isnative] } then {
099ac3dd
MS
21 continue
22}
23
24# Until "set follow-fork-mode" and "catch fork" are implemented on
25# other targets...
26#
27if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-*-linux*"]} then {
28 continue
29}
30
31set prms_id 0
32set bug_id 0
33
34set testfile "multi-forks"
35set srcfile ${testfile}.c
36set binfile ${objdir}/${subdir}/${testfile}
37
38if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
39 untested multi-forks.exp
40 return -1
099ac3dd
MS
41}
42
43# Start with a fresh gdb
44
45gdb_exit
46gdb_start
47gdb_reinitialize_dir $srcdir/$subdir
48gdb_load ${binfile}
49
50global 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.
cb4dce93
JK
54
55set 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
62proc 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}
099ac3dd 110
aad9a193
JB
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.
099ac3dd
MS
119
120# First set gdb to follow the child.
121# The result should be that each of the 4 forks returns zero.
122
123runto_main
cb4dce93
JK
124gdb_test "set follow child"
125continue_to_exit_bp_loc
099ac3dd
MS
126
127gdb_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
099ac3dd 132runto_main
099ac3dd 133gdb_test "set follow parent" "" ""
cb4dce93 134continue_to_exit_bp_loc
099ac3dd
MS
135
136gdb_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
143runto_main
cb4dce93 144gdb_breakpoint $exit_bp_loc
099ac3dd
MS
145
146gdb_test "help set detach-on-fork" "whether gdb will detach the child.*" \
147 "help set detach"
148
149gdb_test "show detach-on-fork" "on." "show detach default on"
150
151gdb_test "set detach off" "" "set detach off"
152
153#
154# We will now run every fork up to the exit bp,
155# eventually winding up with 16 forks.
156#
157
9078e690
DJ
158for {set i 1} {$i <= 15} {incr i} {
159 gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit $i"
160 gdb_test "info fork" " 4 .* 3 .* 2 .* 1 .*" "info fork $i"
37f08e04 161 gdb_test "restart $i" "(_dl_sysinfo_int80|fork|__kernel_(v|)syscall).*" \
9cb82863 162 "restart $i"
9078e690 163}
099ac3dd
MS
164
165gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit 16"
166gdb_test "info fork" " 4 .* 3 .* 2 .* 1 .*" "info fork 16"
167gdb_test "restart 0" " main .*" "restart final"
168
169#
170# Now we should examine all the pids.
171#
172
173#
f73adfeb 174# Test detach fork
099ac3dd
MS
175#
176
177# [assumes we're at #0]
f73adfeb
AS
178gdb_test "detach fork 1" "Detached .*" "Detach 1"
179gdb_test "detach fork 2" "Detached .*" "Detach 2"
180gdb_test "detach fork 3" "Detached .*" "Detach 3"
181gdb_test "detach fork 4" "Detached .*" "Detach 4"
099ac3dd
MS
182
183#
b8db102d 184# Test delete fork
099ac3dd
MS
185#
186
b8db102d
MS
187gdb_test "delete fork 5" "" "Delete 5"
188gdb_test "info fork 5" "No fork number 5." "Did delete 5"
189gdb_test "delete fork 6" "" "Delete 6"
190gdb_test "info fork 6" "No fork number 6." "Did delete 6"
191gdb_test "delete fork 7" "" "Delete 7"
192gdb_test "info fork 7" "No fork number 7." "Did delete 7"
193gdb_test "delete fork 8" "" "Delete 8"
194gdb_test "info fork 8" "No fork number 8." "Did delete 8"
195gdb_test "delete fork 9" "" "Delete 9"
196gdb_test "info fork 9" "No fork number 9." "Did delete 9"
197gdb_test "delete fork 10" "" "Delete 10"
198gdb_test "info fork 10" "No fork number 10." "Did delete 10"
199gdb_test "delete fork 11" "" "Delete 11"
200gdb_test "info fork 11" "No fork number 11." "Did delete 11"
201gdb_test "delete fork 12" "" "Delete 12"
202gdb_test "info fork 12" "No fork number 12." "Did delete 12"
203gdb_test "delete fork 13" "" "Delete 13"
204gdb_test "info fork 13" "No fork number 13." "Did delete 13"
205gdb_test "delete fork 14" "" "Delete 14"
206gdb_test "info fork 14" "No fork number 14." "Did delete 14"
207gdb_test "delete fork 15" "" "Delete 15"
208gdb_test "info fork 15" "No fork number 15." "Did delete 15"
099ac3dd
MS
209
210return 0
211
This page took 0.33457 seconds and 4 git commands to generate.