Automatic Copyright Year update after running gdb/copyright.py
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
CommitLineData
88b9d363 1# Copyright 1997-2022 Free Software Foundation, Inc.
74cf1395
JM
2
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
e22f8b7c 5# the Free Software Foundation; either version 3 of the License, or
74cf1395 6# (at your option) any later version.
e22f8b7c 7#
74cf1395
JM
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.
e22f8b7c 12#
74cf1395 13# You should have received a copy of the GNU General Public License
e85a822c 14# along with this program. If not, see <http://www.gnu.org/licenses/>.
74cf1395 15
2d4e0376
YQ
16# Until "set follow-fork-mode" and "catch fork" are implemented on
17# other targets...
18#
3ca22649 19if { ![istarget "*-*-linux*"] && ![istarget "*-*-openbsd*"] } then {
2d4e0376
YQ
20 continue
21}
74cf1395 22
81f47ac2
AH
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.
25if [gdb_debug_enabled] {
26 untested "debug is enabled"
27 return 0
28}
29
289f9037
TT
30standard_testfile
31
5b362f04 32if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
289f9037 33 return -1
74cf1395
JM
34}
35
e85a822c
DJ
36proc check_fork_catchpoints {} {
37 global gdb_prompt
38
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" {
77b06cd7 43 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
e85a822c
DJ
44 unsupported "continue to first fork catchpoint"
45 }
46 -re ".*Catchpoint.*$gdb_prompt $" {
47 set has_fork_catchpoints 1
48 pass "continue to first fork catchpoint"
49 }
50 }
51
52 if {$has_fork_catchpoints == 0} {
53 unsupported "fork catchpoints"
54 return -code return
55 }
56}
57
a1aa2221
LM
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'.
67proc test_follow_fork { who detach cmd } {
a76e022a 68 global gdb_prompt
a1aa2221
LM
69 global srcfile
70 global testfile
a76e022a 71
a1aa2221 72 with_test_prefix "follow $who, detach $detach, command \"$cmd\"" {
a76e022a 73
a1aa2221
LM
74 # Start a new debugger session each time so defaults are legitimate.
75 clean_restart $testfile
a76e022a 76
a1aa2221
LM
77 if ![runto_main] {
78 untested "could not run to main"
79 return -1
80 }
74cf1395 81
a1aa2221
LM
82 # The "Detaching..." and "Attaching..." messages may be hidden by
83 # default.
84 gdb_test_no_output "set verbose"
a76e022a 85
a1aa2221
LM
86 # Set follow-fork-mode if we aren't using the default.
87 if {$who == "default"} {
88 set who "parent"
89 } else {
90 gdb_test_no_output "set follow-fork $who"
91 }
a76e022a 92
a1aa2221 93 gdb_test "show follow-fork" \
11af934b 94 "Debugger response to a program call of fork or vfork is \"$who\"."
a76e022a 95
a1aa2221
LM
96 # Set detach-on-fork mode if we aren't using the default.
97 if {$detach == "default"} {
98 set detach "on"
99 } else {
100 gdb_test_no_output "set detach-on-fork $detach"
101 }
a76e022a 102
a1aa2221 103 gdb_test "show detach-on-fork" \
11af934b 104 "Whether gdb will detach.* fork is $detach."
a1aa2221
LM
105
106 # Set a breakpoint after the fork if we aren't single-stepping
107 # past the fork.
108 if {$cmd == "continue"} {
109 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
110 gdb_test "break ${srcfile}:$bp_after_fork" \
111 "Breakpoint.*, line $bp_after_fork.*" \
112 "set breakpoint after fork"
113 }
74cf1395 114
a1aa2221
LM
115 # Set up the output we expect to see after we run.
116 set expected_re ""
117 if {$who == "child"} {
f67c0c91 118 set expected_re "\\\[Attaching after.* fork to.*"
6f259a23 119 if {$detach == "on"} {
f67c0c91 120 append expected_re "\\\[Detaching after fork from .*"
6f259a23
DB
121 }
122 append expected_re "set breakpoint here.*"
a1aa2221 123 } elseif {$who == "parent" && $detach == "on"} {
f67c0c91 124 set expected_re "\\\[Detaching after fork from .*set breakpoint here.*"
a1aa2221
LM
125 } else {
126 set expected_re ".*set breakpoint here.*"
127 }
a76e022a 128
a1aa2221
LM
129 # Test running past and following the fork, using the parameters
130 # set above.
131 gdb_test $cmd $expected_re "$cmd past fork"
a76e022a 132
a1aa2221
LM
133 # Check that we have the inferiors arranged correctly after
134 # following the fork.
135 set resume_unfollowed 0
136 if {$who == "parent" && $detach == "on"} {
a76e022a 137
a1aa2221 138 # Follow parent / detach child: the only inferior is the parent.
11af934b 139 gdb_test "info inferiors" "\\* 1 .* process.*"
a76e022a 140
a1aa2221
LM
141 } elseif {$who == "parent" && $detach == "off"} {
142
143 # Follow parent / keep child: two inferiors under debug, the
144 # parent is the current inferior.
11af934b 145 gdb_test "info inferiors" "\\* 1 .*process.* 2 .*process.*"
a1aa2221
LM
146
147 gdb_test "inferior 2" "Switching to inferior 2 .*"
148 set resume_unfollowed 1
149
150 } elseif {$who == "child" && $detach == "on"} {
151
152 # Follow child / detach parent: the child is under debug and is
153 # the current inferior. The parent is listed but is not under
154 # debug.
11af934b 155 gdb_test "info inferiors" " 1 .*<null>.*\\* 2 .*process.*"
a1aa2221
LM
156
157 } elseif {$who == "child" && $detach == "off"} {
158
159 # Follow child / keep parent: two inferiors under debug, the
160 # child is the current inferior.
11af934b 161 gdb_test "info inferiors" " 1 .*process.*\\* 2 .*process.*"
a1aa2221
LM
162
163 gdb_test "inferior 1" "Switching to inferior 1 .*"
164 set resume_unfollowed 1
165 }
166
167 if {$resume_unfollowed == 1} {
168 if {$cmd == "next 2"} {
169
170 gdb_continue_to_end "continue unfollowed inferior to end"
171
172 } elseif {$cmd == "continue"} {
173
174 gdb_continue_to_breakpoint \
175 "continue unfollowed inferior to bp" \
176 ".* set breakpoint here.*"
177 }
178 }
179 }
74cf1395
JM
180}
181
7462c383
TV
182set reading_in_symbols_re {(?:\r\nReading in symbols for [^\r\n]*)?}
183
74cf1395 184proc catch_fork_child_follow {} {
a76e022a
MS
185 global gdb_prompt
186 global srcfile
7462c383 187 global reading_in_symbols_re
a76e022a
MS
188
189 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
190
7462c383
TV
191 gdb_test "catch fork" \
192 "Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
a76e022a
MS
193 "explicit child follow, set catch fork"
194
195 # Verify that the catchpoint is mentioned in an "info breakpoints",
196 # and further that the catchpoint mentions no process id.
197 #
198 set test_name "info shows catchpoint without pid"
199 gdb_test_multiple "info breakpoints" "$test_name" {
200 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
201 pass "$test_name"
202 }
203 }
204
205 gdb_test "continue" \
2c04da0d 206 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
207 "explicit child follow, catch fork"
208
209 # Verify that the catchpoint is mentioned in an "info breakpoints",
210 # and further that the catchpoint managed to capture a process id.
211 #
212 set test_name "info shows catchpoint without pid"
213 gdb_test_multiple "info breakpoints" "$test_name" {
214 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
215 pass "$test_name"
216 }
217 }
218
219 gdb_test_no_output "set follow-fork child"
220
221 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
222 "Temporary breakpoint.*, line $bp_after_fork.*" \
223 "set follow-fork child, tbreak"
224
f67c0c91 225 set expected_re "\\\[Attaching after.* fork to.*\\\[Detaching after fork from"
6f259a23
DB
226 append expected_re ".* at .*$bp_after_fork.*"
227 gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
a76e022a
MS
228
229 # The parent has been detached; allow time for any output it might
230 # generate to arrive, so that output doesn't get confused with
231 # any expected debugger output from a subsequent testpoint.
232 #
233 exec sleep 1
234
235 gdb_test "delete breakpoints" \
236 "" \
237 "set follow-fork child, cleanup" \
e9101ff5 238 "Delete all breakpoints. \\(y or n\\) $" \
a76e022a 239 "y"
74cf1395
JM
240}
241
b242c3c2 242proc catch_fork_unpatch_child {} {
a76e022a
MS
243 global gdb_prompt
244 global srcfile
245
246 set bp_exit [gdb_get_line_number "at exit"]
247
248 gdb_test "break callee" "file .*$srcfile, line .*" \
249 "unpatch child, break at callee"
250 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
251 "unpatch child, set catch fork"
252
253 gdb_test "continue" \
2c04da0d 254 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
255 "unpatch child, catch fork"
256
257 # Delete all breakpoints and catchpoints.
258 delete_breakpoints
259
260 # Force $srcfile as the current GDB source can be in glibc sourcetree.
261 gdb_test "break $srcfile:$bp_exit" \
262 "Breakpoint .*file .*$srcfile, line .*" \
263 "unpatch child, breakpoint at exit call"
264
27d3a1a2
MS
265 gdb_test_no_output "set follow-fork child" \
266 "unpatch child, set follow-fork child"
a76e022a
MS
267
268 set test "unpatch child, unpatched parent breakpoints from child"
269 gdb_test_multiple "continue" $test {
270 -re "at exit.*$gdb_prompt $" {
271 pass "$test"
272 }
273 -re "SIGTRAP.*$gdb_prompt $" {
274 fail "$test"
275
276 # Explicitly kill this child, so we can continue gracefully
277 # with further testing...
278 send_gdb "kill\n"
279 gdb_expect {
280 -re ".*Kill the program being debugged.*y or n. $" {
281 send_gdb "y\n"
282 gdb_expect -re "$gdb_prompt $" {}
283 }
284 }
285 }
286 }
b242c3c2
PA
287}
288
74cf1395 289proc tcatch_fork_parent_follow {} {
a76e022a
MS
290 global gdb_prompt
291 global srcfile
7462c383 292 global reading_in_symbols_re
a76e022a
MS
293
294 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
295
7462c383
TV
296 gdb_test "catch fork" \
297 "Catchpoint \[0-9\]* \\(fork\\)$reading_in_symbols_re" \
a76e022a
MS
298 "explicit parent follow, set tcatch fork"
299
74cf1395
JM
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.
303
2c04da0d
UW
304 gdb_test "continue" \
305 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
a76e022a
MS
306 "explicit parent follow, tcatch fork"
307
308 gdb_test_no_output "set follow-fork parent"
309
310 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
311 "Temporary breakpoint.*, line $bp_after_fork.*" \
312 "set follow-fork parent, tbreak"
313
314 gdb_test "continue" \
f67c0c91 315 "\\\[Detaching after fork from.* at .*$bp_after_fork.*" \
a76e022a
MS
316 "set follow-fork parent, hit tbreak"
317
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.
321 #
322 exec sleep 1
323
324 gdb_test "delete breakpoints" \
325 "" \
326 "set follow-fork parent, cleanup" \
e9101ff5 327 "Delete all breakpoints. \\(y or n\\) $" \
a76e022a 328 "y"
74cf1395
JM
329}
330
331proc do_fork_tests {} {
a76e022a 332 global gdb_prompt
a1aa2221 333 global testfile
74cf1395 334
a76e022a
MS
335 # Verify that help is available for "set follow-fork-mode".
336 #
337 gdb_test "help set follow-fork-mode" \
338 "Set debugger response to a program call of fork or vfork..*
74cf1395
JM
339A 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.*
ef04f396 342The unfollowed process will continue to run..*
11af934b 343By default, the debugger will follow the parent process..*"
a76e022a
MS
344
345 # Verify that we can set follow-fork-mode, using an abbreviation
346 # for both the flag and its value.
347 #
348 gdb_test_no_output "set follow-fork ch"
349
350 gdb_test "show follow-fork" \
351 "Debugger response to a program call of fork or vfork is \"child\".*" \
352 "set follow-fork, using abbreviations"
353
354 # Verify that we cannot set follow-fork-mode to nonsense.
355 #
356 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
357 "set follow-fork to nonsense is prohibited"
358
359 gdb_test_no_output "set follow-fork parent" "reset parent"
360
361 # Check that fork catchpoints are supported, as an indicator for whether
362 # fork-following is supported.
363 if [runto_main] then { check_fork_catchpoints }
364
a1aa2221
LM
365 # Test the basic follow-fork functionality using all combinations of
366 # values for follow-fork-mode and detach-on-fork, using either a
367 # breakpoint or single-step to execute past the fork.
a76e022a 368 #
a1aa2221
LM
369 # The first loop should be sufficient to test the defaults. There
370 # is no need to test using the defaults in other permutations (e.g.
371 # "default" "on", "parent" "default", etc.).
372 foreach cmd {"next 2" "continue"} {
373 test_follow_fork "default" "default" $cmd
374 }
a76e022a 375
a1aa2221
LM
376 # Now test all explicit permutations.
377 foreach who {"parent" "child"} {
378 foreach detach {"on" "off"} {
379 foreach cmd {"next 2" "continue"} {
380 test_follow_fork $who $detach $cmd
381 }
382 }
383 }
a76e022a 384
a1aa2221 385 # Catchpoint tests.
a76e022a 386
a1aa2221
LM
387 # Restart to eliminate any effects of the follow-fork tests.
388 clean_restart $testfile
389 gdb_test_no_output "set verbose"
a76e022a
MS
390
391 # Test the ability to catch a fork, specify that the child be
392 # followed, and continue. Make the catchpoint permanent.
393 #
394 if [runto_main] then { catch_fork_child_follow }
395
396 # Test that parent breakpoints are successfully detached from the
397 # child at fork time, even if the user removes them from the
398 # breakpoints list after stopping at a fork catchpoint.
399 if [runto_main] then { catch_fork_unpatch_child }
400
401 # Test the ability to catch a fork, specify via a -do clause that
402 # the parent be followed, and continue. Make the catchpoint temporary.
403 #
404 if [runto_main] then { tcatch_fork_parent_follow }
74cf1395
JM
405}
406
74cf1395
JM
407# This is a test of gdb's ability to follow the parent, child or both
408# parent and child of a Unix fork() system call.
409#
410do_fork_tests
411
412return 0
This page took 2.666609 seconds and 4 git commands to generate.