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/procdesc.h>
36 #define _WANT_MIPS_REGNUM
37 #include <sys/ptrace.h>
38 #include <sys/queue.h>
40 #include <sys/syscall.h>
41 #include <sys/sysctl.h>
45 #include <machine/cpufunc.h>
48 #include <semaphore.h>
56 * Architectures with a user-visible breakpoint().
58 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
59 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
61 #define HAVE_BREAKPOINT
65 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
67 #ifdef HAVE_BREAKPOINT
68 #if defined(__aarch64__)
69 #define SKIP_BREAK(reg) ((reg)->elr += 4)
70 #elif defined(__amd64__) || defined(__i386__)
71 #define SKIP_BREAK(reg)
72 #elif defined(__arm__)
73 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
74 #elif defined(__mips__)
75 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
76 #elif defined(__riscv)
77 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
78 #elif defined(__sparc64__)
79 #define SKIP_BREAK(reg) do { \
80 (reg)->r_tpc = (reg)->r_tnpc + 4; \
87 * A variant of ATF_REQUIRE that is suitable for use in child
88 * processes. This only works if the parent process is tripped up by
89 * the early exit and fails some requirement itself.
91 #define CHILD_REQUIRE(exp) do { \
93 child_fail_require(__FILE__, __LINE__, \
98 child_fail_require(const char *file, int line, const char *str)
102 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
103 write(2, buf, strlen(buf));
111 /* Attach the parent process as a tracer of this process. */
112 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
114 /* Trigger a stop. */
119 attach_child(pid_t pid)
124 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
126 wpid = waitpid(pid, &status, 0);
127 ATF_REQUIRE(wpid == pid);
128 ATF_REQUIRE(WIFSTOPPED(status));
129 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
133 wait_for_zombie(pid_t pid)
137 * Wait for a process to exit. This is kind of gross, but
138 * there is not a better way.
140 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
141 * with ESRCH. After that change, a valid struct kinfo_proc
142 * is returned for zombies with ki_stat set to SZOMB.
145 struct kinfo_proc kp;
151 mib[2] = KERN_PROC_PID;
154 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
155 ATF_REQUIRE(errno == ESRCH);
158 if (kp.ki_stat == SZOMB)
165 * Verify that a parent debugger process "sees" the exit of a debugged
166 * process exactly once when attached via PT_TRACE_ME.
168 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
169 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
174 ATF_REQUIRE((child = fork()) != -1);
182 /* Parent process. */
184 /* The first wait() should report the stop from SIGSTOP. */
185 wpid = waitpid(child, &status, 0);
186 ATF_REQUIRE(wpid == child);
187 ATF_REQUIRE(WIFSTOPPED(status));
188 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
190 /* Continue the child ignoring the SIGSTOP. */
191 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
193 /* The second wait() should report the exit status. */
194 wpid = waitpid(child, &status, 0);
195 ATF_REQUIRE(wpid == child);
196 ATF_REQUIRE(WIFEXITED(status));
197 ATF_REQUIRE(WEXITSTATUS(status) == 1);
199 /* The child should no longer exist. */
200 wpid = waitpid(child, &status, 0);
201 ATF_REQUIRE(wpid == -1);
202 ATF_REQUIRE(errno == ECHILD);
206 * Verify that a parent debugger process "sees" the exit of a debugged
207 * process exactly once when attached via PT_ATTACH.
209 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
210 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
213 int cpipe[2], status;
216 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
217 atf_tc_skip("https://bugs.freebsd.org/244055");
219 ATF_REQUIRE(pipe(cpipe) == 0);
220 ATF_REQUIRE((child = fork()) != -1);
225 /* Wait for the parent to attach. */
226 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
232 /* Parent process. */
234 /* Attach to the child process. */
237 /* Continue the child ignoring the SIGSTOP. */
238 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
240 /* Signal the child to exit. */
243 /* The second wait() should report the exit status. */
244 wpid = waitpid(child, &status, 0);
245 ATF_REQUIRE(wpid == child);
246 ATF_REQUIRE(WIFEXITED(status));
247 ATF_REQUIRE(WEXITSTATUS(status) == 1);
249 /* The child should no longer exist. */
250 wpid = waitpid(child, &status, 0);
251 ATF_REQUIRE(wpid == -1);
252 ATF_REQUIRE(errno == ECHILD);
256 * Verify that a parent process "sees" the exit of a debugged process only
257 * after the debugger has seen it.
259 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
260 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
262 pid_t child, debugger, wpid;
263 int cpipe[2], dpipe[2], status;
266 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
267 atf_tc_skip("https://bugs.freebsd.org/239399");
269 ATF_REQUIRE(pipe(cpipe) == 0);
270 ATF_REQUIRE((child = fork()) != -1);
276 /* Wait for parent to be ready. */
277 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
283 ATF_REQUIRE(pipe(dpipe) == 0);
284 ATF_REQUIRE((debugger = fork()) != -1);
287 /* Debugger process. */
290 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
292 wpid = waitpid(child, &status, 0);
293 CHILD_REQUIRE(wpid == child);
294 CHILD_REQUIRE(WIFSTOPPED(status));
295 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
297 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
299 /* Signal parent that debugger is attached. */
300 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
302 /* Wait for parent's failed wait. */
303 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
305 wpid = waitpid(child, &status, 0);
306 CHILD_REQUIRE(wpid == child);
307 CHILD_REQUIRE(WIFEXITED(status));
308 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
314 /* Parent process. */
316 /* Wait for the debugger to attach to the child. */
317 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
319 /* Release the child. */
320 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
321 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
324 wait_for_zombie(child);
327 * This wait should return a pid of 0 to indicate no status to
328 * report. The parent should see the child as non-exited
329 * until the debugger sees the exit.
331 wpid = waitpid(child, &status, WNOHANG);
332 ATF_REQUIRE(wpid == 0);
334 /* Signal the debugger to wait for the child. */
337 /* Wait for the debugger. */
338 wpid = waitpid(debugger, &status, 0);
339 ATF_REQUIRE(wpid == debugger);
340 ATF_REQUIRE(WIFEXITED(status));
341 ATF_REQUIRE(WEXITSTATUS(status) == 0);
343 /* The child process should now be ready. */
344 wpid = waitpid(child, &status, WNOHANG);
345 ATF_REQUIRE(wpid == child);
346 ATF_REQUIRE(WIFEXITED(status));
347 ATF_REQUIRE(WEXITSTATUS(status) == 1);
351 * Verify that a parent process "sees" the exit of a debugged process
352 * only after a non-direct-child debugger has seen it. In particular,
353 * various wait() calls in the parent must avoid failing with ESRCH by
354 * checking the parent's orphan list for the debugee.
356 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
357 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
359 pid_t child, debugger, fpid, wpid;
360 int cpipe[2], dpipe[2], status;
363 ATF_REQUIRE(pipe(cpipe) == 0);
364 ATF_REQUIRE((child = fork()) != -1);
370 /* Wait for parent to be ready. */
371 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
377 ATF_REQUIRE(pipe(dpipe) == 0);
378 ATF_REQUIRE((debugger = fork()) != -1);
381 /* Debugger parent. */
384 * Fork again and drop the debugger parent so that the
385 * debugger is not a child of the main parent.
387 CHILD_REQUIRE((fpid = fork()) != -1);
391 /* Debugger process. */
394 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
396 wpid = waitpid(child, &status, 0);
397 CHILD_REQUIRE(wpid == child);
398 CHILD_REQUIRE(WIFSTOPPED(status));
399 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
401 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
403 /* Signal parent that debugger is attached. */
404 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
406 /* Wait for parent's failed wait. */
407 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
409 wpid = waitpid(child, &status, 0);
410 CHILD_REQUIRE(wpid == child);
411 CHILD_REQUIRE(WIFEXITED(status));
412 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
418 /* Parent process. */
420 /* Wait for the debugger parent process to exit. */
421 wpid = waitpid(debugger, &status, 0);
422 ATF_REQUIRE(wpid == debugger);
423 ATF_REQUIRE(WIFEXITED(status));
424 ATF_REQUIRE(WEXITSTATUS(status) == 2);
426 /* A WNOHANG wait here should see the non-exited child. */
427 wpid = waitpid(child, &status, WNOHANG);
428 ATF_REQUIRE(wpid == 0);
430 /* Wait for the debugger to attach to the child. */
431 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
433 /* Release the child. */
434 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
435 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
438 wait_for_zombie(child);
441 * This wait should return a pid of 0 to indicate no status to
442 * report. The parent should see the child as non-exited
443 * until the debugger sees the exit.
445 wpid = waitpid(child, &status, WNOHANG);
446 ATF_REQUIRE(wpid == 0);
448 /* Signal the debugger to wait for the child. */
449 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
451 /* Wait for the debugger. */
452 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
455 /* The child process should now be ready. */
456 wpid = waitpid(child, &status, WNOHANG);
457 ATF_REQUIRE(wpid == child);
458 ATF_REQUIRE(WIFEXITED(status));
459 ATF_REQUIRE(WEXITSTATUS(status) == 1);
463 * Make sure that we can collect the exit status of an orphaned process.
465 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
466 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
469 int cpipe1[2], cpipe2[2], gcpipe[2], status;
472 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
473 atf_tc_skip("https://bugs.freebsd.org/244056");
475 ATF_REQUIRE(pipe(cpipe1) == 0);
476 ATF_REQUIRE(pipe(cpipe2) == 0);
477 ATF_REQUIRE(pipe(gcpipe) == 0);
479 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
481 ATF_REQUIRE((child = fork()) != -1);
483 CHILD_REQUIRE((gchild = fork()) != -1);
487 n = read(gcpipe[0], &status, sizeof(status));
488 } while (n == -1 && errno == EINTR);
492 CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
494 CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
499 ATF_REQUIRE(read(cpipe1[0], &gchild, sizeof(gchild)) == sizeof(gchild));
501 ATF_REQUIRE(ptrace(PT_ATTACH, gchild, NULL, 0) == 0);
504 ATF_REQUIRE(write(cpipe2[1], &status, sizeof(status)) ==
506 ATF_REQUIRE(waitpid(child, &status, 0) == child);
507 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
510 ATF_REQUIRE(write(gcpipe[1], &status, sizeof(status)) ==
512 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
513 ATF_REQUIRE(WIFSTOPPED(status));
514 ATF_REQUIRE(ptrace(PT_DETACH, gchild, (caddr_t)1, 0) == 0);
515 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
516 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
518 ATF_REQUIRE(close(cpipe1[0]) == 0);
519 ATF_REQUIRE(close(cpipe1[1]) == 0);
520 ATF_REQUIRE(close(cpipe2[0]) == 0);
521 ATF_REQUIRE(close(cpipe2[1]) == 0);
522 ATF_REQUIRE(close(gcpipe[0]) == 0);
523 ATF_REQUIRE(close(gcpipe[1]) == 0);
527 * The parent process should always act the same regardless of how the
528 * debugger is attached to it.
531 follow_fork_parent(bool use_vfork)
537 CHILD_REQUIRE((fpid = vfork()) != -1);
539 CHILD_REQUIRE((fpid = fork()) != -1);
545 wpid = waitpid(fpid, &status, 0);
546 CHILD_REQUIRE(wpid == fpid);
547 CHILD_REQUIRE(WIFEXITED(status));
548 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
554 * Helper routine for follow fork tests. This waits for two stops
555 * that report both "sides" of a fork. It returns the pid of the new
559 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
561 struct ptrace_lwpinfo pl;
562 bool fork_reported[2];
566 fork_reported[0] = false;
567 fork_reported[1] = false;
571 * Each process should report a fork event. The parent should
572 * report a PL_FLAG_FORKED event, and the child should report
573 * a PL_FLAG_CHILD event.
575 for (i = 0; i < 2; i++) {
576 wpid = wait(&status);
577 ATF_REQUIRE(wpid > 0);
578 ATF_REQUIRE(WIFSTOPPED(status));
580 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
582 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
584 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
585 (PL_FLAG_FORKED | PL_FLAG_CHILD));
586 if (pl.pl_flags & PL_FLAG_CHILD) {
587 ATF_REQUIRE(wpid != parent);
588 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
589 ATF_REQUIRE(!fork_reported[1]);
593 ATF_REQUIRE(child == wpid);
596 fork_reported[1] = true;
598 ATF_REQUIRE(wpid == parent);
599 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
600 ATF_REQUIRE(!fork_reported[0]);
602 child = pl.pl_child_pid;
604 ATF_REQUIRE(child == pl.pl_child_pid);
607 fork_reported[0] = true;
615 * Verify that a new child process is stopped after a followed fork and
616 * that the traced parent sees the exit of the child after the debugger
617 * when both processes remain attached to the debugger.
619 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
620 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
622 pid_t children[2], fpid, wpid;
625 ATF_REQUIRE((fpid = fork()) != -1);
628 follow_fork_parent(false);
631 /* Parent process. */
634 /* The first wait() should report the stop from SIGSTOP. */
635 wpid = waitpid(children[0], &status, 0);
636 ATF_REQUIRE(wpid == children[0]);
637 ATF_REQUIRE(WIFSTOPPED(status));
638 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
640 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
642 /* Continue the child ignoring the SIGSTOP. */
643 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
645 children[1] = handle_fork_events(children[0], NULL);
646 ATF_REQUIRE(children[1] > 0);
648 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
649 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
652 * The child can't exit until the grandchild reports status, so the
653 * grandchild should report its exit first to the debugger.
655 wpid = wait(&status);
656 ATF_REQUIRE(wpid == children[1]);
657 ATF_REQUIRE(WIFEXITED(status));
658 ATF_REQUIRE(WEXITSTATUS(status) == 2);
660 wpid = wait(&status);
661 ATF_REQUIRE(wpid == children[0]);
662 ATF_REQUIRE(WIFEXITED(status));
663 ATF_REQUIRE(WEXITSTATUS(status) == 1);
665 wpid = wait(&status);
666 ATF_REQUIRE(wpid == -1);
667 ATF_REQUIRE(errno == ECHILD);
671 * Verify that a new child process is stopped after a followed fork
672 * and that the traced parent sees the exit of the child when the new
673 * child process is detached after it reports its fork.
675 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
676 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
678 pid_t children[2], fpid, wpid;
681 ATF_REQUIRE((fpid = fork()) != -1);
684 follow_fork_parent(false);
687 /* Parent process. */
690 /* The first wait() should report the stop from SIGSTOP. */
691 wpid = waitpid(children[0], &status, 0);
692 ATF_REQUIRE(wpid == children[0]);
693 ATF_REQUIRE(WIFSTOPPED(status));
694 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
696 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
698 /* Continue the child ignoring the SIGSTOP. */
699 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
701 children[1] = handle_fork_events(children[0], NULL);
702 ATF_REQUIRE(children[1] > 0);
704 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
705 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
708 * Should not see any status from the grandchild now, only the
711 wpid = wait(&status);
712 ATF_REQUIRE(wpid == children[0]);
713 ATF_REQUIRE(WIFEXITED(status));
714 ATF_REQUIRE(WEXITSTATUS(status) == 1);
716 wpid = wait(&status);
717 ATF_REQUIRE(wpid == -1);
718 ATF_REQUIRE(errno == ECHILD);
722 * Verify that a new child process is stopped after a followed fork
723 * and that the traced parent sees the exit of the child when the
724 * traced parent is detached after the fork.
726 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
727 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
729 pid_t children[2], fpid, wpid;
732 ATF_REQUIRE((fpid = fork()) != -1);
735 follow_fork_parent(false);
738 /* Parent process. */
741 /* The first wait() should report the stop from SIGSTOP. */
742 wpid = waitpid(children[0], &status, 0);
743 ATF_REQUIRE(wpid == children[0]);
744 ATF_REQUIRE(WIFSTOPPED(status));
745 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
747 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
749 /* Continue the child ignoring the SIGSTOP. */
750 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
752 children[1] = handle_fork_events(children[0], NULL);
753 ATF_REQUIRE(children[1] > 0);
755 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
756 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
759 * The child can't exit until the grandchild reports status, so the
760 * grandchild should report its exit first to the debugger.
762 * Even though the child process is detached, it is still a
763 * child of the debugger, so it will still report it's exit
764 * after the grandchild.
766 wpid = wait(&status);
767 ATF_REQUIRE(wpid == children[1]);
768 ATF_REQUIRE(WIFEXITED(status));
769 ATF_REQUIRE(WEXITSTATUS(status) == 2);
771 wpid = wait(&status);
772 ATF_REQUIRE(wpid == children[0]);
773 ATF_REQUIRE(WIFEXITED(status));
774 ATF_REQUIRE(WEXITSTATUS(status) == 1);
776 wpid = wait(&status);
777 ATF_REQUIRE(wpid == -1);
778 ATF_REQUIRE(errno == ECHILD);
782 attach_fork_parent(int cpipe[2])
788 /* Double-fork to disassociate from the debugger. */
789 CHILD_REQUIRE((fpid = fork()) != -1);
793 /* Send the pid of the disassociated child to the debugger. */
795 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
797 /* Wait for the debugger to attach. */
798 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
802 * Verify that a new child process is stopped after a followed fork and
803 * that the traced parent sees the exit of the child after the debugger
804 * when both processes remain attached to the debugger. In this test
805 * the parent that forks is not a direct child of the debugger.
807 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
808 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
810 pid_t children[2], fpid, wpid;
811 int cpipe[2], status;
813 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
814 atf_tc_skip("https://bugs.freebsd.org/239397");
816 ATF_REQUIRE(pipe(cpipe) == 0);
817 ATF_REQUIRE((fpid = fork()) != -1);
819 attach_fork_parent(cpipe);
820 follow_fork_parent(false);
823 /* Parent process. */
826 /* Wait for the direct child to exit. */
827 wpid = waitpid(fpid, &status, 0);
828 ATF_REQUIRE(wpid == fpid);
829 ATF_REQUIRE(WIFEXITED(status));
830 ATF_REQUIRE(WEXITSTATUS(status) == 3);
832 /* Read the pid of the fork parent. */
833 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
834 sizeof(children[0]));
836 /* Attach to the fork parent. */
837 attach_child(children[0]);
839 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
841 /* Continue the fork parent ignoring the SIGSTOP. */
842 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
844 /* Signal the fork parent to continue. */
847 children[1] = handle_fork_events(children[0], NULL);
848 ATF_REQUIRE(children[1] > 0);
850 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
851 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
854 * The fork parent can't exit until the child reports status,
855 * so the child should report its exit first to the debugger.
857 wpid = wait(&status);
858 ATF_REQUIRE(wpid == children[1]);
859 ATF_REQUIRE(WIFEXITED(status));
860 ATF_REQUIRE(WEXITSTATUS(status) == 2);
862 wpid = wait(&status);
863 ATF_REQUIRE(wpid == children[0]);
864 ATF_REQUIRE(WIFEXITED(status));
865 ATF_REQUIRE(WEXITSTATUS(status) == 1);
867 wpid = wait(&status);
868 ATF_REQUIRE(wpid == -1);
869 ATF_REQUIRE(errno == ECHILD);
873 * Verify that a new child process is stopped after a followed fork
874 * and that the traced parent sees the exit of the child when the new
875 * child process is detached after it reports its fork. In this test
876 * the parent that forks is not a direct child of the debugger.
878 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
879 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
881 pid_t children[2], fpid, wpid;
882 int cpipe[2], status;
884 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
885 atf_tc_skip("https://bugs.freebsd.org/239292");
887 ATF_REQUIRE(pipe(cpipe) == 0);
888 ATF_REQUIRE((fpid = fork()) != -1);
890 attach_fork_parent(cpipe);
891 follow_fork_parent(false);
894 /* Parent process. */
897 /* Wait for the direct child to exit. */
898 wpid = waitpid(fpid, &status, 0);
899 ATF_REQUIRE(wpid == fpid);
900 ATF_REQUIRE(WIFEXITED(status));
901 ATF_REQUIRE(WEXITSTATUS(status) == 3);
903 /* Read the pid of the fork parent. */
904 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
905 sizeof(children[0]));
907 /* Attach to the fork parent. */
908 attach_child(children[0]);
910 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
912 /* Continue the fork parent ignoring the SIGSTOP. */
913 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
915 /* Signal the fork parent to continue. */
918 children[1] = handle_fork_events(children[0], NULL);
919 ATF_REQUIRE(children[1] > 0);
921 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
922 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
925 * Should not see any status from the child now, only the fork
928 wpid = wait(&status);
929 ATF_REQUIRE(wpid == children[0]);
930 ATF_REQUIRE(WIFEXITED(status));
931 ATF_REQUIRE(WEXITSTATUS(status) == 1);
933 wpid = wait(&status);
934 ATF_REQUIRE(wpid == -1);
935 ATF_REQUIRE(errno == ECHILD);
939 * Verify that a new child process is stopped after a followed fork
940 * and that the traced parent sees the exit of the child when the
941 * traced parent is detached after the fork. In this test the parent
942 * that forks is not a direct child of the debugger.
944 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
945 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
947 pid_t children[2], fpid, wpid;
948 int cpipe[2], status;
950 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
951 atf_tc_skip("https://bugs.freebsd.org/239425");
953 ATF_REQUIRE(pipe(cpipe) == 0);
954 ATF_REQUIRE((fpid = fork()) != -1);
956 attach_fork_parent(cpipe);
957 follow_fork_parent(false);
960 /* Parent process. */
963 /* Wait for the direct child to exit. */
964 wpid = waitpid(fpid, &status, 0);
965 ATF_REQUIRE(wpid == fpid);
966 ATF_REQUIRE(WIFEXITED(status));
967 ATF_REQUIRE(WEXITSTATUS(status) == 3);
969 /* Read the pid of the fork parent. */
970 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
971 sizeof(children[0]));
973 /* Attach to the fork parent. */
974 attach_child(children[0]);
976 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
978 /* Continue the fork parent ignoring the SIGSTOP. */
979 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
981 /* Signal the fork parent to continue. */
984 children[1] = handle_fork_events(children[0], NULL);
985 ATF_REQUIRE(children[1] > 0);
987 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
988 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
991 * Should not see any status from the fork parent now, only
994 wpid = wait(&status);
995 ATF_REQUIRE(wpid == children[1]);
996 ATF_REQUIRE(WIFEXITED(status));
997 ATF_REQUIRE(WEXITSTATUS(status) == 2);
999 wpid = wait(&status);
1000 ATF_REQUIRE(wpid == -1);
1001 ATF_REQUIRE(errno == ECHILD);
1005 * Verify that a child process does not see an unrelated debugger as its
1006 * parent but sees its original parent process.
1008 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1009 ATF_TC_BODY(ptrace__getppid, tc)
1011 pid_t child, debugger, ppid, wpid;
1012 int cpipe[2], dpipe[2], status;
1015 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
1016 atf_tc_skip("https://bugs.freebsd.org/240510");
1019 ATF_REQUIRE(pipe(cpipe) == 0);
1020 ATF_REQUIRE((child = fork()) != -1);
1023 /* Child process. */
1026 /* Wait for parent to be ready. */
1027 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1029 /* Report the parent PID to the parent. */
1031 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1038 ATF_REQUIRE(pipe(dpipe) == 0);
1039 ATF_REQUIRE((debugger = fork()) != -1);
1041 if (debugger == 0) {
1042 /* Debugger process. */
1045 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1047 wpid = waitpid(child, &status, 0);
1048 CHILD_REQUIRE(wpid == child);
1049 CHILD_REQUIRE(WIFSTOPPED(status));
1050 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1052 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1054 /* Signal parent that debugger is attached. */
1055 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1057 /* Wait for traced child to exit. */
1058 wpid = waitpid(child, &status, 0);
1059 CHILD_REQUIRE(wpid == child);
1060 CHILD_REQUIRE(WIFEXITED(status));
1061 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1067 /* Parent process. */
1069 /* Wait for the debugger to attach to the child. */
1070 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1072 /* Release the child. */
1073 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1075 /* Read the parent PID from the child. */
1076 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1079 ATF_REQUIRE(ppid == getpid());
1081 /* Wait for the debugger. */
1082 wpid = waitpid(debugger, &status, 0);
1083 ATF_REQUIRE(wpid == debugger);
1084 ATF_REQUIRE(WIFEXITED(status));
1085 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1087 /* The child process should now be ready. */
1088 wpid = waitpid(child, &status, WNOHANG);
1089 ATF_REQUIRE(wpid == child);
1090 ATF_REQUIRE(WIFEXITED(status));
1091 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1095 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1096 * child process created via fork() reports the correct value.
1098 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1099 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1101 struct ptrace_lwpinfo pl[2];
1102 pid_t children[2], fpid, wpid;
1105 ATF_REQUIRE((fpid = fork()) != -1);
1108 follow_fork_parent(false);
1111 /* Parent process. */
1114 /* The first wait() should report the stop from SIGSTOP. */
1115 wpid = waitpid(children[0], &status, 0);
1116 ATF_REQUIRE(wpid == children[0]);
1117 ATF_REQUIRE(WIFSTOPPED(status));
1118 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1120 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1122 /* Continue the child ignoring the SIGSTOP. */
1123 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1125 /* Wait for both halves of the fork event to get reported. */
1126 children[1] = handle_fork_events(children[0], pl);
1127 ATF_REQUIRE(children[1] > 0);
1129 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1130 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1131 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1132 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1133 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1135 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1136 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1139 * The child can't exit until the grandchild reports status, so the
1140 * grandchild should report its exit first to the debugger.
1142 wpid = wait(&status);
1143 ATF_REQUIRE(wpid == children[1]);
1144 ATF_REQUIRE(WIFEXITED(status));
1145 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1147 wpid = wait(&status);
1148 ATF_REQUIRE(wpid == children[0]);
1149 ATF_REQUIRE(WIFEXITED(status));
1150 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1152 wpid = wait(&status);
1153 ATF_REQUIRE(wpid == -1);
1154 ATF_REQUIRE(errno == ECHILD);
1158 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1159 * child process created via vfork() reports the correct value.
1161 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1162 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1164 struct ptrace_lwpinfo pl[2];
1165 pid_t children[2], fpid, wpid;
1168 ATF_REQUIRE((fpid = fork()) != -1);
1171 follow_fork_parent(true);
1174 /* Parent process. */
1177 /* The first wait() should report the stop from SIGSTOP. */
1178 wpid = waitpid(children[0], &status, 0);
1179 ATF_REQUIRE(wpid == children[0]);
1180 ATF_REQUIRE(WIFSTOPPED(status));
1181 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1183 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1185 /* Continue the child ignoring the SIGSTOP. */
1186 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1188 /* Wait for both halves of the fork event to get reported. */
1189 children[1] = handle_fork_events(children[0], pl);
1190 ATF_REQUIRE(children[1] > 0);
1192 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1193 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1194 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1195 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1196 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1198 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1199 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1202 * The child can't exit until the grandchild reports status, so the
1203 * grandchild should report its exit first to the debugger.
1205 wpid = wait(&status);
1206 ATF_REQUIRE(wpid == children[1]);
1207 ATF_REQUIRE(WIFEXITED(status));
1208 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1210 wpid = wait(&status);
1211 ATF_REQUIRE(wpid == children[0]);
1212 ATF_REQUIRE(WIFEXITED(status));
1213 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1215 wpid = wait(&status);
1216 ATF_REQUIRE(wpid == -1);
1217 ATF_REQUIRE(errno == ECHILD);
1221 simple_thread(void *arg __unused)
1228 simple_thread_main(void)
1232 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1233 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1238 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1239 * thread reports the correct value.
1241 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1242 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1244 struct ptrace_lwpinfo pl;
1249 ATF_REQUIRE((fpid = fork()) != -1);
1252 simple_thread_main();
1255 /* The first wait() should report the stop from SIGSTOP. */
1256 wpid = waitpid(fpid, &status, 0);
1257 ATF_REQUIRE(wpid == fpid);
1258 ATF_REQUIRE(WIFSTOPPED(status));
1259 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1261 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1263 mainlwp = pl.pl_lwpid;
1266 * Continue the child ignoring the SIGSTOP and tracing all
1267 * system call exits.
1269 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1272 * Wait for the new thread to arrive. pthread_create() might
1273 * invoke any number of system calls. For now we just wait
1274 * for the new thread to arrive and make sure it reports a
1275 * valid system call code. If ptrace grows thread event
1276 * reporting then this test can be made more precise.
1279 wpid = waitpid(fpid, &status, 0);
1280 ATF_REQUIRE(wpid == fpid);
1281 ATF_REQUIRE(WIFSTOPPED(status));
1282 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1284 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1286 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1287 ATF_REQUIRE(pl.pl_syscall_code != 0);
1288 if (pl.pl_lwpid != mainlwp)
1289 /* New thread seen. */
1292 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1295 /* Wait for the child to exit. */
1296 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1298 wpid = waitpid(fpid, &status, 0);
1299 ATF_REQUIRE(wpid == fpid);
1300 if (WIFEXITED(status))
1303 ATF_REQUIRE(WIFSTOPPED(status));
1304 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1305 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1308 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1310 wpid = wait(&status);
1311 ATF_REQUIRE(wpid == -1);
1312 ATF_REQUIRE(errno == ECHILD);
1316 * Verify that the expected LWP events are reported for a child thread.
1318 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1319 ATF_TC_BODY(ptrace__lwp_events, tc)
1321 struct ptrace_lwpinfo pl;
1326 ATF_REQUIRE((fpid = fork()) != -1);
1329 simple_thread_main();
1332 /* The first wait() should report the stop from SIGSTOP. */
1333 wpid = waitpid(fpid, &status, 0);
1334 ATF_REQUIRE(wpid == fpid);
1335 ATF_REQUIRE(WIFSTOPPED(status));
1336 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1338 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1340 lwps[0] = pl.pl_lwpid;
1342 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1344 /* Continue the child ignoring the SIGSTOP. */
1345 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1347 /* The first event should be for the child thread's birth. */
1348 wpid = waitpid(fpid, &status, 0);
1349 ATF_REQUIRE(wpid == fpid);
1350 ATF_REQUIRE(WIFSTOPPED(status));
1351 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1353 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1354 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1355 (PL_FLAG_BORN | PL_FLAG_SCX));
1356 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1357 lwps[1] = pl.pl_lwpid;
1359 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1361 /* The next event should be for the child thread's death. */
1362 wpid = waitpid(fpid, &status, 0);
1363 ATF_REQUIRE(wpid == fpid);
1364 ATF_REQUIRE(WIFSTOPPED(status));
1365 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1367 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1368 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1369 (PL_FLAG_EXITED | PL_FLAG_SCE));
1370 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1372 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1374 /* The last event should be for the child process's exit. */
1375 wpid = waitpid(fpid, &status, 0);
1376 ATF_REQUIRE(WIFEXITED(status));
1377 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1379 wpid = wait(&status);
1380 ATF_REQUIRE(wpid == -1);
1381 ATF_REQUIRE(errno == ECHILD);
1385 exec_thread(void *arg __unused)
1388 execl("/usr/bin/true", "true", NULL);
1393 exec_thread_main(void)
1397 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1404 * Verify that the expected LWP events are reported for a multithreaded
1405 * process that calls execve(2).
1407 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1408 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1410 struct ptrace_lwpinfo pl;
1415 ATF_REQUIRE((fpid = fork()) != -1);
1421 /* The first wait() should report the stop from SIGSTOP. */
1422 wpid = waitpid(fpid, &status, 0);
1423 ATF_REQUIRE(wpid == fpid);
1424 ATF_REQUIRE(WIFSTOPPED(status));
1425 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1427 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1429 lwps[0] = pl.pl_lwpid;
1431 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1433 /* Continue the child ignoring the SIGSTOP. */
1434 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1436 /* The first event should be for the child thread's birth. */
1437 wpid = waitpid(fpid, &status, 0);
1438 ATF_REQUIRE(wpid == fpid);
1439 ATF_REQUIRE(WIFSTOPPED(status));
1440 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1442 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1443 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1444 (PL_FLAG_BORN | PL_FLAG_SCX));
1445 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1446 lwps[1] = pl.pl_lwpid;
1448 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1451 * The next event should be for the main thread's death due to
1452 * single threading from execve().
1454 wpid = waitpid(fpid, &status, 0);
1455 ATF_REQUIRE(wpid == fpid);
1456 ATF_REQUIRE(WIFSTOPPED(status));
1457 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1459 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1460 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1462 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1464 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1466 /* The next event should be for the child process's exec. */
1467 wpid = waitpid(fpid, &status, 0);
1468 ATF_REQUIRE(WIFSTOPPED(status));
1469 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1471 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1472 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1473 (PL_FLAG_EXEC | PL_FLAG_SCX));
1474 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1476 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1478 /* The last event should be for the child process's exit. */
1479 wpid = waitpid(fpid, &status, 0);
1480 ATF_REQUIRE(WIFEXITED(status));
1481 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1483 wpid = wait(&status);
1484 ATF_REQUIRE(wpid == -1);
1485 ATF_REQUIRE(errno == ECHILD);
1489 handler(int sig __unused)
1497 signal(SIGINFO, handler);
1503 * Verify that the expected ptrace event is reported for a signal.
1505 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1506 ATF_TC_BODY(ptrace__siginfo, tc)
1508 struct ptrace_lwpinfo pl;
1512 ATF_REQUIRE((fpid = fork()) != -1);
1518 /* The first wait() should report the stop from SIGSTOP. */
1519 wpid = waitpid(fpid, &status, 0);
1520 ATF_REQUIRE(wpid == fpid);
1521 ATF_REQUIRE(WIFSTOPPED(status));
1522 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1524 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1526 /* The next event should be for the SIGINFO. */
1527 wpid = waitpid(fpid, &status, 0);
1528 ATF_REQUIRE(WIFSTOPPED(status));
1529 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1531 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1532 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1533 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1534 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1535 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1537 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1539 /* The last event should be for the child process's exit. */
1540 wpid = waitpid(fpid, &status, 0);
1541 ATF_REQUIRE(WIFEXITED(status));
1542 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1544 wpid = wait(&status);
1545 ATF_REQUIRE(wpid == -1);
1546 ATF_REQUIRE(errno == ECHILD);
1550 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1552 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1553 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1558 ATF_REQUIRE((fpid = fork()) != -1);
1564 /* The first wait() should report the stop from SIGSTOP. */
1565 wpid = waitpid(fpid, &status, 0);
1566 ATF_REQUIRE(wpid == fpid);
1567 ATF_REQUIRE(WIFSTOPPED(status));
1568 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1571 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1572 sizeof(events)) == 0);
1574 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1576 /* Should get one event at exit. */
1577 wpid = waitpid(fpid, &status, 0);
1578 ATF_REQUIRE(WIFEXITED(status));
1579 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1581 wpid = wait(&status);
1582 ATF_REQUIRE(wpid == -1);
1583 ATF_REQUIRE(errno == ECHILD);
1586 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1587 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1589 struct ptrace_lwpinfo pl;
1593 ATF_REQUIRE((fpid = fork()) != -1);
1599 /* The first wait() should report the stop from SIGSTOP. */
1600 wpid = waitpid(fpid, &status, 0);
1601 ATF_REQUIRE(wpid == fpid);
1602 ATF_REQUIRE(WIFSTOPPED(status));
1603 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1605 events = PTRACE_EXEC;
1606 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1607 sizeof(events)) == 0);
1609 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1611 /* The next event should be for the child process's exec. */
1612 wpid = waitpid(fpid, &status, 0);
1613 ATF_REQUIRE(WIFSTOPPED(status));
1614 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1616 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1617 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1618 (PL_FLAG_EXEC | PL_FLAG_SCX));
1620 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1622 /* The last event should be for the child process's exit. */
1623 wpid = waitpid(fpid, &status, 0);
1624 ATF_REQUIRE(WIFEXITED(status));
1625 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1627 wpid = wait(&status);
1628 ATF_REQUIRE(wpid == -1);
1629 ATF_REQUIRE(errno == ECHILD);
1632 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1633 ATF_TC_BODY(ptrace__event_mask, tc)
1638 ATF_REQUIRE((fpid = fork()) != -1);
1644 /* The first wait() should report the stop from SIGSTOP. */
1645 wpid = waitpid(fpid, &status, 0);
1646 ATF_REQUIRE(wpid == fpid);
1647 ATF_REQUIRE(WIFSTOPPED(status));
1648 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1650 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1651 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1652 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1653 sizeof(events)) == 0);
1654 ATF_REQUIRE(events & PTRACE_FORK);
1655 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1656 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1657 sizeof(events)) == 0);
1658 ATF_REQUIRE(!(events & PTRACE_FORK));
1660 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1661 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1662 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1663 sizeof(events)) == 0);
1664 ATF_REQUIRE(events & PTRACE_LWP);
1665 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1666 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1667 sizeof(events)) == 0);
1668 ATF_REQUIRE(!(events & PTRACE_LWP));
1670 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1672 /* Should get one event at exit. */
1673 wpid = waitpid(fpid, &status, 0);
1674 ATF_REQUIRE(WIFEXITED(status));
1675 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1677 wpid = wait(&status);
1678 ATF_REQUIRE(wpid == -1);
1679 ATF_REQUIRE(errno == ECHILD);
1683 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1685 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1686 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1688 struct ptrace_lwpinfo pl;
1692 ATF_REQUIRE((fpid = fork()) != -1);
1695 follow_fork_parent(true);
1698 /* The first wait() should report the stop from SIGSTOP. */
1699 wpid = waitpid(fpid, &status, 0);
1700 ATF_REQUIRE(wpid == fpid);
1701 ATF_REQUIRE(WIFSTOPPED(status));
1702 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1704 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1705 sizeof(events)) == 0);
1706 events |= PTRACE_VFORK;
1707 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1708 sizeof(events)) == 0);
1710 /* Continue the child ignoring the SIGSTOP. */
1711 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1713 /* The next event should report the end of the vfork. */
1714 wpid = wait(&status);
1715 ATF_REQUIRE(wpid == fpid);
1716 ATF_REQUIRE(WIFSTOPPED(status));
1717 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1718 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1719 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1721 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1723 wpid = wait(&status);
1724 ATF_REQUIRE(wpid == fpid);
1725 ATF_REQUIRE(WIFEXITED(status));
1726 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1728 wpid = wait(&status);
1729 ATF_REQUIRE(wpid == -1);
1730 ATF_REQUIRE(errno == ECHILD);
1733 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1734 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1736 struct ptrace_lwpinfo pl[2];
1737 pid_t children[2], fpid, wpid;
1740 ATF_REQUIRE((fpid = fork()) != -1);
1743 follow_fork_parent(true);
1746 /* Parent process. */
1749 /* The first wait() should report the stop from SIGSTOP. */
1750 wpid = waitpid(children[0], &status, 0);
1751 ATF_REQUIRE(wpid == children[0]);
1752 ATF_REQUIRE(WIFSTOPPED(status));
1753 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1755 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1756 sizeof(events)) == 0);
1757 events |= PTRACE_FORK | PTRACE_VFORK;
1758 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1759 sizeof(events)) == 0);
1761 /* Continue the child ignoring the SIGSTOP. */
1762 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1764 /* Wait for both halves of the fork event to get reported. */
1765 children[1] = handle_fork_events(children[0], pl);
1766 ATF_REQUIRE(children[1] > 0);
1768 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1770 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1771 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1774 * The child can't exit until the grandchild reports status, so the
1775 * grandchild should report its exit first to the debugger.
1777 wpid = waitpid(children[1], &status, 0);
1778 ATF_REQUIRE(wpid == children[1]);
1779 ATF_REQUIRE(WIFEXITED(status));
1780 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1783 * The child should report it's vfork() completion before it
1786 wpid = wait(&status);
1787 ATF_REQUIRE(wpid == children[0]);
1788 ATF_REQUIRE(WIFSTOPPED(status));
1789 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1790 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1792 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1794 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1796 wpid = wait(&status);
1797 ATF_REQUIRE(wpid == children[0]);
1798 ATF_REQUIRE(WIFEXITED(status));
1799 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1801 wpid = wait(&status);
1802 ATF_REQUIRE(wpid == -1);
1803 ATF_REQUIRE(errno == ECHILD);
1806 #ifdef HAVE_BREAKPOINT
1808 * Verify that no more events are reported after PT_KILL except for the
1809 * process exit when stopped due to a breakpoint trap.
1811 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1812 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1817 ATF_REQUIRE((fpid = fork()) != -1);
1824 /* The first wait() should report the stop from SIGSTOP. */
1825 wpid = waitpid(fpid, &status, 0);
1826 ATF_REQUIRE(wpid == fpid);
1827 ATF_REQUIRE(WIFSTOPPED(status));
1828 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1830 /* Continue the child ignoring the SIGSTOP. */
1831 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1833 /* The second wait() should report hitting the breakpoint. */
1834 wpid = waitpid(fpid, &status, 0);
1835 ATF_REQUIRE(wpid == fpid);
1836 ATF_REQUIRE(WIFSTOPPED(status));
1837 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1839 /* Kill the child process. */
1840 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1842 /* The last wait() should report the SIGKILL. */
1843 wpid = waitpid(fpid, &status, 0);
1844 ATF_REQUIRE(wpid == fpid);
1845 ATF_REQUIRE(WIFSIGNALED(status));
1846 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1848 wpid = wait(&status);
1849 ATF_REQUIRE(wpid == -1);
1850 ATF_REQUIRE(errno == ECHILD);
1852 #endif /* HAVE_BREAKPOINT */
1855 * Verify that no more events are reported after PT_KILL except for the
1856 * process exit when stopped inside of a system call.
1858 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1859 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1861 struct ptrace_lwpinfo pl;
1865 ATF_REQUIRE((fpid = fork()) != -1);
1872 /* The first wait() should report the stop from SIGSTOP. */
1873 wpid = waitpid(fpid, &status, 0);
1874 ATF_REQUIRE(wpid == fpid);
1875 ATF_REQUIRE(WIFSTOPPED(status));
1876 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1878 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1879 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1881 /* The second wait() should report a system call entry for getpid(). */
1882 wpid = waitpid(fpid, &status, 0);
1883 ATF_REQUIRE(wpid == fpid);
1884 ATF_REQUIRE(WIFSTOPPED(status));
1885 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1887 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1888 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1890 /* Kill the child process. */
1891 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1893 /* The last wait() should report the SIGKILL. */
1894 wpid = waitpid(fpid, &status, 0);
1895 ATF_REQUIRE(wpid == fpid);
1896 ATF_REQUIRE(WIFSIGNALED(status));
1897 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1899 wpid = wait(&status);
1900 ATF_REQUIRE(wpid == -1);
1901 ATF_REQUIRE(errno == ECHILD);
1905 * Verify that no more events are reported after PT_KILL except for the
1906 * process exit when killing a multithreaded process.
1908 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1909 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1911 struct ptrace_lwpinfo pl;
1916 ATF_REQUIRE((fpid = fork()) != -1);
1919 simple_thread_main();
1922 /* The first wait() should report the stop from SIGSTOP. */
1923 wpid = waitpid(fpid, &status, 0);
1924 ATF_REQUIRE(wpid == fpid);
1925 ATF_REQUIRE(WIFSTOPPED(status));
1926 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1928 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1930 main_lwp = pl.pl_lwpid;
1932 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1934 /* Continue the child ignoring the SIGSTOP. */
1935 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1937 /* The first event should be for the child thread's birth. */
1938 wpid = waitpid(fpid, &status, 0);
1939 ATF_REQUIRE(wpid == fpid);
1940 ATF_REQUIRE(WIFSTOPPED(status));
1941 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1943 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1944 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1945 (PL_FLAG_BORN | PL_FLAG_SCX));
1946 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1948 /* Kill the child process. */
1949 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1951 /* The last wait() should report the SIGKILL. */
1952 wpid = waitpid(fpid, &status, 0);
1953 ATF_REQUIRE(wpid == fpid);
1954 ATF_REQUIRE(WIFSIGNALED(status));
1955 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1957 wpid = wait(&status);
1958 ATF_REQUIRE(wpid == -1);
1959 ATF_REQUIRE(errno == ECHILD);
1963 mask_usr1_thread(void *arg)
1965 pthread_barrier_t *pbarrier;
1968 pbarrier = (pthread_barrier_t*)arg;
1970 sigemptyset(&sigmask);
1971 sigaddset(&sigmask, SIGUSR1);
1972 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1974 /* Sync up with other thread after sigmask updated. */
1975 pthread_barrier_wait(pbarrier);
1984 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1985 * and prevents spurious stops due to those other signals.
1987 ATF_TC(ptrace__PT_KILL_competing_signal);
1988 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1991 atf_tc_set_md_var(tc, "require.user", "root");
1993 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1999 pthread_barrier_t barrier;
2000 struct sched_param sched_param;
2002 ATF_REQUIRE((fpid = fork()) != -1);
2004 /* Bind to one CPU so only one thread at a time will run. */
2006 CPU_SET(0, &setmask);
2008 CHILD_REQUIRE(cpuset(&setid) == 0);
2009 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2010 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2012 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2014 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2015 (void*)&barrier) == 0);
2018 * Give the main thread higher priority. The test always
2019 * assumes that, if both threads are able to run, the main
2020 * thread runs first.
2022 sched_param.sched_priority =
2023 (sched_get_priority_max(SCHED_FIFO) +
2024 sched_get_priority_min(SCHED_FIFO)) / 2;
2025 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2026 SCHED_FIFO, &sched_param) == 0);
2027 sched_param.sched_priority -= RQ_PPQ;
2028 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2029 &sched_param) == 0);
2032 sigemptyset(&sigmask);
2033 sigaddset(&sigmask, SIGUSR2);
2034 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2036 /* Sync up with other thread after sigmask updated. */
2037 pthread_barrier_wait(&barrier);
2047 /* The first wait() should report the stop from SIGSTOP. */
2048 wpid = waitpid(fpid, &status, 0);
2049 ATF_REQUIRE(wpid == fpid);
2050 ATF_REQUIRE(WIFSTOPPED(status));
2051 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2053 /* Continue the child ignoring the SIGSTOP. */
2054 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2056 /* Send a signal that only the second thread can handle. */
2057 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2059 /* The second wait() should report the SIGUSR2. */
2060 wpid = waitpid(fpid, &status, 0);
2061 ATF_REQUIRE(wpid == fpid);
2062 ATF_REQUIRE(WIFSTOPPED(status));
2063 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2065 /* Send a signal that only the first thread can handle. */
2066 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2068 /* Replace the SIGUSR2 with a kill. */
2069 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2071 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2072 wpid = waitpid(fpid, &status, 0);
2073 ATF_REQUIRE(wpid == fpid);
2074 ATF_REQUIRE(WIFSIGNALED(status));
2075 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2077 wpid = wait(&status);
2078 ATF_REQUIRE(wpid == -1);
2079 ATF_REQUIRE(errno == ECHILD);
2083 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2084 * and prevents spurious stops caused by those events.
2086 ATF_TC(ptrace__PT_KILL_competing_stop);
2087 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2090 atf_tc_set_md_var(tc, "require.user", "root");
2092 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2098 pthread_barrier_t barrier;
2100 struct ptrace_lwpinfo pl;
2101 struct sched_param sched_param;
2103 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
2104 atf_tc_skip("https://bugs.freebsd.org/220841");
2106 ATF_REQUIRE((fpid = fork()) != -1);
2110 /* Bind to one CPU so only one thread at a time will run. */
2112 CPU_SET(0, &setmask);
2114 CHILD_REQUIRE(cpuset(&setid) == 0);
2115 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2116 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2118 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2120 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2121 (void*)&barrier) == 0);
2124 * Give the main thread higher priority. The test always
2125 * assumes that, if both threads are able to run, the main
2126 * thread runs first.
2128 sched_param.sched_priority =
2129 (sched_get_priority_max(SCHED_FIFO) +
2130 sched_get_priority_min(SCHED_FIFO)) / 2;
2131 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2132 SCHED_FIFO, &sched_param) == 0);
2133 sched_param.sched_priority -= RQ_PPQ;
2134 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2135 &sched_param) == 0);
2138 sigemptyset(&sigmask);
2139 sigaddset(&sigmask, SIGUSR2);
2140 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2142 /* Sync up with other thread after sigmask updated. */
2143 pthread_barrier_wait(&barrier);
2145 /* Sync up with the test before doing the getpid(). */
2152 /* The first wait() should report the stop from SIGSTOP. */
2153 wpid = waitpid(fpid, &status, 0);
2154 ATF_REQUIRE(wpid == fpid);
2155 ATF_REQUIRE(WIFSTOPPED(status));
2156 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2158 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2159 main_lwp = pl.pl_lwpid;
2161 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2162 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2165 * Continue until child is done with setup, which is indicated with
2166 * SIGSTOP. Ignore system calls in the meantime.
2169 wpid = waitpid(fpid, &status, 0);
2170 ATF_REQUIRE(wpid == fpid);
2171 ATF_REQUIRE(WIFSTOPPED(status));
2172 if (WSTOPSIG(status) == SIGTRAP) {
2173 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2175 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2177 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2180 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2183 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2184 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2186 wpid = waitpid(fpid, &status, 0);
2187 ATF_REQUIRE(wpid == fpid);
2188 ATF_REQUIRE(WIFSTOPPED(status));
2189 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2191 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2193 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2194 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2195 /* Prevent the main thread from hitting its syscall exit for now. */
2196 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2199 * Proceed, allowing second thread to hit syscall exit for
2200 * pthread_barrier_wait().
2202 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2204 wpid = waitpid(fpid, &status, 0);
2205 ATF_REQUIRE(wpid == fpid);
2206 ATF_REQUIRE(WIFSTOPPED(status));
2207 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2209 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2211 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2212 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2214 /* Send a signal that only the second thread can handle. */
2215 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2217 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2219 /* The next wait() should report the SIGUSR2. */
2220 wpid = waitpid(fpid, &status, 0);
2221 ATF_REQUIRE(wpid == fpid);
2222 ATF_REQUIRE(WIFSTOPPED(status));
2223 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2225 /* Allow the main thread to try to finish its system call. */
2226 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2229 * At this point, the main thread is in the middle of a system call and
2230 * has been resumed. The second thread has taken a SIGUSR2 which will
2231 * be replaced with a SIGKILL below. The main thread will get to run
2232 * first. It should notice the kill request (even though the signal
2233 * replacement occurred in the other thread) and exit accordingly. It
2234 * should not stop for the system call exit event.
2237 /* Replace the SIGUSR2 with a kill. */
2238 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2240 /* The last wait() should report the SIGKILL (not a syscall exit). */
2241 wpid = waitpid(fpid, &status, 0);
2242 ATF_REQUIRE(wpid == fpid);
2243 ATF_REQUIRE(WIFSIGNALED(status));
2244 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2246 wpid = wait(&status);
2247 ATF_REQUIRE(wpid == -1);
2248 ATF_REQUIRE(errno == ECHILD);
2252 sigusr1_handler(int sig)
2255 CHILD_REQUIRE(sig == SIGUSR1);
2260 * Verify that even if the signal queue is full for a child process,
2261 * a PT_KILL will kill the process.
2263 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2264 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2268 int max_pending_per_proc;
2272 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2274 ATF_REQUIRE((fpid = fork()) != -1);
2280 /* The first wait() should report the stop from SIGSTOP. */
2281 wpid = waitpid(fpid, &status, 0);
2282 ATF_REQUIRE(wpid == fpid);
2283 ATF_REQUIRE(WIFSTOPPED(status));
2284 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2286 len = sizeof(max_pending_per_proc);
2287 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2288 &max_pending_per_proc, &len, NULL, 0) == 0);
2290 /* Fill the signal queue. */
2291 for (i = 0; i < max_pending_per_proc; ++i)
2292 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2294 /* Kill the child process. */
2295 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2297 /* The last wait() should report the SIGKILL. */
2298 wpid = waitpid(fpid, &status, 0);
2299 ATF_REQUIRE(wpid == fpid);
2300 ATF_REQUIRE(WIFSIGNALED(status));
2301 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2303 wpid = wait(&status);
2304 ATF_REQUIRE(wpid == -1);
2305 ATF_REQUIRE(errno == ECHILD);
2309 * Verify that when stopped at a system call entry, a signal can be
2310 * requested with PT_CONTINUE which will be delivered once the system
2313 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2314 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2316 struct ptrace_lwpinfo pl;
2320 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2322 ATF_REQUIRE((fpid = fork()) != -1);
2329 /* The first wait() should report the stop from SIGSTOP. */
2330 wpid = waitpid(fpid, &status, 0);
2331 ATF_REQUIRE(wpid == fpid);
2332 ATF_REQUIRE(WIFSTOPPED(status));
2333 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2335 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2336 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2338 /* The second wait() should report a system call entry for getpid(). */
2339 wpid = waitpid(fpid, &status, 0);
2340 ATF_REQUIRE(wpid == fpid);
2341 ATF_REQUIRE(WIFSTOPPED(status));
2342 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2344 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2345 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2347 /* Continue the child process with a signal. */
2348 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2352 * The last wait() should report exit 2, i.e., a normal _exit
2353 * from the signal handler. In the meantime, catch and proceed
2354 * past any syscall stops.
2356 wpid = waitpid(fpid, &status, 0);
2357 ATF_REQUIRE(wpid == fpid);
2358 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2359 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2360 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2361 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2363 ATF_REQUIRE(WIFEXITED(status));
2364 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2369 wpid = wait(&status);
2370 ATF_REQUIRE(wpid == -1);
2371 ATF_REQUIRE(errno == ECHILD);
2375 sigusr1_counting_handler(int sig)
2377 static int counter = 0;
2379 CHILD_REQUIRE(sig == SIGUSR1);
2386 * Verify that, when continuing from a stop at system call entry and exit,
2387 * a signal can be requested from both stops, and both will be delivered when
2388 * the system call is complete.
2390 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2391 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2393 struct ptrace_lwpinfo pl;
2397 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2399 ATF_REQUIRE((fpid = fork()) != -1);
2406 /* The first wait() should report the stop from SIGSTOP. */
2407 wpid = waitpid(fpid, &status, 0);
2408 ATF_REQUIRE(wpid == fpid);
2409 ATF_REQUIRE(WIFSTOPPED(status));
2410 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2412 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2413 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2415 /* The second wait() should report a system call entry for getpid(). */
2416 wpid = waitpid(fpid, &status, 0);
2417 ATF_REQUIRE(wpid == fpid);
2418 ATF_REQUIRE(WIFSTOPPED(status));
2419 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2421 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2422 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2424 /* Continue the child process with a signal. */
2425 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2427 /* The third wait() should report a system call exit for getpid(). */
2428 wpid = waitpid(fpid, &status, 0);
2429 ATF_REQUIRE(wpid == fpid);
2430 ATF_REQUIRE(WIFSTOPPED(status));
2431 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2433 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2434 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2436 /* Continue the child process with a signal. */
2437 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2441 * The last wait() should report exit 2, i.e., a normal _exit
2442 * from the signal handler. In the meantime, catch and proceed
2443 * past any syscall stops.
2445 wpid = waitpid(fpid, &status, 0);
2446 ATF_REQUIRE(wpid == fpid);
2447 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2448 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2449 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2450 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2452 ATF_REQUIRE(WIFEXITED(status));
2453 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2458 wpid = wait(&status);
2459 ATF_REQUIRE(wpid == -1);
2460 ATF_REQUIRE(errno == ECHILD);
2464 * Verify that even if the signal queue is full for a child process,
2465 * a PT_CONTINUE with a signal will not result in loss of that signal.
2467 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2468 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2472 int max_pending_per_proc;
2476 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2477 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2479 ATF_REQUIRE((fpid = fork()) != -1);
2485 /* The first wait() should report the stop from SIGSTOP. */
2486 wpid = waitpid(fpid, &status, 0);
2487 ATF_REQUIRE(wpid == fpid);
2488 ATF_REQUIRE(WIFSTOPPED(status));
2489 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2491 len = sizeof(max_pending_per_proc);
2492 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2493 &max_pending_per_proc, &len, NULL, 0) == 0);
2495 /* Fill the signal queue. */
2496 for (i = 0; i < max_pending_per_proc; ++i)
2497 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2499 /* Continue with signal. */
2500 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2503 wpid = waitpid(fpid, &status, 0);
2504 ATF_REQUIRE(wpid == fpid);
2505 if (WIFSTOPPED(status)) {
2506 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2507 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2510 * The last wait() should report normal _exit from the
2513 ATF_REQUIRE(WIFEXITED(status));
2514 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2519 wpid = wait(&status);
2520 ATF_REQUIRE(wpid == -1);
2521 ATF_REQUIRE(errno == ECHILD);
2524 static sem_t sigusr1_sem;
2525 static int got_usr1;
2528 sigusr1_sempost_handler(int sig __unused)
2532 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2536 * Verify that even if the signal queue is full for a child process,
2537 * and the signal is masked, a PT_CONTINUE with a signal will not
2538 * result in loss of that signal.
2540 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2541 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2543 struct ptrace_lwpinfo pl;
2546 int max_pending_per_proc;
2551 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2552 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2553 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2556 ATF_REQUIRE((fpid = fork()) != -1);
2558 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2559 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2560 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2563 CHILD_REQUIRE(got_usr1 == 0);
2565 /* Allow the pending SIGUSR1 in now. */
2566 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2567 /* Wait to receive the SIGUSR1. */
2569 err = sem_wait(&sigusr1_sem);
2570 CHILD_REQUIRE(err == 0 || errno == EINTR);
2571 } while (err != 0 && errno == EINTR);
2572 CHILD_REQUIRE(got_usr1 == 1);
2576 /* The first wait() should report the stop from SIGSTOP. */
2577 wpid = waitpid(fpid, &status, 0);
2578 ATF_REQUIRE(wpid == fpid);
2579 ATF_REQUIRE(WIFSTOPPED(status));
2580 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2582 len = sizeof(max_pending_per_proc);
2583 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2584 &max_pending_per_proc, &len, NULL, 0) == 0);
2586 /* Fill the signal queue. */
2587 for (i = 0; i < max_pending_per_proc; ++i)
2588 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2590 /* Continue with signal. */
2591 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2593 /* Collect and ignore all of the SIGUSR2. */
2594 for (i = 0; i < max_pending_per_proc; ++i) {
2595 wpid = waitpid(fpid, &status, 0);
2596 ATF_REQUIRE(wpid == fpid);
2597 ATF_REQUIRE(WIFSTOPPED(status));
2598 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2599 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2602 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2603 wpid = waitpid(fpid, &status, 0);
2604 ATF_REQUIRE(wpid == fpid);
2605 ATF_REQUIRE(WIFSTOPPED(status));
2606 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2607 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2608 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2610 /* Continue the child, ignoring the SIGUSR1. */
2611 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2613 /* The last wait() should report exit after receiving SIGUSR1. */
2614 wpid = waitpid(fpid, &status, 0);
2615 ATF_REQUIRE(wpid == fpid);
2616 ATF_REQUIRE(WIFEXITED(status));
2617 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2619 wpid = wait(&status);
2620 ATF_REQUIRE(wpid == -1);
2621 ATF_REQUIRE(errno == ECHILD);
2625 * Verify that, after stopping due to a signal, that signal can be
2626 * replaced with another signal.
2628 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2629 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2631 struct ptrace_lwpinfo pl;
2635 ATF_REQUIRE((fpid = fork()) != -1);
2642 /* The first wait() should report the stop from SIGSTOP. */
2643 wpid = waitpid(fpid, &status, 0);
2644 ATF_REQUIRE(wpid == fpid);
2645 ATF_REQUIRE(WIFSTOPPED(status));
2646 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2648 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2650 /* Send a signal without ptrace. */
2651 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2653 /* The second wait() should report a SIGINT was received. */
2654 wpid = waitpid(fpid, &status, 0);
2655 ATF_REQUIRE(wpid == fpid);
2656 ATF_REQUIRE(WIFSTOPPED(status));
2657 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2659 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2660 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2661 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2663 /* Continue the child process with a different signal. */
2664 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2667 * The last wait() should report having died due to the new
2670 wpid = waitpid(fpid, &status, 0);
2671 ATF_REQUIRE(wpid == fpid);
2672 ATF_REQUIRE(WIFSIGNALED(status));
2673 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2675 wpid = wait(&status);
2676 ATF_REQUIRE(wpid == -1);
2677 ATF_REQUIRE(errno == ECHILD);
2681 * Verify that a signal can be passed through to the child even when there
2682 * was no true signal originally. Such cases arise when a SIGTRAP is
2683 * invented for e.g, system call stops.
2685 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2686 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2688 struct ptrace_lwpinfo pl;
2693 ATF_REQUIRE((fpid = fork()) != -1);
2696 /* SIGTRAP expected to cause exit on syscall entry. */
2697 rl.rlim_cur = rl.rlim_max = 0;
2698 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2703 /* The first wait() should report the stop from SIGSTOP. */
2704 wpid = waitpid(fpid, &status, 0);
2705 ATF_REQUIRE(wpid == fpid);
2706 ATF_REQUIRE(WIFSTOPPED(status));
2707 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2709 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2710 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2712 /* The second wait() should report a system call entry for getpid(). */
2713 wpid = waitpid(fpid, &status, 0);
2714 ATF_REQUIRE(wpid == fpid);
2715 ATF_REQUIRE(WIFSTOPPED(status));
2716 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2718 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2719 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2721 /* Continue the child process with a SIGTRAP. */
2722 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2726 * The last wait() should report exit due to SIGTRAP. In the
2727 * meantime, catch and proceed past any syscall stops.
2729 wpid = waitpid(fpid, &status, 0);
2730 ATF_REQUIRE(wpid == fpid);
2731 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2732 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2733 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2734 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2736 ATF_REQUIRE(WIFSIGNALED(status));
2737 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2742 wpid = wait(&status);
2743 ATF_REQUIRE(wpid == -1);
2744 ATF_REQUIRE(errno == ECHILD);
2749 * A mixed bag PT_CONTINUE with signal test.
2751 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2752 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2754 struct ptrace_lwpinfo pl;
2758 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2760 ATF_REQUIRE((fpid = fork()) != -1);
2767 /* The first wait() should report the stop from SIGSTOP. */
2768 wpid = waitpid(fpid, &status, 0);
2769 ATF_REQUIRE(wpid == fpid);
2770 ATF_REQUIRE(WIFSTOPPED(status));
2771 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2773 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2774 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2776 /* The second wait() should report a system call entry for getpid(). */
2777 wpid = waitpid(fpid, &status, 0);
2778 ATF_REQUIRE(wpid == fpid);
2779 ATF_REQUIRE(WIFSTOPPED(status));
2780 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2782 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2783 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2785 /* Continue with the first SIGUSR1. */
2786 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2788 /* The next wait() should report a system call exit for getpid(). */
2789 wpid = waitpid(fpid, &status, 0);
2790 ATF_REQUIRE(wpid == fpid);
2791 ATF_REQUIRE(WIFSTOPPED(status));
2792 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2794 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2795 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2797 /* Send an ABRT without ptrace. */
2798 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2800 /* Continue normally. */
2801 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2803 /* The next wait() should report the SIGABRT. */
2804 wpid = waitpid(fpid, &status, 0);
2805 ATF_REQUIRE(wpid == fpid);
2806 ATF_REQUIRE(WIFSTOPPED(status));
2807 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2809 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2810 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2811 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2813 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2814 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2818 * The last wait() should report exit 2, i.e., a normal _exit
2819 * from the signal handler. In the meantime, catch and proceed
2820 * past any syscall stops.
2822 wpid = waitpid(fpid, &status, 0);
2823 ATF_REQUIRE(wpid == fpid);
2824 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2825 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2826 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2827 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2829 ATF_REQUIRE(WIFEXITED(status));
2830 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2835 wpid = wait(&status);
2836 ATF_REQUIRE(wpid == -1);
2837 ATF_REQUIRE(errno == ECHILD);
2842 * Verify a signal delivered by ptrace is noticed by kevent(2).
2844 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2845 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2848 int status, kq, nevents;
2851 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2853 ATF_REQUIRE((fpid = fork()) != -1);
2855 CHILD_REQUIRE((kq = kqueue()) > 0);
2856 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2857 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2862 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2863 if (nevents == -1 && errno == EINTR)
2865 CHILD_REQUIRE(nevents > 0);
2866 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2867 CHILD_REQUIRE(kev.ident == SIGUSR1);
2874 /* The first wait() should report the stop from SIGSTOP. */
2875 wpid = waitpid(fpid, &status, 0);
2876 ATF_REQUIRE(wpid == fpid);
2877 ATF_REQUIRE(WIFSTOPPED(status));
2878 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2880 /* Continue with the SIGUSR1. */
2881 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2884 * The last wait() should report normal exit with code 1.
2886 wpid = waitpid(fpid, &status, 0);
2887 ATF_REQUIRE(wpid == fpid);
2888 ATF_REQUIRE(WIFEXITED(status));
2889 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2891 wpid = wait(&status);
2892 ATF_REQUIRE(wpid == -1);
2893 ATF_REQUIRE(errno == ECHILD);
2897 signal_thread(void *arg)
2902 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2904 /* Wait for this thread to receive a SIGUSR1. */
2906 err = sem_wait(&sigusr1_sem);
2907 CHILD_REQUIRE(err == 0 || errno == EINTR);
2908 } while (err != 0 && errno == EINTR);
2910 /* Free our companion thread from the barrier. */
2911 pthread_barrier_wait(pbarrier);
2914 * Swap ignore duties; the next SIGUSR1 should go to the
2917 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2918 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2919 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2921 /* Sync up threads after swapping signal masks. */
2922 pthread_barrier_wait(pbarrier);
2924 /* Wait until our companion has received its SIGUSR1. */
2925 pthread_barrier_wait(pbarrier);
2931 * Verify that a traced process with blocked signal received the
2932 * signal from kill() once unmasked.
2934 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2935 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2937 struct ptrace_lwpinfo pl;
2942 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2943 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2946 ATF_REQUIRE((fpid = fork()) != -1);
2948 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2949 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2950 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2953 CHILD_REQUIRE(got_usr1 == 0);
2955 /* Allow the pending SIGUSR1 in now. */
2956 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2957 /* Wait to receive a SIGUSR1. */
2959 err = sem_wait(&sigusr1_sem);
2960 CHILD_REQUIRE(err == 0 || errno == EINTR);
2961 } while (err != 0 && errno == EINTR);
2962 CHILD_REQUIRE(got_usr1 == 1);
2966 /* The first wait() should report the stop from SIGSTOP. */
2967 wpid = waitpid(fpid, &status, 0);
2968 ATF_REQUIRE(wpid == fpid);
2969 ATF_REQUIRE(WIFSTOPPED(status));
2970 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2971 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2972 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2974 /* Send blocked SIGUSR1 which should cause a stop. */
2975 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2977 /* Continue the child ignoring the SIGSTOP. */
2978 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2980 /* The next wait() should report the kill(SIGUSR1) was received. */
2981 wpid = waitpid(fpid, &status, 0);
2982 ATF_REQUIRE(wpid == fpid);
2983 ATF_REQUIRE(WIFSTOPPED(status));
2984 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2985 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2986 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2988 /* Continue the child, allowing in the SIGUSR1. */
2989 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2991 /* The last wait() should report normal exit with code 1. */
2992 wpid = waitpid(fpid, &status, 0);
2993 ATF_REQUIRE(wpid == fpid);
2994 ATF_REQUIRE(WIFEXITED(status));
2995 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2997 wpid = wait(&status);
2998 ATF_REQUIRE(wpid == -1);
2999 ATF_REQUIRE(errno == ECHILD);
3003 * Verify that a traced process with blocked signal received the
3004 * signal from PT_CONTINUE once unmasked.
3006 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
3007 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
3009 struct ptrace_lwpinfo pl;
3014 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3015 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3018 ATF_REQUIRE((fpid = fork()) != -1);
3020 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3021 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3022 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3025 CHILD_REQUIRE(got_usr1 == 0);
3027 /* Allow the pending SIGUSR1 in now. */
3028 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3029 /* Wait to receive a SIGUSR1. */
3031 err = sem_wait(&sigusr1_sem);
3032 CHILD_REQUIRE(err == 0 || errno == EINTR);
3033 } while (err != 0 && errno == EINTR);
3035 CHILD_REQUIRE(got_usr1 == 1);
3039 /* The first wait() should report the stop from SIGSTOP. */
3040 wpid = waitpid(fpid, &status, 0);
3041 ATF_REQUIRE(wpid == fpid);
3042 ATF_REQUIRE(WIFSTOPPED(status));
3043 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3044 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3045 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3047 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3048 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3050 /* The next wait() should report the SIGUSR1 was received. */
3051 wpid = waitpid(fpid, &status, 0);
3052 ATF_REQUIRE(wpid == fpid);
3053 ATF_REQUIRE(WIFSTOPPED(status));
3054 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3055 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3056 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3058 /* Continue the child, ignoring the SIGUSR1. */
3059 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3061 /* The last wait() should report normal exit with code 1. */
3062 wpid = waitpid(fpid, &status, 0);
3063 ATF_REQUIRE(wpid == fpid);
3064 ATF_REQUIRE(WIFEXITED(status));
3065 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3067 wpid = wait(&status);
3068 ATF_REQUIRE(wpid == -1);
3069 ATF_REQUIRE(errno == ECHILD);
3073 * Verify that if ptrace stops due to a signal but continues with
3074 * a different signal that the new signal is routed to a thread
3075 * that can accept it, and that the thread is awakened by the signal
3076 * in a timely manner.
3078 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3079 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3085 pthread_barrier_t barrier;
3087 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3088 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3089 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3091 ATF_REQUIRE((fpid = fork()) != -1);
3093 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3095 /* The other thread should receive the first SIGUSR1. */
3096 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3097 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3098 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3102 /* Wait until other thread has received its SIGUSR1. */
3103 pthread_barrier_wait(&barrier);
3106 * Swap ignore duties; the next SIGUSR1 should go to this
3109 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3111 /* Sync up threads after swapping signal masks. */
3112 pthread_barrier_wait(&barrier);
3115 * Sync up with test code; we're ready for the next SIGUSR1
3120 /* Wait for this thread to receive a SIGUSR1. */
3122 err = sem_wait(&sigusr1_sem);
3123 CHILD_REQUIRE(err == 0 || errno == EINTR);
3124 } while (err != 0 && errno == EINTR);
3126 /* Free the other thread from the barrier. */
3127 pthread_barrier_wait(&barrier);
3129 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3134 /* The first wait() should report the stop from SIGSTOP. */
3135 wpid = waitpid(fpid, &status, 0);
3136 ATF_REQUIRE(wpid == fpid);
3137 ATF_REQUIRE(WIFSTOPPED(status));
3138 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3140 /* Continue the child ignoring the SIGSTOP. */
3141 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3144 * Send a signal without ptrace that either thread will accept (USR2,
3147 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3149 /* The second wait() should report a SIGUSR2 was received. */
3150 wpid = waitpid(fpid, &status, 0);
3151 ATF_REQUIRE(wpid == fpid);
3152 ATF_REQUIRE(WIFSTOPPED(status));
3153 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3155 /* Continue the child, changing the signal to USR1. */
3156 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3158 /* The next wait() should report the stop from SIGSTOP. */
3159 wpid = waitpid(fpid, &status, 0);
3160 ATF_REQUIRE(wpid == fpid);
3161 ATF_REQUIRE(WIFSTOPPED(status));
3162 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3164 /* Continue the child ignoring the SIGSTOP. */
3165 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3167 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3169 /* The next wait() should report a SIGUSR2 was received. */
3170 wpid = waitpid(fpid, &status, 0);
3171 ATF_REQUIRE(wpid == fpid);
3172 ATF_REQUIRE(WIFSTOPPED(status));
3173 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3175 /* Continue the child, changing the signal to USR1. */
3176 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3178 /* The last wait() should report normal exit with code 1. */
3179 wpid = waitpid(fpid, &status, 0);
3180 ATF_REQUIRE(wpid == fpid);
3181 ATF_REQUIRE(WIFEXITED(status));
3182 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3184 wpid = wait(&status);
3185 ATF_REQUIRE(wpid == -1);
3186 ATF_REQUIRE(errno == ECHILD);
3190 raise_sigstop_thread(void *arg __unused)
3198 sleep_thread(void *arg __unused)
3206 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3215 * Become the reaper for this process tree. We need to be able to check
3216 * that both child and grandchild have died.
3218 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3221 ATF_REQUIRE(fpid >= 0);
3224 CHILD_REQUIRE(fpid >= 0);
3228 /* Pin to CPU 0 to serialize thread execution. */
3230 CPU_SET(0, &setmask);
3231 CHILD_REQUIRE(cpuset(&setid) == 0);
3232 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3233 CPU_WHICH_CPUSET, setid,
3234 sizeof(setmask), &setmask) == 0);
3236 if (sigstop_from_main_thread) {
3238 * We expect the SIGKILL sent when our parent
3239 * dies to be delivered to the new thread.
3240 * Raise the SIGSTOP in this thread so the
3243 CHILD_REQUIRE(pthread_create(&t, NULL,
3244 sleep_thread, NULL) == 0);
3248 * We expect the SIGKILL to be delivered to
3249 * this thread. After creating the new thread,
3250 * just get off the CPU so the other thread can
3251 * raise the SIGSTOP.
3253 CHILD_REQUIRE(pthread_create(&t, NULL,
3254 raise_sigstop_thread, NULL) == 0);
3260 /* First stop is trace_me() immediately after fork. */
3261 wpid = waitpid(fpid, &status, 0);
3262 CHILD_REQUIRE(wpid == fpid);
3263 CHILD_REQUIRE(WIFSTOPPED(status));
3264 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3266 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3268 /* Second stop is from the raise(SIGSTOP). */
3269 wpid = waitpid(fpid, &status, 0);
3270 CHILD_REQUIRE(wpid == fpid);
3271 CHILD_REQUIRE(WIFSTOPPED(status));
3272 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3275 * Terminate tracing process without detaching. Our child
3282 * We should get a normal exit from our immediate child and a SIGKILL
3283 * exit from our grandchild. The latter case is the interesting one.
3284 * Our grandchild should not have stopped due to the SIGSTOP that was
3285 * left dangling when its parent died.
3287 for (i = 0; i < 2; ++i) {
3288 wpid = wait(&status);
3290 ATF_REQUIRE(WIFEXITED(status) &&
3291 WEXITSTATUS(status) == 0);
3293 ATF_REQUIRE(WIFSIGNALED(status) &&
3294 WTERMSIG(status) == SIGKILL);
3299 * These two tests ensure that if the tracing process exits without detaching
3300 * just after the child received a SIGSTOP, the child is cleanly killed and
3301 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3302 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3303 * different threads, the SIGKILL must win. There are two variants of this
3304 * test, designed to catch the case where the SIGKILL is delivered to the
3305 * younger thread (the first test) and the case where the SIGKILL is delivered
3306 * to the older thread (the second test). This behavior has changed in the
3307 * past, so make no assumption.
3309 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3310 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3313 atf_tc_set_md_var(tc, "require.user", "root");
3315 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3318 terminate_with_pending_sigstop(true);
3321 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3322 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3325 atf_tc_set_md_var(tc, "require.user", "root");
3327 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3330 terminate_with_pending_sigstop(false);
3334 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3337 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3338 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3340 struct ptrace_lwpinfo pl;
3342 int status, event_mask, new_event_mask;
3344 ATF_REQUIRE((fpid = fork()) != -1);
3351 /* The first wait() should report the stop from trace_me(). */
3352 wpid = waitpid(fpid, &status, 0);
3353 ATF_REQUIRE(wpid == fpid);
3354 ATF_REQUIRE(WIFSTOPPED(status));
3355 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3357 /* Set several unobtrusive event bits. */
3358 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3359 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3360 sizeof(event_mask)) == 0);
3362 /* Send a SIGKILL without using ptrace. */
3363 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3365 /* Continue the child ignoring the SIGSTOP. */
3366 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3368 /* The next stop should be due to the SIGKILL. */
3369 wpid = waitpid(fpid, &status, 0);
3370 ATF_REQUIRE(wpid == fpid);
3371 ATF_REQUIRE(WIFSTOPPED(status));
3372 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3374 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3375 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3376 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3378 /* Continue the child ignoring the SIGKILL. */
3379 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3381 /* The next wait() should report the stop from SIGSTOP. */
3382 wpid = waitpid(fpid, &status, 0);
3383 ATF_REQUIRE(wpid == fpid);
3384 ATF_REQUIRE(WIFSTOPPED(status));
3385 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3387 /* Check the current event mask. It should not have changed. */
3389 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3390 sizeof(new_event_mask)) == 0);
3391 ATF_REQUIRE(event_mask == new_event_mask);
3393 /* Continue the child to let it exit. */
3394 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3396 /* The last event should be for the child process's exit. */
3397 wpid = waitpid(fpid, &status, 0);
3398 ATF_REQUIRE(WIFEXITED(status));
3399 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3401 wpid = wait(&status);
3402 ATF_REQUIRE(wpid == -1);
3403 ATF_REQUIRE(errno == ECHILD);
3407 flock_thread(void *arg)
3412 (void)flock(fd, LOCK_EX);
3413 (void)flock(fd, LOCK_UN);
3418 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3419 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3420 * on a lock. This is a regression test for r318191.
3422 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3423 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3425 pthread_barrier_t barrier;
3426 pthread_barrierattr_t battr;
3429 int error, fd, i, status;
3431 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3432 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3433 PTHREAD_PROCESS_SHARED) == 0);
3434 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3436 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3437 fd = mkstemp(tmpfile);
3438 ATF_REQUIRE(fd >= 0);
3440 ATF_REQUIRE((child = fork()) != -1);
3445 error = pthread_barrier_wait(&barrier);
3446 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3449 cfd = open(tmpfile, O_RDONLY);
3454 * We want at least two threads blocked on the file lock since
3455 * the SIGSTOP from PT_ATTACH may kick one of them out of
3458 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3460 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3462 if (pthread_join(t[0], NULL) != 0)
3464 if (pthread_join(t[1], NULL) != 0)
3469 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3471 error = pthread_barrier_wait(&barrier);
3472 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3475 * Give the child some time to block. Is there a better way to do this?
3480 * Attach and give the child 3 seconds to stop.
3482 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3483 for (i = 0; i < 3; i++) {
3484 wpid = waitpid(child, &status, WNOHANG);
3485 if (wpid == child && WIFSTOPPED(status) &&
3486 WSTOPSIG(status) == SIGSTOP)
3490 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3492 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3494 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3495 ATF_REQUIRE(unlink(tmpfile) == 0);
3496 ATF_REQUIRE(close(fd) == 0);
3500 sigusr1_step_handler(int sig)
3503 CHILD_REQUIRE(sig == SIGUSR1);
3508 * Verify that PT_STEP with a signal invokes the signal before
3509 * stepping the next instruction (and that the next instruction is
3510 * stepped correctly).
3512 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3513 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3515 struct ptrace_lwpinfo pl;
3519 ATF_REQUIRE((fpid = fork()) != -1);
3522 signal(SIGUSR1, sigusr1_step_handler);
3527 /* The first wait() should report the stop from SIGSTOP. */
3528 wpid = waitpid(fpid, &status, 0);
3529 ATF_REQUIRE(wpid == fpid);
3530 ATF_REQUIRE(WIFSTOPPED(status));
3531 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3533 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3535 /* The next stop should report the SIGABRT in the child body. */
3536 wpid = waitpid(fpid, &status, 0);
3537 ATF_REQUIRE(wpid == fpid);
3538 ATF_REQUIRE(WIFSTOPPED(status));
3539 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3541 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3542 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3543 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3545 /* Step the child process inserting SIGUSR1. */
3546 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3548 /* The next stop should report the SIGABRT in the signal handler. */
3549 wpid = waitpid(fpid, &status, 0);
3550 ATF_REQUIRE(wpid == fpid);
3551 ATF_REQUIRE(WIFSTOPPED(status));
3552 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3554 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3555 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3556 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3558 /* Continue the child process discarding the signal. */
3559 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3561 /* The next stop should report a trace trap from PT_STEP. */
3562 wpid = waitpid(fpid, &status, 0);
3563 ATF_REQUIRE(wpid == fpid);
3564 ATF_REQUIRE(WIFSTOPPED(status));
3565 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3567 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3568 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3569 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3570 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3572 /* Continue the child to let it exit. */
3573 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3575 /* The last event should be for the child process's exit. */
3576 wpid = waitpid(fpid, &status, 0);
3577 ATF_REQUIRE(WIFEXITED(status));
3578 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3580 wpid = wait(&status);
3581 ATF_REQUIRE(wpid == -1);
3582 ATF_REQUIRE(errno == ECHILD);
3585 #ifdef HAVE_BREAKPOINT
3587 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3588 * for a breakpoint trap.
3590 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3591 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3593 struct ptrace_lwpinfo pl;
3597 ATF_REQUIRE((fpid = fork()) != -1);
3604 /* The first wait() should report the stop from SIGSTOP. */
3605 wpid = waitpid(fpid, &status, 0);
3606 ATF_REQUIRE(wpid == fpid);
3607 ATF_REQUIRE(WIFSTOPPED(status));
3608 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3610 /* Continue the child ignoring the SIGSTOP. */
3611 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3613 /* The second wait() should report hitting the breakpoint. */
3614 wpid = waitpid(fpid, &status, 0);
3615 ATF_REQUIRE(wpid == fpid);
3616 ATF_REQUIRE(WIFSTOPPED(status));
3617 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3619 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3620 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3621 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3622 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3624 /* Kill the child process. */
3625 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3627 /* The last wait() should report the SIGKILL. */
3628 wpid = waitpid(fpid, &status, 0);
3629 ATF_REQUIRE(wpid == fpid);
3630 ATF_REQUIRE(WIFSIGNALED(status));
3631 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3633 wpid = wait(&status);
3634 ATF_REQUIRE(wpid == -1);
3635 ATF_REQUIRE(errno == ECHILD);
3637 #endif /* HAVE_BREAKPOINT */
3640 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3641 * for a single-step trap from PT_STEP.
3643 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3644 ATF_TC_BODY(ptrace__step_siginfo, tc)
3646 struct ptrace_lwpinfo pl;
3650 ATF_REQUIRE((fpid = fork()) != -1);
3656 /* The first wait() should report the stop from SIGSTOP. */
3657 wpid = waitpid(fpid, &status, 0);
3658 ATF_REQUIRE(wpid == fpid);
3659 ATF_REQUIRE(WIFSTOPPED(status));
3660 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3662 /* Step the child ignoring the SIGSTOP. */
3663 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3665 /* The second wait() should report a single-step trap. */
3666 wpid = waitpid(fpid, &status, 0);
3667 ATF_REQUIRE(wpid == fpid);
3668 ATF_REQUIRE(WIFSTOPPED(status));
3669 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3671 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3672 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3673 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3674 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3676 /* Continue the child process. */
3677 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3679 /* The last event should be for the child process's exit. */
3680 wpid = waitpid(fpid, &status, 0);
3681 ATF_REQUIRE(WIFEXITED(status));
3682 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3684 wpid = wait(&status);
3685 ATF_REQUIRE(wpid == -1);
3686 ATF_REQUIRE(errno == ECHILD);
3689 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3691 continue_thread(void *arg __unused)
3698 continue_thread_main(void)
3700 pthread_t threads[2];
3702 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3704 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3706 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3707 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3712 * Ensure that PT_CONTINUE clears the status of the thread that
3713 * triggered the stop even if a different thread's LWP was passed to
3716 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3717 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3719 struct ptrace_lwpinfo pl;
3726 ATF_REQUIRE((fpid = fork()) != -1);
3729 continue_thread_main();
3732 /* The first wait() should report the stop from SIGSTOP. */
3733 wpid = waitpid(fpid, &status, 0);
3734 ATF_REQUIRE(wpid == fpid);
3735 ATF_REQUIRE(WIFSTOPPED(status));
3736 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3738 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3741 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3743 /* Continue the child ignoring the SIGSTOP. */
3744 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3746 /* One of the new threads should report it's birth. */
3747 wpid = waitpid(fpid, &status, 0);
3748 ATF_REQUIRE(wpid == fpid);
3749 ATF_REQUIRE(WIFSTOPPED(status));
3750 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3752 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3753 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3754 (PL_FLAG_BORN | PL_FLAG_SCX));
3755 lwps[0] = pl.pl_lwpid;
3758 * Suspend this thread to ensure both threads are alive before
3759 * hitting the breakpoint.
3761 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3763 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3765 /* Second thread should report it's birth. */
3766 wpid = waitpid(fpid, &status, 0);
3767 ATF_REQUIRE(wpid == fpid);
3768 ATF_REQUIRE(WIFSTOPPED(status));
3769 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3771 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3772 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3773 (PL_FLAG_BORN | PL_FLAG_SCX));
3774 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3775 lwps[1] = pl.pl_lwpid;
3777 /* Resume both threads waiting for breakpoint events. */
3778 hit_break[0] = hit_break[1] = false;
3779 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3780 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3782 /* One thread should report a breakpoint. */
3783 wpid = waitpid(fpid, &status, 0);
3784 ATF_REQUIRE(wpid == fpid);
3785 ATF_REQUIRE(WIFSTOPPED(status));
3786 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3788 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3789 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3790 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3791 pl.pl_siginfo.si_code == TRAP_BRKPT);
3792 if (pl.pl_lwpid == lwps[0])
3796 hit_break[i] = true;
3797 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3799 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3802 * Resume both threads but pass the other thread's LWPID to
3805 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3808 * Will now get two thread exit events and one more breakpoint
3811 for (j = 0; j < 3; j++) {
3812 wpid = waitpid(fpid, &status, 0);
3813 ATF_REQUIRE(wpid == fpid);
3814 ATF_REQUIRE(WIFSTOPPED(status));
3815 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3817 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3820 if (pl.pl_lwpid == lwps[0])
3825 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3826 if (pl.pl_flags & PL_FLAG_EXITED) {
3827 ATF_REQUIRE_MSG(hit_break[i],
3828 "exited thread did not report breakpoint");
3831 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3832 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3833 pl.pl_siginfo.si_code == TRAP_BRKPT);
3834 ATF_REQUIRE_MSG(!hit_break[i],
3835 "double breakpoint event");
3836 hit_break[i] = true;
3837 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3840 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3844 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3847 /* Both threads should have exited. */
3848 ATF_REQUIRE(lwps[0] == 0);
3849 ATF_REQUIRE(lwps[1] == 0);
3851 /* The last event should be for the child process's exit. */
3852 wpid = waitpid(fpid, &status, 0);
3853 ATF_REQUIRE(WIFEXITED(status));
3854 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3856 wpid = wait(&status);
3857 ATF_REQUIRE(wpid == -1);
3858 ATF_REQUIRE(errno == ECHILD);
3863 * Verify that PT_LWPINFO doesn't return stale siginfo.
3865 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3866 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3868 struct ptrace_lwpinfo pl;
3872 ATF_REQUIRE((fpid = fork()) != -1);
3879 /* The first wait() should report the stop from SIGSTOP. */
3880 wpid = waitpid(fpid, &status, 0);
3881 ATF_REQUIRE(wpid == fpid);
3882 ATF_REQUIRE(WIFSTOPPED(status));
3883 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3885 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3887 /* The next stop should report the SIGABRT in the child body. */
3888 wpid = waitpid(fpid, &status, 0);
3889 ATF_REQUIRE(wpid == fpid);
3890 ATF_REQUIRE(WIFSTOPPED(status));
3891 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3893 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3894 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3895 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3898 * Continue the process ignoring the signal, but enabling
3901 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3904 * The next stop should report a system call entry from
3905 * exit(). PL_FLAGS_SI should not be set.
3907 wpid = waitpid(fpid, &status, 0);
3908 ATF_REQUIRE(wpid == fpid);
3909 ATF_REQUIRE(WIFSTOPPED(status));
3910 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3912 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3913 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3914 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3916 /* Disable syscall tracing and continue the child to let it exit. */
3917 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3918 sizeof(events)) == 0);
3919 events &= ~PTRACE_SYSCALL;
3920 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3921 sizeof(events)) == 0);
3922 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3924 /* The last event should be for the child process's exit. */
3925 wpid = waitpid(fpid, &status, 0);
3926 ATF_REQUIRE(WIFEXITED(status));
3927 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3929 wpid = wait(&status);
3930 ATF_REQUIRE(wpid == -1);
3931 ATF_REQUIRE(errno == ECHILD);
3935 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3937 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3938 ATF_TC_BODY(ptrace__syscall_args, tc)
3940 struct ptrace_lwpinfo pl;
3941 struct ptrace_sc_ret psr;
3946 ATF_REQUIRE((fpid = fork()) != -1);
3954 /* The first wait() should report the stop from SIGSTOP. */
3955 wpid = waitpid(fpid, &status, 0);
3956 ATF_REQUIRE(wpid == fpid);
3957 ATF_REQUIRE(WIFSTOPPED(status));
3958 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3961 * Continue the process ignoring the signal, but enabling
3964 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3967 * The next stop should be the syscall entry from getpid().
3969 wpid = waitpid(fpid, &status, 0);
3970 ATF_REQUIRE(wpid == fpid);
3971 ATF_REQUIRE(WIFSTOPPED(status));
3972 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3974 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3975 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3976 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3978 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3981 * The next stop should be the syscall exit from getpid().
3983 wpid = waitpid(fpid, &status, 0);
3984 ATF_REQUIRE(wpid == fpid);
3985 ATF_REQUIRE(WIFSTOPPED(status));
3986 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3988 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3989 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
3990 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3992 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
3993 sizeof(psr)) != -1);
3994 ATF_REQUIRE(psr.sr_error == 0);
3995 ATF_REQUIRE(psr.sr_retval[0] == wpid);
3997 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4000 * The next stop should be the syscall entry from kill().
4002 wpid = waitpid(fpid, &status, 0);
4003 ATF_REQUIRE(wpid == fpid);
4004 ATF_REQUIRE(WIFSTOPPED(status));
4005 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4007 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4008 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4009 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
4010 ATF_REQUIRE(pl.pl_syscall_narg == 2);
4012 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4013 sizeof(args)) != -1);
4014 ATF_REQUIRE(args[0] == wpid);
4015 ATF_REQUIRE(args[1] == 0);
4017 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4020 * The next stop should be the syscall exit from kill().
4022 wpid = waitpid(fpid, &status, 0);
4023 ATF_REQUIRE(wpid == fpid);
4024 ATF_REQUIRE(WIFSTOPPED(status));
4025 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4027 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4028 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4029 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
4031 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4032 sizeof(psr)) != -1);
4033 ATF_REQUIRE(psr.sr_error == 0);
4035 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4038 * The next stop should be the syscall entry from close().
4040 wpid = waitpid(fpid, &status, 0);
4041 ATF_REQUIRE(wpid == fpid);
4042 ATF_REQUIRE(WIFSTOPPED(status));
4043 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4045 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4046 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4047 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4048 ATF_REQUIRE(pl.pl_syscall_narg == 1);
4050 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4051 sizeof(args)) != -1);
4052 ATF_REQUIRE(args[0] == 3);
4054 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4057 * The next stop should be the syscall exit from close().
4059 wpid = waitpid(fpid, &status, 0);
4060 ATF_REQUIRE(wpid == fpid);
4061 ATF_REQUIRE(WIFSTOPPED(status));
4062 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4064 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4065 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4066 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4068 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4069 sizeof(psr)) != -1);
4070 ATF_REQUIRE(psr.sr_error == EBADF);
4072 /* Disable syscall tracing and continue the child to let it exit. */
4073 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4074 sizeof(events)) == 0);
4075 events &= ~PTRACE_SYSCALL;
4076 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4077 sizeof(events)) == 0);
4078 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4080 /* The last event should be for the child process's exit. */
4081 wpid = waitpid(fpid, &status, 0);
4082 ATF_REQUIRE(WIFEXITED(status));
4083 ATF_REQUIRE(WEXITSTATUS(status) == 1);
4085 wpid = wait(&status);
4086 ATF_REQUIRE(wpid == -1);
4087 ATF_REQUIRE(errno == ECHILD);
4091 * Verify that when the process is traced that it isn't reparent
4092 * to the init process when we close all process descriptors.
4094 ATF_TC(ptrace__proc_reparent);
4095 ATF_TC_HEAD(ptrace__proc_reparent, tc)
4098 atf_tc_set_md_var(tc, "timeout", "2");
4100 ATF_TC_BODY(ptrace__proc_reparent, tc)
4102 pid_t traced, debuger, wpid;
4105 traced = pdfork(&pd, 0);
4106 ATF_REQUIRE(traced >= 0);
4111 ATF_REQUIRE(pd >= 0);
4114 ATF_REQUIRE(debuger >= 0);
4116 /* The traced process is reparented to debuger. */
4117 ATF_REQUIRE(ptrace(PT_ATTACH, traced, 0, 0) == 0);
4118 wpid = waitpid(traced, &status, 0);
4119 ATF_REQUIRE(wpid == traced);
4120 ATF_REQUIRE(WIFSTOPPED(status));
4121 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
4122 ATF_REQUIRE(close(pd) == 0);
4123 ATF_REQUIRE(ptrace(PT_DETACH, traced, (caddr_t)1, 0) == 0);
4125 /* We closed pd so we should not have any child. */
4126 wpid = wait(&status);
4127 ATF_REQUIRE(wpid == -1);
4128 ATF_REQUIRE(errno == ECHILD);
4133 ATF_REQUIRE(close(pd) == 0);
4134 wpid = waitpid(debuger, &status, 0);
4135 ATF_REQUIRE(wpid == debuger);
4136 ATF_REQUIRE(WEXITSTATUS(status) == 0);
4138 /* Check if we still have any child. */
4139 wpid = wait(&status);
4140 ATF_REQUIRE(wpid == -1);
4141 ATF_REQUIRE(errno == ECHILD);
4145 * Ensure that traced processes created with pdfork(2) are visible to
4148 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_wait_child);
4149 ATF_TC_BODY(ptrace__procdesc_wait_child, tc)
4154 child = pdfork(&pd, 0);
4155 ATF_REQUIRE(child >= 0);
4159 (void)raise(SIGSTOP);
4163 wpid = waitpid(child, &status, 0);
4164 ATF_REQUIRE(wpid == child);
4165 ATF_REQUIRE(WIFSTOPPED(status));
4166 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
4168 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4170 wpid = wait(&status);
4171 ATF_REQUIRE(wpid == child);
4172 ATF_REQUIRE(WIFSTOPPED(status));
4173 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
4175 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4178 * If process was created by pdfork, the return code have to
4179 * be collected through process descriptor.
4181 wpid = wait(&status);
4182 ATF_REQUIRE(wpid == -1);
4183 ATF_REQUIRE(errno == ECHILD);
4185 ATF_REQUIRE(close(pd) != -1);
4189 * Ensure that traced processes created with pdfork(2) are not visible
4190 * after returning to parent - waitid(P_ALL).
4192 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_reparent_wait_child);
4193 ATF_TC_BODY(ptrace__procdesc_reparent_wait_child, tc)
4195 pid_t traced, debuger, wpid;
4198 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
4199 atf_tc_skip("https://bugs.freebsd.org/243605");
4201 traced = pdfork(&pd, 0);
4202 ATF_REQUIRE(traced >= 0);
4207 ATF_REQUIRE(pd >= 0);
4210 ATF_REQUIRE(debuger >= 0);
4212 /* The traced process is reparented to debuger. */
4213 ATF_REQUIRE(ptrace(PT_ATTACH, traced, 0, 0) == 0);
4214 wpid = waitpid(traced, &status, 0);
4215 ATF_REQUIRE(wpid == traced);
4216 ATF_REQUIRE(WIFSTOPPED(status));
4217 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
4219 /* Allow process to die. */
4220 ATF_REQUIRE(ptrace(PT_CONTINUE, traced, (caddr_t)1, 0) == 0);
4221 wpid = waitpid(traced, &status, 0);
4222 ATF_REQUIRE(wpid == traced);
4223 ATF_REQUIRE(WIFEXITED(status));
4224 ATF_REQUIRE(WEXITSTATUS(status) == 0);
4226 /* Reparent back to the orginal process. */
4227 ATF_REQUIRE(close(pd) == 0);
4231 wpid = waitpid(debuger, &status, 0);
4232 ATF_REQUIRE(wpid == debuger);
4233 ATF_REQUIRE(WEXITSTATUS(status) == 0);
4236 * We have a child but it has a process descriptori
4237 * so we should not be able to collect it process.
4239 wpid = wait(&status);
4240 ATF_REQUIRE(wpid == -1);
4241 ATF_REQUIRE(errno == ECHILD);
4243 ATF_REQUIRE(close(pd) == 0);
4249 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4250 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4251 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4252 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4253 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4254 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4255 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4256 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4257 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4259 ptrace__follow_fork_child_detached_unrelated_debugger);
4261 ptrace__follow_fork_parent_detached_unrelated_debugger);
4262 ATF_TP_ADD_TC(tp, ptrace__getppid);
4263 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4264 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4265 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4266 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4267 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4268 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4269 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4270 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4271 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4272 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4273 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4274 #ifdef HAVE_BREAKPOINT
4275 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4277 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4278 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4279 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4280 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4281 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4282 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4284 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4285 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4286 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4287 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4288 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4289 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4290 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4291 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4292 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4293 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4294 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4295 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4296 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4297 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4298 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4299 #ifdef HAVE_BREAKPOINT
4300 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4302 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4303 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4304 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4306 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4307 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4308 ATF_TP_ADD_TC(tp, ptrace__proc_reparent);
4309 ATF_TP_ADD_TC(tp, ptrace__procdesc_wait_child);
4310 ATF_TP_ADD_TC(tp, ptrace__procdesc_reparent_wait_child);
4312 return (atf_no_error());