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 | |
71ce889d PA |
16 | # Various tests of gdb's ability to follow the parent or child of a |
17 | # Unix vfork system call. A vfork parent is blocked until the child | |
18 | # either execs or exits --- since those events take somewhat different | |
19 | # code paths in GDB, both variants are exercised. | |
20 | ||
2d4e0376 YQ |
21 | # Until "set follow-fork-mode" and "catch vfork" are implemented on |
22 | # other targets... | |
23 | # | |
3ca22649 | 24 | if {![istarget "*-linux*"]} then { |
2d4e0376 YQ |
25 | continue |
26 | } | |
27 | ||
771bc0af | 28 | standard_testfile |
74cf1395 | 29 | |
3fef966c | 30 | set compile_options debug |
3fef966c TT |
31 | |
32 | if {[build_executable $testfile.exp $testfile $srcfile $compile_options] == -1} { | |
771bc0af PA |
33 | untested "failed to compile $testfile" |
34 | return -1 | |
74cf1395 JM |
35 | } |
36 | ||
771bc0af PA |
37 | set testfile2 "vforked-prog" |
38 | set srcfile2 ${testfile2}.c | |
39 | ||
3fef966c | 40 | if {[build_executable $testfile.exp $testfile2 $srcfile2 $compile_options] == -1} { |
771bc0af PA |
41 | untested "failed to compile $testfile2" |
42 | return -1 | |
74cf1395 JM |
43 | } |
44 | ||
74cf1395 JM |
45 | # A few of these tests require a little more time than the standard |
46 | # timeout allows. | |
47 | set oldtimeout $timeout | |
48 | set timeout [expr "$timeout + 10"] | |
49 | ||
39c1263a PA |
50 | # Start with a fresh GDB, with verbosity enabled, and run to main. On |
51 | # error, behave as "return", so we don't try to continue testing with | |
52 | # a borked session. | |
53 | proc setup_gdb {} { | |
31d913c7 | 54 | global testfile srcfile |
39c1263a PA |
55 | |
56 | clean_restart $testfile | |
57 | ||
58 | # The "Detaching..." and "Attaching..." messages may be hidden by | |
59 | # default. | |
60 | gdb_test_no_output "set verbose" | |
61 | ||
62 | if ![runto_main] { | |
63 | return -code return | |
64 | } | |
31d913c7 YQ |
65 | |
66 | set tbreak_line [gdb_get_line_number " VFORK " $srcfile] | |
67 | gdb_test "tbreak ${tbreak_line}" | |
68 | gdb_continue_to_breakpoint ".*" | |
39c1263a PA |
69 | } |
70 | ||
e85a822c DJ |
71 | proc check_vfork_catchpoints {} { |
72 | global gdb_prompt | |
73 | global has_vfork_catchpoints | |
74 | ||
39c1263a PA |
75 | setup_gdb |
76 | ||
e85a822c DJ |
77 | # Verify that the system supports "catch vfork". |
78 | gdb_test "catch vfork" "Catchpoint \[0-9\]* \\(vfork\\)" "insert first vfork catchpoint" | |
79 | set has_vfork_catchpoints 0 | |
80 | gdb_test_multiple "continue" "continue to first vfork catchpoint" { | |
77b06cd7 | 81 | -re ".*Your system does not support this type\r\nof catchpoint.*$gdb_prompt $" { |
e85a822c DJ |
82 | unsupported "continue to first vfork catchpoint" |
83 | } | |
84 | -re ".*Catchpoint.*$gdb_prompt $" { | |
85 | set has_vfork_catchpoints 1 | |
86 | pass "continue to first vfork catchpoint" | |
87 | } | |
88 | } | |
89 | ||
90 | if {$has_vfork_catchpoints == 0} { | |
91 | unsupported "vfork catchpoints" | |
92 | return -code return | |
93 | } | |
94 | } | |
95 | ||
74cf1395 | 96 | proc vfork_parent_follow_through_step {} { |
339aeac6 | 97 | with_test_prefix "vfork parent follow, through step" { |
74cf1395 JM |
98 | global gdb_prompt |
99 | ||
39c1263a PA |
100 | setup_gdb |
101 | ||
339aeac6 | 102 | gdb_test_no_output "set follow-fork parent" |
771bc0af | 103 | |
339aeac6 | 104 | set test "step" |
771bc0af | 105 | gdb_test_multiple "next" $test { |
6f259a23 | 106 | -re "Detaching after vfork from.*if \\(pid == 0\\).*$gdb_prompt " { |
339aeac6 | 107 | pass $test |
771bc0af | 108 | } |
74cf1395 JM |
109 | } |
110 | # The child has been detached; allow time for any output it might | |
111 | # generate to arrive, so that output doesn't get confused with | |
112 | # any gdb_expected debugger output from a subsequent testpoint. | |
113 | # | |
114 | exec sleep 1 | |
339aeac6 | 115 | }} |
74cf1395 JM |
116 | |
117 | proc vfork_parent_follow_to_bp {} { | |
339aeac6 | 118 | with_test_prefix "vfork parent follow, to bp" { |
74cf1395 | 119 | global gdb_prompt |
e85a822c | 120 | global srcfile |
74cf1395 | 121 | |
39c1263a PA |
122 | setup_gdb |
123 | ||
339aeac6 | 124 | gdb_test_no_output "set follow-fork parent" |
771bc0af | 125 | |
43ae8f01 PA |
126 | set bp_location [gdb_get_line_number "printf (\"I'm the proud parent of child"] |
127 | gdb_test "break ${srcfile}:${bp_location}" ".*" "break, vfork to bp" | |
771bc0af | 128 | |
339aeac6 | 129 | set test "continue to bp" |
771bc0af | 130 | gdb_test_multiple "continue" $test { |
6f259a23 | 131 | -re ".*Detaching after vfork from child process.*Breakpoint.*${bp_location}.*$gdb_prompt " { |
771bc0af PA |
132 | pass $test |
133 | } | |
74cf1395 JM |
134 | } |
135 | # The child has been detached; allow time for any output it might | |
136 | # generate to arrive, so that output doesn't get confused with | |
137 | # any expected debugger output from a subsequent testpoint. | |
138 | # | |
139 | exec sleep 1 | |
339aeac6 | 140 | }} |
74cf1395 | 141 | |
71ce889d PA |
142 | proc vfork_child_follow_to_exit {} { |
143 | with_test_prefix "vfork child follow, to exit" { | |
144 | global gdb_prompt | |
145 | ||
146 | setup_gdb | |
147 | ||
148 | gdb_test_no_output "set follow-fork child" | |
149 | ||
150 | set test "continue to child exit" | |
151 | gdb_test_multiple "continue" $test { | |
152 | -re "Couldn't get registers.*$gdb_prompt " { | |
f50f4e56 | 153 | # PR gdb/14766 |
71ce889d PA |
154 | fail "$test" |
155 | } | |
156 | -re "Attaching after.* vfork to.*Detaching vfork parent .* after child exit.*$gdb_prompt " { | |
157 | pass $test | |
158 | } | |
159 | } | |
160 | # The parent has been detached; allow time for any output it might | |
161 | # generate to arrive, so that output doesn't get confused with | |
162 | # any gdb_expected debugger output from a subsequent testpoint. | |
163 | # | |
164 | exec sleep 1 | |
165 | }} | |
166 | ||
74cf1395 | 167 | proc vfork_and_exec_child_follow_to_main_bp {} { |
339aeac6 | 168 | with_test_prefix "vfork and exec child follow, to main bp" { |
74cf1395 | 169 | global gdb_prompt |
43ae8f01 | 170 | global srcfile2 |
74cf1395 | 171 | |
39c1263a PA |
172 | setup_gdb |
173 | ||
339aeac6 | 174 | gdb_test_no_output "set follow-fork child" |
771bc0af | 175 | |
43ae8f01 PA |
176 | set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] |
177 | ||
339aeac6 | 178 | set test "continue to bp" |
771bc0af | 179 | gdb_test_multiple "continue" $test { |
6f259a23 | 180 | -re "Attaching after.* vfork to.*Detaching vfork parent.*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " { |
771bc0af PA |
181 | pass $test |
182 | } | |
74cf1395 JM |
183 | } |
184 | # The parent has been detached; allow time for any output it might | |
185 | # generate to arrive, so that output doesn't get confused with | |
186 | # any gdb_expected debugger output from a subsequent testpoint. | |
187 | # | |
188 | exec sleep 1 | |
339aeac6 | 189 | }} |
74cf1395 JM |
190 | |
191 | proc vfork_and_exec_child_follow_through_step {} { | |
339aeac6 | 192 | with_test_prefix "vfork and exec child follow, through step" { |
74cf1395 | 193 | global gdb_prompt |
43ae8f01 | 194 | global srcfile2 |
74cf1395 | 195 | |
39c1263a PA |
196 | setup_gdb |
197 | ||
339aeac6 | 198 | gdb_test_no_output "set follow-fork child" |
771bc0af | 199 | |
339aeac6 | 200 | set test "step over vfork" |
3ca22649 SM |
201 | |
202 | # The ideal support is to be able to debug the child even | |
203 | # before it execs. Thus, "next" lands on the next line after | |
204 | # the vfork. | |
205 | gdb_test_multiple "next" $test { | |
206 | -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " { | |
207 | pass "$test" | |
6dbc8997 | 208 | } |
74cf1395 JM |
209 | } |
210 | # The parent has been detached; allow time for any output it might | |
211 | # generate to arrive, so that output doesn't get confused with | |
212 | # any expected debugger output from a subsequent testpoint. | |
213 | # | |
214 | exec sleep 1 | |
339aeac6 | 215 | }} |
74cf1395 | 216 | |
9a706302 JK |
217 | proc continue_to_vfork {} { |
218 | global gdb_prompt | |
219 | ||
220 | # A vfork catchpoint may stop in either "vfork" or "_vfork". | |
221 | set test "continue to vfork" | |
222 | gdb_test_multiple "continue" $test { | |
223 | -re "vfork \\(\\) at .*$gdb_prompt $" { | |
224 | pass $test | |
225 | } | |
226 | -re "0x\[0-9a-fA-F\]*.*(vfork|__kernel_v?syscall).*$gdb_prompt " { | |
227 | pass $test | |
228 | } | |
229 | } | |
230 | } | |
231 | ||
74cf1395 | 232 | proc tcatch_vfork_then_parent_follow {} { |
339aeac6 | 233 | with_test_prefix "vfork parent follow, finish after tcatch vfork" { |
74cf1395 JM |
234 | global gdb_prompt |
235 | global srcfile | |
236 | ||
39c1263a PA |
237 | setup_gdb |
238 | ||
339aeac6 | 239 | gdb_test_no_output "set follow-fork parent" |
771bc0af | 240 | |
339aeac6 | 241 | gdb_test "tcatch vfork" "Catchpoint .*(vfork).*" |
9a706302 JK |
242 | |
243 | continue_to_vfork | |
771bc0af | 244 | |
43ae8f01 | 245 | set linenum [gdb_get_line_number "pid = vfork ();"] |
339aeac6 | 246 | set test "finish" |
771bc0af | 247 | gdb_test_multiple "finish" $test { |
43ae8f01 | 248 | -re "Run till exit from.*vfork.*0x\[0-9a-fA-F\]* in main .* at .*${srcfile}:${linenum}.*$gdb_prompt " { |
771bc0af PA |
249 | pass $test |
250 | } | |
a9c64011 AS |
251 | -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " { |
252 | send_gdb "finish\n" | |
253 | exp_continue | |
254 | } | |
74cf1395 JM |
255 | } |
256 | # The child has been detached; allow time for any output it might | |
257 | # generate to arrive, so that output doesn't get confused with | |
258 | # any expected debugger output from a subsequent testpoint. | |
259 | # | |
260 | exec sleep 1 | |
339aeac6 | 261 | }} |
74cf1395 | 262 | |
71ce889d | 263 | proc tcatch_vfork_then_child_follow_exec {} { |
339aeac6 | 264 | with_test_prefix "vfork child follow, finish after tcatch vfork" { |
74cf1395 | 265 | global gdb_prompt |
e85a822c | 266 | global srcfile |
74cf1395 JM |
267 | global srcfile2 |
268 | ||
39c1263a PA |
269 | setup_gdb |
270 | ||
339aeac6 | 271 | gdb_test_no_output "set follow-fork child" |
771bc0af | 272 | |
339aeac6 | 273 | gdb_test "tcatch vfork" "Catchpoint .*(vfork).*" |
9a706302 JK |
274 | |
275 | continue_to_vfork | |
771bc0af | 276 | |
43ae8f01 PA |
277 | set linenum1 [gdb_get_line_number "pid = vfork ();"] |
278 | set linenum2 [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] | |
279 | ||
339aeac6 | 280 | set test "finish" |
771bc0af | 281 | gdb_test_multiple "finish" $test { |
43ae8f01 | 282 | -re "Run till exit from.*vfork.*${srcfile}:${linenum1}.*$gdb_prompt " { |
771bc0af PA |
283 | pass $test |
284 | } | |
a9c64011 AS |
285 | -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " { |
286 | send_gdb "finish\n" | |
287 | exp_continue | |
288 | } | |
43ae8f01 | 289 | -re "Run till exit from.*vfork.*${srcfile2}:${linenum2}.*$gdb_prompt " { |
771bc0af PA |
290 | pass "$test (followed exec)" |
291 | } | |
74cf1395 JM |
292 | } |
293 | # The parent has been detached; allow time for any output it might | |
294 | # generate to arrive, so that output doesn't get confused with | |
295 | # any expected debugger output from a subsequent testpoint. | |
296 | # | |
297 | exec sleep 1 | |
339aeac6 | 298 | }} |
74cf1395 | 299 | |
71ce889d PA |
300 | proc tcatch_vfork_then_child_follow_exit {} { |
301 | with_test_prefix "vfork child follow, finish after tcatch vfork" { | |
74cf1395 | 302 | global gdb_prompt |
71ce889d PA |
303 | global srcfile |
304 | ||
305 | setup_gdb | |
306 | ||
307 | gdb_test_no_output "set follow-fork child" | |
308 | ||
309 | gdb_test "tcatch vfork" "Catchpoint .*(vfork).*" | |
9a706302 JK |
310 | |
311 | continue_to_vfork | |
71ce889d PA |
312 | |
313 | set test "finish" | |
314 | gdb_test_multiple "finish" $test { | |
71ce889d PA |
315 | -re "Run till exit from.*vfork.*exited normally.*$gdb_prompt " { |
316 | setup_kfail "gdb/14762" *-*-* | |
317 | fail $test | |
318 | } | |
319 | -re "Run till exit from.*vfork.*pid = vfork \\(\\).*$gdb_prompt " { | |
320 | pass $test | |
321 | } | |
322 | -re "Run till exit from.*__kernel_v?syscall.*0x\[0-9a-fA-F\]* in vfork .*$gdb_prompt " { | |
323 | send_gdb "finish\n" | |
324 | exp_continue | |
325 | } | |
326 | } | |
327 | # The parent has been detached; allow time for any output it might | |
328 | # generate to arrive, so that output doesn't get confused with | |
329 | # any expected debugger output from a subsequent testpoint. | |
330 | # | |
331 | exec sleep 1 | |
332 | }} | |
74cf1395 | 333 | |
68c9da30 PA |
334 | proc vfork_relations_in_info_inferiors { variant } { |
335 | with_test_prefix "vfork relations in info inferiors" { | |
336 | global gdb_prompt | |
337 | ||
338 | setup_gdb | |
339 | ||
340 | gdb_test_no_output "set follow-fork child" | |
341 | ||
342 | set test "step over vfork" | |
343 | gdb_test_multiple "next" $test { | |
344 | -re "Attaching after .* vfork to child.*if \\(pid == 0\\).*$gdb_prompt " { | |
345 | pass "$test" | |
346 | } | |
347 | } | |
348 | ||
349 | gdb_test "info inferiors" \ | |
b05b1202 | 350 | ".*is vfork parent of inferior 2.*is vfork child of inferior 1" \ |
68c9da30 PA |
351 | "info inferiors shows vfork parent/child relation" |
352 | ||
353 | if { $variant == "exec" } { | |
354 | global srcfile2 | |
355 | ||
356 | set linenum [gdb_get_line_number "printf(\"Hello from vforked-prog" ${srcfile2}] | |
357 | set test "continue to bp" | |
358 | gdb_test_multiple "continue" $test { | |
359 | -re ".*xecuting new program.*Breakpoint.*vforked-prog.c:${linenum}.*$gdb_prompt " { | |
360 | pass $test | |
361 | } | |
362 | } | |
363 | } else { | |
364 | set test "continue to child exit" | |
365 | gdb_test_multiple "continue" $test { | |
366 | -re "exited normally.*$gdb_prompt " { | |
367 | pass $test | |
368 | } | |
369 | } | |
370 | } | |
371 | ||
372 | set test "vfork relation no longer appears in info inferiors" | |
373 | gdb_test_multiple "info inferiors" $test { | |
374 | -re "is vfork child of inferior 1.*$gdb_prompt $" { | |
375 | fail $test | |
376 | } | |
377 | -re "is vfork parent of inferior 2.*$gdb_prompt $" { | |
378 | fail $test | |
379 | } | |
380 | -re "$gdb_prompt $" { | |
381 | pass $test | |
382 | } | |
383 | } | |
384 | }} | |
385 | ||
71ce889d PA |
386 | proc do_vfork_and_follow_parent_tests {} { |
387 | global gdb_prompt | |
e85a822c | 388 | |
74cf1395 JM |
389 | # Try following the parent process by stepping through a call to |
390 | # vfork. Do this without catchpoints. | |
39c1263a | 391 | vfork_parent_follow_through_step |
74cf1395 JM |
392 | |
393 | # Try following the parent process by setting a breakpoint on the | |
394 | # other side of a vfork, and running to that point. Do this | |
395 | # without catchpoints. | |
39c1263a | 396 | vfork_parent_follow_to_bp |
74cf1395 | 397 | |
71ce889d PA |
398 | # Try catching a vfork, and stepping out to the parent. |
399 | # | |
400 | tcatch_vfork_then_parent_follow | |
401 | } | |
402 | ||
403 | proc do_vfork_and_follow_child_tests_exec {} { | |
74cf1395 JM |
404 | # Try following the child process by just continuing through the |
405 | # vfork, and letting the parent's breakpoint on "main" be auto- | |
406 | # magically reset in the child. | |
407 | # | |
39c1263a | 408 | vfork_and_exec_child_follow_to_main_bp |
74cf1395 JM |
409 | |
410 | # Try following the child process by stepping through a call to | |
411 | # vfork. The child also executes an exec. Since the child cannot | |
412 | # be debugged until after it has exec'd, and since there's a bp on | |
413 | # "main" in the parent, and since the bp's for the parent are | |
414 | # recomputed in the exec'd child, the step through a vfork should | |
415 | # land us in the "main" for the exec'd child, too. | |
416 | # | |
39c1263a | 417 | vfork_and_exec_child_follow_through_step |
74cf1395 | 418 | |
74cf1395 JM |
419 | # Try catching a vfork, and stepping out to the child. |
420 | # | |
71ce889d | 421 | tcatch_vfork_then_child_follow_exec |
74cf1395 JM |
422 | |
423 | # Test the ability to follow both child and parent of a vfork. Do | |
424 | # this without catchpoints. | |
425 | # ??rehrauer: NYI. Will add testpoints here when implemented. | |
426 | # | |
427 | ||
428 | # Test the ability to have the debugger ask the user at vfork-time | |
429 | # whether to follow the parent, child or both. Do this without | |
430 | # catchpoints. | |
431 | # ??rehrauer: NYI. Will add testpoints here when implemented. | |
432 | # | |
68c9da30 PA |
433 | |
434 | # Step over a vfork in the child, do "info inferiors" and check the | |
435 | # parent/child relation is displayed. Run the child over the exec, | |
436 | # and confirm the relation is no longer displayed in "info | |
437 | # inferiors". | |
438 | # | |
439 | vfork_relations_in_info_inferiors "exec" | |
74cf1395 JM |
440 | } |
441 | ||
71ce889d PA |
442 | proc do_vfork_and_follow_child_tests_exit {} { |
443 | # Try following the child process by just continuing through the | |
444 | # vfork, and letting the child exit. | |
445 | # | |
446 | vfork_child_follow_to_exit | |
447 | ||
448 | # Try catching a vfork, and stepping out to the child. | |
449 | # | |
450 | tcatch_vfork_then_child_follow_exit | |
68c9da30 PA |
451 | |
452 | # Step over a vfork in the child, do "info inferiors" and check the | |
453 | # parent/child relation is displayed. Run the child to completion, | |
454 | # and confirm the relation is no longer displayed in "info | |
455 | # inferiors". | |
456 | # | |
457 | vfork_relations_in_info_inferiors "exit" | |
71ce889d PA |
458 | } |
459 | ||
460 | with_test_prefix "check vfork support" { | |
461 | # Check that vfork catchpoints are supported, as an indicator for | |
462 | # whether vfork-following is supported. | |
463 | check_vfork_catchpoints | |
464 | } | |
465 | ||
2fd33e94 DB |
466 | # Follow parent and follow child vfork tests with a child that execs. |
467 | with_test_prefix "exec" { | |
468 | # These are tests of gdb's ability to follow the parent of a Unix | |
469 | # vfork system call. The child will subsequently call a variant | |
470 | # of the Unix exec system call. | |
471 | do_vfork_and_follow_parent_tests | |
472 | ||
473 | # These are tests of gdb's ability to follow the child of a Unix | |
474 | # vfork system call. The child will subsequently call a variant | |
475 | # of a Unix exec system call. | |
476 | # | |
477 | do_vfork_and_follow_child_tests_exec | |
71ce889d PA |
478 | } |
479 | ||
480 | # Switch to test the case of the child exiting. We can't use | |
481 | # standard_testfile here because we don't want to overwrite the binary | |
482 | # of the previous tests. | |
483 | set testfile "foll-vfork-exit" | |
484 | set srcfile ${testfile}.c | |
485 | set binfile [standard_output_file ${testfile}] | |
486 | ||
487 | if {[build_executable $testfile.exp $testfile $srcfile] == -1} { | |
488 | untested "failed to build $testfile" | |
489 | return | |
490 | } | |
491 | ||
492 | # Follow parent and follow child vfork tests with a child that exits. | |
493 | with_test_prefix "exit" { | |
494 | # These are tests of gdb's ability to follow the parent of a Unix | |
495 | # vfork system call. The child will subsequently exit. | |
496 | do_vfork_and_follow_parent_tests | |
497 | ||
498 | # These are tests of gdb's ability to follow the child of a Unix | |
499 | # vfork system call. The child will subsequently exit. | |
500 | # | |
501 | do_vfork_and_follow_child_tests_exit | |
502 | } | |
74cf1395 JM |
503 | |
504 | set timeout $oldtimeout | |
505 | return 0 |