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