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