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