]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - gdb/testsuite/gdb.base/foll-vfork.exp
2012-11-02 Pedro Alves <palves@redhat.com>
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-vfork.exp
1 # Copyright 1997, 1999, 2007-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 { [is_remote target] || ![isnative] } then {
17 continue
18 }
19
20 # Until "set follow-fork-mode" and "catch vfork" are implemented on
21 # other targets...
22 #
23 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
24 continue
25 }
26
27 # Test to see if we are on an HP-UX 10.20 and if so,
28 # do not run these tests as catching vfork is disabled for
29 # 10.20.
30
31 if [istarget "hppa*-hp-hpux10.20"] then {
32 return 0
33 }
34
35 # NOTE drow/2002-12-06: I don't know what the referenced kernel problem
36 # is, but it appears to be fixed in recent HP/UX versions.
37
38 ##if [istarget "hppa2.0w-hp-hpux*"] {
39 ## warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
40 ## return 0
41 ##}
42
43 standard_testfile
44
45 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
46 untested "failed to compile $testfile"
47 return -1
48 }
49
50 set testfile2 "vforked-prog"
51 set srcfile2 ${testfile2}.c
52
53 if {[build_executable $testfile.exp $testfile2 $srcfile2 {debug}] == -1} {
54 untested "failed to compile $testfile2"
55 return -1
56 }
57
58 # A few of these tests require a little more time than the standard
59 # timeout allows.
60 set oldtimeout $timeout
61 set timeout [expr "$timeout + 10"]
62
63 proc check_vfork_catchpoints {} {
64 global gdb_prompt
65 global has_vfork_catchpoints
66
67 # Verify that the system supports "catch vfork".
68 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
69 set has_vfork_catchpoints 0
70 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
71 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
72 unsupported "continue to first vfork catchpoint"
73 }
74 -re ".*Catchpoint.*$gdb_prompt $" {
75 set has_vfork_catchpoints 1
76 pass "continue to first vfork catchpoint"
77 }
78 }
79
80 if {$has_vfork_catchpoints == 0} {
81 unsupported "vfork catchpoints"
82 return -code return
83 }
84 }
85
86 proc vfork_parent_follow_through_step {} {
87 global gdb_prompt
88
89 gdb_test_no_output "set follow-fork parent" \
90 "set follow-fork parent, vfork through step"
91
92 set test "vfork parent follow, through step"
93 gdb_test_multiple "next" $test {
94 -re "Detaching after fork from.*if \\(pid == 0\\).*$gdb_prompt " {
95 pass "vfork parent follow, through step"
96 }
97 }
98 # The child has been detached; allow time for any output it might
99 # generate to arrive, so that output doesn't get confused with
100 # any gdb_expected debugger output from a subsequent testpoint.
101 #
102 exec sleep 1
103 }
104
105 proc vfork_parent_follow_to_bp {} {
106 global gdb_prompt
107 global srcfile
108
109 gdb_test_no_output "set follow-fork parent" \
110 "set follow-fork parent, vfork to bp"
111
112 set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"]
113 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
114
115 set test "vfork parent follow, to bp"
116 gdb_test_multiple "continue" $test {
117 -re ".*Detaching after fork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
118 pass $test
119 }
120 }
121 # The child has been detached; allow time for any output it might
122 # generate to arrive, so that output doesn't get confused with
123 # any expected debugger output from a subsequent testpoint.
124 #
125 exec sleep 1
126 }
127
128 # Kill child and reload symbols.
129 proc kill_child {} {
130 global binfile
131 global gdb_prompt
132
133 set test "killing inferior"
134 gdb_test_multiple "kill" $test {
135 -re ".*Kill the program being debugged.*y or n. $" {
136 gdb_test_no_output "y" ""
137 set test2 "file $binfile"
138 gdb_test_multiple "file $binfile" $test2 {
139 -re ".*Load new symbol table from.*y or n. $" {
140 send_gdb "y\n"
141 gdb_test_multiple "" "loading symbols" {
142 -re "Reading symbols from.*$gdb_prompt $" {
143 }
144 }
145 }
146 -re ".*gdb_prompt $" {
147 }
148 }
149 }
150 -re ".*$gdb_prompt $" {
151 }
152 }
153 }
154
155 proc vfork_and_exec_child_follow_to_main_bp {} {
156 global gdb_prompt
157 global srcfile2
158
159 gdb_test_no_output \
160 "set follow-fork child" \
161 "set follow-fork child, vfork and exec to main bp"
162
163 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
164
165 set test "vfork and exec child follow, to main bp"
166 gdb_test_multiple "continue" $test {
167 -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
168 pass $test
169 }
170 }
171 # The parent has been detached; allow time for any output it might
172 # generate to arrive, so that output doesn't get confused with
173 # any gdb_expected debugger output from a subsequent testpoint.
174 #
175 exec sleep 1
176
177 # Explicitly kill this child, or a subsequent rerun actually runs
178 # the exec'd child, not the original program...
179 kill_child
180 }
181
182 proc vfork_and_exec_child_follow_through_step {} {
183 global gdb_prompt
184 global srcfile2
185
186 # This test cannot be performed prior to HP-UX 10.30, because ptrace-based
187 # debugging of a vforking program basically doesn't allow the child to do
188 # things like hit a breakpoint between a vfork and exec. This means that
189 # saying "set follow-fork child; next" at a vfork() call won't work, because
190 # the implementation of "next" sets a "step resume" breakpoint at the
191 # return from the vfork(), which the child will hit on its way to exec'ing.
192 #
193 if { ![istarget "hppa*-*-hpux11.*"] } {
194 verbose "vfork child-following next test ignored for non-hppa or pre-HP/UX-10.30 targets."
195 return 0
196 }
197
198 gdb_test_no_output \
199 "set follow-fork child" \
200 "set follow-fork child, vfork and exec through step"
201
202 set test "vfork and exec child follow, through step"
203 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
204 gdb_test_multiple "next" $test {
205 -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
206 pass "$test"
207 }
208 }
209 # The parent has been detached; allow time for any output it might
210 # generate to arrive, so that output doesn't get confused with
211 # any expected debugger output from a subsequent testpoint.
212 #
213 exec sleep 1
214
215 # Explicitly kill this child, or a subsequent rerun actually runs
216 # the exec'd child, not the original program...
217 kill_child
218 }
219
220 proc tcatch_vfork_then_parent_follow {} {
221 global gdb_prompt
222 global srcfile
223
224 gdb_test_no_output \
225 "set follow-fork parent" \
226 "set follow-fork parent, tcatch vfork"
227
228 gdb_test "tcatch vfork" \
229 "Catchpoint .*(vfork).*" \
230 "vfork parent follow, set tcatch vfork"
231
232 # HP-UX 10.20 seems to stop you in "vfork", while more recent
233 # HP-UXs stop you in "_vfork".
234 set test "vfork parent follow, tcatch vfork"
235 gdb_test_multiple "continue" $test {
236 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
237 pass $test
238 }
239 -re "vfork \\(\\) at.*$gdb_prompt " {
240 pass $test
241 }
242 }
243
244 set linenum [gdb_get_line_number "pid = vfork ();"]
245 set test "vfork parent follow, finish after tcatch vfork"
246 gdb_test_multiple "finish" $test {
247 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
248 pass $test
249 }
250 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
251 send_gdb "finish\n"
252 exp_continue
253 }
254 }
255 # The child has been detached; allow time for any output it might
256 # generate to arrive, so that output doesn't get confused with
257 # any expected debugger output from a subsequent testpoint.
258 #
259 exec sleep 1
260 }
261
262 proc tcatch_vfork_then_child_follow {} {
263 global gdb_prompt
264 global srcfile
265 global srcfile2
266
267 gdb_test_no_output \
268 "set follow-fork child" \
269 "set follow-fork child, tcatch vfork"
270
271 gdb_test "tcatch vfork" \
272 "Catchpoint .*(vfork).*" \
273 "vfork child follow, set tcatch vfork"
274
275 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
276 # stop you in "_vfork".
277 set test "vfork child follow, tcatch vfork"
278 gdb_test_multiple "continue" $test {
279 -re "vfork \\(\\) at .*$gdb_prompt $" {
280 pass $test
281 }
282 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
283 pass $test
284 }
285 }
286
287 set linenum1 [gdb_get_line_number "pid = vfork ();"]
288 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
289
290 set test "vfork child follow, finish after tcatch vfork"
291 gdb_test_multiple "finish" $test {
292 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
293 pass $test
294 }
295 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
296 send_gdb "finish\n"
297 exp_continue
298 }
299 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
300 pass "$test (followed exec)"
301 }
302 }
303 # The parent has been detached; allow time for any output it might
304 # generate to arrive, so that output doesn't get confused with
305 # any expected debugger output from a subsequent testpoint.
306 #
307 exec sleep 1
308 }
309
310 proc do_vfork_and_exec_tests {} {
311 global gdb_prompt
312
313 # Check that vfork catchpoints are supported, as an indicator for whether
314 # vfork-following is supported.
315 if [runto_main] then { check_vfork_catchpoints }
316
317 # Try following the parent process by stepping through a call to
318 # vfork. Do this without catchpoints.
319 if [runto_main] then { vfork_parent_follow_through_step }
320
321 # Try following the parent process by setting a breakpoint on the
322 # other side of a vfork, and running to that point. Do this
323 # without catchpoints.
324 if [runto_main] then { vfork_parent_follow_to_bp }
325
326 # Try following the child process by just continuing through the
327 # vfork, and letting the parent's breakpoint on "main" be auto-
328 # magically reset in the child.
329 #
330 if [runto_main] then { vfork_and_exec_child_follow_to_main_bp }
331
332 # Try following the child process by stepping through a call to
333 # vfork. The child also executes an exec. Since the child cannot
334 # be debugged until after it has exec'd, and since there's a bp on
335 # "main" in the parent, and since the bp's for the parent are
336 # recomputed in the exec'd child, the step through a vfork should
337 # land us in the "main" for the exec'd child, too.
338 #
339 if [runto_main] then { vfork_and_exec_child_follow_through_step }
340
341 # Try catching a vfork, and stepping out to the parent.
342 #
343 if [runto_main] then { tcatch_vfork_then_parent_follow }
344
345 # Try catching a vfork, and stepping out to the child.
346 #
347 if [runto_main] then { tcatch_vfork_then_child_follow }
348
349 # Test the ability to follow both child and parent of a vfork. Do
350 # this without catchpoints.
351 # ??rehrauer: NYI. Will add testpoints here when implemented.
352 #
353
354 # Test the ability to have the debugger ask the user at vfork-time
355 # whether to follow the parent, child or both. Do this without
356 # catchpoints.
357 # ??rehrauer: NYI. Will add testpoints here when implemented.
358 #
359 }
360
361 # Start with a fresh gdb
362 clean_restart $testfile
363
364 # The "Detaching..." and "Attaching..." messages may be hidden by
365 # default.
366 gdb_test_no_output "set verbose"
367
368 # This is a test of gdb's ability to follow the parent or child
369 # of a Unix vfork() system call. (The child will subsequently
370 # call a variant of a Unix exec() system call.)
371 #
372 do_vfork_and_exec_tests
373
374 set timeout $oldtimeout
375 return 0