Add support for --start option in -exec-run GDB/MI command.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / multi-forks.exp
CommitLineData
28e7fd62 1# Copyright 2005-2013 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
db9d7fc5 16if { [is_remote target] || ![isnative] } then {
099ac3dd
MS
17 continue
18}
19
20# Until "set follow-fork-mode" and "catch fork" are implemented on
21# other targets...
22#
23if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-*-linux*"]} then {
24 continue
25}
26
099ac3dd 27
f8b41b00 28standard_testfile .c
099ac3dd
MS
29
30if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
31 untested multi-forks.exp
32 return -1
099ac3dd
MS
33}
34
35# Start with a fresh gdb
36
f8b41b00 37clean_restart ${binfile}
099ac3dd
MS
38
39global gdb_prompt
40
41# This is a test of gdb's ability to follow the parent, child or both
42# parent and child of multiple Unix fork() system calls.
cb4dce93
JK
43
44set exit_bp_loc [gdb_get_line_number "Set exit breakpoint here."]
45
46# Insert a breakpoint at the location provided by the exit_bp_loc global
47# and resume the execution until hitting that breakpoint. We also make
48# sure to consume all the expected output from all processes as well,
49# to make sure it doesn't cause trouble during a subsequent test.
50
51proc continue_to_exit_bp_loc {} {
52 global exit_bp_loc decimal gdb_prompt
53
54 gdb_breakpoint $exit_bp_loc
55
56 send_gdb "continue\n"
57
58 # The output from the child processes can be interleaved arbitrarily
59 # with the output from GDB and the parent process. If we don't
60 # consume it all now, it can confuse later interactions.
61 set seen_done 0
62 set seen_break 0
63 set seen_prompt 0
64 set seen_timeout 0
65 while { ($seen_done < 16 || ! $seen_prompt) && ! $seen_timeout } {
66 # We don't know what order the interesting things will arrive in.
67 # Using a pattern of the form 'x|y|z' instead of -re x ... -re y
68 # ... -re z ensures that expect always chooses the match that
69 # occurs leftmost in the input, and not the pattern appearing
70 # first in the script that occurs anywhere in the input, so that
71 # we don't skip anything.
72 gdb_expect {
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}
099ac3dd 99
aad9a193
JB
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.
099ac3dd
MS
108
109# First set gdb to follow the child.
110# The result should be that each of the 4 forks returns zero.
111
112runto_main
27d3a1a2 113gdb_test_no_output "set follow-fork child"
cb4dce93 114continue_to_exit_bp_loc
099ac3dd
MS
115
116gdb_test "print pids" "\\$.* = \\{0, 0, 0, 0\\}.*" "follow child, print pids"
117
118# Now set gdb to follow the parent.
119# Result should be that none of the 4 forks returns zero.
120
099ac3dd 121runto_main
35ec993f 122gdb_test_no_output "set follow-fork parent" ""
cb4dce93 123continue_to_exit_bp_loc
099ac3dd
MS
124
125gdb_test "print pids\[0\]==0 || pids\[1\]==0 || pids\[2\]==0 || pids\[3\]==0" \
126 " = 0" "follow parent, print pids"
127
128#
129# Now test with detach-on-fork off.
130#
131
2277426b
PA
132# detach-on-fork isn't implemented on hpux.
133#
134if {![istarget "*-*-linux*"]} then {
135 continue
136}
137
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
DJ
157for {set i 1} {$i <= 15} {incr i} {
158 gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit $i"
2277426b
PA
159 gdb_test "info inferior" " 5 .* 4 .* 3 .* 2 .*" "info inferior $i"
160 gdb_test "inferior $i + 1" "(_dl_sysinfo_int80|fork|__kernel_(v|)syscall).*" \
161 "inferior $i"
9078e690 162}
099ac3dd
MS
163
164gdb_test "continue" "Breakpoint .* main .*exit.*" "Run to exit 16"
2277426b
PA
165gdb_test "info inferiors" " 5 .* 4 .* 3 .* 2 .*" "info inferior 16"
166gdb_test "inferior 2" " main .*" "restart final"
099ac3dd
MS
167
168#
169# Now we should examine all the pids.
170#
171
172#
2277426b 173# Test detach inferior
099ac3dd
MS
174#
175
2277426b
PA
176# [assumes we're at #1]
177gdb_test "detach inferior 2" "Detaching .*" "Detach 2"
178gdb_test "detach inferior 3" "Detaching .*" "Detach 3"
179gdb_test "detach inferior 4" "Detaching .*" "Detach 4"
180gdb_test "detach inferior 5" "Detaching .*" "Detach 5"
099ac3dd
MS
181
182#
2277426b
PA
183# Test kill inferior
184#
099ac3dd 185
27d3a1a2 186gdb_test_no_output "kill inferior 6" "Kill 6"
6c95b8df 187gdb_test "info inferior 6" "<null>.*" "Did kill 6"
27d3a1a2 188gdb_test_no_output "kill inferior 7" "Kill 7"
6c95b8df 189gdb_test "info inferior 7" "<null>.*" "Did kill 7"
27d3a1a2 190gdb_test_no_output "kill inferior 8" "Kill 8"
6c95b8df 191gdb_test "info inferior 8" "<null>.*" "Did kill 8"
27d3a1a2 192gdb_test_no_output "kill inferior 9" "Kill 9"
6c95b8df 193gdb_test "info inferior 9" "<null>.*" "Did kill 9"
27d3a1a2 194gdb_test_no_output "kill inferior 10" "Kill 10"
6c95b8df 195gdb_test "info inferior 10" "<null>.*" "Did kill 10"
27d3a1a2 196gdb_test_no_output "kill inferior 11" "Kill 11"
6c95b8df 197gdb_test "info inferior 11" "<null>.*" "Did kill 11"
27d3a1a2 198gdb_test_no_output "kill inferior 12" "Kill 12"
6c95b8df 199gdb_test "info inferior 12" "<null>.*" "Did kill 12"
27d3a1a2 200gdb_test_no_output "kill inferior 13" "Kill 13"
6c95b8df 201gdb_test "info inferior 13" "<null>.*" "Did kill 13"
27d3a1a2 202gdb_test_no_output "kill inferior 14" "Kill 14"
6c95b8df 203gdb_test "info inferior 14" "<null>.*" "Did kill 14"
27d3a1a2 204gdb_test_no_output "kill inferior 15" "Kill 15"
6c95b8df 205gdb_test "info inferior 15" "<null>.*" "Did kill 15"
27d3a1a2 206gdb_test_no_output "kill inferior 16" "Kill 16"
6c95b8df 207gdb_test "info inferior 16" "<null>.*" "Did kill 16"
099ac3dd
MS
208
209return 0
This page took 0.784434 seconds and 4 git commands to generate.