summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.base/foll-vfork.exp
blob: e55d7005635e2073a8da0e7b8f7c24f48ae5e327 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
#   Copyright 1997, 1999, 2007, 2008, 2009, 2010
#   Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

if { [is_remote target] || ![isnative] } then {
    continue
}


# NOTE drow/2002-12-06: I don't know what the referenced kernel problem
# is, but it appears to be fixed in recent HP/UX versions.

##if [istarget "hppa2.0w-hp-hpux*"] {
##  warning "Don't run gdb.base/foll-vfork.exp until JAGaa43495 kernel problem is fixed."
##  return 0
##}

global srcfile
set testfile "foll-vfork"
set testfile2 "vforked-prog"
set srcfile ${testfile}.c
set srcfile2 ${testfile2}.c
set binfile ${objdir}/${subdir}/${testfile}
set binfile2 ${objdir}/${subdir}/${testfile2}

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
     untested foll-vfork.exp
     return -1
}

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile2}" "${binfile2}" executable {debug}] != "" } {
     untested foll-vfork.exp
     return -1
}


# Until "set follow-fork-mode" and "catch vfork" are implemented on
# other targets...
#
if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
    continue
}

# Test to see if we are on an HP-UX 10.20 and if so,
# do not run these tests as catching vfork is disabled for
# 10.20.

if [istarget "hppa*-hp-hpux10.20"] then {
    return 0
}

# A few of these tests require a little more time than the standard
# timeout allows.
set oldtimeout $timeout
set timeout [expr "$timeout + 10"]

proc check_vfork_catchpoints {} {
  global gdb_prompt
  global has_vfork_catchpoints

  # Verify that the system supports "catch vfork".
  gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint"
  set has_vfork_catchpoints 0
  gdb_test_multiple "continue" "continue to first vfork catchpoint" {
    -re ".*Your system does not support vfork catchpoints.*$gdb_prompt $" {
      unsupported "continue to first vfork catchpoint"
    }
    -re ".*Catchpoint.*$gdb_prompt $" {
      set has_vfork_catchpoints 1
      pass "continue to first vfork catchpoint"
    }
  }

  if {$has_vfork_catchpoints == 0} {
    unsupported "vfork catchpoints"
    return -code return
  }
}

proc vfork_parent_follow_through_step {} {
   global gdb_prompt

   send_gdb "set follow-fork parent\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork parent, vfork through step"}
      timeout         {fail "set follow-fork parent, vfork through step"}
   }
   send_gdb "next\n"
   gdb_expect {
      -re "Detaching after fork from.*13.*$gdb_prompt "\
                      {pass "vfork parent follow, through step"}
      -re "$gdb_prompt $" {fail "vfork parent follow, through step"}
      timeout         {fail "(timeout) vfork parent follow, through step" }
   }
   # The child has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any gdb_expected debugger output from a subsequent testpoint.
   #
   exec sleep 1
}

proc vfork_parent_follow_to_bp {} {
   global gdb_prompt
   global srcfile

   send_gdb "set follow-fork parent\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork parent, vfork to bp"}
      timeout         {fail "set follow-fork parent, vfork to bp"}
   }
   send_gdb "break ${srcfile}:18\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "break, vfork to bp"}
      timeout         {fail "break, vfork to bp"}
   }
   send_gdb "continue\n"
   gdb_expect {
      -re ".*Detaching after fork from child process.*Breakpoint.*18.*$gdb_prompt "\
                      {pass "vfork parent follow, to bp"}
      -re "$gdb_prompt $" {fail "vfork parent follow, to bp"}
      timeout         {fail "(timeout) vfork parent follow, to bp" }
   }
   # The child has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any expected debugger output from a subsequent testpoint.
   #
   exec sleep 1
}

