2012-11-02 Pedro Alves <palves@redhat.com>
[deliverable/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 # Various tests of gdb's ability to follow the parent or child of a
17 # Unix vfork system call. A vfork parent is blocked until the child
18 # either execs or exits --- since those events take somewhat different
19 # code paths in GDB, both variants are exercised.
20
21 if { [is_remote target] || ![isnative] } then {
22 continue
23 }
24
25 # Until "set follow-fork-mode" and "catch vfork" are implemented on
26 # other targets...
27 #
28 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
29 continue
30 }
31
32 # Test to see if we are on an HP-UX 10.20 and if so,
33 # do not run these tests as catching vfork is disabled for
34 # 10.20.
35
36 if [istarget "hppa*-hp-hpux10.20"] then {
37 return 0
38 }
39
40 # NOTE drow/2002-12-06: I don't know what the referenced kernel problem
41 # is, but it appears to be fixed in recent HP/UX versions.
42
43 ##if [istarget "hppa2.0w-hp-hpux*"] {
44 ## warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
45 ## return 0
46 ##}
47
48 standard_testfile
49
50 if {[build_executable $testfile.exp $testfile $srcfile {debug}] == -1} {
51 untested "failed to compile $testfile"
52 return -1
53 }
54
55 set testfile2 "vforked-prog"
56 set srcfile2 ${testfile2}.c
57
58 if {[build_executable $testfile.exp $testfile2 $srcfile2 {debug}] == -1} {
59 untested "failed to compile $testfile2"
60 return -1
61 }
62
63 # A few of these tests require a little more time than the standard
64 # timeout allows.
65 set oldtimeout $timeout
66 set timeout [expr "$timeout + 10"]
67
68 # Start with a fresh GDB, with verbosity enabled, and run to main. On
69 # error, behave as "return", so we don't try to continue testing with
70 # a borked session.
71 proc setup_gdb {} {
72 global testfile
73
74 clean_restart $testfile
75
76 # The "Detaching..." and "Attaching..." messages may be hidden by
77 # default.
78 gdb_test_no_output "set verbose"
79
80 if ![runto_main] {
81 return -code return
82 }
83 }
84
85 proc check_vfork_catchpoints {} {
86 global gdb_prompt
87 global has_vfork_catchpoints
88
89 setup_gdb
90
91 # Verify that the system supports "catch vfork".
92 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
93 set has_vfork_catchpoints 0
94 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
95 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
96 unsupported "continue to first vfork catchpoint"
97 }
98 -re ".*Catchpoint.*$gdb_prompt $" {
99 set has_vfork_catchpoints 1
100 pass "continue to first vfork catchpoint"
101 }
102 }
103
104 if {$has_vfork_catchpoints == 0} {
105 unsupported "vfork catchpoints"
106 return -code return
107 }
108 }
109
110 proc vfork_parent_follow_through_step {} {
111 with_test_prefix "vfork parent follow, through step" {
112 global gdb_prompt
113
114 setup_gdb
115
116 gdb_test_no_output "set follow-fork parent"
117
118 set test "step"
119 gdb_test_multiple "next" $test {
120 -re "Detaching after fork from.*if \\(pid == 0\\).*$gdb_prompt " {
121 pass $test
122 }
123 }
124 # The child has been detached; allow time for any output it might
125 # generate to arrive, so that output doesn't get confused with
126 # any gdb_expected debugger output from a subsequent testpoint.
127 #
128 exec sleep 1
129 }}
130
131 proc vfork_parent_follow_to_bp {} {
132 with_test_prefix "vfork parent follow, to bp" {
133 global gdb_prompt
134 global srcfile
135
136 setup_gdb
137
138 gdb_test_no_output "set follow-fork parent"
139
140 set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"]
141 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
142
143 set test "continue to bp"
144 gdb_test_multiple "continue" $test {
145 -re ".*Detaching after fork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
146 pass $test
147 }
148 }
149 # The child has been detached; allow time for any output it might
150 # generate to arrive, so that output doesn't get confused with
151 # any expected debugger output from a subsequent testpoint.
152 #
153 exec sleep 1
154 }}
155
156 proc vfork_child_follow_to_exit {} {
157 with_test_prefix "vfork child follow, to exit" {
158 global gdb_prompt
159
160 setup_gdb
161
162 gdb_test_no_output "set follow-fork child"
163
164 set test "continue to child exit"
165 gdb_test_multiple "continue" $test {
166 -re "Couldn't get registers.*$gdb_prompt " {
167 setup_kfail "gdb/14766" *-*-*
168 fail "$test"
169 }
170 -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
171 pass $test
172 }
173 }
174 # The parent has been detached; allow time for any output it might
175 # generate to arrive, so that output doesn't get confused with
176 # any gdb_expected debugger output from a subsequent testpoint.
177 #
178 exec sleep 1
179 }}
180
181 proc vfork_and_exec_child_follow_to_main_bp {} {
182 with_test_prefix "vfork and exec child follow, to main bp" {
183 global gdb_prompt
184 global srcfile2
185
186 setup_gdb
187
188 gdb_test_no_output "set follow-fork child"
189
190 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
191
192 set test "continue to bp"
193 gdb_test_multiple "continue" $test {
194 -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
195 pass $test
196 }
197 }
198 # The parent has been detached; allow time for any output it might
199 # generate to arrive, so that output doesn't get confused with
200 # any gdb_expected debugger output from a subsequent testpoint.
201 #
202 exec sleep 1
203 }}
204
205 proc vfork_and_exec_child_follow_through_step {} {
206 with_test_prefix "vfork and exec child follow, through step" {
207 global gdb_prompt
208 global srcfile2
209
210 if { [istarget "hppa*-*-hpux*"] && ![istarget "hppa*-*-hpux11.*"] } {
211 # This test cannot be performed prior to HP-UX 10.30, because
212 # ptrace-based debugging of a vforking program basically doesn't
213 # allow the child to do things like hit a breakpoint between a
214 # vfork and exec. This means that saying "set follow-fork
215 # child; next" at a vfork() call won't work, because the
216 # implementation of "next" sets a "step resume" breakpoint at
217 # the return from the vfork(), which the child will hit on its
218 # way to exec'ing.
219 #
220 verbose "vfork child-following next test ignored for pre-HP/UX-10.30 targets."
221 return 0
222 }
223
224 setup_gdb
225
226 gdb_test_no_output "set follow-fork child"
227
228 set test "step over vfork"
229 if { [istarget "hppa*-*-hpux*"]} {
230 # Since the child cannot be debugged until after it has exec'd,
231 # and since there's a bp on "main" in the parent, and since the
232 # bp's for the parent are recomputed in the exec'd child, the
233 # step through a vfork should land us in the "main" for the
234 # exec'd child, too.
235 #
236 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
237 gdb_test_multiple "next" $test {
238 -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
239 pass "$test"
240 }
241 }
242 } else {
243 # The ideal support is to be able to debug the child even
244 # before it execs. Thus, "next" lands on the next line after
245 # the vfork.
246 gdb_test_multiple "next" $test {
247 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
248 pass "$test"
249 }
250 }
251 }
252 # The parent has been detached; allow time for any output it might
253 # generate to arrive, so that output doesn't get confused with
254 # any expected debugger output from a subsequent testpoint.
255 #
256 exec sleep 1
257 }}
258
259 proc tcatch_vfork_then_parent_follow {} {
260 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
261 global gdb_prompt
262 global srcfile
263
264 setup_gdb
265
266 gdb_test_no_output "set follow-fork parent"
267
268 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
269
270 # HP-UX 10.20 seems to stop you in "vfork", while more recent
271 # HP-UXs stop you in "_vfork".
272 set test "continue to vfork"
273 gdb_test_multiple "continue" $test {
274 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
275 pass $test
276 }
277 -re "vfork \\(\\) at.*$gdb_prompt " {
278 pass $test
279 }
280 }
281
282 set linenum [gdb_get_line_number "pid = vfork ();"]
283 set test "finish"
284 gdb_test_multiple "finish" $test {
285 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
286 pass $test
287 }
288 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
289 send_gdb "finish\n"
290 exp_continue
291 }
292 }
293 # The child has been detached; allow time for any output it might
294 # generate to arrive, so that output doesn't get confused with
295 # any expected debugger output from a subsequent testpoint.
296 #
297 exec sleep 1
298 }}
299
300 proc tcatch_vfork_then_child_follow_exec {} {
301 with_test_prefix "vfork child follow, finish after tcatch vfork" {
302 global gdb_prompt
303 global srcfile
304 global srcfile2
305
306 setup_gdb
307
308 gdb_test_no_output "set follow-fork child"
309
310 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
311
312 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
313 # stop you in "_vfork".
314 set test "continue to vfork"
315 gdb_test_multiple "continue" $test {
316 -re "vfork \\(\\) at .*$gdb_prompt $" {
317 pass $test
318 }
319 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
320 pass $test
321 }
322 }
323
324 set linenum1 [gdb_get_line_number "pid = vfork ();"]
325 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
326
327 set test "finish"
328 gdb_test_multiple "finish" $test {
329 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
330 pass $test
331 }
332 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
333 send_gdb "finish\n"
334 exp_continue
335 }
336 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
337 pass "$test (followed exec)"
338 }
339 }
340 # The parent has been detached; allow time for any output it might
341 # generate to arrive, so that output doesn't get confused with
342 # any expected debugger output from a subsequent testpoint.
343 #
344 exec sleep 1
345 }}
346
347 proc tcatch_vfork_then_child_follow_exit {} {
348 with_test_prefix "vfork child follow, finish after tcatch vfork" {
349 global gdb_prompt
350 global srcfile
351
352 setup_gdb
353
354 gdb_test_no_output "set follow-fork child"
355
356 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
357
358 # HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
359 # stop you in "_vfork".
360 set test "continue to vfork"
361 gdb_test_multiple "continue" $test {
362 -re "vfork \\(\\) at .*$gdb_prompt $" {
363 pass $test
364 }
365 -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " {
366 pass $test
367 }
368 }
369
370 set test "finish"
371 gdb_test_multiple "finish" $test {
372 -re "Couldn't get registers.*$gdb_prompt " {
373 setup_kfail "gdb/14766" *-*-*
374 fail "$test "
375 }
376 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
377 setup_kfail "gdb/14762" *-*-*
378 fail $test
379 }
380 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
381 pass $test
382 }
383 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
384 send_gdb "finish\n"
385 exp_continue
386 }
387 }
388 # The parent has been detached; allow time for any output it might
389 # generate to arrive, so that output doesn't get confused with
390 # any expected debugger output from a subsequent testpoint.
391 #
392 exec sleep 1
393 }}
394
395 proc do_vfork_and_follow_parent_tests {} {
396 global gdb_prompt
397
398 # Try following the parent process by stepping through a call to
399 # vfork. Do this without catchpoints.
400 vfork_parent_follow_through_step
401
402 # Try following the parent process by setting a breakpoint on the
403 # other side of a vfork, and running to that point. Do this
404 # without catchpoints.
405 vfork_parent_follow_to_bp
406
407 # Try catching a vfork, and stepping out to the parent.
408 #
409 tcatch_vfork_then_parent_follow
410 }
411
412 proc do_vfork_and_follow_child_tests_exec {} {
413 # Try following the child process by just continuing through the
414 # vfork, and letting the parent's breakpoint on "main" be auto-
415 # magically reset in the child.
416 #
417 vfork_and_exec_child_follow_to_main_bp
418
419 # Try following the child process by stepping through a call to
420 # vfork. The child also executes an exec. Since the child cannot
421 # be debugged until after it has exec'd, and since there's a bp on
422 # "main" in the parent, and since the bp's for the parent are
423 # recomputed in the exec'd child, the step through a vfork should
424 # land us in the "main" for the exec'd child, too.
425 #
426 vfork_and_exec_child_follow_through_step
427
428 # Try catching a vfork, and stepping out to the child.
429 #
430 tcatch_vfork_then_child_follow_exec
431
432 # Test the ability to follow both child and parent of a vfork. Do
433 # this without catchpoints.
434 # ??rehrauer: NYI. Will add testpoints here when implemented.
435 #
436
437 # Test the ability to have the debugger ask the user at vfork-time
438 # whether to follow the parent, child or both. Do this without
439 # catchpoints.
440 # ??rehrauer: NYI. Will add testpoints here when implemented.
441 #
442 }
443
444 proc do_vfork_and_follow_child_tests_exit {} {
445 # Try following the child process by just continuing through the
446 # vfork, and letting the child exit.
447 #
448 vfork_child_follow_to_exit
449
450 # Try catching a vfork, and stepping out to the child.
451 #
452 tcatch_vfork_then_child_follow_exit
453 }
454
455 with_test_prefix "check vfork support" {
456 # Check that vfork catchpoints are supported, as an indicator for
457 # whether vfork-following is supported.
458 check_vfork_catchpoints
459 }
460
461 # Follow parent and follow child vfork tests with a child that execs.
462 with_test_prefix "exec" {
463 # These are tests of gdb's ability to follow the parent of a Unix
464 # vfork system call. The child will subsequently call a variant
465 # of the Unix exec system call.
466 do_vfork_and_follow_parent_tests
467
468 # These are tests of gdb's ability to follow the child of a Unix
469 # vfork system call. The child will subsequently call a variant
470 # of a Unix exec system call.
471 #
472 do_vfork_and_follow_child_tests_exec
473 }
474
475 # Switch to test the case of the child exiting. We can't use
476 # standard_testfile here because we don't want to overwrite the binary
477 # of the previous tests.
478 set testfile "foll-vfork-exit"
479 set srcfile ${testfile}.c
480 set binfile [standard_output_file ${testfile}]
481
482 if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
483 untested "failed to build $testfile"
484 return
485 }
486
487 # Follow parent and follow child vfork tests with a child that exits.
488 with_test_prefix "exit" {
489 # These are tests of gdb's ability to follow the parent of a Unix
490 # vfork system call. The child will subsequently exit.
491 do_vfork_and_follow_parent_tests
492
493 # These are tests of gdb's ability to follow the child of a Unix
494 # vfork system call. The child will subsequently exit.
495 #
496 do_vfork_and_follow_child_tests_exit
497 }
498
499 set timeout $oldtimeout
500 return 0
This page took 0.040672 seconds and 5 git commands to generate.