]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/testsuite/gdb.base/foll-vfork.exp
GDB copyright headers update after running GDB's copyright.py script.
[thirdparty/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-vfork.exp
CommitLineData
618f726f 1# Copyright 1997-2016 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
71ce889d
PA
16# Various tests of gdb's ability to follow the parent or child of a
17# Unix vfork system call. A vfork parent is blocked until the child
18# either execs or exits --- since those events take somewhat different
19# code paths in GDB, both variants are exercised.
20
2d4e0376
YQ
21# Until "set follow-fork-mode" and "catch vfork" are implemented on
22# other targets...
23#
3ca22649 24if {![istarget "*-linux*"]} then {
2d4e0376
YQ
25 continue
26}
27
771bc0af 28standard_testfile
74cf1395 29
3fef966c
TT
30set compile_options debug
31set dirname [relative_filename [pwd] [file dirname $binfile]]
32lappend compile_options "additional_flags=-DBASEDIR=\"$dirname\""
33
34if {[build_executable $testfile.exp $testfile $srcfile $compile_options] == -1} {
771bc0af
PA
35 untested "failed to compile $testfile"
36 return -1
74cf1395
JM
37}
38
771bc0af
PA
39set testfile2 "vforked-prog"
40set srcfile2 ${testfile2}.c
41
3fef966c 42if {[build_executable $testfile.exp $testfile2 $srcfile2 $compile_options] == -1} {
771bc0af
PA
43 untested "failed to compile $testfile2"
44 return -1
74cf1395
JM
45}
46
74cf1395
JM
47# A few of these tests require a little more time than the standard
48# timeout allows.
49set oldtimeout $timeout
50set timeout [expr "$timeout + 10"]
51
39c1263a
PA
52# Start with a fresh GDB, with verbosity enabled, and run to main. On
53# error, behave as "return", so we don't try to continue testing with
54# a borked session.
55proc setup_gdb {} {
56 global testfile
57
58 clean_restart $testfile
59
60 # The "Detaching..." and "Attaching..." messages may be hidden by
61 # default.
62 gdb_test_no_output "set verbose"
63
64 if ![runto_main] {
65 return -code return
66 }
67}
68
e85a822c
DJ
69proc check_vfork_catchpoints {} {
70 global gdb_prompt
71 global has_vfork_catchpoints
72
39c1263a
PA
73 setup_gdb
74
e85a822c
DJ
75 # Verify that the system supports "catch vfork".
76 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
77 set has_vfork_catchpoints 0
78 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
77b06cd7 79 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
e85a822c
DJ
80 unsupported "continue to first vfork catchpoint"
81 }
82 -re ".*Catchpoint.*$gdb_prompt $" {
83 set has_vfork_catchpoints 1
84 pass "continue to first vfork catchpoint"
85 }
86 }
87
88 if {$has_vfork_catchpoints == 0} {
89 unsupported "vfork catchpoints"
90 return -code return
91 }
92}
93
74cf1395 94proc vfork_parent_follow_through_step {} {
339aeac6 95 with_test_prefix "vfork parent follow, through step" {
74cf1395
JM
96 global gdb_prompt
97
39c1263a
PA
98 setup_gdb
99
339aeac6 100 gdb_test_no_output "set follow-fork parent"
771bc0af 101
339aeac6 102 set test "step"
771bc0af 103 gdb_test_multiple "next" $test {
6f259a23 104 -re "Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " {
339aeac6 105 pass $test
771bc0af 106 }
74cf1395
JM
107 }
108 # The child has been detached; allow time for any output it might
109 # generate to arrive, so that output doesn't get confused with
110 # any gdb_expected debugger output from a subsequent testpoint.
111 #
112 exec sleep 1
339aeac6 113}}
74cf1395
JM
114
115proc vfork_parent_follow_to_bp {} {
339aeac6 116 with_test_prefix "vfork parent follow, to bp" {
74cf1395 117 global gdb_prompt
e85a822c 118 global srcfile
74cf1395 119
39c1263a
PA
120 setup_gdb
121
339aeac6 122 gdb_test_no_output "set follow-fork parent"
771bc0af 123
43ae8f01
PA
124 set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"]
125 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
771bc0af 126
339aeac6 127 set test "continue to bp"
771bc0af 128 gdb_test_multiple "continue" $test {
6f259a23 129 -re ".*Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " {
771bc0af
PA
130 pass $test
131 }
74cf1395
JM
132 }
133 # The child has been detached; allow time for any output it might
134 # generate to arrive, so that output doesn't get confused with
135 # any expected debugger output from a subsequent testpoint.
136 #
137 exec sleep 1
339aeac6 138}}
74cf1395 139
71ce889d
PA
140proc vfork_child_follow_to_exit {} {
141 with_test_prefix "vfork child follow, to exit" {
142 global gdb_prompt
143
144 setup_gdb
145
146 gdb_test_no_output "set follow-fork child"
147
148 set test "continue to child exit"
149 gdb_test_multiple "continue" $test {
150 -re "Couldn't get registers.*$gdb_prompt " {
f50f4e56 151 # PR gdb/14766
71ce889d
PA
152 fail "$test"
153 }
154 -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " {
155 pass $test
156 }
157 }
158 # The parent has been detached; allow time for any output it might
159 # generate to arrive, so that output doesn't get confused with
160 # any gdb_expected debugger output from a subsequent testpoint.
161 #
162 exec sleep 1
163}}
164
74cf1395 165proc vfork_and_exec_child_follow_to_main_bp {} {
339aeac6 166 with_test_prefix "vfork and exec child follow, to main bp" {
74cf1395 167 global gdb_prompt
43ae8f01 168 global srcfile2
74cf1395 169
39c1263a
PA
170 setup_gdb
171
339aeac6 172 gdb_test_no_output "set follow-fork child"
771bc0af 173
43ae8f01
PA
174 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
175
339aeac6 176 set test "continue to bp"
771bc0af 177 gdb_test_multiple "continue" $test {
6f259a23 178 -re "Attaching after.* vfork to.*Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
771bc0af
PA
179 pass $test
180 }
74cf1395
JM
181 }
182 # The parent has been detached; allow time for any output it might
183 # generate to arrive, so that output doesn't get confused with
184 # any gdb_expected debugger output from a subsequent testpoint.
185 #
186 exec sleep 1
339aeac6 187}}
74cf1395
JM
188
189proc vfork_and_exec_child_follow_through_step {} {
339aeac6 190 with_test_prefix "vfork and exec child follow, through step" {
74cf1395 191 global gdb_prompt
43ae8f01 192 global srcfile2
74cf1395 193
39c1263a
PA
194 setup_gdb
195
339aeac6 196 gdb_test_no_output "set follow-fork child"
771bc0af 197
339aeac6 198 set test "step over vfork"
3ca22649
SM
199
200 # The ideal support is to be able to debug the child even
201 # before it execs. Thus, "next" lands on the next line after
202 # the vfork.
203 gdb_test_multiple "next" $test {
204 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
205 pass "$test"
6dbc8997 206 }
74cf1395
JM
207 }
208 # The parent has been detached; allow time for any output it might
209 # generate to arrive, so that output doesn't get confused with
210 # any expected debugger output from a subsequent testpoint.
211 #
212 exec sleep 1
339aeac6 213}}
74cf1395
JM
214
215proc tcatch_vfork_then_parent_follow {} {
339aeac6 216 with_test_prefix "vfork parent follow, finish after tcatch vfork" {
74cf1395
JM
217 global gdb_prompt
218 global srcfile
219
39c1263a
PA
220 setup_gdb
221
339aeac6 222 gdb_test_no_output "set follow-fork parent"
771bc0af 223
339aeac6 224 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
fe33faff
SM
225 gdb_test "continue" "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*" \
226 "continue to vfork"
771bc0af 227
43ae8f01 228 set linenum [gdb_get_line_number "pid = vfork ();"]
339aeac6 229 set test "finish"
771bc0af 230 gdb_test_multiple "finish" $test {
43ae8f01 231 -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " {
771bc0af
PA
232 pass $test
233 }
a9c64011
AS
234 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
235 send_gdb "finish\n"
236 exp_continue
237 }
74cf1395
JM
238 }
239 # The child has been detached; allow time for any output it might
240 # generate to arrive, so that output doesn't get confused with
241 # any expected debugger output from a subsequent testpoint.
242 #
243 exec sleep 1
339aeac6 244}}
74cf1395 245
71ce889d 246proc tcatch_vfork_then_child_follow_exec {} {
339aeac6 247 with_test_prefix "vfork child follow, finish after tcatch vfork" {
74cf1395 248 global gdb_prompt
e85a822c 249 global srcfile
74cf1395
JM
250 global srcfile2
251
39c1263a
PA
252 setup_gdb
253
339aeac6 254 gdb_test_no_output "set follow-fork child"
771bc0af 255
339aeac6 256 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
fe33faff
SM
257 gdb_test "continue" "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*" \
258 "continue to vfork"
771bc0af 259
43ae8f01
PA
260 set linenum1 [gdb_get_line_number "pid = vfork ();"]
261 set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
262
339aeac6 263 set test "finish"
771bc0af 264 gdb_test_multiple "finish" $test {
43ae8f01 265 -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " {
771bc0af
PA
266 pass $test
267 }
a9c64011
AS
268 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
269 send_gdb "finish\n"
270 exp_continue
271 }
43ae8f01 272 -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " {
771bc0af
PA
273 pass "$test (followed exec)"
274 }
74cf1395
JM
275 }
276 # The parent has been detached; allow time for any output it might
277 # generate to arrive, so that output doesn't get confused with
278 # any expected debugger output from a subsequent testpoint.
279 #
280 exec sleep 1
339aeac6 281}}
74cf1395 282
71ce889d
PA
283proc tcatch_vfork_then_child_follow_exit {} {
284 with_test_prefix "vfork child follow, finish after tcatch vfork" {
74cf1395 285 global gdb_prompt
71ce889d
PA
286 global srcfile
287
288 setup_gdb
289
290 gdb_test_no_output "set follow-fork child"
291
292 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
fe33faff
SM
293 gdb_test "continue" "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*" \
294 "continue to vfork"
71ce889d
PA
295
296 set test "finish"
297 gdb_test_multiple "finish" $test {
71ce889d
PA
298 -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " {
299 setup_kfail "gdb/14762" *-*-*
300 fail $test
301 }
302 -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " {
303 pass $test
304 }
305 -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
306 send_gdb "finish\n"
307 exp_continue
308 }
309 }
310 # The parent has been detached; allow time for any output it might
311 # generate to arrive, so that output doesn't get confused with
312 # any expected debugger output from a subsequent testpoint.
313 #
314 exec sleep 1
315}}
74cf1395 316
68c9da30
PA
317proc vfork_relations_in_info_inferiors { variant } {
318 with_test_prefix "vfork relations in info inferiors" {
319 global gdb_prompt
320
321 setup_gdb
322
323 gdb_test_no_output "set follow-fork child"
324
325 set test "step over vfork"
326 gdb_test_multiple "next" $test {
327 -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " {
328 pass "$test"
329 }
330 }
331
332 gdb_test "info inferiors" \
7e0aa6aa 333 ".*is vfork parent of inferior 2.*is vfork child of inferior 1" \
68c9da30
PA
334 "info inferiors shows vfork parent/child relation"
335
336 if { $variant == "exec" } {
337 global srcfile2
338
339 set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}]
340 set test "continue to bp"
341 gdb_test_multiple "continue" $test {
342 -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " {
343 pass $test
344 }
345 }
346 } else {
347 set test "continue to child exit"
348 gdb_test_multiple "continue" $test {
349 -re "exited normally.*$gdb_prompt " {
350 pass $test
351 }
352 }
353 }
354
355 set test "vfork relation no longer appears in info inferiors"
356 gdb_test_multiple "info inferiors" $test {
357 -re "is vfork child of inferior 1.*$gdb_prompt $" {
358 fail $test
359 }
360 -re "is vfork parent of inferior 2.*$gdb_prompt $" {
361 fail $test
362 }
363 -re "$gdb_prompt $" {
364 pass $test
365 }
366 }
367}}
368
71ce889d
PA
369proc do_vfork_and_follow_parent_tests {} {
370 global gdb_prompt
e85a822c 371
74cf1395
JM
372 # Try following the parent process by stepping through a call to
373 # vfork. Do this without catchpoints.
39c1263a 374 vfork_parent_follow_through_step
74cf1395
JM
375
376 # Try following the parent process by setting a breakpoint on the
377 # other side of a vfork, and running to that point. Do this
378 # without catchpoints.
39c1263a 379 vfork_parent_follow_to_bp
74cf1395 380
71ce889d
PA
381 # Try catching a vfork, and stepping out to the parent.
382 #
383 tcatch_vfork_then_parent_follow
384}
385
386proc do_vfork_and_follow_child_tests_exec {} {
74cf1395
JM
387 # Try following the child process by just continuing through the
388 # vfork, and letting the parent's breakpoint on "main" be auto-
389 # magically reset in the child.
390 #
39c1263a 391 vfork_and_exec_child_follow_to_main_bp
74cf1395
JM
392
393 # Try following the child process by stepping through a call to
394 # vfork. The child also executes an exec. Since the child cannot
395 # be debugged until after it has exec'd, and since there's a bp on
396 # "main" in the parent, and since the bp's for the parent are
397 # recomputed in the exec'd child, the step through a vfork should
398 # land us in the "main" for the exec'd child, too.
399 #
39c1263a 400 vfork_and_exec_child_follow_through_step
74cf1395 401
74cf1395
JM
402 # Try catching a vfork, and stepping out to the child.
403 #
71ce889d 404 tcatch_vfork_then_child_follow_exec
74cf1395
JM
405
406 # Test the ability to follow both child and parent of a vfork. Do
407 # this without catchpoints.
408 # ??rehrauer: NYI. Will add testpoints here when implemented.
409 #
410
411 # Test the ability to have the debugger ask the user at vfork-time
412 # whether to follow the parent, child or both. Do this without
413 # catchpoints.
414 # ??rehrauer: NYI. Will add testpoints here when implemented.
415 #
68c9da30
PA
416
417 # Step over a vfork in the child, do "info inferiors" and check the
418 # parent/child relation is displayed. Run the child over the exec,
419 # and confirm the relation is no longer displayed in "info
420 # inferiors".
421 #
422 vfork_relations_in_info_inferiors "exec"
74cf1395
JM
423}
424
71ce889d
PA
425proc do_vfork_and_follow_child_tests_exit {} {
426 # Try following the child process by just continuing through the
427 # vfork, and letting the child exit.
428 #
429 vfork_child_follow_to_exit
430
431 # Try catching a vfork, and stepping out to the child.
432 #
433 tcatch_vfork_then_child_follow_exit
68c9da30
PA
434
435 # Step over a vfork in the child, do "info inferiors" and check the
436 # parent/child relation is displayed. Run the child to completion,
437 # and confirm the relation is no longer displayed in "info
438 # inferiors".
439 #
440 vfork_relations_in_info_inferiors "exit"
71ce889d
PA
441}
442
443with_test_prefix "check vfork support" {
444 # Check that vfork catchpoints are supported, as an indicator for
445 # whether vfork-following is supported.
446 check_vfork_catchpoints
447}
448
2fd33e94
DB
449# Follow parent and follow child vfork tests with a child that execs.
450with_test_prefix "exec" {
451 # These are tests of gdb's ability to follow the parent of a Unix
452 # vfork system call. The child will subsequently call a variant
453 # of the Unix exec system call.
454 do_vfork_and_follow_parent_tests
455
456 # These are tests of gdb's ability to follow the child of a Unix
457 # vfork system call. The child will subsequently call a variant
458 # of a Unix exec system call.
459 #
460 do_vfork_and_follow_child_tests_exec
71ce889d
PA
461}
462
463# Switch to test the case of the child exiting. We can't use
464# standard_testfile here because we don't want to overwrite the binary
465# of the previous tests.
466set testfile "foll-vfork-exit"
467set srcfile ${testfile}.c
468set binfile [standard_output_file ${testfile}]
469
470if {[build_executable $testfile.exp $testfile $srcfile] == -1} {
471 untested "failed to build $testfile"
472 return
473}
474
475# Follow parent and follow child vfork tests with a child that exits.
476with_test_prefix "exit" {
477 # These are tests of gdb's ability to follow the parent of a Unix
478 # vfork system call. The child will subsequently exit.
479 do_vfork_and_follow_parent_tests
480
481 # These are tests of gdb's ability to follow the child of a Unix
482 # vfork system call. The child will subsequently exit.
483 #
484 do_vfork_and_follow_child_tests_exit
485}
74cf1395
JM
486
487set timeout $oldtimeout
488return 0