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