2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/types.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
35 #include <sys/procctl.h>
36 #define _WANT_MIPS_REGNUM
37 #include <sys/ptrace.h>
38 #include <sys/queue.h>
40 #include <sys/syscall.h>
41 #include <sys/sysctl.h>
45 #include <machine/cpufunc.h>
48 #include <semaphore.h>
56 * Architectures with a user-visible breakpoint().
58 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
59 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
61 #define HAVE_BREAKPOINT
65 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
67 #ifdef HAVE_BREAKPOINT
68 #if defined(__aarch64__)
69 #define SKIP_BREAK(reg) ((reg)->elr += 4)
70 #elif defined(__amd64__) || defined(__i386__)
71 #define SKIP_BREAK(reg)
72 #elif defined(__arm__)
73 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
74 #elif defined(__mips__)
75 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
76 #elif defined(__riscv)
77 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
78 #elif defined(__sparc64__)
79 #define SKIP_BREAK(reg) do { \
80 (reg)->r_tpc = (reg)->r_tnpc + 4; \
87 * A variant of ATF_REQUIRE that is suitable for use in child
88 * processes. This only works if the parent process is tripped up by
89 * the early exit and fails some requirement itself.
91 #define CHILD_REQUIRE(exp) do { \
93 child_fail_require(__FILE__, __LINE__, \
98 child_fail_require(const char *file, int line, const char *str)
102 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
103 write(2, buf, strlen(buf));
111 /* Attach the parent process as a tracer of this process. */
112 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
114 /* Trigger a stop. */
119 attach_child(pid_t pid)
124 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
126 wpid = waitpid(pid, &status, 0);
127 ATF_REQUIRE(wpid == pid);
128 ATF_REQUIRE(WIFSTOPPED(status));
129 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
133 wait_for_zombie(pid_t pid)
137 * Wait for a process to exit. This is kind of gross, but
138 * there is not a better way.
140 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
141 * with ESRCH. After that change, a valid struct kinfo_proc
142 * is returned for zombies with ki_stat set to SZOMB.
145 struct kinfo_proc kp;
151 mib[2] = KERN_PROC_PID;
154 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
155 ATF_REQUIRE(errno == ESRCH);
158 if (kp.ki_stat == SZOMB)
165 * Verify that a parent debugger process "sees" the exit of a debugged
166 * process exactly once when attached via PT_TRACE_ME.
168 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
169 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
174 ATF_REQUIRE((child = fork()) != -1);
182 /* Parent process. */
184 /* The first wait() should report the stop from SIGSTOP. */
185 wpid = waitpid(child, &status, 0);
186 ATF_REQUIRE(wpid == child);
187 ATF_REQUIRE(WIFSTOPPED(status));
188 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
190 /* Continue the child ignoring the SIGSTOP. */
191 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
193 /* The second wait() should report the exit status. */
194 wpid = waitpid(child, &status, 0);
195 ATF_REQUIRE(wpid == child);
196 ATF_REQUIRE(WIFEXITED(status));
197 ATF_REQUIRE(WEXITSTATUS(status) == 1);
199 /* The child should no longer exist. */
200 wpid = waitpid(child, &status, 0);
201 ATF_REQUIRE(wpid == -1);
202 ATF_REQUIRE(errno == ECHILD);
206 * Verify that a parent debugger process "sees" the exit of a debugged
207 * process exactly once when attached via PT_ATTACH.
209 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
210 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
213 int cpipe[2], status;
216 ATF_REQUIRE(pipe(cpipe) == 0);
217 ATF_REQUIRE((child = fork()) != -1);
222 /* Wait for the parent to attach. */
223 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
229 /* Parent process. */
231 /* Attach to the child process. */
234 /* Continue the child ignoring the SIGSTOP. */
235 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
237 /* Signal the child to exit. */
240 /* The second wait() should report the exit status. */
241 wpid = waitpid(child, &status, 0);
242 ATF_REQUIRE(wpid == child);
243 ATF_REQUIRE(WIFEXITED(status));
244 ATF_REQUIRE(WEXITSTATUS(status) == 1);
246 /* The child should no longer exist. */
247 wpid = waitpid(child, &status, 0);
248 ATF_REQUIRE(wpid == -1);
249 ATF_REQUIRE(errno == ECHILD);
253 * Verify that a parent process "sees" the exit of a debugged process only
254 * after the debugger has seen it.
256 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
257 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
259 pid_t child, debugger, wpid;
260 int cpipe[2], dpipe[2], status;
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_REQUIRE(pipe(cpipe) == 0);
805 ATF_REQUIRE((fpid = fork()) != -1);
807 attach_fork_parent(cpipe);
808 follow_fork_parent(false);
811 /* Parent process. */
814 /* Wait for the direct child to exit. */
815 wpid = waitpid(fpid, &status, 0);
816 ATF_REQUIRE(wpid == fpid);
817 ATF_REQUIRE(WIFEXITED(status));
818 ATF_REQUIRE(WEXITSTATUS(status) == 3);
820 /* Read the pid of the fork parent. */
821 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
822 sizeof(children[0]));
824 /* Attach to the fork parent. */
825 attach_child(children[0]);
827 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
829 /* Continue the fork parent ignoring the SIGSTOP. */
830 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
832 /* Signal the fork parent to continue. */
835 children[1] = handle_fork_events(children[0], NULL);
836 ATF_REQUIRE(children[1] > 0);
838 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
839 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
842 * The fork parent can't exit until the child reports status,
843 * so the child should report its exit first to the debugger.
845 wpid = wait(&status);
846 ATF_REQUIRE(wpid == children[1]);
847 ATF_REQUIRE(WIFEXITED(status));
848 ATF_REQUIRE(WEXITSTATUS(status) == 2);
850 wpid = wait(&status);
851 ATF_REQUIRE(wpid == children[0]);
852 ATF_REQUIRE(WIFEXITED(status));
853 ATF_REQUIRE(WEXITSTATUS(status) == 1);
855 wpid = wait(&status);
856 ATF_REQUIRE(wpid == -1);
857 ATF_REQUIRE(errno == ECHILD);
861 * Verify that a new child process is stopped after a followed fork
862 * and that the traced parent sees the exit of the child when the new
863 * child process is detached after it reports its fork. In this test
864 * the parent that forks is not a direct child of the debugger.
866 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
867 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
869 pid_t children[2], fpid, wpid;
870 int cpipe[2], status;
872 ATF_REQUIRE(pipe(cpipe) == 0);
873 ATF_REQUIRE((fpid = fork()) != -1);
875 attach_fork_parent(cpipe);
876 follow_fork_parent(false);
879 /* Parent process. */
882 /* Wait for the direct child to exit. */
883 wpid = waitpid(fpid, &status, 0);
884 ATF_REQUIRE(wpid == fpid);
885 ATF_REQUIRE(WIFEXITED(status));
886 ATF_REQUIRE(WEXITSTATUS(status) == 3);
888 /* Read the pid of the fork parent. */
889 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
890 sizeof(children[0]));
892 /* Attach to the fork parent. */
893 attach_child(children[0]);
895 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
897 /* Continue the fork parent ignoring the SIGSTOP. */
898 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
900 /* Signal the fork parent to continue. */
903 children[1] = handle_fork_events(children[0], NULL);
904 ATF_REQUIRE(children[1] > 0);
906 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
907 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
910 * Should not see any status from the child now, only the fork
913 wpid = wait(&status);
914 ATF_REQUIRE(wpid == children[0]);
915 ATF_REQUIRE(WIFEXITED(status));
916 ATF_REQUIRE(WEXITSTATUS(status) == 1);
918 wpid = wait(&status);
919 ATF_REQUIRE(wpid == -1);
920 ATF_REQUIRE(errno == ECHILD);
924 * Verify that a new child process is stopped after a followed fork
925 * and that the traced parent sees the exit of the child when the
926 * traced parent is detached after the fork. In this test the parent
927 * that forks is not a direct child of the debugger.
929 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
930 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
932 pid_t children[2], fpid, wpid;
933 int cpipe[2], status;
935 ATF_REQUIRE(pipe(cpipe) == 0);
936 ATF_REQUIRE((fpid = fork()) != -1);
938 attach_fork_parent(cpipe);
939 follow_fork_parent(false);
942 /* Parent process. */
945 /* Wait for the direct child to exit. */
946 wpid = waitpid(fpid, &status, 0);
947 ATF_REQUIRE(wpid == fpid);
948 ATF_REQUIRE(WIFEXITED(status));
949 ATF_REQUIRE(WEXITSTATUS(status) == 3);
951 /* Read the pid of the fork parent. */
952 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
953 sizeof(children[0]));
955 /* Attach to the fork parent. */
956 attach_child(children[0]);
958 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
960 /* Continue the fork parent ignoring the SIGSTOP. */
961 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
963 /* Signal the fork parent to continue. */
966 children[1] = handle_fork_events(children[0], NULL);
967 ATF_REQUIRE(children[1] > 0);
969 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
970 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
973 * Should not see any status from the fork parent now, only
976 wpid = wait(&status);
977 ATF_REQUIRE(wpid == children[1]);
978 ATF_REQUIRE(WIFEXITED(status));
979 ATF_REQUIRE(WEXITSTATUS(status) == 2);
981 wpid = wait(&status);
982 ATF_REQUIRE(wpid == -1);
983 ATF_REQUIRE(errno == ECHILD);
987 * Verify that a child process does not see an unrelated debugger as its
988 * parent but sees its original parent process.
990 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
991 ATF_TC_BODY(ptrace__getppid, tc)
993 pid_t child, debugger, ppid, wpid;
994 int cpipe[2], dpipe[2], status;
997 ATF_REQUIRE(pipe(cpipe) == 0);
998 ATF_REQUIRE((child = fork()) != -1);
1001 /* Child process. */
1004 /* Wait for parent to be ready. */
1005 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1007 /* Report the parent PID to the parent. */
1009 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1016 ATF_REQUIRE(pipe(dpipe) == 0);
1017 ATF_REQUIRE((debugger = fork()) != -1);
1019 if (debugger == 0) {
1020 /* Debugger process. */
1023 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1025 wpid = waitpid(child, &status, 0);
1026 CHILD_REQUIRE(wpid == child);
1027 CHILD_REQUIRE(WIFSTOPPED(status));
1028 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1030 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1032 /* Signal parent that debugger is attached. */
1033 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1035 /* Wait for traced child to exit. */
1036 wpid = waitpid(child, &status, 0);
1037 CHILD_REQUIRE(wpid == child);
1038 CHILD_REQUIRE(WIFEXITED(status));
1039 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1045 /* Parent process. */
1047 /* Wait for the debugger to attach to the child. */
1048 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1050 /* Release the child. */
1051 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1053 /* Read the parent PID from the child. */
1054 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1057 ATF_REQUIRE(ppid == getpid());
1059 /* Wait for the debugger. */
1060 wpid = waitpid(debugger, &status, 0);
1061 ATF_REQUIRE(wpid == debugger);
1062 ATF_REQUIRE(WIFEXITED(status));
1063 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1065 /* The child process should now be ready. */
1066 wpid = waitpid(child, &status, WNOHANG);
1067 ATF_REQUIRE(wpid == child);
1068 ATF_REQUIRE(WIFEXITED(status));
1069 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1073 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1074 * child process created via fork() reports the correct value.
1076 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1077 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1079 struct ptrace_lwpinfo pl[2];
1080 pid_t children[2], fpid, wpid;
1083 ATF_REQUIRE((fpid = fork()) != -1);
1086 follow_fork_parent(false);
1089 /* Parent process. */
1092 /* The first wait() should report the stop from SIGSTOP. */
1093 wpid = waitpid(children[0], &status, 0);
1094 ATF_REQUIRE(wpid == children[0]);
1095 ATF_REQUIRE(WIFSTOPPED(status));
1096 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1098 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1100 /* Continue the child ignoring the SIGSTOP. */
1101 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1103 /* Wait for both halves of the fork event to get reported. */
1104 children[1] = handle_fork_events(children[0], pl);
1105 ATF_REQUIRE(children[1] > 0);
1107 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1108 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1109 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1110 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1111 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1113 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1114 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1117 * The child can't exit until the grandchild reports status, so the
1118 * grandchild should report its exit first to the debugger.
1120 wpid = wait(&status);
1121 ATF_REQUIRE(wpid == children[1]);
1122 ATF_REQUIRE(WIFEXITED(status));
1123 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1125 wpid = wait(&status);
1126 ATF_REQUIRE(wpid == children[0]);
1127 ATF_REQUIRE(WIFEXITED(status));
1128 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1130 wpid = wait(&status);
1131 ATF_REQUIRE(wpid == -1);
1132 ATF_REQUIRE(errno == ECHILD);
1136 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1137 * child process created via vfork() reports the correct value.
1139 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1140 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1142 struct ptrace_lwpinfo pl[2];
1143 pid_t children[2], fpid, wpid;
1146 ATF_REQUIRE((fpid = fork()) != -1);
1149 follow_fork_parent(true);
1152 /* Parent process. */
1155 /* The first wait() should report the stop from SIGSTOP. */
1156 wpid = waitpid(children[0], &status, 0);
1157 ATF_REQUIRE(wpid == children[0]);
1158 ATF_REQUIRE(WIFSTOPPED(status));
1159 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1161 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1163 /* Continue the child ignoring the SIGSTOP. */
1164 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1166 /* Wait for both halves of the fork event to get reported. */
1167 children[1] = handle_fork_events(children[0], pl);
1168 ATF_REQUIRE(children[1] > 0);
1170 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1171 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1172 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1173 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1174 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1176 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1177 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1180 * The child can't exit until the grandchild reports status, so the
1181 * grandchild should report its exit first to the debugger.
1183 wpid = wait(&status);
1184 ATF_REQUIRE(wpid == children[1]);
1185 ATF_REQUIRE(WIFEXITED(status));
1186 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1188 wpid = wait(&status);
1189 ATF_REQUIRE(wpid == children[0]);
1190 ATF_REQUIRE(WIFEXITED(status));
1191 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1193 wpid = wait(&status);
1194 ATF_REQUIRE(wpid == -1);
1195 ATF_REQUIRE(errno == ECHILD);
1199 simple_thread(void *arg __unused)
1206 simple_thread_main(void)
1210 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1211 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1216 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1217 * thread reports the correct value.
1219 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1220 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1222 struct ptrace_lwpinfo pl;
1227 ATF_REQUIRE((fpid = fork()) != -1);
1230 simple_thread_main();
1233 /* The first wait() should report the stop from SIGSTOP. */
1234 wpid = waitpid(fpid, &status, 0);
1235 ATF_REQUIRE(wpid == fpid);
1236 ATF_REQUIRE(WIFSTOPPED(status));
1237 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1239 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1241 mainlwp = pl.pl_lwpid;
1244 * Continue the child ignoring the SIGSTOP and tracing all
1245 * system call exits.
1247 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1250 * Wait for the new thread to arrive. pthread_create() might
1251 * invoke any number of system calls. For now we just wait
1252 * for the new thread to arrive and make sure it reports a
1253 * valid system call code. If ptrace grows thread event
1254 * reporting then this test can be made more precise.
1257 wpid = waitpid(fpid, &status, 0);
1258 ATF_REQUIRE(wpid == fpid);
1259 ATF_REQUIRE(WIFSTOPPED(status));
1260 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1262 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1264 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1265 ATF_REQUIRE(pl.pl_syscall_code != 0);
1266 if (pl.pl_lwpid != mainlwp)
1267 /* New thread seen. */
1270 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1273 /* Wait for the child to exit. */
1274 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1276 wpid = waitpid(fpid, &status, 0);
1277 ATF_REQUIRE(wpid == fpid);
1278 if (WIFEXITED(status))
1281 ATF_REQUIRE(WIFSTOPPED(status));
1282 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1283 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1286 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1288 wpid = wait(&status);
1289 ATF_REQUIRE(wpid == -1);
1290 ATF_REQUIRE(errno == ECHILD);
1294 * Verify that the expected LWP events are reported for a child thread.
1296 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1297 ATF_TC_BODY(ptrace__lwp_events, tc)
1299 struct ptrace_lwpinfo pl;
1304 ATF_REQUIRE((fpid = fork()) != -1);
1307 simple_thread_main();
1310 /* The first wait() should report the stop from SIGSTOP. */
1311 wpid = waitpid(fpid, &status, 0);
1312 ATF_REQUIRE(wpid == fpid);
1313 ATF_REQUIRE(WIFSTOPPED(status));
1314 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1316 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1318 lwps[0] = pl.pl_lwpid;
1320 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1322 /* Continue the child ignoring the SIGSTOP. */
1323 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1325 /* The first event should be for the child thread's birth. */
1326 wpid = waitpid(fpid, &status, 0);
1327 ATF_REQUIRE(wpid == fpid);
1328 ATF_REQUIRE(WIFSTOPPED(status));
1329 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1331 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1332 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1333 (PL_FLAG_BORN | PL_FLAG_SCX));
1334 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1335 lwps[1] = pl.pl_lwpid;
1337 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1339 /* The next event should be for the child thread's death. */
1340 wpid = waitpid(fpid, &status, 0);
1341 ATF_REQUIRE(wpid == fpid);
1342 ATF_REQUIRE(WIFSTOPPED(status));
1343 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1345 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1346 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1347 (PL_FLAG_EXITED | PL_FLAG_SCE));
1348 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1350 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1352 /* The last event should be for the child process's exit. */
1353 wpid = waitpid(fpid, &status, 0);
1354 ATF_REQUIRE(WIFEXITED(status));
1355 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1357 wpid = wait(&status);
1358 ATF_REQUIRE(wpid == -1);
1359 ATF_REQUIRE(errno == ECHILD);
1363 exec_thread(void *arg __unused)
1366 execl("/usr/bin/true", "true", NULL);
1371 exec_thread_main(void)
1375 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1382 * Verify that the expected LWP events are reported for a multithreaded
1383 * process that calls execve(2).
1385 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1386 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1388 struct ptrace_lwpinfo pl;
1393 ATF_REQUIRE((fpid = fork()) != -1);
1399 /* The first wait() should report the stop from SIGSTOP. */
1400 wpid = waitpid(fpid, &status, 0);
1401 ATF_REQUIRE(wpid == fpid);
1402 ATF_REQUIRE(WIFSTOPPED(status));
1403 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1405 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1407 lwps[0] = pl.pl_lwpid;
1409 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1411 /* Continue the child ignoring the SIGSTOP. */
1412 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1414 /* The first event should be for the child thread's birth. */
1415 wpid = waitpid(fpid, &status, 0);
1416 ATF_REQUIRE(wpid == fpid);
1417 ATF_REQUIRE(WIFSTOPPED(status));
1418 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1420 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1421 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1422 (PL_FLAG_BORN | PL_FLAG_SCX));
1423 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1424 lwps[1] = pl.pl_lwpid;
1426 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1429 * The next event should be for the main thread's death due to
1430 * single threading from execve().
1432 wpid = waitpid(fpid, &status, 0);
1433 ATF_REQUIRE(wpid == fpid);
1434 ATF_REQUIRE(WIFSTOPPED(status));
1435 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1437 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1438 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1440 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1442 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1444 /* The next event should be for the child process's exec. */
1445 wpid = waitpid(fpid, &status, 0);
1446 ATF_REQUIRE(WIFSTOPPED(status));
1447 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1449 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1450 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1451 (PL_FLAG_EXEC | PL_FLAG_SCX));
1452 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1454 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1456 /* The last event should be for the child process's exit. */
1457 wpid = waitpid(fpid, &status, 0);
1458 ATF_REQUIRE(WIFEXITED(status));
1459 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1461 wpid = wait(&status);
1462 ATF_REQUIRE(wpid == -1);
1463 ATF_REQUIRE(errno == ECHILD);
1467 handler(int sig __unused)
1475 signal(SIGINFO, handler);
1481 * Verify that the expected ptrace event is reported for a signal.
1483 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1484 ATF_TC_BODY(ptrace__siginfo, tc)
1486 struct ptrace_lwpinfo pl;
1490 ATF_REQUIRE((fpid = fork()) != -1);
1496 /* The first wait() should report the stop from SIGSTOP. */
1497 wpid = waitpid(fpid, &status, 0);
1498 ATF_REQUIRE(wpid == fpid);
1499 ATF_REQUIRE(WIFSTOPPED(status));
1500 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1502 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1504 /* The next event should be for the SIGINFO. */
1505 wpid = waitpid(fpid, &status, 0);
1506 ATF_REQUIRE(WIFSTOPPED(status));
1507 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1509 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1510 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1511 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1512 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1513 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1515 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1517 /* The last event should be for the child process's exit. */
1518 wpid = waitpid(fpid, &status, 0);
1519 ATF_REQUIRE(WIFEXITED(status));
1520 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1522 wpid = wait(&status);
1523 ATF_REQUIRE(wpid == -1);
1524 ATF_REQUIRE(errno == ECHILD);
1528 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1530 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1531 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1536 ATF_REQUIRE((fpid = fork()) != -1);
1542 /* The first wait() should report the stop from SIGSTOP. */
1543 wpid = waitpid(fpid, &status, 0);
1544 ATF_REQUIRE(wpid == fpid);
1545 ATF_REQUIRE(WIFSTOPPED(status));
1546 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1549 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1550 sizeof(events)) == 0);
1552 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1554 /* Should get one event at exit. */
1555 wpid = waitpid(fpid, &status, 0);
1556 ATF_REQUIRE(WIFEXITED(status));
1557 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1559 wpid = wait(&status);
1560 ATF_REQUIRE(wpid == -1);
1561 ATF_REQUIRE(errno == ECHILD);
1564 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1565 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1567 struct ptrace_lwpinfo pl;
1571 ATF_REQUIRE((fpid = fork()) != -1);
1577 /* The first wait() should report the stop from SIGSTOP. */
1578 wpid = waitpid(fpid, &status, 0);
1579 ATF_REQUIRE(wpid == fpid);
1580 ATF_REQUIRE(WIFSTOPPED(status));
1581 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1583 events = PTRACE_EXEC;
1584 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1585 sizeof(events)) == 0);
1587 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1589 /* The next event should be for the child process's exec. */
1590 wpid = waitpid(fpid, &status, 0);
1591 ATF_REQUIRE(WIFSTOPPED(status));
1592 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1594 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1595 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1596 (PL_FLAG_EXEC | PL_FLAG_SCX));
1598 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1600 /* The last event should be for the child process's exit. */
1601 wpid = waitpid(fpid, &status, 0);
1602 ATF_REQUIRE(WIFEXITED(status));
1603 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1605 wpid = wait(&status);
1606 ATF_REQUIRE(wpid == -1);
1607 ATF_REQUIRE(errno == ECHILD);
1610 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1611 ATF_TC_BODY(ptrace__event_mask, tc)
1616 ATF_REQUIRE((fpid = fork()) != -1);
1622 /* The first wait() should report the stop from SIGSTOP. */
1623 wpid = waitpid(fpid, &status, 0);
1624 ATF_REQUIRE(wpid == fpid);
1625 ATF_REQUIRE(WIFSTOPPED(status));
1626 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1628 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1629 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1630 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1631 sizeof(events)) == 0);
1632 ATF_REQUIRE(events & PTRACE_FORK);
1633 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1634 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1635 sizeof(events)) == 0);
1636 ATF_REQUIRE(!(events & PTRACE_FORK));
1638 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1639 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1640 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1641 sizeof(events)) == 0);
1642 ATF_REQUIRE(events & PTRACE_LWP);
1643 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1644 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1645 sizeof(events)) == 0);
1646 ATF_REQUIRE(!(events & PTRACE_LWP));
1648 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1650 /* Should get one event at exit. */
1651 wpid = waitpid(fpid, &status, 0);
1652 ATF_REQUIRE(WIFEXITED(status));
1653 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1655 wpid = wait(&status);
1656 ATF_REQUIRE(wpid == -1);
1657 ATF_REQUIRE(errno == ECHILD);
1661 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1663 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1664 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1666 struct ptrace_lwpinfo pl;
1670 ATF_REQUIRE((fpid = fork()) != -1);
1673 follow_fork_parent(true);
1676 /* The first wait() should report the stop from SIGSTOP. */
1677 wpid = waitpid(fpid, &status, 0);
1678 ATF_REQUIRE(wpid == fpid);
1679 ATF_REQUIRE(WIFSTOPPED(status));
1680 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1682 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1683 sizeof(events)) == 0);
1684 events |= PTRACE_VFORK;
1685 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1686 sizeof(events)) == 0);
1688 /* Continue the child ignoring the SIGSTOP. */
1689 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1691 /* The next event should report the end of the vfork. */
1692 wpid = wait(&status);
1693 ATF_REQUIRE(wpid == fpid);
1694 ATF_REQUIRE(WIFSTOPPED(status));
1695 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1696 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1697 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1699 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1701 wpid = wait(&status);
1702 ATF_REQUIRE(wpid == fpid);
1703 ATF_REQUIRE(WIFEXITED(status));
1704 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1706 wpid = wait(&status);
1707 ATF_REQUIRE(wpid == -1);
1708 ATF_REQUIRE(errno == ECHILD);
1711 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1712 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1714 struct ptrace_lwpinfo pl[2];
1715 pid_t children[2], fpid, wpid;
1718 ATF_REQUIRE((fpid = fork()) != -1);
1721 follow_fork_parent(true);
1724 /* Parent process. */
1727 /* The first wait() should report the stop from SIGSTOP. */
1728 wpid = waitpid(children[0], &status, 0);
1729 ATF_REQUIRE(wpid == children[0]);
1730 ATF_REQUIRE(WIFSTOPPED(status));
1731 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1733 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1734 sizeof(events)) == 0);
1735 events |= PTRACE_FORK | PTRACE_VFORK;
1736 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1737 sizeof(events)) == 0);
1739 /* Continue the child ignoring the SIGSTOP. */
1740 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1742 /* Wait for both halves of the fork event to get reported. */
1743 children[1] = handle_fork_events(children[0], pl);
1744 ATF_REQUIRE(children[1] > 0);
1746 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1748 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1749 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1752 * The child can't exit until the grandchild reports status, so the
1753 * grandchild should report its exit first to the debugger.
1755 wpid = waitpid(children[1], &status, 0);
1756 ATF_REQUIRE(wpid == children[1]);
1757 ATF_REQUIRE(WIFEXITED(status));
1758 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1761 * The child should report it's vfork() completion before it
1764 wpid = wait(&status);
1765 ATF_REQUIRE(wpid == children[0]);
1766 ATF_REQUIRE(WIFSTOPPED(status));
1767 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1768 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1770 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1772 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1774 wpid = wait(&status);
1775 ATF_REQUIRE(wpid == children[0]);
1776 ATF_REQUIRE(WIFEXITED(status));
1777 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1779 wpid = wait(&status);
1780 ATF_REQUIRE(wpid == -1);
1781 ATF_REQUIRE(errno == ECHILD);
1784 #ifdef HAVE_BREAKPOINT
1786 * Verify that no more events are reported after PT_KILL except for the
1787 * process exit when stopped due to a breakpoint trap.
1789 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1790 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1795 ATF_REQUIRE((fpid = fork()) != -1);
1802 /* The first wait() should report the stop from SIGSTOP. */
1803 wpid = waitpid(fpid, &status, 0);
1804 ATF_REQUIRE(wpid == fpid);
1805 ATF_REQUIRE(WIFSTOPPED(status));
1806 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1808 /* Continue the child ignoring the SIGSTOP. */
1809 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1811 /* The second wait() should report hitting the breakpoint. */
1812 wpid = waitpid(fpid, &status, 0);
1813 ATF_REQUIRE(wpid == fpid);
1814 ATF_REQUIRE(WIFSTOPPED(status));
1815 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1817 /* Kill the child process. */
1818 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1820 /* The last wait() should report the SIGKILL. */
1821 wpid = waitpid(fpid, &status, 0);
1822 ATF_REQUIRE(wpid == fpid);
1823 ATF_REQUIRE(WIFSIGNALED(status));
1824 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1826 wpid = wait(&status);
1827 ATF_REQUIRE(wpid == -1);
1828 ATF_REQUIRE(errno == ECHILD);
1830 #endif /* HAVE_BREAKPOINT */
1833 * Verify that no more events are reported after PT_KILL except for the
1834 * process exit when stopped inside of a system call.
1836 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1837 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1839 struct ptrace_lwpinfo pl;
1843 ATF_REQUIRE((fpid = fork()) != -1);
1850 /* The first wait() should report the stop from SIGSTOP. */
1851 wpid = waitpid(fpid, &status, 0);
1852 ATF_REQUIRE(wpid == fpid);
1853 ATF_REQUIRE(WIFSTOPPED(status));
1854 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1856 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1857 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1859 /* The second wait() should report a system call entry for getpid(). */
1860 wpid = waitpid(fpid, &status, 0);
1861 ATF_REQUIRE(wpid == fpid);
1862 ATF_REQUIRE(WIFSTOPPED(status));
1863 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1865 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1866 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1868 /* Kill the child process. */
1869 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1871 /* The last wait() should report the SIGKILL. */
1872 wpid = waitpid(fpid, &status, 0);
1873 ATF_REQUIRE(wpid == fpid);
1874 ATF_REQUIRE(WIFSIGNALED(status));
1875 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1877 wpid = wait(&status);
1878 ATF_REQUIRE(wpid == -1);
1879 ATF_REQUIRE(errno == ECHILD);
1883 * Verify that no more events are reported after PT_KILL except for the
1884 * process exit when killing a multithreaded process.
1886 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1887 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1889 struct ptrace_lwpinfo pl;
1894 ATF_REQUIRE((fpid = fork()) != -1);
1897 simple_thread_main();
1900 /* The first wait() should report the stop from SIGSTOP. */
1901 wpid = waitpid(fpid, &status, 0);
1902 ATF_REQUIRE(wpid == fpid);
1903 ATF_REQUIRE(WIFSTOPPED(status));
1904 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1906 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1908 main_lwp = pl.pl_lwpid;
1910 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1912 /* Continue the child ignoring the SIGSTOP. */
1913 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1915 /* The first event should be for the child thread's birth. */
1916 wpid = waitpid(fpid, &status, 0);
1917 ATF_REQUIRE(wpid == fpid);
1918 ATF_REQUIRE(WIFSTOPPED(status));
1919 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1921 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1922 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1923 (PL_FLAG_BORN | PL_FLAG_SCX));
1924 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1926 /* Kill the child process. */
1927 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1929 /* The last wait() should report the SIGKILL. */
1930 wpid = waitpid(fpid, &status, 0);
1931 ATF_REQUIRE(wpid == fpid);
1932 ATF_REQUIRE(WIFSIGNALED(status));
1933 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1935 wpid = wait(&status);
1936 ATF_REQUIRE(wpid == -1);
1937 ATF_REQUIRE(errno == ECHILD);
1941 mask_usr1_thread(void *arg)
1943 pthread_barrier_t *pbarrier;
1946 pbarrier = (pthread_barrier_t*)arg;
1948 sigemptyset(&sigmask);
1949 sigaddset(&sigmask, SIGUSR1);
1950 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1952 /* Sync up with other thread after sigmask updated. */
1953 pthread_barrier_wait(pbarrier);
1962 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1963 * and prevents spurious stops due to those other signals.
1965 ATF_TC(ptrace__PT_KILL_competing_signal);
1966 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1969 atf_tc_set_md_var(tc, "require.user", "root");
1971 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1977 pthread_barrier_t barrier;
1978 struct sched_param sched_param;
1980 ATF_REQUIRE((fpid = fork()) != -1);
1982 /* Bind to one CPU so only one thread at a time will run. */
1984 CPU_SET(0, &setmask);
1986 CHILD_REQUIRE(cpuset(&setid) == 0);
1987 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1988 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1990 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1992 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1993 (void*)&barrier) == 0);
1996 * Give the main thread higher priority. The test always
1997 * assumes that, if both threads are able to run, the main
1998 * thread runs first.
2000 sched_param.sched_priority =
2001 (sched_get_priority_max(SCHED_FIFO) +
2002 sched_get_priority_min(SCHED_FIFO)) / 2;
2003 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2004 SCHED_FIFO, &sched_param) == 0);
2005 sched_param.sched_priority -= RQ_PPQ;
2006 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2007 &sched_param) == 0);
2010 sigemptyset(&sigmask);
2011 sigaddset(&sigmask, SIGUSR2);
2012 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2014 /* Sync up with other thread after sigmask updated. */
2015 pthread_barrier_wait(&barrier);
2025 /* The first wait() should report the stop from SIGSTOP. */
2026 wpid = waitpid(fpid, &status, 0);
2027 ATF_REQUIRE(wpid == fpid);
2028 ATF_REQUIRE(WIFSTOPPED(status));
2029 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2031 /* Continue the child ignoring the SIGSTOP. */
2032 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2034 /* Send a signal that only the second thread can handle. */
2035 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2037 /* The second wait() should report the SIGUSR2. */
2038 wpid = waitpid(fpid, &status, 0);
2039 ATF_REQUIRE(wpid == fpid);
2040 ATF_REQUIRE(WIFSTOPPED(status));
2041 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2043 /* Send a signal that only the first thread can handle. */
2044 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2046 /* Replace the SIGUSR2 with a kill. */
2047 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2049 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2050 wpid = waitpid(fpid, &status, 0);
2051 ATF_REQUIRE(wpid == fpid);
2052 ATF_REQUIRE(WIFSIGNALED(status));
2053 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2055 wpid = wait(&status);
2056 ATF_REQUIRE(wpid == -1);
2057 ATF_REQUIRE(errno == ECHILD);
2061 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2062 * and prevents spurious stops caused by those events.
2064 ATF_TC(ptrace__PT_KILL_competing_stop);
2065 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2068 atf_tc_set_md_var(tc, "require.user", "root");
2070 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2076 pthread_barrier_t barrier;
2078 struct ptrace_lwpinfo pl;
2079 struct sched_param sched_param;
2081 ATF_REQUIRE((fpid = fork()) != -1);
2085 /* Bind to one CPU so only one thread at a time will run. */
2087 CPU_SET(0, &setmask);
2089 CHILD_REQUIRE(cpuset(&setid) == 0);
2090 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2091 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2093 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2095 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2096 (void*)&barrier) == 0);
2099 * Give the main thread higher priority. The test always
2100 * assumes that, if both threads are able to run, the main
2101 * thread runs first.
2103 sched_param.sched_priority =
2104 (sched_get_priority_max(SCHED_FIFO) +
2105 sched_get_priority_min(SCHED_FIFO)) / 2;
2106 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2107 SCHED_FIFO, &sched_param) == 0);
2108 sched_param.sched_priority -= RQ_PPQ;
2109 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2110 &sched_param) == 0);
2113 sigemptyset(&sigmask);
2114 sigaddset(&sigmask, SIGUSR2);
2115 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2117 /* Sync up with other thread after sigmask updated. */
2118 pthread_barrier_wait(&barrier);
2120 /* Sync up with the test before doing the getpid(). */
2127 /* The first wait() should report the stop from SIGSTOP. */
2128 wpid = waitpid(fpid, &status, 0);
2129 ATF_REQUIRE(wpid == fpid);
2130 ATF_REQUIRE(WIFSTOPPED(status));
2131 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2133 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2134 main_lwp = pl.pl_lwpid;
2136 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2137 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2140 * Continue until child is done with setup, which is indicated with
2141 * SIGSTOP. Ignore system calls in the meantime.
2144 wpid = waitpid(fpid, &status, 0);
2145 ATF_REQUIRE(wpid == fpid);
2146 ATF_REQUIRE(WIFSTOPPED(status));
2147 if (WSTOPSIG(status) == SIGTRAP) {
2148 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2150 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2152 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2155 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2158 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2159 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2161 wpid = waitpid(fpid, &status, 0);
2162 ATF_REQUIRE(wpid == fpid);
2163 ATF_REQUIRE(WIFSTOPPED(status));
2164 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2166 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2168 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2169 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2170 /* Prevent the main thread from hitting its syscall exit for now. */
2171 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2174 * Proceed, allowing second thread to hit syscall exit for
2175 * pthread_barrier_wait().
2177 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2179 wpid = waitpid(fpid, &status, 0);
2180 ATF_REQUIRE(wpid == fpid);
2181 ATF_REQUIRE(WIFSTOPPED(status));
2182 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2184 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2186 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2187 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2189 /* Send a signal that only the second thread can handle. */
2190 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2192 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2194 /* The next wait() should report the SIGUSR2. */
2195 wpid = waitpid(fpid, &status, 0);
2196 ATF_REQUIRE(wpid == fpid);
2197 ATF_REQUIRE(WIFSTOPPED(status));
2198 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2200 /* Allow the main thread to try to finish its system call. */
2201 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2204 * At this point, the main thread is in the middle of a system call and
2205 * has been resumed. The second thread has taken a SIGUSR2 which will
2206 * be replaced with a SIGKILL below. The main thread will get to run
2207 * first. It should notice the kill request (even though the signal
2208 * replacement occurred in the other thread) and exit accordingly. It
2209 * should not stop for the system call exit event.
2212 /* Replace the SIGUSR2 with a kill. */
2213 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2215 /* The last wait() should report the SIGKILL (not a syscall exit). */
2216 wpid = waitpid(fpid, &status, 0);
2217 ATF_REQUIRE(wpid == fpid);
2218 ATF_REQUIRE(WIFSIGNALED(status));
2219 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2221 wpid = wait(&status);
2222 ATF_REQUIRE(wpid == -1);
2223 ATF_REQUIRE(errno == ECHILD);
2227 sigusr1_handler(int sig)
2230 CHILD_REQUIRE(sig == SIGUSR1);
2235 * Verify that even if the signal queue is full for a child process,
2236 * a PT_KILL will kill the process.
2238 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2239 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2243 int max_pending_per_proc;
2247 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2249 ATF_REQUIRE((fpid = fork()) != -1);
2255 /* The first wait() should report the stop from SIGSTOP. */
2256 wpid = waitpid(fpid, &status, 0);
2257 ATF_REQUIRE(wpid == fpid);
2258 ATF_REQUIRE(WIFSTOPPED(status));
2259 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2261 len = sizeof(max_pending_per_proc);
2262 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2263 &max_pending_per_proc, &len, NULL, 0) == 0);
2265 /* Fill the signal queue. */
2266 for (i = 0; i < max_pending_per_proc; ++i)
2267 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2269 /* Kill the child process. */
2270 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2272 /* The last wait() should report the SIGKILL. */
2273 wpid = waitpid(fpid, &status, 0);
2274 ATF_REQUIRE(wpid == fpid);
2275 ATF_REQUIRE(WIFSIGNALED(status));
2276 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2278 wpid = wait(&status);
2279 ATF_REQUIRE(wpid == -1);
2280 ATF_REQUIRE(errno == ECHILD);
2284 * Verify that when stopped at a system call entry, a signal can be
2285 * requested with PT_CONTINUE which will be delivered once the system
2288 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2289 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2291 struct ptrace_lwpinfo pl;
2295 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2297 ATF_REQUIRE((fpid = fork()) != -1);
2304 /* The first wait() should report the stop from SIGSTOP. */
2305 wpid = waitpid(fpid, &status, 0);
2306 ATF_REQUIRE(wpid == fpid);
2307 ATF_REQUIRE(WIFSTOPPED(status));
2308 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2310 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2311 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2313 /* The second wait() should report a system call entry for getpid(). */
2314 wpid = waitpid(fpid, &status, 0);
2315 ATF_REQUIRE(wpid == fpid);
2316 ATF_REQUIRE(WIFSTOPPED(status));
2317 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2319 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2320 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2322 /* Continue the child process with a signal. */
2323 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2327 * The last wait() should report exit 2, i.e., a normal _exit
2328 * from the signal handler. In the meantime, catch and proceed
2329 * past any syscall stops.
2331 wpid = waitpid(fpid, &status, 0);
2332 ATF_REQUIRE(wpid == fpid);
2333 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2334 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2335 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2336 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2338 ATF_REQUIRE(WIFEXITED(status));
2339 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2344 wpid = wait(&status);
2345 ATF_REQUIRE(wpid == -1);
2346 ATF_REQUIRE(errno == ECHILD);
2350 sigusr1_counting_handler(int sig)
2352 static int counter = 0;
2354 CHILD_REQUIRE(sig == SIGUSR1);
2361 * Verify that, when continuing from a stop at system call entry and exit,
2362 * a signal can be requested from both stops, and both will be delivered when
2363 * the system call is complete.
2365 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2366 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2368 struct ptrace_lwpinfo pl;
2372 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2374 ATF_REQUIRE((fpid = fork()) != -1);
2381 /* The first wait() should report the stop from SIGSTOP. */
2382 wpid = waitpid(fpid, &status, 0);
2383 ATF_REQUIRE(wpid == fpid);
2384 ATF_REQUIRE(WIFSTOPPED(status));
2385 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2387 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2388 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2390 /* The second wait() should report a system call entry for getpid(). */
2391 wpid = waitpid(fpid, &status, 0);
2392 ATF_REQUIRE(wpid == fpid);
2393 ATF_REQUIRE(WIFSTOPPED(status));
2394 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2396 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2397 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2399 /* Continue the child process with a signal. */
2400 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2402 /* The third wait() should report a system call exit for getpid(). */
2403 wpid = waitpid(fpid, &status, 0);
2404 ATF_REQUIRE(wpid == fpid);
2405 ATF_REQUIRE(WIFSTOPPED(status));
2406 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2408 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2409 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2411 /* Continue the child process with a signal. */
2412 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2416 * The last wait() should report exit 2, i.e., a normal _exit
2417 * from the signal handler. In the meantime, catch and proceed
2418 * past any syscall stops.
2420 wpid = waitpid(fpid, &status, 0);
2421 ATF_REQUIRE(wpid == fpid);
2422 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2423 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2424 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2425 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2427 ATF_REQUIRE(WIFEXITED(status));
2428 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2433 wpid = wait(&status);
2434 ATF_REQUIRE(wpid == -1);
2435 ATF_REQUIRE(errno == ECHILD);
2439 * Verify that even if the signal queue is full for a child process,
2440 * a PT_CONTINUE with a signal will not result in loss of that signal.
2442 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2443 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2447 int max_pending_per_proc;
2451 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2452 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2454 ATF_REQUIRE((fpid = fork()) != -1);
2460 /* The first wait() should report the stop from SIGSTOP. */
2461 wpid = waitpid(fpid, &status, 0);
2462 ATF_REQUIRE(wpid == fpid);
2463 ATF_REQUIRE(WIFSTOPPED(status));
2464 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2466 len = sizeof(max_pending_per_proc);
2467 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2468 &max_pending_per_proc, &len, NULL, 0) == 0);
2470 /* Fill the signal queue. */
2471 for (i = 0; i < max_pending_per_proc; ++i)
2472 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2474 /* Continue with signal. */
2475 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2478 wpid = waitpid(fpid, &status, 0);
2479 ATF_REQUIRE(wpid == fpid);
2480 if (WIFSTOPPED(status)) {
2481 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2482 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2485 * The last wait() should report normal _exit from the
2488 ATF_REQUIRE(WIFEXITED(status));
2489 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2494 wpid = wait(&status);
2495 ATF_REQUIRE(wpid == -1);
2496 ATF_REQUIRE(errno == ECHILD);
2499 static sem_t sigusr1_sem;
2500 static int got_usr1;
2503 sigusr1_sempost_handler(int sig __unused)
2507 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2511 * Verify that even if the signal queue is full for a child process,
2512 * and the signal is masked, a PT_CONTINUE with a signal will not
2513 * result in loss of that signal.
2515 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2516 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2518 struct ptrace_lwpinfo pl;
2521 int max_pending_per_proc;
2526 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2527 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2528 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2531 ATF_REQUIRE((fpid = fork()) != -1);
2533 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2534 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2535 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2538 CHILD_REQUIRE(got_usr1 == 0);
2540 /* Allow the pending SIGUSR1 in now. */
2541 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2542 /* Wait to receive the SIGUSR1. */
2544 err = sem_wait(&sigusr1_sem);
2545 CHILD_REQUIRE(err == 0 || errno == EINTR);
2546 } while (err != 0 && errno == EINTR);
2547 CHILD_REQUIRE(got_usr1 == 1);
2551 /* The first wait() should report the stop from SIGSTOP. */
2552 wpid = waitpid(fpid, &status, 0);
2553 ATF_REQUIRE(wpid == fpid);
2554 ATF_REQUIRE(WIFSTOPPED(status));
2555 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2557 len = sizeof(max_pending_per_proc);
2558 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2559 &max_pending_per_proc, &len, NULL, 0) == 0);
2561 /* Fill the signal queue. */
2562 for (i = 0; i < max_pending_per_proc; ++i)
2563 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2565 /* Continue with signal. */
2566 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2568 /* Collect and ignore all of the SIGUSR2. */
2569 for (i = 0; i < max_pending_per_proc; ++i) {
2570 wpid = waitpid(fpid, &status, 0);
2571 ATF_REQUIRE(wpid == fpid);
2572 ATF_REQUIRE(WIFSTOPPED(status));
2573 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2574 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2577 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2578 wpid = waitpid(fpid, &status, 0);
2579 ATF_REQUIRE(wpid == fpid);
2580 ATF_REQUIRE(WIFSTOPPED(status));
2581 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2582 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2583 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2585 /* Continue the child, ignoring the SIGUSR1. */
2586 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2588 /* The last wait() should report exit after receiving SIGUSR1. */
2589 wpid = waitpid(fpid, &status, 0);
2590 ATF_REQUIRE(wpid == fpid);
2591 ATF_REQUIRE(WIFEXITED(status));
2592 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2594 wpid = wait(&status);
2595 ATF_REQUIRE(wpid == -1);
2596 ATF_REQUIRE(errno == ECHILD);
2600 * Verify that, after stopping due to a signal, that signal can be
2601 * replaced with another signal.
2603 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2604 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2606 struct ptrace_lwpinfo pl;
2610 ATF_REQUIRE((fpid = fork()) != -1);
2617 /* The first wait() should report the stop from SIGSTOP. */
2618 wpid = waitpid(fpid, &status, 0);
2619 ATF_REQUIRE(wpid == fpid);
2620 ATF_REQUIRE(WIFSTOPPED(status));
2621 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2623 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2625 /* Send a signal without ptrace. */
2626 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2628 /* The second wait() should report a SIGINT was received. */
2629 wpid = waitpid(fpid, &status, 0);
2630 ATF_REQUIRE(wpid == fpid);
2631 ATF_REQUIRE(WIFSTOPPED(status));
2632 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2634 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2635 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2636 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2638 /* Continue the child process with a different signal. */
2639 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2642 * The last wait() should report having died due to the new
2645 wpid = waitpid(fpid, &status, 0);
2646 ATF_REQUIRE(wpid == fpid);
2647 ATF_REQUIRE(WIFSIGNALED(status));
2648 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2650 wpid = wait(&status);
2651 ATF_REQUIRE(wpid == -1);
2652 ATF_REQUIRE(errno == ECHILD);
2656 * Verify that a signal can be passed through to the child even when there
2657 * was no true signal originally. Such cases arise when a SIGTRAP is
2658 * invented for e.g, system call stops.
2660 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2661 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2663 struct ptrace_lwpinfo pl;
2668 ATF_REQUIRE((fpid = fork()) != -1);
2671 /* SIGTRAP expected to cause exit on syscall entry. */
2672 rl.rlim_cur = rl.rlim_max = 0;
2673 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2678 /* The first wait() should report the stop from SIGSTOP. */
2679 wpid = waitpid(fpid, &status, 0);
2680 ATF_REQUIRE(wpid == fpid);
2681 ATF_REQUIRE(WIFSTOPPED(status));
2682 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2684 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2685 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2687 /* The second wait() should report a system call entry for getpid(). */
2688 wpid = waitpid(fpid, &status, 0);
2689 ATF_REQUIRE(wpid == fpid);
2690 ATF_REQUIRE(WIFSTOPPED(status));
2691 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2693 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2694 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2696 /* Continue the child process with a SIGTRAP. */
2697 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2701 * The last wait() should report exit due to SIGTRAP. In the
2702 * meantime, catch and proceed past any syscall stops.
2704 wpid = waitpid(fpid, &status, 0);
2705 ATF_REQUIRE(wpid == fpid);
2706 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2707 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2708 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2709 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2711 ATF_REQUIRE(WIFSIGNALED(status));
2712 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2717 wpid = wait(&status);
2718 ATF_REQUIRE(wpid == -1);
2719 ATF_REQUIRE(errno == ECHILD);
2724 * A mixed bag PT_CONTINUE with signal test.
2726 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2727 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2729 struct ptrace_lwpinfo pl;
2733 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2735 ATF_REQUIRE((fpid = fork()) != -1);
2742 /* The first wait() should report the stop from SIGSTOP. */
2743 wpid = waitpid(fpid, &status, 0);
2744 ATF_REQUIRE(wpid == fpid);
2745 ATF_REQUIRE(WIFSTOPPED(status));
2746 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2748 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2749 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2751 /* The second wait() should report a system call entry for getpid(). */
2752 wpid = waitpid(fpid, &status, 0);
2753 ATF_REQUIRE(wpid == fpid);
2754 ATF_REQUIRE(WIFSTOPPED(status));
2755 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2757 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2758 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2760 /* Continue with the first SIGUSR1. */
2761 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2763 /* The next wait() should report a system call exit for getpid(). */
2764 wpid = waitpid(fpid, &status, 0);
2765 ATF_REQUIRE(wpid == fpid);
2766 ATF_REQUIRE(WIFSTOPPED(status));
2767 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2769 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2770 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2772 /* Send an ABRT without ptrace. */
2773 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2775 /* Continue normally. */
2776 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2778 /* The next wait() should report the SIGABRT. */
2779 wpid = waitpid(fpid, &status, 0);
2780 ATF_REQUIRE(wpid == fpid);
2781 ATF_REQUIRE(WIFSTOPPED(status));
2782 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2784 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2785 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2786 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2788 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2789 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2793 * The last wait() should report exit 2, i.e., a normal _exit
2794 * from the signal handler. In the meantime, catch and proceed
2795 * past any syscall stops.
2797 wpid = waitpid(fpid, &status, 0);
2798 ATF_REQUIRE(wpid == fpid);
2799 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2800 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2801 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2802 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2804 ATF_REQUIRE(WIFEXITED(status));
2805 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2810 wpid = wait(&status);
2811 ATF_REQUIRE(wpid == -1);
2812 ATF_REQUIRE(errno == ECHILD);
2817 * Verify a signal delivered by ptrace is noticed by kevent(2).
2819 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2820 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2823 int status, kq, nevents;
2826 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2828 ATF_REQUIRE((fpid = fork()) != -1);
2830 CHILD_REQUIRE((kq = kqueue()) > 0);
2831 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2832 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2837 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2838 if (nevents == -1 && errno == EINTR)
2840 CHILD_REQUIRE(nevents > 0);
2841 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2842 CHILD_REQUIRE(kev.ident == SIGUSR1);
2849 /* The first wait() should report the stop from SIGSTOP. */
2850 wpid = waitpid(fpid, &status, 0);
2851 ATF_REQUIRE(wpid == fpid);
2852 ATF_REQUIRE(WIFSTOPPED(status));
2853 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2855 /* Continue with the SIGUSR1. */
2856 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2859 * The last wait() should report normal exit with code 1.
2861 wpid = waitpid(fpid, &status, 0);
2862 ATF_REQUIRE(wpid == fpid);
2863 ATF_REQUIRE(WIFEXITED(status));
2864 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2866 wpid = wait(&status);
2867 ATF_REQUIRE(wpid == -1);
2868 ATF_REQUIRE(errno == ECHILD);
2872 signal_thread(void *arg)
2877 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2879 /* Wait for this thread to receive a SIGUSR1. */
2881 err = sem_wait(&sigusr1_sem);
2882 CHILD_REQUIRE(err == 0 || errno == EINTR);
2883 } while (err != 0 && errno == EINTR);
2885 /* Free our companion thread from the barrier. */
2886 pthread_barrier_wait(pbarrier);
2889 * Swap ignore duties; the next SIGUSR1 should go to the
2892 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2893 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2894 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2896 /* Sync up threads after swapping signal masks. */
2897 pthread_barrier_wait(pbarrier);
2899 /* Wait until our companion has received its SIGUSR1. */
2900 pthread_barrier_wait(pbarrier);
2906 * Verify that a traced process with blocked signal received the
2907 * signal from kill() once unmasked.
2909 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2910 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2912 struct ptrace_lwpinfo pl;
2917 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2918 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2921 ATF_REQUIRE((fpid = fork()) != -1);
2923 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2924 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2925 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2928 CHILD_REQUIRE(got_usr1 == 0);
2930 /* Allow the pending SIGUSR1 in now. */
2931 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2932 /* Wait to receive a SIGUSR1. */
2934 err = sem_wait(&sigusr1_sem);
2935 CHILD_REQUIRE(err == 0 || errno == EINTR);
2936 } while (err != 0 && errno == EINTR);
2937 CHILD_REQUIRE(got_usr1 == 1);
2941 /* The first wait() should report the stop from SIGSTOP. */
2942 wpid = waitpid(fpid, &status, 0);
2943 ATF_REQUIRE(wpid == fpid);
2944 ATF_REQUIRE(WIFSTOPPED(status));
2945 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2946 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2947 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2949 /* Send blocked SIGUSR1 which should cause a stop. */
2950 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2952 /* Continue the child ignoring the SIGSTOP. */
2953 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2955 /* The next wait() should report the kill(SIGUSR1) was received. */
2956 wpid = waitpid(fpid, &status, 0);
2957 ATF_REQUIRE(wpid == fpid);
2958 ATF_REQUIRE(WIFSTOPPED(status));
2959 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2960 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2961 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2963 /* Continue the child, allowing in the SIGUSR1. */
2964 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2966 /* The last wait() should report normal exit with code 1. */
2967 wpid = waitpid(fpid, &status, 0);
2968 ATF_REQUIRE(wpid == fpid);
2969 ATF_REQUIRE(WIFEXITED(status));
2970 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2972 wpid = wait(&status);
2973 ATF_REQUIRE(wpid == -1);
2974 ATF_REQUIRE(errno == ECHILD);
2978 * Verify that a traced process with blocked signal received the
2979 * signal from PT_CONTINUE once unmasked.
2981 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2982 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2984 struct ptrace_lwpinfo pl;
2989 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2990 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2993 ATF_REQUIRE((fpid = fork()) != -1);
2995 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2996 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2997 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3000 CHILD_REQUIRE(got_usr1 == 0);
3002 /* Allow the pending SIGUSR1 in now. */
3003 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3004 /* Wait to receive a SIGUSR1. */
3006 err = sem_wait(&sigusr1_sem);
3007 CHILD_REQUIRE(err == 0 || errno == EINTR);
3008 } while (err != 0 && errno == EINTR);
3010 CHILD_REQUIRE(got_usr1 == 1);
3014 /* The first wait() should report the stop from SIGSTOP. */
3015 wpid = waitpid(fpid, &status, 0);
3016 ATF_REQUIRE(wpid == fpid);
3017 ATF_REQUIRE(WIFSTOPPED(status));
3018 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3019 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3020 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3022 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3023 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3025 /* The next wait() should report the SIGUSR1 was received. */
3026 wpid = waitpid(fpid, &status, 0);
3027 ATF_REQUIRE(wpid == fpid);
3028 ATF_REQUIRE(WIFSTOPPED(status));
3029 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3030 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3031 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3033 /* Continue the child, ignoring the SIGUSR1. */
3034 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3036 /* The last wait() should report normal exit with code 1. */
3037 wpid = waitpid(fpid, &status, 0);
3038 ATF_REQUIRE(wpid == fpid);
3039 ATF_REQUIRE(WIFEXITED(status));
3040 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3042 wpid = wait(&status);
3043 ATF_REQUIRE(wpid == -1);
3044 ATF_REQUIRE(errno == ECHILD);
3048 * Verify that if ptrace stops due to a signal but continues with
3049 * a different signal that the new signal is routed to a thread
3050 * that can accept it, and that the thread is awakened by the signal
3051 * in a timely manner.
3053 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3054 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3060 pthread_barrier_t barrier;
3062 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3063 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3064 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3066 ATF_REQUIRE((fpid = fork()) != -1);
3068 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3070 /* The other thread should receive the first SIGUSR1. */
3071 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3072 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3073 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3077 /* Wait until other thread has received its SIGUSR1. */
3078 pthread_barrier_wait(&barrier);
3081 * Swap ignore duties; the next SIGUSR1 should go to this
3084 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3086 /* Sync up threads after swapping signal masks. */
3087 pthread_barrier_wait(&barrier);
3090 * Sync up with test code; we're ready for the next SIGUSR1
3095 /* Wait for this thread to receive a SIGUSR1. */
3097 err = sem_wait(&sigusr1_sem);
3098 CHILD_REQUIRE(err == 0 || errno == EINTR);
3099 } while (err != 0 && errno == EINTR);
3101 /* Free the other thread from the barrier. */
3102 pthread_barrier_wait(&barrier);
3104 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3109 /* The first wait() should report the stop from SIGSTOP. */
3110 wpid = waitpid(fpid, &status, 0);
3111 ATF_REQUIRE(wpid == fpid);
3112 ATF_REQUIRE(WIFSTOPPED(status));
3113 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3115 /* Continue the child ignoring the SIGSTOP. */
3116 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3119 * Send a signal without ptrace that either thread will accept (USR2,
3122 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3124 /* The second wait() should report a SIGUSR2 was received. */
3125 wpid = waitpid(fpid, &status, 0);
3126 ATF_REQUIRE(wpid == fpid);
3127 ATF_REQUIRE(WIFSTOPPED(status));
3128 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3130 /* Continue the child, changing the signal to USR1. */
3131 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3133 /* The next wait() should report the stop from SIGSTOP. */
3134 wpid = waitpid(fpid, &status, 0);
3135 ATF_REQUIRE(wpid == fpid);
3136 ATF_REQUIRE(WIFSTOPPED(status));
3137 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3139 /* Continue the child ignoring the SIGSTOP. */
3140 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3142 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3144 /* The next wait() should report a SIGUSR2 was received. */
3145 wpid = waitpid(fpid, &status, 0);
3146 ATF_REQUIRE(wpid == fpid);
3147 ATF_REQUIRE(WIFSTOPPED(status));
3148 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3150 /* Continue the child, changing the signal to USR1. */
3151 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3153 /* The last wait() should report normal exit with code 1. */
3154 wpid = waitpid(fpid, &status, 0);
3155 ATF_REQUIRE(wpid == fpid);
3156 ATF_REQUIRE(WIFEXITED(status));
3157 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3159 wpid = wait(&status);
3160 ATF_REQUIRE(wpid == -1);
3161 ATF_REQUIRE(errno == ECHILD);
3165 raise_sigstop_thread(void *arg __unused)
3173 sleep_thread(void *arg __unused)
3181 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3190 * Become the reaper for this process tree. We need to be able to check
3191 * that both child and grandchild have died.
3193 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3196 ATF_REQUIRE(fpid >= 0);
3199 CHILD_REQUIRE(fpid >= 0);
3203 /* Pin to CPU 0 to serialize thread execution. */
3205 CPU_SET(0, &setmask);
3206 CHILD_REQUIRE(cpuset(&setid) == 0);
3207 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3208 CPU_WHICH_CPUSET, setid,
3209 sizeof(setmask), &setmask) == 0);
3211 if (sigstop_from_main_thread) {
3213 * We expect the SIGKILL sent when our parent
3214 * dies to be delivered to the new thread.
3215 * Raise the SIGSTOP in this thread so the
3218 CHILD_REQUIRE(pthread_create(&t, NULL,
3219 sleep_thread, NULL) == 0);
3223 * We expect the SIGKILL to be delivered to
3224 * this thread. After creating the new thread,
3225 * just get off the CPU so the other thread can
3226 * raise the SIGSTOP.
3228 CHILD_REQUIRE(pthread_create(&t, NULL,
3229 raise_sigstop_thread, NULL) == 0);
3235 /* First stop is trace_me() immediately after fork. */
3236 wpid = waitpid(fpid, &status, 0);
3237 CHILD_REQUIRE(wpid == fpid);
3238 CHILD_REQUIRE(WIFSTOPPED(status));
3239 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3241 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3243 /* Second stop is from the raise(SIGSTOP). */
3244 wpid = waitpid(fpid, &status, 0);
3245 CHILD_REQUIRE(wpid == fpid);
3246 CHILD_REQUIRE(WIFSTOPPED(status));
3247 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3250 * Terminate tracing process without detaching. Our child
3257 * We should get a normal exit from our immediate child and a SIGKILL
3258 * exit from our grandchild. The latter case is the interesting one.
3259 * Our grandchild should not have stopped due to the SIGSTOP that was
3260 * left dangling when its parent died.
3262 for (i = 0; i < 2; ++i) {
3263 wpid = wait(&status);
3265 ATF_REQUIRE(WIFEXITED(status) &&
3266 WEXITSTATUS(status) == 0);
3268 ATF_REQUIRE(WIFSIGNALED(status) &&
3269 WTERMSIG(status) == SIGKILL);
3274 * These two tests ensure that if the tracing process exits without detaching
3275 * just after the child received a SIGSTOP, the child is cleanly killed and
3276 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3277 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3278 * different threads, the SIGKILL must win. There are two variants of this
3279 * test, designed to catch the case where the SIGKILL is delivered to the
3280 * younger thread (the first test) and the case where the SIGKILL is delivered
3281 * to the older thread (the second test). This behavior has changed in the
3282 * past, so make no assumption.
3284 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3285 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3288 atf_tc_set_md_var(tc, "require.user", "root");
3290 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3293 terminate_with_pending_sigstop(true);
3296 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3297 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3300 atf_tc_set_md_var(tc, "require.user", "root");
3302 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3305 terminate_with_pending_sigstop(false);
3309 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3312 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3313 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3315 struct ptrace_lwpinfo pl;
3317 int status, event_mask, new_event_mask;
3319 ATF_REQUIRE((fpid = fork()) != -1);
3326 /* The first wait() should report the stop from trace_me(). */
3327 wpid = waitpid(fpid, &status, 0);
3328 ATF_REQUIRE(wpid == fpid);
3329 ATF_REQUIRE(WIFSTOPPED(status));
3330 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3332 /* Set several unobtrusive event bits. */
3333 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3334 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3335 sizeof(event_mask)) == 0);
3337 /* Send a SIGKILL without using ptrace. */
3338 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3340 /* Continue the child ignoring the SIGSTOP. */
3341 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3343 /* The next stop should be due to the SIGKILL. */
3344 wpid = waitpid(fpid, &status, 0);
3345 ATF_REQUIRE(wpid == fpid);
3346 ATF_REQUIRE(WIFSTOPPED(status));
3347 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3349 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3350 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3351 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3353 /* Continue the child ignoring the SIGKILL. */
3354 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3356 /* The next wait() should report the stop from SIGSTOP. */
3357 wpid = waitpid(fpid, &status, 0);
3358 ATF_REQUIRE(wpid == fpid);
3359 ATF_REQUIRE(WIFSTOPPED(status));
3360 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3362 /* Check the current event mask. It should not have changed. */
3364 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3365 sizeof(new_event_mask)) == 0);
3366 ATF_REQUIRE(event_mask == new_event_mask);
3368 /* Continue the child to let it exit. */
3369 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3371 /* The last event should be for the child process's exit. */
3372 wpid = waitpid(fpid, &status, 0);
3373 ATF_REQUIRE(WIFEXITED(status));
3374 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3376 wpid = wait(&status);
3377 ATF_REQUIRE(wpid == -1);
3378 ATF_REQUIRE(errno == ECHILD);
3382 flock_thread(void *arg)
3387 (void)flock(fd, LOCK_EX);
3388 (void)flock(fd, LOCK_UN);
3393 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3394 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3395 * on a lock. This is a regression test for r318191.
3397 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3398 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3400 pthread_barrier_t barrier;
3401 pthread_barrierattr_t battr;
3404 int error, fd, i, status;
3406 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3407 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3408 PTHREAD_PROCESS_SHARED) == 0);
3409 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3411 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3412 fd = mkstemp(tmpfile);
3413 ATF_REQUIRE(fd >= 0);
3415 ATF_REQUIRE((child = fork()) != -1);
3420 error = pthread_barrier_wait(&barrier);
3421 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3424 cfd = open(tmpfile, O_RDONLY);
3429 * We want at least two threads blocked on the file lock since
3430 * the SIGSTOP from PT_ATTACH may kick one of them out of
3433 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3435 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3437 if (pthread_join(t[0], NULL) != 0)
3439 if (pthread_join(t[1], NULL) != 0)
3444 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3446 error = pthread_barrier_wait(&barrier);
3447 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3450 * Give the child some time to block. Is there a better way to do this?
3455 * Attach and give the child 3 seconds to stop.
3457 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3458 for (i = 0; i < 3; i++) {
3459 wpid = waitpid(child, &status, WNOHANG);
3460 if (wpid == child && WIFSTOPPED(status) &&
3461 WSTOPSIG(status) == SIGSTOP)
3465 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3467 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3469 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3470 ATF_REQUIRE(unlink(tmpfile) == 0);
3471 ATF_REQUIRE(close(fd) == 0);
3475 sigusr1_step_handler(int sig)
3478 CHILD_REQUIRE(sig == SIGUSR1);
3483 * Verify that PT_STEP with a signal invokes the signal before
3484 * stepping the next instruction (and that the next instruction is
3485 * stepped correctly).
3487 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3488 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3490 struct ptrace_lwpinfo pl;
3494 ATF_REQUIRE((fpid = fork()) != -1);
3497 signal(SIGUSR1, sigusr1_step_handler);
3502 /* The first wait() should report the stop from SIGSTOP. */
3503 wpid = waitpid(fpid, &status, 0);
3504 ATF_REQUIRE(wpid == fpid);
3505 ATF_REQUIRE(WIFSTOPPED(status));
3506 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3508 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3510 /* The next stop should report the SIGABRT in the child body. */
3511 wpid = waitpid(fpid, &status, 0);
3512 ATF_REQUIRE(wpid == fpid);
3513 ATF_REQUIRE(WIFSTOPPED(status));
3514 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3516 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3517 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3518 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3520 /* Step the child process inserting SIGUSR1. */
3521 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3523 /* The next stop should report the SIGABRT in the signal handler. */
3524 wpid = waitpid(fpid, &status, 0);
3525 ATF_REQUIRE(wpid == fpid);
3526 ATF_REQUIRE(WIFSTOPPED(status));
3527 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3529 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3530 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3531 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3533 /* Continue the child process discarding the signal. */
3534 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3536 /* The next stop should report a trace trap from PT_STEP. */
3537 wpid = waitpid(fpid, &status, 0);
3538 ATF_REQUIRE(wpid == fpid);
3539 ATF_REQUIRE(WIFSTOPPED(status));
3540 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3542 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3543 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3544 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3545 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3547 /* Continue the child to let it exit. */
3548 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3550 /* The last event should be for the child process's exit. */
3551 wpid = waitpid(fpid, &status, 0);
3552 ATF_REQUIRE(WIFEXITED(status));
3553 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3555 wpid = wait(&status);
3556 ATF_REQUIRE(wpid == -1);
3557 ATF_REQUIRE(errno == ECHILD);
3560 #ifdef HAVE_BREAKPOINT
3562 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3563 * for a breakpoint trap.
3565 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3566 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3568 struct ptrace_lwpinfo pl;
3572 ATF_REQUIRE((fpid = fork()) != -1);
3579 /* The first wait() should report the stop from SIGSTOP. */
3580 wpid = waitpid(fpid, &status, 0);
3581 ATF_REQUIRE(wpid == fpid);
3582 ATF_REQUIRE(WIFSTOPPED(status));
3583 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3585 /* Continue the child ignoring the SIGSTOP. */
3586 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3588 /* The second wait() should report hitting the breakpoint. */
3589 wpid = waitpid(fpid, &status, 0);
3590 ATF_REQUIRE(wpid == fpid);
3591 ATF_REQUIRE(WIFSTOPPED(status));
3592 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3594 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3595 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3596 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3597 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3599 /* Kill the child process. */
3600 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3602 /* The last wait() should report the SIGKILL. */
3603 wpid = waitpid(fpid, &status, 0);
3604 ATF_REQUIRE(wpid == fpid);
3605 ATF_REQUIRE(WIFSIGNALED(status));
3606 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3608 wpid = wait(&status);
3609 ATF_REQUIRE(wpid == -1);
3610 ATF_REQUIRE(errno == ECHILD);
3612 #endif /* HAVE_BREAKPOINT */
3615 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3616 * for a single-step trap from PT_STEP.
3618 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3619 ATF_TC_BODY(ptrace__step_siginfo, tc)
3621 struct ptrace_lwpinfo pl;
3625 ATF_REQUIRE((fpid = fork()) != -1);
3631 /* The first wait() should report the stop from SIGSTOP. */
3632 wpid = waitpid(fpid, &status, 0);
3633 ATF_REQUIRE(wpid == fpid);
3634 ATF_REQUIRE(WIFSTOPPED(status));
3635 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3637 /* Step the child ignoring the SIGSTOP. */
3638 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3640 /* The second wait() should report a single-step trap. */
3641 wpid = waitpid(fpid, &status, 0);
3642 ATF_REQUIRE(wpid == fpid);
3643 ATF_REQUIRE(WIFSTOPPED(status));
3644 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3646 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3647 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3648 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3649 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3651 /* Continue the child process. */
3652 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3654 /* The last event should be for the child process's exit. */
3655 wpid = waitpid(fpid, &status, 0);
3656 ATF_REQUIRE(WIFEXITED(status));
3657 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3659 wpid = wait(&status);
3660 ATF_REQUIRE(wpid == -1);
3661 ATF_REQUIRE(errno == ECHILD);
3664 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3666 continue_thread(void *arg __unused)
3673 continue_thread_main(void)
3675 pthread_t threads[2];
3677 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3679 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3681 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3682 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3687 * Ensure that PT_CONTINUE clears the status of the thread that
3688 * triggered the stop even if a different thread's LWP was passed to
3691 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3692 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3694 struct ptrace_lwpinfo pl;
3701 ATF_REQUIRE((fpid = fork()) != -1);
3704 continue_thread_main();
3707 /* The first wait() should report the stop from SIGSTOP. */
3708 wpid = waitpid(fpid, &status, 0);
3709 ATF_REQUIRE(wpid == fpid);
3710 ATF_REQUIRE(WIFSTOPPED(status));
3711 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3713 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3716 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3718 /* Continue the child ignoring the SIGSTOP. */
3719 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3721 /* One of the new threads should report it's birth. */
3722 wpid = waitpid(fpid, &status, 0);
3723 ATF_REQUIRE(wpid == fpid);
3724 ATF_REQUIRE(WIFSTOPPED(status));
3725 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3727 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3728 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3729 (PL_FLAG_BORN | PL_FLAG_SCX));
3730 lwps[0] = pl.pl_lwpid;
3733 * Suspend this thread to ensure both threads are alive before
3734 * hitting the breakpoint.
3736 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3738 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3740 /* Second thread should report it's birth. */
3741 wpid = waitpid(fpid, &status, 0);
3742 ATF_REQUIRE(wpid == fpid);
3743 ATF_REQUIRE(WIFSTOPPED(status));
3744 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3746 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3747 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3748 (PL_FLAG_BORN | PL_FLAG_SCX));
3749 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3750 lwps[1] = pl.pl_lwpid;
3752 /* Resume both threads waiting for breakpoint events. */
3753 hit_break[0] = hit_break[1] = false;
3754 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3755 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3757 /* One thread should report a breakpoint. */
3758 wpid = waitpid(fpid, &status, 0);
3759 ATF_REQUIRE(wpid == fpid);
3760 ATF_REQUIRE(WIFSTOPPED(status));
3761 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3763 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3764 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3765 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3766 pl.pl_siginfo.si_code == TRAP_BRKPT);
3767 if (pl.pl_lwpid == lwps[0])
3771 hit_break[i] = true;
3772 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3774 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3777 * Resume both threads but pass the other thread's LWPID to
3780 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3783 * Will now get two thread exit events and one more breakpoint
3786 for (j = 0; j < 3; j++) {
3787 wpid = waitpid(fpid, &status, 0);
3788 ATF_REQUIRE(wpid == fpid);
3789 ATF_REQUIRE(WIFSTOPPED(status));
3790 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3792 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3795 if (pl.pl_lwpid == lwps[0])
3800 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3801 if (pl.pl_flags & PL_FLAG_EXITED) {
3802 ATF_REQUIRE_MSG(hit_break[i],
3803 "exited thread did not report breakpoint");
3806 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3807 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3808 pl.pl_siginfo.si_code == TRAP_BRKPT);
3809 ATF_REQUIRE_MSG(!hit_break[i],
3810 "double breakpoint event");
3811 hit_break[i] = true;
3812 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3815 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3819 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3822 /* Both threads should have exited. */
3823 ATF_REQUIRE(lwps[0] == 0);
3824 ATF_REQUIRE(lwps[1] == 0);
3826 /* The last event should be for the child process's exit. */
3827 wpid = waitpid(fpid, &status, 0);
3828 ATF_REQUIRE(WIFEXITED(status));
3829 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3831 wpid = wait(&status);
3832 ATF_REQUIRE(wpid == -1);
3833 ATF_REQUIRE(errno == ECHILD);
3838 * Verify that PT_LWPINFO doesn't return stale siginfo.
3840 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3841 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3843 struct ptrace_lwpinfo pl;
3847 ATF_REQUIRE((fpid = fork()) != -1);
3854 /* The first wait() should report the stop from SIGSTOP. */
3855 wpid = waitpid(fpid, &status, 0);
3856 ATF_REQUIRE(wpid == fpid);
3857 ATF_REQUIRE(WIFSTOPPED(status));
3858 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3860 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3862 /* The next stop should report the SIGABRT in the child body. */
3863 wpid = waitpid(fpid, &status, 0);
3864 ATF_REQUIRE(wpid == fpid);
3865 ATF_REQUIRE(WIFSTOPPED(status));
3866 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3868 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3869 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3870 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3873 * Continue the process ignoring the signal, but enabling
3876 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3879 * The next stop should report a system call entry from
3880 * exit(). PL_FLAGS_SI should not be set.
3882 wpid = waitpid(fpid, &status, 0);
3883 ATF_REQUIRE(wpid == fpid);
3884 ATF_REQUIRE(WIFSTOPPED(status));
3885 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3887 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3888 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3889 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3891 /* Disable syscall tracing and continue the child to let it exit. */
3892 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3893 sizeof(events)) == 0);
3894 events &= ~PTRACE_SYSCALL;
3895 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3896 sizeof(events)) == 0);
3897 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3899 /* The last event should be for the child process's exit. */
3900 wpid = waitpid(fpid, &status, 0);
3901 ATF_REQUIRE(WIFEXITED(status));
3902 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3904 wpid = wait(&status);
3905 ATF_REQUIRE(wpid == -1);
3906 ATF_REQUIRE(errno == ECHILD);
3912 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3913 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3914 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3915 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3916 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
3917 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3918 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3919 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3920 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3922 ptrace__follow_fork_child_detached_unrelated_debugger);
3924 ptrace__follow_fork_parent_detached_unrelated_debugger);
3925 ATF_TP_ADD_TC(tp, ptrace__getppid);
3926 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3927 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3928 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3929 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3930 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3931 ATF_TP_ADD_TC(tp, ptrace__siginfo);
3932 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3933 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3934 ATF_TP_ADD_TC(tp, ptrace__event_mask);
3935 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3936 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3937 #ifdef HAVE_BREAKPOINT
3938 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3940 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3941 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3942 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3943 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3944 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3945 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3947 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3948 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3949 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3950 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3951 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3952 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3953 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3954 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3955 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3956 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3957 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3958 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3959 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3960 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3961 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3962 #ifdef HAVE_BREAKPOINT
3963 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
3965 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
3966 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3967 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3969 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
3971 return (atf_no_error());