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