Update copyright year range in header of all files managed by GDB
[binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
blob59a664952c3331902a8aed4a37c4959d8a4d6773
1 #   Copyright 1997-2023 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 # Test relies on checking follow-fork output. Do not run if gdb debug is
17 # enabled as it will be redirected to the log.
18 if [gdb_debug_enabled] {
19     untested "debug is enabled"
20     return 0
23 standard_testfile
25 if {[build_executable "failed to prepare" $testfile $srcfile debug]} {
26     return -1
29 # Restart GDB and run the inferior to main.  Return 1 on success, 0 on failure.
31 proc setup {} {
32     clean_restart $::testfile
34     if { ![runto_main] } {
35         return 0
36     }
38     return 1
41 # Check that fork catchpoints are supported, as an indicator for whether
42 # fork-following is supported.  Return 1 if they are, else 0.
44 proc_with_prefix check_fork_catchpoints {} {
45   global gdb_prompt
47   if { ![setup] } {
48       return
49   }
51   # Verify that the system supports "catch fork".
52   gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
53   set has_fork_catchpoints 0
54   gdb_test_multiple "continue" "continue to first fork catchpoint" {
55     -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
56       unsupported "continue to first fork catchpoint"
57     }
58     -re ".*Catchpoint.*$gdb_prompt $" {
59       set has_fork_catchpoints 1
60       pass "continue to first fork catchpoint"
61     }
62   }
64   return $has_fork_catchpoints
67 # Test follow-fork to ensure that the correct process is followed, that
68 # the followed process stops where it is expected to stop, that processes
69 # are detached (or not) as expected, and that the inferior list has the
70 # expected contents after following the fork.  WHO is the argument to
71 # the 'set follow-fork-mode' command, DETACH is the argument to the 
72 # 'set detach-on-fork' command, and CMD is the GDB command used to 
73 # execute the program past the fork.  If the value of WHO or DETACH is
74 # 'default', the corresponding GDB command is skipped for that test.
75 # The value of CMD must be either 'next 2' or 'continue'.
76 proc_with_prefix test_follow_fork { follow-fork-mode detach-on-fork cmd } {
77     global gdb_prompt
78     global srcfile
79     global testfile
81     # Start a new debugger session each time so defaults are legitimate.
82     if { ![setup] } {
83         return
84     }
86     # The "Detaching..." and "Attaching..." messages may be hidden by
87     # default.
88     gdb_test_no_output "set verbose"
90     # Set follow-fork-mode if we aren't using the default.
91     if {${follow-fork-mode} == "default"} {
92         set follow-fork-mode "parent"
93     } else {
94         gdb_test_no_output "set follow-fork ${follow-fork-mode}"
95     }
97     gdb_test "show follow-fork" \
98     "Debugger response to a program call of fork or vfork is \"${follow-fork-mode}\"."
100     # Set detach-on-fork mode if we aren't using the default.
101     if {${detach-on-fork} == "default"} {
102         set detach-on-fork "on"
103     } else {
104         gdb_test_no_output "set detach-on-fork ${detach-on-fork}"
105     }
107     gdb_test "show detach-on-fork" \
108              "Whether gdb will detach.* fork is ${detach-on-fork}."
110     # Set a breakpoint after the fork if we aren't single-stepping
111     # past the fork.
112     if {$cmd == "continue"} {
113         set bp_after_fork [gdb_get_line_number "set breakpoint here"]
114         gdb_test "break ${srcfile}:$bp_after_fork" \
115                  "Breakpoint.*, line $bp_after_fork.*" \
116                  "set breakpoint after fork"
117     }
119     # Set up the output we expect to see after we run.
120     set expected_re ""
121     if {${follow-fork-mode} == "child"} {
122         set expected_re "\\\[Attaching after.* fork to.*"
123         if {${detach-on-fork} == "on"} {
124             append expected_re "\\\[Detaching after fork from .*"
125         }
126         append expected_re "set breakpoint here.*"
127     } elseif {${follow-fork-mode} == "parent" && ${detach-on-fork} == "on"} {
128         set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
129     } else {
130         set expected_re ".*set breakpoint here.*"
131     }
133     # Test running past and following the fork, using the parameters
134     # set above.
135     gdb_test $cmd $expected_re "$cmd past fork"
137     # Check that we have the inferiors arranged correctly after
138     # following the fork.
139     set resume_unfollowed 0
140     if {${follow-fork-mode} == "parent" && ${detach-on-fork} == "on"} {
142         # Follow parent / detach child: the only inferior is the parent.
143         gdb_test "info inferiors" "\\* 1 .* process.*"
145     } elseif {${follow-fork-mode} == "parent" && ${detach-on-fork} == "off"} {
147         # Follow parent / keep child: two inferiors under debug, the
148         # parent is the current inferior.
149         gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*"
151         gdb_test "inferior 2" "Switching to inferior 2 .*"
152         set resume_unfollowed 1
154     } elseif {${follow-fork-mode} == "child" && ${detach-on-fork} == "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
158         # debug.
159         gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*"
161     } elseif {${follow-fork-mode} == "child" && ${detach-on-fork} == "off"} {
163         # Follow child / keep parent: two inferiors under debug, the
164         # child is the current inferior.
165         gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*"
167         gdb_test "inferior 1" "Switching to inferior 1 .*"
168         set resume_unfollowed 1
169     }
171     if {$resume_unfollowed == 1} {
172         if {$cmd == "next 2"} {
174             gdb_continue_to_end "continue unfollowed inferior to end"
176         } elseif {$cmd == "continue"} {
178             gdb_continue_to_breakpoint \
179                 "continue unfollowed inferior to bp" \
180                 ".* set breakpoint here.*"
181         }
182     }
184     # If we end up with two inferiors, verify that they each end up with their
185     # own program space.  Do this by setting a breakpoint, if we see two
186     # locations it means there are two program spaces.
187     if {${detach-on-fork} == "off" || ${follow-fork-mode} == "child"} {
188         set bpnum "<unset>"
189         gdb_test_multiple "break callee" "break callee" {
190             -re -wrap "Breakpoint ($::decimal) at $::hex: callee\\. \\(2 locations\\)" {
191                 set bpnum $expect_out(1,string)
192                 pass $gdb_test_name
193             }
194         }
196         set any {[^\r\n]+}
198         set loc1_inf1 "$bpnum\\.1 $any inf 1"
199         set loc1_inf2 "$bpnum\\.1 $any inf 2"
201         set loc2_inf1 "$bpnum\\.2 $any inf 1"
202         set loc2_inf2 "$bpnum\\.2 $any inf 2"
204         gdb_test "info breakpoints $bpnum" \
205             "($loc1_inf1\r\n$loc2_inf2|$loc1_inf2\r\n$loc2_inf1)" \
206             "info breakpoints"
207     }
210 set reading_in_symbols_re {(?:\r\nReading in symbols for [^\r\n]*)?}
212 # Test the ability to catch a fork, specify that the child be
213 # followed, and continue.  Make the catchpoint permanent.
215 proc_with_prefix catch_fork_child_follow {} {
216     global gdb_prompt
217     global srcfile
218     global reading_in_symbols_re
220     if { ![setup] } {
221         return
222     }
224     set bp_after_fork [gdb_get_line_number "set breakpoint here"]
226     gdb_test "catch fork" \
227         "Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
228         "explicit child follow, set catch fork"
230     # Verify that the catchpoint is mentioned in an "info breakpoints",
231     # and further that the catchpoint mentions no process id.
232     gdb_test "info breakpoints" \
233         ".*catchpoint.*keep y.*fork\[\r\n\]+" \
234         "info breakpoints before fork"
236     gdb_test "continue" \
237         "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
238         "explicit child follow, catch fork"
240     # Verify that the catchpoint is mentioned in an "info breakpoints",
241     # and further that the catchpoint managed to capture a process id.
242     gdb_test "info breakpoints" \
243         ".*catchpoint.*keep y.*fork, process.*" \
244         "info breakpoints after fork"
246     gdb_test_no_output "set follow-fork child"
248     gdb_test "tbreak ${srcfile}:$bp_after_fork" \
249         "Temporary breakpoint.*, line $bp_after_fork.*" \
250         "set follow-fork child, tbreak"
252     set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
253     append expected_re ".* at .*$bp_after_fork.*"
254     gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
256     # The parent has been detached; allow time for any output it might
257     # generate to arrive, so that output doesn't get confused with
258     # any expected debugger output from a subsequent testpoint.
259     #
260     exec sleep 1
262     gdb_test "delete breakpoints" \
263         "" \
264         "set follow-fork child, cleanup" \
265         "Delete all breakpoints. \\(y or n\\) $" \
266         "y"
269 # Test that parent breakpoints are successfully detached from the
270 # child at fork time, even if the user removes them from the
271 # breakpoints list after stopping at a fork catchpoint.
273 proc_with_prefix catch_fork_unpatch_child {} {
274     global gdb_prompt
275     global srcfile
277     if { ![setup] } {
278         return
279     }
281     set bp_exit [gdb_get_line_number "at exit"]
283     gdb_test "break callee" "file .*$srcfile, line .*" \
284         "unpatch child, break at callee"
285     gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
286         "unpatch child, set catch fork"
288     gdb_test "continue" \
289         "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
290         "unpatch child, catch fork"
292     # Delete all breakpoints and catchpoints.
293     delete_breakpoints
295     # Force $srcfile as the current GDB source can be in glibc sourcetree.
296     gdb_test "break $srcfile:$bp_exit" \
297         "Breakpoint .*file .*$srcfile, line .*" \
298         "unpatch child, breakpoint at exit call"
300     gdb_test_no_output "set follow-fork child" \
301         "unpatch child, set follow-fork child"
303     set test "unpatch child, unpatched parent breakpoints from child"
304     gdb_test_multiple "continue" $test {
305         -re "at exit.*$gdb_prompt $" {
306             pass "$test"
307         }
308         -re "SIGTRAP.*$gdb_prompt $" {
309             fail "$test"
311             # Explicitly kill this child, so we can continue gracefully
312             # with further testing...
313             send_gdb "kill\n"
314             gdb_expect {
315                 -re ".*Kill the program being debugged.*y or n. $" {
316                     send_gdb "y\n"
317                     gdb_expect -re "$gdb_prompt $" {}
318                 }
319             }
320         }
321     }
324 # Test the ability to catch a fork, specify via a -do clause that
325 # the parent be followed, and continue.  Make the catchpoint temporary.
327 proc_with_prefix tcatch_fork_parent_follow {} {
328     global gdb_prompt
329     global srcfile
330     global reading_in_symbols_re
332     if { ![setup] } {
333         return
334     }
336     set bp_after_fork [gdb_get_line_number "set breakpoint here"]
338     gdb_test "catch fork" \
339         "Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
340         "explicit parent follow, set tcatch fork"
342 # ??rehrauer: I don't yet know how to get the id of the tcatch
343 # via this script, so that I can add a -do list to it.  For now,
344 # do the follow stuff after the catch happens.
346     gdb_test "continue" \
347         "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
348         "explicit parent follow, tcatch fork"
350     gdb_test_no_output "set follow-fork parent"
352     gdb_test "tbreak ${srcfile}:$bp_after_fork" \
353         "Temporary breakpoint.*, line $bp_after_fork.*" \
354         "set follow-fork parent, tbreak"
356     gdb_test "continue" \
357         "\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
358         "set follow-fork parent, hit tbreak"
360     # The child has been detached; allow time for any output it might
361     # generate to arrive, so that output doesn't get confused with
362     # any expected debugger output from a subsequent testpoint.
363     #
364     exec sleep 1
366     gdb_test "delete breakpoints" \
367         "" \
368         "set follow-fork parent, cleanup" \
369         "Delete all breakpoints. \\(y or n\\) $" \
370         "y"
373 # Test simple things about the "set follow-fork-mode" command.
375 proc_with_prefix test_set_follow_fork_command {} {
376     clean_restart
378     # Verify that help is available for "set follow-fork-mode".
379     #
380     gdb_test "help set follow-fork-mode" \
381         "Set debugger response to a program call of fork or vfork..*
382 A fork or vfork creates a new process.  follow-fork-mode can be:.*
383 .*parent  - the original process is debugged after a fork.*
384 .*child   - the new process is debugged after a fork.*
385 The unfollowed process will continue to run..*
386 By default, the debugger will follow the parent process..*"
388     # Verify that we can set follow-fork-mode, using an abbreviation
389     # for both the flag and its value.
390     #
391     gdb_test_no_output "set follow-fork ch"
393     gdb_test "show follow-fork" \
394         "Debugger response to a program call of fork or vfork is \"child\".*" \
395         "set follow-fork, using abbreviations"
397     # Verify that we cannot set follow-fork-mode to nonsense.
398     #
399     gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
400         "set follow-fork to nonsense is prohibited"
402     gdb_test_no_output "set follow-fork parent" "reset parent"
405 test_set_follow_fork_command
407 if { ![check_fork_catchpoints] } {
408     untested "follow-fork not supported"
409     return
412 # Test the basic follow-fork functionality using all combinations of
413 # values for follow-fork-mode and detach-on-fork, using either a
414 # breakpoint or single-step to execute past the fork.
416 # The first loop should be sufficient to test the defaults.  There
417 # is no need to test using the defaults in other permutations (e.g.
418 # "default" "on", "parent" "default", etc.).
419 foreach_with_prefix cmd {"next 2" "continue"} {
420     test_follow_fork "default" "default" $cmd
423 # Now test all explicit permutations.
424 foreach_with_prefix follow-fork-mode {"parent" "child"} {
425     foreach_with_prefix detach-on-fork {"on" "off"} {
426         foreach_with_prefix cmd {"next 2" "continue"} {
427             test_follow_fork ${follow-fork-mode} ${detach-on-fork} $cmd
428         }
429     }
432 # Catchpoint tests.
434 catch_fork_child_follow
435 catch_fork_unpatch_child
436 tcatch_fork_parent_follow