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