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