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 #include <sys/types.h>
28 #include <sys/cpuset.h>
30 #include <sys/event.h>
33 #include <sys/procctl.h>
34 #include <sys/procdesc.h>
35 #include <sys/ptrace.h>
36 #include <sys/procfs.h>
37 #include <sys/queue.h>
39 #include <sys/syscall.h>
40 #include <sys/sysctl.h>
44 #include <machine/cpufunc.h>
47 #include <semaphore.h>
56 * Architectures with a user-visible breakpoint().
58 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
59 defined(__i386__) || defined(__riscv)
60 #define HAVE_BREAKPOINT
64 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
66 #ifdef HAVE_BREAKPOINT
67 #if defined(__aarch64__)
68 #define SKIP_BREAK(reg) ((reg)->elr += 4)
69 #elif defined(__amd64__) || defined(__i386__)
70 #define SKIP_BREAK(reg)
71 #elif defined(__arm__)
72 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
73 #elif defined(__riscv)
74 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
79 * A variant of ATF_REQUIRE that is suitable for use in child
80 * processes. This only works if the parent process is tripped up by
81 * the early exit and fails some requirement itself.
83 #define CHILD_REQUIRE(exp) do { \
85 child_fail_require(__FILE__, __LINE__, \
89 #define CHILD_REQUIRE_EQ(actual, expected) do { \
90 __typeof__(expected) _e = expected; \
91 __typeof__(actual) _a = actual; \
93 child_fail_require(__FILE__, __LINE__, #actual \
94 " (%jd) == " #expected " (%jd) not met\n", \
95 (intmax_t)_a, (intmax_t)_e); \
99 child_fail_require(const char *file, int line, const char *fmt, ...)
104 /* Use write() not fprintf() to avoid possible duplicate output. */
105 snprintf(buf, sizeof(buf), "%s:%d: ", file, line);
106 write(STDERR_FILENO, buf, strlen(buf));
108 vsnprintf(buf, sizeof(buf), fmt, ap);
109 write(STDERR_FILENO, buf, strlen(buf));
115 #define REQUIRE_EQ(actual, expected) do { \
116 __typeof__(expected) _e = expected; \
117 __typeof__(actual) _a = actual; \
118 ATF_REQUIRE_MSG(_e == _a, #actual " (%jd) == " \
119 #expected " (%jd) not met", (intmax_t)_a, (intmax_t)_e); \
126 /* Attach the parent process as a tracer of this process. */
127 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
129 /* Trigger a stop. */
134 attach_child(pid_t pid)
139 REQUIRE_EQ(ptrace(PT_ATTACH, pid, NULL, 0), 0);
141 wpid = waitpid(pid, &status, 0);
142 REQUIRE_EQ(wpid, pid);
143 ATF_REQUIRE(WIFSTOPPED(status));
144 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
148 wait_for_zombie(pid_t pid)
152 * Wait for a process to exit. This is kind of gross, but
153 * there is not a better way.
155 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
156 * with ESRCH. After that change, a valid struct kinfo_proc
157 * is returned for zombies with ki_stat set to SZOMB.
160 struct kinfo_proc kp;
166 mib[2] = KERN_PROC_PID;
169 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
170 REQUIRE_EQ(errno, ESRCH);
173 if (kp.ki_stat == SZOMB)
180 * Verify that a parent debugger process "sees" the exit of a debugged
181 * process exactly once when attached via PT_TRACE_ME.
183 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
184 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
189 ATF_REQUIRE((child = fork()) != -1);
197 /* Parent process. */
199 /* The first wait() should report the stop from SIGSTOP. */
200 wpid = waitpid(child, &status, 0);
201 REQUIRE_EQ(wpid, child);
202 ATF_REQUIRE(WIFSTOPPED(status));
203 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
205 /* Continue the child ignoring the SIGSTOP. */
206 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
208 /* The second wait() should report the exit status. */
209 wpid = waitpid(child, &status, 0);
210 REQUIRE_EQ(wpid, child);
211 ATF_REQUIRE(WIFEXITED(status));
212 REQUIRE_EQ(WEXITSTATUS(status), 1);
214 /* The child should no longer exist. */
215 wpid = waitpid(child, &status, 0);
216 REQUIRE_EQ(wpid, -1);
217 REQUIRE_EQ(errno, ECHILD);
221 * Verify that a parent debugger process "sees" the exit of a debugged
222 * process exactly once when attached via PT_ATTACH.
224 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
225 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
228 int cpipe[2], status;
231 REQUIRE_EQ(pipe(cpipe), 0);
232 ATF_REQUIRE((child = fork()) != -1);
237 /* Wait for the parent to attach. */
238 CHILD_REQUIRE_EQ(0, read(cpipe[1], &c, sizeof(c)));
244 /* Parent process. */
246 /* Attach to the child process. */
249 /* Continue the child ignoring the SIGSTOP. */
250 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
252 /* Signal the child to exit. */
255 /* The second wait() should report the exit status. */
256 wpid = waitpid(child, &status, 0);
257 REQUIRE_EQ(wpid, child);
258 ATF_REQUIRE(WIFEXITED(status));
259 REQUIRE_EQ(WEXITSTATUS(status), 1);
261 /* The child should no longer exist. */
262 wpid = waitpid(child, &status, 0);
263 REQUIRE_EQ(wpid, -1);
264 REQUIRE_EQ(errno, ECHILD);
268 * Verify that a parent process "sees" the exit of a debugged process only
269 * after the debugger has seen it.
271 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
272 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
274 pid_t child, debugger, wpid;
275 int cpipe[2], dpipe[2], status;
278 REQUIRE_EQ(pipe(cpipe), 0);
279 ATF_REQUIRE((child = fork()) != -1);
285 /* Wait for parent to be ready. */
286 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
293 REQUIRE_EQ(pipe(dpipe), 0);
294 ATF_REQUIRE((debugger = fork()) != -1);
297 /* Debugger process. */
300 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
302 wpid = waitpid(child, &status, 0);
303 CHILD_REQUIRE_EQ(wpid, child);
304 CHILD_REQUIRE(WIFSTOPPED(status));
305 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
307 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
309 /* Signal parent that debugger is attached. */
310 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
313 /* Wait for parent's failed wait. */
314 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)), 0);
316 wpid = waitpid(child, &status, 0);
317 CHILD_REQUIRE_EQ(wpid, child);
318 CHILD_REQUIRE(WIFEXITED(status));
319 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
325 /* Parent process. */
327 /* Wait for the debugger to attach to the child. */
328 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
330 /* Release the child. */
331 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
332 REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
335 wait_for_zombie(child);
338 * This wait should return a pid of 0 to indicate no status to
339 * report. The parent should see the child as non-exited
340 * until the debugger sees the exit.
342 wpid = waitpid(child, &status, WNOHANG);
345 /* Signal the debugger to wait for the child. */
348 /* Wait for the debugger. */
349 wpid = waitpid(debugger, &status, 0);
350 REQUIRE_EQ(wpid, debugger);
351 ATF_REQUIRE(WIFEXITED(status));
352 REQUIRE_EQ(WEXITSTATUS(status), 0);
354 /* The child process should now be ready. */
355 wpid = waitpid(child, &status, WNOHANG);
356 REQUIRE_EQ(wpid, child);
357 ATF_REQUIRE(WIFEXITED(status));
358 REQUIRE_EQ(WEXITSTATUS(status), 1);
362 * Verify that a parent process "sees" the exit of a debugged process
363 * only after a non-direct-child debugger has seen it. In particular,
364 * various wait() calls in the parent must avoid failing with ESRCH by
365 * checking the parent's orphan list for the debugee.
367 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
368 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
370 pid_t child, debugger, fpid, wpid;
371 int cpipe[2], dpipe[2], status;
374 REQUIRE_EQ(pipe(cpipe), 0);
375 ATF_REQUIRE((child = fork()) != -1);
381 /* Wait for parent to be ready. */
382 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
389 REQUIRE_EQ(pipe(dpipe), 0);
390 ATF_REQUIRE((debugger = fork()) != -1);
393 /* Debugger parent. */
396 * Fork again and drop the debugger parent so that the
397 * debugger is not a child of the main parent.
399 CHILD_REQUIRE((fpid = fork()) != -1);
403 /* Debugger process. */
406 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
408 wpid = waitpid(child, &status, 0);
409 CHILD_REQUIRE_EQ(wpid, child);
410 CHILD_REQUIRE(WIFSTOPPED(status));
411 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
413 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
415 /* Signal parent that debugger is attached. */
416 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
419 /* Wait for parent's failed wait. */
420 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)),
423 wpid = waitpid(child, &status, 0);
424 CHILD_REQUIRE_EQ(wpid, child);
425 CHILD_REQUIRE(WIFEXITED(status));
426 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
432 /* Parent process. */
434 /* Wait for the debugger parent process to exit. */
435 wpid = waitpid(debugger, &status, 0);
436 REQUIRE_EQ(wpid, debugger);
437 ATF_REQUIRE(WIFEXITED(status));
438 REQUIRE_EQ(WEXITSTATUS(status), 2);
440 /* A WNOHANG wait here should see the non-exited child. */
441 wpid = waitpid(child, &status, WNOHANG);
444 /* Wait for the debugger to attach to the child. */
445 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
447 /* Release the child. */
448 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
449 REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
452 wait_for_zombie(child);
455 * This wait should return a pid of 0 to indicate no status to
456 * report. The parent should see the child as non-exited
457 * until the debugger sees the exit.
459 wpid = waitpid(child, &status, WNOHANG);
462 /* Signal the debugger to wait for the child. */
463 REQUIRE_EQ(write(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
465 /* Wait for the debugger. */
466 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), 0);
469 /* The child process should now be ready. */
470 wpid = waitpid(child, &status, WNOHANG);
471 REQUIRE_EQ(wpid, child);
472 ATF_REQUIRE(WIFEXITED(status));
473 REQUIRE_EQ(WEXITSTATUS(status), 1);
477 * Make sure that we can collect the exit status of an orphaned process.
479 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
480 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
483 int cpipe1[2], cpipe2[2], gcpipe[2], status;
486 REQUIRE_EQ(pipe(cpipe1), 0);
487 REQUIRE_EQ(pipe(cpipe2), 0);
488 REQUIRE_EQ(pipe(gcpipe), 0);
490 REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
492 ATF_REQUIRE((child = fork()) != -1);
494 CHILD_REQUIRE((gchild = fork()) != -1);
498 n = read(gcpipe[0], &status, sizeof(status));
499 } while (n == -1 && errno == EINTR);
503 CHILD_REQUIRE_EQ(write(cpipe1[1], &gchild, sizeof(gchild)),
504 (ssize_t)sizeof(gchild));
505 CHILD_REQUIRE_EQ(read(cpipe2[0], &status, sizeof(status)),
506 (ssize_t)sizeof(status));
510 REQUIRE_EQ(read(cpipe1[0], &gchild, sizeof(gchild)),
511 (ssize_t)sizeof(gchild));
513 REQUIRE_EQ(ptrace(PT_ATTACH, gchild, NULL, 0), 0);
516 REQUIRE_EQ(write(cpipe2[1], &status, sizeof(status)),
517 (ssize_t)sizeof(status));
518 REQUIRE_EQ(waitpid(child, &status, 0), child);
519 ATF_REQUIRE(WIFEXITED(status));
520 REQUIRE_EQ(WEXITSTATUS(status), 0);
523 REQUIRE_EQ(write(gcpipe[1], &status, sizeof(status)),
524 (ssize_t)sizeof(status));
525 REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
526 ATF_REQUIRE(WIFSTOPPED(status));
527 REQUIRE_EQ(ptrace(PT_DETACH, gchild, (caddr_t)1, 0), 0);
528 REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
529 ATF_REQUIRE(WIFEXITED(status));
530 REQUIRE_EQ(WEXITSTATUS(status), 0);
532 REQUIRE_EQ(close(cpipe1[0]), 0);
533 REQUIRE_EQ(close(cpipe1[1]), 0);
534 REQUIRE_EQ(close(cpipe2[0]), 0);
535 REQUIRE_EQ(close(cpipe2[1]), 0);
536 REQUIRE_EQ(close(gcpipe[0]), 0);
537 REQUIRE_EQ(close(gcpipe[1]), 0);
541 * The parent process should always act the same regardless of how the
542 * debugger is attached to it.
545 follow_fork_parent(bool use_vfork)
551 CHILD_REQUIRE((fpid = vfork()) != -1);
553 CHILD_REQUIRE((fpid = fork()) != -1);
559 wpid = waitpid(fpid, &status, 0);
560 CHILD_REQUIRE_EQ(wpid, fpid);
561 CHILD_REQUIRE(WIFEXITED(status));
562 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 2);
568 * Helper routine for follow fork tests. This waits for two stops
569 * that report both "sides" of a fork. It returns the pid of the new
573 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
575 struct ptrace_lwpinfo pl;
576 bool fork_reported[2];
580 fork_reported[0] = false;
581 fork_reported[1] = false;
585 * Each process should report a fork event. The parent should
586 * report a PL_FLAG_FORKED event, and the child should report
587 * a PL_FLAG_CHILD event.
589 for (i = 0; i < 2; i++) {
590 wpid = wait(&status);
591 ATF_REQUIRE(wpid > 0);
592 ATF_REQUIRE(WIFSTOPPED(status));
594 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
596 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
598 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
599 (PL_FLAG_FORKED | PL_FLAG_CHILD));
600 if (pl.pl_flags & PL_FLAG_CHILD) {
601 ATF_REQUIRE(wpid != parent);
602 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
603 ATF_REQUIRE(!fork_reported[1]);
607 REQUIRE_EQ(child, wpid);
610 fork_reported[1] = true;
612 REQUIRE_EQ(wpid, parent);
613 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
614 ATF_REQUIRE(!fork_reported[0]);
616 child = pl.pl_child_pid;
618 REQUIRE_EQ(child, pl.pl_child_pid);
621 fork_reported[0] = true;
629 * Verify that a new child process is stopped after a followed fork and
630 * that the traced parent sees the exit of the child after the debugger
631 * when both processes remain attached to the debugger.
633 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
634 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
636 pid_t children[2], fpid, wpid;
639 ATF_REQUIRE((fpid = fork()) != -1);
642 follow_fork_parent(false);
645 /* Parent process. */
648 /* The first wait() should report the stop from SIGSTOP. */
649 wpid = waitpid(children[0], &status, 0);
650 REQUIRE_EQ(wpid, children[0]);
651 ATF_REQUIRE(WIFSTOPPED(status));
652 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
654 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
656 /* Continue the child ignoring the SIGSTOP. */
657 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
659 children[1] = handle_fork_events(children[0], NULL);
660 ATF_REQUIRE(children[1] > 0);
662 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
663 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
666 * The child can't exit until the grandchild reports status, so the
667 * grandchild should report its exit first to the debugger.
669 wpid = wait(&status);
670 REQUIRE_EQ(wpid, children[1]);
671 ATF_REQUIRE(WIFEXITED(status));
672 REQUIRE_EQ(WEXITSTATUS(status), 2);
674 wpid = wait(&status);
675 REQUIRE_EQ(wpid, children[0]);
676 ATF_REQUIRE(WIFEXITED(status));
677 REQUIRE_EQ(WEXITSTATUS(status), 1);
679 wpid = wait(&status);
680 REQUIRE_EQ(wpid, -1);
681 REQUIRE_EQ(errno, ECHILD);
685 * Verify that a new child process is stopped after a followed fork
686 * and that the traced parent sees the exit of the child when the new
687 * child process is detached after it reports its fork.
689 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
690 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
692 pid_t children[2], fpid, wpid;
695 ATF_REQUIRE((fpid = fork()) != -1);
698 follow_fork_parent(false);
701 /* Parent process. */
704 /* The first wait() should report the stop from SIGSTOP. */
705 wpid = waitpid(children[0], &status, 0);
706 REQUIRE_EQ(wpid, children[0]);
707 ATF_REQUIRE(WIFSTOPPED(status));
708 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
710 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
712 /* Continue the child ignoring the SIGSTOP. */
713 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
715 children[1] = handle_fork_events(children[0], NULL);
716 ATF_REQUIRE(children[1] > 0);
718 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
719 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
722 * Should not see any status from the grandchild now, only the
725 wpid = wait(&status);
726 REQUIRE_EQ(wpid, children[0]);
727 ATF_REQUIRE(WIFEXITED(status));
728 REQUIRE_EQ(WEXITSTATUS(status), 1);
730 wpid = wait(&status);
731 REQUIRE_EQ(wpid, -1);
732 REQUIRE_EQ(errno, ECHILD);
736 * Verify that a new child process is stopped after a followed fork
737 * and that the traced parent sees the exit of the child when the
738 * traced parent is detached after the fork.
740 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
741 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
743 pid_t children[2], fpid, wpid;
746 ATF_REQUIRE((fpid = fork()) != -1);
749 follow_fork_parent(false);
752 /* Parent process. */
755 /* The first wait() should report the stop from SIGSTOP. */
756 wpid = waitpid(children[0], &status, 0);
757 REQUIRE_EQ(wpid, children[0]);
758 ATF_REQUIRE(WIFSTOPPED(status));
759 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
761 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
763 /* Continue the child ignoring the SIGSTOP. */
764 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
766 children[1] = handle_fork_events(children[0], NULL);
767 ATF_REQUIRE(children[1] > 0);
769 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
770 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
773 * The child can't exit until the grandchild reports status, so the
774 * grandchild should report its exit first to the debugger.
776 * Even though the child process is detached, it is still a
777 * child of the debugger, so it will still report it's exit
778 * after the grandchild.
780 wpid = wait(&status);
781 REQUIRE_EQ(wpid, children[1]);
782 ATF_REQUIRE(WIFEXITED(status));
783 REQUIRE_EQ(WEXITSTATUS(status), 2);
785 wpid = wait(&status);
786 REQUIRE_EQ(wpid, children[0]);
787 ATF_REQUIRE(WIFEXITED(status));
788 REQUIRE_EQ(WEXITSTATUS(status), 1);
790 wpid = wait(&status);
791 REQUIRE_EQ(wpid, -1);
792 REQUIRE_EQ(errno, ECHILD);
796 attach_fork_parent(int cpipe[2])
802 /* Double-fork to disassociate from the debugger. */
803 CHILD_REQUIRE((fpid = fork()) != -1);
807 /* Send the pid of the disassociated child to the debugger. */
809 CHILD_REQUIRE_EQ(write(cpipe[1], &fpid, sizeof(fpid)),
810 (ssize_t)sizeof(fpid));
812 /* Wait for the debugger to attach. */
813 CHILD_REQUIRE_EQ(read(cpipe[1], &fpid, sizeof(fpid)), 0);
817 * Verify that a new child process is stopped after a followed fork and
818 * that the traced parent sees the exit of the child after the debugger
819 * when both processes remain attached to the debugger. In this test
820 * the parent that forks is not a direct child of the debugger.
822 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
823 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
825 pid_t children[2], fpid, wpid;
826 int cpipe[2], status;
828 REQUIRE_EQ(pipe(cpipe), 0);
829 ATF_REQUIRE((fpid = fork()) != -1);
831 attach_fork_parent(cpipe);
832 follow_fork_parent(false);
835 /* Parent process. */
838 /* Wait for the direct child to exit. */
839 wpid = waitpid(fpid, &status, 0);
840 REQUIRE_EQ(wpid, fpid);
841 ATF_REQUIRE(WIFEXITED(status));
842 REQUIRE_EQ(WEXITSTATUS(status), 3);
844 /* Read the pid of the fork parent. */
845 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
846 (ssize_t)sizeof(children[0]));
848 /* Attach to the fork parent. */
849 attach_child(children[0]);
851 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
853 /* Continue the fork parent ignoring the SIGSTOP. */
854 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
856 /* Signal the fork parent to continue. */
859 children[1] = handle_fork_events(children[0], NULL);
860 ATF_REQUIRE(children[1] > 0);
862 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
863 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
866 * The fork parent can't exit until the child reports status,
867 * so the child should report its exit first to the debugger.
869 wpid = wait(&status);
870 REQUIRE_EQ(wpid, children[1]);
871 ATF_REQUIRE(WIFEXITED(status));
872 REQUIRE_EQ(WEXITSTATUS(status), 2);
874 wpid = wait(&status);
875 REQUIRE_EQ(wpid, children[0]);
876 ATF_REQUIRE(WIFEXITED(status));
877 REQUIRE_EQ(WEXITSTATUS(status), 1);
879 wpid = wait(&status);
880 REQUIRE_EQ(wpid, -1);
881 REQUIRE_EQ(errno, ECHILD);
885 * Verify that a new child process is stopped after a followed fork
886 * and that the traced parent sees the exit of the child when the new
887 * child process is detached after it reports its fork. In this test
888 * the parent that forks is not a direct child of the debugger.
890 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
891 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
893 pid_t children[2], fpid, wpid;
894 int cpipe[2], status;
896 REQUIRE_EQ(pipe(cpipe), 0);
897 ATF_REQUIRE((fpid = fork()) != -1);
899 attach_fork_parent(cpipe);
900 follow_fork_parent(false);
903 /* Parent process. */
906 /* Wait for the direct child to exit. */
907 wpid = waitpid(fpid, &status, 0);
908 REQUIRE_EQ(wpid, fpid);
909 ATF_REQUIRE(WIFEXITED(status));
910 REQUIRE_EQ(WEXITSTATUS(status), 3);
912 /* Read the pid of the fork parent. */
913 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
914 (ssize_t)sizeof(children[0]));
916 /* Attach to the fork parent. */
917 attach_child(children[0]);
919 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
921 /* Continue the fork parent ignoring the SIGSTOP. */
922 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
924 /* Signal the fork parent to continue. */
927 children[1] = handle_fork_events(children[0], NULL);
928 ATF_REQUIRE(children[1] > 0);
930 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
931 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
934 * Should not see any status from the child now, only the fork
937 wpid = wait(&status);
938 REQUIRE_EQ(wpid, children[0]);
939 ATF_REQUIRE(WIFEXITED(status));
940 REQUIRE_EQ(WEXITSTATUS(status), 1);
942 wpid = wait(&status);
943 REQUIRE_EQ(wpid, -1);
944 REQUIRE_EQ(errno, ECHILD);
948 * Verify that a new child process is stopped after a followed fork
949 * and that the traced parent sees the exit of the child when the
950 * traced parent is detached after the fork. In this test the parent
951 * that forks is not a direct child of the debugger.
953 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
954 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
956 pid_t children[2], fpid, wpid;
957 int cpipe[2], status;
959 REQUIRE_EQ(pipe(cpipe), 0);
960 ATF_REQUIRE((fpid = fork()) != -1);
962 attach_fork_parent(cpipe);
963 follow_fork_parent(false);
966 /* Parent process. */
969 /* Wait for the direct child to exit. */
970 wpid = waitpid(fpid, &status, 0);
971 REQUIRE_EQ(wpid, fpid);
972 ATF_REQUIRE(WIFEXITED(status));
973 REQUIRE_EQ(WEXITSTATUS(status), 3);
975 /* Read the pid of the fork parent. */
976 REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
977 (ssize_t)sizeof(children[0]));
979 /* Attach to the fork parent. */
980 attach_child(children[0]);
982 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
984 /* Continue the fork parent ignoring the SIGSTOP. */
985 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
987 /* Signal the fork parent to continue. */
990 children[1] = handle_fork_events(children[0], NULL);
991 ATF_REQUIRE(children[1] > 0);
993 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
994 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
997 * Should not see any status from the fork parent now, only
1000 wpid = wait(&status);
1001 REQUIRE_EQ(wpid, children[1]);
1002 ATF_REQUIRE(WIFEXITED(status));
1003 REQUIRE_EQ(WEXITSTATUS(status), 2);
1005 wpid = wait(&status);
1006 REQUIRE_EQ(wpid, -1);
1007 REQUIRE_EQ(errno, ECHILD);
1011 * Verify that a child process does not see an unrelated debugger as its
1012 * parent but sees its original parent process.
1014 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1015 ATF_TC_BODY(ptrace__getppid, tc)
1017 pid_t child, debugger, ppid, wpid;
1018 int cpipe[2], dpipe[2], status;
1021 REQUIRE_EQ(pipe(cpipe), 0);
1022 ATF_REQUIRE((child = fork()) != -1);
1025 /* Child process. */
1028 /* Wait for parent to be ready. */
1029 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)),
1030 (ssize_t)sizeof(c));
1032 /* Report the parent PID to the parent. */
1034 CHILD_REQUIRE_EQ(write(cpipe[1], &ppid, sizeof(ppid)),
1035 (ssize_t)sizeof(ppid));
1041 REQUIRE_EQ(pipe(dpipe), 0);
1042 ATF_REQUIRE((debugger = fork()) != -1);
1044 if (debugger == 0) {
1045 /* Debugger process. */
1048 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1050 wpid = waitpid(child, &status, 0);
1051 CHILD_REQUIRE_EQ(wpid, child);
1052 CHILD_REQUIRE(WIFSTOPPED(status));
1053 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1055 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1057 /* Signal parent that debugger is attached. */
1058 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)),
1059 (ssize_t)sizeof(c));
1061 /* Wait for traced child to exit. */
1062 wpid = waitpid(child, &status, 0);
1063 CHILD_REQUIRE_EQ(wpid, child);
1064 CHILD_REQUIRE(WIFEXITED(status));
1065 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
1071 /* Parent process. */
1073 /* Wait for the debugger to attach to the child. */
1074 REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
1076 /* Release the child. */
1077 REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), (ssize_t)sizeof(c));
1079 /* Read the parent PID from the child. */
1080 REQUIRE_EQ(read(cpipe[0], &ppid, sizeof(ppid)), (ssize_t)sizeof(ppid));
1083 REQUIRE_EQ(ppid, getpid());
1085 /* Wait for the debugger. */
1086 wpid = waitpid(debugger, &status, 0);
1087 REQUIRE_EQ(wpid, debugger);
1088 ATF_REQUIRE(WIFEXITED(status));
1089 REQUIRE_EQ(WEXITSTATUS(status), 0);
1091 /* The child process should now be ready. */
1092 wpid = waitpid(child, &status, WNOHANG);
1093 REQUIRE_EQ(wpid, child);
1094 ATF_REQUIRE(WIFEXITED(status));
1095 REQUIRE_EQ(WEXITSTATUS(status), 1);
1099 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1100 * child process created via fork() reports the correct value.
1102 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1103 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1105 struct ptrace_lwpinfo pl[2];
1106 pid_t children[2], fpid, wpid;
1109 ATF_REQUIRE((fpid = fork()) != -1);
1112 follow_fork_parent(false);
1115 /* Parent process. */
1118 /* The first wait() should report the stop from SIGSTOP. */
1119 wpid = waitpid(children[0], &status, 0);
1120 REQUIRE_EQ(wpid, children[0]);
1121 ATF_REQUIRE(WIFSTOPPED(status));
1122 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1124 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1126 /* Continue the child ignoring the SIGSTOP. */
1127 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1129 /* Wait for both halves of the fork event to get reported. */
1130 children[1] = handle_fork_events(children[0], pl);
1131 ATF_REQUIRE(children[1] > 0);
1133 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1134 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1135 REQUIRE_EQ(pl[0].pl_syscall_code, (unsigned)SYS_fork);
1136 REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1137 REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1139 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1140 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1143 * The child can't exit until the grandchild reports status, so the
1144 * grandchild should report its exit first to the debugger.
1146 wpid = wait(&status);
1147 REQUIRE_EQ(wpid, children[1]);
1148 ATF_REQUIRE(WIFEXITED(status));
1149 REQUIRE_EQ(WEXITSTATUS(status), 2);
1151 wpid = wait(&status);
1152 REQUIRE_EQ(wpid, children[0]);
1153 ATF_REQUIRE(WIFEXITED(status));
1154 REQUIRE_EQ(WEXITSTATUS(status), 1);
1156 wpid = wait(&status);
1157 REQUIRE_EQ(wpid, -1);
1158 REQUIRE_EQ(errno, ECHILD);
1162 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1163 * child process created via vfork() reports the correct value.
1165 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1166 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1168 struct ptrace_lwpinfo pl[2];
1169 pid_t children[2], fpid, wpid;
1172 ATF_REQUIRE((fpid = fork()) != -1);
1175 follow_fork_parent(true);
1178 /* Parent process. */
1181 /* The first wait() should report the stop from SIGSTOP. */
1182 wpid = waitpid(children[0], &status, 0);
1183 REQUIRE_EQ(wpid, children[0]);
1184 ATF_REQUIRE(WIFSTOPPED(status));
1185 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1187 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1189 /* Continue the child ignoring the SIGSTOP. */
1190 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1192 /* Wait for both halves of the fork event to get reported. */
1193 children[1] = handle_fork_events(children[0], pl);
1194 ATF_REQUIRE(children[1] > 0);
1196 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1197 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1198 REQUIRE_EQ(pl[0].pl_syscall_code, (unsigned)SYS_vfork);
1199 REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1200 REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1202 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1203 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1206 * The child can't exit until the grandchild reports status, so the
1207 * grandchild should report its exit first to the debugger.
1209 wpid = wait(&status);
1210 REQUIRE_EQ(wpid, children[1]);
1211 ATF_REQUIRE(WIFEXITED(status));
1212 REQUIRE_EQ(WEXITSTATUS(status), 2);
1214 wpid = wait(&status);
1215 REQUIRE_EQ(wpid, children[0]);
1216 ATF_REQUIRE(WIFEXITED(status));
1217 REQUIRE_EQ(WEXITSTATUS(status), 1);
1219 wpid = wait(&status);
1220 REQUIRE_EQ(wpid, -1);
1221 REQUIRE_EQ(errno, ECHILD);
1225 simple_thread(void *arg __unused)
1232 simple_thread_main(void)
1236 CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, simple_thread, NULL), 0);
1237 CHILD_REQUIRE_EQ(pthread_join(thread, NULL), 0);
1242 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1243 * thread reports the correct value.
1245 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1246 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1248 struct ptrace_lwpinfo pl;
1253 ATF_REQUIRE((fpid = fork()) != -1);
1256 simple_thread_main();
1259 /* The first wait() should report the stop from SIGSTOP. */
1260 wpid = waitpid(fpid, &status, 0);
1261 REQUIRE_EQ(wpid, fpid);
1262 ATF_REQUIRE(WIFSTOPPED(status));
1263 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1265 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1267 mainlwp = pl.pl_lwpid;
1270 * Continue the child ignoring the SIGSTOP and tracing all
1271 * system call exits.
1273 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1276 * Wait for the new thread to arrive. pthread_create() might
1277 * invoke any number of system calls. For now we just wait
1278 * for the new thread to arrive and make sure it reports a
1279 * valid system call code. If ptrace grows thread event
1280 * reporting then this test can be made more precise.
1283 wpid = waitpid(fpid, &status, 0);
1284 REQUIRE_EQ(wpid, fpid);
1285 ATF_REQUIRE(WIFSTOPPED(status));
1286 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1288 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1290 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1291 ATF_REQUIRE(pl.pl_syscall_code != 0);
1292 if (pl.pl_lwpid != mainlwp)
1293 /* New thread seen. */
1296 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1299 /* Wait for the child to exit. */
1300 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1302 wpid = waitpid(fpid, &status, 0);
1303 REQUIRE_EQ(wpid, fpid);
1304 if (WIFEXITED(status))
1307 ATF_REQUIRE(WIFSTOPPED(status));
1308 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1309 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1312 REQUIRE_EQ(WEXITSTATUS(status), 1);
1314 wpid = wait(&status);
1315 REQUIRE_EQ(wpid, -1);
1316 REQUIRE_EQ(errno, ECHILD);
1320 * Verify that the expected LWP events are reported for a child thread.
1322 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1323 ATF_TC_BODY(ptrace__lwp_events, tc)
1325 struct ptrace_lwpinfo pl;
1330 ATF_REQUIRE((fpid = fork()) != -1);
1333 simple_thread_main();
1336 /* The first wait() should report the stop from SIGSTOP. */
1337 wpid = waitpid(fpid, &status, 0);
1338 REQUIRE_EQ(wpid, fpid);
1339 ATF_REQUIRE(WIFSTOPPED(status));
1340 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1342 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1344 lwps[0] = pl.pl_lwpid;
1346 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1348 /* Continue the child ignoring the SIGSTOP. */
1349 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1351 /* The first event should be for the child thread's birth. */
1352 wpid = waitpid(fpid, &status, 0);
1353 REQUIRE_EQ(wpid, fpid);
1354 ATF_REQUIRE(WIFSTOPPED(status));
1355 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1357 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1358 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1359 (PL_FLAG_BORN | PL_FLAG_SCX));
1360 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1361 lwps[1] = pl.pl_lwpid;
1363 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1365 /* The next event should be for the child thread's death. */
1366 wpid = waitpid(fpid, &status, 0);
1367 REQUIRE_EQ(wpid, fpid);
1368 ATF_REQUIRE(WIFSTOPPED(status));
1369 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1371 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1372 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1373 (PL_FLAG_EXITED | PL_FLAG_SCE));
1374 REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1376 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1378 /* The last event should be for the child process's exit. */
1379 wpid = waitpid(fpid, &status, 0);
1380 ATF_REQUIRE(WIFEXITED(status));
1381 REQUIRE_EQ(WEXITSTATUS(status), 1);
1383 wpid = wait(&status);
1384 REQUIRE_EQ(wpid, -1);
1385 REQUIRE_EQ(errno, ECHILD);
1389 exec_thread(void *arg __unused)
1392 execl("/usr/bin/true", "true", NULL);
1397 exec_thread_main(void)
1401 CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, exec_thread, NULL), 0);
1408 * Verify that the expected LWP events are reported for a multithreaded
1409 * process that calls execve(2).
1411 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1412 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1414 struct ptrace_lwpinfo pl;
1419 ATF_REQUIRE((fpid = fork()) != -1);
1425 /* The first wait() should report the stop from SIGSTOP. */
1426 wpid = waitpid(fpid, &status, 0);
1427 REQUIRE_EQ(wpid, fpid);
1428 ATF_REQUIRE(WIFSTOPPED(status));
1429 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1431 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1433 lwps[0] = pl.pl_lwpid;
1435 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1437 /* Continue the child ignoring the SIGSTOP. */
1438 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1440 /* The first event should be for the child thread's birth. */
1441 wpid = waitpid(fpid, &status, 0);
1442 REQUIRE_EQ(wpid, fpid);
1443 ATF_REQUIRE(WIFSTOPPED(status));
1444 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1446 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1447 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1448 (PL_FLAG_BORN | PL_FLAG_SCX));
1449 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1450 lwps[1] = pl.pl_lwpid;
1452 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1455 * The next event should be for the main thread's death due to
1456 * single threading from execve().
1458 wpid = waitpid(fpid, &status, 0);
1459 REQUIRE_EQ(wpid, fpid);
1460 ATF_REQUIRE(WIFSTOPPED(status));
1461 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1463 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1464 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1466 REQUIRE_EQ(pl.pl_lwpid, lwps[0]);
1468 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1470 /* The next event should be for the child process's exec. */
1471 wpid = waitpid(fpid, &status, 0);
1472 ATF_REQUIRE(WIFSTOPPED(status));
1473 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1475 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1476 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1477 (PL_FLAG_EXEC | PL_FLAG_SCX));
1478 REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1480 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1482 /* The last event should be for the child process's exit. */
1483 wpid = waitpid(fpid, &status, 0);
1484 ATF_REQUIRE(WIFEXITED(status));
1485 REQUIRE_EQ(WEXITSTATUS(status), 0);
1487 wpid = wait(&status);
1488 REQUIRE_EQ(wpid, -1);
1489 REQUIRE_EQ(errno, ECHILD);
1493 handler(int sig __unused)
1501 signal(SIGINFO, handler);
1507 * Verify that the expected ptrace event is reported for a signal.
1509 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1510 ATF_TC_BODY(ptrace__siginfo, tc)
1512 struct ptrace_lwpinfo pl;
1516 ATF_REQUIRE((fpid = fork()) != -1);
1522 /* The first wait() should report the stop from SIGSTOP. */
1523 wpid = waitpid(fpid, &status, 0);
1524 REQUIRE_EQ(wpid, fpid);
1525 ATF_REQUIRE(WIFSTOPPED(status));
1526 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1528 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1530 /* The next event should be for the SIGINFO. */
1531 wpid = waitpid(fpid, &status, 0);
1532 ATF_REQUIRE(WIFSTOPPED(status));
1533 REQUIRE_EQ(WSTOPSIG(status), SIGINFO);
1535 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1536 REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
1537 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1538 REQUIRE_EQ(pl.pl_siginfo.si_code, SI_LWP);
1539 REQUIRE_EQ(pl.pl_siginfo.si_pid, wpid);
1541 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1543 /* The last event should be for the child process's exit. */
1544 wpid = waitpid(fpid, &status, 0);
1545 ATF_REQUIRE(WIFEXITED(status));
1546 REQUIRE_EQ(WEXITSTATUS(status), 0);
1548 wpid = wait(&status);
1549 REQUIRE_EQ(wpid, -1);
1550 REQUIRE_EQ(errno, ECHILD);
1554 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1556 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1557 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1562 ATF_REQUIRE((fpid = fork()) != -1);
1568 /* The first wait() should report the stop from SIGSTOP. */
1569 wpid = waitpid(fpid, &status, 0);
1570 REQUIRE_EQ(wpid, fpid);
1571 ATF_REQUIRE(WIFSTOPPED(status));
1572 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1575 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1576 sizeof(events)) == 0);
1578 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1580 /* Should get one event at exit. */
1581 wpid = waitpid(fpid, &status, 0);
1582 ATF_REQUIRE(WIFEXITED(status));
1583 REQUIRE_EQ(WEXITSTATUS(status), 0);
1585 wpid = wait(&status);
1586 REQUIRE_EQ(wpid, -1);
1587 REQUIRE_EQ(errno, ECHILD);
1590 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1591 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1593 struct ptrace_lwpinfo pl;
1597 ATF_REQUIRE((fpid = fork()) != -1);
1603 /* The first wait() should report the stop from SIGSTOP. */
1604 wpid = waitpid(fpid, &status, 0);
1605 REQUIRE_EQ(wpid, fpid);
1606 ATF_REQUIRE(WIFSTOPPED(status));
1607 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1609 events = PTRACE_EXEC;
1610 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1611 sizeof(events)) == 0);
1613 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1615 /* The next event should be for the child process's exec. */
1616 wpid = waitpid(fpid, &status, 0);
1617 ATF_REQUIRE(WIFSTOPPED(status));
1618 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1620 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1621 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1622 (PL_FLAG_EXEC | PL_FLAG_SCX));
1624 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1626 /* The last event should be for the child process's exit. */
1627 wpid = waitpid(fpid, &status, 0);
1628 ATF_REQUIRE(WIFEXITED(status));
1629 REQUIRE_EQ(WEXITSTATUS(status), 0);
1631 wpid = wait(&status);
1632 REQUIRE_EQ(wpid, -1);
1633 REQUIRE_EQ(errno, ECHILD);
1636 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1637 ATF_TC_BODY(ptrace__event_mask, tc)
1642 ATF_REQUIRE((fpid = fork()) != -1);
1648 /* The first wait() should report the stop from SIGSTOP. */
1649 wpid = waitpid(fpid, &status, 0);
1650 REQUIRE_EQ(wpid, fpid);
1651 ATF_REQUIRE(WIFSTOPPED(status));
1652 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1654 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1655 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1656 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1657 sizeof(events)) == 0);
1658 ATF_REQUIRE(events & PTRACE_FORK);
1659 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1660 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1661 sizeof(events)) == 0);
1662 ATF_REQUIRE(!(events & PTRACE_FORK));
1664 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1665 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1666 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1667 sizeof(events)) == 0);
1668 ATF_REQUIRE(events & PTRACE_LWP);
1669 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1670 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1671 sizeof(events)) == 0);
1672 ATF_REQUIRE(!(events & PTRACE_LWP));
1674 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1676 /* Should get one event at exit. */
1677 wpid = waitpid(fpid, &status, 0);
1678 ATF_REQUIRE(WIFEXITED(status));
1679 REQUIRE_EQ(WEXITSTATUS(status), 0);
1681 wpid = wait(&status);
1682 REQUIRE_EQ(wpid, -1);
1683 REQUIRE_EQ(errno, ECHILD);
1687 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1689 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1690 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1692 struct ptrace_lwpinfo pl;
1696 ATF_REQUIRE((fpid = fork()) != -1);
1699 follow_fork_parent(true);
1702 /* The first wait() should report the stop from SIGSTOP. */
1703 wpid = waitpid(fpid, &status, 0);
1704 REQUIRE_EQ(wpid, fpid);
1705 ATF_REQUIRE(WIFSTOPPED(status));
1706 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1708 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1709 sizeof(events)) == 0);
1710 events |= PTRACE_VFORK;
1711 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1712 sizeof(events)) == 0);
1714 /* Continue the child ignoring the SIGSTOP. */
1715 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1717 /* The next event should report the end of the vfork. */
1718 wpid = wait(&status);
1719 REQUIRE_EQ(wpid, fpid);
1720 ATF_REQUIRE(WIFSTOPPED(status));
1721 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1722 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1723 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1725 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1727 wpid = wait(&status);
1728 REQUIRE_EQ(wpid, fpid);
1729 ATF_REQUIRE(WIFEXITED(status));
1730 REQUIRE_EQ(WEXITSTATUS(status), 1);
1732 wpid = wait(&status);
1733 REQUIRE_EQ(wpid, -1);
1734 REQUIRE_EQ(errno, ECHILD);
1737 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1738 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1740 struct ptrace_lwpinfo pl[2];
1741 pid_t children[2], fpid, wpid;
1744 ATF_REQUIRE((fpid = fork()) != -1);
1747 follow_fork_parent(true);
1750 /* Parent process. */
1753 /* The first wait() should report the stop from SIGSTOP. */
1754 wpid = waitpid(children[0], &status, 0);
1755 REQUIRE_EQ(wpid, children[0]);
1756 ATF_REQUIRE(WIFSTOPPED(status));
1757 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1759 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1760 sizeof(events)) == 0);
1761 events |= PTRACE_FORK | PTRACE_VFORK;
1762 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1763 sizeof(events)) == 0);
1765 /* Continue the child ignoring the SIGSTOP. */
1766 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1768 /* Wait for both halves of the fork event to get reported. */
1769 children[1] = handle_fork_events(children[0], pl);
1770 ATF_REQUIRE(children[1] > 0);
1772 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1774 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1775 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1778 * The child can't exit until the grandchild reports status, so the
1779 * grandchild should report its exit first to the debugger.
1781 wpid = waitpid(children[1], &status, 0);
1782 REQUIRE_EQ(wpid, children[1]);
1783 ATF_REQUIRE(WIFEXITED(status));
1784 REQUIRE_EQ(WEXITSTATUS(status), 2);
1787 * The child should report it's vfork() completion before it
1790 wpid = wait(&status);
1791 REQUIRE_EQ(wpid, children[0]);
1792 ATF_REQUIRE(WIFSTOPPED(status));
1793 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1794 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1796 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1798 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1800 wpid = wait(&status);
1801 REQUIRE_EQ(wpid, children[0]);
1802 ATF_REQUIRE(WIFEXITED(status));
1803 REQUIRE_EQ(WEXITSTATUS(status), 1);
1805 wpid = wait(&status);
1806 REQUIRE_EQ(wpid, -1);
1807 REQUIRE_EQ(errno, ECHILD);
1810 #ifdef HAVE_BREAKPOINT
1812 * Verify that no more events are reported after PT_KILL except for the
1813 * process exit when stopped due to a breakpoint trap.
1815 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1816 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1821 ATF_REQUIRE((fpid = fork()) != -1);
1828 /* The first wait() should report the stop from SIGSTOP. */
1829 wpid = waitpid(fpid, &status, 0);
1830 REQUIRE_EQ(wpid, fpid);
1831 ATF_REQUIRE(WIFSTOPPED(status));
1832 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1834 /* Continue the child ignoring the SIGSTOP. */
1835 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1837 /* The second wait() should report hitting the breakpoint. */
1838 wpid = waitpid(fpid, &status, 0);
1839 REQUIRE_EQ(wpid, fpid);
1840 ATF_REQUIRE(WIFSTOPPED(status));
1841 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1843 /* Kill the child process. */
1844 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1846 /* The last wait() should report the SIGKILL. */
1847 wpid = waitpid(fpid, &status, 0);
1848 REQUIRE_EQ(wpid, fpid);
1849 ATF_REQUIRE(WIFSIGNALED(status));
1850 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1852 wpid = wait(&status);
1853 REQUIRE_EQ(wpid, -1);
1854 REQUIRE_EQ(errno, ECHILD);
1856 #endif /* HAVE_BREAKPOINT */
1859 * Verify that no more events are reported after PT_KILL except for the
1860 * process exit when stopped inside of a system call.
1862 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1863 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1865 struct ptrace_lwpinfo pl;
1869 ATF_REQUIRE((fpid = fork()) != -1);
1876 /* The first wait() should report the stop from SIGSTOP. */
1877 wpid = waitpid(fpid, &status, 0);
1878 REQUIRE_EQ(wpid, fpid);
1879 ATF_REQUIRE(WIFSTOPPED(status));
1880 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1882 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1883 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
1885 /* The second wait() should report a system call entry for getpid(). */
1886 wpid = waitpid(fpid, &status, 0);
1887 REQUIRE_EQ(wpid, fpid);
1888 ATF_REQUIRE(WIFSTOPPED(status));
1889 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1891 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1892 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1894 /* Kill the child process. */
1895 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1897 /* The last wait() should report the SIGKILL. */
1898 wpid = waitpid(fpid, &status, 0);
1899 REQUIRE_EQ(wpid, fpid);
1900 ATF_REQUIRE(WIFSIGNALED(status));
1901 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1903 wpid = wait(&status);
1904 REQUIRE_EQ(wpid, -1);
1905 REQUIRE_EQ(errno, ECHILD);
1909 * Verify that no more events are reported after PT_KILL except for the
1910 * process exit when killing a multithreaded process.
1912 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1913 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1915 struct ptrace_lwpinfo pl;
1920 ATF_REQUIRE((fpid = fork()) != -1);
1923 simple_thread_main();
1926 /* The first wait() should report the stop from SIGSTOP. */
1927 wpid = waitpid(fpid, &status, 0);
1928 REQUIRE_EQ(wpid, fpid);
1929 ATF_REQUIRE(WIFSTOPPED(status));
1930 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1932 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1934 main_lwp = pl.pl_lwpid;
1936 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1938 /* Continue the child ignoring the SIGSTOP. */
1939 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1941 /* The first event should be for the child thread's birth. */
1942 wpid = waitpid(fpid, &status, 0);
1943 REQUIRE_EQ(wpid, fpid);
1944 ATF_REQUIRE(WIFSTOPPED(status));
1945 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1947 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1948 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1949 (PL_FLAG_BORN | PL_FLAG_SCX));
1950 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1952 /* Kill the child process. */
1953 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1955 /* The last wait() should report the SIGKILL. */
1956 wpid = waitpid(fpid, &status, 0);
1957 REQUIRE_EQ(wpid, fpid);
1958 ATF_REQUIRE(WIFSIGNALED(status));
1959 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1961 wpid = wait(&status);
1962 REQUIRE_EQ(wpid, -1);
1963 REQUIRE_EQ(errno, ECHILD);
1967 mask_usr1_thread(void *arg)
1969 pthread_barrier_t *pbarrier;
1972 pbarrier = (pthread_barrier_t*)arg;
1974 sigemptyset(&sigmask);
1975 sigaddset(&sigmask, SIGUSR1);
1976 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
1978 /* Sync up with other thread after sigmask updated. */
1979 pthread_barrier_wait(pbarrier);
1988 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1989 * and prevents spurious stops due to those other signals.
1991 ATF_TC(ptrace__PT_KILL_competing_signal);
1992 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1995 atf_tc_set_md_var(tc, "require.user", "root");
1997 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
2003 pthread_barrier_t barrier;
2004 struct sched_param sched_param;
2006 ATF_REQUIRE((fpid = fork()) != -1);
2008 /* Bind to one CPU so only one thread at a time will run. */
2010 CPU_SET(0, &setmask);
2012 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2013 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2014 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2016 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2018 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2019 (void*)&barrier) == 0);
2022 * Give the main thread higher priority. The test always
2023 * assumes that, if both threads are able to run, the main
2024 * thread runs first.
2026 sched_param.sched_priority =
2027 (sched_get_priority_max(SCHED_FIFO) +
2028 sched_get_priority_min(SCHED_FIFO)) / 2;
2029 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2030 SCHED_FIFO, &sched_param) == 0);
2031 sched_param.sched_priority -= RQ_PPQ;
2032 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2033 &sched_param) == 0);
2036 sigemptyset(&sigmask);
2037 sigaddset(&sigmask, SIGUSR2);
2038 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2040 /* Sync up with other thread after sigmask updated. */
2041 pthread_barrier_wait(&barrier);
2051 /* The first wait() should report the stop from SIGSTOP. */
2052 wpid = waitpid(fpid, &status, 0);
2053 REQUIRE_EQ(wpid, fpid);
2054 ATF_REQUIRE(WIFSTOPPED(status));
2055 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2057 /* Continue the child ignoring the SIGSTOP. */
2058 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2060 /* Send a signal that only the second thread can handle. */
2061 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2063 /* The second wait() should report the SIGUSR2. */
2064 wpid = waitpid(fpid, &status, 0);
2065 REQUIRE_EQ(wpid, fpid);
2066 ATF_REQUIRE(WIFSTOPPED(status));
2067 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2069 /* Send a signal that only the first thread can handle. */
2070 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2072 /* Replace the SIGUSR2 with a kill. */
2073 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2075 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2076 wpid = waitpid(fpid, &status, 0);
2077 REQUIRE_EQ(wpid, fpid);
2078 ATF_REQUIRE(WIFSIGNALED(status));
2079 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2081 wpid = wait(&status);
2082 REQUIRE_EQ(wpid, -1);
2083 REQUIRE_EQ(errno, ECHILD);
2087 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2088 * and prevents spurious stops caused by those events.
2090 ATF_TC(ptrace__PT_KILL_competing_stop);
2091 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2094 atf_tc_set_md_var(tc, "require.user", "root");
2096 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2102 pthread_barrier_t barrier;
2104 struct ptrace_lwpinfo pl;
2105 struct sched_param sched_param;
2107 ATF_REQUIRE((fpid = fork()) != -1);
2111 /* Bind to one CPU so only one thread at a time will run. */
2113 CPU_SET(0, &setmask);
2115 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2116 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2117 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2119 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2121 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2122 (void*)&barrier) == 0);
2125 * Give the main thread higher priority. The test always
2126 * assumes that, if both threads are able to run, the main
2127 * thread runs first.
2129 sched_param.sched_priority =
2130 (sched_get_priority_max(SCHED_FIFO) +
2131 sched_get_priority_min(SCHED_FIFO)) / 2;
2132 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2133 SCHED_FIFO, &sched_param) == 0);
2134 sched_param.sched_priority -= RQ_PPQ;
2135 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2136 &sched_param) == 0);
2139 sigemptyset(&sigmask);
2140 sigaddset(&sigmask, SIGUSR2);
2141 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2143 /* Sync up with other thread after sigmask updated. */
2144 pthread_barrier_wait(&barrier);
2146 /* Sync up with the test before doing the getpid(). */
2153 /* The first wait() should report the stop from SIGSTOP. */
2154 wpid = waitpid(fpid, &status, 0);
2155 REQUIRE_EQ(wpid, fpid);
2156 ATF_REQUIRE(WIFSTOPPED(status));
2157 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2159 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2160 main_lwp = pl.pl_lwpid;
2162 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2163 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2166 * Continue until child is done with setup, which is indicated with
2167 * SIGSTOP. Ignore system calls in the meantime.
2170 wpid = waitpid(fpid, &status, 0);
2171 REQUIRE_EQ(wpid, fpid);
2172 ATF_REQUIRE(WIFSTOPPED(status));
2173 if (WSTOPSIG(status) == SIGTRAP) {
2174 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2176 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2178 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2181 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2184 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2185 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2187 wpid = waitpid(fpid, &status, 0);
2188 REQUIRE_EQ(wpid, fpid);
2189 ATF_REQUIRE(WIFSTOPPED(status));
2190 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2192 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2194 REQUIRE_EQ(pl.pl_lwpid, main_lwp);
2195 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2196 /* Prevent the main thread from hitting its syscall exit for now. */
2197 REQUIRE_EQ(ptrace(PT_SUSPEND, main_lwp, 0, 0), 0);
2200 * Proceed, allowing second thread to hit syscall exit for
2201 * pthread_barrier_wait().
2203 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2205 wpid = waitpid(fpid, &status, 0);
2206 REQUIRE_EQ(wpid, fpid);
2207 ATF_REQUIRE(WIFSTOPPED(status));
2208 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2210 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2212 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2213 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2215 /* Send a signal that only the second thread can handle. */
2216 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2218 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2220 /* The next wait() should report the SIGUSR2. */
2221 wpid = waitpid(fpid, &status, 0);
2222 REQUIRE_EQ(wpid, fpid);
2223 ATF_REQUIRE(WIFSTOPPED(status));
2224 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2226 /* Allow the main thread to try to finish its system call. */
2227 REQUIRE_EQ(ptrace(PT_RESUME, main_lwp, 0, 0), 0);
2230 * At this point, the main thread is in the middle of a system call and
2231 * has been resumed. The second thread has taken a SIGUSR2 which will
2232 * be replaced with a SIGKILL below. The main thread will get to run
2233 * first. It should notice the kill request (even though the signal
2234 * replacement occurred in the other thread) and exit accordingly. It
2235 * should not stop for the system call exit event.
2238 /* Replace the SIGUSR2 with a kill. */
2239 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2241 /* The last wait() should report the SIGKILL (not a syscall exit). */
2242 wpid = waitpid(fpid, &status, 0);
2243 REQUIRE_EQ(wpid, fpid);
2244 ATF_REQUIRE(WIFSIGNALED(status));
2245 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2247 wpid = wait(&status);
2248 REQUIRE_EQ(wpid, -1);
2249 REQUIRE_EQ(errno, ECHILD);
2253 sigusr1_handler(int sig)
2256 CHILD_REQUIRE_EQ(sig, SIGUSR1);
2261 * Verify that even if the signal queue is full for a child process,
2262 * a PT_KILL will kill the process.
2264 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2265 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2269 int max_pending_per_proc;
2273 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2275 ATF_REQUIRE((fpid = fork()) != -1);
2281 /* The first wait() should report the stop from SIGSTOP. */
2282 wpid = waitpid(fpid, &status, 0);
2283 REQUIRE_EQ(wpid, fpid);
2284 ATF_REQUIRE(WIFSTOPPED(status));
2285 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2287 len = sizeof(max_pending_per_proc);
2288 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2289 &max_pending_per_proc, &len, NULL, 0) == 0);
2291 /* Fill the signal queue. */
2292 for (i = 0; i < max_pending_per_proc; ++i)
2293 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2295 /* Kill the child process. */
2296 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2298 /* The last wait() should report the SIGKILL. */
2299 wpid = waitpid(fpid, &status, 0);
2300 REQUIRE_EQ(wpid, fpid);
2301 ATF_REQUIRE(WIFSIGNALED(status));
2302 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2304 wpid = wait(&status);
2305 REQUIRE_EQ(wpid, -1);
2306 REQUIRE_EQ(errno, ECHILD);
2310 * Verify that when stopped at a system call entry, a signal can be
2311 * requested with PT_CONTINUE which will be delivered once the system
2314 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2315 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2317 struct ptrace_lwpinfo pl;
2321 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2323 ATF_REQUIRE((fpid = fork()) != -1);
2330 /* The first wait() should report the stop from SIGSTOP. */
2331 wpid = waitpid(fpid, &status, 0);
2332 REQUIRE_EQ(wpid, fpid);
2333 ATF_REQUIRE(WIFSTOPPED(status));
2334 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2336 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2337 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2339 /* The second wait() should report a system call entry for getpid(). */
2340 wpid = waitpid(fpid, &status, 0);
2341 REQUIRE_EQ(wpid, fpid);
2342 ATF_REQUIRE(WIFSTOPPED(status));
2343 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2345 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2346 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2348 /* Continue the child process with a signal. */
2349 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2353 * The last wait() should report exit 2, i.e., a normal _exit
2354 * from the signal handler. In the meantime, catch and proceed
2355 * past any syscall stops.
2357 wpid = waitpid(fpid, &status, 0);
2358 REQUIRE_EQ(wpid, fpid);
2359 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2360 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2361 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2362 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2364 ATF_REQUIRE(WIFEXITED(status));
2365 REQUIRE_EQ(WEXITSTATUS(status), 2);
2370 wpid = wait(&status);
2371 REQUIRE_EQ(wpid, -1);
2372 REQUIRE_EQ(errno, ECHILD);
2376 sigusr1_counting_handler(int sig)
2378 static int counter = 0;
2380 CHILD_REQUIRE_EQ(sig, SIGUSR1);
2387 * Verify that, when continuing from a stop at system call entry and exit,
2388 * a signal can be requested from both stops, and both will be delivered when
2389 * the system call is complete.
2391 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2392 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2394 struct ptrace_lwpinfo pl;
2398 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2400 ATF_REQUIRE((fpid = fork()) != -1);
2407 /* The first wait() should report the stop from SIGSTOP. */
2408 wpid = waitpid(fpid, &status, 0);
2409 REQUIRE_EQ(wpid, fpid);
2410 ATF_REQUIRE(WIFSTOPPED(status));
2411 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2413 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2414 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2416 /* The second wait() should report a system call entry for getpid(). */
2417 wpid = waitpid(fpid, &status, 0);
2418 REQUIRE_EQ(wpid, fpid);
2419 ATF_REQUIRE(WIFSTOPPED(status));
2420 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2422 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2423 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2425 /* Continue the child process with a signal. */
2426 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2428 /* The third wait() should report a system call exit for getpid(). */
2429 wpid = waitpid(fpid, &status, 0);
2430 REQUIRE_EQ(wpid, fpid);
2431 ATF_REQUIRE(WIFSTOPPED(status));
2432 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2434 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2435 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2437 /* Continue the child process with a signal. */
2438 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2442 * The last wait() should report exit 2, i.e., a normal _exit
2443 * from the signal handler. In the meantime, catch and proceed
2444 * past any syscall stops.
2446 wpid = waitpid(fpid, &status, 0);
2447 REQUIRE_EQ(wpid, fpid);
2448 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2449 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2450 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2451 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2453 ATF_REQUIRE(WIFEXITED(status));
2454 REQUIRE_EQ(WEXITSTATUS(status), 2);
2459 wpid = wait(&status);
2460 REQUIRE_EQ(wpid, -1);
2461 REQUIRE_EQ(errno, ECHILD);
2465 * Verify that even if the signal queue is full for a child process,
2466 * a PT_CONTINUE with a signal will not result in loss of that signal.
2468 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2469 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2473 int max_pending_per_proc;
2477 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2478 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2480 ATF_REQUIRE((fpid = fork()) != -1);
2486 /* The first wait() should report the stop from SIGSTOP. */
2487 wpid = waitpid(fpid, &status, 0);
2488 REQUIRE_EQ(wpid, fpid);
2489 ATF_REQUIRE(WIFSTOPPED(status));
2490 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2492 len = sizeof(max_pending_per_proc);
2493 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2494 &max_pending_per_proc, &len, NULL, 0) == 0);
2496 /* Fill the signal queue. */
2497 for (i = 0; i < max_pending_per_proc; ++i)
2498 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2500 /* Continue with signal. */
2501 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2504 wpid = waitpid(fpid, &status, 0);
2505 REQUIRE_EQ(wpid, fpid);
2506 if (WIFSTOPPED(status)) {
2507 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2508 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2511 * The last wait() should report normal _exit from the
2514 ATF_REQUIRE(WIFEXITED(status));
2515 REQUIRE_EQ(WEXITSTATUS(status), 2);
2520 wpid = wait(&status);
2521 REQUIRE_EQ(wpid, -1);
2522 REQUIRE_EQ(errno, ECHILD);
2525 static sem_t sigusr1_sem;
2526 static int got_usr1;
2529 sigusr1_sempost_handler(int sig __unused)
2533 CHILD_REQUIRE_EQ(sem_post(&sigusr1_sem), 0);
2537 * Verify that even if the signal queue is full for a child process,
2538 * and the signal is masked, a PT_CONTINUE with a signal will not
2539 * result in loss of that signal.
2541 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2542 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2544 struct ptrace_lwpinfo pl;
2547 int max_pending_per_proc;
2552 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2553 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2554 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2557 ATF_REQUIRE((fpid = fork()) != -1);
2559 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2560 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2561 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2564 CHILD_REQUIRE_EQ(got_usr1, 0);
2566 /* Allow the pending SIGUSR1 in now. */
2567 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2568 /* Wait to receive the SIGUSR1. */
2570 err = sem_wait(&sigusr1_sem);
2571 CHILD_REQUIRE(err == 0 || errno == EINTR);
2572 } while (err != 0 && errno == EINTR);
2573 CHILD_REQUIRE_EQ(got_usr1, 1);
2577 /* The first wait() should report the stop from SIGSTOP. */
2578 wpid = waitpid(fpid, &status, 0);
2579 REQUIRE_EQ(wpid, fpid);
2580 ATF_REQUIRE(WIFSTOPPED(status));
2581 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2583 len = sizeof(max_pending_per_proc);
2584 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2585 &max_pending_per_proc, &len, NULL, 0) == 0);
2587 /* Fill the signal queue. */
2588 for (i = 0; i < max_pending_per_proc; ++i)
2589 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2591 /* Continue with signal. */
2592 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2594 /* Collect and ignore all of the SIGUSR2. */
2595 for (i = 0; i < max_pending_per_proc; ++i) {
2596 wpid = waitpid(fpid, &status, 0);
2597 REQUIRE_EQ(wpid, fpid);
2598 ATF_REQUIRE(WIFSTOPPED(status));
2599 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2600 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2603 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2604 wpid = waitpid(fpid, &status, 0);
2605 REQUIRE_EQ(wpid, fpid);
2606 ATF_REQUIRE(WIFSTOPPED(status));
2607 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
2608 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2609 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
2611 /* Continue the child, ignoring the SIGUSR1. */
2612 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2614 /* The last wait() should report exit after receiving SIGUSR1. */
2615 wpid = waitpid(fpid, &status, 0);
2616 REQUIRE_EQ(wpid, fpid);
2617 ATF_REQUIRE(WIFEXITED(status));
2618 REQUIRE_EQ(WEXITSTATUS(status), 1);
2620 wpid = wait(&status);
2621 REQUIRE_EQ(wpid, -1);
2622 REQUIRE_EQ(errno, ECHILD);
2626 * Verify that, after stopping due to a signal, that signal can be
2627 * replaced with another signal.
2629 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2630 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2632 struct ptrace_lwpinfo pl;
2636 ATF_REQUIRE((fpid = fork()) != -1);
2643 /* The first wait() should report the stop from SIGSTOP. */
2644 wpid = waitpid(fpid, &status, 0);
2645 REQUIRE_EQ(wpid, fpid);
2646 ATF_REQUIRE(WIFSTOPPED(status));
2647 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2649 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2651 /* Send a signal without ptrace. */
2652 REQUIRE_EQ(kill(fpid, SIGINT), 0);
2654 /* The second wait() should report a SIGINT was received. */
2655 wpid = waitpid(fpid, &status, 0);
2656 REQUIRE_EQ(wpid, fpid);
2657 ATF_REQUIRE(WIFSTOPPED(status));
2658 REQUIRE_EQ(WSTOPSIG(status), SIGINT);
2660 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2661 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2662 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGINT);
2664 /* Continue the child process with a different signal. */
2665 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM), 0);
2668 * The last wait() should report having died due to the new
2671 wpid = waitpid(fpid, &status, 0);
2672 REQUIRE_EQ(wpid, fpid);
2673 ATF_REQUIRE(WIFSIGNALED(status));
2674 REQUIRE_EQ(WTERMSIG(status), SIGTERM);
2676 wpid = wait(&status);
2677 REQUIRE_EQ(wpid, -1);
2678 REQUIRE_EQ(errno, ECHILD);
2682 * Verify that a signal can be passed through to the child even when there
2683 * was no true signal originally. Such cases arise when a SIGTRAP is
2684 * invented for e.g, system call stops.
2686 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2687 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2689 struct ptrace_lwpinfo pl;
2694 ATF_REQUIRE((fpid = fork()) != -1);
2697 /* SIGTRAP expected to cause exit on syscall entry. */
2698 rl.rlim_cur = rl.rlim_max = 0;
2699 REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0);
2704 /* The first wait() should report the stop from SIGSTOP. */
2705 wpid = waitpid(fpid, &status, 0);
2706 REQUIRE_EQ(wpid, fpid);
2707 ATF_REQUIRE(WIFSTOPPED(status));
2708 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2710 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2711 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2713 /* The second wait() should report a system call entry for getpid(). */
2714 wpid = waitpid(fpid, &status, 0);
2715 REQUIRE_EQ(wpid, fpid);
2716 ATF_REQUIRE(WIFSTOPPED(status));
2717 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2719 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2720 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2722 /* Continue the child process with a SIGTRAP. */
2723 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP), 0);
2727 * The last wait() should report exit due to SIGTRAP. In the
2728 * meantime, catch and proceed past any syscall stops.
2730 wpid = waitpid(fpid, &status, 0);
2731 REQUIRE_EQ(wpid, fpid);
2732 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2733 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2734 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2735 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2737 ATF_REQUIRE(WIFSIGNALED(status));
2738 REQUIRE_EQ(WTERMSIG(status), SIGTRAP);
2743 wpid = wait(&status);
2744 REQUIRE_EQ(wpid, -1);
2745 REQUIRE_EQ(errno, ECHILD);
2749 * A mixed bag PT_CONTINUE with signal test.
2751 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2752 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2754 struct ptrace_lwpinfo pl;
2758 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2760 ATF_REQUIRE((fpid = fork()) != -1);
2767 /* The first wait() should report the stop from SIGSTOP. */
2768 wpid = waitpid(fpid, &status, 0);
2769 REQUIRE_EQ(wpid, fpid);
2770 ATF_REQUIRE(WIFSTOPPED(status));
2771 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2773 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2774 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2776 /* The second wait() should report a system call entry for getpid(). */
2777 wpid = waitpid(fpid, &status, 0);
2778 REQUIRE_EQ(wpid, fpid);
2779 ATF_REQUIRE(WIFSTOPPED(status));
2780 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2782 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2783 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2785 /* Continue with the first SIGUSR1. */
2786 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2788 /* The next wait() should report a system call exit for getpid(). */
2789 wpid = waitpid(fpid, &status, 0);
2790 REQUIRE_EQ(wpid, fpid);
2791 ATF_REQUIRE(WIFSTOPPED(status));
2792 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2794 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2795 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2797 /* Send an ABRT without ptrace. */
2798 REQUIRE_EQ(kill(fpid, SIGABRT), 0);
2800 /* Continue normally. */
2801 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2803 /* The next wait() should report the SIGABRT. */
2804 wpid = waitpid(fpid, &status, 0);
2805 REQUIRE_EQ(wpid, fpid);
2806 ATF_REQUIRE(WIFSTOPPED(status));
2807 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
2809 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2810 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2811 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
2813 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2814 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2818 * The last wait() should report exit 2, i.e., a normal _exit
2819 * from the signal handler. In the meantime, catch and proceed
2820 * past any syscall stops.
2822 wpid = waitpid(fpid, &status, 0);
2823 REQUIRE_EQ(wpid, fpid);
2824 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2825 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2826 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2827 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2829 ATF_REQUIRE(WIFEXITED(status));
2830 REQUIRE_EQ(WEXITSTATUS(status), 2);
2835 wpid = wait(&status);
2836 REQUIRE_EQ(wpid, -1);
2837 REQUIRE_EQ(errno, ECHILD);
2841 * Verify a signal delivered by ptrace is noticed by kevent(2).
2843 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2844 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2847 int status, kq, nevents;
2850 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2852 ATF_REQUIRE((fpid = fork()) != -1);
2854 CHILD_REQUIRE((kq = kqueue()) > 0);
2855 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2856 CHILD_REQUIRE_EQ(kevent(kq, &kev, 1, NULL, 0, NULL), 0);
2861 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2862 if (nevents == -1 && errno == EINTR)
2864 CHILD_REQUIRE(nevents > 0);
2865 CHILD_REQUIRE_EQ(kev.filter, EVFILT_SIGNAL);
2866 CHILD_REQUIRE_EQ(kev.ident, (uintptr_t)SIGUSR1);
2873 /* The first wait() should report the stop from SIGSTOP. */
2874 wpid = waitpid(fpid, &status, 0);
2875 REQUIRE_EQ(wpid, fpid);
2876 ATF_REQUIRE(WIFSTOPPED(status));
2877 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2879 /* Continue with the SIGUSR1. */
2880 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2883 * The last wait() should report normal exit with code 1.
2885 wpid = waitpid(fpid, &status, 0);
2886 REQUIRE_EQ(wpid, fpid);
2887 ATF_REQUIRE(WIFEXITED(status));
2888 REQUIRE_EQ(WEXITSTATUS(status), 1);
2890 wpid = wait(&status);
2891 REQUIRE_EQ(wpid, -1);
2892 REQUIRE_EQ(errno, ECHILD);
2896 signal_thread(void *arg)
2901 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2903 /* Wait for this thread to receive a SIGUSR1. */
2905 err = sem_wait(&sigusr1_sem);
2906 CHILD_REQUIRE(err == 0 || errno == EINTR);
2907 } while (err != 0 && errno == EINTR);
2909 /* Free our companion thread from the barrier. */
2910 pthread_barrier_wait(pbarrier);
2913 * Swap ignore duties; the next SIGUSR1 should go to the
2916 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2917 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2918 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2920 /* Sync up threads after swapping signal masks. */
2921 pthread_barrier_wait(pbarrier);
2923 /* Wait until our companion has received its SIGUSR1. */
2924 pthread_barrier_wait(pbarrier);
2930 * Verify that a traced process with blocked signal received the
2931 * signal from kill() once unmasked.
2933 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2934 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2936 struct ptrace_lwpinfo pl;
2941 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2942 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2945 ATF_REQUIRE((fpid = fork()) != -1);
2947 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2948 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2949 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2952 CHILD_REQUIRE_EQ(got_usr1, 0);
2954 /* Allow the pending SIGUSR1 in now. */
2955 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2956 /* Wait to receive a SIGUSR1. */
2958 err = sem_wait(&sigusr1_sem);
2959 CHILD_REQUIRE(err == 0 || errno == EINTR);
2960 } while (err != 0 && errno == EINTR);
2961 CHILD_REQUIRE_EQ(got_usr1, 1);
2965 /* The first wait() should report the stop from SIGSTOP. */
2966 wpid = waitpid(fpid, &status, 0);
2967 REQUIRE_EQ(wpid, fpid);
2968 ATF_REQUIRE(WIFSTOPPED(status));
2969 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2970 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2971 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
2973 /* Send blocked SIGUSR1 which should cause a stop. */
2974 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2976 /* Continue the child ignoring the SIGSTOP. */
2977 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2979 /* The next wait() should report the kill(SIGUSR1) was received. */
2980 wpid = waitpid(fpid, &status, 0);
2981 REQUIRE_EQ(wpid, fpid);
2982 ATF_REQUIRE(WIFSTOPPED(status));
2983 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
2984 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2985 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
2987 /* Continue the child, allowing in the SIGUSR1. */
2988 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2990 /* The last wait() should report normal exit with code 1. */
2991 wpid = waitpid(fpid, &status, 0);
2992 REQUIRE_EQ(wpid, fpid);
2993 ATF_REQUIRE(WIFEXITED(status));
2994 REQUIRE_EQ(WEXITSTATUS(status), 1);
2996 wpid = wait(&status);
2997 REQUIRE_EQ(wpid, -1);
2998 REQUIRE_EQ(errno, ECHILD);
3002 * Verify that a traced process with blocked signal received the
3003 * signal from PT_CONTINUE once unmasked.
3005 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
3006 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
3008 struct ptrace_lwpinfo pl;
3013 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3014 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3017 ATF_REQUIRE((fpid = fork()) != -1);
3019 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3020 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3021 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
3024 CHILD_REQUIRE_EQ(got_usr1, 0);
3026 /* Allow the pending SIGUSR1 in now. */
3027 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
3028 /* Wait to receive a SIGUSR1. */
3030 err = sem_wait(&sigusr1_sem);
3031 CHILD_REQUIRE(err == 0 || errno == EINTR);
3032 } while (err != 0 && errno == EINTR);
3034 CHILD_REQUIRE_EQ(got_usr1, 1);
3038 /* The first wait() should report the stop from SIGSTOP. */
3039 wpid = waitpid(fpid, &status, 0);
3040 REQUIRE_EQ(wpid, fpid);
3041 ATF_REQUIRE(WIFSTOPPED(status));
3042 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3043 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3044 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
3046 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3047 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3049 /* The next wait() should report the SIGUSR1 was received. */
3050 wpid = waitpid(fpid, &status, 0);
3051 REQUIRE_EQ(wpid, fpid);
3052 ATF_REQUIRE(WIFSTOPPED(status));
3053 REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
3054 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3055 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
3057 /* Continue the child, ignoring the SIGUSR1. */
3058 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3060 /* The last wait() should report normal exit with code 1. */
3061 wpid = waitpid(fpid, &status, 0);
3062 REQUIRE_EQ(wpid, fpid);
3063 ATF_REQUIRE(WIFEXITED(status));
3064 REQUIRE_EQ(WEXITSTATUS(status), 1);
3066 wpid = wait(&status);
3067 REQUIRE_EQ(wpid, -1);
3068 REQUIRE_EQ(errno, ECHILD);
3072 * Verify that if ptrace stops due to a signal but continues with
3073 * a different signal that the new signal is routed to a thread
3074 * that can accept it, and that the thread is awakened by the signal
3075 * in a timely manner.
3077 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3078 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3084 pthread_barrier_t barrier;
3086 REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
3087 REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3088 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3090 ATF_REQUIRE((fpid = fork()) != -1);
3092 CHILD_REQUIRE_EQ(pthread_create(&t, NULL, signal_thread,
3093 (void *)&barrier), 0);
3095 /* The other thread should receive the first SIGUSR1. */
3096 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3097 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3098 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
3102 /* Wait until other thread has received its SIGUSR1. */
3103 pthread_barrier_wait(&barrier);
3106 * Swap ignore duties; the next SIGUSR1 should go to this
3109 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL),
3112 /* Sync up threads after swapping signal masks. */
3113 pthread_barrier_wait(&barrier);
3116 * Sync up with test code; we're ready for the next SIGUSR1
3121 /* Wait for this thread to receive a SIGUSR1. */
3123 err = sem_wait(&sigusr1_sem);
3124 CHILD_REQUIRE(err == 0 || errno == EINTR);
3125 } while (err != 0 && errno == EINTR);
3127 /* Free the other thread from the barrier. */
3128 pthread_barrier_wait(&barrier);
3130 CHILD_REQUIRE_EQ(pthread_join(t, NULL), 0);
3135 /* The first wait() should report the stop from SIGSTOP. */
3136 wpid = waitpid(fpid, &status, 0);
3137 REQUIRE_EQ(wpid, fpid);
3138 ATF_REQUIRE(WIFSTOPPED(status));
3139 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3141 /* Continue the child ignoring the SIGSTOP. */
3142 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3145 * Send a signal without ptrace that either thread will accept (USR2,
3148 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3150 /* The second wait() should report a SIGUSR2 was received. */
3151 wpid = waitpid(fpid, &status, 0);
3152 REQUIRE_EQ(wpid, fpid);
3153 ATF_REQUIRE(WIFSTOPPED(status));
3154 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3156 /* Continue the child, changing the signal to USR1. */
3157 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3159 /* The next wait() should report the stop from SIGSTOP. */
3160 wpid = waitpid(fpid, &status, 0);
3161 REQUIRE_EQ(wpid, fpid);
3162 ATF_REQUIRE(WIFSTOPPED(status));
3163 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3165 /* Continue the child ignoring the SIGSTOP. */
3166 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3168 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3170 /* The next wait() should report a SIGUSR2 was received. */
3171 wpid = waitpid(fpid, &status, 0);
3172 REQUIRE_EQ(wpid, fpid);
3173 ATF_REQUIRE(WIFSTOPPED(status));
3174 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3176 /* Continue the child, changing the signal to USR1. */
3177 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3179 /* The last wait() should report normal exit with code 1. */
3180 wpid = waitpid(fpid, &status, 0);
3181 REQUIRE_EQ(wpid, fpid);
3182 ATF_REQUIRE(WIFEXITED(status));
3183 REQUIRE_EQ(WEXITSTATUS(status), 1);
3185 wpid = wait(&status);
3186 REQUIRE_EQ(wpid, -1);
3187 REQUIRE_EQ(errno, ECHILD);
3191 * Verify that PT_GETREGSET returns registers and PT_SETREGSET updates them.
3193 ATF_TC_WITHOUT_HEAD(ptrace__PT_REGSET);
3194 ATF_TC_BODY(ptrace__PT_REGSET, tc)
3196 #if defined(__aarch64__)
3197 struct arm64_addr_mask addr_mask;
3199 struct prstatus prstatus;
3204 ATF_REQUIRE((child = fork()) != -1);
3210 /* The first wait() should report the stop from SIGSTOP. */
3211 wpid = waitpid(child, &status, 0);
3212 REQUIRE_EQ(wpid, child);
3213 ATF_REQUIRE(WIFSTOPPED(status));
3214 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3216 /* Check the size is returned when vec.iov_base is NULL */
3217 vec.iov_base = NULL;
3219 ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&vec, NT_PRSTATUS) !=
3221 ATF_REQUIRE(vec.iov_len == sizeof(prstatus));
3222 ATF_REQUIRE(vec.iov_base == NULL);
3224 /* Read the registers. */
3225 memset(&prstatus, 0, sizeof(prstatus));
3226 vec.iov_base = &prstatus;
3227 ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&vec, NT_PRSTATUS) !=
3229 ATF_REQUIRE(vec.iov_len == sizeof(prstatus));
3230 ATF_REQUIRE(vec.iov_base == &prstatus);
3231 ATF_REQUIRE(prstatus.pr_statussz == sizeof(prstatus));
3233 /* Write the registers back. */
3234 ATF_REQUIRE(ptrace(PT_SETREGSET, wpid, (caddr_t)&vec, NT_PRSTATUS) !=
3237 #if defined(__aarch64__)
3238 vec.iov_base = &addr_mask;
3239 vec.iov_len = sizeof(addr_mask);
3240 ATF_REQUIRE(ptrace(PT_GETREGSET, wpid, (caddr_t)&vec,
3241 NT_ARM_ADDR_MASK) != -1);
3242 REQUIRE_EQ(addr_mask.code, addr_mask.data);
3243 ATF_REQUIRE(addr_mask.code == 0 ||
3244 addr_mask.code == 0xff7f000000000000UL);
3247 REQUIRE_EQ(ptrace(PT_CONTINUE, child, (caddr_t)1, 0), 0);
3249 /* The second wait() should report the exit status. */
3250 wpid = waitpid(child, &status, 0);
3251 REQUIRE_EQ(wpid, child);
3252 ATF_REQUIRE(WIFEXITED(status));
3253 REQUIRE_EQ(WEXITSTATUS(status), 1);
3255 /* The child should no longer exist. */
3256 wpid = waitpid(child, &status, 0);
3257 REQUIRE_EQ(wpid, -1);
3258 REQUIRE_EQ(errno, ECHILD);
3262 raise_sigstop_thread(void *arg __unused)
3270 sleep_thread(void *arg __unused)
3278 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3287 * Become the reaper for this process tree. We need to be able to check
3288 * that both child and grandchild have died.
3290 REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
3293 ATF_REQUIRE(fpid >= 0);
3296 CHILD_REQUIRE(fpid >= 0);
3300 /* Pin to CPU 0 to serialize thread execution. */
3302 CPU_SET(0, &setmask);
3303 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
3304 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3305 CPU_WHICH_CPUSET, setid,
3306 sizeof(setmask), &setmask) == 0);
3308 if (sigstop_from_main_thread) {
3310 * We expect the SIGKILL sent when our parent
3311 * dies to be delivered to the new thread.
3312 * Raise the SIGSTOP in this thread so the
3315 CHILD_REQUIRE(pthread_create(&t, NULL,
3316 sleep_thread, NULL) == 0);
3320 * We expect the SIGKILL to be delivered to
3321 * this thread. After creating the new thread,
3322 * just get off the CPU so the other thread can
3323 * raise the SIGSTOP.
3325 CHILD_REQUIRE(pthread_create(&t, NULL,
3326 raise_sigstop_thread, NULL) == 0);
3332 /* First stop is trace_me() immediately after fork. */
3333 wpid = waitpid(fpid, &status, 0);
3334 CHILD_REQUIRE_EQ(wpid, fpid);
3335 CHILD_REQUIRE(WIFSTOPPED(status));
3336 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3338 CHILD_REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3340 /* Second stop is from the raise(SIGSTOP). */
3341 wpid = waitpid(fpid, &status, 0);
3342 CHILD_REQUIRE_EQ(wpid, fpid);
3343 CHILD_REQUIRE(WIFSTOPPED(status));
3344 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3347 * Terminate tracing process without detaching. Our child
3354 * We should get a normal exit from our immediate child and a SIGKILL
3355 * exit from our grandchild. The latter case is the interesting one.
3356 * Our grandchild should not have stopped due to the SIGSTOP that was
3357 * left dangling when its parent died.
3359 for (i = 0; i < 2; ++i) {
3360 wpid = wait(&status);
3362 ATF_REQUIRE(WIFEXITED(status));
3363 REQUIRE_EQ(WEXITSTATUS(status), 0);
3365 ATF_REQUIRE(WIFSIGNALED(status));
3366 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3372 * These two tests ensure that if the tracing process exits without detaching
3373 * just after the child received a SIGSTOP, the child is cleanly killed and
3374 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3375 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3376 * different threads, the SIGKILL must win. There are two variants of this
3377 * test, designed to catch the case where the SIGKILL is delivered to the
3378 * younger thread (the first test) and the case where the SIGKILL is delivered
3379 * to the older thread (the second test). This behavior has changed in the
3380 * past, so make no assumption.
3382 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3383 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3386 atf_tc_set_md_var(tc, "require.user", "root");
3388 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3391 terminate_with_pending_sigstop(true);
3394 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3395 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3398 atf_tc_set_md_var(tc, "require.user", "root");
3400 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3403 terminate_with_pending_sigstop(false);
3407 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3410 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3411 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3413 struct ptrace_lwpinfo pl;
3415 int status, event_mask, new_event_mask;
3417 ATF_REQUIRE((fpid = fork()) != -1);
3424 /* The first wait() should report the stop from trace_me(). */
3425 wpid = waitpid(fpid, &status, 0);
3426 REQUIRE_EQ(wpid, fpid);
3427 ATF_REQUIRE(WIFSTOPPED(status));
3428 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3430 /* Set several unobtrusive event bits. */
3431 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3432 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3433 sizeof(event_mask)) == 0);
3435 /* Send a SIGKILL without using ptrace. */
3436 REQUIRE_EQ(kill(fpid, SIGKILL), 0);
3438 /* Continue the child ignoring the SIGSTOP. */
3439 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3441 /* The next stop should be due to the SIGKILL. */
3442 wpid = waitpid(fpid, &status, 0);
3443 REQUIRE_EQ(wpid, fpid);
3444 ATF_REQUIRE(WIFSTOPPED(status));
3445 REQUIRE_EQ(WSTOPSIG(status), SIGKILL);
3447 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3448 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3449 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGKILL);
3451 /* Continue the child ignoring the SIGKILL. */
3452 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3454 /* The next wait() should report the stop from SIGSTOP. */
3455 wpid = waitpid(fpid, &status, 0);
3456 REQUIRE_EQ(wpid, fpid);
3457 ATF_REQUIRE(WIFSTOPPED(status));
3458 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3460 /* Check the current event mask. It should not have changed. */
3462 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3463 sizeof(new_event_mask)) == 0);
3464 REQUIRE_EQ(event_mask, new_event_mask);
3466 /* Continue the child to let it exit. */
3467 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3469 /* The last event should be for the child process's exit. */
3470 wpid = waitpid(fpid, &status, 0);
3471 ATF_REQUIRE(WIFEXITED(status));
3472 REQUIRE_EQ(WEXITSTATUS(status), 0);
3474 wpid = wait(&status);
3475 REQUIRE_EQ(wpid, -1);
3476 REQUIRE_EQ(errno, ECHILD);
3480 flock_thread(void *arg)
3485 (void)flock(fd, LOCK_EX);
3486 (void)flock(fd, LOCK_UN);
3491 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3492 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3493 * on a lock. This is a regression test for r318191.
3495 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3496 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3498 pthread_barrier_t barrier;
3499 pthread_barrierattr_t battr;
3502 int error, fd, i, status;
3504 REQUIRE_EQ(pthread_barrierattr_init(&battr), 0);
3505 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3506 PTHREAD_PROCESS_SHARED) == 0);
3507 REQUIRE_EQ(pthread_barrier_init(&barrier, &battr, 2), 0);
3509 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3510 fd = mkstemp(tmpfile);
3511 ATF_REQUIRE(fd >= 0);
3513 ATF_REQUIRE((child = fork()) != -1);
3518 error = pthread_barrier_wait(&barrier);
3519 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3522 cfd = open(tmpfile, O_RDONLY);
3527 * We want at least two threads blocked on the file lock since
3528 * the SIGSTOP from PT_ATTACH may kick one of them out of
3531 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3533 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3535 if (pthread_join(t[0], NULL) != 0)
3537 if (pthread_join(t[1], NULL) != 0)
3542 REQUIRE_EQ(flock(fd, LOCK_EX), 0);
3544 error = pthread_barrier_wait(&barrier);
3545 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3548 * Give the child some time to block. Is there a better way to do this?
3553 * Attach and give the child 3 seconds to stop.
3555 REQUIRE_EQ(ptrace(PT_ATTACH, child, NULL, 0), 0);
3556 for (i = 0; i < 3; i++) {
3557 wpid = waitpid(child, &status, WNOHANG);
3558 if (wpid == child && WIFSTOPPED(status) &&
3559 WSTOPSIG(status) == SIGSTOP)
3563 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3565 REQUIRE_EQ(ptrace(PT_DETACH, child, NULL, 0), 0);
3567 REQUIRE_EQ(flock(fd, LOCK_UN), 0);
3568 REQUIRE_EQ(unlink(tmpfile), 0);
3569 REQUIRE_EQ(close(fd), 0);
3573 sigusr1_step_handler(int sig)
3576 CHILD_REQUIRE_EQ(sig, SIGUSR1);
3581 * Verify that PT_STEP with a signal invokes the signal before
3582 * stepping the next instruction (and that the next instruction is
3583 * stepped correctly).
3585 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3586 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3588 struct ptrace_lwpinfo pl;
3592 ATF_REQUIRE((fpid = fork()) != -1);
3595 signal(SIGUSR1, sigusr1_step_handler);
3600 /* The first wait() should report the stop from SIGSTOP. */
3601 wpid = waitpid(fpid, &status, 0);
3602 REQUIRE_EQ(wpid, fpid);
3603 ATF_REQUIRE(WIFSTOPPED(status));
3604 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3606 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3608 /* The next stop should report the SIGABRT in the child body. */
3609 wpid = waitpid(fpid, &status, 0);
3610 REQUIRE_EQ(wpid, fpid);
3611 ATF_REQUIRE(WIFSTOPPED(status));
3612 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3614 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3615 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3616 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3618 /* Step the child process inserting SIGUSR1. */
3619 REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1), 0);
3621 /* The next stop should report the SIGABRT in the signal handler. */
3622 wpid = waitpid(fpid, &status, 0);
3623 REQUIRE_EQ(wpid, fpid);
3624 ATF_REQUIRE(WIFSTOPPED(status));
3625 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3627 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3628 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3629 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3631 /* Continue the child process discarding the signal. */
3632 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3634 /* The next stop should report a trace trap from PT_STEP. */
3635 wpid = waitpid(fpid, &status, 0);
3636 REQUIRE_EQ(wpid, fpid);
3637 ATF_REQUIRE(WIFSTOPPED(status));
3638 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3640 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3641 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3642 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3643 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3645 /* Continue the child to let it exit. */
3646 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3648 /* The last event should be for the child process's exit. */
3649 wpid = waitpid(fpid, &status, 0);
3650 ATF_REQUIRE(WIFEXITED(status));
3651 REQUIRE_EQ(WEXITSTATUS(status), 1);
3653 wpid = wait(&status);
3654 REQUIRE_EQ(wpid, -1);
3655 REQUIRE_EQ(errno, ECHILD);
3658 #ifdef HAVE_BREAKPOINT
3660 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3661 * for a breakpoint trap.
3663 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3664 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3666 struct ptrace_lwpinfo pl;
3670 ATF_REQUIRE((fpid = fork()) != -1);
3677 /* The first wait() should report the stop from SIGSTOP. */
3678 wpid = waitpid(fpid, &status, 0);
3679 REQUIRE_EQ(wpid, fpid);
3680 ATF_REQUIRE(WIFSTOPPED(status));
3681 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3683 /* Continue the child ignoring the SIGSTOP. */
3684 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3686 /* The second wait() should report hitting the breakpoint. */
3687 wpid = waitpid(fpid, &status, 0);
3688 REQUIRE_EQ(wpid, fpid);
3689 ATF_REQUIRE(WIFSTOPPED(status));
3690 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3692 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3693 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3694 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3695 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3697 /* Kill the child process. */
3698 REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
3700 /* The last wait() should report the SIGKILL. */
3701 wpid = waitpid(fpid, &status, 0);
3702 REQUIRE_EQ(wpid, fpid);
3703 ATF_REQUIRE(WIFSIGNALED(status));
3704 REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3706 wpid = wait(&status);
3707 REQUIRE_EQ(wpid, -1);
3708 REQUIRE_EQ(errno, ECHILD);
3710 #endif /* HAVE_BREAKPOINT */
3713 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3714 * for a single-step trap from PT_STEP.
3716 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3717 ATF_TC_BODY(ptrace__step_siginfo, tc)
3719 struct ptrace_lwpinfo pl;
3723 ATF_REQUIRE((fpid = fork()) != -1);
3729 /* The first wait() should report the stop from SIGSTOP. */
3730 wpid = waitpid(fpid, &status, 0);
3731 REQUIRE_EQ(wpid, fpid);
3732 ATF_REQUIRE(WIFSTOPPED(status));
3733 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3735 /* Step the child ignoring the SIGSTOP. */
3736 REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, 0), 0);
3738 /* The second wait() should report a single-step trap. */
3739 wpid = waitpid(fpid, &status, 0);
3740 REQUIRE_EQ(wpid, fpid);
3741 ATF_REQUIRE(WIFSTOPPED(status));
3742 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3744 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3745 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3746 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3747 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3749 /* Continue the child process. */
3750 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3752 /* The last event should be for the child process's exit. */
3753 wpid = waitpid(fpid, &status, 0);
3754 ATF_REQUIRE(WIFEXITED(status));
3755 REQUIRE_EQ(WEXITSTATUS(status), 1);
3757 wpid = wait(&status);
3758 REQUIRE_EQ(wpid, -1);
3759 REQUIRE_EQ(errno, ECHILD);
3762 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3764 continue_thread(void *arg __unused)
3771 continue_thread_main(void)
3773 pthread_t threads[2];
3775 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3777 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3779 CHILD_REQUIRE_EQ(pthread_join(threads[0], NULL), 0);
3780 CHILD_REQUIRE_EQ(pthread_join(threads[1], NULL), 0);
3785 * Ensure that PT_CONTINUE clears the status of the thread that
3786 * triggered the stop even if a different thread's LWP was passed to
3789 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3790 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3792 struct ptrace_lwpinfo pl;
3799 ATF_REQUIRE((fpid = fork()) != -1);
3802 continue_thread_main();
3805 /* The first wait() should report the stop from SIGSTOP. */
3806 wpid = waitpid(fpid, &status, 0);
3807 REQUIRE_EQ(wpid, fpid);
3808 ATF_REQUIRE(WIFSTOPPED(status));
3809 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3811 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3814 REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
3816 /* Continue the child ignoring the SIGSTOP. */
3817 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3819 /* One of the new threads should report it's birth. */
3820 wpid = waitpid(fpid, &status, 0);
3821 REQUIRE_EQ(wpid, fpid);
3822 ATF_REQUIRE(WIFSTOPPED(status));
3823 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3825 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3826 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3827 (PL_FLAG_BORN | PL_FLAG_SCX));
3828 lwps[0] = pl.pl_lwpid;
3831 * Suspend this thread to ensure both threads are alive before
3832 * hitting the breakpoint.
3834 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3836 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3838 /* Second thread should report it's birth. */
3839 wpid = waitpid(fpid, &status, 0);
3840 REQUIRE_EQ(wpid, fpid);
3841 ATF_REQUIRE(WIFSTOPPED(status));
3842 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3844 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3845 REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3846 (PL_FLAG_BORN | PL_FLAG_SCX));
3847 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3848 lwps[1] = pl.pl_lwpid;
3850 /* Resume both threads waiting for breakpoint events. */
3851 hit_break[0] = hit_break[1] = false;
3852 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3853 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3855 /* One thread should report a breakpoint. */
3856 wpid = waitpid(fpid, &status, 0);
3857 REQUIRE_EQ(wpid, fpid);
3858 ATF_REQUIRE(WIFSTOPPED(status));
3859 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3861 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3862 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3863 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3864 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3865 if (pl.pl_lwpid == lwps[0])
3869 hit_break[i] = true;
3870 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3872 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3875 * Resume both threads but pass the other thread's LWPID to
3878 REQUIRE_EQ(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0), 0);
3881 * Will now get two thread exit events and one more breakpoint
3884 for (j = 0; j < 3; j++) {
3885 wpid = waitpid(fpid, &status, 0);
3886 REQUIRE_EQ(wpid, fpid);
3887 ATF_REQUIRE(WIFSTOPPED(status));
3888 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3890 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3893 if (pl.pl_lwpid == lwps[0])
3898 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3899 if (pl.pl_flags & PL_FLAG_EXITED) {
3900 ATF_REQUIRE_MSG(hit_break[i],
3901 "exited thread did not report breakpoint");
3904 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3905 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3906 REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3907 ATF_REQUIRE_MSG(!hit_break[i],
3908 "double breakpoint event");
3909 hit_break[i] = true;
3910 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3913 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3917 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3920 /* Both threads should have exited. */
3921 REQUIRE_EQ(lwps[0], 0);
3922 REQUIRE_EQ(lwps[1], 0);
3924 /* The last event should be for the child process's exit. */
3925 wpid = waitpid(fpid, &status, 0);
3926 ATF_REQUIRE(WIFEXITED(status));
3927 REQUIRE_EQ(WEXITSTATUS(status), 1);
3929 wpid = wait(&status);
3930 REQUIRE_EQ(wpid, -1);
3931 REQUIRE_EQ(errno, ECHILD);
3936 * Verify that PT_LWPINFO doesn't return stale siginfo.
3938 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3939 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3941 struct ptrace_lwpinfo pl;
3945 ATF_REQUIRE((fpid = fork()) != -1);
3952 /* The first wait() should report the stop from SIGSTOP. */
3953 wpid = waitpid(fpid, &status, 0);
3954 REQUIRE_EQ(wpid, fpid);
3955 ATF_REQUIRE(WIFSTOPPED(status));
3956 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3958 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3960 /* The next stop should report the SIGABRT in the child body. */
3961 wpid = waitpid(fpid, &status, 0);
3962 REQUIRE_EQ(wpid, fpid);
3963 ATF_REQUIRE(WIFSTOPPED(status));
3964 REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3966 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3967 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3968 REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3971 * Continue the process ignoring the signal, but enabling
3974 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
3977 * The next stop should report a system call entry from
3978 * exit(). PL_FLAGS_SI should not be set.
3980 wpid = waitpid(fpid, &status, 0);
3981 REQUIRE_EQ(wpid, fpid);
3982 ATF_REQUIRE(WIFSTOPPED(status));
3983 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3985 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3986 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3987 REQUIRE_EQ((pl.pl_flags & PL_FLAG_SI), 0);
3989 /* Disable syscall tracing and continue the child to let it exit. */
3990 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3991 sizeof(events)) == 0);
3992 events &= ~PTRACE_SYSCALL;
3993 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3994 sizeof(events)) == 0);
3995 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3997 /* The last event should be for the child process's exit. */
3998 wpid = waitpid(fpid, &status, 0);
3999 ATF_REQUIRE(WIFEXITED(status));
4000 REQUIRE_EQ(WEXITSTATUS(status), 1);
4002 wpid = wait(&status);
4003 REQUIRE_EQ(wpid, -1);
4004 REQUIRE_EQ(errno, ECHILD);
4008 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
4010 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
4011 ATF_TC_BODY(ptrace__syscall_args, tc)
4013 struct ptrace_lwpinfo pl;
4014 struct ptrace_sc_ret psr;
4019 ATF_REQUIRE((fpid = fork()) != -1);
4023 /* Close a fd that should not exist. */
4028 /* The first wait() should report the stop from SIGSTOP. */
4029 wpid = waitpid(fpid, &status, 0);
4030 REQUIRE_EQ(wpid, fpid);
4031 ATF_REQUIRE(WIFSTOPPED(status));
4032 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4035 * Continue the process ignoring the signal, but enabling
4038 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
4041 * The next stop should be the syscall entry from getpid().
4043 wpid = waitpid(fpid, &status, 0);
4044 REQUIRE_EQ(wpid, fpid);
4045 ATF_REQUIRE(WIFSTOPPED(status));
4046 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4048 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4049 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4050 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_getpid);
4052 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4055 * The next stop should be the syscall exit from getpid().
4057 wpid = waitpid(fpid, &status, 0);
4058 REQUIRE_EQ(wpid, fpid);
4059 ATF_REQUIRE(WIFSTOPPED(status));
4060 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4062 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4063 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4064 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_getpid);
4066 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4067 sizeof(psr)) != -1);
4068 REQUIRE_EQ(psr.sr_error, 0);
4069 REQUIRE_EQ(psr.sr_retval[0], wpid);
4071 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4074 * The next stop should be the syscall entry from kill().
4076 wpid = waitpid(fpid, &status, 0);
4077 REQUIRE_EQ(wpid, fpid);
4078 ATF_REQUIRE(WIFSTOPPED(status));
4079 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4081 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4082 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4083 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_kill);
4084 REQUIRE_EQ(pl.pl_syscall_narg, 2u);
4086 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4087 sizeof(args)) != -1);
4088 REQUIRE_EQ(args[0], wpid);
4089 REQUIRE_EQ(args[1], 0);
4091 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4094 * The next stop should be the syscall exit from kill().
4096 wpid = waitpid(fpid, &status, 0);
4097 REQUIRE_EQ(wpid, fpid);
4098 ATF_REQUIRE(WIFSTOPPED(status));
4099 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4101 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4102 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4103 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_kill);
4105 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4106 sizeof(psr)) != -1);
4107 REQUIRE_EQ(psr.sr_error, 0);
4109 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4112 * The next stop should be the syscall entry from close().
4114 wpid = waitpid(fpid, &status, 0);
4115 REQUIRE_EQ(wpid, fpid);
4116 ATF_REQUIRE(WIFSTOPPED(status));
4117 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4119 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4120 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4121 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_close);
4122 REQUIRE_EQ(pl.pl_syscall_narg, 1u);
4124 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4125 sizeof(args)) != -1);
4126 REQUIRE_EQ(args[0], 12345);
4128 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4131 * The next stop should be the syscall exit from close().
4133 wpid = waitpid(fpid, &status, 0);
4134 REQUIRE_EQ(wpid, fpid);
4135 ATF_REQUIRE(WIFSTOPPED(status));
4136 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4138 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4139 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4140 REQUIRE_EQ(pl.pl_syscall_code, (unsigned)SYS_close);
4142 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4143 sizeof(psr)) != -1);
4144 REQUIRE_EQ(psr.sr_error, EBADF);
4146 /* Disable syscall tracing and continue the child to let it exit. */
4147 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4148 sizeof(events)) == 0);
4149 events &= ~PTRACE_SYSCALL;
4150 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4151 sizeof(events)) == 0);
4152 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4154 /* The last event should be for the child process's exit. */
4155 wpid = waitpid(fpid, &status, 0);
4156 ATF_REQUIRE(WIFEXITED(status));
4157 REQUIRE_EQ(WEXITSTATUS(status), 1);
4159 wpid = wait(&status);
4160 REQUIRE_EQ(wpid, -1);
4161 REQUIRE_EQ(errno, ECHILD);
4165 * Verify that when the process is traced that it isn't reparent
4166 * to the init process when we close all process descriptors.
4168 ATF_TC(ptrace__proc_reparent);
4169 ATF_TC_HEAD(ptrace__proc_reparent, tc)
4172 atf_tc_set_md_var(tc, "timeout", "2");
4174 ATF_TC_BODY(ptrace__proc_reparent, tc)
4176 pid_t traced, debuger, wpid;
4179 traced = pdfork(&pd, 0);
4180 ATF_REQUIRE(traced >= 0);
4185 ATF_REQUIRE(pd >= 0);
4188 ATF_REQUIRE(debuger >= 0);
4190 /* The traced process is reparented to debuger. */
4191 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4192 wpid = waitpid(traced, &status, 0);
4193 REQUIRE_EQ(wpid, traced);
4194 ATF_REQUIRE(WIFSTOPPED(status));
4195 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4196 REQUIRE_EQ(close(pd), 0);
4197 REQUIRE_EQ(ptrace(PT_DETACH, traced, (caddr_t)1, 0), 0);
4199 /* We closed pd so we should not have any child. */
4200 wpid = wait(&status);
4201 REQUIRE_EQ(wpid, -1);
4202 REQUIRE_EQ(errno, ECHILD);
4207 REQUIRE_EQ(close(pd), 0);
4208 wpid = waitpid(debuger, &status, 0);
4209 REQUIRE_EQ(wpid, debuger);
4210 REQUIRE_EQ(WEXITSTATUS(status), 0);
4212 /* Check if we still have any child. */
4213 wpid = wait(&status);
4214 REQUIRE_EQ(wpid, -1);
4215 REQUIRE_EQ(errno, ECHILD);
4219 * Ensure that traced processes created with pdfork(2) are visible to
4222 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_wait_child);
4223 ATF_TC_BODY(ptrace__procdesc_wait_child, tc)
4228 child = pdfork(&pd, 0);
4229 ATF_REQUIRE(child >= 0);
4233 (void)raise(SIGSTOP);
4237 wpid = waitpid(child, &status, 0);
4238 REQUIRE_EQ(wpid, child);
4239 ATF_REQUIRE(WIFSTOPPED(status));
4240 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4242 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4244 wpid = wait(&status);
4245 REQUIRE_EQ(wpid, child);
4246 ATF_REQUIRE(WIFSTOPPED(status));
4247 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4249 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4252 * If process was created by pdfork, the return code have to
4253 * be collected through process descriptor.
4255 wpid = wait(&status);
4256 REQUIRE_EQ(wpid, -1);
4257 REQUIRE_EQ(errno, ECHILD);
4259 ATF_REQUIRE(close(pd) != -1);
4263 * Ensure that traced processes created with pdfork(2) are not visible
4264 * after returning to parent - waitid(P_ALL).
4266 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_reparent_wait_child);
4267 ATF_TC_BODY(ptrace__procdesc_reparent_wait_child, tc)
4269 pid_t traced, debuger, wpid;
4272 traced = pdfork(&pd, 0);
4273 ATF_REQUIRE(traced >= 0);
4278 ATF_REQUIRE(pd >= 0);
4280 /* Wait until the child process has stopped before fork()ing again. */
4281 REQUIRE_EQ(traced, waitpid(traced, &status, WSTOPPED));
4283 ATF_REQUIRE(debuger >= 0);
4285 /* The traced process is reparented to debuger. */
4286 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4287 wpid = waitpid(traced, &status, 0);
4288 REQUIRE_EQ(wpid, traced);
4289 ATF_REQUIRE(WIFSTOPPED(status));
4290 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4292 /* Allow process to die. */
4293 REQUIRE_EQ(ptrace(PT_CONTINUE, traced, (caddr_t)1, 0), 0);
4294 wpid = waitpid(traced, &status, 0);
4295 REQUIRE_EQ(wpid, traced);
4296 ATF_REQUIRE(WIFEXITED(status));
4297 REQUIRE_EQ(WEXITSTATUS(status), 0);
4299 /* Reparent back to the orginal process. */
4300 REQUIRE_EQ(close(pd), 0);
4304 wpid = waitpid(debuger, &status, 0);
4305 REQUIRE_EQ(wpid, debuger);
4306 REQUIRE_EQ(WEXITSTATUS(status), 0);
4309 * We have a child but it has a process descriptori
4310 * so we should not be able to collect it process.
4312 wpid = wait(&status);
4313 REQUIRE_EQ(wpid, -1);
4314 REQUIRE_EQ(errno, ECHILD);
4316 REQUIRE_EQ(close(pd), 0);
4320 * Try using PT_SC_REMOTE to get the PID of a traced child process.
4322 ATF_TC_WITHOUT_HEAD(ptrace__PT_SC_REMOTE_getpid);
4323 ATF_TC_BODY(ptrace__PT_SC_REMOTE_getpid, tc)
4325 struct ptrace_sc_remote pscr;
4329 ATF_REQUIRE((fpid = fork()) != -1);
4337 pscr.pscr_syscall = SYS_getpid;
4338 pscr.pscr_nargs = 0;
4339 pscr.pscr_args = NULL;
4340 ATF_REQUIRE(ptrace(PT_SC_REMOTE, fpid, (caddr_t)&pscr, sizeof(pscr)) !=
4342 ATF_REQUIRE_MSG(pscr.pscr_ret.sr_error == 0,
4343 "remote getpid failed with error %d", pscr.pscr_ret.sr_error);
4344 ATF_REQUIRE_MSG(pscr.pscr_ret.sr_retval[0] == fpid,
4345 "unexpected return value %jd instead of %d",
4346 (intmax_t)pscr.pscr_ret.sr_retval[0], fpid);
4348 wpid = waitpid(fpid, &status, 0);
4349 REQUIRE_EQ(wpid, fpid);
4350 ATF_REQUIRE(WIFSTOPPED(status));
4351 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4353 pscr.pscr_syscall = SYS_getppid;
4354 pscr.pscr_nargs = 0;
4355 pscr.pscr_args = NULL;
4356 ATF_REQUIRE(ptrace(PT_SC_REMOTE, fpid, (caddr_t)&pscr, sizeof(pscr)) !=
4358 ATF_REQUIRE_MSG(pscr.pscr_ret.sr_error == 0,
4359 "remote getppid failed with error %d", pscr.pscr_ret.sr_error);
4360 ATF_REQUIRE_MSG(pscr.pscr_ret.sr_retval[0] == getpid(),
4361 "unexpected return value %jd instead of %d",
4362 (intmax_t)pscr.pscr_ret.sr_retval[0], fpid);
4364 wpid = waitpid(fpid, &status, 0);
4365 REQUIRE_EQ(wpid, fpid);
4366 ATF_REQUIRE(WIFSTOPPED(status));
4367 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4369 ATF_REQUIRE(ptrace(PT_DETACH, fpid, (caddr_t)1, 0) != -1);
4374 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4375 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4376 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4377 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4378 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4379 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4380 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4381 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4382 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4384 ptrace__follow_fork_child_detached_unrelated_debugger);
4386 ptrace__follow_fork_parent_detached_unrelated_debugger);
4387 ATF_TP_ADD_TC(tp, ptrace__getppid);
4388 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4389 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4390 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4391 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4392 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4393 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4394 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4395 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4396 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4397 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4398 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4399 #ifdef HAVE_BREAKPOINT
4400 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4402 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4403 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4404 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4405 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4406 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4407 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4409 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4410 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4411 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4412 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4413 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4414 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4415 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4416 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4417 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4418 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4419 ATF_TP_ADD_TC(tp, ptrace__PT_REGSET);
4420 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4421 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4422 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4423 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4424 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4425 #ifdef HAVE_BREAKPOINT
4426 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4428 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4429 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4430 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4432 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4433 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4434 ATF_TP_ADD_TC(tp, ptrace__proc_reparent);
4435 ATF_TP_ADD_TC(tp, ptrace__procdesc_wait_child);
4436 ATF_TP_ADD_TC(tp, ptrace__procdesc_reparent_wait_child);
4437 ATF_TP_ADD_TC(tp, ptrace__PT_SC_REMOTE_getpid);
4439 return (atf_no_error());