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/procctl.h>
35 #include <sys/ptrace.h>
36 #include <sys/queue.h>
38 #include <sys/syscall.h>
39 #include <sys/sysctl.h>
43 #include <machine/cpufunc.h>
46 #include <semaphore.h>
54 * A variant of ATF_REQUIRE that is suitable for use in child
55 * processes. This only works if the parent process is tripped up by
56 * the early exit and fails some requirement itself.
58 #define CHILD_REQUIRE(exp) do { \
60 child_fail_require(__FILE__, __LINE__, \
65 child_fail_require(const char *file, int line, const char *str)
69 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
70 write(2, buf, strlen(buf));
78 /* Attach the parent process as a tracer of this process. */
79 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
86 attach_child(pid_t pid)
91 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
93 wpid = waitpid(pid, &status, 0);
94 ATF_REQUIRE(wpid == pid);
95 ATF_REQUIRE(WIFSTOPPED(status));
96 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
100 wait_for_zombie(pid_t pid)
104 * Wait for a process to exit. This is kind of gross, but
105 * there is not a better way.
108 struct kinfo_proc kp;
114 mib[2] = KERN_PROC_PID;
117 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
118 /* The KERN_PROC_PID sysctl fails for zombies. */
119 ATF_REQUIRE(errno == ESRCH);
127 * Verify that a parent debugger process "sees" the exit of a debugged
128 * process exactly once when attached via PT_TRACE_ME.
130 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
131 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
136 ATF_REQUIRE((child = fork()) != -1);
144 /* Parent process. */
146 /* The first wait() should report the stop from SIGSTOP. */
147 wpid = waitpid(child, &status, 0);
148 ATF_REQUIRE(wpid == child);
149 ATF_REQUIRE(WIFSTOPPED(status));
150 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
152 /* Continue the child ignoring the SIGSTOP. */
153 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
155 /* The second wait() should report the exit status. */
156 wpid = waitpid(child, &status, 0);
157 ATF_REQUIRE(wpid == child);
158 ATF_REQUIRE(WIFEXITED(status));
159 ATF_REQUIRE(WEXITSTATUS(status) == 1);
161 /* The child should no longer exist. */
162 wpid = waitpid(child, &status, 0);
163 ATF_REQUIRE(wpid == -1);
164 ATF_REQUIRE(errno == ECHILD);
168 * Verify that a parent debugger process "sees" the exit of a debugged
169 * process exactly once when attached via PT_ATTACH.
171 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
172 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
175 int cpipe[2], status;
178 ATF_REQUIRE(pipe(cpipe) == 0);
179 ATF_REQUIRE((child = fork()) != -1);
184 /* Wait for the parent to attach. */
185 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
191 /* Parent process. */
193 /* Attach to the child process. */
196 /* Continue the child ignoring the SIGSTOP. */
197 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
199 /* Signal the child to exit. */
202 /* The second wait() should report the exit status. */
203 wpid = waitpid(child, &status, 0);
204 ATF_REQUIRE(wpid == child);
205 ATF_REQUIRE(WIFEXITED(status));
206 ATF_REQUIRE(WEXITSTATUS(status) == 1);
208 /* The child should no longer exist. */
209 wpid = waitpid(child, &status, 0);
210 ATF_REQUIRE(wpid == -1);
211 ATF_REQUIRE(errno == ECHILD);
215 * Verify that a parent process "sees" the exit of a debugged process only
216 * after the debugger has seen it.
218 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
219 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
221 pid_t child, debugger, wpid;
222 int cpipe[2], dpipe[2], status;
225 ATF_REQUIRE(pipe(cpipe) == 0);
226 ATF_REQUIRE((child = fork()) != -1);
232 /* Wait for parent to be ready. */
233 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
239 ATF_REQUIRE(pipe(dpipe) == 0);
240 ATF_REQUIRE((debugger = fork()) != -1);
243 /* Debugger process. */
246 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
248 wpid = waitpid(child, &status, 0);
249 CHILD_REQUIRE(wpid == child);
250 CHILD_REQUIRE(WIFSTOPPED(status));
251 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
253 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
255 /* Signal parent that debugger is attached. */
256 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
258 /* Wait for parent's failed wait. */
259 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
261 wpid = waitpid(child, &status, 0);
262 CHILD_REQUIRE(wpid == child);
263 CHILD_REQUIRE(WIFEXITED(status));
264 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
270 /* Parent process. */
272 /* Wait for the debugger to attach to the child. */
273 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
275 /* Release the child. */
276 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
277 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
280 wait_for_zombie(child);
283 * This wait should return a pid of 0 to indicate no status to
284 * report. The parent should see the child as non-exited
285 * until the debugger sees the exit.
287 wpid = waitpid(child, &status, WNOHANG);
288 ATF_REQUIRE(wpid == 0);
290 /* Signal the debugger to wait for the child. */
293 /* Wait for the debugger. */
294 wpid = waitpid(debugger, &status, 0);
295 ATF_REQUIRE(wpid == debugger);
296 ATF_REQUIRE(WIFEXITED(status));
297 ATF_REQUIRE(WEXITSTATUS(status) == 0);
299 /* The child process should now be ready. */
300 wpid = waitpid(child, &status, WNOHANG);
301 ATF_REQUIRE(wpid == child);
302 ATF_REQUIRE(WIFEXITED(status));
303 ATF_REQUIRE(WEXITSTATUS(status) == 1);
307 * Verify that a parent process "sees" the exit of a debugged process
308 * only after a non-direct-child debugger has seen it. In particular,
309 * various wait() calls in the parent must avoid failing with ESRCH by
310 * checking the parent's orphan list for the debugee.
312 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
313 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
315 pid_t child, debugger, fpid, wpid;
316 int cpipe[2], dpipe[2], status;
319 ATF_REQUIRE(pipe(cpipe) == 0);
320 ATF_REQUIRE((child = fork()) != -1);
326 /* Wait for parent to be ready. */
327 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
333 ATF_REQUIRE(pipe(dpipe) == 0);
334 ATF_REQUIRE((debugger = fork()) != -1);
337 /* Debugger parent. */
340 * Fork again and drop the debugger parent so that the
341 * debugger is not a child of the main parent.
343 CHILD_REQUIRE((fpid = fork()) != -1);
347 /* Debugger process. */
350 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
352 wpid = waitpid(child, &status, 0);
353 CHILD_REQUIRE(wpid == child);
354 CHILD_REQUIRE(WIFSTOPPED(status));
355 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
357 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
359 /* Signal parent that debugger is attached. */
360 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
362 /* Wait for parent's failed wait. */
363 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
365 wpid = waitpid(child, &status, 0);
366 CHILD_REQUIRE(wpid == child);
367 CHILD_REQUIRE(WIFEXITED(status));
368 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
374 /* Parent process. */
376 /* Wait for the debugger parent process to exit. */
377 wpid = waitpid(debugger, &status, 0);
378 ATF_REQUIRE(wpid == debugger);
379 ATF_REQUIRE(WIFEXITED(status));
380 ATF_REQUIRE(WEXITSTATUS(status) == 2);
382 /* A WNOHANG wait here should see the non-exited child. */
383 wpid = waitpid(child, &status, WNOHANG);
384 ATF_REQUIRE(wpid == 0);
386 /* Wait for the debugger to attach to the child. */
387 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
389 /* Release the child. */
390 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
391 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
394 wait_for_zombie(child);
397 * This wait should return a pid of 0 to indicate no status to
398 * report. The parent should see the child as non-exited
399 * until the debugger sees the exit.
401 wpid = waitpid(child, &status, WNOHANG);
402 ATF_REQUIRE(wpid == 0);
404 /* Signal the debugger to wait for the child. */
405 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
407 /* Wait for the debugger. */
408 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
411 /* The child process should now be ready. */
412 wpid = waitpid(child, &status, WNOHANG);
413 ATF_REQUIRE(wpid == child);
414 ATF_REQUIRE(WIFEXITED(status));
415 ATF_REQUIRE(WEXITSTATUS(status) == 1);
419 * The parent process should always act the same regardless of how the
420 * debugger is attached to it.
423 follow_fork_parent(bool use_vfork)
429 CHILD_REQUIRE((fpid = vfork()) != -1);
431 CHILD_REQUIRE((fpid = fork()) != -1);
437 wpid = waitpid(fpid, &status, 0);
438 CHILD_REQUIRE(wpid == fpid);
439 CHILD_REQUIRE(WIFEXITED(status));
440 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
446 * Helper routine for follow fork tests. This waits for two stops
447 * that report both "sides" of a fork. It returns the pid of the new
451 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
453 struct ptrace_lwpinfo pl;
454 bool fork_reported[2];
458 fork_reported[0] = false;
459 fork_reported[1] = false;
463 * Each process should report a fork event. The parent should
464 * report a PL_FLAG_FORKED event, and the child should report
465 * a PL_FLAG_CHILD event.
467 for (i = 0; i < 2; i++) {
468 wpid = wait(&status);
469 ATF_REQUIRE(wpid > 0);
470 ATF_REQUIRE(WIFSTOPPED(status));
472 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
474 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
476 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
477 (PL_FLAG_FORKED | PL_FLAG_CHILD));
478 if (pl.pl_flags & PL_FLAG_CHILD) {
479 ATF_REQUIRE(wpid != parent);
480 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
481 ATF_REQUIRE(!fork_reported[1]);
485 ATF_REQUIRE(child == wpid);
488 fork_reported[1] = true;
490 ATF_REQUIRE(wpid == parent);
491 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
492 ATF_REQUIRE(!fork_reported[0]);
494 child = pl.pl_child_pid;
496 ATF_REQUIRE(child == pl.pl_child_pid);
499 fork_reported[0] = true;
507 * Verify that a new child process is stopped after a followed fork and
508 * that the traced parent sees the exit of the child after the debugger
509 * when both processes remain attached to the debugger.
511 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
512 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
514 pid_t children[0], fpid, wpid;
517 ATF_REQUIRE((fpid = fork()) != -1);
520 follow_fork_parent(false);
523 /* Parent process. */
526 /* The first wait() should report the stop from SIGSTOP. */
527 wpid = waitpid(children[0], &status, 0);
528 ATF_REQUIRE(wpid == children[0]);
529 ATF_REQUIRE(WIFSTOPPED(status));
530 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
532 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
534 /* Continue the child ignoring the SIGSTOP. */
535 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
537 children[1] = handle_fork_events(children[0], NULL);
538 ATF_REQUIRE(children[1] > 0);
540 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
541 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
544 * The child can't exit until the grandchild reports status, so the
545 * grandchild should report its exit first to the debugger.
547 wpid = wait(&status);
548 ATF_REQUIRE(wpid == children[1]);
549 ATF_REQUIRE(WIFEXITED(status));
550 ATF_REQUIRE(WEXITSTATUS(status) == 2);
552 wpid = wait(&status);
553 ATF_REQUIRE(wpid == children[0]);
554 ATF_REQUIRE(WIFEXITED(status));
555 ATF_REQUIRE(WEXITSTATUS(status) == 1);
557 wpid = wait(&status);
558 ATF_REQUIRE(wpid == -1);
559 ATF_REQUIRE(errno == ECHILD);
563 * Verify that a new child process is stopped after a followed fork
564 * and that the traced parent sees the exit of the child when the new
565 * child process is detached after it reports its fork.
567 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
568 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
570 pid_t children[0], fpid, wpid;
573 ATF_REQUIRE((fpid = fork()) != -1);
576 follow_fork_parent(false);
579 /* Parent process. */
582 /* The first wait() should report the stop from SIGSTOP. */
583 wpid = waitpid(children[0], &status, 0);
584 ATF_REQUIRE(wpid == children[0]);
585 ATF_REQUIRE(WIFSTOPPED(status));
586 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
588 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
590 /* Continue the child ignoring the SIGSTOP. */
591 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
593 children[1] = handle_fork_events(children[0], NULL);
594 ATF_REQUIRE(children[1] > 0);
596 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
597 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
600 * Should not see any status from the grandchild now, only the
603 wpid = wait(&status);
604 ATF_REQUIRE(wpid == children[0]);
605 ATF_REQUIRE(WIFEXITED(status));
606 ATF_REQUIRE(WEXITSTATUS(status) == 1);
608 wpid = wait(&status);
609 ATF_REQUIRE(wpid == -1);
610 ATF_REQUIRE(errno == ECHILD);
614 * Verify that a new child process is stopped after a followed fork
615 * and that the traced parent sees the exit of the child when the
616 * traced parent is detached after the fork.
618 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
619 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
621 pid_t children[0], fpid, wpid;
624 ATF_REQUIRE((fpid = fork()) != -1);
627 follow_fork_parent(false);
630 /* Parent process. */
633 /* The first wait() should report the stop from SIGSTOP. */
634 wpid = waitpid(children[0], &status, 0);
635 ATF_REQUIRE(wpid == children[0]);
636 ATF_REQUIRE(WIFSTOPPED(status));
637 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
639 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
641 /* Continue the child ignoring the SIGSTOP. */
642 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
644 children[1] = handle_fork_events(children[0], NULL);
645 ATF_REQUIRE(children[1] > 0);
647 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
648 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
651 * The child can't exit until the grandchild reports status, so the
652 * grandchild should report its exit first to the debugger.
654 * Even though the child process is detached, it is still a
655 * child of the debugger, so it will still report it's exit
656 * after the grandchild.
658 wpid = wait(&status);
659 ATF_REQUIRE(wpid == children[1]);
660 ATF_REQUIRE(WIFEXITED(status));
661 ATF_REQUIRE(WEXITSTATUS(status) == 2);
663 wpid = wait(&status);
664 ATF_REQUIRE(wpid == children[0]);
665 ATF_REQUIRE(WIFEXITED(status));
666 ATF_REQUIRE(WEXITSTATUS(status) == 1);
668 wpid = wait(&status);
669 ATF_REQUIRE(wpid == -1);
670 ATF_REQUIRE(errno == ECHILD);
674 attach_fork_parent(int cpipe[2])
680 /* Double-fork to disassociate from the debugger. */
681 CHILD_REQUIRE((fpid = fork()) != -1);
685 /* Send the pid of the disassociated child to the debugger. */
687 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
689 /* Wait for the debugger to attach. */
690 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
694 * Verify that a new child process is stopped after a followed fork and
695 * that the traced parent sees the exit of the child after the debugger
696 * when both processes remain attached to the debugger. In this test
697 * the parent that forks is not a direct child of the debugger.
699 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
700 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
702 pid_t children[0], fpid, wpid;
703 int cpipe[2], status;
705 ATF_REQUIRE(pipe(cpipe) == 0);
706 ATF_REQUIRE((fpid = fork()) != -1);
708 attach_fork_parent(cpipe);
709 follow_fork_parent(false);
712 /* Parent process. */
715 /* Wait for the direct child to exit. */
716 wpid = waitpid(fpid, &status, 0);
717 ATF_REQUIRE(wpid == fpid);
718 ATF_REQUIRE(WIFEXITED(status));
719 ATF_REQUIRE(WEXITSTATUS(status) == 3);
721 /* Read the pid of the fork parent. */
722 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
723 sizeof(children[0]));
725 /* Attach to the fork parent. */
726 attach_child(children[0]);
728 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
730 /* Continue the fork parent ignoring the SIGSTOP. */
731 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
733 /* Signal the fork parent to continue. */
736 children[1] = handle_fork_events(children[0], NULL);
737 ATF_REQUIRE(children[1] > 0);
739 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
740 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
743 * The fork parent can't exit until the child reports status,
744 * so the child should report its exit first to the debugger.
746 wpid = wait(&status);
747 ATF_REQUIRE(wpid == children[1]);
748 ATF_REQUIRE(WIFEXITED(status));
749 ATF_REQUIRE(WEXITSTATUS(status) == 2);
751 wpid = wait(&status);
752 ATF_REQUIRE(wpid == children[0]);
753 ATF_REQUIRE(WIFEXITED(status));
754 ATF_REQUIRE(WEXITSTATUS(status) == 1);
756 wpid = wait(&status);
757 ATF_REQUIRE(wpid == -1);
758 ATF_REQUIRE(errno == ECHILD);
762 * Verify that a new child process is stopped after a followed fork
763 * and that the traced parent sees the exit of the child when the new
764 * child process is detached after it reports its fork. In this test
765 * the parent that forks is not a direct child of the debugger.
767 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
768 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
770 pid_t children[0], fpid, wpid;
771 int cpipe[2], status;
773 ATF_REQUIRE(pipe(cpipe) == 0);
774 ATF_REQUIRE((fpid = fork()) != -1);
776 attach_fork_parent(cpipe);
777 follow_fork_parent(false);
780 /* Parent process. */
783 /* Wait for the direct child to exit. */
784 wpid = waitpid(fpid, &status, 0);
785 ATF_REQUIRE(wpid == fpid);
786 ATF_REQUIRE(WIFEXITED(status));
787 ATF_REQUIRE(WEXITSTATUS(status) == 3);
789 /* Read the pid of the fork parent. */
790 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
791 sizeof(children[0]));
793 /* Attach to the fork parent. */
794 attach_child(children[0]);
796 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
798 /* Continue the fork parent ignoring the SIGSTOP. */
799 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
801 /* Signal the fork parent to continue. */
804 children[1] = handle_fork_events(children[0], NULL);
805 ATF_REQUIRE(children[1] > 0);
807 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
808 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
811 * Should not see any status from the child now, only the fork
814 wpid = wait(&status);
815 ATF_REQUIRE(wpid == children[0]);
816 ATF_REQUIRE(WIFEXITED(status));
817 ATF_REQUIRE(WEXITSTATUS(status) == 1);
819 wpid = wait(&status);
820 ATF_REQUIRE(wpid == -1);
821 ATF_REQUIRE(errno == ECHILD);
825 * Verify that a new child process is stopped after a followed fork
826 * and that the traced parent sees the exit of the child when the
827 * traced parent is detached after the fork. In this test the parent
828 * that forks is not a direct child of the debugger.
830 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
831 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
833 pid_t children[0], fpid, wpid;
834 int cpipe[2], status;
836 ATF_REQUIRE(pipe(cpipe) == 0);
837 ATF_REQUIRE((fpid = fork()) != -1);
839 attach_fork_parent(cpipe);
840 follow_fork_parent(false);
843 /* Parent process. */
846 /* Wait for the direct child to exit. */
847 wpid = waitpid(fpid, &status, 0);
848 ATF_REQUIRE(wpid == fpid);
849 ATF_REQUIRE(WIFEXITED(status));
850 ATF_REQUIRE(WEXITSTATUS(status) == 3);
852 /* Read the pid of the fork parent. */
853 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
854 sizeof(children[0]));
856 /* Attach to the fork parent. */
857 attach_child(children[0]);
859 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
861 /* Continue the fork parent ignoring the SIGSTOP. */
862 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
864 /* Signal the fork parent to continue. */
867 children[1] = handle_fork_events(children[0], NULL);
868 ATF_REQUIRE(children[1] > 0);
870 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
871 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
874 * Should not see any status from the fork parent now, only
877 wpid = wait(&status);
878 ATF_REQUIRE(wpid == children[1]);
879 ATF_REQUIRE(WIFEXITED(status));
880 ATF_REQUIRE(WEXITSTATUS(status) == 2);
882 wpid = wait(&status);
883 ATF_REQUIRE(wpid == -1);
884 ATF_REQUIRE(errno == ECHILD);
888 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
889 * child process created via fork() reports the correct value.
891 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
892 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
894 struct ptrace_lwpinfo pl[2];
895 pid_t children[2], fpid, wpid;
898 ATF_REQUIRE((fpid = fork()) != -1);
901 follow_fork_parent(false);
904 /* Parent process. */
907 /* The first wait() should report the stop from SIGSTOP. */
908 wpid = waitpid(children[0], &status, 0);
909 ATF_REQUIRE(wpid == children[0]);
910 ATF_REQUIRE(WIFSTOPPED(status));
911 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
913 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
915 /* Continue the child ignoring the SIGSTOP. */
916 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
918 /* Wait for both halves of the fork event to get reported. */
919 children[1] = handle_fork_events(children[0], pl);
920 ATF_REQUIRE(children[1] > 0);
922 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
923 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
924 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
925 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
926 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
928 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
929 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
932 * The child can't exit until the grandchild reports status, so the
933 * grandchild should report its exit first to the debugger.
935 wpid = wait(&status);
936 ATF_REQUIRE(wpid == children[1]);
937 ATF_REQUIRE(WIFEXITED(status));
938 ATF_REQUIRE(WEXITSTATUS(status) == 2);
940 wpid = wait(&status);
941 ATF_REQUIRE(wpid == children[0]);
942 ATF_REQUIRE(WIFEXITED(status));
943 ATF_REQUIRE(WEXITSTATUS(status) == 1);
945 wpid = wait(&status);
946 ATF_REQUIRE(wpid == -1);
947 ATF_REQUIRE(errno == ECHILD);
951 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
952 * child process created via vfork() reports the correct value.
954 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
955 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
957 struct ptrace_lwpinfo pl[2];
958 pid_t children[2], fpid, wpid;
961 ATF_REQUIRE((fpid = fork()) != -1);
964 follow_fork_parent(true);
967 /* Parent process. */
970 /* The first wait() should report the stop from SIGSTOP. */
971 wpid = waitpid(children[0], &status, 0);
972 ATF_REQUIRE(wpid == children[0]);
973 ATF_REQUIRE(WIFSTOPPED(status));
974 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
976 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
978 /* Continue the child ignoring the SIGSTOP. */
979 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
981 /* Wait for both halves of the fork event to get reported. */
982 children[1] = handle_fork_events(children[0], pl);
983 ATF_REQUIRE(children[1] > 0);
985 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
986 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
987 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
988 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
989 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
991 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
992 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
995 * The child can't exit until the grandchild reports status, so the
996 * grandchild should report its exit first to the debugger.
998 wpid = wait(&status);
999 ATF_REQUIRE(wpid == children[1]);
1000 ATF_REQUIRE(WIFEXITED(status));
1001 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1003 wpid = wait(&status);
1004 ATF_REQUIRE(wpid == children[0]);
1005 ATF_REQUIRE(WIFEXITED(status));
1006 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1008 wpid = wait(&status);
1009 ATF_REQUIRE(wpid == -1);
1010 ATF_REQUIRE(errno == ECHILD);
1014 simple_thread(void *arg __unused)
1021 simple_thread_main(void)
1025 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1026 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1031 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1032 * thread reports the correct value.
1034 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1035 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1037 struct ptrace_lwpinfo pl;
1042 ATF_REQUIRE((fpid = fork()) != -1);
1045 simple_thread_main();
1048 /* The first wait() should report the stop from SIGSTOP. */
1049 wpid = waitpid(fpid, &status, 0);
1050 ATF_REQUIRE(wpid == fpid);
1051 ATF_REQUIRE(WIFSTOPPED(status));
1052 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1054 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1056 mainlwp = pl.pl_lwpid;
1059 * Continue the child ignoring the SIGSTOP and tracing all
1060 * system call exits.
1062 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1065 * Wait for the new thread to arrive. pthread_create() might
1066 * invoke any number of system calls. For now we just wait
1067 * for the new thread to arrive and make sure it reports a
1068 * valid system call code. If ptrace grows thread event
1069 * reporting then this test can be made more precise.
1072 wpid = waitpid(fpid, &status, 0);
1073 ATF_REQUIRE(wpid == fpid);
1074 ATF_REQUIRE(WIFSTOPPED(status));
1075 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1077 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1079 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1080 ATF_REQUIRE(pl.pl_syscall_code != 0);
1081 if (pl.pl_lwpid != mainlwp)
1082 /* New thread seen. */
1085 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1088 /* Wait for the child to exit. */
1089 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1091 wpid = waitpid(fpid, &status, 0);
1092 ATF_REQUIRE(wpid == fpid);
1093 if (WIFEXITED(status))
1096 ATF_REQUIRE(WIFSTOPPED(status));
1097 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1098 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1101 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1103 wpid = wait(&status);
1104 ATF_REQUIRE(wpid == -1);
1105 ATF_REQUIRE(errno == ECHILD);
1109 * Verify that the expected LWP events are reported for a child thread.
1111 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1112 ATF_TC_BODY(ptrace__lwp_events, tc)
1114 struct ptrace_lwpinfo pl;
1119 ATF_REQUIRE((fpid = fork()) != -1);
1122 simple_thread_main();
1125 /* The first wait() should report the stop from SIGSTOP. */
1126 wpid = waitpid(fpid, &status, 0);
1127 ATF_REQUIRE(wpid == fpid);
1128 ATF_REQUIRE(WIFSTOPPED(status));
1129 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1131 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1133 lwps[0] = pl.pl_lwpid;
1135 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1137 /* Continue the child ignoring the SIGSTOP. */
1138 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1140 /* The first event should be for the child thread's birth. */
1141 wpid = waitpid(fpid, &status, 0);
1142 ATF_REQUIRE(wpid == fpid);
1143 ATF_REQUIRE(WIFSTOPPED(status));
1144 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1146 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1147 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1148 (PL_FLAG_BORN | PL_FLAG_SCX));
1149 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1150 lwps[1] = pl.pl_lwpid;
1152 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1154 /* The next event should be for the child thread's death. */
1155 wpid = waitpid(fpid, &status, 0);
1156 ATF_REQUIRE(wpid == fpid);
1157 ATF_REQUIRE(WIFSTOPPED(status));
1158 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1160 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1161 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1162 (PL_FLAG_EXITED | PL_FLAG_SCE));
1163 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1165 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1167 /* The last event should be for the child process's exit. */
1168 wpid = waitpid(fpid, &status, 0);
1169 ATF_REQUIRE(WIFEXITED(status));
1170 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1172 wpid = wait(&status);
1173 ATF_REQUIRE(wpid == -1);
1174 ATF_REQUIRE(errno == ECHILD);
1178 exec_thread(void *arg __unused)
1181 execl("/usr/bin/true", "true", NULL);
1186 exec_thread_main(void)
1190 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1197 * Verify that the expected LWP events are reported for a multithreaded
1198 * process that calls execve(2).
1200 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1201 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1203 struct ptrace_lwpinfo pl;
1208 ATF_REQUIRE((fpid = fork()) != -1);
1214 /* The first wait() should report the stop from SIGSTOP. */
1215 wpid = waitpid(fpid, &status, 0);
1216 ATF_REQUIRE(wpid == fpid);
1217 ATF_REQUIRE(WIFSTOPPED(status));
1218 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1220 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1222 lwps[0] = pl.pl_lwpid;
1224 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1226 /* Continue the child ignoring the SIGSTOP. */
1227 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1229 /* The first event should be for the child thread's birth. */
1230 wpid = waitpid(fpid, &status, 0);
1231 ATF_REQUIRE(wpid == fpid);
1232 ATF_REQUIRE(WIFSTOPPED(status));
1233 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1235 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1236 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1237 (PL_FLAG_BORN | PL_FLAG_SCX));
1238 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1239 lwps[1] = pl.pl_lwpid;
1241 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1244 * The next event should be for the main thread's death due to
1245 * single threading from execve().
1247 wpid = waitpid(fpid, &status, 0);
1248 ATF_REQUIRE(wpid == fpid);
1249 ATF_REQUIRE(WIFSTOPPED(status));
1250 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1252 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1253 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1255 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1257 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1259 /* The next event should be for the child process's exec. */
1260 wpid = waitpid(fpid, &status, 0);
1261 ATF_REQUIRE(WIFSTOPPED(status));
1262 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1264 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1265 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1266 (PL_FLAG_EXEC | PL_FLAG_SCX));
1267 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1269 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1271 /* The last event should be for the child process's exit. */
1272 wpid = waitpid(fpid, &status, 0);
1273 ATF_REQUIRE(WIFEXITED(status));
1274 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1276 wpid = wait(&status);
1277 ATF_REQUIRE(wpid == -1);
1278 ATF_REQUIRE(errno == ECHILD);
1282 handler(int sig __unused)
1290 signal(SIGINFO, handler);
1296 * Verify that the expected ptrace event is reported for a signal.
1298 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1299 ATF_TC_BODY(ptrace__siginfo, tc)
1301 struct ptrace_lwpinfo pl;
1305 ATF_REQUIRE((fpid = fork()) != -1);
1311 /* The first wait() should report the stop from SIGSTOP. */
1312 wpid = waitpid(fpid, &status, 0);
1313 ATF_REQUIRE(wpid == fpid);
1314 ATF_REQUIRE(WIFSTOPPED(status));
1315 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1317 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1319 /* The next event should be for the SIGINFO. */
1320 wpid = waitpid(fpid, &status, 0);
1321 ATF_REQUIRE(WIFSTOPPED(status));
1322 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1324 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1325 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1326 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1327 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1328 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1330 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1332 /* The last event should be for the child process's exit. */
1333 wpid = waitpid(fpid, &status, 0);
1334 ATF_REQUIRE(WIFEXITED(status));
1335 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1337 wpid = wait(&status);
1338 ATF_REQUIRE(wpid == -1);
1339 ATF_REQUIRE(errno == ECHILD);
1343 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1345 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1346 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1351 ATF_REQUIRE((fpid = fork()) != -1);
1357 /* The first wait() should report the stop from SIGSTOP. */
1358 wpid = waitpid(fpid, &status, 0);
1359 ATF_REQUIRE(wpid == fpid);
1360 ATF_REQUIRE(WIFSTOPPED(status));
1361 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1364 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1365 sizeof(events)) == 0);
1367 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1369 /* Should get one event at exit. */
1370 wpid = waitpid(fpid, &status, 0);
1371 ATF_REQUIRE(WIFEXITED(status));
1372 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1374 wpid = wait(&status);
1375 ATF_REQUIRE(wpid == -1);
1376 ATF_REQUIRE(errno == ECHILD);
1379 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1380 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1382 struct ptrace_lwpinfo pl;
1386 ATF_REQUIRE((fpid = fork()) != -1);
1392 /* The first wait() should report the stop from SIGSTOP. */
1393 wpid = waitpid(fpid, &status, 0);
1394 ATF_REQUIRE(wpid == fpid);
1395 ATF_REQUIRE(WIFSTOPPED(status));
1396 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1398 events = PTRACE_EXEC;
1399 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1400 sizeof(events)) == 0);
1402 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1404 /* The next event should be for the child process's exec. */
1405 wpid = waitpid(fpid, &status, 0);
1406 ATF_REQUIRE(WIFSTOPPED(status));
1407 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1409 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1410 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1411 (PL_FLAG_EXEC | PL_FLAG_SCX));
1413 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1415 /* The last event should be for the child process's exit. */
1416 wpid = waitpid(fpid, &status, 0);
1417 ATF_REQUIRE(WIFEXITED(status));
1418 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1420 wpid = wait(&status);
1421 ATF_REQUIRE(wpid == -1);
1422 ATF_REQUIRE(errno == ECHILD);
1425 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1426 ATF_TC_BODY(ptrace__event_mask, tc)
1431 ATF_REQUIRE((fpid = fork()) != -1);
1437 /* The first wait() should report the stop from SIGSTOP. */
1438 wpid = waitpid(fpid, &status, 0);
1439 ATF_REQUIRE(wpid == fpid);
1440 ATF_REQUIRE(WIFSTOPPED(status));
1441 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1443 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1444 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1445 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1446 sizeof(events)) == 0);
1447 ATF_REQUIRE(events & PTRACE_FORK);
1448 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1449 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1450 sizeof(events)) == 0);
1451 ATF_REQUIRE(!(events & PTRACE_FORK));
1453 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1454 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1455 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1456 sizeof(events)) == 0);
1457 ATF_REQUIRE(events & PTRACE_LWP);
1458 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1459 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1460 sizeof(events)) == 0);
1461 ATF_REQUIRE(!(events & PTRACE_LWP));
1463 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1465 /* Should get one event at exit. */
1466 wpid = waitpid(fpid, &status, 0);
1467 ATF_REQUIRE(WIFEXITED(status));
1468 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1470 wpid = wait(&status);
1471 ATF_REQUIRE(wpid == -1);
1472 ATF_REQUIRE(errno == ECHILD);
1476 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1478 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1479 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1481 struct ptrace_lwpinfo pl;
1485 ATF_REQUIRE((fpid = fork()) != -1);
1488 follow_fork_parent(true);
1491 /* The first wait() should report the stop from SIGSTOP. */
1492 wpid = waitpid(fpid, &status, 0);
1493 ATF_REQUIRE(wpid == fpid);
1494 ATF_REQUIRE(WIFSTOPPED(status));
1495 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1497 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1498 sizeof(events)) == 0);
1499 events |= PTRACE_VFORK;
1500 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1501 sizeof(events)) == 0);
1503 /* Continue the child ignoring the SIGSTOP. */
1504 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1506 /* The next event should report the end of the vfork. */
1507 wpid = wait(&status);
1508 ATF_REQUIRE(wpid == fpid);
1509 ATF_REQUIRE(WIFSTOPPED(status));
1510 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1511 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1512 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1514 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1516 wpid = wait(&status);
1517 ATF_REQUIRE(wpid == fpid);
1518 ATF_REQUIRE(WIFEXITED(status));
1519 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1521 wpid = wait(&status);
1522 ATF_REQUIRE(wpid == -1);
1523 ATF_REQUIRE(errno == ECHILD);
1526 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1527 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1529 struct ptrace_lwpinfo pl[2];
1530 pid_t children[2], fpid, wpid;
1533 ATF_REQUIRE((fpid = fork()) != -1);
1536 follow_fork_parent(true);
1539 /* Parent process. */
1542 /* The first wait() should report the stop from SIGSTOP. */
1543 wpid = waitpid(children[0], &status, 0);
1544 ATF_REQUIRE(wpid == children[0]);
1545 ATF_REQUIRE(WIFSTOPPED(status));
1546 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1548 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1549 sizeof(events)) == 0);
1550 events |= PTRACE_FORK | PTRACE_VFORK;
1551 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1552 sizeof(events)) == 0);
1554 /* Continue the child ignoring the SIGSTOP. */
1555 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1557 /* Wait for both halves of the fork event to get reported. */
1558 children[1] = handle_fork_events(children[0], pl);
1559 ATF_REQUIRE(children[1] > 0);
1561 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1563 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1564 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1567 * The child can't exit until the grandchild reports status, so the
1568 * grandchild should report its exit first to the debugger.
1570 wpid = waitpid(children[1], &status, 0);
1571 ATF_REQUIRE(wpid == children[1]);
1572 ATF_REQUIRE(WIFEXITED(status));
1573 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1576 * The child should report it's vfork() completion before it
1579 wpid = wait(&status);
1580 ATF_REQUIRE(wpid == children[0]);
1581 ATF_REQUIRE(WIFSTOPPED(status));
1582 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1583 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1585 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1587 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1589 wpid = wait(&status);
1590 ATF_REQUIRE(wpid == children[0]);
1591 ATF_REQUIRE(WIFEXITED(status));
1592 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1594 wpid = wait(&status);
1595 ATF_REQUIRE(wpid == -1);
1596 ATF_REQUIRE(errno == ECHILD);
1600 * XXX: There's nothing inherently platform specific about this test, however a
1601 * userspace visible breakpoint() is a prerequisite.
1603 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1605 * Verify that no more events are reported after PT_KILL except for the
1606 * process exit when stopped due to a breakpoint trap.
1608 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1609 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1614 ATF_REQUIRE((fpid = fork()) != -1);
1621 /* The first wait() should report the stop from SIGSTOP. */
1622 wpid = waitpid(fpid, &status, 0);
1623 ATF_REQUIRE(wpid == fpid);
1624 ATF_REQUIRE(WIFSTOPPED(status));
1625 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1627 /* Continue the child ignoring the SIGSTOP. */
1628 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1630 /* The second wait() should report hitting the breakpoint. */
1631 wpid = waitpid(fpid, &status, 0);
1632 ATF_REQUIRE(wpid == fpid);
1633 ATF_REQUIRE(WIFSTOPPED(status));
1634 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1636 /* Kill the child process. */
1637 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1639 /* The last wait() should report the SIGKILL. */
1640 wpid = waitpid(fpid, &status, 0);
1641 ATF_REQUIRE(wpid == fpid);
1642 ATF_REQUIRE(WIFSIGNALED(status));
1643 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1645 wpid = wait(&status);
1646 ATF_REQUIRE(wpid == -1);
1647 ATF_REQUIRE(errno == ECHILD);
1649 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1652 * Verify that no more events are reported after PT_KILL except for the
1653 * process exit when stopped inside of a system call.
1655 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1656 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1658 struct ptrace_lwpinfo pl;
1662 ATF_REQUIRE((fpid = fork()) != -1);
1669 /* The first wait() should report the stop from SIGSTOP. */
1670 wpid = waitpid(fpid, &status, 0);
1671 ATF_REQUIRE(wpid == fpid);
1672 ATF_REQUIRE(WIFSTOPPED(status));
1673 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1675 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1676 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1678 /* The second wait() should report a system call entry for getpid(). */
1679 wpid = waitpid(fpid, &status, 0);
1680 ATF_REQUIRE(wpid == fpid);
1681 ATF_REQUIRE(WIFSTOPPED(status));
1682 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1684 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1685 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1687 /* Kill the child process. */
1688 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1690 /* The last wait() should report the SIGKILL. */
1691 wpid = waitpid(fpid, &status, 0);
1692 ATF_REQUIRE(wpid == fpid);
1693 ATF_REQUIRE(WIFSIGNALED(status));
1694 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1696 wpid = wait(&status);
1697 ATF_REQUIRE(wpid == -1);
1698 ATF_REQUIRE(errno == ECHILD);
1702 * Verify that no more events are reported after PT_KILL except for the
1703 * process exit when killing a multithreaded process.
1705 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1706 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1708 struct ptrace_lwpinfo pl;
1713 ATF_REQUIRE((fpid = fork()) != -1);
1716 simple_thread_main();
1719 /* The first wait() should report the stop from SIGSTOP. */
1720 wpid = waitpid(fpid, &status, 0);
1721 ATF_REQUIRE(wpid == fpid);
1722 ATF_REQUIRE(WIFSTOPPED(status));
1723 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1725 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1727 main_lwp = pl.pl_lwpid;
1729 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1731 /* Continue the child ignoring the SIGSTOP. */
1732 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1734 /* The first event should be for the child thread's birth. */
1735 wpid = waitpid(fpid, &status, 0);
1736 ATF_REQUIRE(wpid == fpid);
1737 ATF_REQUIRE(WIFSTOPPED(status));
1738 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1740 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1741 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1742 (PL_FLAG_BORN | PL_FLAG_SCX));
1743 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1745 /* Kill the child process. */
1746 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1748 /* The last wait() should report the SIGKILL. */
1749 wpid = waitpid(fpid, &status, 0);
1750 ATF_REQUIRE(wpid == fpid);
1751 ATF_REQUIRE(WIFSIGNALED(status));
1752 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1754 wpid = wait(&status);
1755 ATF_REQUIRE(wpid == -1);
1756 ATF_REQUIRE(errno == ECHILD);
1760 mask_usr1_thread(void *arg)
1762 pthread_barrier_t *pbarrier;
1765 pbarrier = (pthread_barrier_t*)arg;
1767 sigemptyset(&sigmask);
1768 sigaddset(&sigmask, SIGUSR1);
1769 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1771 /* Sync up with other thread after sigmask updated. */
1772 pthread_barrier_wait(pbarrier);
1781 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1782 * and prevents spurious stops due to those other signals.
1784 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1785 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1791 pthread_barrier_t barrier;
1792 struct sched_param sched_param;
1794 ATF_REQUIRE((fpid = fork()) != -1);
1796 /* Bind to one CPU so only one thread at a time will run. */
1798 CPU_SET(0, &setmask);
1800 CHILD_REQUIRE(cpuset(&setid) == 0);
1801 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1802 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1804 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1806 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1807 (void*)&barrier) == 0);
1810 * Give the main thread higher priority. The test always
1811 * assumes that, if both threads are able to run, the main
1812 * thread runs first.
1814 sched_param.sched_priority =
1815 (sched_get_priority_max(SCHED_FIFO) +
1816 sched_get_priority_min(SCHED_FIFO)) / 2;
1817 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1818 SCHED_FIFO, &sched_param) == 0);
1819 sched_param.sched_priority -= RQ_PPQ;
1820 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1821 &sched_param) == 0);
1824 sigemptyset(&sigmask);
1825 sigaddset(&sigmask, SIGUSR2);
1826 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1828 /* Sync up with other thread after sigmask updated. */
1829 pthread_barrier_wait(&barrier);
1839 /* The first wait() should report the stop from SIGSTOP. */
1840 wpid = waitpid(fpid, &status, 0);
1841 ATF_REQUIRE(wpid == fpid);
1842 ATF_REQUIRE(WIFSTOPPED(status));
1843 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1845 /* Continue the child ignoring the SIGSTOP. */
1846 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1848 /* Send a signal that only the second thread can handle. */
1849 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1851 /* The second wait() should report the SIGUSR2. */
1852 wpid = waitpid(fpid, &status, 0);
1853 ATF_REQUIRE(wpid == fpid);
1854 ATF_REQUIRE(WIFSTOPPED(status));
1855 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1857 /* Send a signal that only the first thread can handle. */
1858 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1860 /* Replace the SIGUSR2 with a kill. */
1861 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1863 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1864 wpid = waitpid(fpid, &status, 0);
1865 ATF_REQUIRE(wpid == fpid);
1866 ATF_REQUIRE(WIFSIGNALED(status));
1867 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1869 wpid = wait(&status);
1870 ATF_REQUIRE(wpid == -1);
1871 ATF_REQUIRE(errno == ECHILD);
1875 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1876 * and prevents spurious stops caused by those events.
1878 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1879 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1885 pthread_barrier_t barrier;
1887 struct ptrace_lwpinfo pl;
1888 struct sched_param sched_param;
1890 ATF_REQUIRE((fpid = fork()) != -1);
1894 /* Bind to one CPU so only one thread at a time will run. */
1896 CPU_SET(0, &setmask);
1898 CHILD_REQUIRE(cpuset(&setid) == 0);
1899 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1900 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1902 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1904 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1905 (void*)&barrier) == 0);
1908 * Give the main thread higher priority. The test always
1909 * assumes that, if both threads are able to run, the main
1910 * thread runs first.
1912 sched_param.sched_priority =
1913 (sched_get_priority_max(SCHED_FIFO) +
1914 sched_get_priority_min(SCHED_FIFO)) / 2;
1915 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1916 SCHED_FIFO, &sched_param) == 0);
1917 sched_param.sched_priority -= RQ_PPQ;
1918 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1919 &sched_param) == 0);
1922 sigemptyset(&sigmask);
1923 sigaddset(&sigmask, SIGUSR2);
1924 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1926 /* Sync up with other thread after sigmask updated. */
1927 pthread_barrier_wait(&barrier);
1929 /* Sync up with the test before doing the getpid(). */
1936 /* The first wait() should report the stop from SIGSTOP. */
1937 wpid = waitpid(fpid, &status, 0);
1938 ATF_REQUIRE(wpid == fpid);
1939 ATF_REQUIRE(WIFSTOPPED(status));
1940 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1942 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1943 main_lwp = pl.pl_lwpid;
1945 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1946 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1949 * Continue until child is done with setup, which is indicated with
1950 * SIGSTOP. Ignore system calls in the meantime.
1953 wpid = waitpid(fpid, &status, 0);
1954 ATF_REQUIRE(wpid == fpid);
1955 ATF_REQUIRE(WIFSTOPPED(status));
1956 if (WSTOPSIG(status) == SIGTRAP) {
1957 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1959 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
1961 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1964 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1967 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
1968 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1970 wpid = waitpid(fpid, &status, 0);
1971 ATF_REQUIRE(wpid == fpid);
1972 ATF_REQUIRE(WIFSTOPPED(status));
1973 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1975 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1977 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
1978 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1979 /* Prevent the main thread from hitting its syscall exit for now. */
1980 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
1983 * Proceed, allowing second thread to hit syscall exit for
1984 * pthread_barrier_wait().
1986 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1988 wpid = waitpid(fpid, &status, 0);
1989 ATF_REQUIRE(wpid == fpid);
1990 ATF_REQUIRE(WIFSTOPPED(status));
1991 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1993 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1995 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1996 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
1998 /* Send a signal that only the second thread can handle. */
1999 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2001 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2003 /* The next wait() should report the SIGUSR2. */
2004 wpid = waitpid(fpid, &status, 0);
2005 ATF_REQUIRE(wpid == fpid);
2006 ATF_REQUIRE(WIFSTOPPED(status));
2007 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2009 /* Allow the main thread to try to finish its system call. */
2010 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2013 * At this point, the main thread is in the middle of a system call and
2014 * has been resumed. The second thread has taken a SIGUSR2 which will
2015 * be replaced with a SIGKILL below. The main thread will get to run
2016 * first. It should notice the kill request (even though the signal
2017 * replacement occurred in the other thread) and exit accordingly. It
2018 * should not stop for the system call exit event.
2021 /* Replace the SIGUSR2 with a kill. */
2022 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2024 /* The last wait() should report the SIGKILL (not a syscall exit). */
2025 wpid = waitpid(fpid, &status, 0);
2026 ATF_REQUIRE(wpid == fpid);
2027 ATF_REQUIRE(WIFSIGNALED(status));
2028 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2030 wpid = wait(&status);
2031 ATF_REQUIRE(wpid == -1);
2032 ATF_REQUIRE(errno == ECHILD);
2036 sigusr1_handler(int sig)
2039 CHILD_REQUIRE(sig == SIGUSR1);
2044 * Verify that even if the signal queue is full for a child process,
2045 * a PT_KILL will kill the process.
2047 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2048 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2052 int max_pending_per_proc;
2056 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2058 ATF_REQUIRE((fpid = fork()) != -1);
2064 /* The first wait() should report the stop from SIGSTOP. */
2065 wpid = waitpid(fpid, &status, 0);
2066 ATF_REQUIRE(wpid == fpid);
2067 ATF_REQUIRE(WIFSTOPPED(status));
2068 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2070 len = sizeof(max_pending_per_proc);
2071 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2072 &max_pending_per_proc, &len, NULL, 0) == 0);
2074 /* Fill the signal queue. */
2075 for (i = 0; i < max_pending_per_proc; ++i)
2076 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2078 /* Kill the child process. */
2079 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2081 /* The last wait() should report the SIGKILL. */
2082 wpid = waitpid(fpid, &status, 0);
2083 ATF_REQUIRE(wpid == fpid);
2084 ATF_REQUIRE(WIFSIGNALED(status));
2085 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2087 wpid = wait(&status);
2088 ATF_REQUIRE(wpid == -1);
2089 ATF_REQUIRE(errno == ECHILD);
2093 * Verify that when stopped at a system call entry, a signal can be
2094 * requested with PT_CONTINUE which will be delivered once the system
2097 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2098 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2100 struct ptrace_lwpinfo pl;
2104 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2106 ATF_REQUIRE((fpid = fork()) != -1);
2113 /* The first wait() should report the stop from SIGSTOP. */
2114 wpid = waitpid(fpid, &status, 0);
2115 ATF_REQUIRE(wpid == fpid);
2116 ATF_REQUIRE(WIFSTOPPED(status));
2117 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2119 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2120 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2122 /* The second wait() should report a system call entry for getpid(). */
2123 wpid = waitpid(fpid, &status, 0);
2124 ATF_REQUIRE(wpid == fpid);
2125 ATF_REQUIRE(WIFSTOPPED(status));
2126 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2128 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2129 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2131 /* Continue the child process with a signal. */
2132 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2136 * The last wait() should report exit 2, i.e., a normal _exit
2137 * from the signal handler. In the meantime, catch and proceed
2138 * past any syscall stops.
2140 wpid = waitpid(fpid, &status, 0);
2141 ATF_REQUIRE(wpid == fpid);
2142 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2143 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2144 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2145 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2147 ATF_REQUIRE(WIFEXITED(status));
2148 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2153 wpid = wait(&status);
2154 ATF_REQUIRE(wpid == -1);
2155 ATF_REQUIRE(errno == ECHILD);
2159 sigusr1_counting_handler(int sig)
2161 static int counter = 0;
2163 CHILD_REQUIRE(sig == SIGUSR1);
2170 * Verify that, when continuing from a stop at system call entry and exit,
2171 * a signal can be requested from both stops, and both will be delivered when
2172 * the system call is complete.
2174 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2175 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2177 struct ptrace_lwpinfo pl;
2181 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2183 ATF_REQUIRE((fpid = fork()) != -1);
2190 /* The first wait() should report the stop from SIGSTOP. */
2191 wpid = waitpid(fpid, &status, 0);
2192 ATF_REQUIRE(wpid == fpid);
2193 ATF_REQUIRE(WIFSTOPPED(status));
2194 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2196 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2197 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2199 /* The second wait() should report a system call entry for getpid(). */
2200 wpid = waitpid(fpid, &status, 0);
2201 ATF_REQUIRE(wpid == fpid);
2202 ATF_REQUIRE(WIFSTOPPED(status));
2203 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2205 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2206 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2208 /* Continue the child process with a signal. */
2209 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2211 /* The third wait() should report a system call exit for getpid(). */
2212 wpid = waitpid(fpid, &status, 0);
2213 ATF_REQUIRE(wpid == fpid);
2214 ATF_REQUIRE(WIFSTOPPED(status));
2215 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2217 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2218 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2220 /* Continue the child process with a signal. */
2221 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2225 * The last wait() should report exit 2, i.e., a normal _exit
2226 * from the signal handler. In the meantime, catch and proceed
2227 * past any syscall stops.
2229 wpid = waitpid(fpid, &status, 0);
2230 ATF_REQUIRE(wpid == fpid);
2231 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2232 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2233 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2234 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2236 ATF_REQUIRE(WIFEXITED(status));
2237 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2242 wpid = wait(&status);
2243 ATF_REQUIRE(wpid == -1);
2244 ATF_REQUIRE(errno == ECHILD);
2248 * Verify that even if the signal queue is full for a child process,
2249 * a PT_CONTINUE with a signal will not result in loss of that signal.
2251 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2252 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2256 int max_pending_per_proc;
2260 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2261 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2263 ATF_REQUIRE((fpid = fork()) != -1);
2269 /* The first wait() should report the stop from SIGSTOP. */
2270 wpid = waitpid(fpid, &status, 0);
2271 ATF_REQUIRE(wpid == fpid);
2272 ATF_REQUIRE(WIFSTOPPED(status));
2273 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2275 len = sizeof(max_pending_per_proc);
2276 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2277 &max_pending_per_proc, &len, NULL, 0) == 0);
2279 /* Fill the signal queue. */
2280 for (i = 0; i < max_pending_per_proc; ++i)
2281 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2283 /* Continue with signal. */
2284 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2287 wpid = waitpid(fpid, &status, 0);
2288 ATF_REQUIRE(wpid == fpid);
2289 if (WIFSTOPPED(status)) {
2290 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2291 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2294 * The last wait() should report normal _exit from the
2297 ATF_REQUIRE(WIFEXITED(status));
2298 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2303 wpid = wait(&status);
2304 ATF_REQUIRE(wpid == -1);
2305 ATF_REQUIRE(errno == ECHILD);
2309 * Verify that, after stopping due to a signal, that signal can be
2310 * replaced with another signal.
2312 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2313 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2315 struct ptrace_lwpinfo pl;
2319 ATF_REQUIRE((fpid = fork()) != -1);
2326 /* The first wait() should report the stop from SIGSTOP. */
2327 wpid = waitpid(fpid, &status, 0);
2328 ATF_REQUIRE(wpid == fpid);
2329 ATF_REQUIRE(WIFSTOPPED(status));
2330 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2332 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2334 /* Send a signal without ptrace. */
2335 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2337 /* The second wait() should report a SIGINT was received. */
2338 wpid = waitpid(fpid, &status, 0);
2339 ATF_REQUIRE(wpid == fpid);
2340 ATF_REQUIRE(WIFSTOPPED(status));
2341 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2343 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2344 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2345 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2347 /* Continue the child process with a different signal. */
2348 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2351 * The last wait() should report having died due to the new
2354 wpid = waitpid(fpid, &status, 0);
2355 ATF_REQUIRE(wpid == fpid);
2356 ATF_REQUIRE(WIFSIGNALED(status));
2357 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2359 wpid = wait(&status);
2360 ATF_REQUIRE(wpid == -1);
2361 ATF_REQUIRE(errno == ECHILD);
2365 * Verify that a signal can be passed through to the child even when there
2366 * was no true signal originally. Such cases arise when a SIGTRAP is
2367 * invented for e.g, system call stops.
2369 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2370 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2372 struct ptrace_lwpinfo pl;
2376 ATF_REQUIRE((fpid = fork()) != -1);
2383 /* The first wait() should report the stop from SIGSTOP. */
2384 wpid = waitpid(fpid, &status, 0);
2385 ATF_REQUIRE(wpid == fpid);
2386 ATF_REQUIRE(WIFSTOPPED(status));
2387 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2389 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2390 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2392 /* The second wait() should report a system call entry for getpid(). */
2393 wpid = waitpid(fpid, &status, 0);
2394 ATF_REQUIRE(wpid == fpid);
2395 ATF_REQUIRE(WIFSTOPPED(status));
2396 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2398 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2399 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2401 /* Continue the child process with a SIGTRAP. */
2402 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2406 * The last wait() should report exit due to SIGTRAP. In the
2407 * meantime, catch and proceed past any syscall stops.
2409 wpid = waitpid(fpid, &status, 0);
2410 ATF_REQUIRE(wpid == fpid);
2411 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2412 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2413 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2414 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2416 ATF_REQUIRE(WIFSIGNALED(status));
2417 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2422 wpid = wait(&status);
2423 ATF_REQUIRE(wpid == -1);
2424 ATF_REQUIRE(errno == ECHILD);
2429 * A mixed bag PT_CONTINUE with signal test.
2431 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2432 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2434 struct ptrace_lwpinfo pl;
2438 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2440 ATF_REQUIRE((fpid = fork()) != -1);
2447 /* The first wait() should report the stop from SIGSTOP. */
2448 wpid = waitpid(fpid, &status, 0);
2449 ATF_REQUIRE(wpid == fpid);
2450 ATF_REQUIRE(WIFSTOPPED(status));
2451 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2453 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2454 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2456 /* The second wait() should report a system call entry for getpid(). */
2457 wpid = waitpid(fpid, &status, 0);
2458 ATF_REQUIRE(wpid == fpid);
2459 ATF_REQUIRE(WIFSTOPPED(status));
2460 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2462 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2463 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2465 /* Continue with the first SIGUSR1. */
2466 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2468 /* The next wait() should report a system call exit for getpid(). */
2469 wpid = waitpid(fpid, &status, 0);
2470 ATF_REQUIRE(wpid == fpid);
2471 ATF_REQUIRE(WIFSTOPPED(status));
2472 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2474 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2475 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2477 /* Send an ABRT without ptrace. */
2478 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2480 /* Continue normally. */
2481 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2483 /* The next wait() should report the SIGABRT. */
2484 wpid = waitpid(fpid, &status, 0);
2485 ATF_REQUIRE(wpid == fpid);
2486 ATF_REQUIRE(WIFSTOPPED(status));
2487 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2489 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2490 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2491 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2493 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2494 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2498 * The last wait() should report exit 2, i.e., a normal _exit
2499 * from the signal handler. In the meantime, catch and proceed
2500 * past any syscall stops.
2502 wpid = waitpid(fpid, &status, 0);
2503 ATF_REQUIRE(wpid == fpid);
2504 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2505 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2506 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2507 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2509 ATF_REQUIRE(WIFEXITED(status));
2510 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2515 wpid = wait(&status);
2516 ATF_REQUIRE(wpid == -1);
2517 ATF_REQUIRE(errno == ECHILD);
2522 * Verify a signal delivered by ptrace is noticed by kevent(2).
2524 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2525 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2528 int status, kq, nevents;
2531 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2533 ATF_REQUIRE((fpid = fork()) != -1);
2535 CHILD_REQUIRE((kq = kqueue()) > 0);
2536 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2537 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2542 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2543 if (nevents == -1 && errno == EINTR)
2545 CHILD_REQUIRE(nevents > 0);
2546 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2547 CHILD_REQUIRE(kev.ident == SIGUSR1);
2554 /* The first wait() should report the stop from SIGSTOP. */
2555 wpid = waitpid(fpid, &status, 0);
2556 ATF_REQUIRE(wpid == fpid);
2557 ATF_REQUIRE(WIFSTOPPED(status));
2558 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2560 /* Continue with the SIGUSR1. */
2561 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2564 * The last wait() should report normal exit with code 1.
2566 wpid = waitpid(fpid, &status, 0);
2567 ATF_REQUIRE(wpid == fpid);
2568 ATF_REQUIRE(WIFEXITED(status));
2569 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2571 wpid = wait(&status);
2572 ATF_REQUIRE(wpid == -1);
2573 ATF_REQUIRE(errno == ECHILD);
2576 static sem_t sigusr1_sem;
2579 sigusr1_sempost_handler(int sig __unused)
2582 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2586 signal_thread(void *arg)
2591 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2593 /* Wait for this thread to receive a SIGUSR1. */
2595 err = sem_wait(&sigusr1_sem);
2596 CHILD_REQUIRE(err == 0 || errno == EINTR);
2597 } while (err != 0 && errno == EINTR);
2599 /* Free our companion thread from the barrier. */
2600 pthread_barrier_wait(pbarrier);
2603 * Swap ignore duties; the next SIGUSR1 should go to the
2606 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2607 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2608 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2610 /* Sync up threads after swapping signal masks. */
2611 pthread_barrier_wait(pbarrier);
2613 /* Wait until our companion has received its SIGUSR1. */
2614 pthread_barrier_wait(pbarrier);
2620 * Verify that if ptrace stops due to a signal but continues with
2621 * a different signal that the new signal is routed to a thread
2622 * that can accept it, and that that thread is awakened by the signal
2623 * in a timely manner.
2625 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2626 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2632 pthread_barrier_t barrier;
2634 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2635 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2636 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2638 ATF_REQUIRE((fpid = fork()) != -1);
2640 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2642 /* The other thread should receive the first SIGUSR1. */
2643 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2644 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2645 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2649 /* Wait until other thread has received its SIGUSR1. */
2650 pthread_barrier_wait(&barrier);
2653 * Swap ignore duties; the next SIGUSR1 should go to this
2656 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2658 /* Sync up threads after swapping signal masks. */
2659 pthread_barrier_wait(&barrier);
2662 * Sync up with test code; we're ready for the next SIGUSR1
2667 /* Wait for this thread to receive a SIGUSR1. */
2669 err = sem_wait(&sigusr1_sem);
2670 CHILD_REQUIRE(err == 0 || errno == EINTR);
2671 } while (err != 0 && errno == EINTR);
2673 /* Free the other thread from the barrier. */
2674 pthread_barrier_wait(&barrier);
2676 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2681 /* The first wait() should report the stop from SIGSTOP. */
2682 wpid = waitpid(fpid, &status, 0);
2683 ATF_REQUIRE(wpid == fpid);
2684 ATF_REQUIRE(WIFSTOPPED(status));
2685 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2687 /* Continue the child ignoring the SIGSTOP. */
2688 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2691 * Send a signal without ptrace that either thread will accept (USR2,
2694 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2696 /* The second wait() should report a SIGUSR2 was received. */
2697 wpid = waitpid(fpid, &status, 0);
2698 ATF_REQUIRE(wpid == fpid);
2699 ATF_REQUIRE(WIFSTOPPED(status));
2700 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2702 /* Continue the child, changing the signal to USR1. */
2703 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2705 /* The next wait() should report the stop from SIGSTOP. */
2706 wpid = waitpid(fpid, &status, 0);
2707 ATF_REQUIRE(wpid == fpid);
2708 ATF_REQUIRE(WIFSTOPPED(status));
2709 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2711 /* Continue the child ignoring the SIGSTOP. */
2712 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2714 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2716 /* The next wait() should report a SIGUSR2 was received. */
2717 wpid = waitpid(fpid, &status, 0);
2718 ATF_REQUIRE(wpid == fpid);
2719 ATF_REQUIRE(WIFSTOPPED(status));
2720 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2722 /* Continue the child, changing the signal to USR1. */
2723 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2725 /* The last wait() should report normal exit with code 1. */
2726 wpid = waitpid(fpid, &status, 0);
2727 ATF_REQUIRE(wpid == fpid);
2728 ATF_REQUIRE(WIFEXITED(status));
2729 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2731 wpid = wait(&status);
2732 ATF_REQUIRE(wpid == -1);
2733 ATF_REQUIRE(errno == ECHILD);
2737 raise_sigstop_thread(void *arg __unused)
2745 sleep_thread(void *arg __unused)
2753 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
2762 * Become the reaper for this process tree. We need to be able to check
2763 * that both child and grandchild have died.
2765 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
2768 ATF_REQUIRE(fpid >= 0);
2771 CHILD_REQUIRE(fpid >= 0);
2775 /* Pin to CPU 0 to serialize thread execution. */
2777 CPU_SET(0, &setmask);
2778 CHILD_REQUIRE(cpuset(&setid) == 0);
2779 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2780 CPU_WHICH_CPUSET, setid,
2781 sizeof(setmask), &setmask) == 0);
2783 if (sigstop_from_main_thread) {
2785 * We expect the SIGKILL sent when our parent
2786 * dies to be delivered to the new thread.
2787 * Raise the SIGSTOP in this thread so the
2790 CHILD_REQUIRE(pthread_create(&t, NULL,
2791 sleep_thread, NULL) == 0);
2795 * We expect the SIGKILL to be delivered to
2796 * this thread. After creating the new thread,
2797 * just get off the CPU so the other thread can
2798 * raise the SIGSTOP.
2800 CHILD_REQUIRE(pthread_create(&t, NULL,
2801 raise_sigstop_thread, NULL) == 0);
2807 /* First stop is trace_me() immediately after fork. */
2808 wpid = waitpid(fpid, &status, 0);
2809 CHILD_REQUIRE(wpid == fpid);
2810 CHILD_REQUIRE(WIFSTOPPED(status));
2811 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2813 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2815 /* Second stop is from the raise(SIGSTOP). */
2816 wpid = waitpid(fpid, &status, 0);
2817 CHILD_REQUIRE(wpid == fpid);
2818 CHILD_REQUIRE(WIFSTOPPED(status));
2819 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2822 * Terminate tracing process without detaching. Our child
2829 * We should get a normal exit from our immediate child and a SIGKILL
2830 * exit from our grandchild. The latter case is the interesting one.
2831 * Our grandchild should not have stopped due to the SIGSTOP that was
2832 * left dangling when its parent died.
2834 for (i = 0; i < 2; ++i) {
2835 wpid = wait(&status);
2837 ATF_REQUIRE(WIFEXITED(status) &&
2838 WEXITSTATUS(status) == 0);
2840 ATF_REQUIRE(WIFSIGNALED(status) &&
2841 WTERMSIG(status) == SIGKILL);
2846 * These two tests ensure that if the tracing process exits without detaching
2847 * just after the child received a SIGSTOP, the child is cleanly killed and
2848 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
2849 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
2850 * different threads, the SIGKILL must win. There are two variants of this
2851 * test, designed to catch the case where the SIGKILL is delivered to the
2852 * younger thread (the first test) and the case where the SIGKILL is delivered
2853 * to the older thread (the second test). This behavior has changed in the
2854 * past, so make no assumption.
2856 ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop1);
2857 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
2860 terminate_with_pending_sigstop(true);
2862 ATF_TC_WITHOUT_HEAD(ptrace__parent_terminate_with_pending_sigstop2);
2863 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
2866 terminate_with_pending_sigstop(false);
2870 * Verify that after ptrace() discards a SIGKILL signal, the event mask
2873 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
2874 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
2876 struct ptrace_lwpinfo pl;
2878 int status, event_mask, new_event_mask;
2880 ATF_REQUIRE((fpid = fork()) != -1);
2887 /* The first wait() should report the stop from trace_me(). */
2888 wpid = waitpid(fpid, &status, 0);
2889 ATF_REQUIRE(wpid == fpid);
2890 ATF_REQUIRE(WIFSTOPPED(status));
2891 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2893 /* Set several unobtrusive event bits. */
2894 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
2895 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
2896 sizeof(event_mask)) == 0);
2898 /* Send a SIGKILL without using ptrace. */
2899 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
2901 /* Continue the child ignoring the SIGSTOP. */
2902 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2904 /* The next stop should be due to the SIGKILL. */
2905 wpid = waitpid(fpid, &status, 0);
2906 ATF_REQUIRE(wpid == fpid);
2907 ATF_REQUIRE(WIFSTOPPED(status));
2908 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
2910 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2911 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2912 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
2914 /* Continue the child ignoring the SIGKILL. */
2915 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2917 /* The next wait() should report the stop from SIGSTOP. */
2918 wpid = waitpid(fpid, &status, 0);
2919 ATF_REQUIRE(wpid == fpid);
2920 ATF_REQUIRE(WIFSTOPPED(status));
2921 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2923 /* Check the current event mask. It should not have changed. */
2925 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
2926 sizeof(new_event_mask)) == 0);
2927 ATF_REQUIRE(event_mask == new_event_mask);
2929 /* Continue the child to let it exit. */
2930 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2932 /* The last event should be for the child process's exit. */
2933 wpid = waitpid(fpid, &status, 0);
2934 ATF_REQUIRE(WIFEXITED(status));
2935 ATF_REQUIRE(WEXITSTATUS(status) == 0);
2937 wpid = wait(&status);
2938 ATF_REQUIRE(wpid == -1);
2939 ATF_REQUIRE(errno == ECHILD);
2945 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2946 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
2947 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
2948 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
2949 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
2950 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
2951 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
2952 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
2954 ptrace__follow_fork_child_detached_unrelated_debugger);
2956 ptrace__follow_fork_parent_detached_unrelated_debugger);
2957 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2958 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2959 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
2960 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
2961 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
2962 ATF_TP_ADD_TC(tp, ptrace__siginfo);
2963 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
2964 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
2965 ATF_TP_ADD_TC(tp, ptrace__event_mask);
2966 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2967 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2968 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
2969 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2971 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
2972 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
2973 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
2974 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
2975 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
2976 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
2978 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2979 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2980 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
2981 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2982 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
2983 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
2984 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2985 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
2986 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
2987 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
2989 return (atf_no_error());