2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/ptrace.h>
32 #include <sys/sysctl.h>
43 * A variant of ATF_REQUIRE that is suitable for use in child
44 * processes. This only works if the parent process is tripped up by
45 * the early exit and fails some requirement itself.
47 #define CHILD_REQUIRE(exp) do { \
49 child_fail_require(__FILE__, __LINE__, \
54 child_fail_require(const char *file, int line, const char *str)
58 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
59 write(2, buf, strlen(buf));
67 /* Attach the parent process as a tracer of this process. */
68 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
75 attach_child(pid_t pid)
80 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
82 wpid = waitpid(pid, &status, 0);
83 ATF_REQUIRE(wpid == pid);
84 ATF_REQUIRE(WIFSTOPPED(status));
85 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
89 wait_for_zombie(pid_t pid)
93 * Wait for a process to exit. This is kind of gross, but
94 * there is not a better way.
103 mib[2] = KERN_PROC_PID;
106 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
107 /* The KERN_PROC_PID sysctl fails for zombies. */
108 ATF_REQUIRE(errno == ESRCH);
116 * Verify that a parent debugger process "sees" the exit of a debugged
117 * process exactly once when attached via PT_TRACE_ME.
119 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
120 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
125 ATF_REQUIRE((child = fork()) != -1);
133 /* Parent process. */
135 /* The first wait() should report the stop from SIGSTOP. */
136 wpid = waitpid(child, &status, 0);
137 ATF_REQUIRE(wpid == child);
138 ATF_REQUIRE(WIFSTOPPED(status));
139 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
141 /* Continue the child ignoring the SIGSTOP. */
142 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
144 /* The second wait() should report the exit status. */
145 wpid = waitpid(child, &status, 0);
146 ATF_REQUIRE(wpid == child);
147 ATF_REQUIRE(WIFEXITED(status));
148 ATF_REQUIRE(WEXITSTATUS(status) == 1);
150 /* The child should no longer exist. */
151 wpid = waitpid(child, &status, 0);
152 ATF_REQUIRE(wpid == -1);
153 ATF_REQUIRE(errno == ECHILD);
157 * Verify that a parent debugger process "sees" the exit of a debugged
158 * process exactly once when attached via PT_ATTACH.
160 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
161 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
164 int cpipe[2], status;
167 ATF_REQUIRE(pipe(cpipe) == 0);
168 ATF_REQUIRE((child = fork()) != -1);
173 /* Wait for the parent to attach. */
174 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
180 /* Parent process. */
182 /* Attach to the child process. */
185 /* Continue the child ignoring the SIGSTOP. */
186 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
188 /* Signal the child to exit. */
191 /* The second wait() should report the exit status. */
192 wpid = waitpid(child, &status, 0);
193 ATF_REQUIRE(wpid == child);
194 ATF_REQUIRE(WIFEXITED(status));
195 ATF_REQUIRE(WEXITSTATUS(status) == 1);
197 /* The child should no longer exist. */
198 wpid = waitpid(child, &status, 0);
199 ATF_REQUIRE(wpid == -1);
200 ATF_REQUIRE(errno == ECHILD);
204 * Verify that a parent process "sees" the exit of a debugged process only
205 * after the debugger has seen it.
207 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
208 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
210 pid_t child, debugger, wpid;
211 int cpipe[2], dpipe[2], status;
214 ATF_REQUIRE(pipe(cpipe) == 0);
215 ATF_REQUIRE((child = fork()) != -1);
221 /* Wait for parent to be ready. */
222 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
228 ATF_REQUIRE(pipe(dpipe) == 0);
229 ATF_REQUIRE((debugger = fork()) != -1);
232 /* Debugger process. */
235 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
237 wpid = waitpid(child, &status, 0);
238 CHILD_REQUIRE(wpid == child);
239 CHILD_REQUIRE(WIFSTOPPED(status));
240 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
242 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
244 /* Signal parent that debugger is attached. */
245 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
247 /* Wait for parent's failed wait. */
248 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
250 wpid = waitpid(child, &status, 0);
251 CHILD_REQUIRE(wpid == child);
252 CHILD_REQUIRE(WIFEXITED(status));
253 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
259 /* Parent process. */
261 /* Wait for the debugger to attach to the child. */
262 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
264 /* Release the child. */
265 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
266 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
269 wait_for_zombie(child);
272 * This wait should return a pid of 0 to indicate no status to
273 * report. The parent should see the child as non-exited
274 * until the debugger sees the exit.
276 wpid = waitpid(child, &status, WNOHANG);
277 ATF_REQUIRE(wpid == 0);
279 /* Signal the debugger to wait for the child. */
282 /* Wait for the debugger. */
283 wpid = waitpid(debugger, &status, 0);
284 ATF_REQUIRE(wpid == debugger);
285 ATF_REQUIRE(WIFEXITED(status));
286 ATF_REQUIRE(WEXITSTATUS(status) == 0);
288 /* The child process should now be ready. */
289 wpid = waitpid(child, &status, WNOHANG);
290 ATF_REQUIRE(wpid == child);
291 ATF_REQUIRE(WIFEXITED(status));
292 ATF_REQUIRE(WEXITSTATUS(status) == 1);
296 * Verify that a parent process "sees" the exit of a debugged process
297 * only after a non-direct-child debugger has seen it. In particular,
298 * various wait() calls in the parent must avoid failing with ESRCH by
299 * checking the parent's orphan list for the debugee.
301 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
302 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
304 pid_t child, debugger, fpid, wpid;
305 int cpipe[2], dpipe[2], status;
308 ATF_REQUIRE(pipe(cpipe) == 0);
309 ATF_REQUIRE((child = fork()) != -1);
315 /* Wait for parent to be ready. */
316 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
322 ATF_REQUIRE(pipe(dpipe) == 0);
323 ATF_REQUIRE((debugger = fork()) != -1);
326 /* Debugger parent. */
329 * Fork again and drop the debugger parent so that the
330 * debugger is not a child of the main parent.
332 CHILD_REQUIRE((fpid = fork()) != -1);
336 /* Debugger process. */
339 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
341 wpid = waitpid(child, &status, 0);
342 CHILD_REQUIRE(wpid == child);
343 CHILD_REQUIRE(WIFSTOPPED(status));
344 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
346 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
348 /* Signal parent that debugger is attached. */
349 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
351 /* Wait for parent's failed wait. */
352 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
354 wpid = waitpid(child, &status, 0);
355 CHILD_REQUIRE(wpid == child);
356 CHILD_REQUIRE(WIFEXITED(status));
357 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
363 /* Parent process. */
365 /* Wait for the debugger parent process to exit. */
366 wpid = waitpid(debugger, &status, 0);
367 ATF_REQUIRE(wpid == debugger);
368 ATF_REQUIRE(WIFEXITED(status));
369 ATF_REQUIRE(WEXITSTATUS(status) == 2);
371 /* A WNOHANG wait here should see the non-exited child. */
372 wpid = waitpid(child, &status, WNOHANG);
373 ATF_REQUIRE(wpid == 0);
375 /* Wait for the debugger to attach to the child. */
376 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
378 /* Release the child. */
379 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
380 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
383 wait_for_zombie(child);
386 * This wait should return a pid of 0 to indicate no status to
387 * report. The parent should see the child as non-exited
388 * until the debugger sees the exit.
390 wpid = waitpid(child, &status, WNOHANG);
391 ATF_REQUIRE(wpid == 0);
393 /* Signal the debugger to wait for the child. */
394 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
396 /* Wait for the debugger. */
397 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
400 /* The child process should now be ready. */
401 wpid = waitpid(child, &status, WNOHANG);
402 ATF_REQUIRE(wpid == child);
403 ATF_REQUIRE(WIFEXITED(status));
404 ATF_REQUIRE(WEXITSTATUS(status) == 1);
408 * The parent process should always act the same regardless of how the
409 * debugger is attached to it.
412 follow_fork_parent(void)
417 CHILD_REQUIRE((fpid = fork()) != -1);
423 wpid = waitpid(fpid, &status, 0);
424 CHILD_REQUIRE(wpid == fpid);
425 CHILD_REQUIRE(WIFEXITED(status));
426 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
432 * Helper routine for follow fork tests. This waits for two stops
433 * that report both "sides" of a fork. It returns the pid of the new
437 handle_fork_events(pid_t parent)
439 struct ptrace_lwpinfo pl;
440 bool fork_reported[2];
444 fork_reported[0] = false;
445 fork_reported[1] = false;
449 * Each process should report a fork event. The parent should
450 * report a PL_FLAG_FORKED event, and the child should report
451 * a PL_FLAG_CHILD event.
453 for (i = 0; i < 2; i++) {
454 wpid = wait(&status);
455 ATF_REQUIRE(wpid > 0);
456 ATF_REQUIRE(WIFSTOPPED(status));
458 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
460 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
462 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
463 (PL_FLAG_FORKED | PL_FLAG_CHILD));
464 if (pl.pl_flags & PL_FLAG_CHILD) {
465 ATF_REQUIRE(wpid != parent);
466 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
467 ATF_REQUIRE(!fork_reported[1]);
471 ATF_REQUIRE(child == wpid);
472 fork_reported[1] = true;
474 ATF_REQUIRE(wpid == parent);
475 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
476 ATF_REQUIRE(!fork_reported[0]);
478 child = pl.pl_child_pid;
480 ATF_REQUIRE(child == pl.pl_child_pid);
481 fork_reported[0] = true;
489 * Verify that a new child process is stopped after a followed fork and
490 * that the traced parent sees the exit of the child after the debugger
491 * when both processes remain attached to the debugger.
493 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
494 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
496 pid_t children[0], fpid, wpid;
499 ATF_REQUIRE((fpid = fork()) != -1);
502 follow_fork_parent();
505 /* Parent process. */
508 /* The first wait() should report the stop from SIGSTOP. */
509 wpid = waitpid(children[0], &status, 0);
510 ATF_REQUIRE(wpid == children[0]);
511 ATF_REQUIRE(WIFSTOPPED(status));
512 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
514 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
516 /* Continue the child ignoring the SIGSTOP. */
517 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
519 children[1] = handle_fork_events(children[0]);
520 ATF_REQUIRE(children[1] > 0);
522 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
523 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
526 * The child can't exit until the grandchild reports status, so the
527 * grandchild should report its exit first to the debugger.
529 wpid = wait(&status);
530 ATF_REQUIRE(wpid == children[1]);
531 ATF_REQUIRE(WIFEXITED(status));
532 ATF_REQUIRE(WEXITSTATUS(status) == 2);
534 wpid = wait(&status);
535 ATF_REQUIRE(wpid == children[0]);
536 ATF_REQUIRE(WIFEXITED(status));
537 ATF_REQUIRE(WEXITSTATUS(status) == 1);
539 wpid = wait(&status);
540 ATF_REQUIRE(wpid == -1);
541 ATF_REQUIRE(errno == ECHILD);
545 * Verify that a new child process is stopped after a followed fork
546 * and that the traced parent sees the exit of the child when the new
547 * child process is detached after it reports its fork.
549 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
550 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
552 pid_t children[0], fpid, wpid;
555 ATF_REQUIRE((fpid = fork()) != -1);
558 follow_fork_parent();
561 /* Parent process. */
564 /* The first wait() should report the stop from SIGSTOP. */
565 wpid = waitpid(children[0], &status, 0);
566 ATF_REQUIRE(wpid == children[0]);
567 ATF_REQUIRE(WIFSTOPPED(status));
568 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
570 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
572 /* Continue the child ignoring the SIGSTOP. */
573 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
575 children[1] = handle_fork_events(children[0]);
576 ATF_REQUIRE(children[1] > 0);
578 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
579 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
582 * Should not see any status from the grandchild now, only the
585 wpid = wait(&status);
586 ATF_REQUIRE(wpid == children[0]);
587 ATF_REQUIRE(WIFEXITED(status));
588 ATF_REQUIRE(WEXITSTATUS(status) == 1);
590 wpid = wait(&status);
591 ATF_REQUIRE(wpid == -1);
592 ATF_REQUIRE(errno == ECHILD);
596 * Verify that a new child process is stopped after a followed fork
597 * and that the traced parent sees the exit of the child when the
598 * traced parent is detached after the fork.
600 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
601 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
603 pid_t children[0], fpid, wpid;
606 ATF_REQUIRE((fpid = fork()) != -1);
609 follow_fork_parent();
612 /* Parent process. */
615 /* The first wait() should report the stop from SIGSTOP. */
616 wpid = waitpid(children[0], &status, 0);
617 ATF_REQUIRE(wpid == children[0]);
618 ATF_REQUIRE(WIFSTOPPED(status));
619 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
621 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
623 /* Continue the child ignoring the SIGSTOP. */
624 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
626 children[1] = handle_fork_events(children[0]);
627 ATF_REQUIRE(children[1] > 0);
629 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
630 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
633 * The child can't exit until the grandchild reports status, so the
634 * grandchild should report its exit first to the debugger.
636 * Even though the child process is detached, it is still a
637 * child of the debugger, so it will still report it's exit
638 * after the grandchild.
640 wpid = wait(&status);
641 ATF_REQUIRE(wpid == children[1]);
642 ATF_REQUIRE(WIFEXITED(status));
643 ATF_REQUIRE(WEXITSTATUS(status) == 2);
645 wpid = wait(&status);
646 ATF_REQUIRE(wpid == children[0]);
647 ATF_REQUIRE(WIFEXITED(status));
648 ATF_REQUIRE(WEXITSTATUS(status) == 1);
650 wpid = wait(&status);
651 ATF_REQUIRE(wpid == -1);
652 ATF_REQUIRE(errno == ECHILD);
656 attach_fork_parent(int cpipe[2])
662 /* Double-fork to disassociate from the debugger. */
663 CHILD_REQUIRE((fpid = fork()) != -1);
667 /* Send the pid of the disassociated child to the debugger. */
669 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
671 /* Wait for the debugger to attach. */
672 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
676 * Verify that a new child process is stopped after a followed fork and
677 * that the traced parent sees the exit of the child after the debugger
678 * when both processes remain attached to the debugger. In this test
679 * the parent that forks is not a direct child of the debugger.
681 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
682 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
684 pid_t children[0], fpid, wpid;
685 int cpipe[2], status;
687 ATF_REQUIRE(pipe(cpipe) == 0);
688 ATF_REQUIRE((fpid = fork()) != -1);
690 attach_fork_parent(cpipe);
691 follow_fork_parent();
694 /* Parent process. */
697 /* Wait for the direct child to exit. */
698 wpid = waitpid(fpid, &status, 0);
699 ATF_REQUIRE(wpid == fpid);
700 ATF_REQUIRE(WIFEXITED(status));
701 ATF_REQUIRE(WEXITSTATUS(status) == 3);
703 /* Read the pid of the fork parent. */
704 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
705 sizeof(children[0]));
707 /* Attach to the fork parent. */
708 attach_child(children[0]);
710 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
712 /* Continue the fork parent ignoring the SIGSTOP. */
713 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
715 /* Signal the fork parent to continue. */
718 children[1] = handle_fork_events(children[0]);
719 ATF_REQUIRE(children[1] > 0);
721 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
722 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
725 * The fork parent can't exit until the child reports status,
726 * so the child should report its exit first to the debugger.
728 wpid = wait(&status);
729 ATF_REQUIRE(wpid == children[1]);
730 ATF_REQUIRE(WIFEXITED(status));
731 ATF_REQUIRE(WEXITSTATUS(status) == 2);
733 wpid = wait(&status);
734 ATF_REQUIRE(wpid == children[0]);
735 ATF_REQUIRE(WIFEXITED(status));
736 ATF_REQUIRE(WEXITSTATUS(status) == 1);
738 wpid = wait(&status);
739 ATF_REQUIRE(wpid == -1);
740 ATF_REQUIRE(errno == ECHILD);
744 * Verify that a new child process is stopped after a followed fork
745 * and that the traced parent sees the exit of the child when the new
746 * child process is detached after it reports its fork. In this test
747 * the parent that forks is not a direct child of the debugger.
749 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
750 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
752 pid_t children[0], fpid, wpid;
753 int cpipe[2], status;
755 ATF_REQUIRE(pipe(cpipe) == 0);
756 ATF_REQUIRE((fpid = fork()) != -1);
758 attach_fork_parent(cpipe);
759 follow_fork_parent();
762 /* Parent process. */
765 /* Wait for the direct child to exit. */
766 wpid = waitpid(fpid, &status, 0);
767 ATF_REQUIRE(wpid == fpid);
768 ATF_REQUIRE(WIFEXITED(status));
769 ATF_REQUIRE(WEXITSTATUS(status) == 3);
771 /* Read the pid of the fork parent. */
772 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
773 sizeof(children[0]));
775 /* Attach to the fork parent. */
776 attach_child(children[0]);
778 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
780 /* Continue the fork parent ignoring the SIGSTOP. */
781 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
783 /* Signal the fork parent to continue. */
786 children[1] = handle_fork_events(children[0]);
787 ATF_REQUIRE(children[1] > 0);
789 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
790 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
793 * Should not see any status from the child now, only the fork
796 wpid = wait(&status);
797 ATF_REQUIRE(wpid == children[0]);
798 ATF_REQUIRE(WIFEXITED(status));
799 ATF_REQUIRE(WEXITSTATUS(status) == 1);
801 wpid = wait(&status);
802 ATF_REQUIRE(wpid == -1);
803 ATF_REQUIRE(errno == ECHILD);
807 * Verify that a new child process is stopped after a followed fork
808 * and that the traced parent sees the exit of the child when the
809 * traced parent is detached after the fork. In this test the parent
810 * that forks is not a direct child of the debugger.
812 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
813 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
815 pid_t children[0], fpid, wpid;
816 int cpipe[2], status;
818 ATF_REQUIRE(pipe(cpipe) == 0);
819 ATF_REQUIRE((fpid = fork()) != -1);
821 attach_fork_parent(cpipe);
822 follow_fork_parent();
825 /* Parent process. */
828 /* Wait for the direct child to exit. */
829 wpid = waitpid(fpid, &status, 0);
830 ATF_REQUIRE(wpid == fpid);
831 ATF_REQUIRE(WIFEXITED(status));
832 ATF_REQUIRE(WEXITSTATUS(status) == 3);
834 /* Read the pid of the fork parent. */
835 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
836 sizeof(children[0]));
838 /* Attach to the fork parent. */
839 attach_child(children[0]);
841 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
843 /* Continue the fork parent ignoring the SIGSTOP. */
844 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
846 /* Signal the fork parent to continue. */
849 children[1] = handle_fork_events(children[0]);
850 ATF_REQUIRE(children[1] > 0);
852 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
853 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
856 * Should not see any status from the fork parent now, only
859 wpid = wait(&status);
860 ATF_REQUIRE(wpid == children[1]);
861 ATF_REQUIRE(WIFEXITED(status));
862 ATF_REQUIRE(WEXITSTATUS(status) == 2);
864 wpid = wait(&status);
865 ATF_REQUIRE(wpid == -1);
866 ATF_REQUIRE(errno == ECHILD);
872 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
873 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
874 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
875 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
876 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
877 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
878 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
879 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
881 ptrace__follow_fork_child_detached_unrelated_debugger);
883 ptrace__follow_fork_parent_detached_unrelated_debugger);
885 return (atf_no_error());