2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
35 #include <sys/procctl.h>
36 #include <sys/ptrace.h>
37 #include <sys/queue.h>
39 #include <sys/syscall.h>
40 #include <sys/sysctl.h>
44 #include <machine/cpufunc.h>
47 #include <semaphore.h>
55 * A variant of ATF_REQUIRE that is suitable for use in child
56 * processes. This only works if the parent process is tripped up by
57 * the early exit and fails some requirement itself.
59 #define CHILD_REQUIRE(exp) do { \
61 child_fail_require(__FILE__, __LINE__, \
66 child_fail_require(const char *file, int line, const char *str)
70 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
71 write(2, buf, strlen(buf));
79 /* Attach the parent process as a tracer of this process. */
80 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
87 attach_child(pid_t pid)
92 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
94 wpid = waitpid(pid, &status, 0);
95 ATF_REQUIRE(wpid == pid);
96 ATF_REQUIRE(WIFSTOPPED(status));
97 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
101 wait_for_zombie(pid_t pid)
105 * Wait for a process to exit. This is kind of gross, but
106 * there is not a better way.
108 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
109 * with ESRCH. After that change, a valid struct kinfo_proc
110 * is returned for zombies with ki_stat set to SZOMB.
113 struct kinfo_proc kp;
119 mib[2] = KERN_PROC_PID;
122 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
123 ATF_REQUIRE(errno == ESRCH);
126 if (kp.ki_stat == SZOMB)
133 * Verify that a parent debugger process "sees" the exit of a debugged
134 * process exactly once when attached via PT_TRACE_ME.
136 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
137 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
142 ATF_REQUIRE((child = fork()) != -1);
150 /* Parent process. */
152 /* The first wait() should report the stop from SIGSTOP. */
153 wpid = waitpid(child, &status, 0);
154 ATF_REQUIRE(wpid == child);
155 ATF_REQUIRE(WIFSTOPPED(status));
156 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
158 /* Continue the child ignoring the SIGSTOP. */
159 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
161 /* The second wait() should report the exit status. */
162 wpid = waitpid(child, &status, 0);
163 ATF_REQUIRE(wpid == child);
164 ATF_REQUIRE(WIFEXITED(status));
165 ATF_REQUIRE(WEXITSTATUS(status) == 1);
167 /* The child should no longer exist. */
168 wpid = waitpid(child, &status, 0);
169 ATF_REQUIRE(wpid == -1);
170 ATF_REQUIRE(errno == ECHILD);
174 * Verify that a parent debugger process "sees" the exit of a debugged
175 * process exactly once when attached via PT_ATTACH.
177 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
178 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
181 int cpipe[2], status;
184 ATF_REQUIRE(pipe(cpipe) == 0);
185 ATF_REQUIRE((child = fork()) != -1);
190 /* Wait for the parent to attach. */
191 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
197 /* Parent process. */
199 /* Attach to the child process. */
202 /* Continue the child ignoring the SIGSTOP. */
203 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
205 /* Signal the child to exit. */
208 /* The second wait() should report the exit status. */
209 wpid = waitpid(child, &status, 0);
210 ATF_REQUIRE(wpid == child);
211 ATF_REQUIRE(WIFEXITED(status));
212 ATF_REQUIRE(WEXITSTATUS(status) == 1);
214 /* The child should no longer exist. */
215 wpid = waitpid(child, &status, 0);
216 ATF_REQUIRE(wpid == -1);
217 ATF_REQUIRE(errno == ECHILD);
221 * Verify that a parent process "sees" the exit of a debugged process only
222 * after the debugger has seen it.
224 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
225 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
227 pid_t child, debugger, wpid;
228 int cpipe[2], dpipe[2], status;
231 ATF_REQUIRE(pipe(cpipe) == 0);
232 ATF_REQUIRE((child = fork()) != -1);
238 /* Wait for parent to be ready. */
239 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
245 ATF_REQUIRE(pipe(dpipe) == 0);
246 ATF_REQUIRE((debugger = fork()) != -1);
249 /* Debugger process. */
252 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
254 wpid = waitpid(child, &status, 0);
255 CHILD_REQUIRE(wpid == child);
256 CHILD_REQUIRE(WIFSTOPPED(status));
257 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
259 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
261 /* Signal parent that debugger is attached. */
262 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
264 /* Wait for parent's failed wait. */
265 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
267 wpid = waitpid(child, &status, 0);
268 CHILD_REQUIRE(wpid == child);
269 CHILD_REQUIRE(WIFEXITED(status));
270 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
276 /* Parent process. */
278 /* Wait for the debugger to attach to the child. */
279 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
281 /* Release the child. */
282 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
283 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
286 wait_for_zombie(child);
289 * This wait should return a pid of 0 to indicate no status to
290 * report. The parent should see the child as non-exited
291 * until the debugger sees the exit.
293 wpid = waitpid(child, &status, WNOHANG);
294 ATF_REQUIRE(wpid == 0);
296 /* Signal the debugger to wait for the child. */
299 /* Wait for the debugger. */
300 wpid = waitpid(debugger, &status, 0);
301 ATF_REQUIRE(wpid == debugger);
302 ATF_REQUIRE(WIFEXITED(status));
303 ATF_REQUIRE(WEXITSTATUS(status) == 0);
305 /* The child process should now be ready. */
306 wpid = waitpid(child, &status, WNOHANG);
307 ATF_REQUIRE(wpid == child);
308 ATF_REQUIRE(WIFEXITED(status));
309 ATF_REQUIRE(WEXITSTATUS(status) == 1);
313 * Verify that a parent process "sees" the exit of a debugged process
314 * only after a non-direct-child debugger has seen it. In particular,
315 * various wait() calls in the parent must avoid failing with ESRCH by
316 * checking the parent's orphan list for the debugee.
318 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
319 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
321 pid_t child, debugger, fpid, wpid;
322 int cpipe[2], dpipe[2], status;
325 ATF_REQUIRE(pipe(cpipe) == 0);
326 ATF_REQUIRE((child = fork()) != -1);
332 /* Wait for parent to be ready. */
333 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
339 ATF_REQUIRE(pipe(dpipe) == 0);
340 ATF_REQUIRE((debugger = fork()) != -1);
343 /* Debugger parent. */
346 * Fork again and drop the debugger parent so that the
347 * debugger is not a child of the main parent.
349 CHILD_REQUIRE((fpid = fork()) != -1);
353 /* Debugger process. */
356 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
358 wpid = waitpid(child, &status, 0);
359 CHILD_REQUIRE(wpid == child);
360 CHILD_REQUIRE(WIFSTOPPED(status));
361 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
363 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
365 /* Signal parent that debugger is attached. */
366 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
368 /* Wait for parent's failed wait. */
369 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
371 wpid = waitpid(child, &status, 0);
372 CHILD_REQUIRE(wpid == child);
373 CHILD_REQUIRE(WIFEXITED(status));
374 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
380 /* Parent process. */
382 /* Wait for the debugger parent process to exit. */
383 wpid = waitpid(debugger, &status, 0);
384 ATF_REQUIRE(wpid == debugger);
385 ATF_REQUIRE(WIFEXITED(status));
386 ATF_REQUIRE(WEXITSTATUS(status) == 2);
388 /* A WNOHANG wait here should see the non-exited child. */
389 wpid = waitpid(child, &status, WNOHANG);
390 ATF_REQUIRE(wpid == 0);
392 /* Wait for the debugger to attach to the child. */
393 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
395 /* Release the child. */
396 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
397 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
400 wait_for_zombie(child);
403 * This wait should return a pid of 0 to indicate no status to
404 * report. The parent should see the child as non-exited
405 * until the debugger sees the exit.
407 wpid = waitpid(child, &status, WNOHANG);
408 ATF_REQUIRE(wpid == 0);
410 /* Signal the debugger to wait for the child. */
411 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
413 /* Wait for the debugger. */
414 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
417 /* The child process should now be ready. */
418 wpid = waitpid(child, &status, WNOHANG);
419 ATF_REQUIRE(wpid == child);
420 ATF_REQUIRE(WIFEXITED(status));
421 ATF_REQUIRE(WEXITSTATUS(status) == 1);
425 * The parent process should always act the same regardless of how the
426 * debugger is attached to it.
429 follow_fork_parent(bool use_vfork)
435 CHILD_REQUIRE((fpid = vfork()) != -1);
437 CHILD_REQUIRE((fpid = fork()) != -1);
443 wpid = waitpid(fpid, &status, 0);
444 CHILD_REQUIRE(wpid == fpid);
445 CHILD_REQUIRE(WIFEXITED(status));
446 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
452 * Helper routine for follow fork tests. This waits for two stops
453 * that report both "sides" of a fork. It returns the pid of the new
457 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
459 struct ptrace_lwpinfo pl;
460 bool fork_reported[2];
464 fork_reported[0] = false;
465 fork_reported[1] = false;
469 * Each process should report a fork event. The parent should
470 * report a PL_FLAG_FORKED event, and the child should report
471 * a PL_FLAG_CHILD event.
473 for (i = 0; i < 2; i++) {
474 wpid = wait(&status);
475 ATF_REQUIRE(wpid > 0);
476 ATF_REQUIRE(WIFSTOPPED(status));
478 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
480 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
482 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
483 (PL_FLAG_FORKED | PL_FLAG_CHILD));
484 if (pl.pl_flags & PL_FLAG_CHILD) {
485 ATF_REQUIRE(wpid != parent);
486 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
487 ATF_REQUIRE(!fork_reported[1]);
491 ATF_REQUIRE(child == wpid);
494 fork_reported[1] = true;
496 ATF_REQUIRE(wpid == parent);
497 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
498 ATF_REQUIRE(!fork_reported[0]);
500 child = pl.pl_child_pid;
502 ATF_REQUIRE(child == pl.pl_child_pid);
505 fork_reported[0] = true;
513 * Verify that a new child process is stopped after a followed fork and
514 * that the traced parent sees the exit of the child after the debugger
515 * when both processes remain attached to the debugger.
517 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
518 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
520 pid_t children[2], fpid, wpid;
523 ATF_REQUIRE((fpid = fork()) != -1);
526 follow_fork_parent(false);
529 /* Parent process. */
532 /* The first wait() should report the stop from SIGSTOP. */
533 wpid = waitpid(children[0], &status, 0);
534 ATF_REQUIRE(wpid == children[0]);
535 ATF_REQUIRE(WIFSTOPPED(status));
536 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
538 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
540 /* Continue the child ignoring the SIGSTOP. */
541 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
543 children[1] = handle_fork_events(children[0], NULL);
544 ATF_REQUIRE(children[1] > 0);
546 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
547 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
550 * The child can't exit until the grandchild reports status, so the
551 * grandchild should report its exit first to the debugger.
553 wpid = wait(&status);
554 ATF_REQUIRE(wpid == children[1]);
555 ATF_REQUIRE(WIFEXITED(status));
556 ATF_REQUIRE(WEXITSTATUS(status) == 2);
558 wpid = wait(&status);
559 ATF_REQUIRE(wpid == children[0]);
560 ATF_REQUIRE(WIFEXITED(status));
561 ATF_REQUIRE(WEXITSTATUS(status) == 1);
563 wpid = wait(&status);
564 ATF_REQUIRE(wpid == -1);
565 ATF_REQUIRE(errno == ECHILD);
569 * Verify that a new child process is stopped after a followed fork
570 * and that the traced parent sees the exit of the child when the new
571 * child process is detached after it reports its fork.
573 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
574 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
576 pid_t children[2], fpid, wpid;
579 ATF_REQUIRE((fpid = fork()) != -1);
582 follow_fork_parent(false);
585 /* Parent process. */
588 /* The first wait() should report the stop from SIGSTOP. */
589 wpid = waitpid(children[0], &status, 0);
590 ATF_REQUIRE(wpid == children[0]);
591 ATF_REQUIRE(WIFSTOPPED(status));
592 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
594 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
596 /* Continue the child ignoring the SIGSTOP. */
597 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
599 children[1] = handle_fork_events(children[0], NULL);
600 ATF_REQUIRE(children[1] > 0);
602 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
603 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
606 * Should not see any status from the grandchild now, only the
609 wpid = wait(&status);
610 ATF_REQUIRE(wpid == children[0]);
611 ATF_REQUIRE(WIFEXITED(status));
612 ATF_REQUIRE(WEXITSTATUS(status) == 1);
614 wpid = wait(&status);
615 ATF_REQUIRE(wpid == -1);
616 ATF_REQUIRE(errno == ECHILD);
620 * Verify that a new child process is stopped after a followed fork
621 * and that the traced parent sees the exit of the child when the
622 * traced parent is detached after the fork.
624 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
625 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
627 pid_t children[2], fpid, wpid;
630 ATF_REQUIRE((fpid = fork()) != -1);
633 follow_fork_parent(false);
636 /* Parent process. */
639 /* The first wait() should report the stop from SIGSTOP. */
640 wpid = waitpid(children[0], &status, 0);
641 ATF_REQUIRE(wpid == children[0]);
642 ATF_REQUIRE(WIFSTOPPED(status));
643 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
645 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
647 /* Continue the child ignoring the SIGSTOP. */
648 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
650 children[1] = handle_fork_events(children[0], NULL);
651 ATF_REQUIRE(children[1] > 0);
653 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
654 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
657 * The child can't exit until the grandchild reports status, so the
658 * grandchild should report its exit first to the debugger.
660 * Even though the child process is detached, it is still a
661 * child of the debugger, so it will still report it's exit
662 * after the grandchild.
664 wpid = wait(&status);
665 ATF_REQUIRE(wpid == children[1]);
666 ATF_REQUIRE(WIFEXITED(status));
667 ATF_REQUIRE(WEXITSTATUS(status) == 2);
669 wpid = wait(&status);
670 ATF_REQUIRE(wpid == children[0]);
671 ATF_REQUIRE(WIFEXITED(status));
672 ATF_REQUIRE(WEXITSTATUS(status) == 1);
674 wpid = wait(&status);
675 ATF_REQUIRE(wpid == -1);
676 ATF_REQUIRE(errno == ECHILD);
680 attach_fork_parent(int cpipe[2])
686 /* Double-fork to disassociate from the debugger. */
687 CHILD_REQUIRE((fpid = fork()) != -1);
691 /* Send the pid of the disassociated child to the debugger. */
693 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
695 /* Wait for the debugger to attach. */
696 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
700 * Verify that a new child process is stopped after a followed fork and
701 * that the traced parent sees the exit of the child after the debugger
702 * when both processes remain attached to the debugger. In this test
703 * the parent that forks is not a direct child of the debugger.
705 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
706 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
708 pid_t children[2], fpid, wpid;
709 int cpipe[2], status;
711 ATF_REQUIRE(pipe(cpipe) == 0);
712 ATF_REQUIRE((fpid = fork()) != -1);
714 attach_fork_parent(cpipe);
715 follow_fork_parent(false);
718 /* Parent process. */
721 /* Wait for the direct child to exit. */
722 wpid = waitpid(fpid, &status, 0);
723 ATF_REQUIRE(wpid == fpid);
724 ATF_REQUIRE(WIFEXITED(status));
725 ATF_REQUIRE(WEXITSTATUS(status) == 3);
727 /* Read the pid of the fork parent. */
728 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
729 sizeof(children[0]));
731 /* Attach to the fork parent. */
732 attach_child(children[0]);
734 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
736 /* Continue the fork parent ignoring the SIGSTOP. */
737 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
739 /* Signal the fork parent to continue. */
742 children[1] = handle_fork_events(children[0], NULL);
743 ATF_REQUIRE(children[1] > 0);
745 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
746 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
749 * The fork parent can't exit until the child reports status,
750 * so the child should report its exit first to the debugger.
752 wpid = wait(&status);
753 ATF_REQUIRE(wpid == children[1]);
754 ATF_REQUIRE(WIFEXITED(status));
755 ATF_REQUIRE(WEXITSTATUS(status) == 2);
757 wpid = wait(&status);
758 ATF_REQUIRE(wpid == children[0]);
759 ATF_REQUIRE(WIFEXITED(status));
760 ATF_REQUIRE(WEXITSTATUS(status) == 1);
762 wpid = wait(&status);
763 ATF_REQUIRE(wpid == -1);
764 ATF_REQUIRE(errno == ECHILD);
768 * Verify that a new child process is stopped after a followed fork
769 * and that the traced parent sees the exit of the child when the new
770 * child process is detached after it reports its fork. In this test
771 * the parent that forks is not a direct child of the debugger.
773 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
774 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
776 pid_t children[2], fpid, wpid;
777 int cpipe[2], status;
779 ATF_REQUIRE(pipe(cpipe) == 0);
780 ATF_REQUIRE((fpid = fork()) != -1);
782 attach_fork_parent(cpipe);
783 follow_fork_parent(false);
786 /* Parent process. */
789 /* Wait for the direct child to exit. */
790 wpid = waitpid(fpid, &status, 0);
791 ATF_REQUIRE(wpid == fpid);
792 ATF_REQUIRE(WIFEXITED(status));
793 ATF_REQUIRE(WEXITSTATUS(status) == 3);
795 /* Read the pid of the fork parent. */
796 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
797 sizeof(children[0]));
799 /* Attach to the fork parent. */
800 attach_child(children[0]);
802 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
804 /* Continue the fork parent ignoring the SIGSTOP. */
805 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
807 /* Signal the fork parent to continue. */
810 children[1] = handle_fork_events(children[0], NULL);
811 ATF_REQUIRE(children[1] > 0);
813 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
814 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
817 * Should not see any status from the child now, only the fork
820 wpid = wait(&status);
821 ATF_REQUIRE(wpid == children[0]);
822 ATF_REQUIRE(WIFEXITED(status));
823 ATF_REQUIRE(WEXITSTATUS(status) == 1);
825 wpid = wait(&status);
826 ATF_REQUIRE(wpid == -1);
827 ATF_REQUIRE(errno == ECHILD);
831 * Verify that a new child process is stopped after a followed fork
832 * and that the traced parent sees the exit of the child when the
833 * traced parent is detached after the fork. In this test the parent
834 * that forks is not a direct child of the debugger.
836 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
837 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
839 pid_t children[2], fpid, wpid;
840 int cpipe[2], status;
842 ATF_REQUIRE(pipe(cpipe) == 0);
843 ATF_REQUIRE((fpid = fork()) != -1);
845 attach_fork_parent(cpipe);
846 follow_fork_parent(false);
849 /* Parent process. */
852 /* Wait for the direct child to exit. */
853 wpid = waitpid(fpid, &status, 0);
854 ATF_REQUIRE(wpid == fpid);
855 ATF_REQUIRE(WIFEXITED(status));
856 ATF_REQUIRE(WEXITSTATUS(status) == 3);
858 /* Read the pid of the fork parent. */
859 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
860 sizeof(children[0]));
862 /* Attach to the fork parent. */
863 attach_child(children[0]);
865 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
867 /* Continue the fork parent ignoring the SIGSTOP. */
868 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
870 /* Signal the fork parent to continue. */
873 children[1] = handle_fork_events(children[0], NULL);
874 ATF_REQUIRE(children[1] > 0);
876 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
877 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
880 * Should not see any status from the fork parent now, only
883 wpid = wait(&status);
884 ATF_REQUIRE(wpid == children[1]);
885 ATF_REQUIRE(WIFEXITED(status));
886 ATF_REQUIRE(WEXITSTATUS(status) == 2);
888 wpid = wait(&status);
889 ATF_REQUIRE(wpid == -1);
890 ATF_REQUIRE(errno == ECHILD);
894 * Verify that a child process does not see an unrelated debugger as its
895 * parent but sees its original parent process.
897 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
898 ATF_TC_BODY(ptrace__getppid, tc)
900 pid_t child, debugger, ppid, wpid;
901 int cpipe[2], dpipe[2], status;
904 ATF_REQUIRE(pipe(cpipe) == 0);
905 ATF_REQUIRE((child = fork()) != -1);
911 /* Wait for parent to be ready. */
912 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
914 /* Report the parent PID to the parent. */
916 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
923 ATF_REQUIRE(pipe(dpipe) == 0);
924 ATF_REQUIRE((debugger = fork()) != -1);
927 /* Debugger process. */
930 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
932 wpid = waitpid(child, &status, 0);
933 CHILD_REQUIRE(wpid == child);
934 CHILD_REQUIRE(WIFSTOPPED(status));
935 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
937 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
939 /* Signal parent that debugger is attached. */
940 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
942 /* Wait for traced child to exit. */
943 wpid = waitpid(child, &status, 0);
944 CHILD_REQUIRE(wpid == child);
945 CHILD_REQUIRE(WIFEXITED(status));
946 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
952 /* Parent process. */
954 /* Wait for the debugger to attach to the child. */
955 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
957 /* Release the child. */
958 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
960 /* Read the parent PID from the child. */
961 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
964 ATF_REQUIRE(ppid == getpid());
966 /* Wait for the debugger. */
967 wpid = waitpid(debugger, &status, 0);
968 ATF_REQUIRE(wpid == debugger);
969 ATF_REQUIRE(WIFEXITED(status));
970 ATF_REQUIRE(WEXITSTATUS(status) == 0);
972 /* The child process should now be ready. */
973 wpid = waitpid(child, &status, WNOHANG);
974 ATF_REQUIRE(wpid == child);
975 ATF_REQUIRE(WIFEXITED(status));
976 ATF_REQUIRE(WEXITSTATUS(status) == 1);
980 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
981 * child process created via fork() reports the correct value.
983 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
984 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
986 struct ptrace_lwpinfo pl[2];
987 pid_t children[2], fpid, wpid;
990 ATF_REQUIRE((fpid = fork()) != -1);
993 follow_fork_parent(false);
996 /* Parent process. */
999 /* The first wait() should report the stop from SIGSTOP. */
1000 wpid = waitpid(children[0], &status, 0);
1001 ATF_REQUIRE(wpid == children[0]);
1002 ATF_REQUIRE(WIFSTOPPED(status));
1003 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1005 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1007 /* Continue the child ignoring the SIGSTOP. */
1008 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1010 /* Wait for both halves of the fork event to get reported. */
1011 children[1] = handle_fork_events(children[0], pl);
1012 ATF_REQUIRE(children[1] > 0);
1014 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1015 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1016 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1017 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1018 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1020 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1021 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1024 * The child can't exit until the grandchild reports status, so the
1025 * grandchild should report its exit first to the debugger.
1027 wpid = wait(&status);
1028 ATF_REQUIRE(wpid == children[1]);
1029 ATF_REQUIRE(WIFEXITED(status));
1030 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1032 wpid = wait(&status);
1033 ATF_REQUIRE(wpid == children[0]);
1034 ATF_REQUIRE(WIFEXITED(status));
1035 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1037 wpid = wait(&status);
1038 ATF_REQUIRE(wpid == -1);
1039 ATF_REQUIRE(errno == ECHILD);
1043 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1044 * child process created via vfork() reports the correct value.
1046 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1047 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1049 struct ptrace_lwpinfo pl[2];
1050 pid_t children[2], fpid, wpid;
1053 ATF_REQUIRE((fpid = fork()) != -1);
1056 follow_fork_parent(true);
1059 /* Parent process. */
1062 /* The first wait() should report the stop from SIGSTOP. */
1063 wpid = waitpid(children[0], &status, 0);
1064 ATF_REQUIRE(wpid == children[0]);
1065 ATF_REQUIRE(WIFSTOPPED(status));
1066 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1068 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1070 /* Continue the child ignoring the SIGSTOP. */
1071 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1073 /* Wait for both halves of the fork event to get reported. */
1074 children[1] = handle_fork_events(children[0], pl);
1075 ATF_REQUIRE(children[1] > 0);
1077 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1078 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1079 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1080 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1081 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1083 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1084 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1087 * The child can't exit until the grandchild reports status, so the
1088 * grandchild should report its exit first to the debugger.
1090 wpid = wait(&status);
1091 ATF_REQUIRE(wpid == children[1]);
1092 ATF_REQUIRE(WIFEXITED(status));
1093 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1095 wpid = wait(&status);
1096 ATF_REQUIRE(wpid == children[0]);
1097 ATF_REQUIRE(WIFEXITED(status));
1098 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1100 wpid = wait(&status);
1101 ATF_REQUIRE(wpid == -1);
1102 ATF_REQUIRE(errno == ECHILD);
1106 simple_thread(void *arg __unused)
1113 simple_thread_main(void)
1117 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1118 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1123 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1124 * thread reports the correct value.
1126 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1127 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1129 struct ptrace_lwpinfo pl;
1134 ATF_REQUIRE((fpid = fork()) != -1);
1137 simple_thread_main();
1140 /* The first wait() should report the stop from SIGSTOP. */
1141 wpid = waitpid(fpid, &status, 0);
1142 ATF_REQUIRE(wpid == fpid);
1143 ATF_REQUIRE(WIFSTOPPED(status));
1144 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1146 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1148 mainlwp = pl.pl_lwpid;
1151 * Continue the child ignoring the SIGSTOP and tracing all
1152 * system call exits.
1154 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1157 * Wait for the new thread to arrive. pthread_create() might
1158 * invoke any number of system calls. For now we just wait
1159 * for the new thread to arrive and make sure it reports a
1160 * valid system call code. If ptrace grows thread event
1161 * reporting then this test can be made more precise.
1164 wpid = waitpid(fpid, &status, 0);
1165 ATF_REQUIRE(wpid == fpid);
1166 ATF_REQUIRE(WIFSTOPPED(status));
1167 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1169 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1171 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1172 ATF_REQUIRE(pl.pl_syscall_code != 0);
1173 if (pl.pl_lwpid != mainlwp)
1174 /* New thread seen. */
1177 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1180 /* Wait for the child to exit. */
1181 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1183 wpid = waitpid(fpid, &status, 0);
1184 ATF_REQUIRE(wpid == fpid);
1185 if (WIFEXITED(status))
1188 ATF_REQUIRE(WIFSTOPPED(status));
1189 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1190 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1193 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1195 wpid = wait(&status);
1196 ATF_REQUIRE(wpid == -1);
1197 ATF_REQUIRE(errno == ECHILD);
1201 * Verify that the expected LWP events are reported for a child thread.
1203 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1204 ATF_TC_BODY(ptrace__lwp_events, tc)
1206 struct ptrace_lwpinfo pl;
1211 ATF_REQUIRE((fpid = fork()) != -1);
1214 simple_thread_main();
1217 /* The first wait() should report the stop from SIGSTOP. */
1218 wpid = waitpid(fpid, &status, 0);
1219 ATF_REQUIRE(wpid == fpid);
1220 ATF_REQUIRE(WIFSTOPPED(status));
1221 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1223 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1225 lwps[0] = pl.pl_lwpid;
1227 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1229 /* Continue the child ignoring the SIGSTOP. */
1230 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1232 /* The first event should be for the child thread's birth. */
1233 wpid = waitpid(fpid, &status, 0);
1234 ATF_REQUIRE(wpid == fpid);
1235 ATF_REQUIRE(WIFSTOPPED(status));
1236 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1238 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1239 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1240 (PL_FLAG_BORN | PL_FLAG_SCX));
1241 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1242 lwps[1] = pl.pl_lwpid;
1244 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1246 /* The next event should be for the child thread's death. */
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)) ==
1254 (PL_FLAG_EXITED | PL_FLAG_SCE));
1255 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1257 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1259 /* The last event should be for the child process's exit. */
1260 wpid = waitpid(fpid, &status, 0);
1261 ATF_REQUIRE(WIFEXITED(status));
1262 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1264 wpid = wait(&status);
1265 ATF_REQUIRE(wpid == -1);
1266 ATF_REQUIRE(errno == ECHILD);
1270 exec_thread(void *arg __unused)
1273 execl("/usr/bin/true", "true", NULL);
1278 exec_thread_main(void)
1282 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1289 * Verify that the expected LWP events are reported for a multithreaded
1290 * process that calls execve(2).
1292 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1293 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1295 struct ptrace_lwpinfo pl;
1300 ATF_REQUIRE((fpid = fork()) != -1);
1306 /* The first wait() should report the stop from SIGSTOP. */
1307 wpid = waitpid(fpid, &status, 0);
1308 ATF_REQUIRE(wpid == fpid);
1309 ATF_REQUIRE(WIFSTOPPED(status));
1310 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1312 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1314 lwps[0] = pl.pl_lwpid;
1316 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1318 /* Continue the child ignoring the SIGSTOP. */
1319 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1321 /* The first event should be for the child thread's birth. */
1322 wpid = waitpid(fpid, &status, 0);
1323 ATF_REQUIRE(wpid == fpid);
1324 ATF_REQUIRE(WIFSTOPPED(status));
1325 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1327 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1328 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1329 (PL_FLAG_BORN | PL_FLAG_SCX));
1330 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1331 lwps[1] = pl.pl_lwpid;
1333 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1336 * The next event should be for the main thread's death due to
1337 * single threading from execve().
1339 wpid = waitpid(fpid, &status, 0);
1340 ATF_REQUIRE(wpid == fpid);
1341 ATF_REQUIRE(WIFSTOPPED(status));
1342 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1344 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1345 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1347 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1349 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1351 /* The next event should be for the child process's exec. */
1352 wpid = waitpid(fpid, &status, 0);
1353 ATF_REQUIRE(WIFSTOPPED(status));
1354 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1356 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1357 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1358 (PL_FLAG_EXEC | PL_FLAG_SCX));
1359 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1361 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1363 /* The last event should be for the child process's exit. */
1364 wpid = waitpid(fpid, &status, 0);
1365 ATF_REQUIRE(WIFEXITED(status));
1366 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1368 wpid = wait(&status);
1369 ATF_REQUIRE(wpid == -1);
1370 ATF_REQUIRE(errno == ECHILD);
1374 handler(int sig __unused)
1382 signal(SIGINFO, handler);
1388 * Verify that the expected ptrace event is reported for a signal.
1390 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1391 ATF_TC_BODY(ptrace__siginfo, tc)
1393 struct ptrace_lwpinfo pl;
1397 ATF_REQUIRE((fpid = fork()) != -1);
1403 /* The first wait() should report the stop from SIGSTOP. */
1404 wpid = waitpid(fpid, &status, 0);
1405 ATF_REQUIRE(wpid == fpid);
1406 ATF_REQUIRE(WIFSTOPPED(status));
1407 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1409 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1411 /* The next event should be for the SIGINFO. */
1412 wpid = waitpid(fpid, &status, 0);
1413 ATF_REQUIRE(WIFSTOPPED(status));
1414 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1416 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1417 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1418 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1419 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1420 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1422 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1424 /* The last event should be for the child process's exit. */
1425 wpid = waitpid(fpid, &status, 0);
1426 ATF_REQUIRE(WIFEXITED(status));
1427 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1429 wpid = wait(&status);
1430 ATF_REQUIRE(wpid == -1);
1431 ATF_REQUIRE(errno == ECHILD);
1435 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1437 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1438 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1443 ATF_REQUIRE((fpid = fork()) != -1);
1449 /* The first wait() should report the stop from SIGSTOP. */
1450 wpid = waitpid(fpid, &status, 0);
1451 ATF_REQUIRE(wpid == fpid);
1452 ATF_REQUIRE(WIFSTOPPED(status));
1453 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1456 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1457 sizeof(events)) == 0);
1459 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1461 /* Should get one event at exit. */
1462 wpid = waitpid(fpid, &status, 0);
1463 ATF_REQUIRE(WIFEXITED(status));
1464 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1466 wpid = wait(&status);
1467 ATF_REQUIRE(wpid == -1);
1468 ATF_REQUIRE(errno == ECHILD);
1471 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1472 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1474 struct ptrace_lwpinfo pl;
1478 ATF_REQUIRE((fpid = fork()) != -1);
1484 /* The first wait() should report the stop from SIGSTOP. */
1485 wpid = waitpid(fpid, &status, 0);
1486 ATF_REQUIRE(wpid == fpid);
1487 ATF_REQUIRE(WIFSTOPPED(status));
1488 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1490 events = PTRACE_EXEC;
1491 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1492 sizeof(events)) == 0);
1494 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1496 /* The next event should be for the child process's exec. */
1497 wpid = waitpid(fpid, &status, 0);
1498 ATF_REQUIRE(WIFSTOPPED(status));
1499 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1501 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1502 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1503 (PL_FLAG_EXEC | PL_FLAG_SCX));
1505 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1507 /* The last event should be for the child process's exit. */
1508 wpid = waitpid(fpid, &status, 0);
1509 ATF_REQUIRE(WIFEXITED(status));
1510 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1512 wpid = wait(&status);
1513 ATF_REQUIRE(wpid == -1);
1514 ATF_REQUIRE(errno == ECHILD);
1517 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1518 ATF_TC_BODY(ptrace__event_mask, tc)
1523 ATF_REQUIRE((fpid = fork()) != -1);
1529 /* The first wait() should report the stop from SIGSTOP. */
1530 wpid = waitpid(fpid, &status, 0);
1531 ATF_REQUIRE(wpid == fpid);
1532 ATF_REQUIRE(WIFSTOPPED(status));
1533 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1535 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1536 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1537 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1538 sizeof(events)) == 0);
1539 ATF_REQUIRE(events & PTRACE_FORK);
1540 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1541 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1542 sizeof(events)) == 0);
1543 ATF_REQUIRE(!(events & PTRACE_FORK));
1545 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1546 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1547 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1548 sizeof(events)) == 0);
1549 ATF_REQUIRE(events & PTRACE_LWP);
1550 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1551 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1552 sizeof(events)) == 0);
1553 ATF_REQUIRE(!(events & PTRACE_LWP));
1555 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1557 /* Should get one event at exit. */
1558 wpid = waitpid(fpid, &status, 0);
1559 ATF_REQUIRE(WIFEXITED(status));
1560 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1562 wpid = wait(&status);
1563 ATF_REQUIRE(wpid == -1);
1564 ATF_REQUIRE(errno == ECHILD);
1568 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1570 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1571 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1573 struct ptrace_lwpinfo pl;
1577 ATF_REQUIRE((fpid = fork()) != -1);
1580 follow_fork_parent(true);
1583 /* The first wait() should report the stop from SIGSTOP. */
1584 wpid = waitpid(fpid, &status, 0);
1585 ATF_REQUIRE(wpid == fpid);
1586 ATF_REQUIRE(WIFSTOPPED(status));
1587 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1589 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1590 sizeof(events)) == 0);
1591 events |= PTRACE_VFORK;
1592 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1593 sizeof(events)) == 0);
1595 /* Continue the child ignoring the SIGSTOP. */
1596 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1598 /* The next event should report the end of the vfork. */
1599 wpid = wait(&status);
1600 ATF_REQUIRE(wpid == fpid);
1601 ATF_REQUIRE(WIFSTOPPED(status));
1602 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1603 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1604 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1606 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1608 wpid = wait(&status);
1609 ATF_REQUIRE(wpid == fpid);
1610 ATF_REQUIRE(WIFEXITED(status));
1611 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1613 wpid = wait(&status);
1614 ATF_REQUIRE(wpid == -1);
1615 ATF_REQUIRE(errno == ECHILD);
1618 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1619 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1621 struct ptrace_lwpinfo pl[2];
1622 pid_t children[2], fpid, wpid;
1625 ATF_REQUIRE((fpid = fork()) != -1);
1628 follow_fork_parent(true);
1631 /* Parent process. */
1634 /* The first wait() should report the stop from SIGSTOP. */
1635 wpid = waitpid(children[0], &status, 0);
1636 ATF_REQUIRE(wpid == children[0]);
1637 ATF_REQUIRE(WIFSTOPPED(status));
1638 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1640 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1641 sizeof(events)) == 0);
1642 events |= PTRACE_FORK | PTRACE_VFORK;
1643 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1644 sizeof(events)) == 0);
1646 /* Continue the child ignoring the SIGSTOP. */
1647 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1649 /* Wait for both halves of the fork event to get reported. */
1650 children[1] = handle_fork_events(children[0], pl);
1651 ATF_REQUIRE(children[1] > 0);
1653 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1655 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1656 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1659 * The child can't exit until the grandchild reports status, so the
1660 * grandchild should report its exit first to the debugger.
1662 wpid = waitpid(children[1], &status, 0);
1663 ATF_REQUIRE(wpid == children[1]);
1664 ATF_REQUIRE(WIFEXITED(status));
1665 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1668 * The child should report it's vfork() completion before it
1671 wpid = wait(&status);
1672 ATF_REQUIRE(wpid == children[0]);
1673 ATF_REQUIRE(WIFSTOPPED(status));
1674 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1675 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1677 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1679 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1681 wpid = wait(&status);
1682 ATF_REQUIRE(wpid == children[0]);
1683 ATF_REQUIRE(WIFEXITED(status));
1684 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1686 wpid = wait(&status);
1687 ATF_REQUIRE(wpid == -1);
1688 ATF_REQUIRE(errno == ECHILD);
1692 * XXX: There's nothing inherently platform specific about this test, however a
1693 * userspace visible breakpoint() is a prerequisite.
1695 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1697 * Verify that no more events are reported after PT_KILL except for the
1698 * process exit when stopped due to a breakpoint trap.
1700 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1701 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1706 ATF_REQUIRE((fpid = fork()) != -1);
1713 /* The first wait() should report the stop from SIGSTOP. */
1714 wpid = waitpid(fpid, &status, 0);
1715 ATF_REQUIRE(wpid == fpid);
1716 ATF_REQUIRE(WIFSTOPPED(status));
1717 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1719 /* Continue the child ignoring the SIGSTOP. */
1720 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1722 /* The second wait() should report hitting the breakpoint. */
1723 wpid = waitpid(fpid, &status, 0);
1724 ATF_REQUIRE(wpid == fpid);
1725 ATF_REQUIRE(WIFSTOPPED(status));
1726 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1728 /* Kill the child process. */
1729 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1731 /* The last wait() should report the SIGKILL. */
1732 wpid = waitpid(fpid, &status, 0);
1733 ATF_REQUIRE(wpid == fpid);
1734 ATF_REQUIRE(WIFSIGNALED(status));
1735 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1737 wpid = wait(&status);
1738 ATF_REQUIRE(wpid == -1);
1739 ATF_REQUIRE(errno == ECHILD);
1741 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1744 * Verify that no more events are reported after PT_KILL except for the
1745 * process exit when stopped inside of a system call.
1747 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1748 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1750 struct ptrace_lwpinfo pl;
1754 ATF_REQUIRE((fpid = fork()) != -1);
1761 /* The first wait() should report the stop from SIGSTOP. */
1762 wpid = waitpid(fpid, &status, 0);
1763 ATF_REQUIRE(wpid == fpid);
1764 ATF_REQUIRE(WIFSTOPPED(status));
1765 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1767 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1768 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1770 /* The second wait() should report a system call entry for getpid(). */
1771 wpid = waitpid(fpid, &status, 0);
1772 ATF_REQUIRE(wpid == fpid);
1773 ATF_REQUIRE(WIFSTOPPED(status));
1774 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1776 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1777 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1779 /* Kill the child process. */
1780 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1782 /* The last wait() should report the SIGKILL. */
1783 wpid = waitpid(fpid, &status, 0);
1784 ATF_REQUIRE(wpid == fpid);
1785 ATF_REQUIRE(WIFSIGNALED(status));
1786 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1788 wpid = wait(&status);
1789 ATF_REQUIRE(wpid == -1);
1790 ATF_REQUIRE(errno == ECHILD);
1794 * Verify that no more events are reported after PT_KILL except for the
1795 * process exit when killing a multithreaded process.
1797 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1798 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1800 struct ptrace_lwpinfo pl;
1805 ATF_REQUIRE((fpid = fork()) != -1);
1808 simple_thread_main();
1811 /* The first wait() should report the stop from SIGSTOP. */
1812 wpid = waitpid(fpid, &status, 0);
1813 ATF_REQUIRE(wpid == fpid);
1814 ATF_REQUIRE(WIFSTOPPED(status));
1815 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1817 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1819 main_lwp = pl.pl_lwpid;
1821 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1823 /* Continue the child ignoring the SIGSTOP. */
1824 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1826 /* The first event should be for the child thread's birth. */
1827 wpid = waitpid(fpid, &status, 0);
1828 ATF_REQUIRE(wpid == fpid);
1829 ATF_REQUIRE(WIFSTOPPED(status));
1830 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1832 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1833 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1834 (PL_FLAG_BORN | PL_FLAG_SCX));
1835 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1837 /* Kill the child process. */
1838 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1840 /* The last wait() should report the SIGKILL. */
1841 wpid = waitpid(fpid, &status, 0);
1842 ATF_REQUIRE(wpid == fpid);
1843 ATF_REQUIRE(WIFSIGNALED(status));
1844 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1846 wpid = wait(&status);
1847 ATF_REQUIRE(wpid == -1);
1848 ATF_REQUIRE(errno == ECHILD);
1852 mask_usr1_thread(void *arg)
1854 pthread_barrier_t *pbarrier;
1857 pbarrier = (pthread_barrier_t*)arg;
1859 sigemptyset(&sigmask);
1860 sigaddset(&sigmask, SIGUSR1);
1861 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1863 /* Sync up with other thread after sigmask updated. */
1864 pthread_barrier_wait(pbarrier);
1873 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1874 * and prevents spurious stops due to those other signals.
1876 ATF_TC(ptrace__PT_KILL_competing_signal);
1877 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1880 atf_tc_set_md_var(tc, "require.user", "root");
1882 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1888 pthread_barrier_t barrier;
1889 struct sched_param sched_param;
1891 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);
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 /* Continue the child ignoring the SIGSTOP. */
1943 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1945 /* Send a signal that only the second thread can handle. */
1946 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1948 /* The second wait() should report the SIGUSR2. */
1949 wpid = waitpid(fpid, &status, 0);
1950 ATF_REQUIRE(wpid == fpid);
1951 ATF_REQUIRE(WIFSTOPPED(status));
1952 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1954 /* Send a signal that only the first thread can handle. */
1955 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1957 /* Replace the SIGUSR2 with a kill. */
1958 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1960 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1961 wpid = waitpid(fpid, &status, 0);
1962 ATF_REQUIRE(wpid == fpid);
1963 ATF_REQUIRE(WIFSIGNALED(status));
1964 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1966 wpid = wait(&status);
1967 ATF_REQUIRE(wpid == -1);
1968 ATF_REQUIRE(errno == ECHILD);
1972 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1973 * and prevents spurious stops caused by those events.
1975 ATF_TC(ptrace__PT_KILL_competing_stop);
1976 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
1979 atf_tc_set_md_var(tc, "require.user", "root");
1981 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1987 pthread_barrier_t barrier;
1989 struct ptrace_lwpinfo pl;
1990 struct sched_param sched_param;
1992 ATF_REQUIRE((fpid = fork()) != -1);
1996 /* Bind to one CPU so only one thread at a time will run. */
1998 CPU_SET(0, &setmask);
2000 CHILD_REQUIRE(cpuset(&setid) == 0);
2001 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2002 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2004 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2006 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2007 (void*)&barrier) == 0);
2010 * Give the main thread higher priority. The test always
2011 * assumes that, if both threads are able to run, the main
2012 * thread runs first.
2014 sched_param.sched_priority =
2015 (sched_get_priority_max(SCHED_FIFO) +
2016 sched_get_priority_min(SCHED_FIFO)) / 2;
2017 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2018 SCHED_FIFO, &sched_param) == 0);
2019 sched_param.sched_priority -= RQ_PPQ;
2020 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2021 &sched_param) == 0);
2024 sigemptyset(&sigmask);
2025 sigaddset(&sigmask, SIGUSR2);
2026 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2028 /* Sync up with other thread after sigmask updated. */
2029 pthread_barrier_wait(&barrier);
2031 /* Sync up with the test before doing the getpid(). */
2038 /* The first wait() should report the stop from SIGSTOP. */
2039 wpid = waitpid(fpid, &status, 0);
2040 ATF_REQUIRE(wpid == fpid);
2041 ATF_REQUIRE(WIFSTOPPED(status));
2042 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2044 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2045 main_lwp = pl.pl_lwpid;
2047 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2048 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2051 * Continue until child is done with setup, which is indicated with
2052 * SIGSTOP. Ignore system calls in the meantime.
2055 wpid = waitpid(fpid, &status, 0);
2056 ATF_REQUIRE(wpid == fpid);
2057 ATF_REQUIRE(WIFSTOPPED(status));
2058 if (WSTOPSIG(status) == SIGTRAP) {
2059 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2061 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2063 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2066 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2069 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2070 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2072 wpid = waitpid(fpid, &status, 0);
2073 ATF_REQUIRE(wpid == fpid);
2074 ATF_REQUIRE(WIFSTOPPED(status));
2075 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2077 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2079 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2080 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2081 /* Prevent the main thread from hitting its syscall exit for now. */
2082 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2085 * Proceed, allowing second thread to hit syscall exit for
2086 * pthread_barrier_wait().
2088 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2090 wpid = waitpid(fpid, &status, 0);
2091 ATF_REQUIRE(wpid == fpid);
2092 ATF_REQUIRE(WIFSTOPPED(status));
2093 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2095 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2097 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2098 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2100 /* Send a signal that only the second thread can handle. */
2101 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2103 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2105 /* The next wait() should report the SIGUSR2. */
2106 wpid = waitpid(fpid, &status, 0);
2107 ATF_REQUIRE(wpid == fpid);
2108 ATF_REQUIRE(WIFSTOPPED(status));
2109 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2111 /* Allow the main thread to try to finish its system call. */
2112 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2115 * At this point, the main thread is in the middle of a system call and
2116 * has been resumed. The second thread has taken a SIGUSR2 which will
2117 * be replaced with a SIGKILL below. The main thread will get to run
2118 * first. It should notice the kill request (even though the signal
2119 * replacement occurred in the other thread) and exit accordingly. It
2120 * should not stop for the system call exit event.
2123 /* Replace the SIGUSR2 with a kill. */
2124 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2126 /* The last wait() should report the SIGKILL (not a syscall exit). */
2127 wpid = waitpid(fpid, &status, 0);
2128 ATF_REQUIRE(wpid == fpid);
2129 ATF_REQUIRE(WIFSIGNALED(status));
2130 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2132 wpid = wait(&status);
2133 ATF_REQUIRE(wpid == -1);
2134 ATF_REQUIRE(errno == ECHILD);
2138 sigusr1_handler(int sig)
2141 CHILD_REQUIRE(sig == SIGUSR1);
2146 * Verify that even if the signal queue is full for a child process,
2147 * a PT_KILL will kill the process.
2149 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2150 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2154 int max_pending_per_proc;
2158 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2160 ATF_REQUIRE((fpid = fork()) != -1);
2166 /* The first wait() should report the stop from SIGSTOP. */
2167 wpid = waitpid(fpid, &status, 0);
2168 ATF_REQUIRE(wpid == fpid);
2169 ATF_REQUIRE(WIFSTOPPED(status));
2170 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2172 len = sizeof(max_pending_per_proc);
2173 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2174 &max_pending_per_proc, &len, NULL, 0) == 0);
2176 /* Fill the signal queue. */
2177 for (i = 0; i < max_pending_per_proc; ++i)
2178 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2180 /* Kill the child process. */
2181 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2183 /* The last wait() should report the SIGKILL. */
2184 wpid = waitpid(fpid, &status, 0);
2185 ATF_REQUIRE(wpid == fpid);
2186 ATF_REQUIRE(WIFSIGNALED(status));
2187 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2189 wpid = wait(&status);
2190 ATF_REQUIRE(wpid == -1);
2191 ATF_REQUIRE(errno == ECHILD);
2195 * Verify that when stopped at a system call entry, a signal can be
2196 * requested with PT_CONTINUE which will be delivered once the system
2199 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2200 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2202 struct ptrace_lwpinfo pl;
2206 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2208 ATF_REQUIRE((fpid = fork()) != -1);
2215 /* The first wait() should report the stop from SIGSTOP. */
2216 wpid = waitpid(fpid, &status, 0);
2217 ATF_REQUIRE(wpid == fpid);
2218 ATF_REQUIRE(WIFSTOPPED(status));
2219 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2221 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2222 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2224 /* The second wait() should report a system call entry for getpid(). */
2225 wpid = waitpid(fpid, &status, 0);
2226 ATF_REQUIRE(wpid == fpid);
2227 ATF_REQUIRE(WIFSTOPPED(status));
2228 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2230 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2231 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2233 /* Continue the child process with a signal. */
2234 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2238 * The last wait() should report exit 2, i.e., a normal _exit
2239 * from the signal handler. In the meantime, catch and proceed
2240 * past any syscall stops.
2242 wpid = waitpid(fpid, &status, 0);
2243 ATF_REQUIRE(wpid == fpid);
2244 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2245 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2246 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2247 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2249 ATF_REQUIRE(WIFEXITED(status));
2250 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2255 wpid = wait(&status);
2256 ATF_REQUIRE(wpid == -1);
2257 ATF_REQUIRE(errno == ECHILD);
2261 sigusr1_counting_handler(int sig)
2263 static int counter = 0;
2265 CHILD_REQUIRE(sig == SIGUSR1);
2272 * Verify that, when continuing from a stop at system call entry and exit,
2273 * a signal can be requested from both stops, and both will be delivered when
2274 * the system call is complete.
2276 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2277 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2279 struct ptrace_lwpinfo pl;
2283 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2285 ATF_REQUIRE((fpid = fork()) != -1);
2292 /* The first wait() should report the stop from SIGSTOP. */
2293 wpid = waitpid(fpid, &status, 0);
2294 ATF_REQUIRE(wpid == fpid);
2295 ATF_REQUIRE(WIFSTOPPED(status));
2296 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2298 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2299 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2301 /* The second wait() should report a system call entry for getpid(). */
2302 wpid = waitpid(fpid, &status, 0);
2303 ATF_REQUIRE(wpid == fpid);
2304 ATF_REQUIRE(WIFSTOPPED(status));
2305 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2307 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2308 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2310 /* Continue the child process with a signal. */
2311 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2313 /* The third wait() should report a system call exit for getpid(). */
2314 wpid = waitpid(fpid, &status, 0);
2315 ATF_REQUIRE(wpid == fpid);
2316 ATF_REQUIRE(WIFSTOPPED(status));
2317 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2319 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2320 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2322 /* Continue the child process with a signal. */
2323 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2327 * The last wait() should report exit 2, i.e., a normal _exit
2328 * from the signal handler. In the meantime, catch and proceed
2329 * past any syscall stops.
2331 wpid = waitpid(fpid, &status, 0);
2332 ATF_REQUIRE(wpid == fpid);
2333 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2334 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2335 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2336 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2338 ATF_REQUIRE(WIFEXITED(status));
2339 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2344 wpid = wait(&status);
2345 ATF_REQUIRE(wpid == -1);
2346 ATF_REQUIRE(errno == ECHILD);
2350 * Verify that even if the signal queue is full for a child process,
2351 * a PT_CONTINUE with a signal will not result in loss of that signal.
2353 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2354 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2358 int max_pending_per_proc;
2362 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2363 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2365 ATF_REQUIRE((fpid = fork()) != -1);
2371 /* The first wait() should report the stop from SIGSTOP. */
2372 wpid = waitpid(fpid, &status, 0);
2373 ATF_REQUIRE(wpid == fpid);
2374 ATF_REQUIRE(WIFSTOPPED(status));
2375 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2377 len = sizeof(max_pending_per_proc);
2378 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2379 &max_pending_per_proc, &len, NULL, 0) == 0);
2381 /* Fill the signal queue. */
2382 for (i = 0; i < max_pending_per_proc; ++i)
2383 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2385 /* Continue with signal. */
2386 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2389 wpid = waitpid(fpid, &status, 0);
2390 ATF_REQUIRE(wpid == fpid);
2391 if (WIFSTOPPED(status)) {
2392 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2393 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2396 * The last wait() should report normal _exit from the
2399 ATF_REQUIRE(WIFEXITED(status));
2400 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2405 wpid = wait(&status);
2406 ATF_REQUIRE(wpid == -1);
2407 ATF_REQUIRE(errno == ECHILD);
2410 static sem_t sigusr1_sem;
2411 static int got_usr1;
2414 sigusr1_sempost_handler(int sig __unused)
2418 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2422 * Verify that even if the signal queue is full for a child process,
2423 * and the signal is masked, a PT_CONTINUE with a signal will not
2424 * result in loss of that signal.
2426 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2427 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2429 struct ptrace_lwpinfo pl;
2432 int max_pending_per_proc;
2437 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2438 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2439 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2442 ATF_REQUIRE((fpid = fork()) != -1);
2444 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2445 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2446 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2449 CHILD_REQUIRE(got_usr1 == 0);
2451 /* Allow the pending SIGUSR1 in now. */
2452 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2453 /* Wait to receive the SIGUSR1. */
2455 err = sem_wait(&sigusr1_sem);
2456 CHILD_REQUIRE(err == 0 || errno == EINTR);
2457 } while (err != 0 && errno == EINTR);
2458 CHILD_REQUIRE(got_usr1 == 1);
2462 /* The first wait() should report the stop from SIGSTOP. */
2463 wpid = waitpid(fpid, &status, 0);
2464 ATF_REQUIRE(wpid == fpid);
2465 ATF_REQUIRE(WIFSTOPPED(status));
2466 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2468 len = sizeof(max_pending_per_proc);
2469 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2470 &max_pending_per_proc, &len, NULL, 0) == 0);
2472 /* Fill the signal queue. */
2473 for (i = 0; i < max_pending_per_proc; ++i)
2474 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2476 /* Continue with signal. */
2477 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2479 /* Collect and ignore all of the SIGUSR2. */
2480 for (i = 0; i < max_pending_per_proc; ++i) {
2481 wpid = waitpid(fpid, &status, 0);
2482 ATF_REQUIRE(wpid == fpid);
2483 ATF_REQUIRE(WIFSTOPPED(status));
2484 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2485 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2488 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2489 wpid = waitpid(fpid, &status, 0);
2490 ATF_REQUIRE(wpid == fpid);
2491 ATF_REQUIRE(WIFSTOPPED(status));
2492 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2493 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2494 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2496 /* Continue the child, ignoring the SIGUSR1. */
2497 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2499 /* The last wait() should report exit after receiving SIGUSR1. */
2500 wpid = waitpid(fpid, &status, 0);
2501 ATF_REQUIRE(wpid == fpid);
2502 ATF_REQUIRE(WIFEXITED(status));
2503 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2505 wpid = wait(&status);
2506 ATF_REQUIRE(wpid == -1);
2507 ATF_REQUIRE(errno == ECHILD);
2511 * Verify that, after stopping due to a signal, that signal can be
2512 * replaced with another signal.
2514 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2515 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2517 struct ptrace_lwpinfo pl;
2521 ATF_REQUIRE((fpid = fork()) != -1);
2528 /* The first wait() should report the stop from SIGSTOP. */
2529 wpid = waitpid(fpid, &status, 0);
2530 ATF_REQUIRE(wpid == fpid);
2531 ATF_REQUIRE(WIFSTOPPED(status));
2532 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2534 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2536 /* Send a signal without ptrace. */
2537 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2539 /* The second wait() should report a SIGINT was received. */
2540 wpid = waitpid(fpid, &status, 0);
2541 ATF_REQUIRE(wpid == fpid);
2542 ATF_REQUIRE(WIFSTOPPED(status));
2543 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2545 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2546 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2547 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2549 /* Continue the child process with a different signal. */
2550 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2553 * The last wait() should report having died due to the new
2556 wpid = waitpid(fpid, &status, 0);
2557 ATF_REQUIRE(wpid == fpid);
2558 ATF_REQUIRE(WIFSIGNALED(status));
2559 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2561 wpid = wait(&status);
2562 ATF_REQUIRE(wpid == -1);
2563 ATF_REQUIRE(errno == ECHILD);
2567 * Verify that a signal can be passed through to the child even when there
2568 * was no true signal originally. Such cases arise when a SIGTRAP is
2569 * invented for e.g, system call stops.
2571 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2572 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2574 struct ptrace_lwpinfo pl;
2579 ATF_REQUIRE((fpid = fork()) != -1);
2582 /* SIGTRAP expected to cause exit on syscall entry. */
2583 rl.rlim_cur = rl.rlim_max = 0;
2584 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2589 /* The first wait() should report the stop from SIGSTOP. */
2590 wpid = waitpid(fpid, &status, 0);
2591 ATF_REQUIRE(wpid == fpid);
2592 ATF_REQUIRE(WIFSTOPPED(status));
2593 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2595 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2596 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2598 /* The second wait() should report a system call entry for getpid(). */
2599 wpid = waitpid(fpid, &status, 0);
2600 ATF_REQUIRE(wpid == fpid);
2601 ATF_REQUIRE(WIFSTOPPED(status));
2602 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2604 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2605 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2607 /* Continue the child process with a SIGTRAP. */
2608 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2612 * The last wait() should report exit due to SIGTRAP. In the
2613 * meantime, catch and proceed past any syscall stops.
2615 wpid = waitpid(fpid, &status, 0);
2616 ATF_REQUIRE(wpid == fpid);
2617 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2618 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2619 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2620 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2622 ATF_REQUIRE(WIFSIGNALED(status));
2623 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2628 wpid = wait(&status);
2629 ATF_REQUIRE(wpid == -1);
2630 ATF_REQUIRE(errno == ECHILD);
2635 * A mixed bag PT_CONTINUE with signal test.
2637 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2638 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2640 struct ptrace_lwpinfo pl;
2644 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2646 ATF_REQUIRE((fpid = fork()) != -1);
2653 /* The first wait() should report the stop from SIGSTOP. */
2654 wpid = waitpid(fpid, &status, 0);
2655 ATF_REQUIRE(wpid == fpid);
2656 ATF_REQUIRE(WIFSTOPPED(status));
2657 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2659 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2660 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2662 /* The second wait() should report a system call entry for getpid(). */
2663 wpid = waitpid(fpid, &status, 0);
2664 ATF_REQUIRE(wpid == fpid);
2665 ATF_REQUIRE(WIFSTOPPED(status));
2666 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2668 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2669 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2671 /* Continue with the first SIGUSR1. */
2672 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2674 /* The next wait() should report a system call exit for getpid(). */
2675 wpid = waitpid(fpid, &status, 0);
2676 ATF_REQUIRE(wpid == fpid);
2677 ATF_REQUIRE(WIFSTOPPED(status));
2678 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2680 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2681 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2683 /* Send an ABRT without ptrace. */
2684 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2686 /* Continue normally. */
2687 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2689 /* The next wait() should report the SIGABRT. */
2690 wpid = waitpid(fpid, &status, 0);
2691 ATF_REQUIRE(wpid == fpid);
2692 ATF_REQUIRE(WIFSTOPPED(status));
2693 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2695 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2696 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2697 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2699 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2700 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2704 * The last wait() should report exit 2, i.e., a normal _exit
2705 * from the signal handler. In the meantime, catch and proceed
2706 * past any syscall stops.
2708 wpid = waitpid(fpid, &status, 0);
2709 ATF_REQUIRE(wpid == fpid);
2710 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2711 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2712 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2713 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2715 ATF_REQUIRE(WIFEXITED(status));
2716 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2721 wpid = wait(&status);
2722 ATF_REQUIRE(wpid == -1);
2723 ATF_REQUIRE(errno == ECHILD);
2728 * Verify a signal delivered by ptrace is noticed by kevent(2).
2730 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2731 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2734 int status, kq, nevents;
2737 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2739 ATF_REQUIRE((fpid = fork()) != -1);
2741 CHILD_REQUIRE((kq = kqueue()) > 0);
2742 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2743 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2748 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2749 if (nevents == -1 && errno == EINTR)
2751 CHILD_REQUIRE(nevents > 0);
2752 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2753 CHILD_REQUIRE(kev.ident == SIGUSR1);
2760 /* The first wait() should report the stop from SIGSTOP. */
2761 wpid = waitpid(fpid, &status, 0);
2762 ATF_REQUIRE(wpid == fpid);
2763 ATF_REQUIRE(WIFSTOPPED(status));
2764 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2766 /* Continue with the SIGUSR1. */
2767 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2770 * The last wait() should report normal exit with code 1.
2772 wpid = waitpid(fpid, &status, 0);
2773 ATF_REQUIRE(wpid == fpid);
2774 ATF_REQUIRE(WIFEXITED(status));
2775 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2777 wpid = wait(&status);
2778 ATF_REQUIRE(wpid == -1);
2779 ATF_REQUIRE(errno == ECHILD);
2783 signal_thread(void *arg)
2788 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2790 /* Wait for this thread to receive a SIGUSR1. */
2792 err = sem_wait(&sigusr1_sem);
2793 CHILD_REQUIRE(err == 0 || errno == EINTR);
2794 } while (err != 0 && errno == EINTR);
2796 /* Free our companion thread from the barrier. */
2797 pthread_barrier_wait(pbarrier);
2800 * Swap ignore duties; the next SIGUSR1 should go to the
2803 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2804 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2805 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2807 /* Sync up threads after swapping signal masks. */
2808 pthread_barrier_wait(pbarrier);
2810 /* Wait until our companion has received its SIGUSR1. */
2811 pthread_barrier_wait(pbarrier);
2817 * Verify that a traced process with blocked signal received the
2818 * signal from kill() once unmasked.
2820 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2821 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2823 struct ptrace_lwpinfo pl;
2828 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2829 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2832 ATF_REQUIRE((fpid = fork()) != -1);
2834 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2835 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2836 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2839 CHILD_REQUIRE(got_usr1 == 0);
2841 /* Allow the pending SIGUSR1 in now. */
2842 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2843 /* Wait to receive a SIGUSR1. */
2845 err = sem_wait(&sigusr1_sem);
2846 CHILD_REQUIRE(err == 0 || errno == EINTR);
2847 } while (err != 0 && errno == EINTR);
2848 CHILD_REQUIRE(got_usr1 == 1);
2852 /* The first wait() should report the stop from SIGSTOP. */
2853 wpid = waitpid(fpid, &status, 0);
2854 ATF_REQUIRE(wpid == fpid);
2855 ATF_REQUIRE(WIFSTOPPED(status));
2856 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2857 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2858 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2860 /* Send blocked SIGUSR1 which should cause a stop. */
2861 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2863 /* Continue the child ignoring the SIGSTOP. */
2864 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2866 /* The next wait() should report the kill(SIGUSR1) was received. */
2867 wpid = waitpid(fpid, &status, 0);
2868 ATF_REQUIRE(wpid == fpid);
2869 ATF_REQUIRE(WIFSTOPPED(status));
2870 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2871 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2872 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2874 /* Continue the child, allowing in the SIGUSR1. */
2875 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2877 /* The last wait() should report normal exit with code 1. */
2878 wpid = waitpid(fpid, &status, 0);
2879 ATF_REQUIRE(wpid == fpid);
2880 ATF_REQUIRE(WIFEXITED(status));
2881 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2883 wpid = wait(&status);
2884 ATF_REQUIRE(wpid == -1);
2885 ATF_REQUIRE(errno == ECHILD);
2889 * Verify that a traced process with blocked signal received the
2890 * signal from PT_CONTINUE once unmasked.
2892 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2893 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2895 struct ptrace_lwpinfo pl;
2900 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2901 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2904 ATF_REQUIRE((fpid = fork()) != -1);
2906 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2907 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2908 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2911 CHILD_REQUIRE(got_usr1 == 0);
2913 /* Allow the pending SIGUSR1 in now. */
2914 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2915 /* Wait to receive a SIGUSR1. */
2917 err = sem_wait(&sigusr1_sem);
2918 CHILD_REQUIRE(err == 0 || errno == EINTR);
2919 } while (err != 0 && errno == EINTR);
2921 CHILD_REQUIRE(got_usr1 == 1);
2925 /* The first wait() should report the stop from SIGSTOP. */
2926 wpid = waitpid(fpid, &status, 0);
2927 ATF_REQUIRE(wpid == fpid);
2928 ATF_REQUIRE(WIFSTOPPED(status));
2929 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2930 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2931 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2933 /* Continue the child replacing SIGSTOP with SIGUSR1. */
2934 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2936 /* The next wait() should report the SIGUSR1 was received. */
2937 wpid = waitpid(fpid, &status, 0);
2938 ATF_REQUIRE(wpid == fpid);
2939 ATF_REQUIRE(WIFSTOPPED(status));
2940 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2941 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2942 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2944 /* Continue the child, ignoring the SIGUSR1. */
2945 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2947 /* The last wait() should report normal exit with code 1. */
2948 wpid = waitpid(fpid, &status, 0);
2949 ATF_REQUIRE(wpid == fpid);
2950 ATF_REQUIRE(WIFEXITED(status));
2951 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2953 wpid = wait(&status);
2954 ATF_REQUIRE(wpid == -1);
2955 ATF_REQUIRE(errno == ECHILD);
2959 * Verify that if ptrace stops due to a signal but continues with
2960 * a different signal that the new signal is routed to a thread
2961 * that can accept it, and that the thread is awakened by the signal
2962 * in a timely manner.
2964 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2965 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2971 pthread_barrier_t barrier;
2973 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2974 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2975 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2977 ATF_REQUIRE((fpid = fork()) != -1);
2979 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2981 /* The other thread should receive the first SIGUSR1. */
2982 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2983 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2984 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2988 /* Wait until other thread has received its SIGUSR1. */
2989 pthread_barrier_wait(&barrier);
2992 * Swap ignore duties; the next SIGUSR1 should go to this
2995 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2997 /* Sync up threads after swapping signal masks. */
2998 pthread_barrier_wait(&barrier);
3001 * Sync up with test code; we're ready for the next SIGUSR1
3006 /* Wait for this thread to receive a SIGUSR1. */
3008 err = sem_wait(&sigusr1_sem);
3009 CHILD_REQUIRE(err == 0 || errno == EINTR);
3010 } while (err != 0 && errno == EINTR);
3012 /* Free the other thread from the barrier. */
3013 pthread_barrier_wait(&barrier);
3015 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3020 /* The first wait() should report the stop from SIGSTOP. */
3021 wpid = waitpid(fpid, &status, 0);
3022 ATF_REQUIRE(wpid == fpid);
3023 ATF_REQUIRE(WIFSTOPPED(status));
3024 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3026 /* Continue the child ignoring the SIGSTOP. */
3027 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3030 * Send a signal without ptrace that either thread will accept (USR2,
3033 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3035 /* The second wait() should report a SIGUSR2 was received. */
3036 wpid = waitpid(fpid, &status, 0);
3037 ATF_REQUIRE(wpid == fpid);
3038 ATF_REQUIRE(WIFSTOPPED(status));
3039 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3041 /* Continue the child, changing the signal to USR1. */
3042 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3044 /* The next wait() should report the stop from SIGSTOP. */
3045 wpid = waitpid(fpid, &status, 0);
3046 ATF_REQUIRE(wpid == fpid);
3047 ATF_REQUIRE(WIFSTOPPED(status));
3048 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3050 /* Continue the child ignoring the SIGSTOP. */
3051 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3053 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3055 /* The next wait() should report a SIGUSR2 was received. */
3056 wpid = waitpid(fpid, &status, 0);
3057 ATF_REQUIRE(wpid == fpid);
3058 ATF_REQUIRE(WIFSTOPPED(status));
3059 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3061 /* Continue the child, changing the signal to USR1. */
3062 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3064 /* The last wait() should report normal exit with code 1. */
3065 wpid = waitpid(fpid, &status, 0);
3066 ATF_REQUIRE(wpid == fpid);
3067 ATF_REQUIRE(WIFEXITED(status));
3068 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3070 wpid = wait(&status);
3071 ATF_REQUIRE(wpid == -1);
3072 ATF_REQUIRE(errno == ECHILD);
3076 raise_sigstop_thread(void *arg __unused)
3084 sleep_thread(void *arg __unused)
3092 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3101 * Become the reaper for this process tree. We need to be able to check
3102 * that both child and grandchild have died.
3104 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3107 ATF_REQUIRE(fpid >= 0);
3110 CHILD_REQUIRE(fpid >= 0);
3114 /* Pin to CPU 0 to serialize thread execution. */
3116 CPU_SET(0, &setmask);
3117 CHILD_REQUIRE(cpuset(&setid) == 0);
3118 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3119 CPU_WHICH_CPUSET, setid,
3120 sizeof(setmask), &setmask) == 0);
3122 if (sigstop_from_main_thread) {
3124 * We expect the SIGKILL sent when our parent
3125 * dies to be delivered to the new thread.
3126 * Raise the SIGSTOP in this thread so the
3129 CHILD_REQUIRE(pthread_create(&t, NULL,
3130 sleep_thread, NULL) == 0);
3134 * We expect the SIGKILL to be delivered to
3135 * this thread. After creating the new thread,
3136 * just get off the CPU so the other thread can
3137 * raise the SIGSTOP.
3139 CHILD_REQUIRE(pthread_create(&t, NULL,
3140 raise_sigstop_thread, NULL) == 0);
3146 /* First stop is trace_me() immediately after fork. */
3147 wpid = waitpid(fpid, &status, 0);
3148 CHILD_REQUIRE(wpid == fpid);
3149 CHILD_REQUIRE(WIFSTOPPED(status));
3150 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3152 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3154 /* Second stop is from the raise(SIGSTOP). */
3155 wpid = waitpid(fpid, &status, 0);
3156 CHILD_REQUIRE(wpid == fpid);
3157 CHILD_REQUIRE(WIFSTOPPED(status));
3158 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3161 * Terminate tracing process without detaching. Our child
3168 * We should get a normal exit from our immediate child and a SIGKILL
3169 * exit from our grandchild. The latter case is the interesting one.
3170 * Our grandchild should not have stopped due to the SIGSTOP that was
3171 * left dangling when its parent died.
3173 for (i = 0; i < 2; ++i) {
3174 wpid = wait(&status);
3176 ATF_REQUIRE(WIFEXITED(status) &&
3177 WEXITSTATUS(status) == 0);
3179 ATF_REQUIRE(WIFSIGNALED(status) &&
3180 WTERMSIG(status) == SIGKILL);
3185 * These two tests ensure that if the tracing process exits without detaching
3186 * just after the child received a SIGSTOP, the child is cleanly killed and
3187 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3188 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3189 * different threads, the SIGKILL must win. There are two variants of this
3190 * test, designed to catch the case where the SIGKILL is delivered to the
3191 * younger thread (the first test) and the case where the SIGKILL is delivered
3192 * to the older thread (the second test). This behavior has changed in the
3193 * past, so make no assumption.
3195 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3196 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3199 atf_tc_set_md_var(tc, "require.user", "root");
3201 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3204 terminate_with_pending_sigstop(true);
3207 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3208 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3211 atf_tc_set_md_var(tc, "require.user", "root");
3213 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3216 terminate_with_pending_sigstop(false);
3220 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3223 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3224 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3226 struct ptrace_lwpinfo pl;
3228 int status, event_mask, new_event_mask;
3230 ATF_REQUIRE((fpid = fork()) != -1);
3237 /* The first wait() should report the stop from trace_me(). */
3238 wpid = waitpid(fpid, &status, 0);
3239 ATF_REQUIRE(wpid == fpid);
3240 ATF_REQUIRE(WIFSTOPPED(status));
3241 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3243 /* Set several unobtrusive event bits. */
3244 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3245 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3246 sizeof(event_mask)) == 0);
3248 /* Send a SIGKILL without using ptrace. */
3249 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3251 /* Continue the child ignoring the SIGSTOP. */
3252 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3254 /* The next stop should be due to the SIGKILL. */
3255 wpid = waitpid(fpid, &status, 0);
3256 ATF_REQUIRE(wpid == fpid);
3257 ATF_REQUIRE(WIFSTOPPED(status));
3258 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3260 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3261 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3262 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3264 /* Continue the child ignoring the SIGKILL. */
3265 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3267 /* The next wait() should report the stop from SIGSTOP. */
3268 wpid = waitpid(fpid, &status, 0);
3269 ATF_REQUIRE(wpid == fpid);
3270 ATF_REQUIRE(WIFSTOPPED(status));
3271 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3273 /* Check the current event mask. It should not have changed. */
3275 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3276 sizeof(new_event_mask)) == 0);
3277 ATF_REQUIRE(event_mask == new_event_mask);
3279 /* Continue the child to let it exit. */
3280 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3282 /* The last event should be for the child process's exit. */
3283 wpid = waitpid(fpid, &status, 0);
3284 ATF_REQUIRE(WIFEXITED(status));
3285 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3287 wpid = wait(&status);
3288 ATF_REQUIRE(wpid == -1);
3289 ATF_REQUIRE(errno == ECHILD);
3293 flock_thread(void *arg)
3298 (void)flock(fd, LOCK_EX);
3299 (void)flock(fd, LOCK_UN);
3304 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3305 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3306 * on a lock. This is a regression test for r318191.
3308 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3309 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3311 pthread_barrier_t barrier;
3312 pthread_barrierattr_t battr;
3315 int error, fd, i, status;
3317 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3318 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3319 PTHREAD_PROCESS_SHARED) == 0);
3320 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3322 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3323 fd = mkstemp(tmpfile);
3324 ATF_REQUIRE(fd >= 0);
3326 ATF_REQUIRE((child = fork()) != -1);
3331 error = pthread_barrier_wait(&barrier);
3332 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3335 cfd = open(tmpfile, O_RDONLY);
3340 * We want at least two threads blocked on the file lock since
3341 * the SIGSTOP from PT_ATTACH may kick one of them out of
3344 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3346 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3348 if (pthread_join(t[0], NULL) != 0)
3350 if (pthread_join(t[1], NULL) != 0)
3355 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3357 error = pthread_barrier_wait(&barrier);
3358 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3361 * Give the child some time to block. Is there a better way to do this?
3366 * Attach and give the child 3 seconds to stop.
3368 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3369 for (i = 0; i < 3; i++) {
3370 wpid = waitpid(child, &status, WNOHANG);
3371 if (wpid == child && WIFSTOPPED(status) &&
3372 WSTOPSIG(status) == SIGSTOP)
3376 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3378 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3380 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3381 ATF_REQUIRE(unlink(tmpfile) == 0);
3382 ATF_REQUIRE(close(fd) == 0);
3386 sigusr1_step_handler(int sig)
3389 CHILD_REQUIRE(sig == SIGUSR1);
3394 * Verify that PT_STEP with a signal invokes the signal before
3395 * stepping the next instruction (and that the next instruction is
3396 * stepped correctly).
3398 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3399 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3401 struct ptrace_lwpinfo pl;
3405 ATF_REQUIRE((fpid = fork()) != -1);
3408 signal(SIGUSR1, sigusr1_step_handler);
3413 /* The first wait() should report the stop from SIGSTOP. */
3414 wpid = waitpid(fpid, &status, 0);
3415 ATF_REQUIRE(wpid == fpid);
3416 ATF_REQUIRE(WIFSTOPPED(status));
3417 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3419 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3421 /* The next stop should report the SIGABRT in the child body. */
3422 wpid = waitpid(fpid, &status, 0);
3423 ATF_REQUIRE(wpid == fpid);
3424 ATF_REQUIRE(WIFSTOPPED(status));
3425 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3427 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3428 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3429 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3431 /* Step the child process inserting SIGUSR1. */
3432 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3434 /* The next stop should report the SIGABRT in the signal handler. */
3435 wpid = waitpid(fpid, &status, 0);
3436 ATF_REQUIRE(wpid == fpid);
3437 ATF_REQUIRE(WIFSTOPPED(status));
3438 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3440 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3441 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3442 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3444 /* Continue the child process discarding the signal. */
3445 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3447 /* The next stop should report a trace trap from PT_STEP. */
3448 wpid = waitpid(fpid, &status, 0);
3449 ATF_REQUIRE(wpid == fpid);
3450 ATF_REQUIRE(WIFSTOPPED(status));
3451 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3453 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3454 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3455 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3456 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3458 /* Continue the child to let it exit. */
3459 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3461 /* The last event should be for the child process's exit. */
3462 wpid = waitpid(fpid, &status, 0);
3463 ATF_REQUIRE(WIFEXITED(status));
3464 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3466 wpid = wait(&status);
3467 ATF_REQUIRE(wpid == -1);
3468 ATF_REQUIRE(errno == ECHILD);
3471 #if defined(__amd64__) || defined(__i386__)
3473 * Only x86 both define breakpoint() and have a PC after breakpoint so
3474 * that restarting doesn't retrigger the breakpoint.
3477 continue_thread(void *arg)
3484 continue_thread_main(void)
3486 pthread_t threads[2];
3488 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3490 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3492 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3493 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3498 * Ensure that PT_CONTINUE clears the status of the thread that
3499 * triggered the stop even if a different thread's LWP was passed to
3502 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3503 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3505 struct ptrace_lwpinfo pl;
3511 ATF_REQUIRE((fpid = fork()) != -1);
3514 continue_thread_main();
3517 /* The first wait() should report the stop from SIGSTOP. */
3518 wpid = waitpid(fpid, &status, 0);
3519 ATF_REQUIRE(wpid == fpid);
3520 ATF_REQUIRE(WIFSTOPPED(status));
3521 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3523 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3526 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3528 /* Continue the child ignoring the SIGSTOP. */
3529 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3531 /* One of the new threads should report it's birth. */
3532 wpid = waitpid(fpid, &status, 0);
3533 ATF_REQUIRE(wpid == fpid);
3534 ATF_REQUIRE(WIFSTOPPED(status));
3535 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3537 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3538 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3539 (PL_FLAG_BORN | PL_FLAG_SCX));
3540 lwps[0] = pl.pl_lwpid;
3543 * Suspend this thread to ensure both threads are alive before
3544 * hitting the breakpoint.
3546 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3548 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3550 /* Second thread should report it's birth. */
3551 wpid = waitpid(fpid, &status, 0);
3552 ATF_REQUIRE(wpid == fpid);
3553 ATF_REQUIRE(WIFSTOPPED(status));
3554 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3556 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3557 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3558 (PL_FLAG_BORN | PL_FLAG_SCX));
3559 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3560 lwps[1] = pl.pl_lwpid;
3562 /* Resume both threads waiting for breakpoint events. */
3563 hit_break[0] = hit_break[1] = false;
3564 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3565 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3567 /* One thread should report a breakpoint. */
3568 wpid = waitpid(fpid, &status, 0);
3569 ATF_REQUIRE(wpid == fpid);
3570 ATF_REQUIRE(WIFSTOPPED(status));
3571 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3573 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3574 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3575 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3576 pl.pl_siginfo.si_code == TRAP_BRKPT);
3577 if (pl.pl_lwpid == lwps[0])
3581 hit_break[i] = true;
3584 * Resume both threads but pass the other thread's LWPID to
3587 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3590 * Will now get two thread exit events and one more breakpoint
3593 for (j = 0; j < 3; j++) {
3594 wpid = waitpid(fpid, &status, 0);
3595 ATF_REQUIRE(wpid == fpid);
3596 ATF_REQUIRE(WIFSTOPPED(status));
3597 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3599 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3602 if (pl.pl_lwpid == lwps[0])
3607 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3608 if (pl.pl_flags & PL_FLAG_EXITED) {
3609 ATF_REQUIRE_MSG(hit_break[i],
3610 "exited thread did not report breakpoint");
3613 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3614 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3615 pl.pl_siginfo.si_code == TRAP_BRKPT);
3616 ATF_REQUIRE_MSG(!hit_break[i],
3617 "double breakpoint event");
3618 hit_break[i] = true;
3621 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3624 /* Both threads should have exited. */
3625 ATF_REQUIRE(lwps[0] == 0);
3626 ATF_REQUIRE(lwps[1] == 0);
3628 /* The last event should be for the child process's exit. */
3629 wpid = waitpid(fpid, &status, 0);
3630 ATF_REQUIRE(WIFEXITED(status));
3631 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3633 wpid = wait(&status);
3634 ATF_REQUIRE(wpid == -1);
3635 ATF_REQUIRE(errno == ECHILD);
3642 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3643 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3644 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3645 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3646 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3647 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3648 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3649 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3651 ptrace__follow_fork_child_detached_unrelated_debugger);
3653 ptrace__follow_fork_parent_detached_unrelated_debugger);
3654 ATF_TP_ADD_TC(tp, ptrace__getppid);
3655 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3656 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3657 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3658 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3659 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3660 ATF_TP_ADD_TC(tp, ptrace__siginfo);
3661 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3662 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3663 ATF_TP_ADD_TC(tp, ptrace__event_mask);
3664 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3665 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3666 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
3667 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3669 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3670 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3671 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3672 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3673 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3674 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3676 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3677 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3678 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3679 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3680 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3681 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3682 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3683 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3684 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3685 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3686 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3687 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3688 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3689 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3690 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3691 #if defined(__amd64__) || defined(__i386__)
3692 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3695 return (atf_no_error());