2 * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/types.h>
30 #include <sys/cpuset.h>
31 #include <sys/event.h>
34 #include <sys/procctl.h>
35 #include <sys/ptrace.h>
36 #include <sys/queue.h>
38 #include <sys/syscall.h>
39 #include <sys/sysctl.h>
43 #include <machine/cpufunc.h>
46 #include <semaphore.h>
54 * Architectures with a user-visible breakpoint().
56 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) || \
57 defined(__i386__) || defined(__mips__) || defined(__riscv) || \
59 #define HAVE_BREAKPOINT
63 * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
65 #ifdef HAVE_BREAKPOINT
66 #if defined(__aarch64__)
67 #define SKIP_BREAK(reg) ((reg)->elr += 4)
68 #elif defined(__amd64__) || defined(__i386__)
69 #define SKIP_BREAK(reg)
70 #elif defined(__arm__)
71 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
72 #elif defined(__mips__)
73 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
74 #elif defined(__riscv)
75 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
76 #elif defined(__sparc64__)
77 #define SKIP_BREAK(reg) do { \
78 (reg)->r_tpc = (reg)->r_tnpc + 4; \
85 * A variant of ATF_REQUIRE that is suitable for use in child
86 * processes. This only works if the parent process is tripped up by
87 * the early exit and fails some requirement itself.
89 #define CHILD_REQUIRE(exp) do { \
91 child_fail_require(__FILE__, __LINE__, \
96 child_fail_require(const char *file, int line, const char *str)
100 snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
101 write(2, buf, strlen(buf));
109 /* Attach the parent process as a tracer of this process. */
110 CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
112 /* Trigger a stop. */
117 attach_child(pid_t pid)
122 ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
124 wpid = waitpid(pid, &status, 0);
125 ATF_REQUIRE(wpid == pid);
126 ATF_REQUIRE(WIFSTOPPED(status));
127 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
131 wait_for_zombie(pid_t pid)
135 * Wait for a process to exit. This is kind of gross, but
136 * there is not a better way.
138 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
139 * with ESRCH. After that change, a valid struct kinfo_proc
140 * is returned for zombies with ki_stat set to SZOMB.
143 struct kinfo_proc kp;
149 mib[2] = KERN_PROC_PID;
152 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
153 ATF_REQUIRE(errno == ESRCH);
156 if (kp.ki_stat == SZOMB)
163 * Verify that a parent debugger process "sees" the exit of a debugged
164 * process exactly once when attached via PT_TRACE_ME.
166 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
167 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
172 ATF_REQUIRE((child = fork()) != -1);
180 /* Parent process. */
182 /* The first wait() should report the stop from SIGSTOP. */
183 wpid = waitpid(child, &status, 0);
184 ATF_REQUIRE(wpid == child);
185 ATF_REQUIRE(WIFSTOPPED(status));
186 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
188 /* Continue the child ignoring the SIGSTOP. */
189 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
191 /* The second wait() should report the exit status. */
192 wpid = waitpid(child, &status, 0);
193 ATF_REQUIRE(wpid == child);
194 ATF_REQUIRE(WIFEXITED(status));
195 ATF_REQUIRE(WEXITSTATUS(status) == 1);
197 /* The child should no longer exist. */
198 wpid = waitpid(child, &status, 0);
199 ATF_REQUIRE(wpid == -1);
200 ATF_REQUIRE(errno == ECHILD);
204 * Verify that a parent debugger process "sees" the exit of a debugged
205 * process exactly once when attached via PT_ATTACH.
207 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
208 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
211 int cpipe[2], status;
214 ATF_REQUIRE(pipe(cpipe) == 0);
215 ATF_REQUIRE((child = fork()) != -1);
220 /* Wait for the parent to attach. */
221 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
227 /* Parent process. */
229 /* Attach to the child process. */
232 /* Continue the child ignoring the SIGSTOP. */
233 ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
235 /* Signal the child to exit. */
238 /* The second wait() should report the exit status. */
239 wpid = waitpid(child, &status, 0);
240 ATF_REQUIRE(wpid == child);
241 ATF_REQUIRE(WIFEXITED(status));
242 ATF_REQUIRE(WEXITSTATUS(status) == 1);
244 /* The child should no longer exist. */
245 wpid = waitpid(child, &status, 0);
246 ATF_REQUIRE(wpid == -1);
247 ATF_REQUIRE(errno == ECHILD);
251 * Verify that a parent process "sees" the exit of a debugged process only
252 * after the debugger has seen it.
254 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
255 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
257 pid_t child, debugger, wpid;
258 int cpipe[2], dpipe[2], status;
261 ATF_REQUIRE(pipe(cpipe) == 0);
262 ATF_REQUIRE((child = fork()) != -1);
268 /* Wait for parent to be ready. */
269 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
275 ATF_REQUIRE(pipe(dpipe) == 0);
276 ATF_REQUIRE((debugger = fork()) != -1);
279 /* Debugger process. */
282 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
284 wpid = waitpid(child, &status, 0);
285 CHILD_REQUIRE(wpid == child);
286 CHILD_REQUIRE(WIFSTOPPED(status));
287 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
289 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
291 /* Signal parent that debugger is attached. */
292 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
294 /* Wait for parent's failed wait. */
295 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
297 wpid = waitpid(child, &status, 0);
298 CHILD_REQUIRE(wpid == child);
299 CHILD_REQUIRE(WIFEXITED(status));
300 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
306 /* Parent process. */
308 /* Wait for the debugger to attach to the child. */
309 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
311 /* Release the child. */
312 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
313 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
316 wait_for_zombie(child);
319 * This wait should return a pid of 0 to indicate no status to
320 * report. The parent should see the child as non-exited
321 * until the debugger sees the exit.
323 wpid = waitpid(child, &status, WNOHANG);
324 ATF_REQUIRE(wpid == 0);
326 /* Signal the debugger to wait for the child. */
329 /* Wait for the debugger. */
330 wpid = waitpid(debugger, &status, 0);
331 ATF_REQUIRE(wpid == debugger);
332 ATF_REQUIRE(WIFEXITED(status));
333 ATF_REQUIRE(WEXITSTATUS(status) == 0);
335 /* The child process should now be ready. */
336 wpid = waitpid(child, &status, WNOHANG);
337 ATF_REQUIRE(wpid == child);
338 ATF_REQUIRE(WIFEXITED(status));
339 ATF_REQUIRE(WEXITSTATUS(status) == 1);
343 * Verify that a parent process "sees" the exit of a debugged process
344 * only after a non-direct-child debugger has seen it. In particular,
345 * various wait() calls in the parent must avoid failing with ESRCH by
346 * checking the parent's orphan list for the debugee.
348 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
349 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
351 pid_t child, debugger, fpid, wpid;
352 int cpipe[2], dpipe[2], status;
355 ATF_REQUIRE(pipe(cpipe) == 0);
356 ATF_REQUIRE((child = fork()) != -1);
362 /* Wait for parent to be ready. */
363 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
369 ATF_REQUIRE(pipe(dpipe) == 0);
370 ATF_REQUIRE((debugger = fork()) != -1);
373 /* Debugger parent. */
376 * Fork again and drop the debugger parent so that the
377 * debugger is not a child of the main parent.
379 CHILD_REQUIRE((fpid = fork()) != -1);
383 /* Debugger process. */
386 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
388 wpid = waitpid(child, &status, 0);
389 CHILD_REQUIRE(wpid == child);
390 CHILD_REQUIRE(WIFSTOPPED(status));
391 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
393 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
395 /* Signal parent that debugger is attached. */
396 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
398 /* Wait for parent's failed wait. */
399 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
401 wpid = waitpid(child, &status, 0);
402 CHILD_REQUIRE(wpid == child);
403 CHILD_REQUIRE(WIFEXITED(status));
404 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
410 /* Parent process. */
412 /* Wait for the debugger parent process to exit. */
413 wpid = waitpid(debugger, &status, 0);
414 ATF_REQUIRE(wpid == debugger);
415 ATF_REQUIRE(WIFEXITED(status));
416 ATF_REQUIRE(WEXITSTATUS(status) == 2);
418 /* A WNOHANG wait here should see the non-exited child. */
419 wpid = waitpid(child, &status, WNOHANG);
420 ATF_REQUIRE(wpid == 0);
422 /* Wait for the debugger to attach to the child. */
423 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
425 /* Release the child. */
426 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
427 ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
430 wait_for_zombie(child);
433 * This wait should return a pid of 0 to indicate no status to
434 * report. The parent should see the child as non-exited
435 * until the debugger sees the exit.
437 wpid = waitpid(child, &status, WNOHANG);
438 ATF_REQUIRE(wpid == 0);
440 /* Signal the debugger to wait for the child. */
441 ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
443 /* Wait for the debugger. */
444 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
447 /* The child process should now be ready. */
448 wpid = waitpid(child, &status, WNOHANG);
449 ATF_REQUIRE(wpid == child);
450 ATF_REQUIRE(WIFEXITED(status));
451 ATF_REQUIRE(WEXITSTATUS(status) == 1);
455 * The parent process should always act the same regardless of how the
456 * debugger is attached to it.
459 follow_fork_parent(bool use_vfork)
465 CHILD_REQUIRE((fpid = vfork()) != -1);
467 CHILD_REQUIRE((fpid = fork()) != -1);
473 wpid = waitpid(fpid, &status, 0);
474 CHILD_REQUIRE(wpid == fpid);
475 CHILD_REQUIRE(WIFEXITED(status));
476 CHILD_REQUIRE(WEXITSTATUS(status) == 2);
482 * Helper routine for follow fork tests. This waits for two stops
483 * that report both "sides" of a fork. It returns the pid of the new
487 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
489 struct ptrace_lwpinfo pl;
490 bool fork_reported[2];
494 fork_reported[0] = false;
495 fork_reported[1] = false;
499 * Each process should report a fork event. The parent should
500 * report a PL_FLAG_FORKED event, and the child should report
501 * a PL_FLAG_CHILD event.
503 for (i = 0; i < 2; i++) {
504 wpid = wait(&status);
505 ATF_REQUIRE(wpid > 0);
506 ATF_REQUIRE(WIFSTOPPED(status));
508 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
510 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
512 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
513 (PL_FLAG_FORKED | PL_FLAG_CHILD));
514 if (pl.pl_flags & PL_FLAG_CHILD) {
515 ATF_REQUIRE(wpid != parent);
516 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
517 ATF_REQUIRE(!fork_reported[1]);
521 ATF_REQUIRE(child == wpid);
524 fork_reported[1] = true;
526 ATF_REQUIRE(wpid == parent);
527 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
528 ATF_REQUIRE(!fork_reported[0]);
530 child = pl.pl_child_pid;
532 ATF_REQUIRE(child == pl.pl_child_pid);
535 fork_reported[0] = true;
543 * Verify that a new child process is stopped after a followed fork and
544 * that the traced parent sees the exit of the child after the debugger
545 * when both processes remain attached to the debugger.
547 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
548 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
550 pid_t children[2], fpid, wpid;
553 ATF_REQUIRE((fpid = fork()) != -1);
556 follow_fork_parent(false);
559 /* Parent process. */
562 /* The first wait() should report the stop from SIGSTOP. */
563 wpid = waitpid(children[0], &status, 0);
564 ATF_REQUIRE(wpid == children[0]);
565 ATF_REQUIRE(WIFSTOPPED(status));
566 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
568 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
570 /* Continue the child ignoring the SIGSTOP. */
571 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
573 children[1] = handle_fork_events(children[0], NULL);
574 ATF_REQUIRE(children[1] > 0);
576 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
577 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
580 * The child can't exit until the grandchild reports status, so the
581 * grandchild should report its exit first to the debugger.
583 wpid = wait(&status);
584 ATF_REQUIRE(wpid == children[1]);
585 ATF_REQUIRE(WIFEXITED(status));
586 ATF_REQUIRE(WEXITSTATUS(status) == 2);
588 wpid = wait(&status);
589 ATF_REQUIRE(wpid == children[0]);
590 ATF_REQUIRE(WIFEXITED(status));
591 ATF_REQUIRE(WEXITSTATUS(status) == 1);
593 wpid = wait(&status);
594 ATF_REQUIRE(wpid == -1);
595 ATF_REQUIRE(errno == ECHILD);
599 * Verify that a new child process is stopped after a followed fork
600 * and that the traced parent sees the exit of the child when the new
601 * child process is detached after it reports its fork.
603 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
604 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
606 pid_t children[2], fpid, wpid;
609 ATF_REQUIRE((fpid = fork()) != -1);
612 follow_fork_parent(false);
615 /* Parent process. */
618 /* The first wait() should report the stop from SIGSTOP. */
619 wpid = waitpid(children[0], &status, 0);
620 ATF_REQUIRE(wpid == children[0]);
621 ATF_REQUIRE(WIFSTOPPED(status));
622 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
624 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
626 /* Continue the child ignoring the SIGSTOP. */
627 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
629 children[1] = handle_fork_events(children[0], NULL);
630 ATF_REQUIRE(children[1] > 0);
632 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
633 ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
636 * Should not see any status from the grandchild now, only the
639 wpid = wait(&status);
640 ATF_REQUIRE(wpid == children[0]);
641 ATF_REQUIRE(WIFEXITED(status));
642 ATF_REQUIRE(WEXITSTATUS(status) == 1);
644 wpid = wait(&status);
645 ATF_REQUIRE(wpid == -1);
646 ATF_REQUIRE(errno == ECHILD);
650 * Verify that a new child process is stopped after a followed fork
651 * and that the traced parent sees the exit of the child when the
652 * traced parent is detached after the fork.
654 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
655 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
657 pid_t children[2], fpid, wpid;
660 ATF_REQUIRE((fpid = fork()) != -1);
663 follow_fork_parent(false);
666 /* Parent process. */
669 /* The first wait() should report the stop from SIGSTOP. */
670 wpid = waitpid(children[0], &status, 0);
671 ATF_REQUIRE(wpid == children[0]);
672 ATF_REQUIRE(WIFSTOPPED(status));
673 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
675 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
677 /* Continue the child ignoring the SIGSTOP. */
678 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
680 children[1] = handle_fork_events(children[0], NULL);
681 ATF_REQUIRE(children[1] > 0);
683 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
684 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
687 * The child can't exit until the grandchild reports status, so the
688 * grandchild should report its exit first to the debugger.
690 * Even though the child process is detached, it is still a
691 * child of the debugger, so it will still report it's exit
692 * after the grandchild.
694 wpid = wait(&status);
695 ATF_REQUIRE(wpid == children[1]);
696 ATF_REQUIRE(WIFEXITED(status));
697 ATF_REQUIRE(WEXITSTATUS(status) == 2);
699 wpid = wait(&status);
700 ATF_REQUIRE(wpid == children[0]);
701 ATF_REQUIRE(WIFEXITED(status));
702 ATF_REQUIRE(WEXITSTATUS(status) == 1);
704 wpid = wait(&status);
705 ATF_REQUIRE(wpid == -1);
706 ATF_REQUIRE(errno == ECHILD);
710 attach_fork_parent(int cpipe[2])
716 /* Double-fork to disassociate from the debugger. */
717 CHILD_REQUIRE((fpid = fork()) != -1);
721 /* Send the pid of the disassociated child to the debugger. */
723 CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
725 /* Wait for the debugger to attach. */
726 CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
730 * Verify that a new child process is stopped after a followed fork and
731 * that the traced parent sees the exit of the child after the debugger
732 * when both processes remain attached to the debugger. In this test
733 * the parent that forks is not a direct child of the debugger.
735 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
736 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
738 pid_t children[2], fpid, wpid;
739 int cpipe[2], status;
741 ATF_REQUIRE(pipe(cpipe) == 0);
742 ATF_REQUIRE((fpid = fork()) != -1);
744 attach_fork_parent(cpipe);
745 follow_fork_parent(false);
748 /* Parent process. */
751 /* Wait for the direct child to exit. */
752 wpid = waitpid(fpid, &status, 0);
753 ATF_REQUIRE(wpid == fpid);
754 ATF_REQUIRE(WIFEXITED(status));
755 ATF_REQUIRE(WEXITSTATUS(status) == 3);
757 /* Read the pid of the fork parent. */
758 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
759 sizeof(children[0]));
761 /* Attach to the fork parent. */
762 attach_child(children[0]);
764 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
766 /* Continue the fork parent ignoring the SIGSTOP. */
767 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
769 /* Signal the fork parent to continue. */
772 children[1] = handle_fork_events(children[0], NULL);
773 ATF_REQUIRE(children[1] > 0);
775 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
776 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
779 * The fork parent can't exit until the child reports status,
780 * so the child should report its exit first to the debugger.
782 wpid = wait(&status);
783 ATF_REQUIRE(wpid == children[1]);
784 ATF_REQUIRE(WIFEXITED(status));
785 ATF_REQUIRE(WEXITSTATUS(status) == 2);
787 wpid = wait(&status);
788 ATF_REQUIRE(wpid == children[0]);
789 ATF_REQUIRE(WIFEXITED(status));
790 ATF_REQUIRE(WEXITSTATUS(status) == 1);
792 wpid = wait(&status);
793 ATF_REQUIRE(wpid == -1);
794 ATF_REQUIRE(errno == ECHILD);
798 * Verify that a new child process is stopped after a followed fork
799 * and that the traced parent sees the exit of the child when the new
800 * child process is detached after it reports its fork. In this test
801 * the parent that forks is not a direct child of the debugger.
803 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
804 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
806 pid_t children[2], fpid, wpid;
807 int cpipe[2], status;
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_DETACH, children[1], (caddr_t)1, 0) != -1);
847 * Should not see any status from the child now, only the fork
850 wpid = wait(&status);
851 ATF_REQUIRE(wpid == children[0]);
852 ATF_REQUIRE(WIFEXITED(status));
853 ATF_REQUIRE(WEXITSTATUS(status) == 1);
855 wpid = wait(&status);
856 ATF_REQUIRE(wpid == -1);
857 ATF_REQUIRE(errno == ECHILD);
861 * Verify that a new child process is stopped after a followed fork
862 * and that the traced parent sees the exit of the child when the
863 * traced parent is detached after the fork. In this test the parent
864 * that forks is not a direct child of the debugger.
866 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
867 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
869 pid_t children[2], fpid, wpid;
870 int cpipe[2], status;
872 ATF_REQUIRE(pipe(cpipe) == 0);
873 ATF_REQUIRE((fpid = fork()) != -1);
875 attach_fork_parent(cpipe);
876 follow_fork_parent(false);
879 /* Parent process. */
882 /* Wait for the direct child to exit. */
883 wpid = waitpid(fpid, &status, 0);
884 ATF_REQUIRE(wpid == fpid);
885 ATF_REQUIRE(WIFEXITED(status));
886 ATF_REQUIRE(WEXITSTATUS(status) == 3);
888 /* Read the pid of the fork parent. */
889 ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
890 sizeof(children[0]));
892 /* Attach to the fork parent. */
893 attach_child(children[0]);
895 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
897 /* Continue the fork parent ignoring the SIGSTOP. */
898 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
900 /* Signal the fork parent to continue. */
903 children[1] = handle_fork_events(children[0], NULL);
904 ATF_REQUIRE(children[1] > 0);
906 ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
907 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
910 * Should not see any status from the fork parent now, only
913 wpid = wait(&status);
914 ATF_REQUIRE(wpid == children[1]);
915 ATF_REQUIRE(WIFEXITED(status));
916 ATF_REQUIRE(WEXITSTATUS(status) == 2);
918 wpid = wait(&status);
919 ATF_REQUIRE(wpid == -1);
920 ATF_REQUIRE(errno == ECHILD);
924 * Verify that a child process does not see an unrelated debugger as its
925 * parent but sees its original parent process.
927 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
928 ATF_TC_BODY(ptrace__getppid, tc)
930 pid_t child, debugger, ppid, wpid;
931 int cpipe[2], dpipe[2], status;
934 ATF_REQUIRE(pipe(cpipe) == 0);
935 ATF_REQUIRE((child = fork()) != -1);
941 /* Wait for parent to be ready. */
942 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
944 /* Report the parent PID to the parent. */
946 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
953 ATF_REQUIRE(pipe(dpipe) == 0);
954 ATF_REQUIRE((debugger = fork()) != -1);
957 /* Debugger process. */
960 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
962 wpid = waitpid(child, &status, 0);
963 CHILD_REQUIRE(wpid == child);
964 CHILD_REQUIRE(WIFSTOPPED(status));
965 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
967 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
969 /* Signal parent that debugger is attached. */
970 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
972 /* Wait for traced child to exit. */
973 wpid = waitpid(child, &status, 0);
974 CHILD_REQUIRE(wpid == child);
975 CHILD_REQUIRE(WIFEXITED(status));
976 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
982 /* Parent process. */
984 /* Wait for the debugger to attach to the child. */
985 ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
987 /* Release the child. */
988 ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
990 /* Read the parent PID from the child. */
991 ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
994 ATF_REQUIRE(ppid == getpid());
996 /* Wait for the debugger. */
997 wpid = waitpid(debugger, &status, 0);
998 ATF_REQUIRE(wpid == debugger);
999 ATF_REQUIRE(WIFEXITED(status));
1000 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1002 /* The child process should now be ready. */
1003 wpid = waitpid(child, &status, WNOHANG);
1004 ATF_REQUIRE(wpid == child);
1005 ATF_REQUIRE(WIFEXITED(status));
1006 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1010 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1011 * child process created via fork() reports the correct value.
1013 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1014 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1016 struct ptrace_lwpinfo pl[2];
1017 pid_t children[2], fpid, wpid;
1020 ATF_REQUIRE((fpid = fork()) != -1);
1023 follow_fork_parent(false);
1026 /* Parent process. */
1029 /* The first wait() should report the stop from SIGSTOP. */
1030 wpid = waitpid(children[0], &status, 0);
1031 ATF_REQUIRE(wpid == children[0]);
1032 ATF_REQUIRE(WIFSTOPPED(status));
1033 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1035 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1037 /* Continue the child ignoring the SIGSTOP. */
1038 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1040 /* Wait for both halves of the fork event to get reported. */
1041 children[1] = handle_fork_events(children[0], pl);
1042 ATF_REQUIRE(children[1] > 0);
1044 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1045 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1046 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1047 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1048 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1050 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1051 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1054 * The child can't exit until the grandchild reports status, so the
1055 * grandchild should report its exit first to the debugger.
1057 wpid = wait(&status);
1058 ATF_REQUIRE(wpid == children[1]);
1059 ATF_REQUIRE(WIFEXITED(status));
1060 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1062 wpid = wait(&status);
1063 ATF_REQUIRE(wpid == children[0]);
1064 ATF_REQUIRE(WIFEXITED(status));
1065 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1067 wpid = wait(&status);
1068 ATF_REQUIRE(wpid == -1);
1069 ATF_REQUIRE(errno == ECHILD);
1073 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1074 * child process created via vfork() reports the correct value.
1076 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1077 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1079 struct ptrace_lwpinfo pl[2];
1080 pid_t children[2], fpid, wpid;
1083 ATF_REQUIRE((fpid = fork()) != -1);
1086 follow_fork_parent(true);
1089 /* Parent process. */
1092 /* The first wait() should report the stop from SIGSTOP. */
1093 wpid = waitpid(children[0], &status, 0);
1094 ATF_REQUIRE(wpid == children[0]);
1095 ATF_REQUIRE(WIFSTOPPED(status));
1096 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1098 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1100 /* Continue the child ignoring the SIGSTOP. */
1101 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1103 /* Wait for both halves of the fork event to get reported. */
1104 children[1] = handle_fork_events(children[0], pl);
1105 ATF_REQUIRE(children[1] > 0);
1107 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1108 ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1109 ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1110 ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1111 ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1113 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1114 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1117 * The child can't exit until the grandchild reports status, so the
1118 * grandchild should report its exit first to the debugger.
1120 wpid = wait(&status);
1121 ATF_REQUIRE(wpid == children[1]);
1122 ATF_REQUIRE(WIFEXITED(status));
1123 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1125 wpid = wait(&status);
1126 ATF_REQUIRE(wpid == children[0]);
1127 ATF_REQUIRE(WIFEXITED(status));
1128 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1130 wpid = wait(&status);
1131 ATF_REQUIRE(wpid == -1);
1132 ATF_REQUIRE(errno == ECHILD);
1136 simple_thread(void *arg __unused)
1143 simple_thread_main(void)
1147 CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1148 CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1153 * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1154 * thread reports the correct value.
1156 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1157 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1159 struct ptrace_lwpinfo pl;
1164 ATF_REQUIRE((fpid = fork()) != -1);
1167 simple_thread_main();
1170 /* The first wait() should report the stop from SIGSTOP. */
1171 wpid = waitpid(fpid, &status, 0);
1172 ATF_REQUIRE(wpid == fpid);
1173 ATF_REQUIRE(WIFSTOPPED(status));
1174 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1176 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1178 mainlwp = pl.pl_lwpid;
1181 * Continue the child ignoring the SIGSTOP and tracing all
1182 * system call exits.
1184 ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1187 * Wait for the new thread to arrive. pthread_create() might
1188 * invoke any number of system calls. For now we just wait
1189 * for the new thread to arrive and make sure it reports a
1190 * valid system call code. If ptrace grows thread event
1191 * reporting then this test can be made more precise.
1194 wpid = waitpid(fpid, &status, 0);
1195 ATF_REQUIRE(wpid == fpid);
1196 ATF_REQUIRE(WIFSTOPPED(status));
1197 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1199 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1201 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1202 ATF_REQUIRE(pl.pl_syscall_code != 0);
1203 if (pl.pl_lwpid != mainlwp)
1204 /* New thread seen. */
1207 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1210 /* Wait for the child to exit. */
1211 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1213 wpid = waitpid(fpid, &status, 0);
1214 ATF_REQUIRE(wpid == fpid);
1215 if (WIFEXITED(status))
1218 ATF_REQUIRE(WIFSTOPPED(status));
1219 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1220 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1223 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1225 wpid = wait(&status);
1226 ATF_REQUIRE(wpid == -1);
1227 ATF_REQUIRE(errno == ECHILD);
1231 * Verify that the expected LWP events are reported for a child thread.
1233 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1234 ATF_TC_BODY(ptrace__lwp_events, tc)
1236 struct ptrace_lwpinfo pl;
1241 ATF_REQUIRE((fpid = fork()) != -1);
1244 simple_thread_main();
1247 /* The first wait() should report the stop from SIGSTOP. */
1248 wpid = waitpid(fpid, &status, 0);
1249 ATF_REQUIRE(wpid == fpid);
1250 ATF_REQUIRE(WIFSTOPPED(status));
1251 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1253 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1255 lwps[0] = pl.pl_lwpid;
1257 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1259 /* Continue the child ignoring the SIGSTOP. */
1260 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1262 /* The first event should be for the child thread's birth. */
1263 wpid = waitpid(fpid, &status, 0);
1264 ATF_REQUIRE(wpid == fpid);
1265 ATF_REQUIRE(WIFSTOPPED(status));
1266 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1268 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1269 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1270 (PL_FLAG_BORN | PL_FLAG_SCX));
1271 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1272 lwps[1] = pl.pl_lwpid;
1274 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1276 /* The next event should be for the child thread's death. */
1277 wpid = waitpid(fpid, &status, 0);
1278 ATF_REQUIRE(wpid == fpid);
1279 ATF_REQUIRE(WIFSTOPPED(status));
1280 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1282 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1283 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1284 (PL_FLAG_EXITED | PL_FLAG_SCE));
1285 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1287 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1289 /* The last event should be for the child process's exit. */
1290 wpid = waitpid(fpid, &status, 0);
1291 ATF_REQUIRE(WIFEXITED(status));
1292 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1294 wpid = wait(&status);
1295 ATF_REQUIRE(wpid == -1);
1296 ATF_REQUIRE(errno == ECHILD);
1300 exec_thread(void *arg __unused)
1303 execl("/usr/bin/true", "true", NULL);
1308 exec_thread_main(void)
1312 CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1319 * Verify that the expected LWP events are reported for a multithreaded
1320 * process that calls execve(2).
1322 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1323 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1325 struct ptrace_lwpinfo pl;
1330 ATF_REQUIRE((fpid = fork()) != -1);
1336 /* The first wait() should report the stop from SIGSTOP. */
1337 wpid = waitpid(fpid, &status, 0);
1338 ATF_REQUIRE(wpid == fpid);
1339 ATF_REQUIRE(WIFSTOPPED(status));
1340 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1342 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1344 lwps[0] = pl.pl_lwpid;
1346 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1348 /* Continue the child ignoring the SIGSTOP. */
1349 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1351 /* The first event should be for the child thread's birth. */
1352 wpid = waitpid(fpid, &status, 0);
1353 ATF_REQUIRE(wpid == fpid);
1354 ATF_REQUIRE(WIFSTOPPED(status));
1355 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1357 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1358 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1359 (PL_FLAG_BORN | PL_FLAG_SCX));
1360 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1361 lwps[1] = pl.pl_lwpid;
1363 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1366 * The next event should be for the main thread's death due to
1367 * single threading from execve().
1369 wpid = waitpid(fpid, &status, 0);
1370 ATF_REQUIRE(wpid == fpid);
1371 ATF_REQUIRE(WIFSTOPPED(status));
1372 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1374 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1375 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1377 ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1379 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1381 /* The next event should be for the child process's exec. */
1382 wpid = waitpid(fpid, &status, 0);
1383 ATF_REQUIRE(WIFSTOPPED(status));
1384 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1386 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1387 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1388 (PL_FLAG_EXEC | PL_FLAG_SCX));
1389 ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1391 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1393 /* The last event should be for the child process's exit. */
1394 wpid = waitpid(fpid, &status, 0);
1395 ATF_REQUIRE(WIFEXITED(status));
1396 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1398 wpid = wait(&status);
1399 ATF_REQUIRE(wpid == -1);
1400 ATF_REQUIRE(errno == ECHILD);
1404 handler(int sig __unused)
1412 signal(SIGINFO, handler);
1418 * Verify that the expected ptrace event is reported for a signal.
1420 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1421 ATF_TC_BODY(ptrace__siginfo, tc)
1423 struct ptrace_lwpinfo pl;
1427 ATF_REQUIRE((fpid = fork()) != -1);
1433 /* The first wait() should report the stop from SIGSTOP. */
1434 wpid = waitpid(fpid, &status, 0);
1435 ATF_REQUIRE(wpid == fpid);
1436 ATF_REQUIRE(WIFSTOPPED(status));
1437 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1439 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1441 /* The next event should be for the SIGINFO. */
1442 wpid = waitpid(fpid, &status, 0);
1443 ATF_REQUIRE(WIFSTOPPED(status));
1444 ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1446 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1447 ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1448 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1449 ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1450 ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1452 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1454 /* The last event should be for the child process's exit. */
1455 wpid = waitpid(fpid, &status, 0);
1456 ATF_REQUIRE(WIFEXITED(status));
1457 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1459 wpid = wait(&status);
1460 ATF_REQUIRE(wpid == -1);
1461 ATF_REQUIRE(errno == ECHILD);
1465 * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1467 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1468 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1473 ATF_REQUIRE((fpid = fork()) != -1);
1479 /* The first wait() should report the stop from SIGSTOP. */
1480 wpid = waitpid(fpid, &status, 0);
1481 ATF_REQUIRE(wpid == fpid);
1482 ATF_REQUIRE(WIFSTOPPED(status));
1483 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1486 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1487 sizeof(events)) == 0);
1489 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1491 /* Should get one event at exit. */
1492 wpid = waitpid(fpid, &status, 0);
1493 ATF_REQUIRE(WIFEXITED(status));
1494 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1496 wpid = wait(&status);
1497 ATF_REQUIRE(wpid == -1);
1498 ATF_REQUIRE(errno == ECHILD);
1501 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1502 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1504 struct ptrace_lwpinfo pl;
1508 ATF_REQUIRE((fpid = fork()) != -1);
1514 /* The first wait() should report the stop from SIGSTOP. */
1515 wpid = waitpid(fpid, &status, 0);
1516 ATF_REQUIRE(wpid == fpid);
1517 ATF_REQUIRE(WIFSTOPPED(status));
1518 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1520 events = PTRACE_EXEC;
1521 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1522 sizeof(events)) == 0);
1524 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1526 /* The next event should be for the child process's exec. */
1527 wpid = waitpid(fpid, &status, 0);
1528 ATF_REQUIRE(WIFSTOPPED(status));
1529 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1531 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1532 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1533 (PL_FLAG_EXEC | PL_FLAG_SCX));
1535 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1537 /* The last event should be for the child process's exit. */
1538 wpid = waitpid(fpid, &status, 0);
1539 ATF_REQUIRE(WIFEXITED(status));
1540 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1542 wpid = wait(&status);
1543 ATF_REQUIRE(wpid == -1);
1544 ATF_REQUIRE(errno == ECHILD);
1547 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1548 ATF_TC_BODY(ptrace__event_mask, tc)
1553 ATF_REQUIRE((fpid = fork()) != -1);
1559 /* The first wait() should report the stop from SIGSTOP. */
1560 wpid = waitpid(fpid, &status, 0);
1561 ATF_REQUIRE(wpid == fpid);
1562 ATF_REQUIRE(WIFSTOPPED(status));
1563 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1565 /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1566 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1567 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1568 sizeof(events)) == 0);
1569 ATF_REQUIRE(events & PTRACE_FORK);
1570 ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1571 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1572 sizeof(events)) == 0);
1573 ATF_REQUIRE(!(events & PTRACE_FORK));
1575 /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1576 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1577 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1578 sizeof(events)) == 0);
1579 ATF_REQUIRE(events & PTRACE_LWP);
1580 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1581 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1582 sizeof(events)) == 0);
1583 ATF_REQUIRE(!(events & PTRACE_LWP));
1585 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1587 /* Should get one event at exit. */
1588 wpid = waitpid(fpid, &status, 0);
1589 ATF_REQUIRE(WIFEXITED(status));
1590 ATF_REQUIRE(WEXITSTATUS(status) == 0);
1592 wpid = wait(&status);
1593 ATF_REQUIRE(wpid == -1);
1594 ATF_REQUIRE(errno == ECHILD);
1598 * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1600 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1601 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1603 struct ptrace_lwpinfo pl;
1607 ATF_REQUIRE((fpid = fork()) != -1);
1610 follow_fork_parent(true);
1613 /* The first wait() should report the stop from SIGSTOP. */
1614 wpid = waitpid(fpid, &status, 0);
1615 ATF_REQUIRE(wpid == fpid);
1616 ATF_REQUIRE(WIFSTOPPED(status));
1617 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1619 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1620 sizeof(events)) == 0);
1621 events |= PTRACE_VFORK;
1622 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1623 sizeof(events)) == 0);
1625 /* Continue the child ignoring the SIGSTOP. */
1626 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1628 /* The next event should report the end of the vfork. */
1629 wpid = wait(&status);
1630 ATF_REQUIRE(wpid == fpid);
1631 ATF_REQUIRE(WIFSTOPPED(status));
1632 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1633 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1634 ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1636 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1638 wpid = wait(&status);
1639 ATF_REQUIRE(wpid == fpid);
1640 ATF_REQUIRE(WIFEXITED(status));
1641 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1643 wpid = wait(&status);
1644 ATF_REQUIRE(wpid == -1);
1645 ATF_REQUIRE(errno == ECHILD);
1648 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1649 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1651 struct ptrace_lwpinfo pl[2];
1652 pid_t children[2], fpid, wpid;
1655 ATF_REQUIRE((fpid = fork()) != -1);
1658 follow_fork_parent(true);
1661 /* Parent process. */
1664 /* The first wait() should report the stop from SIGSTOP. */
1665 wpid = waitpid(children[0], &status, 0);
1666 ATF_REQUIRE(wpid == children[0]);
1667 ATF_REQUIRE(WIFSTOPPED(status));
1668 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1670 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1671 sizeof(events)) == 0);
1672 events |= PTRACE_FORK | PTRACE_VFORK;
1673 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1674 sizeof(events)) == 0);
1676 /* Continue the child ignoring the SIGSTOP. */
1677 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1679 /* Wait for both halves of the fork event to get reported. */
1680 children[1] = handle_fork_events(children[0], pl);
1681 ATF_REQUIRE(children[1] > 0);
1683 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1685 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1686 ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1689 * The child can't exit until the grandchild reports status, so the
1690 * grandchild should report its exit first to the debugger.
1692 wpid = waitpid(children[1], &status, 0);
1693 ATF_REQUIRE(wpid == children[1]);
1694 ATF_REQUIRE(WIFEXITED(status));
1695 ATF_REQUIRE(WEXITSTATUS(status) == 2);
1698 * The child should report it's vfork() completion before it
1701 wpid = wait(&status);
1702 ATF_REQUIRE(wpid == children[0]);
1703 ATF_REQUIRE(WIFSTOPPED(status));
1704 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1705 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1707 ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1709 ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1711 wpid = wait(&status);
1712 ATF_REQUIRE(wpid == children[0]);
1713 ATF_REQUIRE(WIFEXITED(status));
1714 ATF_REQUIRE(WEXITSTATUS(status) == 1);
1716 wpid = wait(&status);
1717 ATF_REQUIRE(wpid == -1);
1718 ATF_REQUIRE(errno == ECHILD);
1721 #ifdef HAVE_BREAKPOINT
1723 * Verify that no more events are reported after PT_KILL except for the
1724 * process exit when stopped due to a breakpoint trap.
1726 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1727 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1732 ATF_REQUIRE((fpid = fork()) != -1);
1739 /* The first wait() should report the stop from SIGSTOP. */
1740 wpid = waitpid(fpid, &status, 0);
1741 ATF_REQUIRE(wpid == fpid);
1742 ATF_REQUIRE(WIFSTOPPED(status));
1743 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1745 /* Continue the child ignoring the SIGSTOP. */
1746 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1748 /* The second wait() should report hitting the breakpoint. */
1749 wpid = waitpid(fpid, &status, 0);
1750 ATF_REQUIRE(wpid == fpid);
1751 ATF_REQUIRE(WIFSTOPPED(status));
1752 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1754 /* Kill the child process. */
1755 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1757 /* The last wait() should report the SIGKILL. */
1758 wpid = waitpid(fpid, &status, 0);
1759 ATF_REQUIRE(wpid == fpid);
1760 ATF_REQUIRE(WIFSIGNALED(status));
1761 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1763 wpid = wait(&status);
1764 ATF_REQUIRE(wpid == -1);
1765 ATF_REQUIRE(errno == ECHILD);
1767 #endif /* HAVE_BREAKPOINT */
1770 * Verify that no more events are reported after PT_KILL except for the
1771 * process exit when stopped inside of a system call.
1773 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1774 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1776 struct ptrace_lwpinfo pl;
1780 ATF_REQUIRE((fpid = fork()) != -1);
1787 /* The first wait() should report the stop from SIGSTOP. */
1788 wpid = waitpid(fpid, &status, 0);
1789 ATF_REQUIRE(wpid == fpid);
1790 ATF_REQUIRE(WIFSTOPPED(status));
1791 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1793 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1794 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1796 /* The second wait() should report a system call entry for getpid(). */
1797 wpid = waitpid(fpid, &status, 0);
1798 ATF_REQUIRE(wpid == fpid);
1799 ATF_REQUIRE(WIFSTOPPED(status));
1800 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1802 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1803 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1805 /* Kill the child process. */
1806 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1808 /* The last wait() should report the SIGKILL. */
1809 wpid = waitpid(fpid, &status, 0);
1810 ATF_REQUIRE(wpid == fpid);
1811 ATF_REQUIRE(WIFSIGNALED(status));
1812 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1814 wpid = wait(&status);
1815 ATF_REQUIRE(wpid == -1);
1816 ATF_REQUIRE(errno == ECHILD);
1820 * Verify that no more events are reported after PT_KILL except for the
1821 * process exit when killing a multithreaded process.
1823 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1824 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1826 struct ptrace_lwpinfo pl;
1831 ATF_REQUIRE((fpid = fork()) != -1);
1834 simple_thread_main();
1837 /* The first wait() should report the stop from SIGSTOP. */
1838 wpid = waitpid(fpid, &status, 0);
1839 ATF_REQUIRE(wpid == fpid);
1840 ATF_REQUIRE(WIFSTOPPED(status));
1841 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1843 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1845 main_lwp = pl.pl_lwpid;
1847 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1849 /* Continue the child ignoring the SIGSTOP. */
1850 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1852 /* The first event should be for the child thread's birth. */
1853 wpid = waitpid(fpid, &status, 0);
1854 ATF_REQUIRE(wpid == fpid);
1855 ATF_REQUIRE(WIFSTOPPED(status));
1856 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1858 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1859 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1860 (PL_FLAG_BORN | PL_FLAG_SCX));
1861 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1863 /* Kill the child process. */
1864 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1866 /* The last wait() should report the SIGKILL. */
1867 wpid = waitpid(fpid, &status, 0);
1868 ATF_REQUIRE(wpid == fpid);
1869 ATF_REQUIRE(WIFSIGNALED(status));
1870 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1872 wpid = wait(&status);
1873 ATF_REQUIRE(wpid == -1);
1874 ATF_REQUIRE(errno == ECHILD);
1878 mask_usr1_thread(void *arg)
1880 pthread_barrier_t *pbarrier;
1883 pbarrier = (pthread_barrier_t*)arg;
1885 sigemptyset(&sigmask);
1886 sigaddset(&sigmask, SIGUSR1);
1887 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1889 /* Sync up with other thread after sigmask updated. */
1890 pthread_barrier_wait(pbarrier);
1899 * Verify that the SIGKILL from PT_KILL takes priority over other signals
1900 * and prevents spurious stops due to those other signals.
1902 ATF_TC(ptrace__PT_KILL_competing_signal);
1903 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1906 atf_tc_set_md_var(tc, "require.user", "root");
1908 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1914 pthread_barrier_t barrier;
1915 struct sched_param sched_param;
1917 ATF_REQUIRE((fpid = fork()) != -1);
1919 /* Bind to one CPU so only one thread at a time will run. */
1921 CPU_SET(0, &setmask);
1923 CHILD_REQUIRE(cpuset(&setid) == 0);
1924 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1925 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1927 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1929 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1930 (void*)&barrier) == 0);
1933 * Give the main thread higher priority. The test always
1934 * assumes that, if both threads are able to run, the main
1935 * thread runs first.
1937 sched_param.sched_priority =
1938 (sched_get_priority_max(SCHED_FIFO) +
1939 sched_get_priority_min(SCHED_FIFO)) / 2;
1940 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1941 SCHED_FIFO, &sched_param) == 0);
1942 sched_param.sched_priority -= RQ_PPQ;
1943 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1944 &sched_param) == 0);
1947 sigemptyset(&sigmask);
1948 sigaddset(&sigmask, SIGUSR2);
1949 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1951 /* Sync up with other thread after sigmask updated. */
1952 pthread_barrier_wait(&barrier);
1962 /* The first wait() should report the stop from SIGSTOP. */
1963 wpid = waitpid(fpid, &status, 0);
1964 ATF_REQUIRE(wpid == fpid);
1965 ATF_REQUIRE(WIFSTOPPED(status));
1966 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1968 /* Continue the child ignoring the SIGSTOP. */
1969 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1971 /* Send a signal that only the second thread can handle. */
1972 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1974 /* The second wait() should report the SIGUSR2. */
1975 wpid = waitpid(fpid, &status, 0);
1976 ATF_REQUIRE(wpid == fpid);
1977 ATF_REQUIRE(WIFSTOPPED(status));
1978 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1980 /* Send a signal that only the first thread can handle. */
1981 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1983 /* Replace the SIGUSR2 with a kill. */
1984 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1986 /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1987 wpid = waitpid(fpid, &status, 0);
1988 ATF_REQUIRE(wpid == fpid);
1989 ATF_REQUIRE(WIFSIGNALED(status));
1990 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1992 wpid = wait(&status);
1993 ATF_REQUIRE(wpid == -1);
1994 ATF_REQUIRE(errno == ECHILD);
1998 * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1999 * and prevents spurious stops caused by those events.
2001 ATF_TC(ptrace__PT_KILL_competing_stop);
2002 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2005 atf_tc_set_md_var(tc, "require.user", "root");
2007 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2013 pthread_barrier_t barrier;
2015 struct ptrace_lwpinfo pl;
2016 struct sched_param sched_param;
2018 ATF_REQUIRE((fpid = fork()) != -1);
2022 /* Bind to one CPU so only one thread at a time will run. */
2024 CPU_SET(0, &setmask);
2026 CHILD_REQUIRE(cpuset(&setid) == 0);
2027 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2028 CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2030 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2032 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2033 (void*)&barrier) == 0);
2036 * Give the main thread higher priority. The test always
2037 * assumes that, if both threads are able to run, the main
2038 * thread runs first.
2040 sched_param.sched_priority =
2041 (sched_get_priority_max(SCHED_FIFO) +
2042 sched_get_priority_min(SCHED_FIFO)) / 2;
2043 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2044 SCHED_FIFO, &sched_param) == 0);
2045 sched_param.sched_priority -= RQ_PPQ;
2046 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2047 &sched_param) == 0);
2050 sigemptyset(&sigmask);
2051 sigaddset(&sigmask, SIGUSR2);
2052 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2054 /* Sync up with other thread after sigmask updated. */
2055 pthread_barrier_wait(&barrier);
2057 /* Sync up with the test before doing the getpid(). */
2064 /* The first wait() should report the stop from SIGSTOP. */
2065 wpid = waitpid(fpid, &status, 0);
2066 ATF_REQUIRE(wpid == fpid);
2067 ATF_REQUIRE(WIFSTOPPED(status));
2068 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2070 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2071 main_lwp = pl.pl_lwpid;
2073 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2074 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2077 * Continue until child is done with setup, which is indicated with
2078 * SIGSTOP. Ignore system calls in the meantime.
2081 wpid = waitpid(fpid, &status, 0);
2082 ATF_REQUIRE(wpid == fpid);
2083 ATF_REQUIRE(WIFSTOPPED(status));
2084 if (WSTOPSIG(status) == SIGTRAP) {
2085 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2087 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2089 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2092 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2095 /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2096 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2098 wpid = waitpid(fpid, &status, 0);
2099 ATF_REQUIRE(wpid == fpid);
2100 ATF_REQUIRE(WIFSTOPPED(status));
2101 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2103 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2105 ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2106 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2107 /* Prevent the main thread from hitting its syscall exit for now. */
2108 ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2111 * Proceed, allowing second thread to hit syscall exit for
2112 * pthread_barrier_wait().
2114 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2116 wpid = waitpid(fpid, &status, 0);
2117 ATF_REQUIRE(wpid == fpid);
2118 ATF_REQUIRE(WIFSTOPPED(status));
2119 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2121 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2123 ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2124 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2126 /* Send a signal that only the second thread can handle. */
2127 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2129 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2131 /* The next wait() should report the SIGUSR2. */
2132 wpid = waitpid(fpid, &status, 0);
2133 ATF_REQUIRE(wpid == fpid);
2134 ATF_REQUIRE(WIFSTOPPED(status));
2135 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2137 /* Allow the main thread to try to finish its system call. */
2138 ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2141 * At this point, the main thread is in the middle of a system call and
2142 * has been resumed. The second thread has taken a SIGUSR2 which will
2143 * be replaced with a SIGKILL below. The main thread will get to run
2144 * first. It should notice the kill request (even though the signal
2145 * replacement occurred in the other thread) and exit accordingly. It
2146 * should not stop for the system call exit event.
2149 /* Replace the SIGUSR2 with a kill. */
2150 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2152 /* The last wait() should report the SIGKILL (not a syscall exit). */
2153 wpid = waitpid(fpid, &status, 0);
2154 ATF_REQUIRE(wpid == fpid);
2155 ATF_REQUIRE(WIFSIGNALED(status));
2156 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2158 wpid = wait(&status);
2159 ATF_REQUIRE(wpid == -1);
2160 ATF_REQUIRE(errno == ECHILD);
2164 sigusr1_handler(int sig)
2167 CHILD_REQUIRE(sig == SIGUSR1);
2172 * Verify that even if the signal queue is full for a child process,
2173 * a PT_KILL will kill the process.
2175 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2176 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2180 int max_pending_per_proc;
2184 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2186 ATF_REQUIRE((fpid = fork()) != -1);
2192 /* The first wait() should report the stop from SIGSTOP. */
2193 wpid = waitpid(fpid, &status, 0);
2194 ATF_REQUIRE(wpid == fpid);
2195 ATF_REQUIRE(WIFSTOPPED(status));
2196 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2198 len = sizeof(max_pending_per_proc);
2199 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2200 &max_pending_per_proc, &len, NULL, 0) == 0);
2202 /* Fill the signal queue. */
2203 for (i = 0; i < max_pending_per_proc; ++i)
2204 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2206 /* Kill the child process. */
2207 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2209 /* The last wait() should report the SIGKILL. */
2210 wpid = waitpid(fpid, &status, 0);
2211 ATF_REQUIRE(wpid == fpid);
2212 ATF_REQUIRE(WIFSIGNALED(status));
2213 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2215 wpid = wait(&status);
2216 ATF_REQUIRE(wpid == -1);
2217 ATF_REQUIRE(errno == ECHILD);
2221 * Verify that when stopped at a system call entry, a signal can be
2222 * requested with PT_CONTINUE which will be delivered once the system
2225 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2226 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2228 struct ptrace_lwpinfo pl;
2232 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2234 ATF_REQUIRE((fpid = fork()) != -1);
2241 /* The first wait() should report the stop from SIGSTOP. */
2242 wpid = waitpid(fpid, &status, 0);
2243 ATF_REQUIRE(wpid == fpid);
2244 ATF_REQUIRE(WIFSTOPPED(status));
2245 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2247 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2248 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2250 /* The second wait() should report a system call entry for getpid(). */
2251 wpid = waitpid(fpid, &status, 0);
2252 ATF_REQUIRE(wpid == fpid);
2253 ATF_REQUIRE(WIFSTOPPED(status));
2254 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2256 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2257 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2259 /* Continue the child process with a signal. */
2260 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2264 * The last wait() should report exit 2, i.e., a normal _exit
2265 * from the signal handler. In the meantime, catch and proceed
2266 * past any syscall stops.
2268 wpid = waitpid(fpid, &status, 0);
2269 ATF_REQUIRE(wpid == fpid);
2270 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2271 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2272 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2273 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2275 ATF_REQUIRE(WIFEXITED(status));
2276 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2281 wpid = wait(&status);
2282 ATF_REQUIRE(wpid == -1);
2283 ATF_REQUIRE(errno == ECHILD);
2287 sigusr1_counting_handler(int sig)
2289 static int counter = 0;
2291 CHILD_REQUIRE(sig == SIGUSR1);
2298 * Verify that, when continuing from a stop at system call entry and exit,
2299 * a signal can be requested from both stops, and both will be delivered when
2300 * the system call is complete.
2302 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2303 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2305 struct ptrace_lwpinfo pl;
2309 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_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);
2339 /* The third wait() should report a system call exit for getpid(). */
2340 wpid = waitpid(fpid, &status, 0);
2341 ATF_REQUIRE(wpid == fpid);
2342 ATF_REQUIRE(WIFSTOPPED(status));
2343 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2345 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2346 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2348 /* Continue the child process with a signal. */
2349 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2353 * The last wait() should report exit 2, i.e., a normal _exit
2354 * from the signal handler. In the meantime, catch and proceed
2355 * past any syscall stops.
2357 wpid = waitpid(fpid, &status, 0);
2358 ATF_REQUIRE(wpid == fpid);
2359 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2360 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2361 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2362 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2364 ATF_REQUIRE(WIFEXITED(status));
2365 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2370 wpid = wait(&status);
2371 ATF_REQUIRE(wpid == -1);
2372 ATF_REQUIRE(errno == ECHILD);
2376 * Verify that even if the signal queue is full for a child process,
2377 * a PT_CONTINUE with a signal will not result in loss of that signal.
2379 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2380 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2384 int max_pending_per_proc;
2388 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2389 ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2391 ATF_REQUIRE((fpid = fork()) != -1);
2397 /* The first wait() should report the stop from SIGSTOP. */
2398 wpid = waitpid(fpid, &status, 0);
2399 ATF_REQUIRE(wpid == fpid);
2400 ATF_REQUIRE(WIFSTOPPED(status));
2401 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2403 len = sizeof(max_pending_per_proc);
2404 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2405 &max_pending_per_proc, &len, NULL, 0) == 0);
2407 /* Fill the signal queue. */
2408 for (i = 0; i < max_pending_per_proc; ++i)
2409 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2411 /* Continue with signal. */
2412 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2415 wpid = waitpid(fpid, &status, 0);
2416 ATF_REQUIRE(wpid == fpid);
2417 if (WIFSTOPPED(status)) {
2418 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2419 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2422 * The last wait() should report normal _exit from the
2425 ATF_REQUIRE(WIFEXITED(status));
2426 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2431 wpid = wait(&status);
2432 ATF_REQUIRE(wpid == -1);
2433 ATF_REQUIRE(errno == ECHILD);
2436 static sem_t sigusr1_sem;
2437 static int got_usr1;
2440 sigusr1_sempost_handler(int sig __unused)
2444 CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2448 * Verify that even if the signal queue is full for a child process,
2449 * and the signal is masked, a PT_CONTINUE with a signal will not
2450 * result in loss of that signal.
2452 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2453 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2455 struct ptrace_lwpinfo pl;
2458 int max_pending_per_proc;
2463 ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2464 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2465 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2468 ATF_REQUIRE((fpid = fork()) != -1);
2470 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2471 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2472 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2475 CHILD_REQUIRE(got_usr1 == 0);
2477 /* Allow the pending SIGUSR1 in now. */
2478 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2479 /* Wait to receive the SIGUSR1. */
2481 err = sem_wait(&sigusr1_sem);
2482 CHILD_REQUIRE(err == 0 || errno == EINTR);
2483 } while (err != 0 && errno == EINTR);
2484 CHILD_REQUIRE(got_usr1 == 1);
2488 /* The first wait() should report the stop from SIGSTOP. */
2489 wpid = waitpid(fpid, &status, 0);
2490 ATF_REQUIRE(wpid == fpid);
2491 ATF_REQUIRE(WIFSTOPPED(status));
2492 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2494 len = sizeof(max_pending_per_proc);
2495 ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2496 &max_pending_per_proc, &len, NULL, 0) == 0);
2498 /* Fill the signal queue. */
2499 for (i = 0; i < max_pending_per_proc; ++i)
2500 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2502 /* Continue with signal. */
2503 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2505 /* Collect and ignore all of the SIGUSR2. */
2506 for (i = 0; i < max_pending_per_proc; ++i) {
2507 wpid = waitpid(fpid, &status, 0);
2508 ATF_REQUIRE(wpid == fpid);
2509 ATF_REQUIRE(WIFSTOPPED(status));
2510 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2511 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2514 /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2515 wpid = waitpid(fpid, &status, 0);
2516 ATF_REQUIRE(wpid == fpid);
2517 ATF_REQUIRE(WIFSTOPPED(status));
2518 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2519 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2520 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2522 /* Continue the child, ignoring the SIGUSR1. */
2523 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2525 /* The last wait() should report exit after receiving SIGUSR1. */
2526 wpid = waitpid(fpid, &status, 0);
2527 ATF_REQUIRE(wpid == fpid);
2528 ATF_REQUIRE(WIFEXITED(status));
2529 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2531 wpid = wait(&status);
2532 ATF_REQUIRE(wpid == -1);
2533 ATF_REQUIRE(errno == ECHILD);
2537 * Verify that, after stopping due to a signal, that signal can be
2538 * replaced with another signal.
2540 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2541 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2543 struct ptrace_lwpinfo pl;
2547 ATF_REQUIRE((fpid = fork()) != -1);
2554 /* The first wait() should report the stop from SIGSTOP. */
2555 wpid = waitpid(fpid, &status, 0);
2556 ATF_REQUIRE(wpid == fpid);
2557 ATF_REQUIRE(WIFSTOPPED(status));
2558 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2560 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2562 /* Send a signal without ptrace. */
2563 ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2565 /* The second wait() should report a SIGINT was received. */
2566 wpid = waitpid(fpid, &status, 0);
2567 ATF_REQUIRE(wpid == fpid);
2568 ATF_REQUIRE(WIFSTOPPED(status));
2569 ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2571 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2572 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2573 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2575 /* Continue the child process with a different signal. */
2576 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2579 * The last wait() should report having died due to the new
2582 wpid = waitpid(fpid, &status, 0);
2583 ATF_REQUIRE(wpid == fpid);
2584 ATF_REQUIRE(WIFSIGNALED(status));
2585 ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2587 wpid = wait(&status);
2588 ATF_REQUIRE(wpid == -1);
2589 ATF_REQUIRE(errno == ECHILD);
2593 * Verify that a signal can be passed through to the child even when there
2594 * was no true signal originally. Such cases arise when a SIGTRAP is
2595 * invented for e.g, system call stops.
2597 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2598 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2600 struct ptrace_lwpinfo pl;
2605 ATF_REQUIRE((fpid = fork()) != -1);
2608 /* SIGTRAP expected to cause exit on syscall entry. */
2609 rl.rlim_cur = rl.rlim_max = 0;
2610 ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2615 /* The first wait() should report the stop from SIGSTOP. */
2616 wpid = waitpid(fpid, &status, 0);
2617 ATF_REQUIRE(wpid == fpid);
2618 ATF_REQUIRE(WIFSTOPPED(status));
2619 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2621 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2622 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2624 /* The second wait() should report a system call entry for getpid(). */
2625 wpid = waitpid(fpid, &status, 0);
2626 ATF_REQUIRE(wpid == fpid);
2627 ATF_REQUIRE(WIFSTOPPED(status));
2628 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2630 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2631 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2633 /* Continue the child process with a SIGTRAP. */
2634 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2638 * The last wait() should report exit due to SIGTRAP. In the
2639 * meantime, catch and proceed past any syscall stops.
2641 wpid = waitpid(fpid, &status, 0);
2642 ATF_REQUIRE(wpid == fpid);
2643 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2644 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2645 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2646 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2648 ATF_REQUIRE(WIFSIGNALED(status));
2649 ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2654 wpid = wait(&status);
2655 ATF_REQUIRE(wpid == -1);
2656 ATF_REQUIRE(errno == ECHILD);
2661 * A mixed bag PT_CONTINUE with signal test.
2663 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2664 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2666 struct ptrace_lwpinfo pl;
2670 ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2672 ATF_REQUIRE((fpid = fork()) != -1);
2679 /* The first wait() should report the stop from SIGSTOP. */
2680 wpid = waitpid(fpid, &status, 0);
2681 ATF_REQUIRE(wpid == fpid);
2682 ATF_REQUIRE(WIFSTOPPED(status));
2683 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2685 /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2686 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2688 /* The second wait() should report a system call entry for getpid(). */
2689 wpid = waitpid(fpid, &status, 0);
2690 ATF_REQUIRE(wpid == fpid);
2691 ATF_REQUIRE(WIFSTOPPED(status));
2692 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2694 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2695 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2697 /* Continue with the first SIGUSR1. */
2698 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2700 /* The next wait() should report a system call exit for getpid(). */
2701 wpid = waitpid(fpid, &status, 0);
2702 ATF_REQUIRE(wpid == fpid);
2703 ATF_REQUIRE(WIFSTOPPED(status));
2704 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2706 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2707 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2709 /* Send an ABRT without ptrace. */
2710 ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2712 /* Continue normally. */
2713 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2715 /* The next wait() should report the SIGABRT. */
2716 wpid = waitpid(fpid, &status, 0);
2717 ATF_REQUIRE(wpid == fpid);
2718 ATF_REQUIRE(WIFSTOPPED(status));
2719 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2721 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2722 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2723 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2725 /* Continue, replacing the SIGABRT with another SIGUSR1. */
2726 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2730 * The last wait() should report exit 2, i.e., a normal _exit
2731 * from the signal handler. In the meantime, catch and proceed
2732 * past any syscall stops.
2734 wpid = waitpid(fpid, &status, 0);
2735 ATF_REQUIRE(wpid == fpid);
2736 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2737 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2738 ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2739 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2741 ATF_REQUIRE(WIFEXITED(status));
2742 ATF_REQUIRE(WEXITSTATUS(status) == 2);
2747 wpid = wait(&status);
2748 ATF_REQUIRE(wpid == -1);
2749 ATF_REQUIRE(errno == ECHILD);
2754 * Verify a signal delivered by ptrace is noticed by kevent(2).
2756 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2757 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2760 int status, kq, nevents;
2763 ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2765 ATF_REQUIRE((fpid = fork()) != -1);
2767 CHILD_REQUIRE((kq = kqueue()) > 0);
2768 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2769 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2774 nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2775 if (nevents == -1 && errno == EINTR)
2777 CHILD_REQUIRE(nevents > 0);
2778 CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2779 CHILD_REQUIRE(kev.ident == SIGUSR1);
2786 /* The first wait() should report the stop from SIGSTOP. */
2787 wpid = waitpid(fpid, &status, 0);
2788 ATF_REQUIRE(wpid == fpid);
2789 ATF_REQUIRE(WIFSTOPPED(status));
2790 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2792 /* Continue with the SIGUSR1. */
2793 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2796 * The last wait() should report normal exit with code 1.
2798 wpid = waitpid(fpid, &status, 0);
2799 ATF_REQUIRE(wpid == fpid);
2800 ATF_REQUIRE(WIFEXITED(status));
2801 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2803 wpid = wait(&status);
2804 ATF_REQUIRE(wpid == -1);
2805 ATF_REQUIRE(errno == ECHILD);
2809 signal_thread(void *arg)
2814 pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2816 /* Wait for this thread to receive a SIGUSR1. */
2818 err = sem_wait(&sigusr1_sem);
2819 CHILD_REQUIRE(err == 0 || errno == EINTR);
2820 } while (err != 0 && errno == EINTR);
2822 /* Free our companion thread from the barrier. */
2823 pthread_barrier_wait(pbarrier);
2826 * Swap ignore duties; the next SIGUSR1 should go to the
2829 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2830 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2831 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2833 /* Sync up threads after swapping signal masks. */
2834 pthread_barrier_wait(pbarrier);
2836 /* Wait until our companion has received its SIGUSR1. */
2837 pthread_barrier_wait(pbarrier);
2843 * Verify that a traced process with blocked signal received the
2844 * signal from kill() once unmasked.
2846 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2847 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2849 struct ptrace_lwpinfo pl;
2854 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2855 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2858 ATF_REQUIRE((fpid = fork()) != -1);
2860 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2861 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2862 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2865 CHILD_REQUIRE(got_usr1 == 0);
2867 /* Allow the pending SIGUSR1 in now. */
2868 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2869 /* Wait to receive a SIGUSR1. */
2871 err = sem_wait(&sigusr1_sem);
2872 CHILD_REQUIRE(err == 0 || errno == EINTR);
2873 } while (err != 0 && errno == EINTR);
2874 CHILD_REQUIRE(got_usr1 == 1);
2878 /* The first wait() should report the stop from SIGSTOP. */
2879 wpid = waitpid(fpid, &status, 0);
2880 ATF_REQUIRE(wpid == fpid);
2881 ATF_REQUIRE(WIFSTOPPED(status));
2882 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2883 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2884 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2886 /* Send blocked SIGUSR1 which should cause a stop. */
2887 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2889 /* Continue the child ignoring the SIGSTOP. */
2890 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2892 /* The next wait() should report the kill(SIGUSR1) was received. */
2893 wpid = waitpid(fpid, &status, 0);
2894 ATF_REQUIRE(wpid == fpid);
2895 ATF_REQUIRE(WIFSTOPPED(status));
2896 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2897 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2898 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2900 /* Continue the child, allowing in the SIGUSR1. */
2901 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2903 /* The last wait() should report normal exit with code 1. */
2904 wpid = waitpid(fpid, &status, 0);
2905 ATF_REQUIRE(wpid == fpid);
2906 ATF_REQUIRE(WIFEXITED(status));
2907 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2909 wpid = wait(&status);
2910 ATF_REQUIRE(wpid == -1);
2911 ATF_REQUIRE(errno == ECHILD);
2915 * Verify that a traced process with blocked signal received the
2916 * signal from PT_CONTINUE once unmasked.
2918 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2919 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2921 struct ptrace_lwpinfo pl;
2926 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2927 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2930 ATF_REQUIRE((fpid = fork()) != -1);
2932 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2933 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2934 CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2937 CHILD_REQUIRE(got_usr1 == 0);
2939 /* Allow the pending SIGUSR1 in now. */
2940 CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2941 /* Wait to receive a SIGUSR1. */
2943 err = sem_wait(&sigusr1_sem);
2944 CHILD_REQUIRE(err == 0 || errno == EINTR);
2945 } while (err != 0 && errno == EINTR);
2947 CHILD_REQUIRE(got_usr1 == 1);
2951 /* The first wait() should report the stop from SIGSTOP. */
2952 wpid = waitpid(fpid, &status, 0);
2953 ATF_REQUIRE(wpid == fpid);
2954 ATF_REQUIRE(WIFSTOPPED(status));
2955 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2956 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2957 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2959 /* Continue the child replacing SIGSTOP with SIGUSR1. */
2960 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2962 /* The next wait() should report the SIGUSR1 was received. */
2963 wpid = waitpid(fpid, &status, 0);
2964 ATF_REQUIRE(wpid == fpid);
2965 ATF_REQUIRE(WIFSTOPPED(status));
2966 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2967 ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2968 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2970 /* Continue the child, ignoring the SIGUSR1. */
2971 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2973 /* The last wait() should report normal exit with code 1. */
2974 wpid = waitpid(fpid, &status, 0);
2975 ATF_REQUIRE(wpid == fpid);
2976 ATF_REQUIRE(WIFEXITED(status));
2977 ATF_REQUIRE(WEXITSTATUS(status) == 1);
2979 wpid = wait(&status);
2980 ATF_REQUIRE(wpid == -1);
2981 ATF_REQUIRE(errno == ECHILD);
2985 * Verify that if ptrace stops due to a signal but continues with
2986 * a different signal that the new signal is routed to a thread
2987 * that can accept it, and that the thread is awakened by the signal
2988 * in a timely manner.
2990 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2991 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2997 pthread_barrier_t barrier;
2999 ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
3000 ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
3001 ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3003 ATF_REQUIRE((fpid = fork()) != -1);
3005 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
3007 /* The other thread should receive the first SIGUSR1. */
3008 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
3009 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
3010 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
3014 /* Wait until other thread has received its SIGUSR1. */
3015 pthread_barrier_wait(&barrier);
3018 * Swap ignore duties; the next SIGUSR1 should go to this
3021 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
3023 /* Sync up threads after swapping signal masks. */
3024 pthread_barrier_wait(&barrier);
3027 * Sync up with test code; we're ready for the next SIGUSR1
3032 /* Wait for this thread to receive a SIGUSR1. */
3034 err = sem_wait(&sigusr1_sem);
3035 CHILD_REQUIRE(err == 0 || errno == EINTR);
3036 } while (err != 0 && errno == EINTR);
3038 /* Free the other thread from the barrier. */
3039 pthread_barrier_wait(&barrier);
3041 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3046 /* The first wait() should report the stop from SIGSTOP. */
3047 wpid = waitpid(fpid, &status, 0);
3048 ATF_REQUIRE(wpid == fpid);
3049 ATF_REQUIRE(WIFSTOPPED(status));
3050 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3052 /* Continue the child ignoring the SIGSTOP. */
3053 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3056 * Send a signal without ptrace that either thread will accept (USR2,
3059 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3061 /* The second wait() should report a SIGUSR2 was received. */
3062 wpid = waitpid(fpid, &status, 0);
3063 ATF_REQUIRE(wpid == fpid);
3064 ATF_REQUIRE(WIFSTOPPED(status));
3065 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3067 /* Continue the child, changing the signal to USR1. */
3068 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3070 /* The next wait() should report the stop from SIGSTOP. */
3071 wpid = waitpid(fpid, &status, 0);
3072 ATF_REQUIRE(wpid == fpid);
3073 ATF_REQUIRE(WIFSTOPPED(status));
3074 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3076 /* Continue the child ignoring the SIGSTOP. */
3077 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3079 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3081 /* The next wait() should report a SIGUSR2 was received. */
3082 wpid = waitpid(fpid, &status, 0);
3083 ATF_REQUIRE(wpid == fpid);
3084 ATF_REQUIRE(WIFSTOPPED(status));
3085 ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3087 /* Continue the child, changing the signal to USR1. */
3088 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3090 /* The last wait() should report normal exit with code 1. */
3091 wpid = waitpid(fpid, &status, 0);
3092 ATF_REQUIRE(wpid == fpid);
3093 ATF_REQUIRE(WIFEXITED(status));
3094 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3096 wpid = wait(&status);
3097 ATF_REQUIRE(wpid == -1);
3098 ATF_REQUIRE(errno == ECHILD);
3102 raise_sigstop_thread(void *arg __unused)
3110 sleep_thread(void *arg __unused)
3118 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3127 * Become the reaper for this process tree. We need to be able to check
3128 * that both child and grandchild have died.
3130 ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3133 ATF_REQUIRE(fpid >= 0);
3136 CHILD_REQUIRE(fpid >= 0);
3140 /* Pin to CPU 0 to serialize thread execution. */
3142 CPU_SET(0, &setmask);
3143 CHILD_REQUIRE(cpuset(&setid) == 0);
3144 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3145 CPU_WHICH_CPUSET, setid,
3146 sizeof(setmask), &setmask) == 0);
3148 if (sigstop_from_main_thread) {
3150 * We expect the SIGKILL sent when our parent
3151 * dies to be delivered to the new thread.
3152 * Raise the SIGSTOP in this thread so the
3155 CHILD_REQUIRE(pthread_create(&t, NULL,
3156 sleep_thread, NULL) == 0);
3160 * We expect the SIGKILL to be delivered to
3161 * this thread. After creating the new thread,
3162 * just get off the CPU so the other thread can
3163 * raise the SIGSTOP.
3165 CHILD_REQUIRE(pthread_create(&t, NULL,
3166 raise_sigstop_thread, NULL) == 0);
3172 /* First stop is trace_me() immediately after fork. */
3173 wpid = waitpid(fpid, &status, 0);
3174 CHILD_REQUIRE(wpid == fpid);
3175 CHILD_REQUIRE(WIFSTOPPED(status));
3176 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3178 CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3180 /* Second stop is from the raise(SIGSTOP). */
3181 wpid = waitpid(fpid, &status, 0);
3182 CHILD_REQUIRE(wpid == fpid);
3183 CHILD_REQUIRE(WIFSTOPPED(status));
3184 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3187 * Terminate tracing process without detaching. Our child
3194 * We should get a normal exit from our immediate child and a SIGKILL
3195 * exit from our grandchild. The latter case is the interesting one.
3196 * Our grandchild should not have stopped due to the SIGSTOP that was
3197 * left dangling when its parent died.
3199 for (i = 0; i < 2; ++i) {
3200 wpid = wait(&status);
3202 ATF_REQUIRE(WIFEXITED(status) &&
3203 WEXITSTATUS(status) == 0);
3205 ATF_REQUIRE(WIFSIGNALED(status) &&
3206 WTERMSIG(status) == SIGKILL);
3211 * These two tests ensure that if the tracing process exits without detaching
3212 * just after the child received a SIGSTOP, the child is cleanly killed and
3213 * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3214 * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3215 * different threads, the SIGKILL must win. There are two variants of this
3216 * test, designed to catch the case where the SIGKILL is delivered to the
3217 * younger thread (the first test) and the case where the SIGKILL is delivered
3218 * to the older thread (the second test). This behavior has changed in the
3219 * past, so make no assumption.
3221 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3222 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3225 atf_tc_set_md_var(tc, "require.user", "root");
3227 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3230 terminate_with_pending_sigstop(true);
3233 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3234 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3237 atf_tc_set_md_var(tc, "require.user", "root");
3239 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3242 terminate_with_pending_sigstop(false);
3246 * Verify that after ptrace() discards a SIGKILL signal, the event mask
3249 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3250 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3252 struct ptrace_lwpinfo pl;
3254 int status, event_mask, new_event_mask;
3256 ATF_REQUIRE((fpid = fork()) != -1);
3263 /* The first wait() should report the stop from trace_me(). */
3264 wpid = waitpid(fpid, &status, 0);
3265 ATF_REQUIRE(wpid == fpid);
3266 ATF_REQUIRE(WIFSTOPPED(status));
3267 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3269 /* Set several unobtrusive event bits. */
3270 event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3271 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3272 sizeof(event_mask)) == 0);
3274 /* Send a SIGKILL without using ptrace. */
3275 ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3277 /* Continue the child ignoring the SIGSTOP. */
3278 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3280 /* The next stop should be due to the SIGKILL. */
3281 wpid = waitpid(fpid, &status, 0);
3282 ATF_REQUIRE(wpid == fpid);
3283 ATF_REQUIRE(WIFSTOPPED(status));
3284 ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3286 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3287 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3288 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3290 /* Continue the child ignoring the SIGKILL. */
3291 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3293 /* The next wait() should report the stop from SIGSTOP. */
3294 wpid = waitpid(fpid, &status, 0);
3295 ATF_REQUIRE(wpid == fpid);
3296 ATF_REQUIRE(WIFSTOPPED(status));
3297 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3299 /* Check the current event mask. It should not have changed. */
3301 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3302 sizeof(new_event_mask)) == 0);
3303 ATF_REQUIRE(event_mask == new_event_mask);
3305 /* Continue the child to let it exit. */
3306 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3308 /* The last event should be for the child process's exit. */
3309 wpid = waitpid(fpid, &status, 0);
3310 ATF_REQUIRE(WIFEXITED(status));
3311 ATF_REQUIRE(WEXITSTATUS(status) == 0);
3313 wpid = wait(&status);
3314 ATF_REQUIRE(wpid == -1);
3315 ATF_REQUIRE(errno == ECHILD);
3319 flock_thread(void *arg)
3324 (void)flock(fd, LOCK_EX);
3325 (void)flock(fd, LOCK_UN);
3330 * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3331 * We rely on the fact that the lockf implementation sets SBDRY before blocking
3332 * on a lock. This is a regression test for r318191.
3334 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3335 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3337 pthread_barrier_t barrier;
3338 pthread_barrierattr_t battr;
3341 int error, fd, i, status;
3343 ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3344 ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3345 PTHREAD_PROCESS_SHARED) == 0);
3346 ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3348 (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3349 fd = mkstemp(tmpfile);
3350 ATF_REQUIRE(fd >= 0);
3352 ATF_REQUIRE((child = fork()) != -1);
3357 error = pthread_barrier_wait(&barrier);
3358 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3361 cfd = open(tmpfile, O_RDONLY);
3366 * We want at least two threads blocked on the file lock since
3367 * the SIGSTOP from PT_ATTACH may kick one of them out of
3370 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3372 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3374 if (pthread_join(t[0], NULL) != 0)
3376 if (pthread_join(t[1], NULL) != 0)
3381 ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3383 error = pthread_barrier_wait(&barrier);
3384 ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3387 * Give the child some time to block. Is there a better way to do this?
3392 * Attach and give the child 3 seconds to stop.
3394 ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3395 for (i = 0; i < 3; i++) {
3396 wpid = waitpid(child, &status, WNOHANG);
3397 if (wpid == child && WIFSTOPPED(status) &&
3398 WSTOPSIG(status) == SIGSTOP)
3402 ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3404 ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3406 ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3407 ATF_REQUIRE(unlink(tmpfile) == 0);
3408 ATF_REQUIRE(close(fd) == 0);
3412 sigusr1_step_handler(int sig)
3415 CHILD_REQUIRE(sig == SIGUSR1);
3420 * Verify that PT_STEP with a signal invokes the signal before
3421 * stepping the next instruction (and that the next instruction is
3422 * stepped correctly).
3424 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3425 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3427 struct ptrace_lwpinfo pl;
3431 ATF_REQUIRE((fpid = fork()) != -1);
3434 signal(SIGUSR1, sigusr1_step_handler);
3439 /* The first wait() should report the stop from SIGSTOP. */
3440 wpid = waitpid(fpid, &status, 0);
3441 ATF_REQUIRE(wpid == fpid);
3442 ATF_REQUIRE(WIFSTOPPED(status));
3443 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3445 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3447 /* The next stop should report the SIGABRT in the child body. */
3448 wpid = waitpid(fpid, &status, 0);
3449 ATF_REQUIRE(wpid == fpid);
3450 ATF_REQUIRE(WIFSTOPPED(status));
3451 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3453 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3454 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3455 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3457 /* Step the child process inserting SIGUSR1. */
3458 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3460 /* The next stop should report the SIGABRT in the signal handler. */
3461 wpid = waitpid(fpid, &status, 0);
3462 ATF_REQUIRE(wpid == fpid);
3463 ATF_REQUIRE(WIFSTOPPED(status));
3464 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3466 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3467 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3468 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3470 /* Continue the child process discarding the signal. */
3471 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3473 /* The next stop should report a trace trap from PT_STEP. */
3474 wpid = waitpid(fpid, &status, 0);
3475 ATF_REQUIRE(wpid == fpid);
3476 ATF_REQUIRE(WIFSTOPPED(status));
3477 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3479 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3480 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3481 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3482 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3484 /* Continue the child to let it exit. */
3485 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3487 /* The last event should be for the child process's exit. */
3488 wpid = waitpid(fpid, &status, 0);
3489 ATF_REQUIRE(WIFEXITED(status));
3490 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3492 wpid = wait(&status);
3493 ATF_REQUIRE(wpid == -1);
3494 ATF_REQUIRE(errno == ECHILD);
3497 #ifdef HAVE_BREAKPOINT
3499 * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3500 * for a breakpoint trap.
3502 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3503 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3505 struct ptrace_lwpinfo pl;
3509 ATF_REQUIRE((fpid = fork()) != -1);
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 /* Continue the child ignoring the SIGSTOP. */
3523 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3525 /* The second wait() should report hitting the breakpoint. */
3526 wpid = waitpid(fpid, &status, 0);
3527 ATF_REQUIRE(wpid == fpid);
3528 ATF_REQUIRE(WIFSTOPPED(status));
3529 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3531 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3532 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3533 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3534 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_BRKPT);
3536 /* Kill the child process. */
3537 ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
3539 /* The last wait() should report the SIGKILL. */
3540 wpid = waitpid(fpid, &status, 0);
3541 ATF_REQUIRE(wpid == fpid);
3542 ATF_REQUIRE(WIFSIGNALED(status));
3543 ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
3545 wpid = wait(&status);
3546 ATF_REQUIRE(wpid == -1);
3547 ATF_REQUIRE(errno == ECHILD);
3549 #endif /* HAVE_BREAKPOINT */
3552 * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3553 * for a single-step trap from PT_STEP.
3555 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3556 ATF_TC_BODY(ptrace__step_siginfo, tc)
3558 struct ptrace_lwpinfo pl;
3562 ATF_REQUIRE((fpid = fork()) != -1);
3568 /* The first wait() should report the stop from SIGSTOP. */
3569 wpid = waitpid(fpid, &status, 0);
3570 ATF_REQUIRE(wpid == fpid);
3571 ATF_REQUIRE(WIFSTOPPED(status));
3572 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3574 /* Step the child ignoring the SIGSTOP. */
3575 ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, 0) == 0);
3577 /* The second wait() should report a single-step trap. */
3578 wpid = waitpid(fpid, &status, 0);
3579 ATF_REQUIRE(wpid == fpid);
3580 ATF_REQUIRE(WIFSTOPPED(status));
3581 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3583 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3584 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3585 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP);
3586 ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3588 /* Continue the child process. */
3589 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3591 /* The last event should be for the child process's exit. */
3592 wpid = waitpid(fpid, &status, 0);
3593 ATF_REQUIRE(WIFEXITED(status));
3594 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3596 wpid = wait(&status);
3597 ATF_REQUIRE(wpid == -1);
3598 ATF_REQUIRE(errno == ECHILD);
3601 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3603 continue_thread(void *arg __unused)
3610 continue_thread_main(void)
3612 pthread_t threads[2];
3614 CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3616 CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3618 CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3619 CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3624 * Ensure that PT_CONTINUE clears the status of the thread that
3625 * triggered the stop even if a different thread's LWP was passed to
3628 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3629 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3631 struct ptrace_lwpinfo pl;
3638 ATF_REQUIRE((fpid = fork()) != -1);
3641 continue_thread_main();
3644 /* The first wait() should report the stop from SIGSTOP. */
3645 wpid = waitpid(fpid, &status, 0);
3646 ATF_REQUIRE(wpid == fpid);
3647 ATF_REQUIRE(WIFSTOPPED(status));
3648 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3650 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3653 ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3655 /* Continue the child ignoring the SIGSTOP. */
3656 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3658 /* One of the new threads should report it's birth. */
3659 wpid = waitpid(fpid, &status, 0);
3660 ATF_REQUIRE(wpid == fpid);
3661 ATF_REQUIRE(WIFSTOPPED(status));
3662 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3664 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3665 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3666 (PL_FLAG_BORN | PL_FLAG_SCX));
3667 lwps[0] = pl.pl_lwpid;
3670 * Suspend this thread to ensure both threads are alive before
3671 * hitting the breakpoint.
3673 ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3675 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3677 /* Second thread should report it's birth. */
3678 wpid = waitpid(fpid, &status, 0);
3679 ATF_REQUIRE(wpid == fpid);
3680 ATF_REQUIRE(WIFSTOPPED(status));
3681 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3683 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3684 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3685 (PL_FLAG_BORN | PL_FLAG_SCX));
3686 ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3687 lwps[1] = pl.pl_lwpid;
3689 /* Resume both threads waiting for breakpoint events. */
3690 hit_break[0] = hit_break[1] = false;
3691 ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3692 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3694 /* One thread should report a breakpoint. */
3695 wpid = waitpid(fpid, &status, 0);
3696 ATF_REQUIRE(wpid == fpid);
3697 ATF_REQUIRE(WIFSTOPPED(status));
3698 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3700 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3701 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3702 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3703 pl.pl_siginfo.si_code == TRAP_BRKPT);
3704 if (pl.pl_lwpid == lwps[0])
3708 hit_break[i] = true;
3709 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3711 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®, 0) != -1);
3714 * Resume both threads but pass the other thread's LWPID to
3717 ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3720 * Will now get two thread exit events and one more breakpoint
3723 for (j = 0; j < 3; j++) {
3724 wpid = waitpid(fpid, &status, 0);
3725 ATF_REQUIRE(wpid == fpid);
3726 ATF_REQUIRE(WIFSTOPPED(status));
3727 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3729 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3732 if (pl.pl_lwpid == lwps[0])
3737 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3738 if (pl.pl_flags & PL_FLAG_EXITED) {
3739 ATF_REQUIRE_MSG(hit_break[i],
3740 "exited thread did not report breakpoint");
3743 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3744 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3745 pl.pl_siginfo.si_code == TRAP_BRKPT);
3746 ATF_REQUIRE_MSG(!hit_break[i],
3747 "double breakpoint event");
3748 hit_break[i] = true;
3749 ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)®,
3752 ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)®,
3756 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3759 /* Both threads should have exited. */
3760 ATF_REQUIRE(lwps[0] == 0);
3761 ATF_REQUIRE(lwps[1] == 0);
3763 /* The last event should be for the child process's exit. */
3764 wpid = waitpid(fpid, &status, 0);
3765 ATF_REQUIRE(WIFEXITED(status));
3766 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3768 wpid = wait(&status);
3769 ATF_REQUIRE(wpid == -1);
3770 ATF_REQUIRE(errno == ECHILD);
3775 * Verify that PT_LWPINFO doesn't return stale siginfo.
3777 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3778 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3780 struct ptrace_lwpinfo pl;
3784 ATF_REQUIRE((fpid = fork()) != -1);
3791 /* The first wait() should report the stop from SIGSTOP. */
3792 wpid = waitpid(fpid, &status, 0);
3793 ATF_REQUIRE(wpid == fpid);
3794 ATF_REQUIRE(WIFSTOPPED(status));
3795 ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3797 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3799 /* The next stop should report the SIGABRT in the child body. */
3800 wpid = waitpid(fpid, &status, 0);
3801 ATF_REQUIRE(wpid == fpid);
3802 ATF_REQUIRE(WIFSTOPPED(status));
3803 ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3805 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3806 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3807 ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3810 * Continue the process ignoring the signal, but enabling
3813 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
3816 * The next stop should report a system call entry from
3817 * exit(). PL_FLAGS_SI should not be set.
3819 wpid = waitpid(fpid, &status, 0);
3820 ATF_REQUIRE(wpid == fpid);
3821 ATF_REQUIRE(WIFSTOPPED(status));
3822 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3824 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3825 ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3826 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) == 0);
3828 /* Disable syscall tracing and continue the child to let it exit. */
3829 ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3830 sizeof(events)) == 0);
3831 events &= ~PTRACE_SYSCALL;
3832 ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3833 sizeof(events)) == 0);
3834 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3836 /* The last event should be for the child process's exit. */
3837 wpid = waitpid(fpid, &status, 0);
3838 ATF_REQUIRE(WIFEXITED(status));
3839 ATF_REQUIRE(WEXITSTATUS(status) == 1);
3841 wpid = wait(&status);
3842 ATF_REQUIRE(wpid == -1);
3843 ATF_REQUIRE(errno == ECHILD);
3849 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3850 ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3851 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3852 ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3853 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3854 ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3855 ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3856 ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3858 ptrace__follow_fork_child_detached_unrelated_debugger);
3860 ptrace__follow_fork_parent_detached_unrelated_debugger);
3861 ATF_TP_ADD_TC(tp, ptrace__getppid);
3862 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3863 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3864 ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3865 ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3866 ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3867 ATF_TP_ADD_TC(tp, ptrace__siginfo);
3868 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3869 ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3870 ATF_TP_ADD_TC(tp, ptrace__event_mask);
3871 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3872 ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3873 #ifdef HAVE_BREAKPOINT
3874 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3876 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3877 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3878 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3879 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3880 ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3881 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3883 ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3884 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3885 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3886 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3887 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3888 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3889 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3890 ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3891 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3892 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3893 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3894 ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3895 ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3896 ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3897 ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3898 #ifdef HAVE_BREAKPOINT
3899 ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
3901 ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
3902 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3903 ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3905 ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
3907 return (atf_no_error());