Commit | Line | Data |
---|---|---|
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 | |
16 | if $tracelevel then { | |
17 | strace $tracelevel | |
18 | } | |
19 | ||
db9d7fc5 | 20 | if { [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 | # | |
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}] != "" } { | |
b60f0898 JB |
39 | untested multi-forks.exp |
40 | return -1 | |
099ac3dd MS |
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. | |
cb4dce93 JK |
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 | } | |
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 | ||
123 | runto_main | |
6c95b8df | 124 | gdb_test "set follow-fork child" |
cb4dce93 | 125 | continue_to_exit_bp_loc |
099ac3dd MS |
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 | ||
099ac3dd | 132 | runto_main |
6c95b8df | 133 | gdb_test "set follow-fork parent" "" "" |
cb4dce93 | 134 | continue_to_exit_bp_loc |
099ac3dd MS |
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 | ||
2277426b PA |
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 | ||
099ac3dd | 156 | runto_main |
cb4dce93 | 157 | gdb_breakpoint $exit_bp_loc |
099ac3dd MS |
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, | |
2277426b | 168 | # eventually winding up with 16 inferiors. |
099ac3dd MS |
169 | # |
170 | ||
9078e690 DJ |
171 | for {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 | |
178 | gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit 16" | |
2277426b PA |
179 | gdb_test "info inferiors" " 5 .* 4 .* 3 .* 2 .*" "info inferior 16" |
180 | gdb_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] |
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" | |
099ac3dd MS |
195 | |
196 | # | |
2277426b PA |
197 | # Test kill inferior |
198 | # | |
099ac3dd | 199 | |
2277426b | 200 | gdb_test "kill inferior 6" "" "Kill 6" |
6c95b8df | 201 | gdb_test "info inferior 6" "<null>.*" "Did kill 6" |
2277426b | 202 | gdb_test "kill inferior 7" "" "Kill 7" |
6c95b8df | 203 | gdb_test "info inferior 7" "<null>.*" "Did kill 7" |
2277426b | 204 | gdb_test "kill inferior 8" "" "Kill 8" |
6c95b8df | 205 | gdb_test "info inferior 8" "<null>.*" "Did kill 8" |
2277426b | 206 | gdb_test "kill inferior 9" "" "Kill 9" |
6c95b8df | 207 | gdb_test "info inferior 9" "<null>.*" "Did kill 9" |
2277426b | 208 | gdb_test "kill inferior 10" "" "Kill 10" |
6c95b8df | 209 | gdb_test "info inferior 10" "<null>.*" "Did kill 10" |
2277426b | 210 | gdb_test "kill inferior 11" "" "Kill 11" |
6c95b8df | 211 | gdb_test "info inferior 11" "<null>.*" "Did kill 11" |
2277426b | 212 | gdb_test "kill inferior 12" "" "Kill 12" |
6c95b8df | 213 | gdb_test "info inferior 12" "<null>.*" "Did kill 12" |
2277426b | 214 | gdb_test "kill inferior 13" "" "Kill 13" |
6c95b8df | 215 | gdb_test "info inferior 13" "<null>.*" "Did kill 13" |
2277426b | 216 | gdb_test "kill inferior 14" "" "Kill 14" |
6c95b8df | 217 | gdb_test "info inferior 14" "<null>.*" "Did kill 14" |
2277426b | 218 | gdb_test "kill inferior 15" "" "Kill 15" |
6c95b8df | 219 | gdb_test "info inferior 15" "<null>.*" "Did kill 15" |
2277426b | 220 | gdb_test "kill inferior 16" "" "Kill 16" |
6c95b8df | 221 | gdb_test "info inferior 16" "<null>.*" "Did kill 16" |
099ac3dd MS |
222 | |
223 | return 0 |