Fix build failure in inf-ptrace.c.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
CommitLineData
0fb0cc75 1# Copyright 1997, 1999, 2007, 2008, 2009 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
74cf1395
JM
20set prms_id 0
21set bug_id 0
22
e85a822c 23global srcfile
74cf1395
JM
24set testfile "foll-fork"
25set srcfile ${testfile}.c
26set binfile ${objdir}/${subdir}/${testfile}
27
28if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
29 untested foll-fork.exp
30 return -1
74cf1395
JM
31}
32
33
34
35# Until "set follow-fork-mode" and "catch fork" are implemented on
36# other targets...
37#
e85a822c 38if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
74cf1395
JM
39 continue
40}
41
e85a822c
DJ
42proc check_fork_catchpoints {} {
43 global gdb_prompt
44
45 # Verify that the system supports "catch fork".
46 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
47 set has_fork_catchpoints 0
48 gdb_test_multiple "continue" "continue to first fork catchpoint" {
49 -re ".*Your system does not support fork catchpoints.*$gdb_prompt $" {
50 unsupported "continue to first fork catchpoint"
51 }
52 -re ".*Catchpoint.*$gdb_prompt $" {
53 set has_fork_catchpoints 1
54 pass "continue to first fork catchpoint"
55 }
56 }
57
58 if {$has_fork_catchpoints == 0} {
59 unsupported "fork catchpoints"
60 return -code return
61 }
62}
63
74cf1395
JM
64proc default_fork_parent_follow {} {
65 global gdb_prompt
66
6c95b8df 67 send_gdb "show follow-fork\n"
74cf1395
JM
68 gdb_expect {
69 -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
70 {pass "default show parent follow, no catchpoints"}
71 -re "$gdb_prompt $" {fail "default show parent follow, no catchpoints"}
72 timeout {fail "(timeout) default show parent follow, no catchpoints"}
73 }
74 send_gdb "next 2\n"
75 gdb_expect {
76 -re "Detaching after fork from.*$gdb_prompt $"\
77 {pass "default parent follow, no catchpoints"}
78 -re "$gdb_prompt $" {fail "default parent follow, no catchpoints"}
79 timeout {fail "(timeout) default parent follow, no catchpoints" }
80 }
81 # The child has been detached; allow time for any output it might
82 # generate to arrive, so that output doesn't get confused with
83 # any expected debugger output from a subsequent testpoint.
84 #
85 exec sleep 1
86}
87
88proc explicit_fork_parent_follow {} {
89 global gdb_prompt
90
6c95b8df 91 send_gdb "set follow-fork parent\n"
74cf1395 92 gdb_expect {
6c95b8df
PA
93 -re "$gdb_prompt $" {pass "set follow-fork parent"}
94 timeout {fail "(timeout) set follow-fork parent"}
74cf1395 95 }
6c95b8df 96 send_gdb "show follow-fork\n"
74cf1395
JM
97 gdb_expect {
98 -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
99 {pass "explicit show parent follow, no catchpoints"}
100 -re "$gdb_prompt $" {fail "explicit show parent follow, no catchpoints"}
101 timeout {fail "(timeout) explicit show parent follow, no catchpoints"}
102 }
103 send_gdb "next 2\n"
104 gdb_expect {
105 -re "Detaching after fork from.*$gdb_prompt $"\
106 {pass "explicit parent follow, no catchpoints"}
107 -re "$gdb_prompt $" {fail "explicit parent follow, no catchpoints"}
108 timeout {fail "(timeout) explicit parent follow, no catchpoints"}
109 }
110 # The child has been detached; allow time for any output it might
111 # generate to arrive, so that output doesn't get confused with
112 # any expected debugger output from a subsequent testpoint.
113 #
114 exec sleep 1
115}
116
117proc explicit_fork_child_follow {} {
118 global gdb_prompt
119
6c95b8df 120 send_gdb "set follow-fork child\n"
74cf1395 121 gdb_expect {
6c95b8df
PA
122 -re "$gdb_prompt $" {pass "set follow-fork child"}
123 timeout {fail "(timeout) set follow-fork child"}
74cf1395 124 }
6c95b8df 125 send_gdb "show follow-fork\n"
74cf1395
JM
126 gdb_expect {
127 -re "Debugger response to a program call of fork or vfork is \"child\"..*$gdb_prompt $"\
128 {pass "explicit show child follow, no catchpoints"}
129 -re "$gdb_prompt $" {fail "explicit show child follow, no catchpoints"}
130 timeout {fail "(timeout) explicit show child follow, no catchpoints"}
131 }
132 send_gdb "next 2\n"
133 gdb_expect {
6c95b8df 134 -re "Attaching after.* fork to.*$gdb_prompt $"\
74cf1395
JM
135 {pass "explicit child follow, no catchpoints"}
136 -re "$gdb_prompt $" {fail "explicit child follow, no catchpoints"}
137 timeout {fail "(timeout) explicit child follow, no catchpoints"}
138 }
139 # The child has been detached; allow time for any output it might
140 # generate to arrive, so that output doesn't get confused with
141 # any gdb_expected debugger output from a subsequent testpoint.
142 #
143 exec sleep 1
144}
145
146proc catch_fork_child_follow {} {
147 global gdb_prompt
e85a822c 148 global srcfile
74cf1395 149
b242c3c2
PA
150 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
151
74cf1395
JM
152 send_gdb "catch fork\n"
153 gdb_expect {
154 -re "Catchpoint .*(fork).*$gdb_prompt $"\
155 {pass "explicit child follow, set catch fork"}
156 -re "$gdb_prompt $" {fail "explicit child follow, set catch fork"}
157 timeout {fail "(timeout) explicit child follow, set catch fork"}
158 }
159
160 # Verify that the catchpoint is mentioned in an "info breakpoints",
161 # and further that the catchpoint mentions no process id.
162 #
cce89356
JB
163 set test_name "info shows catchpoint without pid"
164 gdb_test_multiple "info breakpoints" "$test_name" {
165 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
166 pass "$test_name"
167 }
74cf1395
JM
168 }
169
170 send_gdb "continue\n"
171 gdb_expect {
a9c64011 172 -re "Catchpoint.*(forked process.*),.*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
74cf1395
JM
173 {pass "explicit child follow, catch fork"}
174 -re "$gdb_prompt $" {fail "explicit child follow, catch fork"}
175 timeout {fail "(timeout) explicit child follow, catch fork"}
176 }
177
178 # Verify that the catchpoint is mentioned in an "info breakpoints",
179 # and further that the catchpoint managed to capture a process id.
180 #
cce89356
JB
181 set test_name "info shows catchpoint without pid"
182 gdb_test_multiple "info breakpoints" "$test_name" {
183 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
184 pass "$test_name"
185 }
74cf1395
JM
186 }
187
6c95b8df 188 send_gdb "set follow-fork child\n"
74cf1395 189 gdb_expect {
6c95b8df
PA
190 -re "$gdb_prompt $" {pass "set follow-fork child"}
191 timeout {fail "(timeout) set follow-fork child"}
74cf1395 192 }
b242c3c2 193 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
74cf1395 194 gdb_expect {
b242c3c2 195 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
196 {pass "set follow-fork child, tbreak"}
197 -re "$gdb_prompt $" {fail "set follow-fork child, tbreak"}
198 timeout {fail "(timeout) set follow-fork child, tbreak"}
74cf1395
JM
199 }
200 send_gdb "continue\n"
201 gdb_expect {
6c95b8df
PA
202 -re "Attaching after.* fork to.* at .*$bp_after_fork.*$gdb_prompt $"\
203 {pass "set follow-fork child, hit tbreak"}
204 -re "$gdb_prompt $" {fail "set follow-fork child, hit tbreak"}
205 timeout {fail "(timeout) set follow-fork child, hit tbreak"}
74cf1395 206 }
b242c3c2 207 # The parent has been detached; allow time for any output it might
74cf1395
JM
208 # generate to arrive, so that output doesn't get confused with
209 # any expected debugger output from a subsequent testpoint.
210 #
211 exec sleep 1
212 send_gdb "delete breakpoints\n"
213 gdb_expect {
214 -re "Delete all breakpoints.*$" {
215 send_gdb "y\n"
216 gdb_expect {
217 -re "$gdb_prompt $"\
6c95b8df
PA
218 {pass "set follow-fork child, cleanup"}
219 timeout {fail "(timeout) set follow-fork child, cleanup"}
74cf1395
JM
220 }
221 }
6c95b8df
PA
222 -re "$gdb_prompt $" {fail "set follow-fork child, cleanup"}
223 timeout {fail "(timeout) set follow-fork child, cleanup"}
74cf1395
JM
224 }
225}
226
b242c3c2
PA
227proc catch_fork_unpatch_child {} {
228 global gdb_prompt
229 global srcfile
230
231 set bp_exit [gdb_get_line_number "at exit"]
232
233 gdb_test "break callee" "file .*$srcfile, line .*" "unpatch child, break at callee"
234 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "unpatch child, set catch fork"
235
236 gdb_test "continue" \
237 "Catchpoint.*\\(forked process.*\\).*,.*in .*(fork|__kernel_v?syscall).*" \
238 "unpatch child, catch fork"
239
240 # Delete all breakpoints and catchpoints.
241 delete_breakpoints
242
d7167b4c
JK
243 # Force $srcfile as the current GDB source can be in glibc sourcetree.
244 gdb_test "break $srcfile:$bp_exit" \
b242c3c2
PA
245 "Breakpoint .*file .*$srcfile, line .*" \
246 "unpatch child, breakpoint at exit call"
247
6c95b8df 248 gdb_test "set follow-fork child" "" "unpatch child, set follow-fork child"
b242c3c2
PA
249
250 set test "unpatch child, unpatched parent breakpoints from child"
251 gdb_test_multiple "continue" $test {
252 -re "at exit.*$gdb_prompt $" {
253 pass "$test"
254 }
255 -re "SIGTRAP.*$gdb_prompt $" {
256 fail "$test"
257
258 # Explicitly kill this child, so we can continue gracefully
259 # with further testing...
260 send_gdb "kill\n"
261 gdb_expect {
262 -re ".*Kill the program being debugged.*y or n. $" {
263 send_gdb "y\n"
264 gdb_expect -re "$gdb_prompt $" {}
265 }
266 }
267 }
268 -re ".*$gdb_prompt $" {
269 fail "$test (unknown output)"
270 }
271 timeout {
272 fail "$test (timeout)"
273 }
274 }
275}
276
74cf1395
JM
277proc tcatch_fork_parent_follow {} {
278 global gdb_prompt
e85a822c 279 global srcfile
74cf1395 280
b242c3c2
PA
281 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
282
74cf1395
JM
283 send_gdb "catch fork\n"
284 gdb_expect {
285 -re "Catchpoint .*(fork).*$gdb_prompt $"\
286 {pass "explicit parent follow, set tcatch fork"}
287 -re "$gdb_prompt $" {fail "explicit parent follow, set tcatch fork"}
288 timeout {fail "(timeout) explicit parent follow, set tcatch fork"}
289 }
290# ??rehrauer: I don't yet know how to get the id of the tcatch
291# via this script, so that I can add a -do list to it. For now,
292# do the follow stuff after the catch happens.
293
294 send_gdb "continue\n"
295 gdb_expect {
a9c64011 296 -re ".*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
74cf1395
JM
297 {pass "explicit parent follow, tcatch fork"}
298 -re "$gdb_prompt $" {fail "explicit parent follow, tcatch fork"}
299 timeout {fail "(timeout) explicit parent follow, tcatch fork"}
300 }
6c95b8df 301 send_gdb "set follow-fork parent\n"
74cf1395 302 gdb_expect {
6c95b8df
PA
303 -re "$gdb_prompt $" {pass "set follow-fork parent"}
304 timeout {fail "(timeout) set follow-fork parent"}
74cf1395 305 }
b242c3c2 306 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
74cf1395 307 gdb_expect {
b242c3c2 308 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
309 {pass "set follow-fork parent, tbreak"}
310 -re "$gdb_prompt $" {fail "set follow-fork parent, tbreak"}
311 timeout {fail "(timeout) set follow-fork child, tbreak"}
74cf1395
JM
312 }
313 send_gdb "continue\n"
314 gdb_expect {
b242c3c2 315 -re ".*Detaching after fork from.* at .*$bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
316 {pass "set follow-fork parent, hit tbreak"}
317 -re "$gdb_prompt $" {fail "set follow-fork parent, hit tbreak"}
318 timeout {fail "(timeout) set follow-fork parent, hit tbreak"}
74cf1395
JM
319 }
320 # The child has been detached; allow time for any output it might
321 # generate to arrive, so that output doesn't get confused with
322 # any expected debugger output from a subsequent testpoint.
323 #
324 exec sleep 1
325 send_gdb "delete breakpoints\n"
326 gdb_expect {
327 -re "Delete all breakpoints.*$" {
328 send_gdb "y\n"
329 gdb_expect {
330 -re "$gdb_prompt $"\
6c95b8df
PA
331 {pass "set follow-fork parent, cleanup"}
332 timeout {fail "(timeout) set follow-fork parent, cleanup"}
74cf1395
JM
333 }
334 }
6c95b8df
PA
335 -re "$gdb_prompt $" {fail "set follow-fork parent, cleanup"}
336 timeout {fail "(timeout) set follow-fork parent, cleanup"}
74cf1395
JM
337 }
338}
339
340proc do_fork_tests {} {
341 global gdb_prompt
342
343 # Verify that help is available for "set follow-fork-mode".
344 #
345 send_gdb "help set follow-fork-mode\n"
346 gdb_expect {
347 -re "Set debugger response to a program call of fork or vfork..*
348A fork or vfork creates a new process. follow-fork-mode can be:.*
349.*parent - the original process is debugged after a fork.*
350.*child - the new process is debugged after a fork.*
ef04f396 351The unfollowed process will continue to run..*
74cf1395 352By default, the debugger will follow the parent process..*$gdb_prompt $"\
6c95b8df 353 { pass "help set follow-fork" }
74cf1395 354 -re "$gdb_prompt $" { fail "help set follow" }
6c95b8df 355 timeout { fail "(timeout) help set follow-fork" }
74cf1395
JM
356 }
357
358 # Verify that we can set follow-fork-mode, using an abbreviation
359 # for both the flag and its value.
360 #
6c95b8df
PA
361 send_gdb "set follow-fork ch\n"
362 send_gdb "show follow-fork\n"
74cf1395
JM
363 gdb_expect {
364 -re "Debugger response to a program call of fork or vfork is \"child\".*$gdb_prompt $"\
6c95b8df
PA
365 {pass "set follow-fork, using abbreviations"}
366 timeout {fail "(timeout) set follow-fork, using abbreviations"}
74cf1395
JM
367 }
368
369 # Verify that we cannot set follow-fork-mode to nonsense.
370 #
6c95b8df 371 send_gdb "set follow-fork chork\n"
74cf1395
JM
372 gdb_expect {
373 -re "Undefined item: \"chork\".*$gdb_prompt $"\
6c95b8df
PA
374 {pass "set follow-fork to nonsense is prohibited"}
375 -re "$gdb_prompt $" {fail "set follow-fork to nonsense is prohibited"}
376 timeout {fail "(timeout) set follow-fork to nonsense is prohibited"}
74cf1395 377 }
6c95b8df 378 send_gdb "set follow-fork parent\n"
74cf1395 379 gdb_expect {
6c95b8df
PA
380 -re "$gdb_prompt $" {pass "set follow-fork to nonsense is prohibited (reset parent)"}
381 timeout {fail "set follow-fork to nonsense is prohibited (reset parent)"}
74cf1395
JM
382 }
383
e85a822c
DJ
384 # Check that fork catchpoints are supported, as an indicator for whether
385 # fork-following is supported.
386 if [runto_main] then { check_fork_catchpoints }
387
74cf1395
JM
388 # Test the default behaviour, which is to follow the parent of a
389 # fork, and detach from the child. Do this without catchpoints.
390 #
391 if [runto_main] then { default_fork_parent_follow }
392
393 # Test the ability to explicitly follow the parent of a fork, and
394 # detach from the child. Do this without catchpoints.
395 #
396 if [runto_main] then { explicit_fork_parent_follow }
397
398 # Test the ability to follow the child of a fork, and detach from
399 # the parent. Do this without catchpoints.
400 #
401 if [runto_main] then { explicit_fork_child_follow }
402
403 # Test the ability to follow both child and parent of a fork. Do
404 # this without catchpoints.
405 # ??rehrauer: NYI. Will add testpoints here when implemented.
406 #
407
408 # Test the ability to have the debugger ask the user at fork-time
409 # whether to follow the parent, child or both. Do this without
410 # catchpoints.
411 # ??rehrauer: NYI. Will add testpoints here when implemented.
412 #
413
414 # Test the ability to catch a fork, specify that the child be
415 # followed, and continue. Make the catchpoint permanent.
416 #
417 if [runto_main] then { catch_fork_child_follow }
418
b242c3c2
PA
419 # Test that parent breakpoints are successfully detached from the
420 # child at fork time, even if the user removes them from the
421 # breakpoints list after stopping at a fork catchpoint.
422 if [runto_main] then { catch_fork_unpatch_child }
423
74cf1395
JM
424 # Test the ability to catch a fork, specify via a -do clause that
425 # the parent be followed, and continue. Make the catchpoint temporary.
426 #
427 if [runto_main] then { tcatch_fork_parent_follow }
428}
429
430# Start with a fresh gdb
431
432gdb_exit
433gdb_start
434gdb_reinitialize_dir $srcdir/$subdir
435gdb_load ${binfile}
436
e85a822c
DJ
437# The "Detaching..." and "Attaching..." messages may be hidden by
438# default.
439gdb_test "set verbose" ""
74cf1395
JM
440
441# This is a test of gdb's ability to follow the parent, child or both
442# parent and child of a Unix fork() system call.
443#
444do_fork_tests
445
446return 0
This page took 0.982118 seconds and 4 git commands to generate.