1#   Copyright 1997, 1999, 2007, 2008, 2009, 2010, 2011
2#   Free Software Foundation, Inc.
3
4# This program is free software; you can redistribute it and/or modify
5# it under the terms of the GNU General Public License as published by
6# the Free Software Foundation; either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12# GNU General Public License for more details.
13#
14# You should have received a copy of the GNU General Public License
15# along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17if { [is_remote target] || ![isnative] } then {
18    continue
19}
20
21
22# NOTE drow/2002-12-06: I don't know what the referenced kernel problem
23# is, but it appears to be fixed in recent HP/UX versions.
24
25##if [istarget "hppa2.0w-hp-hpux*"] {
26##  warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
27##  return 0
28##}
29
30global srcfile
31set testfile "foll-vfork"
32set testfile2 "vforked-prog"
33set srcfile ${testfile}.c
34set srcfile2 ${testfile2}.c
35set binfile ${objdir}/${subdir}/${testfile}
36set binfile2 ${objdir}/${subdir}/${testfile2}
37
38if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
39     untested foll-vfork.exp
40     return -1
41}
42
43if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
44     untested foll-vfork.exp
45     return -1
46}
47
48
49# Until "set follow-fork-mode" and "catch vfork" are implemented on
50# other targets...
51#
52if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
53    continue
54}
55
56# Test to see if we are on an HP-UX 10.20 and if so,
57# do not run these tests as catching vfork is disabled for
58# 10.20.
59
60if [istarget "hppa*-hp-hpux10.20"] then {
61    return 0
62}
63
64# A few of these tests require a little more time than the standard
65# timeout allows.
66set oldtimeout $timeout
67set timeout [expr "$timeout + 10"]
68
69proc check_vfork_catchpoints {} {
70  global gdb_prompt
71  global has_vfork_catchpoints
72
73  # Verify that the system supports "catch vfork".
74  gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
75  set has_vfork_catchpoints 0
76  gdb_test_multiple "continue" "continue to first vfork catchpoint" {
77    -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
78      unsupported "continue to first vfork catchpoint"
79    }
80    -re ".*Catchpoint.*$gdb_prompt $" {
81      set has_vfork_catchpoints 1
82      pass "continue to first vfork catchpoint"
83    }
84  }
85
86  if {$has_vfork_catchpoints == 0} {
87    unsupported "vfork catchpoints"
88    return -code return
89  }
90}
91
92proc vfork_parent_follow_through_step {} {
93   global gdb_prompt
94
95   send_gdb "set follow-fork parent\n"
96   gdb_expect {
97      -re "$gdb_prompt $" {pass "set follow-fork parent, vfork through step"}
98      timeout         {fail "set follow-fork parent, vfork through step"}
99   }
100   send_gdb "next\n"
101   gdb_expect {
102      -re "Detaching after fork from.*13.*$gdb_prompt "\
103                      {pass "vfork parent follow, through step"}
104      -re "$gdb_prompt $" {fail "vfork parent follow, through step"}
105      timeout         {fail "(timeout) vfork parent follow, through step" }
106   }
107   # The child has been detached; allow time for any output it might
108   # generate to arrive, so that output doesn't get confused with
109   # any gdb_expected debugger output from a subsequent testpoint.
110   #
111   exec sleep 1
112}
113
114proc vfork_parent_follow_to_bp {} {
115   global gdb_prompt
116   global srcfile
117
118   send_gdb "set follow-fork parent\n"
119   gdb_expect {
120      -re "$gdb_prompt $" {pass "set follow-fork parent, vfork to bp"}
121      timeout         {fail "set follow-fork parent, vfork to bp"}
122   }
123   send_gdb "break ${srcfile}:18\n"
124   gdb_expect {
125      -re "$gdb_prompt $" {pass "break, vfork to bp"}
126      timeout         {fail "break, vfork to bp"}
127   }
128   send_gdb "continue\n"
129   gdb_expect {
130      -re ".*Detaching after fork from child process.*Breakpoint.*18.*$gdb_prompt "\
131                      {pass "vfork parent follow, to bp"}
132      -re "$gdb_prompt $" {fail "vfork parent follow, to bp"}
133      timeout         {fail "(timeout) vfork parent follow, to bp" }
134   }
135   # The child has been detached; allow time for any output it might
136   # generate to arrive, so that output doesn't get confused with
137   # any expected debugger output from a subsequent testpoint.
138   #
139   exec sleep 1
140}
141
142proc vfork_and_exec_child_follow_to_main_bp {} {
143   global gdb_prompt
144   global binfile
145
146   send_gdb "set follow-fork child\n"
147   gdb_expect {
148      -re "$gdb_prompt $" {pass "set follow-fork child, vfork and exec to main bp"}
149      timeout         {fail "set follow-fork child, vfork and exec to main bp"}
150   }
151   send_gdb "continue\n"
152   gdb_expect {
153      -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:9.*$gdb_prompt "\
154                      {pass "vfork and exec child follow, to main bp"}
155      -re "$gdb_prompt $" {fail "vfork and exec child follow, to main bp"}
156      timeout         {fail "(timeout) vfork and exec child follow, to main bp" }
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   # Explicitly kill this child, or a subsequent rerun actually runs
165   # the exec'd child, not the original program...
166   send_gdb "kill\n"
167   gdb_expect {
168      -re ".*Kill the program being debugged.*y or n. $" {
169         send_gdb "y\n"
170         send_gdb "file $binfile\n"
171         gdb_expect {
172            -re ".*Load new symbol table from.*y or n. $" {
173               send_gdb "y\n"
174               gdb_expect {
175                  -re "Reading symbols from.*$gdb_prompt $" {}
176                  timeout { fail "loading symbols (timeout)"; return }
177               }
178            }
179            -re ".*gdb_prompt $" {}
180            timeout { fail "loading symbols (timeout)"; return }
181          }
182      }
183      -re ".*$gdb_prompt $" {}
184      timeout { fail "killing inferior (timeout)" ; return }
185    }
186}
187
188proc vfork_and_exec_child_follow_through_step {} {
189   global gdb_prompt
190   global binfile
191
192# This test cannot be performed prior to HP-UX 10.30, because ptrace-based
193# debugging of a vforking program basically doesn't allow the child to do
194# things like hit a breakpoint between a vfork and exec.  This means that
195# saying "set follow-fork child; next" at a vfork() call won't work, because
196# the implementation of "next" sets a "step resume" breakpoint at the
197# return from the vfork(), which the child will hit on its way to exec'ing.
198#
199   if { ![istarget "hppa*-*-hpux11.*"] } {
200      verbose "vfork child-following next test ignored for non-hppa or pre-HP/UX-10.30 targets."
201      return 0
202   }
203
204   send_gdb "set follow-fork child\n"
205   gdb_expect {
206      -re "$gdb_prompt $" {pass "set follow-fork child, vfork and exec through step"}
207      timeout         {fail "set follow-fork child, vfork and exec through step"}
208   }
209   send_gdb "next\n"
210   gdb_expect {
211      -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:9.*$gdb_prompt "\
212                      {pass "vfork and exec child follow, through step"}
213      -re "$gdb_prompt $" {fail "vfork and exec child follow, through step"}
214      timeout         {fail "(timeout) vfork and exec child follow, through step" }
215   }
216   # The parent has been detached; allow time for any output it might
217   # generate to arrive, so that output doesn't get confused with
218   # any expected debugger output from a subsequent testpoint.
219   #
220   exec sleep 1
221
222   # Explicitly kill this child, or a subsequent rerun actually runs
223   # the exec'd child, not the original program...
224   send_gdb "kill\n"
225   gdb_expect {
226      -re ".*Kill the program being debugged.*y or n. $" {
227         send_gdb "y\n"
228         send_gdb "file $binfile\n"
229         gdb_expect {
230            -re ".*Load new symbol table from.*y or n. $" {
231               send_gdb "y\n"
232               gdb_expect {
233                  -re "Reading symbols from.*$gdb_prompt $" {}
234                  timeout { fail "loading symbols (timeout)"; return }
235               }
236            }
237            -re ".*gdb_prompt $" {}
238            timeout { fail "loading symbols (timeout)"; return }
239          }
240      }
241      -re ".*$gdb_prompt $" {}
242      timeout { fail "killing inferior (timeout)" ; return }
243    }
244}
245
246proc tcatch_vfork_then_parent_follow {} {
247   global gdb_prompt
248   global srcfile
249
250   send_gdb "set follow-fork parent\n"
251   gdb_expect {
252      -re "$gdb_prompt $" {pass "set follow-fork parent, tcatch vfork"}
253      timeout         {fail "set follow-fork parent, tcatch vfork"}
254   }
255   send_gdb "tcatch vfork\n"
256   gdb_expect {
257      -re "Catchpoint .*(vfork).*$gdb_prompt $"\
258                      {pass "vfork parent follow, set tcatch vfork"}
259      -re "$gdb_prompt $" {fail "vfork parent follow, set tcatch vfork"}
260      timeout         {fail "(timeout) vfork parent follow, set tcatch vfork"}
261   }
262   send_gdb "continue\n"
263# HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
264# stop you in "_vfork".
265   gdb_expect {
266      -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt "\
267                      {pass "vfork parent follow, tcatch vfork"}
268      -re "vfork \\(\\) at.*$gdb_prompt "\
269                      {pass "vfork parent follow, tcatch vfork"}
270      -re "$gdb_prompt $" {fail "vfork parent follow, tcatch vfork"}
271      timeout         {fail "(timeout) vfork parent follow, tcatch vfork"}
272   }
273   send_gdb "finish\n"
274   gdb_expect {
275      -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:12.*$gdb_prompt "\
276                      {pass "vfork parent follow, finish after tcatch vfork"}
277      -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
278	  send_gdb "finish\n"
279	  exp_continue
280      }
281      -re "$gdb_prompt $" {fail "vfork parent follow, finish after tcatch vfork"}
282      timeout         {fail "(timeout) vfork parent follow, finish after tcatch vfork" }
283   }
284   # The child has been detached; allow time for any output it might
285   # generate to arrive, so that output doesn't get confused with
286   # any expected debugger output from a subsequent testpoint.
287   #
288   exec sleep 1
289}
290
291proc tcatch_vfork_then_child_follow {} {
292   global gdb_prompt
293   global srcfile
294   global srcfile2
295
296   send_gdb "set follow-fork child\n"
297   gdb_expect {
298      -re "$gdb_prompt $" {pass "set follow-fork child, tcatch vfork"}
299      timeout         {fail "set follow-fork child, tcatch vfork"}
300   }
301   send_gdb "tcatch vfork\n"
302   gdb_expect {
303      -re "Catchpoint .*(vfork).*$gdb_prompt $"\
304                      {pass "vfork child follow, set tcatch vfork"}
305      -re "$gdb_prompt $" {fail "vfork child follow, set tcatch vfork"}
306      timeout         {fail "(timeout) vfork child follow, set tcatch vfork"}
307   }
308   send_gdb "continue\n"
309# HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
310# stop you in "_vfork".
311   gdb_expect {
312      -re "vfork \\(\\) at .*$gdb_prompt $"\
313                      {pass "vfork child follow, tcatch vfork"}
314      -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt "\
315                      {pass "vfork child follow, tcatch vfork"}
316      -re "$gdb_prompt $" {fail "vfork child follow, tcatch vfork"}
317      timeout         {fail "(timeout) vfork child follow, tcatch vfork"}
318   }
319   send_gdb "finish\n"
320   gdb_expect {
321      -re "Run till exit from.*vfork.*${srcfile}:12.*$gdb_prompt "\
322                      {pass "vfork child follow, finish after tcatch vfork"}
323      -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
324	  send_gdb "finish\n"
325	  exp_continue
326      }
327      -re "Run till exit from.*vfork.*${srcfile2}:9.*$gdb_prompt "\
328                      {pass "vfork child follow, finish after tcatch vfork (followed exec)"}
329      -re "$gdb_prompt $" {fail "vfork child follow, finish after tcatch vfork"}
330      timeout         {fail "(timeout) vfork child follow, finish after tcatch vfork" }
331   }
332   # The parent has been detached; allow time for any output it might
333   # generate to arrive, so that output doesn't get confused with
334   # any expected debugger output from a subsequent testpoint.
335   #
336   exec sleep 1
337}
338
339proc do_vfork_and_exec_tests {} {
340   global gdb_prompt
341
342   # Check that vfork catchpoints are supported, as an indicator for whether
343   # vfork-following is supported.
344   if [runto_main] then { check_vfork_catchpoints }
345
346   # Try following the parent process by stepping through a call to
347   # vfork.  Do this without catchpoints.
348   if [runto_main] then { vfork_parent_follow_through_step }
349
350   # Try following the parent process by setting a breakpoint on the
351   # other side of a vfork, and running to that point.  Do this
352   # without catchpoints.
353   if [runto_main] then { vfork_parent_follow_to_bp }
354
355   # Try following the child process by just continuing through the
356   # vfork, and letting the parent's breakpoint on "main" be auto-
357   # magically reset in the child.
358   #
359   if [runto_main] then { vfork_and_exec_child_follow_to_main_bp }
360
361   # Try following the child process by stepping through a call to
362   # vfork.  The child also executes an exec.  Since the child cannot
363   # be debugged until after it has exec'd, and since there's a bp on
364   # "main" in the parent, and since the bp's for the parent are
365   # recomputed in the exec'd child, the step through a vfork should
366   # land us in the "main" for the exec'd child, too.
367   #
368   if [runto_main] then { vfork_and_exec_child_follow_through_step }
369
370   # Try catching a vfork, and stepping out to the parent.
371   #
372   if [runto_main] then { tcatch_vfork_then_parent_follow }
373
374   # Try catching a vfork, and stepping out to the child.
375   #
376   if [runto_main] then { tcatch_vfork_then_child_follow }
377
378   # Test the ability to follow both child and parent of a vfork.  Do
379   # this without catchpoints.
380   # ??rehrauer: NYI.  Will add testpoints here when implemented.
381   #
382
383   # Test the ability to have the debugger ask the user at vfork-time
384   # whether to follow the parent, child or both.  Do this without
385   # catchpoints.
386   # ??rehrauer: NYI.  Will add testpoints here when implemented.
387   #
388}
389
390# Start with a fresh gdb
391
392gdb_exit
393gdb_start
394gdb_reinitialize_dir $srcdir/$subdir
395gdb_load ${binfile}
396
397# The "Detaching..." and "Attaching..." messages may be hidden by
398# default.
399gdb_test_no_output "set verbose"
400
401# This is a test of gdb's ability to follow the parent or child
402# of a Unix vfork() system call.  (The child will subsequently
403# call a variant of a Unix exec() system call.)
404#
405do_vfork_and_exec_tests
406
407set timeout $oldtimeout
408return 0
409