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