Add support for --start option in -exec-run GDB/MI command.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
CommitLineData
28e7fd62 1# Copyright 1997-2013 Free Software Foundation, Inc.
74cf1395
JM
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
74cf1395 6# (at your option) any later version.
e22f8b7c 7#
74cf1395
JM
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#
74cf1395 13# You should have received a copy of the GNU General Public License
e85a822c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
74cf1395 15
e85a822c 16if { [is_remote target] || ![isnative] } then {
5a2468f5
JM
17 continue
18}
19
2d4e0376
YQ
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}
74cf1395 26
289f9037
TT
27standard_testfile
28
29if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} {
30 untested $testfile.exp
31 return -1
74cf1395
JM
32}
33
e85a822c
DJ
34proc check_fork_catchpoints {} {
35 global gdb_prompt
36
37 # Verify that the system supports "catch fork".
38 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
39 set has_fork_catchpoints 0
40 gdb_test_multiple "continue" "continue to first fork catchpoint" {
77b06cd7 41 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
e85a822c
DJ
42 unsupported "continue to first fork catchpoint"
43 }
44 -re ".*Catchpoint.*$gdb_prompt $" {
45 set has_fork_catchpoints 1
46 pass "continue to first fork catchpoint"
47 }
48 }
49
50 if {$has_fork_catchpoints == 0} {
51 unsupported "fork catchpoints"
52 return -code return
53 }
54}
55
74cf1395 56proc default_fork_parent_follow {} {
a76e022a
MS
57 global gdb_prompt
58
59 gdb_test "show follow-fork" \
60 "Debugger response to a program call of fork or vfork is \"parent\".*" \
61 "default show parent follow, no catchpoints"
62
63 gdb_test "next 2" \
64 "Detaching after fork from.*" \
65 "default parent follow, no catchpoints"
66
67 # The child has been detached; allow time for any output it might
68 # generate to arrive, so that output doesn't get confused with
69 # any expected debugger output from a subsequent testpoint.
70 #
71 exec sleep 1
74cf1395
JM
72}
73
74proc explicit_fork_parent_follow {} {
a76e022a
MS
75 global gdb_prompt
76
77 gdb_test_no_output "set follow-fork parent"
78
79 gdb_test "show follow-fork" \
80 "Debugger response to a program call of fork or vfork is \"parent\"." \
81 "explicit show parent follow, no catchpoints"
82
83 gdb_test "next 2" "Detaching after fork from.*" \
84 "explicit parent follow, no catchpoints"
85
86 # The child has been detached; allow time for any output it might
87 # generate to arrive, so that output doesn't get confused with
88 # any expected debugger output from a subsequent testpoint.
89 #
90 exec sleep 1
74cf1395
JM
91}
92
93proc explicit_fork_child_follow {} {
a76e022a
MS
94 global gdb_prompt
95
96 gdb_test_no_output "set follow-fork child"
97
98 gdb_test "show follow-fork" \
99 "Debugger response to a program call of fork or vfork is \"child\"." \
100 "explicit show child follow, no catchpoints"
101
102 gdb_test "next 2" "Attaching after.* fork to.*" \
103 "explicit child follow, no catchpoints"
104
105 # The child has been detached; allow time for any output it might
106 # generate to arrive, so that output doesn't get confused with
107 # any gdb_expected debugger output from a subsequent testpoint.
108 #
109 exec sleep 1
74cf1395
JM
110}
111
112proc catch_fork_child_follow {} {
a76e022a
MS
113 global gdb_prompt
114 global srcfile
115
116 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
117
2c04da0d 118 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
a76e022a
MS
119 "explicit child follow, set catch fork"
120
121 # Verify that the catchpoint is mentioned in an "info breakpoints",
122 # and further that the catchpoint mentions no process id.
123 #
124 set test_name "info shows catchpoint without pid"
125 gdb_test_multiple "info breakpoints" "$test_name" {
126 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
127 pass "$test_name"
128 }
129 }
130
131 gdb_test "continue" \
2c04da0d 132 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
133 "explicit child follow, catch fork"
134
135 # Verify that the catchpoint is mentioned in an "info breakpoints",
136 # and further that the catchpoint managed to capture a process id.
137 #
138 set test_name "info shows catchpoint without pid"
139 gdb_test_multiple "info breakpoints" "$test_name" {
140 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
141 pass "$test_name"
142 }
143 }
144
145 gdb_test_no_output "set follow-fork child"
146
147 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
148 "Temporary breakpoint.*, line $bp_after_fork.*" \
149 "set follow-fork child, tbreak"
150
151 gdb_test "continue" \
152 "Attaching after.* fork to.* at .*$bp_after_fork.*" \
153 "set follow-fork child, hit tbreak"
154
155 # The parent has been detached; allow time for any output it might
156 # generate to arrive, so that output doesn't get confused with
157 # any expected debugger output from a subsequent testpoint.
158 #
159 exec sleep 1
160
161 gdb_test "delete breakpoints" \
162 "" \
163 "set follow-fork child, cleanup" \
e9101ff5 164 "Delete all breakpoints. \\(y or n\\) $" \
a76e022a 165 "y"
74cf1395
JM
166}
167
b242c3c2 168proc catch_fork_unpatch_child {} {
a76e022a
MS
169 global gdb_prompt
170 global srcfile
171
172 set bp_exit [gdb_get_line_number "at exit"]
173
174 gdb_test "break callee" "file .*$srcfile, line .*" \
175 "unpatch child, break at callee"
176 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
177 "unpatch child, set catch fork"
178
179 gdb_test "continue" \
2c04da0d 180 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
181 "unpatch child, catch fork"
182
183 # Delete all breakpoints and catchpoints.
184 delete_breakpoints
185
186 # Force $srcfile as the current GDB source can be in glibc sourcetree.
187 gdb_test "break $srcfile:$bp_exit" \
188 "Breakpoint .*file .*$srcfile, line .*" \
189 "unpatch child, breakpoint at exit call"
190
27d3a1a2
MS
191 gdb_test_no_output "set follow-fork child" \
192 "unpatch child, set follow-fork child"
a76e022a
MS
193
194 set test "unpatch child, unpatched parent breakpoints from child"
195 gdb_test_multiple "continue" $test {
196 -re "at exit.*$gdb_prompt $" {
197 pass "$test"
198 }
199 -re "SIGTRAP.*$gdb_prompt $" {
200 fail "$test"
201
202 # Explicitly kill this child, so we can continue gracefully
203 # with further testing...
204 send_gdb "kill\n"
205 gdb_expect {
206 -re ".*Kill the program being debugged.*y or n. $" {
207 send_gdb "y\n"
208 gdb_expect -re "$gdb_prompt $" {}
209 }
210 }
211 }
212 }
b242c3c2
PA
213}
214
74cf1395 215proc tcatch_fork_parent_follow {} {
a76e022a
MS
216 global gdb_prompt
217 global srcfile
218
219 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
220
2c04da0d 221 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
a76e022a
MS
222 "explicit parent follow, set tcatch fork"
223
74cf1395
JM
224# ??rehrauer: I don't yet know how to get the id of the tcatch
225# via this script, so that I can add a -do list to it. For now,
226# do the follow stuff after the catch happens.
227
2c04da0d
UW
228 gdb_test "continue" \
229 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
230 "explicit parent follow, tcatch fork"
231
232 gdb_test_no_output "set follow-fork parent"
233
234 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
235 "Temporary breakpoint.*, line $bp_after_fork.*" \
236 "set follow-fork parent, tbreak"
237
238 gdb_test "continue" \
239 "Detaching after fork from.* at .*$bp_after_fork.*" \
240 "set follow-fork parent, hit tbreak"
241
242 # The child has been detached; allow time for any output it might
243 # generate to arrive, so that output doesn't get confused with
244 # any expected debugger output from a subsequent testpoint.
245 #
246 exec sleep 1
247
248 gdb_test "delete breakpoints" \
249 "" \
250 "set follow-fork parent, cleanup" \
e9101ff5 251 "Delete all breakpoints. \\(y or n\\) $" \
a76e022a 252 "y"
74cf1395
JM
253}
254
255proc do_fork_tests {} {
a76e022a 256 global gdb_prompt
74cf1395 257
a76e022a
MS
258 # Verify that help is available for "set follow-fork-mode".
259 #
260 gdb_test "help set follow-fork-mode" \
261 "Set debugger response to a program call of fork or vfork..*
74cf1395
JM
262A fork or vfork creates a new process. follow-fork-mode can be:.*
263.*parent - the original process is debugged after a fork.*
264.*child - the new process is debugged after a fork.*
ef04f396 265The unfollowed process will continue to run..*
a76e022a
MS
266By default, the debugger will follow the parent process..*" \
267 "help set follow-fork"
268
269 # Verify that we can set follow-fork-mode, using an abbreviation
270 # for both the flag and its value.
271 #
272 gdb_test_no_output "set follow-fork ch"
273
274 gdb_test "show follow-fork" \
275 "Debugger response to a program call of fork or vfork is \"child\".*" \
276 "set follow-fork, using abbreviations"
277
278 # Verify that we cannot set follow-fork-mode to nonsense.
279 #
280 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
281 "set follow-fork to nonsense is prohibited"
282
283 gdb_test_no_output "set follow-fork parent" "reset parent"
284
285 # Check that fork catchpoints are supported, as an indicator for whether
286 # fork-following is supported.
287 if [runto_main] then { check_fork_catchpoints }
288
289 # Test the default behaviour, which is to follow the parent of a
290 # fork, and detach from the child. Do this without catchpoints.
291 #
292 if [runto_main] then { default_fork_parent_follow }
293
294 # Test the ability to explicitly follow the parent of a fork, and
295 # detach from the child. Do this without catchpoints.
296 #
297 if [runto_main] then { explicit_fork_parent_follow }
298
299 # Test the ability to follow the child of a fork, and detach from
300 # the parent. Do this without catchpoints.
301 #
302 if [runto_main] then { explicit_fork_child_follow }
303
304 # Test the ability to follow both child and parent of a fork. Do
305 # this without catchpoints.
306 # ??rehrauer: NYI. Will add testpoints here when implemented.
307 #
308
309 # Test the ability to have the debugger ask the user at fork-time
310 # whether to follow the parent, child or both. Do this without
311 # catchpoints.
312 # ??rehrauer: NYI. Will add testpoints here when implemented.
313 #
314
315 # Test the ability to catch a fork, specify that the child be
316 # followed, and continue. Make the catchpoint permanent.
317 #
318 if [runto_main] then { catch_fork_child_follow }
319
320 # Test that parent breakpoints are successfully detached from the
321 # child at fork time, even if the user removes them from the
322 # breakpoints list after stopping at a fork catchpoint.
323 if [runto_main] then { catch_fork_unpatch_child }
324
325 # Test the ability to catch a fork, specify via a -do clause that
326 # the parent be followed, and continue. Make the catchpoint temporary.
327 #
328 if [runto_main] then { tcatch_fork_parent_follow }
74cf1395
JM
329}
330
e85a822c
DJ
331# The "Detaching..." and "Attaching..." messages may be hidden by
332# default.
27d3a1a2 333gdb_test_no_output "set verbose"
74cf1395
JM
334
335# This is a test of gdb's ability to follow the parent, child or both
336# parent and child of a Unix fork() system call.
337#
338do_fork_tests
339
340return 0
This page took 1.394286 seconds and 4 git commands to generate.