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