proc vfork_and_exec_child_follow_to_main_bp {} {
   global gdb_prompt
   global binfile

   send_gdb "set follow-fork child\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork child, vfork and exec to main bp"}
      timeout         {fail "set follow-fork child, vfork and exec to main bp"}
   }
   send_gdb "continue\n"
   gdb_expect {
      -re "Attaching after.* vfork to.*xecuting new program.*Breakpoint.*vforked-prog.c:9.*$gdb_prompt "\
                      {pass "vfork and exec child follow, to main bp"}
      -re "$gdb_prompt $" {fail "vfork and exec child follow, to main bp"}
      timeout         {fail "(timeout) vfork and exec child follow, to main bp" }
   }
   # The parent has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any gdb_expected debugger output from a subsequent testpoint.
   #
   exec sleep 1

   # Explicitly kill this child, or a subsequent rerun actually runs
   # the exec'd child, not the original program...
   send_gdb "kill\n"
   gdb_expect {
      -re ".*Kill the program being debugged.*y or n. $" {
         send_gdb "y\n"
         send_gdb "file $binfile\n"
         gdb_expect {
            -re ".*Load new symbol table from.*y or n. $" {
               send_gdb "y\n"
               gdb_expect {
                  -re "Reading symbols from.*$gdb_prompt $" {}
                  timeout { fail "loading symbols (timeout)"; return }
               }
            }
            -re ".*gdb_prompt $" {}
            timeout { fail "loading symbols (timeout)"; return }
          }
      }
      -re ".*$gdb_prompt $" {}
      timeout { fail "killing inferior (timeout)" ; return }
    }
}

proc vfork_and_exec_child_follow_through_step {} {
   global gdb_prompt
   global binfile

# This test cannot be performed prior to HP-UX 10.30, because ptrace-based
# debugging of a vforking program basically doesn't allow the child to do
# things like hit a breakpoint between a vfork and exec.  This means that
# saying "set follow-fork child; next" at a vfork() call won't work, because
# the implementation of "next" sets a "step resume" breakpoint at the
# return from the vfork(), which the child will hit on its way to exec'ing.
#
   if { ![istarget "hppa*-*-hpux11.*"] } {
      verbose "vfork child-following next test ignored for non-hppa or pre-HP/UX-10.30 targets."
      return 0
   }

   send_gdb "set follow-fork child\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork child, vfork and exec through step"}
      timeout         {fail "set follow-fork child, vfork and exec through step"}
   }
   send_gdb "next\n"
   gdb_expect {
      -re "Attaching after fork to.*Executing new program.*Breakpoint.*vforked-prog.c:9.*$gdb_prompt "\
                      {pass "vfork and exec child follow, through step"}
      -re "$gdb_prompt $" {fail "vfork and exec child follow, through step"}
      timeout         {fail "(timeout) vfork and exec child follow, through step" }
   }
   # The parent has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any expected debugger output from a subsequent testpoint.
   #
   exec sleep 1

   # Explicitly kill this child, or a subsequent rerun actually runs
   # the exec'd child, not the original program...
   send_gdb "kill\n"
   gdb_expect {
      -re ".*Kill the program being debugged.*y or n. $" {
         send_gdb "y\n"
         send_gdb "file $binfile\n"
         gdb_expect {
            -re ".*Load new symbol table from.*y or n. $" {
               send_gdb "y\n"
               gdb_expect {
                  -re "Reading symbols from.*$gdb_prompt $" {}
                  timeout { fail "loading symbols (timeout)"; return }
               }
            }
            -re ".*gdb_prompt $" {}
            timeout { fail "loading symbols (timeout)"; return }
          }
      }
      -re ".*$gdb_prompt $" {}
      timeout { fail "killing inferior (timeout)" ; return }
    }
}

proc tcatch_vfork_then_parent_follow {} {
   global gdb_prompt
   global srcfile

   send_gdb "set follow-fork parent\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork parent, tcatch vfork"}
      timeout         {fail "set follow-fork parent, tcatch vfork"}
   }
   send_gdb "tcatch vfork\n"
   gdb_expect {
      -re "Catchpoint .*(vfork).*$gdb_prompt $"\
                      {pass "vfork parent follow, set tcatch vfork"}
      -re "$gdb_prompt $" {fail "vfork parent follow, set tcatch vfork"}
      timeout         {fail "(timeout) vfork parent follow, set tcatch vfork"}
   }
   send_gdb "continue\n"
# HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
# stop you in "_vfork".
   gdb_expect {
      -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt "\
                      {pass "vfork parent follow, tcatch vfork"}
      -re "vfork \\(\\) at.*$gdb_prompt "\
                      {pass "vfork parent follow, tcatch vfork"}
      -re "$gdb_prompt $" {fail "vfork parent follow, tcatch vfork"}
      timeout         {fail "(timeout) vfork parent follow, tcatch vfork"}
   }
   send_gdb "finish\n"
   gdb_expect {
      -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:12.*$gdb_prompt "\
                      {pass "vfork parent follow, finish after tcatch vfork"}
      -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
	  send_gdb "finish\n"
	  exp_continue
      }
      -re "$gdb_prompt $" {fail "vfork parent follow, finish after tcatch vfork"}
      timeout         {fail "(timeout) vfork parent follow, finish after tcatch vfork" }
   }
   # The child has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any expected debugger output from a subsequent testpoint.
   #
   exec sleep 1
}

