2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/cpuset.h>
31 #include <sys/event.h>
34 #include <sys/procctl.h>
35 #include <sys/ptrace.h>
36 #include <sys/queue.h>
38 #include <sys/syscall.h>
39 #include <sys/sysctl.h>
43 #include <machine/cpufunc.h>
46 #include <semaphore.h>
54 * Architectures with a user-visible breakpoint().
56 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
57 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
59 #define HAVE_BREAKPOINT
63 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
65 #ifdef HAVE_BREAKPOINT
66 #if defined(__aarch64__)
67 #define SKIP_BREAK(reg) ((reg)->elr += 4)
68 #elif defined(__amd64__) || defined(__i386__)
69 #define SKIP_BREAK(reg)
70 #elif defined(__arm__)
71 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
72 #elif defined(__mips__)
73 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
74 #elif defined(__riscv)
75 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
76 #elif defined(__sparc64__)
77 #define SKIP_BREAK(reg) do { \
78 (reg)->r_tpc = (reg)->r_tnpc + 4; \
85 * A variant of ATF_REQUIRE that is suitable for use in child
86 * processes. This only works if the parent process is tripped up by
87 * the early exit and fails some requirement itself.
89 #define CHILD_REQUIRE(exp) do { \
91 child_fail_require(__FILE__, __LINE__, \
96 child_fail_require(const char *file, int line, const char *str)
100 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
101 write(2, buf, strlen(buf));
109 /* Attach the parent process as a tracer of this process. */
110 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
112 /* Trigger a stop. */
117 attach_child(pid_t pid)
122 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
124 wpid = waitpid(pid, &status, 0);
125 ATF_REQUIRE(wpid == pid);
126 ATF_REQUIRE(WIFSTOPPED(status));
127 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
131 wait_for_zombie(pid_t pid)
135 * Wait for a process to exit. This is kind of gross, but
136 * there is not a better way.
138 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
139 * with ESRCH. After that change, a valid struct kinfo_proc
140 * is returned for zombies with ki_stat set to SZOMB.
143 struct kinfo_proc kp;
149 mib[2] = KERN_PROC_PID;
152 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
153 ATF_REQUIRE(errno == ESRCH);
156 if (kp.ki_stat == SZOMB)
163 * Verify that a parent debugger process "sees" the exit of a debugged
164 * process exactly once when attached via PT_TRACE_ME.
166 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
167 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
172 ATF_REQUIRE((child = fork()) != -1);
180 /* Parent process. */
182 /* The first wait() should report the stop from SIGSTOP. */
183 wpid = waitpid(child, &status, 0);
184 ATF_REQUIRE(wpid == child);
185 ATF_REQUIRE(WIFSTOPPED(status));
186 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
188 /* Continue the child ignoring the SIGSTOP. */
189 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
191 /* The second wait() should report the exit status. */
192 wpid = waitpid(child, &status, 0);
193 ATF_REQUIRE(wpid == child);
194 ATF_REQUIRE(WIFEXITED(status));
195 ATF_REQUIRE(WEXITSTATUS(status) == 1);
197 /* The child should no longer exist. */
198 wpid = waitpid(child, &status, 0);
199 ATF_REQUIRE(wpid == -1);
200 ATF_REQUIRE(errno == ECHILD);
204 * Verify that a parent debugger process "sees" the exit of a debugged
205 * process exactly once when attached via PT_ATTACH.
207 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
208 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
211 int cpipe[2], status;
214 ATF_REQUIRE(pipe(cpipe) == 0);
215 ATF_REQUIRE((child = fork()) != -1);
220 /* Wait for the parent to attach. */
221 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
227 /* Parent process. */
229 /* Attach to the child process. */
232 /* Continue the child ignoring the SIGSTOP. */
233 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
235 /* Signal the child to exit. */
238 /* The second wait() should report the exit status. */
239 wpid = waitpid(child, &status, 0);
240 ATF_REQUIRE(wpid == child);
241 ATF_REQUIRE(WIFEXITED(status));
242 ATF_REQUIRE(WEXITSTATUS(status) == 1);
244 /* The child should no longer exist. */
245 wpid = waitpid(child, &status, 0);
246 ATF_REQUIRE(wpid == -1);
247 ATF_REQUIRE(errno == ECHILD);
251 * Verify that a parent process "sees" the exit of a debugged process only
252 * after the debugger has seen it.
254 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
255 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
257 pid_t child, debugger, wpid;
258 int cpipe[2], dpipe[2], status;
261 ATF_REQUIRE(pipe(cpipe) == 0);
262 ATF_REQUIRE((child = fork()) != -1);
268 /* Wait for parent to be ready. */
269 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
275 ATF_REQUIRE(pipe(dpipe) == 0);
276 ATF_REQUIRE((debugger = fork()) != -1);
279 /* Debugger process. */
282 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
284 wpid = waitpid(child, &status, 0);
285 CHILD_REQUIRE(wpid == child);
286 CHILD_REQUIRE(WIFSTOPPED(status));
287 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
289 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
291 /* Signal parent that debugger is attached. */
292 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
294 /* Wait for parent's failed wait. */
295 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
297 wpid = waitpid(child, &status, 0);
298 CHILD_REQUIRE(wpid == child);
299 CHILD_REQUIRE(WIFEXITED(status));
300 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
306 /* Parent process. */
308 /* Wait for the debugger to attach to the child. */
309 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
311 /* Release the child. */
312 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
313 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
316 wait_for_zombie(child);
319 * This wait should return a pid of 0 to indicate no status to
320 * report. The parent should see the child as non-exited
321 * until the debugger sees the exit.
323 wpid = waitpid(child, &status, WNOHANG);
324 ATF_REQUIRE(wpid == 0);
326 /* Signal the debugger to wait for the child. */
329 /* Wait for the debugger. */
330 wpid = waitpid(debugger, &status, 0);
331 ATF_REQUIRE(wpid == debugger);
332 ATF_REQUIRE(WIFEXITED(status));
333 ATF_REQUIRE(WEXITSTATUS(status) == 0);
335 /* The child process should now be ready. */
336 wpid = waitpid(child, &status, WNOHANG);
337 ATF_REQUIRE(wpid == child);
338 ATF_REQUIRE(WIFEXITED(status));
339 ATF_REQUIRE(WEXITSTATUS(status) == 1);
343 * Verify that a parent process "sees" the exit of a debugged process
344 * only after a non-direct-child debugger has seen it. In particular,
345 * various wait() calls in the parent must avoid failing with ESRCH by
346 * checking the parent's orphan list for the debugee.
348 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
349 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
351 pid_t child, debugger, fpid, wpid;
352 int cpipe[2], dpipe[2], status;
355 ATF_REQUIRE(pipe(cpipe) == 0);
356 ATF_REQUIRE((child = fork()) != -1);
362 /* Wait for parent to be ready. */
363 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
369 ATF_REQUIRE(pipe(dpipe) == 0);
370 ATF_REQUIRE((debugger = fork()) != -1);
373 /* Debugger parent. */
376 * Fork again and drop the debugger parent so that the
377 * debugger is not a child of the main parent.
379 CHILD_REQUIRE((fpid = fork()) != -1);
383 /* Debugger process. */
386 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
388 wpid = waitpid(child, &status, 0);
389 CHILD_REQUIRE(wpid == child);
390 CHILD_REQUIRE(WIFSTOPPED(status));
391 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
393 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
395 /* Signal parent that debugger is attached. */
396 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
398 /* Wait for parent's failed wait. */
399 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
401 wpid = waitpid(child, &status, 0);
402 CHILD_REQUIRE(wpid == child);
403 CHILD_REQUIRE(WIFEXITED(status));
404 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
410 /* Parent process. */
412 /* Wait for the debugger parent process to exit. */
413 wpid = waitpid(debugger, &status, 0);
414 ATF_REQUIRE(wpid == debugger);
415 ATF_REQUIRE(WIFEXITED(status));
416 ATF_REQUIRE(WEXITSTATUS(status) == 2);
418 /* A WNOHANG wait here should see the non-exited child. */
419 wpid = waitpid(child, &status, WNOHANG);
420 ATF_REQUIRE(wpid == 0);
422 /* Wait for the debugger to attach to the child. */
423 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
425 /* Release the child. */
426 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
427 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
430 wait_for_zombie(child);
433 * This wait should return a pid of 0 to indicate no status to
434 * report. The parent should see the child as non-exited
435 * until the debugger sees the exit.
437 wpid = waitpid(child, &status, WNOHANG);
438 ATF_REQUIRE(wpid == 0);
440 /* Signal the debugger to wait for the child. */
441 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
443 /* Wait for the debugger. */
444 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
447 /* The child process should now be ready. */
448 wpid = waitpid(child, &status, WNOHANG);
449 ATF_REQUIRE(wpid == child);
450 ATF_REQUIRE(WIFEXITED(status));
451 ATF_REQUIRE(WEXITSTATUS(status) == 1);
455 * Make sure that we can collect the exit status of an orphaned process.
457 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
458 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
461 int cpipe1[2], cpipe2[2], gcpipe[2], status;
464 ATF_REQUIRE(pipe(cpipe1) == 0);
465 ATF_REQUIRE(pipe(cpipe2) == 0);
466 ATF_REQUIRE(pipe(gcpipe) == 0);
468 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
470 ATF_REQUIRE((child = fork()) != -1);
472 CHILD_REQUIRE((gchild = fork()) != -1);
476 n = read(gcpipe[0], &status, sizeof(status));
477 } while (n == -1 && errno == EINTR);
481 CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
483 CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
488 ATF_REQUIRE(read(cpipe1[0], &gchild, sizeof(gchild)) == sizeof(gchild));
490 ATF_REQUIRE(ptrace(PT_ATTACH, gchild, NULL, 0) == 0);
493 ATF_REQUIRE(write(cpipe2[1], &status, sizeof(status)) ==
495 ATF_REQUIRE(waitpid(child, &status, 0) == child);
496 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
499 ATF_REQUIRE(write(gcpipe[1], &status, sizeof(status)) ==
501 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
502 ATF_REQUIRE(WIFSTOPPED(status));
503 ATF_REQUIRE(ptrace(PT_DETACH, gchild, (caddr_t)1, 0) == 0);
504 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
505 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
507 ATF_REQUIRE(close(cpipe1[0]) == 0);
508 ATF_REQUIRE(close(cpipe1[1]) == 0);
509 ATF_REQUIRE(close(cpipe2[0]) == 0);
510 ATF_REQUIRE(close(cpipe2[1]) == 0);
511 ATF_REQUIRE(close(gcpipe[0]) == 0);
512 ATF_REQUIRE(close(gcpipe[1]) == 0);
516 * The parent process should always act the same regardless of how the
517 * debugger is attached to it.
520 follow_fork_parent(bool use_vfork)
526 CHILD_REQUIRE((fpid = vfork()) != -1);
528 CHILD_REQUIRE((fpid = fork()) != -1);
534 wpid = waitpid(fpid, &status, 0);
535 CHILD_REQUIRE(wpid == fpid);
536 CHILD_REQUIRE(WIFEXITED(status));
537 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
543 * Helper routine for follow fork tests. This waits for two stops
544 * that report both "sides" of a fork. It returns the pid of the new
548 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
550 struct ptrace_lwpinfo pl;
551 bool fork_reported[2];
555 fork_reported[0] = false;
556 fork_reported[1] = false;
560 * Each process should report a fork event. The parent should
561 * report a PL_FLAG_FORKED event, and the child should report
562 * a PL_FLAG_CHILD event.
564 for (i = 0; i < 2; i++) {
565 wpid = wait(&status);
566 ATF_REQUIRE(wpid > 0);
567 ATF_REQUIRE(WIFSTOPPED(status));
569 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
571 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
573 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
574 (PL_FLAG_FORKED | PL_FLAG_CHILD));
575 if (pl.pl_flags & PL_FLAG_CHILD) {
576 ATF_REQUIRE(wpid != parent);
577 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
578 ATF_REQUIRE(!fork_reported[1]);
582 ATF_REQUIRE(child == wpid);
585 fork_reported[1] = true;
587 ATF_REQUIRE(wpid == parent);
588 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
589 ATF_REQUIRE(!fork_reported[0]);
591 child = pl.pl_child_pid;
593 ATF_REQUIRE(child == pl.pl_child_pid);
596 fork_reported[0] = true;
604 * Verify that a new child process is stopped after a followed fork and
605 * that the traced parent sees the exit of the child after the debugger
606 * when both processes remain attached to the debugger.
608 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
609 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
611 pid_t children[2], fpid, wpid;
614 ATF_REQUIRE((fpid = fork()) != -1);
617 follow_fork_parent(false);
620 /* Parent process. */
623 /* The first wait() should report the stop from SIGSTOP. */
624 wpid = waitpid(children[0], &status, 0);
625 ATF_REQUIRE(wpid == children[0]);
626 ATF_REQUIRE(WIFSTOPPED(status));
627 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
629 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
631 /* Continue the child ignoring the SIGSTOP. */
632 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
634 children[1] = handle_fork_events(children[0], NULL);
635 ATF_REQUIRE(children[1] > 0);
637 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
638 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
641 * The child can't exit until the grandchild reports status, so the
642 * grandchild should report its exit first to the debugger.
644 wpid = wait(&status);
645 ATF_REQUIRE(wpid == children[1]);
646 ATF_REQUIRE(WIFEXITED(status));
647 ATF_REQUIRE(WEXITSTATUS(status) == 2);
649 wpid = wait(&status);
650 ATF_REQUIRE(wpid == children[0]);
651 ATF_REQUIRE(WIFEXITED(status));
652 ATF_REQUIRE(WEXITSTATUS(status) == 1);
654 wpid = wait(&status);
655 ATF_REQUIRE(wpid == -1);
656 ATF_REQUIRE(errno == ECHILD);
660 * Verify that a new child process is stopped after a followed fork
661 * and that the traced parent sees the exit of the child when the new
662 * child process is detached after it reports its fork.
664 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
665 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
667 pid_t children[2], fpid, wpid;
670 ATF_REQUIRE((fpid = fork()) != -1);
673 follow_fork_parent(false);
676 /* Parent process. */
679 /* The first wait() should report the stop from SIGSTOP. */
680 wpid = waitpid(children[0], &status, 0);
681 ATF_REQUIRE(wpid == children[0]);
682 ATF_REQUIRE(WIFSTOPPED(status));
683 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
685 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
687 /* Continue the child ignoring the SIGSTOP. */
688 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
690 children[1] = handle_fork_events(children[0], NULL);
691 ATF_REQUIRE(children[1] > 0);
693 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
694 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
697 * Should not see any status from the grandchild now, only the
700 wpid = wait(&status);
701 ATF_REQUIRE(wpid == children[0]);
702 ATF_REQUIRE(WIFEXITED(status));
703 ATF_REQUIRE(WEXITSTATUS(status) == 1);
705 wpid = wait(&status);
706 ATF_REQUIRE(wpid == -1);
707 ATF_REQUIRE(errno == ECHILD);
711 * Verify that a new child process is stopped after a followed fork
712 * and that the traced parent sees the exit of the child when the
713 * traced parent is detached after the fork.
715 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
716 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
718 pid_t children[2], fpid, wpid;
721 ATF_REQUIRE((fpid = fork()) != -1);
724 follow_fork_parent(false);
727 /* Parent process. */
730 /* The first wait() should report the stop from SIGSTOP. */
731 wpid = waitpid(children[0], &status, 0);
732 ATF_REQUIRE(wpid == children[0]);
733 ATF_REQUIRE(WIFSTOPPED(status));
734 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
736 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
738 /* Continue the child ignoring the SIGSTOP. */
739 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
741 children[1] = handle_fork_events(children[0], NULL);
742 ATF_REQUIRE(children[1] > 0);
744 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
745 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
748 * The child can't exit until the grandchild reports status, so the
749 * grandchild should report its exit first to the debugger.
751 * Even though the child process is detached, it is still a
752 * child of the debugger, so it will still report it's exit
753 * after the grandchild.
755 wpid = wait(&status);
756 ATF_REQUIRE(wpid == children[1]);
757 ATF_REQUIRE(WIFEXITED(status));
758 ATF_REQUIRE(WEXITSTATUS(status) == 2);
760 wpid = wait(&status);
761 ATF_REQUIRE(wpid == children[0]);
762 ATF_REQUIRE(WIFEXITED(status));
763 ATF_REQUIRE(WEXITSTATUS(status) == 1);
765 wpid = wait(&status);
766 ATF_REQUIRE(wpid == -1);
767 ATF_REQUIRE(errno == ECHILD);
771 attach_fork_parent(int cpipe[2])
777 /* Double-fork to disassociate from the debugger. */
778 CHILD_REQUIRE((fpid = fork()) != -1);
782 /* Send the pid of the disassociated child to the debugger. */
784 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
786 /* Wait for the debugger to attach. */
787 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
791 * Verify that a new child process is stopped after a followed fork and
792 * that the traced parent sees the exit of the child after the debugger
793 * when both processes remain attached to the debugger. In this test
794 * the parent that forks is not a direct child of the debugger.
796 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
797 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
799 pid_t children[2], fpid, wpid;
800 int cpipe[2], status;
802 ATF_REQUIRE(pipe(cpipe) == 0);
803 ATF_REQUIRE((fpid = fork()) != -1);
805 attach_fork_parent(cpipe);
806 follow_fork_parent(false);
809 /* Parent process. */
812 /* Wait for the direct child to exit. */
813 wpid = waitpid(fpid, &status, 0);
814 ATF_REQUIRE(wpid == fpid);
815 ATF_REQUIRE(WIFEXITED(status));
816 ATF_REQUIRE(WEXITSTATUS(status) == 3);
818 /* Read the pid of the fork parent. */
819 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
820 sizeof(children[0]));
822 /* Attach to the fork parent. */
823 attach_child(children[0]);
825 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
827 /* Continue the fork parent ignoring the SIGSTOP. */
828 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
830 /* Signal the fork parent to continue. */
833 children[1] = handle_fork_events(children[0], NULL);
834 ATF_REQUIRE(children[1] > 0);
836 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
837 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
840 * The fork parent can't exit until the child reports status,
841 * so the child should report its exit first to the debugger.
843 wpid = wait(&status);
844 ATF_REQUIRE(wpid == children[1]);
845 ATF_REQUIRE(WIFEXITED(status));
846 ATF_REQUIRE(WEXITSTATUS(status) == 2);
848 wpid = wait(&status);
849 ATF_REQUIRE(wpid == children[0]);
850 ATF_REQUIRE(WIFEXITED(status));
851 ATF_REQUIRE(WEXITSTATUS(status) == 1);
853 wpid = wait(&status);
854 ATF_REQUIRE(wpid == -1);
855 ATF_REQUIRE(errno == ECHILD);
859 * Verify that a new child process is stopped after a followed fork
860 * and that the traced parent sees the exit of the child when the new
861 * child process is detached after it reports its fork. In this test
862 * the parent that forks is not a direct child of the debugger.
864 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
865 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
867 pid_t children[2], fpid, wpid;
868 int cpipe[2], status;
870 ATF_REQUIRE(pipe(cpipe) == 0);
871 ATF_REQUIRE((fpid = fork()) != -1);
873 attach_fork_parent(cpipe);
874 follow_fork_parent(false);
877 /* Parent process. */
880 /* Wait for the direct child to exit. */
881 wpid = waitpid(fpid, &status, 0);
882 ATF_REQUIRE(wpid == fpid);
883 ATF_REQUIRE(WIFEXITED(status));
884 ATF_REQUIRE(WEXITSTATUS(status) == 3);
886 /* Read the pid of the fork parent. */
887 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
888 sizeof(children[0]));
890 /* Attach to the fork parent. */
891 attach_child(children[0]);
893 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
895 /* Continue the fork parent ignoring the SIGSTOP. */
896 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
898 /* Signal the fork parent to continue. */
901 children[1] = handle_fork_events(children[0], NULL);
902 ATF_REQUIRE(children[1] > 0);
904 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
905 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
908 * Should not see any status from the child now, only the fork
911 wpid = wait(&status);
912 ATF_REQUIRE(wpid == children[0]);
913 ATF_REQUIRE(WIFEXITED(status));
914 ATF_REQUIRE(WEXITSTATUS(status) == 1);
916 wpid = wait(&status);
917 ATF_REQUIRE(wpid == -1);
918 ATF_REQUIRE(errno == ECHILD);
922 * Verify that a new child process is stopped after a followed fork
923 * and that the traced parent sees the exit of the child when the
924 * traced parent is detached after the fork. In this test the parent
925 * that forks is not a direct child of the debugger.
927 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
928 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
930 pid_t children[2], fpid, wpid;
931 int cpipe[2], status;
933 ATF_REQUIRE(pipe(cpipe) == 0);
934 ATF_REQUIRE((fpid = fork()) != -1);
936 attach_fork_parent(cpipe);
937 follow_fork_parent(false);
940 /* Parent process. */
943 /* Wait for the direct child to exit. */
944 wpid = waitpid(fpid, &status, 0);
945 ATF_REQUIRE(wpid == fpid);
946 ATF_REQUIRE(WIFEXITED(status));
947 ATF_REQUIRE(WEXITSTATUS(status) == 3);
949 /* Read the pid of the fork parent. */
950 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
951 sizeof(children[0]));
953 /* Attach to the fork parent. */
954 attach_child(children[0]);
956 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
958 /* Continue the fork parent ignoring the SIGSTOP. */
959 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
961 /* Signal the fork parent to continue. */
964 children[1] = handle_fork_events(children[0], NULL);
965 ATF_REQUIRE(children[1] > 0);
967 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
968 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
971 * Should not see any status from the fork parent now, only
974 wpid = wait(&status);
975 ATF_REQUIRE(wpid == children[1]);
976 ATF_REQUIRE(WIFEXITED(status));
977 ATF_REQUIRE(WEXITSTATUS(status) == 2);
979 wpid = wait(&status);
980 ATF_REQUIRE(wpid == -1);
981 ATF_REQUIRE(errno == ECHILD);
985 * Verify that a child process does not see an unrelated debugger as its
986 * parent but sees its original parent process.
988 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
989 ATF_TC_BODY(ptrace__getppid, tc)
991 pid_t child, debugger, ppid, wpid;
992 int cpipe[2], dpipe[2], status;
995 ATF_REQUIRE(pipe(cpipe) == 0);
996 ATF_REQUIRE((child = fork()) != -1);
1002 /* Wait for parent to be ready. */
1003 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1005 /* Report the parent PID to the parent. */
1007 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1014 ATF_REQUIRE(pipe(dpipe) == 0);
1015 ATF_REQUIRE((debugger = fork()) != -1);
1017 if (debugger == 0) {
1018 /* Debugger process. */
1021 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1023 wpid = waitpid(child, &status, 0);
1024 CHILD_REQUIRE(wpid == child);
1025 CHILD_REQUIRE(WIFSTOPPED(status));
1026 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1028 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1030 /* Signal parent that debugger is attached. */
1031 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1033 /* Wait for traced child to exit. */
1034 wpid = waitpid(child, &status, 0);
1035 CHILD_REQUIRE(wpid == child);
1036 CHILD_REQUIRE(WIFEXITED(status));
1037 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1043 /* Parent process. */
1045 /* Wait for the debugger to attach to the child. */
1046 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1048 /* Release the child. */
1049 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1051 /* Read the parent PID from the child. */
1052 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1055 ATF_REQUIRE(ppid == getpid());
1057 /* Wait for the debugger. */
1058 wpid = waitpid(debugger, &status, 0);
1059 ATF_REQUIRE(wpid == debugger);
1060 ATF_REQUIRE(WIFEXITED(status));
1061 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1063 /* The child process should now be ready. */
1064 wpid = waitpid(child, &status, WNOHANG);
1065 ATF_REQUIRE(wpid == child);
1066 ATF_REQUIRE(WIFEXITED(status));
1067 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1071 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1072 * child process created via fork() reports the correct value.
1074 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1075 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1077 struct ptrace_lwpinfo pl[2];
1078 pid_t children[2], fpid, wpid;
1081 ATF_REQUIRE((fpid = fork()) != -1);
1084 follow_fork_parent(false);
1087 /* Parent process. */
1090 /* The first wait() should report the stop from SIGSTOP. */
1091 wpid = waitpid(children[0], &status, 0);
1092 ATF_REQUIRE(wpid == children[0]);
1093 ATF_REQUIRE(WIFSTOPPED(status));
1094 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1096 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1098 /* Continue the child ignoring the SIGSTOP. */
1099 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1101 /* Wait for both halves of the fork event to get reported. */
1102 children[1] = handle_fork_events(children[0], pl);
1103 ATF_REQUIRE(children[1] > 0);
1105 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1106 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1107 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1108 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1109 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1111 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1112 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1115 * The child can't exit until the grandchild reports status, so the
1116 * grandchild should report its exit first to the debugger.
1118 wpid = wait(&status);
1119 ATF_REQUIRE(wpid == children[1]);
1120 ATF_REQUIRE(WIFEXITED(status));
1121 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1123 wpid = wait(&status);
1124 ATF_REQUIRE(wpid == children[0]);
1125 ATF_REQUIRE(WIFEXITED(status));
1126 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1128 wpid = wait(&status);
1129 ATF_REQUIRE(wpid == -1);
1130 ATF_REQUIRE(errno == ECHILD);
1134 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1135 * child process created via vfork() reports the correct value.
1137 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1138 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1140 struct ptrace_lwpinfo pl[2];
1141 pid_t children[2], fpid, wpid;
1144 ATF_REQUIRE((fpid = fork()) != -1);
1147 follow_fork_parent(true);
1150 /* Parent process. */
1153 /* The first wait() should report the stop from SIGSTOP. */
1154 wpid = waitpid(children[0], &status, 0);
1155 ATF_REQUIRE(wpid == children[0]);
1156 ATF_REQUIRE(WIFSTOPPED(status));
1157 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1159 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1161 /* Continue the child ignoring the SIGSTOP. */
1162 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1164 /* Wait for both halves of the fork event to get reported. */
1165 children[1] = handle_fork_events(children[0], pl);
1166 ATF_REQUIRE(children[1] > 0);
1168 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1169 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1170 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1171 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1172 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1174 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1175 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1178 * The child can't exit until the grandchild reports status, so the
1179 * grandchild should report its exit first to the debugger.
1181 wpid = wait(&status);
1182 ATF_REQUIRE(wpid == children[1]);
1183 ATF_REQUIRE(WIFEXITED(status));
1184 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1186 wpid = wait(&status);
1187 ATF_REQUIRE(wpid == children[0]);
1188 ATF_REQUIRE(WIFEXITED(status));
1189 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1191 wpid = wait(&status);
1192 ATF_REQUIRE(wpid == -1);
1193 ATF_REQUIRE(errno == ECHILD);
1197 simple_thread(void *arg __unused)
1204 simple_thread_main(void)
1208 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1209 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1214 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1215 * thread reports the correct value.
1217 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1218 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1220 struct ptrace_lwpinfo pl;
1225 ATF_REQUIRE((fpid = fork()) != -1);
1228 simple_thread_main();
1231 /* The first wait() should report the stop from SIGSTOP. */
1232 wpid = waitpid(fpid, &status, 0);
1233 ATF_REQUIRE(wpid == fpid);
1234 ATF_REQUIRE(WIFSTOPPED(status));
1235 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1237 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1239 mainlwp = pl.pl_lwpid;
1242 * Continue the child ignoring the SIGSTOP and tracing all
1243 * system call exits.
1245 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1248 * Wait for the new thread to arrive. pthread_create() might
1249 * invoke any number of system calls. For now we just wait
1250 * for the new thread to arrive and make sure it reports a
1251 * valid system call code. If ptrace grows thread event
1252 * reporting then this test can be made more precise.
1255 wpid = waitpid(fpid, &status, 0);
1256 ATF_REQUIRE(wpid == fpid);
1257 ATF_REQUIRE(WIFSTOPPED(status));
1258 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1260 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1262 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1263 ATF_REQUIRE(pl.pl_syscall_code != 0);
1264 if (pl.pl_lwpid != mainlwp)
1265 /* New thread seen. */
1268 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1271 /* Wait for the child to exit. */
1272 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1274 wpid = waitpid(fpid, &status, 0);
1275 ATF_REQUIRE(wpid == fpid);
1276 if (WIFEXITED(status))
1279 ATF_REQUIRE(WIFSTOPPED(status));
1280 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1281 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1284 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1286 wpid = wait(&status);
1287 ATF_REQUIRE(wpid == -1);
1288 ATF_REQUIRE(errno == ECHILD);
1292 * Verify that the expected LWP events are reported for a child thread.
1294 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1295 ATF_TC_BODY(ptrace__lwp_events, tc)
1297 struct ptrace_lwpinfo pl;
1302 ATF_REQUIRE((fpid = fork()) != -1);
1305 simple_thread_main();
1308 /* The first wait() should report the stop from SIGSTOP. */
1309 wpid = waitpid(fpid, &status, 0);
1310 ATF_REQUIRE(wpid == fpid);
1311 ATF_REQUIRE(WIFSTOPPED(status));
1312 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1314 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1316 lwps[0] = pl.pl_lwpid;
1318 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1320 /* Continue the child ignoring the SIGSTOP. */
1321 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1323 /* The first event should be for the child thread's birth. */
1324 wpid = waitpid(fpid, &status, 0);
1325 ATF_REQUIRE(wpid == fpid);
1326 ATF_REQUIRE(WIFSTOPPED(status));
1327 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1329 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1330 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1331 (PL_FLAG_BORN | PL_FLAG_SCX));
1332 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1333 lwps[1] = pl.pl_lwpid;
1335 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1337 /* The next event should be for the child thread's death. */
1338 wpid = waitpid(fpid, &status, 0);
1339 ATF_REQUIRE(wpid == fpid);
1340 ATF_REQUIRE(WIFSTOPPED(status));
1341 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1343 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1344 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1345 (PL_FLAG_EXITED | PL_FLAG_SCE));
1346 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1348 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1350 /* The last event should be for the child process's exit. */
1351 wpid = waitpid(fpid, &status, 0);
1352 ATF_REQUIRE(WIFEXITED(status));
1353 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1355 wpid = wait(&status);
1356 ATF_REQUIRE(wpid == -1);
1357 ATF_REQUIRE(errno == ECHILD);
1361 exec_thread(void *arg __unused)
1364 execl("/usr/bin/true", "true", NULL);
1369 exec_thread_main(void)
1373 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1380 * Verify that the expected LWP events are reported for a multithreaded
1381 * process that calls execve(2).
1383 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1384 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1386 struct ptrace_lwpinfo pl;
1391 ATF_REQUIRE((fpid = fork()) != -1);
1397 /* The first wait() should report the stop from SIGSTOP. */
1398 wpid = waitpid(fpid, &status, 0);
1399 ATF_REQUIRE(wpid == fpid);
1400 ATF_REQUIRE(WIFSTOPPED(status));
1401 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1403 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1405 lwps[0] = pl.pl_lwpid;
1407 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1409 /* Continue the child ignoring the SIGSTOP. */
1410 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1412 /* The first event should be for the child thread's birth. */
1413 wpid = waitpid(fpid, &status, 0);
1414 ATF_REQUIRE(wpid == fpid);
1415 ATF_REQUIRE(WIFSTOPPED(status));
1416 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1418 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1419 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1420 (PL_FLAG_BORN | PL_FLAG_SCX));
1421 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1422 lwps[1] = pl.pl_lwpid;
1424 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1427 * The next event should be for the main thread's death due to
1428 * single threading from execve().
1430 wpid = waitpid(fpid, &status, 0);
1431 ATF_REQUIRE(wpid == fpid);
1432 ATF_REQUIRE(WIFSTOPPED(status));
1433 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1435 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1436 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1438 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1440 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1442 /* The next event should be for the child process's exec. */
1443 wpid = waitpid(fpid, &status, 0);
1444 ATF_REQUIRE(WIFSTOPPED(status));
1445 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1447 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1448 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1449 (PL_FLAG_EXEC | PL_FLAG_SCX));
1450 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1452 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1454 /* The last event should be for the child process's exit. */
1455 wpid = waitpid(fpid, &status, 0);
1456 ATF_REQUIRE(WIFEXITED(status));
1457 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1459 wpid = wait(&status);
1460 ATF_REQUIRE(wpid == -1);
1461 ATF_REQUIRE(errno == ECHILD);
1465 handler(int sig __unused)
1473 signal(SIGINFO, handler);
1479 * Verify that the expected ptrace event is reported for a signal.
1481 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1482 ATF_TC_BODY(ptrace__siginfo, tc)
1484 struct ptrace_lwpinfo pl;
1488 ATF_REQUIRE((fpid = fork()) != -1);
1494 /* The first wait() should report the stop from SIGSTOP. */
1495 wpid = waitpid(fpid, &status, 0);
1496 ATF_REQUIRE(wpid == fpid);
1497 ATF_REQUIRE(WIFSTOPPED(status));
1498 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1500 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1502 /* The next event should be for the SIGINFO. */
1503 wpid = waitpid(fpid, &status, 0);
1504 ATF_REQUIRE(WIFSTOPPED(status));
1505 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1507 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1508 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1509 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1510 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1511 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1513 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1515 /* The last event should be for the child process's exit. */
1516 wpid = waitpid(fpid, &status, 0);
1517 ATF_REQUIRE(WIFEXITED(status));
1518 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1520 wpid = wait(&status);
1521 ATF_REQUIRE(wpid == -1);
1522 ATF_REQUIRE(errno == ECHILD);
1526 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1528 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1529 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1534 ATF_REQUIRE((fpid = fork()) != -1);
1540 /* The first wait() should report the stop from SIGSTOP. */
1541 wpid = waitpid(fpid, &status, 0);
1542 ATF_REQUIRE(wpid == fpid);
1543 ATF_REQUIRE(WIFSTOPPED(status));
1544 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1547 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1548 sizeof(events)) == 0);
1550 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1552 /* Should get one event at exit. */
1553 wpid = waitpid(fpid, &status, 0);
1554 ATF_REQUIRE(WIFEXITED(status));
1555 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1557 wpid = wait(&status);
1558 ATF_REQUIRE(wpid == -1);
1559 ATF_REQUIRE(errno == ECHILD);
1562 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1563 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1565 struct ptrace_lwpinfo pl;
1569 ATF_REQUIRE((fpid = fork()) != -1);
1575 /* The first wait() should report the stop from SIGSTOP. */
1576 wpid = waitpid(fpid, &status, 0);
1577 ATF_REQUIRE(wpid == fpid);
1578 ATF_REQUIRE(WIFSTOPPED(status));
1579 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1581 events = PTRACE_EXEC;
1582 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1583 sizeof(events)) == 0);
1585 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1587 /* The next event should be for the child process's exec. */
1588 wpid = waitpid(fpid, &status, 0);
1589 ATF_REQUIRE(WIFSTOPPED(status));
1590 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1592 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1593 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1594 (PL_FLAG_EXEC | PL_FLAG_SCX));
1596 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1598 /* The last event should be for the child process's exit. */
1599 wpid = waitpid(fpid, &status, 0);
1600 ATF_REQUIRE(WIFEXITED(status));
1601 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1603 wpid = wait(&status);
1604 ATF_REQUIRE(wpid == -1);
1605 ATF_REQUIRE(errno == ECHILD);
1608 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1609 ATF_TC_BODY(ptrace__event_mask, tc)
1614 ATF_REQUIRE((fpid = fork()) != -1);
1620 /* The first wait() should report the stop from SIGSTOP. */
1621 wpid = waitpid(fpid, &status, 0);
1622 ATF_REQUIRE(wpid == fpid);
1623 ATF_REQUIRE(WIFSTOPPED(status));
1624 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1626 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1627 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1628 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1629 sizeof(events)) == 0);
1630 ATF_REQUIRE(events & PTRACE_FORK);
1631 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1632 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1633 sizeof(events)) == 0);
1634 ATF_REQUIRE(!(events & PTRACE_FORK));
1636 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1637 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1638 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1639 sizeof(events)) == 0);
1640 ATF_REQUIRE(events & PTRACE_LWP);
1641 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1642 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1643 sizeof(events)) == 0);
1644 ATF_REQUIRE(!(events & PTRACE_LWP));
1646 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1648 /* Should get one event at exit. */
1649 wpid = waitpid(fpid, &status, 0);
1650 ATF_REQUIRE(WIFEXITED(status));
1651 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1653 wpid = wait(&status);
1654 ATF_REQUIRE(wpid == -1);
1655 ATF_REQUIRE(errno == ECHILD);
1659 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1661 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1662 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1664 struct ptrace_lwpinfo pl;
1668 ATF_REQUIRE((fpid = fork()) != -1);
1671 follow_fork_parent(true);
1674 /* The first wait() should report the stop from SIGSTOP. */
1675 wpid = waitpid(fpid, &status, 0);
1676 ATF_REQUIRE(wpid == fpid);
1677 ATF_REQUIRE(WIFSTOPPED(status));
1678 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1680 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1681 sizeof(events)) == 0);
1682 events |= PTRACE_VFORK;
1683 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1684 sizeof(events)) == 0);
1686 /* Continue the child ignoring the SIGSTOP. */
1687 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1689 /* The next event should report the end of the vfork. */
1690 wpid = wait(&status);
1691 ATF_REQUIRE(wpid == fpid);
1692 ATF_REQUIRE(WIFSTOPPED(status));
1693 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1694 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1695 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1697 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1699 wpid = wait(&status);
1700 ATF_REQUIRE(wpid == fpid);
1701 ATF_REQUIRE(WIFEXITED(status));
1702 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1704 wpid = wait(&status);
1705 ATF_REQUIRE(wpid == -1);
1706 ATF_REQUIRE(errno == ECHILD);
1709 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1710 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1712 struct ptrace_lwpinfo pl[2];
1713 pid_t children[2], fpid, wpid;
1716 ATF_REQUIRE((fpid = fork()) != -1);
1719 follow_fork_parent(true);
1722 /* Parent process. */
1725 /* The first wait() should report the stop from SIGSTOP. */
1726 wpid = waitpid(children[0], &status, 0);
1727 ATF_REQUIRE(wpid == children[0]);
1728 ATF_REQUIRE(WIFSTOPPED(status));
1729 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1731 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1732 sizeof(events)) == 0);
1733 events |= PTRACE_FORK | PTRACE_VFORK;
1734 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1735 sizeof(events)) == 0);
1737 /* Continue the child ignoring the SIGSTOP. */
1738 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1740 /* Wait for both halves of the fork event to get reported. */
1741 children[1] = handle_fork_events(children[0], pl);
1742 ATF_REQUIRE(children[1] > 0);
1744 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1746 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1747 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1750 * The child can't exit until the grandchild reports status, so the
1751 * grandchild should report its exit first to the debugger.
1753 wpid = waitpid(children[1], &status, 0);
1754 ATF_REQUIRE(wpid == children[1]);
1755 ATF_REQUIRE(WIFEXITED(status));
1756 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1759 * The child should report it's vfork() completion before it
1762 wpid = wait(&status);
1763 ATF_REQUIRE(wpid == children[0]);
1764 ATF_REQUIRE(WIFSTOPPED(status));
1765 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1766 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1768 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1770 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1772 wpid = wait(&status);
1773 ATF_REQUIRE(wpid == children[0]);
1774 ATF_REQUIRE(WIFEXITED(status));
1775 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1777 wpid = wait(&status);
1778 ATF_REQUIRE(wpid == -1);
1779 ATF_REQUIRE(errno == ECHILD);
1782 #ifdef HAVE_BREAKPOINT
1784 * Verify that no more events are reported after PT_KILL except for the
1785 * process exit when stopped due to a breakpoint trap.
1787 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1788 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1793 ATF_REQUIRE((fpid = fork()) != -1);
1800 /* The first wait() should report the stop from SIGSTOP. */
1801 wpid = waitpid(fpid, &status, 0);
1802 ATF_REQUIRE(wpid == fpid);
1803 ATF_REQUIRE(WIFSTOPPED(status));
1804 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1806 /* Continue the child ignoring the SIGSTOP. */
1807 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1809 /* The second wait() should report hitting the breakpoint. */
1810 wpid = waitpid(fpid, &status, 0);
1811 ATF_REQUIRE(wpid == fpid);
1812 ATF_REQUIRE(WIFSTOPPED(status));
1813 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1815 /* Kill the child process. */
1816 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1818 /* The last wait() should report the SIGKILL. */
1819 wpid = waitpid(fpid, &status, 0);
1820 ATF_REQUIRE(wpid == fpid);
1821 ATF_REQUIRE(WIFSIGNALED(status));
1822 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1824 wpid = wait(&status);
1825 ATF_REQUIRE(wpid == -1);
1826 ATF_REQUIRE(errno == ECHILD);
1828 #endif /* HAVE_BREAKPOINT */
1831 * Verify that no more events are reported after PT_KILL except for the
1832 * process exit when stopped inside of a system call.
1834 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1835 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1837 struct ptrace_lwpinfo pl;
1841 ATF_REQUIRE((fpid = fork()) != -1);
1848 /* The first wait() should report the stop from SIGSTOP. */
1849 wpid = waitpid(fpid, &status, 0);
1850 ATF_REQUIRE(wpid == fpid);
1851 ATF_REQUIRE(WIFSTOPPED(status));
1852 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1854 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1855 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1857 /* The second wait() should report a system call entry for getpid(). */
1858 wpid = waitpid(fpid, &status, 0);
1859 ATF_REQUIRE(wpid == fpid);
1860 ATF_REQUIRE(WIFSTOPPED(status));
1861 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1863 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1864 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1866 /* Kill the child process. */
1867 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1869 /* The last wait() should report the SIGKILL. */
1870 wpid = waitpid(fpid, &status, 0);
1871 ATF_REQUIRE(wpid == fpid);
1872 ATF_REQUIRE(WIFSIGNALED(status));
1873 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1875 wpid = wait(&status);
1876 ATF_REQUIRE(wpid == -1);
1877 ATF_REQUIRE(errno == ECHILD);
1881 * Verify that no more events are reported after PT_KILL except for the
1882 * process exit when killing a multithreaded process.
1884 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1885 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1887 struct ptrace_lwpinfo pl;
1892 ATF_REQUIRE((fpid = fork()) != -1);
1895 simple_thread_main();
1898 /* The first wait() should report the stop from SIGSTOP. */
1899 wpid = waitpid(fpid, &status, 0);
1900 ATF_REQUIRE(wpid == fpid);
1901 ATF_REQUIRE(WIFSTOPPED(status));
1902 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1904 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1906 main_lwp = pl.pl_lwpid;
1908 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1910 /* Continue the child ignoring the SIGSTOP. */
1911 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1913 /* The first event should be for the child thread's birth. */
1914 wpid = waitpid(fpid, &status, 0);
1915 ATF_REQUIRE(wpid == fpid);
1916 ATF_REQUIRE(WIFSTOPPED(status));
1917 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1919 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1920 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1921 (PL_FLAG_BORN | PL_FLAG_SCX));
1922 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1924 /* Kill the child process. */
1925 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1927 /* The last wait() should report the SIGKILL. */
1928 wpid = waitpid(fpid, &status, 0);
1929 ATF_REQUIRE(wpid == fpid);
1930 ATF_REQUIRE(WIFSIGNALED(status));
1931 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1933 wpid = wait(&status);
1934 ATF_REQUIRE(wpid == -1);
1935 ATF_REQUIRE(errno == ECHILD);
1939 mask_usr1_thread(void *arg)
1941 pthread_barrier_t *pbarrier;
1944 pbarrier = (pthread_barrier_t*)arg;
1946 sigemptyset(&sigmask);
1947 sigaddset(&sigmask, SIGUSR1);
1948 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1950 /* Sync up with other thread after sigmask updated. */
1951 pthread_barrier_wait(pbarrier);
1960 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1961 * and prevents spurious stops due to those other signals.
1963 ATF_TC(ptrace__PT_KILL_competing_signal);
1964 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1967 atf_tc_set_md_var(tc, "require.user", "root");
1969 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1975 pthread_barrier_t barrier;
1976 struct sched_param sched_param;
1978 ATF_REQUIRE((fpid = fork()) != -1);
1980 /* Bind to one CPU so only one thread at a time will run. */
1982 CPU_SET(0, &setmask);
1984 CHILD_REQUIRE(cpuset(&setid) == 0);
1985 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1986 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1988 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1990 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1991 (void*)&barrier) == 0);
1994 * Give the main thread higher priority. The test always
1995 * assumes that, if both threads are able to run, the main
1996 * thread runs first.
1998 sched_param.sched_priority =
1999 (sched_get_priority_max(SCHED_FIFO) +
2000 sched_get_priority_min(SCHED_FIFO)) / 2;
2001 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2002 SCHED_FIFO, &sched_param) == 0);
2003 sched_param.sched_priority -= RQ_PPQ;
2004 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2005 &sched_param) == 0);
2008 sigemptyset(&sigmask);
2009 sigaddset(&sigmask, SIGUSR2);
2010 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2012 /* Sync up with other thread after sigmask updated. */
2013 pthread_barrier_wait(&barrier);
2023 /* The first wait() should report the stop from SIGSTOP. */
2024 wpid = waitpid(fpid, &status, 0);
2025 ATF_REQUIRE(wpid == fpid);
2026 ATF_REQUIRE(WIFSTOPPED(status));
2027 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2029 /* Continue the child ignoring the SIGSTOP. */
2030 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2032 /* Send a signal that only the second thread can handle. */
2033 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2035 /* The second wait() should report the SIGUSR2. */
2036 wpid = waitpid(fpid, &status, 0);
2037 ATF_REQUIRE(wpid == fpid);
2038 ATF_REQUIRE(WIFSTOPPED(status));
2039 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2041 /* Send a signal that only the first thread can handle. */
2042 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2044 /* Replace the SIGUSR2 with a kill. */
2045 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2047 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2048 wpid = waitpid(fpid, &status, 0);
2049 ATF_REQUIRE(wpid == fpid);
2050 ATF_REQUIRE(WIFSIGNALED(status));
2051 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2053 wpid = wait(&status);
2054 ATF_REQUIRE(wpid == -1);
2055 ATF_REQUIRE(errno == ECHILD);
2059 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2060 * and prevents spurious stops caused by those events.
2062 ATF_TC(ptrace__PT_KILL_competing_stop);
2063 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2066 atf_tc_set_md_var(tc, "require.user", "root");
2068 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2074 pthread_barrier_t barrier;
2076 struct ptrace_lwpinfo pl;
2077 struct sched_param sched_param;
2079 ATF_REQUIRE((fpid = fork()) != -1);
2083 /* Bind to one CPU so only one thread at a time will run. */
2085 CPU_SET(0, &setmask);
2087 CHILD_REQUIRE(cpuset(&setid) == 0);
2088 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2089 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2091 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2093 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2094 (void*)&barrier) == 0);
2097 * Give the main thread higher priority. The test always
2098 * assumes that, if both threads are able to run, the main
2099 * thread runs first.
2101 sched_param.sched_priority =
2102 (sched_get_priority_max(SCHED_FIFO) +
2103 sched_get_priority_min(SCHED_FIFO)) / 2;
2104 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2105 SCHED_FIFO, &sched_param) == 0);
2106 sched_param.sched_priority -= RQ_PPQ;
2107 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2108 &sched_param) == 0);
2111 sigemptyset(&sigmask);
2112 sigaddset(&sigmask, SIGUSR2);
2113 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2115 /* Sync up with other thread after sigmask updated. */
2116 pthread_barrier_wait(&barrier);
2118 /* Sync up with the test before doing the getpid(). */
2125 /* The first wait() should report the stop from SIGSTOP. */
2126 wpid = waitpid(fpid, &status, 0);
2127 ATF_REQUIRE(wpid == fpid);
2128 ATF_REQUIRE(WIFSTOPPED(status));
2129 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2131 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2132 main_lwp = pl.pl_lwpid;
2134 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2135 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2138 * Continue until child is done with setup, which is indicated with
2139 * SIGSTOP. Ignore system calls in the meantime.
2142 wpid = waitpid(fpid, &status, 0);
2143 ATF_REQUIRE(wpid == fpid);
2144 ATF_REQUIRE(WIFSTOPPED(status));
2145 if (WSTOPSIG(status) == SIGTRAP) {
2146 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2148 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2150 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2153 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2156 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2157 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2159 wpid = waitpid(fpid, &status, 0);
2160 ATF_REQUIRE(wpid == fpid);
2161 ATF_REQUIRE(WIFSTOPPED(status));
2162 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2164 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2166 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2167 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2168 /* Prevent the main thread from hitting its syscall exit for now. */
2169 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2172 * Proceed, allowing second thread to hit syscall exit for
2173 * pthread_barrier_wait().
2175 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2177 wpid = waitpid(fpid, &status, 0);
2178 ATF_REQUIRE(wpid == fpid);
2179 ATF_REQUIRE(WIFSTOPPED(status));
2180 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2182 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2184 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2185 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2187 /* Send a signal that only the second thread can handle. */
2188 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2190 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2192 /* The next wait() should report the SIGUSR2. */
2193 wpid = waitpid(fpid, &status, 0);
2194 ATF_REQUIRE(wpid == fpid);
2195 ATF_REQUIRE(WIFSTOPPED(status));
2196 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2198 /* Allow the main thread to try to finish its system call. */
2199 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2202 * At this point, the main thread is in the middle of a system call and
2203 * has been resumed. The second thread has taken a SIGUSR2 which will
2204 * be replaced with a SIGKILL below. The main thread will get to run
2205 * first. It should notice the kill request (even though the signal
2206 * replacement occurred in the other thread) and exit accordingly. It
2207 * should not stop for the system call exit event.
2210 /* Replace the SIGUSR2 with a kill. */
2211 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2213 /* The last wait() should report the SIGKILL (not a syscall exit). */
2214 wpid = waitpid(fpid, &status, 0);
2215 ATF_REQUIRE(wpid == fpid);
2216 ATF_REQUIRE(WIFSIGNALED(status));
2217 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2219 wpid = wait(&status);
2220 ATF_REQUIRE(wpid == -1);
2221 ATF_REQUIRE(errno == ECHILD);
2225 sigusr1_handler(int sig)
2228 CHILD_REQUIRE(sig == SIGUSR1);
2233 * Verify that even if the signal queue is full for a child process,
2234 * a PT_KILL will kill the process.
2236 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2237 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2241 int max_pending_per_proc;
2245 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2247 ATF_REQUIRE((fpid = fork()) != -1);
2253 /* The first wait() should report the stop from SIGSTOP. */
2254 wpid = waitpid(fpid, &status, 0);
2255 ATF_REQUIRE(wpid == fpid);
2256 ATF_REQUIRE(WIFSTOPPED(status));
2257 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2259 len = sizeof(max_pending_per_proc);
2260 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2261 &max_pending_per_proc, &len, NULL, 0) == 0);
2263 /* Fill the signal queue. */
2264 for (i = 0; i < max_pending_per_proc; ++i)
2265 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2267 /* Kill the child process. */
2268 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2270 /* The last wait() should report the SIGKILL. */
2271 wpid = waitpid(fpid, &status, 0);
2272 ATF_REQUIRE(wpid == fpid);
2273 ATF_REQUIRE(WIFSIGNALED(status));
2274 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2276 wpid = wait(&status);
2277 ATF_REQUIRE(wpid == -1);
2278 ATF_REQUIRE(errno == ECHILD);
2282 * Verify that when stopped at a system call entry, a signal can be
2283 * requested with PT_CONTINUE which will be delivered once the system
2286 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2287 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2289 struct ptrace_lwpinfo pl;
2293 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2295 ATF_REQUIRE((fpid = fork()) != -1);
2302 /* The first wait() should report the stop from SIGSTOP. */
2303 wpid = waitpid(fpid, &status, 0);
2304 ATF_REQUIRE(wpid == fpid);
2305 ATF_REQUIRE(WIFSTOPPED(status));
2306 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2308 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2309 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2311 /* The second wait() should report a system call entry for getpid(). */
2312 wpid = waitpid(fpid, &status, 0);
2313 ATF_REQUIRE(wpid == fpid);
2314 ATF_REQUIRE(WIFSTOPPED(status));
2315 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2317 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2318 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2320 /* Continue the child process with a signal. */
2321 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2325 * The last wait() should report exit 2, i.e., a normal _exit
2326 * from the signal handler. In the meantime, catch and proceed
2327 * past any syscall stops.
2329 wpid = waitpid(fpid, &status, 0);
2330 ATF_REQUIRE(wpid == fpid);
2331 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2332 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2333 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2334 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2336 ATF_REQUIRE(WIFEXITED(status));
2337 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2342 wpid = wait(&status);
2343 ATF_REQUIRE(wpid == -1);
2344 ATF_REQUIRE(errno == ECHILD);
2348 sigusr1_counting_handler(int sig)
2350 static int counter = 0;
2352 CHILD_REQUIRE(sig == SIGUSR1);
2359 * Verify that, when continuing from a stop at system call entry and exit,
2360 * a signal can be requested from both stops, and both will be delivered when
2361 * the system call is complete.
2363 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2364 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2366 struct ptrace_lwpinfo pl;
2370 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2372 ATF_REQUIRE((fpid = fork()) != -1);
2379 /* The first wait() should report the stop from SIGSTOP. */
2380 wpid = waitpid(fpid, &status, 0);
2381 ATF_REQUIRE(wpid == fpid);
2382 ATF_REQUIRE(WIFSTOPPED(status));
2383 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2385 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2386 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2388 /* The second wait() should report a system call entry for getpid(). */
2389 wpid = waitpid(fpid, &status, 0);
2390 ATF_REQUIRE(wpid == fpid);
2391 ATF_REQUIRE(WIFSTOPPED(status));
2392 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2394 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2395 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2397 /* Continue the child process with a signal. */
2398 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2400 /* The third wait() should report a system call exit for getpid(). */
2401 wpid = waitpid(fpid, &status, 0);
2402 ATF_REQUIRE(wpid == fpid);
2403 ATF_REQUIRE(WIFSTOPPED(status));
2404 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2406 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2407 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2409 /* Continue the child process with a signal. */
2410 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2414 * The last wait() should report exit 2, i.e., a normal _exit
2415 * from the signal handler. In the meantime, catch and proceed
2416 * past any syscall stops.
2418 wpid = waitpid(fpid, &status, 0);
2419 ATF_REQUIRE(wpid == fpid);
2420 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2421 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2422 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2423 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2425 ATF_REQUIRE(WIFEXITED(status));
2426 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2431 wpid = wait(&status);
2432 ATF_REQUIRE(wpid == -1);
2433 ATF_REQUIRE(errno == ECHILD);
2437 * Verify that even if the signal queue is full for a child process,
2438 * a PT_CONTINUE with a signal will not result in loss of that signal.
2440 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2441 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2445 int max_pending_per_proc;
2449 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2450 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2452 ATF_REQUIRE((fpid = fork()) != -1);
2458 /* The first wait() should report the stop from SIGSTOP. */
2459 wpid = waitpid(fpid, &status, 0);
2460 ATF_REQUIRE(wpid == fpid);
2461 ATF_REQUIRE(WIFSTOPPED(status));
2462 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2464 len = sizeof(max_pending_per_proc);
2465 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2466 &max_pending_per_proc, &len, NULL, 0) == 0);
2468 /* Fill the signal queue. */
2469 for (i = 0; i < max_pending_per_proc; ++i)
2470 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2472 /* Continue with signal. */
2473 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2476 wpid = waitpid(fpid, &status, 0);
2477 ATF_REQUIRE(wpid == fpid);
2478 if (WIFSTOPPED(status)) {
2479 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2480 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2483 * The last wait() should report normal _exit from the
2486 ATF_REQUIRE(WIFEXITED(status));
2487 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2492 wpid = wait(&status);
2493 ATF_REQUIRE(wpid == -1);
2494 ATF_REQUIRE(errno == ECHILD);
2497 static sem_t sigusr1_sem;
2498 static int got_usr1;
2501 sigusr1_sempost_handler(int sig __unused)
2505 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2509 * Verify that even if the signal queue is full for a child process,
2510 * and the signal is masked, a PT_CONTINUE with a signal will not
2511 * result in loss of that signal.
2513 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2514 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2516 struct ptrace_lwpinfo pl;
2519 int max_pending_per_proc;
2524 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2525 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2526 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2529 ATF_REQUIRE((fpid = fork()) != -1);
2531 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2532 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2533 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2536 CHILD_REQUIRE(got_usr1 == 0);
2538 /* Allow the pending SIGUSR1 in now. */
2539 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2540 /* Wait to receive the SIGUSR1. */
2542 err = sem_wait(&sigusr1_sem);
2543 CHILD_REQUIRE(err == 0 || errno == EINTR);
2544 } while (err != 0 && errno == EINTR);
2545 CHILD_REQUIRE(got_usr1 == 1);
2549 /* The first wait() should report the stop from SIGSTOP. */
2550 wpid = waitpid(fpid, &status, 0);
2551 ATF_REQUIRE(wpid == fpid);
2552 ATF_REQUIRE(WIFSTOPPED(status));
2553 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2555 len = sizeof(max_pending_per_proc);
2556 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2557 &max_pending_per_proc, &len, NULL, 0) == 0);
2559 /* Fill the signal queue. */
2560 for (i = 0; i < max_pending_per_proc; ++i)
2561 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2563 /* Continue with signal. */
2564 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2566 /* Collect and ignore all of the SIGUSR2. */
2567 for (i = 0; i < max_pending_per_proc; ++i) {
2568 wpid = waitpid(fpid, &status, 0);
2569 ATF_REQUIRE(wpid == fpid);
2570 ATF_REQUIRE(WIFSTOPPED(status));
2571 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2572 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2575 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2576 wpid = waitpid(fpid, &status, 0);
2577 ATF_REQUIRE(wpid == fpid);
2578 ATF_REQUIRE(WIFSTOPPED(status));
2579 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2580 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2581 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2583 /* Continue the child, ignoring the SIGUSR1. */
2584 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2586 /* The last wait() should report exit after receiving SIGUSR1. */
2587 wpid = waitpid(fpid, &status, 0);
2588 ATF_REQUIRE(wpid == fpid);
2589 ATF_REQUIRE(WIFEXITED(status));
2590 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2592 wpid = wait(&status);
2593 ATF_REQUIRE(wpid == -1);
2594 ATF_REQUIRE(errno == ECHILD);
2598 * Verify that, after stopping due to a signal, that signal can be
2599 * replaced with another signal.
2601 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2602 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2604 struct ptrace_lwpinfo pl;
2608 ATF_REQUIRE((fpid = fork()) != -1);
2615 /* The first wait() should report the stop from SIGSTOP. */
2616 wpid = waitpid(fpid, &status, 0);
2617 ATF_REQUIRE(wpid == fpid);
2618 ATF_REQUIRE(WIFSTOPPED(status));
2619 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2621 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2623 /* Send a signal without ptrace. */
2624 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2626 /* The second wait() should report a SIGINT was received. */
2627 wpid = waitpid(fpid, &status, 0);
2628 ATF_REQUIRE(wpid == fpid);
2629 ATF_REQUIRE(WIFSTOPPED(status));
2630 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2632 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2633 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2634 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2636 /* Continue the child process with a different signal. */
2637 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2640 * The last wait() should report having died due to the new
2643 wpid = waitpid(fpid, &status, 0);
2644 ATF_REQUIRE(wpid == fpid);
2645 ATF_REQUIRE(WIFSIGNALED(status));
2646 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2648 wpid = wait(&status);
2649 ATF_REQUIRE(wpid == -1);
2650 ATF_REQUIRE(errno == ECHILD);
2654 * Verify that a signal can be passed through to the child even when there
2655 * was no true signal originally. Such cases arise when a SIGTRAP is
2656 * invented for e.g, system call stops.
2658 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2659 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2661 struct ptrace_lwpinfo pl;
2666 ATF_REQUIRE((fpid = fork()) != -1);
2669 /* SIGTRAP expected to cause exit on syscall entry. */
2670 rl.rlim_cur = rl.rlim_max = 0;
2671 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2676 /* The first wait() should report the stop from SIGSTOP. */
2677 wpid = waitpid(fpid, &status, 0);
2678 ATF_REQUIRE(wpid == fpid);
2679 ATF_REQUIRE(WIFSTOPPED(status));
2680 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2682 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2683 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2685 /* The second wait() should report a system call entry for getpid(). */
2686 wpid = waitpid(fpid, &status, 0);
2687 ATF_REQUIRE(wpid == fpid);
2688 ATF_REQUIRE(WIFSTOPPED(status));
2689 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2691 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2692 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2694 /* Continue the child process with a SIGTRAP. */
2695 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2699 * The last wait() should report exit due to SIGTRAP. In the
2700 * meantime, catch and proceed past any syscall stops.
2702 wpid = waitpid(fpid, &status, 0);
2703 ATF_REQUIRE(wpid == fpid);
2704 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2705 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2706 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2707 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2709 ATF_REQUIRE(WIFSIGNALED(status));
2710 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2715 wpid = wait(&status);
2716 ATF_REQUIRE(wpid == -1);
2717 ATF_REQUIRE(errno == ECHILD);
2722 * A mixed bag PT_CONTINUE with signal test.
2724 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2725 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2727 struct ptrace_lwpinfo pl;
2731 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2733 ATF_REQUIRE((fpid = fork()) != -1);
2740 /* The first wait() should report the stop from SIGSTOP. */
2741 wpid = waitpid(fpid, &status, 0);
2742 ATF_REQUIRE(wpid == fpid);
2743 ATF_REQUIRE(WIFSTOPPED(status));
2744 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2746 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2747 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2749 /* The second wait() should report a system call entry for getpid(). */
2750 wpid = waitpid(fpid, &status, 0);
2751 ATF_REQUIRE(wpid == fpid);
2752 ATF_REQUIRE(WIFSTOPPED(status));
2753 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2755 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2756 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2758 /* Continue with the first SIGUSR1. */
2759 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2761 /* The next wait() should report a system call exit for getpid(). */
2762 wpid = waitpid(fpid, &status, 0);
2763 ATF_REQUIRE(wpid == fpid);
2764 ATF_REQUIRE(WIFSTOPPED(status));
2765 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2767 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2768 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2770 /* Send an ABRT without ptrace. */
2771 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2773 /* Continue normally. */
2774 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2776 /* The next wait() should report the SIGABRT. */
2777 wpid = waitpid(fpid, &status, 0);
2778 ATF_REQUIRE(wpid == fpid);
2779 ATF_REQUIRE(WIFSTOPPED(status));
2780 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2782 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2783 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2784 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2786 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2787 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2791 * The last wait() should report exit 2, i.e., a normal _exit
2792 * from the signal handler. In the meantime, catch and proceed
2793 * past any syscall stops.
2795 wpid = waitpid(fpid, &status, 0);
2796 ATF_REQUIRE(wpid == fpid);
2797 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2798 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2799 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2800 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2802 ATF_REQUIRE(WIFEXITED(status));
2803 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2808 wpid = wait(&status);
2809 ATF_REQUIRE(wpid == -1);
2810 ATF_REQUIRE(errno == ECHILD);
2815 * Verify a signal delivered by ptrace is noticed by kevent(2).
2817 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2818 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2821 int status, kq, nevents;
2824 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2826 ATF_REQUIRE((fpid = fork()) != -1);
2828 CHILD_REQUIRE((kq = kqueue()) > 0);
2829 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2830 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2835 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2836 if (nevents == -1 && errno == EINTR)
2838 CHILD_REQUIRE(nevents > 0);
2839 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2840 CHILD_REQUIRE(kev.ident == SIGUSR1);
2847 /* The first wait() should report the stop from SIGSTOP. */
2848 wpid = waitpid(fpid, &status, 0);
2849 ATF_REQUIRE(wpid == fpid);
2850 ATF_REQUIRE(WIFSTOPPED(status));
2851 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2853 /* Continue with the SIGUSR1. */
2854 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2857 * The last wait() should report normal exit with code 1.
2859 wpid = waitpid(fpid, &status, 0);
2860 ATF_REQUIRE(wpid == fpid);
2861 ATF_REQUIRE(WIFEXITED(status));
2862 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2864 wpid = wait(&status);
2865 ATF_REQUIRE(wpid == -1);
2866 ATF_REQUIRE(errno == ECHILD);
2870 signal_thread(void *arg)
2875 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2877 /* Wait for this thread to receive a SIGUSR1. */
2879 err = sem_wait(&sigusr1_sem);
2880 CHILD_REQUIRE(err == 0 || errno == EINTR);
2881 } while (err != 0 && errno == EINTR);
2883 /* Free our companion thread from the barrier. */
2884 pthread_barrier_wait(pbarrier);
2887 * Swap ignore duties; the next SIGUSR1 should go to the
2890 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2891 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2892 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2894 /* Sync up threads after swapping signal masks. */
2895 pthread_barrier_wait(pbarrier);
2897 /* Wait until our companion has received its SIGUSR1. */
2898 pthread_barrier_wait(pbarrier);
2904 * Verify that a traced process with blocked signal received the
2905 * signal from kill() once unmasked.
2907 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2908 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2910 struct ptrace_lwpinfo pl;
2915 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2916 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2919 ATF_REQUIRE((fpid = fork()) != -1);
2921 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2922 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2923 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2926 CHILD_REQUIRE(got_usr1 == 0);
2928 /* Allow the pending SIGUSR1 in now. */
2929 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2930 /* Wait to receive a SIGUSR1. */
2932 err = sem_wait(&sigusr1_sem);
2933 CHILD_REQUIRE(err == 0 || errno == EINTR);
2934 } while (err != 0 && errno == EINTR);
2935 CHILD_REQUIRE(got_usr1 == 1);
2939 /* The first wait() should report the stop from SIGSTOP. */
2940 wpid = waitpid(fpid, &status, 0);
2941 ATF_REQUIRE(wpid == fpid);
2942 ATF_REQUIRE(WIFSTOPPED(status));
2943 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2944 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2945 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2947 /* Send blocked SIGUSR1 which should cause a stop. */
2948 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2950 /* Continue the child ignoring the SIGSTOP. */
2951 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2953 /* The next wait() should report the kill(SIGUSR1) was received. */
2954 wpid = waitpid(fpid, &status, 0);
2955 ATF_REQUIRE(wpid == fpid);
2956 ATF_REQUIRE(WIFSTOPPED(status));
2957 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2958 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2959 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2961 /* Continue the child, allowing in the SIGUSR1. */
2962 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2964 /* The last wait() should report normal exit with code 1. */
2965 wpid = waitpid(fpid, &status, 0);
2966 ATF_REQUIRE(wpid == fpid);
2967 ATF_REQUIRE(WIFEXITED(status));
2968 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2970 wpid = wait(&status);
2971 ATF_REQUIRE(wpid == -1);
2972 ATF_REQUIRE(errno == ECHILD);
2976 * Verify that a traced process with blocked signal received the
2977 * signal from PT_CONTINUE once unmasked.
2979 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2980 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2982 struct ptrace_lwpinfo pl;
2987 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2988 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2991 ATF_REQUIRE((fpid = fork()) != -1);
2993 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2994 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2995 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2998 CHILD_REQUIRE(got_usr1 == 0);
3000 /* Allow the pending SIGUSR1 in now. */
3001 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3002 /* Wait to receive a SIGUSR1. */
3004 err = sem_wait(&sigusr1_sem);
3005 CHILD_REQUIRE(err == 0 || errno == EINTR);
3006 } while (err != 0 && errno == EINTR);
3008 CHILD_REQUIRE(got_usr1 == 1);
3012 /* The first wait() should report the stop from SIGSTOP. */
3013 wpid = waitpid(fpid, &status, 0);
3014 ATF_REQUIRE(wpid == fpid);
3015 ATF_REQUIRE(WIFSTOPPED(status));
3016 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3017 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3018 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3020 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3021 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3023 /* The next wait() should report the SIGUSR1 was received. */
3024 wpid = waitpid(fpid, &status, 0);
3025 ATF_REQUIRE(wpid == fpid);
3026 ATF_REQUIRE(WIFSTOPPED(status));
3027 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3028 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3029 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3031 /* Continue the child, ignoring the SIGUSR1. */
3032 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3034 /* The last wait() should report normal exit with code 1. */
3035 wpid = waitpid(fpid, &status, 0);
3036 ATF_REQUIRE(wpid == fpid);
3037 ATF_REQUIRE(WIFEXITED(status));
3038 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3040 wpid = wait(&status);
3041 ATF_REQUIRE(wpid == -1);
3042 ATF_REQUIRE(errno == ECHILD);
3046 * Verify that if ptrace stops due to a signal but continues with
3047 * a different signal that the new signal is routed to a thread
3048 * that can accept it, and that the thread is awakened by the signal
3049 * in a timely manner.
3051 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3052 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3058 pthread_barrier_t barrier;
3060 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3061 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3062 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3064 ATF_REQUIRE((fpid = fork()) != -1);
3066 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3068 /* The other thread should receive the first SIGUSR1. */
3069 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3070 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3071 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3075 /* Wait until other thread has received its SIGUSR1. */
3076 pthread_barrier_wait(&barrier);
3079 * Swap ignore duties; the next SIGUSR1 should go to this
3082 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3084 /* Sync up threads after swapping signal masks. */
3085 pthread_barrier_wait(&barrier);
3088 * Sync up with test code; we're ready for the next SIGUSR1
3093 /* Wait for this thread to receive a SIGUSR1. */
3095 err = sem_wait(&sigusr1_sem);
3096 CHILD_REQUIRE(err == 0 || errno == EINTR);
3097 } while (err != 0 && errno == EINTR);
3099 /* Free the other thread from the barrier. */
3100 pthread_barrier_wait(&barrier);
3102 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3107 /* The first wait() should report the stop from SIGSTOP. */
3108 wpid = waitpid(fpid, &status, 0);
3109 ATF_REQUIRE(wpid == fpid);
3110 ATF_REQUIRE(WIFSTOPPED(status));
3111 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3113 /* Continue the child ignoring the SIGSTOP. */
3114 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3117 * Send a signal without ptrace that either thread will accept (USR2,
3120 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3122 /* The second wait() should report a SIGUSR2 was received. */
3123 wpid = waitpid(fpid, &status, 0);
3124 ATF_REQUIRE(wpid == fpid);
3125 ATF_REQUIRE(WIFSTOPPED(status));
3126 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3128 /* Continue the child, changing the signal to USR1. */
3129 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3131 /* The next wait() should report the stop from SIGSTOP. */
3132 wpid = waitpid(fpid, &status, 0);
3133 ATF_REQUIRE(wpid == fpid);
3134 ATF_REQUIRE(WIFSTOPPED(status));
3135 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3137 /* Continue the child ignoring the SIGSTOP. */
3138 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3140 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3142 /* The next wait() should report a SIGUSR2 was received. */
3143 wpid = waitpid(fpid, &status, 0);
3144 ATF_REQUIRE(wpid == fpid);
3145 ATF_REQUIRE(WIFSTOPPED(status));
3146 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3148 /* Continue the child, changing the signal to USR1. */
3149 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3151 /* The last wait() should report normal exit with code 1. */
3152 wpid = waitpid(fpid, &status, 0);
3153 ATF_REQUIRE(wpid == fpid);
3154 ATF_REQUIRE(WIFEXITED(status));
3155 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3157 wpid = wait(&status);
3158 ATF_REQUIRE(wpid == -1);
3159 ATF_REQUIRE(errno == ECHILD);
3163 raise_sigstop_thread(void *arg __unused)
3171 sleep_thread(void *arg __unused)
3179 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3188 * Become the reaper for this process tree. We need to be able to check
3189 * that both child and grandchild have died.
3191 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3194 ATF_REQUIRE(fpid >= 0);
3197 CHILD_REQUIRE(fpid >= 0);
3201 /* Pin to CPU 0 to serialize thread execution. */
3203 CPU_SET(0, &setmask);
3204 CHILD_REQUIRE(cpuset(&setid) == 0);
3205 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3206 CPU_WHICH_CPUSET, setid,
3207 sizeof(setmask), &setmask) == 0);
3209 if (sigstop_from_main_thread) {
3211 * We expect the SIGKILL sent when our parent
3212 * dies to be delivered to the new thread.
3213 * Raise the SIGSTOP in this thread so the
3216 CHILD_REQUIRE(pthread_create(&t, NULL,
3217 sleep_thread, NULL) == 0);
3221 * We expect the SIGKILL to be delivered to
3222 * this thread. After creating the new thread,
3223 * just get off the CPU so the other thread can
3224 * raise the SIGSTOP.
3226 CHILD_REQUIRE(pthread_create(&t, NULL,
3227 raise_sigstop_thread, NULL) == 0);
3233 /* First stop is trace_me() immediately after fork. */
3234 wpid = waitpid(fpid, &status, 0);
3235 CHILD_REQUIRE(wpid == fpid);
3236 CHILD_REQUIRE(WIFSTOPPED(status));
3237 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3239 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3241 /* Second stop is from the raise(SIGSTOP). */
3242 wpid = waitpid(fpid, &status, 0);
3243 CHILD_REQUIRE(wpid == fpid);
3244 CHILD_REQUIRE(WIFSTOPPED(status));
3245 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3248 * Terminate tracing process without detaching. Our child
3255 * We should get a normal exit from our immediate child and a SIGKILL
3256 * exit from our grandchild. The latter case is the interesting one.
3257 * Our grandchild should not have stopped due to the SIGSTOP that was
3258 * left dangling when its parent died.
3260 for (i = 0; i < 2; ++i) {
3261 wpid = wait(&status);
3263 ATF_REQUIRE(WIFEXITED(status) &&
3264 WEXITSTATUS(status) == 0);
3266 ATF_REQUIRE(WIFSIGNALED(status) &&
3267 WTERMSIG(status) == SIGKILL);
3272 * These two tests ensure that if the tracing process exits without detaching
3273 * just after the child received a SIGSTOP, the child is cleanly killed and
3274 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3275 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3276 * different threads, the SIGKILL must win. There are two variants of this
3277 * test, designed to catch the case where the SIGKILL is delivered to the
3278 * younger thread (the first test) and the case where the SIGKILL is delivered
3279 * to the older thread (the second test). This behavior has changed in the
3280 * past, so make no assumption.
3282 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3283 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3286 atf_tc_set_md_var(tc, "require.user", "root");
3288 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3291 terminate_with_pending_sigstop(true);
3294 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3295 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3298 atf_tc_set_md_var(tc, "require.user", "root");
3300 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3303 terminate_with_pending_sigstop(false);
3307 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3310 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3311 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3313 struct ptrace_lwpinfo pl;
3315 int status, event_mask, new_event_mask;
3317 ATF_REQUIRE((fpid = fork()) != -1);
3324 /* The first wait() should report the stop from trace_me(). */
3325 wpid = waitpid(fpid, &status, 0);
3326 ATF_REQUIRE(wpid == fpid);
3327 ATF_REQUIRE(WIFSTOPPED(status));
3328 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3330 /* Set several unobtrusive event bits. */
3331 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3332 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3333 sizeof(event_mask)) == 0);
3335 /* Send a SIGKILL without using ptrace. */
3336 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3338 /* Continue the child ignoring the SIGSTOP. */
3339 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3341 /* The next stop should be due to the SIGKILL. */
3342 wpid = waitpid(fpid, &status, 0);
3343 ATF_REQUIRE(wpid == fpid);
3344 ATF_REQUIRE(WIFSTOPPED(status));
3345 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3347 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3348 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3349 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3351 /* Continue the child ignoring the SIGKILL. */
3352 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3354 /* The next wait() should report the stop from SIGSTOP. */
3355 wpid = waitpid(fpid, &status, 0);
3356 ATF_REQUIRE(wpid == fpid);
3357 ATF_REQUIRE(WIFSTOPPED(status));
3358 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3360 /* Check the current event mask. It should not have changed. */
3362 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3363 sizeof(new_event_mask)) == 0);
3364 ATF_REQUIRE(event_mask == new_event_mask);
3366 /* Continue the child to let it exit. */
3367 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3369 /* The last event should be for the child process's exit. */
3370 wpid = waitpid(fpid, &status, 0);
3371 ATF_REQUIRE(WIFEXITED(status));
3372 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3374 wpid = wait(&status);
3375 ATF_REQUIRE(wpid == -1);
3376 ATF_REQUIRE(errno == ECHILD);
3380 flock_thread(void *arg)
3385 (void)flock(fd, LOCK_EX);
3386 (void)flock(fd, LOCK_UN);
3391 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3392 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3393 * on a lock. This is a regression test for r318191.
3395 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3396 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3398 pthread_barrier_t barrier;
3399 pthread_barrierattr_t battr;
3402 int error, fd, i, status;
3404 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3405 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3406 PTHREAD_PROCESS_SHARED) == 0);
3407 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3409 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3410 fd = mkstemp(tmpfile);
3411 ATF_REQUIRE(fd >= 0);
3413 ATF_REQUIRE((child = fork()) != -1);
3418 error = pthread_barrier_wait(&barrier);
3419 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3422 cfd = open(tmpfile, O_RDONLY);
3427 * We want at least two threads blocked on the file lock since
3428 * the SIGSTOP from PT_ATTACH may kick one of them out of
3431 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3433 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3435 if (pthread_join(t[0], NULL) != 0)
3437 if (pthread_join(t[1], NULL) != 0)
3442 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3444 error = pthread_barrier_wait(&barrier);
3445 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3448 * Give the child some time to block. Is there a better way to do this?
3453 * Attach and give the child 3 seconds to stop.
3455 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3456 for (i = 0; i < 3; i++) {
3457 wpid = waitpid(child, &status, WNOHANG);
3458 if (wpid == child && WIFSTOPPED(status) &&
3459 WSTOPSIG(status) == SIGSTOP)
3463 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3465 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3467 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3468 ATF_REQUIRE(unlink(tmpfile) == 0);
3469 ATF_REQUIRE(close(fd) == 0);
3473 sigusr1_step_handler(int sig)
3476 CHILD_REQUIRE(sig == SIGUSR1);
3481 * Verify that PT_STEP with a signal invokes the signal before
3482 * stepping the next instruction (and that the next instruction is
3483 * stepped correctly).
3485 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3486 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3488 struct ptrace_lwpinfo pl;
3492 ATF_REQUIRE((fpid = fork()) != -1);
3495 signal(SIGUSR1, sigusr1_step_handler);
3500 /* The first wait() should report the stop from SIGSTOP. */
3501 wpid = waitpid(fpid, &status, 0);
3502 ATF_REQUIRE(wpid == fpid);
3503 ATF_REQUIRE(WIFSTOPPED(status));
3504 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3506 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3508 /* The next stop should report the SIGABRT in the child body. */
3509 wpid = waitpid(fpid, &status, 0);
3510 ATF_REQUIRE(wpid == fpid);
3511 ATF_REQUIRE(WIFSTOPPED(status));
3512 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3514 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3515 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3516 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3518 /* Step the child process inserting SIGUSR1. */
3519 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3521 /* The next stop should report the SIGABRT in the signal handler. */
3522 wpid = waitpid(fpid, &status, 0);
3523 ATF_REQUIRE(wpid == fpid);
3524 ATF_REQUIRE(WIFSTOPPED(status));
3525 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3527 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3528 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3529 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3531 /* Continue the child process discarding the signal. */
3532 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3534 /* The next stop should report a trace trap from PT_STEP. */
3535 wpid = waitpid(fpid, &status, 0);
3536 ATF_REQUIRE(wpid == fpid);
3537 ATF_REQUIRE(WIFSTOPPED(status));
3538 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3540 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3541 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3542 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3543 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3545 /* Continue the child to let it exit. */
3546 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3548 /* The last event should be for the child process's exit. */
3549 wpid = waitpid(fpid, &status, 0);
3550 ATF_REQUIRE(WIFEXITED(status));
3551 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3553 wpid = wait(&status);
3554 ATF_REQUIRE(wpid == -1);
3555 ATF_REQUIRE(errno == ECHILD);
3558 #ifdef HAVE_BREAKPOINT
3560 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3561 * for a breakpoint trap.
3563 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3564 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3566 struct ptrace_lwpinfo pl;
3570 ATF_REQUIRE((fpid = fork()) != -1);
3577 /* The first wait() should report the stop from SIGSTOP. */
3578 wpid = waitpid(fpid, &status, 0);
3579 ATF_REQUIRE(wpid == fpid);
3580 ATF_REQUIRE(WIFSTOPPED(status));
3581 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3583 /* Continue the child ignoring the SIGSTOP. */
3584 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3586 /* The second wait() should report hitting the breakpoint. */
3587 wpid = waitpid(fpid, &status, 0);
3588 ATF_REQUIRE(wpid == fpid);
3589 ATF_REQUIRE(WIFSTOPPED(status));
3590 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3592 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3593 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3594 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3595 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3597 /* Kill the child process. */
3598 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3600 /* The last wait() should report the SIGKILL. */
3601 wpid = waitpid(fpid, &status, 0);
3602 ATF_REQUIRE(wpid == fpid);
3603 ATF_REQUIRE(WIFSIGNALED(status));
3604 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3606 wpid = wait(&status);
3607 ATF_REQUIRE(wpid == -1);
3608 ATF_REQUIRE(errno == ECHILD);
3610 #endif /* HAVE_BREAKPOINT */
3613 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3614 * for a single-step trap from PT_STEP.
3616 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3617 ATF_TC_BODY(ptrace__step_siginfo, tc)
3619 struct ptrace_lwpinfo pl;
3623 ATF_REQUIRE((fpid = fork()) != -1);
3629 /* The first wait() should report the stop from SIGSTOP. */
3630 wpid = waitpid(fpid, &status, 0);
3631 ATF_REQUIRE(wpid == fpid);
3632 ATF_REQUIRE(WIFSTOPPED(status));
3633 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3635 /* Step the child ignoring the SIGSTOP. */
3636 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3638 /* The second wait() should report a single-step trap. */
3639 wpid = waitpid(fpid, &status, 0);
3640 ATF_REQUIRE(wpid == fpid);
3641 ATF_REQUIRE(WIFSTOPPED(status));
3642 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3644 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3645 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3646 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3647 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3649 /* Continue the child process. */
3650 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3652 /* The last event should be for the child process's exit. */
3653 wpid = waitpid(fpid, &status, 0);
3654 ATF_REQUIRE(WIFEXITED(status));
3655 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3657 wpid = wait(&status);
3658 ATF_REQUIRE(wpid == -1);
3659 ATF_REQUIRE(errno == ECHILD);
3662 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3664 continue_thread(void *arg __unused)
3671 continue_thread_main(void)
3673 pthread_t threads[2];
3675 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3677 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3679 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3680 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3685 * Ensure that PT_CONTINUE clears the status of the thread that
3686 * triggered the stop even if a different thread's LWP was passed to
3689 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3690 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3692 struct ptrace_lwpinfo pl;
3699 ATF_REQUIRE((fpid = fork()) != -1);
3702 continue_thread_main();
3705 /* The first wait() should report the stop from SIGSTOP. */
3706 wpid = waitpid(fpid, &status, 0);
3707 ATF_REQUIRE(wpid == fpid);
3708 ATF_REQUIRE(WIFSTOPPED(status));
3709 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3711 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3714 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3716 /* Continue the child ignoring the SIGSTOP. */
3717 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3719 /* One of the new threads should report it's birth. */
3720 wpid = waitpid(fpid, &status, 0);
3721 ATF_REQUIRE(wpid == fpid);
3722 ATF_REQUIRE(WIFSTOPPED(status));
3723 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3725 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3726 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3727 (PL_FLAG_BORN | PL_FLAG_SCX));
3728 lwps[0] = pl.pl_lwpid;
3731 * Suspend this thread to ensure both threads are alive before
3732 * hitting the breakpoint.
3734 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3736 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3738 /* Second thread should report it's birth. */
3739 wpid = waitpid(fpid, &status, 0);
3740 ATF_REQUIRE(wpid == fpid);
3741 ATF_REQUIRE(WIFSTOPPED(status));
3742 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3744 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3745 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3746 (PL_FLAG_BORN | PL_FLAG_SCX));
3747 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3748 lwps[1] = pl.pl_lwpid;
3750 /* Resume both threads waiting for breakpoint events. */
3751 hit_break[0] = hit_break[1] = false;
3752 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3753 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3755 /* One thread should report a breakpoint. */
3756 wpid = waitpid(fpid, &status, 0);
3757 ATF_REQUIRE(wpid == fpid);
3758 ATF_REQUIRE(WIFSTOPPED(status));
3759 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3761 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3762 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3763 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3764 pl.pl_siginfo.si_code == TRAP_BRKPT);
3765 if (pl.pl_lwpid == lwps[0])
3769 hit_break[i] = true;
3770 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3772 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3775 * Resume both threads but pass the other thread's LWPID to
3778 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3781 * Will now get two thread exit events and one more breakpoint
3784 for (j = 0; j < 3; j++) {
3785 wpid = waitpid(fpid, &status, 0);
3786 ATF_REQUIRE(wpid == fpid);
3787 ATF_REQUIRE(WIFSTOPPED(status));
3788 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3790 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3793 if (pl.pl_lwpid == lwps[0])
3798 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3799 if (pl.pl_flags & PL_FLAG_EXITED) {
3800 ATF_REQUIRE_MSG(hit_break[i],
3801 "exited thread did not report breakpoint");
3804 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3805 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3806 pl.pl_siginfo.si_code == TRAP_BRKPT);
3807 ATF_REQUIRE_MSG(!hit_break[i],
3808 "double breakpoint event");
3809 hit_break[i] = true;
3810 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3813 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3817 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3820 /* Both threads should have exited. */
3821 ATF_REQUIRE(lwps[0] == 0);
3822 ATF_REQUIRE(lwps[1] == 0);
3824 /* The last event should be for the child process's exit. */
3825 wpid = waitpid(fpid, &status, 0);
3826 ATF_REQUIRE(WIFEXITED(status));
3827 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3829 wpid = wait(&status);
3830 ATF_REQUIRE(wpid == -1);
3831 ATF_REQUIRE(errno == ECHILD);
3836 * Verify that PT_LWPINFO doesn't return stale siginfo.
3838 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3839 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3841 struct ptrace_lwpinfo pl;
3845 ATF_REQUIRE((fpid = fork()) != -1);
3852 /* The first wait() should report the stop from SIGSTOP. */
3853 wpid = waitpid(fpid, &status, 0);
3854 ATF_REQUIRE(wpid == fpid);
3855 ATF_REQUIRE(WIFSTOPPED(status));
3856 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3858 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3860 /* The next stop should report the SIGABRT in the child body. */
3861 wpid = waitpid(fpid, &status, 0);
3862 ATF_REQUIRE(wpid == fpid);
3863 ATF_REQUIRE(WIFSTOPPED(status));
3864 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3866 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3867 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3868 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3871 * Continue the process ignoring the signal, but enabling
3874 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3877 * The next stop should report a system call entry from
3878 * exit(). PL_FLAGS_SI should not be set.
3880 wpid = waitpid(fpid, &status, 0);
3881 ATF_REQUIRE(wpid == fpid);
3882 ATF_REQUIRE(WIFSTOPPED(status));
3883 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3885 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3886 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3887 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3889 /* Disable syscall tracing and continue the child to let it exit. */
3890 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3891 sizeof(events)) == 0);
3892 events &= ~PTRACE_SYSCALL;
3893 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3894 sizeof(events)) == 0);
3895 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3897 /* The last event should be for the child process's exit. */
3898 wpid = waitpid(fpid, &status, 0);
3899 ATF_REQUIRE(WIFEXITED(status));
3900 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3902 wpid = wait(&status);
3903 ATF_REQUIRE(wpid == -1);
3904 ATF_REQUIRE(errno == ECHILD);
3910 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3911 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3912 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3913 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3914 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
3915 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3916 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3917 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3918 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3920 ptrace__follow_fork_child_detached_unrelated_debugger);
3922 ptrace__follow_fork_parent_detached_unrelated_debugger);
3923 ATF_TP_ADD_TC(tp, ptrace__getppid);
3924 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3925 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3926 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3927 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3928 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3929 ATF_TP_ADD_TC(tp, ptrace__siginfo);
3930 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3931 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3932 ATF_TP_ADD_TC(tp, ptrace__event_mask);
3933 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3934 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3935 #ifdef HAVE_BREAKPOINT
3936 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3938 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3939 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3940 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3941 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3942 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3943 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3945 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3946 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3947 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3948 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3949 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3950 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3951 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3952 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3953 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3954 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3955 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3956 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3957 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3958 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3959 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3960 #ifdef HAVE_BREAKPOINT
3961 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
3963 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
3964 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3965 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3967 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
3969 return (atf_no_error());