2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/cpuset.h>
31 #include <sys/event.h>
34 #include <sys/procctl.h>
35 #include <sys/procdesc.h>
36 #define _WANT_MIPS_REGNUM
37 #include <sys/ptrace.h>
38 #include <sys/queue.h>
40 #include <sys/syscall.h>
41 #include <sys/sysctl.h>
45 #include <machine/cpufunc.h>
48 #include <semaphore.h>
57 * Architectures with a user-visible breakpoint().
59 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
60 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
62 #define HAVE_BREAKPOINT
66 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
68 #ifdef HAVE_BREAKPOINT
69 #if defined(__aarch64__)
70 #define SKIP_BREAK(reg) ((reg)->elr += 4)
71 #elif defined(__amd64__) || defined(__i386__)
72 #define SKIP_BREAK(reg)
73 #elif defined(__arm__)
74 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
75 #elif defined(__mips__)
76 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
77 #elif defined(__riscv)
78 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
79 #elif defined(__sparc64__)
80 #define SKIP_BREAK(reg) do { \
81 (reg)->r_tpc = (reg)->r_tnpc + 4; \
88 * A variant of ATF_REQUIRE that is suitable for use in child
89 * processes. This only works if the parent process is tripped up by
90 * the early exit and fails some requirement itself.
92 #define CHILD_REQUIRE(exp) do { \
94 child_fail_require(__FILE__, __LINE__, \
98 #define CHILD_REQUIRE_EQ(actual, expected) do { \
99 __typeof__(expected) _e = expected; \
100 __typeof__(actual) _a = actual; \
102 child_fail_require(__FILE__, __LINE__, #actual \
103 " (%jd) == " #expected " (%jd) not met\n", \
104 (intmax_t)_a, (intmax_t)_e); \
108 child_fail_require(const char *file, int line, const char *fmt, ...)
113 /* Use write() not fprintf() to avoid possible duplicate output. */
114 snprintf(buf, sizeof(buf), "%s:%d: ", file, line);
115 write(STDERR_FILENO, buf, strlen(buf));
117 vsnprintf(buf, sizeof(buf), fmt, ap);
118 write(STDERR_FILENO, buf, strlen(buf));
124 #define REQUIRE_EQ(actual, expected) do { \
125 __typeof__(expected) _e = expected; \
126 __typeof__(actual) _a = actual; \
127 ATF_REQUIRE_MSG(_e == _a, #actual " (%jd) == " \
128 #expected " (%jd) not met", (intmax_t)_a, (intmax_t)_e); \
135 /* Attach the parent process as a tracer of this process. */
136 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
138 /* Trigger a stop. */
143 attach_child(pid_t pid)
148 REQUIRE_EQ(ptrace(PT_ATTACH, pid, NULL, 0), 0);
150 wpid = waitpid(pid, &status, 0);
151 REQUIRE_EQ(wpid, pid);
152 ATF_REQUIRE(WIFSTOPPED(status));
153 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
157 wait_for_zombie(pid_t pid)
161 * Wait for a process to exit. This is kind of gross, but
162 * there is not a better way.
164 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
165 * with ESRCH. After that change, a valid struct kinfo_proc
166 * is returned for zombies with ki_stat set to SZOMB.
169 struct kinfo_proc kp;
175 mib[2] = KERN_PROC_PID;
178 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
179 REQUIRE_EQ(errno, ESRCH);
182 if (kp.ki_stat == SZOMB)
189 * Verify that a parent debugger process "sees" the exit of a debugged
190 * process exactly once when attached via PT_TRACE_ME.
192 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
193 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
198 ATF_REQUIRE((child = fork()) != -1);
206 /* Parent process. */
208 /* The first wait() should report the stop from SIGSTOP. */
209 wpid = waitpid(child, &status, 0);
210 REQUIRE_EQ(wpid, child);
211 ATF_REQUIRE(WIFSTOPPED(status));
212 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
214 /* Continue the child ignoring the SIGSTOP. */
215 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
217 /* The second wait() should report the exit status. */
218 wpid = waitpid(child, &status, 0);
219 REQUIRE_EQ(wpid, child);
220 ATF_REQUIRE(WIFEXITED(status));
221 REQUIRE_EQ(WEXITSTATUS(status), 1);
223 /* The child should no longer exist. */
224 wpid = waitpid(child, &status, 0);
225 REQUIRE_EQ(wpid, -1);
226 REQUIRE_EQ(errno, ECHILD);
230 * Verify that a parent debugger process "sees" the exit of a debugged
231 * process exactly once when attached via PT_ATTACH.
233 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
234 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
237 int cpipe[2], status;
240 REQUIRE_EQ(pipe(cpipe), 0);
241 ATF_REQUIRE((child = fork()) != -1);
246 /* Wait for the parent to attach. */
247 CHILD_REQUIRE_EQ(0, read(cpipe[1], &c, sizeof(c)));
253 /* Parent process. */
255 /* Attach to the child process. */
258 /* Continue the child ignoring the SIGSTOP. */
259 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
261 /* Signal the child to exit. */
264 /* The second wait() should report the exit status. */
265 wpid = waitpid(child, &status, 0);
266 REQUIRE_EQ(wpid, child);
267 ATF_REQUIRE(WIFEXITED(status));
268 REQUIRE_EQ(WEXITSTATUS(status), 1);
270 /* The child should no longer exist. */
271 wpid = waitpid(child, &status, 0);
272 REQUIRE_EQ(wpid, -1);
273 REQUIRE_EQ(errno, ECHILD);
277 * Verify that a parent process "sees" the exit of a debugged process only
278 * after the debugger has seen it.
280 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
281 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
283 pid_t child, debugger, wpid;
284 int cpipe[2], dpipe[2], status;
287 REQUIRE_EQ(pipe(cpipe), 0);
288 ATF_REQUIRE((child = fork()) != -1);
294 /* Wait for parent to be ready. */
295 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
302 REQUIRE_EQ(pipe(dpipe), 0);
303 ATF_REQUIRE((debugger = fork()) != -1);
306 /* Debugger process. */
309 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
311 wpid = waitpid(child, &status, 0);
312 CHILD_REQUIRE_EQ(wpid, child);
313 CHILD_REQUIRE(WIFSTOPPED(status));
314 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
316 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
318 /* Signal parent that debugger is attached. */
319 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
322 /* Wait for parent's failed wait. */
323 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)), 0);
325 wpid = waitpid(child, &status, 0);
326 CHILD_REQUIRE_EQ(wpid, child);
327 CHILD_REQUIRE(WIFEXITED(status));
328 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
334 /* Parent process. */
336 /* Wait for the debugger to attach to the child. */
337 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
339 /* Release the child. */
340 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
341 REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
344 wait_for_zombie(child);
347 * This wait should return a pid of 0 to indicate no status to
348 * report. The parent should see the child as non-exited
349 * until the debugger sees the exit.
351 wpid = waitpid(child, &status, WNOHANG);
354 /* Signal the debugger to wait for the child. */
357 /* Wait for the debugger. */
358 wpid = waitpid(debugger, &status, 0);
359 REQUIRE_EQ(wpid, debugger);
360 ATF_REQUIRE(WIFEXITED(status));
361 REQUIRE_EQ(WEXITSTATUS(status), 0);
363 /* The child process should now be ready. */
364 wpid = waitpid(child, &status, WNOHANG);
365 REQUIRE_EQ(wpid, child);
366 ATF_REQUIRE(WIFEXITED(status));
367 REQUIRE_EQ(WEXITSTATUS(status), 1);
371 * Verify that a parent process "sees" the exit of a debugged process
372 * only after a non-direct-child debugger has seen it. In particular,
373 * various wait() calls in the parent must avoid failing with ESRCH by
374 * checking the parent's orphan list for the debugee.
376 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
377 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
379 pid_t child, debugger, fpid, wpid;
380 int cpipe[2], dpipe[2], status;
383 REQUIRE_EQ(pipe(cpipe), 0);
384 ATF_REQUIRE((child = fork()) != -1);
390 /* Wait for parent to be ready. */
391 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
398 REQUIRE_EQ(pipe(dpipe), 0);
399 ATF_REQUIRE((debugger = fork()) != -1);
402 /* Debugger parent. */
405 * Fork again and drop the debugger parent so that the
406 * debugger is not a child of the main parent.
408 CHILD_REQUIRE((fpid = fork()) != -1);
412 /* Debugger process. */
415 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
417 wpid = waitpid(child, &status, 0);
418 CHILD_REQUIRE_EQ(wpid, child);
419 CHILD_REQUIRE(WIFSTOPPED(status));
420 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
422 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
424 /* Signal parent that debugger is attached. */
425 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
428 /* Wait for parent's failed wait. */
429 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)),
432 wpid = waitpid(child, &status, 0);
433 CHILD_REQUIRE_EQ(wpid, child);
434 CHILD_REQUIRE(WIFEXITED(status));
435 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
441 /* Parent process. */
443 /* Wait for the debugger parent process to exit. */
444 wpid = waitpid(debugger, &status, 0);
445 REQUIRE_EQ(wpid, debugger);
446 ATF_REQUIRE(WIFEXITED(status));
447 REQUIRE_EQ(WEXITSTATUS(status), 2);
449 /* A WNOHANG wait here should see the non-exited child. */
450 wpid = waitpid(child, &status, WNOHANG);
453 /* Wait for the debugger to attach to the child. */
454 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
456 /* Release the child. */
457 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
458 REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
461 wait_for_zombie(child);
464 * This wait should return a pid of 0 to indicate no status to
465 * report. The parent should see the child as non-exited
466 * until the debugger sees the exit.
468 wpid = waitpid(child, &status, WNOHANG);
471 /* Signal the debugger to wait for the child. */
472 REQUIRE_EQ(write(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
474 /* Wait for the debugger. */
475 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), 0);
478 /* The child process should now be ready. */
479 wpid = waitpid(child, &status, WNOHANG);
480 REQUIRE_EQ(wpid, child);
481 ATF_REQUIRE(WIFEXITED(status));
482 REQUIRE_EQ(WEXITSTATUS(status), 1);
486 * Make sure that we can collect the exit status of an orphaned process.
488 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
489 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
492 int cpipe1[2], cpipe2[2], gcpipe[2], status;
495 REQUIRE_EQ(pipe(cpipe1), 0);
496 REQUIRE_EQ(pipe(cpipe2), 0);
497 REQUIRE_EQ(pipe(gcpipe), 0);
499 REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
501 ATF_REQUIRE((child = fork()) != -1);
503 CHILD_REQUIRE((gchild = fork()) != -1);
507 n = read(gcpipe[0], &status, sizeof(status));
508 } while (n == -1 && errno == EINTR);
512 CHILD_REQUIRE_EQ(write(cpipe1[1], &gchild, sizeof(gchild)),
513 (ssize_t)sizeof(gchild));
514 CHILD_REQUIRE_EQ(read(cpipe2[0], &status, sizeof(status)),
515 (ssize_t)sizeof(status));
519 REQUIRE_EQ(read(cpipe1[0], &gchild, sizeof(gchild)),
520 (ssize_t)sizeof(gchild));
522 REQUIRE_EQ(ptrace(PT_ATTACH, gchild, NULL, 0), 0);
525 REQUIRE_EQ(write(cpipe2[1], &status, sizeof(status)),
526 (ssize_t)sizeof(status));
527 REQUIRE_EQ(waitpid(child, &status, 0), child);
528 ATF_REQUIRE(WIFEXITED(status));
529 REQUIRE_EQ(WEXITSTATUS(status), 0);
532 REQUIRE_EQ(write(gcpipe[1], &status, sizeof(status)),
533 (ssize_t)sizeof(status));
534 REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
535 ATF_REQUIRE(WIFSTOPPED(status));
536 REQUIRE_EQ(ptrace(PT_DETACH, gchild, (caddr_t)1, 0), 0);
537 REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
538 ATF_REQUIRE(WIFEXITED(status));
539 REQUIRE_EQ(WEXITSTATUS(status), 0);
541 REQUIRE_EQ(close(cpipe1[0]), 0);
542 REQUIRE_EQ(close(cpipe1[1]), 0);
543 REQUIRE_EQ(close(cpipe2[0]), 0);
544 REQUIRE_EQ(close(cpipe2[1]), 0);
545 REQUIRE_EQ(close(gcpipe[0]), 0);
546 REQUIRE_EQ(close(gcpipe[1]), 0);
550 * The parent process should always act the same regardless of how the
551 * debugger is attached to it.
554 follow_fork_parent(bool use_vfork)
560 CHILD_REQUIRE((fpid = vfork()) != -1);
562 CHILD_REQUIRE((fpid = fork()) != -1);
568 wpid = waitpid(fpid, &status, 0);
569 CHILD_REQUIRE_EQ(wpid, fpid);
570 CHILD_REQUIRE(WIFEXITED(status));
571 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 2);
577 * Helper routine for follow fork tests. This waits for two stops
578 * that report both "sides" of a fork. It returns the pid of the new
582 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
584 struct ptrace_lwpinfo pl;
585 bool fork_reported[2];
589 fork_reported[0] = false;
590 fork_reported[1] = false;
594 * Each process should report a fork event. The parent should
595 * report a PL_FLAG_FORKED event, and the child should report
596 * a PL_FLAG_CHILD event.
598 for (i = 0; i < 2; i++) {
599 wpid = wait(&status);
600 ATF_REQUIRE(wpid > 0);
601 ATF_REQUIRE(WIFSTOPPED(status));
603 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
605 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
607 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
608 (PL_FLAG_FORKED | PL_FLAG_CHILD));
609 if (pl.pl_flags & PL_FLAG_CHILD) {
610 ATF_REQUIRE(wpid != parent);
611 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
612 ATF_REQUIRE(!fork_reported[1]);
616 REQUIRE_EQ(child, wpid);
619 fork_reported[1] = true;
621 REQUIRE_EQ(wpid, parent);
622 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
623 ATF_REQUIRE(!fork_reported[0]);
625 child = pl.pl_child_pid;
627 REQUIRE_EQ(child, pl.pl_child_pid);
630 fork_reported[0] = true;
638 * Verify that a new child process is stopped after a followed fork and
639 * that the traced parent sees the exit of the child after the debugger
640 * when both processes remain attached to the debugger.
642 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
643 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
645 pid_t children[2], fpid, wpid;
648 ATF_REQUIRE((fpid = fork()) != -1);
651 follow_fork_parent(false);
654 /* Parent process. */
657 /* The first wait() should report the stop from SIGSTOP. */
658 wpid = waitpid(children[0], &status, 0);
659 REQUIRE_EQ(wpid, children[0]);
660 ATF_REQUIRE(WIFSTOPPED(status));
661 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
663 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
665 /* Continue the child ignoring the SIGSTOP. */
666 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
668 children[1] = handle_fork_events(children[0], NULL);
669 ATF_REQUIRE(children[1] > 0);
671 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
672 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
675 * The child can't exit until the grandchild reports status, so the
676 * grandchild should report its exit first to the debugger.
678 wpid = wait(&status);
679 REQUIRE_EQ(wpid, children[1]);
680 ATF_REQUIRE(WIFEXITED(status));
681 REQUIRE_EQ(WEXITSTATUS(status), 2);
683 wpid = wait(&status);
684 REQUIRE_EQ(wpid, children[0]);
685 ATF_REQUIRE(WIFEXITED(status));
686 REQUIRE_EQ(WEXITSTATUS(status), 1);
688 wpid = wait(&status);
689 REQUIRE_EQ(wpid, -1);
690 REQUIRE_EQ(errno, ECHILD);
694 * Verify that a new child process is stopped after a followed fork
695 * and that the traced parent sees the exit of the child when the new
696 * child process is detached after it reports its fork.
698 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
699 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
701 pid_t children[2], fpid, wpid;
704 ATF_REQUIRE((fpid = fork()) != -1);
707 follow_fork_parent(false);
710 /* Parent process. */
713 /* The first wait() should report the stop from SIGSTOP. */
714 wpid = waitpid(children[0], &status, 0);
715 REQUIRE_EQ(wpid, children[0]);
716 ATF_REQUIRE(WIFSTOPPED(status));
717 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
719 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
721 /* Continue the child ignoring the SIGSTOP. */
722 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
724 children[1] = handle_fork_events(children[0], NULL);
725 ATF_REQUIRE(children[1] > 0);
727 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
728 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
731 * Should not see any status from the grandchild now, only the
734 wpid = wait(&status);
735 REQUIRE_EQ(wpid, children[0]);
736 ATF_REQUIRE(WIFEXITED(status));
737 REQUIRE_EQ(WEXITSTATUS(status), 1);
739 wpid = wait(&status);
740 REQUIRE_EQ(wpid, -1);
741 REQUIRE_EQ(errno, ECHILD);
745 * Verify that a new child process is stopped after a followed fork
746 * and that the traced parent sees the exit of the child when the
747 * traced parent is detached after the fork.
749 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
750 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
752 pid_t children[2], fpid, wpid;
755 ATF_REQUIRE((fpid = fork()) != -1);
758 follow_fork_parent(false);
761 /* Parent process. */
764 /* The first wait() should report the stop from SIGSTOP. */
765 wpid = waitpid(children[0], &status, 0);
766 REQUIRE_EQ(wpid, children[0]);
767 ATF_REQUIRE(WIFSTOPPED(status));
768 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
770 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
772 /* Continue the child ignoring the SIGSTOP. */
773 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
775 children[1] = handle_fork_events(children[0], NULL);
776 ATF_REQUIRE(children[1] > 0);
778 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
779 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
782 * The child can't exit until the grandchild reports status, so the
783 * grandchild should report its exit first to the debugger.
785 * Even though the child process is detached, it is still a
786 * child of the debugger, so it will still report it's exit
787 * after the grandchild.
789 wpid = wait(&status);
790 REQUIRE_EQ(wpid, children[1]);
791 ATF_REQUIRE(WIFEXITED(status));
792 REQUIRE_EQ(WEXITSTATUS(status), 2);
794 wpid = wait(&status);
795 REQUIRE_EQ(wpid, children[0]);
796 ATF_REQUIRE(WIFEXITED(status));
797 REQUIRE_EQ(WEXITSTATUS(status), 1);
799 wpid = wait(&status);
800 REQUIRE_EQ(wpid, -1);
801 REQUIRE_EQ(errno, ECHILD);
805 attach_fork_parent(int cpipe[2])
811 /* Double-fork to disassociate from the debugger. */
812 CHILD_REQUIRE((fpid = fork()) != -1);
816 /* Send the pid of the disassociated child to the debugger. */
818 CHILD_REQUIRE_EQ(write(cpipe[1], &fpid, sizeof(fpid)),
819 (ssize_t)sizeof(fpid));
821 /* Wait for the debugger to attach. */
822 CHILD_REQUIRE_EQ(read(cpipe[1], &fpid, sizeof(fpid)), 0);
826 * Verify that a new child process is stopped after a followed fork and
827 * that the traced parent sees the exit of the child after the debugger
828 * when both processes remain attached to the debugger. In this test
829 * the parent that forks is not a direct child of the debugger.
831 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
832 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
834 pid_t children[2], fpid, wpid;
835 int cpipe[2], status;
837 REQUIRE_EQ(pipe(cpipe), 0);
838 ATF_REQUIRE((fpid = fork()) != -1);
840 attach_fork_parent(cpipe);
841 follow_fork_parent(false);
844 /* Parent process. */
847 /* Wait for the direct child to exit. */
848 wpid = waitpid(fpid, &status, 0);
849 REQUIRE_EQ(wpid, fpid);
850 ATF_REQUIRE(WIFEXITED(status));
851 REQUIRE_EQ(WEXITSTATUS(status), 3);
853 /* Read the pid of the fork parent. */
854 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
855 (ssize_t)sizeof(children[0]));
857 /* Attach to the fork parent. */
858 attach_child(children[0]);
860 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
862 /* Continue the fork parent ignoring the SIGSTOP. */
863 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
865 /* Signal the fork parent to continue. */
868 children[1] = handle_fork_events(children[0], NULL);
869 ATF_REQUIRE(children[1] > 0);
871 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
872 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
875 * The fork parent can't exit until the child reports status,
876 * so the child should report its exit first to the debugger.
878 wpid = wait(&status);
879 REQUIRE_EQ(wpid, children[1]);
880 ATF_REQUIRE(WIFEXITED(status));
881 REQUIRE_EQ(WEXITSTATUS(status), 2);
883 wpid = wait(&status);
884 REQUIRE_EQ(wpid, children[0]);
885 ATF_REQUIRE(WIFEXITED(status));
886 REQUIRE_EQ(WEXITSTATUS(status), 1);
888 wpid = wait(&status);
889 REQUIRE_EQ(wpid, -1);
890 REQUIRE_EQ(errno, ECHILD);
894 * Verify that a new child process is stopped after a followed fork
895 * and that the traced parent sees the exit of the child when the new
896 * child process is detached after it reports its fork. In this test
897 * the parent that forks is not a direct child of the debugger.
899 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
900 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
902 pid_t children[2], fpid, wpid;
903 int cpipe[2], status;
905 REQUIRE_EQ(pipe(cpipe), 0);
906 ATF_REQUIRE((fpid = fork()) != -1);
908 attach_fork_parent(cpipe);
909 follow_fork_parent(false);
912 /* Parent process. */
915 /* Wait for the direct child to exit. */
916 wpid = waitpid(fpid, &status, 0);
917 REQUIRE_EQ(wpid, fpid);
918 ATF_REQUIRE(WIFEXITED(status));
919 REQUIRE_EQ(WEXITSTATUS(status), 3);
921 /* Read the pid of the fork parent. */
922 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
923 (ssize_t)sizeof(children[0]));
925 /* Attach to the fork parent. */
926 attach_child(children[0]);
928 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
930 /* Continue the fork parent ignoring the SIGSTOP. */
931 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
933 /* Signal the fork parent to continue. */
936 children[1] = handle_fork_events(children[0], NULL);
937 ATF_REQUIRE(children[1] > 0);
939 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
940 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
943 * Should not see any status from the child now, only the fork
946 wpid = wait(&status);
947 REQUIRE_EQ(wpid, children[0]);
948 ATF_REQUIRE(WIFEXITED(status));
949 REQUIRE_EQ(WEXITSTATUS(status), 1);
951 wpid = wait(&status);
952 REQUIRE_EQ(wpid, -1);
953 REQUIRE_EQ(errno, ECHILD);
957 * Verify that a new child process is stopped after a followed fork
958 * and that the traced parent sees the exit of the child when the
959 * traced parent is detached after the fork. In this test the parent
960 * that forks is not a direct child of the debugger.
962 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
963 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
965 pid_t children[2], fpid, wpid;
966 int cpipe[2], status;
968 REQUIRE_EQ(pipe(cpipe), 0);
969 ATF_REQUIRE((fpid = fork()) != -1);
971 attach_fork_parent(cpipe);
972 follow_fork_parent(false);
975 /* Parent process. */
978 /* Wait for the direct child to exit. */
979 wpid = waitpid(fpid, &status, 0);
980 REQUIRE_EQ(wpid, fpid);
981 ATF_REQUIRE(WIFEXITED(status));
982 REQUIRE_EQ(WEXITSTATUS(status), 3);
984 /* Read the pid of the fork parent. */
985 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
986 (ssize_t)sizeof(children[0]));
988 /* Attach to the fork parent. */
989 attach_child(children[0]);
991 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
993 /* Continue the fork parent ignoring the SIGSTOP. */
994 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
996 /* Signal the fork parent to continue. */
999 children[1] = handle_fork_events(children[0], NULL);
1000 ATF_REQUIRE(children[1] > 0);
1002 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
1003 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1006 * Should not see any status from the fork parent now, only
1009 wpid = wait(&status);
1010 REQUIRE_EQ(wpid, children[1]);
1011 ATF_REQUIRE(WIFEXITED(status));
1012 REQUIRE_EQ(WEXITSTATUS(status), 2);
1014 wpid = wait(&status);
1015 REQUIRE_EQ(wpid, -1);
1016 REQUIRE_EQ(errno, ECHILD);
1020 * Verify that a child process does not see an unrelated debugger as its
1021 * parent but sees its original parent process.
1023 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1024 ATF_TC_BODY(ptrace__getppid, tc)
1026 pid_t child, debugger, ppid, wpid;
1027 int cpipe[2], dpipe[2], status;
1030 REQUIRE_EQ(pipe(cpipe), 0);
1031 ATF_REQUIRE((child = fork()) != -1);
1034 /* Child process. */
1037 /* Wait for parent to be ready. */
1038 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
1039 (ssize_t)sizeof(c));
1041 /* Report the parent PID to the parent. */
1043 CHILD_REQUIRE_EQ(write(cpipe[1], &ppid, sizeof(ppid)),
1044 (ssize_t)sizeof(ppid));
1050 REQUIRE_EQ(pipe(dpipe), 0);
1051 ATF_REQUIRE((debugger = fork()) != -1);
1053 if (debugger == 0) {
1054 /* Debugger process. */
1057 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1059 wpid = waitpid(child, &status, 0);
1060 CHILD_REQUIRE_EQ(wpid, child);
1061 CHILD_REQUIRE(WIFSTOPPED(status));
1062 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1064 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1066 /* Signal parent that debugger is attached. */
1067 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
1068 (ssize_t)sizeof(c));
1070 /* Wait for traced child to exit. */
1071 wpid = waitpid(child, &status, 0);
1072 CHILD_REQUIRE_EQ(wpid, child);
1073 CHILD_REQUIRE(WIFEXITED(status));
1074 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
1080 /* Parent process. */
1082 /* Wait for the debugger to attach to the child. */
1083 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
1085 /* Release the child. */
1086 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
1088 /* Read the parent PID from the child. */
1089 REQUIRE_EQ(read(cpipe[0], &ppid, sizeof(ppid)), (ssize_t)sizeof(ppid));
1092 REQUIRE_EQ(ppid, getpid());
1094 /* Wait for the debugger. */
1095 wpid = waitpid(debugger, &status, 0);
1096 REQUIRE_EQ(wpid, debugger);
1097 ATF_REQUIRE(WIFEXITED(status));
1098 REQUIRE_EQ(WEXITSTATUS(status), 0);
1100 /* The child process should now be ready. */
1101 wpid = waitpid(child, &status, WNOHANG);
1102 REQUIRE_EQ(wpid, child);
1103 ATF_REQUIRE(WIFEXITED(status));
1104 REQUIRE_EQ(WEXITSTATUS(status), 1);
1108 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1109 * child process created via fork() reports the correct value.
1111 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1112 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1114 struct ptrace_lwpinfo pl[2];
1115 pid_t children[2], fpid, wpid;
1118 ATF_REQUIRE((fpid = fork()) != -1);
1121 follow_fork_parent(false);
1124 /* Parent process. */
1127 /* The first wait() should report the stop from SIGSTOP. */
1128 wpid = waitpid(children[0], &status, 0);
1129 REQUIRE_EQ(wpid, children[0]);
1130 ATF_REQUIRE(WIFSTOPPED(status));
1131 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1133 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1135 /* Continue the child ignoring the SIGSTOP. */
1136 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1138 /* Wait for both halves of the fork event to get reported. */
1139 children[1] = handle_fork_events(children[0], pl);
1140 ATF_REQUIRE(children[1] > 0);
1142 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1143 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1144 REQUIRE_EQ(pl[0].pl_syscall_code, (unsigned)SYS_fork);
1145 REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1146 REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1148 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1149 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1152 * The child can't exit until the grandchild reports status, so the
1153 * grandchild should report its exit first to the debugger.
1155 wpid = wait(&status);
1156 REQUIRE_EQ(wpid, children[1]);
1157 ATF_REQUIRE(WIFEXITED(status));
1158 REQUIRE_EQ(WEXITSTATUS(status), 2);
1160 wpid = wait(&status);
1161 REQUIRE_EQ(wpid, children[0]);
1162 ATF_REQUIRE(WIFEXITED(status));
1163 REQUIRE_EQ(WEXITSTATUS(status), 1);
1165 wpid = wait(&status);
1166 REQUIRE_EQ(wpid, -1);
1167 REQUIRE_EQ(errno, ECHILD);
1171 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1172 * child process created via vfork() reports the correct value.
1174 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1175 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1177 struct ptrace_lwpinfo pl[2];
1178 pid_t children[2], fpid, wpid;
1181 ATF_REQUIRE((fpid = fork()) != -1);
1184 follow_fork_parent(true);
1187 /* Parent process. */
1190 /* The first wait() should report the stop from SIGSTOP. */
1191 wpid = waitpid(children[0], &status, 0);
1192 REQUIRE_EQ(wpid, children[0]);
1193 ATF_REQUIRE(WIFSTOPPED(status));
1194 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1196 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1198 /* Continue the child ignoring the SIGSTOP. */
1199 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1201 /* Wait for both halves of the fork event to get reported. */
1202 children[1] = handle_fork_events(children[0], pl);
1203 ATF_REQUIRE(children[1] > 0);
1205 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1206 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1207 REQUIRE_EQ(pl[0].pl_syscall_code, (unsigned)SYS_vfork);
1208 REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1209 REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1211 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1212 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1215 * The child can't exit until the grandchild reports status, so the
1216 * grandchild should report its exit first to the debugger.
1218 wpid = wait(&status);
1219 REQUIRE_EQ(wpid, children[1]);
1220 ATF_REQUIRE(WIFEXITED(status));
1221 REQUIRE_EQ(WEXITSTATUS(status), 2);
1223 wpid = wait(&status);
1224 REQUIRE_EQ(wpid, children[0]);
1225 ATF_REQUIRE(WIFEXITED(status));
1226 REQUIRE_EQ(WEXITSTATUS(status), 1);
1228 wpid = wait(&status);
1229 REQUIRE_EQ(wpid, -1);
1230 REQUIRE_EQ(errno, ECHILD);
1234 simple_thread(void *arg __unused)
1241 simple_thread_main(void)
1245 CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, simple_thread, NULL), 0);
1246 CHILD_REQUIRE_EQ(pthread_join(thread, NULL), 0);
1251 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1252 * thread reports the correct value.
1254 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1255 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1257 struct ptrace_lwpinfo pl;
1262 ATF_REQUIRE((fpid = fork()) != -1);
1265 simple_thread_main();
1268 /* The first wait() should report the stop from SIGSTOP. */
1269 wpid = waitpid(fpid, &status, 0);
1270 REQUIRE_EQ(wpid, fpid);
1271 ATF_REQUIRE(WIFSTOPPED(status));
1272 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1274 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1276 mainlwp = pl.pl_lwpid;
1279 * Continue the child ignoring the SIGSTOP and tracing all
1280 * system call exits.
1282 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1285 * Wait for the new thread to arrive. pthread_create() might
1286 * invoke any number of system calls. For now we just wait
1287 * for the new thread to arrive and make sure it reports a
1288 * valid system call code. If ptrace grows thread event
1289 * reporting then this test can be made more precise.
1292 wpid = waitpid(fpid, &status, 0);
1293 REQUIRE_EQ(wpid, fpid);
1294 ATF_REQUIRE(WIFSTOPPED(status));
1295 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1297 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1299 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1300 ATF_REQUIRE(pl.pl_syscall_code != 0);
1301 if (pl.pl_lwpid != mainlwp)
1302 /* New thread seen. */
1305 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1308 /* Wait for the child to exit. */
1309 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1311 wpid = waitpid(fpid, &status, 0);
1312 REQUIRE_EQ(wpid, fpid);
1313 if (WIFEXITED(status))
1316 ATF_REQUIRE(WIFSTOPPED(status));
1317 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1318 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1321 REQUIRE_EQ(WEXITSTATUS(status), 1);
1323 wpid = wait(&status);
1324 REQUIRE_EQ(wpid, -1);
1325 REQUIRE_EQ(errno, ECHILD);
1329 * Verify that the expected LWP events are reported for a child thread.
1331 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1332 ATF_TC_BODY(ptrace__lwp_events, tc)
1334 struct ptrace_lwpinfo pl;
1339 ATF_REQUIRE((fpid = fork()) != -1);
1342 simple_thread_main();
1345 /* The first wait() should report the stop from SIGSTOP. */
1346 wpid = waitpid(fpid, &status, 0);
1347 REQUIRE_EQ(wpid, fpid);
1348 ATF_REQUIRE(WIFSTOPPED(status));
1349 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1351 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1353 lwps[0] = pl.pl_lwpid;
1355 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1357 /* Continue the child ignoring the SIGSTOP. */
1358 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1360 /* The first event should be for the child thread's birth. */
1361 wpid = waitpid(fpid, &status, 0);
1362 REQUIRE_EQ(wpid, fpid);
1363 ATF_REQUIRE(WIFSTOPPED(status));
1364 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1366 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1367 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1368 (PL_FLAG_BORN | PL_FLAG_SCX));
1369 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1370 lwps[1] = pl.pl_lwpid;
1372 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1374 /* The next event should be for the child thread's death. */
1375 wpid = waitpid(fpid, &status, 0);
1376 REQUIRE_EQ(wpid, fpid);
1377 ATF_REQUIRE(WIFSTOPPED(status));
1378 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1380 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1381 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1382 (PL_FLAG_EXITED | PL_FLAG_SCE));
1383 REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1385 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1387 /* The last event should be for the child process's exit. */
1388 wpid = waitpid(fpid, &status, 0);
1389 ATF_REQUIRE(WIFEXITED(status));
1390 REQUIRE_EQ(WEXITSTATUS(status), 1);
1392 wpid = wait(&status);
1393 REQUIRE_EQ(wpid, -1);
1394 REQUIRE_EQ(errno, ECHILD);
1398 exec_thread(void *arg __unused)
1401 execl("/usr/bin/true", "true", NULL);
1406 exec_thread_main(void)
1410 CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, exec_thread, NULL), 0);
1417 * Verify that the expected LWP events are reported for a multithreaded
1418 * process that calls execve(2).
1420 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1421 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1423 struct ptrace_lwpinfo pl;
1428 ATF_REQUIRE((fpid = fork()) != -1);
1434 /* The first wait() should report the stop from SIGSTOP. */
1435 wpid = waitpid(fpid, &status, 0);
1436 REQUIRE_EQ(wpid, fpid);
1437 ATF_REQUIRE(WIFSTOPPED(status));
1438 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1440 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1442 lwps[0] = pl.pl_lwpid;
1444 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1446 /* Continue the child ignoring the SIGSTOP. */
1447 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1449 /* The first event should be for the child thread's birth. */
1450 wpid = waitpid(fpid, &status, 0);
1451 REQUIRE_EQ(wpid, fpid);
1452 ATF_REQUIRE(WIFSTOPPED(status));
1453 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1455 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1456 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1457 (PL_FLAG_BORN | PL_FLAG_SCX));
1458 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1459 lwps[1] = pl.pl_lwpid;
1461 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1464 * The next event should be for the main thread's death due to
1465 * single threading from execve().
1467 wpid = waitpid(fpid, &status, 0);
1468 REQUIRE_EQ(wpid, fpid);
1469 ATF_REQUIRE(WIFSTOPPED(status));
1470 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1472 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1473 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1475 REQUIRE_EQ(pl.pl_lwpid, lwps[0]);
1477 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1479 /* The next event should be for the child process's exec. */
1480 wpid = waitpid(fpid, &status, 0);
1481 ATF_REQUIRE(WIFSTOPPED(status));
1482 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1484 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1485 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1486 (PL_FLAG_EXEC | PL_FLAG_SCX));
1487 REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1489 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1491 /* The last event should be for the child process's exit. */
1492 wpid = waitpid(fpid, &status, 0);
1493 ATF_REQUIRE(WIFEXITED(status));
1494 REQUIRE_EQ(WEXITSTATUS(status), 0);
1496 wpid = wait(&status);
1497 REQUIRE_EQ(wpid, -1);
1498 REQUIRE_EQ(errno, ECHILD);
1502 handler(int sig __unused)
1510 signal(SIGINFO, handler);
1516 * Verify that the expected ptrace event is reported for a signal.
1518 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1519 ATF_TC_BODY(ptrace__siginfo, tc)
1521 struct ptrace_lwpinfo pl;
1525 ATF_REQUIRE((fpid = fork()) != -1);
1531 /* The first wait() should report the stop from SIGSTOP. */
1532 wpid = waitpid(fpid, &status, 0);
1533 REQUIRE_EQ(wpid, fpid);
1534 ATF_REQUIRE(WIFSTOPPED(status));
1535 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1537 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1539 /* The next event should be for the SIGINFO. */
1540 wpid = waitpid(fpid, &status, 0);
1541 ATF_REQUIRE(WIFSTOPPED(status));
1542 REQUIRE_EQ(WSTOPSIG(status), SIGINFO);
1544 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1545 REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
1546 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1547 REQUIRE_EQ(pl.pl_siginfo.si_code, SI_LWP);
1548 REQUIRE_EQ(pl.pl_siginfo.si_pid, wpid);
1550 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1552 /* The last event should be for the child process's exit. */
1553 wpid = waitpid(fpid, &status, 0);
1554 ATF_REQUIRE(WIFEXITED(status));
1555 REQUIRE_EQ(WEXITSTATUS(status), 0);
1557 wpid = wait(&status);
1558 REQUIRE_EQ(wpid, -1);
1559 REQUIRE_EQ(errno, ECHILD);
1563 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1565 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1566 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1571 ATF_REQUIRE((fpid = fork()) != -1);
1577 /* The first wait() should report the stop from SIGSTOP. */
1578 wpid = waitpid(fpid, &status, 0);
1579 REQUIRE_EQ(wpid, fpid);
1580 ATF_REQUIRE(WIFSTOPPED(status));
1581 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1584 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1585 sizeof(events)) == 0);
1587 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1589 /* Should get one event at exit. */
1590 wpid = waitpid(fpid, &status, 0);
1591 ATF_REQUIRE(WIFEXITED(status));
1592 REQUIRE_EQ(WEXITSTATUS(status), 0);
1594 wpid = wait(&status);
1595 REQUIRE_EQ(wpid, -1);
1596 REQUIRE_EQ(errno, ECHILD);
1599 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1600 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1602 struct ptrace_lwpinfo pl;
1606 ATF_REQUIRE((fpid = fork()) != -1);
1612 /* The first wait() should report the stop from SIGSTOP. */
1613 wpid = waitpid(fpid, &status, 0);
1614 REQUIRE_EQ(wpid, fpid);
1615 ATF_REQUIRE(WIFSTOPPED(status));
1616 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1618 events = PTRACE_EXEC;
1619 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1620 sizeof(events)) == 0);
1622 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1624 /* The next event should be for the child process's exec. */
1625 wpid = waitpid(fpid, &status, 0);
1626 ATF_REQUIRE(WIFSTOPPED(status));
1627 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1629 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1630 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1631 (PL_FLAG_EXEC | PL_FLAG_SCX));
1633 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1635 /* The last event should be for the child process's exit. */
1636 wpid = waitpid(fpid, &status, 0);
1637 ATF_REQUIRE(WIFEXITED(status));
1638 REQUIRE_EQ(WEXITSTATUS(status), 0);
1640 wpid = wait(&status);
1641 REQUIRE_EQ(wpid, -1);
1642 REQUIRE_EQ(errno, ECHILD);
1645 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1646 ATF_TC_BODY(ptrace__event_mask, tc)
1651 ATF_REQUIRE((fpid = fork()) != -1);
1657 /* The first wait() should report the stop from SIGSTOP. */
1658 wpid = waitpid(fpid, &status, 0);
1659 REQUIRE_EQ(wpid, fpid);
1660 ATF_REQUIRE(WIFSTOPPED(status));
1661 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1663 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1664 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1665 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1666 sizeof(events)) == 0);
1667 ATF_REQUIRE(events & PTRACE_FORK);
1668 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1669 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1670 sizeof(events)) == 0);
1671 ATF_REQUIRE(!(events & PTRACE_FORK));
1673 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1674 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1675 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1676 sizeof(events)) == 0);
1677 ATF_REQUIRE(events & PTRACE_LWP);
1678 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1679 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1680 sizeof(events)) == 0);
1681 ATF_REQUIRE(!(events & PTRACE_LWP));
1683 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1685 /* Should get one event at exit. */
1686 wpid = waitpid(fpid, &status, 0);
1687 ATF_REQUIRE(WIFEXITED(status));
1688 REQUIRE_EQ(WEXITSTATUS(status), 0);
1690 wpid = wait(&status);
1691 REQUIRE_EQ(wpid, -1);
1692 REQUIRE_EQ(errno, ECHILD);
1696 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1698 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1699 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1701 struct ptrace_lwpinfo pl;
1705 ATF_REQUIRE((fpid = fork()) != -1);
1708 follow_fork_parent(true);
1711 /* The first wait() should report the stop from SIGSTOP. */
1712 wpid = waitpid(fpid, &status, 0);
1713 REQUIRE_EQ(wpid, fpid);
1714 ATF_REQUIRE(WIFSTOPPED(status));
1715 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1717 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1718 sizeof(events)) == 0);
1719 events |= PTRACE_VFORK;
1720 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1721 sizeof(events)) == 0);
1723 /* Continue the child ignoring the SIGSTOP. */
1724 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1726 /* The next event should report the end of the vfork. */
1727 wpid = wait(&status);
1728 REQUIRE_EQ(wpid, fpid);
1729 ATF_REQUIRE(WIFSTOPPED(status));
1730 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1731 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1732 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1734 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1736 wpid = wait(&status);
1737 REQUIRE_EQ(wpid, fpid);
1738 ATF_REQUIRE(WIFEXITED(status));
1739 REQUIRE_EQ(WEXITSTATUS(status), 1);
1741 wpid = wait(&status);
1742 REQUIRE_EQ(wpid, -1);
1743 REQUIRE_EQ(errno, ECHILD);
1746 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1747 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1749 struct ptrace_lwpinfo pl[2];
1750 pid_t children[2], fpid, wpid;
1753 ATF_REQUIRE((fpid = fork()) != -1);
1756 follow_fork_parent(true);
1759 /* Parent process. */
1762 /* The first wait() should report the stop from SIGSTOP. */
1763 wpid = waitpid(children[0], &status, 0);
1764 REQUIRE_EQ(wpid, children[0]);
1765 ATF_REQUIRE(WIFSTOPPED(status));
1766 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1768 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1769 sizeof(events)) == 0);
1770 events |= PTRACE_FORK | PTRACE_VFORK;
1771 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1772 sizeof(events)) == 0);
1774 /* Continue the child ignoring the SIGSTOP. */
1775 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1777 /* Wait for both halves of the fork event to get reported. */
1778 children[1] = handle_fork_events(children[0], pl);
1779 ATF_REQUIRE(children[1] > 0);
1781 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1783 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1784 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1787 * The child can't exit until the grandchild reports status, so the
1788 * grandchild should report its exit first to the debugger.
1790 wpid = waitpid(children[1], &status, 0);
1791 REQUIRE_EQ(wpid, children[1]);
1792 ATF_REQUIRE(WIFEXITED(status));
1793 REQUIRE_EQ(WEXITSTATUS(status), 2);
1796 * The child should report it's vfork() completion before it
1799 wpid = wait(&status);
1800 REQUIRE_EQ(wpid, children[0]);
1801 ATF_REQUIRE(WIFSTOPPED(status));
1802 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1803 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1805 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1807 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1809 wpid = wait(&status);
1810 REQUIRE_EQ(wpid, children[0]);
1811 ATF_REQUIRE(WIFEXITED(status));
1812 REQUIRE_EQ(WEXITSTATUS(status), 1);
1814 wpid = wait(&status);
1815 REQUIRE_EQ(wpid, -1);
1816 REQUIRE_EQ(errno, ECHILD);
1819 #ifdef HAVE_BREAKPOINT
1821 * Verify that no more events are reported after PT_KILL except for the
1822 * process exit when stopped due to a breakpoint trap.
1824 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1825 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1830 ATF_REQUIRE((fpid = fork()) != -1);
1837 /* The first wait() should report the stop from SIGSTOP. */
1838 wpid = waitpid(fpid, &status, 0);
1839 REQUIRE_EQ(wpid, fpid);
1840 ATF_REQUIRE(WIFSTOPPED(status));
1841 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1843 /* Continue the child ignoring the SIGSTOP. */
1844 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1846 /* The second wait() should report hitting the breakpoint. */
1847 wpid = waitpid(fpid, &status, 0);
1848 REQUIRE_EQ(wpid, fpid);
1849 ATF_REQUIRE(WIFSTOPPED(status));
1850 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1852 /* Kill the child process. */
1853 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1855 /* The last wait() should report the SIGKILL. */
1856 wpid = waitpid(fpid, &status, 0);
1857 REQUIRE_EQ(wpid, fpid);
1858 ATF_REQUIRE(WIFSIGNALED(status));
1859 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1861 wpid = wait(&status);
1862 REQUIRE_EQ(wpid, -1);
1863 REQUIRE_EQ(errno, ECHILD);
1865 #endif /* HAVE_BREAKPOINT */
1868 * Verify that no more events are reported after PT_KILL except for the
1869 * process exit when stopped inside of a system call.
1871 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1872 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1874 struct ptrace_lwpinfo pl;
1878 ATF_REQUIRE((fpid = fork()) != -1);
1885 /* The first wait() should report the stop from SIGSTOP. */
1886 wpid = waitpid(fpid, &status, 0);
1887 REQUIRE_EQ(wpid, fpid);
1888 ATF_REQUIRE(WIFSTOPPED(status));
1889 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1891 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1892 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
1894 /* The second wait() should report a system call entry for getpid(). */
1895 wpid = waitpid(fpid, &status, 0);
1896 REQUIRE_EQ(wpid, fpid);
1897 ATF_REQUIRE(WIFSTOPPED(status));
1898 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1900 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1901 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1903 /* Kill the child process. */
1904 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1906 /* The last wait() should report the SIGKILL. */
1907 wpid = waitpid(fpid, &status, 0);
1908 REQUIRE_EQ(wpid, fpid);
1909 ATF_REQUIRE(WIFSIGNALED(status));
1910 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1912 wpid = wait(&status);
1913 REQUIRE_EQ(wpid, -1);
1914 REQUIRE_EQ(errno, ECHILD);
1918 * Verify that no more events are reported after PT_KILL except for the
1919 * process exit when killing a multithreaded process.
1921 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1922 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1924 struct ptrace_lwpinfo pl;
1929 ATF_REQUIRE((fpid = fork()) != -1);
1932 simple_thread_main();
1935 /* The first wait() should report the stop from SIGSTOP. */
1936 wpid = waitpid(fpid, &status, 0);
1937 REQUIRE_EQ(wpid, fpid);
1938 ATF_REQUIRE(WIFSTOPPED(status));
1939 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1941 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1943 main_lwp = pl.pl_lwpid;
1945 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1947 /* Continue the child ignoring the SIGSTOP. */
1948 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1950 /* The first event should be for the child thread's birth. */
1951 wpid = waitpid(fpid, &status, 0);
1952 REQUIRE_EQ(wpid, fpid);
1953 ATF_REQUIRE(WIFSTOPPED(status));
1954 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1956 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1957 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1958 (PL_FLAG_BORN | PL_FLAG_SCX));
1959 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1961 /* Kill the child process. */
1962 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1964 /* The last wait() should report the SIGKILL. */
1965 wpid = waitpid(fpid, &status, 0);
1966 REQUIRE_EQ(wpid, fpid);
1967 ATF_REQUIRE(WIFSIGNALED(status));
1968 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1970 wpid = wait(&status);
1971 REQUIRE_EQ(wpid, -1);
1972 REQUIRE_EQ(errno, ECHILD);
1976 mask_usr1_thread(void *arg)
1978 pthread_barrier_t *pbarrier;
1981 pbarrier = (pthread_barrier_t*)arg;
1983 sigemptyset(&sigmask);
1984 sigaddset(&sigmask, SIGUSR1);
1985 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
1987 /* Sync up with other thread after sigmask updated. */
1988 pthread_barrier_wait(pbarrier);
1997 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1998 * and prevents spurious stops due to those other signals.
2000 ATF_TC(ptrace__PT_KILL_competing_signal);
2001 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
2004 atf_tc_set_md_var(tc, "require.user", "root");
2006 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
2012 pthread_barrier_t barrier;
2013 struct sched_param sched_param;
2015 ATF_REQUIRE((fpid = fork()) != -1);
2017 /* Bind to one CPU so only one thread at a time will run. */
2019 CPU_SET(0, &setmask);
2021 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2022 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2023 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2025 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2027 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2028 (void*)&barrier) == 0);
2031 * Give the main thread higher priority. The test always
2032 * assumes that, if both threads are able to run, the main
2033 * thread runs first.
2035 sched_param.sched_priority =
2036 (sched_get_priority_max(SCHED_FIFO) +
2037 sched_get_priority_min(SCHED_FIFO)) / 2;
2038 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2039 SCHED_FIFO, &sched_param) == 0);
2040 sched_param.sched_priority -= RQ_PPQ;
2041 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2042 &sched_param) == 0);
2045 sigemptyset(&sigmask);
2046 sigaddset(&sigmask, SIGUSR2);
2047 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2049 /* Sync up with other thread after sigmask updated. */
2050 pthread_barrier_wait(&barrier);
2060 /* The first wait() should report the stop from SIGSTOP. */
2061 wpid = waitpid(fpid, &status, 0);
2062 REQUIRE_EQ(wpid, fpid);
2063 ATF_REQUIRE(WIFSTOPPED(status));
2064 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2066 /* Continue the child ignoring the SIGSTOP. */
2067 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2069 /* Send a signal that only the second thread can handle. */
2070 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2072 /* The second wait() should report the SIGUSR2. */
2073 wpid = waitpid(fpid, &status, 0);
2074 REQUIRE_EQ(wpid, fpid);
2075 ATF_REQUIRE(WIFSTOPPED(status));
2076 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2078 /* Send a signal that only the first thread can handle. */
2079 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2081 /* Replace the SIGUSR2 with a kill. */
2082 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2084 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2085 wpid = waitpid(fpid, &status, 0);
2086 REQUIRE_EQ(wpid, fpid);
2087 ATF_REQUIRE(WIFSIGNALED(status));
2088 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2090 wpid = wait(&status);
2091 REQUIRE_EQ(wpid, -1);
2092 REQUIRE_EQ(errno, ECHILD);
2096 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2097 * and prevents spurious stops caused by those events.
2099 ATF_TC(ptrace__PT_KILL_competing_stop);
2100 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2103 atf_tc_set_md_var(tc, "require.user", "root");
2105 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2111 pthread_barrier_t barrier;
2113 struct ptrace_lwpinfo pl;
2114 struct sched_param sched_param;
2116 ATF_REQUIRE((fpid = fork()) != -1);
2120 /* Bind to one CPU so only one thread at a time will run. */
2122 CPU_SET(0, &setmask);
2124 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2125 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2126 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2128 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2130 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2131 (void*)&barrier) == 0);
2134 * Give the main thread higher priority. The test always
2135 * assumes that, if both threads are able to run, the main
2136 * thread runs first.
2138 sched_param.sched_priority =
2139 (sched_get_priority_max(SCHED_FIFO) +
2140 sched_get_priority_min(SCHED_FIFO)) / 2;
2141 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2142 SCHED_FIFO, &sched_param) == 0);
2143 sched_param.sched_priority -= RQ_PPQ;
2144 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2145 &sched_param) == 0);
2148 sigemptyset(&sigmask);
2149 sigaddset(&sigmask, SIGUSR2);
2150 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2152 /* Sync up with other thread after sigmask updated. */
2153 pthread_barrier_wait(&barrier);
2155 /* Sync up with the test before doing the getpid(). */
2162 /* The first wait() should report the stop from SIGSTOP. */
2163 wpid = waitpid(fpid, &status, 0);
2164 REQUIRE_EQ(wpid, fpid);
2165 ATF_REQUIRE(WIFSTOPPED(status));
2166 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2168 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2169 main_lwp = pl.pl_lwpid;
2171 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2172 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2175 * Continue until child is done with setup, which is indicated with
2176 * SIGSTOP. Ignore system calls in the meantime.
2179 wpid = waitpid(fpid, &status, 0);
2180 REQUIRE_EQ(wpid, fpid);
2181 ATF_REQUIRE(WIFSTOPPED(status));
2182 if (WSTOPSIG(status) == SIGTRAP) {
2183 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2185 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2187 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2190 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2193 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2194 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2196 wpid = waitpid(fpid, &status, 0);
2197 REQUIRE_EQ(wpid, fpid);
2198 ATF_REQUIRE(WIFSTOPPED(status));
2199 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2201 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2203 REQUIRE_EQ(pl.pl_lwpid, main_lwp);
2204 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2205 /* Prevent the main thread from hitting its syscall exit for now. */
2206 REQUIRE_EQ(ptrace(PT_SUSPEND, main_lwp, 0, 0), 0);
2209 * Proceed, allowing second thread to hit syscall exit for
2210 * pthread_barrier_wait().
2212 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2214 wpid = waitpid(fpid, &status, 0);
2215 REQUIRE_EQ(wpid, fpid);
2216 ATF_REQUIRE(WIFSTOPPED(status));
2217 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2219 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2221 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2222 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2224 /* Send a signal that only the second thread can handle. */
2225 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2227 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2229 /* The next wait() should report the SIGUSR2. */
2230 wpid = waitpid(fpid, &status, 0);
2231 REQUIRE_EQ(wpid, fpid);
2232 ATF_REQUIRE(WIFSTOPPED(status));
2233 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2235 /* Allow the main thread to try to finish its system call. */
2236 REQUIRE_EQ(ptrace(PT_RESUME, main_lwp, 0, 0), 0);
2239 * At this point, the main thread is in the middle of a system call and
2240 * has been resumed. The second thread has taken a SIGUSR2 which will
2241 * be replaced with a SIGKILL below. The main thread will get to run
2242 * first. It should notice the kill request (even though the signal
2243 * replacement occurred in the other thread) and exit accordingly. It
2244 * should not stop for the system call exit event.
2247 /* Replace the SIGUSR2 with a kill. */
2248 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2250 /* The last wait() should report the SIGKILL (not a syscall exit). */
2251 wpid = waitpid(fpid, &status, 0);
2252 REQUIRE_EQ(wpid, fpid);
2253 ATF_REQUIRE(WIFSIGNALED(status));
2254 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2256 wpid = wait(&status);
2257 REQUIRE_EQ(wpid, -1);
2258 REQUIRE_EQ(errno, ECHILD);
2262 sigusr1_handler(int sig)
2265 CHILD_REQUIRE_EQ(sig, SIGUSR1);
2270 * Verify that even if the signal queue is full for a child process,
2271 * a PT_KILL will kill the process.
2273 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2274 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2278 int max_pending_per_proc;
2282 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2284 ATF_REQUIRE((fpid = fork()) != -1);
2290 /* The first wait() should report the stop from SIGSTOP. */
2291 wpid = waitpid(fpid, &status, 0);
2292 REQUIRE_EQ(wpid, fpid);
2293 ATF_REQUIRE(WIFSTOPPED(status));
2294 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2296 len = sizeof(max_pending_per_proc);
2297 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2298 &max_pending_per_proc, &len, NULL, 0) == 0);
2300 /* Fill the signal queue. */
2301 for (i = 0; i < max_pending_per_proc; ++i)
2302 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2304 /* Kill the child process. */
2305 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2307 /* The last wait() should report the SIGKILL. */
2308 wpid = waitpid(fpid, &status, 0);
2309 REQUIRE_EQ(wpid, fpid);
2310 ATF_REQUIRE(WIFSIGNALED(status));
2311 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2313 wpid = wait(&status);
2314 REQUIRE_EQ(wpid, -1);
2315 REQUIRE_EQ(errno, ECHILD);
2319 * Verify that when stopped at a system call entry, a signal can be
2320 * requested with PT_CONTINUE which will be delivered once the system
2323 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2324 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2326 struct ptrace_lwpinfo pl;
2330 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2332 ATF_REQUIRE((fpid = fork()) != -1);
2339 /* The first wait() should report the stop from SIGSTOP. */
2340 wpid = waitpid(fpid, &status, 0);
2341 REQUIRE_EQ(wpid, fpid);
2342 ATF_REQUIRE(WIFSTOPPED(status));
2343 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2345 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2346 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2348 /* The second wait() should report a system call entry for getpid(). */
2349 wpid = waitpid(fpid, &status, 0);
2350 REQUIRE_EQ(wpid, fpid);
2351 ATF_REQUIRE(WIFSTOPPED(status));
2352 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2354 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2355 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2357 /* Continue the child process with a signal. */
2358 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2362 * The last wait() should report exit 2, i.e., a normal _exit
2363 * from the signal handler. In the meantime, catch and proceed
2364 * past any syscall stops.
2366 wpid = waitpid(fpid, &status, 0);
2367 REQUIRE_EQ(wpid, fpid);
2368 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2369 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2370 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2371 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2373 ATF_REQUIRE(WIFEXITED(status));
2374 REQUIRE_EQ(WEXITSTATUS(status), 2);
2379 wpid = wait(&status);
2380 REQUIRE_EQ(wpid, -1);
2381 REQUIRE_EQ(errno, ECHILD);
2385 sigusr1_counting_handler(int sig)
2387 static int counter = 0;
2389 CHILD_REQUIRE_EQ(sig, SIGUSR1);
2396 * Verify that, when continuing from a stop at system call entry and exit,
2397 * a signal can be requested from both stops, and both will be delivered when
2398 * the system call is complete.
2400 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2401 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2403 struct ptrace_lwpinfo pl;
2407 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2409 ATF_REQUIRE((fpid = fork()) != -1);
2416 /* The first wait() should report the stop from SIGSTOP. */
2417 wpid = waitpid(fpid, &status, 0);
2418 REQUIRE_EQ(wpid, fpid);
2419 ATF_REQUIRE(WIFSTOPPED(status));
2420 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2422 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2423 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2425 /* The second wait() should report a system call entry for getpid(). */
2426 wpid = waitpid(fpid, &status, 0);
2427 REQUIRE_EQ(wpid, fpid);
2428 ATF_REQUIRE(WIFSTOPPED(status));
2429 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2431 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2432 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2434 /* Continue the child process with a signal. */
2435 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2437 /* The third wait() should report a system call exit for getpid(). */
2438 wpid = waitpid(fpid, &status, 0);
2439 REQUIRE_EQ(wpid, fpid);
2440 ATF_REQUIRE(WIFSTOPPED(status));
2441 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2443 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2444 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2446 /* Continue the child process with a signal. */
2447 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2451 * The last wait() should report exit 2, i.e., a normal _exit
2452 * from the signal handler. In the meantime, catch and proceed
2453 * past any syscall stops.
2455 wpid = waitpid(fpid, &status, 0);
2456 REQUIRE_EQ(wpid, fpid);
2457 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2458 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2459 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2460 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2462 ATF_REQUIRE(WIFEXITED(status));
2463 REQUIRE_EQ(WEXITSTATUS(status), 2);
2468 wpid = wait(&status);
2469 REQUIRE_EQ(wpid, -1);
2470 REQUIRE_EQ(errno, ECHILD);
2474 * Verify that even if the signal queue is full for a child process,
2475 * a PT_CONTINUE with a signal will not result in loss of that signal.
2477 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2478 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2482 int max_pending_per_proc;
2486 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2487 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2489 ATF_REQUIRE((fpid = fork()) != -1);
2495 /* The first wait() should report the stop from SIGSTOP. */
2496 wpid = waitpid(fpid, &status, 0);
2497 REQUIRE_EQ(wpid, fpid);
2498 ATF_REQUIRE(WIFSTOPPED(status));
2499 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2501 len = sizeof(max_pending_per_proc);
2502 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2503 &max_pending_per_proc, &len, NULL, 0) == 0);
2505 /* Fill the signal queue. */
2506 for (i = 0; i < max_pending_per_proc; ++i)
2507 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2509 /* Continue with signal. */
2510 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2513 wpid = waitpid(fpid, &status, 0);
2514 REQUIRE_EQ(wpid, fpid);
2515 if (WIFSTOPPED(status)) {
2516 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2517 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2520 * The last wait() should report normal _exit from the
2523 ATF_REQUIRE(WIFEXITED(status));
2524 REQUIRE_EQ(WEXITSTATUS(status), 2);
2529 wpid = wait(&status);
2530 REQUIRE_EQ(wpid, -1);
2531 REQUIRE_EQ(errno, ECHILD);
2534 static sem_t sigusr1_sem;
2535 static int got_usr1;
2538 sigusr1_sempost_handler(int sig __unused)
2542 CHILD_REQUIRE_EQ(sem_post(&sigusr1_sem), 0);
2546 * Verify that even if the signal queue is full for a child process,
2547 * and the signal is masked, a PT_CONTINUE with a signal will not
2548 * result in loss of that signal.
2550 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2551 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2553 struct ptrace_lwpinfo pl;
2556 int max_pending_per_proc;
2561 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2562 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2563 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2566 ATF_REQUIRE((fpid = fork()) != -1);
2568 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2569 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2570 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2573 CHILD_REQUIRE_EQ(got_usr1, 0);
2575 /* Allow the pending SIGUSR1 in now. */
2576 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2577 /* Wait to receive the SIGUSR1. */
2579 err = sem_wait(&sigusr1_sem);
2580 CHILD_REQUIRE(err == 0 || errno == EINTR);
2581 } while (err != 0 && errno == EINTR);
2582 CHILD_REQUIRE_EQ(got_usr1, 1);
2586 /* The first wait() should report the stop from SIGSTOP. */
2587 wpid = waitpid(fpid, &status, 0);
2588 REQUIRE_EQ(wpid, fpid);
2589 ATF_REQUIRE(WIFSTOPPED(status));
2590 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2592 len = sizeof(max_pending_per_proc);
2593 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2594 &max_pending_per_proc, &len, NULL, 0) == 0);
2596 /* Fill the signal queue. */
2597 for (i = 0; i < max_pending_per_proc; ++i)
2598 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2600 /* Continue with signal. */
2601 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2603 /* Collect and ignore all of the SIGUSR2. */
2604 for (i = 0; i < max_pending_per_proc; ++i) {
2605 wpid = waitpid(fpid, &status, 0);
2606 REQUIRE_EQ(wpid, fpid);
2607 ATF_REQUIRE(WIFSTOPPED(status));
2608 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2609 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2612 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2613 wpid = waitpid(fpid, &status, 0);
2614 REQUIRE_EQ(wpid, fpid);
2615 ATF_REQUIRE(WIFSTOPPED(status));
2616 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
2617 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2618 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
2620 /* Continue the child, ignoring the SIGUSR1. */
2621 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2623 /* The last wait() should report exit after receiving SIGUSR1. */
2624 wpid = waitpid(fpid, &status, 0);
2625 REQUIRE_EQ(wpid, fpid);
2626 ATF_REQUIRE(WIFEXITED(status));
2627 REQUIRE_EQ(WEXITSTATUS(status), 1);
2629 wpid = wait(&status);
2630 REQUIRE_EQ(wpid, -1);
2631 REQUIRE_EQ(errno, ECHILD);
2635 * Verify that, after stopping due to a signal, that signal can be
2636 * replaced with another signal.
2638 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2639 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2641 struct ptrace_lwpinfo pl;
2645 ATF_REQUIRE((fpid = fork()) != -1);
2652 /* The first wait() should report the stop from SIGSTOP. */
2653 wpid = waitpid(fpid, &status, 0);
2654 REQUIRE_EQ(wpid, fpid);
2655 ATF_REQUIRE(WIFSTOPPED(status));
2656 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2658 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2660 /* Send a signal without ptrace. */
2661 REQUIRE_EQ(kill(fpid, SIGINT), 0);
2663 /* The second wait() should report a SIGINT was received. */
2664 wpid = waitpid(fpid, &status, 0);
2665 REQUIRE_EQ(wpid, fpid);
2666 ATF_REQUIRE(WIFSTOPPED(status));
2667 REQUIRE_EQ(WSTOPSIG(status), SIGINT);
2669 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2670 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2671 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGINT);
2673 /* Continue the child process with a different signal. */
2674 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM), 0);
2677 * The last wait() should report having died due to the new
2680 wpid = waitpid(fpid, &status, 0);
2681 REQUIRE_EQ(wpid, fpid);
2682 ATF_REQUIRE(WIFSIGNALED(status));
2683 REQUIRE_EQ(WTERMSIG(status), SIGTERM);
2685 wpid = wait(&status);
2686 REQUIRE_EQ(wpid, -1);
2687 REQUIRE_EQ(errno, ECHILD);
2691 * Verify that a signal can be passed through to the child even when there
2692 * was no true signal originally. Such cases arise when a SIGTRAP is
2693 * invented for e.g, system call stops.
2695 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2696 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2698 struct ptrace_lwpinfo pl;
2703 ATF_REQUIRE((fpid = fork()) != -1);
2706 /* SIGTRAP expected to cause exit on syscall entry. */
2707 rl.rlim_cur = rl.rlim_max = 0;
2708 REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0);
2713 /* The first wait() should report the stop from SIGSTOP. */
2714 wpid = waitpid(fpid, &status, 0);
2715 REQUIRE_EQ(wpid, fpid);
2716 ATF_REQUIRE(WIFSTOPPED(status));
2717 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2719 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2720 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2722 /* The second wait() should report a system call entry for getpid(). */
2723 wpid = waitpid(fpid, &status, 0);
2724 REQUIRE_EQ(wpid, fpid);
2725 ATF_REQUIRE(WIFSTOPPED(status));
2726 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2728 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2729 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2731 /* Continue the child process with a SIGTRAP. */
2732 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP), 0);
2736 * The last wait() should report exit due to SIGTRAP. In the
2737 * meantime, catch and proceed past any syscall stops.
2739 wpid = waitpid(fpid, &status, 0);
2740 REQUIRE_EQ(wpid, fpid);
2741 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2742 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2743 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2744 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2746 ATF_REQUIRE(WIFSIGNALED(status));
2747 REQUIRE_EQ(WTERMSIG(status), SIGTRAP);
2752 wpid = wait(&status);
2753 REQUIRE_EQ(wpid, -1);
2754 REQUIRE_EQ(errno, ECHILD);
2758 * A mixed bag PT_CONTINUE with signal test.
2760 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2761 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2763 struct ptrace_lwpinfo pl;
2767 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2769 ATF_REQUIRE((fpid = fork()) != -1);
2776 /* The first wait() should report the stop from SIGSTOP. */
2777 wpid = waitpid(fpid, &status, 0);
2778 REQUIRE_EQ(wpid, fpid);
2779 ATF_REQUIRE(WIFSTOPPED(status));
2780 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2782 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2783 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2785 /* The second wait() should report a system call entry for getpid(). */
2786 wpid = waitpid(fpid, &status, 0);
2787 REQUIRE_EQ(wpid, fpid);
2788 ATF_REQUIRE(WIFSTOPPED(status));
2789 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2791 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2792 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2794 /* Continue with the first SIGUSR1. */
2795 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2797 /* The next wait() should report a system call exit for getpid(). */
2798 wpid = waitpid(fpid, &status, 0);
2799 REQUIRE_EQ(wpid, fpid);
2800 ATF_REQUIRE(WIFSTOPPED(status));
2801 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2803 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2804 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2806 /* Send an ABRT without ptrace. */
2807 REQUIRE_EQ(kill(fpid, SIGABRT), 0);
2809 /* Continue normally. */
2810 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2812 /* The next wait() should report the SIGABRT. */
2813 wpid = waitpid(fpid, &status, 0);
2814 REQUIRE_EQ(wpid, fpid);
2815 ATF_REQUIRE(WIFSTOPPED(status));
2816 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
2818 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2819 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2820 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
2822 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2823 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2827 * The last wait() should report exit 2, i.e., a normal _exit
2828 * from the signal handler. In the meantime, catch and proceed
2829 * past any syscall stops.
2831 wpid = waitpid(fpid, &status, 0);
2832 REQUIRE_EQ(wpid, fpid);
2833 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2834 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2835 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2836 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2838 ATF_REQUIRE(WIFEXITED(status));
2839 REQUIRE_EQ(WEXITSTATUS(status), 2);
2844 wpid = wait(&status);
2845 REQUIRE_EQ(wpid, -1);
2846 REQUIRE_EQ(errno, ECHILD);
2850 * Verify a signal delivered by ptrace is noticed by kevent(2).
2852 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2853 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2856 int status, kq, nevents;
2859 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2861 ATF_REQUIRE((fpid = fork()) != -1);
2863 CHILD_REQUIRE((kq = kqueue()) > 0);
2864 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2865 CHILD_REQUIRE_EQ(kevent(kq, &kev, 1, NULL, 0, NULL), 0);
2870 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2871 if (nevents == -1 && errno == EINTR)
2873 CHILD_REQUIRE(nevents > 0);
2874 CHILD_REQUIRE_EQ(kev.filter, EVFILT_SIGNAL);
2875 CHILD_REQUIRE_EQ(kev.ident, (uintptr_t)SIGUSR1);
2882 /* The first wait() should report the stop from SIGSTOP. */
2883 wpid = waitpid(fpid, &status, 0);
2884 REQUIRE_EQ(wpid, fpid);
2885 ATF_REQUIRE(WIFSTOPPED(status));
2886 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2888 /* Continue with the SIGUSR1. */
2889 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2892 * The last wait() should report normal exit with code 1.
2894 wpid = waitpid(fpid, &status, 0);
2895 REQUIRE_EQ(wpid, fpid);
2896 ATF_REQUIRE(WIFEXITED(status));
2897 REQUIRE_EQ(WEXITSTATUS(status), 1);
2899 wpid = wait(&status);
2900 REQUIRE_EQ(wpid, -1);
2901 REQUIRE_EQ(errno, ECHILD);
2905 signal_thread(void *arg)
2910 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2912 /* Wait for this thread to receive a SIGUSR1. */
2914 err = sem_wait(&sigusr1_sem);
2915 CHILD_REQUIRE(err == 0 || errno == EINTR);
2916 } while (err != 0 && errno == EINTR);
2918 /* Free our companion thread from the barrier. */
2919 pthread_barrier_wait(pbarrier);
2922 * Swap ignore duties; the next SIGUSR1 should go to the
2925 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2926 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2927 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2929 /* Sync up threads after swapping signal masks. */
2930 pthread_barrier_wait(pbarrier);
2932 /* Wait until our companion has received its SIGUSR1. */
2933 pthread_barrier_wait(pbarrier);
2939 * Verify that a traced process with blocked signal received the
2940 * signal from kill() once unmasked.
2942 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2943 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2945 struct ptrace_lwpinfo pl;
2950 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2951 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2954 ATF_REQUIRE((fpid = fork()) != -1);
2956 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2957 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2958 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2961 CHILD_REQUIRE_EQ(got_usr1, 0);
2963 /* Allow the pending SIGUSR1 in now. */
2964 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2965 /* Wait to receive a SIGUSR1. */
2967 err = sem_wait(&sigusr1_sem);
2968 CHILD_REQUIRE(err == 0 || errno == EINTR);
2969 } while (err != 0 && errno == EINTR);
2970 CHILD_REQUIRE_EQ(got_usr1, 1);
2974 /* The first wait() should report the stop from SIGSTOP. */
2975 wpid = waitpid(fpid, &status, 0);
2976 REQUIRE_EQ(wpid, fpid);
2977 ATF_REQUIRE(WIFSTOPPED(status));
2978 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2979 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2980 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
2982 /* Send blocked SIGUSR1 which should cause a stop. */
2983 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2985 /* Continue the child ignoring the SIGSTOP. */
2986 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2988 /* The next wait() should report the kill(SIGUSR1) was received. */
2989 wpid = waitpid(fpid, &status, 0);
2990 REQUIRE_EQ(wpid, fpid);
2991 ATF_REQUIRE(WIFSTOPPED(status));
2992 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
2993 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2994 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
2996 /* Continue the child, allowing in the SIGUSR1. */
2997 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2999 /* The last wait() should report normal exit with code 1. */
3000 wpid = waitpid(fpid, &status, 0);
3001 REQUIRE_EQ(wpid, fpid);
3002 ATF_REQUIRE(WIFEXITED(status));
3003 REQUIRE_EQ(WEXITSTATUS(status), 1);
3005 wpid = wait(&status);
3006 REQUIRE_EQ(wpid, -1);
3007 REQUIRE_EQ(errno, ECHILD);
3011 * Verify that a traced process with blocked signal received the
3012 * signal from PT_CONTINUE once unmasked.
3014 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
3015 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
3017 struct ptrace_lwpinfo pl;
3022 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3023 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3026 ATF_REQUIRE((fpid = fork()) != -1);
3028 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3029 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3030 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
3033 CHILD_REQUIRE_EQ(got_usr1, 0);
3035 /* Allow the pending SIGUSR1 in now. */
3036 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
3037 /* Wait to receive a SIGUSR1. */
3039 err = sem_wait(&sigusr1_sem);
3040 CHILD_REQUIRE(err == 0 || errno == EINTR);
3041 } while (err != 0 && errno == EINTR);
3043 CHILD_REQUIRE_EQ(got_usr1, 1);
3047 /* The first wait() should report the stop from SIGSTOP. */
3048 wpid = waitpid(fpid, &status, 0);
3049 REQUIRE_EQ(wpid, fpid);
3050 ATF_REQUIRE(WIFSTOPPED(status));
3051 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3052 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3053 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
3055 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3056 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3058 /* The next wait() should report the SIGUSR1 was received. */
3059 wpid = waitpid(fpid, &status, 0);
3060 REQUIRE_EQ(wpid, fpid);
3061 ATF_REQUIRE(WIFSTOPPED(status));
3062 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
3063 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3064 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
3066 /* Continue the child, ignoring the SIGUSR1. */
3067 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3069 /* The last wait() should report normal exit with code 1. */
3070 wpid = waitpid(fpid, &status, 0);
3071 REQUIRE_EQ(wpid, fpid);
3072 ATF_REQUIRE(WIFEXITED(status));
3073 REQUIRE_EQ(WEXITSTATUS(status), 1);
3075 wpid = wait(&status);
3076 REQUIRE_EQ(wpid, -1);
3077 REQUIRE_EQ(errno, ECHILD);
3081 * Verify that if ptrace stops due to a signal but continues with
3082 * a different signal that the new signal is routed to a thread
3083 * that can accept it, and that the thread is awakened by the signal
3084 * in a timely manner.
3086 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3087 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3093 pthread_barrier_t barrier;
3095 REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
3096 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3097 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3099 ATF_REQUIRE((fpid = fork()) != -1);
3101 CHILD_REQUIRE_EQ(pthread_create(&t, NULL, signal_thread,
3102 (void *)&barrier), 0);
3104 /* The other thread should receive the first SIGUSR1. */
3105 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3106 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3107 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
3111 /* Wait until other thread has received its SIGUSR1. */
3112 pthread_barrier_wait(&barrier);
3115 * Swap ignore duties; the next SIGUSR1 should go to this
3118 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL),
3121 /* Sync up threads after swapping signal masks. */
3122 pthread_barrier_wait(&barrier);
3125 * Sync up with test code; we're ready for the next SIGUSR1
3130 /* Wait for this thread to receive a SIGUSR1. */
3132 err = sem_wait(&sigusr1_sem);
3133 CHILD_REQUIRE(err == 0 || errno == EINTR);
3134 } while (err != 0 && errno == EINTR);
3136 /* Free the other thread from the barrier. */
3137 pthread_barrier_wait(&barrier);
3139 CHILD_REQUIRE_EQ(pthread_join(t, NULL), 0);
3144 /* The first wait() should report the stop from SIGSTOP. */
3145 wpid = waitpid(fpid, &status, 0);
3146 REQUIRE_EQ(wpid, fpid);
3147 ATF_REQUIRE(WIFSTOPPED(status));
3148 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3150 /* Continue the child ignoring the SIGSTOP. */
3151 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3154 * Send a signal without ptrace that either thread will accept (USR2,
3157 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3159 /* The second wait() should report a SIGUSR2 was received. */
3160 wpid = waitpid(fpid, &status, 0);
3161 REQUIRE_EQ(wpid, fpid);
3162 ATF_REQUIRE(WIFSTOPPED(status));
3163 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3165 /* Continue the child, changing the signal to USR1. */
3166 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3168 /* The next wait() should report the stop from SIGSTOP. */
3169 wpid = waitpid(fpid, &status, 0);
3170 REQUIRE_EQ(wpid, fpid);
3171 ATF_REQUIRE(WIFSTOPPED(status));
3172 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3174 /* Continue the child ignoring the SIGSTOP. */
3175 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3177 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3179 /* The next wait() should report a SIGUSR2 was received. */
3180 wpid = waitpid(fpid, &status, 0);
3181 REQUIRE_EQ(wpid, fpid);
3182 ATF_REQUIRE(WIFSTOPPED(status));
3183 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3185 /* Continue the child, changing the signal to USR1. */
3186 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3188 /* The last wait() should report normal exit with code 1. */
3189 wpid = waitpid(fpid, &status, 0);
3190 REQUIRE_EQ(wpid, fpid);
3191 ATF_REQUIRE(WIFEXITED(status));
3192 REQUIRE_EQ(WEXITSTATUS(status), 1);
3194 wpid = wait(&status);
3195 REQUIRE_EQ(wpid, -1);
3196 REQUIRE_EQ(errno, ECHILD);
3200 raise_sigstop_thread(void *arg __unused)
3208 sleep_thread(void *arg __unused)
3216 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3225 * Become the reaper for this process tree. We need to be able to check
3226 * that both child and grandchild have died.
3228 REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
3231 ATF_REQUIRE(fpid >= 0);
3234 CHILD_REQUIRE(fpid >= 0);
3238 /* Pin to CPU 0 to serialize thread execution. */
3240 CPU_SET(0, &setmask);
3241 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
3242 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3243 CPU_WHICH_CPUSET, setid,
3244 sizeof(setmask), &setmask) == 0);
3246 if (sigstop_from_main_thread) {
3248 * We expect the SIGKILL sent when our parent
3249 * dies to be delivered to the new thread.
3250 * Raise the SIGSTOP in this thread so the
3253 CHILD_REQUIRE(pthread_create(&t, NULL,
3254 sleep_thread, NULL) == 0);
3258 * We expect the SIGKILL to be delivered to
3259 * this thread. After creating the new thread,
3260 * just get off the CPU so the other thread can
3261 * raise the SIGSTOP.
3263 CHILD_REQUIRE(pthread_create(&t, NULL,
3264 raise_sigstop_thread, NULL) == 0);
3270 /* First stop is trace_me() immediately after fork. */
3271 wpid = waitpid(fpid, &status, 0);
3272 CHILD_REQUIRE_EQ(wpid, fpid);
3273 CHILD_REQUIRE(WIFSTOPPED(status));
3274 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3276 CHILD_REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3278 /* Second stop is from the raise(SIGSTOP). */
3279 wpid = waitpid(fpid, &status, 0);
3280 CHILD_REQUIRE_EQ(wpid, fpid);
3281 CHILD_REQUIRE(WIFSTOPPED(status));
3282 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3285 * Terminate tracing process without detaching. Our child
3292 * We should get a normal exit from our immediate child and a SIGKILL
3293 * exit from our grandchild. The latter case is the interesting one.
3294 * Our grandchild should not have stopped due to the SIGSTOP that was
3295 * left dangling when its parent died.
3297 for (i = 0; i < 2; ++i) {
3298 wpid = wait(&status);
3300 ATF_REQUIRE(WIFEXITED(status));
3301 REQUIRE_EQ(WEXITSTATUS(status), 0);
3303 ATF_REQUIRE(WIFSIGNALED(status));
3304 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3310 * These two tests ensure that if the tracing process exits without detaching
3311 * just after the child received a SIGSTOP, the child is cleanly killed and
3312 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3313 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3314 * different threads, the SIGKILL must win. There are two variants of this
3315 * test, designed to catch the case where the SIGKILL is delivered to the
3316 * younger thread (the first test) and the case where the SIGKILL is delivered
3317 * to the older thread (the second test). This behavior has changed in the
3318 * past, so make no assumption.
3320 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3321 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3324 atf_tc_set_md_var(tc, "require.user", "root");
3326 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3329 terminate_with_pending_sigstop(true);
3332 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3333 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3336 atf_tc_set_md_var(tc, "require.user", "root");
3338 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3341 terminate_with_pending_sigstop(false);
3345 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3348 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3349 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3351 struct ptrace_lwpinfo pl;
3353 int status, event_mask, new_event_mask;
3355 ATF_REQUIRE((fpid = fork()) != -1);
3362 /* The first wait() should report the stop from trace_me(). */
3363 wpid = waitpid(fpid, &status, 0);
3364 REQUIRE_EQ(wpid, fpid);
3365 ATF_REQUIRE(WIFSTOPPED(status));
3366 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3368 /* Set several unobtrusive event bits. */
3369 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3370 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3371 sizeof(event_mask)) == 0);
3373 /* Send a SIGKILL without using ptrace. */
3374 REQUIRE_EQ(kill(fpid, SIGKILL), 0);
3376 /* Continue the child ignoring the SIGSTOP. */
3377 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3379 /* The next stop should be due to the SIGKILL. */
3380 wpid = waitpid(fpid, &status, 0);
3381 REQUIRE_EQ(wpid, fpid);
3382 ATF_REQUIRE(WIFSTOPPED(status));
3383 REQUIRE_EQ(WSTOPSIG(status), SIGKILL);
3385 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3386 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3387 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGKILL);
3389 /* Continue the child ignoring the SIGKILL. */
3390 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3392 /* The next wait() should report the stop from SIGSTOP. */
3393 wpid = waitpid(fpid, &status, 0);
3394 REQUIRE_EQ(wpid, fpid);
3395 ATF_REQUIRE(WIFSTOPPED(status));
3396 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3398 /* Check the current event mask. It should not have changed. */
3400 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3401 sizeof(new_event_mask)) == 0);
3402 REQUIRE_EQ(event_mask, new_event_mask);
3404 /* Continue the child to let it exit. */
3405 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3407 /* The last event should be for the child process's exit. */
3408 wpid = waitpid(fpid, &status, 0);
3409 ATF_REQUIRE(WIFEXITED(status));
3410 REQUIRE_EQ(WEXITSTATUS(status), 0);
3412 wpid = wait(&status);
3413 REQUIRE_EQ(wpid, -1);
3414 REQUIRE_EQ(errno, ECHILD);
3418 flock_thread(void *arg)
3423 (void)flock(fd, LOCK_EX);
3424 (void)flock(fd, LOCK_UN);
3429 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3430 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3431 * on a lock. This is a regression test for r318191.
3433 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3434 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3436 pthread_barrier_t barrier;
3437 pthread_barrierattr_t battr;
3440 int error, fd, i, status;
3442 REQUIRE_EQ(pthread_barrierattr_init(&battr), 0);
3443 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3444 PTHREAD_PROCESS_SHARED) == 0);
3445 REQUIRE_EQ(pthread_barrier_init(&barrier, &battr, 2), 0);
3447 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3448 fd = mkstemp(tmpfile);
3449 ATF_REQUIRE(fd >= 0);
3451 ATF_REQUIRE((child = fork()) != -1);
3456 error = pthread_barrier_wait(&barrier);
3457 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3460 cfd = open(tmpfile, O_RDONLY);
3465 * We want at least two threads blocked on the file lock since
3466 * the SIGSTOP from PT_ATTACH may kick one of them out of
3469 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3471 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3473 if (pthread_join(t[0], NULL) != 0)
3475 if (pthread_join(t[1], NULL) != 0)
3480 REQUIRE_EQ(flock(fd, LOCK_EX), 0);
3482 error = pthread_barrier_wait(&barrier);
3483 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3486 * Give the child some time to block. Is there a better way to do this?
3491 * Attach and give the child 3 seconds to stop.
3493 REQUIRE_EQ(ptrace(PT_ATTACH, child, NULL, 0), 0);
3494 for (i = 0; i < 3; i++) {
3495 wpid = waitpid(child, &status, WNOHANG);
3496 if (wpid == child && WIFSTOPPED(status) &&
3497 WSTOPSIG(status) == SIGSTOP)
3501 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3503 REQUIRE_EQ(ptrace(PT_DETACH, child, NULL, 0), 0);
3505 REQUIRE_EQ(flock(fd, LOCK_UN), 0);
3506 REQUIRE_EQ(unlink(tmpfile), 0);
3507 REQUIRE_EQ(close(fd), 0);
3511 sigusr1_step_handler(int sig)
3514 CHILD_REQUIRE_EQ(sig, SIGUSR1);
3519 * Verify that PT_STEP with a signal invokes the signal before
3520 * stepping the next instruction (and that the next instruction is
3521 * stepped correctly).
3523 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3524 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3526 struct ptrace_lwpinfo pl;
3530 ATF_REQUIRE((fpid = fork()) != -1);
3533 signal(SIGUSR1, sigusr1_step_handler);
3538 /* The first wait() should report the stop from SIGSTOP. */
3539 wpid = waitpid(fpid, &status, 0);
3540 REQUIRE_EQ(wpid, fpid);
3541 ATF_REQUIRE(WIFSTOPPED(status));
3542 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3544 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3546 /* The next stop should report the SIGABRT in the child body. */
3547 wpid = waitpid(fpid, &status, 0);
3548 REQUIRE_EQ(wpid, fpid);
3549 ATF_REQUIRE(WIFSTOPPED(status));
3550 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3552 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3553 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3554 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3556 /* Step the child process inserting SIGUSR1. */
3557 REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1), 0);
3559 /* The next stop should report the SIGABRT in the signal handler. */
3560 wpid = waitpid(fpid, &status, 0);
3561 REQUIRE_EQ(wpid, fpid);
3562 ATF_REQUIRE(WIFSTOPPED(status));
3563 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3565 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3566 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3567 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3569 /* Continue the child process discarding the signal. */
3570 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3572 /* The next stop should report a trace trap from PT_STEP. */
3573 wpid = waitpid(fpid, &status, 0);
3574 REQUIRE_EQ(wpid, fpid);
3575 ATF_REQUIRE(WIFSTOPPED(status));
3576 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3578 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3579 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3580 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3581 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3583 /* Continue the child to let it exit. */
3584 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3586 /* The last event should be for the child process's exit. */
3587 wpid = waitpid(fpid, &status, 0);
3588 ATF_REQUIRE(WIFEXITED(status));
3589 REQUIRE_EQ(WEXITSTATUS(status), 1);
3591 wpid = wait(&status);
3592 REQUIRE_EQ(wpid, -1);
3593 REQUIRE_EQ(errno, ECHILD);
3596 #ifdef HAVE_BREAKPOINT
3598 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3599 * for a breakpoint trap.
3601 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3602 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3604 struct ptrace_lwpinfo pl;
3608 ATF_REQUIRE((fpid = fork()) != -1);
3615 /* The first wait() should report the stop from SIGSTOP. */
3616 wpid = waitpid(fpid, &status, 0);
3617 REQUIRE_EQ(wpid, fpid);
3618 ATF_REQUIRE(WIFSTOPPED(status));
3619 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3621 /* Continue the child ignoring the SIGSTOP. */
3622 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3624 /* The second wait() should report hitting the breakpoint. */
3625 wpid = waitpid(fpid, &status, 0);
3626 REQUIRE_EQ(wpid, fpid);
3627 ATF_REQUIRE(WIFSTOPPED(status));
3628 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3630 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3631 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3632 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3633 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3635 /* Kill the child process. */
3636 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
3638 /* The last wait() should report the SIGKILL. */
3639 wpid = waitpid(fpid, &status, 0);
3640 REQUIRE_EQ(wpid, fpid);
3641 ATF_REQUIRE(WIFSIGNALED(status));
3642 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3644 wpid = wait(&status);
3645 REQUIRE_EQ(wpid, -1);
3646 REQUIRE_EQ(errno, ECHILD);
3648 #endif /* HAVE_BREAKPOINT */
3651 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3652 * for a single-step trap from PT_STEP.
3654 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3655 ATF_TC_BODY(ptrace__step_siginfo, tc)
3657 struct ptrace_lwpinfo pl;
3661 ATF_REQUIRE((fpid = fork()) != -1);
3667 /* The first wait() should report the stop from SIGSTOP. */
3668 wpid = waitpid(fpid, &status, 0);
3669 REQUIRE_EQ(wpid, fpid);
3670 ATF_REQUIRE(WIFSTOPPED(status));
3671 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3673 /* Step the child ignoring the SIGSTOP. */
3674 REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, 0), 0);
3676 /* The second wait() should report a single-step trap. */
3677 wpid = waitpid(fpid, &status, 0);
3678 REQUIRE_EQ(wpid, fpid);
3679 ATF_REQUIRE(WIFSTOPPED(status));
3680 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3682 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3683 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3684 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3685 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3687 /* Continue the child process. */
3688 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3690 /* The last event should be for the child process's exit. */
3691 wpid = waitpid(fpid, &status, 0);
3692 ATF_REQUIRE(WIFEXITED(status));
3693 REQUIRE_EQ(WEXITSTATUS(status), 1);
3695 wpid = wait(&status);
3696 REQUIRE_EQ(wpid, -1);
3697 REQUIRE_EQ(errno, ECHILD);
3700 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3702 continue_thread(void *arg __unused)
3709 continue_thread_main(void)
3711 pthread_t threads[2];
3713 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3715 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3717 CHILD_REQUIRE_EQ(pthread_join(threads[0], NULL), 0);
3718 CHILD_REQUIRE_EQ(pthread_join(threads[1], NULL), 0);
3723 * Ensure that PT_CONTINUE clears the status of the thread that
3724 * triggered the stop even if a different thread's LWP was passed to
3727 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3728 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3730 struct ptrace_lwpinfo pl;
3737 ATF_REQUIRE((fpid = fork()) != -1);
3740 continue_thread_main();
3743 /* The first wait() should report the stop from SIGSTOP. */
3744 wpid = waitpid(fpid, &status, 0);
3745 REQUIRE_EQ(wpid, fpid);
3746 ATF_REQUIRE(WIFSTOPPED(status));
3747 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3749 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3752 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
3754 /* Continue the child ignoring the SIGSTOP. */
3755 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3757 /* One of the new threads should report it's birth. */
3758 wpid = waitpid(fpid, &status, 0);
3759 REQUIRE_EQ(wpid, fpid);
3760 ATF_REQUIRE(WIFSTOPPED(status));
3761 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3763 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3764 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3765 (PL_FLAG_BORN | PL_FLAG_SCX));
3766 lwps[0] = pl.pl_lwpid;
3769 * Suspend this thread to ensure both threads are alive before
3770 * hitting the breakpoint.
3772 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3774 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3776 /* Second thread should report it's birth. */
3777 wpid = waitpid(fpid, &status, 0);
3778 REQUIRE_EQ(wpid, fpid);
3779 ATF_REQUIRE(WIFSTOPPED(status));
3780 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3782 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3783 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3784 (PL_FLAG_BORN | PL_FLAG_SCX));
3785 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3786 lwps[1] = pl.pl_lwpid;
3788 /* Resume both threads waiting for breakpoint events. */
3789 hit_break[0] = hit_break[1] = false;
3790 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3791 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3793 /* One thread should report a breakpoint. */
3794 wpid = waitpid(fpid, &status, 0);
3795 REQUIRE_EQ(wpid, fpid);
3796 ATF_REQUIRE(WIFSTOPPED(status));
3797 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3799 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3800 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3801 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3802 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3803 if (pl.pl_lwpid == lwps[0])
3807 hit_break[i] = true;
3808 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3810 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3813 * Resume both threads but pass the other thread's LWPID to
3816 REQUIRE_EQ(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0), 0);
3819 * Will now get two thread exit events and one more breakpoint
3822 for (j = 0; j < 3; j++) {
3823 wpid = waitpid(fpid, &status, 0);
3824 REQUIRE_EQ(wpid, fpid);
3825 ATF_REQUIRE(WIFSTOPPED(status));
3826 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3828 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3831 if (pl.pl_lwpid == lwps[0])
3836 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3837 if (pl.pl_flags & PL_FLAG_EXITED) {
3838 ATF_REQUIRE_MSG(hit_break[i],
3839 "exited thread did not report breakpoint");
3842 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3843 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3844 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3845 ATF_REQUIRE_MSG(!hit_break[i],
3846 "double breakpoint event");
3847 hit_break[i] = true;
3848 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3851 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3855 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3858 /* Both threads should have exited. */
3859 REQUIRE_EQ(lwps[0], 0);
3860 REQUIRE_EQ(lwps[1], 0);
3862 /* The last event should be for the child process's exit. */
3863 wpid = waitpid(fpid, &status, 0);
3864 ATF_REQUIRE(WIFEXITED(status));
3865 REQUIRE_EQ(WEXITSTATUS(status), 1);
3867 wpid = wait(&status);
3868 REQUIRE_EQ(wpid, -1);
3869 REQUIRE_EQ(errno, ECHILD);
3874 * Verify that PT_LWPINFO doesn't return stale siginfo.
3876 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3877 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3879 struct ptrace_lwpinfo pl;
3883 ATF_REQUIRE((fpid = fork()) != -1);
3890 /* The first wait() should report the stop from SIGSTOP. */
3891 wpid = waitpid(fpid, &status, 0);
3892 REQUIRE_EQ(wpid, fpid);
3893 ATF_REQUIRE(WIFSTOPPED(status));
3894 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3896 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3898 /* The next stop should report the SIGABRT in the child body. */
3899 wpid = waitpid(fpid, &status, 0);
3900 REQUIRE_EQ(wpid, fpid);
3901 ATF_REQUIRE(WIFSTOPPED(status));
3902 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3904 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3905 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3906 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3909 * Continue the process ignoring the signal, but enabling
3912 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
3915 * The next stop should report a system call entry from
3916 * exit(). PL_FLAGS_SI should not be set.
3918 wpid = waitpid(fpid, &status, 0);
3919 REQUIRE_EQ(wpid, fpid);
3920 ATF_REQUIRE(WIFSTOPPED(status));
3921 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3923 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3924 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3925 REQUIRE_EQ((pl.pl_flags & PL_FLAG_SI), 0);
3927 /* Disable syscall tracing and continue the child to let it exit. */
3928 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3929 sizeof(events)) == 0);
3930 events &= ~PTRACE_SYSCALL;
3931 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3932 sizeof(events)) == 0);
3933 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3935 /* The last event should be for the child process's exit. */
3936 wpid = waitpid(fpid, &status, 0);
3937 ATF_REQUIRE(WIFEXITED(status));
3938 REQUIRE_EQ(WEXITSTATUS(status), 1);
3940 wpid = wait(&status);
3941 REQUIRE_EQ(wpid, -1);
3942 REQUIRE_EQ(errno, ECHILD);
3946 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3948 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3949 ATF_TC_BODY(ptrace__syscall_args, tc)
3951 struct ptrace_lwpinfo pl;
3952 struct ptrace_sc_ret psr;
3957 ATF_REQUIRE((fpid = fork()) != -1);
3961 /* Close a fd that should not exist. */
3966 /* The first wait() should report the stop from SIGSTOP. */
3967 wpid = waitpid(fpid, &status, 0);
3968 REQUIRE_EQ(wpid, fpid);
3969 ATF_REQUIRE(WIFSTOPPED(status));
3970 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3973 * Continue the process ignoring the signal, but enabling
3976 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
3979 * The next stop should be the syscall entry from getpid().
3981 wpid = waitpid(fpid, &status, 0);
3982 REQUIRE_EQ(wpid, fpid);
3983 ATF_REQUIRE(WIFSTOPPED(status));
3984 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3986 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3987 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3988 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_getpid);
3990 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3993 * The next stop should be the syscall exit from getpid().
3995 wpid = waitpid(fpid, &status, 0);
3996 REQUIRE_EQ(wpid, fpid);
3997 ATF_REQUIRE(WIFSTOPPED(status));
3998 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4000 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4001 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4002 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_getpid);
4004 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4005 sizeof(psr)) != -1);
4006 REQUIRE_EQ(psr.sr_error, 0);
4007 REQUIRE_EQ(psr.sr_retval[0], wpid);
4009 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4012 * The next stop should be the syscall entry from kill().
4014 wpid = waitpid(fpid, &status, 0);
4015 REQUIRE_EQ(wpid, fpid);
4016 ATF_REQUIRE(WIFSTOPPED(status));
4017 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4019 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4020 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4021 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_kill);
4022 REQUIRE_EQ(pl.pl_syscall_narg, 2u);
4024 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4025 sizeof(args)) != -1);
4026 REQUIRE_EQ(args[0], wpid);
4027 REQUIRE_EQ(args[1], 0);
4029 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4032 * The next stop should be the syscall exit from kill().
4034 wpid = waitpid(fpid, &status, 0);
4035 REQUIRE_EQ(wpid, fpid);
4036 ATF_REQUIRE(WIFSTOPPED(status));
4037 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4039 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4040 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4041 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_kill);
4043 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4044 sizeof(psr)) != -1);
4045 REQUIRE_EQ(psr.sr_error, 0);
4047 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4050 * The next stop should be the syscall entry from close().
4052 wpid = waitpid(fpid, &status, 0);
4053 REQUIRE_EQ(wpid, fpid);
4054 ATF_REQUIRE(WIFSTOPPED(status));
4055 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4057 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4058 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4059 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_close);
4060 REQUIRE_EQ(pl.pl_syscall_narg, 1u);
4062 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4063 sizeof(args)) != -1);
4064 REQUIRE_EQ(args[0], 12345);
4066 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4069 * The next stop should be the syscall exit from close().
4071 wpid = waitpid(fpid, &status, 0);
4072 REQUIRE_EQ(wpid, fpid);
4073 ATF_REQUIRE(WIFSTOPPED(status));
4074 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4076 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4077 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4078 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_close);
4080 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4081 sizeof(psr)) != -1);
4082 REQUIRE_EQ(psr.sr_error, EBADF);
4084 /* Disable syscall tracing and continue the child to let it exit. */
4085 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4086 sizeof(events)) == 0);
4087 events &= ~PTRACE_SYSCALL;
4088 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4089 sizeof(events)) == 0);
4090 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4092 /* The last event should be for the child process's exit. */
4093 wpid = waitpid(fpid, &status, 0);
4094 ATF_REQUIRE(WIFEXITED(status));
4095 REQUIRE_EQ(WEXITSTATUS(status), 1);
4097 wpid = wait(&status);
4098 REQUIRE_EQ(wpid, -1);
4099 REQUIRE_EQ(errno, ECHILD);
4103 * Verify that when the process is traced that it isn't reparent
4104 * to the init process when we close all process descriptors.
4106 ATF_TC(ptrace__proc_reparent);
4107 ATF_TC_HEAD(ptrace__proc_reparent, tc)
4110 atf_tc_set_md_var(tc, "timeout", "2");
4112 ATF_TC_BODY(ptrace__proc_reparent, tc)
4114 pid_t traced, debuger, wpid;
4117 traced = pdfork(&pd, 0);
4118 ATF_REQUIRE(traced >= 0);
4123 ATF_REQUIRE(pd >= 0);
4126 ATF_REQUIRE(debuger >= 0);
4128 /* The traced process is reparented to debuger. */
4129 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4130 wpid = waitpid(traced, &status, 0);
4131 REQUIRE_EQ(wpid, traced);
4132 ATF_REQUIRE(WIFSTOPPED(status));
4133 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4134 REQUIRE_EQ(close(pd), 0);
4135 REQUIRE_EQ(ptrace(PT_DETACH, traced, (caddr_t)1, 0), 0);
4137 /* We closed pd so we should not have any child. */
4138 wpid = wait(&status);
4139 REQUIRE_EQ(wpid, -1);
4140 REQUIRE_EQ(errno, ECHILD);
4145 REQUIRE_EQ(close(pd), 0);
4146 wpid = waitpid(debuger, &status, 0);
4147 REQUIRE_EQ(wpid, debuger);
4148 REQUIRE_EQ(WEXITSTATUS(status), 0);
4150 /* Check if we still have any child. */
4151 wpid = wait(&status);
4152 REQUIRE_EQ(wpid, -1);
4153 REQUIRE_EQ(errno, ECHILD);
4157 * Ensure that traced processes created with pdfork(2) are visible to
4160 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_wait_child);
4161 ATF_TC_BODY(ptrace__procdesc_wait_child, tc)
4166 child = pdfork(&pd, 0);
4167 ATF_REQUIRE(child >= 0);
4171 (void)raise(SIGSTOP);
4175 wpid = waitpid(child, &status, 0);
4176 REQUIRE_EQ(wpid, child);
4177 ATF_REQUIRE(WIFSTOPPED(status));
4178 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4180 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4182 wpid = wait(&status);
4183 REQUIRE_EQ(wpid, child);
4184 ATF_REQUIRE(WIFSTOPPED(status));
4185 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4187 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4190 * If process was created by pdfork, the return code have to
4191 * be collected through process descriptor.
4193 wpid = wait(&status);
4194 REQUIRE_EQ(wpid, -1);
4195 REQUIRE_EQ(errno, ECHILD);
4197 ATF_REQUIRE(close(pd) != -1);
4201 * Ensure that traced processes created with pdfork(2) are not visible
4202 * after returning to parent - waitid(P_ALL).
4204 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_reparent_wait_child);
4205 ATF_TC_BODY(ptrace__procdesc_reparent_wait_child, tc)
4207 pid_t traced, debuger, wpid;
4210 traced = pdfork(&pd, 0);
4211 ATF_REQUIRE(traced >= 0);
4216 ATF_REQUIRE(pd >= 0);
4218 /* Wait until the child process has stopped before fork()ing again. */
4219 REQUIRE_EQ(traced, waitpid(traced, &status, WSTOPPED));
4221 ATF_REQUIRE(debuger >= 0);
4223 /* The traced process is reparented to debuger. */
4224 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4225 wpid = waitpid(traced, &status, 0);
4226 REQUIRE_EQ(wpid, traced);
4227 ATF_REQUIRE(WIFSTOPPED(status));
4228 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4230 /* Allow process to die. */
4231 REQUIRE_EQ(ptrace(PT_CONTINUE, traced, (caddr_t)1, 0), 0);
4232 wpid = waitpid(traced, &status, 0);
4233 REQUIRE_EQ(wpid, traced);
4234 ATF_REQUIRE(WIFEXITED(status));
4235 REQUIRE_EQ(WEXITSTATUS(status), 0);
4237 /* Reparent back to the orginal process. */
4238 REQUIRE_EQ(close(pd), 0);
4242 wpid = waitpid(debuger, &status, 0);
4243 REQUIRE_EQ(wpid, debuger);
4244 REQUIRE_EQ(WEXITSTATUS(status), 0);
4247 * We have a child but it has a process descriptori
4248 * so we should not be able to collect it process.
4250 wpid = wait(&status);
4251 REQUIRE_EQ(wpid, -1);
4252 REQUIRE_EQ(errno, ECHILD);
4254 REQUIRE_EQ(close(pd), 0);
4260 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4261 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4262 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4263 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4264 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4265 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4266 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4267 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4268 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4270 ptrace__follow_fork_child_detached_unrelated_debugger);
4272 ptrace__follow_fork_parent_detached_unrelated_debugger);
4273 ATF_TP_ADD_TC(tp, ptrace__getppid);
4274 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4275 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4276 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4277 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4278 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4279 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4280 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4281 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4282 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4283 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4284 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4285 #ifdef HAVE_BREAKPOINT
4286 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4288 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4289 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4290 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4291 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4292 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4293 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4295 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4296 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4297 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4298 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4299 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4300 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4301 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4302 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4303 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4304 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4305 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4306 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4307 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4308 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4309 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4310 #ifdef HAVE_BREAKPOINT
4311 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4313 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4314 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4315 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4317 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4318 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4319 ATF_TP_ADD_TC(tp, ptrace__proc_reparent);
4320 ATF_TP_ADD_TC(tp, ptrace__procdesc_wait_child);
4321 ATF_TP_ADD_TC(tp, ptrace__procdesc_reparent_wait_child);
4323 return (atf_no_error());