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