2009-10-19 Pedro Alves <pedro@codesourcery.com>
[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
6c95b8df 124gdb_test "set follow-fork child"
cb4dce93 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
6c95b8df 133gdb_test "set follow-fork 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
2277426b
PA
143# detach-on-fork isn't implemented on hpux.
144#
145if {![istarget "*-*-linux*"]} then {
146 continue
147}
148
149# Start with a fresh gdb
150
151gdb_exit
152gdb_start
153gdb_reinitialize_dir $srcdir/$subdir
154gdb_load ${binfile}
155
099ac3dd 156runto_main
cb4dce93 157gdb_breakpoint $exit_bp_loc
099ac3dd
MS
158
159gdb_test "help set detach-on-fork" "whether gdb will detach the child.*" \
160 "help set detach"
161
162gdb_test "show detach-on-fork" "on." "show detach default on"
163
164gdb_test "set detach off" "" "set detach off"
165
166#
167# We will now run every fork up to the exit bp,
2277426b 168# eventually winding up with 16 inferiors.
099ac3dd
MS
169#
170
9078e690
DJ
171for {set i 1} {$i <= 15} {incr i} {
172 gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit $i"
2277426b
PA
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"
9078e690 176}
099ac3dd
MS
177
178gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit 16"
2277426b
PA
179gdb_test "info inferiors" " 5 .* 4 .* 3 .* 2 .*" "info inferior 16"
180gdb_test "inferior 2" " main .*" "restart final"
099ac3dd
MS
181
182#
183# Now we should examine all the pids.
184#
185
186#
2277426b 187# Test detach inferior
099ac3dd
MS
188#
189
2277426b
PA
190# [assumes we're at #1]
191gdb_test "detach inferior 2" "Detaching .*" "Detach 2"
192gdb_test "detach inferior 3" "Detaching .*" "Detach 3"
193gdb_test "detach inferior 4" "Detaching .*" "Detach 4"
194gdb_test "detach inferior 5" "Detaching .*" "Detach 5"
099ac3dd
MS
195
196#
2277426b
PA
197# Test kill inferior
198#
099ac3dd 199
2277426b 200gdb_test "kill inferior 6" "" "Kill 6"
6c95b8df 201gdb_test "info inferior 6" "<null>.*" "Did kill 6"
2277426b 202gdb_test "kill inferior 7" "" "Kill 7"
6c95b8df 203gdb_test "info inferior 7" "<null>.*" "Did kill 7"
2277426b 204gdb_test "kill inferior 8" "" "Kill 8"
6c95b8df 205gdb_test "info inferior 8" "<null>.*" "Did kill 8"
2277426b 206gdb_test "kill inferior 9" "" "Kill 9"
6c95b8df 207gdb_test "info inferior 9" "<null>.*" "Did kill 9"
2277426b 208gdb_test "kill inferior 10" "" "Kill 10"
6c95b8df 209gdb_test "info inferior 10" "<null>.*" "Did kill 10"
2277426b 210gdb_test "kill inferior 11" "" "Kill 11"
6c95b8df 211gdb_test "info inferior 11" "<null>.*" "Did kill 11"
2277426b 212gdb_test "kill inferior 12" "" "Kill 12"
6c95b8df 213gdb_test "info inferior 12" "<null>.*" "Did kill 12"
2277426b 214gdb_test "kill inferior 13" "" "Kill 13"
6c95b8df 215gdb_test "info inferior 13" "<null>.*" "Did kill 13"
2277426b 216gdb_test "kill inferior 14" "" "Kill 14"
6c95b8df 217gdb_test "info inferior 14" "<null>.*" "Did kill 14"
2277426b 218gdb_test "kill inferior 15" "" "Kill 15"
6c95b8df 219gdb_test "info inferior 15" "<null>.*" "Did kill 15"
2277426b 220gdb_test "kill inferior 16" "" "Kill 16"
6c95b8df 221gdb_test "info inferior 16" "<null>.*" "Did kill 16"
099ac3dd
MS
222
223return 0
This page took 0.360927 seconds and 4 git commands to generate.