catch-fork-kill.exp revision 1.1.1.1
1# Copyright 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# When we intercept a fork/vfork with a catchpoint, the child is left
17# stopped.  At that point, if we kill the parent, we should kill the
18# child as well.  This test makes sure that works.  See PR gdb/19494.
19
20# The main idea of the test is make sure that when the program stops
21# for a fork catchpoint, and the user kills the parent, gdb also kills
22# the unfollowed fork child.  Since the child hasn't been added as an
23# inferior at that point, we need some other portable way to detect
24# that the child is gone.  The test uses a pipe for that.  The program
25# forks twice, so you have grandparent, child and grandchild.  The
26# grandchild inherits the write side of the pipe.  The grandparent
27# hangs reading from the pipe, since nothing ever writes to it.  If,
28# when GDB kills the child, it also kills the grandchild, then the
29# grandparent's pipe read returns 0/EOF and the test passes.
30# Otherwise, if GDB doesn't kill the grandchild, then the pipe read
31# never returns and the test times out.
32
33standard_testfile
34
35# Build two programs -- one for fork, and another for vfork.
36set testfile_fork "${testfile}-fork"
37set testfile_vfork "${testfile}-vfork"
38
39foreach kind {"fork" "vfork"} {
40    set compile_options "debug additional_flags=-DFORK=$kind"
41    set testfile [set testfile_$kind]
42    if {[build_executable $testfile.exp $testfile ${srcfile} \
43	     ${compile_options}] == -1} {
44	untested "failed to compile $testfile"
45	return -1
46    }
47}
48
49# The test proper.  FORK_KIND is either "fork" or "vfork".  EXIT_KIND
50# is either "exit" (run the parent to exit) or "kill" (kill parent).
51
52proc do_test {fork_kind exit_kind} {
53    global testfile testfile_$fork_kind
54
55    set testfile [set testfile_$fork_kind]
56
57    with_test_prefix "$fork_kind" {
58	clean_restart $testfile
59
60	if ![runto_main] {
61	    untested "could not run to main"
62	    return -1
63	}
64
65	gdb_test_no_output "set follow-fork child"
66	gdb_test_no_output "set detach-on-fork off"
67
68	gdb_test "catch $fork_kind" "Catchpoint .*($fork_kind).*"
69
70	gdb_test "continue" \
71	    "Catchpoint \[0-9\]* \\(${fork_kind}ed process \[0-9\]*\\),.*" \
72	    "continue to child ${fork_kind}"
73
74	gdb_test "continue" \
75	    "Catchpoint \[0-9\]* \\(${fork_kind}ed process \[0-9\]*\\),.*" \
76	    "continue to grandchild ${fork_kind}"
77
78	gdb_test "kill inferior 2" "" "kill child" \
79	    "Kill the program being debugged.*y or n. $" "y"
80
81	gdb_test "inferior 1" "Switching to inferior 1 .*" "switch to parent"
82
83	if {$exit_kind == "exit"} {
84	    gdb_test "break grandparent_done" "Breakpoint .*"
85	    gdb_test "continue" "hit Breakpoint .*, grandparent_done.*"
86	} elseif {$exit_kind == "kill"} {
87	    gdb_test "kill" "" "kill parent" \
88		"Kill the program being debugged.*y or n. $" "y"
89	} else {
90	    perror "unreachable"
91	}
92    }
93}
94
95foreach_with_prefix fork-kind {"fork" "vfork"} {
96    foreach_with_prefix exit-kind {"exit" "kill"} {
97	do_test ${fork-kind} ${exit-kind}
98    }
99}
100