1 # Copyright 1997-2024 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 # 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.
21 # Until "set follow-fork-mode" and "catch vfork" are implemented on
24 if {![istarget "*-linux*"]} {
28 standard_testfile .c -exit.c vforked-prog.c
31 set binfile2 ${testfile}-exit
32 set binfile3 vforked-prog
34 if {[build_executable "compile $binfile" $binfile $srcfile] == -1} {
35 untested "failed to compile first test binary"
39 if {[build_executable "compile $binfile2" $binfile2 $srcfile2] == -1} {
40 untested "failed to compile second test binary"
44 if {[build_executable "compile $binfile3" $binfile3 $srcfile3] == -1} {
45 untested "failed to compile third test binary"
49 # If required, download the program that we exec after vfork to the
51 if { [is_remote target] } {
52 gdb_remote_download target [standard_output_file $binfile3]
55 # Start with a fresh GDB, with verbosity enabled, and run to main. On
56 # error, behave as "return", so we don't try to continue testing with
58 proc setup_gdb { binfile srcfile } {
59 clean_restart $binfile
65 gdb_breakpoint [gdb_get_line_number " VFORK " $srcfile] temporary
66 gdb_continue_to_breakpoint "at VFORK"
69 proc check_vfork_catchpoints {} {
70 # Because setup_gdb uses 'return -code return' which would return to
71 # our caller we need to wrap this call, spot when setup_gdb failed
72 # (with return code 2), and then issue our own 'return -code return'.
73 set code [catch {setup_gdb $::binfile $::srcfile} string]
75 unsupported "vfork catchpoints"
79 # Verify that the system supports "catch vfork".
80 gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
81 set has_vfork_catchpoints 0
82 gdb_test_multiple "continue" "continue to first vfork catchpoint" {
83 -re -wrap ".*Your system does not support this type\r\nof catchpoint.*" {
84 unsupported "continue to first vfork catchpoint"
86 -re -wrap ".*Catchpoint.*" {
87 set has_vfork_catchpoints 1
88 pass "continue to first vfork catchpoint"
92 if {$has_vfork_catchpoints == 0} {
93 unsupported "vfork catchpoints"
98 proc_with_prefix vfork_parent_follow_through_step { binfile srcfile } {
99 setup_gdb $binfile $srcfile
101 gdb_test_no_output "set follow-fork parent"
103 gdb_test_multiple "next" "" {
104 -re -wrap "\\\[Detaching after vfork from.*if \\(pid == 0\\).*" {
110 proc_with_prefix vfork_parent_follow_to_bp { binfile srcfile } {
111 setup_gdb $binfile $srcfile
113 gdb_test_no_output "set follow-fork parent"
116 [gdb_get_line_number "I'm the proud parent of child" $srcfile]
117 gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp"
119 gdb_test_multiple "continue" "continue to bp" {
120 -re -wrap ".*\\\[Detaching after vfork from child process.*Breakpoint.*${bp_location}.*" {
126 proc_with_prefix vfork_child_follow_to_exit { binfile srcfile } {
127 setup_gdb $binfile $srcfile
129 gdb_test_no_output "set follow-fork child"
131 gdb_test_multiple "continue" "continue to child exit" {
132 -re -wrap "Couldn't get registers.*" {
133 kfail $gdb_test_name "gdb/14766"
135 -re -wrap "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent .* after child exit.*" {
141 proc_with_prefix vfork_and_exec_child_follow_to_main_bp { binfile srcfile } {
142 setup_gdb $binfile $srcfile
144 gdb_test_no_output "set follow-fork child"
146 set linenum [gdb_get_line_number "Hello from vforked-prog" ${::srcfile3}]
148 gdb_test_multiple "continue" "continue to bp" {
149 -re -wrap "\\\[Attaching after.* vfork to.*\\\[Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*" {
155 proc_with_prefix vfork_and_exec_child_follow_through_step { binfile srcfile } {
156 setup_gdb $binfile $srcfile
158 gdb_test_no_output "set follow-fork child"
160 # The ideal support is to be able to debug the child even
161 # before it execs. Thus, "next" lands on the next line after
163 gdb_test_multiple "next" "next over vfork" {
164 -re -wrap "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*" {
170 proc continue_to_vfork {} {
171 # A vfork catchpoint may stop in either "vfork" or "_vfork".
172 gdb_test_multiple "continue" "continue to vfork" {
173 -re -wrap "vfork \\(\\) at .*" {
176 -re -wrap "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*" {
182 proc_with_prefix tcatch_vfork_then_parent_follow { binfile srcfile } {
183 setup_gdb $binfile $srcfile
185 gdb_test_no_output "set follow-fork parent"
187 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
191 set linenum [gdb_get_line_number "pid = vfork ();" $srcfile]
192 gdb_test_multiple "finish" "" {
193 -re -wrap "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*" {
196 -re -wrap "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*" {
203 proc_with_prefix tcatch_vfork_then_child_follow_exec { binfile srcfile } {
204 setup_gdb $binfile $srcfile
206 gdb_test_no_output "set follow-fork child"
208 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
212 set linenum1 [gdb_get_line_number "pid = vfork ();" $srcfile]
213 set linenum2 [gdb_get_line_number "Hello from vforked-prog" ${::srcfile3}]
215 gdb_test_multiple "finish" "" {
216 -re -wrap "Run till exit from.*vfork.*${srcfile}:${linenum1}.*" {
219 -re -wrap "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*" {
223 -re -wrap "Run till exit from.*vfork.*${::srcfile3}:${linenum2}.*" {
224 pass "$gdb_test_name (followed exec)"
229 proc tcatch_vfork_then_child_follow_exit { binfile srcfile } {
230 setup_gdb $binfile $srcfile
232 gdb_test_no_output "set follow-fork child"
234 gdb_test "tcatch vfork" "Catchpoint .*(vfork).*"
238 gdb_test_multiple "finish" "" {
239 -re -wrap "Run till exit from.*vfork.*exited normally.*" {
240 setup_kfail "gdb/14762" *-*-*
243 -re -wrap "Run till exit from.*vfork.*pid = vfork \\(\\).*" {
246 -re -wrap "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*" {
253 proc_with_prefix vfork_relations_in_info_inferiors { variant binfile srcfile non_stop } {
254 setup_gdb $binfile $srcfile
256 gdb_test_no_output "set follow-fork child"
258 gdb_test_multiple "next" "next over vfork" {
259 -re -wrap "\\\[Attaching after .* vfork to child.*if \\(pid == 0\\).*" {
265 gdb_test "inferior 2" ".*"
268 gdb_test "info inferiors" \
269 ".*is vfork parent of inferior 2.*is vfork child of inferior 1" \
270 "info inferiors shows vfork parent/child relation"
272 if { $variant == "exec" } {
273 set linenum [gdb_get_line_number "Hello from vforked-prog" ${::srcfile3}]
274 gdb_test_multiple "continue" "continue to bp" {
275 -re -wrap ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*" {
280 gdb_continue_to_end "continue until exit" "continue" true
283 gdb_test_multiple "info inferiors" "vfork relation no longer appears in info inferiors" {
284 -re -wrap "is vfork child of inferior 1.*" {
287 -re -wrap "is vfork parent of inferior 2.*" {
296 proc do_vfork_and_follow_parent_tests { binfile srcfile } {
297 # Try following the parent process by stepping through a call to
298 # vfork. Do this without catchpoints.
299 vfork_parent_follow_through_step $binfile $srcfile
301 # Try following the parent process by setting a breakpoint on the
302 # other side of a vfork, and running to that point. Do this
303 # without catchpoints.
304 vfork_parent_follow_to_bp $binfile $srcfile
306 # Try catching a vfork, and stepping out to the parent.
308 tcatch_vfork_then_parent_follow $binfile $srcfile
311 proc do_vfork_and_follow_child_tests_exec { binfile srcfile non_stop } {
312 # Try following the child process by just continuing through the
313 # vfork, and letting the parent's breakpoint on "main" be auto-
314 # magically reset in the child.
316 vfork_and_exec_child_follow_to_main_bp $binfile $srcfile
318 # Try following the child process by stepping through a call to
319 # vfork. The child also executes an exec. Since the child cannot
320 # be debugged until after it has exec'd, and since there's a bp on
321 # "main" in the parent, and since the bp's for the parent are
322 # recomputed in the exec'd child, the step through a vfork should
323 # land us in the "main" for the exec'd child, too.
325 vfork_and_exec_child_follow_through_step $binfile $srcfile
327 # Try catching a vfork, and stepping out to the child.
329 tcatch_vfork_then_child_follow_exec $binfile $srcfile
331 # Test the ability to follow both child and parent of a vfork. Do
332 # this without catchpoints.
333 # ??rehrauer: NYI. Will add testpoints here when implemented.
336 # Test the ability to have the debugger ask the user at vfork-time
337 # whether to follow the parent, child or both. Do this without
339 # ??rehrauer: NYI. Will add testpoints here when implemented.
342 # Step over a vfork in the child, do "info inferiors" and check the
343 # parent/child relation is displayed. Run the child over the exec,
344 # and confirm the relation is no longer displayed in "info
347 vfork_relations_in_info_inferiors "exec" $binfile $srcfile $non_stop
350 proc do_vfork_and_follow_child_tests_exit { binfile srcfile non_stop } {
351 # Try following the child process by just continuing through the
352 # vfork, and letting the child exit.
354 vfork_child_follow_to_exit $binfile $srcfile
356 # Try catching a vfork, and stepping out to the child.
358 tcatch_vfork_then_child_follow_exit $binfile $srcfile
360 # Step over a vfork in the child, do "info inferiors" and check the
361 # parent/child relation is displayed. Run the child to completion,
362 # and confirm the relation is no longer displayed in "info
365 vfork_relations_in_info_inferiors "exit" $binfile $srcfile $non_stop
368 with_test_prefix "check vfork support" {
369 # Check that vfork catchpoints are supported, as an indicator for
370 # whether vfork-following is supported.
371 check_vfork_catchpoints
374 # Follow parent and follow child vfork tests with a child that execs.
375 proc_with_prefix exec_tests { binfile srcfile non_stop } {
376 # These are tests of gdb's ability to follow the parent of a Unix
377 # vfork system call. The child will subsequently call a variant
378 # of the Unix exec system call.
379 do_vfork_and_follow_parent_tests $binfile $srcfile
381 # These are tests of gdb's ability to follow the child of a Unix
382 # vfork system call. The child will subsequently call a variant
383 # of a Unix exec system call.
385 do_vfork_and_follow_child_tests_exec $binfile $srcfile $non_stop
388 # Follow parent and follow child vfork tests with a child that exits.
389 proc_with_prefix exit_tests { binfile srcfile non_stop } {
390 # These are tests of gdb's ability to follow the parent of a Unix
391 # vfork system call. The child will subsequently exit.
392 do_vfork_and_follow_parent_tests $binfile $srcfile
394 # These are tests of gdb's ability to follow the child of a Unix
395 # vfork system call. The child will subsequently exit.
397 do_vfork_and_follow_child_tests_exit $binfile $srcfile $non_stop
400 # Using the remote protocol with schedule-multiple turned triggers bug
401 # gdb/30574, so avoid this for now.
402 if {[target_info exists gdb_protocol]
403 && ([target_info gdb_protocol] == "remote"
404 || [target_info gdb_protocol] == "extended-remote")} {
407 set sm_modes { off on }
410 # A few of these tests require a little more time than the standard timeout
412 with_timeout_factor 2 {
413 foreach_with_prefix target-non-stop {auto on off} {
414 foreach_with_prefix non-stop {off on} {
415 foreach_with_prefix schedule-multiple $sm_modes {
416 save_vars { ::GDBFLAGS } {
417 # These flags will be picked up by the call to
418 # clean_restart inside setup_gdb.
419 append ::GDBFLAGS " -ex \"maintenance set target-non-stop ${target-non-stop}\""
420 append ::GDBFLAGS " -ex \"set non-stop ${non-stop}\""
421 append ::GDBFLAGS " -ex \"set schedule-multiple ${schedule-multiple}\""
423 exec_tests $binfile $srcfile ${non-stop}
424 exit_tests $binfile2 $srcfile2 ${non-stop}