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