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 #include <sys/ptrace.h>
37 #include <sys/queue.h>
39 #include <sys/syscall.h>
40 #include <sys/sysctl.h>
44 #include <machine/cpufunc.h>
47 #include <semaphore.h>
55 * Architectures with a user-visible breakpoint().
57 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
58 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
60 #define HAVE_BREAKPOINT
64 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
66 #ifdef HAVE_BREAKPOINT
67 #if defined(__aarch64__)
68 #define SKIP_BREAK(reg) ((reg)->elr += 4)
69 #elif defined(__amd64__) || defined(__i386__)
70 #define SKIP_BREAK(reg)
71 #elif defined(__arm__)
72 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
73 #elif defined(__mips__)
74 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
75 #elif defined(__riscv)
76 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
77 #elif defined(__sparc64__)
78 #define SKIP_BREAK(reg) do { \
79 (reg)->r_tpc = (reg)->r_tnpc + 4; \
86 * A variant of ATF_REQUIRE that is suitable for use in child
87 * processes. This only works if the parent process is tripped up by
88 * the early exit and fails some requirement itself.
90 #define CHILD_REQUIRE(exp) do { \
92 child_fail_require(__FILE__, __LINE__, \
97 child_fail_require(const char *file, int line, const char *str)
101 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
102 write(2, buf, strlen(buf));
110 /* Attach the parent process as a tracer of this process. */
111 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
113 /* Trigger a stop. */
118 attach_child(pid_t pid)
123 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
125 wpid = waitpid(pid, &status, 0);
126 ATF_REQUIRE(wpid == pid);
127 ATF_REQUIRE(WIFSTOPPED(status));
128 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
132 wait_for_zombie(pid_t pid)
136 * Wait for a process to exit. This is kind of gross, but
137 * there is not a better way.
139 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
140 * with ESRCH. After that change, a valid struct kinfo_proc
141 * is returned for zombies with ki_stat set to SZOMB.
144 struct kinfo_proc kp;
150 mib[2] = KERN_PROC_PID;
153 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
154 ATF_REQUIRE(errno == ESRCH);
157 if (kp.ki_stat == SZOMB)
164 * Verify that a parent debugger process "sees" the exit of a debugged
165 * process exactly once when attached via PT_TRACE_ME.
167 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
168 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
173 ATF_REQUIRE((child = fork()) != -1);
181 /* Parent process. */
183 /* The first wait() should report the stop from SIGSTOP. */
184 wpid = waitpid(child, &status, 0);
185 ATF_REQUIRE(wpid == child);
186 ATF_REQUIRE(WIFSTOPPED(status));
187 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
189 /* Continue the child ignoring the SIGSTOP. */
190 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
192 /* The second wait() should report the exit status. */
193 wpid = waitpid(child, &status, 0);
194 ATF_REQUIRE(wpid == child);
195 ATF_REQUIRE(WIFEXITED(status));
196 ATF_REQUIRE(WEXITSTATUS(status) == 1);
198 /* The child should no longer exist. */
199 wpid = waitpid(child, &status, 0);
200 ATF_REQUIRE(wpid == -1);
201 ATF_REQUIRE(errno == ECHILD);
205 * Verify that a parent debugger process "sees" the exit of a debugged
206 * process exactly once when attached via PT_ATTACH.
208 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
209 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
212 int cpipe[2], status;
215 ATF_REQUIRE(pipe(cpipe) == 0);
216 ATF_REQUIRE((child = fork()) != -1);
221 /* Wait for the parent to attach. */
222 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
228 /* Parent process. */
230 /* Attach to the child process. */
233 /* Continue the child ignoring the SIGSTOP. */
234 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
236 /* Signal the child to exit. */
239 /* The second wait() should report the exit status. */
240 wpid = waitpid(child, &status, 0);
241 ATF_REQUIRE(wpid == child);
242 ATF_REQUIRE(WIFEXITED(status));
243 ATF_REQUIRE(WEXITSTATUS(status) == 1);
245 /* The child should no longer exist. */
246 wpid = waitpid(child, &status, 0);
247 ATF_REQUIRE(wpid == -1);
248 ATF_REQUIRE(errno == ECHILD);
252 * Verify that a parent process "sees" the exit of a debugged process only
253 * after the debugger has seen it.
255 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
256 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
258 pid_t child, debugger, wpid;
259 int cpipe[2], dpipe[2], status;
262 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
263 atf_tc_skip("https://bugs.freebsd.org/239399");
265 ATF_REQUIRE(pipe(cpipe) == 0);
266 ATF_REQUIRE((child = fork()) != -1);
272 /* Wait for parent to be ready. */
273 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
279 ATF_REQUIRE(pipe(dpipe) == 0);
280 ATF_REQUIRE((debugger = fork()) != -1);
283 /* Debugger process. */
286 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
288 wpid = waitpid(child, &status, 0);
289 CHILD_REQUIRE(wpid == child);
290 CHILD_REQUIRE(WIFSTOPPED(status));
291 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
293 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
295 /* Signal parent that debugger is attached. */
296 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
298 /* Wait for parent's failed wait. */
299 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
301 wpid = waitpid(child, &status, 0);
302 CHILD_REQUIRE(wpid == child);
303 CHILD_REQUIRE(WIFEXITED(status));
304 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
310 /* Parent process. */
312 /* Wait for the debugger to attach to the child. */
313 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
315 /* Release the child. */
316 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
317 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
320 wait_for_zombie(child);
323 * This wait should return a pid of 0 to indicate no status to
324 * report. The parent should see the child as non-exited
325 * until the debugger sees the exit.
327 wpid = waitpid(child, &status, WNOHANG);
328 ATF_REQUIRE(wpid == 0);
330 /* Signal the debugger to wait for the child. */
333 /* Wait for the debugger. */
334 wpid = waitpid(debugger, &status, 0);
335 ATF_REQUIRE(wpid == debugger);
336 ATF_REQUIRE(WIFEXITED(status));
337 ATF_REQUIRE(WEXITSTATUS(status) == 0);
339 /* The child process should now be ready. */
340 wpid = waitpid(child, &status, WNOHANG);
341 ATF_REQUIRE(wpid == child);
342 ATF_REQUIRE(WIFEXITED(status));
343 ATF_REQUIRE(WEXITSTATUS(status) == 1);
347 * Verify that a parent process "sees" the exit of a debugged process
348 * only after a non-direct-child debugger has seen it. In particular,
349 * various wait() calls in the parent must avoid failing with ESRCH by
350 * checking the parent's orphan list for the debugee.
352 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
353 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
355 pid_t child, debugger, fpid, wpid;
356 int cpipe[2], dpipe[2], status;
359 ATF_REQUIRE(pipe(cpipe) == 0);
360 ATF_REQUIRE((child = fork()) != -1);
366 /* Wait for parent to be ready. */
367 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
373 ATF_REQUIRE(pipe(dpipe) == 0);
374 ATF_REQUIRE((debugger = fork()) != -1);
377 /* Debugger parent. */
380 * Fork again and drop the debugger parent so that the
381 * debugger is not a child of the main parent.
383 CHILD_REQUIRE((fpid = fork()) != -1);
387 /* Debugger process. */
390 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
392 wpid = waitpid(child, &status, 0);
393 CHILD_REQUIRE(wpid == child);
394 CHILD_REQUIRE(WIFSTOPPED(status));
395 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
397 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
399 /* Signal parent that debugger is attached. */
400 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
402 /* Wait for parent's failed wait. */
403 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
405 wpid = waitpid(child, &status, 0);
406 CHILD_REQUIRE(wpid == child);
407 CHILD_REQUIRE(WIFEXITED(status));
408 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
414 /* Parent process. */
416 /* Wait for the debugger parent process to exit. */
417 wpid = waitpid(debugger, &status, 0);
418 ATF_REQUIRE(wpid == debugger);
419 ATF_REQUIRE(WIFEXITED(status));
420 ATF_REQUIRE(WEXITSTATUS(status) == 2);
422 /* A WNOHANG wait here should see the non-exited child. */
423 wpid = waitpid(child, &status, WNOHANG);
424 ATF_REQUIRE(wpid == 0);
426 /* Wait for the debugger to attach to the child. */
427 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
429 /* Release the child. */
430 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
431 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
434 wait_for_zombie(child);
437 * This wait should return a pid of 0 to indicate no status to
438 * report. The parent should see the child as non-exited
439 * until the debugger sees the exit.
441 wpid = waitpid(child, &status, WNOHANG);
442 ATF_REQUIRE(wpid == 0);
444 /* Signal the debugger to wait for the child. */
445 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
447 /* Wait for the debugger. */
448 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
451 /* The child process should now be ready. */
452 wpid = waitpid(child, &status, WNOHANG);
453 ATF_REQUIRE(wpid == child);
454 ATF_REQUIRE(WIFEXITED(status));
455 ATF_REQUIRE(WEXITSTATUS(status) == 1);
459 * Make sure that we can collect the exit status of an orphaned process.
461 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
462 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
465 int cpipe1[2], cpipe2[2], gcpipe[2], status;
468 ATF_REQUIRE(pipe(cpipe1) == 0);
469 ATF_REQUIRE(pipe(cpipe2) == 0);
470 ATF_REQUIRE(pipe(gcpipe) == 0);
472 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
474 ATF_REQUIRE((child = fork()) != -1);
476 CHILD_REQUIRE((gchild = fork()) != -1);
480 n = read(gcpipe[0], &status, sizeof(status));
481 } while (n == -1 && errno == EINTR);
485 CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
487 CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
492 ATF_REQUIRE(read(cpipe1[0], &gchild, sizeof(gchild)) == sizeof(gchild));
494 ATF_REQUIRE(ptrace(PT_ATTACH, gchild, NULL, 0) == 0);
497 ATF_REQUIRE(write(cpipe2[1], &status, sizeof(status)) ==
499 ATF_REQUIRE(waitpid(child, &status, 0) == child);
500 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
503 ATF_REQUIRE(write(gcpipe[1], &status, sizeof(status)) ==
505 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
506 ATF_REQUIRE(WIFSTOPPED(status));
507 ATF_REQUIRE(ptrace(PT_DETACH, gchild, (caddr_t)1, 0) == 0);
508 ATF_REQUIRE(waitpid(gchild, &status, 0) == gchild);
509 ATF_REQUIRE(WIFEXITED(status) && WEXITSTATUS(status) == 0);
511 ATF_REQUIRE(close(cpipe1[0]) == 0);
512 ATF_REQUIRE(close(cpipe1[1]) == 0);
513 ATF_REQUIRE(close(cpipe2[0]) == 0);
514 ATF_REQUIRE(close(cpipe2[1]) == 0);
515 ATF_REQUIRE(close(gcpipe[0]) == 0);
516 ATF_REQUIRE(close(gcpipe[1]) == 0);
520 * The parent process should always act the same regardless of how the
521 * debugger is attached to it.
524 follow_fork_parent(bool use_vfork)
530 CHILD_REQUIRE((fpid = vfork()) != -1);
532 CHILD_REQUIRE((fpid = fork()) != -1);
538 wpid = waitpid(fpid, &status, 0);
539 CHILD_REQUIRE(wpid == fpid);
540 CHILD_REQUIRE(WIFEXITED(status));
541 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
547 * Helper routine for follow fork tests. This waits for two stops
548 * that report both "sides" of a fork. It returns the pid of the new
552 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
554 struct ptrace_lwpinfo pl;
555 bool fork_reported[2];
559 fork_reported[0] = false;
560 fork_reported[1] = false;
564 * Each process should report a fork event. The parent should
565 * report a PL_FLAG_FORKED event, and the child should report
566 * a PL_FLAG_CHILD event.
568 for (i = 0; i < 2; i++) {
569 wpid = wait(&status);
570 ATF_REQUIRE(wpid > 0);
571 ATF_REQUIRE(WIFSTOPPED(status));
573 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
575 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
577 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
578 (PL_FLAG_FORKED | PL_FLAG_CHILD));
579 if (pl.pl_flags & PL_FLAG_CHILD) {
580 ATF_REQUIRE(wpid != parent);
581 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
582 ATF_REQUIRE(!fork_reported[1]);
586 ATF_REQUIRE(child == wpid);
589 fork_reported[1] = true;
591 ATF_REQUIRE(wpid == parent);
592 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
593 ATF_REQUIRE(!fork_reported[0]);
595 child = pl.pl_child_pid;
597 ATF_REQUIRE(child == pl.pl_child_pid);
600 fork_reported[0] = true;
608 * Verify that a new child process is stopped after a followed fork and
609 * that the traced parent sees the exit of the child after the debugger
610 * when both processes remain attached to the debugger.
612 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
613 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
615 pid_t children[2], fpid, wpid;
618 ATF_REQUIRE((fpid = fork()) != -1);
621 follow_fork_parent(false);
624 /* Parent process. */
627 /* The first wait() should report the stop from SIGSTOP. */
628 wpid = waitpid(children[0], &status, 0);
629 ATF_REQUIRE(wpid == children[0]);
630 ATF_REQUIRE(WIFSTOPPED(status));
631 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
633 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
635 /* Continue the child ignoring the SIGSTOP. */
636 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
638 children[1] = handle_fork_events(children[0], NULL);
639 ATF_REQUIRE(children[1] > 0);
641 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
642 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
645 * The child can't exit until the grandchild reports status, so the
646 * grandchild should report its exit first to the debugger.
648 wpid = wait(&status);
649 ATF_REQUIRE(wpid == children[1]);
650 ATF_REQUIRE(WIFEXITED(status));
651 ATF_REQUIRE(WEXITSTATUS(status) == 2);
653 wpid = wait(&status);
654 ATF_REQUIRE(wpid == children[0]);
655 ATF_REQUIRE(WIFEXITED(status));
656 ATF_REQUIRE(WEXITSTATUS(status) == 1);
658 wpid = wait(&status);
659 ATF_REQUIRE(wpid == -1);
660 ATF_REQUIRE(errno == ECHILD);
664 * Verify that a new child process is stopped after a followed fork
665 * and that the traced parent sees the exit of the child when the new
666 * child process is detached after it reports its fork.
668 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
669 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
671 pid_t children[2], fpid, wpid;
674 ATF_REQUIRE((fpid = fork()) != -1);
677 follow_fork_parent(false);
680 /* Parent process. */
683 /* The first wait() should report the stop from SIGSTOP. */
684 wpid = waitpid(children[0], &status, 0);
685 ATF_REQUIRE(wpid == children[0]);
686 ATF_REQUIRE(WIFSTOPPED(status));
687 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
689 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
691 /* Continue the child ignoring the SIGSTOP. */
692 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
694 children[1] = handle_fork_events(children[0], NULL);
695 ATF_REQUIRE(children[1] > 0);
697 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
698 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
701 * Should not see any status from the grandchild now, only the
704 wpid = wait(&status);
705 ATF_REQUIRE(wpid == children[0]);
706 ATF_REQUIRE(WIFEXITED(status));
707 ATF_REQUIRE(WEXITSTATUS(status) == 1);
709 wpid = wait(&status);
710 ATF_REQUIRE(wpid == -1);
711 ATF_REQUIRE(errno == ECHILD);
715 * Verify that a new child process is stopped after a followed fork
716 * and that the traced parent sees the exit of the child when the
717 * traced parent is detached after the fork.
719 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
720 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
722 pid_t children[2], fpid, wpid;
725 ATF_REQUIRE((fpid = fork()) != -1);
728 follow_fork_parent(false);
731 /* Parent process. */
734 /* The first wait() should report the stop from SIGSTOP. */
735 wpid = waitpid(children[0], &status, 0);
736 ATF_REQUIRE(wpid == children[0]);
737 ATF_REQUIRE(WIFSTOPPED(status));
738 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
740 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
742 /* Continue the child ignoring the SIGSTOP. */
743 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
745 children[1] = handle_fork_events(children[0], NULL);
746 ATF_REQUIRE(children[1] > 0);
748 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
749 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
752 * The child can't exit until the grandchild reports status, so the
753 * grandchild should report its exit first to the debugger.
755 * Even though the child process is detached, it is still a
756 * child of the debugger, so it will still report it's exit
757 * after the grandchild.
759 wpid = wait(&status);
760 ATF_REQUIRE(wpid == children[1]);
761 ATF_REQUIRE(WIFEXITED(status));
762 ATF_REQUIRE(WEXITSTATUS(status) == 2);
764 wpid = wait(&status);
765 ATF_REQUIRE(wpid == children[0]);
766 ATF_REQUIRE(WIFEXITED(status));
767 ATF_REQUIRE(WEXITSTATUS(status) == 1);
769 wpid = wait(&status);
770 ATF_REQUIRE(wpid == -1);
771 ATF_REQUIRE(errno == ECHILD);
775 attach_fork_parent(int cpipe[2])
781 /* Double-fork to disassociate from the debugger. */
782 CHILD_REQUIRE((fpid = fork()) != -1);
786 /* Send the pid of the disassociated child to the debugger. */
788 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
790 /* Wait for the debugger to attach. */
791 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
795 * Verify that a new child process is stopped after a followed fork and
796 * that the traced parent sees the exit of the child after the debugger
797 * when both processes remain attached to the debugger. In this test
798 * the parent that forks is not a direct child of the debugger.
800 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
801 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
803 pid_t children[2], fpid, wpid;
804 int cpipe[2], status;
806 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
807 atf_tc_skip("https://bugs.freebsd.org/239397");
809 ATF_REQUIRE(pipe(cpipe) == 0);
810 ATF_REQUIRE((fpid = fork()) != -1);
812 attach_fork_parent(cpipe);
813 follow_fork_parent(false);
816 /* Parent process. */
819 /* Wait for the direct child to exit. */
820 wpid = waitpid(fpid, &status, 0);
821 ATF_REQUIRE(wpid == fpid);
822 ATF_REQUIRE(WIFEXITED(status));
823 ATF_REQUIRE(WEXITSTATUS(status) == 3);
825 /* Read the pid of the fork parent. */
826 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
827 sizeof(children[0]));
829 /* Attach to the fork parent. */
830 attach_child(children[0]);
832 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
834 /* Continue the fork parent ignoring the SIGSTOP. */
835 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
837 /* Signal the fork parent to continue. */
840 children[1] = handle_fork_events(children[0], NULL);
841 ATF_REQUIRE(children[1] > 0);
843 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
844 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
847 * The fork parent can't exit until the child reports status,
848 * so the child should report its exit first to the debugger.
850 wpid = wait(&status);
851 ATF_REQUIRE(wpid == children[1]);
852 ATF_REQUIRE(WIFEXITED(status));
853 ATF_REQUIRE(WEXITSTATUS(status) == 2);
855 wpid = wait(&status);
856 ATF_REQUIRE(wpid == children[0]);
857 ATF_REQUIRE(WIFEXITED(status));
858 ATF_REQUIRE(WEXITSTATUS(status) == 1);
860 wpid = wait(&status);
861 ATF_REQUIRE(wpid == -1);
862 ATF_REQUIRE(errno == ECHILD);
866 * Verify that a new child process is stopped after a followed fork
867 * and that the traced parent sees the exit of the child when the new
868 * child process is detached after it reports its fork. In this test
869 * the parent that forks is not a direct child of the debugger.
871 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
872 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
874 pid_t children[2], fpid, wpid;
875 int cpipe[2], status;
877 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
878 atf_tc_skip("https://bugs.freebsd.org/239292");
880 ATF_REQUIRE(pipe(cpipe) == 0);
881 ATF_REQUIRE((fpid = fork()) != -1);
883 attach_fork_parent(cpipe);
884 follow_fork_parent(false);
887 /* Parent process. */
890 /* Wait for the direct child to exit. */
891 wpid = waitpid(fpid, &status, 0);
892 ATF_REQUIRE(wpid == fpid);
893 ATF_REQUIRE(WIFEXITED(status));
894 ATF_REQUIRE(WEXITSTATUS(status) == 3);
896 /* Read the pid of the fork parent. */
897 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
898 sizeof(children[0]));
900 /* Attach to the fork parent. */
901 attach_child(children[0]);
903 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
905 /* Continue the fork parent ignoring the SIGSTOP. */
906 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
908 /* Signal the fork parent to continue. */
911 children[1] = handle_fork_events(children[0], NULL);
912 ATF_REQUIRE(children[1] > 0);
914 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
915 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
918 * Should not see any status from the child now, only the fork
921 wpid = wait(&status);
922 ATF_REQUIRE(wpid == children[0]);
923 ATF_REQUIRE(WIFEXITED(status));
924 ATF_REQUIRE(WEXITSTATUS(status) == 1);
926 wpid = wait(&status);
927 ATF_REQUIRE(wpid == -1);
928 ATF_REQUIRE(errno == ECHILD);
932 * Verify that a new child process is stopped after a followed fork
933 * and that the traced parent sees the exit of the child when the
934 * traced parent is detached after the fork. In this test the parent
935 * that forks is not a direct child of the debugger.
937 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
938 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
940 pid_t children[2], fpid, wpid;
941 int cpipe[2], status;
943 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
944 atf_tc_skip("https://bugs.freebsd.org/239425");
946 ATF_REQUIRE(pipe(cpipe) == 0);
947 ATF_REQUIRE((fpid = fork()) != -1);
949 attach_fork_parent(cpipe);
950 follow_fork_parent(false);
953 /* Parent process. */
956 /* Wait for the direct child to exit. */
957 wpid = waitpid(fpid, &status, 0);
958 ATF_REQUIRE(wpid == fpid);
959 ATF_REQUIRE(WIFEXITED(status));
960 ATF_REQUIRE(WEXITSTATUS(status) == 3);
962 /* Read the pid of the fork parent. */
963 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
964 sizeof(children[0]));
966 /* Attach to the fork parent. */
967 attach_child(children[0]);
969 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
971 /* Continue the fork parent ignoring the SIGSTOP. */
972 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
974 /* Signal the fork parent to continue. */
977 children[1] = handle_fork_events(children[0], NULL);
978 ATF_REQUIRE(children[1] > 0);
980 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
981 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
984 * Should not see any status from the fork parent now, only
987 wpid = wait(&status);
988 ATF_REQUIRE(wpid == children[1]);
989 ATF_REQUIRE(WIFEXITED(status));
990 ATF_REQUIRE(WEXITSTATUS(status) == 2);
992 wpid = wait(&status);
993 ATF_REQUIRE(wpid == -1);
994 ATF_REQUIRE(errno == ECHILD);
998 * Verify that a child process does not see an unrelated debugger as its
999 * parent but sees its original parent process.
1001 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1002 ATF_TC_BODY(ptrace__getppid, tc)
1004 pid_t child, debugger, ppid, wpid;
1005 int cpipe[2], dpipe[2], status;
1008 ATF_REQUIRE(pipe(cpipe) == 0);
1009 ATF_REQUIRE((child = fork()) != -1);
1012 /* Child process. */
1015 /* Wait for parent to be ready. */
1016 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
1018 /* Report the parent PID to the parent. */
1020 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1027 ATF_REQUIRE(pipe(dpipe) == 0);
1028 ATF_REQUIRE((debugger = fork()) != -1);
1030 if (debugger == 0) {
1031 /* Debugger process. */
1034 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1036 wpid = waitpid(child, &status, 0);
1037 CHILD_REQUIRE(wpid == child);
1038 CHILD_REQUIRE(WIFSTOPPED(status));
1039 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1041 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1043 /* Signal parent that debugger is attached. */
1044 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
1046 /* Wait for traced child to exit. */
1047 wpid = waitpid(child, &status, 0);
1048 CHILD_REQUIRE(wpid == child);
1049 CHILD_REQUIRE(WIFEXITED(status));
1050 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
1056 /* Parent process. */
1058 /* Wait for the debugger to attach to the child. */
1059 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
1061 /* Release the child. */
1062 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
1064 /* Read the parent PID from the child. */
1065 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
1068 ATF_REQUIRE(ppid == getpid());
1070 /* Wait for the debugger. */
1071 wpid = waitpid(debugger, &status, 0);
1072 ATF_REQUIRE(wpid == debugger);
1073 ATF_REQUIRE(WIFEXITED(status));
1074 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1076 /* The child process should now be ready. */
1077 wpid = waitpid(child, &status, WNOHANG);
1078 ATF_REQUIRE(wpid == child);
1079 ATF_REQUIRE(WIFEXITED(status));
1080 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1084 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1085 * child process created via fork() reports the correct value.
1087 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1088 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1090 struct ptrace_lwpinfo pl[2];
1091 pid_t children[2], fpid, wpid;
1094 ATF_REQUIRE((fpid = fork()) != -1);
1097 follow_fork_parent(false);
1100 /* Parent process. */
1103 /* The first wait() should report the stop from SIGSTOP. */
1104 wpid = waitpid(children[0], &status, 0);
1105 ATF_REQUIRE(wpid == children[0]);
1106 ATF_REQUIRE(WIFSTOPPED(status));
1107 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1109 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1111 /* Continue the child ignoring the SIGSTOP. */
1112 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1114 /* Wait for both halves of the fork event to get reported. */
1115 children[1] = handle_fork_events(children[0], pl);
1116 ATF_REQUIRE(children[1] > 0);
1118 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1119 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1120 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1121 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1122 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1124 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1125 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1128 * The child can't exit until the grandchild reports status, so the
1129 * grandchild should report its exit first to the debugger.
1131 wpid = wait(&status);
1132 ATF_REQUIRE(wpid == children[1]);
1133 ATF_REQUIRE(WIFEXITED(status));
1134 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1136 wpid = wait(&status);
1137 ATF_REQUIRE(wpid == children[0]);
1138 ATF_REQUIRE(WIFEXITED(status));
1139 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1141 wpid = wait(&status);
1142 ATF_REQUIRE(wpid == -1);
1143 ATF_REQUIRE(errno == ECHILD);
1147 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1148 * child process created via vfork() reports the correct value.
1150 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1151 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1153 struct ptrace_lwpinfo pl[2];
1154 pid_t children[2], fpid, wpid;
1157 ATF_REQUIRE((fpid = fork()) != -1);
1160 follow_fork_parent(true);
1163 /* Parent process. */
1166 /* The first wait() should report the stop from SIGSTOP. */
1167 wpid = waitpid(children[0], &status, 0);
1168 ATF_REQUIRE(wpid == children[0]);
1169 ATF_REQUIRE(WIFSTOPPED(status));
1170 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1172 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1174 /* Continue the child ignoring the SIGSTOP. */
1175 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1177 /* Wait for both halves of the fork event to get reported. */
1178 children[1] = handle_fork_events(children[0], pl);
1179 ATF_REQUIRE(children[1] > 0);
1181 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1182 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1183 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1184 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1185 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1187 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1188 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1191 * The child can't exit until the grandchild reports status, so the
1192 * grandchild should report its exit first to the debugger.
1194 wpid = wait(&status);
1195 ATF_REQUIRE(wpid == children[1]);
1196 ATF_REQUIRE(WIFEXITED(status));
1197 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1199 wpid = wait(&status);
1200 ATF_REQUIRE(wpid == children[0]);
1201 ATF_REQUIRE(WIFEXITED(status));
1202 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1204 wpid = wait(&status);
1205 ATF_REQUIRE(wpid == -1);
1206 ATF_REQUIRE(errno == ECHILD);
1210 simple_thread(void *arg __unused)
1217 simple_thread_main(void)
1221 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1222 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1227 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1228 * thread reports the correct value.
1230 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1231 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1233 struct ptrace_lwpinfo pl;
1238 ATF_REQUIRE((fpid = fork()) != -1);
1241 simple_thread_main();
1244 /* The first wait() should report the stop from SIGSTOP. */
1245 wpid = waitpid(fpid, &status, 0);
1246 ATF_REQUIRE(wpid == fpid);
1247 ATF_REQUIRE(WIFSTOPPED(status));
1248 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1250 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1252 mainlwp = pl.pl_lwpid;
1255 * Continue the child ignoring the SIGSTOP and tracing all
1256 * system call exits.
1258 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1261 * Wait for the new thread to arrive. pthread_create() might
1262 * invoke any number of system calls. For now we just wait
1263 * for the new thread to arrive and make sure it reports a
1264 * valid system call code. If ptrace grows thread event
1265 * reporting then this test can be made more precise.
1268 wpid = waitpid(fpid, &status, 0);
1269 ATF_REQUIRE(wpid == fpid);
1270 ATF_REQUIRE(WIFSTOPPED(status));
1271 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1273 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1275 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1276 ATF_REQUIRE(pl.pl_syscall_code != 0);
1277 if (pl.pl_lwpid != mainlwp)
1278 /* New thread seen. */
1281 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1284 /* Wait for the child to exit. */
1285 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1287 wpid = waitpid(fpid, &status, 0);
1288 ATF_REQUIRE(wpid == fpid);
1289 if (WIFEXITED(status))
1292 ATF_REQUIRE(WIFSTOPPED(status));
1293 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1294 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1297 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1299 wpid = wait(&status);
1300 ATF_REQUIRE(wpid == -1);
1301 ATF_REQUIRE(errno == ECHILD);
1305 * Verify that the expected LWP events are reported for a child thread.
1307 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1308 ATF_TC_BODY(ptrace__lwp_events, tc)
1310 struct ptrace_lwpinfo pl;
1315 ATF_REQUIRE((fpid = fork()) != -1);
1318 simple_thread_main();
1321 /* The first wait() should report the stop from SIGSTOP. */
1322 wpid = waitpid(fpid, &status, 0);
1323 ATF_REQUIRE(wpid == fpid);
1324 ATF_REQUIRE(WIFSTOPPED(status));
1325 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1327 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1329 lwps[0] = pl.pl_lwpid;
1331 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1333 /* Continue the child ignoring the SIGSTOP. */
1334 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1336 /* The first event should be for the child thread's birth. */
1337 wpid = waitpid(fpid, &status, 0);
1338 ATF_REQUIRE(wpid == fpid);
1339 ATF_REQUIRE(WIFSTOPPED(status));
1340 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1342 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1343 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1344 (PL_FLAG_BORN | PL_FLAG_SCX));
1345 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1346 lwps[1] = pl.pl_lwpid;
1348 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1350 /* The next event should be for the child thread's death. */
1351 wpid = waitpid(fpid, &status, 0);
1352 ATF_REQUIRE(wpid == fpid);
1353 ATF_REQUIRE(WIFSTOPPED(status));
1354 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1356 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1357 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1358 (PL_FLAG_EXITED | PL_FLAG_SCE));
1359 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1361 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1363 /* The last event should be for the child process's exit. */
1364 wpid = waitpid(fpid, &status, 0);
1365 ATF_REQUIRE(WIFEXITED(status));
1366 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1368 wpid = wait(&status);
1369 ATF_REQUIRE(wpid == -1);
1370 ATF_REQUIRE(errno == ECHILD);
1374 exec_thread(void *arg __unused)
1377 execl("/usr/bin/true", "true", NULL);
1382 exec_thread_main(void)
1386 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1393 * Verify that the expected LWP events are reported for a multithreaded
1394 * process that calls execve(2).
1396 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1397 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1399 struct ptrace_lwpinfo pl;
1404 ATF_REQUIRE((fpid = fork()) != -1);
1410 /* The first wait() should report the stop from SIGSTOP. */
1411 wpid = waitpid(fpid, &status, 0);
1412 ATF_REQUIRE(wpid == fpid);
1413 ATF_REQUIRE(WIFSTOPPED(status));
1414 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1416 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1418 lwps[0] = pl.pl_lwpid;
1420 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1422 /* Continue the child ignoring the SIGSTOP. */
1423 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1425 /* The first event should be for the child thread's birth. */
1426 wpid = waitpid(fpid, &status, 0);
1427 ATF_REQUIRE(wpid == fpid);
1428 ATF_REQUIRE(WIFSTOPPED(status));
1429 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1431 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1432 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1433 (PL_FLAG_BORN | PL_FLAG_SCX));
1434 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1435 lwps[1] = pl.pl_lwpid;
1437 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1440 * The next event should be for the main thread's death due to
1441 * single threading from execve().
1443 wpid = waitpid(fpid, &status, 0);
1444 ATF_REQUIRE(wpid == fpid);
1445 ATF_REQUIRE(WIFSTOPPED(status));
1446 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1448 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1449 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1451 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1453 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1455 /* The next event should be for the child process's exec. */
1456 wpid = waitpid(fpid, &status, 0);
1457 ATF_REQUIRE(WIFSTOPPED(status));
1458 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1460 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1461 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1462 (PL_FLAG_EXEC | PL_FLAG_SCX));
1463 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1465 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1467 /* The last event should be for the child process's exit. */
1468 wpid = waitpid(fpid, &status, 0);
1469 ATF_REQUIRE(WIFEXITED(status));
1470 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1472 wpid = wait(&status);
1473 ATF_REQUIRE(wpid == -1);
1474 ATF_REQUIRE(errno == ECHILD);
1478 handler(int sig __unused)
1486 signal(SIGINFO, handler);
1492 * Verify that the expected ptrace event is reported for a signal.
1494 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1495 ATF_TC_BODY(ptrace__siginfo, tc)
1497 struct ptrace_lwpinfo pl;
1501 ATF_REQUIRE((fpid = fork()) != -1);
1507 /* The first wait() should report the stop from SIGSTOP. */
1508 wpid = waitpid(fpid, &status, 0);
1509 ATF_REQUIRE(wpid == fpid);
1510 ATF_REQUIRE(WIFSTOPPED(status));
1511 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1513 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1515 /* The next event should be for the SIGINFO. */
1516 wpid = waitpid(fpid, &status, 0);
1517 ATF_REQUIRE(WIFSTOPPED(status));
1518 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1520 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1521 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1522 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1523 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1524 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1526 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1528 /* The last event should be for the child process's exit. */
1529 wpid = waitpid(fpid, &status, 0);
1530 ATF_REQUIRE(WIFEXITED(status));
1531 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1533 wpid = wait(&status);
1534 ATF_REQUIRE(wpid == -1);
1535 ATF_REQUIRE(errno == ECHILD);
1539 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1541 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1542 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1547 ATF_REQUIRE((fpid = fork()) != -1);
1553 /* The first wait() should report the stop from SIGSTOP. */
1554 wpid = waitpid(fpid, &status, 0);
1555 ATF_REQUIRE(wpid == fpid);
1556 ATF_REQUIRE(WIFSTOPPED(status));
1557 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1560 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1561 sizeof(events)) == 0);
1563 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1565 /* Should get one event at exit. */
1566 wpid = waitpid(fpid, &status, 0);
1567 ATF_REQUIRE(WIFEXITED(status));
1568 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1570 wpid = wait(&status);
1571 ATF_REQUIRE(wpid == -1);
1572 ATF_REQUIRE(errno == ECHILD);
1575 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1576 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1578 struct ptrace_lwpinfo pl;
1582 ATF_REQUIRE((fpid = fork()) != -1);
1588 /* The first wait() should report the stop from SIGSTOP. */
1589 wpid = waitpid(fpid, &status, 0);
1590 ATF_REQUIRE(wpid == fpid);
1591 ATF_REQUIRE(WIFSTOPPED(status));
1592 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1594 events = PTRACE_EXEC;
1595 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1596 sizeof(events)) == 0);
1598 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1600 /* The next event should be for the child process's exec. */
1601 wpid = waitpid(fpid, &status, 0);
1602 ATF_REQUIRE(WIFSTOPPED(status));
1603 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1605 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1606 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1607 (PL_FLAG_EXEC | PL_FLAG_SCX));
1609 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1611 /* The last event should be for the child process's exit. */
1612 wpid = waitpid(fpid, &status, 0);
1613 ATF_REQUIRE(WIFEXITED(status));
1614 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1616 wpid = wait(&status);
1617 ATF_REQUIRE(wpid == -1);
1618 ATF_REQUIRE(errno == ECHILD);
1621 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1622 ATF_TC_BODY(ptrace__event_mask, tc)
1627 ATF_REQUIRE((fpid = fork()) != -1);
1633 /* The first wait() should report the stop from SIGSTOP. */
1634 wpid = waitpid(fpid, &status, 0);
1635 ATF_REQUIRE(wpid == fpid);
1636 ATF_REQUIRE(WIFSTOPPED(status));
1637 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1639 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1640 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1641 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1642 sizeof(events)) == 0);
1643 ATF_REQUIRE(events & PTRACE_FORK);
1644 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1645 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1646 sizeof(events)) == 0);
1647 ATF_REQUIRE(!(events & PTRACE_FORK));
1649 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1650 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1651 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1652 sizeof(events)) == 0);
1653 ATF_REQUIRE(events & PTRACE_LWP);
1654 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1655 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1656 sizeof(events)) == 0);
1657 ATF_REQUIRE(!(events & PTRACE_LWP));
1659 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1661 /* Should get one event at exit. */
1662 wpid = waitpid(fpid, &status, 0);
1663 ATF_REQUIRE(WIFEXITED(status));
1664 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1666 wpid = wait(&status);
1667 ATF_REQUIRE(wpid == -1);
1668 ATF_REQUIRE(errno == ECHILD);
1672 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1674 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1675 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1677 struct ptrace_lwpinfo pl;
1681 ATF_REQUIRE((fpid = fork()) != -1);
1684 follow_fork_parent(true);
1687 /* The first wait() should report the stop from SIGSTOP. */
1688 wpid = waitpid(fpid, &status, 0);
1689 ATF_REQUIRE(wpid == fpid);
1690 ATF_REQUIRE(WIFSTOPPED(status));
1691 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1693 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1694 sizeof(events)) == 0);
1695 events |= PTRACE_VFORK;
1696 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1697 sizeof(events)) == 0);
1699 /* Continue the child ignoring the SIGSTOP. */
1700 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1702 /* The next event should report the end of the vfork. */
1703 wpid = wait(&status);
1704 ATF_REQUIRE(wpid == fpid);
1705 ATF_REQUIRE(WIFSTOPPED(status));
1706 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1707 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1708 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1710 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1712 wpid = wait(&status);
1713 ATF_REQUIRE(wpid == fpid);
1714 ATF_REQUIRE(WIFEXITED(status));
1715 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1717 wpid = wait(&status);
1718 ATF_REQUIRE(wpid == -1);
1719 ATF_REQUIRE(errno == ECHILD);
1722 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1723 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1725 struct ptrace_lwpinfo pl[2];
1726 pid_t children[2], fpid, wpid;
1729 ATF_REQUIRE((fpid = fork()) != -1);
1732 follow_fork_parent(true);
1735 /* Parent process. */
1738 /* The first wait() should report the stop from SIGSTOP. */
1739 wpid = waitpid(children[0], &status, 0);
1740 ATF_REQUIRE(wpid == children[0]);
1741 ATF_REQUIRE(WIFSTOPPED(status));
1742 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1744 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1745 sizeof(events)) == 0);
1746 events |= PTRACE_FORK | PTRACE_VFORK;
1747 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1748 sizeof(events)) == 0);
1750 /* Continue the child ignoring the SIGSTOP. */
1751 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1753 /* Wait for both halves of the fork event to get reported. */
1754 children[1] = handle_fork_events(children[0], pl);
1755 ATF_REQUIRE(children[1] > 0);
1757 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1759 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1760 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1763 * The child can't exit until the grandchild reports status, so the
1764 * grandchild should report its exit first to the debugger.
1766 wpid = waitpid(children[1], &status, 0);
1767 ATF_REQUIRE(wpid == children[1]);
1768 ATF_REQUIRE(WIFEXITED(status));
1769 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1772 * The child should report it's vfork() completion before it
1775 wpid = wait(&status);
1776 ATF_REQUIRE(wpid == children[0]);
1777 ATF_REQUIRE(WIFSTOPPED(status));
1778 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1779 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1781 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1783 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1785 wpid = wait(&status);
1786 ATF_REQUIRE(wpid == children[0]);
1787 ATF_REQUIRE(WIFEXITED(status));
1788 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1790 wpid = wait(&status);
1791 ATF_REQUIRE(wpid == -1);
1792 ATF_REQUIRE(errno == ECHILD);
1795 #ifdef HAVE_BREAKPOINT
1797 * Verify that no more events are reported after PT_KILL except for the
1798 * process exit when stopped due to a breakpoint trap.
1800 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1801 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1806 ATF_REQUIRE((fpid = fork()) != -1);
1813 /* The first wait() should report the stop from SIGSTOP. */
1814 wpid = waitpid(fpid, &status, 0);
1815 ATF_REQUIRE(wpid == fpid);
1816 ATF_REQUIRE(WIFSTOPPED(status));
1817 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1819 /* Continue the child ignoring the SIGSTOP. */
1820 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1822 /* The second wait() should report hitting the breakpoint. */
1823 wpid = waitpid(fpid, &status, 0);
1824 ATF_REQUIRE(wpid == fpid);
1825 ATF_REQUIRE(WIFSTOPPED(status));
1826 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1828 /* Kill the child process. */
1829 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1831 /* The last wait() should report the SIGKILL. */
1832 wpid = waitpid(fpid, &status, 0);
1833 ATF_REQUIRE(wpid == fpid);
1834 ATF_REQUIRE(WIFSIGNALED(status));
1835 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1837 wpid = wait(&status);
1838 ATF_REQUIRE(wpid == -1);
1839 ATF_REQUIRE(errno == ECHILD);
1841 #endif /* HAVE_BREAKPOINT */
1844 * Verify that no more events are reported after PT_KILL except for the
1845 * process exit when stopped inside of a system call.
1847 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1848 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1850 struct ptrace_lwpinfo pl;
1854 ATF_REQUIRE((fpid = fork()) != -1);
1861 /* The first wait() should report the stop from SIGSTOP. */
1862 wpid = waitpid(fpid, &status, 0);
1863 ATF_REQUIRE(wpid == fpid);
1864 ATF_REQUIRE(WIFSTOPPED(status));
1865 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1867 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1868 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1870 /* The second wait() should report a system call entry for getpid(). */
1871 wpid = waitpid(fpid, &status, 0);
1872 ATF_REQUIRE(wpid == fpid);
1873 ATF_REQUIRE(WIFSTOPPED(status));
1874 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1876 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1877 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1879 /* Kill the child process. */
1880 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1882 /* The last wait() should report the SIGKILL. */
1883 wpid = waitpid(fpid, &status, 0);
1884 ATF_REQUIRE(wpid == fpid);
1885 ATF_REQUIRE(WIFSIGNALED(status));
1886 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1888 wpid = wait(&status);
1889 ATF_REQUIRE(wpid == -1);
1890 ATF_REQUIRE(errno == ECHILD);
1894 * Verify that no more events are reported after PT_KILL except for the
1895 * process exit when killing a multithreaded process.
1897 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1898 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1900 struct ptrace_lwpinfo pl;
1905 ATF_REQUIRE((fpid = fork()) != -1);
1908 simple_thread_main();
1911 /* The first wait() should report the stop from SIGSTOP. */
1912 wpid = waitpid(fpid, &status, 0);
1913 ATF_REQUIRE(wpid == fpid);
1914 ATF_REQUIRE(WIFSTOPPED(status));
1915 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1917 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1919 main_lwp = pl.pl_lwpid;
1921 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1923 /* Continue the child ignoring the SIGSTOP. */
1924 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1926 /* The first event should be for the child thread's birth. */
1927 wpid = waitpid(fpid, &status, 0);
1928 ATF_REQUIRE(wpid == fpid);
1929 ATF_REQUIRE(WIFSTOPPED(status));
1930 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1932 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1933 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1934 (PL_FLAG_BORN | PL_FLAG_SCX));
1935 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1937 /* Kill the child process. */
1938 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1940 /* The last wait() should report the SIGKILL. */
1941 wpid = waitpid(fpid, &status, 0);
1942 ATF_REQUIRE(wpid == fpid);
1943 ATF_REQUIRE(WIFSIGNALED(status));
1944 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1946 wpid = wait(&status);
1947 ATF_REQUIRE(wpid == -1);
1948 ATF_REQUIRE(errno == ECHILD);
1952 mask_usr1_thread(void *arg)
1954 pthread_barrier_t *pbarrier;
1957 pbarrier = (pthread_barrier_t*)arg;
1959 sigemptyset(&sigmask);
1960 sigaddset(&sigmask, SIGUSR1);
1961 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1963 /* Sync up with other thread after sigmask updated. */
1964 pthread_barrier_wait(pbarrier);
1973 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1974 * and prevents spurious stops due to those other signals.
1976 ATF_TC(ptrace__PT_KILL_competing_signal);
1977 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1980 atf_tc_set_md_var(tc, "require.user", "root");
1982 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1988 pthread_barrier_t barrier;
1989 struct sched_param sched_param;
1991 ATF_REQUIRE((fpid = fork()) != -1);
1993 /* Bind to one CPU so only one thread at a time will run. */
1995 CPU_SET(0, &setmask);
1997 CHILD_REQUIRE(cpuset(&setid) == 0);
1998 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1999 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2001 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2003 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2004 (void*)&barrier) == 0);
2007 * Give the main thread higher priority. The test always
2008 * assumes that, if both threads are able to run, the main
2009 * thread runs first.
2011 sched_param.sched_priority =
2012 (sched_get_priority_max(SCHED_FIFO) +
2013 sched_get_priority_min(SCHED_FIFO)) / 2;
2014 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2015 SCHED_FIFO, &sched_param) == 0);
2016 sched_param.sched_priority -= RQ_PPQ;
2017 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2018 &sched_param) == 0);
2021 sigemptyset(&sigmask);
2022 sigaddset(&sigmask, SIGUSR2);
2023 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2025 /* Sync up with other thread after sigmask updated. */
2026 pthread_barrier_wait(&barrier);
2036 /* The first wait() should report the stop from SIGSTOP. */
2037 wpid = waitpid(fpid, &status, 0);
2038 ATF_REQUIRE(wpid == fpid);
2039 ATF_REQUIRE(WIFSTOPPED(status));
2040 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2042 /* Continue the child ignoring the SIGSTOP. */
2043 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2045 /* Send a signal that only the second thread can handle. */
2046 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2048 /* The second wait() should report the SIGUSR2. */
2049 wpid = waitpid(fpid, &status, 0);
2050 ATF_REQUIRE(wpid == fpid);
2051 ATF_REQUIRE(WIFSTOPPED(status));
2052 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2054 /* Send a signal that only the first thread can handle. */
2055 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2057 /* Replace the SIGUSR2 with a kill. */
2058 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2060 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2061 wpid = waitpid(fpid, &status, 0);
2062 ATF_REQUIRE(wpid == fpid);
2063 ATF_REQUIRE(WIFSIGNALED(status));
2064 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2066 wpid = wait(&status);
2067 ATF_REQUIRE(wpid == -1);
2068 ATF_REQUIRE(errno == ECHILD);
2072 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2073 * and prevents spurious stops caused by those events.
2075 ATF_TC(ptrace__PT_KILL_competing_stop);
2076 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2079 atf_tc_set_md_var(tc, "require.user", "root");
2081 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2087 pthread_barrier_t barrier;
2089 struct ptrace_lwpinfo pl;
2090 struct sched_param sched_param;
2092 if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
2093 atf_tc_skip("https://bugs.freebsd.org/220841");
2095 ATF_REQUIRE((fpid = fork()) != -1);
2099 /* Bind to one CPU so only one thread at a time will run. */
2101 CPU_SET(0, &setmask);
2103 CHILD_REQUIRE(cpuset(&setid) == 0);
2104 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2105 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2107 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2109 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2110 (void*)&barrier) == 0);
2113 * Give the main thread higher priority. The test always
2114 * assumes that, if both threads are able to run, the main
2115 * thread runs first.
2117 sched_param.sched_priority =
2118 (sched_get_priority_max(SCHED_FIFO) +
2119 sched_get_priority_min(SCHED_FIFO)) / 2;
2120 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2121 SCHED_FIFO, &sched_param) == 0);
2122 sched_param.sched_priority -= RQ_PPQ;
2123 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2124 &sched_param) == 0);
2127 sigemptyset(&sigmask);
2128 sigaddset(&sigmask, SIGUSR2);
2129 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2131 /* Sync up with other thread after sigmask updated. */
2132 pthread_barrier_wait(&barrier);
2134 /* Sync up with the test before doing the getpid(). */
2141 /* The first wait() should report the stop from SIGSTOP. */
2142 wpid = waitpid(fpid, &status, 0);
2143 ATF_REQUIRE(wpid == fpid);
2144 ATF_REQUIRE(WIFSTOPPED(status));
2145 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2147 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2148 main_lwp = pl.pl_lwpid;
2150 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2151 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2154 * Continue until child is done with setup, which is indicated with
2155 * SIGSTOP. Ignore system calls in the meantime.
2158 wpid = waitpid(fpid, &status, 0);
2159 ATF_REQUIRE(wpid == fpid);
2160 ATF_REQUIRE(WIFSTOPPED(status));
2161 if (WSTOPSIG(status) == SIGTRAP) {
2162 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2164 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2166 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2169 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2172 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2173 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2175 wpid = waitpid(fpid, &status, 0);
2176 ATF_REQUIRE(wpid == fpid);
2177 ATF_REQUIRE(WIFSTOPPED(status));
2178 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2180 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2182 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2183 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2184 /* Prevent the main thread from hitting its syscall exit for now. */
2185 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2188 * Proceed, allowing second thread to hit syscall exit for
2189 * pthread_barrier_wait().
2191 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2193 wpid = waitpid(fpid, &status, 0);
2194 ATF_REQUIRE(wpid == fpid);
2195 ATF_REQUIRE(WIFSTOPPED(status));
2196 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2198 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2200 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2201 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2203 /* Send a signal that only the second thread can handle. */
2204 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2206 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2208 /* The next wait() should report the SIGUSR2. */
2209 wpid = waitpid(fpid, &status, 0);
2210 ATF_REQUIRE(wpid == fpid);
2211 ATF_REQUIRE(WIFSTOPPED(status));
2212 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2214 /* Allow the main thread to try to finish its system call. */
2215 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2218 * At this point, the main thread is in the middle of a system call and
2219 * has been resumed. The second thread has taken a SIGUSR2 which will
2220 * be replaced with a SIGKILL below. The main thread will get to run
2221 * first. It should notice the kill request (even though the signal
2222 * replacement occurred in the other thread) and exit accordingly. It
2223 * should not stop for the system call exit event.
2226 /* Replace the SIGUSR2 with a kill. */
2227 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2229 /* The last wait() should report the SIGKILL (not a syscall exit). */
2230 wpid = waitpid(fpid, &status, 0);
2231 ATF_REQUIRE(wpid == fpid);
2232 ATF_REQUIRE(WIFSIGNALED(status));
2233 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2235 wpid = wait(&status);
2236 ATF_REQUIRE(wpid == -1);
2237 ATF_REQUIRE(errno == ECHILD);
2241 sigusr1_handler(int sig)
2244 CHILD_REQUIRE(sig == SIGUSR1);
2249 * Verify that even if the signal queue is full for a child process,
2250 * a PT_KILL will kill the process.
2252 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2253 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2257 int max_pending_per_proc;
2261 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2263 ATF_REQUIRE((fpid = fork()) != -1);
2269 /* The first wait() should report the stop from SIGSTOP. */
2270 wpid = waitpid(fpid, &status, 0);
2271 ATF_REQUIRE(wpid == fpid);
2272 ATF_REQUIRE(WIFSTOPPED(status));
2273 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2275 len = sizeof(max_pending_per_proc);
2276 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2277 &max_pending_per_proc, &len, NULL, 0) == 0);
2279 /* Fill the signal queue. */
2280 for (i = 0; i < max_pending_per_proc; ++i)
2281 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2283 /* Kill the child process. */
2284 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2286 /* The last wait() should report the SIGKILL. */
2287 wpid = waitpid(fpid, &status, 0);
2288 ATF_REQUIRE(wpid == fpid);
2289 ATF_REQUIRE(WIFSIGNALED(status));
2290 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2292 wpid = wait(&status);
2293 ATF_REQUIRE(wpid == -1);
2294 ATF_REQUIRE(errno == ECHILD);
2298 * Verify that when stopped at a system call entry, a signal can be
2299 * requested with PT_CONTINUE which will be delivered once the system
2302 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2303 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2305 struct ptrace_lwpinfo pl;
2309 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2311 ATF_REQUIRE((fpid = fork()) != -1);
2318 /* The first wait() should report the stop from SIGSTOP. */
2319 wpid = waitpid(fpid, &status, 0);
2320 ATF_REQUIRE(wpid == fpid);
2321 ATF_REQUIRE(WIFSTOPPED(status));
2322 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2324 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2325 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2327 /* The second wait() should report a system call entry for getpid(). */
2328 wpid = waitpid(fpid, &status, 0);
2329 ATF_REQUIRE(wpid == fpid);
2330 ATF_REQUIRE(WIFSTOPPED(status));
2331 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2333 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2334 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2336 /* Continue the child process with a signal. */
2337 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2341 * The last wait() should report exit 2, i.e., a normal _exit
2342 * from the signal handler. In the meantime, catch and proceed
2343 * past any syscall stops.
2345 wpid = waitpid(fpid, &status, 0);
2346 ATF_REQUIRE(wpid == fpid);
2347 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2348 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2349 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2350 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2352 ATF_REQUIRE(WIFEXITED(status));
2353 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2358 wpid = wait(&status);
2359 ATF_REQUIRE(wpid == -1);
2360 ATF_REQUIRE(errno == ECHILD);
2364 sigusr1_counting_handler(int sig)
2366 static int counter = 0;
2368 CHILD_REQUIRE(sig == SIGUSR1);
2375 * Verify that, when continuing from a stop at system call entry and exit,
2376 * a signal can be requested from both stops, and both will be delivered when
2377 * the system call is complete.
2379 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2380 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2382 struct ptrace_lwpinfo pl;
2386 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2388 ATF_REQUIRE((fpid = fork()) != -1);
2395 /* The first wait() should report the stop from SIGSTOP. */
2396 wpid = waitpid(fpid, &status, 0);
2397 ATF_REQUIRE(wpid == fpid);
2398 ATF_REQUIRE(WIFSTOPPED(status));
2399 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2401 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2402 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2404 /* The second wait() should report a system call entry for getpid(). */
2405 wpid = waitpid(fpid, &status, 0);
2406 ATF_REQUIRE(wpid == fpid);
2407 ATF_REQUIRE(WIFSTOPPED(status));
2408 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2410 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2411 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2413 /* Continue the child process with a signal. */
2414 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2416 /* The third wait() should report a system call exit for getpid(). */
2417 wpid = waitpid(fpid, &status, 0);
2418 ATF_REQUIRE(wpid == fpid);
2419 ATF_REQUIRE(WIFSTOPPED(status));
2420 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2422 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2423 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2425 /* Continue the child process with a signal. */
2426 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2430 * The last wait() should report exit 2, i.e., a normal _exit
2431 * from the signal handler. In the meantime, catch and proceed
2432 * past any syscall stops.
2434 wpid = waitpid(fpid, &status, 0);
2435 ATF_REQUIRE(wpid == fpid);
2436 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2437 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2438 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2439 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2441 ATF_REQUIRE(WIFEXITED(status));
2442 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2447 wpid = wait(&status);
2448 ATF_REQUIRE(wpid == -1);
2449 ATF_REQUIRE(errno == ECHILD);
2453 * Verify that even if the signal queue is full for a child process,
2454 * a PT_CONTINUE with a signal will not result in loss of that signal.
2456 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2457 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2461 int max_pending_per_proc;
2465 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2466 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2468 ATF_REQUIRE((fpid = fork()) != -1);
2474 /* The first wait() should report the stop from SIGSTOP. */
2475 wpid = waitpid(fpid, &status, 0);
2476 ATF_REQUIRE(wpid == fpid);
2477 ATF_REQUIRE(WIFSTOPPED(status));
2478 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2480 len = sizeof(max_pending_per_proc);
2481 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2482 &max_pending_per_proc, &len, NULL, 0) == 0);
2484 /* Fill the signal queue. */
2485 for (i = 0; i < max_pending_per_proc; ++i)
2486 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2488 /* Continue with signal. */
2489 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2492 wpid = waitpid(fpid, &status, 0);
2493 ATF_REQUIRE(wpid == fpid);
2494 if (WIFSTOPPED(status)) {
2495 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2496 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2499 * The last wait() should report normal _exit from the
2502 ATF_REQUIRE(WIFEXITED(status));
2503 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2508 wpid = wait(&status);
2509 ATF_REQUIRE(wpid == -1);
2510 ATF_REQUIRE(errno == ECHILD);
2513 static sem_t sigusr1_sem;
2514 static int got_usr1;
2517 sigusr1_sempost_handler(int sig __unused)
2521 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2525 * Verify that even if the signal queue is full for a child process,
2526 * and the signal is masked, a PT_CONTINUE with a signal will not
2527 * result in loss of that signal.
2529 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2530 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2532 struct ptrace_lwpinfo pl;
2535 int max_pending_per_proc;
2540 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2541 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2542 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2545 ATF_REQUIRE((fpid = fork()) != -1);
2547 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2548 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2549 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2552 CHILD_REQUIRE(got_usr1 == 0);
2554 /* Allow the pending SIGUSR1 in now. */
2555 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2556 /* Wait to receive the SIGUSR1. */
2558 err = sem_wait(&sigusr1_sem);
2559 CHILD_REQUIRE(err == 0 || errno == EINTR);
2560 } while (err != 0 && errno == EINTR);
2561 CHILD_REQUIRE(got_usr1 == 1);
2565 /* The first wait() should report the stop from SIGSTOP. */
2566 wpid = waitpid(fpid, &status, 0);
2567 ATF_REQUIRE(wpid == fpid);
2568 ATF_REQUIRE(WIFSTOPPED(status));
2569 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2571 len = sizeof(max_pending_per_proc);
2572 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2573 &max_pending_per_proc, &len, NULL, 0) == 0);
2575 /* Fill the signal queue. */
2576 for (i = 0; i < max_pending_per_proc; ++i)
2577 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2579 /* Continue with signal. */
2580 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2582 /* Collect and ignore all of the SIGUSR2. */
2583 for (i = 0; i < max_pending_per_proc; ++i) {
2584 wpid = waitpid(fpid, &status, 0);
2585 ATF_REQUIRE(wpid == fpid);
2586 ATF_REQUIRE(WIFSTOPPED(status));
2587 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2588 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2591 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2592 wpid = waitpid(fpid, &status, 0);
2593 ATF_REQUIRE(wpid == fpid);
2594 ATF_REQUIRE(WIFSTOPPED(status));
2595 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2596 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2597 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2599 /* Continue the child, ignoring the SIGUSR1. */
2600 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2602 /* The last wait() should report exit after receiving SIGUSR1. */
2603 wpid = waitpid(fpid, &status, 0);
2604 ATF_REQUIRE(wpid == fpid);
2605 ATF_REQUIRE(WIFEXITED(status));
2606 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2608 wpid = wait(&status);
2609 ATF_REQUIRE(wpid == -1);
2610 ATF_REQUIRE(errno == ECHILD);
2614 * Verify that, after stopping due to a signal, that signal can be
2615 * replaced with another signal.
2617 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2618 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2620 struct ptrace_lwpinfo pl;
2624 ATF_REQUIRE((fpid = fork()) != -1);
2631 /* The first wait() should report the stop from SIGSTOP. */
2632 wpid = waitpid(fpid, &status, 0);
2633 ATF_REQUIRE(wpid == fpid);
2634 ATF_REQUIRE(WIFSTOPPED(status));
2635 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2637 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2639 /* Send a signal without ptrace. */
2640 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2642 /* The second wait() should report a SIGINT was received. */
2643 wpid = waitpid(fpid, &status, 0);
2644 ATF_REQUIRE(wpid == fpid);
2645 ATF_REQUIRE(WIFSTOPPED(status));
2646 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2648 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2649 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2650 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2652 /* Continue the child process with a different signal. */
2653 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2656 * The last wait() should report having died due to the new
2659 wpid = waitpid(fpid, &status, 0);
2660 ATF_REQUIRE(wpid == fpid);
2661 ATF_REQUIRE(WIFSIGNALED(status));
2662 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2664 wpid = wait(&status);
2665 ATF_REQUIRE(wpid == -1);
2666 ATF_REQUIRE(errno == ECHILD);
2670 * Verify that a signal can be passed through to the child even when there
2671 * was no true signal originally. Such cases arise when a SIGTRAP is
2672 * invented for e.g, system call stops.
2674 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2675 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2677 struct ptrace_lwpinfo pl;
2682 ATF_REQUIRE((fpid = fork()) != -1);
2685 /* SIGTRAP expected to cause exit on syscall entry. */
2686 rl.rlim_cur = rl.rlim_max = 0;
2687 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2692 /* The first wait() should report the stop from SIGSTOP. */
2693 wpid = waitpid(fpid, &status, 0);
2694 ATF_REQUIRE(wpid == fpid);
2695 ATF_REQUIRE(WIFSTOPPED(status));
2696 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2698 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2699 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2701 /* The second wait() should report a system call entry for getpid(). */
2702 wpid = waitpid(fpid, &status, 0);
2703 ATF_REQUIRE(wpid == fpid);
2704 ATF_REQUIRE(WIFSTOPPED(status));
2705 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2707 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2708 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2710 /* Continue the child process with a SIGTRAP. */
2711 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2715 * The last wait() should report exit due to SIGTRAP. In the
2716 * meantime, catch and proceed past any syscall stops.
2718 wpid = waitpid(fpid, &status, 0);
2719 ATF_REQUIRE(wpid == fpid);
2720 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2721 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2722 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2723 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2725 ATF_REQUIRE(WIFSIGNALED(status));
2726 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2731 wpid = wait(&status);
2732 ATF_REQUIRE(wpid == -1);
2733 ATF_REQUIRE(errno == ECHILD);
2738 * A mixed bag PT_CONTINUE with signal test.
2740 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2741 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2743 struct ptrace_lwpinfo pl;
2747 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2749 ATF_REQUIRE((fpid = fork()) != -1);
2756 /* The first wait() should report the stop from SIGSTOP. */
2757 wpid = waitpid(fpid, &status, 0);
2758 ATF_REQUIRE(wpid == fpid);
2759 ATF_REQUIRE(WIFSTOPPED(status));
2760 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2762 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2763 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2765 /* The second wait() should report a system call entry for getpid(). */
2766 wpid = waitpid(fpid, &status, 0);
2767 ATF_REQUIRE(wpid == fpid);
2768 ATF_REQUIRE(WIFSTOPPED(status));
2769 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2771 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2772 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2774 /* Continue with the first SIGUSR1. */
2775 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2777 /* The next wait() should report a system call exit for getpid(). */
2778 wpid = waitpid(fpid, &status, 0);
2779 ATF_REQUIRE(wpid == fpid);
2780 ATF_REQUIRE(WIFSTOPPED(status));
2781 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2783 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2784 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2786 /* Send an ABRT without ptrace. */
2787 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2789 /* Continue normally. */
2790 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2792 /* The next wait() should report the SIGABRT. */
2793 wpid = waitpid(fpid, &status, 0);
2794 ATF_REQUIRE(wpid == fpid);
2795 ATF_REQUIRE(WIFSTOPPED(status));
2796 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2798 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2799 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2800 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2802 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2803 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2807 * The last wait() should report exit 2, i.e., a normal _exit
2808 * from the signal handler. In the meantime, catch and proceed
2809 * past any syscall stops.
2811 wpid = waitpid(fpid, &status, 0);
2812 ATF_REQUIRE(wpid == fpid);
2813 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2814 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2815 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2816 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2818 ATF_REQUIRE(WIFEXITED(status));
2819 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2824 wpid = wait(&status);
2825 ATF_REQUIRE(wpid == -1);
2826 ATF_REQUIRE(errno == ECHILD);
2831 * Verify a signal delivered by ptrace is noticed by kevent(2).
2833 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2834 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2837 int status, kq, nevents;
2840 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2842 ATF_REQUIRE((fpid = fork()) != -1);
2844 CHILD_REQUIRE((kq = kqueue()) > 0);
2845 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2846 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2851 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2852 if (nevents == -1 && errno == EINTR)
2854 CHILD_REQUIRE(nevents > 0);
2855 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2856 CHILD_REQUIRE(kev.ident == SIGUSR1);
2863 /* The first wait() should report the stop from SIGSTOP. */
2864 wpid = waitpid(fpid, &status, 0);
2865 ATF_REQUIRE(wpid == fpid);
2866 ATF_REQUIRE(WIFSTOPPED(status));
2867 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2869 /* Continue with the SIGUSR1. */
2870 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2873 * The last wait() should report normal exit with code 1.
2875 wpid = waitpid(fpid, &status, 0);
2876 ATF_REQUIRE(wpid == fpid);
2877 ATF_REQUIRE(WIFEXITED(status));
2878 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2880 wpid = wait(&status);
2881 ATF_REQUIRE(wpid == -1);
2882 ATF_REQUIRE(errno == ECHILD);
2886 signal_thread(void *arg)
2891 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2893 /* Wait for this thread to receive a SIGUSR1. */
2895 err = sem_wait(&sigusr1_sem);
2896 CHILD_REQUIRE(err == 0 || errno == EINTR);
2897 } while (err != 0 && errno == EINTR);
2899 /* Free our companion thread from the barrier. */
2900 pthread_barrier_wait(pbarrier);
2903 * Swap ignore duties; the next SIGUSR1 should go to the
2906 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2907 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2908 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2910 /* Sync up threads after swapping signal masks. */
2911 pthread_barrier_wait(pbarrier);
2913 /* Wait until our companion has received its SIGUSR1. */
2914 pthread_barrier_wait(pbarrier);
2920 * Verify that a traced process with blocked signal received the
2921 * signal from kill() once unmasked.
2923 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2924 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2926 struct ptrace_lwpinfo pl;
2931 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2932 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2935 ATF_REQUIRE((fpid = fork()) != -1);
2937 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2938 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2939 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2942 CHILD_REQUIRE(got_usr1 == 0);
2944 /* Allow the pending SIGUSR1 in now. */
2945 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2946 /* Wait to receive a SIGUSR1. */
2948 err = sem_wait(&sigusr1_sem);
2949 CHILD_REQUIRE(err == 0 || errno == EINTR);
2950 } while (err != 0 && errno == EINTR);
2951 CHILD_REQUIRE(got_usr1 == 1);
2955 /* The first wait() should report the stop from SIGSTOP. */
2956 wpid = waitpid(fpid, &status, 0);
2957 ATF_REQUIRE(wpid == fpid);
2958 ATF_REQUIRE(WIFSTOPPED(status));
2959 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2960 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2961 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2963 /* Send blocked SIGUSR1 which should cause a stop. */
2964 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2966 /* Continue the child ignoring the SIGSTOP. */
2967 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2969 /* The next wait() should report the kill(SIGUSR1) was received. */
2970 wpid = waitpid(fpid, &status, 0);
2971 ATF_REQUIRE(wpid == fpid);
2972 ATF_REQUIRE(WIFSTOPPED(status));
2973 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2974 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2975 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2977 /* Continue the child, allowing in the SIGUSR1. */
2978 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2980 /* The last wait() should report normal exit with code 1. */
2981 wpid = waitpid(fpid, &status, 0);
2982 ATF_REQUIRE(wpid == fpid);
2983 ATF_REQUIRE(WIFEXITED(status));
2984 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2986 wpid = wait(&status);
2987 ATF_REQUIRE(wpid == -1);
2988 ATF_REQUIRE(errno == ECHILD);
2992 * Verify that a traced process with blocked signal received the
2993 * signal from PT_CONTINUE once unmasked.
2995 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2996 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2998 struct ptrace_lwpinfo pl;
3003 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3004 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3007 ATF_REQUIRE((fpid = fork()) != -1);
3009 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3010 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3011 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
3014 CHILD_REQUIRE(got_usr1 == 0);
3016 /* Allow the pending SIGUSR1 in now. */
3017 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3018 /* Wait to receive a SIGUSR1. */
3020 err = sem_wait(&sigusr1_sem);
3021 CHILD_REQUIRE(err == 0 || errno == EINTR);
3022 } while (err != 0 && errno == EINTR);
3024 CHILD_REQUIRE(got_usr1 == 1);
3028 /* The first wait() should report the stop from SIGSTOP. */
3029 wpid = waitpid(fpid, &status, 0);
3030 ATF_REQUIRE(wpid == fpid);
3031 ATF_REQUIRE(WIFSTOPPED(status));
3032 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3033 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3034 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
3036 /* Continue the child replacing SIGSTOP with SIGUSR1. */
3037 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3039 /* The next wait() should report the SIGUSR1 was received. */
3040 wpid = waitpid(fpid, &status, 0);
3041 ATF_REQUIRE(wpid == fpid);
3042 ATF_REQUIRE(WIFSTOPPED(status));
3043 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
3044 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3045 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
3047 /* Continue the child, ignoring the SIGUSR1. */
3048 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3050 /* The last wait() should report normal exit with code 1. */
3051 wpid = waitpid(fpid, &status, 0);
3052 ATF_REQUIRE(wpid == fpid);
3053 ATF_REQUIRE(WIFEXITED(status));
3054 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3056 wpid = wait(&status);
3057 ATF_REQUIRE(wpid == -1);
3058 ATF_REQUIRE(errno == ECHILD);
3062 * Verify that if ptrace stops due to a signal but continues with
3063 * a different signal that the new signal is routed to a thread
3064 * that can accept it, and that the thread is awakened by the signal
3065 * in a timely manner.
3067 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3068 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3074 pthread_barrier_t barrier;
3076 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3077 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3078 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3080 ATF_REQUIRE((fpid = fork()) != -1);
3082 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3084 /* The other thread should receive the first SIGUSR1. */
3085 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3086 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3087 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3091 /* Wait until other thread has received its SIGUSR1. */
3092 pthread_barrier_wait(&barrier);
3095 * Swap ignore duties; the next SIGUSR1 should go to this
3098 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3100 /* Sync up threads after swapping signal masks. */
3101 pthread_barrier_wait(&barrier);
3104 * Sync up with test code; we're ready for the next SIGUSR1
3109 /* Wait for this thread to receive a SIGUSR1. */
3111 err = sem_wait(&sigusr1_sem);
3112 CHILD_REQUIRE(err == 0 || errno == EINTR);
3113 } while (err != 0 && errno == EINTR);
3115 /* Free the other thread from the barrier. */
3116 pthread_barrier_wait(&barrier);
3118 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3123 /* The first wait() should report the stop from SIGSTOP. */
3124 wpid = waitpid(fpid, &status, 0);
3125 ATF_REQUIRE(wpid == fpid);
3126 ATF_REQUIRE(WIFSTOPPED(status));
3127 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3129 /* Continue the child ignoring the SIGSTOP. */
3130 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3133 * Send a signal without ptrace that either thread will accept (USR2,
3136 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3138 /* The second wait() should report a SIGUSR2 was received. */
3139 wpid = waitpid(fpid, &status, 0);
3140 ATF_REQUIRE(wpid == fpid);
3141 ATF_REQUIRE(WIFSTOPPED(status));
3142 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3144 /* Continue the child, changing the signal to USR1. */
3145 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3147 /* The next wait() should report the stop from SIGSTOP. */
3148 wpid = waitpid(fpid, &status, 0);
3149 ATF_REQUIRE(wpid == fpid);
3150 ATF_REQUIRE(WIFSTOPPED(status));
3151 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3153 /* Continue the child ignoring the SIGSTOP. */
3154 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3156 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3158 /* The next wait() should report a SIGUSR2 was received. */
3159 wpid = waitpid(fpid, &status, 0);
3160 ATF_REQUIRE(wpid == fpid);
3161 ATF_REQUIRE(WIFSTOPPED(status));
3162 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3164 /* Continue the child, changing the signal to USR1. */
3165 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3167 /* The last wait() should report normal exit with code 1. */
3168 wpid = waitpid(fpid, &status, 0);
3169 ATF_REQUIRE(wpid == fpid);
3170 ATF_REQUIRE(WIFEXITED(status));
3171 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3173 wpid = wait(&status);
3174 ATF_REQUIRE(wpid == -1);
3175 ATF_REQUIRE(errno == ECHILD);
3179 raise_sigstop_thread(void *arg __unused)
3187 sleep_thread(void *arg __unused)
3195 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3204 * Become the reaper for this process tree. We need to be able to check
3205 * that both child and grandchild have died.
3207 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3210 ATF_REQUIRE(fpid >= 0);
3213 CHILD_REQUIRE(fpid >= 0);
3217 /* Pin to CPU 0 to serialize thread execution. */
3219 CPU_SET(0, &setmask);
3220 CHILD_REQUIRE(cpuset(&setid) == 0);
3221 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3222 CPU_WHICH_CPUSET, setid,
3223 sizeof(setmask), &setmask) == 0);
3225 if (sigstop_from_main_thread) {
3227 * We expect the SIGKILL sent when our parent
3228 * dies to be delivered to the new thread.
3229 * Raise the SIGSTOP in this thread so the
3232 CHILD_REQUIRE(pthread_create(&t, NULL,
3233 sleep_thread, NULL) == 0);
3237 * We expect the SIGKILL to be delivered to
3238 * this thread. After creating the new thread,
3239 * just get off the CPU so the other thread can
3240 * raise the SIGSTOP.
3242 CHILD_REQUIRE(pthread_create(&t, NULL,
3243 raise_sigstop_thread, NULL) == 0);
3249 /* First stop is trace_me() immediately after fork. */
3250 wpid = waitpid(fpid, &status, 0);
3251 CHILD_REQUIRE(wpid == fpid);
3252 CHILD_REQUIRE(WIFSTOPPED(status));
3253 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3255 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3257 /* Second stop is from the raise(SIGSTOP). */
3258 wpid = waitpid(fpid, &status, 0);
3259 CHILD_REQUIRE(wpid == fpid);
3260 CHILD_REQUIRE(WIFSTOPPED(status));
3261 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3264 * Terminate tracing process without detaching. Our child
3271 * We should get a normal exit from our immediate child and a SIGKILL
3272 * exit from our grandchild. The latter case is the interesting one.
3273 * Our grandchild should not have stopped due to the SIGSTOP that was
3274 * left dangling when its parent died.
3276 for (i = 0; i < 2; ++i) {
3277 wpid = wait(&status);
3279 ATF_REQUIRE(WIFEXITED(status) &&
3280 WEXITSTATUS(status) == 0);
3282 ATF_REQUIRE(WIFSIGNALED(status) &&
3283 WTERMSIG(status) == SIGKILL);
3288 * These two tests ensure that if the tracing process exits without detaching
3289 * just after the child received a SIGSTOP, the child is cleanly killed and
3290 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3291 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3292 * different threads, the SIGKILL must win. There are two variants of this
3293 * test, designed to catch the case where the SIGKILL is delivered to the
3294 * younger thread (the first test) and the case where the SIGKILL is delivered
3295 * to the older thread (the second test). This behavior has changed in the
3296 * past, so make no assumption.
3298 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3299 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3302 atf_tc_set_md_var(tc, "require.user", "root");
3304 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3307 terminate_with_pending_sigstop(true);
3310 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3311 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3314 atf_tc_set_md_var(tc, "require.user", "root");
3316 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3319 terminate_with_pending_sigstop(false);
3323 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3326 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3327 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3329 struct ptrace_lwpinfo pl;
3331 int status, event_mask, new_event_mask;
3333 ATF_REQUIRE((fpid = fork()) != -1);
3340 /* The first wait() should report the stop from trace_me(). */
3341 wpid = waitpid(fpid, &status, 0);
3342 ATF_REQUIRE(wpid == fpid);
3343 ATF_REQUIRE(WIFSTOPPED(status));
3344 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3346 /* Set several unobtrusive event bits. */
3347 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3348 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3349 sizeof(event_mask)) == 0);
3351 /* Send a SIGKILL without using ptrace. */
3352 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3354 /* Continue the child ignoring the SIGSTOP. */
3355 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3357 /* The next stop should be due to the SIGKILL. */
3358 wpid = waitpid(fpid, &status, 0);
3359 ATF_REQUIRE(wpid == fpid);
3360 ATF_REQUIRE(WIFSTOPPED(status));
3361 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3363 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3364 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3365 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3367 /* Continue the child ignoring the SIGKILL. */
3368 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3370 /* The next wait() should report the stop from SIGSTOP. */
3371 wpid = waitpid(fpid, &status, 0);
3372 ATF_REQUIRE(wpid == fpid);
3373 ATF_REQUIRE(WIFSTOPPED(status));
3374 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3376 /* Check the current event mask. It should not have changed. */
3378 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3379 sizeof(new_event_mask)) == 0);
3380 ATF_REQUIRE(event_mask == new_event_mask);
3382 /* Continue the child to let it exit. */
3383 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3385 /* The last event should be for the child process's exit. */
3386 wpid = waitpid(fpid, &status, 0);
3387 ATF_REQUIRE(WIFEXITED(status));
3388 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3390 wpid = wait(&status);
3391 ATF_REQUIRE(wpid == -1);
3392 ATF_REQUIRE(errno == ECHILD);
3396 flock_thread(void *arg)
3401 (void)flock(fd, LOCK_EX);
3402 (void)flock(fd, LOCK_UN);
3407 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3408 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3409 * on a lock. This is a regression test for r318191.
3411 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3412 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3414 pthread_barrier_t barrier;
3415 pthread_barrierattr_t battr;
3418 int error, fd, i, status;
3420 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3421 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3422 PTHREAD_PROCESS_SHARED) == 0);
3423 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3425 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3426 fd = mkstemp(tmpfile);
3427 ATF_REQUIRE(fd >= 0);
3429 ATF_REQUIRE((child = fork()) != -1);
3434 error = pthread_barrier_wait(&barrier);
3435 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3438 cfd = open(tmpfile, O_RDONLY);
3443 * We want at least two threads blocked on the file lock since
3444 * the SIGSTOP from PT_ATTACH may kick one of them out of
3447 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3449 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3451 if (pthread_join(t[0], NULL) != 0)
3453 if (pthread_join(t[1], NULL) != 0)
3458 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3460 error = pthread_barrier_wait(&barrier);
3461 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3464 * Give the child some time to block. Is there a better way to do this?
3469 * Attach and give the child 3 seconds to stop.
3471 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3472 for (i = 0; i < 3; i++) {
3473 wpid = waitpid(child, &status, WNOHANG);
3474 if (wpid == child && WIFSTOPPED(status) &&
3475 WSTOPSIG(status) == SIGSTOP)
3479 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3481 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3483 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3484 ATF_REQUIRE(unlink(tmpfile) == 0);
3485 ATF_REQUIRE(close(fd) == 0);
3489 sigusr1_step_handler(int sig)
3492 CHILD_REQUIRE(sig == SIGUSR1);
3497 * Verify that PT_STEP with a signal invokes the signal before
3498 * stepping the next instruction (and that the next instruction is
3499 * stepped correctly).
3501 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3502 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3504 struct ptrace_lwpinfo pl;
3508 ATF_REQUIRE((fpid = fork()) != -1);
3511 signal(SIGUSR1, sigusr1_step_handler);
3516 /* The first wait() should report the stop from SIGSTOP. */
3517 wpid = waitpid(fpid, &status, 0);
3518 ATF_REQUIRE(wpid == fpid);
3519 ATF_REQUIRE(WIFSTOPPED(status));
3520 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3522 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3524 /* The next stop should report the SIGABRT in the child body. */
3525 wpid = waitpid(fpid, &status, 0);
3526 ATF_REQUIRE(wpid == fpid);
3527 ATF_REQUIRE(WIFSTOPPED(status));
3528 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3530 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3531 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3532 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3534 /* Step the child process inserting SIGUSR1. */
3535 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3537 /* The next stop should report the SIGABRT in the signal handler. */
3538 wpid = waitpid(fpid, &status, 0);
3539 ATF_REQUIRE(wpid == fpid);
3540 ATF_REQUIRE(WIFSTOPPED(status));
3541 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3543 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3544 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3545 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3547 /* Continue the child process discarding the signal. */
3548 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3550 /* The next stop should report a trace trap from PT_STEP. */
3551 wpid = waitpid(fpid, &status, 0);
3552 ATF_REQUIRE(wpid == fpid);
3553 ATF_REQUIRE(WIFSTOPPED(status));
3554 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3556 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3557 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3558 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3559 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3561 /* Continue the child to let it exit. */
3562 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3564 /* The last event should be for the child process's exit. */
3565 wpid = waitpid(fpid, &status, 0);
3566 ATF_REQUIRE(WIFEXITED(status));
3567 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3569 wpid = wait(&status);
3570 ATF_REQUIRE(wpid == -1);
3571 ATF_REQUIRE(errno == ECHILD);
3574 #ifdef HAVE_BREAKPOINT
3576 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3577 * for a breakpoint trap.
3579 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3580 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3582 struct ptrace_lwpinfo pl;
3586 ATF_REQUIRE((fpid = fork()) != -1);
3593 /* The first wait() should report the stop from SIGSTOP. */
3594 wpid = waitpid(fpid, &status, 0);
3595 ATF_REQUIRE(wpid == fpid);
3596 ATF_REQUIRE(WIFSTOPPED(status));
3597 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3599 /* Continue the child ignoring the SIGSTOP. */
3600 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3602 /* The second wait() should report hitting the breakpoint. */
3603 wpid = waitpid(fpid, &status, 0);
3604 ATF_REQUIRE(wpid == fpid);
3605 ATF_REQUIRE(WIFSTOPPED(status));
3606 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3608 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3609 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3610 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3611 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3613 /* Kill the child process. */
3614 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3616 /* The last wait() should report the SIGKILL. */
3617 wpid = waitpid(fpid, &status, 0);
3618 ATF_REQUIRE(wpid == fpid);
3619 ATF_REQUIRE(WIFSIGNALED(status));
3620 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3622 wpid = wait(&status);
3623 ATF_REQUIRE(wpid == -1);
3624 ATF_REQUIRE(errno == ECHILD);
3626 #endif /* HAVE_BREAKPOINT */
3629 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3630 * for a single-step trap from PT_STEP.
3632 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3633 ATF_TC_BODY(ptrace__step_siginfo, tc)
3635 struct ptrace_lwpinfo pl;
3639 ATF_REQUIRE((fpid = fork()) != -1);
3645 /* The first wait() should report the stop from SIGSTOP. */
3646 wpid = waitpid(fpid, &status, 0);
3647 ATF_REQUIRE(wpid == fpid);
3648 ATF_REQUIRE(WIFSTOPPED(status));
3649 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3651 /* Step the child ignoring the SIGSTOP. */
3652 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3654 /* The second wait() should report a single-step trap. */
3655 wpid = waitpid(fpid, &status, 0);
3656 ATF_REQUIRE(wpid == fpid);
3657 ATF_REQUIRE(WIFSTOPPED(status));
3658 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3660 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3661 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3662 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3663 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3665 /* Continue the child process. */
3666 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3668 /* The last event should be for the child process's exit. */
3669 wpid = waitpid(fpid, &status, 0);
3670 ATF_REQUIRE(WIFEXITED(status));
3671 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3673 wpid = wait(&status);
3674 ATF_REQUIRE(wpid == -1);
3675 ATF_REQUIRE(errno == ECHILD);
3678 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3680 continue_thread(void *arg __unused)
3687 continue_thread_main(void)
3689 pthread_t threads[2];
3691 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3693 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3695 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3696 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3701 * Ensure that PT_CONTINUE clears the status of the thread that
3702 * triggered the stop even if a different thread's LWP was passed to
3705 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3706 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3708 struct ptrace_lwpinfo pl;
3715 ATF_REQUIRE((fpid = fork()) != -1);
3718 continue_thread_main();
3721 /* The first wait() should report the stop from SIGSTOP. */
3722 wpid = waitpid(fpid, &status, 0);
3723 ATF_REQUIRE(wpid == fpid);
3724 ATF_REQUIRE(WIFSTOPPED(status));
3725 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3727 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3730 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3732 /* Continue the child ignoring the SIGSTOP. */
3733 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3735 /* One of the new threads should report it's birth. */
3736 wpid = waitpid(fpid, &status, 0);
3737 ATF_REQUIRE(wpid == fpid);
3738 ATF_REQUIRE(WIFSTOPPED(status));
3739 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3741 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3742 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3743 (PL_FLAG_BORN | PL_FLAG_SCX));
3744 lwps[0] = pl.pl_lwpid;
3747 * Suspend this thread to ensure both threads are alive before
3748 * hitting the breakpoint.
3750 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3752 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3754 /* Second thread should report it's birth. */
3755 wpid = waitpid(fpid, &status, 0);
3756 ATF_REQUIRE(wpid == fpid);
3757 ATF_REQUIRE(WIFSTOPPED(status));
3758 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3760 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3761 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3762 (PL_FLAG_BORN | PL_FLAG_SCX));
3763 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3764 lwps[1] = pl.pl_lwpid;
3766 /* Resume both threads waiting for breakpoint events. */
3767 hit_break[0] = hit_break[1] = false;
3768 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3769 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3771 /* One thread should report a breakpoint. */
3772 wpid = waitpid(fpid, &status, 0);
3773 ATF_REQUIRE(wpid == fpid);
3774 ATF_REQUIRE(WIFSTOPPED(status));
3775 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3777 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3778 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3779 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3780 pl.pl_siginfo.si_code == TRAP_BRKPT);
3781 if (pl.pl_lwpid == lwps[0])
3785 hit_break[i] = true;
3786 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3788 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3791 * Resume both threads but pass the other thread's LWPID to
3794 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3797 * Will now get two thread exit events and one more breakpoint
3800 for (j = 0; j < 3; j++) {
3801 wpid = waitpid(fpid, &status, 0);
3802 ATF_REQUIRE(wpid == fpid);
3803 ATF_REQUIRE(WIFSTOPPED(status));
3804 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3806 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3809 if (pl.pl_lwpid == lwps[0])
3814 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3815 if (pl.pl_flags & PL_FLAG_EXITED) {
3816 ATF_REQUIRE_MSG(hit_break[i],
3817 "exited thread did not report breakpoint");
3820 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3821 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3822 pl.pl_siginfo.si_code == TRAP_BRKPT);
3823 ATF_REQUIRE_MSG(!hit_break[i],
3824 "double breakpoint event");
3825 hit_break[i] = true;
3826 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3829 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3833 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3836 /* Both threads should have exited. */
3837 ATF_REQUIRE(lwps[0] == 0);
3838 ATF_REQUIRE(lwps[1] == 0);
3840 /* The last event should be for the child process's exit. */
3841 wpid = waitpid(fpid, &status, 0);
3842 ATF_REQUIRE(WIFEXITED(status));
3843 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3845 wpid = wait(&status);
3846 ATF_REQUIRE(wpid == -1);
3847 ATF_REQUIRE(errno == ECHILD);
3852 * Verify that PT_LWPINFO doesn't return stale siginfo.
3854 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3855 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3857 struct ptrace_lwpinfo pl;
3861 ATF_REQUIRE((fpid = fork()) != -1);
3868 /* The first wait() should report the stop from SIGSTOP. */
3869 wpid = waitpid(fpid, &status, 0);
3870 ATF_REQUIRE(wpid == fpid);
3871 ATF_REQUIRE(WIFSTOPPED(status));
3872 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3874 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3876 /* The next stop should report the SIGABRT in the child body. */
3877 wpid = waitpid(fpid, &status, 0);
3878 ATF_REQUIRE(wpid == fpid);
3879 ATF_REQUIRE(WIFSTOPPED(status));
3880 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3882 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3883 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3884 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3887 * Continue the process ignoring the signal, but enabling
3890 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3893 * The next stop should report a system call entry from
3894 * exit(). PL_FLAGS_SI should not be set.
3896 wpid = waitpid(fpid, &status, 0);
3897 ATF_REQUIRE(wpid == fpid);
3898 ATF_REQUIRE(WIFSTOPPED(status));
3899 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3901 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3902 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3903 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3905 /* Disable syscall tracing and continue the child to let it exit. */
3906 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3907 sizeof(events)) == 0);
3908 events &= ~PTRACE_SYSCALL;
3909 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3910 sizeof(events)) == 0);
3911 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3913 /* The last event should be for the child process's exit. */
3914 wpid = waitpid(fpid, &status, 0);
3915 ATF_REQUIRE(WIFEXITED(status));
3916 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3918 wpid = wait(&status);
3919 ATF_REQUIRE(wpid == -1);
3920 ATF_REQUIRE(errno == ECHILD);
3924 * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3926 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3927 ATF_TC_BODY(ptrace__syscall_args, tc)
3929 struct ptrace_lwpinfo pl;
3930 struct ptrace_sc_ret psr;
3935 ATF_REQUIRE((fpid = fork()) != -1);
3943 /* The first wait() should report the stop from SIGSTOP. */
3944 wpid = waitpid(fpid, &status, 0);
3945 ATF_REQUIRE(wpid == fpid);
3946 ATF_REQUIRE(WIFSTOPPED(status));
3947 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3950 * Continue the process ignoring the signal, but enabling
3953 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3956 * The next stop should be the syscall entry from getpid().
3958 wpid = waitpid(fpid, &status, 0);
3959 ATF_REQUIRE(wpid == fpid);
3960 ATF_REQUIRE(WIFSTOPPED(status));
3961 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3963 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3964 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3965 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3967 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3970 * The next stop should be the syscall exit from getpid().
3972 wpid = waitpid(fpid, &status, 0);
3973 ATF_REQUIRE(wpid == fpid);
3974 ATF_REQUIRE(WIFSTOPPED(status));
3975 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3977 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3978 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
3979 ATF_REQUIRE(pl.pl_syscall_code == SYS_getpid);
3981 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
3982 sizeof(psr)) != -1);
3983 ATF_REQUIRE(psr.sr_error == 0);
3984 ATF_REQUIRE(psr.sr_retval[0] == wpid);
3986 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3989 * The next stop should be the syscall entry from kill().
3991 wpid = waitpid(fpid, &status, 0);
3992 ATF_REQUIRE(wpid == fpid);
3993 ATF_REQUIRE(WIFSTOPPED(status));
3994 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3996 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3997 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3998 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
3999 ATF_REQUIRE(pl.pl_syscall_narg == 2);
4001 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4002 sizeof(args)) != -1);
4003 ATF_REQUIRE(args[0] == wpid);
4004 ATF_REQUIRE(args[1] == 0);
4006 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4009 * The next stop should be the syscall exit from kill().
4011 wpid = waitpid(fpid, &status, 0);
4012 ATF_REQUIRE(wpid == fpid);
4013 ATF_REQUIRE(WIFSTOPPED(status));
4014 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4016 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4017 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4018 ATF_REQUIRE(pl.pl_syscall_code == SYS_kill);
4020 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4021 sizeof(psr)) != -1);
4022 ATF_REQUIRE(psr.sr_error == 0);
4024 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4027 * The next stop should be the syscall entry from close().
4029 wpid = waitpid(fpid, &status, 0);
4030 ATF_REQUIRE(wpid == fpid);
4031 ATF_REQUIRE(WIFSTOPPED(status));
4032 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4034 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4035 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4036 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4037 ATF_REQUIRE(pl.pl_syscall_narg == 1);
4039 ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4040 sizeof(args)) != -1);
4041 ATF_REQUIRE(args[0] == 3);
4043 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4046 * The next stop should be the syscall exit from close().
4048 wpid = waitpid(fpid, &status, 0);
4049 ATF_REQUIRE(wpid == fpid);
4050 ATF_REQUIRE(WIFSTOPPED(status));
4051 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
4053 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4054 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4055 ATF_REQUIRE(pl.pl_syscall_code == SYS_close);
4057 ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4058 sizeof(psr)) != -1);
4059 ATF_REQUIRE(psr.sr_error == EBADF);
4061 /* Disable syscall tracing and continue the child to let it exit. */
4062 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4063 sizeof(events)) == 0);
4064 events &= ~PTRACE_SYSCALL;
4065 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4066 sizeof(events)) == 0);
4067 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
4069 /* The last event should be for the child process's exit. */
4070 wpid = waitpid(fpid, &status, 0);
4071 ATF_REQUIRE(WIFEXITED(status));
4072 ATF_REQUIRE(WEXITSTATUS(status) == 1);
4074 wpid = wait(&status);
4075 ATF_REQUIRE(wpid == -1);
4076 ATF_REQUIRE(errno == ECHILD);
4080 * Verify that when the process is traced that it isn't reparent
4081 * to the init process when we close all process descriptors.
4083 ATF_TC(ptrace__proc_reparent);
4084 ATF_TC_HEAD(ptrace__proc_reparent, tc)
4087 atf_tc_set_md_var(tc, "timeout", "2");
4089 ATF_TC_BODY(ptrace__proc_reparent, tc)
4091 pid_t traced, debuger, wpid;
4094 traced = pdfork(&pd, 0);
4095 ATF_REQUIRE(traced >= 0);
4100 ATF_REQUIRE(pd >= 0);
4103 ATF_REQUIRE(debuger >= 0);
4105 /* The traced process is reparented to debuger. */
4106 ATF_REQUIRE(ptrace(PT_ATTACH, traced, 0, 0) == 0);
4107 wpid = waitpid(traced, &status, 0);
4108 ATF_REQUIRE(wpid == traced);
4109 ATF_REQUIRE(WIFSTOPPED(status));
4110 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
4111 ATF_REQUIRE(close(pd) == 0);
4112 ATF_REQUIRE(ptrace(PT_DETACH, traced, (caddr_t)1, 0) == 0);
4114 /* We closed pd so we should not have any child. */
4115 wpid = wait(&status);
4116 ATF_REQUIRE(wpid == -1);
4117 ATF_REQUIRE(errno == ECHILD);
4122 ATF_REQUIRE(close(pd) == 0);
4123 wpid = waitpid(debuger, &status, 0);
4124 ATF_REQUIRE(wpid == debuger);
4125 ATF_REQUIRE(WEXITSTATUS(status) == 0);
4127 /* Check if we still have any child. */
4128 wpid = wait(&status);
4129 ATF_REQUIRE(wpid == -1);
4130 ATF_REQUIRE(errno == ECHILD);
4136 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4137 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4138 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4139 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4140 ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4141 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4142 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4143 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4144 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4146 ptrace__follow_fork_child_detached_unrelated_debugger);
4148 ptrace__follow_fork_parent_detached_unrelated_debugger);
4149 ATF_TP_ADD_TC(tp, ptrace__getppid);
4150 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4151 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4152 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4153 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4154 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4155 ATF_TP_ADD_TC(tp, ptrace__siginfo);
4156 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4157 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4158 ATF_TP_ADD_TC(tp, ptrace__event_mask);
4159 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4160 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4161 #ifdef HAVE_BREAKPOINT
4162 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4164 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4165 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4166 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4167 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4168 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4169 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4171 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4172 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4173 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4174 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4175 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4176 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4177 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4178 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4179 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4180 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4181 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4182 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4183 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4184 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4185 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4186 #ifdef HAVE_BREAKPOINT
4187 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4189 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4190 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4191 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4193 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4194 ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4195 ATF_TP_ADD_TC(tp, ptrace__proc_reparent);
4197 return (atf_no_error());