summaryrefslogtreecommitdiff
path: root/gdb/testsuite/gdb.hp/foll-fork.exp
blob: 7231764f3b4b6967a2c3291eba1492cc6fe51b0a (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
#   Copyright (C) 1997 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */

# Please email any bugs, comments, and/or additions to this file to:
# bug-gdb@prep.ai.mit.edu

if $tracelevel then {
	strace $tracelevel
	}

set prms_id 0
set bug_id 0

# are we on a target board
if ![isnative] then {
    return
}

if {![istarget "hppa*-*-hpux10.30"] && ![istarget "hppa*-*-hpux11.*"]} {
    #setup_xfail "*-*.*"
    return 0
}

set testfile "foll-fork"
set srcfile ${testfile}.c
set binfile ${objdir}/${subdir}/${testfile}

# build the first test case
#if  { [compile "-g ${srcdir}/${subdir}/${srcfile} -o ${binfile} "] != "" } {
#    execute_anywhere "rm -f ${objdir}/${subdir}/${testfile}.tmp"
    # built the second test case since we can't use prototypes
#    warning "Prototypes not supported, rebuilding with -DNO_PROTOTYPES"
#    execute_anywhere "echo set prototypes 0 > ${objdir}/${subdir}/${testfile}.tmp"
#    if  { [compile "-g -DNO_PROTOTYPES ${srcdir}/${subdir}/${srcfile} -o ${binfile} "] != "" } {
#	perror "Couldn't compile ${testfile}.c"
#	return -1
#    }
#}

if  { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
     gdb_suppress_entire_file "Testcase compile failed, so all tests in this file will automatically fail."
}



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

proc default_fork_parent_follow {} {
   global gdb_prompt

   send_gdb "show follow\n"
   gdb_expect {
      -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
                      {pass "default show parent follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "default show parent follow, no catchpoints"}
      timeout         {fail "(timeout) default show parent follow, no catchpoints"}
   }
   send_gdb "next 2\n"
   gdb_expect {
      -re "Detaching after fork from.*$gdb_prompt $"\
                      {pass "default parent follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "default parent follow, no catchpoints"}
      timeout         {fail "(timeout) default parent follow, no catchpoints" }
   }
   # 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 explicit_fork_parent_follow {} {
   global gdb_prompt

   send_gdb "set follow parent\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow parent"}
      timeout         {fail "(timeout) set follow parent"}
   }
   send_gdb "show follow\n"
   gdb_expect {
      -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
                      {pass "explicit show parent follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "explicit show parent follow, no catchpoints"}
      timeout         {fail "(timeout) explicit show parent follow, no catchpoints"}
   }
   send_gdb "next 2\n"
   gdb_expect {
      -re "Detaching after fork from.*$gdb_prompt $"\
                      {pass "explicit parent follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "explicit parent follow, no catchpoints"}
      timeout         {fail "(timeout) explicit parent follow, no catchpoints"}
   }
   # 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 explicit_fork_child_follow {} {
   global gdb_prompt

   send_gdb "set follow child\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow child"}
      timeout         {fail "(timeout) set follow child"}
   }
   send_gdb "show follow\n"
   gdb_expect {
      -re "Debugger response to a program call of fork or vfork is \"child\"..*$gdb_prompt $"\
                      {pass "explicit show child follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "explicit show child follow, no catchpoints"}
      timeout         {fail "(timeout) explicit show child follow, no catchpoints"}
   }
   send_gdb "next 2\n"
   gdb_expect {
      -re "Detaching from program:.*Attaching after fork to.*$gdb_prompt $"\
                      {pass "explicit child follow, no catchpoints"}
      -re "$gdb_prompt $" {fail "explicit child follow, no catchpoints"}
      timeout         {fail "(timeout) explicit child follow, no catchpoints"}
   }
   # 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 catch_fork_child_follow {} {
   global gdb_prompt

   send_gdb "catch fork\n"
   gdb_expect {
      -re "Catchpoint .*(fork).*$gdb_prompt $"\
                      {pass "explicit child follow, set catch fork"}
      -re "$gdb_prompt $" {fail "explicit child follow, set catch fork"}
      timeout         {fail "(timeout) explicit child follow, set catch fork"}
   }

   # Verify that the catchpoint is mentioned in an "info breakpoints",
   # and further that the catchpoint mentions no process id.
   #
   send_gdb "info breakpoints\n"
   gdb_expect {
     -re ".*catch fork.*keep y.*$gdb_prompt $"\
                     {pass "info shows catchpoint without pid"}
     -re ".*catch fork.*process .*$gdb_prompt $"\
                     {fail "info shows catchpoint without pid"}
     -re "$gdb_prompt $" {fail "info shows catchpoint without pid"}
     timeout         {fail "(timeout) info shows catchpoint without pid"}
   }

   send_gdb "continue\n"
   gdb_expect {
      -re "Catchpoint.*(forked process.*),.*in _fork_sys.*$gdb_prompt $"\
                      {pass "explicit child follow, catch fork"}
      -re "$gdb_prompt $" {fail "explicit child follow, catch fork"}
      timeout         {fail "(timeout) explicit child follow, catch fork"}
   }

   # Verify that the catchpoint is mentioned in an "info breakpoints",
   # and further that the catchpoint managed to capture a process id.
   #
   send_gdb "info breakpoints\n"
   gdb_expect {
     -re ".*catch fork .*process \[0-9\]+.*$gdb_prompt $"\
                     {pass "info shows catchpoint pid"}
     -re "$gdb_prompt $" {fail "info shows catchpoint pid"}
     timeout         {fail "(timeout) info shows catchpoint pid"}
   }

   send_gdb "set follow child\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow child"}
      timeout         {fail "(timeout) set follow child"}
   }
   send_gdb "tbreak 15\n"
   gdb_expect {
      -re "Breakpoint.*, line 15.*$gdb_prompt $"\
                      {pass "set follow child, tbreak"}
      -re "$gdb_prompt $" {fail "set follow child, tbreak"}
      timeout         {fail "(timeout) set follow child, tbreak"}
   }
   send_gdb "continue\n"
   gdb_expect {
      -re ".*Detaching from program:.*Attaching after fork to.* at .*15.*$gdb_prompt $"\
                      {pass "set follow child, hit tbreak"}
      -re "$gdb_prompt $" {fail "set follow child, hit tbreak"}
      timeout         {fail "(timeout) set follow child, hit tbreak"}
   }
   # 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
   send_gdb "delete breakpoints\n"
   gdb_expect {
      -re "Delete all breakpoints.*$" {
         send_gdb "y\n"
         gdb_expect {
            -re "$gdb_prompt $"\
                    {pass "set follow child, cleanup"}
            timeout {fail "(timeout) set follow child, cleanup"}
         }
      }
      -re "$gdb_prompt $" {fail "set follow child, cleanup"}
      timeout         {fail "(timeout) set follow child, cleanup"}
   }
}

