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 {
25 if {[prepare_for_testing
"failed to prepare" $testfile $srcfile debug]} {
29 proc check_fork_catchpoints
{} {
32 # Verify that the
system supports
"catch fork".
33 gdb_test
"catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
34 set has_fork_catchpoints
0
35 gdb_test_multiple
"continue" "continue to first fork catchpoint" {
36 -re
".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
37 unsupported
"continue to first fork catchpoint"
39 -re
".*Catchpoint.*$gdb_prompt $" {
40 set has_fork_catchpoints
1
41 pass
"continue to first fork catchpoint"
45 if {$has_fork_catchpoints
== 0} {
46 unsupported
"fork catchpoints"
51 # Test follow
-fork to ensure that the correct process is followed
, that
52 # the followed process stops where it is expected to stop
, that processes
53 # are detached
(or not
) as expected
, and that the inferior list has the
54 # expected contents after following the fork. WHO is the
argument to
55 # the
'set follow-fork-mode' command
, DETACH is the
argument to the
56 #
'set detach-on-fork' command
, and CMD is the GDB command used to
57 #
execute the
program past the fork.
If the value of WHO or DETACH is
58 #
'default', the corresponding GDB command is skipped
for that test.
59 # The value of CMD must be either
'next 2' or
'continue'.
60 proc test_follow_fork
{ who detach cmd
} {
65 with_test_prefix
"follow $who, detach $detach, command \"$cmd\"" {
67 # Start a new debugger session each time so defaults are legitimate.
68 clean_restart $testfile
71 untested
"could not run to main"
75 # The
"Detaching..." and "Attaching..." messages may be hidden by
77 gdb_test_no_output
"set verbose"
79 #
Set follow
-fork
-mode if we aren
't using the default.
80 if {$who == "default"} {
83 gdb_test_no_output "set follow-fork $who"
86 gdb_test "show follow-fork" \
87 "Debugger response to a program call of fork or vfork is \"$who\"." \
90 # Set detach-on-fork mode if we aren't using the default.
91 if {$detach
== "default"} {
94 gdb_test_no_output
"set detach-on-fork $detach"
97 gdb_test
"show detach-on-fork" \
98 "Whether gdb will detach.* fork is $detach." \
101 #
Set a breakpoint after the fork
if we aren
't single-stepping
103 if {$cmd == "continue"} {
104 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
105 gdb_test "break ${srcfile}:$bp_after_fork" \
106 "Breakpoint.*, line $bp_after_fork.*" \
107 "set breakpoint after fork"
110 # Set up the output we expect to see after we run.
112 if {$who == "child"} {
113 set expected_re "\\\[Attaching after.* fork to.*"
114 if {$detach == "on"} {
115 append expected_re "\\\[Detaching after fork from .*"
117 append expected_re "set breakpoint here.*"
118 } elseif {$who == "parent" && $detach == "on"} {
119 set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
121 set expected_re ".*set breakpoint here.*"
124 # Test running past and following the fork, using the parameters
126 gdb_test $cmd $expected_re "$cmd past fork"
128 # Check that we have the inferiors arranged correctly after
129 # following the fork.
130 set resume_unfollowed 0
131 if {$who == "parent" && $detach == "on"} {
133 # Follow parent / detach child: the only inferior is the parent.
134 gdb_test "info inferiors" "\\* 1 .* process.*" \
137 } elseif {$who == "parent" && $detach == "off"} {
139 # Follow parent / keep child: two inferiors under debug, the
140 # parent is the current inferior.
141 gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*" \
144 gdb_test "inferior 2" "Switching to inferior 2 .*"
145 set resume_unfollowed 1
147 } elseif {$who == "child" && $detach == "on"} {
149 # Follow child / detach parent: the child is under debug and is
150 # the current inferior. The parent is listed but is not under
152 gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*" \
155 } elseif {$who == "child" && $detach == "off"} {
157 # Follow child / keep parent: two inferiors under debug, the
158 # child is the current inferior.
159 gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*" \
162 gdb_test "inferior 1" "Switching to inferior 1 .*"
163 set resume_unfollowed 1
166 if {$resume_unfollowed == 1} {
167 if {$cmd == "next 2"} {
169 gdb_continue_to_end "continue unfollowed inferior to end"
171 } elseif {$cmd == "continue"} {
173 gdb_continue_to_breakpoint \
174 "continue unfollowed inferior to bp" \
175 ".* set breakpoint here.*"
181 proc catch_fork_child_follow {} {
185 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
187 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
188 "explicit child follow, set catch fork"
190 # Verify that the catchpoint is mentioned in an "info breakpoints",
191 # and further that the catchpoint mentions no process id.
193 set test_name "info shows catchpoint without pid"
194 gdb_test_multiple "info breakpoints" "$test_name" {
195 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
200 gdb_test "continue" \
201 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
202 "explicit child follow, catch fork"
204 # Verify that the catchpoint is mentioned in an "info breakpoints",
205 # and further that the catchpoint managed to capture a process id.
207 set test_name "info shows catchpoint without pid"
208 gdb_test_multiple "info breakpoints" "$test_name" {
209 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
214 gdb_test_no_output "set follow-fork child"
216 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
217 "Temporary breakpoint.*, line $bp_after_fork.*" \
218 "set follow-fork child, tbreak"
220 set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
221 append expected_re ".* at .*$bp_after_fork.*"
222 gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
224 # The parent has been detached; allow time for any output it might
225 # generate to arrive, so that output doesn't
get confused with
226 #
any expected debugger output from a subsequent testpoint.
230 gdb_test
"delete breakpoints" \
232 "set follow-fork child, cleanup" \
233 "Delete all breakpoints. \\(y or n\\) $" \
237 proc catch_fork_unpatch_child
{} {
241 set bp_exit
[gdb_get_line_number
"at exit"]
243 gdb_test
"break callee" "file .*$srcfile, line .*" \
244 "unpatch child, break at callee"
245 gdb_test
"catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
246 "unpatch child, set catch fork"
248 gdb_test
"continue" \
249 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
250 "unpatch child, catch fork"
252 #
Delete all breakpoints and catchpoints.
255 # Force $srcfile as the current GDB source can be in glibc sourcetree.
256 gdb_test
"break $srcfile:$bp_exit" \
257 "Breakpoint .*file .*$srcfile, line .*" \
258 "unpatch child, breakpoint at exit call"
260 gdb_test_no_output
"set follow-fork child" \
261 "unpatch child, set follow-fork child"
263 set test
"unpatch child, unpatched parent breakpoints from child"
264 gdb_test_multiple
"continue" $test {
265 -re
"at exit.*$gdb_prompt $" {
268 -re
"SIGTRAP.*$gdb_prompt $" {
271 # Explicitly kill this child
, so we can
continue gracefully
272 # with further testing...
275 -re
".*Kill the program being debugged.*y or n. $" {
277 gdb_expect
-re
"$gdb_prompt $" {}
284 proc tcatch_fork_parent_follow
{} {
288 set bp_after_fork
[gdb_get_line_number
"set breakpoint here"]
290 gdb_test
"catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
291 "explicit parent follow, set tcatch fork"
293 # ??rehrauer
: I don
't yet know how to get the id of the tcatch
294 # via this script, so that I can add a -do list to it. For now,
295 # do the follow stuff after the catch happens.
297 gdb_test "continue" \
298 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
299 "explicit parent follow, tcatch fork"
301 gdb_test_no_output "set follow-fork parent"
303 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
304 "Temporary breakpoint.*, line $bp_after_fork.*" \
305 "set follow-fork parent, tbreak"
307 gdb_test "continue" \
308 "\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
309 "set follow-fork parent, hit tbreak"
311 # The child has been detached; allow time for any output it might
312 # generate to arrive, so that output doesn't
get confused with
313 #
any expected debugger output from a subsequent testpoint.
317 gdb_test
"delete breakpoints" \
319 "set follow-fork parent, cleanup" \
320 "Delete all breakpoints. \\(y or n\\) $" \
324 proc do_fork_tests
{} {
328 # Verify that help is available
for "set follow-fork-mode".
330 gdb_test
"help set follow-fork-mode" \
331 "Set debugger response to a program call of fork or vfork..*
332 A fork or vfork creates a new process. follow
-fork
-mode can be
:.
*
333 .
*parent
- the original process is debugged after a fork.
*
334 .
*child
- the new process is debugged after a fork.
*
335 The unfollowed process will
continue to run..
*
336 By default
, the debugger will follow the parent process..
*" \
337 "help set follow-fork"
339 # Verify that we can
set follow
-fork
-mode, using an abbreviation
340 #
for both the flag and its value.
342 gdb_test_no_output
"set follow-fork ch"
344 gdb_test
"show follow-fork" \
345 "Debugger response to a program call of fork or vfork is \"child\".*" \
346 "set follow-fork, using abbreviations"
348 # Verify that we cannot
set follow
-fork
-mode to nonsense.
350 gdb_test
"set follow-fork chork" "Undefined item: \"chork\".*" \
351 "set follow-fork to nonsense is prohibited"
353 gdb_test_no_output
"set follow-fork parent" "reset parent"
355 # Check that fork catchpoints are supported
, as an indicator
for whether
356 # fork
-following is supported.
357 if [runto_main
] then { check_fork_catchpoints
}
359 # Test the basic follow
-fork functionality using all combinations of
360 #
values for follow
-fork
-mode and detach
-on-fork
, using either a
361 # breakpoint or single
-step to
execute past the fork.
363 # The first loop should be sufficient to test the defaults. There
364 # is no need to test using the defaults in other permutations
(e.g.
365 #
"default" "on", "parent" "default", etc.).
366 foreach cmd
{"next 2" "continue"} {
367 test_follow_fork
"default" "default" $cmd
370 # Now test all explicit permutations.
371 foreach who
{"parent" "child"} {
372 foreach detach
{"on" "off"} {
373 foreach cmd
{"next 2" "continue"} {
374 test_follow_fork $who $detach $cmd
381 # Restart to eliminate
any effects of the follow
-fork tests.
382 clean_restart $testfile
383 gdb_test_no_output
"set verbose"
385 # Test the ability to catch a fork
, specify that the child be
386 # followed
, and
continue. Make the catchpoint permanent.
388 if [runto_main
] then { catch_fork_child_follow
}
390 # Test that parent breakpoints are successfully detached from the
391 # child at fork time
, even
if the user removes them from the
392 # breakpoints list after stopping at a fork catchpoint.
393 if [runto_main
] then { catch_fork_unpatch_child
}
395 # Test the ability to catch a fork
, specify via a
-do clause that
396 # the parent be followed
, and
continue. Make the catchpoint temporary.
398 if [runto_main
] then { tcatch_fork_parent_follow
}
401 # This is a test of gdb
's ability to follow the parent, child or both
402 # parent and child of a Unix fork() system call.