2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/cpuset.h>
31 #include <sys/event.h>
34 #include <sys/procctl.h>
35 #include <sys/ptrace.h>
36 #include <sys/queue.h>
38 #include <sys/syscall.h>
39 #include <sys/sysctl.h>
43 #include <machine/cpufunc.h>
46 #include <semaphore.h>
54 * Architectures with a user-visible breakpoint().
56 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
57 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
59 #define HAVE_BREAKPOINT
63 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
65 #ifdef HAVE_BREAKPOINT
66 #if defined(__aarch64__)
67 #define SKIP_BREAK(reg) ((reg)->elr += 4)
68 #elif defined(__amd64__) || defined(__i386__)
69 #define SKIP_BREAK(reg)
70 #elif defined(__arm__)
71 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
72 #elif defined(__mips__)
73 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
74 #elif defined(__riscv)
75 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
76 #elif defined(__sparc64__)
77 #define SKIP_BREAK(reg) do { \
78 (reg)->r_tpc = (reg)->r_tnpc + 4; \
85 * A variant of ATF_REQUIRE that is suitable for use in child
86 * processes. This only works if the parent process is tripped up by
87 * the early exit and fails some requirement itself.
89 #define CHILD_REQUIRE(exp) do { \
91 child_fail_require(__FILE__, __LINE__, \
96 child_fail_require(const char *file, int line, const char *str)
100 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
101 write(2, buf, strlen(buf));
109 /* Attach the parent process as a tracer of this process. */
110 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
112 /* Trigger a stop. */
117 attach_child(pid_t pid)
122 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
124 wpid = waitpid(pid, &status, 0);
125 ATF_REQUIRE(wpid == pid);
126 ATF_REQUIRE(WIFSTOPPED(status));
127 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
131 wait_for_zombie(pid_t pid)
135 * Wait for a process to exit. This is kind of gross, but
136 * there is not a better way.
138 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
139 * with ESRCH. After that change, a valid struct kinfo_proc
140 * is returned for zombies with ki_stat set to SZOMB.
143 struct kinfo_proc kp;
149 mib[2] = KERN_PROC_PID;
152 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
153 ATF_REQUIRE(errno == ESRCH);
156 if (kp.ki_stat == SZOMB)
163 * Verify that a parent debugger process "sees" the exit of a debugged
164 * process exactly once when attached via PT_TRACE_ME.
166 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
167 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
172 ATF_REQUIRE((child = fork()) != -1);
180 /* Parent process. */
182 /* The first wait() should report the stop from SIGSTOP. */
183 wpid = waitpid(child, &status, 0);
184 ATF_REQUIRE(wpid == child);
185 ATF_REQUIRE(WIFSTOPPED(status));
186 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
188 /* Continue the child ignoring the SIGSTOP. */
189 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
191 /* The second wait() should report the exit status. */
192 wpid = waitpid(child, &status, 0);
193 ATF_REQUIRE(wpid == child);
194 ATF_REQUIRE(WIFEXITED(status));
195 ATF_REQUIRE(WEXITSTATUS(status) == 1);
197 /* The child should no longer exist. */
198 wpid = waitpid(child, &status, 0);
199 ATF_REQUIRE(wpid == -1);
200 ATF_REQUIRE(errno == ECHILD);
204 * Verify that a parent debugger process "sees" the exit of a debugged
205 * process exactly once when attached via PT_ATTACH.
207 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
208 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
211 int cpipe[2], status;
214 ATF_REQUIRE(pipe(cpipe) == 0);
215 ATF_REQUIRE((child = fork()) != -1);
220 /* Wait for the parent to attach. */
221 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
227 /* Parent process. */
229 /* Attach to the child process. */
232 /* Continue the child ignoring the SIGSTOP. */
233 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
235 /* Signal the child to exit. */
238 /* The second wait() should report the exit status. */
239 wpid = waitpid(child, &status, 0);
240 ATF_REQUIRE(wpid == child);
241 ATF_REQUIRE(WIFEXITED(status));
242 ATF_REQUIRE(WEXITSTATUS(status) == 1);
244 /* The child should no longer exist. */
245 wpid = waitpid(child, &status, 0);
246 ATF_REQUIRE(wpid == -1);
247 ATF_REQUIRE(errno == ECHILD);
251 * Verify that a parent process "sees" the exit of a debugged process only
252 * after the debugger has seen it.
254 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
255 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
257 pid_t child, debugger, wpid;
258 int cpipe[2], dpipe[2], status;
261 atf_tc_skip("https://bugs.freebsd.org/239399");
263 ATF_REQUIRE(pipe(cpipe) == 0);
264 ATF_REQUIRE((child = fork()) != -1);
270 /* Wait for parent to be ready. */
271 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
277 ATF_REQUIRE(pipe(dpipe) == 0);
278 ATF_REQUIRE((debugger = fork()) != -1);
281 /* Debugger process. */
284 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
286 wpid = waitpid(child, &status, 0);
287 CHILD_REQUIRE(wpid == child);
288 CHILD_REQUIRE(WIFSTOPPED(status));
289 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
291 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
293 /* Signal parent that debugger is attached. */
294 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
296 /* Wait for parent's failed wait. */
297 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
299 wpid = waitpid(child, &status, 0);
300 CHILD_REQUIRE(wpid == child);
301 CHILD_REQUIRE(WIFEXITED(status));
302 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
308 /* Parent process. */
310 /* Wait for the debugger to attach to the child. */
311 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
313 /* Release the child. */
314 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
315 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
318 wait_for_zombie(child);
321 * This wait should return a pid of 0 to indicate no status to
322 * report. The parent should see the child as non-exited
323 * until the debugger sees the exit.
325 wpid = waitpid(child, &status, WNOHANG);
326 ATF_REQUIRE(wpid == 0);
328 /* Signal the debugger to wait for the child. */
331 /* Wait for the debugger. */
332 wpid = waitpid(debugger, &status, 0);
333 ATF_REQUIRE(wpid == debugger);
334 ATF_REQUIRE(WIFEXITED(status));
335 ATF_REQUIRE(WEXITSTATUS(status) == 0);
337 /* The child process should now be ready. */
338 wpid = waitpid(child, &status, WNOHANG);
339 ATF_REQUIRE(wpid == child);
340 ATF_REQUIRE(WIFEXITED(status));
341 ATF_REQUIRE(WEXITSTATUS(status) == 1);
345 * Verify that a parent process "sees" the exit of a debugged process
346 * only after a non-direct-child debugger has seen it. In particular,
347 * various wait() calls in the parent must avoid failing with ESRCH by
348 * checking the parent's orphan list for the debugee.
350 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
351 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
353 pid_t child, debugger, fpid, wpid;
354 int cpipe[2], dpipe[2], status;
357 ATF_REQUIRE(pipe(cpipe) == 0);
358 ATF_REQUIRE((child = fork()) != -1);
364 /* Wait for parent to be ready. */
365 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
371 ATF_REQUIRE(pipe(dpipe) == 0);
372 ATF_REQUIRE((debugger = fork()) != -1);
375 /* Debugger parent. */
378 * Fork again and drop the debugger parent so that the
379 * debugger is not a child of the main parent.
381 CHILD_REQUIRE((fpid = fork()) != -1);
385 /* Debugger process. */
388 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
390 wpid = waitpid(child, &status, 0);
391 CHILD_REQUIRE(wpid == child);
392 CHILD_REQUIRE(WIFSTOPPED(status));
393 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
395 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
397 /* Signal parent that debugger is attached. */
398 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
400 /* Wait for parent's failed wait. */
401 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
403 wpid = waitpid(child, &status, 0);
404 CHILD_REQUIRE(wpid == child);
405 CHILD_REQUIRE(WIFEXITED(status));
406 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
412 /* Parent process. */
414 /* Wait for the debugger parent process to exit. */
415 wpid = waitpid(debugger, &status, 0);
416 ATF_REQUIRE(wpid == debugger);
417 ATF_REQUIRE(WIFEXITED(status));
418 ATF_REQUIRE(WEXITSTATUS(status) == 2);
420 /* A WNOHANG wait here should see the non-exited child. */
421 wpid = waitpid(child, &status, WNOHANG);
422 ATF_REQUIRE(wpid == 0);
424 /* Wait for the debugger to attach to the child. */
425 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
427 /* Release the child. */
428 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
429 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
432 wait_for_zombie(child);
435 * This wait should return a pid of 0 to indicate no status to
436 * report. The parent should see the child as non-exited
437 * until the debugger sees the exit.
439 wpid = waitpid(child, &status, WNOHANG);
440 ATF_REQUIRE(wpid == 0);
442 /* Signal the debugger to wait for the child. */
443 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
445 /* Wait for the debugger. */
446 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
449 /* The child process should now be ready. */
450 wpid = waitpid(child, &status, WNOHANG);
451 ATF_REQUIRE(wpid == child);
452 ATF_REQUIRE(WIFEXITED(status));
453 ATF_REQUIRE(WEXITSTATUS(status) == 1);
457 * Make sure that we can collect the exit status of an orphaned process.
459 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
460 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
463 int cpipe1[2], cpipe2[2], gcpipe[2], status;
466 ATF_REQUIRE(pipe(cpipe1) == 0);
467 ATF_REQUIRE(pipe(cpipe2) == 0);
468 ATF_REQUIRE(pipe(gcpipe) == 0);
470 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
472 ATF_REQUIRE((child = fork()) != -1);
474 CHILD_REQUIRE((gchild = fork()) != -1);
478 n = read(gcpipe[0], &status, sizeof(status));
479 } while (n == -1 && errno == EINTR);
483 CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
485 CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
490 ATF_REQUIRE(read(cpipe1[0], &gchild, sizeof(gchild)) == sizeof(gchild));
492 ATF_REQUIRE(ptrace(PT_ATTACH, gchild, NULL, 0) == 0);
495 ATF_REQUIRE(write(cpipe2[1], &status, sizeof(status)) ==
497 ATF_REQUIRE(waitpid(child, &status, 0) == child);
498 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
501 ATF_REQUIRE(write(gcpipe[1], &status, sizeof(status)) ==
503 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
504 ATF_REQUIRE(WIFSTOPPED(status));
505 ATF_REQUIRE(ptrace(PT_DETACH, gchild, (caddr_t)1, 0) == 0);
506 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
507 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
509 ATF_REQUIRE(close(cpipe1[0]) == 0);
510 ATF_REQUIRE(close(cpipe1[1]) == 0);
511 ATF_REQUIRE(close(cpipe2[0]) == 0);
512 ATF_REQUIRE(close(cpipe2[1]) == 0);
513 ATF_REQUIRE(close(gcpipe[0]) == 0);
514 ATF_REQUIRE(close(gcpipe[1]) == 0);
518 * The parent process should always act the same regardless of how the
519 * debugger is attached to it.
522 follow_fork_parent(bool use_vfork)
528 CHILD_REQUIRE((fpid = vfork()) != -1);
530 CHILD_REQUIRE((fpid = fork()) != -1);
536 wpid = waitpid(fpid, &status, 0);
537 CHILD_REQUIRE(wpid == fpid);
538 CHILD_REQUIRE(WIFEXITED(status));
539 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
545 * Helper routine for follow fork tests. This waits for two stops
546 * that report both "sides" of a fork. It returns the pid of the new
550 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
552 struct ptrace_lwpinfo pl;
553 bool fork_reported[2];
557 fork_reported[0] = false;
558 fork_reported[1] = false;
562 * Each process should report a fork event. The parent should
563 * report a PL_FLAG_FORKED event, and the child should report
564 * a PL_FLAG_CHILD event.
566 for (i = 0; i < 2; i++) {
567 wpid = wait(&status);
568 ATF_REQUIRE(wpid > 0);
569 ATF_REQUIRE(WIFSTOPPED(status));
571 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
573 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
575 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
576 (PL_FLAG_FORKED | PL_FLAG_CHILD));
577 if (pl.pl_flags & PL_FLAG_CHILD) {
578 ATF_REQUIRE(wpid != parent);
579 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
580 ATF_REQUIRE(!fork_reported[1]);
584 ATF_REQUIRE(child == wpid);
587 fork_reported[1] = true;
589 ATF_REQUIRE(wpid == parent);
590 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
591 ATF_REQUIRE(!fork_reported[0]);
593 child = pl.pl_child_pid;
595 ATF_REQUIRE(child == pl.pl_child_pid);
598 fork_reported[0] = true;
606 * Verify that a new child process is stopped after a followed fork and
607 * that the traced parent sees the exit of the child after the debugger
608 * when both processes remain attached to the debugger.
610 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
611 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
613 pid_t children[2], fpid, wpid;
616 ATF_REQUIRE((fpid = fork()) != -1);
619 follow_fork_parent(false);
622 /* Parent process. */
625 /* The first wait() should report the stop from SIGSTOP. */
626 wpid = waitpid(children[0], &status, 0);
627 ATF_REQUIRE(wpid == children[0]);
628 ATF_REQUIRE(WIFSTOPPED(status));
629 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
631 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
633 /* Continue the child ignoring the SIGSTOP. */
634 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
636 children[1] = handle_fork_events(children[0], NULL);
637 ATF_REQUIRE(children[1] > 0);
639 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
640 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
643 * The child can't exit until the grandchild reports status, so the
644 * grandchild should report its exit first to the debugger.
646 wpid = wait(&status);
647 ATF_REQUIRE(wpid == children[1]);
648 ATF_REQUIRE(WIFEXITED(status));
649 ATF_REQUIRE(WEXITSTATUS(status) == 2);
651 wpid = wait(&status);
652 ATF_REQUIRE(wpid == children[0]);
653 ATF_REQUIRE(WIFEXITED(status));
654 ATF_REQUIRE(WEXITSTATUS(status) == 1);
656 wpid = wait(&status);
657 ATF_REQUIRE(wpid == -1);
658 ATF_REQUIRE(errno == ECHILD);
662 * Verify that a new child process is stopped after a followed fork
663 * and that the traced parent sees the exit of the child when the new
664 * child process is detached after it reports its fork.
666 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
667 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
669 pid_t children[2], fpid, wpid;
672 ATF_REQUIRE((fpid = fork()) != -1);
675 follow_fork_parent(false);
678 /* Parent process. */
681 /* The first wait() should report the stop from SIGSTOP. */
682 wpid = waitpid(children[0], &status, 0);
683 ATF_REQUIRE(wpid == children[0]);
684 ATF_REQUIRE(WIFSTOPPED(status));
685 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
687 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
689 /* Continue the child ignoring the SIGSTOP. */
690 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
692 children[1] = handle_fork_events(children[0], NULL);
693 ATF_REQUIRE(children[1] > 0);
695 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
696 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
699 * Should not see any status from the grandchild now, only the
702 wpid = wait(&status);
703 ATF_REQUIRE(wpid == children[0]);
704 ATF_REQUIRE(WIFEXITED(status));
705 ATF_REQUIRE(WEXITSTATUS(status) == 1);
707 wpid = wait(&status);
708 ATF_REQUIRE(wpid == -1);
709 ATF_REQUIRE(errno == ECHILD);
713 * Verify that a new child process is stopped after a followed fork
714 * and that the traced parent sees the exit of the child when the
715 * traced parent is detached after the fork.
717 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
718 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
720 pid_t children[2], fpid, wpid;
723 ATF_REQUIRE((fpid = fork()) != -1);
726 follow_fork_parent(false);
729 /* Parent process. */
732 /* The first wait() should report the stop from SIGSTOP. */
733 wpid = waitpid(children[0], &status, 0);
734 ATF_REQUIRE(wpid == children[0]);
735 ATF_REQUIRE(WIFSTOPPED(status));
736 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
738 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
740 /* Continue the child ignoring the SIGSTOP. */
741 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
743 children[1] = handle_fork_events(children[0], NULL);
744 ATF_REQUIRE(children[1] > 0);
746 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
747 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
750 * The child can't exit until the grandchild reports status, so the
751 * grandchild should report its exit first to the debugger.
753 * Even though the child process is detached, it is still a
754 * child of the debugger, so it will still report it's exit
755 * after the grandchild.
757 wpid = wait(&status);
758 ATF_REQUIRE(wpid == children[1]);
759 ATF_REQUIRE(WIFEXITED(status));
760 ATF_REQUIRE(WEXITSTATUS(status) == 2);
762 wpid = wait(&status);
763 ATF_REQUIRE(wpid == children[0]);
764 ATF_REQUIRE(WIFEXITED(status));
765 ATF_REQUIRE(WEXITSTATUS(status) == 1);
767 wpid = wait(&status);
768 ATF_REQUIRE(wpid == -1);
769 ATF_REQUIRE(errno == ECHILD);
773 attach_fork_parent(int cpipe[2])
779 /* Double-fork to disassociate from the debugger. */
780 CHILD_REQUIRE((fpid = fork()) != -1);
784 /* Send the pid of the disassociated child to the debugger. */
786 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
788 /* Wait for the debugger to attach. */
789 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
793 * Verify that a new child process is stopped after a followed fork and
794 * that the traced parent sees the exit of the child after the debugger
795 * when both processes remain attached to the debugger. In this test
796 * the parent that forks is not a direct child of the debugger.
798 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
799 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
801 pid_t children[2], fpid, wpid;
802 int cpipe[2], status;
804 atf_tc_skip("https://bugs.freebsd.org/239397");
806 ATF_REQUIRE(pipe(cpipe) == 0);
807 ATF_REQUIRE((fpid = fork()) != -1);
809 attach_fork_parent(cpipe);
810 follow_fork_parent(false);
813 /* Parent process. */
816 /* Wait for the direct child to exit. */
817 wpid = waitpid(fpid, &status, 0);
818 ATF_REQUIRE(wpid == fpid);
819 ATF_REQUIRE(WIFEXITED(status));
820 ATF_REQUIRE(WEXITSTATUS(status) == 3);
822 /* Read the pid of the fork parent. */
823 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
824 sizeof(children[0]));
826 /* Attach to the fork parent. */
827 attach_child(children[0]);
829 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
831 /* Continue the fork parent ignoring the SIGSTOP. */
832 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
834 /* Signal the fork parent to continue. */
837 children[1] = handle_fork_events(children[0], NULL);
838 ATF_REQUIRE(children[1] > 0);
840 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
841 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
844 * The fork parent can't exit until the child reports status,
845 * so the child should report its exit first to the debugger.
847 wpid = wait(&status);
848 ATF_REQUIRE(wpid == children[1]);
849 ATF_REQUIRE(WIFEXITED(status));
850 ATF_REQUIRE(WEXITSTATUS(status) == 2);
852 wpid = wait(&status);
853 ATF_REQUIRE(wpid == children[0]);
854 ATF_REQUIRE(WIFEXITED(status));
855 ATF_REQUIRE(WEXITSTATUS(status) == 1);
857 wpid = wait(&status);
858 ATF_REQUIRE(wpid == -1);
859 ATF_REQUIRE(errno == ECHILD);
863 * Verify that a new child process is stopped after a followed fork
864 * and that the traced parent sees the exit of the child when the new
865 * child process is detached after it reports its fork. In this test
866 * the parent that forks is not a direct child of the debugger.
868 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
869 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
871 pid_t children[2], fpid, wpid;
872 int cpipe[2], status;
874 atf_tc_skip("https://bugs.freebsd.org/239292");
876 ATF_REQUIRE(pipe(cpipe) == 0);
877 ATF_REQUIRE((fpid = fork()) != -1);
879 attach_fork_parent(cpipe);
880 follow_fork_parent(false);
883 /* Parent process. */
886 /* Wait for the direct child to exit. */
887 wpid = waitpid(fpid, &status, 0);
888 ATF_REQUIRE(wpid == fpid);
889 ATF_REQUIRE(WIFEXITED(status));
890 ATF_REQUIRE(WEXITSTATUS(status) == 3);
892 /* Read the pid of the fork parent. */
893 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
894 sizeof(children[0]));
896 /* Attach to the fork parent. */
897 attach_child(children[0]);
899 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
901 /* Continue the fork parent ignoring the SIGSTOP. */
902 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
904 /* Signal the fork parent to continue. */
907 children[1] = handle_fork_events(children[0], NULL);
908 ATF_REQUIRE(children[1] > 0);
910 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
911 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
914 * Should not see any status from the child now, only the fork
917 wpid = wait(&status);
918 ATF_REQUIRE(wpid == children[0]);
919 ATF_REQUIRE(WIFEXITED(status));
920 ATF_REQUIRE(WEXITSTATUS(status) == 1);
922 wpid = wait(&status);
923 ATF_REQUIRE(wpid == -1);
924 ATF_REQUIRE(errno == ECHILD);
928 * Verify that a new child process is stopped after a followed fork
929 * and that the traced parent sees the exit of the child when the
930 * traced parent is detached after the fork. In this test the parent
931 * that forks is not a direct child of the debugger.
933 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
934 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
936 pid_t children[2], fpid, wpid;
937 int cpipe[2], status;
939 atf_tc_skip("https://bugs.freebsd.org/239425");
941 ATF_REQUIRE(pipe(cpipe) == 0);
942 ATF_REQUIRE((fpid = fork()) != -1);
944 attach_fork_parent(cpipe);
945 follow_fork_parent(false);
948 /* Parent process. */
951 /* Wait for the direct child to exit. */
952 wpid = waitpid(fpid, &status, 0);
953 ATF_REQUIRE(wpid == fpid);
954 ATF_REQUIRE(WIFEXITED(status));
955 ATF_REQUIRE(WEXITSTATUS(status) == 3);
957 /* Read the pid of the fork parent. */
958 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
959 sizeof(children[0]));
961 /* Attach to the fork parent. */
962 attach_child(children[0]);
964 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
966 /* Continue the fork parent ignoring the SIGSTOP. */
967 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
969 /* Signal the fork parent to continue. */
972 children[1] = handle_fork_events(children[0], NULL);
973 ATF_REQUIRE(children[1] > 0);
975 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
976 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
979 * Should not see any status from the fork parent now, only
982 wpid = wait(&status);
983 ATF_REQUIRE(wpid == children[1]);
984 ATF_REQUIRE(WIFEXITED(status));
985 ATF_REQUIRE(WEXITSTATUS(status) == 2);
987 wpid = wait(&status);
988 ATF_REQUIRE(wpid == -1);
989 ATF_REQUIRE(errno == ECHILD);
993 * Verify that a child process does not see an unrelated debugger as its
994 * parent but sees its original parent process.
996 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
997 ATF_TC_BODY(ptrace__getppid, tc)
999 pid_t child, debugger, ppid, wpid;
1000 int cpipe[2], dpipe[2], status;
1003 ATF_REQUIRE(pipe(cpipe) == 0);
1004 ATF_REQUIRE((child = fork()) != -1);
1007 /* Child process. */
1010 /* Wait for parent to be ready. */
1011 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1013 /* Report the parent PID to the parent. */
1015 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1022 ATF_REQUIRE(pipe(dpipe) == 0);
1023 ATF_REQUIRE((debugger = fork()) != -1);
1025 if (debugger == 0) {
1026 /* Debugger process. */
1029 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1031 wpid = waitpid(child, &status, 0);
1032 CHILD_REQUIRE(wpid == child);
1033 CHILD_REQUIRE(WIFSTOPPED(status));
1034 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1036 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1038 /* Signal parent that debugger is attached. */
1039 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1041 /* Wait for traced child to exit. */
1042 wpid = waitpid(child, &status, 0);
1043 CHILD_REQUIRE(wpid == child);
1044 CHILD_REQUIRE(WIFEXITED(status));
1045 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1051 /* Parent process. */
1053 /* Wait for the debugger to attach to the child. */
1054 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1056 /* Release the child. */
1057 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1059 /* Read the parent PID from the child. */
1060 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1063 ATF_REQUIRE(ppid == getpid());
1065 /* Wait for the debugger. */
1066 wpid = waitpid(debugger, &status, 0);
1067 ATF_REQUIRE(wpid == debugger);
1068 ATF_REQUIRE(WIFEXITED(status));
1069 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1071 /* The child process should now be ready. */
1072 wpid = waitpid(child, &status, WNOHANG);
1073 ATF_REQUIRE(wpid == child);
1074 ATF_REQUIRE(WIFEXITED(status));
1075 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1079 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1080 * child process created via fork() reports the correct value.
1082 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1083 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1085 struct ptrace_lwpinfo pl[2];
1086 pid_t children[2], fpid, wpid;
1089 ATF_REQUIRE((fpid = fork()) != -1);
1092 follow_fork_parent(false);
1095 /* Parent process. */
1098 /* The first wait() should report the stop from SIGSTOP. */
1099 wpid = waitpid(children[0], &status, 0);
1100 ATF_REQUIRE(wpid == children[0]);
1101 ATF_REQUIRE(WIFSTOPPED(status));
1102 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1104 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1106 /* Continue the child ignoring the SIGSTOP. */
1107 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1109 /* Wait for both halves of the fork event to get reported. */
1110 children[1] = handle_fork_events(children[0], pl);
1111 ATF_REQUIRE(children[1] > 0);
1113 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1114 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1115 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1116 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1117 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1119 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1120 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1123 * The child can't exit until the grandchild reports status, so the
1124 * grandchild should report its exit first to the debugger.
1126 wpid = wait(&status);
1127 ATF_REQUIRE(wpid == children[1]);
1128 ATF_REQUIRE(WIFEXITED(status));
1129 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1131 wpid = wait(&status);
1132 ATF_REQUIRE(wpid == children[0]);
1133 ATF_REQUIRE(WIFEXITED(status));
1134 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1136 wpid = wait(&status);
1137 ATF_REQUIRE(wpid == -1);
1138 ATF_REQUIRE(errno == ECHILD);
1142 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1143 * child process created via vfork() reports the correct value.
1145 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1146 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1148 struct ptrace_lwpinfo pl[2];
1149 pid_t children[2], fpid, wpid;
1152 ATF_REQUIRE((fpid = fork()) != -1);
1155 follow_fork_parent(true);
1158 /* Parent process. */
1161 /* The first wait() should report the stop from SIGSTOP. */
1162 wpid = waitpid(children[0], &status, 0);
1163 ATF_REQUIRE(wpid == children[0]);
1164 ATF_REQUIRE(WIFSTOPPED(status));
1165 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1167 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1169 /* Continue the child ignoring the SIGSTOP. */
1170 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1172 /* Wait for both halves of the fork event to get reported. */
1173 children[1] = handle_fork_events(children[0], pl);
1174 ATF_REQUIRE(children[1] > 0);
1176 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1177 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1178 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1179 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1180 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1182 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1183 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1186 * The child can't exit until the grandchild reports status, so the
1187 * grandchild should report its exit first to the debugger.
1189 wpid = wait(&status);
1190 ATF_REQUIRE(wpid == children[1]);
1191 ATF_REQUIRE(WIFEXITED(status));
1192 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1194 wpid = wait(&status);
1195 ATF_REQUIRE(wpid == children[0]);
1196 ATF_REQUIRE(WIFEXITED(status));
1197 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1199 wpid = wait(&status);
1200 ATF_REQUIRE(wpid == -1);
1201 ATF_REQUIRE(errno == ECHILD);
1205 simple_thread(void *arg __unused)
1212 simple_thread_main(void)
1216 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1217 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1222 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1223 * thread reports the correct value.
1225 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1226 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1228 struct ptrace_lwpinfo pl;
1233 ATF_REQUIRE((fpid = fork()) != -1);
1236 simple_thread_main();
1239 /* The first wait() should report the stop from SIGSTOP. */
1240 wpid = waitpid(fpid, &status, 0);
1241 ATF_REQUIRE(wpid == fpid);
1242 ATF_REQUIRE(WIFSTOPPED(status));
1243 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1245 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1247 mainlwp = pl.pl_lwpid;
1250 * Continue the child ignoring the SIGSTOP and tracing all
1251 * system call exits.
1253 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1256 * Wait for the new thread to arrive. pthread_create() might
1257 * invoke any number of system calls. For now we just wait
1258 * for the new thread to arrive and make sure it reports a
1259 * valid system call code. If ptrace grows thread event
1260 * reporting then this test can be made more precise.
1263 wpid = waitpid(fpid, &status, 0);
1264 ATF_REQUIRE(wpid == fpid);
1265 ATF_REQUIRE(WIFSTOPPED(status));
1266 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1268 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1270 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1271 ATF_REQUIRE(pl.pl_syscall_code != 0);
1272 if (pl.pl_lwpid != mainlwp)
1273 /* New thread seen. */
1276 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1279 /* Wait for the child to exit. */
1280 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1282 wpid = waitpid(fpid, &status, 0);
1283 ATF_REQUIRE(wpid == fpid);
1284 if (WIFEXITED(status))
1287 ATF_REQUIRE(WIFSTOPPED(status));
1288 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1289 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1292 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1294 wpid = wait(&status);
1295 ATF_REQUIRE(wpid == -1);
1296 ATF_REQUIRE(errno == ECHILD);
1300 * Verify that the expected LWP events are reported for a child thread.
1302 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1303 ATF_TC_BODY(ptrace__lwp_events, tc)
1305 struct ptrace_lwpinfo pl;
1310 ATF_REQUIRE((fpid = fork()) != -1);
1313 simple_thread_main();
1316 /* The first wait() should report the stop from SIGSTOP. */
1317 wpid = waitpid(fpid, &status, 0);
1318 ATF_REQUIRE(wpid == fpid);
1319 ATF_REQUIRE(WIFSTOPPED(status));
1320 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1322 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1324 lwps[0] = pl.pl_lwpid;
1326 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1328 /* Continue the child ignoring the SIGSTOP. */
1329 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1331 /* The first event should be for the child thread's birth. */
1332 wpid = waitpid(fpid, &status, 0);
1333 ATF_REQUIRE(wpid == fpid);
1334 ATF_REQUIRE(WIFSTOPPED(status));
1335 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1337 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1338 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1339 (PL_FLAG_BORN | PL_FLAG_SCX));
1340 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1341 lwps[1] = pl.pl_lwpid;
1343 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1345 /* The next event should be for the child thread's death. */
1346 wpid = waitpid(fpid, &status, 0);
1347 ATF_REQUIRE(wpid == fpid);
1348 ATF_REQUIRE(WIFSTOPPED(status));
1349 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1351 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1352 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1353 (PL_FLAG_EXITED | PL_FLAG_SCE));
1354 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1356 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1358 /* The last event should be for the child process's exit. */
1359 wpid = waitpid(fpid, &status, 0);
1360 ATF_REQUIRE(WIFEXITED(status));
1361 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1363 wpid = wait(&status);
1364 ATF_REQUIRE(wpid == -1);
1365 ATF_REQUIRE(errno == ECHILD);
1369 exec_thread(void *arg __unused)
1372 execl("/usr/bin/true", "true", NULL);
1377 exec_thread_main(void)
1381 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1388 * Verify that the expected LWP events are reported for a multithreaded
1389 * process that calls execve(2).
1391 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1392 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1394 struct ptrace_lwpinfo pl;
1399 ATF_REQUIRE((fpid = fork()) != -1);
1405 /* The first wait() should report the stop from SIGSTOP. */
1406 wpid = waitpid(fpid, &status, 0);
1407 ATF_REQUIRE(wpid == fpid);
1408 ATF_REQUIRE(WIFSTOPPED(status));
1409 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1411 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1413 lwps[0] = pl.pl_lwpid;
1415 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1417 /* Continue the child ignoring the SIGSTOP. */
1418 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1420 /* The first event should be for the child thread's birth. */
1421 wpid = waitpid(fpid, &status, 0);
1422 ATF_REQUIRE(wpid == fpid);
1423 ATF_REQUIRE(WIFSTOPPED(status));
1424 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1426 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1427 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1428 (PL_FLAG_BORN | PL_FLAG_SCX));
1429 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1430 lwps[1] = pl.pl_lwpid;
1432 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1435 * The next event should be for the main thread's death due to
1436 * single threading from execve().
1438 wpid = waitpid(fpid, &status, 0);
1439 ATF_REQUIRE(wpid == fpid);
1440 ATF_REQUIRE(WIFSTOPPED(status));
1441 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1443 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1444 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1446 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1448 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1450 /* The next event should be for the child process's exec. */
1451 wpid = waitpid(fpid, &status, 0);
1452 ATF_REQUIRE(WIFSTOPPED(status));
1453 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1455 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1456 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1457 (PL_FLAG_EXEC | PL_FLAG_SCX));
1458 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1460 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1462 /* The last event should be for the child process's exit. */
1463 wpid = waitpid(fpid, &status, 0);
1464 ATF_REQUIRE(WIFEXITED(status));
1465 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1467 wpid = wait(&status);
1468 ATF_REQUIRE(wpid == -1);
1469 ATF_REQUIRE(errno == ECHILD);
1473 handler(int sig __unused)
1481 signal(SIGINFO, handler);
1487 * Verify that the expected ptrace event is reported for a signal.
1489 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1490 ATF_TC_BODY(ptrace__siginfo, tc)
1492 struct ptrace_lwpinfo pl;
1496 ATF_REQUIRE((fpid = fork()) != -1);
1502 /* The first wait() should report the stop from SIGSTOP. */
1503 wpid = waitpid(fpid, &status, 0);
1504 ATF_REQUIRE(wpid == fpid);
1505 ATF_REQUIRE(WIFSTOPPED(status));
1506 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1508 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1510 /* The next event should be for the SIGINFO. */
1511 wpid = waitpid(fpid, &status, 0);
1512 ATF_REQUIRE(WIFSTOPPED(status));
1513 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1515 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1516 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1517 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1518 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1519 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1521 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1523 /* The last event should be for the child process's exit. */
1524 wpid = waitpid(fpid, &status, 0);
1525 ATF_REQUIRE(WIFEXITED(status));
1526 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1528 wpid = wait(&status);
1529 ATF_REQUIRE(wpid == -1);
1530 ATF_REQUIRE(errno == ECHILD);
1534 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1536 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1537 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1542 ATF_REQUIRE((fpid = fork()) != -1);
1548 /* The first wait() should report the stop from SIGSTOP. */
1549 wpid = waitpid(fpid, &status, 0);
1550 ATF_REQUIRE(wpid == fpid);
1551 ATF_REQUIRE(WIFSTOPPED(status));
1552 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1555 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1556 sizeof(events)) == 0);
1558 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1560 /* Should get one event at exit. */
1561 wpid = waitpid(fpid, &status, 0);
1562 ATF_REQUIRE(WIFEXITED(status));
1563 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1565 wpid = wait(&status);
1566 ATF_REQUIRE(wpid == -1);
1567 ATF_REQUIRE(errno == ECHILD);
1570 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1571 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1573 struct ptrace_lwpinfo pl;
1577 ATF_REQUIRE((fpid = fork()) != -1);
1583 /* The first wait() should report the stop from SIGSTOP. */
1584 wpid = waitpid(fpid, &status, 0);
1585 ATF_REQUIRE(wpid == fpid);
1586 ATF_REQUIRE(WIFSTOPPED(status));
1587 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1589 events = PTRACE_EXEC;
1590 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1591 sizeof(events)) == 0);
1593 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1595 /* The next event should be for the child process's exec. */
1596 wpid = waitpid(fpid, &status, 0);
1597 ATF_REQUIRE(WIFSTOPPED(status));
1598 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1600 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1601 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1602 (PL_FLAG_EXEC | PL_FLAG_SCX));
1604 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1606 /* The last event should be for the child process's exit. */
1607 wpid = waitpid(fpid, &status, 0);
1608 ATF_REQUIRE(WIFEXITED(status));
1609 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1611 wpid = wait(&status);
1612 ATF_REQUIRE(wpid == -1);
1613 ATF_REQUIRE(errno == ECHILD);
1616 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1617 ATF_TC_BODY(ptrace__event_mask, tc)
1622 ATF_REQUIRE((fpid = fork()) != -1);
1628 /* The first wait() should report the stop from SIGSTOP. */
1629 wpid = waitpid(fpid, &status, 0);
1630 ATF_REQUIRE(wpid == fpid);
1631 ATF_REQUIRE(WIFSTOPPED(status));
1632 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1634 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1635 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1636 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1637 sizeof(events)) == 0);
1638 ATF_REQUIRE(events & PTRACE_FORK);
1639 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1640 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1641 sizeof(events)) == 0);
1642 ATF_REQUIRE(!(events & PTRACE_FORK));
1644 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1645 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1646 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1647 sizeof(events)) == 0);
1648 ATF_REQUIRE(events & PTRACE_LWP);
1649 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1650 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1651 sizeof(events)) == 0);
1652 ATF_REQUIRE(!(events & PTRACE_LWP));
1654 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1656 /* Should get one event at exit. */
1657 wpid = waitpid(fpid, &status, 0);
1658 ATF_REQUIRE(WIFEXITED(status));
1659 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1661 wpid = wait(&status);
1662 ATF_REQUIRE(wpid == -1);
1663 ATF_REQUIRE(errno == ECHILD);
1667 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1669 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1670 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1672 struct ptrace_lwpinfo pl;
1676 ATF_REQUIRE((fpid = fork()) != -1);
1679 follow_fork_parent(true);
1682 /* The first wait() should report the stop from SIGSTOP. */
1683 wpid = waitpid(fpid, &status, 0);
1684 ATF_REQUIRE(wpid == fpid);
1685 ATF_REQUIRE(WIFSTOPPED(status));
1686 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1688 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1689 sizeof(events)) == 0);
1690 events |= PTRACE_VFORK;
1691 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1692 sizeof(events)) == 0);
1694 /* Continue the child ignoring the SIGSTOP. */
1695 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1697 /* The next event should report the end of the vfork. */
1698 wpid = wait(&status);
1699 ATF_REQUIRE(wpid == fpid);
1700 ATF_REQUIRE(WIFSTOPPED(status));
1701 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1702 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1703 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1705 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1707 wpid = wait(&status);
1708 ATF_REQUIRE(wpid == fpid);
1709 ATF_REQUIRE(WIFEXITED(status));
1710 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1712 wpid = wait(&status);
1713 ATF_REQUIRE(wpid == -1);
1714 ATF_REQUIRE(errno == ECHILD);
1717 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1718 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1720 struct ptrace_lwpinfo pl[2];
1721 pid_t children[2], fpid, wpid;
1724 ATF_REQUIRE((fpid = fork()) != -1);
1727 follow_fork_parent(true);
1730 /* Parent process. */
1733 /* The first wait() should report the stop from SIGSTOP. */
1734 wpid = waitpid(children[0], &status, 0);
1735 ATF_REQUIRE(wpid == children[0]);
1736 ATF_REQUIRE(WIFSTOPPED(status));
1737 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1739 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1740 sizeof(events)) == 0);
1741 events |= PTRACE_FORK | PTRACE_VFORK;
1742 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1743 sizeof(events)) == 0);
1745 /* Continue the child ignoring the SIGSTOP. */
1746 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1748 /* Wait for both halves of the fork event to get reported. */
1749 children[1] = handle_fork_events(children[0], pl);
1750 ATF_REQUIRE(children[1] > 0);
1752 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1754 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1755 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1758 * The child can't exit until the grandchild reports status, so the
1759 * grandchild should report its exit first to the debugger.
1761 wpid = waitpid(children[1], &status, 0);
1762 ATF_REQUIRE(wpid == children[1]);
1763 ATF_REQUIRE(WIFEXITED(status));
1764 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1767 * The child should report it's vfork() completion before it
1770 wpid = wait(&status);
1771 ATF_REQUIRE(wpid == children[0]);
1772 ATF_REQUIRE(WIFSTOPPED(status));
1773 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1774 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1776 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1778 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1780 wpid = wait(&status);
1781 ATF_REQUIRE(wpid == children[0]);
1782 ATF_REQUIRE(WIFEXITED(status));
1783 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1785 wpid = wait(&status);
1786 ATF_REQUIRE(wpid == -1);
1787 ATF_REQUIRE(errno == ECHILD);
1790 #ifdef HAVE_BREAKPOINT
1792 * Verify that no more events are reported after PT_KILL except for the
1793 * process exit when stopped due to a breakpoint trap.
1795 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1796 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1801 ATF_REQUIRE((fpid = fork()) != -1);
1808 /* The first wait() should report the stop from SIGSTOP. */
1809 wpid = waitpid(fpid, &status, 0);
1810 ATF_REQUIRE(wpid == fpid);
1811 ATF_REQUIRE(WIFSTOPPED(status));
1812 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1814 /* Continue the child ignoring the SIGSTOP. */
1815 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1817 /* The second wait() should report hitting the breakpoint. */
1818 wpid = waitpid(fpid, &status, 0);
1819 ATF_REQUIRE(wpid == fpid);
1820 ATF_REQUIRE(WIFSTOPPED(status));
1821 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1823 /* Kill the child process. */
1824 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1826 /* The last wait() should report the SIGKILL. */
1827 wpid = waitpid(fpid, &status, 0);
1828 ATF_REQUIRE(wpid == fpid);
1829 ATF_REQUIRE(WIFSIGNALED(status));
1830 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1832 wpid = wait(&status);
1833 ATF_REQUIRE(wpid == -1);
1834 ATF_REQUIRE(errno == ECHILD);
1836 #endif /* HAVE_BREAKPOINT */
1839 * Verify that no more events are reported after PT_KILL except for the
1840 * process exit when stopped inside of a system call.
1842 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1843 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1845 struct ptrace_lwpinfo pl;
1849 ATF_REQUIRE((fpid = fork()) != -1);
1856 /* The first wait() should report the stop from SIGSTOP. */
1857 wpid = waitpid(fpid, &status, 0);
1858 ATF_REQUIRE(wpid == fpid);
1859 ATF_REQUIRE(WIFSTOPPED(status));
1860 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1862 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1863 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1865 /* The second wait() should report a system call entry for getpid(). */
1866 wpid = waitpid(fpid, &status, 0);
1867 ATF_REQUIRE(wpid == fpid);
1868 ATF_REQUIRE(WIFSTOPPED(status));
1869 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1871 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1872 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1874 /* Kill the child process. */
1875 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1877 /* The last wait() should report the SIGKILL. */
1878 wpid = waitpid(fpid, &status, 0);
1879 ATF_REQUIRE(wpid == fpid);
1880 ATF_REQUIRE(WIFSIGNALED(status));
1881 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1883 wpid = wait(&status);
1884 ATF_REQUIRE(wpid == -1);
1885 ATF_REQUIRE(errno == ECHILD);
1889 * Verify that no more events are reported after PT_KILL except for the
1890 * process exit when killing a multithreaded process.
1892 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1893 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1895 struct ptrace_lwpinfo pl;
1900 ATF_REQUIRE((fpid = fork()) != -1);
1903 simple_thread_main();
1906 /* The first wait() should report the stop from SIGSTOP. */
1907 wpid = waitpid(fpid, &status, 0);
1908 ATF_REQUIRE(wpid == fpid);
1909 ATF_REQUIRE(WIFSTOPPED(status));
1910 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1912 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1914 main_lwp = pl.pl_lwpid;
1916 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1918 /* Continue the child ignoring the SIGSTOP. */
1919 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1921 /* The first event should be for the child thread's birth. */
1922 wpid = waitpid(fpid, &status, 0);
1923 ATF_REQUIRE(wpid == fpid);
1924 ATF_REQUIRE(WIFSTOPPED(status));
1925 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1927 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1928 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1929 (PL_FLAG_BORN | PL_FLAG_SCX));
1930 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1932 /* Kill the child process. */
1933 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1935 /* The last wait() should report the SIGKILL. */
1936 wpid = waitpid(fpid, &status, 0);
1937 ATF_REQUIRE(wpid == fpid);
1938 ATF_REQUIRE(WIFSIGNALED(status));
1939 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1941 wpid = wait(&status);
1942 ATF_REQUIRE(wpid == -1);
1943 ATF_REQUIRE(errno == ECHILD);
1947 mask_usr1_thread(void *arg)
1949 pthread_barrier_t *pbarrier;
1952 pbarrier = (pthread_barrier_t*)arg;
1954 sigemptyset(&sigmask);
1955 sigaddset(&sigmask, SIGUSR1);
1956 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1958 /* Sync up with other thread after sigmask updated. */
1959 pthread_barrier_wait(pbarrier);
1968 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1969 * and prevents spurious stops due to those other signals.
1971 ATF_TC(ptrace__PT_KILL_competing_signal);
1972 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1975 atf_tc_set_md_var(tc, "require.user", "root");
1977 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1983 pthread_barrier_t barrier;
1984 struct sched_param sched_param;
1986 ATF_REQUIRE((fpid = fork()) != -1);
1988 /* Bind to one CPU so only one thread at a time will run. */
1990 CPU_SET(0, &setmask);
1992 CHILD_REQUIRE(cpuset(&setid) == 0);
1993 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1994 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1996 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1998 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1999 (void*)&barrier) == 0);
2002 * Give the main thread higher priority. The test always
2003 * assumes that, if both threads are able to run, the main
2004 * thread runs first.
2006 sched_param.sched_priority =
2007 (sched_get_priority_max(SCHED_FIFO) +
2008 sched_get_priority_min(SCHED_FIFO)) / 2;
2009 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2010 SCHED_FIFO, &sched_param) == 0);
2011 sched_param.sched_priority -= RQ_PPQ;
2012 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2013 &sched_param) == 0);
2016 sigemptyset(&sigmask);
2017 sigaddset(&sigmask, SIGUSR2);
2018 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2020 /* Sync up with other thread after sigmask updated. */
2021 pthread_barrier_wait(&barrier);
2031 /* The first wait() should report the stop from SIGSTOP. */
2032 wpid = waitpid(fpid, &status, 0);
2033 ATF_REQUIRE(wpid == fpid);
2034 ATF_REQUIRE(WIFSTOPPED(status));
2035 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2037 /* Continue the child ignoring the SIGSTOP. */
2038 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2040 /* Send a signal that only the second thread can handle. */
2041 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2043 /* The second wait() should report the SIGUSR2. */
2044 wpid = waitpid(fpid, &status, 0);
2045 ATF_REQUIRE(wpid == fpid);
2046 ATF_REQUIRE(WIFSTOPPED(status));
2047 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2049 /* Send a signal that only the first thread can handle. */
2050 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2052 /* Replace the SIGUSR2 with a kill. */
2053 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2055 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2056 wpid = waitpid(fpid, &status, 0);
2057 ATF_REQUIRE(wpid == fpid);
2058 ATF_REQUIRE(WIFSIGNALED(status));
2059 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2061 wpid = wait(&status);
2062 ATF_REQUIRE(wpid == -1);
2063 ATF_REQUIRE(errno == ECHILD);
2067 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2068 * and prevents spurious stops caused by those events.
2070 ATF_TC(ptrace__PT_KILL_competing_stop);
2071 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2074 atf_tc_set_md_var(tc, "require.user", "root");
2076 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2082 pthread_barrier_t barrier;
2084 struct ptrace_lwpinfo pl;
2085 struct sched_param sched_param;
2087 atf_tc_skip("https://bugs.freebsd.org/220841");
2089 ATF_REQUIRE((fpid = fork()) != -1);
2093 /* Bind to one CPU so only one thread at a time will run. */
2095 CPU_SET(0, &setmask);
2097 CHILD_REQUIRE(cpuset(&setid) == 0);
2098 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2099 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2101 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2103 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2104 (void*)&barrier) == 0);
2107 * Give the main thread higher priority. The test always
2108 * assumes that, if both threads are able to run, the main
2109 * thread runs first.
2111 sched_param.sched_priority =
2112 (sched_get_priority_max(SCHED_FIFO) +
2113 sched_get_priority_min(SCHED_FIFO)) / 2;
2114 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2115 SCHED_FIFO, &sched_param) == 0);
2116 sched_param.sched_priority -= RQ_PPQ;
2117 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2118 &sched_param) == 0);
2121 sigemptyset(&sigmask);
2122 sigaddset(&sigmask, SIGUSR2);
2123 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2125 /* Sync up with other thread after sigmask updated. */
2126 pthread_barrier_wait(&barrier);
2128 /* Sync up with the test before doing the getpid(). */
2135 /* The first wait() should report the stop from SIGSTOP. */
2136 wpid = waitpid(fpid, &status, 0);
2137 ATF_REQUIRE(wpid == fpid);
2138 ATF_REQUIRE(WIFSTOPPED(status));
2139 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2141 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2142 main_lwp = pl.pl_lwpid;
2144 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2145 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2148 * Continue until child is done with setup, which is indicated with
2149 * SIGSTOP. Ignore system calls in the meantime.
2152 wpid = waitpid(fpid, &status, 0);
2153 ATF_REQUIRE(wpid == fpid);
2154 ATF_REQUIRE(WIFSTOPPED(status));
2155 if (WSTOPSIG(status) == SIGTRAP) {
2156 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2158 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2160 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2163 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2166 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2167 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2169 wpid = waitpid(fpid, &status, 0);
2170 ATF_REQUIRE(wpid == fpid);
2171 ATF_REQUIRE(WIFSTOPPED(status));
2172 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2174 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2176 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2177 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2178 /* Prevent the main thread from hitting its syscall exit for now. */
2179 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2182 * Proceed, allowing second thread to hit syscall exit for
2183 * pthread_barrier_wait().
2185 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2187 wpid = waitpid(fpid, &status, 0);
2188 ATF_REQUIRE(wpid == fpid);
2189 ATF_REQUIRE(WIFSTOPPED(status));
2190 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2192 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2194 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2195 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2197 /* Send a signal that only the second thread can handle. */
2198 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2200 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2202 /* The next wait() should report the SIGUSR2. */
2203 wpid = waitpid(fpid, &status, 0);
2204 ATF_REQUIRE(wpid == fpid);
2205 ATF_REQUIRE(WIFSTOPPED(status));
2206 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2208 /* Allow the main thread to try to finish its system call. */
2209 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2212 * At this point, the main thread is in the middle of a system call and
2213 * has been resumed. The second thread has taken a SIGUSR2 which will
2214 * be replaced with a SIGKILL below. The main thread will get to run
2215 * first. It should notice the kill request (even though the signal
2216 * replacement occurred in the other thread) and exit accordingly. It
2217 * should not stop for the system call exit event.
2220 /* Replace the SIGUSR2 with a kill. */
2221 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2223 /* The last wait() should report the SIGKILL (not a syscall exit). */
2224 wpid = waitpid(fpid, &status, 0);
2225 ATF_REQUIRE(wpid == fpid);
2226 ATF_REQUIRE(WIFSIGNALED(status));
2227 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2229 wpid = wait(&status);
2230 ATF_REQUIRE(wpid == -1);
2231 ATF_REQUIRE(errno == ECHILD);
2235 sigusr1_handler(int sig)
2238 CHILD_REQUIRE(sig == SIGUSR1);
2243 * Verify that even if the signal queue is full for a child process,
2244 * a PT_KILL will kill the process.
2246 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2247 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2251 int max_pending_per_proc;
2255 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2257 ATF_REQUIRE((fpid = fork()) != -1);
2263 /* The first wait() should report the stop from SIGSTOP. */
2264 wpid = waitpid(fpid, &status, 0);
2265 ATF_REQUIRE(wpid == fpid);
2266 ATF_REQUIRE(WIFSTOPPED(status));
2267 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2269 len = sizeof(max_pending_per_proc);
2270 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2271 &max_pending_per_proc, &len, NULL, 0) == 0);
2273 /* Fill the signal queue. */
2274 for (i = 0; i < max_pending_per_proc; ++i)
2275 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2277 /* Kill the child process. */
2278 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2280 /* The last wait() should report the SIGKILL. */
2281 wpid = waitpid(fpid, &status, 0);
2282 ATF_REQUIRE(wpid == fpid);
2283 ATF_REQUIRE(WIFSIGNALED(status));
2284 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2286 wpid = wait(&status);
2287 ATF_REQUIRE(wpid == -1);
2288 ATF_REQUIRE(errno == ECHILD);
2292 * Verify that when stopped at a system call entry, a signal can be
2293 * requested with PT_CONTINUE which will be delivered once the system
2296 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2297 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2299 struct ptrace_lwpinfo pl;
2303 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2305 ATF_REQUIRE((fpid = fork()) != -1);
2312 /* The first wait() should report the stop from SIGSTOP. */
2313 wpid = waitpid(fpid, &status, 0);
2314 ATF_REQUIRE(wpid == fpid);
2315 ATF_REQUIRE(WIFSTOPPED(status));
2316 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2318 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2319 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2321 /* The second wait() should report a system call entry for getpid(). */
2322 wpid = waitpid(fpid, &status, 0);
2323 ATF_REQUIRE(wpid == fpid);
2324 ATF_REQUIRE(WIFSTOPPED(status));
2325 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2327 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2328 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2330 /* Continue the child process with a signal. */
2331 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2335 * The last wait() should report exit 2, i.e., a normal _exit
2336 * from the signal handler. In the meantime, catch and proceed
2337 * past any syscall stops.
2339 wpid = waitpid(fpid, &status, 0);
2340 ATF_REQUIRE(wpid == fpid);
2341 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2342 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2343 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2344 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2346 ATF_REQUIRE(WIFEXITED(status));
2347 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2352 wpid = wait(&status);
2353 ATF_REQUIRE(wpid == -1);
2354 ATF_REQUIRE(errno == ECHILD);
2358 sigusr1_counting_handler(int sig)
2360 static int counter = 0;
2362 CHILD_REQUIRE(sig == SIGUSR1);
2369 * Verify that, when continuing from a stop at system call entry and exit,
2370 * a signal can be requested from both stops, and both will be delivered when
2371 * the system call is complete.
2373 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2374 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2376 struct ptrace_lwpinfo pl;
2380 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2382 ATF_REQUIRE((fpid = fork()) != -1);
2389 /* The first wait() should report the stop from SIGSTOP. */
2390 wpid = waitpid(fpid, &status, 0);
2391 ATF_REQUIRE(wpid == fpid);
2392 ATF_REQUIRE(WIFSTOPPED(status));
2393 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2395 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2396 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2398 /* The second wait() should report a system call entry for getpid(). */
2399 wpid = waitpid(fpid, &status, 0);
2400 ATF_REQUIRE(wpid == fpid);
2401 ATF_REQUIRE(WIFSTOPPED(status));
2402 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2404 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2405 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2407 /* Continue the child process with a signal. */
2408 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2410 /* The third wait() should report a system call exit for getpid(). */
2411 wpid = waitpid(fpid, &status, 0);
2412 ATF_REQUIRE(wpid == fpid);
2413 ATF_REQUIRE(WIFSTOPPED(status));
2414 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2416 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2417 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2419 /* Continue the child process with a signal. */
2420 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2424 * The last wait() should report exit 2, i.e., a normal _exit
2425 * from the signal handler. In the meantime, catch and proceed
2426 * past any syscall stops.
2428 wpid = waitpid(fpid, &status, 0);
2429 ATF_REQUIRE(wpid == fpid);
2430 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2431 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2432 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2433 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2435 ATF_REQUIRE(WIFEXITED(status));
2436 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2441 wpid = wait(&status);
2442 ATF_REQUIRE(wpid == -1);
2443 ATF_REQUIRE(errno == ECHILD);
2447 * Verify that even if the signal queue is full for a child process,
2448 * a PT_CONTINUE with a signal will not result in loss of that signal.
2450 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2451 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2455 int max_pending_per_proc;
2459 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2460 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2462 ATF_REQUIRE((fpid = fork()) != -1);
2468 /* The first wait() should report the stop from SIGSTOP. */
2469 wpid = waitpid(fpid, &status, 0);
2470 ATF_REQUIRE(wpid == fpid);
2471 ATF_REQUIRE(WIFSTOPPED(status));
2472 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2474 len = sizeof(max_pending_per_proc);
2475 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2476 &max_pending_per_proc, &len, NULL, 0) == 0);
2478 /* Fill the signal queue. */
2479 for (i = 0; i < max_pending_per_proc; ++i)
2480 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2482 /* Continue with signal. */
2483 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2486 wpid = waitpid(fpid, &status, 0);
2487 ATF_REQUIRE(wpid == fpid);
2488 if (WIFSTOPPED(status)) {
2489 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2490 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2493 * The last wait() should report normal _exit from the
2496 ATF_REQUIRE(WIFEXITED(status));
2497 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2502 wpid = wait(&status);
2503 ATF_REQUIRE(wpid == -1);
2504 ATF_REQUIRE(errno == ECHILD);
2507 static sem_t sigusr1_sem;
2508 static int got_usr1;
2511 sigusr1_sempost_handler(int sig __unused)
2515 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2519 * Verify that even if the signal queue is full for a child process,
2520 * and the signal is masked, a PT_CONTINUE with a signal will not
2521 * result in loss of that signal.
2523 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2524 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2526 struct ptrace_lwpinfo pl;
2529 int max_pending_per_proc;
2534 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2535 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2536 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2539 ATF_REQUIRE((fpid = fork()) != -1);
2541 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2542 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2543 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2546 CHILD_REQUIRE(got_usr1 == 0);
2548 /* Allow the pending SIGUSR1 in now. */
2549 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2550 /* Wait to receive the SIGUSR1. */
2552 err = sem_wait(&sigusr1_sem);
2553 CHILD_REQUIRE(err == 0 || errno == EINTR);
2554 } while (err != 0 && errno == EINTR);
2555 CHILD_REQUIRE(got_usr1 == 1);
2559 /* The first wait() should report the stop from SIGSTOP. */
2560 wpid = waitpid(fpid, &status, 0);
2561 ATF_REQUIRE(wpid == fpid);
2562 ATF_REQUIRE(WIFSTOPPED(status));
2563 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2565 len = sizeof(max_pending_per_proc);
2566 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2567 &max_pending_per_proc, &len, NULL, 0) == 0);
2569 /* Fill the signal queue. */
2570 for (i = 0; i < max_pending_per_proc; ++i)
2571 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2573 /* Continue with signal. */
2574 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2576 /* Collect and ignore all of the SIGUSR2. */
2577 for (i = 0; i < max_pending_per_proc; ++i) {
2578 wpid = waitpid(fpid, &status, 0);
2579 ATF_REQUIRE(wpid == fpid);
2580 ATF_REQUIRE(WIFSTOPPED(status));
2581 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2582 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2585 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2586 wpid = waitpid(fpid, &status, 0);
2587 ATF_REQUIRE(wpid == fpid);
2588 ATF_REQUIRE(WIFSTOPPED(status));
2589 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2590 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2591 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2593 /* Continue the child, ignoring the SIGUSR1. */
2594 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2596 /* The last wait() should report exit after receiving SIGUSR1. */
2597 wpid = waitpid(fpid, &status, 0);
2598 ATF_REQUIRE(wpid == fpid);
2599 ATF_REQUIRE(WIFEXITED(status));
2600 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2602 wpid = wait(&status);
2603 ATF_REQUIRE(wpid == -1);
2604 ATF_REQUIRE(errno == ECHILD);
2608 * Verify that, after stopping due to a signal, that signal can be
2609 * replaced with another signal.
2611 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2612 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2614 struct ptrace_lwpinfo pl;
2618 ATF_REQUIRE((fpid = fork()) != -1);
2625 /* The first wait() should report the stop from SIGSTOP. */
2626 wpid = waitpid(fpid, &status, 0);
2627 ATF_REQUIRE(wpid == fpid);
2628 ATF_REQUIRE(WIFSTOPPED(status));
2629 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2631 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2633 /* Send a signal without ptrace. */
2634 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2636 /* The second wait() should report a SIGINT was received. */
2637 wpid = waitpid(fpid, &status, 0);
2638 ATF_REQUIRE(wpid == fpid);
2639 ATF_REQUIRE(WIFSTOPPED(status));
2640 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2642 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2643 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2644 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2646 /* Continue the child process with a different signal. */
2647 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2650 * The last wait() should report having died due to the new
2653 wpid = waitpid(fpid, &status, 0);
2654 ATF_REQUIRE(wpid == fpid);
2655 ATF_REQUIRE(WIFSIGNALED(status));
2656 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2658 wpid = wait(&status);
2659 ATF_REQUIRE(wpid == -1);
2660 ATF_REQUIRE(errno == ECHILD);
2664 * Verify that a signal can be passed through to the child even when there
2665 * was no true signal originally. Such cases arise when a SIGTRAP is
2666 * invented for e.g, system call stops.
2668 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2669 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2671 struct ptrace_lwpinfo pl;
2676 ATF_REQUIRE((fpid = fork()) != -1);
2679 /* SIGTRAP expected to cause exit on syscall entry. */
2680 rl.rlim_cur = rl.rlim_max = 0;
2681 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2686 /* The first wait() should report the stop from SIGSTOP. */
2687 wpid = waitpid(fpid, &status, 0);
2688 ATF_REQUIRE(wpid == fpid);
2689 ATF_REQUIRE(WIFSTOPPED(status));
2690 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2692 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2693 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2695 /* The second wait() should report a system call entry for getpid(). */
2696 wpid = waitpid(fpid, &status, 0);
2697 ATF_REQUIRE(wpid == fpid);
2698 ATF_REQUIRE(WIFSTOPPED(status));
2699 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2701 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2702 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2704 /* Continue the child process with a SIGTRAP. */
2705 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2709 * The last wait() should report exit due to SIGTRAP. In the
2710 * meantime, catch and proceed past any syscall stops.
2712 wpid = waitpid(fpid, &status, 0);
2713 ATF_REQUIRE(wpid == fpid);
2714 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2715 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2716 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2717 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2719 ATF_REQUIRE(WIFSIGNALED(status));
2720 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2725 wpid = wait(&status);
2726 ATF_REQUIRE(wpid == -1);
2727 ATF_REQUIRE(errno == ECHILD);
2732 * A mixed bag PT_CONTINUE with signal test.
2734 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2735 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2737 struct ptrace_lwpinfo pl;
2741 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2743 ATF_REQUIRE((fpid = fork()) != -1);
2750 /* The first wait() should report the stop from SIGSTOP. */
2751 wpid = waitpid(fpid, &status, 0);
2752 ATF_REQUIRE(wpid == fpid);
2753 ATF_REQUIRE(WIFSTOPPED(status));
2754 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2756 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2757 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2759 /* The second wait() should report a system call entry for getpid(). */
2760 wpid = waitpid(fpid, &status, 0);
2761 ATF_REQUIRE(wpid == fpid);
2762 ATF_REQUIRE(WIFSTOPPED(status));
2763 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2765 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2766 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2768 /* Continue with the first SIGUSR1. */
2769 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2771 /* The next wait() should report a system call exit for getpid(). */
2772 wpid = waitpid(fpid, &status, 0);
2773 ATF_REQUIRE(wpid == fpid);
2774 ATF_REQUIRE(WIFSTOPPED(status));
2775 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2777 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2778 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2780 /* Send an ABRT without ptrace. */
2781 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2783 /* Continue normally. */
2784 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2786 /* The next wait() should report the SIGABRT. */
2787 wpid = waitpid(fpid, &status, 0);
2788 ATF_REQUIRE(wpid == fpid);
2789 ATF_REQUIRE(WIFSTOPPED(status));
2790 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2792 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2793 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2794 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2796 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2797 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2801 * The last wait() should report exit 2, i.e., a normal _exit
2802 * from the signal handler. In the meantime, catch and proceed
2803 * past any syscall stops.
2805 wpid = waitpid(fpid, &status, 0);
2806 ATF_REQUIRE(wpid == fpid);
2807 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2808 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2809 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2810 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2812 ATF_REQUIRE(WIFEXITED(status));
2813 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2818 wpid = wait(&status);
2819 ATF_REQUIRE(wpid == -1);
2820 ATF_REQUIRE(errno == ECHILD);
2825 * Verify a signal delivered by ptrace is noticed by kevent(2).
2827 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2828 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2831 int status, kq, nevents;
2834 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2836 ATF_REQUIRE((fpid = fork()) != -1);
2838 CHILD_REQUIRE((kq = kqueue()) > 0);
2839 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2840 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2845 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2846 if (nevents == -1 && errno == EINTR)
2848 CHILD_REQUIRE(nevents > 0);
2849 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2850 CHILD_REQUIRE(kev.ident == SIGUSR1);
2857 /* The first wait() should report the stop from SIGSTOP. */
2858 wpid = waitpid(fpid, &status, 0);
2859 ATF_REQUIRE(wpid == fpid);
2860 ATF_REQUIRE(WIFSTOPPED(status));
2861 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2863 /* Continue with the SIGUSR1. */
2864 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2867 * The last wait() should report normal exit with code 1.
2869 wpid = waitpid(fpid, &status, 0);
2870 ATF_REQUIRE(wpid == fpid);
2871 ATF_REQUIRE(WIFEXITED(status));
2872 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2874 wpid = wait(&status);
2875 ATF_REQUIRE(wpid == -1);
2876 ATF_REQUIRE(errno == ECHILD);
2880 signal_thread(void *arg)
2885 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2887 /* Wait for this thread to receive a SIGUSR1. */
2889 err = sem_wait(&sigusr1_sem);
2890 CHILD_REQUIRE(err == 0 || errno == EINTR);
2891 } while (err != 0 && errno == EINTR);
2893 /* Free our companion thread from the barrier. */
2894 pthread_barrier_wait(pbarrier);
2897 * Swap ignore duties; the next SIGUSR1 should go to the
2900 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2901 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2902 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2904 /* Sync up threads after swapping signal masks. */
2905 pthread_barrier_wait(pbarrier);
2907 /* Wait until our companion has received its SIGUSR1. */
2908 pthread_barrier_wait(pbarrier);
2914 * Verify that a traced process with blocked signal received the
2915 * signal from kill() once unmasked.
2917 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2918 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2920 struct ptrace_lwpinfo pl;
2925 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2926 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2929 ATF_REQUIRE((fpid = fork()) != -1);
2931 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2932 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2933 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2936 CHILD_REQUIRE(got_usr1 == 0);
2938 /* Allow the pending SIGUSR1 in now. */
2939 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2940 /* Wait to receive a SIGUSR1. */
2942 err = sem_wait(&sigusr1_sem);
2943 CHILD_REQUIRE(err == 0 || errno == EINTR);
2944 } while (err != 0 && errno == EINTR);
2945 CHILD_REQUIRE(got_usr1 == 1);
2949 /* The first wait() should report the stop from SIGSTOP. */
2950 wpid = waitpid(fpid, &status, 0);
2951 ATF_REQUIRE(wpid == fpid);
2952 ATF_REQUIRE(WIFSTOPPED(status));
2953 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2954 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2955 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2957 /* Send blocked SIGUSR1 which should cause a stop. */
2958 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2960 /* Continue the child ignoring the SIGSTOP. */
2961 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2963 /* The next wait() should report the kill(SIGUSR1) was received. */
2964 wpid = waitpid(fpid, &status, 0);
2965 ATF_REQUIRE(wpid == fpid);
2966 ATF_REQUIRE(WIFSTOPPED(status));
2967 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2968 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2969 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2971 /* Continue the child, allowing in the SIGUSR1. */
2972 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2974 /* The last wait() should report normal exit with code 1. */
2975 wpid = waitpid(fpid, &status, 0);
2976 ATF_REQUIRE(wpid == fpid);
2977 ATF_REQUIRE(WIFEXITED(status));
2978 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2980 wpid = wait(&status);
2981 ATF_REQUIRE(wpid == -1);
2982 ATF_REQUIRE(errno == ECHILD);
2986 * Verify that a traced process with blocked signal received the
2987 * signal from PT_CONTINUE once unmasked.
2989 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2990 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2992 struct ptrace_lwpinfo pl;
2997 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2998 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3001 ATF_REQUIRE((fpid = fork()) != -1);
3003 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3004 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3005 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3008 CHILD_REQUIRE(got_usr1 == 0);
3010 /* Allow the pending SIGUSR1 in now. */
3011 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3012 /* Wait to receive a SIGUSR1. */
3014 err = sem_wait(&sigusr1_sem);
3015 CHILD_REQUIRE(err == 0 || errno == EINTR);
3016 } while (err != 0 && errno == EINTR);
3018 CHILD_REQUIRE(got_usr1 == 1);
3022 /* The first wait() should report the stop from SIGSTOP. */
3023 wpid = waitpid(fpid, &status, 0);
3024 ATF_REQUIRE(wpid == fpid);
3025 ATF_REQUIRE(WIFSTOPPED(status));
3026 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3027 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3028 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3030 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3031 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3033 /* The next wait() should report the SIGUSR1 was received. */
3034 wpid = waitpid(fpid, &status, 0);
3035 ATF_REQUIRE(wpid == fpid);
3036 ATF_REQUIRE(WIFSTOPPED(status));
3037 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3038 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3039 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3041 /* Continue the child, ignoring the SIGUSR1. */
3042 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3044 /* The last wait() should report normal exit with code 1. */
3045 wpid = waitpid(fpid, &status, 0);
3046 ATF_REQUIRE(wpid == fpid);
3047 ATF_REQUIRE(WIFEXITED(status));
3048 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3050 wpid = wait(&status);
3051 ATF_REQUIRE(wpid == -1);
3052 ATF_REQUIRE(errno == ECHILD);
3056 * Verify that if ptrace stops due to a signal but continues with
3057 * a different signal that the new signal is routed to a thread
3058 * that can accept it, and that the thread is awakened by the signal
3059 * in a timely manner.
3061 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3062 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3068 pthread_barrier_t barrier;
3070 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3071 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3072 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3074 ATF_REQUIRE((fpid = fork()) != -1);
3076 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3078 /* The other thread should receive the first SIGUSR1. */
3079 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3080 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3081 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3085 /* Wait until other thread has received its SIGUSR1. */
3086 pthread_barrier_wait(&barrier);
3089 * Swap ignore duties; the next SIGUSR1 should go to this
3092 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3094 /* Sync up threads after swapping signal masks. */
3095 pthread_barrier_wait(&barrier);
3098 * Sync up with test code; we're ready for the next SIGUSR1
3103 /* Wait for this thread to receive a SIGUSR1. */
3105 err = sem_wait(&sigusr1_sem);
3106 CHILD_REQUIRE(err == 0 || errno == EINTR);
3107 } while (err != 0 && errno == EINTR);
3109 /* Free the other thread from the barrier. */
3110 pthread_barrier_wait(&barrier);
3112 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3117 /* The first wait() should report the stop from SIGSTOP. */
3118 wpid = waitpid(fpid, &status, 0);
3119 ATF_REQUIRE(wpid == fpid);
3120 ATF_REQUIRE(WIFSTOPPED(status));
3121 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3123 /* Continue the child ignoring the SIGSTOP. */
3124 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3127 * Send a signal without ptrace that either thread will accept (USR2,
3130 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3132 /* The second wait() should report a SIGUSR2 was received. */
3133 wpid = waitpid(fpid, &status, 0);
3134 ATF_REQUIRE(wpid == fpid);
3135 ATF_REQUIRE(WIFSTOPPED(status));
3136 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3138 /* Continue the child, changing the signal to USR1. */
3139 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3141 /* The next wait() should report the stop from SIGSTOP. */
3142 wpid = waitpid(fpid, &status, 0);
3143 ATF_REQUIRE(wpid == fpid);
3144 ATF_REQUIRE(WIFSTOPPED(status));
3145 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3147 /* Continue the child ignoring the SIGSTOP. */
3148 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3150 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3152 /* The next wait() should report a SIGUSR2 was received. */
3153 wpid = waitpid(fpid, &status, 0);
3154 ATF_REQUIRE(wpid == fpid);
3155 ATF_REQUIRE(WIFSTOPPED(status));
3156 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3158 /* Continue the child, changing the signal to USR1. */
3159 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3161 /* The last wait() should report normal exit with code 1. */
3162 wpid = waitpid(fpid, &status, 0);
3163 ATF_REQUIRE(wpid == fpid);
3164 ATF_REQUIRE(WIFEXITED(status));
3165 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3167 wpid = wait(&status);
3168 ATF_REQUIRE(wpid == -1);
3169 ATF_REQUIRE(errno == ECHILD);
3173 raise_sigstop_thread(void *arg __unused)
3181 sleep_thread(void *arg __unused)
3189 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3198 * Become the reaper for this process tree. We need to be able to check
3199 * that both child and grandchild have died.
3201 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3204 ATF_REQUIRE(fpid >= 0);
3207 CHILD_REQUIRE(fpid >= 0);
3211 /* Pin to CPU 0 to serialize thread execution. */
3213 CPU_SET(0, &setmask);
3214 CHILD_REQUIRE(cpuset(&setid) == 0);
3215 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3216 CPU_WHICH_CPUSET, setid,
3217 sizeof(setmask), &setmask) == 0);
3219 if (sigstop_from_main_thread) {
3221 * We expect the SIGKILL sent when our parent
3222 * dies to be delivered to the new thread.
3223 * Raise the SIGSTOP in this thread so the
3226 CHILD_REQUIRE(pthread_create(&t, NULL,
3227 sleep_thread, NULL) == 0);
3231 * We expect the SIGKILL to be delivered to
3232 * this thread. After creating the new thread,
3233 * just get off the CPU so the other thread can
3234 * raise the SIGSTOP.
3236 CHILD_REQUIRE(pthread_create(&t, NULL,
3237 raise_sigstop_thread, NULL) == 0);
3243 /* First stop is trace_me() immediately after fork. */
3244 wpid = waitpid(fpid, &status, 0);
3245 CHILD_REQUIRE(wpid == fpid);
3246 CHILD_REQUIRE(WIFSTOPPED(status));
3247 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3249 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3251 /* Second stop is from the raise(SIGSTOP). */
3252 wpid = waitpid(fpid, &status, 0);
3253 CHILD_REQUIRE(wpid == fpid);
3254 CHILD_REQUIRE(WIFSTOPPED(status));
3255 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3258 * Terminate tracing process without detaching. Our child
3265 * We should get a normal exit from our immediate child and a SIGKILL
3266 * exit from our grandchild. The latter case is the interesting one.
3267 * Our grandchild should not have stopped due to the SIGSTOP that was
3268 * left dangling when its parent died.
3270 for (i = 0; i < 2; ++i) {
3271 wpid = wait(&status);
3273 ATF_REQUIRE(WIFEXITED(status) &&
3274 WEXITSTATUS(status) == 0);
3276 ATF_REQUIRE(WIFSIGNALED(status) &&
3277 WTERMSIG(status) == SIGKILL);
3282 * These two tests ensure that if the tracing process exits without detaching
3283 * just after the child received a SIGSTOP, the child is cleanly killed and
3284 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3285 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3286 * different threads, the SIGKILL must win. There are two variants of this
3287 * test, designed to catch the case where the SIGKILL is delivered to the
3288 * younger thread (the first test) and the case where the SIGKILL is delivered
3289 * to the older thread (the second test). This behavior has changed in the
3290 * past, so make no assumption.
3292 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3293 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3296 atf_tc_set_md_var(tc, "require.user", "root");
3298 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3301 terminate_with_pending_sigstop(true);
3304 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3305 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3308 atf_tc_set_md_var(tc, "require.user", "root");
3310 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3313 terminate_with_pending_sigstop(false);
3317 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3320 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3321 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3323 struct ptrace_lwpinfo pl;
3325 int status, event_mask, new_event_mask;
3327 ATF_REQUIRE((fpid = fork()) != -1);
3334 /* The first wait() should report the stop from trace_me(). */
3335 wpid = waitpid(fpid, &status, 0);
3336 ATF_REQUIRE(wpid == fpid);
3337 ATF_REQUIRE(WIFSTOPPED(status));
3338 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3340 /* Set several unobtrusive event bits. */
3341 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3342 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3343 sizeof(event_mask)) == 0);
3345 /* Send a SIGKILL without using ptrace. */
3346 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3348 /* Continue the child ignoring the SIGSTOP. */
3349 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3351 /* The next stop should be due to the SIGKILL. */
3352 wpid = waitpid(fpid, &status, 0);
3353 ATF_REQUIRE(wpid == fpid);
3354 ATF_REQUIRE(WIFSTOPPED(status));
3355 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3357 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3358 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3359 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3361 /* Continue the child ignoring the SIGKILL. */
3362 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3364 /* The next wait() should report the stop from SIGSTOP. */
3365 wpid = waitpid(fpid, &status, 0);
3366 ATF_REQUIRE(wpid == fpid);
3367 ATF_REQUIRE(WIFSTOPPED(status));
3368 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3370 /* Check the current event mask. It should not have changed. */
3372 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3373 sizeof(new_event_mask)) == 0);
3374 ATF_REQUIRE(event_mask == new_event_mask);
3376 /* Continue the child to let it exit. */
3377 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3379 /* The last event should be for the child process's exit. */
3380 wpid = waitpid(fpid, &status, 0);
3381 ATF_REQUIRE(WIFEXITED(status));
3382 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3384 wpid = wait(&status);
3385 ATF_REQUIRE(wpid == -1);
3386 ATF_REQUIRE(errno == ECHILD);
3390 flock_thread(void *arg)
3395 (void)flock(fd, LOCK_EX);
3396 (void)flock(fd, LOCK_UN);
3401 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3402 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3403 * on a lock. This is a regression test for r318191.
3405 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3406 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3408 pthread_barrier_t barrier;
3409 pthread_barrierattr_t battr;
3412 int error, fd, i, status;
3414 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3415 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3416 PTHREAD_PROCESS_SHARED) == 0);
3417 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3419 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3420 fd = mkstemp(tmpfile);
3421 ATF_REQUIRE(fd >= 0);
3423 ATF_REQUIRE((child = fork()) != -1);
3428 error = pthread_barrier_wait(&barrier);
3429 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3432 cfd = open(tmpfile, O_RDONLY);
3437 * We want at least two threads blocked on the file lock since
3438 * the SIGSTOP from PT_ATTACH may kick one of them out of
3441 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3443 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3445 if (pthread_join(t[0], NULL) != 0)
3447 if (pthread_join(t[1], NULL) != 0)
3452 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3454 error = pthread_barrier_wait(&barrier);
3455 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3458 * Give the child some time to block. Is there a better way to do this?
3463 * Attach and give the child 3 seconds to stop.
3465 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3466 for (i = 0; i < 3; i++) {
3467 wpid = waitpid(child, &status, WNOHANG);
3468 if (wpid == child && WIFSTOPPED(status) &&
3469 WSTOPSIG(status) == SIGSTOP)
3473 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3475 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3477 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3478 ATF_REQUIRE(unlink(tmpfile) == 0);
3479 ATF_REQUIRE(close(fd) == 0);
3483 sigusr1_step_handler(int sig)
3486 CHILD_REQUIRE(sig == SIGUSR1);
3491 * Verify that PT_STEP with a signal invokes the signal before
3492 * stepping the next instruction (and that the next instruction is
3493 * stepped correctly).
3495 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3496 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3498 struct ptrace_lwpinfo pl;
3502 ATF_REQUIRE((fpid = fork()) != -1);
3505 signal(SIGUSR1, sigusr1_step_handler);
3510 /* The first wait() should report the stop from SIGSTOP. */
3511 wpid = waitpid(fpid, &status, 0);
3512 ATF_REQUIRE(wpid == fpid);
3513 ATF_REQUIRE(WIFSTOPPED(status));
3514 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3516 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3518 /* The next stop should report the SIGABRT in the child body. */
3519 wpid = waitpid(fpid, &status, 0);
3520 ATF_REQUIRE(wpid == fpid);
3521 ATF_REQUIRE(WIFSTOPPED(status));
3522 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3524 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3525 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3526 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3528 /* Step the child process inserting SIGUSR1. */
3529 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3531 /* The next stop should report the SIGABRT in the signal handler. */
3532 wpid = waitpid(fpid, &status, 0);
3533 ATF_REQUIRE(wpid == fpid);
3534 ATF_REQUIRE(WIFSTOPPED(status));
3535 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3537 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3538 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3539 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3541 /* Continue the child process discarding the signal. */
3542 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3544 /* The next stop should report a trace trap from PT_STEP. */
3545 wpid = waitpid(fpid, &status, 0);
3546 ATF_REQUIRE(wpid == fpid);
3547 ATF_REQUIRE(WIFSTOPPED(status));
3548 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3550 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3551 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3552 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3553 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3555 /* Continue the child to let it exit. */
3556 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3558 /* The last event should be for the child process's exit. */
3559 wpid = waitpid(fpid, &status, 0);
3560 ATF_REQUIRE(WIFEXITED(status));
3561 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3563 wpid = wait(&status);
3564 ATF_REQUIRE(wpid == -1);
3565 ATF_REQUIRE(errno == ECHILD);
3568 #ifdef HAVE_BREAKPOINT
3570 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3571 * for a breakpoint trap.
3573 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3574 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3576 struct ptrace_lwpinfo pl;
3580 ATF_REQUIRE((fpid = fork()) != -1);
3587 /* The first wait() should report the stop from SIGSTOP. */
3588 wpid = waitpid(fpid, &status, 0);
3589 ATF_REQUIRE(wpid == fpid);
3590 ATF_REQUIRE(WIFSTOPPED(status));
3591 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3593 /* Continue the child ignoring the SIGSTOP. */
3594 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3596 /* The second wait() should report hitting the breakpoint. */
3597 wpid = waitpid(fpid, &status, 0);
3598 ATF_REQUIRE(wpid == fpid);
3599 ATF_REQUIRE(WIFSTOPPED(status));
3600 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3602 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3603 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3604 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3605 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3607 /* Kill the child process. */
3608 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3610 /* The last wait() should report the SIGKILL. */
3611 wpid = waitpid(fpid, &status, 0);
3612 ATF_REQUIRE(wpid == fpid);
3613 ATF_REQUIRE(WIFSIGNALED(status));
3614 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3616 wpid = wait(&status);
3617 ATF_REQUIRE(wpid == -1);
3618 ATF_REQUIRE(errno == ECHILD);
3620 #endif /* HAVE_BREAKPOINT */
3623 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3624 * for a single-step trap from PT_STEP.
3626 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3627 ATF_TC_BODY(ptrace__step_siginfo, tc)
3629 struct ptrace_lwpinfo pl;
3633 ATF_REQUIRE((fpid = fork()) != -1);
3639 /* The first wait() should report the stop from SIGSTOP. */
3640 wpid = waitpid(fpid, &status, 0);
3641 ATF_REQUIRE(wpid == fpid);
3642 ATF_REQUIRE(WIFSTOPPED(status));
3643 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3645 /* Step the child ignoring the SIGSTOP. */
3646 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3648 /* The second wait() should report a single-step trap. */
3649 wpid = waitpid(fpid, &status, 0);
3650 ATF_REQUIRE(wpid == fpid);
3651 ATF_REQUIRE(WIFSTOPPED(status));
3652 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3654 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3655 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3656 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3657 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3659 /* Continue the child process. */
3660 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3662 /* The last event should be for the child process's exit. */
3663 wpid = waitpid(fpid, &status, 0);
3664 ATF_REQUIRE(WIFEXITED(status));
3665 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3667 wpid = wait(&status);
3668 ATF_REQUIRE(wpid == -1);
3669 ATF_REQUIRE(errno == ECHILD);
3672 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3674 continue_thread(void *arg __unused)
3681 continue_thread_main(void)
3683 pthread_t threads[2];
3685 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3687 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3689 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3690 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3695 * Ensure that PT_CONTINUE clears the status of the thread that
3696 * triggered the stop even if a different thread's LWP was passed to
3699 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3700 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3702 struct ptrace_lwpinfo pl;
3709 ATF_REQUIRE((fpid = fork()) != -1);
3712 continue_thread_main();
3715 /* The first wait() should report the stop from SIGSTOP. */
3716 wpid = waitpid(fpid, &status, 0);
3717 ATF_REQUIRE(wpid == fpid);
3718 ATF_REQUIRE(WIFSTOPPED(status));
3719 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3721 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3724 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3726 /* Continue the child ignoring the SIGSTOP. */
3727 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3729 /* One of the new threads should report it's birth. */
3730 wpid = waitpid(fpid, &status, 0);
3731 ATF_REQUIRE(wpid == fpid);
3732 ATF_REQUIRE(WIFSTOPPED(status));
3733 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3735 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3736 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3737 (PL_FLAG_BORN | PL_FLAG_SCX));
3738 lwps[0] = pl.pl_lwpid;
3741 * Suspend this thread to ensure both threads are alive before
3742 * hitting the breakpoint.
3744 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3746 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3748 /* Second thread should report it's birth. */
3749 wpid = waitpid(fpid, &status, 0);
3750 ATF_REQUIRE(wpid == fpid);
3751 ATF_REQUIRE(WIFSTOPPED(status));
3752 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3754 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3755 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3756 (PL_FLAG_BORN | PL_FLAG_SCX));
3757 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3758 lwps[1] = pl.pl_lwpid;
3760 /* Resume both threads waiting for breakpoint events. */
3761 hit_break[0] = hit_break[1] = false;
3762 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3763 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3765 /* One thread should report a breakpoint. */
3766 wpid = waitpid(fpid, &status, 0);
3767 ATF_REQUIRE(wpid == fpid);
3768 ATF_REQUIRE(WIFSTOPPED(status));
3769 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3771 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3772 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3773 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3774 pl.pl_siginfo.si_code == TRAP_BRKPT);
3775 if (pl.pl_lwpid == lwps[0])
3779 hit_break[i] = true;
3780 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3782 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3785 * Resume both threads but pass the other thread's LWPID to
3788 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3791 * Will now get two thread exit events and one more breakpoint
3794 for (j = 0; j < 3; j++) {
3795 wpid = waitpid(fpid, &status, 0);
3796 ATF_REQUIRE(wpid == fpid);
3797 ATF_REQUIRE(WIFSTOPPED(status));
3798 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3800 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3803 if (pl.pl_lwpid == lwps[0])
3808 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3809 if (pl.pl_flags & PL_FLAG_EXITED) {
3810 ATF_REQUIRE_MSG(hit_break[i],
3811 "exited thread did not report breakpoint");
3814 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3815 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3816 pl.pl_siginfo.si_code == TRAP_BRKPT);
3817 ATF_REQUIRE_MSG(!hit_break[i],
3818 "double breakpoint event");
3819 hit_break[i] = true;
3820 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3823 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3827 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3830 /* Both threads should have exited. */
3831 ATF_REQUIRE(lwps[0] == 0);
3832 ATF_REQUIRE(lwps[1] == 0);
3834 /* The last event should be for the child process's exit. */
3835 wpid = waitpid(fpid, &status, 0);
3836 ATF_REQUIRE(WIFEXITED(status));
3837 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3839 wpid = wait(&status);
3840 ATF_REQUIRE(wpid == -1);
3841 ATF_REQUIRE(errno == ECHILD);
3846 * Verify that PT_LWPINFO doesn't return stale siginfo.
3848 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3849 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3851 struct ptrace_lwpinfo pl;
3855 ATF_REQUIRE((fpid = fork()) != -1);
3862 /* The first wait() should report the stop from SIGSTOP. */
3863 wpid = waitpid(fpid, &status, 0);
3864 ATF_REQUIRE(wpid == fpid);
3865 ATF_REQUIRE(WIFSTOPPED(status));
3866 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3868 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3870 /* The next stop should report the SIGABRT in the child body. */
3871 wpid = waitpid(fpid, &status, 0);
3872 ATF_REQUIRE(wpid == fpid);
3873 ATF_REQUIRE(WIFSTOPPED(status));
3874 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3876 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3877 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3878 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3881 * Continue the process ignoring the signal, but enabling
3884 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3887 * The next stop should report a system call entry from
3888 * exit(). PL_FLAGS_SI should not be set.
3890 wpid = waitpid(fpid, &status, 0);
3891 ATF_REQUIRE(wpid == fpid);
3892 ATF_REQUIRE(WIFSTOPPED(status));
3893 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3895 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3896 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3897 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3899 /* Disable syscall tracing and continue the child to let it exit. */
3900 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3901 sizeof(events)) == 0);
3902 events &= ~PTRACE_SYSCALL;
3903 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3904 sizeof(events)) == 0);
3905 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3907 /* The last event should be for the child process's exit. */
3908 wpid = waitpid(fpid, &status, 0);
3909 ATF_REQUIRE(WIFEXITED(status));
3910 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3912 wpid = wait(&status);
3913 ATF_REQUIRE(wpid == -1);
3914 ATF_REQUIRE(errno == ECHILD);
3918 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3920 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3921 ATF_TC_BODY(ptrace__syscall_args, tc)
3923 struct ptrace_lwpinfo pl;
3924 struct ptrace_sc_ret psr;
3929 ATF_REQUIRE((fpid = fork()) != -1);
3937 /* The first wait() should report the stop from SIGSTOP. */
3938 wpid = waitpid(fpid, &status, 0);
3939 ATF_REQUIRE(wpid == fpid);
3940 ATF_REQUIRE(WIFSTOPPED(status));
3941 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3944 * Continue the process ignoring the signal, but enabling
3947 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3950 * The next stop should be the syscall entry from getpid().
3952 wpid = waitpid(fpid, &status, 0);
3953 ATF_REQUIRE(wpid == fpid);
3954 ATF_REQUIRE(WIFSTOPPED(status));
3955 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3957 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3958 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3959 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3961 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3964 * The next stop should be the syscall exit from getpid().
3966 wpid = waitpid(fpid, &status, 0);
3967 ATF_REQUIRE(wpid == fpid);
3968 ATF_REQUIRE(WIFSTOPPED(status));
3969 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3971 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3972 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
3973 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3975 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
3976 sizeof(psr)) != -1);
3977 ATF_REQUIRE(psr.sr_error == 0);
3978 ATF_REQUIRE(psr.sr_retval[0] == wpid);
3980 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3983 * The next stop should be the syscall entry from kill().
3985 wpid = waitpid(fpid, &status, 0);
3986 ATF_REQUIRE(wpid == fpid);
3987 ATF_REQUIRE(WIFSTOPPED(status));
3988 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3990 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3991 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3992 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
3993 ATF_REQUIRE(pl.pl_syscall_narg == 2);
3995 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
3996 sizeof(args)) != -1);
3997 ATF_REQUIRE(args[0] == wpid);
3998 ATF_REQUIRE(args[1] == 0);
4000 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4003 * The next stop should be the syscall exit from kill().
4005 wpid = waitpid(fpid, &status, 0);
4006 ATF_REQUIRE(wpid == fpid);
4007 ATF_REQUIRE(WIFSTOPPED(status));
4008 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4010 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4011 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4012 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
4014 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4015 sizeof(psr)) != -1);
4016 ATF_REQUIRE(psr.sr_error == 0);
4018 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4021 * The next stop should be the syscall entry from close().
4023 wpid = waitpid(fpid, &status, 0);
4024 ATF_REQUIRE(wpid == fpid);
4025 ATF_REQUIRE(WIFSTOPPED(status));
4026 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4028 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4029 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4030 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4031 ATF_REQUIRE(pl.pl_syscall_narg == 1);
4033 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4034 sizeof(args)) != -1);
4035 ATF_REQUIRE(args[0] == 3);
4037 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4040 * The next stop should be the syscall exit from close().
4042 wpid = waitpid(fpid, &status, 0);
4043 ATF_REQUIRE(wpid == fpid);
4044 ATF_REQUIRE(WIFSTOPPED(status));
4045 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4047 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4048 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4049 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4051 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4052 sizeof(psr)) != -1);
4053 ATF_REQUIRE(psr.sr_error == EBADF);
4055 /* Disable syscall tracing and continue the child to let it exit. */
4056 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4057 sizeof(events)) == 0);
4058 events &= ~PTRACE_SYSCALL;
4059 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4060 sizeof(events)) == 0);
4061 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4063 /* The last event should be for the child process's exit. */
4064 wpid = waitpid(fpid, &status, 0);
4065 ATF_REQUIRE(WIFEXITED(status));
4066 ATF_REQUIRE(WEXITSTATUS(status) == 1);
4068 wpid = wait(&status);
4069 ATF_REQUIRE(wpid == -1);
4070 ATF_REQUIRE(errno == ECHILD);
4076 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4077 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4078 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4079 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4080 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4081 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4082 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4083 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4084 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4086 ptrace__follow_fork_child_detached_unrelated_debugger);
4088 ptrace__follow_fork_parent_detached_unrelated_debugger);
4089 ATF_TP_ADD_TC(tp, ptrace__getppid);
4090 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4091 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4092 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4093 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4094 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4095 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4096 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4097 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4098 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4099 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4100 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4101 #ifdef HAVE_BREAKPOINT
4102 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4104 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4105 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4106 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4107 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4108 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4109 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4111 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4112 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4113 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4114 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4115 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4116 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4117 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4118 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4119 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4120 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4121 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4122 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4123 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4124 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4125 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4126 #ifdef HAVE_BREAKPOINT
4127 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4129 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4130 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4131 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4133 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4134 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4136 return (atf_no_error());