proc tcatch_fork_parent_follow {} {
   global gdb_prompt

   send_gdb "catch fork\n"
   gdb_expect {
      -re "Catchpoint .*(fork).*$gdb_prompt $"\
                      {pass "explicit parent follow, set tcatch fork"}
      -re "$gdb_prompt $" {fail "explicit parent follow, set tcatch fork"}
      timeout         {fail "(timeout) explicit parent follow, set tcatch fork"}
   }
# ??rehrauer: I don't yet know how to get the id of the tcatch
# via this script, so that I can add a -do list to it.  For now,
# do the follow stuff after the catch happens.

   send_gdb "continue\n"
   gdb_expect {
      -re ".*in _fork_sys.*$gdb_prompt $"\
                      {pass "explicit parent follow, tcatch fork"}
      -re "$gdb_prompt $" {fail "explicit parent follow, tcatch fork"}
      timeout         {fail "(timeout) explicit parent follow, tcatch fork"}
   }
   send_gdb "set follow parent\n"
   gdb_expect {
      -re "$gdb_prompt $" {pass "set follow parent"}
      timeout         {fail "(timeout) set follow parent"}
   }
   send_gdb "tbreak 15\n"
   gdb_expect {
      -re "Breakpoint.*, line 15.*$gdb_prompt $"\
                      {pass "set follow parent, tbreak"}
      -re "$gdb_prompt $" {fail "set follow parent, tbreak"}
      timeout         {fail "(timeout) set follow child, tbreak"}
   }
   send_gdb "continue\n"
   gdb_expect {
      -re ".*Detaching after fork from.* at .*15.*$gdb_prompt $"\
                      {pass "set follow parent, hit tbreak"}
      -re "$gdb_prompt $" {fail "set follow parent, hit tbreak"}
      timeout         {fail "(timeout) set follow parent, hit tbreak"}
   }
   # 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
   send_gdb "delete breakpoints\n"
   gdb_expect {
      -re "Delete all breakpoints.*$" {
         send_gdb "y\n"
         gdb_expect {
            -re "$gdb_prompt $"\
                    {pass "set follow parent, cleanup"}
            timeout {fail "(timeout) set follow parent, cleanup"}
         }
      }
      -re "$gdb_prompt $" {fail "set follow parent, cleanup"}
      timeout         {fail "(timeout) set follow parent, cleanup"}
   }
}

