1 # Copyright 1997, 1999, 2007-2012 Free Software Foundation, Inc.
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
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
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.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16 if { [is_remote target] || ![isnative] } then {
20 # Until "set follow-fork-mode" and "catch fork" are implemented on
23 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
28 set testfile "foll-fork"
29 set srcfile ${testfile}.c
30 set binfile ${objdir}/${subdir}/${testfile}
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33 untested foll-fork.exp
37 proc check_fork_catchpoints {} {
40 # Verify that the system supports "catch fork".
41 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
42 set has_fork_catchpoints 0
43 gdb_test_multiple "continue" "continue to first fork catchpoint" {
44 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
45 unsupported "continue to first fork catchpoint"
47 -re ".*Catchpoint.*$gdb_prompt $" {
48 set has_fork_catchpoints 1
49 pass "continue to first fork catchpoint"
53 if {$has_fork_catchpoints == 0} {
54 unsupported "fork catchpoints"
59 proc default_fork_parent_follow {} {
62 gdb_test "show follow-fork" \
63 "Debugger response to a program call of fork or vfork is \"parent\".*" \
64 "default show parent follow, no catchpoints"
67 "Detaching after fork from.*" \
68 "default parent follow, no catchpoints"
70 # The child has been detached; allow time for any output it might
71 # generate to arrive, so that output doesn't get confused with
72 # any expected debugger output from a subsequent testpoint.
77 proc explicit_fork_parent_follow {} {
80 gdb_test_no_output "set follow-fork parent"
82 gdb_test "show follow-fork" \
83 "Debugger response to a program call of fork or vfork is \"parent\"." \
84 "explicit show parent follow, no catchpoints"
86 gdb_test "next 2" "Detaching after fork from.*" \
87 "explicit parent follow, no catchpoints"
89 # The child has been detached; allow time for any output it might
90 # generate to arrive, so that output doesn't get confused with
91 # any expected debugger output from a subsequent testpoint.
96 proc explicit_fork_child_follow {} {
99 gdb_test_no_output "set follow-fork child"
101 gdb_test "show follow-fork" \
102 "Debugger response to a program call of fork or vfork is \"child\"." \
103 "explicit show child follow, no catchpoints"
105 gdb_test "next 2" "Attaching after.* fork to.*" \
106 "explicit child follow, no catchpoints"
108 # The child has been detached; allow time for any output it might
109 # generate to arrive, so that output doesn't get confused with
110 # any gdb_expected debugger output from a subsequent testpoint.
115 proc catch_fork_child_follow {} {
119 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
121 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
122 "explicit child follow, set catch fork"
124 # Verify that the catchpoint is mentioned in an "info breakpoints",
125 # and further that the catchpoint mentions no process id.
127 set test_name "info shows catchpoint without pid"
128 gdb_test_multiple "info breakpoints" "$test_name" {
129 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
134 gdb_test "continue" \
135 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
136 "explicit child follow, catch fork"
138 # Verify that the catchpoint is mentioned in an "info breakpoints",
139 # and further that the catchpoint managed to capture a process id.
141 set test_name "info shows catchpoint without pid"
142 gdb_test_multiple "info breakpoints" "$test_name" {
143 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
148 gdb_test_no_output "set follow-fork child"
150 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
151 "Temporary breakpoint.*, line $bp_after_fork.*" \
152 "set follow-fork child, tbreak"
154 gdb_test "continue" \
155 "Attaching after.* fork to.* at .*$bp_after_fork.*" \
156 "set follow-fork child, hit tbreak"
158 # The parent has been detached; allow time for any output it might
159 # generate to arrive, so that output doesn't get confused with
160 # any expected debugger output from a subsequent testpoint.
164 gdb_test "delete breakpoints" \
166 "set follow-fork child, cleanup" \
167 "Delete all breakpoints. \\(y or n\\) $" \
171 proc catch_fork_unpatch_child {} {
175 set bp_exit [gdb_get_line_number "at exit"]
177 gdb_test "break callee" "file .*$srcfile, line .*" \
178 "unpatch child, break at callee"
179 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
180 "unpatch child, set catch fork"
182 gdb_test "continue" \
183 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
184 "unpatch child, catch fork"
186 # Delete all breakpoints and catchpoints.
189 # Force $srcfile as the current GDB source can be in glibc sourcetree.
190 gdb_test "break $srcfile:$bp_exit" \
191 "Breakpoint .*file .*$srcfile, line .*" \
192 "unpatch child, breakpoint at exit call"
194 gdb_test_no_output "set follow-fork child" \
195 "unpatch child, set follow-fork child"
197 set test "unpatch child, unpatched parent breakpoints from child"
198 gdb_test_multiple "continue" $test {
199 -re "at exit.*$gdb_prompt $" {
202 -re "SIGTRAP.*$gdb_prompt $" {
205 # Explicitly kill this child, so we can continue gracefully
206 # with further testing...
209 -re ".*Kill the program being debugged.*y or n. $" {
211 gdb_expect -re "$gdb_prompt $" {}
218 proc tcatch_fork_parent_follow {} {
222 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
224 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
225 "explicit parent follow, set tcatch fork"
227 # ??rehrauer: I don't yet know how to get the id of the tcatch
228 # via this script, so that I can add a -do list to it. For now,
229 # do the follow stuff after the catch happens.
231 gdb_test "continue" \
232 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
233 "explicit parent follow, tcatch fork"
235 gdb_test_no_output "set follow-fork parent"
237 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
238 "Temporary breakpoint.*, line $bp_after_fork.*" \
239 "set follow-fork parent, tbreak"
241 gdb_test "continue" \
242 "Detaching after fork from.* at .*$bp_after_fork.*" \
243 "set follow-fork parent, hit tbreak"
245 # The child has been detached; allow time for any output it might
246 # generate to arrive, so that output doesn't get confused with
247 # any expected debugger output from a subsequent testpoint.
251 gdb_test "delete breakpoints" \
253 "set follow-fork parent, cleanup" \
254 "Delete all breakpoints. \\(y or n\\) $" \
258 proc do_fork_tests {} {
261 # Verify that help is available for "set follow-fork-mode".
263 gdb_test "help set follow-fork-mode" \
264 "Set debugger response to a program call of fork or vfork..*
265 A fork or vfork creates a new process. follow-fork-mode can be:.*
266 .*parent - the original process is debugged after a fork.*
267 .*child - the new process is debugged after a fork.*
268 The unfollowed process will continue to run..*
269 By default, the debugger will follow the parent process..*" \
270 "help set follow-fork"
272 # Verify that we can set follow-fork-mode, using an abbreviation
273 # for both the flag and its value.
275 gdb_test_no_output "set follow-fork ch"
277 gdb_test "show follow-fork" \
278 "Debugger response to a program call of fork or vfork is \"child\".*" \
279 "set follow-fork, using abbreviations"
281 # Verify that we cannot set follow-fork-mode to nonsense.
283 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
284 "set follow-fork to nonsense is prohibited"
286 gdb_test_no_output "set follow-fork parent" "reset parent"
288 # Check that fork catchpoints are supported, as an indicator for whether
289 # fork-following is supported.
290 if [runto_main] then { check_fork_catchpoints }
292 # Test the default behaviour, which is to follow the parent of a
293 # fork, and detach from the child. Do this without catchpoints.
295 if [runto_main] then { default_fork_parent_follow }
297 # Test the ability to explicitly follow the parent of a fork, and
298 # detach from the child. Do this without catchpoints.
300 if [runto_main] then { explicit_fork_parent_follow }
302 # Test the ability to follow the child of a fork, and detach from
303 # the parent. Do this without catchpoints.
305 if [runto_main] then { explicit_fork_child_follow }
307 # Test the ability to follow both child and parent of a fork. Do
308 # this without catchpoints.
309 # ??rehrauer: NYI. Will add testpoints here when implemented.
312 # Test the ability to have the debugger ask the user at fork-time
313 # whether to follow the parent, child or both. Do this without
315 # ??rehrauer: NYI. Will add testpoints here when implemented.
318 # Test the ability to catch a fork, specify that the child be
319 # followed, and continue. Make the catchpoint permanent.
321 if [runto_main] then { catch_fork_child_follow }
323 # Test that parent breakpoints are successfully detached from the
324 # child at fork time, even if the user removes them from the
325 # breakpoints list after stopping at a fork catchpoint.
326 if [runto_main] then { catch_fork_unpatch_child }
328 # Test the ability to catch a fork, specify via a -do clause that
329 # the parent be followed, and continue. Make the catchpoint temporary.
331 if [runto_main] then { tcatch_fork_parent_follow }
334 # Start with a fresh gdb
338 gdb_reinitialize_dir $srcdir/$subdir
341 # The "Detaching..." and "Attaching..." messages may be hidden by
343 gdb_test_no_output "set verbose"
345 # This is a test of gdb's ability to follow the parent, child or both
346 # parent and child of a Unix fork() system call.