Update year range in copyright notice of all files owned by the GDB project.
[deliverable/binutils-gdb.git] / gdb / testsuite / gdb.base / foll-fork.exp
1 # Copyright 1997-2015 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 standard_testfile
28
29 if {[prepare_for_testing $testfile.exp $testfile $srcfile debug]} {
30 untested $testfile.exp
31 return -1
32 }
33
34 proc check_fork_catchpoints {} {
35 global gdb_prompt
36
37 # Verify that the system supports "catch fork".
38 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" "insert first fork catchpoint"
39 set has_fork_catchpoints 0
40 gdb_test_multiple "continue" "continue to first fork catchpoint" {
41 -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" {
42 unsupported "continue to first fork catchpoint"
43 }
44 -re ".*Catchpoint.*$gdb_prompt $" {
45 set has_fork_catchpoints 1
46 pass "continue to first fork catchpoint"
47 }
48 }
49
50 if {$has_fork_catchpoints == 0} {
51 unsupported "fork catchpoints"
52 return -code return
53 }
54 }
55
56 # Test follow-fork to ensure that the correct process is followed, that
57 # the followed process stops where it is expected to stop, that processes
58 # are detached (or not) as expected, and that the inferior list has the
59 # expected contents after following the fork. WHO is the argument to
60 # the 'set follow-fork-mode' command, DETACH is the argument to the
61 # 'set detach-on-fork' command, and CMD is the GDB command used to
62 # execute the program past the fork. If the value of WHO or DETACH is
63 # 'default', the corresponding GDB command is skipped for that test.
64 # The value of CMD must be either 'next 2' or 'continue'.
65 proc test_follow_fork { who detach cmd } {
66 global gdb_prompt
67 global srcfile
68 global testfile
69
70 with_test_prefix "follow $who, detach $detach, command \"$cmd\"" {
71
72 # Start a new debugger session each time so defaults are legitimate.
73 clean_restart $testfile
74
75 if ![runto_main] {
76 untested "could not run to main"
77 return -1
78 }
79
80 # The "Detaching..." and "Attaching..." messages may be hidden by
81 # default.
82 gdb_test_no_output "set verbose"
83
84 # Set follow-fork-mode if we aren't using the default.
85 if {$who == "default"} {
86 set who "parent"
87 } else {
88 gdb_test_no_output "set follow-fork $who"
89 }
90
91 gdb_test "show follow-fork" \
92 "Debugger response to a program call of fork or vfork is \"$who\"." \
93 "show follow-fork"
94
95 # Set detach-on-fork mode if we aren't using the default.
96 if {$detach == "default"} {
97 set detach "on"
98 } else {
99 gdb_test_no_output "set detach-on-fork $detach"
100 }
101
102 gdb_test "show detach-on-fork" \
103 "Whether gdb will detach.* fork is $detach." \
104 "show detach-on-fork"
105
106 # Set a breakpoint after the fork if we aren't single-stepping
107 # past the fork.
108 if {$cmd == "continue"} {
109 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
110 gdb_test "break ${srcfile}:$bp_after_fork" \
111 "Breakpoint.*, line $bp_after_fork.*" \
112 "set breakpoint after fork"
113 }
114
115 # Set up the output we expect to see after we run.
116 set expected_re ""
117 if {$who == "child"} {
118 set expected_re "Attaching after.* fork to.*"
119 if {$detach == "on"} {
120 append expected_re "Detaching after fork from .*"
121 }
122 append expected_re "set breakpoint here.*"
123 } elseif {$who == "parent" && $detach == "on"} {
124 set expected_re "Detaching after fork from .*set breakpoint here.*"
125 } else {
126 set expected_re ".*set breakpoint here.*"
127 }
128
129 # Test running past and following the fork, using the parameters
130 # set above.
131 gdb_test $cmd $expected_re "$cmd past fork"
132
133 # Check that we have the inferiors arranged correctly after
134 # following the fork.
135 set resume_unfollowed 0
136 if {$who == "parent" && $detach == "on"} {
137
138 # Follow parent / detach child: the only inferior is the parent.
139 gdb_test "info inferiors" "\\* 1 .* process.*" \
140 "info inferiors"
141
142 } elseif {$who == "parent" && $detach == "off"} {
143
144 # Follow parent / keep child: two inferiors under debug, the
145 # parent is the current inferior.
146 gdb_test "info inferiors" " 2 .*process.*\\* 1 .*process.*" \
147 "info inferiors"
148
149 gdb_test "inferior 2" "Switching to inferior 2 .*"
150 set resume_unfollowed 1
151
152 } elseif {$who == "child" && $detach == "on"} {
153
154 # Follow child / detach parent: the child is under debug and is
155 # the current inferior. The parent is listed but is not under
156 # debug.
157 gdb_test "info inferiors" "\\* 2 .*process.* 1 .*<null>.*" \
158 "info inferiors"
159
160 } elseif {$who == "child" && $detach == "off"} {
161
162 # Follow child / keep parent: two inferiors under debug, the
163 # child is the current inferior.
164 gdb_test "info inferiors" "\\* 2 .*process.* 1 .*process.*" \
165 "info inferiors"
166
167 gdb_test "inferior 1" "Switching to inferior 1 .*"
168 set resume_unfollowed 1
169 }
170
171 if {$resume_unfollowed == 1} {
172 if {$cmd == "next 2"} {
173
174 gdb_continue_to_end "continue unfollowed inferior to end"
175
176 } elseif {$cmd == "continue"} {
177
178 gdb_continue_to_breakpoint \
179 "continue unfollowed inferior to bp" \
180 ".* set breakpoint here.*"
181 }
182 }
183 }
184 }
185
186 proc catch_fork_child_follow {} {
187 global gdb_prompt
188 global srcfile
189
190 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
191
192 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
193 "explicit child follow, set catch fork"
194
195 # Verify that the catchpoint is mentioned in an "info breakpoints",
196 # and further that the catchpoint mentions no process id.
197 #
198 set test_name "info shows catchpoint without pid"
199 gdb_test_multiple "info breakpoints" "$test_name" {
200 -re ".*catchpoint.*keep y.*fork\[\r\n\]+$gdb_prompt $" {
201 pass "$test_name"
202 }
203 }
204
205 gdb_test "continue" \
206 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
207 "explicit child follow, catch fork"
208
209 # Verify that the catchpoint is mentioned in an "info breakpoints",
210 # and further that the catchpoint managed to capture a process id.
211 #
212 set test_name "info shows catchpoint without pid"
213 gdb_test_multiple "info breakpoints" "$test_name" {
214 -re ".*catchpoint.*keep y.*fork, process.*$gdb_prompt $" {
215 pass "$test_name"
216 }
217 }
218
219 gdb_test_no_output "set follow-fork child"
220
221 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
222 "Temporary breakpoint.*, line $bp_after_fork.*" \
223 "set follow-fork child, tbreak"
224
225 set expected_re "Attaching after.* fork to.*Detaching after fork from"
226 append expected_re ".* at .*$bp_after_fork.*"
227 gdb_test "continue" $expected_re "set follow-fork child, hit tbreak"
228
229 # The parent has been detached; allow time for any output it might
230 # generate to arrive, so that output doesn't get confused with
231 # any expected debugger output from a subsequent testpoint.
232 #
233 exec sleep 1
234
235 gdb_test "delete breakpoints" \
236 "" \
237 "set follow-fork child, cleanup" \
238 "Delete all breakpoints. \\(y or n\\) $" \
239 "y"
240 }
241
242 proc catch_fork_unpatch_child {} {
243 global gdb_prompt
244 global srcfile
245
246 set bp_exit [gdb_get_line_number "at exit"]
247
248 gdb_test "break callee" "file .*$srcfile, line .*" \
249 "unpatch child, break at callee"
250 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
251 "unpatch child, set catch fork"
252
253 gdb_test "continue" \
254 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
255 "unpatch child, catch fork"
256
257 # Delete all breakpoints and catchpoints.
258 delete_breakpoints
259
260 # Force $srcfile as the current GDB source can be in glibc sourcetree.
261 gdb_test "break $srcfile:$bp_exit" \
262 "Breakpoint .*file .*$srcfile, line .*" \
263 "unpatch child, breakpoint at exit call"
264
265 gdb_test_no_output "set follow-fork child" \
266 "unpatch child, set follow-fork child"
267
268 set test "unpatch child, unpatched parent breakpoints from child"
269 gdb_test_multiple "continue" $test {
270 -re "at exit.*$gdb_prompt $" {
271 pass "$test"
272 }
273 -re "SIGTRAP.*$gdb_prompt $" {
274 fail "$test"
275
276 # Explicitly kill this child, so we can continue gracefully
277 # with further testing...
278 send_gdb "kill\n"
279 gdb_expect {
280 -re ".*Kill the program being debugged.*y or n. $" {
281 send_gdb "y\n"
282 gdb_expect -re "$gdb_prompt $" {}
283 }
284 }
285 }
286 }
287 }
288
289 proc tcatch_fork_parent_follow {} {
290 global gdb_prompt
291 global srcfile
292
293 set bp_after_fork [gdb_get_line_number "set breakpoint here"]
294
295 gdb_test "catch fork" "Catchpoint \[0-9\]* \\(fork\\)" \
296 "explicit parent follow, set tcatch fork"
297
298 # ??rehrauer: I don't yet know how to get the id of the tcatch
299 # via this script, so that I can add a -do list to it. For now,
300 # do the follow stuff after the catch happens.
301
302 gdb_test "continue" \
303 "Catchpoint \[0-9\]* \\(forked process \[0-9\]*\\),.*" \
304 "explicit parent follow, tcatch fork"
305
306 gdb_test_no_output "set follow-fork parent"
307
308 gdb_test "tbreak ${srcfile}:$bp_after_fork" \
309 "Temporary breakpoint.*, line $bp_after_fork.*" \
310 "set follow-fork parent, tbreak"
311
312 gdb_test "continue" \
313 "Detaching after fork from.* at .*$bp_after_fork.*" \
314 "set follow-fork parent, hit tbreak"
315
316 # The child has been detached; allow time for any output it might
317 # generate to arrive, so that output doesn't get confused with
318 # any expected debugger output from a subsequent testpoint.
319 #
320 exec sleep 1
321
322 gdb_test "delete breakpoints" \
323 "" \
324 "set follow-fork parent, cleanup" \
325 "Delete all breakpoints. \\(y or n\\) $" \
326 "y"
327 }
328
329 proc do_fork_tests {} {
330 global gdb_prompt
331 global testfile
332
333 # Verify that help is available for "set follow-fork-mode".
334 #
335 gdb_test "help set follow-fork-mode" \
336 "Set debugger response to a program call of fork or vfork..*
337 A fork or vfork creates a new process. follow-fork-mode can be:.*
338 .*parent - the original process is debugged after a fork.*
339 .*child - the new process is debugged after a fork.*
340 The unfollowed process will continue to run..*
341 By default, the debugger will follow the parent process..*" \
342 "help set follow-fork"
343
344 # Verify that we can set follow-fork-mode, using an abbreviation
345 # for both the flag and its value.
346 #
347 gdb_test_no_output "set follow-fork ch"
348
349 gdb_test "show follow-fork" \
350 "Debugger response to a program call of fork or vfork is \"child\".*" \
351 "set follow-fork, using abbreviations"
352
353 # Verify that we cannot set follow-fork-mode to nonsense.
354 #
355 gdb_test "set follow-fork chork" "Undefined item: \"chork\".*" \
356 "set follow-fork to nonsense is prohibited"
357
358 gdb_test_no_output "set follow-fork parent" "reset parent"
359
360 # Check that fork catchpoints are supported, as an indicator for whether
361 # fork-following is supported.
362 if [runto_main] then { check_fork_catchpoints }
363
364 # Test the basic follow-fork functionality using all combinations of
365 # values for follow-fork-mode and detach-on-fork, using either a
366 # breakpoint or single-step to execute past the fork.
367 #
368 # The first loop should be sufficient to test the defaults. There
369 # is no need to test using the defaults in other permutations (e.g.
370 # "default" "on", "parent" "default", etc.).
371 foreach cmd {"next 2" "continue"} {
372 test_follow_fork "default" "default" $cmd
373 }
374
375 # Now test all explicit permutations.
376 foreach who {"parent" "child"} {
377 foreach detach {"on" "off"} {
378 foreach cmd {"next 2" "continue"} {
379 test_follow_fork $who $detach $cmd
380 }
381 }
382 }
383
384 # Catchpoint tests.
385
386 # Restart to eliminate any effects of the follow-fork tests.
387 clean_restart $testfile
388 gdb_test_no_output "set verbose"
389
390 # Test the ability to catch a fork, specify that the child be
391 # followed, and continue. Make the catchpoint permanent.
392 #
393 if [runto_main] then { catch_fork_child_follow }
394
395 # Test that parent breakpoints are successfully detached from the
396 # child at fork time, even if the user removes them from the
397 # breakpoints list after stopping at a fork catchpoint.
398 if [runto_main] then { catch_fork_unpatch_child }
399
400 # Test the ability to catch a fork, specify via a -do clause that
401 # the parent be followed, and continue. Make the catchpoint temporary.
402 #
403 if [runto_main] then { tcatch_fork_parent_follow }
404 }
405
406 # The "Detaching..." and "Attaching..." messages may be hidden by
407 # default.
408 gdb_test_no_output "set verbose"
409
410 # This is a test of gdb's ability to follow the parent, child or both
411 # parent and child of a Unix fork() system call.
412 #
413 do_fork_tests
414
415 return 0
This page took 0.0395219999999999 seconds and 5 git commands to generate.