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