proc tcatch_vfork_then_child_follow {} {
   global gdb_prompt
   global srcfile
   global srcfile2

   send_gdb "set follow-fork child\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow-fork child, tcatch vfork"}
      timeout         {fail "set follow-fork child, tcatch vfork"}
   }
   send_gdb "tcatch vfork\n"
   gdb_expect {
      -re "Catchpoint .*(vfork).*$gdb_prompt $"\
                      {pass "vfork child follow, set tcatch vfork"}
      -re "$gdb_prompt $" {fail "vfork child follow, set tcatch vfork"}
      timeout         {fail "(timeout) vfork child follow, set tcatch vfork"}
   }
   send_gdb "continue\n"
# HP-UX 10.20 seems to stop you in "vfork", while more recent HP-UXs
# stop you in "_vfork".
   gdb_expect {
      -re "vfork \\(\\) at .*$gdb_prompt $"\
                      {pass "vfork child follow, tcatch vfork"}
      -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt "\
                      {pass "vfork child follow, tcatch vfork"}
      -re "$gdb_prompt $" {fail "vfork child follow, tcatch vfork"}
      timeout         {fail "(timeout) vfork child follow, tcatch vfork"}
   }
   send_gdb "finish\n"
   gdb_expect {
      -re "Run till exit from.*vfork.*${srcfile}:12.*$gdb_prompt "\
                      {pass "vfork child follow, finish after tcatch vfork"}
      -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " {
	  send_gdb "finish\n"
	  exp_continue
      }
      -re "Run till exit from.*vfork.*${srcfile2}:9.*$gdb_prompt "\
                      {pass "vfork child follow, finish after tcatch vfork (followed exec)"}
      -re "$gdb_prompt $" {fail "vfork child follow, finish after tcatch vfork"}
      timeout         {fail "(timeout) vfork child follow, finish after tcatch vfork" }
   }
   # The parent has been detached; allow time for any output it might
   # generate to arrive, so that output doesn't get confused with
   # any expected debugger output from a subsequent testpoint.
   #
   exec sleep 1
}

proc do_vfork_and_exec_tests {} {
   global gdb_prompt

   # Check that vfork catchpoints are supported, as an indicator for whether
   # vfork-following is supported.
   if [runto_main] then { check_vfork_catchpoints }

   # Try following the parent process by stepping through a call to
   # vfork.  Do this without catchpoints.
   if [runto_main] then { vfork_parent_follow_through_step }

   # Try following the parent process by setting a breakpoint on the
   # other side of a vfork, and running to that point.  Do this
   # without catchpoints.
   if [runto_main] then { vfork_parent_follow_to_bp }

   # Try following the child process by just continuing through the
   # vfork, and letting the parent's breakpoint on "main" be auto-
   # magically reset in the child.
   #
   if [runto_main] then { vfork_and_exec_child_follow_to_main_bp }

   # Try following the child process by stepping through a call to
   # vfork.  The child also executes an exec.  Since the child cannot
   # be debugged until after it has exec'd, and since there's a bp on
   # "main" in the parent, and since the bp's for the parent are
   # recomputed in the exec'd child, the step through a vfork should
   # land us in the "main" for the exec'd child, too.
   #
   if [runto_main] then { vfork_and_exec_child_follow_through_step }

   # Try catching a vfork, and stepping out to the parent.
   #
   if [runto_main] then { tcatch_vfork_then_parent_follow }

   # Try catching a vfork, and stepping out to the child.
   #
   if [runto_main] then { tcatch_vfork_then_child_follow }

   # Test the ability to follow both child and parent of a vfork.  Do
   # this without catchpoints.
   # ??rehrauer: NYI.  Will add testpoints here when implemented.
   #

   # Test the ability to have the debugger ask the user at vfork-time
   # whether to follow the parent, child or both.  Do this without
   # catchpoints.
   # ??rehrauer: NYI.  Will add testpoints here when implemented.
   #
}

# Start with a fresh gdb

gdb_exit
gdb_start
gdb_reinitialize_dir $srcdir/$subdir
gdb_load ${binfile}

# The "Detaching..." and "Attaching..." messages may be hidden by
# default.
gdb_test_no_output "set verbose"

# This is a test of gdb's ability to follow the parent or child
# of a Unix vfork() system call.  (The child will subsequently
# call a variant of a Unix exec() system call.)
#
do_vfork_and_exec_tests

set timeout $oldtimeout
return 0