2010-05-24 Michael Snyder <msnyder@vmware.com>
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
CommitLineData
4c38e0a4
JB
1# Copyright 1997, 1999, 2007, 2008, 2009, 2010
2# Free Software Foundation, Inc.
74cf1395
JM
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
e22f8b7c 6# the Free Software Foundation; either version 3 of the License, or
74cf1395 7# (at your option) any later version.
e22f8b7c 8#
74cf1395
JM
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.
e22f8b7c 13#
74cf1395 14# You should have received a copy of the GNU General Public License
e85a822c 15# along with this program. If not, see <http://www.gnu.org/licenses/>.
74cf1395 16
e85a822c 17if { [is_remote target] || ![isnative] } then {
5a2468f5
JM
18 continue
19}
20
74cf1395 21
e85a822c 22global srcfile
74cf1395
JM
23set testfile "foll-fork"
24set srcfile ${testfile}.c
25set binfile ${objdir}/${subdir}/${testfile}
26
27if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
b60f0898
JB
28 untested foll-fork.exp
29 return -1
74cf1395
JM
30}
31
32
33
34# Until "set follow-fork-mode" and "catch fork" are implemented on
35# other targets...
36#
e85a822c 37if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
74cf1395
JM
38 continue
39}
40
e85a822c
DJ
41proc check_fork_catchpoints {} {
42 global gdb_prompt
43
44 # Verify that the system supports "catch fork".
45 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
46 set has_fork_catchpoints 0
47 gdb_test_multiple "continue" "continue to first fork catchpoint" {
48 -re ".*Your system does not support fork catchpoints.*$gdb_prompt $" {
49 unsupported "continue to first fork catchpoint"
50 }
51 -re ".*Catchpoint.*$gdb_prompt $" {
52 set has_fork_catchpoints 1
53 pass "continue to first fork catchpoint"
54 }
55 }
56
57 if {$has_fork_catchpoints == 0} {
58 unsupported "fork catchpoints"
59 return -code return
60 }
61}
62
74cf1395
JM
63proc default_fork_parent_follow {} {
64 global gdb_prompt
65
6c95b8df 66 send_gdb "show follow-fork\n"
74cf1395
JM
67 gdb_expect {
68 -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
69 {pass "default show parent follow, no catchpoints"}
70 -re "$gdb_prompt $" {fail "default show parent follow, no catchpoints"}
71 timeout {fail "(timeout) default show parent follow, no catchpoints"}
72 }
73 send_gdb "next 2\n"
74 gdb_expect {
75 -re "Detaching after fork from.*$gdb_prompt $"\
76 {pass "default parent follow, no catchpoints"}
77 -re "$gdb_prompt $" {fail "default parent follow, no catchpoints"}
78 timeout {fail "(timeout) default parent follow, no catchpoints" }
79 }
80 # The child has been detached; allow time for any output it might
81 # generate to arrive, so that output doesn't get confused with
82 # any expected debugger output from a subsequent testpoint.
83 #
84 exec sleep 1
85}
86
87proc explicit_fork_parent_follow {} {
88 global gdb_prompt
89
6c95b8df 90 send_gdb "set follow-fork parent\n"
74cf1395 91 gdb_expect {
6c95b8df
PA
92 -re "$gdb_prompt $" {pass "set follow-fork parent"}
93 timeout {fail "(timeout) set follow-fork parent"}
74cf1395 94 }
6c95b8df 95 send_gdb "show follow-fork\n"
74cf1395
JM
96 gdb_expect {
97 -re "Debugger response to a program call of fork or vfork is \"parent\"..*$gdb_prompt $"\
98 {pass "explicit show parent follow, no catchpoints"}
99 -re "$gdb_prompt $" {fail "explicit show parent follow, no catchpoints"}
100 timeout {fail "(timeout) explicit show parent follow, no catchpoints"}
101 }
102 send_gdb "next 2\n"
103 gdb_expect {
104 -re "Detaching after fork from.*$gdb_prompt $"\
105 {pass "explicit parent follow, no catchpoints"}
106 -re "$gdb_prompt $" {fail "explicit parent follow, no catchpoints"}
107 timeout {fail "(timeout) explicit parent follow, no catchpoints"}
108 }
109 # The child has been detached; allow time for any output it might
110 # generate to arrive, so that output doesn't get confused with
111 # any expected debugger output from a subsequent testpoint.
112 #
113 exec sleep 1
114}
115
116proc explicit_fork_child_follow {} {
117 global gdb_prompt
118
6c95b8df 119 send_gdb "set follow-fork child\n"
74cf1395 120 gdb_expect {
6c95b8df
PA
121 -re "$gdb_prompt $" {pass "set follow-fork child"}
122 timeout {fail "(timeout) set follow-fork child"}
74cf1395 123 }
6c95b8df 124 send_gdb "show follow-fork\n"
74cf1395
JM
125 gdb_expect {
126 -re "Debugger response to a program call of fork or vfork is \"child\"..*$gdb_prompt $"\
127 {pass "explicit show child follow, no catchpoints"}
128 -re "$gdb_prompt $" {fail "explicit show child follow, no catchpoints"}
129 timeout {fail "(timeout) explicit show child follow, no catchpoints"}
130 }
131 send_gdb "next 2\n"
132 gdb_expect {
6c95b8df 133 -re "Attaching after.* fork to.*$gdb_prompt $"\
74cf1395
JM
134 {pass "explicit child follow, no catchpoints"}
135 -re "$gdb_prompt $" {fail "explicit child follow, no catchpoints"}
136 timeout {fail "(timeout) explicit child follow, no catchpoints"}
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 gdb_expected debugger output from a subsequent testpoint.
141 #
142 exec sleep 1
143}
144
145proc catch_fork_child_follow {} {
146 global gdb_prompt
e85a822c 147 global srcfile
74cf1395 148
b242c3c2
PA
149 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
150
74cf1395
JM
151 send_gdb "catch fork\n"
152 gdb_expect {
153 -re "Catchpoint .*(fork).*$gdb_prompt $"\
154 {pass "explicit child follow, set catch fork"}
155 -re "$gdb_prompt $" {fail "explicit child follow, set catch fork"}
156 timeout {fail "(timeout) explicit child follow, set catch fork"}
157 }
158
159 # Verify that the catchpoint is mentioned in an "info breakpoints",
160 # and further that the catchpoint mentions no process id.
161 #
cce89356
JB
162 set test_name "info shows catchpoint without pid"
163 gdb_test_multiple "info breakpoints" "$test_name" {
164 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
165 pass "$test_name"
166 }
74cf1395
JM
167 }
168
169 send_gdb "continue\n"
170 gdb_expect {
a9c64011 171 -re "Catchpoint.*(forked process.*),.*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
74cf1395
JM
172 {pass "explicit child follow, catch fork"}
173 -re "$gdb_prompt $" {fail "explicit child follow, catch fork"}
174 timeout {fail "(timeout) explicit child follow, catch fork"}
175 }
176
177 # Verify that the catchpoint is mentioned in an "info breakpoints",
178 # and further that the catchpoint managed to capture a process id.
179 #
cce89356
JB
180 set test_name "info shows catchpoint without pid"
181 gdb_test_multiple "info breakpoints" "$test_name" {
182 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
183 pass "$test_name"
184 }
74cf1395
JM
185 }
186
6c95b8df 187 send_gdb "set follow-fork child\n"
74cf1395 188 gdb_expect {
6c95b8df
PA
189 -re "$gdb_prompt $" {pass "set follow-fork child"}
190 timeout {fail "(timeout) set follow-fork child"}
74cf1395 191 }
b242c3c2 192 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
74cf1395 193 gdb_expect {
b242c3c2 194 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
195 {pass "set follow-fork child, tbreak"}
196 -re "$gdb_prompt $" {fail "set follow-fork child, tbreak"}
197 timeout {fail "(timeout) set follow-fork child, tbreak"}
74cf1395
JM
198 }
199 send_gdb "continue\n"
200 gdb_expect {
6c95b8df
PA
201 -re "Attaching after.* fork to.* at .*$bp_after_fork.*$gdb_prompt $"\
202 {pass "set follow-fork child, hit tbreak"}
203 -re "$gdb_prompt $" {fail "set follow-fork child, hit tbreak"}
204 timeout {fail "(timeout) set follow-fork child, hit tbreak"}
74cf1395 205 }
b242c3c2 206 # The parent has been detached; allow time for any output it might
74cf1395
JM
207 # generate to arrive, so that output doesn't get confused with
208 # any expected debugger output from a subsequent testpoint.
209 #
210 exec sleep 1
211 send_gdb "delete breakpoints\n"
212 gdb_expect {
213 -re "Delete all breakpoints.*$" {
214 send_gdb "y\n"
215 gdb_expect {
216 -re "$gdb_prompt $"\
6c95b8df
PA
217 {pass "set follow-fork child, cleanup"}
218 timeout {fail "(timeout) set follow-fork child, cleanup"}
74cf1395
JM
219 }
220 }
6c95b8df
PA
221 -re "$gdb_prompt $" {fail "set follow-fork child, cleanup"}
222 timeout {fail "(timeout) set follow-fork child, cleanup"}
74cf1395
JM
223 }
224}
225
b242c3c2
PA
226proc catch_fork_unpatch_child {} {
227 global gdb_prompt
228 global srcfile
229
230 set bp_exit [gdb_get_line_number "at exit"]
231
232 gdb_test "break callee" "file .*$srcfile, line .*" "unpatch child, break at callee"
233 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "unpatch child, set catch fork"
234
235 gdb_test "continue" \
236 "Catchpoint.*\\(forked process.*\\).*,.*in .*(fork|__kernel_v?syscall).*" \
237 "unpatch child, catch fork"
238
239 # Delete all breakpoints and catchpoints.
240 delete_breakpoints
241
d7167b4c
JK
242 # Force $srcfile as the current GDB source can be in glibc sourcetree.
243 gdb_test "break $srcfile:$bp_exit" \
b242c3c2
PA
244 "Breakpoint .*file .*$srcfile, line .*" \
245 "unpatch child, breakpoint at exit call"
246
6c95b8df 247 gdb_test "set follow-fork child" "" "unpatch child, set follow-fork child"
b242c3c2
PA
248
249 set test "unpatch child, unpatched parent breakpoints from child"
250 gdb_test_multiple "continue" $test {
251 -re "at exit.*$gdb_prompt $" {
252 pass "$test"
253 }
254 -re "SIGTRAP.*$gdb_prompt $" {
255 fail "$test"
256
257 # Explicitly kill this child, so we can continue gracefully
258 # with further testing...
259 send_gdb "kill\n"
260 gdb_expect {
261 -re ".*Kill the program being debugged.*y or n. $" {
262 send_gdb "y\n"
263 gdb_expect -re "$gdb_prompt $" {}
264 }
265 }
266 }
267 -re ".*$gdb_prompt $" {
268 fail "$test (unknown output)"
269 }
270 timeout {
271 fail "$test (timeout)"
272 }
273 }
274}
275
74cf1395
JM
276proc tcatch_fork_parent_follow {} {
277 global gdb_prompt
e85a822c 278 global srcfile
74cf1395 279
b242c3c2
PA
280 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
281
74cf1395
JM
282 send_gdb "catch fork\n"
283 gdb_expect {
284 -re "Catchpoint .*(fork).*$gdb_prompt $"\
285 {pass "explicit parent follow, set tcatch fork"}
286 -re "$gdb_prompt $" {fail "explicit parent follow, set tcatch fork"}
287 timeout {fail "(timeout) explicit parent follow, set tcatch fork"}
288 }
289# ??rehrauer: I don't yet know how to get the id of the tcatch
290# via this script, so that I can add a -do list to it. For now,
291# do the follow stuff after the catch happens.
292
293 send_gdb "continue\n"
294 gdb_expect {
a9c64011 295 -re ".*in .*(fork|__kernel_v?syscall).*$gdb_prompt $"\
74cf1395
JM
296 {pass "explicit parent follow, tcatch fork"}
297 -re "$gdb_prompt $" {fail "explicit parent follow, tcatch fork"}
298 timeout {fail "(timeout) explicit parent follow, tcatch fork"}
299 }
6c95b8df 300 send_gdb "set follow-fork parent\n"
74cf1395 301 gdb_expect {
6c95b8df
PA
302 -re "$gdb_prompt $" {pass "set follow-fork parent"}
303 timeout {fail "(timeout) set follow-fork parent"}
74cf1395 304 }
b242c3c2 305 send_gdb "tbreak ${srcfile}:$bp_after_fork\n"
74cf1395 306 gdb_expect {
b242c3c2 307 -re "Temporary breakpoint.*, line $bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
308 {pass "set follow-fork parent, tbreak"}
309 -re "$gdb_prompt $" {fail "set follow-fork parent, tbreak"}
310 timeout {fail "(timeout) set follow-fork child, tbreak"}
74cf1395
JM
311 }
312 send_gdb "continue\n"
313 gdb_expect {
b242c3c2 314 -re ".*Detaching after fork from.* at .*$bp_after_fork.*$gdb_prompt $"\
6c95b8df
PA
315 {pass "set follow-fork parent, hit tbreak"}
316 -re "$gdb_prompt $" {fail "set follow-fork parent, hit tbreak"}
317 timeout {fail "(timeout) set follow-fork parent, hit tbreak"}
74cf1395
JM
318 }
319 # The child has been detached; allow time for any output it might
320 # generate to arrive, so that output doesn't get confused with
321 # any expected debugger output from a subsequent testpoint.
322 #
323 exec sleep 1
324 send_gdb "delete breakpoints\n"
325 gdb_expect {
326 -re "Delete all breakpoints.*$" {
327 send_gdb "y\n"
328 gdb_expect {
329 -re "$gdb_prompt $"\
6c95b8df
PA
330 {pass "set follow-fork parent, cleanup"}
331 timeout {fail "(timeout) set follow-fork parent, cleanup"}
74cf1395
JM
332 }
333 }
6c95b8df
PA
334 -re "$gdb_prompt $" {fail "set follow-fork parent, cleanup"}
335 timeout {fail "(timeout) set follow-fork parent, cleanup"}
74cf1395
JM
336 }
337}
338
339proc do_fork_tests {} {
340 global gdb_prompt
341
342 # Verify that help is available for "set follow-fork-mode".
343 #
344 send_gdb "help set follow-fork-mode\n"
345 gdb_expect {
346 -re "Set debugger response to a program call of fork or vfork..*
347A fork or vfork creates a new process. follow-fork-mode can be:.*
348.*parent - the original process is debugged after a fork.*
349.*child - the new process is debugged after a fork.*
ef04f396 350The unfollowed process will continue to run..*
74cf1395 351By default, the debugger will follow the parent process..*$gdb_prompt $"\
6c95b8df 352 { pass "help set follow-fork" }
74cf1395 353 -re "$gdb_prompt $" { fail "help set follow" }
6c95b8df 354 timeout { fail "(timeout) help set follow-fork" }
74cf1395
JM
355 }
356
357 # Verify that we can set follow-fork-mode, using an abbreviation
358 # for both the flag and its value.
359 #
6c95b8df
PA
360 send_gdb "set follow-fork ch\n"
361 send_gdb "show follow-fork\n"
74cf1395
JM
362 gdb_expect {
363 -re "Debugger response to a program call of fork or vfork is \"child\".*$gdb_prompt $"\
6c95b8df
PA
364 {pass "set follow-fork, using abbreviations"}
365 timeout {fail "(timeout) set follow-fork, using abbreviations"}
74cf1395
JM
366 }
367
368 # Verify that we cannot set follow-fork-mode to nonsense.
369 #
6c95b8df 370 send_gdb "set follow-fork chork\n"
74cf1395
JM
371 gdb_expect {
372 -re "Undefined item: \"chork\".*$gdb_prompt $"\
6c95b8df
PA
373 {pass "set follow-fork to nonsense is prohibited"}
374 -re "$gdb_prompt $" {fail "set follow-fork to nonsense is prohibited"}
375 timeout {fail "(timeout) set follow-fork to nonsense is prohibited"}
74cf1395 376 }
6c95b8df 377 send_gdb "set follow-fork parent\n"
74cf1395 378 gdb_expect {
6c95b8df
PA
379 -re "$gdb_prompt $" {pass "set follow-fork to nonsense is prohibited (reset parent)"}
380 timeout {fail "set follow-fork to nonsense is prohibited (reset parent)"}
74cf1395
JM
381 }
382
e85a822c
DJ
383 # Check that fork catchpoints are supported, as an indicator for whether
384 # fork-following is supported.
385 if [runto_main] then { check_fork_catchpoints }
386
74cf1395
JM
387 # Test the default behaviour, which is to follow the parent of a
388 # fork, and detach from the child. Do this without catchpoints.
389 #
390 if [runto_main] then { default_fork_parent_follow }
391
392 # Test the ability to explicitly follow the parent of a fork, and
393 # detach from the child. Do this without catchpoints.
394 #
395 if [runto_main] then { explicit_fork_parent_follow }
396
397 # Test the ability to follow the child of a fork, and detach from
398 # the parent. Do this without catchpoints.
399 #
400 if [runto_main] then { explicit_fork_child_follow }
401
402 # Test the ability to follow both child and parent of a fork. Do
403 # this without catchpoints.
404 # ??rehrauer: NYI. Will add testpoints here when implemented.
405 #
406
407 # Test the ability to have the debugger ask the user at fork-time
408 # whether to follow the parent, child or both. Do this without
409 # catchpoints.
410 # ??rehrauer: NYI. Will add testpoints here when implemented.
411 #
412
413 # Test the ability to catch a fork, specify that the child be
414 # followed, and continue. Make the catchpoint permanent.
415 #
416 if [runto_main] then { catch_fork_child_follow }
417
b242c3c2
PA
418 # Test that parent breakpoints are successfully detached from the
419 # child at fork time, even if the user removes them from the
420 # breakpoints list after stopping at a fork catchpoint.
421 if [runto_main] then { catch_fork_unpatch_child }
422
74cf1395
JM
423 # Test the ability to catch a fork, specify via a -do clause that
424 # the parent be followed, and continue. Make the catchpoint temporary.
425 #
426 if [runto_main] then { tcatch_fork_parent_follow }
427}
428
429# Start with a fresh gdb
430
431gdb_exit
432gdb_start
433gdb_reinitialize_dir $srcdir/$subdir
434gdb_load ${binfile}
435
e85a822c
DJ
436# The "Detaching..." and "Attaching..." messages may be hidden by
437# default.
438gdb_test "set verbose" ""
74cf1395
JM
439
440# This is a test of gdb's ability to follow the parent, child or both
441# parent and child of a Unix fork() system call.
442#
443do_fork_tests
444
445return 0
This page took 0.952295 seconds and 4 git commands to generate.