1# Copyright 2016-2023 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" 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 return -1 62 } 63 64 gdb_test_no_output "set follow-fork child" 65 gdb_test_no_output "set detach-on-fork off" 66 67 gdb_test "catch $fork_kind" "Catchpoint .*($fork_kind).*" 68 69 gdb_test "continue" \ 70 "Catchpoint \[0-9\]* \\(${fork_kind}ed process \[0-9\]*\\),.*" \ 71 "continue to child ${fork_kind}" 72 73 gdb_test "continue" \ 74 "Catchpoint \[0-9\]* \\(${fork_kind}ed process \[0-9\]*\\),.*" \ 75 "continue to grandchild ${fork_kind}" 76 77 gdb_test "kill inferior 2" "" "kill child" 78 79 gdb_test "inferior 1" "Switching to inferior 1 .*" "switch to parent" 80 81 if {$exit_kind == "exit"} { 82 gdb_test "break grandparent_done" "Breakpoint .*" 83 gdb_test "continue" "hit Breakpoint .*, grandparent_done.*" 84 } elseif {$exit_kind == "kill"} { 85 gdb_test "kill" "" "kill parent" \ 86 "Kill the program being debugged.*y or n. $" "y" 87 } else { 88 perror "unreachable" 89 } 90 } 91} 92 93foreach_with_prefix fork-kind {"fork" "vfork"} { 94 foreach_with_prefix exit-kind {"exit" "kill"} { 95 do_test ${fork-kind} ${exit-kind} 96 } 97} 98