1 # Copyright 1997, 1999, 2007, 2008, 2009, 2010
2 # Free Software Foundation, Inc.
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
6 # the Free Software Foundation; either version 3 of the License, or
7 # (at your option) any later version.
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.
14 # You should have received a copy of the GNU General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 if { [is_remote target] || ![isnative] } then {
25 set testfile "foll-fork"
26 set srcfile ${testfile}.c
27 set binfile ${objdir}/${subdir}/${testfile}
29 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
30 untested foll-fork.exp
36 # Until "set follow-fork-mode" and "catch fork" are implemented on
39 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
43 proc check_fork_catchpoints {} {
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"
53 -re ".*Catchpoint.*$gdb_prompt $" {
54 set has_fork_catchpoints 1
55 pass "continue to first fork catchpoint"
59 if {$has_fork_catchpoints == 0} {
60 unsupported "fork catchpoints"
65 proc default_fork_parent_follow {} {
68 send_gdb "show follow-fork\n"
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"}
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" }
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.
89 proc explicit_fork_parent_follow {} {
92 send_gdb "set follow-fork parent\n"
94 -re "$gdb_prompt $" {pass "set follow-fork parent"}
95 timeout {fail "(timeout) set follow-fork parent"}
97 send_gdb "show follow-fork\n"
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"}
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"}
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.
118 proc explicit_fork_child_follow {} {
121 send_gdb "set follow-fork child\n"
123 -re "$gdb_prompt $" {pass "set follow-fork child"}
124 timeout {fail "(timeout) set follow-fork child"}
126 send_gdb "show follow-fork\n"
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"}
135 -re "Attaching after.* fork to.*$gdb_prompt $"\
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"}
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.
147 proc catch_fork_child_follow {} {
151 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
153 send_gdb "catch fork\n"
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"}
161 # Verify that the catchpoint is mentioned in an "info breakpoints",
162 # and further that the catchpoint mentions no process id.
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 $" {
171 send_gdb "continue\n"
173 -re "Catchpoint.*(forked process.*),.*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
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"}
179 # Verify that the catchpoint is mentioned in an "info breakpoints",
180 # and further that the catchpoint managed to capture a process id.
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 $" {
189 send_gdb "set follow-fork child\n"
191 -re "$gdb_prompt $" {pass "set follow-fork child"}
192 timeout {fail "(timeout) set follow-fork child"}
194 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
196 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
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"}
201 send_gdb "continue\n"
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"}
208 # The parent has been detached; allow time for any output it might
209 # generate to arrive, so that output doesn't get confused with
210 # any expected debugger output from a subsequent testpoint.
213 send_gdb "delete breakpoints\n"
215 -re "Delete all breakpoints.*$" {
219 {pass "set follow-fork child, cleanup"}
220 timeout {fail "(timeout) set follow-fork child, cleanup"}
223 -re "$gdb_prompt $" {fail "set follow-fork child, cleanup"}
224 timeout {fail "(timeout) set follow-fork child, cleanup"}
228 proc catch_fork_unpatch_child {} {
232 set bp_exit [gdb_get_line_number "at exit"]
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"
237 gdb_test "continue" \
238 "Catchpoint.*\\(forked process.*\\).*,.*in .*(fork|__kernel_v?syscall).*" \
239 "unpatch child, catch fork"
241 # Delete all breakpoints and catchpoints.
244 # Force $srcfile as the current GDB source can be in glibc sourcetree.
245 gdb_test "break $srcfile:$bp_exit" \
246 "Breakpoint .*file .*$srcfile, line .*" \
247 "unpatch child, breakpoint at exit call"
249 gdb_test "set follow-fork child" "" "unpatch child, set follow-fork child"
251 set test "unpatch child, unpatched parent breakpoints from child"
252 gdb_test_multiple "continue" $test {
253 -re "at exit.*$gdb_prompt $" {
256 -re "SIGTRAP.*$gdb_prompt $" {
259 # Explicitly kill this child, so we can continue gracefully
260 # with further testing...
263 -re ".*Kill the program being debugged.*y or n. $" {
265 gdb_expect -re "$gdb_prompt $" {}
269 -re ".*$gdb_prompt $" {
270 fail "$test (unknown output)"
273 fail "$test (timeout)"
278 proc tcatch_fork_parent_follow {} {
282 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
284 send_gdb "catch fork\n"
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"}
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.
295 send_gdb "continue\n"
297 -re ".*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
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"}
302 send_gdb "set follow-fork parent\n"
304 -re "$gdb_prompt $" {pass "set follow-fork parent"}
305 timeout {fail "(timeout) set follow-fork parent"}
307 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
309 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
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"}
314 send_gdb "continue\n"
316 -re ".*Detaching after fork from.* at .*$bp_after_fork.*$gdb_prompt $"\
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"}
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.
326 send_gdb "delete breakpoints\n"
328 -re "Delete all breakpoints.*$" {
332 {pass "set follow-fork parent, cleanup"}
333 timeout {fail "(timeout) set follow-fork parent, cleanup"}
336 -re "$gdb_prompt $" {fail "set follow-fork parent, cleanup"}
337 timeout {fail "(timeout) set follow-fork parent, cleanup"}
341 proc do_fork_tests {} {
344 # Verify that help is available for "set follow-fork-mode".
346 send_gdb "help set follow-fork-mode\n"
348 -re "Set debugger response to a program call of fork or vfork..*
349 A 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.*
352 The unfollowed process will continue to run..*
353 By default, the debugger will follow the parent process..*$gdb_prompt $"\
354 { pass "help set follow-fork" }
355 -re "$gdb_prompt $" { fail "help set follow" }
356 timeout { fail "(timeout) help set follow-fork" }
359 # Verify that we can set follow-fork-mode, using an abbreviation
360 # for both the flag and its value.
362 send_gdb "set follow-fork ch\n"
363 send_gdb "show follow-fork\n"
365 -re "Debugger response to a program call of fork or vfork is \"child\".*$gdb_prompt $"\
366 {pass "set follow-fork, using abbreviations"}
367 timeout {fail "(timeout) set follow-fork, using abbreviations"}
370 # Verify that we cannot set follow-fork-mode to nonsense.
372 send_gdb "set follow-fork chork\n"
374 -re "Undefined item: \"chork\".*$gdb_prompt $"\
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"}
379 send_gdb "set follow-fork parent\n"
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)"}
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 }
389 # Test the default behaviour, which is to follow the parent of a
390 # fork, and detach from the child. Do this without catchpoints.
392 if [runto_main] then { default_fork_parent_follow }
394 # Test the ability to explicitly follow the parent of a fork, and
395 # detach from the child. Do this without catchpoints.
397 if [runto_main] then { explicit_fork_parent_follow }
399 # Test the ability to follow the child of a fork, and detach from
400 # the parent. Do this without catchpoints.
402 if [runto_main] then { explicit_fork_child_follow }
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.
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
412 # ??rehrauer: NYI. Will add testpoints here when implemented.
415 # Test the ability to catch a fork, specify that the child be
416 # followed, and continue. Make the catchpoint permanent.
418 if [runto_main] then { catch_fork_child_follow }
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 }
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.
428 if [runto_main] then { tcatch_fork_parent_follow }
431 # Start with a fresh gdb
435 gdb_reinitialize_dir $srcdir/$subdir
438 # The "Detaching..." and "Attaching..." messages may be hidden by
440 gdb_test "set verbose" ""
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.