1 # Copyright 1997-2019 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 # Until "set follow-fork-mode" and "catch fork" are implemented on
19 if { ![istarget "*-*-linux*"] && ![istarget "*-*-openbsd*"] } then {
23 # Test relies on checking follow-fork output. Do not run if gdb debug is
24 # enabled as it will be redirected to the log.
25 if [gdb_debug_enabled] {
26 untested "debug is enabled"
32 if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
36 proc check_fork_catchpoints {} {
39 # Verify that the system supports "catch fork".
40 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
41 set has_fork_catchpoints 0
42 gdb_test_multiple "continue" "continue to first fork catchpoint" {
43 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
44 unsupported "continue to first fork catchpoint"
46 -re ".*Catchpoint.*$gdb_prompt $" {
47 set has_fork_catchpoints 1
48 pass "continue to first fork catchpoint"
52 if {$has_fork_catchpoints == 0} {
53 unsupported "fork catchpoints"
58 # Test follow-fork to ensure that the correct process is followed, that
59 # the followed process stops where it is expected to stop, that processes
60 # are detached (or not) as expected, and that the inferior list has the
61 # expected contents after following the fork. WHO is the argument to
62 # the 'set follow-fork-mode' command, DETACH is the argument to the
63 # 'set detach-on-fork' command, and CMD is the GDB command used to
64 # execute the program past the fork. If the value of WHO or DETACH is
65 # 'default', the corresponding GDB command is skipped for that test.
66 # The value of CMD must be either 'next 2' or 'continue'.
67 proc test_follow_fork { who detach cmd } {
72 with_test_prefix "follow $who, detach $detach, command \"$cmd\"" {
74 # Start a new debugger session each time so defaults are legitimate.
75 clean_restart $testfile
78 untested "could not run to main"
82 # The "Detaching..." and "Attaching..." messages may be hidden by
84 gdb_test_no_output "set verbose"
86 # Set follow-fork-mode if we aren't using the default.
87 if {$who == "default"} {
90 gdb_test_no_output "set follow-fork $who"
93 gdb_test "show follow-fork" \
94 "Debugger response to a program call of fork or vfork is \"$who\"." \
97 # Set detach-on-fork mode if we aren't using the default.
98 if {$detach == "default"} {
101 gdb_test_no_output "set detach-on-fork $detach"
104 gdb_test "show detach-on-fork" \
105 "Whether gdb will detach.* fork is $detach." \
106 "show detach-on-fork"
108 # Set a breakpoint after the fork if we aren't single-stepping
110 if {$cmd == "continue"} {
111 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
112 gdb_test "break ${srcfile}:$bp_after_fork" \
113 "Breakpoint.*, line $bp_after_fork.*" \
114 "set breakpoint after fork"
117 # Set up the output we expect to see after we run.
119 if {$who == "child"} {
120 set expected_re "\\\[Attaching after.* fork to.*"
121 if {$detach == "on"} {
122 append expected_re "\\\[Detaching after fork from .*"
124 append expected_re "set breakpoint here.*"
125 } elseif {$who == "parent" && $detach == "on"} {
126 set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
128 set expected_re ".*set breakpoint here.*"
131 # Test running past and following the fork, using the parameters
133 gdb_test $cmd $expected_re "$cmd past fork"
135 # Check that we have the inferiors arranged correctly after
136 # following the fork.
137 set resume_unfollowed 0
138 if {$who == "parent" && $detach == "on"} {
140 # Follow parent / detach child: the only inferior is the parent.
141 gdb_test "info inferiors" "\\* 1 .* process.*" \
144 } elseif {$who == "parent" && $detach == "off"} {
146 # Follow parent / keep child: two inferiors under debug, the
147 # parent is the current inferior.
148 gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*" \
151 gdb_test "inferior 2" "Switching to inferior 2 .*"
152 set resume_unfollowed 1
154 } elseif {$who == "child" && $detach == "on"} {
156 # Follow child / detach parent: the child is under debug and is
157 # the current inferior. The parent is listed but is not under
159 gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*" \
162 } elseif {$who == "child" && $detach == "off"} {
164 # Follow child / keep parent: two inferiors under debug, the
165 # child is the current inferior.
166 gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*" \
169 gdb_test "inferior 1" "Switching to inferior 1 .*"
170 set resume_unfollowed 1
173 if {$resume_unfollowed == 1} {
174 if {$cmd == "next 2"} {
176 gdb_continue_to_end "continue unfollowed inferior to end"
178 } elseif {$cmd == "continue"} {
180 gdb_continue_to_breakpoint \
181 "continue unfollowed inferior to bp" \
182 ".* set breakpoint here.*"
188 proc catch_fork_child_follow {} {
192 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
194 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
195 "explicit child follow, set catch fork"
197 # Verify that the catchpoint is mentioned in an "info breakpoints",
198 # and further that the catchpoint mentions no process id.
200 set test_name "info shows catchpoint without pid"
201 gdb_test_multiple "info breakpoints" "$test_name" {
202 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
207 gdb_test "continue" \
208 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
209 "explicit child follow, catch fork"
211 # Verify that the catchpoint is mentioned in an "info breakpoints",
212 # and further that the catchpoint managed to capture a process id.
214 set test_name "info shows catchpoint without pid"
215 gdb_test_multiple "info breakpoints" "$test_name" {
216 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
221 gdb_test_no_output "set follow-fork child"
223 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
224 "Temporary breakpoint.*, line $bp_after_fork.*" \
225 "set follow-fork child, tbreak"
227 set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
228 append expected_re ".* at .*$bp_after_fork.*"
229 gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
231 # The parent has been detached; allow time for any output it might
232 # generate to arrive, so that output doesn't get confused with
233 # any expected debugger output from a subsequent testpoint.
237 gdb_test "delete breakpoints" \
239 "set follow-fork child, cleanup" \
240 "Delete all breakpoints. \\(y or n\\) $" \
244 proc catch_fork_unpatch_child {} {
248 set bp_exit [gdb_get_line_number "at exit"]
250 gdb_test "break callee" "file .*$srcfile, line .*" \
251 "unpatch child, break at callee"
252 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
253 "unpatch child, set catch fork"
255 gdb_test "continue" \
256 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
257 "unpatch child, catch fork"
259 # Delete all breakpoints and catchpoints.
262 # Force $srcfile as the current GDB source can be in glibc sourcetree.
263 gdb_test "break $srcfile:$bp_exit" \
264 "Breakpoint .*file .*$srcfile, line .*" \
265 "unpatch child, breakpoint at exit call"
267 gdb_test_no_output "set follow-fork child" \
268 "unpatch child, set follow-fork child"
270 set test "unpatch child, unpatched parent breakpoints from child"
271 gdb_test_multiple "continue" $test {
272 -re "at exit.*$gdb_prompt $" {
275 -re "SIGTRAP.*$gdb_prompt $" {
278 # Explicitly kill this child, so we can continue gracefully
279 # with further testing...
282 -re ".*Kill the program being debugged.*y or n. $" {
284 gdb_expect -re "$gdb_prompt $" {}
291 proc tcatch_fork_parent_follow {} {
295 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
297 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
298 "explicit parent follow, set tcatch fork"
300 # ??rehrauer: I don't yet know how to get the id of the tcatch
301 # via this script, so that I can add a -do list to it. For now,
302 # do the follow stuff after the catch happens.
304 gdb_test "continue" \
305 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
306 "explicit parent follow, tcatch fork"
308 gdb_test_no_output "set follow-fork parent"
310 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
311 "Temporary breakpoint.*, line $bp_after_fork.*" \
312 "set follow-fork parent, tbreak"
314 gdb_test "continue" \
315 "\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
316 "set follow-fork parent, hit tbreak"
318 # The child has been detached; allow time for any output it might
319 # generate to arrive, so that output doesn't get confused with
320 # any expected debugger output from a subsequent testpoint.
324 gdb_test "delete breakpoints" \
326 "set follow-fork parent, cleanup" \
327 "Delete all breakpoints. \\(y or n\\) $" \
331 proc do_fork_tests {} {
335 # Verify that help is available for "set follow-fork-mode".
337 gdb_test "help set follow-fork-mode" \
338 "Set debugger response to a program call of fork or vfork..*
339 A fork or vfork creates a new process. follow-fork-mode can be:.*
340 .*parent - the original process is debugged after a fork.*
341 .*child - the new process is debugged after a fork.*
342 The unfollowed process will continue to run..*
343 By default, the debugger will follow the parent process..*" \
344 "help set follow-fork"
346 # Verify that we can set follow-fork-mode, using an abbreviation
347 # for both the flag and its value.
349 gdb_test_no_output "set follow-fork ch"
351 gdb_test "show follow-fork" \
352 "Debugger response to a program call of fork or vfork is \"child\".*" \
353 "set follow-fork, using abbreviations"
355 # Verify that we cannot set follow-fork-mode to nonsense.
357 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
358 "set follow-fork to nonsense is prohibited"
360 gdb_test_no_output "set follow-fork parent" "reset parent"
362 # Check that fork catchpoints are supported, as an indicator for whether
363 # fork-following is supported.
364 if [runto_main] then { check_fork_catchpoints }
366 # Test the basic follow-fork functionality using all combinations of
367 # values for follow-fork-mode and detach-on-fork, using either a
368 # breakpoint or single-step to execute past the fork.
370 # The first loop should be sufficient to test the defaults. There
371 # is no need to test using the defaults in other permutations (e.g.
372 # "default" "on", "parent" "default", etc.).
373 foreach cmd {"next 2" "continue"} {
374 test_follow_fork "default" "default" $cmd
377 # Now test all explicit permutations.
378 foreach who {"parent" "child"} {
379 foreach detach {"on" "off"} {
380 foreach cmd {"next 2" "continue"} {
381 test_follow_fork $who $detach $cmd
388 # Restart to eliminate any effects of the follow-fork tests.
389 clean_restart $testfile
390 gdb_test_no_output "set verbose"
392 # Test the ability to catch a fork, specify that the child be
393 # followed, and continue. Make the catchpoint permanent.
395 if [runto_main] then { catch_fork_child_follow }
397 # Test that parent breakpoints are successfully detached from the
398 # child at fork time, even if the user removes them from the
399 # breakpoints list after stopping at a fork catchpoint.
400 if [runto_main] then { catch_fork_unpatch_child }
402 # Test the ability to catch a fork, specify via a -do clause that
403 # the parent be followed, and continue. Make the catchpoint temporary.
405 if [runto_main] then { tcatch_fork_parent_follow }
408 # This is a test of gdb's ability to follow the parent, child or both
409 # parent and child of a Unix fork() system call.