1# Copyright 2005-2024 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# Until "set follow-fork-mode" and "catch fork" are implemented on 17# other targets... 18# 19require {istarget "*-*-linux*"} 20 21 22standard_testfile .c 23 24set flags {} 25lappend flags debug 26lappend_include_file flags $srcdir/lib/unbuffer_output.c 27 28if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable $flags] != "" } { 29 untested "failed to compile" 30 return -1 31} 32 33# Start with a fresh gdb 34 35clean_restart ${binfile} 36 37global gdb_prompt 38 39# This is a test of gdb's ability to follow the parent, child or both 40# parent and child of multiple Unix fork() system calls. 41 42set exit_bp_loc [gdb_get_line_number "Set exit breakpoint here."] 43 44# Insert a breakpoint at the location provided by the exit_bp_loc global 45# and resume the execution until hitting that breakpoint. We also make 46# sure to consume all the expected output from all processes as well, 47# to make sure it doesn't cause trouble during a subsequent test. 48 49proc continue_to_exit_bp_loc {} { 50 global exit_bp_loc decimal gdb_prompt 51 global inferior_spawn_id gdb_spawn_id 52 53 gdb_breakpoint $exit_bp_loc 54 55 send_gdb "continue\n" 56 57 # The output from the child processes can be interleaved arbitrarily 58 # with the output from GDB and the parent process. If we don't 59 # consume it all now, it can confuse later interactions. 60 set seen_done 0 61 set seen_break 0 62 set seen_prompt 0 63 set seen_timeout 0 64 while { ($seen_done < 16 || ! $seen_prompt) && ! $seen_timeout } { 65 # We don't know what order the interesting things will arrive in. 66 # Using a pattern of the form 'x|y|z' instead of -re x ... -re y 67 # ... -re z ensures that expect always chooses the match that 68 # occurs leftmost in the input, and not the pattern appearing 69 # first in the script that occurs anywhere in the input, so that 70 # we don't skip anything. 71 gdb_expect { 72 -i "$inferior_spawn_id $gdb_spawn_id" 73 -re "($decimal done)|(Breakpoint)|($gdb_prompt)" { 74 if {[info exists expect_out(1,string)]} { 75 incr seen_done 76 } elseif {[info exists expect_out(2,string)]} { 77 set seen_break 1 78 } elseif {[info exists expect_out(3,string)]} { 79 set seen_prompt 1 80 } 81 array unset expect_out 82 } 83 timeout { set seen_timeout 1 } 84 } 85 } 86 87 if { $seen_timeout } { 88 fail "run to exit 2 (timeout)" 89 } elseif { ! $seen_prompt } { 90 fail "run to exit 2 (no prompt)" 91 } elseif { ! $seen_break } { 92 fail "run to exit 2 (no breakpoint hit)" 93 } elseif { $seen_done != 16 } { 94 fail "run to exit 2 (missing done messages)" 95 } else { 96 pass "run to exit 2" 97 } 98} 99 100# The inferior program builds a tree of processes by executing a loop 101# four times, calling fork at each iteration. Thus, at each 102# iteration, the total number of processes doubles; after four 103# iterations, we have 16 processes. Each process saves the results 104# from its 'fork' calls, so we can tell which leaf a given process is 105# by looking at which forks returned zero and which returned a pid: a 106# zero means to take the child's branch; a pid means to take the 107# parent's branch. 108 109foreach mode { "child" "parent" } { 110 clean_restart ${binfile} 111 runto_main 112 113 gdb_test_no_output "set follow-fork $mode" 114 with_test_prefix "follow $mode" { 115 continue_to_exit_bp_loc 116 117 set test "print pids" 118 if { $mode eq "child" } { 119 # Gdb is set to follow the child. 120 # The result should be that each of the 4 forks returns zero. 121 gdb_test "print pids" "\\$.* = \\{0, 0, 0, 0\\}.*" $test 122 } else { 123 # Gdb is set to follow the parent. 124 # Result should be that none of the 4 forks returns zero. 125 set val \ 126 [join [list \ 127 "pids\[0\]==0" \ 128 "pids\[1\]==0" \ 129 "pids\[2\]==0" \ 130 "pids\[3\]==0"] " || "] 131 gdb_test "print $val" " = 0" $test 132 } 133 } 134} 135 136# 137# Now test with detach-on-fork off. 138# 139 140# Start with a fresh gdb 141 142clean_restart ${binfile} 143 144runto_main 145gdb_breakpoint $exit_bp_loc 146 147gdb_test "help set detach-on-fork" "whether gdb will detach the child.*" \ 148 "help set detach" 149 150gdb_test "show detach-on-fork" "on." "show detach default on" 151 152gdb_test_no_output "set detach off" "set detach off" 153 154# 155# We will now run every fork up to the exit bp, 156# eventually winding up with 16 inferiors. 157# 158 159for {set i 1} {$i <= 15} {incr i} { 160 gdb_test_multiple "continue" "run to exit $i" { 161 -re "Continuing\.\r\n" { 162 exp_continue 163 } 164 -re "\[New inferior $decimal \\(process $decimal\\)\]\r\n" { 165 exp_continue 166 } 167 -re -wrap "Breakpoint .* main .*exit.*" { 168 pass $gdb_test_name 169 } 170 } 171 gdb_test "info inferior" " 2 .* 3 .* 4 .* 5 .*" "info inferior $i" 172 gdb_test "inferior $i + 1" "(_dl_sysinfo_int80|fork|__kernel_(v|)syscall).*" \ 173 "inferior $i" 174} 175 176gdb_test "continue" "Breakpoint .* main .*exit.*" "run to exit 16" 177gdb_test "info inferior" " 2 .* 3 .* 4 .* 5 .*" "info inferior 16" 178gdb_test "inferior 2" " main .*" "restart final" 179 180# 181# Now we should examine all the pids. 182# 183 184# 185# Test detach inferior 186# 187 188# [assumes we're at #1] 189gdb_test "detach inferior 2" "Detaching .*" "detach 2" 190gdb_test "detach inferior 3" "Detaching .*" "detach 3" 191gdb_test "detach inferior 4" "Detaching .*" "detach 4" 192gdb_test "detach inferior 5" "Detaching .*" "detach 5" 193 194# 195# Test kill inferior 196# 197 198for {set i 6} { $i <= 16} {incr i} { 199 gdb_test_no_output "kill inferior $i" "kill $i" 200 gdb_test "info inferior $i" "<null>.*" "did kill $i" 201} 202