proc do_fork_tests {} {
   global gdb_prompt

   # Verify that help is available for "set follow-fork-mode".
   #
   send_gdb "help set follow-fork-mode\n"
   gdb_expect {
      -re "Set debugger response to a program call of fork or vfork..*
A fork or vfork creates a new process.  follow-fork-mode can be:.*
.*parent  - the original process is debugged after a fork.*
.*child   - the new process is debugged after a fork.*
.*ask     - the debugger will ask for one of the above choices.*
For \"parent\" or \"child\", the unfollowed process will run free..*
By default, the debugger will follow the parent process..*$gdb_prompt $"\
                      { pass "help set follow" }
      -re "$gdb_prompt $" { fail "help set follow" }
      timeout         { fail "(timeout) help set follow" }
   }

   # Verify that we can set follow-fork-mode, using an abbreviation
   # for both the flag and its value.
   #
   send_gdb "set follow ch\n"
   send_gdb "show fol\n"
   gdb_expect {
     -re "Debugger response to a program call of fork or vfork is \"child\".*$gdb_prompt $"\
             {pass "set follow, using abbreviations"}
     timeout {fail "(timeout) set follow, using abbreviations"}
   }

   # Verify that we cannot set follow-fork-mode to nonsense.
   #
   send_gdb "set follow chork\n"
   gdb_expect {
     -re "Undefined item: \"chork\".*$gdb_prompt $"\
                     {pass "set follow to nonsense is prohibited"}
     -re "$gdb_prompt $" {fail "set follow to nonsense is prohibited"}
     timeout         {fail "(timeout) set follow to nonsense is prohibited"}
   }
   send_gdb "set follow parent\n"
   gdb_expect {
     -re "$gdb_prompt $" {pass "set follow to nonsense is prohibited (reset parent)"}
     timeout         {fail "set follow to nonsense is prohibited (reset parent)"}
   }

   # Test the default behaviour, which is to follow the parent of a
   # fork, and detach from the child.  Do this without catchpoints.
   #
   if [runto_main] then { default_fork_parent_follow }

   # Test the ability to explicitly follow the parent of a fork, and
   # detach from the child.  Do this without catchpoints.
   #
   if [runto_main] then { explicit_fork_parent_follow }

   # Test the ability to follow the child of a fork, and detach from
   # the parent.  Do this without catchpoints.
   #
   if [runto_main] then { explicit_fork_child_follow }

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

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

   # Test the ability to catch a fork, specify that the child be
   # followed, and continue.  Make the catchpoint permanent.
   #
   if [runto_main] then { catch_fork_child_follow }

   # Test the ability to catch a fork, specify via a -do clause that
   # the parent be followed, and continue.  Make the catchpoint temporary.
   #
   if [runto_main] then { tcatch_fork_parent_follow }
}

# Start with a fresh gdb

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


# This is a test of gdb's ability to follow the parent, child or both
# parent and child of a Unix fork() system call.
#
do_fork_tests

return 0