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