Update years in copyright notice for the GDB files.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
1 # Copyright 1997-2013 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 if { [is_remote target] || ![isnative] } then {
17 continue
18 }
19
20 # Until "set follow-fork-mode" and "catch fork" are implemented on
21 # other targets...
22 #
23 if {![istarget "hppa*-hp-hpux*"] && ![istarget "*-linux*"]} then {
24 continue
25 }
26
27 global srcfile
28 set testfile "foll-fork"
29 set srcfile ${testfile}.c
30 set binfile ${objdir}/${subdir}/${testfile}
31
32 if { [gdb_compile "${srcdir}/${subdir}/${srcfile}" "${binfile}" executable {debug}] != "" } {
33 untested foll-fork.exp
34 return -1
35 }
36
37 proc check_fork_catchpoints {} {
38 global gdb_prompt
39
40 # Verify that the system supports "catch fork".
41 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
42 set has_fork_catchpoints 0
43 gdb_test_multiple "continue" "continue to first fork catchpoint" {
44 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
45 unsupported "continue to first fork catchpoint"
46 }
47 -re ".*Catchpoint.*$gdb_prompt $" {
48 set has_fork_catchpoints 1
49 pass "continue to first fork catchpoint"
50 }
51 }
52
53 if {$has_fork_catchpoints == 0} {
54 unsupported "fork catchpoints"
55 return -code return
56 }
57 }
58
59 proc default_fork_parent_follow {} {
60 global gdb_prompt
61
62 gdb_test "show follow-fork" \
63 "Debugger response to a program call of fork or vfork is \"parent\".*" \
64 "default show parent follow, no catchpoints"
65
66 gdb_test "next 2" \
67 "Detaching after fork from.*" \
68 "default parent follow, no catchpoints"
69
70 # The child has been detached; allow time for any output it might
71 # generate to arrive, so that output doesn't get confused with
72 # any expected debugger output from a subsequent testpoint.
73 #
74 exec sleep 1
75 }
76
77 proc explicit_fork_parent_follow {} {
78 global gdb_prompt
79
80 gdb_test_no_output "set follow-fork parent"
81
82 gdb_test "show follow-fork" \
83 "Debugger response to a program call of fork or vfork is \"parent\"." \
84 "explicit show parent follow, no catchpoints"
85
86 gdb_test "next 2" "Detaching after fork from.*" \
87 "explicit parent follow, no catchpoints"
88
89 # The child has been detached; allow time for any output it might
90 # generate to arrive, so that output doesn't get confused with
91 # any expected debugger output from a subsequent testpoint.
92 #
93 exec sleep 1
94 }
95
96 proc explicit_fork_child_follow {} {
97 global gdb_prompt
98
99 gdb_test_no_output "set follow-fork child"
100
101 gdb_test "show follow-fork" \
102 "Debugger response to a program call of fork or vfork is \"child\"." \
103 "explicit show child follow, no catchpoints"
104
105 gdb_test "next 2" "Attaching after.* fork to.*" \
106 "explicit child follow, no catchpoints"
107
108 # The child has been detached; allow time for any output it might
109 # generate to arrive, so that output doesn't get confused with
110 # any gdb_expected debugger output from a subsequent testpoint.
111 #
112 exec sleep 1
113 }
114
115 proc catch_fork_child_follow {} {
116 global gdb_prompt
117 global srcfile
118
119 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
120
121 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
122 "explicit child follow, set catch fork"
123
124 # Verify that the catchpoint is mentioned in an "info breakpoints",
125 # and further that the catchpoint mentions no process id.
126 #
127 set test_name "info shows catchpoint without pid"
128 gdb_test_multiple "info breakpoints" "$test_name" {
129 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
130 pass "$test_name"
131 }
132 }
133
134 gdb_test "continue" \
135 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
136 "explicit child follow, catch fork"
137
138 # Verify that the catchpoint is mentioned in an "info breakpoints",
139 # and further that the catchpoint managed to capture a process id.
140 #
141 set test_name "info shows catchpoint without pid"
142 gdb_test_multiple "info breakpoints" "$test_name" {
143 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
144 pass "$test_name"
145 }
146 }
147
148 gdb_test_no_output "set follow-fork child"
149
150 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
151 "Temporary breakpoint.*, line $bp_after_fork.*" \
152 "set follow-fork child, tbreak"
153
154 gdb_test "continue" \
155 "Attaching after.* fork to.* at .*$bp_after_fork.*" \
156 "set follow-fork child, hit tbreak"
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 expected debugger output from a subsequent testpoint.
161 #
162 exec sleep 1
163
164 gdb_test "delete breakpoints" \
165 "" \
166 "set follow-fork child, cleanup" \
167 "Delete all breakpoints. \\(y or n\\) $" \
168 "y"
169 }
170
171 proc catch_fork_unpatch_child {} {
172 global gdb_prompt
173 global srcfile
174
175 set bp_exit [gdb_get_line_number "at exit"]
176
177 gdb_test "break callee" "file .*$srcfile, line .*" \
178 "unpatch child, break at callee"
179 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
180 "unpatch child, set catch fork"
181
182 gdb_test "continue" \
183 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
184 "unpatch child, catch fork"
185
186 # Delete all breakpoints and catchpoints.
187 delete_breakpoints
188
189 # Force $srcfile as the current GDB source can be in glibc sourcetree.
190 gdb_test "break $srcfile:$bp_exit" \
191 "Breakpoint .*file .*$srcfile, line .*" \
192 "unpatch child, breakpoint at exit call"
193
194 gdb_test_no_output "set follow-fork child" \
195 "unpatch child, set follow-fork child"
196
197 set test "unpatch child, unpatched parent breakpoints from child"
198 gdb_test_multiple "continue" $test {
199 -re "at exit.*$gdb_prompt $" {
200 pass "$test"
201 }
202 -re "SIGTRAP.*$gdb_prompt $" {
203 fail "$test"
204
205 # Explicitly kill this child, so we can continue gracefully
206 # with further testing...
207 send_gdb "kill\n"
208 gdb_expect {
209 -re ".*Kill the program being debugged.*y or n. $" {
210 send_gdb "y\n"
211 gdb_expect -re "$gdb_prompt $" {}
212 }
213 }
214 }
215 }
216 }
217
218 proc tcatch_fork_parent_follow {} {
219 global gdb_prompt
220 global srcfile
221
222 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
223
224 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
225 "explicit parent follow, set tcatch fork"
226
227 # ??rehrauer: I don't yet know how to get the id of the tcatch
228 # via this script, so that I can add a -do list to it. For now,
229 # do the follow stuff after the catch happens.
230
231 gdb_test "continue" \
232 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
233 "explicit parent follow, tcatch fork"
234
235 gdb_test_no_output "set follow-fork parent"
236
237 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
238 "Temporary breakpoint.*, line $bp_after_fork.*" \
239 "set follow-fork parent, tbreak"
240
241 gdb_test "continue" \
242 "Detaching after fork from.* at .*$bp_after_fork.*" \
243 "set follow-fork parent, hit tbreak"
244
245 # The child has been detached; allow time for any output it might
246 # generate to arrive, so that output doesn't get confused with
247 # any expected debugger output from a subsequent testpoint.
248 #
249 exec sleep 1
250
251 gdb_test "delete breakpoints" \
252 "" \
253 "set follow-fork parent, cleanup" \
254 "Delete all breakpoints. \\(y or n\\) $" \
255 "y"
256 }
257
258 proc do_fork_tests {} {
259 global gdb_prompt
260
261 # Verify that help is available for "set follow-fork-mode".
262 #
263 gdb_test "help set follow-fork-mode" \
264 "Set debugger response to a program call of fork or vfork..*
265 A fork or vfork creates a new process. follow-fork-mode can be:.*
266 .*parent - the original process is debugged after a fork.*
267 .*child - the new process is debugged after a fork.*
268 The unfollowed process will continue to run..*
269 By default, the debugger will follow the parent process..*" \
270 "help set follow-fork"
271
272 # Verify that we can set follow-fork-mode, using an abbreviation
273 # for both the flag and its value.
274 #
275 gdb_test_no_output "set follow-fork ch"
276
277 gdb_test "show follow-fork" \
278 "Debugger response to a program call of fork or vfork is \"child\".*" \
279 "set follow-fork, using abbreviations"
280
281 # Verify that we cannot set follow-fork-mode to nonsense.
282 #
283 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
284 "set follow-fork to nonsense is prohibited"
285
286 gdb_test_no_output "set follow-fork parent" "reset parent"
287
288 # Check that fork catchpoints are supported, as an indicator for whether
289 # fork-following is supported.
290 if [runto_main] then { check_fork_catchpoints }
291
292 # Test the default behaviour, which is to follow the parent of a
293 # fork, and detach from the child. Do this without catchpoints.
294 #
295 if [runto_main] then { default_fork_parent_follow }
296
297 # Test the ability to explicitly follow the parent of a fork, and
298 # detach from the child. Do this without catchpoints.
299 #
300 if [runto_main] then { explicit_fork_parent_follow }
301
302 # Test the ability to follow the child of a fork, and detach from
303 # the parent. Do this without catchpoints.
304 #
305 if [runto_main] then { explicit_fork_child_follow }
306
307 # Test the ability to follow both child and parent of a fork. Do
308 # this without catchpoints.
309 # ??rehrauer: NYI. Will add testpoints here when implemented.
310 #
311
312 # Test the ability to have the debugger ask the user at fork-time
313 # whether to follow the parent, child or both. Do this without
314 # catchpoints.
315 # ??rehrauer: NYI. Will add testpoints here when implemented.
316 #
317
318 # Test the ability to catch a fork, specify that the child be
319 # followed, and continue. Make the catchpoint permanent.
320 #
321 if [runto_main] then { catch_fork_child_follow }
322
323 # Test that parent breakpoints are successfully detached from the
324 # child at fork time, even if the user removes them from the
325 # breakpoints list after stopping at a fork catchpoint.
326 if [runto_main] then { catch_fork_unpatch_child }
327
328 # Test the ability to catch a fork, specify via a -do clause that
329 # the parent be followed, and continue. Make the catchpoint temporary.
330 #
331 if [runto_main] then { tcatch_fork_parent_follow }
332 }
333
334 # Start with a fresh gdb
335
336 gdb_exit
337 gdb_start
338 gdb_reinitialize_dir $srcdir/$subdir
339 gdb_load ${binfile}
340
341 # The "Detaching..." and "Attaching..." messages may be hidden by
342 # default.
343 gdb_test_no_output "set verbose"
344
345 # This is a test of gdb's ability to follow the parent, child or both
346 # parent and child of a Unix fork() system call.
347 #
348 do_fork_tests
349
350 return 0
This page took 0.038351 seconds and 5 git commands to generate.