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_tc_skip("https://bugs.freebsd.org/239397");
804 ATF_REQUIRE(pipe(cpipe) == 0);
805 ATF_REQUIRE((fpid = fork()) != -1);
807 attach_fork_parent(cpipe);
808 follow_fork_parent(false);
811 /* Parent process. */
814 /* Wait for the direct child to exit. */
815 wpid = waitpid(fpid, &status, 0);
816 ATF_REQUIRE(wpid == fpid);
817 ATF_REQUIRE(WIFEXITED(status));
818 ATF_REQUIRE(WEXITSTATUS(status) == 3);
820 /* Read the pid of the fork parent. */
821 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
822 sizeof(children[0]));
824 /* Attach to the fork parent. */
825 attach_child(children[0]);
827 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
829 /* Continue the fork parent ignoring the SIGSTOP. */
830 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
832 /* Signal the fork parent to continue. */
835 children[1] = handle_fork_events(children[0], NULL);
836 ATF_REQUIRE(children[1] > 0);
838 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
839 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
842 * The fork parent can't exit until the child reports status,
843 * so the child should report its exit first to the debugger.
845 wpid = wait(&status);
846 ATF_REQUIRE(wpid == children[1]);
847 ATF_REQUIRE(WIFEXITED(status));
848 ATF_REQUIRE(WEXITSTATUS(status) == 2);
850 wpid = wait(&status);
851 ATF_REQUIRE(wpid == children[0]);
852 ATF_REQUIRE(WIFEXITED(status));
853 ATF_REQUIRE(WEXITSTATUS(status) == 1);
855 wpid = wait(&status);
856 ATF_REQUIRE(wpid == -1);
857 ATF_REQUIRE(errno == ECHILD);
861 * Verify that a new child process is stopped after a followed fork
862 * and that the traced parent sees the exit of the child when the new
863 * child process is detached after it reports its fork. In this test
864 * the parent that forks is not a direct child of the debugger.
866 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
867 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
869 pid_t children[2], fpid, wpid;
870 int cpipe[2], status;
872 atf_tc_skip("https://bugs.freebsd.org/239292");
874 ATF_REQUIRE(pipe(cpipe) == 0);
875 ATF_REQUIRE((fpid = fork()) != -1);
877 attach_fork_parent(cpipe);
878 follow_fork_parent(false);
881 /* Parent process. */
884 /* Wait for the direct child to exit. */
885 wpid = waitpid(fpid, &status, 0);
886 ATF_REQUIRE(wpid == fpid);
887 ATF_REQUIRE(WIFEXITED(status));
888 ATF_REQUIRE(WEXITSTATUS(status) == 3);
890 /* Read the pid of the fork parent. */
891 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
892 sizeof(children[0]));
894 /* Attach to the fork parent. */
895 attach_child(children[0]);
897 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
899 /* Continue the fork parent ignoring the SIGSTOP. */
900 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
902 /* Signal the fork parent to continue. */
905 children[1] = handle_fork_events(children[0], NULL);
906 ATF_REQUIRE(children[1] > 0);
908 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
909 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
912 * Should not see any status from the child now, only the fork
915 wpid = wait(&status);
916 ATF_REQUIRE(wpid == children[0]);
917 ATF_REQUIRE(WIFEXITED(status));
918 ATF_REQUIRE(WEXITSTATUS(status) == 1);
920 wpid = wait(&status);
921 ATF_REQUIRE(wpid == -1);
922 ATF_REQUIRE(errno == ECHILD);
926 * Verify that a new child process is stopped after a followed fork
927 * and that the traced parent sees the exit of the child when the
928 * traced parent is detached after the fork. In this test the parent
929 * that forks is not a direct child of the debugger.
931 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
932 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
934 pid_t children[2], fpid, wpid;
935 int cpipe[2], status;
937 ATF_REQUIRE(pipe(cpipe) == 0);
938 ATF_REQUIRE((fpid = fork()) != -1);
940 attach_fork_parent(cpipe);
941 follow_fork_parent(false);
944 /* Parent process. */
947 /* Wait for the direct child to exit. */
948 wpid = waitpid(fpid, &status, 0);
949 ATF_REQUIRE(wpid == fpid);
950 ATF_REQUIRE(WIFEXITED(status));
951 ATF_REQUIRE(WEXITSTATUS(status) == 3);
953 /* Read the pid of the fork parent. */
954 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
955 sizeof(children[0]));
957 /* Attach to the fork parent. */
958 attach_child(children[0]);
960 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
962 /* Continue the fork parent ignoring the SIGSTOP. */
963 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
965 /* Signal the fork parent to continue. */
968 children[1] = handle_fork_events(children[0], NULL);
969 ATF_REQUIRE(children[1] > 0);
971 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
972 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
975 * Should not see any status from the fork parent now, only
978 wpid = wait(&status);
979 ATF_REQUIRE(wpid == children[1]);
980 ATF_REQUIRE(WIFEXITED(status));
981 ATF_REQUIRE(WEXITSTATUS(status) == 2);
983 wpid = wait(&status);
984 ATF_REQUIRE(wpid == -1);
985 ATF_REQUIRE(errno == ECHILD);
989 * Verify that a child process does not see an unrelated debugger as its
990 * parent but sees its original parent process.
992 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
993 ATF_TC_BODY(ptrace__getppid, tc)
995 pid_t child, debugger, ppid, wpid;
996 int cpipe[2], dpipe[2], status;
999 ATF_REQUIRE(pipe(cpipe) == 0);
1000 ATF_REQUIRE((child = fork()) != -1);
1003 /* Child process. */
1006 /* Wait for parent to be ready. */
1007 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1009 /* Report the parent PID to the parent. */
1011 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1018 ATF_REQUIRE(pipe(dpipe) == 0);
1019 ATF_REQUIRE((debugger = fork()) != -1);
1021 if (debugger == 0) {
1022 /* Debugger process. */
1025 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1027 wpid = waitpid(child, &status, 0);
1028 CHILD_REQUIRE(wpid == child);
1029 CHILD_REQUIRE(WIFSTOPPED(status));
1030 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1032 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1034 /* Signal parent that debugger is attached. */
1035 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1037 /* Wait for traced child to exit. */
1038 wpid = waitpid(child, &status, 0);
1039 CHILD_REQUIRE(wpid == child);
1040 CHILD_REQUIRE(WIFEXITED(status));
1041 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1047 /* Parent process. */
1049 /* Wait for the debugger to attach to the child. */
1050 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1052 /* Release the child. */
1053 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1055 /* Read the parent PID from the child. */
1056 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1059 ATF_REQUIRE(ppid == getpid());
1061 /* Wait for the debugger. */
1062 wpid = waitpid(debugger, &status, 0);
1063 ATF_REQUIRE(wpid == debugger);
1064 ATF_REQUIRE(WIFEXITED(status));
1065 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1067 /* The child process should now be ready. */
1068 wpid = waitpid(child, &status, WNOHANG);
1069 ATF_REQUIRE(wpid == child);
1070 ATF_REQUIRE(WIFEXITED(status));
1071 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1075 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1076 * child process created via fork() reports the correct value.
1078 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1079 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1081 struct ptrace_lwpinfo pl[2];
1082 pid_t children[2], fpid, wpid;
1085 ATF_REQUIRE((fpid = fork()) != -1);
1088 follow_fork_parent(false);
1091 /* Parent process. */
1094 /* The first wait() should report the stop from SIGSTOP. */
1095 wpid = waitpid(children[0], &status, 0);
1096 ATF_REQUIRE(wpid == children[0]);
1097 ATF_REQUIRE(WIFSTOPPED(status));
1098 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1100 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1102 /* Continue the child ignoring the SIGSTOP. */
1103 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1105 /* Wait for both halves of the fork event to get reported. */
1106 children[1] = handle_fork_events(children[0], pl);
1107 ATF_REQUIRE(children[1] > 0);
1109 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1110 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1111 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1112 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1113 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1115 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1116 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1119 * The child can't exit until the grandchild reports status, so the
1120 * grandchild should report its exit first to the debugger.
1122 wpid = wait(&status);
1123 ATF_REQUIRE(wpid == children[1]);
1124 ATF_REQUIRE(WIFEXITED(status));
1125 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1127 wpid = wait(&status);
1128 ATF_REQUIRE(wpid == children[0]);
1129 ATF_REQUIRE(WIFEXITED(status));
1130 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1132 wpid = wait(&status);
1133 ATF_REQUIRE(wpid == -1);
1134 ATF_REQUIRE(errno == ECHILD);
1138 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1139 * child process created via vfork() reports the correct value.
1141 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1142 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1144 struct ptrace_lwpinfo pl[2];
1145 pid_t children[2], fpid, wpid;
1148 ATF_REQUIRE((fpid = fork()) != -1);
1151 follow_fork_parent(true);
1154 /* Parent process. */
1157 /* The first wait() should report the stop from SIGSTOP. */
1158 wpid = waitpid(children[0], &status, 0);
1159 ATF_REQUIRE(wpid == children[0]);
1160 ATF_REQUIRE(WIFSTOPPED(status));
1161 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1163 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1165 /* Continue the child ignoring the SIGSTOP. */
1166 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1168 /* Wait for both halves of the fork event to get reported. */
1169 children[1] = handle_fork_events(children[0], pl);
1170 ATF_REQUIRE(children[1] > 0);
1172 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1173 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1174 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1175 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1176 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1178 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1179 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1182 * The child can't exit until the grandchild reports status, so the
1183 * grandchild should report its exit first to the debugger.
1185 wpid = wait(&status);
1186 ATF_REQUIRE(wpid == children[1]);
1187 ATF_REQUIRE(WIFEXITED(status));
1188 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1190 wpid = wait(&status);
1191 ATF_REQUIRE(wpid == children[0]);
1192 ATF_REQUIRE(WIFEXITED(status));
1193 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1195 wpid = wait(&status);
1196 ATF_REQUIRE(wpid == -1);
1197 ATF_REQUIRE(errno == ECHILD);
1201 simple_thread(void *arg __unused)
1208 simple_thread_main(void)
1212 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1213 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1218 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1219 * thread reports the correct value.
1221 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1222 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1224 struct ptrace_lwpinfo pl;
1229 ATF_REQUIRE((fpid = fork()) != -1);
1232 simple_thread_main();
1235 /* The first wait() should report the stop from SIGSTOP. */
1236 wpid = waitpid(fpid, &status, 0);
1237 ATF_REQUIRE(wpid == fpid);
1238 ATF_REQUIRE(WIFSTOPPED(status));
1239 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1241 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1243 mainlwp = pl.pl_lwpid;
1246 * Continue the child ignoring the SIGSTOP and tracing all
1247 * system call exits.
1249 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1252 * Wait for the new thread to arrive. pthread_create() might
1253 * invoke any number of system calls. For now we just wait
1254 * for the new thread to arrive and make sure it reports a
1255 * valid system call code. If ptrace grows thread event
1256 * reporting then this test can be made more precise.
1259 wpid = waitpid(fpid, &status, 0);
1260 ATF_REQUIRE(wpid == fpid);
1261 ATF_REQUIRE(WIFSTOPPED(status));
1262 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1264 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1266 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1267 ATF_REQUIRE(pl.pl_syscall_code != 0);
1268 if (pl.pl_lwpid != mainlwp)
1269 /* New thread seen. */
1272 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1275 /* Wait for the child to exit. */
1276 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1278 wpid = waitpid(fpid, &status, 0);
1279 ATF_REQUIRE(wpid == fpid);
1280 if (WIFEXITED(status))
1283 ATF_REQUIRE(WIFSTOPPED(status));
1284 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1285 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1288 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1290 wpid = wait(&status);
1291 ATF_REQUIRE(wpid == -1);
1292 ATF_REQUIRE(errno == ECHILD);
1296 * Verify that the expected LWP events are reported for a child thread.
1298 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1299 ATF_TC_BODY(ptrace__lwp_events, tc)
1301 struct ptrace_lwpinfo pl;
1306 ATF_REQUIRE((fpid = fork()) != -1);
1309 simple_thread_main();
1312 /* The first wait() should report the stop from SIGSTOP. */
1313 wpid = waitpid(fpid, &status, 0);
1314 ATF_REQUIRE(wpid == fpid);
1315 ATF_REQUIRE(WIFSTOPPED(status));
1316 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1318 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1320 lwps[0] = pl.pl_lwpid;
1322 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1324 /* Continue the child ignoring the SIGSTOP. */
1325 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1327 /* The first event should be for the child thread's birth. */
1328 wpid = waitpid(fpid, &status, 0);
1329 ATF_REQUIRE(wpid == fpid);
1330 ATF_REQUIRE(WIFSTOPPED(status));
1331 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1333 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1334 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1335 (PL_FLAG_BORN | PL_FLAG_SCX));
1336 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1337 lwps[1] = pl.pl_lwpid;
1339 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1341 /* The next event should be for the child thread's death. */
1342 wpid = waitpid(fpid, &status, 0);
1343 ATF_REQUIRE(wpid == fpid);
1344 ATF_REQUIRE(WIFSTOPPED(status));
1345 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1347 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1348 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1349 (PL_FLAG_EXITED | PL_FLAG_SCE));
1350 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1352 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1354 /* The last event should be for the child process's exit. */
1355 wpid = waitpid(fpid, &status, 0);
1356 ATF_REQUIRE(WIFEXITED(status));
1357 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1359 wpid = wait(&status);
1360 ATF_REQUIRE(wpid == -1);
1361 ATF_REQUIRE(errno == ECHILD);
1365 exec_thread(void *arg __unused)
1368 execl("/usr/bin/true", "true", NULL);
1373 exec_thread_main(void)
1377 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1384 * Verify that the expected LWP events are reported for a multithreaded
1385 * process that calls execve(2).
1387 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1388 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1390 struct ptrace_lwpinfo pl;
1395 ATF_REQUIRE((fpid = fork()) != -1);
1401 /* The first wait() should report the stop from SIGSTOP. */
1402 wpid = waitpid(fpid, &status, 0);
1403 ATF_REQUIRE(wpid == fpid);
1404 ATF_REQUIRE(WIFSTOPPED(status));
1405 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1407 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1409 lwps[0] = pl.pl_lwpid;
1411 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1413 /* Continue the child ignoring the SIGSTOP. */
1414 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1416 /* The first event should be for the child thread's birth. */
1417 wpid = waitpid(fpid, &status, 0);
1418 ATF_REQUIRE(wpid == fpid);
1419 ATF_REQUIRE(WIFSTOPPED(status));
1420 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1422 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1423 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1424 (PL_FLAG_BORN | PL_FLAG_SCX));
1425 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1426 lwps[1] = pl.pl_lwpid;
1428 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1431 * The next event should be for the main thread's death due to
1432 * single threading from execve().
1434 wpid = waitpid(fpid, &status, 0);
1435 ATF_REQUIRE(wpid == fpid);
1436 ATF_REQUIRE(WIFSTOPPED(status));
1437 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1439 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1440 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1442 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1444 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1446 /* The next event should be for the child process's exec. */
1447 wpid = waitpid(fpid, &status, 0);
1448 ATF_REQUIRE(WIFSTOPPED(status));
1449 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1451 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1452 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1453 (PL_FLAG_EXEC | PL_FLAG_SCX));
1454 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1456 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1458 /* The last event should be for the child process's exit. */
1459 wpid = waitpid(fpid, &status, 0);
1460 ATF_REQUIRE(WIFEXITED(status));
1461 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1463 wpid = wait(&status);
1464 ATF_REQUIRE(wpid == -1);
1465 ATF_REQUIRE(errno == ECHILD);
1469 handler(int sig __unused)
1477 signal(SIGINFO, handler);
1483 * Verify that the expected ptrace event is reported for a signal.
1485 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1486 ATF_TC_BODY(ptrace__siginfo, tc)
1488 struct ptrace_lwpinfo pl;
1492 ATF_REQUIRE((fpid = fork()) != -1);
1498 /* The first wait() should report the stop from SIGSTOP. */
1499 wpid = waitpid(fpid, &status, 0);
1500 ATF_REQUIRE(wpid == fpid);
1501 ATF_REQUIRE(WIFSTOPPED(status));
1502 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1504 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1506 /* The next event should be for the SIGINFO. */
1507 wpid = waitpid(fpid, &status, 0);
1508 ATF_REQUIRE(WIFSTOPPED(status));
1509 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1511 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1512 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1513 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1514 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1515 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1517 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1519 /* The last event should be for the child process's exit. */
1520 wpid = waitpid(fpid, &status, 0);
1521 ATF_REQUIRE(WIFEXITED(status));
1522 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1524 wpid = wait(&status);
1525 ATF_REQUIRE(wpid == -1);
1526 ATF_REQUIRE(errno == ECHILD);
1530 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1532 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1533 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1538 ATF_REQUIRE((fpid = fork()) != -1);
1544 /* The first wait() should report the stop from SIGSTOP. */
1545 wpid = waitpid(fpid, &status, 0);
1546 ATF_REQUIRE(wpid == fpid);
1547 ATF_REQUIRE(WIFSTOPPED(status));
1548 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1551 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1552 sizeof(events)) == 0);
1554 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1556 /* Should get one event at exit. */
1557 wpid = waitpid(fpid, &status, 0);
1558 ATF_REQUIRE(WIFEXITED(status));
1559 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1561 wpid = wait(&status);
1562 ATF_REQUIRE(wpid == -1);
1563 ATF_REQUIRE(errno == ECHILD);
1566 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1567 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1569 struct ptrace_lwpinfo pl;
1573 ATF_REQUIRE((fpid = fork()) != -1);
1579 /* The first wait() should report the stop from SIGSTOP. */
1580 wpid = waitpid(fpid, &status, 0);
1581 ATF_REQUIRE(wpid == fpid);
1582 ATF_REQUIRE(WIFSTOPPED(status));
1583 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1585 events = PTRACE_EXEC;
1586 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1587 sizeof(events)) == 0);
1589 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1591 /* The next event should be for the child process's exec. */
1592 wpid = waitpid(fpid, &status, 0);
1593 ATF_REQUIRE(WIFSTOPPED(status));
1594 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1596 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1597 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1598 (PL_FLAG_EXEC | PL_FLAG_SCX));
1600 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1602 /* The last event should be for the child process's exit. */
1603 wpid = waitpid(fpid, &status, 0);
1604 ATF_REQUIRE(WIFEXITED(status));
1605 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1607 wpid = wait(&status);
1608 ATF_REQUIRE(wpid == -1);
1609 ATF_REQUIRE(errno == ECHILD);
1612 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1613 ATF_TC_BODY(ptrace__event_mask, tc)
1618 ATF_REQUIRE((fpid = fork()) != -1);
1624 /* The first wait() should report the stop from SIGSTOP. */
1625 wpid = waitpid(fpid, &status, 0);
1626 ATF_REQUIRE(wpid == fpid);
1627 ATF_REQUIRE(WIFSTOPPED(status));
1628 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1630 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1631 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1632 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1633 sizeof(events)) == 0);
1634 ATF_REQUIRE(events & PTRACE_FORK);
1635 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1636 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1637 sizeof(events)) == 0);
1638 ATF_REQUIRE(!(events & PTRACE_FORK));
1640 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1641 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1642 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1643 sizeof(events)) == 0);
1644 ATF_REQUIRE(events & PTRACE_LWP);
1645 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1646 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1647 sizeof(events)) == 0);
1648 ATF_REQUIRE(!(events & PTRACE_LWP));
1650 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1652 /* Should get one event at exit. */
1653 wpid = waitpid(fpid, &status, 0);
1654 ATF_REQUIRE(WIFEXITED(status));
1655 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1657 wpid = wait(&status);
1658 ATF_REQUIRE(wpid == -1);
1659 ATF_REQUIRE(errno == ECHILD);
1663 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1665 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1666 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1668 struct ptrace_lwpinfo pl;
1672 ATF_REQUIRE((fpid = fork()) != -1);
1675 follow_fork_parent(true);
1678 /* The first wait() should report the stop from SIGSTOP. */
1679 wpid = waitpid(fpid, &status, 0);
1680 ATF_REQUIRE(wpid == fpid);
1681 ATF_REQUIRE(WIFSTOPPED(status));
1682 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1684 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1685 sizeof(events)) == 0);
1686 events |= PTRACE_VFORK;
1687 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1688 sizeof(events)) == 0);
1690 /* Continue the child ignoring the SIGSTOP. */
1691 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1693 /* The next event should report the end of the vfork. */
1694 wpid = wait(&status);
1695 ATF_REQUIRE(wpid == fpid);
1696 ATF_REQUIRE(WIFSTOPPED(status));
1697 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1698 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1699 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1701 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1703 wpid = wait(&status);
1704 ATF_REQUIRE(wpid == fpid);
1705 ATF_REQUIRE(WIFEXITED(status));
1706 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1708 wpid = wait(&status);
1709 ATF_REQUIRE(wpid == -1);
1710 ATF_REQUIRE(errno == ECHILD);
1713 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1714 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1716 struct ptrace_lwpinfo pl[2];
1717 pid_t children[2], fpid, wpid;
1720 ATF_REQUIRE((fpid = fork()) != -1);
1723 follow_fork_parent(true);
1726 /* Parent process. */
1729 /* The first wait() should report the stop from SIGSTOP. */
1730 wpid = waitpid(children[0], &status, 0);
1731 ATF_REQUIRE(wpid == children[0]);
1732 ATF_REQUIRE(WIFSTOPPED(status));
1733 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1735 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1736 sizeof(events)) == 0);
1737 events |= PTRACE_FORK | PTRACE_VFORK;
1738 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1739 sizeof(events)) == 0);
1741 /* Continue the child ignoring the SIGSTOP. */
1742 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1744 /* Wait for both halves of the fork event to get reported. */
1745 children[1] = handle_fork_events(children[0], pl);
1746 ATF_REQUIRE(children[1] > 0);
1748 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1750 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1751 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1754 * The child can't exit until the grandchild reports status, so the
1755 * grandchild should report its exit first to the debugger.
1757 wpid = waitpid(children[1], &status, 0);
1758 ATF_REQUIRE(wpid == children[1]);
1759 ATF_REQUIRE(WIFEXITED(status));
1760 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1763 * The child should report it's vfork() completion before it
1766 wpid = wait(&status);
1767 ATF_REQUIRE(wpid == children[0]);
1768 ATF_REQUIRE(WIFSTOPPED(status));
1769 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1770 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1772 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1774 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1776 wpid = wait(&status);
1777 ATF_REQUIRE(wpid == children[0]);
1778 ATF_REQUIRE(WIFEXITED(status));
1779 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1781 wpid = wait(&status);
1782 ATF_REQUIRE(wpid == -1);
1783 ATF_REQUIRE(errno == ECHILD);
1786 #ifdef HAVE_BREAKPOINT
1788 * Verify that no more events are reported after PT_KILL except for the
1789 * process exit when stopped due to a breakpoint trap.
1791 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1792 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1797 ATF_REQUIRE((fpid = fork()) != -1);
1804 /* The first wait() should report the stop from SIGSTOP. */
1805 wpid = waitpid(fpid, &status, 0);
1806 ATF_REQUIRE(wpid == fpid);
1807 ATF_REQUIRE(WIFSTOPPED(status));
1808 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1810 /* Continue the child ignoring the SIGSTOP. */
1811 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1813 /* The second wait() should report hitting the breakpoint. */
1814 wpid = waitpid(fpid, &status, 0);
1815 ATF_REQUIRE(wpid == fpid);
1816 ATF_REQUIRE(WIFSTOPPED(status));
1817 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1819 /* Kill the child process. */
1820 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1822 /* The last wait() should report the SIGKILL. */
1823 wpid = waitpid(fpid, &status, 0);
1824 ATF_REQUIRE(wpid == fpid);
1825 ATF_REQUIRE(WIFSIGNALED(status));
1826 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1828 wpid = wait(&status);
1829 ATF_REQUIRE(wpid == -1);
1830 ATF_REQUIRE(errno == ECHILD);
1832 #endif /* HAVE_BREAKPOINT */
1835 * Verify that no more events are reported after PT_KILL except for the
1836 * process exit when stopped inside of a system call.
1838 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1839 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1841 struct ptrace_lwpinfo pl;
1845 ATF_REQUIRE((fpid = fork()) != -1);
1852 /* The first wait() should report the stop from SIGSTOP. */
1853 wpid = waitpid(fpid, &status, 0);
1854 ATF_REQUIRE(wpid == fpid);
1855 ATF_REQUIRE(WIFSTOPPED(status));
1856 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1858 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1859 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1861 /* The second wait() should report a system call entry for getpid(). */
1862 wpid = waitpid(fpid, &status, 0);
1863 ATF_REQUIRE(wpid == fpid);
1864 ATF_REQUIRE(WIFSTOPPED(status));
1865 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1867 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1868 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1870 /* Kill the child process. */
1871 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1873 /* The last wait() should report the SIGKILL. */
1874 wpid = waitpid(fpid, &status, 0);
1875 ATF_REQUIRE(wpid == fpid);
1876 ATF_REQUIRE(WIFSIGNALED(status));
1877 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1879 wpid = wait(&status);
1880 ATF_REQUIRE(wpid == -1);
1881 ATF_REQUIRE(errno == ECHILD);
1885 * Verify that no more events are reported after PT_KILL except for the
1886 * process exit when killing a multithreaded process.
1888 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1889 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1891 struct ptrace_lwpinfo pl;
1896 ATF_REQUIRE((fpid = fork()) != -1);
1899 simple_thread_main();
1902 /* The first wait() should report the stop from SIGSTOP. */
1903 wpid = waitpid(fpid, &status, 0);
1904 ATF_REQUIRE(wpid == fpid);
1905 ATF_REQUIRE(WIFSTOPPED(status));
1906 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1908 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1910 main_lwp = pl.pl_lwpid;
1912 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1914 /* Continue the child ignoring the SIGSTOP. */
1915 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1917 /* The first event should be for the child thread's birth. */
1918 wpid = waitpid(fpid, &status, 0);
1919 ATF_REQUIRE(wpid == fpid);
1920 ATF_REQUIRE(WIFSTOPPED(status));
1921 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1923 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1924 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1925 (PL_FLAG_BORN | PL_FLAG_SCX));
1926 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1928 /* Kill the child process. */
1929 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1931 /* The last wait() should report the SIGKILL. */
1932 wpid = waitpid(fpid, &status, 0);
1933 ATF_REQUIRE(wpid == fpid);
1934 ATF_REQUIRE(WIFSIGNALED(status));
1935 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1937 wpid = wait(&status);
1938 ATF_REQUIRE(wpid == -1);
1939 ATF_REQUIRE(errno == ECHILD);
1943 mask_usr1_thread(void *arg)
1945 pthread_barrier_t *pbarrier;
1948 pbarrier = (pthread_barrier_t*)arg;
1950 sigemptyset(&sigmask);
1951 sigaddset(&sigmask, SIGUSR1);
1952 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1954 /* Sync up with other thread after sigmask updated. */
1955 pthread_barrier_wait(pbarrier);
1964 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1965 * and prevents spurious stops due to those other signals.
1967 ATF_TC(ptrace__PT_KILL_competing_signal);
1968 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1971 atf_tc_set_md_var(tc, "require.user", "root");
1973 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1979 pthread_barrier_t barrier;
1980 struct sched_param sched_param;
1982 ATF_REQUIRE((fpid = fork()) != -1);
1984 /* Bind to one CPU so only one thread at a time will run. */
1986 CPU_SET(0, &setmask);
1988 CHILD_REQUIRE(cpuset(&setid) == 0);
1989 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1990 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1992 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1994 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1995 (void*)&barrier) == 0);
1998 * Give the main thread higher priority. The test always
1999 * assumes that, if both threads are able to run, the main
2000 * thread runs first.
2002 sched_param.sched_priority =
2003 (sched_get_priority_max(SCHED_FIFO) +
2004 sched_get_priority_min(SCHED_FIFO)) / 2;
2005 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2006 SCHED_FIFO, &sched_param) == 0);
2007 sched_param.sched_priority -= RQ_PPQ;
2008 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2009 &sched_param) == 0);
2012 sigemptyset(&sigmask);
2013 sigaddset(&sigmask, SIGUSR2);
2014 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2016 /* Sync up with other thread after sigmask updated. */
2017 pthread_barrier_wait(&barrier);
2027 /* The first wait() should report the stop from SIGSTOP. */
2028 wpid = waitpid(fpid, &status, 0);
2029 ATF_REQUIRE(wpid == fpid);
2030 ATF_REQUIRE(WIFSTOPPED(status));
2031 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2033 /* Continue the child ignoring the SIGSTOP. */
2034 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2036 /* Send a signal that only the second thread can handle. */
2037 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2039 /* The second wait() should report the SIGUSR2. */
2040 wpid = waitpid(fpid, &status, 0);
2041 ATF_REQUIRE(wpid == fpid);
2042 ATF_REQUIRE(WIFSTOPPED(status));
2043 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2045 /* Send a signal that only the first thread can handle. */
2046 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2048 /* Replace the SIGUSR2 with a kill. */
2049 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2051 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2052 wpid = waitpid(fpid, &status, 0);
2053 ATF_REQUIRE(wpid == fpid);
2054 ATF_REQUIRE(WIFSIGNALED(status));
2055 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2057 wpid = wait(&status);
2058 ATF_REQUIRE(wpid == -1);
2059 ATF_REQUIRE(errno == ECHILD);
2063 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2064 * and prevents spurious stops caused by those events.
2066 ATF_TC(ptrace__PT_KILL_competing_stop);
2067 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2070 atf_tc_set_md_var(tc, "require.user", "root");
2072 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2078 pthread_barrier_t barrier;
2080 struct ptrace_lwpinfo pl;
2081 struct sched_param sched_param;
2083 atf_tc_skip("https://bugs.freebsd.org/220841");
2085 ATF_REQUIRE((fpid = fork()) != -1);
2089 /* Bind to one CPU so only one thread at a time will run. */
2091 CPU_SET(0, &setmask);
2093 CHILD_REQUIRE(cpuset(&setid) == 0);
2094 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2095 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2097 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2099 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2100 (void*)&barrier) == 0);
2103 * Give the main thread higher priority. The test always
2104 * assumes that, if both threads are able to run, the main
2105 * thread runs first.
2107 sched_param.sched_priority =
2108 (sched_get_priority_max(SCHED_FIFO) +
2109 sched_get_priority_min(SCHED_FIFO)) / 2;
2110 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2111 SCHED_FIFO, &sched_param) == 0);
2112 sched_param.sched_priority -= RQ_PPQ;
2113 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2114 &sched_param) == 0);
2117 sigemptyset(&sigmask);
2118 sigaddset(&sigmask, SIGUSR2);
2119 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2121 /* Sync up with other thread after sigmask updated. */
2122 pthread_barrier_wait(&barrier);
2124 /* Sync up with the test before doing the getpid(). */
2131 /* The first wait() should report the stop from SIGSTOP. */
2132 wpid = waitpid(fpid, &status, 0);
2133 ATF_REQUIRE(wpid == fpid);
2134 ATF_REQUIRE(WIFSTOPPED(status));
2135 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2137 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2138 main_lwp = pl.pl_lwpid;
2140 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2141 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2144 * Continue until child is done with setup, which is indicated with
2145 * SIGSTOP. Ignore system calls in the meantime.
2148 wpid = waitpid(fpid, &status, 0);
2149 ATF_REQUIRE(wpid == fpid);
2150 ATF_REQUIRE(WIFSTOPPED(status));
2151 if (WSTOPSIG(status) == SIGTRAP) {
2152 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2154 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2156 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2159 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2162 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2163 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2165 wpid = waitpid(fpid, &status, 0);
2166 ATF_REQUIRE(wpid == fpid);
2167 ATF_REQUIRE(WIFSTOPPED(status));
2168 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2170 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2172 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2173 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2174 /* Prevent the main thread from hitting its syscall exit for now. */
2175 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2178 * Proceed, allowing second thread to hit syscall exit for
2179 * pthread_barrier_wait().
2181 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2183 wpid = waitpid(fpid, &status, 0);
2184 ATF_REQUIRE(wpid == fpid);
2185 ATF_REQUIRE(WIFSTOPPED(status));
2186 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2188 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2190 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2191 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2193 /* Send a signal that only the second thread can handle. */
2194 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2196 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2198 /* The next wait() should report the SIGUSR2. */
2199 wpid = waitpid(fpid, &status, 0);
2200 ATF_REQUIRE(wpid == fpid);
2201 ATF_REQUIRE(WIFSTOPPED(status));
2202 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2204 /* Allow the main thread to try to finish its system call. */
2205 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2208 * At this point, the main thread is in the middle of a system call and
2209 * has been resumed. The second thread has taken a SIGUSR2 which will
2210 * be replaced with a SIGKILL below. The main thread will get to run
2211 * first. It should notice the kill request (even though the signal
2212 * replacement occurred in the other thread) and exit accordingly. It
2213 * should not stop for the system call exit event.
2216 /* Replace the SIGUSR2 with a kill. */
2217 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2219 /* The last wait() should report the SIGKILL (not a syscall exit). */
2220 wpid = waitpid(fpid, &status, 0);
2221 ATF_REQUIRE(wpid == fpid);
2222 ATF_REQUIRE(WIFSIGNALED(status));
2223 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2225 wpid = wait(&status);
2226 ATF_REQUIRE(wpid == -1);
2227 ATF_REQUIRE(errno == ECHILD);
2231 sigusr1_handler(int sig)
2234 CHILD_REQUIRE(sig == SIGUSR1);
2239 * Verify that even if the signal queue is full for a child process,
2240 * a PT_KILL will kill the process.
2242 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2243 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2247 int max_pending_per_proc;
2251 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2253 ATF_REQUIRE((fpid = fork()) != -1);
2259 /* The first wait() should report the stop from SIGSTOP. */
2260 wpid = waitpid(fpid, &status, 0);
2261 ATF_REQUIRE(wpid == fpid);
2262 ATF_REQUIRE(WIFSTOPPED(status));
2263 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2265 len = sizeof(max_pending_per_proc);
2266 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2267 &max_pending_per_proc, &len, NULL, 0) == 0);
2269 /* Fill the signal queue. */
2270 for (i = 0; i < max_pending_per_proc; ++i)
2271 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2273 /* Kill the child process. */
2274 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2276 /* The last wait() should report the SIGKILL. */
2277 wpid = waitpid(fpid, &status, 0);
2278 ATF_REQUIRE(wpid == fpid);
2279 ATF_REQUIRE(WIFSIGNALED(status));
2280 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2282 wpid = wait(&status);
2283 ATF_REQUIRE(wpid == -1);
2284 ATF_REQUIRE(errno == ECHILD);
2288 * Verify that when stopped at a system call entry, a signal can be
2289 * requested with PT_CONTINUE which will be delivered once the system
2292 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2293 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2295 struct ptrace_lwpinfo pl;
2299 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2301 ATF_REQUIRE((fpid = fork()) != -1);
2308 /* The first wait() should report the stop from SIGSTOP. */
2309 wpid = waitpid(fpid, &status, 0);
2310 ATF_REQUIRE(wpid == fpid);
2311 ATF_REQUIRE(WIFSTOPPED(status));
2312 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2314 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2315 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2317 /* The second wait() should report a system call entry for getpid(). */
2318 wpid = waitpid(fpid, &status, 0);
2319 ATF_REQUIRE(wpid == fpid);
2320 ATF_REQUIRE(WIFSTOPPED(status));
2321 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2323 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2324 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2326 /* Continue the child process with a signal. */
2327 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2331 * The last wait() should report exit 2, i.e., a normal _exit
2332 * from the signal handler. In the meantime, catch and proceed
2333 * past any syscall stops.
2335 wpid = waitpid(fpid, &status, 0);
2336 ATF_REQUIRE(wpid == fpid);
2337 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2338 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2339 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2340 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2342 ATF_REQUIRE(WIFEXITED(status));
2343 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2348 wpid = wait(&status);
2349 ATF_REQUIRE(wpid == -1);
2350 ATF_REQUIRE(errno == ECHILD);
2354 sigusr1_counting_handler(int sig)
2356 static int counter = 0;
2358 CHILD_REQUIRE(sig == SIGUSR1);
2365 * Verify that, when continuing from a stop at system call entry and exit,
2366 * a signal can be requested from both stops, and both will be delivered when
2367 * the system call is complete.
2369 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2370 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2372 struct ptrace_lwpinfo pl;
2376 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2378 ATF_REQUIRE((fpid = fork()) != -1);
2385 /* The first wait() should report the stop from SIGSTOP. */
2386 wpid = waitpid(fpid, &status, 0);
2387 ATF_REQUIRE(wpid == fpid);
2388 ATF_REQUIRE(WIFSTOPPED(status));
2389 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2391 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2392 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2394 /* The second wait() should report a system call entry for getpid(). */
2395 wpid = waitpid(fpid, &status, 0);
2396 ATF_REQUIRE(wpid == fpid);
2397 ATF_REQUIRE(WIFSTOPPED(status));
2398 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2400 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2401 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2403 /* Continue the child process with a signal. */
2404 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2406 /* The third wait() should report a system call exit for getpid(). */
2407 wpid = waitpid(fpid, &status, 0);
2408 ATF_REQUIRE(wpid == fpid);
2409 ATF_REQUIRE(WIFSTOPPED(status));
2410 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2412 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2413 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2415 /* Continue the child process with a signal. */
2416 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2420 * The last wait() should report exit 2, i.e., a normal _exit
2421 * from the signal handler. In the meantime, catch and proceed
2422 * past any syscall stops.
2424 wpid = waitpid(fpid, &status, 0);
2425 ATF_REQUIRE(wpid == fpid);
2426 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2427 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2428 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2429 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2431 ATF_REQUIRE(WIFEXITED(status));
2432 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2437 wpid = wait(&status);
2438 ATF_REQUIRE(wpid == -1);
2439 ATF_REQUIRE(errno == ECHILD);
2443 * Verify that even if the signal queue is full for a child process,
2444 * a PT_CONTINUE with a signal will not result in loss of that signal.
2446 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2447 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2451 int max_pending_per_proc;
2455 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2456 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2458 ATF_REQUIRE((fpid = fork()) != -1);
2464 /* The first wait() should report the stop from SIGSTOP. */
2465 wpid = waitpid(fpid, &status, 0);
2466 ATF_REQUIRE(wpid == fpid);
2467 ATF_REQUIRE(WIFSTOPPED(status));
2468 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2470 len = sizeof(max_pending_per_proc);
2471 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2472 &max_pending_per_proc, &len, NULL, 0) == 0);
2474 /* Fill the signal queue. */
2475 for (i = 0; i < max_pending_per_proc; ++i)
2476 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2478 /* Continue with signal. */
2479 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2482 wpid = waitpid(fpid, &status, 0);
2483 ATF_REQUIRE(wpid == fpid);
2484 if (WIFSTOPPED(status)) {
2485 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2486 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2489 * The last wait() should report normal _exit from the
2492 ATF_REQUIRE(WIFEXITED(status));
2493 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2498 wpid = wait(&status);
2499 ATF_REQUIRE(wpid == -1);
2500 ATF_REQUIRE(errno == ECHILD);
2503 static sem_t sigusr1_sem;
2504 static int got_usr1;
2507 sigusr1_sempost_handler(int sig __unused)
2511 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2515 * Verify that even if the signal queue is full for a child process,
2516 * and the signal is masked, a PT_CONTINUE with a signal will not
2517 * result in loss of that signal.
2519 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2520 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2522 struct ptrace_lwpinfo pl;
2525 int max_pending_per_proc;
2530 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2531 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2532 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2535 ATF_REQUIRE((fpid = fork()) != -1);
2537 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2538 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2539 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2542 CHILD_REQUIRE(got_usr1 == 0);
2544 /* Allow the pending SIGUSR1 in now. */
2545 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2546 /* Wait to receive the SIGUSR1. */
2548 err = sem_wait(&sigusr1_sem);
2549 CHILD_REQUIRE(err == 0 || errno == EINTR);
2550 } while (err != 0 && errno == EINTR);
2551 CHILD_REQUIRE(got_usr1 == 1);
2555 /* The first wait() should report the stop from SIGSTOP. */
2556 wpid = waitpid(fpid, &status, 0);
2557 ATF_REQUIRE(wpid == fpid);
2558 ATF_REQUIRE(WIFSTOPPED(status));
2559 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2561 len = sizeof(max_pending_per_proc);
2562 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2563 &max_pending_per_proc, &len, NULL, 0) == 0);
2565 /* Fill the signal queue. */
2566 for (i = 0; i < max_pending_per_proc; ++i)
2567 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2569 /* Continue with signal. */
2570 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2572 /* Collect and ignore all of the SIGUSR2. */
2573 for (i = 0; i < max_pending_per_proc; ++i) {
2574 wpid = waitpid(fpid, &status, 0);
2575 ATF_REQUIRE(wpid == fpid);
2576 ATF_REQUIRE(WIFSTOPPED(status));
2577 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2578 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2581 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2582 wpid = waitpid(fpid, &status, 0);
2583 ATF_REQUIRE(wpid == fpid);
2584 ATF_REQUIRE(WIFSTOPPED(status));
2585 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2586 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2587 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2589 /* Continue the child, ignoring the SIGUSR1. */
2590 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2592 /* The last wait() should report exit after receiving SIGUSR1. */
2593 wpid = waitpid(fpid, &status, 0);
2594 ATF_REQUIRE(wpid == fpid);
2595 ATF_REQUIRE(WIFEXITED(status));
2596 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2598 wpid = wait(&status);
2599 ATF_REQUIRE(wpid == -1);
2600 ATF_REQUIRE(errno == ECHILD);
2604 * Verify that, after stopping due to a signal, that signal can be
2605 * replaced with another signal.
2607 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2608 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2610 struct ptrace_lwpinfo pl;
2614 ATF_REQUIRE((fpid = fork()) != -1);
2621 /* The first wait() should report the stop from SIGSTOP. */
2622 wpid = waitpid(fpid, &status, 0);
2623 ATF_REQUIRE(wpid == fpid);
2624 ATF_REQUIRE(WIFSTOPPED(status));
2625 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2627 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2629 /* Send a signal without ptrace. */
2630 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2632 /* The second wait() should report a SIGINT was received. */
2633 wpid = waitpid(fpid, &status, 0);
2634 ATF_REQUIRE(wpid == fpid);
2635 ATF_REQUIRE(WIFSTOPPED(status));
2636 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2638 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2639 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2640 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2642 /* Continue the child process with a different signal. */
2643 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2646 * The last wait() should report having died due to the new
2649 wpid = waitpid(fpid, &status, 0);
2650 ATF_REQUIRE(wpid == fpid);
2651 ATF_REQUIRE(WIFSIGNALED(status));
2652 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2654 wpid = wait(&status);
2655 ATF_REQUIRE(wpid == -1);
2656 ATF_REQUIRE(errno == ECHILD);
2660 * Verify that a signal can be passed through to the child even when there
2661 * was no true signal originally. Such cases arise when a SIGTRAP is
2662 * invented for e.g, system call stops.
2664 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2665 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2667 struct ptrace_lwpinfo pl;
2672 ATF_REQUIRE((fpid = fork()) != -1);
2675 /* SIGTRAP expected to cause exit on syscall entry. */
2676 rl.rlim_cur = rl.rlim_max = 0;
2677 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2682 /* The first wait() should report the stop from SIGSTOP. */
2683 wpid = waitpid(fpid, &status, 0);
2684 ATF_REQUIRE(wpid == fpid);
2685 ATF_REQUIRE(WIFSTOPPED(status));
2686 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2688 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2689 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2691 /* The second wait() should report a system call entry for getpid(). */
2692 wpid = waitpid(fpid, &status, 0);
2693 ATF_REQUIRE(wpid == fpid);
2694 ATF_REQUIRE(WIFSTOPPED(status));
2695 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2697 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2698 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2700 /* Continue the child process with a SIGTRAP. */
2701 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2705 * The last wait() should report exit due to SIGTRAP. In the
2706 * meantime, catch and proceed past any syscall stops.
2708 wpid = waitpid(fpid, &status, 0);
2709 ATF_REQUIRE(wpid == fpid);
2710 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2711 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2712 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2713 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2715 ATF_REQUIRE(WIFSIGNALED(status));
2716 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2721 wpid = wait(&status);
2722 ATF_REQUIRE(wpid == -1);
2723 ATF_REQUIRE(errno == ECHILD);
2728 * A mixed bag PT_CONTINUE with signal test.
2730 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2731 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2733 struct ptrace_lwpinfo pl;
2737 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2739 ATF_REQUIRE((fpid = fork()) != -1);
2746 /* The first wait() should report the stop from SIGSTOP. */
2747 wpid = waitpid(fpid, &status, 0);
2748 ATF_REQUIRE(wpid == fpid);
2749 ATF_REQUIRE(WIFSTOPPED(status));
2750 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2752 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2753 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2755 /* The second wait() should report a system call entry for getpid(). */
2756 wpid = waitpid(fpid, &status, 0);
2757 ATF_REQUIRE(wpid == fpid);
2758 ATF_REQUIRE(WIFSTOPPED(status));
2759 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2761 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2762 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2764 /* Continue with the first SIGUSR1. */
2765 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2767 /* The next wait() should report a system call exit for getpid(). */
2768 wpid = waitpid(fpid, &status, 0);
2769 ATF_REQUIRE(wpid == fpid);
2770 ATF_REQUIRE(WIFSTOPPED(status));
2771 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2773 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2774 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2776 /* Send an ABRT without ptrace. */
2777 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2779 /* Continue normally. */
2780 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2782 /* The next wait() should report the SIGABRT. */
2783 wpid = waitpid(fpid, &status, 0);
2784 ATF_REQUIRE(wpid == fpid);
2785 ATF_REQUIRE(WIFSTOPPED(status));
2786 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2788 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2789 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2790 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2792 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2793 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2797 * The last wait() should report exit 2, i.e., a normal _exit
2798 * from the signal handler. In the meantime, catch and proceed
2799 * past any syscall stops.
2801 wpid = waitpid(fpid, &status, 0);
2802 ATF_REQUIRE(wpid == fpid);
2803 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2804 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2805 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2806 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2808 ATF_REQUIRE(WIFEXITED(status));
2809 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2814 wpid = wait(&status);
2815 ATF_REQUIRE(wpid == -1);
2816 ATF_REQUIRE(errno == ECHILD);
2821 * Verify a signal delivered by ptrace is noticed by kevent(2).
2823 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2824 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2827 int status, kq, nevents;
2830 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2832 ATF_REQUIRE((fpid = fork()) != -1);
2834 CHILD_REQUIRE((kq = kqueue()) > 0);
2835 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2836 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2841 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2842 if (nevents == -1 && errno == EINTR)
2844 CHILD_REQUIRE(nevents > 0);
2845 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2846 CHILD_REQUIRE(kev.ident == SIGUSR1);
2853 /* The first wait() should report the stop from SIGSTOP. */
2854 wpid = waitpid(fpid, &status, 0);
2855 ATF_REQUIRE(wpid == fpid);
2856 ATF_REQUIRE(WIFSTOPPED(status));
2857 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2859 /* Continue with the SIGUSR1. */
2860 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2863 * The last wait() should report normal exit with code 1.
2865 wpid = waitpid(fpid, &status, 0);
2866 ATF_REQUIRE(wpid == fpid);
2867 ATF_REQUIRE(WIFEXITED(status));
2868 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2870 wpid = wait(&status);
2871 ATF_REQUIRE(wpid == -1);
2872 ATF_REQUIRE(errno == ECHILD);
2876 signal_thread(void *arg)
2881 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2883 /* Wait for this thread to receive a SIGUSR1. */
2885 err = sem_wait(&sigusr1_sem);
2886 CHILD_REQUIRE(err == 0 || errno == EINTR);
2887 } while (err != 0 && errno == EINTR);
2889 /* Free our companion thread from the barrier. */
2890 pthread_barrier_wait(pbarrier);
2893 * Swap ignore duties; the next SIGUSR1 should go to the
2896 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2897 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2898 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2900 /* Sync up threads after swapping signal masks. */
2901 pthread_barrier_wait(pbarrier);
2903 /* Wait until our companion has received its SIGUSR1. */
2904 pthread_barrier_wait(pbarrier);
2910 * Verify that a traced process with blocked signal received the
2911 * signal from kill() once unmasked.
2913 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2914 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2916 struct ptrace_lwpinfo pl;
2921 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2922 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2925 ATF_REQUIRE((fpid = fork()) != -1);
2927 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2928 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2929 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2932 CHILD_REQUIRE(got_usr1 == 0);
2934 /* Allow the pending SIGUSR1 in now. */
2935 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2936 /* Wait to receive a SIGUSR1. */
2938 err = sem_wait(&sigusr1_sem);
2939 CHILD_REQUIRE(err == 0 || errno == EINTR);
2940 } while (err != 0 && errno == EINTR);
2941 CHILD_REQUIRE(got_usr1 == 1);
2945 /* The first wait() should report the stop from SIGSTOP. */
2946 wpid = waitpid(fpid, &status, 0);
2947 ATF_REQUIRE(wpid == fpid);
2948 ATF_REQUIRE(WIFSTOPPED(status));
2949 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2950 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2951 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2953 /* Send blocked SIGUSR1 which should cause a stop. */
2954 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2956 /* Continue the child ignoring the SIGSTOP. */
2957 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2959 /* The next wait() should report the kill(SIGUSR1) was received. */
2960 wpid = waitpid(fpid, &status, 0);
2961 ATF_REQUIRE(wpid == fpid);
2962 ATF_REQUIRE(WIFSTOPPED(status));
2963 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2964 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2965 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2967 /* Continue the child, allowing in the SIGUSR1. */
2968 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2970 /* The last wait() should report normal exit with code 1. */
2971 wpid = waitpid(fpid, &status, 0);
2972 ATF_REQUIRE(wpid == fpid);
2973 ATF_REQUIRE(WIFEXITED(status));
2974 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2976 wpid = wait(&status);
2977 ATF_REQUIRE(wpid == -1);
2978 ATF_REQUIRE(errno == ECHILD);
2982 * Verify that a traced process with blocked signal received the
2983 * signal from PT_CONTINUE once unmasked.
2985 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2986 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2988 struct ptrace_lwpinfo pl;
2993 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2994 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2997 ATF_REQUIRE((fpid = fork()) != -1);
2999 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3000 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3001 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3004 CHILD_REQUIRE(got_usr1 == 0);
3006 /* Allow the pending SIGUSR1 in now. */
3007 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3008 /* Wait to receive a SIGUSR1. */
3010 err = sem_wait(&sigusr1_sem);
3011 CHILD_REQUIRE(err == 0 || errno == EINTR);
3012 } while (err != 0 && errno == EINTR);
3014 CHILD_REQUIRE(got_usr1 == 1);
3018 /* The first wait() should report the stop from SIGSTOP. */
3019 wpid = waitpid(fpid, &status, 0);
3020 ATF_REQUIRE(wpid == fpid);
3021 ATF_REQUIRE(WIFSTOPPED(status));
3022 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3023 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3024 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3026 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3027 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3029 /* The next wait() should report the SIGUSR1 was received. */
3030 wpid = waitpid(fpid, &status, 0);
3031 ATF_REQUIRE(wpid == fpid);
3032 ATF_REQUIRE(WIFSTOPPED(status));
3033 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3034 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3035 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3037 /* Continue the child, ignoring the SIGUSR1. */
3038 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3040 /* The last wait() should report normal exit with code 1. */
3041 wpid = waitpid(fpid, &status, 0);
3042 ATF_REQUIRE(wpid == fpid);
3043 ATF_REQUIRE(WIFEXITED(status));
3044 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3046 wpid = wait(&status);
3047 ATF_REQUIRE(wpid == -1);
3048 ATF_REQUIRE(errno == ECHILD);
3052 * Verify that if ptrace stops due to a signal but continues with
3053 * a different signal that the new signal is routed to a thread
3054 * that can accept it, and that the thread is awakened by the signal
3055 * in a timely manner.
3057 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3058 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3064 pthread_barrier_t barrier;
3066 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3067 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3068 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3070 ATF_REQUIRE((fpid = fork()) != -1);
3072 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3074 /* The other thread should receive the first SIGUSR1. */
3075 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3076 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3077 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3081 /* Wait until other thread has received its SIGUSR1. */
3082 pthread_barrier_wait(&barrier);
3085 * Swap ignore duties; the next SIGUSR1 should go to this
3088 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3090 /* Sync up threads after swapping signal masks. */
3091 pthread_barrier_wait(&barrier);
3094 * Sync up with test code; we're ready for the next SIGUSR1
3099 /* Wait for this thread to receive a SIGUSR1. */
3101 err = sem_wait(&sigusr1_sem);
3102 CHILD_REQUIRE(err == 0 || errno == EINTR);
3103 } while (err != 0 && errno == EINTR);
3105 /* Free the other thread from the barrier. */
3106 pthread_barrier_wait(&barrier);
3108 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3113 /* The first wait() should report the stop from SIGSTOP. */
3114 wpid = waitpid(fpid, &status, 0);
3115 ATF_REQUIRE(wpid == fpid);
3116 ATF_REQUIRE(WIFSTOPPED(status));
3117 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3119 /* Continue the child ignoring the SIGSTOP. */
3120 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3123 * Send a signal without ptrace that either thread will accept (USR2,
3126 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3128 /* The second wait() should report a SIGUSR2 was received. */
3129 wpid = waitpid(fpid, &status, 0);
3130 ATF_REQUIRE(wpid == fpid);
3131 ATF_REQUIRE(WIFSTOPPED(status));
3132 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3134 /* Continue the child, changing the signal to USR1. */
3135 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3137 /* The next wait() should report the stop from SIGSTOP. */
3138 wpid = waitpid(fpid, &status, 0);
3139 ATF_REQUIRE(wpid == fpid);
3140 ATF_REQUIRE(WIFSTOPPED(status));
3141 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3143 /* Continue the child ignoring the SIGSTOP. */
3144 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3146 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3148 /* The next wait() should report a SIGUSR2 was received. */
3149 wpid = waitpid(fpid, &status, 0);
3150 ATF_REQUIRE(wpid == fpid);
3151 ATF_REQUIRE(WIFSTOPPED(status));
3152 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3154 /* Continue the child, changing the signal to USR1. */
3155 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3157 /* The last wait() should report normal exit with code 1. */
3158 wpid = waitpid(fpid, &status, 0);
3159 ATF_REQUIRE(wpid == fpid);
3160 ATF_REQUIRE(WIFEXITED(status));
3161 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3163 wpid = wait(&status);
3164 ATF_REQUIRE(wpid == -1);
3165 ATF_REQUIRE(errno == ECHILD);
3169 raise_sigstop_thread(void *arg __unused)
3177 sleep_thread(void *arg __unused)
3185 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3194 * Become the reaper for this process tree. We need to be able to check
3195 * that both child and grandchild have died.
3197 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3200 ATF_REQUIRE(fpid >= 0);
3203 CHILD_REQUIRE(fpid >= 0);
3207 /* Pin to CPU 0 to serialize thread execution. */
3209 CPU_SET(0, &setmask);
3210 CHILD_REQUIRE(cpuset(&setid) == 0);
3211 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3212 CPU_WHICH_CPUSET, setid,
3213 sizeof(setmask), &setmask) == 0);
3215 if (sigstop_from_main_thread) {
3217 * We expect the SIGKILL sent when our parent
3218 * dies to be delivered to the new thread.
3219 * Raise the SIGSTOP in this thread so the
3222 CHILD_REQUIRE(pthread_create(&t, NULL,
3223 sleep_thread, NULL) == 0);
3227 * We expect the SIGKILL to be delivered to
3228 * this thread. After creating the new thread,
3229 * just get off the CPU so the other thread can
3230 * raise the SIGSTOP.
3232 CHILD_REQUIRE(pthread_create(&t, NULL,
3233 raise_sigstop_thread, NULL) == 0);
3239 /* First stop is trace_me() immediately after fork. */
3240 wpid = waitpid(fpid, &status, 0);
3241 CHILD_REQUIRE(wpid == fpid);
3242 CHILD_REQUIRE(WIFSTOPPED(status));
3243 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3245 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3247 /* Second stop is from the raise(SIGSTOP). */
3248 wpid = waitpid(fpid, &status, 0);
3249 CHILD_REQUIRE(wpid == fpid);
3250 CHILD_REQUIRE(WIFSTOPPED(status));
3251 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3254 * Terminate tracing process without detaching. Our child
3261 * We should get a normal exit from our immediate child and a SIGKILL
3262 * exit from our grandchild. The latter case is the interesting one.
3263 * Our grandchild should not have stopped due to the SIGSTOP that was
3264 * left dangling when its parent died.
3266 for (i = 0; i < 2; ++i) {
3267 wpid = wait(&status);
3269 ATF_REQUIRE(WIFEXITED(status) &&
3270 WEXITSTATUS(status) == 0);
3272 ATF_REQUIRE(WIFSIGNALED(status) &&
3273 WTERMSIG(status) == SIGKILL);
3278 * These two tests ensure that if the tracing process exits without detaching
3279 * just after the child received a SIGSTOP, the child is cleanly killed and
3280 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3281 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3282 * different threads, the SIGKILL must win. There are two variants of this
3283 * test, designed to catch the case where the SIGKILL is delivered to the
3284 * younger thread (the first test) and the case where the SIGKILL is delivered
3285 * to the older thread (the second test). This behavior has changed in the
3286 * past, so make no assumption.
3288 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3289 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3292 atf_tc_set_md_var(tc, "require.user", "root");
3294 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3297 terminate_with_pending_sigstop(true);
3300 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3301 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3304 atf_tc_set_md_var(tc, "require.user", "root");
3306 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3309 terminate_with_pending_sigstop(false);
3313 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3316 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3317 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3319 struct ptrace_lwpinfo pl;
3321 int status, event_mask, new_event_mask;
3323 ATF_REQUIRE((fpid = fork()) != -1);
3330 /* The first wait() should report the stop from trace_me(). */
3331 wpid = waitpid(fpid, &status, 0);
3332 ATF_REQUIRE(wpid == fpid);
3333 ATF_REQUIRE(WIFSTOPPED(status));
3334 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3336 /* Set several unobtrusive event bits. */
3337 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3338 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3339 sizeof(event_mask)) == 0);
3341 /* Send a SIGKILL without using ptrace. */
3342 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3344 /* Continue the child ignoring the SIGSTOP. */
3345 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3347 /* The next stop should be due to the SIGKILL. */
3348 wpid = waitpid(fpid, &status, 0);
3349 ATF_REQUIRE(wpid == fpid);
3350 ATF_REQUIRE(WIFSTOPPED(status));
3351 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3353 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3354 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3355 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3357 /* Continue the child ignoring the SIGKILL. */
3358 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3360 /* The next wait() should report the stop from SIGSTOP. */
3361 wpid = waitpid(fpid, &status, 0);
3362 ATF_REQUIRE(wpid == fpid);
3363 ATF_REQUIRE(WIFSTOPPED(status));
3364 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3366 /* Check the current event mask. It should not have changed. */
3368 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3369 sizeof(new_event_mask)) == 0);
3370 ATF_REQUIRE(event_mask == new_event_mask);
3372 /* Continue the child to let it exit. */
3373 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3375 /* The last event should be for the child process's exit. */
3376 wpid = waitpid(fpid, &status, 0);
3377 ATF_REQUIRE(WIFEXITED(status));
3378 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3380 wpid = wait(&status);
3381 ATF_REQUIRE(wpid == -1);
3382 ATF_REQUIRE(errno == ECHILD);
3386 flock_thread(void *arg)
3391 (void)flock(fd, LOCK_EX);
3392 (void)flock(fd, LOCK_UN);
3397 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3398 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3399 * on a lock. This is a regression test for r318191.
3401 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3402 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3404 pthread_barrier_t barrier;
3405 pthread_barrierattr_t battr;
3408 int error, fd, i, status;
3410 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3411 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3412 PTHREAD_PROCESS_SHARED) == 0);
3413 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3415 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3416 fd = mkstemp(tmpfile);
3417 ATF_REQUIRE(fd >= 0);
3419 ATF_REQUIRE((child = fork()) != -1);
3424 error = pthread_barrier_wait(&barrier);
3425 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3428 cfd = open(tmpfile, O_RDONLY);
3433 * We want at least two threads blocked on the file lock since
3434 * the SIGSTOP from PT_ATTACH may kick one of them out of
3437 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3439 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3441 if (pthread_join(t[0], NULL) != 0)
3443 if (pthread_join(t[1], NULL) != 0)
3448 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3450 error = pthread_barrier_wait(&barrier);
3451 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3454 * Give the child some time to block. Is there a better way to do this?
3459 * Attach and give the child 3 seconds to stop.
3461 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3462 for (i = 0; i < 3; i++) {
3463 wpid = waitpid(child, &status, WNOHANG);
3464 if (wpid == child && WIFSTOPPED(status) &&
3465 WSTOPSIG(status) == SIGSTOP)
3469 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3471 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3473 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3474 ATF_REQUIRE(unlink(tmpfile) == 0);
3475 ATF_REQUIRE(close(fd) == 0);
3479 sigusr1_step_handler(int sig)
3482 CHILD_REQUIRE(sig == SIGUSR1);
3487 * Verify that PT_STEP with a signal invokes the signal before
3488 * stepping the next instruction (and that the next instruction is
3489 * stepped correctly).
3491 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3492 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3494 struct ptrace_lwpinfo pl;
3498 ATF_REQUIRE((fpid = fork()) != -1);
3501 signal(SIGUSR1, sigusr1_step_handler);
3506 /* The first wait() should report the stop from SIGSTOP. */
3507 wpid = waitpid(fpid, &status, 0);
3508 ATF_REQUIRE(wpid == fpid);
3509 ATF_REQUIRE(WIFSTOPPED(status));
3510 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3512 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3514 /* The next stop should report the SIGABRT in the child body. */
3515 wpid = waitpid(fpid, &status, 0);
3516 ATF_REQUIRE(wpid == fpid);
3517 ATF_REQUIRE(WIFSTOPPED(status));
3518 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3520 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3521 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3522 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3524 /* Step the child process inserting SIGUSR1. */
3525 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3527 /* The next stop should report the SIGABRT in the signal handler. */
3528 wpid = waitpid(fpid, &status, 0);
3529 ATF_REQUIRE(wpid == fpid);
3530 ATF_REQUIRE(WIFSTOPPED(status));
3531 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3533 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3534 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3535 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3537 /* Continue the child process discarding the signal. */
3538 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3540 /* The next stop should report a trace trap from PT_STEP. */
3541 wpid = waitpid(fpid, &status, 0);
3542 ATF_REQUIRE(wpid == fpid);
3543 ATF_REQUIRE(WIFSTOPPED(status));
3544 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3546 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3547 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3548 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3549 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3551 /* Continue the child to let it exit. */
3552 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3554 /* The last event should be for the child process's exit. */
3555 wpid = waitpid(fpid, &status, 0);
3556 ATF_REQUIRE(WIFEXITED(status));
3557 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3559 wpid = wait(&status);
3560 ATF_REQUIRE(wpid == -1);
3561 ATF_REQUIRE(errno == ECHILD);
3564 #ifdef HAVE_BREAKPOINT
3566 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3567 * for a breakpoint trap.
3569 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3570 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3572 struct ptrace_lwpinfo pl;
3576 ATF_REQUIRE((fpid = fork()) != -1);
3583 /* The first wait() should report the stop from SIGSTOP. */
3584 wpid = waitpid(fpid, &status, 0);
3585 ATF_REQUIRE(wpid == fpid);
3586 ATF_REQUIRE(WIFSTOPPED(status));
3587 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3589 /* Continue the child ignoring the SIGSTOP. */
3590 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3592 /* The second wait() should report hitting the breakpoint. */
3593 wpid = waitpid(fpid, &status, 0);
3594 ATF_REQUIRE(wpid == fpid);
3595 ATF_REQUIRE(WIFSTOPPED(status));
3596 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3598 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3599 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3600 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3601 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3603 /* Kill the child process. */
3604 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3606 /* The last wait() should report the SIGKILL. */
3607 wpid = waitpid(fpid, &status, 0);
3608 ATF_REQUIRE(wpid == fpid);
3609 ATF_REQUIRE(WIFSIGNALED(status));
3610 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3612 wpid = wait(&status);
3613 ATF_REQUIRE(wpid == -1);
3614 ATF_REQUIRE(errno == ECHILD);
3616 #endif /* HAVE_BREAKPOINT */
3619 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3620 * for a single-step trap from PT_STEP.
3622 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3623 ATF_TC_BODY(ptrace__step_siginfo, tc)
3625 struct ptrace_lwpinfo pl;
3629 ATF_REQUIRE((fpid = fork()) != -1);
3635 /* The first wait() should report the stop from SIGSTOP. */
3636 wpid = waitpid(fpid, &status, 0);
3637 ATF_REQUIRE(wpid == fpid);
3638 ATF_REQUIRE(WIFSTOPPED(status));
3639 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3641 /* Step the child ignoring the SIGSTOP. */
3642 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3644 /* The second wait() should report a single-step trap. */
3645 wpid = waitpid(fpid, &status, 0);
3646 ATF_REQUIRE(wpid == fpid);
3647 ATF_REQUIRE(WIFSTOPPED(status));
3648 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3650 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3651 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3652 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3653 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3655 /* Continue the child process. */
3656 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3658 /* The last event should be for the child process's exit. */
3659 wpid = waitpid(fpid, &status, 0);
3660 ATF_REQUIRE(WIFEXITED(status));
3661 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3663 wpid = wait(&status);
3664 ATF_REQUIRE(wpid == -1);
3665 ATF_REQUIRE(errno == ECHILD);
3668 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3670 continue_thread(void *arg __unused)
3677 continue_thread_main(void)
3679 pthread_t threads[2];
3681 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3683 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3685 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3686 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3691 * Ensure that PT_CONTINUE clears the status of the thread that
3692 * triggered the stop even if a different thread's LWP was passed to
3695 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3696 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3698 struct ptrace_lwpinfo pl;
3705 ATF_REQUIRE((fpid = fork()) != -1);
3708 continue_thread_main();
3711 /* The first wait() should report the stop from SIGSTOP. */
3712 wpid = waitpid(fpid, &status, 0);
3713 ATF_REQUIRE(wpid == fpid);
3714 ATF_REQUIRE(WIFSTOPPED(status));
3715 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3717 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3720 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3722 /* Continue the child ignoring the SIGSTOP. */
3723 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3725 /* One of the new threads should report it's birth. */
3726 wpid = waitpid(fpid, &status, 0);
3727 ATF_REQUIRE(wpid == fpid);
3728 ATF_REQUIRE(WIFSTOPPED(status));
3729 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3731 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3732 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3733 (PL_FLAG_BORN | PL_FLAG_SCX));
3734 lwps[0] = pl.pl_lwpid;
3737 * Suspend this thread to ensure both threads are alive before
3738 * hitting the breakpoint.
3740 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3742 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3744 /* Second thread should report it's birth. */
3745 wpid = waitpid(fpid, &status, 0);
3746 ATF_REQUIRE(wpid == fpid);
3747 ATF_REQUIRE(WIFSTOPPED(status));
3748 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3750 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3751 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3752 (PL_FLAG_BORN | PL_FLAG_SCX));
3753 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3754 lwps[1] = pl.pl_lwpid;
3756 /* Resume both threads waiting for breakpoint events. */
3757 hit_break[0] = hit_break[1] = false;
3758 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3759 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3761 /* One thread should report a breakpoint. */
3762 wpid = waitpid(fpid, &status, 0);
3763 ATF_REQUIRE(wpid == fpid);
3764 ATF_REQUIRE(WIFSTOPPED(status));
3765 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3767 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3768 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3769 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3770 pl.pl_siginfo.si_code == TRAP_BRKPT);
3771 if (pl.pl_lwpid == lwps[0])
3775 hit_break[i] = true;
3776 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3778 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3781 * Resume both threads but pass the other thread's LWPID to
3784 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3787 * Will now get two thread exit events and one more breakpoint
3790 for (j = 0; j < 3; j++) {
3791 wpid = waitpid(fpid, &status, 0);
3792 ATF_REQUIRE(wpid == fpid);
3793 ATF_REQUIRE(WIFSTOPPED(status));
3794 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3796 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3799 if (pl.pl_lwpid == lwps[0])
3804 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3805 if (pl.pl_flags & PL_FLAG_EXITED) {
3806 ATF_REQUIRE_MSG(hit_break[i],
3807 "exited thread did not report breakpoint");
3810 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3811 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3812 pl.pl_siginfo.si_code == TRAP_BRKPT);
3813 ATF_REQUIRE_MSG(!hit_break[i],
3814 "double breakpoint event");
3815 hit_break[i] = true;
3816 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3819 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3823 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3826 /* Both threads should have exited. */
3827 ATF_REQUIRE(lwps[0] == 0);
3828 ATF_REQUIRE(lwps[1] == 0);
3830 /* The last event should be for the child process's exit. */
3831 wpid = waitpid(fpid, &status, 0);
3832 ATF_REQUIRE(WIFEXITED(status));
3833 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3835 wpid = wait(&status);
3836 ATF_REQUIRE(wpid == -1);
3837 ATF_REQUIRE(errno == ECHILD);
3842 * Verify that PT_LWPINFO doesn't return stale siginfo.
3844 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3845 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3847 struct ptrace_lwpinfo pl;
3851 ATF_REQUIRE((fpid = fork()) != -1);
3858 /* The first wait() should report the stop from SIGSTOP. */
3859 wpid = waitpid(fpid, &status, 0);
3860 ATF_REQUIRE(wpid == fpid);
3861 ATF_REQUIRE(WIFSTOPPED(status));
3862 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3864 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3866 /* The next stop should report the SIGABRT in the child body. */
3867 wpid = waitpid(fpid, &status, 0);
3868 ATF_REQUIRE(wpid == fpid);
3869 ATF_REQUIRE(WIFSTOPPED(status));
3870 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3872 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3873 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3874 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3877 * Continue the process ignoring the signal, but enabling
3880 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3883 * The next stop should report a system call entry from
3884 * exit(). PL_FLAGS_SI should not be set.
3886 wpid = waitpid(fpid, &status, 0);
3887 ATF_REQUIRE(wpid == fpid);
3888 ATF_REQUIRE(WIFSTOPPED(status));
3889 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3891 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3892 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3893 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3895 /* Disable syscall tracing and continue the child to let it exit. */
3896 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3897 sizeof(events)) == 0);
3898 events &= ~PTRACE_SYSCALL;
3899 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3900 sizeof(events)) == 0);
3901 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3903 /* The last event should be for the child process's exit. */
3904 wpid = waitpid(fpid, &status, 0);
3905 ATF_REQUIRE(WIFEXITED(status));
3906 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3908 wpid = wait(&status);
3909 ATF_REQUIRE(wpid == -1);
3910 ATF_REQUIRE(errno == ECHILD);
3914 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3916 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3917 ATF_TC_BODY(ptrace__syscall_args, tc)
3919 struct ptrace_lwpinfo pl;
3920 struct ptrace_sc_ret psr;
3925 ATF_REQUIRE((fpid = fork()) != -1);
3933 /* The first wait() should report the stop from SIGSTOP. */
3934 wpid = waitpid(fpid, &status, 0);
3935 ATF_REQUIRE(wpid == fpid);
3936 ATF_REQUIRE(WIFSTOPPED(status));
3937 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3940 * Continue the process ignoring the signal, but enabling
3943 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3946 * The next stop should be the syscall entry from getpid().
3948 wpid = waitpid(fpid, &status, 0);
3949 ATF_REQUIRE(wpid == fpid);
3950 ATF_REQUIRE(WIFSTOPPED(status));
3951 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3953 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3954 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3955 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3957 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3960 * The next stop should be the syscall exit from getpid().
3962 wpid = waitpid(fpid, &status, 0);
3963 ATF_REQUIRE(wpid == fpid);
3964 ATF_REQUIRE(WIFSTOPPED(status));
3965 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3967 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3968 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
3969 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3971 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
3972 sizeof(psr)) != -1);
3973 ATF_REQUIRE(psr.sr_error == 0);
3974 ATF_REQUIRE(psr.sr_retval[0] == wpid);
3976 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3979 * The next stop should be the syscall entry from kill().
3981 wpid = waitpid(fpid, &status, 0);
3982 ATF_REQUIRE(wpid == fpid);
3983 ATF_REQUIRE(WIFSTOPPED(status));
3984 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3986 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3987 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3988 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
3989 ATF_REQUIRE(pl.pl_syscall_narg == 2);
3991 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
3992 sizeof(args)) != -1);
3993 ATF_REQUIRE(args[0] == wpid);
3994 ATF_REQUIRE(args[1] == 0);
3996 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3999 * The next stop should be the syscall exit from kill().
4001 wpid = waitpid(fpid, &status, 0);
4002 ATF_REQUIRE(wpid == fpid);
4003 ATF_REQUIRE(WIFSTOPPED(status));
4004 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4006 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4007 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4008 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
4010 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4011 sizeof(psr)) != -1);
4012 ATF_REQUIRE(psr.sr_error == 0);
4014 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4017 * The next stop should be the syscall entry from close().
4019 wpid = waitpid(fpid, &status, 0);
4020 ATF_REQUIRE(wpid == fpid);
4021 ATF_REQUIRE(WIFSTOPPED(status));
4022 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4024 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4025 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4026 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4027 ATF_REQUIRE(pl.pl_syscall_narg == 1);
4029 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4030 sizeof(args)) != -1);
4031 ATF_REQUIRE(args[0] == 3);
4033 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4036 * The next stop should be the syscall exit from close().
4038 wpid = waitpid(fpid, &status, 0);
4039 ATF_REQUIRE(wpid == fpid);
4040 ATF_REQUIRE(WIFSTOPPED(status));
4041 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4043 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4044 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4045 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4047 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4048 sizeof(psr)) != -1);
4049 ATF_REQUIRE(psr.sr_error == EBADF);
4051 /* Disable syscall tracing and continue the child to let it exit. */
4052 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4053 sizeof(events)) == 0);
4054 events &= ~PTRACE_SYSCALL;
4055 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4056 sizeof(events)) == 0);
4057 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4059 /* The last event should be for the child process's exit. */
4060 wpid = waitpid(fpid, &status, 0);
4061 ATF_REQUIRE(WIFEXITED(status));
4062 ATF_REQUIRE(WEXITSTATUS(status) == 1);
4064 wpid = wait(&status);
4065 ATF_REQUIRE(wpid == -1);
4066 ATF_REQUIRE(errno == ECHILD);
4072 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4073 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4074 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4075 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4076 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4077 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4078 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4079 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4080 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4082 ptrace__follow_fork_child_detached_unrelated_debugger);
4084 ptrace__follow_fork_parent_detached_unrelated_debugger);
4085 ATF_TP_ADD_TC(tp, ptrace__getppid);
4086 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4087 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4088 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4089 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4090 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4091 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4092 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4093 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4094 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4095 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4096 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4097 #ifdef HAVE_BREAKPOINT
4098 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4100 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4101 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4102 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4103 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4104 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4105 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4107 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4108 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4109 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4110 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4111 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4112 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4113 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4114 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4115 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4116 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4117 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4118 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4119 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4120 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4121 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4122 #ifdef HAVE_BREAKPOINT
4123 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4125 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4126 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4127 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4129 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4130 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4132 return (atf_no_error());