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/param.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
34 #include <sys/ptrace.h>
35 #include <sys/queue.h>
37 #include <sys/syscall.h>
38 #include <sys/sysctl.h>
42 #include <machine/cpufunc.h>
45 #include <semaphore.h>
53 * A variant of ATF_REQUIRE that is suitable for use in child
54 * processes. This only works if the parent process is tripped up by
55 * the early exit and fails some requirement itself.
57 #define CHILD_REQUIRE(exp) do { \
59 child_fail_require(__FILE__, __LINE__, \
64 child_fail_require(const char *file, int line, const char *str)
68 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
69 write(2, buf, strlen(buf));
77 /* Attach the parent process as a tracer of this process. */
78 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
85 attach_child(pid_t pid)
90 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
92 wpid = waitpid(pid, &status, 0);
93 ATF_REQUIRE(wpid == pid);
94 ATF_REQUIRE(WIFSTOPPED(status));
95 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
99 wait_for_zombie(pid_t pid)
103 * Wait for a process to exit. This is kind of gross, but
104 * there is not a better way.
107 struct kinfo_proc kp;
113 mib[2] = KERN_PROC_PID;
116 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
117 /* The KERN_PROC_PID sysctl fails for zombies. */
118 ATF_REQUIRE(errno == ESRCH);
126 * Verify that a parent debugger process "sees" the exit of a debugged
127 * process exactly once when attached via PT_TRACE_ME.
129 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
130 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
135 ATF_REQUIRE((child = fork()) != -1);
143 /* Parent process. */
145 /* The first wait() should report the stop from SIGSTOP. */
146 wpid = waitpid(child, &status, 0);
147 ATF_REQUIRE(wpid == child);
148 ATF_REQUIRE(WIFSTOPPED(status));
149 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
151 /* Continue the child ignoring the SIGSTOP. */
152 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
154 /* The second wait() should report the exit status. */
155 wpid = waitpid(child, &status, 0);
156 ATF_REQUIRE(wpid == child);
157 ATF_REQUIRE(WIFEXITED(status));
158 ATF_REQUIRE(WEXITSTATUS(status) == 1);
160 /* The child should no longer exist. */
161 wpid = waitpid(child, &status, 0);
162 ATF_REQUIRE(wpid == -1);
163 ATF_REQUIRE(errno == ECHILD);
167 * Verify that a parent debugger process "sees" the exit of a debugged
168 * process exactly once when attached via PT_ATTACH.
170 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
171 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
174 int cpipe[2], status;
177 ATF_REQUIRE(pipe(cpipe) == 0);
178 ATF_REQUIRE((child = fork()) != -1);
183 /* Wait for the parent to attach. */
184 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
190 /* Parent process. */
192 /* Attach to the child process. */
195 /* Continue the child ignoring the SIGSTOP. */
196 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
198 /* Signal the child to exit. */
201 /* The second wait() should report the exit status. */
202 wpid = waitpid(child, &status, 0);
203 ATF_REQUIRE(wpid == child);
204 ATF_REQUIRE(WIFEXITED(status));
205 ATF_REQUIRE(WEXITSTATUS(status) == 1);
207 /* The child should no longer exist. */
208 wpid = waitpid(child, &status, 0);
209 ATF_REQUIRE(wpid == -1);
210 ATF_REQUIRE(errno == ECHILD);
214 * Verify that a parent process "sees" the exit of a debugged process only
215 * after the debugger has seen it.
217 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
218 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
220 pid_t child, debugger, wpid;
221 int cpipe[2], dpipe[2], status;
224 ATF_REQUIRE(pipe(cpipe) == 0);
225 ATF_REQUIRE((child = fork()) != -1);
231 /* Wait for parent to be ready. */
232 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
238 ATF_REQUIRE(pipe(dpipe) == 0);
239 ATF_REQUIRE((debugger = fork()) != -1);
242 /* Debugger process. */
245 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
247 wpid = waitpid(child, &status, 0);
248 CHILD_REQUIRE(wpid == child);
249 CHILD_REQUIRE(WIFSTOPPED(status));
250 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
252 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
254 /* Signal parent that debugger is attached. */
255 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
257 /* Wait for parent's failed wait. */
258 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
260 wpid = waitpid(child, &status, 0);
261 CHILD_REQUIRE(wpid == child);
262 CHILD_REQUIRE(WIFEXITED(status));
263 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
269 /* Parent process. */
271 /* Wait for the debugger to attach to the child. */
272 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
274 /* Release the child. */
275 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
276 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
279 wait_for_zombie(child);
282 * This wait should return a pid of 0 to indicate no status to
283 * report. The parent should see the child as non-exited
284 * until the debugger sees the exit.
286 wpid = waitpid(child, &status, WNOHANG);
287 ATF_REQUIRE(wpid == 0);
289 /* Signal the debugger to wait for the child. */
292 /* Wait for the debugger. */
293 wpid = waitpid(debugger, &status, 0);
294 ATF_REQUIRE(wpid == debugger);
295 ATF_REQUIRE(WIFEXITED(status));
296 ATF_REQUIRE(WEXITSTATUS(status) == 0);
298 /* The child process should now be ready. */
299 wpid = waitpid(child, &status, WNOHANG);
300 ATF_REQUIRE(wpid == child);
301 ATF_REQUIRE(WIFEXITED(status));
302 ATF_REQUIRE(WEXITSTATUS(status) == 1);
306 * Verify that a parent process "sees" the exit of a debugged process
307 * only after a non-direct-child debugger has seen it. In particular,
308 * various wait() calls in the parent must avoid failing with ESRCH by
309 * checking the parent's orphan list for the debugee.
311 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
312 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
314 pid_t child, debugger, fpid, wpid;
315 int cpipe[2], dpipe[2], status;
318 ATF_REQUIRE(pipe(cpipe) == 0);
319 ATF_REQUIRE((child = fork()) != -1);
325 /* Wait for parent to be ready. */
326 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
332 ATF_REQUIRE(pipe(dpipe) == 0);
333 ATF_REQUIRE((debugger = fork()) != -1);
336 /* Debugger parent. */
339 * Fork again and drop the debugger parent so that the
340 * debugger is not a child of the main parent.
342 CHILD_REQUIRE((fpid = fork()) != -1);
346 /* Debugger process. */
349 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
351 wpid = waitpid(child, &status, 0);
352 CHILD_REQUIRE(wpid == child);
353 CHILD_REQUIRE(WIFSTOPPED(status));
354 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
356 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
358 /* Signal parent that debugger is attached. */
359 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
361 /* Wait for parent's failed wait. */
362 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
364 wpid = waitpid(child, &status, 0);
365 CHILD_REQUIRE(wpid == child);
366 CHILD_REQUIRE(WIFEXITED(status));
367 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
373 /* Parent process. */
375 /* Wait for the debugger parent process to exit. */
376 wpid = waitpid(debugger, &status, 0);
377 ATF_REQUIRE(wpid == debugger);
378 ATF_REQUIRE(WIFEXITED(status));
379 ATF_REQUIRE(WEXITSTATUS(status) == 2);
381 /* A WNOHANG wait here should see the non-exited child. */
382 wpid = waitpid(child, &status, WNOHANG);
383 ATF_REQUIRE(wpid == 0);
385 /* Wait for the debugger to attach to the child. */
386 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
388 /* Release the child. */
389 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
390 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
393 wait_for_zombie(child);
396 * This wait should return a pid of 0 to indicate no status to
397 * report. The parent should see the child as non-exited
398 * until the debugger sees the exit.
400 wpid = waitpid(child, &status, WNOHANG);
401 ATF_REQUIRE(wpid == 0);
403 /* Signal the debugger to wait for the child. */
404 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
406 /* Wait for the debugger. */
407 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
410 /* The child process should now be ready. */
411 wpid = waitpid(child, &status, WNOHANG);
412 ATF_REQUIRE(wpid == child);
413 ATF_REQUIRE(WIFEXITED(status));
414 ATF_REQUIRE(WEXITSTATUS(status) == 1);
418 * The parent process should always act the same regardless of how the
419 * debugger is attached to it.
422 follow_fork_parent(bool use_vfork)
428 CHILD_REQUIRE((fpid = vfork()) != -1);
430 CHILD_REQUIRE((fpid = fork()) != -1);
436 wpid = waitpid(fpid, &status, 0);
437 CHILD_REQUIRE(wpid == fpid);
438 CHILD_REQUIRE(WIFEXITED(status));
439 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
445 * Helper routine for follow fork tests. This waits for two stops
446 * that report both "sides" of a fork. It returns the pid of the new
450 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
452 struct ptrace_lwpinfo pl;
453 bool fork_reported[2];
457 fork_reported[0] = false;
458 fork_reported[1] = false;
462 * Each process should report a fork event. The parent should
463 * report a PL_FLAG_FORKED event, and the child should report
464 * a PL_FLAG_CHILD event.
466 for (i = 0; i < 2; i++) {
467 wpid = wait(&status);
468 ATF_REQUIRE(wpid > 0);
469 ATF_REQUIRE(WIFSTOPPED(status));
471 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
473 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
475 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
476 (PL_FLAG_FORKED | PL_FLAG_CHILD));
477 if (pl.pl_flags & PL_FLAG_CHILD) {
478 ATF_REQUIRE(wpid != parent);
479 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
480 ATF_REQUIRE(!fork_reported[1]);
484 ATF_REQUIRE(child == wpid);
487 fork_reported[1] = true;
489 ATF_REQUIRE(wpid == parent);
490 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
491 ATF_REQUIRE(!fork_reported[0]);
493 child = pl.pl_child_pid;
495 ATF_REQUIRE(child == pl.pl_child_pid);
498 fork_reported[0] = true;
506 * Verify that a new child process is stopped after a followed fork and
507 * that the traced parent sees the exit of the child after the debugger
508 * when both processes remain attached to the debugger.
510 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
511 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
513 pid_t children[0], fpid, wpid;
516 ATF_REQUIRE((fpid = fork()) != -1);
519 follow_fork_parent(false);
522 /* Parent process. */
525 /* The first wait() should report the stop from SIGSTOP. */
526 wpid = waitpid(children[0], &status, 0);
527 ATF_REQUIRE(wpid == children[0]);
528 ATF_REQUIRE(WIFSTOPPED(status));
529 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
531 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
533 /* Continue the child ignoring the SIGSTOP. */
534 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
536 children[1] = handle_fork_events(children[0], NULL);
537 ATF_REQUIRE(children[1] > 0);
539 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
540 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
543 * The child can't exit until the grandchild reports status, so the
544 * grandchild should report its exit first to the debugger.
546 wpid = wait(&status);
547 ATF_REQUIRE(wpid == children[1]);
548 ATF_REQUIRE(WIFEXITED(status));
549 ATF_REQUIRE(WEXITSTATUS(status) == 2);
551 wpid = wait(&status);
552 ATF_REQUIRE(wpid == children[0]);
553 ATF_REQUIRE(WIFEXITED(status));
554 ATF_REQUIRE(WEXITSTATUS(status) == 1);
556 wpid = wait(&status);
557 ATF_REQUIRE(wpid == -1);
558 ATF_REQUIRE(errno == ECHILD);
562 * Verify that a new child process is stopped after a followed fork
563 * and that the traced parent sees the exit of the child when the new
564 * child process is detached after it reports its fork.
566 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
567 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
569 pid_t children[0], fpid, wpid;
572 ATF_REQUIRE((fpid = fork()) != -1);
575 follow_fork_parent(false);
578 /* Parent process. */
581 /* The first wait() should report the stop from SIGSTOP. */
582 wpid = waitpid(children[0], &status, 0);
583 ATF_REQUIRE(wpid == children[0]);
584 ATF_REQUIRE(WIFSTOPPED(status));
585 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
587 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
589 /* Continue the child ignoring the SIGSTOP. */
590 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
592 children[1] = handle_fork_events(children[0], NULL);
593 ATF_REQUIRE(children[1] > 0);
595 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
596 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
599 * Should not see any status from the grandchild now, only the
602 wpid = wait(&status);
603 ATF_REQUIRE(wpid == children[0]);
604 ATF_REQUIRE(WIFEXITED(status));
605 ATF_REQUIRE(WEXITSTATUS(status) == 1);
607 wpid = wait(&status);
608 ATF_REQUIRE(wpid == -1);
609 ATF_REQUIRE(errno == ECHILD);
613 * Verify that a new child process is stopped after a followed fork
614 * and that the traced parent sees the exit of the child when the
615 * traced parent is detached after the fork.
617 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
618 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
620 pid_t children[0], fpid, wpid;
623 ATF_REQUIRE((fpid = fork()) != -1);
626 follow_fork_parent(false);
629 /* Parent process. */
632 /* The first wait() should report the stop from SIGSTOP. */
633 wpid = waitpid(children[0], &status, 0);
634 ATF_REQUIRE(wpid == children[0]);
635 ATF_REQUIRE(WIFSTOPPED(status));
636 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
638 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
640 /* Continue the child ignoring the SIGSTOP. */
641 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
643 children[1] = handle_fork_events(children[0], NULL);
644 ATF_REQUIRE(children[1] > 0);
646 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
647 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
650 * The child can't exit until the grandchild reports status, so the
651 * grandchild should report its exit first to the debugger.
653 * Even though the child process is detached, it is still a
654 * child of the debugger, so it will still report it's exit
655 * after the grandchild.
657 wpid = wait(&status);
658 ATF_REQUIRE(wpid == children[1]);
659 ATF_REQUIRE(WIFEXITED(status));
660 ATF_REQUIRE(WEXITSTATUS(status) == 2);
662 wpid = wait(&status);
663 ATF_REQUIRE(wpid == children[0]);
664 ATF_REQUIRE(WIFEXITED(status));
665 ATF_REQUIRE(WEXITSTATUS(status) == 1);
667 wpid = wait(&status);
668 ATF_REQUIRE(wpid == -1);
669 ATF_REQUIRE(errno == ECHILD);
673 attach_fork_parent(int cpipe[2])
679 /* Double-fork to disassociate from the debugger. */
680 CHILD_REQUIRE((fpid = fork()) != -1);
684 /* Send the pid of the disassociated child to the debugger. */
686 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
688 /* Wait for the debugger to attach. */
689 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
693 * Verify that a new child process is stopped after a followed fork and
694 * that the traced parent sees the exit of the child after the debugger
695 * when both processes remain attached to the debugger. In this test
696 * the parent that forks is not a direct child of the debugger.
698 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
699 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
701 pid_t children[0], fpid, wpid;
702 int cpipe[2], status;
704 ATF_REQUIRE(pipe(cpipe) == 0);
705 ATF_REQUIRE((fpid = fork()) != -1);
707 attach_fork_parent(cpipe);
708 follow_fork_parent(false);
711 /* Parent process. */
714 /* Wait for the direct child to exit. */
715 wpid = waitpid(fpid, &status, 0);
716 ATF_REQUIRE(wpid == fpid);
717 ATF_REQUIRE(WIFEXITED(status));
718 ATF_REQUIRE(WEXITSTATUS(status) == 3);
720 /* Read the pid of the fork parent. */
721 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
722 sizeof(children[0]));
724 /* Attach to the fork parent. */
725 attach_child(children[0]);
727 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
729 /* Continue the fork parent ignoring the SIGSTOP. */
730 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
732 /* Signal the fork parent to continue. */
735 children[1] = handle_fork_events(children[0], NULL);
736 ATF_REQUIRE(children[1] > 0);
738 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
739 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
742 * The fork parent can't exit until the child reports status,
743 * so the child should report its exit first to the debugger.
745 wpid = wait(&status);
746 ATF_REQUIRE(wpid == children[1]);
747 ATF_REQUIRE(WIFEXITED(status));
748 ATF_REQUIRE(WEXITSTATUS(status) == 2);
750 wpid = wait(&status);
751 ATF_REQUIRE(wpid == children[0]);
752 ATF_REQUIRE(WIFEXITED(status));
753 ATF_REQUIRE(WEXITSTATUS(status) == 1);
755 wpid = wait(&status);
756 ATF_REQUIRE(wpid == -1);
757 ATF_REQUIRE(errno == ECHILD);
761 * Verify that a new child process is stopped after a followed fork
762 * and that the traced parent sees the exit of the child when the new
763 * child process is detached after it reports its fork. In this test
764 * the parent that forks is not a direct child of the debugger.
766 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
767 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
769 pid_t children[0], fpid, wpid;
770 int cpipe[2], status;
772 ATF_REQUIRE(pipe(cpipe) == 0);
773 ATF_REQUIRE((fpid = fork()) != -1);
775 attach_fork_parent(cpipe);
776 follow_fork_parent(false);
779 /* Parent process. */
782 /* Wait for the direct child to exit. */
783 wpid = waitpid(fpid, &status, 0);
784 ATF_REQUIRE(wpid == fpid);
785 ATF_REQUIRE(WIFEXITED(status));
786 ATF_REQUIRE(WEXITSTATUS(status) == 3);
788 /* Read the pid of the fork parent. */
789 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
790 sizeof(children[0]));
792 /* Attach to the fork parent. */
793 attach_child(children[0]);
795 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
797 /* Continue the fork parent ignoring the SIGSTOP. */
798 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
800 /* Signal the fork parent to continue. */
803 children[1] = handle_fork_events(children[0], NULL);
804 ATF_REQUIRE(children[1] > 0);
806 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
807 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
810 * Should not see any status from the child now, only the fork
813 wpid = wait(&status);
814 ATF_REQUIRE(wpid == children[0]);
815 ATF_REQUIRE(WIFEXITED(status));
816 ATF_REQUIRE(WEXITSTATUS(status) == 1);
818 wpid = wait(&status);
819 ATF_REQUIRE(wpid == -1);
820 ATF_REQUIRE(errno == ECHILD);
824 * Verify that a new child process is stopped after a followed fork
825 * and that the traced parent sees the exit of the child when the
826 * traced parent is detached after the fork. In this test the parent
827 * that forks is not a direct child of the debugger.
829 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
830 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
832 pid_t children[0], fpid, wpid;
833 int cpipe[2], status;
835 ATF_REQUIRE(pipe(cpipe) == 0);
836 ATF_REQUIRE((fpid = fork()) != -1);
838 attach_fork_parent(cpipe);
839 follow_fork_parent(false);
842 /* Parent process. */
845 /* Wait for the direct child to exit. */
846 wpid = waitpid(fpid, &status, 0);
847 ATF_REQUIRE(wpid == fpid);
848 ATF_REQUIRE(WIFEXITED(status));
849 ATF_REQUIRE(WEXITSTATUS(status) == 3);
851 /* Read the pid of the fork parent. */
852 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
853 sizeof(children[0]));
855 /* Attach to the fork parent. */
856 attach_child(children[0]);
858 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
860 /* Continue the fork parent ignoring the SIGSTOP. */
861 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
863 /* Signal the fork parent to continue. */
866 children[1] = handle_fork_events(children[0], NULL);
867 ATF_REQUIRE(children[1] > 0);
869 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
870 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
873 * Should not see any status from the fork parent now, only
876 wpid = wait(&status);
877 ATF_REQUIRE(wpid == children[1]);
878 ATF_REQUIRE(WIFEXITED(status));
879 ATF_REQUIRE(WEXITSTATUS(status) == 2);
881 wpid = wait(&status);
882 ATF_REQUIRE(wpid == -1);
883 ATF_REQUIRE(errno == ECHILD);
887 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
888 * child process created via fork() reports the correct value.
890 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
891 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
893 struct ptrace_lwpinfo pl[2];
894 pid_t children[2], fpid, wpid;
897 ATF_REQUIRE((fpid = fork()) != -1);
900 follow_fork_parent(false);
903 /* Parent process. */
906 /* The first wait() should report the stop from SIGSTOP. */
907 wpid = waitpid(children[0], &status, 0);
908 ATF_REQUIRE(wpid == children[0]);
909 ATF_REQUIRE(WIFSTOPPED(status));
910 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
912 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
914 /* Continue the child ignoring the SIGSTOP. */
915 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
917 /* Wait for both halves of the fork event to get reported. */
918 children[1] = handle_fork_events(children[0], pl);
919 ATF_REQUIRE(children[1] > 0);
921 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
922 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
923 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
924 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
925 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
927 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
928 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
931 * The child can't exit until the grandchild reports status, so the
932 * grandchild should report its exit first to the debugger.
934 wpid = wait(&status);
935 ATF_REQUIRE(wpid == children[1]);
936 ATF_REQUIRE(WIFEXITED(status));
937 ATF_REQUIRE(WEXITSTATUS(status) == 2);
939 wpid = wait(&status);
940 ATF_REQUIRE(wpid == children[0]);
941 ATF_REQUIRE(WIFEXITED(status));
942 ATF_REQUIRE(WEXITSTATUS(status) == 1);
944 wpid = wait(&status);
945 ATF_REQUIRE(wpid == -1);
946 ATF_REQUIRE(errno == ECHILD);
950 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
951 * child process created via vfork() reports the correct value.
953 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
954 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
956 struct ptrace_lwpinfo pl[2];
957 pid_t children[2], fpid, wpid;
960 ATF_REQUIRE((fpid = fork()) != -1);
963 follow_fork_parent(true);
966 /* Parent process. */
969 /* The first wait() should report the stop from SIGSTOP. */
970 wpid = waitpid(children[0], &status, 0);
971 ATF_REQUIRE(wpid == children[0]);
972 ATF_REQUIRE(WIFSTOPPED(status));
973 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
975 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
977 /* Continue the child ignoring the SIGSTOP. */
978 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
980 /* Wait for both halves of the fork event to get reported. */
981 children[1] = handle_fork_events(children[0], pl);
982 ATF_REQUIRE(children[1] > 0);
984 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
985 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
986 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
987 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
988 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
990 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
991 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
994 * The child can't exit until the grandchild reports status, so the
995 * grandchild should report its exit first to the debugger.
997 wpid = wait(&status);
998 ATF_REQUIRE(wpid == children[1]);
999 ATF_REQUIRE(WIFEXITED(status));
1000 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1002 wpid = wait(&status);
1003 ATF_REQUIRE(wpid == children[0]);
1004 ATF_REQUIRE(WIFEXITED(status));
1005 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1007 wpid = wait(&status);
1008 ATF_REQUIRE(wpid == -1);
1009 ATF_REQUIRE(errno == ECHILD);
1013 simple_thread(void *arg __unused)
1020 simple_thread_main(void)
1024 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1025 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1030 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1031 * thread reports the correct value.
1033 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1034 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1036 struct ptrace_lwpinfo pl;
1041 ATF_REQUIRE((fpid = fork()) != -1);
1044 simple_thread_main();
1047 /* The first wait() should report the stop from SIGSTOP. */
1048 wpid = waitpid(fpid, &status, 0);
1049 ATF_REQUIRE(wpid == fpid);
1050 ATF_REQUIRE(WIFSTOPPED(status));
1051 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1053 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1055 mainlwp = pl.pl_lwpid;
1058 * Continue the child ignoring the SIGSTOP and tracing all
1059 * system call exits.
1061 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1064 * Wait for the new thread to arrive. pthread_create() might
1065 * invoke any number of system calls. For now we just wait
1066 * for the new thread to arrive and make sure it reports a
1067 * valid system call code. If ptrace grows thread event
1068 * reporting then this test can be made more precise.
1071 wpid = waitpid(fpid, &status, 0);
1072 ATF_REQUIRE(wpid == fpid);
1073 ATF_REQUIRE(WIFSTOPPED(status));
1074 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1076 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1078 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1079 ATF_REQUIRE(pl.pl_syscall_code != 0);
1080 if (pl.pl_lwpid != mainlwp)
1081 /* New thread seen. */
1084 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1087 /* Wait for the child to exit. */
1088 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1090 wpid = waitpid(fpid, &status, 0);
1091 ATF_REQUIRE(wpid == fpid);
1092 if (WIFEXITED(status))
1095 ATF_REQUIRE(WIFSTOPPED(status));
1096 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1097 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1100 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1102 wpid = wait(&status);
1103 ATF_REQUIRE(wpid == -1);
1104 ATF_REQUIRE(errno == ECHILD);
1108 * Verify that the expected LWP events are reported for a child thread.
1110 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1111 ATF_TC_BODY(ptrace__lwp_events, tc)
1113 struct ptrace_lwpinfo pl;
1118 ATF_REQUIRE((fpid = fork()) != -1);
1121 simple_thread_main();
1124 /* The first wait() should report the stop from SIGSTOP. */
1125 wpid = waitpid(fpid, &status, 0);
1126 ATF_REQUIRE(wpid == fpid);
1127 ATF_REQUIRE(WIFSTOPPED(status));
1128 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1130 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1132 lwps[0] = pl.pl_lwpid;
1134 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1136 /* Continue the child ignoring the SIGSTOP. */
1137 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1139 /* The first event should be for the child thread's birth. */
1140 wpid = waitpid(fpid, &status, 0);
1141 ATF_REQUIRE(wpid == fpid);
1142 ATF_REQUIRE(WIFSTOPPED(status));
1143 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1145 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1146 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1147 (PL_FLAG_BORN | PL_FLAG_SCX));
1148 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1149 lwps[1] = pl.pl_lwpid;
1151 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1153 /* The next event should be for the child thread's death. */
1154 wpid = waitpid(fpid, &status, 0);
1155 ATF_REQUIRE(wpid == fpid);
1156 ATF_REQUIRE(WIFSTOPPED(status));
1157 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1159 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1160 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1161 (PL_FLAG_EXITED | PL_FLAG_SCE));
1162 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1164 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1166 /* The last event should be for the child process's exit. */
1167 wpid = waitpid(fpid, &status, 0);
1168 ATF_REQUIRE(WIFEXITED(status));
1169 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1171 wpid = wait(&status);
1172 ATF_REQUIRE(wpid == -1);
1173 ATF_REQUIRE(errno == ECHILD);
1177 exec_thread(void *arg __unused)
1180 execl("/usr/bin/true", "true", NULL);
1185 exec_thread_main(void)
1189 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1196 * Verify that the expected LWP events are reported for a multithreaded
1197 * process that calls execve(2).
1199 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1200 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1202 struct ptrace_lwpinfo pl;
1207 ATF_REQUIRE((fpid = fork()) != -1);
1213 /* The first wait() should report the stop from SIGSTOP. */
1214 wpid = waitpid(fpid, &status, 0);
1215 ATF_REQUIRE(wpid == fpid);
1216 ATF_REQUIRE(WIFSTOPPED(status));
1217 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1219 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1221 lwps[0] = pl.pl_lwpid;
1223 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1225 /* Continue the child ignoring the SIGSTOP. */
1226 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1228 /* The first event should be for the child thread's birth. */
1229 wpid = waitpid(fpid, &status, 0);
1230 ATF_REQUIRE(wpid == fpid);
1231 ATF_REQUIRE(WIFSTOPPED(status));
1232 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1234 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1235 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1236 (PL_FLAG_BORN | PL_FLAG_SCX));
1237 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1238 lwps[1] = pl.pl_lwpid;
1240 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1243 * The next event should be for the main thread's death due to
1244 * single threading from execve().
1246 wpid = waitpid(fpid, &status, 0);
1247 ATF_REQUIRE(wpid == fpid);
1248 ATF_REQUIRE(WIFSTOPPED(status));
1249 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1251 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1252 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1254 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1256 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1258 /* The next event should be for the child process's exec. */
1259 wpid = waitpid(fpid, &status, 0);
1260 ATF_REQUIRE(WIFSTOPPED(status));
1261 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1263 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1264 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1265 (PL_FLAG_EXEC | PL_FLAG_SCX));
1266 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1268 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1270 /* The last event should be for the child process's exit. */
1271 wpid = waitpid(fpid, &status, 0);
1272 ATF_REQUIRE(WIFEXITED(status));
1273 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1275 wpid = wait(&status);
1276 ATF_REQUIRE(wpid == -1);
1277 ATF_REQUIRE(errno == ECHILD);
1281 handler(int sig __unused)
1289 signal(SIGINFO, handler);
1295 * Verify that the expected ptrace event is reported for a signal.
1297 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1298 ATF_TC_BODY(ptrace__siginfo, tc)
1300 struct ptrace_lwpinfo pl;
1304 ATF_REQUIRE((fpid = fork()) != -1);
1310 /* The first wait() should report the stop from SIGSTOP. */
1311 wpid = waitpid(fpid, &status, 0);
1312 ATF_REQUIRE(wpid == fpid);
1313 ATF_REQUIRE(WIFSTOPPED(status));
1314 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1316 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1318 /* The next event should be for the SIGINFO. */
1319 wpid = waitpid(fpid, &status, 0);
1320 ATF_REQUIRE(WIFSTOPPED(status));
1321 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1323 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1324 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1325 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1326 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1327 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1329 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1331 /* The last event should be for the child process's exit. */
1332 wpid = waitpid(fpid, &status, 0);
1333 ATF_REQUIRE(WIFEXITED(status));
1334 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1336 wpid = wait(&status);
1337 ATF_REQUIRE(wpid == -1);
1338 ATF_REQUIRE(errno == ECHILD);
1342 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1344 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1345 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1350 ATF_REQUIRE((fpid = fork()) != -1);
1356 /* The first wait() should report the stop from SIGSTOP. */
1357 wpid = waitpid(fpid, &status, 0);
1358 ATF_REQUIRE(wpid == fpid);
1359 ATF_REQUIRE(WIFSTOPPED(status));
1360 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1363 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1364 sizeof(events)) == 0);
1366 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1368 /* Should get one event at exit. */
1369 wpid = waitpid(fpid, &status, 0);
1370 ATF_REQUIRE(WIFEXITED(status));
1371 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1373 wpid = wait(&status);
1374 ATF_REQUIRE(wpid == -1);
1375 ATF_REQUIRE(errno == ECHILD);
1378 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1379 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1381 struct ptrace_lwpinfo pl;
1385 ATF_REQUIRE((fpid = fork()) != -1);
1391 /* The first wait() should report the stop from SIGSTOP. */
1392 wpid = waitpid(fpid, &status, 0);
1393 ATF_REQUIRE(wpid == fpid);
1394 ATF_REQUIRE(WIFSTOPPED(status));
1395 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1397 events = PTRACE_EXEC;
1398 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1399 sizeof(events)) == 0);
1401 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1403 /* The next event should be for the child process's exec. */
1404 wpid = waitpid(fpid, &status, 0);
1405 ATF_REQUIRE(WIFSTOPPED(status));
1406 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1408 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1409 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1410 (PL_FLAG_EXEC | PL_FLAG_SCX));
1412 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1414 /* The last event should be for the child process's exit. */
1415 wpid = waitpid(fpid, &status, 0);
1416 ATF_REQUIRE(WIFEXITED(status));
1417 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1419 wpid = wait(&status);
1420 ATF_REQUIRE(wpid == -1);
1421 ATF_REQUIRE(errno == ECHILD);
1424 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1425 ATF_TC_BODY(ptrace__event_mask, tc)
1430 ATF_REQUIRE((fpid = fork()) != -1);
1436 /* The first wait() should report the stop from SIGSTOP. */
1437 wpid = waitpid(fpid, &status, 0);
1438 ATF_REQUIRE(wpid == fpid);
1439 ATF_REQUIRE(WIFSTOPPED(status));
1440 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1442 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1443 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1444 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1445 sizeof(events)) == 0);
1446 ATF_REQUIRE(events & PTRACE_FORK);
1447 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1448 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1449 sizeof(events)) == 0);
1450 ATF_REQUIRE(!(events & PTRACE_FORK));
1452 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1453 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1454 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1455 sizeof(events)) == 0);
1456 ATF_REQUIRE(events & PTRACE_LWP);
1457 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1458 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1459 sizeof(events)) == 0);
1460 ATF_REQUIRE(!(events & PTRACE_LWP));
1462 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1464 /* Should get one event at exit. */
1465 wpid = waitpid(fpid, &status, 0);
1466 ATF_REQUIRE(WIFEXITED(status));
1467 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1469 wpid = wait(&status);
1470 ATF_REQUIRE(wpid == -1);
1471 ATF_REQUIRE(errno == ECHILD);
1475 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1477 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1478 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1480 struct ptrace_lwpinfo pl;
1484 ATF_REQUIRE((fpid = fork()) != -1);
1487 follow_fork_parent(true);
1490 /* The first wait() should report the stop from SIGSTOP. */
1491 wpid = waitpid(fpid, &status, 0);
1492 ATF_REQUIRE(wpid == fpid);
1493 ATF_REQUIRE(WIFSTOPPED(status));
1494 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1496 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1497 sizeof(events)) == 0);
1498 events |= PTRACE_VFORK;
1499 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1500 sizeof(events)) == 0);
1502 /* Continue the child ignoring the SIGSTOP. */
1503 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1505 /* The next event should report the end of the vfork. */
1506 wpid = wait(&status);
1507 ATF_REQUIRE(wpid == fpid);
1508 ATF_REQUIRE(WIFSTOPPED(status));
1509 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1510 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1511 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1513 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1515 wpid = wait(&status);
1516 ATF_REQUIRE(wpid == fpid);
1517 ATF_REQUIRE(WIFEXITED(status));
1518 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1520 wpid = wait(&status);
1521 ATF_REQUIRE(wpid == -1);
1522 ATF_REQUIRE(errno == ECHILD);
1525 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1526 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1528 struct ptrace_lwpinfo pl[2];
1529 pid_t children[2], fpid, wpid;
1532 ATF_REQUIRE((fpid = fork()) != -1);
1535 follow_fork_parent(true);
1538 /* Parent process. */
1541 /* The first wait() should report the stop from SIGSTOP. */
1542 wpid = waitpid(children[0], &status, 0);
1543 ATF_REQUIRE(wpid == children[0]);
1544 ATF_REQUIRE(WIFSTOPPED(status));
1545 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1547 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1548 sizeof(events)) == 0);
1549 events |= PTRACE_FORK | PTRACE_VFORK;
1550 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1551 sizeof(events)) == 0);
1553 /* Continue the child ignoring the SIGSTOP. */
1554 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1556 /* Wait for both halves of the fork event to get reported. */
1557 children[1] = handle_fork_events(children[0], pl);
1558 ATF_REQUIRE(children[1] > 0);
1560 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1562 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1563 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1566 * The child can't exit until the grandchild reports status, so the
1567 * grandchild should report its exit first to the debugger.
1569 wpid = waitpid(children[1], &status, 0);
1570 ATF_REQUIRE(wpid == children[1]);
1571 ATF_REQUIRE(WIFEXITED(status));
1572 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1575 * The child should report it's vfork() completion before it
1578 wpid = wait(&status);
1579 ATF_REQUIRE(wpid == children[0]);
1580 ATF_REQUIRE(WIFSTOPPED(status));
1581 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1582 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1584 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1586 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1588 wpid = wait(&status);
1589 ATF_REQUIRE(wpid == children[0]);
1590 ATF_REQUIRE(WIFEXITED(status));
1591 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1593 wpid = wait(&status);
1594 ATF_REQUIRE(wpid == -1);
1595 ATF_REQUIRE(errno == ECHILD);
1599 * XXX: There's nothing inherently platform specific about this test, however a
1600 * userspace visible breakpoint() is a prerequisite.
1602 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1604 * Verify that no more events are reported after PT_KILL except for the
1605 * process exit when stopped due to a breakpoint trap.
1607 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1608 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1613 ATF_REQUIRE((fpid = fork()) != -1);
1620 /* The first wait() should report the stop from SIGSTOP. */
1621 wpid = waitpid(fpid, &status, 0);
1622 ATF_REQUIRE(wpid == fpid);
1623 ATF_REQUIRE(WIFSTOPPED(status));
1624 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1626 /* Continue the child ignoring the SIGSTOP. */
1627 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1629 /* The second wait() should report hitting the breakpoint. */
1630 wpid = waitpid(fpid, &status, 0);
1631 ATF_REQUIRE(wpid == fpid);
1632 ATF_REQUIRE(WIFSTOPPED(status));
1633 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1635 /* Kill the child process. */
1636 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1638 /* The last wait() should report the SIGKILL. */
1639 wpid = waitpid(fpid, &status, 0);
1640 ATF_REQUIRE(wpid == fpid);
1641 ATF_REQUIRE(WIFSIGNALED(status));
1642 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1644 wpid = wait(&status);
1645 ATF_REQUIRE(wpid == -1);
1646 ATF_REQUIRE(errno == ECHILD);
1648 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1651 * Verify that no more events are reported after PT_KILL except for the
1652 * process exit when stopped inside of a system call.
1654 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1655 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1657 struct ptrace_lwpinfo pl;
1661 ATF_REQUIRE((fpid = fork()) != -1);
1668 /* The first wait() should report the stop from SIGSTOP. */
1669 wpid = waitpid(fpid, &status, 0);
1670 ATF_REQUIRE(wpid == fpid);
1671 ATF_REQUIRE(WIFSTOPPED(status));
1672 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1674 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1675 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1677 /* The second wait() should report a system call entry for getpid(). */
1678 wpid = waitpid(fpid, &status, 0);
1679 ATF_REQUIRE(wpid == fpid);
1680 ATF_REQUIRE(WIFSTOPPED(status));
1681 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1683 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1684 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1686 /* Kill the child process. */
1687 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1689 /* The last wait() should report the SIGKILL. */
1690 wpid = waitpid(fpid, &status, 0);
1691 ATF_REQUIRE(wpid == fpid);
1692 ATF_REQUIRE(WIFSIGNALED(status));
1693 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1695 wpid = wait(&status);
1696 ATF_REQUIRE(wpid == -1);
1697 ATF_REQUIRE(errno == ECHILD);
1701 * Verify that no more events are reported after PT_KILL except for the
1702 * process exit when killing a multithreaded process.
1704 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1705 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1707 struct ptrace_lwpinfo pl;
1712 ATF_REQUIRE((fpid = fork()) != -1);
1715 simple_thread_main();
1718 /* The first wait() should report the stop from SIGSTOP. */
1719 wpid = waitpid(fpid, &status, 0);
1720 ATF_REQUIRE(wpid == fpid);
1721 ATF_REQUIRE(WIFSTOPPED(status));
1722 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1724 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1726 main_lwp = pl.pl_lwpid;
1728 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1730 /* Continue the child ignoring the SIGSTOP. */
1731 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1733 /* The first event should be for the child thread's birth. */
1734 wpid = waitpid(fpid, &status, 0);
1735 ATF_REQUIRE(wpid == fpid);
1736 ATF_REQUIRE(WIFSTOPPED(status));
1737 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1739 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1740 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1741 (PL_FLAG_BORN | PL_FLAG_SCX));
1742 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1744 /* Kill the child process. */
1745 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1747 /* The last wait() should report the SIGKILL. */
1748 wpid = waitpid(fpid, &status, 0);
1749 ATF_REQUIRE(wpid == fpid);
1750 ATF_REQUIRE(WIFSIGNALED(status));
1751 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1753 wpid = wait(&status);
1754 ATF_REQUIRE(wpid == -1);
1755 ATF_REQUIRE(errno == ECHILD);
1759 mask_usr1_thread(void *arg)
1761 pthread_barrier_t *pbarrier;
1764 pbarrier = (pthread_barrier_t*)arg;
1766 sigemptyset(&sigmask);
1767 sigaddset(&sigmask, SIGUSR1);
1768 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1770 /* Sync up with other thread after sigmask updated. */
1771 pthread_barrier_wait(pbarrier);
1780 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1781 * and prevents spurious stops due to those other signals.
1783 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1784 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1790 pthread_barrier_t barrier;
1791 struct sched_param sched_param;
1793 ATF_REQUIRE((fpid = fork()) != -1);
1795 /* Bind to one CPU so only one thread at a time will run. */
1797 CPU_SET(0, &setmask);
1799 CHILD_REQUIRE(cpuset(&setid) == 0);
1800 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1801 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1803 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1805 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1806 (void*)&barrier) == 0);
1809 * Give the main thread higher priority. The test always
1810 * assumes that, if both threads are able to run, the main
1811 * thread runs first.
1813 sched_param.sched_priority =
1814 (sched_get_priority_max(SCHED_FIFO) +
1815 sched_get_priority_min(SCHED_FIFO)) / 2;
1816 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1817 SCHED_FIFO, &sched_param) == 0);
1818 sched_param.sched_priority -= RQ_PPQ;
1819 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1820 &sched_param) == 0);
1823 sigemptyset(&sigmask);
1824 sigaddset(&sigmask, SIGUSR2);
1825 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1827 /* Sync up with other thread after sigmask updated. */
1828 pthread_barrier_wait(&barrier);
1838 /* The first wait() should report the stop from SIGSTOP. */
1839 wpid = waitpid(fpid, &status, 0);
1840 ATF_REQUIRE(wpid == fpid);
1841 ATF_REQUIRE(WIFSTOPPED(status));
1842 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1844 /* Continue the child ignoring the SIGSTOP. */
1845 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1847 /* Send a signal that only the second thread can handle. */
1848 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1850 /* The second wait() should report the SIGUSR2. */
1851 wpid = waitpid(fpid, &status, 0);
1852 ATF_REQUIRE(wpid == fpid);
1853 ATF_REQUIRE(WIFSTOPPED(status));
1854 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1856 /* Send a signal that only the first thread can handle. */
1857 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1859 /* Replace the SIGUSR2 with a kill. */
1860 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1862 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1863 wpid = waitpid(fpid, &status, 0);
1864 ATF_REQUIRE(wpid == fpid);
1865 ATF_REQUIRE(WIFSIGNALED(status));
1866 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1868 wpid = wait(&status);
1869 ATF_REQUIRE(wpid == -1);
1870 ATF_REQUIRE(errno == ECHILD);
1874 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1875 * and prevents spurious stops caused by those events.
1877 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1878 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1884 pthread_barrier_t barrier;
1886 struct ptrace_lwpinfo pl;
1887 struct sched_param sched_param;
1889 ATF_REQUIRE((fpid = fork()) != -1);
1893 /* Bind to one CPU so only one thread at a time will run. */
1895 CPU_SET(0, &setmask);
1897 CHILD_REQUIRE(cpuset(&setid) == 0);
1898 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1899 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1901 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1903 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1904 (void*)&barrier) == 0);
1907 * Give the main thread higher priority. The test always
1908 * assumes that, if both threads are able to run, the main
1909 * thread runs first.
1911 sched_param.sched_priority =
1912 (sched_get_priority_max(SCHED_FIFO) +
1913 sched_get_priority_min(SCHED_FIFO)) / 2;
1914 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1915 SCHED_FIFO, &sched_param) == 0);
1916 sched_param.sched_priority -= RQ_PPQ;
1917 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1918 &sched_param) == 0);
1921 sigemptyset(&sigmask);
1922 sigaddset(&sigmask, SIGUSR2);
1923 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1925 /* Sync up with other thread after sigmask updated. */
1926 pthread_barrier_wait(&barrier);
1928 /* Sync up with the test before doing the getpid(). */
1935 /* The first wait() should report the stop from SIGSTOP. */
1936 wpid = waitpid(fpid, &status, 0);
1937 ATF_REQUIRE(wpid == fpid);
1938 ATF_REQUIRE(WIFSTOPPED(status));
1939 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1941 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1942 main_lwp = pl.pl_lwpid;
1944 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1945 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1948 * Continue until child is done with setup, which is indicated with
1949 * SIGSTOP. Ignore system calls in the meantime.
1952 wpid = waitpid(fpid, &status, 0);
1953 ATF_REQUIRE(wpid == fpid);
1954 ATF_REQUIRE(WIFSTOPPED(status));
1955 if (WSTOPSIG(status) == SIGTRAP) {
1956 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1958 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
1960 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1963 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1966 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
1967 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1969 wpid = waitpid(fpid, &status, 0);
1970 ATF_REQUIRE(wpid == fpid);
1971 ATF_REQUIRE(WIFSTOPPED(status));
1972 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1974 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1976 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
1977 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1978 /* Prevent the main thread from hitting its syscall exit for now. */
1979 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
1982 * Proceed, allowing second thread to hit syscall exit for
1983 * pthread_barrier_wait().
1985 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1987 wpid = waitpid(fpid, &status, 0);
1988 ATF_REQUIRE(wpid == fpid);
1989 ATF_REQUIRE(WIFSTOPPED(status));
1990 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1992 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1994 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1995 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
1997 /* Send a signal that only the second thread can handle. */
1998 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2000 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2002 /* The next wait() should report the SIGUSR2. */
2003 wpid = waitpid(fpid, &status, 0);
2004 ATF_REQUIRE(wpid == fpid);
2005 ATF_REQUIRE(WIFSTOPPED(status));
2006 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2008 /* Allow the main thread to try to finish its system call. */
2009 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2012 * At this point, the main thread is in the middle of a system call and
2013 * has been resumed. The second thread has taken a SIGUSR2 which will
2014 * be replaced with a SIGKILL below. The main thread will get to run
2015 * first. It should notice the kill request (even though the signal
2016 * replacement occurred in the other thread) and exit accordingly. It
2017 * should not stop for the system call exit event.
2020 /* Replace the SIGUSR2 with a kill. */
2021 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2023 /* The last wait() should report the SIGKILL (not a syscall exit). */
2024 wpid = waitpid(fpid, &status, 0);
2025 ATF_REQUIRE(wpid == fpid);
2026 ATF_REQUIRE(WIFSIGNALED(status));
2027 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2029 wpid = wait(&status);
2030 ATF_REQUIRE(wpid == -1);
2031 ATF_REQUIRE(errno == ECHILD);
2035 sigusr1_handler(int sig)
2038 CHILD_REQUIRE(sig == SIGUSR1);
2043 * Verify that even if the signal queue is full for a child process,
2044 * a PT_KILL will kill the process.
2046 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2047 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2051 int max_pending_per_proc;
2055 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2057 ATF_REQUIRE((fpid = fork()) != -1);
2063 /* The first wait() should report the stop from SIGSTOP. */
2064 wpid = waitpid(fpid, &status, 0);
2065 ATF_REQUIRE(wpid == fpid);
2066 ATF_REQUIRE(WIFSTOPPED(status));
2067 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2069 len = sizeof(max_pending_per_proc);
2070 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2071 &max_pending_per_proc, &len, NULL, 0) == 0);
2073 /* Fill the signal queue. */
2074 for (i = 0; i < max_pending_per_proc; ++i)
2075 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2077 /* Kill the child process. */
2078 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2080 /* The last wait() should report the SIGKILL. */
2081 wpid = waitpid(fpid, &status, 0);
2082 ATF_REQUIRE(wpid == fpid);
2083 ATF_REQUIRE(WIFSIGNALED(status));
2084 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2086 wpid = wait(&status);
2087 ATF_REQUIRE(wpid == -1);
2088 ATF_REQUIRE(errno == ECHILD);
2092 * Verify that when stopped at a system call entry, a signal can be
2093 * requested with PT_CONTINUE which will be delivered once the system
2096 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2097 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2099 struct ptrace_lwpinfo pl;
2103 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2105 ATF_REQUIRE((fpid = fork()) != -1);
2112 /* The first wait() should report the stop from SIGSTOP. */
2113 wpid = waitpid(fpid, &status, 0);
2114 ATF_REQUIRE(wpid == fpid);
2115 ATF_REQUIRE(WIFSTOPPED(status));
2116 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2118 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2119 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2121 /* The second wait() should report a system call entry for getpid(). */
2122 wpid = waitpid(fpid, &status, 0);
2123 ATF_REQUIRE(wpid == fpid);
2124 ATF_REQUIRE(WIFSTOPPED(status));
2125 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2127 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2128 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2130 /* Continue the child process with a signal. */
2131 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2135 * The last wait() should report exit 2, i.e., a normal _exit
2136 * from the signal handler. In the meantime, catch and proceed
2137 * past any syscall stops.
2139 wpid = waitpid(fpid, &status, 0);
2140 ATF_REQUIRE(wpid == fpid);
2141 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2142 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2143 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2144 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2146 ATF_REQUIRE(WIFEXITED(status));
2147 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2152 wpid = wait(&status);
2153 ATF_REQUIRE(wpid == -1);
2154 ATF_REQUIRE(errno == ECHILD);
2158 sigusr1_counting_handler(int sig)
2160 static int counter = 0;
2162 CHILD_REQUIRE(sig == SIGUSR1);
2169 * Verify that, when continuing from a stop at system call entry and exit,
2170 * a signal can be requested from both stops, and both will be delivered when
2171 * the system call is complete.
2173 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2174 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2176 struct ptrace_lwpinfo pl;
2180 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2182 ATF_REQUIRE((fpid = fork()) != -1);
2189 /* The first wait() should report the stop from SIGSTOP. */
2190 wpid = waitpid(fpid, &status, 0);
2191 ATF_REQUIRE(wpid == fpid);
2192 ATF_REQUIRE(WIFSTOPPED(status));
2193 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2195 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2196 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2198 /* The second wait() should report a system call entry for getpid(). */
2199 wpid = waitpid(fpid, &status, 0);
2200 ATF_REQUIRE(wpid == fpid);
2201 ATF_REQUIRE(WIFSTOPPED(status));
2202 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2204 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2205 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2207 /* Continue the child process with a signal. */
2208 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2210 /* The third wait() should report a system call exit for getpid(). */
2211 wpid = waitpid(fpid, &status, 0);
2212 ATF_REQUIRE(wpid == fpid);
2213 ATF_REQUIRE(WIFSTOPPED(status));
2214 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2216 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2217 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2219 /* Continue the child process with a signal. */
2220 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2224 * The last wait() should report exit 2, i.e., a normal _exit
2225 * from the signal handler. In the meantime, catch and proceed
2226 * past any syscall stops.
2228 wpid = waitpid(fpid, &status, 0);
2229 ATF_REQUIRE(wpid == fpid);
2230 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2231 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2232 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2233 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2235 ATF_REQUIRE(WIFEXITED(status));
2236 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2241 wpid = wait(&status);
2242 ATF_REQUIRE(wpid == -1);
2243 ATF_REQUIRE(errno == ECHILD);
2247 * Verify that even if the signal queue is full for a child process,
2248 * a PT_CONTINUE with a signal will not result in loss of that signal.
2250 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2251 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2255 int max_pending_per_proc;
2259 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2260 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2262 ATF_REQUIRE((fpid = fork()) != -1);
2268 /* The first wait() should report the stop from SIGSTOP. */
2269 wpid = waitpid(fpid, &status, 0);
2270 ATF_REQUIRE(wpid == fpid);
2271 ATF_REQUIRE(WIFSTOPPED(status));
2272 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2274 len = sizeof(max_pending_per_proc);
2275 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2276 &max_pending_per_proc, &len, NULL, 0) == 0);
2278 /* Fill the signal queue. */
2279 for (i = 0; i < max_pending_per_proc; ++i)
2280 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2282 /* Continue with signal. */
2283 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2286 wpid = waitpid(fpid, &status, 0);
2287 ATF_REQUIRE(wpid == fpid);
2288 if (WIFSTOPPED(status)) {
2289 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2290 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2293 * The last wait() should report normal _exit from the
2296 ATF_REQUIRE(WIFEXITED(status));
2297 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2302 wpid = wait(&status);
2303 ATF_REQUIRE(wpid == -1);
2304 ATF_REQUIRE(errno == ECHILD);
2308 * Verify that, after stopping due to a signal, that signal can be
2309 * replaced with another signal.
2311 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2312 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2314 struct ptrace_lwpinfo pl;
2318 ATF_REQUIRE((fpid = fork()) != -1);
2325 /* The first wait() should report the stop from SIGSTOP. */
2326 wpid = waitpid(fpid, &status, 0);
2327 ATF_REQUIRE(wpid == fpid);
2328 ATF_REQUIRE(WIFSTOPPED(status));
2329 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2331 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2333 /* Send a signal without ptrace. */
2334 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2336 /* The second wait() should report a SIGINT was received. */
2337 wpid = waitpid(fpid, &status, 0);
2338 ATF_REQUIRE(wpid == fpid);
2339 ATF_REQUIRE(WIFSTOPPED(status));
2340 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2342 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2343 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2344 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2346 /* Continue the child process with a different signal. */
2347 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2350 * The last wait() should report having died due to the new
2353 wpid = waitpid(fpid, &status, 0);
2354 ATF_REQUIRE(wpid == fpid);
2355 ATF_REQUIRE(WIFSIGNALED(status));
2356 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2358 wpid = wait(&status);
2359 ATF_REQUIRE(wpid == -1);
2360 ATF_REQUIRE(errno == ECHILD);
2364 * Verify that a signal can be passed through to the child even when there
2365 * was no true signal originally. Such cases arise when a SIGTRAP is
2366 * invented for e.g, system call stops.
2368 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2369 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2371 struct ptrace_lwpinfo pl;
2375 ATF_REQUIRE((fpid = fork()) != -1);
2382 /* The first wait() should report the stop from SIGSTOP. */
2383 wpid = waitpid(fpid, &status, 0);
2384 ATF_REQUIRE(wpid == fpid);
2385 ATF_REQUIRE(WIFSTOPPED(status));
2386 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2388 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2389 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2391 /* The second wait() should report a system call entry for getpid(). */
2392 wpid = waitpid(fpid, &status, 0);
2393 ATF_REQUIRE(wpid == fpid);
2394 ATF_REQUIRE(WIFSTOPPED(status));
2395 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2397 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2398 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2400 /* Continue the child process with a SIGTRAP. */
2401 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2405 * The last wait() should report exit due to SIGTRAP. In the
2406 * meantime, catch and proceed past any syscall stops.
2408 wpid = waitpid(fpid, &status, 0);
2409 ATF_REQUIRE(wpid == fpid);
2410 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2411 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2412 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2413 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2415 ATF_REQUIRE(WIFSIGNALED(status));
2416 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2421 wpid = wait(&status);
2422 ATF_REQUIRE(wpid == -1);
2423 ATF_REQUIRE(errno == ECHILD);
2428 * A mixed bag PT_CONTINUE with signal test.
2430 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2431 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2433 struct ptrace_lwpinfo pl;
2437 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2439 ATF_REQUIRE((fpid = fork()) != -1);
2446 /* The first wait() should report the stop from SIGSTOP. */
2447 wpid = waitpid(fpid, &status, 0);
2448 ATF_REQUIRE(wpid == fpid);
2449 ATF_REQUIRE(WIFSTOPPED(status));
2450 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2452 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2453 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2455 /* The second wait() should report a system call entry for getpid(). */
2456 wpid = waitpid(fpid, &status, 0);
2457 ATF_REQUIRE(wpid == fpid);
2458 ATF_REQUIRE(WIFSTOPPED(status));
2459 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2461 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2462 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2464 /* Continue with the first SIGUSR1. */
2465 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2467 /* The next wait() should report a system call exit for getpid(). */
2468 wpid = waitpid(fpid, &status, 0);
2469 ATF_REQUIRE(wpid == fpid);
2470 ATF_REQUIRE(WIFSTOPPED(status));
2471 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2473 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2474 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2476 /* Send an ABRT without ptrace. */
2477 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2479 /* Continue normally. */
2480 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2482 /* The next wait() should report the SIGABRT. */
2483 wpid = waitpid(fpid, &status, 0);
2484 ATF_REQUIRE(wpid == fpid);
2485 ATF_REQUIRE(WIFSTOPPED(status));
2486 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2488 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2489 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2490 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2492 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2493 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2497 * The last wait() should report exit 2, i.e., a normal _exit
2498 * from the signal handler. In the meantime, catch and proceed
2499 * past any syscall stops.
2501 wpid = waitpid(fpid, &status, 0);
2502 ATF_REQUIRE(wpid == fpid);
2503 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2504 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2505 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2506 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2508 ATF_REQUIRE(WIFEXITED(status));
2509 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2514 wpid = wait(&status);
2515 ATF_REQUIRE(wpid == -1);
2516 ATF_REQUIRE(errno == ECHILD);
2521 * Verify a signal delivered by ptrace is noticed by kevent(2).
2523 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2524 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2527 int status, kq, nevents;
2530 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2532 ATF_REQUIRE((fpid = fork()) != -1);
2534 CHILD_REQUIRE((kq = kqueue()) > 0);
2535 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2536 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2541 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2542 if (nevents == -1 && errno == EINTR)
2544 CHILD_REQUIRE(nevents > 0);
2545 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2546 CHILD_REQUIRE(kev.ident == SIGUSR1);
2553 /* The first wait() should report the stop from SIGSTOP. */
2554 wpid = waitpid(fpid, &status, 0);
2555 ATF_REQUIRE(wpid == fpid);
2556 ATF_REQUIRE(WIFSTOPPED(status));
2557 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2559 /* Continue with the SIGUSR1. */
2560 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2563 * The last wait() should report normal exit with code 1.
2565 wpid = waitpid(fpid, &status, 0);
2566 ATF_REQUIRE(wpid == fpid);
2567 ATF_REQUIRE(WIFEXITED(status));
2568 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2570 wpid = wait(&status);
2571 ATF_REQUIRE(wpid == -1);
2572 ATF_REQUIRE(errno == ECHILD);
2575 static sem_t sigusr1_sem;
2578 sigusr1_sempost_handler(int sig __unused)
2581 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2585 signal_thread(void *arg)
2590 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2592 /* Wait for this thread to receive a SIGUSR1. */
2594 err = sem_wait(&sigusr1_sem);
2595 CHILD_REQUIRE(err == 0 || errno == EINTR);
2596 } while (err != 0 && errno == EINTR);
2598 /* Free our companion thread from the barrier. */
2599 pthread_barrier_wait(pbarrier);
2602 * Swap ignore duties; the next SIGUSR1 should go to the
2605 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2606 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2607 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2609 /* Sync up threads after swapping signal masks. */
2610 pthread_barrier_wait(pbarrier);
2612 /* Wait until our companion has received its SIGUSR1. */
2613 pthread_barrier_wait(pbarrier);
2619 * Verify that if ptrace stops due to a signal but continues with
2620 * a different signal that the new signal is routed to a thread
2621 * that can accept it, and that that thread is awakened by the signal
2622 * in a timely manner.
2624 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2625 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2631 pthread_barrier_t barrier;
2633 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2634 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2635 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2637 ATF_REQUIRE((fpid = fork()) != -1);
2639 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2641 /* The other thread should receive the first SIGUSR1. */
2642 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2643 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2644 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2648 /* Wait until other thread has received its SIGUSR1. */
2649 pthread_barrier_wait(&barrier);
2652 * Swap ignore duties; the next SIGUSR1 should go to this
2655 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2657 /* Sync up threads after swapping signal masks. */
2658 pthread_barrier_wait(&barrier);
2661 * Sync up with test code; we're ready for the next SIGUSR1
2666 /* Wait for this thread to receive a SIGUSR1. */
2668 err = sem_wait(&sigusr1_sem);
2669 CHILD_REQUIRE(err == 0 || errno == EINTR);
2670 } while (err != 0 && errno == EINTR);
2672 /* Free the other thread from the barrier. */
2673 pthread_barrier_wait(&barrier);
2675 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2680 /* The first wait() should report the stop from SIGSTOP. */
2681 wpid = waitpid(fpid, &status, 0);
2682 ATF_REQUIRE(wpid == fpid);
2683 ATF_REQUIRE(WIFSTOPPED(status));
2684 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2686 /* Continue the child ignoring the SIGSTOP. */
2687 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2690 * Send a signal without ptrace that either thread will accept (USR2,
2693 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2695 /* The second wait() should report a SIGUSR2 was received. */
2696 wpid = waitpid(fpid, &status, 0);
2697 ATF_REQUIRE(wpid == fpid);
2698 ATF_REQUIRE(WIFSTOPPED(status));
2699 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2701 /* Continue the child, changing the signal to USR1. */
2702 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2704 /* The next wait() should report the stop from SIGSTOP. */
2705 wpid = waitpid(fpid, &status, 0);
2706 ATF_REQUIRE(wpid == fpid);
2707 ATF_REQUIRE(WIFSTOPPED(status));
2708 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2710 /* Continue the child ignoring the SIGSTOP. */
2711 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2713 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2715 /* The next wait() should report a SIGUSR2 was received. */
2716 wpid = waitpid(fpid, &status, 0);
2717 ATF_REQUIRE(wpid == fpid);
2718 ATF_REQUIRE(WIFSTOPPED(status));
2719 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2721 /* Continue the child, changing the signal to USR1. */
2722 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2724 /* The last wait() should report normal exit with code 1. */
2725 wpid = waitpid(fpid, &status, 0);
2726 ATF_REQUIRE(wpid == fpid);
2727 ATF_REQUIRE(WIFEXITED(status));
2728 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2730 wpid = wait(&status);
2731 ATF_REQUIRE(wpid == -1);
2732 ATF_REQUIRE(errno == ECHILD);
2738 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2739 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
2740 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
2741 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
2742 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
2743 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
2744 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
2745 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
2747 ptrace__follow_fork_child_detached_unrelated_debugger);
2749 ptrace__follow_fork_parent_detached_unrelated_debugger);
2750 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2751 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2752 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
2753 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
2754 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
2755 ATF_TP_ADD_TC(tp, ptrace__siginfo);
2756 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
2757 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
2758 ATF_TP_ADD_TC(tp, ptrace__event_mask);
2759 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2760 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2761 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
2762 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2764 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
2765 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
2766 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
2767 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
2768 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
2769 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
2771 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2772 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2773 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
2774 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2775 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
2776 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
2777 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2779 return (atf_no_error());