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>
34 #include <sys/ptrace.h>
35 #include <sys/syscall.h>
36 #include <sys/sysctl.h>
40 #include <machine/cpufunc.h>
42 #include <semaphore.h>
50 * A variant of ATF_REQUIRE that is suitable for use in child
51 * processes. This only works if the parent process is tripped up by
52 * the early exit and fails some requirement itself.
54 #define CHILD_REQUIRE(exp) do { \
56 child_fail_require(__FILE__, __LINE__, \
61 child_fail_require(const char *file, int line, const char *str)
65 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
66 write(2, buf, strlen(buf));
74 /* Attach the parent process as a tracer of this process. */
75 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
82 attach_child(pid_t pid)
87 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
89 wpid = waitpid(pid, &status, 0);
90 ATF_REQUIRE(wpid == pid);
91 ATF_REQUIRE(WIFSTOPPED(status));
92 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
96 wait_for_zombie(pid_t pid)
100 * Wait for a process to exit. This is kind of gross, but
101 * there is not a better way.
104 struct kinfo_proc kp;
110 mib[2] = KERN_PROC_PID;
113 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
114 /* The KERN_PROC_PID sysctl fails for zombies. */
115 ATF_REQUIRE(errno == ESRCH);
123 * Verify that a parent debugger process "sees" the exit of a debugged
124 * process exactly once when attached via PT_TRACE_ME.
126 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
127 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
132 ATF_REQUIRE((child = fork()) != -1);
140 /* Parent process. */
142 /* The first wait() should report the stop from SIGSTOP. */
143 wpid = waitpid(child, &status, 0);
144 ATF_REQUIRE(wpid == child);
145 ATF_REQUIRE(WIFSTOPPED(status));
146 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
148 /* Continue the child ignoring the SIGSTOP. */
149 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
151 /* The second wait() should report the exit status. */
152 wpid = waitpid(child, &status, 0);
153 ATF_REQUIRE(wpid == child);
154 ATF_REQUIRE(WIFEXITED(status));
155 ATF_REQUIRE(WEXITSTATUS(status) == 1);
157 /* The child should no longer exist. */
158 wpid = waitpid(child, &status, 0);
159 ATF_REQUIRE(wpid == -1);
160 ATF_REQUIRE(errno == ECHILD);
164 * Verify that a parent debugger process "sees" the exit of a debugged
165 * process exactly once when attached via PT_ATTACH.
167 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
168 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
171 int cpipe[2], status;
174 ATF_REQUIRE(pipe(cpipe) == 0);
175 ATF_REQUIRE((child = fork()) != -1);
180 /* Wait for the parent to attach. */
181 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
187 /* Parent process. */
189 /* Attach to the child process. */
192 /* Continue the child ignoring the SIGSTOP. */
193 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
195 /* Signal the child to exit. */
198 /* The second wait() should report the exit status. */
199 wpid = waitpid(child, &status, 0);
200 ATF_REQUIRE(wpid == child);
201 ATF_REQUIRE(WIFEXITED(status));
202 ATF_REQUIRE(WEXITSTATUS(status) == 1);
204 /* The child should no longer exist. */
205 wpid = waitpid(child, &status, 0);
206 ATF_REQUIRE(wpid == -1);
207 ATF_REQUIRE(errno == ECHILD);
211 * Verify that a parent process "sees" the exit of a debugged process only
212 * after the debugger has seen it.
214 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
215 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
217 pid_t child, debugger, wpid;
218 int cpipe[2], dpipe[2], status;
221 ATF_REQUIRE(pipe(cpipe) == 0);
222 ATF_REQUIRE((child = fork()) != -1);
228 /* Wait for parent to be ready. */
229 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
235 ATF_REQUIRE(pipe(dpipe) == 0);
236 ATF_REQUIRE((debugger = fork()) != -1);
239 /* Debugger process. */
242 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
244 wpid = waitpid(child, &status, 0);
245 CHILD_REQUIRE(wpid == child);
246 CHILD_REQUIRE(WIFSTOPPED(status));
247 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
249 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
251 /* Signal parent that debugger is attached. */
252 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
254 /* Wait for parent's failed wait. */
255 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
257 wpid = waitpid(child, &status, 0);
258 CHILD_REQUIRE(wpid == child);
259 CHILD_REQUIRE(WIFEXITED(status));
260 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
266 /* Parent process. */
268 /* Wait for the debugger to attach to the child. */
269 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
271 /* Release the child. */
272 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
273 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
276 wait_for_zombie(child);
279 * This wait should return a pid of 0 to indicate no status to
280 * report. The parent should see the child as non-exited
281 * until the debugger sees the exit.
283 wpid = waitpid(child, &status, WNOHANG);
284 ATF_REQUIRE(wpid == 0);
286 /* Signal the debugger to wait for the child. */
289 /* Wait for the debugger. */
290 wpid = waitpid(debugger, &status, 0);
291 ATF_REQUIRE(wpid == debugger);
292 ATF_REQUIRE(WIFEXITED(status));
293 ATF_REQUIRE(WEXITSTATUS(status) == 0);
295 /* The child process should now be ready. */
296 wpid = waitpid(child, &status, WNOHANG);
297 ATF_REQUIRE(wpid == child);
298 ATF_REQUIRE(WIFEXITED(status));
299 ATF_REQUIRE(WEXITSTATUS(status) == 1);
303 * Verify that a parent process "sees" the exit of a debugged process
304 * only after a non-direct-child debugger has seen it. In particular,
305 * various wait() calls in the parent must avoid failing with ESRCH by
306 * checking the parent's orphan list for the debugee.
308 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
309 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
311 pid_t child, debugger, fpid, wpid;
312 int cpipe[2], dpipe[2], status;
315 ATF_REQUIRE(pipe(cpipe) == 0);
316 ATF_REQUIRE((child = fork()) != -1);
322 /* Wait for parent to be ready. */
323 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
329 ATF_REQUIRE(pipe(dpipe) == 0);
330 ATF_REQUIRE((debugger = fork()) != -1);
333 /* Debugger parent. */
336 * Fork again and drop the debugger parent so that the
337 * debugger is not a child of the main parent.
339 CHILD_REQUIRE((fpid = fork()) != -1);
343 /* Debugger process. */
346 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
348 wpid = waitpid(child, &status, 0);
349 CHILD_REQUIRE(wpid == child);
350 CHILD_REQUIRE(WIFSTOPPED(status));
351 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
353 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
355 /* Signal parent that debugger is attached. */
356 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
358 /* Wait for parent's failed wait. */
359 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
361 wpid = waitpid(child, &status, 0);
362 CHILD_REQUIRE(wpid == child);
363 CHILD_REQUIRE(WIFEXITED(status));
364 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
370 /* Parent process. */
372 /* Wait for the debugger parent process to exit. */
373 wpid = waitpid(debugger, &status, 0);
374 ATF_REQUIRE(wpid == debugger);
375 ATF_REQUIRE(WIFEXITED(status));
376 ATF_REQUIRE(WEXITSTATUS(status) == 2);
378 /* A WNOHANG wait here should see the non-exited child. */
379 wpid = waitpid(child, &status, WNOHANG);
380 ATF_REQUIRE(wpid == 0);
382 /* Wait for the debugger to attach to the child. */
383 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
385 /* Release the child. */
386 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
387 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
390 wait_for_zombie(child);
393 * This wait should return a pid of 0 to indicate no status to
394 * report. The parent should see the child as non-exited
395 * until the debugger sees the exit.
397 wpid = waitpid(child, &status, WNOHANG);
398 ATF_REQUIRE(wpid == 0);
400 /* Signal the debugger to wait for the child. */
401 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
403 /* Wait for the debugger. */
404 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
407 /* The child process should now be ready. */
408 wpid = waitpid(child, &status, WNOHANG);
409 ATF_REQUIRE(wpid == child);
410 ATF_REQUIRE(WIFEXITED(status));
411 ATF_REQUIRE(WEXITSTATUS(status) == 1);
415 * The parent process should always act the same regardless of how the
416 * debugger is attached to it.
419 follow_fork_parent(bool use_vfork)
425 CHILD_REQUIRE((fpid = vfork()) != -1);
427 CHILD_REQUIRE((fpid = fork()) != -1);
433 wpid = waitpid(fpid, &status, 0);
434 CHILD_REQUIRE(wpid == fpid);
435 CHILD_REQUIRE(WIFEXITED(status));
436 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
442 * Helper routine for follow fork tests. This waits for two stops
443 * that report both "sides" of a fork. It returns the pid of the new
447 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
449 struct ptrace_lwpinfo pl;
450 bool fork_reported[2];
454 fork_reported[0] = false;
455 fork_reported[1] = false;
459 * Each process should report a fork event. The parent should
460 * report a PL_FLAG_FORKED event, and the child should report
461 * a PL_FLAG_CHILD event.
463 for (i = 0; i < 2; i++) {
464 wpid = wait(&status);
465 ATF_REQUIRE(wpid > 0);
466 ATF_REQUIRE(WIFSTOPPED(status));
468 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
470 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
472 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
473 (PL_FLAG_FORKED | PL_FLAG_CHILD));
474 if (pl.pl_flags & PL_FLAG_CHILD) {
475 ATF_REQUIRE(wpid != parent);
476 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
477 ATF_REQUIRE(!fork_reported[1]);
481 ATF_REQUIRE(child == wpid);
484 fork_reported[1] = true;
486 ATF_REQUIRE(wpid == parent);
487 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
488 ATF_REQUIRE(!fork_reported[0]);
490 child = pl.pl_child_pid;
492 ATF_REQUIRE(child == pl.pl_child_pid);
495 fork_reported[0] = true;
503 * Verify that a new child process is stopped after a followed fork and
504 * that the traced parent sees the exit of the child after the debugger
505 * when both processes remain attached to the debugger.
507 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
508 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
510 pid_t children[2], fpid, wpid;
513 ATF_REQUIRE((fpid = fork()) != -1);
516 follow_fork_parent(false);
519 /* Parent process. */
522 /* The first wait() should report the stop from SIGSTOP. */
523 wpid = waitpid(children[0], &status, 0);
524 ATF_REQUIRE(wpid == children[0]);
525 ATF_REQUIRE(WIFSTOPPED(status));
526 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
528 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
530 /* Continue the child ignoring the SIGSTOP. */
531 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
533 children[1] = handle_fork_events(children[0], NULL);
534 ATF_REQUIRE(children[1] > 0);
536 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
537 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
540 * The child can't exit until the grandchild reports status, so the
541 * grandchild should report its exit first to the debugger.
543 wpid = wait(&status);
544 ATF_REQUIRE(wpid == children[1]);
545 ATF_REQUIRE(WIFEXITED(status));
546 ATF_REQUIRE(WEXITSTATUS(status) == 2);
548 wpid = wait(&status);
549 ATF_REQUIRE(wpid == children[0]);
550 ATF_REQUIRE(WIFEXITED(status));
551 ATF_REQUIRE(WEXITSTATUS(status) == 1);
553 wpid = wait(&status);
554 ATF_REQUIRE(wpid == -1);
555 ATF_REQUIRE(errno == ECHILD);
559 * Verify that a new child process is stopped after a followed fork
560 * and that the traced parent sees the exit of the child when the new
561 * child process is detached after it reports its fork.
563 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
564 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
566 pid_t children[2], fpid, wpid;
569 ATF_REQUIRE((fpid = fork()) != -1);
572 follow_fork_parent(false);
575 /* Parent process. */
578 /* The first wait() should report the stop from SIGSTOP. */
579 wpid = waitpid(children[0], &status, 0);
580 ATF_REQUIRE(wpid == children[0]);
581 ATF_REQUIRE(WIFSTOPPED(status));
582 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
584 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
586 /* Continue the child ignoring the SIGSTOP. */
587 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
589 children[1] = handle_fork_events(children[0], NULL);
590 ATF_REQUIRE(children[1] > 0);
592 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
593 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
596 * Should not see any status from the grandchild now, only the
599 wpid = wait(&status);
600 ATF_REQUIRE(wpid == children[0]);
601 ATF_REQUIRE(WIFEXITED(status));
602 ATF_REQUIRE(WEXITSTATUS(status) == 1);
604 wpid = wait(&status);
605 ATF_REQUIRE(wpid == -1);
606 ATF_REQUIRE(errno == ECHILD);
610 * Verify that a new child process is stopped after a followed fork
611 * and that the traced parent sees the exit of the child when the
612 * traced parent is detached after the fork.
614 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
615 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
617 pid_t children[2], fpid, wpid;
620 ATF_REQUIRE((fpid = fork()) != -1);
623 follow_fork_parent(false);
626 /* Parent process. */
629 /* The first wait() should report the stop from SIGSTOP. */
630 wpid = waitpid(children[0], &status, 0);
631 ATF_REQUIRE(wpid == children[0]);
632 ATF_REQUIRE(WIFSTOPPED(status));
633 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
635 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
637 /* Continue the child ignoring the SIGSTOP. */
638 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
640 children[1] = handle_fork_events(children[0], NULL);
641 ATF_REQUIRE(children[1] > 0);
643 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
644 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
647 * The child can't exit until the grandchild reports status, so the
648 * grandchild should report its exit first to the debugger.
650 * Even though the child process is detached, it is still a
651 * child of the debugger, so it will still report it's exit
652 * after the grandchild.
654 wpid = wait(&status);
655 ATF_REQUIRE(wpid == children[1]);
656 ATF_REQUIRE(WIFEXITED(status));
657 ATF_REQUIRE(WEXITSTATUS(status) == 2);
659 wpid = wait(&status);
660 ATF_REQUIRE(wpid == children[0]);
661 ATF_REQUIRE(WIFEXITED(status));
662 ATF_REQUIRE(WEXITSTATUS(status) == 1);
664 wpid = wait(&status);
665 ATF_REQUIRE(wpid == -1);
666 ATF_REQUIRE(errno == ECHILD);
670 attach_fork_parent(int cpipe[2])
676 /* Double-fork to disassociate from the debugger. */
677 CHILD_REQUIRE((fpid = fork()) != -1);
681 /* Send the pid of the disassociated child to the debugger. */
683 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
685 /* Wait for the debugger to attach. */
686 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
690 * Verify that a new child process is stopped after a followed fork and
691 * that the traced parent sees the exit of the child after the debugger
692 * when both processes remain attached to the debugger. In this test
693 * the parent that forks is not a direct child of the debugger.
695 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
696 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
698 pid_t children[2], fpid, wpid;
699 int cpipe[2], status;
701 ATF_REQUIRE(pipe(cpipe) == 0);
702 ATF_REQUIRE((fpid = fork()) != -1);
704 attach_fork_parent(cpipe);
705 follow_fork_parent(false);
708 /* Parent process. */
711 /* Wait for the direct child to exit. */
712 wpid = waitpid(fpid, &status, 0);
713 ATF_REQUIRE(wpid == fpid);
714 ATF_REQUIRE(WIFEXITED(status));
715 ATF_REQUIRE(WEXITSTATUS(status) == 3);
717 /* Read the pid of the fork parent. */
718 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
719 sizeof(children[0]));
721 /* Attach to the fork parent. */
722 attach_child(children[0]);
724 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
726 /* Continue the fork parent ignoring the SIGSTOP. */
727 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
729 /* Signal the fork parent to continue. */
732 children[1] = handle_fork_events(children[0], NULL);
733 ATF_REQUIRE(children[1] > 0);
735 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
736 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
739 * The fork parent can't exit until the child reports status,
740 * so the child should report its exit first to the debugger.
742 wpid = wait(&status);
743 ATF_REQUIRE(wpid == children[1]);
744 ATF_REQUIRE(WIFEXITED(status));
745 ATF_REQUIRE(WEXITSTATUS(status) == 2);
747 wpid = wait(&status);
748 ATF_REQUIRE(wpid == children[0]);
749 ATF_REQUIRE(WIFEXITED(status));
750 ATF_REQUIRE(WEXITSTATUS(status) == 1);
752 wpid = wait(&status);
753 ATF_REQUIRE(wpid == -1);
754 ATF_REQUIRE(errno == ECHILD);
758 * Verify that a new child process is stopped after a followed fork
759 * and that the traced parent sees the exit of the child when the new
760 * child process is detached after it reports its fork. In this test
761 * the parent that forks is not a direct child of the debugger.
763 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
764 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
766 pid_t children[2], fpid, wpid;
767 int cpipe[2], status;
769 ATF_REQUIRE(pipe(cpipe) == 0);
770 ATF_REQUIRE((fpid = fork()) != -1);
772 attach_fork_parent(cpipe);
773 follow_fork_parent(false);
776 /* Parent process. */
779 /* Wait for the direct child to exit. */
780 wpid = waitpid(fpid, &status, 0);
781 ATF_REQUIRE(wpid == fpid);
782 ATF_REQUIRE(WIFEXITED(status));
783 ATF_REQUIRE(WEXITSTATUS(status) == 3);
785 /* Read the pid of the fork parent. */
786 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
787 sizeof(children[0]));
789 /* Attach to the fork parent. */
790 attach_child(children[0]);
792 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
794 /* Continue the fork parent ignoring the SIGSTOP. */
795 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
797 /* Signal the fork parent to continue. */
800 children[1] = handle_fork_events(children[0], NULL);
801 ATF_REQUIRE(children[1] > 0);
803 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
804 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
807 * Should not see any status from the child now, only the fork
810 wpid = wait(&status);
811 ATF_REQUIRE(wpid == children[0]);
812 ATF_REQUIRE(WIFEXITED(status));
813 ATF_REQUIRE(WEXITSTATUS(status) == 1);
815 wpid = wait(&status);
816 ATF_REQUIRE(wpid == -1);
817 ATF_REQUIRE(errno == ECHILD);
821 * Verify that a new child process is stopped after a followed fork
822 * and that the traced parent sees the exit of the child when the
823 * traced parent is detached after the fork. In this test the parent
824 * that forks is not a direct child of the debugger.
826 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
827 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
829 pid_t children[2], fpid, wpid;
830 int cpipe[2], status;
832 ATF_REQUIRE(pipe(cpipe) == 0);
833 ATF_REQUIRE((fpid = fork()) != -1);
835 attach_fork_parent(cpipe);
836 follow_fork_parent(false);
839 /* Parent process. */
842 /* Wait for the direct child to exit. */
843 wpid = waitpid(fpid, &status, 0);
844 ATF_REQUIRE(wpid == fpid);
845 ATF_REQUIRE(WIFEXITED(status));
846 ATF_REQUIRE(WEXITSTATUS(status) == 3);
848 /* Read the pid of the fork parent. */
849 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
850 sizeof(children[0]));
852 /* Attach to the fork parent. */
853 attach_child(children[0]);
855 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
857 /* Continue the fork parent ignoring the SIGSTOP. */
858 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
860 /* Signal the fork parent to continue. */
863 children[1] = handle_fork_events(children[0], NULL);
864 ATF_REQUIRE(children[1] > 0);
866 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
867 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
870 * Should not see any status from the fork parent now, only
873 wpid = wait(&status);
874 ATF_REQUIRE(wpid == children[1]);
875 ATF_REQUIRE(WIFEXITED(status));
876 ATF_REQUIRE(WEXITSTATUS(status) == 2);
878 wpid = wait(&status);
879 ATF_REQUIRE(wpid == -1);
880 ATF_REQUIRE(errno == ECHILD);
884 * Verify that a child process does not see an unrelated debugger as its
885 * parent but sees its original parent process.
887 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
888 ATF_TC_BODY(ptrace__getppid, tc)
890 pid_t child, debugger, ppid, wpid;
891 int cpipe[2], dpipe[2], status;
894 ATF_REQUIRE(pipe(cpipe) == 0);
895 ATF_REQUIRE((child = fork()) != -1);
901 /* Wait for parent to be ready. */
902 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
904 /* Report the parent PID to the parent. */
906 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
913 ATF_REQUIRE(pipe(dpipe) == 0);
914 ATF_REQUIRE((debugger = fork()) != -1);
917 /* Debugger process. */
920 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
922 wpid = waitpid(child, &status, 0);
923 CHILD_REQUIRE(wpid == child);
924 CHILD_REQUIRE(WIFSTOPPED(status));
925 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
927 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
929 /* Signal parent that debugger is attached. */
930 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
932 /* Wait for traced child to exit. */
933 wpid = waitpid(child, &status, 0);
934 CHILD_REQUIRE(wpid == child);
935 CHILD_REQUIRE(WIFEXITED(status));
936 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
942 /* Parent process. */
944 /* Wait for the debugger to attach to the child. */
945 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
947 /* Release the child. */
948 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
950 /* Read the parent PID from the child. */
951 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
954 ATF_REQUIRE(ppid == getpid());
956 /* Wait for the debugger. */
957 wpid = waitpid(debugger, &status, 0);
958 ATF_REQUIRE(wpid == debugger);
959 ATF_REQUIRE(WIFEXITED(status));
960 ATF_REQUIRE(WEXITSTATUS(status) == 0);
962 /* The child process should now be ready. */
963 wpid = waitpid(child, &status, WNOHANG);
964 ATF_REQUIRE(wpid == child);
965 ATF_REQUIRE(WIFEXITED(status));
966 ATF_REQUIRE(WEXITSTATUS(status) == 1);
970 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
971 * child process created via fork() reports the correct value.
973 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
974 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
976 struct ptrace_lwpinfo pl[2];
977 pid_t children[2], fpid, wpid;
980 ATF_REQUIRE((fpid = fork()) != -1);
983 follow_fork_parent(false);
986 /* Parent process. */
989 /* The first wait() should report the stop from SIGSTOP. */
990 wpid = waitpid(children[0], &status, 0);
991 ATF_REQUIRE(wpid == children[0]);
992 ATF_REQUIRE(WIFSTOPPED(status));
993 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
995 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
997 /* Continue the child ignoring the SIGSTOP. */
998 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1000 /* Wait for both halves of the fork event to get reported. */
1001 children[1] = handle_fork_events(children[0], pl);
1002 ATF_REQUIRE(children[1] > 0);
1004 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1005 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1006 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1007 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1008 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1010 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1011 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1014 * The child can't exit until the grandchild reports status, so the
1015 * grandchild should report its exit first to the debugger.
1017 wpid = wait(&status);
1018 ATF_REQUIRE(wpid == children[1]);
1019 ATF_REQUIRE(WIFEXITED(status));
1020 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1022 wpid = wait(&status);
1023 ATF_REQUIRE(wpid == children[0]);
1024 ATF_REQUIRE(WIFEXITED(status));
1025 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1027 wpid = wait(&status);
1028 ATF_REQUIRE(wpid == -1);
1029 ATF_REQUIRE(errno == ECHILD);
1033 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1034 * child process created via vfork() reports the correct value.
1036 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1037 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1039 struct ptrace_lwpinfo pl[2];
1040 pid_t children[2], fpid, wpid;
1043 ATF_REQUIRE((fpid = fork()) != -1);
1046 follow_fork_parent(true);
1049 /* Parent process. */
1052 /* The first wait() should report the stop from SIGSTOP. */
1053 wpid = waitpid(children[0], &status, 0);
1054 ATF_REQUIRE(wpid == children[0]);
1055 ATF_REQUIRE(WIFSTOPPED(status));
1056 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1058 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1060 /* Continue the child ignoring the SIGSTOP. */
1061 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1063 /* Wait for both halves of the fork event to get reported. */
1064 children[1] = handle_fork_events(children[0], pl);
1065 ATF_REQUIRE(children[1] > 0);
1067 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1068 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1069 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1070 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1071 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1073 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1074 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1077 * The child can't exit until the grandchild reports status, so the
1078 * grandchild should report its exit first to the debugger.
1080 wpid = wait(&status);
1081 ATF_REQUIRE(wpid == children[1]);
1082 ATF_REQUIRE(WIFEXITED(status));
1083 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1085 wpid = wait(&status);
1086 ATF_REQUIRE(wpid == children[0]);
1087 ATF_REQUIRE(WIFEXITED(status));
1088 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1090 wpid = wait(&status);
1091 ATF_REQUIRE(wpid == -1);
1092 ATF_REQUIRE(errno == ECHILD);
1096 simple_thread(void *arg __unused)
1103 simple_thread_main(void)
1107 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1108 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1113 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1114 * thread reports the correct value.
1116 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1117 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1119 struct ptrace_lwpinfo pl;
1124 ATF_REQUIRE((fpid = fork()) != -1);
1127 simple_thread_main();
1130 /* The first wait() should report the stop from SIGSTOP. */
1131 wpid = waitpid(fpid, &status, 0);
1132 ATF_REQUIRE(wpid == fpid);
1133 ATF_REQUIRE(WIFSTOPPED(status));
1134 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1136 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1138 mainlwp = pl.pl_lwpid;
1141 * Continue the child ignoring the SIGSTOP and tracing all
1142 * system call exits.
1144 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1147 * Wait for the new thread to arrive. pthread_create() might
1148 * invoke any number of system calls. For now we just wait
1149 * for the new thread to arrive and make sure it reports a
1150 * valid system call code. If ptrace grows thread event
1151 * reporting then this test can be made more precise.
1154 wpid = waitpid(fpid, &status, 0);
1155 ATF_REQUIRE(wpid == fpid);
1156 ATF_REQUIRE(WIFSTOPPED(status));
1157 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1159 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1161 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1162 ATF_REQUIRE(pl.pl_syscall_code != 0);
1163 if (pl.pl_lwpid != mainlwp)
1164 /* New thread seen. */
1167 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1170 /* Wait for the child to exit. */
1171 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1173 wpid = waitpid(fpid, &status, 0);
1174 ATF_REQUIRE(wpid == fpid);
1175 if (WIFEXITED(status))
1178 ATF_REQUIRE(WIFSTOPPED(status));
1179 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1180 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1183 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1185 wpid = wait(&status);
1186 ATF_REQUIRE(wpid == -1);
1187 ATF_REQUIRE(errno == ECHILD);
1191 * Verify that the expected LWP events are reported for a child thread.
1193 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1194 ATF_TC_BODY(ptrace__lwp_events, tc)
1196 struct ptrace_lwpinfo pl;
1201 ATF_REQUIRE((fpid = fork()) != -1);
1204 simple_thread_main();
1207 /* The first wait() should report the stop from SIGSTOP. */
1208 wpid = waitpid(fpid, &status, 0);
1209 ATF_REQUIRE(wpid == fpid);
1210 ATF_REQUIRE(WIFSTOPPED(status));
1211 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1213 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1215 lwps[0] = pl.pl_lwpid;
1217 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1219 /* Continue the child ignoring the SIGSTOP. */
1220 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1222 /* The first event should be for the child thread's birth. */
1223 wpid = waitpid(fpid, &status, 0);
1224 ATF_REQUIRE(wpid == fpid);
1225 ATF_REQUIRE(WIFSTOPPED(status));
1226 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1228 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1229 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1230 (PL_FLAG_BORN | PL_FLAG_SCX));
1231 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1232 lwps[1] = pl.pl_lwpid;
1234 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1236 /* The next event should be for the child thread's death. */
1237 wpid = waitpid(fpid, &status, 0);
1238 ATF_REQUIRE(wpid == fpid);
1239 ATF_REQUIRE(WIFSTOPPED(status));
1240 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1242 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1243 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1244 (PL_FLAG_EXITED | PL_FLAG_SCE));
1245 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1247 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1249 /* The last event should be for the child process's exit. */
1250 wpid = waitpid(fpid, &status, 0);
1251 ATF_REQUIRE(WIFEXITED(status));
1252 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1254 wpid = wait(&status);
1255 ATF_REQUIRE(wpid == -1);
1256 ATF_REQUIRE(errno == ECHILD);
1260 exec_thread(void *arg __unused)
1263 execl("/usr/bin/true", "true", NULL);
1268 exec_thread_main(void)
1272 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1279 * Verify that the expected LWP events are reported for a multithreaded
1280 * process that calls execve(2).
1282 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1283 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1285 struct ptrace_lwpinfo pl;
1290 ATF_REQUIRE((fpid = fork()) != -1);
1296 /* The first wait() should report the stop from SIGSTOP. */
1297 wpid = waitpid(fpid, &status, 0);
1298 ATF_REQUIRE(wpid == fpid);
1299 ATF_REQUIRE(WIFSTOPPED(status));
1300 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1302 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1304 lwps[0] = pl.pl_lwpid;
1306 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1308 /* Continue the child ignoring the SIGSTOP. */
1309 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1311 /* The first event should be for the child thread's birth. */
1312 wpid = waitpid(fpid, &status, 0);
1313 ATF_REQUIRE(wpid == fpid);
1314 ATF_REQUIRE(WIFSTOPPED(status));
1315 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1317 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1318 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1319 (PL_FLAG_BORN | PL_FLAG_SCX));
1320 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1321 lwps[1] = pl.pl_lwpid;
1323 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1326 * The next event should be for the main thread's death due to
1327 * single threading from execve().
1329 wpid = waitpid(fpid, &status, 0);
1330 ATF_REQUIRE(wpid == fpid);
1331 ATF_REQUIRE(WIFSTOPPED(status));
1332 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1334 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1335 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1337 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1339 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1341 /* The next event should be for the child process's exec. */
1342 wpid = waitpid(fpid, &status, 0);
1343 ATF_REQUIRE(WIFSTOPPED(status));
1344 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1346 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1347 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1348 (PL_FLAG_EXEC | PL_FLAG_SCX));
1349 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1351 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1353 /* The last event should be for the child process's exit. */
1354 wpid = waitpid(fpid, &status, 0);
1355 ATF_REQUIRE(WIFEXITED(status));
1356 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1358 wpid = wait(&status);
1359 ATF_REQUIRE(wpid == -1);
1360 ATF_REQUIRE(errno == ECHILD);
1364 handler(int sig __unused)
1372 signal(SIGINFO, handler);
1378 * Verify that the expected ptrace event is reported for a signal.
1380 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1381 ATF_TC_BODY(ptrace__siginfo, tc)
1383 struct ptrace_lwpinfo pl;
1387 ATF_REQUIRE((fpid = fork()) != -1);
1393 /* The first wait() should report the stop from SIGSTOP. */
1394 wpid = waitpid(fpid, &status, 0);
1395 ATF_REQUIRE(wpid == fpid);
1396 ATF_REQUIRE(WIFSTOPPED(status));
1397 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1399 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1401 /* The next event should be for the SIGINFO. */
1402 wpid = waitpid(fpid, &status, 0);
1403 ATF_REQUIRE(WIFSTOPPED(status));
1404 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1406 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1407 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1408 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1409 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1410 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1412 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1414 /* The last event should be for the child process's exit. */
1415 wpid = waitpid(fpid, &status, 0);
1416 ATF_REQUIRE(WIFEXITED(status));
1417 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1419 wpid = wait(&status);
1420 ATF_REQUIRE(wpid == -1);
1421 ATF_REQUIRE(errno == ECHILD);
1425 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1427 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1428 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1433 ATF_REQUIRE((fpid = fork()) != -1);
1439 /* The first wait() should report the stop from SIGSTOP. */
1440 wpid = waitpid(fpid, &status, 0);
1441 ATF_REQUIRE(wpid == fpid);
1442 ATF_REQUIRE(WIFSTOPPED(status));
1443 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1446 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1447 sizeof(events)) == 0);
1449 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1451 /* Should get one event at exit. */
1452 wpid = waitpid(fpid, &status, 0);
1453 ATF_REQUIRE(WIFEXITED(status));
1454 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1456 wpid = wait(&status);
1457 ATF_REQUIRE(wpid == -1);
1458 ATF_REQUIRE(errno == ECHILD);
1461 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1462 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1464 struct ptrace_lwpinfo pl;
1468 ATF_REQUIRE((fpid = fork()) != -1);
1474 /* The first wait() should report the stop from SIGSTOP. */
1475 wpid = waitpid(fpid, &status, 0);
1476 ATF_REQUIRE(wpid == fpid);
1477 ATF_REQUIRE(WIFSTOPPED(status));
1478 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1480 events = PTRACE_EXEC;
1481 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1482 sizeof(events)) == 0);
1484 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1486 /* The next event should be for the child process's exec. */
1487 wpid = waitpid(fpid, &status, 0);
1488 ATF_REQUIRE(WIFSTOPPED(status));
1489 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1491 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1492 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1493 (PL_FLAG_EXEC | PL_FLAG_SCX));
1495 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1497 /* The last event should be for the child process's exit. */
1498 wpid = waitpid(fpid, &status, 0);
1499 ATF_REQUIRE(WIFEXITED(status));
1500 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1502 wpid = wait(&status);
1503 ATF_REQUIRE(wpid == -1);
1504 ATF_REQUIRE(errno == ECHILD);
1507 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1508 ATF_TC_BODY(ptrace__event_mask, tc)
1513 ATF_REQUIRE((fpid = fork()) != -1);
1519 /* The first wait() should report the stop from SIGSTOP. */
1520 wpid = waitpid(fpid, &status, 0);
1521 ATF_REQUIRE(wpid == fpid);
1522 ATF_REQUIRE(WIFSTOPPED(status));
1523 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1525 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1526 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1527 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1528 sizeof(events)) == 0);
1529 ATF_REQUIRE(events & PTRACE_FORK);
1530 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1531 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1532 sizeof(events)) == 0);
1533 ATF_REQUIRE(!(events & PTRACE_FORK));
1535 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1536 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, 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_LWP);
1540 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, 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_LWP));
1545 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1547 /* Should get one event at exit. */
1548 wpid = waitpid(fpid, &status, 0);
1549 ATF_REQUIRE(WIFEXITED(status));
1550 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1552 wpid = wait(&status);
1553 ATF_REQUIRE(wpid == -1);
1554 ATF_REQUIRE(errno == ECHILD);
1558 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1560 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1561 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1563 struct ptrace_lwpinfo pl;
1567 ATF_REQUIRE((fpid = fork()) != -1);
1570 follow_fork_parent(true);
1573 /* The first wait() should report the stop from SIGSTOP. */
1574 wpid = waitpid(fpid, &status, 0);
1575 ATF_REQUIRE(wpid == fpid);
1576 ATF_REQUIRE(WIFSTOPPED(status));
1577 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1579 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1580 sizeof(events)) == 0);
1581 events |= PTRACE_VFORK;
1582 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1583 sizeof(events)) == 0);
1585 /* Continue the child ignoring the SIGSTOP. */
1586 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1588 /* The next event should report the end of the vfork. */
1589 wpid = wait(&status);
1590 ATF_REQUIRE(wpid == fpid);
1591 ATF_REQUIRE(WIFSTOPPED(status));
1592 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1593 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1594 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1596 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1598 wpid = wait(&status);
1599 ATF_REQUIRE(wpid == fpid);
1600 ATF_REQUIRE(WIFEXITED(status));
1601 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1603 wpid = wait(&status);
1604 ATF_REQUIRE(wpid == -1);
1605 ATF_REQUIRE(errno == ECHILD);
1608 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1609 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1611 struct ptrace_lwpinfo pl[2];
1612 pid_t children[2], fpid, wpid;
1615 ATF_REQUIRE((fpid = fork()) != -1);
1618 follow_fork_parent(true);
1621 /* Parent process. */
1624 /* The first wait() should report the stop from SIGSTOP. */
1625 wpid = waitpid(children[0], &status, 0);
1626 ATF_REQUIRE(wpid == children[0]);
1627 ATF_REQUIRE(WIFSTOPPED(status));
1628 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1630 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1631 sizeof(events)) == 0);
1632 events |= PTRACE_FORK | PTRACE_VFORK;
1633 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1634 sizeof(events)) == 0);
1636 /* Continue the child ignoring the SIGSTOP. */
1637 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1639 /* Wait for both halves of the fork event to get reported. */
1640 children[1] = handle_fork_events(children[0], pl);
1641 ATF_REQUIRE(children[1] > 0);
1643 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1645 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1646 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1649 * The child can't exit until the grandchild reports status, so the
1650 * grandchild should report its exit first to the debugger.
1652 wpid = waitpid(children[1], &status, 0);
1653 ATF_REQUIRE(wpid == children[1]);
1654 ATF_REQUIRE(WIFEXITED(status));
1655 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1658 * The child should report it's vfork() completion before it
1661 wpid = wait(&status);
1662 ATF_REQUIRE(wpid == children[0]);
1663 ATF_REQUIRE(WIFSTOPPED(status));
1664 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1665 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1667 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1669 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1671 wpid = wait(&status);
1672 ATF_REQUIRE(wpid == children[0]);
1673 ATF_REQUIRE(WIFEXITED(status));
1674 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1676 wpid = wait(&status);
1677 ATF_REQUIRE(wpid == -1);
1678 ATF_REQUIRE(errno == ECHILD);
1682 * XXX: There's nothing inherently platform specific about this test, however a
1683 * userspace visible breakpoint() is a prerequisite.
1685 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1687 * Verify that no more events are reported after PT_KILL except for the
1688 * process exit when stopped due to a breakpoint trap.
1690 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1691 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1696 ATF_REQUIRE((fpid = fork()) != -1);
1703 /* The first wait() should report the stop from SIGSTOP. */
1704 wpid = waitpid(fpid, &status, 0);
1705 ATF_REQUIRE(wpid == fpid);
1706 ATF_REQUIRE(WIFSTOPPED(status));
1707 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1709 /* Continue the child ignoring the SIGSTOP. */
1710 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1712 /* The second wait() should report hitting the breakpoint. */
1713 wpid = waitpid(fpid, &status, 0);
1714 ATF_REQUIRE(wpid == fpid);
1715 ATF_REQUIRE(WIFSTOPPED(status));
1716 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1718 /* Kill the child process. */
1719 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1721 /* The last wait() should report the SIGKILL. */
1722 wpid = waitpid(fpid, &status, 0);
1723 ATF_REQUIRE(wpid == fpid);
1724 ATF_REQUIRE(WIFSIGNALED(status));
1725 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1727 wpid = wait(&status);
1728 ATF_REQUIRE(wpid == -1);
1729 ATF_REQUIRE(errno == ECHILD);
1731 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1734 * Verify that no more events are reported after PT_KILL except for the
1735 * process exit when stopped inside of a system call.
1737 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1738 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1740 struct ptrace_lwpinfo pl;
1744 ATF_REQUIRE((fpid = fork()) != -1);
1751 /* The first wait() should report the stop from SIGSTOP. */
1752 wpid = waitpid(fpid, &status, 0);
1753 ATF_REQUIRE(wpid == fpid);
1754 ATF_REQUIRE(WIFSTOPPED(status));
1755 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1757 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1758 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1760 /* The second wait() should report a system call entry for getpid(). */
1761 wpid = waitpid(fpid, &status, 0);
1762 ATF_REQUIRE(wpid == fpid);
1763 ATF_REQUIRE(WIFSTOPPED(status));
1764 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1766 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1767 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1769 /* Kill the child process. */
1770 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1772 /* The last wait() should report the SIGKILL. */
1773 wpid = waitpid(fpid, &status, 0);
1774 ATF_REQUIRE(wpid == fpid);
1775 ATF_REQUIRE(WIFSIGNALED(status));
1776 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1778 wpid = wait(&status);
1779 ATF_REQUIRE(wpid == -1);
1780 ATF_REQUIRE(errno == ECHILD);
1784 * Verify that no more events are reported after PT_KILL except for the
1785 * process exit when killing a multithreaded process.
1787 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1788 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1790 struct ptrace_lwpinfo pl;
1795 ATF_REQUIRE((fpid = fork()) != -1);
1798 simple_thread_main();
1801 /* The first wait() should report the stop from SIGSTOP. */
1802 wpid = waitpid(fpid, &status, 0);
1803 ATF_REQUIRE(wpid == fpid);
1804 ATF_REQUIRE(WIFSTOPPED(status));
1805 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1807 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1809 main_lwp = pl.pl_lwpid;
1811 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1813 /* Continue the child ignoring the SIGSTOP. */
1814 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1816 /* The first event should be for the child thread's birth. */
1817 wpid = waitpid(fpid, &status, 0);
1818 ATF_REQUIRE(wpid == fpid);
1819 ATF_REQUIRE(WIFSTOPPED(status));
1820 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1822 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1823 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1824 (PL_FLAG_BORN | PL_FLAG_SCX));
1825 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1827 /* Kill the child process. */
1828 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1830 /* The last wait() should report the SIGKILL. */
1831 wpid = waitpid(fpid, &status, 0);
1832 ATF_REQUIRE(wpid == fpid);
1833 ATF_REQUIRE(WIFSIGNALED(status));
1834 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1836 wpid = wait(&status);
1837 ATF_REQUIRE(wpid == -1);
1838 ATF_REQUIRE(errno == ECHILD);
1842 mask_usr1_thread(void *arg)
1844 pthread_barrier_t *pbarrier;
1847 pbarrier = (pthread_barrier_t*)arg;
1849 sigemptyset(&sigmask);
1850 sigaddset(&sigmask, SIGUSR1);
1851 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1853 /* Sync up with other thread after sigmask updated. */
1854 pthread_barrier_wait(pbarrier);
1863 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1864 * and prevents spurious stops due to those other signals.
1866 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1867 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1873 pthread_barrier_t barrier;
1875 ATF_REQUIRE((fpid = fork()) != -1);
1878 * Bind to one CPU so only one thread at a time will run. This
1879 * test expects that the first thread created (the main thread)
1880 * will be unsuspended first and will block the second thread
1884 CPU_SET(0, &setmask);
1886 CHILD_REQUIRE(cpuset(&setid) == 0);
1887 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1888 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1890 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1892 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1893 (void*)&barrier) == 0);
1896 sigemptyset(&sigmask);
1897 sigaddset(&sigmask, SIGUSR2);
1898 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1900 /* Sync up with other thread after sigmask updated. */
1901 pthread_barrier_wait(&barrier);
1911 /* The first wait() should report the stop from SIGSTOP. */
1912 wpid = waitpid(fpid, &status, 0);
1913 ATF_REQUIRE(wpid == fpid);
1914 ATF_REQUIRE(WIFSTOPPED(status));
1915 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1917 /* Continue the child ignoring the SIGSTOP. */
1918 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1920 /* Send a signal that only the second thread can handle. */
1921 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1923 /* The second wait() should report the SIGUSR2. */
1924 wpid = waitpid(fpid, &status, 0);
1925 ATF_REQUIRE(wpid == fpid);
1926 ATF_REQUIRE(WIFSTOPPED(status));
1927 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1929 /* Send a signal that only the first thread can handle. */
1930 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1932 /* Replace the SIGUSR2 with a kill. */
1933 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1935 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1936 wpid = waitpid(fpid, &status, 0);
1937 ATF_REQUIRE(wpid == fpid);
1938 ATF_REQUIRE(WIFSIGNALED(status));
1939 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1941 wpid = wait(&status);
1942 ATF_REQUIRE(wpid == -1);
1943 ATF_REQUIRE(errno == ECHILD);
1947 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1948 * and prevents spurious stops caused by those events.
1950 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1951 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1957 pthread_barrier_t barrier;
1959 struct ptrace_lwpinfo pl;
1961 ATF_REQUIRE((fpid = fork()) != -1);
1966 * Bind to one CPU so only one thread at a time will run. This
1967 * test expects that the first thread created (the main thread)
1968 * will be unsuspended first and will block the second thread
1972 CPU_SET(0, &setmask);
1974 CHILD_REQUIRE(cpuset(&setid) == 0);
1975 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1976 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1978 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1980 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1981 (void*)&barrier) == 0);
1984 sigemptyset(&sigmask);
1985 sigaddset(&sigmask, SIGUSR2);
1986 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1988 /* Sync up with other thread after sigmask updated. */
1989 pthread_barrier_wait(&barrier);
1991 /* Sync up with the test before doing the getpid(). */
1998 /* The first wait() should report the stop from SIGSTOP. */
1999 wpid = waitpid(fpid, &status, 0);
2000 ATF_REQUIRE(wpid == fpid);
2001 ATF_REQUIRE(WIFSTOPPED(status));
2002 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2004 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2005 main_lwp = pl.pl_lwpid;
2007 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2008 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2011 * Continue until child is done with setup, which is indicated with
2012 * SIGSTOP. Ignore system calls in the meantime.
2015 wpid = waitpid(fpid, &status, 0);
2016 ATF_REQUIRE(wpid == fpid);
2017 ATF_REQUIRE(WIFSTOPPED(status));
2018 if (WSTOPSIG(status) == SIGTRAP) {
2019 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2021 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2023 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2026 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2029 /* Let both threads hit their syscall entries. */
2030 for (i = 0; i < 2; ++i) {
2031 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2033 wpid = waitpid(fpid, &status, 0);
2034 ATF_REQUIRE(wpid == fpid);
2035 ATF_REQUIRE(WIFSTOPPED(status));
2036 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2038 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2040 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2043 * Prevent the main thread from hitting its syscall exit for
2046 if (pl.pl_lwpid == main_lwp)
2047 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2051 /* Send a signal that only the second thread can handle. */
2052 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2054 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2056 /* The second wait() should report the SIGUSR2. */
2057 wpid = waitpid(fpid, &status, 0);
2058 ATF_REQUIRE(wpid == fpid);
2059 ATF_REQUIRE(WIFSTOPPED(status));
2060 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2062 /* Allow the main thread to try to finish its system call. */
2063 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2066 * At this point, the main thread is in the middle of a system call and
2067 * has been resumed. The second thread has taken a signal which will be
2068 * replaced with a SIGKILL. We expect the main thread will get to run
2069 * first. It should notice the kill request and exit accordingly and
2070 * not stop for the system call exit event.
2073 /* Replace the SIGUSR2 with a kill. */
2074 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2076 /* The last wait() should report the SIGKILL (not a syscall exit). */
2077 wpid = waitpid(fpid, &status, 0);
2078 ATF_REQUIRE(wpid == fpid);
2079 ATF_REQUIRE(WIFSIGNALED(status));
2080 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2082 wpid = wait(&status);
2083 ATF_REQUIRE(wpid == -1);
2084 ATF_REQUIRE(errno == ECHILD);
2088 sigusr1_handler(int sig)
2091 CHILD_REQUIRE(sig == SIGUSR1);
2096 * Verify that even if the signal queue is full for a child process,
2097 * a PT_KILL will kill the process.
2099 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2100 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2104 int max_pending_per_proc;
2108 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2110 ATF_REQUIRE((fpid = fork()) != -1);
2116 /* The first wait() should report the stop from SIGSTOP. */
2117 wpid = waitpid(fpid, &status, 0);
2118 ATF_REQUIRE(wpid == fpid);
2119 ATF_REQUIRE(WIFSTOPPED(status));
2120 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2122 len = sizeof(max_pending_per_proc);
2123 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2124 &max_pending_per_proc, &len, NULL, 0) == 0);
2126 /* Fill the signal queue. */
2127 for (i = 0; i < max_pending_per_proc; ++i)
2128 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2130 /* Kill the child process. */
2131 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2133 /* The last wait() should report the SIGKILL. */
2134 wpid = waitpid(fpid, &status, 0);
2135 ATF_REQUIRE(wpid == fpid);
2136 ATF_REQUIRE(WIFSIGNALED(status));
2137 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2139 wpid = wait(&status);
2140 ATF_REQUIRE(wpid == -1);
2141 ATF_REQUIRE(errno == ECHILD);
2145 * Verify that when stopped at a system call entry, a signal can be
2146 * requested with PT_CONTINUE which will be delivered once the system
2149 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2150 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2152 struct ptrace_lwpinfo pl;
2156 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2158 ATF_REQUIRE((fpid = fork()) != -1);
2165 /* The first wait() should report the stop from SIGSTOP. */
2166 wpid = waitpid(fpid, &status, 0);
2167 ATF_REQUIRE(wpid == fpid);
2168 ATF_REQUIRE(WIFSTOPPED(status));
2169 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2171 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2172 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2174 /* The second wait() should report a system call entry for getpid(). */
2175 wpid = waitpid(fpid, &status, 0);
2176 ATF_REQUIRE(wpid == fpid);
2177 ATF_REQUIRE(WIFSTOPPED(status));
2178 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2180 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2181 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2183 /* Continue the child process with a signal. */
2184 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2188 * The last wait() should report exit 2, i.e., a normal _exit
2189 * from the signal handler. In the meantime, catch and proceed
2190 * past any syscall stops.
2192 wpid = waitpid(fpid, &status, 0);
2193 ATF_REQUIRE(wpid == fpid);
2194 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2195 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2196 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2197 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2199 ATF_REQUIRE(WIFEXITED(status));
2200 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2205 wpid = wait(&status);
2206 ATF_REQUIRE(wpid == -1);
2207 ATF_REQUIRE(errno == ECHILD);
2211 sigusr1_counting_handler(int sig)
2213 static int counter = 0;
2215 CHILD_REQUIRE(sig == SIGUSR1);
2222 * Verify that, when continuing from a stop at system call entry and exit,
2223 * a signal can be requested from both stops, and both will be delivered when
2224 * the system call is complete.
2226 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2227 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2229 struct ptrace_lwpinfo pl;
2233 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2235 ATF_REQUIRE((fpid = fork()) != -1);
2242 /* The first wait() should report the stop from SIGSTOP. */
2243 wpid = waitpid(fpid, &status, 0);
2244 ATF_REQUIRE(wpid == fpid);
2245 ATF_REQUIRE(WIFSTOPPED(status));
2246 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2248 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2249 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2251 /* The second wait() should report a system call entry for getpid(). */
2252 wpid = waitpid(fpid, &status, 0);
2253 ATF_REQUIRE(wpid == fpid);
2254 ATF_REQUIRE(WIFSTOPPED(status));
2255 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2257 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2258 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2260 /* Continue the child process with a signal. */
2261 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2263 /* The third wait() should report a system call exit for getpid(). */
2264 wpid = waitpid(fpid, &status, 0);
2265 ATF_REQUIRE(wpid == fpid);
2266 ATF_REQUIRE(WIFSTOPPED(status));
2267 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2269 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2270 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2272 /* Continue the child process with a signal. */
2273 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2277 * The last wait() should report exit 2, i.e., a normal _exit
2278 * from the signal handler. In the meantime, catch and proceed
2279 * past any syscall stops.
2281 wpid = waitpid(fpid, &status, 0);
2282 ATF_REQUIRE(wpid == fpid);
2283 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2284 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2285 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2286 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2288 ATF_REQUIRE(WIFEXITED(status));
2289 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2294 wpid = wait(&status);
2295 ATF_REQUIRE(wpid == -1);
2296 ATF_REQUIRE(errno == ECHILD);
2300 * Verify that even if the signal queue is full for a child process,
2301 * a PT_CONTINUE with a signal will not result in loss of that signal.
2303 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2304 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2308 int max_pending_per_proc;
2312 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2313 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2315 ATF_REQUIRE((fpid = fork()) != -1);
2321 /* The first wait() should report the stop from SIGSTOP. */
2322 wpid = waitpid(fpid, &status, 0);
2323 ATF_REQUIRE(wpid == fpid);
2324 ATF_REQUIRE(WIFSTOPPED(status));
2325 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2327 len = sizeof(max_pending_per_proc);
2328 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2329 &max_pending_per_proc, &len, NULL, 0) == 0);
2331 /* Fill the signal queue. */
2332 for (i = 0; i < max_pending_per_proc; ++i)
2333 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2335 /* Continue with signal. */
2336 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2339 wpid = waitpid(fpid, &status, 0);
2340 ATF_REQUIRE(wpid == fpid);
2341 if (WIFSTOPPED(status)) {
2342 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2343 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2346 * The last wait() should report normal _exit from the
2349 ATF_REQUIRE(WIFEXITED(status));
2350 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2355 wpid = wait(&status);
2356 ATF_REQUIRE(wpid == -1);
2357 ATF_REQUIRE(errno == ECHILD);
2361 * Verify that, after stopping due to a signal, that signal can be
2362 * replaced with another signal.
2364 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2365 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2367 struct ptrace_lwpinfo pl;
2371 ATF_REQUIRE((fpid = fork()) != -1);
2378 /* The first wait() should report the stop from SIGSTOP. */
2379 wpid = waitpid(fpid, &status, 0);
2380 ATF_REQUIRE(wpid == fpid);
2381 ATF_REQUIRE(WIFSTOPPED(status));
2382 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2384 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2386 /* Send a signal without ptrace. */
2387 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2389 /* The second wait() should report a SIGINT was received. */
2390 wpid = waitpid(fpid, &status, 0);
2391 ATF_REQUIRE(wpid == fpid);
2392 ATF_REQUIRE(WIFSTOPPED(status));
2393 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2395 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2396 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2397 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2399 /* Continue the child process with a different signal. */
2400 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2403 * The last wait() should report having died due to the new
2406 wpid = waitpid(fpid, &status, 0);
2407 ATF_REQUIRE(wpid == fpid);
2408 ATF_REQUIRE(WIFSIGNALED(status));
2409 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2411 wpid = wait(&status);
2412 ATF_REQUIRE(wpid == -1);
2413 ATF_REQUIRE(errno == ECHILD);
2417 * Verify that a signal can be passed through to the child even when there
2418 * was no true signal originally. Such cases arise when a SIGTRAP is
2419 * invented for e.g, system call stops.
2421 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2422 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2424 struct ptrace_lwpinfo pl;
2428 ATF_REQUIRE((fpid = fork()) != -1);
2435 /* The first wait() should report the stop from SIGSTOP. */
2436 wpid = waitpid(fpid, &status, 0);
2437 ATF_REQUIRE(wpid == fpid);
2438 ATF_REQUIRE(WIFSTOPPED(status));
2439 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2441 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2442 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2444 /* The second wait() should report a system call entry for getpid(). */
2445 wpid = waitpid(fpid, &status, 0);
2446 ATF_REQUIRE(wpid == fpid);
2447 ATF_REQUIRE(WIFSTOPPED(status));
2448 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2450 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2451 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2453 /* Continue the child process with a SIGTRAP. */
2454 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2458 * The last wait() should report exit due to SIGTRAP. In the
2459 * meantime, catch and proceed past any syscall stops.
2461 wpid = waitpid(fpid, &status, 0);
2462 ATF_REQUIRE(wpid == fpid);
2463 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2464 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2465 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2466 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2468 ATF_REQUIRE(WIFSIGNALED(status));
2469 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2474 wpid = wait(&status);
2475 ATF_REQUIRE(wpid == -1);
2476 ATF_REQUIRE(errno == ECHILD);
2481 * A mixed bag PT_CONTINUE with signal test.
2483 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2484 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2486 struct ptrace_lwpinfo pl;
2490 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2492 ATF_REQUIRE((fpid = fork()) != -1);
2499 /* The first wait() should report the stop from SIGSTOP. */
2500 wpid = waitpid(fpid, &status, 0);
2501 ATF_REQUIRE(wpid == fpid);
2502 ATF_REQUIRE(WIFSTOPPED(status));
2503 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2505 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2506 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2508 /* The second wait() should report a system call entry for getpid(). */
2509 wpid = waitpid(fpid, &status, 0);
2510 ATF_REQUIRE(wpid == fpid);
2511 ATF_REQUIRE(WIFSTOPPED(status));
2512 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2514 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2515 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2517 /* Continue with the first SIGUSR1. */
2518 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2520 /* The next wait() should report a system call exit for getpid(). */
2521 wpid = waitpid(fpid, &status, 0);
2522 ATF_REQUIRE(wpid == fpid);
2523 ATF_REQUIRE(WIFSTOPPED(status));
2524 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2526 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2527 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2529 /* Send an ABRT without ptrace. */
2530 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2532 /* Continue normally. */
2533 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2535 /* The next wait() should report the SIGABRT. */
2536 wpid = waitpid(fpid, &status, 0);
2537 ATF_REQUIRE(wpid == fpid);
2538 ATF_REQUIRE(WIFSTOPPED(status));
2539 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2541 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2542 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2543 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2545 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2546 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2550 * The last wait() should report exit 2, i.e., a normal _exit
2551 * from the signal handler. In the meantime, catch and proceed
2552 * past any syscall stops.
2554 wpid = waitpid(fpid, &status, 0);
2555 ATF_REQUIRE(wpid == fpid);
2556 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2557 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2558 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2559 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2561 ATF_REQUIRE(WIFEXITED(status));
2562 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2567 wpid = wait(&status);
2568 ATF_REQUIRE(wpid == -1);
2569 ATF_REQUIRE(errno == ECHILD);
2574 * Verify a signal delivered by ptrace is noticed by kevent(2).
2576 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2577 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2580 int status, kq, nevents;
2583 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2585 ATF_REQUIRE((fpid = fork()) != -1);
2587 CHILD_REQUIRE((kq = kqueue()) > 0);
2588 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2589 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2594 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2595 if (nevents == -1 && errno == EINTR)
2597 CHILD_REQUIRE(nevents > 0);
2598 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2599 CHILD_REQUIRE(kev.ident == SIGUSR1);
2606 /* The first wait() should report the stop from SIGSTOP. */
2607 wpid = waitpid(fpid, &status, 0);
2608 ATF_REQUIRE(wpid == fpid);
2609 ATF_REQUIRE(WIFSTOPPED(status));
2610 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2612 /* Continue with the SIGUSR1. */
2613 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2616 * The last wait() should report normal exit with code 1.
2618 wpid = waitpid(fpid, &status, 0);
2619 ATF_REQUIRE(wpid == fpid);
2620 ATF_REQUIRE(WIFEXITED(status));
2621 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2623 wpid = wait(&status);
2624 ATF_REQUIRE(wpid == -1);
2625 ATF_REQUIRE(errno == ECHILD);
2628 static sem_t sigusr1_sem;
2631 sigusr1_sempost_handler(int sig __unused)
2634 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2638 signal_thread(void *arg)
2643 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2645 /* Wait for this thread to receive a SIGUSR1. */
2647 err = sem_wait(&sigusr1_sem);
2648 CHILD_REQUIRE(err == 0 || errno == EINTR);
2649 } while (err != 0 && errno == EINTR);
2651 /* Free our companion thread from the barrier. */
2652 pthread_barrier_wait(pbarrier);
2655 * Swap ignore duties; the next SIGUSR1 should go to the
2658 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2659 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2660 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2662 /* Sync up threads after swapping signal masks. */
2663 pthread_barrier_wait(pbarrier);
2665 /* Wait until our companion has received its SIGUSR1. */
2666 pthread_barrier_wait(pbarrier);
2672 * Verify that if ptrace stops due to a signal but continues with
2673 * a different signal that the new signal is routed to a thread
2674 * that can accept it, and that that thread is awakened by the signal
2675 * in a timely manner.
2677 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2678 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2684 pthread_barrier_t barrier;
2686 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2687 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2688 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2690 ATF_REQUIRE((fpid = fork()) != -1);
2692 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2694 /* The other thread should receive the first SIGUSR1. */
2695 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2696 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2697 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2701 /* Wait until other thread has received its SIGUSR1. */
2702 pthread_barrier_wait(&barrier);
2705 * Swap ignore duties; the next SIGUSR1 should go to this
2708 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2710 /* Sync up threads after swapping signal masks. */
2711 pthread_barrier_wait(&barrier);
2714 * Sync up with test code; we're ready for the next SIGUSR1
2719 /* Wait for this thread to receive a SIGUSR1. */
2721 err = sem_wait(&sigusr1_sem);
2722 CHILD_REQUIRE(err == 0 || errno == EINTR);
2723 } while (err != 0 && errno == EINTR);
2725 /* Free the other thread from the barrier. */
2726 pthread_barrier_wait(&barrier);
2728 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2733 /* The first wait() should report the stop from SIGSTOP. */
2734 wpid = waitpid(fpid, &status, 0);
2735 ATF_REQUIRE(wpid == fpid);
2736 ATF_REQUIRE(WIFSTOPPED(status));
2737 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2739 /* Continue the child ignoring the SIGSTOP. */
2740 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2743 * Send a signal without ptrace that either thread will accept (USR2,
2746 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2748 /* The second wait() should report a SIGUSR2 was received. */
2749 wpid = waitpid(fpid, &status, 0);
2750 ATF_REQUIRE(wpid == fpid);
2751 ATF_REQUIRE(WIFSTOPPED(status));
2752 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2754 /* Continue the child, changing the signal to USR1. */
2755 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2757 /* The next wait() should report the stop from SIGSTOP. */
2758 wpid = waitpid(fpid, &status, 0);
2759 ATF_REQUIRE(wpid == fpid);
2760 ATF_REQUIRE(WIFSTOPPED(status));
2761 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2763 /* Continue the child ignoring the SIGSTOP. */
2764 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2766 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2768 /* The next wait() should report a SIGUSR2 was received. */
2769 wpid = waitpid(fpid, &status, 0);
2770 ATF_REQUIRE(wpid == fpid);
2771 ATF_REQUIRE(WIFSTOPPED(status));
2772 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2774 /* Continue the child, changing the signal to USR1. */
2775 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2777 /* The last wait() should report normal exit with code 1. */
2778 wpid = waitpid(fpid, &status, 0);
2779 ATF_REQUIRE(wpid == fpid);
2780 ATF_REQUIRE(WIFEXITED(status));
2781 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2783 wpid = wait(&status);
2784 ATF_REQUIRE(wpid == -1);
2785 ATF_REQUIRE(errno == ECHILD);
2791 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2792 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
2793 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
2794 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
2795 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
2796 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
2797 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
2798 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
2800 ptrace__follow_fork_child_detached_unrelated_debugger);
2802 ptrace__follow_fork_parent_detached_unrelated_debugger);
2803 ATF_TP_ADD_TC(tp, ptrace__getppid);
2804 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2805 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2806 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
2807 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
2808 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
2809 ATF_TP_ADD_TC(tp, ptrace__siginfo);
2810 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
2811 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
2812 ATF_TP_ADD_TC(tp, ptrace__event_mask);
2813 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2814 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2815 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
2816 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2818 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
2819 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
2820 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
2821 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
2822 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
2823 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
2825 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2826 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2827 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
2828 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2829 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
2830 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
2831 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2833 return (atf_no_error());