xref: /freebsd/tests/sys/kern/ptrace_test.c (revision d8a0fe102c0cfdfcd5b818f850eff09d8536c9bc)
1 /*-
2  * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/types.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
33 #include <sys/file.h>
34 #include <sys/time.h>
35 #include <sys/procctl.h>
36 #include <sys/ptrace.h>
37 #include <sys/queue.h>
38 #include <sys/runq.h>
39 #include <sys/syscall.h>
40 #include <sys/sysctl.h>
41 #include <sys/user.h>
42 #include <sys/wait.h>
43 #include <errno.h>
44 #include <machine/cpufunc.h>
45 #include <pthread.h>
46 #include <sched.h>
47 #include <semaphore.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52 #include <atf-c.h>
53 
54 /*
55  * A variant of ATF_REQUIRE that is suitable for use in child
56  * processes.  This only works if the parent process is tripped up by
57  * the early exit and fails some requirement itself.
58  */
59 #define	CHILD_REQUIRE(exp) do {						\
60 		if (!(exp))						\
61 			child_fail_require(__FILE__, __LINE__,		\
62 			    #exp " not met");				\
63 	} while (0)
64 
65 static __dead2 void
66 child_fail_require(const char *file, int line, const char *str)
67 {
68 	char buf[128];
69 
70 	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
71 	write(2, buf, strlen(buf));
72 	_exit(32);
73 }
74 
75 static void
76 trace_me(void)
77 {
78 
79 	/* Attach the parent process as a tracer of this process. */
80 	CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
81 
82 	/* Trigger a stop. */
83 	raise(SIGSTOP);
84 }
85 
86 static void
87 attach_child(pid_t pid)
88 {
89 	pid_t wpid;
90 	int status;
91 
92 	ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
93 
94 	wpid = waitpid(pid, &status, 0);
95 	ATF_REQUIRE(wpid == pid);
96 	ATF_REQUIRE(WIFSTOPPED(status));
97 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
98 }
99 
100 static void
101 wait_for_zombie(pid_t pid)
102 {
103 
104 	/*
105 	 * Wait for a process to exit.  This is kind of gross, but
106 	 * there is not a better way.
107 	 *
108 	 * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
109 	 * with ESRCH.  After that change, a valid struct kinfo_proc
110 	 * is returned for zombies with ki_stat set to SZOMB.
111 	 */
112 	for (;;) {
113 		struct kinfo_proc kp;
114 		size_t len;
115 		int mib[4];
116 
117 		mib[0] = CTL_KERN;
118 		mib[1] = KERN_PROC;
119 		mib[2] = KERN_PROC_PID;
120 		mib[3] = pid;
121 		len = sizeof(kp);
122 		if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
123 			ATF_REQUIRE(errno == ESRCH);
124 			break;
125 		}
126 		if (kp.ki_stat == SZOMB)
127 			break;
128 		usleep(5000);
129 	}
130 }
131 
132 /*
133  * Verify that a parent debugger process "sees" the exit of a debugged
134  * process exactly once when attached via PT_TRACE_ME.
135  */
136 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
137 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
138 {
139 	pid_t child, wpid;
140 	int status;
141 
142 	ATF_REQUIRE((child = fork()) != -1);
143 	if (child == 0) {
144 		/* Child process. */
145 		trace_me();
146 
147 		_exit(1);
148 	}
149 
150 	/* Parent process. */
151 
152 	/* The first wait() should report the stop from SIGSTOP. */
153 	wpid = waitpid(child, &status, 0);
154 	ATF_REQUIRE(wpid == child);
155 	ATF_REQUIRE(WIFSTOPPED(status));
156 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
157 
158 	/* Continue the child ignoring the SIGSTOP. */
159 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
160 
161 	/* The second wait() should report the exit status. */
162 	wpid = waitpid(child, &status, 0);
163 	ATF_REQUIRE(wpid == child);
164 	ATF_REQUIRE(WIFEXITED(status));
165 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
166 
167 	/* The child should no longer exist. */
168 	wpid = waitpid(child, &status, 0);
169 	ATF_REQUIRE(wpid == -1);
170 	ATF_REQUIRE(errno == ECHILD);
171 }
172 
173 /*
174  * Verify that a parent debugger process "sees" the exit of a debugged
175  * process exactly once when attached via PT_ATTACH.
176  */
177 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
178 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
179 {
180 	pid_t child, wpid;
181 	int cpipe[2], status;
182 	char c;
183 
184 	ATF_REQUIRE(pipe(cpipe) == 0);
185 	ATF_REQUIRE((child = fork()) != -1);
186 	if (child == 0) {
187 		/* Child process. */
188 		close(cpipe[0]);
189 
190 		/* Wait for the parent to attach. */
191 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
192 
193 		_exit(1);
194 	}
195 	close(cpipe[1]);
196 
197 	/* Parent process. */
198 
199 	/* Attach to the child process. */
200 	attach_child(child);
201 
202 	/* Continue the child ignoring the SIGSTOP. */
203 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
204 
205 	/* Signal the child to exit. */
206 	close(cpipe[0]);
207 
208 	/* The second wait() should report the exit status. */
209 	wpid = waitpid(child, &status, 0);
210 	ATF_REQUIRE(wpid == child);
211 	ATF_REQUIRE(WIFEXITED(status));
212 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
213 
214 	/* The child should no longer exist. */
215 	wpid = waitpid(child, &status, 0);
216 	ATF_REQUIRE(wpid == -1);
217 	ATF_REQUIRE(errno == ECHILD);
218 }
219 
220 /*
221  * Verify that a parent process "sees" the exit of a debugged process only
222  * after the debugger has seen it.
223  */
224 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
225 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
226 {
227 	pid_t child, debugger, wpid;
228 	int cpipe[2], dpipe[2], status;
229 	char c;
230 
231 	ATF_REQUIRE(pipe(cpipe) == 0);
232 	ATF_REQUIRE((child = fork()) != -1);
233 
234 	if (child == 0) {
235 		/* Child process. */
236 		close(cpipe[0]);
237 
238 		/* Wait for parent to be ready. */
239 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
240 
241 		_exit(1);
242 	}
243 	close(cpipe[1]);
244 
245 	ATF_REQUIRE(pipe(dpipe) == 0);
246 	ATF_REQUIRE((debugger = fork()) != -1);
247 
248 	if (debugger == 0) {
249 		/* Debugger process. */
250 		close(dpipe[0]);
251 
252 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
253 
254 		wpid = waitpid(child, &status, 0);
255 		CHILD_REQUIRE(wpid == child);
256 		CHILD_REQUIRE(WIFSTOPPED(status));
257 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
258 
259 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
260 
261 		/* Signal parent that debugger is attached. */
262 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
263 
264 		/* Wait for parent's failed wait. */
265 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
266 
267 		wpid = waitpid(child, &status, 0);
268 		CHILD_REQUIRE(wpid == child);
269 		CHILD_REQUIRE(WIFEXITED(status));
270 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
271 
272 		_exit(0);
273 	}
274 	close(dpipe[1]);
275 
276 	/* Parent process. */
277 
278 	/* Wait for the debugger to attach to the child. */
279 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
280 
281 	/* Release the child. */
282 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
283 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
284 	close(cpipe[0]);
285 
286 	wait_for_zombie(child);
287 
288 	/*
289 	 * This wait should return a pid of 0 to indicate no status to
290 	 * report.  The parent should see the child as non-exited
291 	 * until the debugger sees the exit.
292 	 */
293 	wpid = waitpid(child, &status, WNOHANG);
294 	ATF_REQUIRE(wpid == 0);
295 
296 	/* Signal the debugger to wait for the child. */
297 	close(dpipe[0]);
298 
299 	/* Wait for the debugger. */
300 	wpid = waitpid(debugger, &status, 0);
301 	ATF_REQUIRE(wpid == debugger);
302 	ATF_REQUIRE(WIFEXITED(status));
303 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
304 
305 	/* The child process should now be ready. */
306 	wpid = waitpid(child, &status, WNOHANG);
307 	ATF_REQUIRE(wpid == child);
308 	ATF_REQUIRE(WIFEXITED(status));
309 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
310 }
311 
312 /*
313  * Verify that a parent process "sees" the exit of a debugged process
314  * only after a non-direct-child debugger has seen it.  In particular,
315  * various wait() calls in the parent must avoid failing with ESRCH by
316  * checking the parent's orphan list for the debugee.
317  */
318 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
319 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
320 {
321 	pid_t child, debugger, fpid, wpid;
322 	int cpipe[2], dpipe[2], status;
323 	char c;
324 
325 	ATF_REQUIRE(pipe(cpipe) == 0);
326 	ATF_REQUIRE((child = fork()) != -1);
327 
328 	if (child == 0) {
329 		/* Child process. */
330 		close(cpipe[0]);
331 
332 		/* Wait for parent to be ready. */
333 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
334 
335 		_exit(1);
336 	}
337 	close(cpipe[1]);
338 
339 	ATF_REQUIRE(pipe(dpipe) == 0);
340 	ATF_REQUIRE((debugger = fork()) != -1);
341 
342 	if (debugger == 0) {
343 		/* Debugger parent. */
344 
345 		/*
346 		 * Fork again and drop the debugger parent so that the
347 		 * debugger is not a child of the main parent.
348 		 */
349 		CHILD_REQUIRE((fpid = fork()) != -1);
350 		if (fpid != 0)
351 			_exit(2);
352 
353 		/* Debugger process. */
354 		close(dpipe[0]);
355 
356 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
357 
358 		wpid = waitpid(child, &status, 0);
359 		CHILD_REQUIRE(wpid == child);
360 		CHILD_REQUIRE(WIFSTOPPED(status));
361 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
362 
363 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
364 
365 		/* Signal parent that debugger is attached. */
366 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
367 
368 		/* Wait for parent's failed wait. */
369 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
370 
371 		wpid = waitpid(child, &status, 0);
372 		CHILD_REQUIRE(wpid == child);
373 		CHILD_REQUIRE(WIFEXITED(status));
374 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
375 
376 		_exit(0);
377 	}
378 	close(dpipe[1]);
379 
380 	/* Parent process. */
381 
382 	/* Wait for the debugger parent process to exit. */
383 	wpid = waitpid(debugger, &status, 0);
384 	ATF_REQUIRE(wpid == debugger);
385 	ATF_REQUIRE(WIFEXITED(status));
386 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
387 
388 	/* A WNOHANG wait here should see the non-exited child. */
389 	wpid = waitpid(child, &status, WNOHANG);
390 	ATF_REQUIRE(wpid == 0);
391 
392 	/* Wait for the debugger to attach to the child. */
393 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
394 
395 	/* Release the child. */
396 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
397 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
398 	close(cpipe[0]);
399 
400 	wait_for_zombie(child);
401 
402 	/*
403 	 * This wait should return a pid of 0 to indicate no status to
404 	 * report.  The parent should see the child as non-exited
405 	 * until the debugger sees the exit.
406 	 */
407 	wpid = waitpid(child, &status, WNOHANG);
408 	ATF_REQUIRE(wpid == 0);
409 
410 	/* Signal the debugger to wait for the child. */
411 	ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
412 
413 	/* Wait for the debugger. */
414 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
415 	close(dpipe[0]);
416 
417 	/* The child process should now be ready. */
418 	wpid = waitpid(child, &status, WNOHANG);
419 	ATF_REQUIRE(wpid == child);
420 	ATF_REQUIRE(WIFEXITED(status));
421 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
422 }
423 
424 /*
425  * The parent process should always act the same regardless of how the
426  * debugger is attached to it.
427  */
428 static __dead2 void
429 follow_fork_parent(bool use_vfork)
430 {
431 	pid_t fpid, wpid;
432 	int status;
433 
434 	if (use_vfork)
435 		CHILD_REQUIRE((fpid = vfork()) != -1);
436 	else
437 		CHILD_REQUIRE((fpid = fork()) != -1);
438 
439 	if (fpid == 0)
440 		/* Child */
441 		_exit(2);
442 
443 	wpid = waitpid(fpid, &status, 0);
444 	CHILD_REQUIRE(wpid == fpid);
445 	CHILD_REQUIRE(WIFEXITED(status));
446 	CHILD_REQUIRE(WEXITSTATUS(status) == 2);
447 
448 	_exit(1);
449 }
450 
451 /*
452  * Helper routine for follow fork tests.  This waits for two stops
453  * that report both "sides" of a fork.  It returns the pid of the new
454  * child process.
455  */
456 static pid_t
457 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
458 {
459 	struct ptrace_lwpinfo pl;
460 	bool fork_reported[2];
461 	pid_t child, wpid;
462 	int i, status;
463 
464 	fork_reported[0] = false;
465 	fork_reported[1] = false;
466 	child = -1;
467 
468 	/*
469 	 * Each process should report a fork event.  The parent should
470 	 * report a PL_FLAG_FORKED event, and the child should report
471 	 * a PL_FLAG_CHILD event.
472 	 */
473 	for (i = 0; i < 2; i++) {
474 		wpid = wait(&status);
475 		ATF_REQUIRE(wpid > 0);
476 		ATF_REQUIRE(WIFSTOPPED(status));
477 
478 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
479 		    sizeof(pl)) != -1);
480 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
481 		    0);
482 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
483 		    (PL_FLAG_FORKED | PL_FLAG_CHILD));
484 		if (pl.pl_flags & PL_FLAG_CHILD) {
485 			ATF_REQUIRE(wpid != parent);
486 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
487 			ATF_REQUIRE(!fork_reported[1]);
488 			if (child == -1)
489 				child = wpid;
490 			else
491 				ATF_REQUIRE(child == wpid);
492 			if (ppl != NULL)
493 				ppl[1] = pl;
494 			fork_reported[1] = true;
495 		} else {
496 			ATF_REQUIRE(wpid == parent);
497 			ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
498 			ATF_REQUIRE(!fork_reported[0]);
499 			if (child == -1)
500 				child = pl.pl_child_pid;
501 			else
502 				ATF_REQUIRE(child == pl.pl_child_pid);
503 			if (ppl != NULL)
504 				ppl[0] = pl;
505 			fork_reported[0] = true;
506 		}
507 	}
508 
509 	return (child);
510 }
511 
512 /*
513  * Verify that a new child process is stopped after a followed fork and
514  * that the traced parent sees the exit of the child after the debugger
515  * when both processes remain attached to the debugger.
516  */
517 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
518 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
519 {
520 	pid_t children[2], fpid, wpid;
521 	int status;
522 
523 	ATF_REQUIRE((fpid = fork()) != -1);
524 	if (fpid == 0) {
525 		trace_me();
526 		follow_fork_parent(false);
527 	}
528 
529 	/* Parent process. */
530 	children[0] = fpid;
531 
532 	/* The first wait() should report the stop from SIGSTOP. */
533 	wpid = waitpid(children[0], &status, 0);
534 	ATF_REQUIRE(wpid == children[0]);
535 	ATF_REQUIRE(WIFSTOPPED(status));
536 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
537 
538 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
539 
540 	/* Continue the child ignoring the SIGSTOP. */
541 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
542 
543 	children[1] = handle_fork_events(children[0], NULL);
544 	ATF_REQUIRE(children[1] > 0);
545 
546 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
547 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
548 
549 	/*
550 	 * The child can't exit until the grandchild reports status, so the
551 	 * grandchild should report its exit first to the debugger.
552 	 */
553 	wpid = wait(&status);
554 	ATF_REQUIRE(wpid == children[1]);
555 	ATF_REQUIRE(WIFEXITED(status));
556 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
557 
558 	wpid = wait(&status);
559 	ATF_REQUIRE(wpid == children[0]);
560 	ATF_REQUIRE(WIFEXITED(status));
561 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
562 
563 	wpid = wait(&status);
564 	ATF_REQUIRE(wpid == -1);
565 	ATF_REQUIRE(errno == ECHILD);
566 }
567 
568 /*
569  * Verify that a new child process is stopped after a followed fork
570  * and that the traced parent sees the exit of the child when the new
571  * child process is detached after it reports its fork.
572  */
573 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
574 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
575 {
576 	pid_t children[2], fpid, wpid;
577 	int status;
578 
579 	ATF_REQUIRE((fpid = fork()) != -1);
580 	if (fpid == 0) {
581 		trace_me();
582 		follow_fork_parent(false);
583 	}
584 
585 	/* Parent process. */
586 	children[0] = fpid;
587 
588 	/* The first wait() should report the stop from SIGSTOP. */
589 	wpid = waitpid(children[0], &status, 0);
590 	ATF_REQUIRE(wpid == children[0]);
591 	ATF_REQUIRE(WIFSTOPPED(status));
592 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
593 
594 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
595 
596 	/* Continue the child ignoring the SIGSTOP. */
597 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
598 
599 	children[1] = handle_fork_events(children[0], NULL);
600 	ATF_REQUIRE(children[1] > 0);
601 
602 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
603 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
604 
605 	/*
606 	 * Should not see any status from the grandchild now, only the
607 	 * child.
608 	 */
609 	wpid = wait(&status);
610 	ATF_REQUIRE(wpid == children[0]);
611 	ATF_REQUIRE(WIFEXITED(status));
612 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
613 
614 	wpid = wait(&status);
615 	ATF_REQUIRE(wpid == -1);
616 	ATF_REQUIRE(errno == ECHILD);
617 }
618 
619 /*
620  * Verify that a new child process is stopped after a followed fork
621  * and that the traced parent sees the exit of the child when the
622  * traced parent is detached after the fork.
623  */
624 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
625 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
626 {
627 	pid_t children[2], fpid, wpid;
628 	int status;
629 
630 	ATF_REQUIRE((fpid = fork()) != -1);
631 	if (fpid == 0) {
632 		trace_me();
633 		follow_fork_parent(false);
634 	}
635 
636 	/* Parent process. */
637 	children[0] = fpid;
638 
639 	/* The first wait() should report the stop from SIGSTOP. */
640 	wpid = waitpid(children[0], &status, 0);
641 	ATF_REQUIRE(wpid == children[0]);
642 	ATF_REQUIRE(WIFSTOPPED(status));
643 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
644 
645 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
646 
647 	/* Continue the child ignoring the SIGSTOP. */
648 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
649 
650 	children[1] = handle_fork_events(children[0], NULL);
651 	ATF_REQUIRE(children[1] > 0);
652 
653 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
654 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
655 
656 	/*
657 	 * The child can't exit until the grandchild reports status, so the
658 	 * grandchild should report its exit first to the debugger.
659 	 *
660 	 * Even though the child process is detached, it is still a
661 	 * child of the debugger, so it will still report it's exit
662 	 * after the grandchild.
663 	 */
664 	wpid = wait(&status);
665 	ATF_REQUIRE(wpid == children[1]);
666 	ATF_REQUIRE(WIFEXITED(status));
667 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
668 
669 	wpid = wait(&status);
670 	ATF_REQUIRE(wpid == children[0]);
671 	ATF_REQUIRE(WIFEXITED(status));
672 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
673 
674 	wpid = wait(&status);
675 	ATF_REQUIRE(wpid == -1);
676 	ATF_REQUIRE(errno == ECHILD);
677 }
678 
679 static void
680 attach_fork_parent(int cpipe[2])
681 {
682 	pid_t fpid;
683 
684 	close(cpipe[0]);
685 
686 	/* Double-fork to disassociate from the debugger. */
687 	CHILD_REQUIRE((fpid = fork()) != -1);
688 	if (fpid != 0)
689 		_exit(3);
690 
691 	/* Send the pid of the disassociated child to the debugger. */
692 	fpid = getpid();
693 	CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
694 
695 	/* Wait for the debugger to attach. */
696 	CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
697 }
698 
699 /*
700  * Verify that a new child process is stopped after a followed fork and
701  * that the traced parent sees the exit of the child after the debugger
702  * when both processes remain attached to the debugger.  In this test
703  * the parent that forks is not a direct child of the debugger.
704  */
705 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
706 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
707 {
708 	pid_t children[2], fpid, wpid;
709 	int cpipe[2], status;
710 
711 	ATF_REQUIRE(pipe(cpipe) == 0);
712 	ATF_REQUIRE((fpid = fork()) != -1);
713 	if (fpid == 0) {
714 		attach_fork_parent(cpipe);
715 		follow_fork_parent(false);
716 	}
717 
718 	/* Parent process. */
719 	close(cpipe[1]);
720 
721 	/* Wait for the direct child to exit. */
722 	wpid = waitpid(fpid, &status, 0);
723 	ATF_REQUIRE(wpid == fpid);
724 	ATF_REQUIRE(WIFEXITED(status));
725 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
726 
727 	/* Read the pid of the fork parent. */
728 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
729 	    sizeof(children[0]));
730 
731 	/* Attach to the fork parent. */
732 	attach_child(children[0]);
733 
734 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
735 
736 	/* Continue the fork parent ignoring the SIGSTOP. */
737 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
738 
739 	/* Signal the fork parent to continue. */
740 	close(cpipe[0]);
741 
742 	children[1] = handle_fork_events(children[0], NULL);
743 	ATF_REQUIRE(children[1] > 0);
744 
745 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
746 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
747 
748 	/*
749 	 * The fork parent can't exit until the child reports status,
750 	 * so the child should report its exit first to the debugger.
751 	 */
752 	wpid = wait(&status);
753 	ATF_REQUIRE(wpid == children[1]);
754 	ATF_REQUIRE(WIFEXITED(status));
755 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
756 
757 	wpid = wait(&status);
758 	ATF_REQUIRE(wpid == children[0]);
759 	ATF_REQUIRE(WIFEXITED(status));
760 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
761 
762 	wpid = wait(&status);
763 	ATF_REQUIRE(wpid == -1);
764 	ATF_REQUIRE(errno == ECHILD);
765 }
766 
767 /*
768  * Verify that a new child process is stopped after a followed fork
769  * and that the traced parent sees the exit of the child when the new
770  * child process is detached after it reports its fork.  In this test
771  * the parent that forks is not a direct child of the debugger.
772  */
773 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
774 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
775 {
776 	pid_t children[2], fpid, wpid;
777 	int cpipe[2], status;
778 
779 	ATF_REQUIRE(pipe(cpipe) == 0);
780 	ATF_REQUIRE((fpid = fork()) != -1);
781 	if (fpid == 0) {
782 		attach_fork_parent(cpipe);
783 		follow_fork_parent(false);
784 	}
785 
786 	/* Parent process. */
787 	close(cpipe[1]);
788 
789 	/* Wait for the direct child to exit. */
790 	wpid = waitpid(fpid, &status, 0);
791 	ATF_REQUIRE(wpid == fpid);
792 	ATF_REQUIRE(WIFEXITED(status));
793 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
794 
795 	/* Read the pid of the fork parent. */
796 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
797 	    sizeof(children[0]));
798 
799 	/* Attach to the fork parent. */
800 	attach_child(children[0]);
801 
802 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
803 
804 	/* Continue the fork parent ignoring the SIGSTOP. */
805 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
806 
807 	/* Signal the fork parent to continue. */
808 	close(cpipe[0]);
809 
810 	children[1] = handle_fork_events(children[0], NULL);
811 	ATF_REQUIRE(children[1] > 0);
812 
813 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
814 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
815 
816 	/*
817 	 * Should not see any status from the child now, only the fork
818 	 * parent.
819 	 */
820 	wpid = wait(&status);
821 	ATF_REQUIRE(wpid == children[0]);
822 	ATF_REQUIRE(WIFEXITED(status));
823 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
824 
825 	wpid = wait(&status);
826 	ATF_REQUIRE(wpid == -1);
827 	ATF_REQUIRE(errno == ECHILD);
828 }
829 
830 /*
831  * Verify that a new child process is stopped after a followed fork
832  * and that the traced parent sees the exit of the child when the
833  * traced parent is detached after the fork.  In this test the parent
834  * that forks is not a direct child of the debugger.
835  */
836 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
837 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
838 {
839 	pid_t children[2], fpid, wpid;
840 	int cpipe[2], status;
841 
842 	ATF_REQUIRE(pipe(cpipe) == 0);
843 	ATF_REQUIRE((fpid = fork()) != -1);
844 	if (fpid == 0) {
845 		attach_fork_parent(cpipe);
846 		follow_fork_parent(false);
847 	}
848 
849 	/* Parent process. */
850 	close(cpipe[1]);
851 
852 	/* Wait for the direct child to exit. */
853 	wpid = waitpid(fpid, &status, 0);
854 	ATF_REQUIRE(wpid == fpid);
855 	ATF_REQUIRE(WIFEXITED(status));
856 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
857 
858 	/* Read the pid of the fork parent. */
859 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
860 	    sizeof(children[0]));
861 
862 	/* Attach to the fork parent. */
863 	attach_child(children[0]);
864 
865 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
866 
867 	/* Continue the fork parent ignoring the SIGSTOP. */
868 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
869 
870 	/* Signal the fork parent to continue. */
871 	close(cpipe[0]);
872 
873 	children[1] = handle_fork_events(children[0], NULL);
874 	ATF_REQUIRE(children[1] > 0);
875 
876 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
877 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
878 
879 	/*
880 	 * Should not see any status from the fork parent now, only
881 	 * the child.
882 	 */
883 	wpid = wait(&status);
884 	ATF_REQUIRE(wpid == children[1]);
885 	ATF_REQUIRE(WIFEXITED(status));
886 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
887 
888 	wpid = wait(&status);
889 	ATF_REQUIRE(wpid == -1);
890 	ATF_REQUIRE(errno == ECHILD);
891 }
892 
893 /*
894  * Verify that a child process does not see an unrelated debugger as its
895  * parent but sees its original parent process.
896  */
897 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
898 ATF_TC_BODY(ptrace__getppid, tc)
899 {
900 	pid_t child, debugger, ppid, wpid;
901 	int cpipe[2], dpipe[2], status;
902 	char c;
903 
904 	ATF_REQUIRE(pipe(cpipe) == 0);
905 	ATF_REQUIRE((child = fork()) != -1);
906 
907 	if (child == 0) {
908 		/* Child process. */
909 		close(cpipe[0]);
910 
911 		/* Wait for parent to be ready. */
912 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
913 
914 		/* Report the parent PID to the parent. */
915 		ppid = getppid();
916 		CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
917 		    sizeof(ppid));
918 
919 		_exit(1);
920 	}
921 	close(cpipe[1]);
922 
923 	ATF_REQUIRE(pipe(dpipe) == 0);
924 	ATF_REQUIRE((debugger = fork()) != -1);
925 
926 	if (debugger == 0) {
927 		/* Debugger process. */
928 		close(dpipe[0]);
929 
930 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
931 
932 		wpid = waitpid(child, &status, 0);
933 		CHILD_REQUIRE(wpid == child);
934 		CHILD_REQUIRE(WIFSTOPPED(status));
935 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
936 
937 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
938 
939 		/* Signal parent that debugger is attached. */
940 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
941 
942 		/* Wait for traced child to exit. */
943 		wpid = waitpid(child, &status, 0);
944 		CHILD_REQUIRE(wpid == child);
945 		CHILD_REQUIRE(WIFEXITED(status));
946 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
947 
948 		_exit(0);
949 	}
950 	close(dpipe[1]);
951 
952 	/* Parent process. */
953 
954 	/* Wait for the debugger to attach to the child. */
955 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
956 
957 	/* Release the child. */
958 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
959 
960 	/* Read the parent PID from the child. */
961 	ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
962 	close(cpipe[0]);
963 
964 	ATF_REQUIRE(ppid == getpid());
965 
966 	/* Wait for the debugger. */
967 	wpid = waitpid(debugger, &status, 0);
968 	ATF_REQUIRE(wpid == debugger);
969 	ATF_REQUIRE(WIFEXITED(status));
970 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
971 
972 	/* The child process should now be ready. */
973 	wpid = waitpid(child, &status, WNOHANG);
974 	ATF_REQUIRE(wpid == child);
975 	ATF_REQUIRE(WIFEXITED(status));
976 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
977 }
978 
979 /*
980  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
981  * child process created via fork() reports the correct value.
982  */
983 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
984 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
985 {
986 	struct ptrace_lwpinfo pl[2];
987 	pid_t children[2], fpid, wpid;
988 	int status;
989 
990 	ATF_REQUIRE((fpid = fork()) != -1);
991 	if (fpid == 0) {
992 		trace_me();
993 		follow_fork_parent(false);
994 	}
995 
996 	/* Parent process. */
997 	children[0] = fpid;
998 
999 	/* The first wait() should report the stop from SIGSTOP. */
1000 	wpid = waitpid(children[0], &status, 0);
1001 	ATF_REQUIRE(wpid == children[0]);
1002 	ATF_REQUIRE(WIFSTOPPED(status));
1003 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1004 
1005 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1006 
1007 	/* Continue the child ignoring the SIGSTOP. */
1008 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1009 
1010 	/* Wait for both halves of the fork event to get reported. */
1011 	children[1] = handle_fork_events(children[0], pl);
1012 	ATF_REQUIRE(children[1] > 0);
1013 
1014 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1015 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1016 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1017 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1018 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1019 
1020 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1021 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1022 
1023 	/*
1024 	 * The child can't exit until the grandchild reports status, so the
1025 	 * grandchild should report its exit first to the debugger.
1026 	 */
1027 	wpid = wait(&status);
1028 	ATF_REQUIRE(wpid == children[1]);
1029 	ATF_REQUIRE(WIFEXITED(status));
1030 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1031 
1032 	wpid = wait(&status);
1033 	ATF_REQUIRE(wpid == children[0]);
1034 	ATF_REQUIRE(WIFEXITED(status));
1035 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1036 
1037 	wpid = wait(&status);
1038 	ATF_REQUIRE(wpid == -1);
1039 	ATF_REQUIRE(errno == ECHILD);
1040 }
1041 
1042 /*
1043  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1044  * child process created via vfork() reports the correct value.
1045  */
1046 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1047 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1048 {
1049 	struct ptrace_lwpinfo pl[2];
1050 	pid_t children[2], fpid, wpid;
1051 	int status;
1052 
1053 	ATF_REQUIRE((fpid = fork()) != -1);
1054 	if (fpid == 0) {
1055 		trace_me();
1056 		follow_fork_parent(true);
1057 	}
1058 
1059 	/* Parent process. */
1060 	children[0] = fpid;
1061 
1062 	/* The first wait() should report the stop from SIGSTOP. */
1063 	wpid = waitpid(children[0], &status, 0);
1064 	ATF_REQUIRE(wpid == children[0]);
1065 	ATF_REQUIRE(WIFSTOPPED(status));
1066 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1067 
1068 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1069 
1070 	/* Continue the child ignoring the SIGSTOP. */
1071 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1072 
1073 	/* Wait for both halves of the fork event to get reported. */
1074 	children[1] = handle_fork_events(children[0], pl);
1075 	ATF_REQUIRE(children[1] > 0);
1076 
1077 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1078 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1079 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1080 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1081 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1082 
1083 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1084 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1085 
1086 	/*
1087 	 * The child can't exit until the grandchild reports status, so the
1088 	 * grandchild should report its exit first to the debugger.
1089 	 */
1090 	wpid = wait(&status);
1091 	ATF_REQUIRE(wpid == children[1]);
1092 	ATF_REQUIRE(WIFEXITED(status));
1093 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1094 
1095 	wpid = wait(&status);
1096 	ATF_REQUIRE(wpid == children[0]);
1097 	ATF_REQUIRE(WIFEXITED(status));
1098 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1099 
1100 	wpid = wait(&status);
1101 	ATF_REQUIRE(wpid == -1);
1102 	ATF_REQUIRE(errno == ECHILD);
1103 }
1104 
1105 static void *
1106 simple_thread(void *arg __unused)
1107 {
1108 
1109 	pthread_exit(NULL);
1110 }
1111 
1112 static __dead2 void
1113 simple_thread_main(void)
1114 {
1115 	pthread_t thread;
1116 
1117 	CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1118 	CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1119 	exit(1);
1120 }
1121 
1122 /*
1123  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1124  * thread reports the correct value.
1125  */
1126 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1127 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1128 {
1129 	struct ptrace_lwpinfo pl;
1130 	pid_t fpid, wpid;
1131 	lwpid_t mainlwp;
1132 	int status;
1133 
1134 	ATF_REQUIRE((fpid = fork()) != -1);
1135 	if (fpid == 0) {
1136 		trace_me();
1137 		simple_thread_main();
1138 	}
1139 
1140 	/* The first wait() should report the stop from SIGSTOP. */
1141 	wpid = waitpid(fpid, &status, 0);
1142 	ATF_REQUIRE(wpid == fpid);
1143 	ATF_REQUIRE(WIFSTOPPED(status));
1144 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1145 
1146 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1147 	    sizeof(pl)) != -1);
1148 	mainlwp = pl.pl_lwpid;
1149 
1150 	/*
1151 	 * Continue the child ignoring the SIGSTOP and tracing all
1152 	 * system call exits.
1153 	 */
1154 	ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1155 
1156 	/*
1157 	 * Wait for the new thread to arrive.  pthread_create() might
1158 	 * invoke any number of system calls.  For now we just wait
1159 	 * for the new thread to arrive and make sure it reports a
1160 	 * valid system call code.  If ptrace grows thread event
1161 	 * reporting then this test can be made more precise.
1162 	 */
1163 	for (;;) {
1164 		wpid = waitpid(fpid, &status, 0);
1165 		ATF_REQUIRE(wpid == fpid);
1166 		ATF_REQUIRE(WIFSTOPPED(status));
1167 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1168 
1169 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1170 		    sizeof(pl)) != -1);
1171 		ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1172 		ATF_REQUIRE(pl.pl_syscall_code != 0);
1173 		if (pl.pl_lwpid != mainlwp)
1174 			/* New thread seen. */
1175 			break;
1176 
1177 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1178 	}
1179 
1180 	/* Wait for the child to exit. */
1181 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1182 	for (;;) {
1183 		wpid = waitpid(fpid, &status, 0);
1184 		ATF_REQUIRE(wpid == fpid);
1185 		if (WIFEXITED(status))
1186 			break;
1187 
1188 		ATF_REQUIRE(WIFSTOPPED(status));
1189 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1190 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1191 	}
1192 
1193 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1194 
1195 	wpid = wait(&status);
1196 	ATF_REQUIRE(wpid == -1);
1197 	ATF_REQUIRE(errno == ECHILD);
1198 }
1199 
1200 /*
1201  * Verify that the expected LWP events are reported for a child thread.
1202  */
1203 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1204 ATF_TC_BODY(ptrace__lwp_events, tc)
1205 {
1206 	struct ptrace_lwpinfo pl;
1207 	pid_t fpid, wpid;
1208 	lwpid_t lwps[2];
1209 	int status;
1210 
1211 	ATF_REQUIRE((fpid = fork()) != -1);
1212 	if (fpid == 0) {
1213 		trace_me();
1214 		simple_thread_main();
1215 	}
1216 
1217 	/* The first wait() should report the stop from SIGSTOP. */
1218 	wpid = waitpid(fpid, &status, 0);
1219 	ATF_REQUIRE(wpid == fpid);
1220 	ATF_REQUIRE(WIFSTOPPED(status));
1221 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1222 
1223 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1224 	    sizeof(pl)) != -1);
1225 	lwps[0] = pl.pl_lwpid;
1226 
1227 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1228 
1229 	/* Continue the child ignoring the SIGSTOP. */
1230 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1231 
1232 	/* The first event should be for the child thread's birth. */
1233 	wpid = waitpid(fpid, &status, 0);
1234 	ATF_REQUIRE(wpid == fpid);
1235 	ATF_REQUIRE(WIFSTOPPED(status));
1236 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1237 
1238 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1239 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1240 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1241 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1242 	lwps[1] = pl.pl_lwpid;
1243 
1244 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1245 
1246 	/* The next event should be for the child thread's death. */
1247 	wpid = waitpid(fpid, &status, 0);
1248 	ATF_REQUIRE(wpid == fpid);
1249 	ATF_REQUIRE(WIFSTOPPED(status));
1250 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1251 
1252 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1253 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1254 	    (PL_FLAG_EXITED | PL_FLAG_SCE));
1255 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1256 
1257 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1258 
1259 	/* The last event should be for the child process's exit. */
1260 	wpid = waitpid(fpid, &status, 0);
1261 	ATF_REQUIRE(WIFEXITED(status));
1262 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1263 
1264 	wpid = wait(&status);
1265 	ATF_REQUIRE(wpid == -1);
1266 	ATF_REQUIRE(errno == ECHILD);
1267 }
1268 
1269 static void *
1270 exec_thread(void *arg __unused)
1271 {
1272 
1273 	execl("/usr/bin/true", "true", NULL);
1274 	exit(127);
1275 }
1276 
1277 static __dead2 void
1278 exec_thread_main(void)
1279 {
1280 	pthread_t thread;
1281 
1282 	CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1283 	for (;;)
1284 		sleep(60);
1285 	exit(1);
1286 }
1287 
1288 /*
1289  * Verify that the expected LWP events are reported for a multithreaded
1290  * process that calls execve(2).
1291  */
1292 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1293 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1294 {
1295 	struct ptrace_lwpinfo pl;
1296 	pid_t fpid, wpid;
1297 	lwpid_t lwps[2];
1298 	int status;
1299 
1300 	ATF_REQUIRE((fpid = fork()) != -1);
1301 	if (fpid == 0) {
1302 		trace_me();
1303 		exec_thread_main();
1304 	}
1305 
1306 	/* The first wait() should report the stop from SIGSTOP. */
1307 	wpid = waitpid(fpid, &status, 0);
1308 	ATF_REQUIRE(wpid == fpid);
1309 	ATF_REQUIRE(WIFSTOPPED(status));
1310 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1311 
1312 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1313 	    sizeof(pl)) != -1);
1314 	lwps[0] = pl.pl_lwpid;
1315 
1316 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1317 
1318 	/* Continue the child ignoring the SIGSTOP. */
1319 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1320 
1321 	/* The first event should be for the child thread's birth. */
1322 	wpid = waitpid(fpid, &status, 0);
1323 	ATF_REQUIRE(wpid == fpid);
1324 	ATF_REQUIRE(WIFSTOPPED(status));
1325 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1326 
1327 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1328 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1329 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1330 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1331 	lwps[1] = pl.pl_lwpid;
1332 
1333 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1334 
1335 	/*
1336 	 * The next event should be for the main thread's death due to
1337 	 * single threading from execve().
1338 	 */
1339 	wpid = waitpid(fpid, &status, 0);
1340 	ATF_REQUIRE(wpid == fpid);
1341 	ATF_REQUIRE(WIFSTOPPED(status));
1342 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1343 
1344 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1345 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1346 	    (PL_FLAG_EXITED));
1347 	ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1348 
1349 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1350 
1351 	/* The next event should be for the child process's exec. */
1352 	wpid = waitpid(fpid, &status, 0);
1353 	ATF_REQUIRE(WIFSTOPPED(status));
1354 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1355 
1356 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1357 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1358 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1359 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1360 
1361 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1362 
1363 	/* The last event should be for the child process's exit. */
1364 	wpid = waitpid(fpid, &status, 0);
1365 	ATF_REQUIRE(WIFEXITED(status));
1366 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1367 
1368 	wpid = wait(&status);
1369 	ATF_REQUIRE(wpid == -1);
1370 	ATF_REQUIRE(errno == ECHILD);
1371 }
1372 
1373 static void
1374 handler(int sig __unused)
1375 {
1376 }
1377 
1378 static void
1379 signal_main(void)
1380 {
1381 
1382 	signal(SIGINFO, handler);
1383 	raise(SIGINFO);
1384 	exit(0);
1385 }
1386 
1387 /*
1388  * Verify that the expected ptrace event is reported for a signal.
1389  */
1390 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1391 ATF_TC_BODY(ptrace__siginfo, tc)
1392 {
1393 	struct ptrace_lwpinfo pl;
1394 	pid_t fpid, wpid;
1395 	int status;
1396 
1397 	ATF_REQUIRE((fpid = fork()) != -1);
1398 	if (fpid == 0) {
1399 		trace_me();
1400 		signal_main();
1401 	}
1402 
1403 	/* The first wait() should report the stop from SIGSTOP. */
1404 	wpid = waitpid(fpid, &status, 0);
1405 	ATF_REQUIRE(wpid == fpid);
1406 	ATF_REQUIRE(WIFSTOPPED(status));
1407 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1408 
1409 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1410 
1411 	/* The next event should be for the SIGINFO. */
1412 	wpid = waitpid(fpid, &status, 0);
1413 	ATF_REQUIRE(WIFSTOPPED(status));
1414 	ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1415 
1416 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1417 	ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1418 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1419 	ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1420 	ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1421 
1422 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1423 
1424 	/* The last event should be for the child process's exit. */
1425 	wpid = waitpid(fpid, &status, 0);
1426 	ATF_REQUIRE(WIFEXITED(status));
1427 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1428 
1429 	wpid = wait(&status);
1430 	ATF_REQUIRE(wpid == -1);
1431 	ATF_REQUIRE(errno == ECHILD);
1432 }
1433 
1434 /*
1435  * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1436  */
1437 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1438 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1439 {
1440 	pid_t fpid, wpid;
1441 	int events, status;
1442 
1443 	ATF_REQUIRE((fpid = fork()) != -1);
1444 	if (fpid == 0) {
1445 		trace_me();
1446 		exec_thread(NULL);
1447 	}
1448 
1449 	/* The first wait() should report the stop from SIGSTOP. */
1450 	wpid = waitpid(fpid, &status, 0);
1451 	ATF_REQUIRE(wpid == fpid);
1452 	ATF_REQUIRE(WIFSTOPPED(status));
1453 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1454 
1455 	events = 0;
1456 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1457 	    sizeof(events)) == 0);
1458 
1459 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1460 
1461 	/* Should get one event at exit. */
1462 	wpid = waitpid(fpid, &status, 0);
1463 	ATF_REQUIRE(WIFEXITED(status));
1464 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1465 
1466 	wpid = wait(&status);
1467 	ATF_REQUIRE(wpid == -1);
1468 	ATF_REQUIRE(errno == ECHILD);
1469 }
1470 
1471 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1472 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1473 {
1474 	struct ptrace_lwpinfo pl;
1475 	pid_t fpid, wpid;
1476 	int events, status;
1477 
1478 	ATF_REQUIRE((fpid = fork()) != -1);
1479 	if (fpid == 0) {
1480 		trace_me();
1481 		exec_thread(NULL);
1482 	}
1483 
1484 	/* The first wait() should report the stop from SIGSTOP. */
1485 	wpid = waitpid(fpid, &status, 0);
1486 	ATF_REQUIRE(wpid == fpid);
1487 	ATF_REQUIRE(WIFSTOPPED(status));
1488 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1489 
1490 	events = PTRACE_EXEC;
1491 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1492 	    sizeof(events)) == 0);
1493 
1494 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1495 
1496 	/* The next event should be for the child process's exec. */
1497 	wpid = waitpid(fpid, &status, 0);
1498 	ATF_REQUIRE(WIFSTOPPED(status));
1499 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1500 
1501 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1502 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1503 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1504 
1505 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1506 
1507 	/* The last event should be for the child process's exit. */
1508 	wpid = waitpid(fpid, &status, 0);
1509 	ATF_REQUIRE(WIFEXITED(status));
1510 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1511 
1512 	wpid = wait(&status);
1513 	ATF_REQUIRE(wpid == -1);
1514 	ATF_REQUIRE(errno == ECHILD);
1515 }
1516 
1517 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1518 ATF_TC_BODY(ptrace__event_mask, tc)
1519 {
1520 	pid_t fpid, wpid;
1521 	int events, status;
1522 
1523 	ATF_REQUIRE((fpid = fork()) != -1);
1524 	if (fpid == 0) {
1525 		trace_me();
1526 		exit(0);
1527 	}
1528 
1529 	/* The first wait() should report the stop from SIGSTOP. */
1530 	wpid = waitpid(fpid, &status, 0);
1531 	ATF_REQUIRE(wpid == fpid);
1532 	ATF_REQUIRE(WIFSTOPPED(status));
1533 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1534 
1535 	/* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1536 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1537 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1538 	    sizeof(events)) == 0);
1539 	ATF_REQUIRE(events & PTRACE_FORK);
1540 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1541 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1542 	    sizeof(events)) == 0);
1543 	ATF_REQUIRE(!(events & PTRACE_FORK));
1544 
1545 	/* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1546 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1547 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1548 	    sizeof(events)) == 0);
1549 	ATF_REQUIRE(events & PTRACE_LWP);
1550 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1551 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1552 	    sizeof(events)) == 0);
1553 	ATF_REQUIRE(!(events & PTRACE_LWP));
1554 
1555 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1556 
1557 	/* Should get one event at exit. */
1558 	wpid = waitpid(fpid, &status, 0);
1559 	ATF_REQUIRE(WIFEXITED(status));
1560 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1561 
1562 	wpid = wait(&status);
1563 	ATF_REQUIRE(wpid == -1);
1564 	ATF_REQUIRE(errno == ECHILD);
1565 }
1566 
1567 /*
1568  * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1569  */
1570 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1571 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1572 {
1573 	struct ptrace_lwpinfo pl;
1574 	pid_t fpid, wpid;
1575 	int events, status;
1576 
1577 	ATF_REQUIRE((fpid = fork()) != -1);
1578 	if (fpid == 0) {
1579 		trace_me();
1580 		follow_fork_parent(true);
1581 	}
1582 
1583 	/* The first wait() should report the stop from SIGSTOP. */
1584 	wpid = waitpid(fpid, &status, 0);
1585 	ATF_REQUIRE(wpid == fpid);
1586 	ATF_REQUIRE(WIFSTOPPED(status));
1587 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1588 
1589 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1590 	    sizeof(events)) == 0);
1591 	events |= PTRACE_VFORK;
1592 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1593 	    sizeof(events)) == 0);
1594 
1595 	/* Continue the child ignoring the SIGSTOP. */
1596 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1597 
1598 	/* The next event should report the end of the vfork. */
1599 	wpid = wait(&status);
1600 	ATF_REQUIRE(wpid == fpid);
1601 	ATF_REQUIRE(WIFSTOPPED(status));
1602 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1603 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1604 	ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1605 
1606 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1607 
1608 	wpid = wait(&status);
1609 	ATF_REQUIRE(wpid == fpid);
1610 	ATF_REQUIRE(WIFEXITED(status));
1611 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1612 
1613 	wpid = wait(&status);
1614 	ATF_REQUIRE(wpid == -1);
1615 	ATF_REQUIRE(errno == ECHILD);
1616 }
1617 
1618 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1619 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1620 {
1621 	struct ptrace_lwpinfo pl[2];
1622 	pid_t children[2], fpid, wpid;
1623 	int events, status;
1624 
1625 	ATF_REQUIRE((fpid = fork()) != -1);
1626 	if (fpid == 0) {
1627 		trace_me();
1628 		follow_fork_parent(true);
1629 	}
1630 
1631 	/* Parent process. */
1632 	children[0] = fpid;
1633 
1634 	/* The first wait() should report the stop from SIGSTOP. */
1635 	wpid = waitpid(children[0], &status, 0);
1636 	ATF_REQUIRE(wpid == children[0]);
1637 	ATF_REQUIRE(WIFSTOPPED(status));
1638 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1639 
1640 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1641 	    sizeof(events)) == 0);
1642 	events |= PTRACE_FORK | PTRACE_VFORK;
1643 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1644 	    sizeof(events)) == 0);
1645 
1646 	/* Continue the child ignoring the SIGSTOP. */
1647 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1648 
1649 	/* Wait for both halves of the fork event to get reported. */
1650 	children[1] = handle_fork_events(children[0], pl);
1651 	ATF_REQUIRE(children[1] > 0);
1652 
1653 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1654 
1655 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1656 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1657 
1658 	/*
1659 	 * The child can't exit until the grandchild reports status, so the
1660 	 * grandchild should report its exit first to the debugger.
1661 	 */
1662 	wpid = waitpid(children[1], &status, 0);
1663 	ATF_REQUIRE(wpid == children[1]);
1664 	ATF_REQUIRE(WIFEXITED(status));
1665 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1666 
1667 	/*
1668 	 * The child should report it's vfork() completion before it
1669 	 * exits.
1670 	 */
1671 	wpid = wait(&status);
1672 	ATF_REQUIRE(wpid == children[0]);
1673 	ATF_REQUIRE(WIFSTOPPED(status));
1674 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1675 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1676 	    -1);
1677 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1678 
1679 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1680 
1681 	wpid = wait(&status);
1682 	ATF_REQUIRE(wpid == children[0]);
1683 	ATF_REQUIRE(WIFEXITED(status));
1684 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1685 
1686 	wpid = wait(&status);
1687 	ATF_REQUIRE(wpid == -1);
1688 	ATF_REQUIRE(errno == ECHILD);
1689 }
1690 
1691 /*
1692  * XXX: There's nothing inherently platform specific about this test, however a
1693  * userspace visible breakpoint() is a prerequisite.
1694  */
1695  #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1696 /*
1697  * Verify that no more events are reported after PT_KILL except for the
1698  * process exit when stopped due to a breakpoint trap.
1699  */
1700 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1701 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1702 {
1703 	pid_t fpid, wpid;
1704 	int status;
1705 
1706 	ATF_REQUIRE((fpid = fork()) != -1);
1707 	if (fpid == 0) {
1708 		trace_me();
1709 		breakpoint();
1710 		exit(1);
1711 	}
1712 
1713 	/* The first wait() should report the stop from SIGSTOP. */
1714 	wpid = waitpid(fpid, &status, 0);
1715 	ATF_REQUIRE(wpid == fpid);
1716 	ATF_REQUIRE(WIFSTOPPED(status));
1717 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1718 
1719 	/* Continue the child ignoring the SIGSTOP. */
1720 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1721 
1722 	/* The second wait() should report hitting the breakpoint. */
1723 	wpid = waitpid(fpid, &status, 0);
1724 	ATF_REQUIRE(wpid == fpid);
1725 	ATF_REQUIRE(WIFSTOPPED(status));
1726 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1727 
1728 	/* Kill the child process. */
1729 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1730 
1731 	/* The last wait() should report the SIGKILL. */
1732 	wpid = waitpid(fpid, &status, 0);
1733 	ATF_REQUIRE(wpid == fpid);
1734 	ATF_REQUIRE(WIFSIGNALED(status));
1735 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1736 
1737 	wpid = wait(&status);
1738 	ATF_REQUIRE(wpid == -1);
1739 	ATF_REQUIRE(errno == ECHILD);
1740 }
1741 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1742 
1743 /*
1744  * Verify that no more events are reported after PT_KILL except for the
1745  * process exit when stopped inside of a system call.
1746  */
1747 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1748 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1749 {
1750 	struct ptrace_lwpinfo pl;
1751 	pid_t fpid, wpid;
1752 	int status;
1753 
1754 	ATF_REQUIRE((fpid = fork()) != -1);
1755 	if (fpid == 0) {
1756 		trace_me();
1757 		getpid();
1758 		exit(1);
1759 	}
1760 
1761 	/* The first wait() should report the stop from SIGSTOP. */
1762 	wpid = waitpid(fpid, &status, 0);
1763 	ATF_REQUIRE(wpid == fpid);
1764 	ATF_REQUIRE(WIFSTOPPED(status));
1765 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1766 
1767 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
1768 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1769 
1770 	/* The second wait() should report a system call entry for getpid(). */
1771 	wpid = waitpid(fpid, &status, 0);
1772 	ATF_REQUIRE(wpid == fpid);
1773 	ATF_REQUIRE(WIFSTOPPED(status));
1774 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1775 
1776 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1777 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1778 
1779 	/* Kill the child process. */
1780 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1781 
1782 	/* The last wait() should report the SIGKILL. */
1783 	wpid = waitpid(fpid, &status, 0);
1784 	ATF_REQUIRE(wpid == fpid);
1785 	ATF_REQUIRE(WIFSIGNALED(status));
1786 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1787 
1788 	wpid = wait(&status);
1789 	ATF_REQUIRE(wpid == -1);
1790 	ATF_REQUIRE(errno == ECHILD);
1791 }
1792 
1793 /*
1794  * Verify that no more events are reported after PT_KILL except for the
1795  * process exit when killing a multithreaded process.
1796  */
1797 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1798 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1799 {
1800 	struct ptrace_lwpinfo pl;
1801 	pid_t fpid, wpid;
1802 	lwpid_t main_lwp;
1803 	int status;
1804 
1805 	ATF_REQUIRE((fpid = fork()) != -1);
1806 	if (fpid == 0) {
1807 		trace_me();
1808 		simple_thread_main();
1809 	}
1810 
1811 	/* The first wait() should report the stop from SIGSTOP. */
1812 	wpid = waitpid(fpid, &status, 0);
1813 	ATF_REQUIRE(wpid == fpid);
1814 	ATF_REQUIRE(WIFSTOPPED(status));
1815 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1816 
1817 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1818 	    sizeof(pl)) != -1);
1819 	main_lwp = pl.pl_lwpid;
1820 
1821 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1822 
1823 	/* Continue the child ignoring the SIGSTOP. */
1824 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1825 
1826 	/* The first event should be for the child thread's birth. */
1827 	wpid = waitpid(fpid, &status, 0);
1828 	ATF_REQUIRE(wpid == fpid);
1829 	ATF_REQUIRE(WIFSTOPPED(status));
1830 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1831 
1832 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1833 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1834 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1835 	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1836 
1837 	/* Kill the child process. */
1838 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1839 
1840 	/* The last wait() should report the SIGKILL. */
1841 	wpid = waitpid(fpid, &status, 0);
1842 	ATF_REQUIRE(wpid == fpid);
1843 	ATF_REQUIRE(WIFSIGNALED(status));
1844 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1845 
1846 	wpid = wait(&status);
1847 	ATF_REQUIRE(wpid == -1);
1848 	ATF_REQUIRE(errno == ECHILD);
1849 }
1850 
1851 static void *
1852 mask_usr1_thread(void *arg)
1853 {
1854 	pthread_barrier_t *pbarrier;
1855 	sigset_t sigmask;
1856 
1857 	pbarrier = (pthread_barrier_t*)arg;
1858 
1859 	sigemptyset(&sigmask);
1860 	sigaddset(&sigmask, SIGUSR1);
1861 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1862 
1863 	/* Sync up with other thread after sigmask updated. */
1864 	pthread_barrier_wait(pbarrier);
1865 
1866 	for (;;)
1867 		sleep(60);
1868 
1869 	return (NULL);
1870 }
1871 
1872 /*
1873  * Verify that the SIGKILL from PT_KILL takes priority over other signals
1874  * and prevents spurious stops due to those other signals.
1875  */
1876 ATF_TC(ptrace__PT_KILL_competing_signal);
1877 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
1878 {
1879 
1880 	atf_tc_set_md_var(tc, "require.user", "root");
1881 }
1882 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1883 {
1884 	pid_t fpid, wpid;
1885 	int status;
1886 	cpuset_t setmask;
1887 	pthread_t t;
1888 	pthread_barrier_t barrier;
1889 	struct sched_param sched_param;
1890 
1891 	ATF_REQUIRE((fpid = fork()) != -1);
1892 	if (fpid == 0) {
1893 		/* Bind to one CPU so only one thread at a time will run. */
1894 		CPU_ZERO(&setmask);
1895 		CPU_SET(0, &setmask);
1896 		cpusetid_t setid;
1897 		CHILD_REQUIRE(cpuset(&setid) == 0);
1898 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1899 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1900 
1901 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1902 
1903 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1904 		    (void*)&barrier) == 0);
1905 
1906 		/*
1907 		 * Give the main thread higher priority. The test always
1908 		 * assumes that, if both threads are able to run, the main
1909 		 * thread runs first.
1910 		 */
1911 		sched_param.sched_priority =
1912 		    (sched_get_priority_max(SCHED_FIFO) +
1913 		    sched_get_priority_min(SCHED_FIFO)) / 2;
1914 		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1915 		    SCHED_FIFO, &sched_param) == 0);
1916 		sched_param.sched_priority -= RQ_PPQ;
1917 		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1918 		    &sched_param) == 0);
1919 
1920 		sigset_t sigmask;
1921 		sigemptyset(&sigmask);
1922 		sigaddset(&sigmask, SIGUSR2);
1923 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1924 
1925 		/* Sync up with other thread after sigmask updated. */
1926 		pthread_barrier_wait(&barrier);
1927 
1928 		trace_me();
1929 
1930 		for (;;)
1931 			sleep(60);
1932 
1933 		exit(1);
1934 	}
1935 
1936 	/* The first wait() should report the stop from SIGSTOP. */
1937 	wpid = waitpid(fpid, &status, 0);
1938 	ATF_REQUIRE(wpid == fpid);
1939 	ATF_REQUIRE(WIFSTOPPED(status));
1940 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1941 
1942 	/* Continue the child ignoring the SIGSTOP. */
1943 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1944 
1945 	/* Send a signal that only the second thread can handle. */
1946 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1947 
1948 	/* The second wait() should report the SIGUSR2. */
1949 	wpid = waitpid(fpid, &status, 0);
1950 	ATF_REQUIRE(wpid == fpid);
1951 	ATF_REQUIRE(WIFSTOPPED(status));
1952 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1953 
1954 	/* Send a signal that only the first thread can handle. */
1955 	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1956 
1957 	/* Replace the SIGUSR2 with a kill. */
1958 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1959 
1960 	/* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1961 	wpid = waitpid(fpid, &status, 0);
1962 	ATF_REQUIRE(wpid == fpid);
1963 	ATF_REQUIRE(WIFSIGNALED(status));
1964 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1965 
1966 	wpid = wait(&status);
1967 	ATF_REQUIRE(wpid == -1);
1968 	ATF_REQUIRE(errno == ECHILD);
1969 }
1970 
1971 /*
1972  * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1973  * and prevents spurious stops caused by those events.
1974  */
1975 ATF_TC(ptrace__PT_KILL_competing_stop);
1976 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
1977 {
1978 
1979 	atf_tc_set_md_var(tc, "require.user", "root");
1980 }
1981 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1982 {
1983 	pid_t fpid, wpid;
1984 	int status;
1985 	cpuset_t setmask;
1986 	pthread_t t;
1987 	pthread_barrier_t barrier;
1988 	lwpid_t main_lwp;
1989 	struct ptrace_lwpinfo pl;
1990 	struct sched_param sched_param;
1991 
1992 	ATF_REQUIRE((fpid = fork()) != -1);
1993 	if (fpid == 0) {
1994 		trace_me();
1995 
1996 		/* Bind to one CPU so only one thread at a time will run. */
1997 		CPU_ZERO(&setmask);
1998 		CPU_SET(0, &setmask);
1999 		cpusetid_t setid;
2000 		CHILD_REQUIRE(cpuset(&setid) == 0);
2001 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2002 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2003 
2004 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2005 
2006 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2007 		    (void*)&barrier) == 0);
2008 
2009 		/*
2010 		 * Give the main thread higher priority. The test always
2011 		 * assumes that, if both threads are able to run, the main
2012 		 * thread runs first.
2013 		 */
2014 		sched_param.sched_priority =
2015 		    (sched_get_priority_max(SCHED_FIFO) +
2016 		    sched_get_priority_min(SCHED_FIFO)) / 2;
2017 		CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2018 		    SCHED_FIFO, &sched_param) == 0);
2019 		sched_param.sched_priority -= RQ_PPQ;
2020 		CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2021 		    &sched_param) == 0);
2022 
2023 		sigset_t sigmask;
2024 		sigemptyset(&sigmask);
2025 		sigaddset(&sigmask, SIGUSR2);
2026 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2027 
2028 		/* Sync up with other thread after sigmask updated. */
2029 		pthread_barrier_wait(&barrier);
2030 
2031 		/* Sync up with the test before doing the getpid(). */
2032 		raise(SIGSTOP);
2033 
2034 		getpid();
2035 		exit(1);
2036 	}
2037 
2038 	/* The first wait() should report the stop from SIGSTOP. */
2039 	wpid = waitpid(fpid, &status, 0);
2040 	ATF_REQUIRE(wpid == fpid);
2041 	ATF_REQUIRE(WIFSTOPPED(status));
2042 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2043 
2044 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2045 	main_lwp = pl.pl_lwpid;
2046 
2047 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2048 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2049 
2050 	/*
2051 	 * Continue until child is done with setup, which is indicated with
2052 	 * SIGSTOP. Ignore system calls in the meantime.
2053 	 */
2054 	for (;;) {
2055 		wpid = waitpid(fpid, &status, 0);
2056 		ATF_REQUIRE(wpid == fpid);
2057 		ATF_REQUIRE(WIFSTOPPED(status));
2058 		if (WSTOPSIG(status) == SIGTRAP) {
2059 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2060 			    sizeof(pl)) != -1);
2061 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2062 		} else {
2063 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2064 			break;
2065 		}
2066 		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2067 	}
2068 
2069 	/* Proceed, allowing main thread to hit syscall entry for getpid(). */
2070 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2071 
2072 	wpid = waitpid(fpid, &status, 0);
2073 	ATF_REQUIRE(wpid == fpid);
2074 	ATF_REQUIRE(WIFSTOPPED(status));
2075 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2076 
2077 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2078 	    sizeof(pl)) != -1);
2079 	ATF_REQUIRE(pl.pl_lwpid == main_lwp);
2080 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2081 	/* Prevent the main thread from hitting its syscall exit for now. */
2082 	ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2083 
2084 	/*
2085 	 * Proceed, allowing second thread to hit syscall exit for
2086 	 * pthread_barrier_wait().
2087 	 */
2088 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2089 
2090 	wpid = waitpid(fpid, &status, 0);
2091 	ATF_REQUIRE(wpid == fpid);
2092 	ATF_REQUIRE(WIFSTOPPED(status));
2093 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2094 
2095 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2096 	    sizeof(pl)) != -1);
2097 	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2098 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2099 
2100 	/* Send a signal that only the second thread can handle. */
2101 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2102 
2103 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2104 
2105 	/* The next wait() should report the SIGUSR2. */
2106 	wpid = waitpid(fpid, &status, 0);
2107 	ATF_REQUIRE(wpid == fpid);
2108 	ATF_REQUIRE(WIFSTOPPED(status));
2109 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2110 
2111 	/* Allow the main thread to try to finish its system call. */
2112 	ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2113 
2114 	/*
2115 	 * At this point, the main thread is in the middle of a system call and
2116 	 * has been resumed. The second thread has taken a SIGUSR2 which will
2117 	 * be replaced with a SIGKILL below. The main thread will get to run
2118 	 * first. It should notice the kill request (even though the signal
2119 	 * replacement occurred in the other thread) and exit accordingly.  It
2120 	 * should not stop for the system call exit event.
2121 	 */
2122 
2123 	/* Replace the SIGUSR2 with a kill. */
2124 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2125 
2126 	/* The last wait() should report the SIGKILL (not a syscall exit). */
2127 	wpid = waitpid(fpid, &status, 0);
2128 	ATF_REQUIRE(wpid == fpid);
2129 	ATF_REQUIRE(WIFSIGNALED(status));
2130 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2131 
2132 	wpid = wait(&status);
2133 	ATF_REQUIRE(wpid == -1);
2134 	ATF_REQUIRE(errno == ECHILD);
2135 }
2136 
2137 static void
2138 sigusr1_handler(int sig)
2139 {
2140 
2141 	CHILD_REQUIRE(sig == SIGUSR1);
2142 	_exit(2);
2143 }
2144 
2145 /*
2146  * Verify that even if the signal queue is full for a child process,
2147  * a PT_KILL will kill the process.
2148  */
2149 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2150 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2151 {
2152 	pid_t fpid, wpid;
2153 	int status;
2154 	int max_pending_per_proc;
2155 	size_t len;
2156 	int i;
2157 
2158 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2159 
2160 	ATF_REQUIRE((fpid = fork()) != -1);
2161 	if (fpid == 0) {
2162 		trace_me();
2163 		exit(1);
2164 	}
2165 
2166 	/* The first wait() should report the stop from SIGSTOP. */
2167 	wpid = waitpid(fpid, &status, 0);
2168 	ATF_REQUIRE(wpid == fpid);
2169 	ATF_REQUIRE(WIFSTOPPED(status));
2170 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2171 
2172 	len = sizeof(max_pending_per_proc);
2173 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2174 	    &max_pending_per_proc, &len, NULL, 0) == 0);
2175 
2176 	/* Fill the signal queue. */
2177 	for (i = 0; i < max_pending_per_proc; ++i)
2178 		ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2179 
2180 	/* Kill the child process. */
2181 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2182 
2183 	/* The last wait() should report the SIGKILL. */
2184 	wpid = waitpid(fpid, &status, 0);
2185 	ATF_REQUIRE(wpid == fpid);
2186 	ATF_REQUIRE(WIFSIGNALED(status));
2187 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2188 
2189 	wpid = wait(&status);
2190 	ATF_REQUIRE(wpid == -1);
2191 	ATF_REQUIRE(errno == ECHILD);
2192 }
2193 
2194 /*
2195  * Verify that when stopped at a system call entry, a signal can be
2196  * requested with PT_CONTINUE which will be delivered once the system
2197  * call is complete.
2198  */
2199 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2200 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2201 {
2202 	struct ptrace_lwpinfo pl;
2203 	pid_t fpid, wpid;
2204 	int status;
2205 
2206 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2207 
2208 	ATF_REQUIRE((fpid = fork()) != -1);
2209 	if (fpid == 0) {
2210 		trace_me();
2211 		getpid();
2212 		exit(1);
2213 	}
2214 
2215 	/* The first wait() should report the stop from SIGSTOP. */
2216 	wpid = waitpid(fpid, &status, 0);
2217 	ATF_REQUIRE(wpid == fpid);
2218 	ATF_REQUIRE(WIFSTOPPED(status));
2219 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2220 
2221 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2222 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2223 
2224 	/* The second wait() should report a system call entry for getpid(). */
2225 	wpid = waitpid(fpid, &status, 0);
2226 	ATF_REQUIRE(wpid == fpid);
2227 	ATF_REQUIRE(WIFSTOPPED(status));
2228 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2229 
2230 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2231 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2232 
2233 	/* Continue the child process with a signal. */
2234 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2235 
2236 	for (;;) {
2237 		/*
2238 		 * The last wait() should report exit 2, i.e., a normal _exit
2239 		 * from the signal handler. In the meantime, catch and proceed
2240 		 * past any syscall stops.
2241 		 */
2242 		wpid = waitpid(fpid, &status, 0);
2243 		ATF_REQUIRE(wpid == fpid);
2244 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2245 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2246 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2247 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2248 		} else {
2249 			ATF_REQUIRE(WIFEXITED(status));
2250 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2251 			break;
2252 		}
2253 	}
2254 
2255 	wpid = wait(&status);
2256 	ATF_REQUIRE(wpid == -1);
2257 	ATF_REQUIRE(errno == ECHILD);
2258 }
2259 
2260 static void
2261 sigusr1_counting_handler(int sig)
2262 {
2263 	static int counter = 0;
2264 
2265 	CHILD_REQUIRE(sig == SIGUSR1);
2266 	counter++;
2267 	if (counter == 2)
2268 		_exit(2);
2269 }
2270 
2271 /*
2272  * Verify that, when continuing from a stop at system call entry and exit,
2273  * a signal can be requested from both stops, and both will be delivered when
2274  * the system call is complete.
2275  */
2276 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2277 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2278 {
2279 	struct ptrace_lwpinfo pl;
2280 	pid_t fpid, wpid;
2281 	int status;
2282 
2283 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2284 
2285 	ATF_REQUIRE((fpid = fork()) != -1);
2286 	if (fpid == 0) {
2287 		trace_me();
2288 		getpid();
2289 		exit(1);
2290 	}
2291 
2292 	/* The first wait() should report the stop from SIGSTOP. */
2293 	wpid = waitpid(fpid, &status, 0);
2294 	ATF_REQUIRE(wpid == fpid);
2295 	ATF_REQUIRE(WIFSTOPPED(status));
2296 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2297 
2298 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2299 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2300 
2301 	/* The second wait() should report a system call entry for getpid(). */
2302 	wpid = waitpid(fpid, &status, 0);
2303 	ATF_REQUIRE(wpid == fpid);
2304 	ATF_REQUIRE(WIFSTOPPED(status));
2305 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2306 
2307 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2308 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2309 
2310 	/* Continue the child process with a signal. */
2311 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2312 
2313 	/* The third wait() should report a system call exit for getpid(). */
2314 	wpid = waitpid(fpid, &status, 0);
2315 	ATF_REQUIRE(wpid == fpid);
2316 	ATF_REQUIRE(WIFSTOPPED(status));
2317 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2318 
2319 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2320 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2321 
2322 	/* Continue the child process with a signal. */
2323 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2324 
2325 	for (;;) {
2326 		/*
2327 		 * The last wait() should report exit 2, i.e., a normal _exit
2328 		 * from the signal handler. In the meantime, catch and proceed
2329 		 * past any syscall stops.
2330 		 */
2331 		wpid = waitpid(fpid, &status, 0);
2332 		ATF_REQUIRE(wpid == fpid);
2333 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2334 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2335 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2336 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2337 		} else {
2338 			ATF_REQUIRE(WIFEXITED(status));
2339 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2340 			break;
2341 		}
2342 	}
2343 
2344 	wpid = wait(&status);
2345 	ATF_REQUIRE(wpid == -1);
2346 	ATF_REQUIRE(errno == ECHILD);
2347 }
2348 
2349 /*
2350  * Verify that even if the signal queue is full for a child process,
2351  * a PT_CONTINUE with a signal will not result in loss of that signal.
2352  */
2353 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2354 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2355 {
2356 	pid_t fpid, wpid;
2357 	int status;
2358 	int max_pending_per_proc;
2359 	size_t len;
2360 	int i;
2361 
2362 	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2363 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2364 
2365 	ATF_REQUIRE((fpid = fork()) != -1);
2366 	if (fpid == 0) {
2367 		trace_me();
2368 		exit(1);
2369 	}
2370 
2371 	/* The first wait() should report the stop from SIGSTOP. */
2372 	wpid = waitpid(fpid, &status, 0);
2373 	ATF_REQUIRE(wpid == fpid);
2374 	ATF_REQUIRE(WIFSTOPPED(status));
2375 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2376 
2377 	len = sizeof(max_pending_per_proc);
2378 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2379 	    &max_pending_per_proc, &len, NULL, 0) == 0);
2380 
2381 	/* Fill the signal queue. */
2382 	for (i = 0; i < max_pending_per_proc; ++i)
2383 		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2384 
2385 	/* Continue with signal. */
2386 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2387 
2388 	for (;;) {
2389 		wpid = waitpid(fpid, &status, 0);
2390 		ATF_REQUIRE(wpid == fpid);
2391 		if (WIFSTOPPED(status)) {
2392 			ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2393 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2394 		} else {
2395 			/*
2396 			 * The last wait() should report normal _exit from the
2397 			 * SIGUSR1 handler.
2398 			 */
2399 			ATF_REQUIRE(WIFEXITED(status));
2400 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2401 			break;
2402 		}
2403 	}
2404 
2405 	wpid = wait(&status);
2406 	ATF_REQUIRE(wpid == -1);
2407 	ATF_REQUIRE(errno == ECHILD);
2408 }
2409 
2410 static sem_t sigusr1_sem;
2411 static int got_usr1;
2412 
2413 static void
2414 sigusr1_sempost_handler(int sig __unused)
2415 {
2416 
2417 	got_usr1++;
2418 	CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2419 }
2420 
2421 /*
2422  * Verify that even if the signal queue is full for a child process,
2423  * and the signal is masked, a PT_CONTINUE with a signal will not
2424  * result in loss of that signal.
2425  */
2426 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2427 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2428 {
2429 	struct ptrace_lwpinfo pl;
2430 	pid_t fpid, wpid;
2431 	int status, err;
2432 	int max_pending_per_proc;
2433 	size_t len;
2434 	int i;
2435 	sigset_t sigmask;
2436 
2437 	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2438 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2439 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2440 
2441 	got_usr1 = 0;
2442 	ATF_REQUIRE((fpid = fork()) != -1);
2443 	if (fpid == 0) {
2444 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2445 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2446 		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2447 
2448 		trace_me();
2449 		CHILD_REQUIRE(got_usr1 == 0);
2450 
2451 		/* Allow the pending SIGUSR1 in now. */
2452 		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2453 		/* Wait to receive the SIGUSR1. */
2454 		do {
2455 			err = sem_wait(&sigusr1_sem);
2456 			CHILD_REQUIRE(err == 0 || errno == EINTR);
2457 		} while (err != 0 && errno == EINTR);
2458 		CHILD_REQUIRE(got_usr1 == 1);
2459 		exit(1);
2460 	}
2461 
2462 	/* The first wait() should report the stop from SIGSTOP. */
2463 	wpid = waitpid(fpid, &status, 0);
2464 	ATF_REQUIRE(wpid == fpid);
2465 	ATF_REQUIRE(WIFSTOPPED(status));
2466 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2467 
2468 	len = sizeof(max_pending_per_proc);
2469 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2470 	    &max_pending_per_proc, &len, NULL, 0) == 0);
2471 
2472 	/* Fill the signal queue. */
2473 	for (i = 0; i < max_pending_per_proc; ++i)
2474 		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2475 
2476 	/* Continue with signal. */
2477 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2478 
2479 	/* Collect and ignore all of the SIGUSR2. */
2480 	for (i = 0; i < max_pending_per_proc; ++i) {
2481 		wpid = waitpid(fpid, &status, 0);
2482 		ATF_REQUIRE(wpid == fpid);
2483 		ATF_REQUIRE(WIFSTOPPED(status));
2484 		ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2485 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2486 	}
2487 
2488 	/* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2489 	wpid = waitpid(fpid, &status, 0);
2490 	ATF_REQUIRE(wpid == fpid);
2491 	ATF_REQUIRE(WIFSTOPPED(status));
2492 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2493 	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2494 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2495 
2496 	/* Continue the child, ignoring the SIGUSR1. */
2497 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2498 
2499 	/* The last wait() should report exit after receiving SIGUSR1. */
2500 	wpid = waitpid(fpid, &status, 0);
2501 	ATF_REQUIRE(wpid == fpid);
2502 	ATF_REQUIRE(WIFEXITED(status));
2503 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2504 
2505 	wpid = wait(&status);
2506 	ATF_REQUIRE(wpid == -1);
2507 	ATF_REQUIRE(errno == ECHILD);
2508 }
2509 
2510 /*
2511  * Verify that, after stopping due to a signal, that signal can be
2512  * replaced with another signal.
2513  */
2514 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2515 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2516 {
2517 	struct ptrace_lwpinfo pl;
2518 	pid_t fpid, wpid;
2519 	int status;
2520 
2521 	ATF_REQUIRE((fpid = fork()) != -1);
2522 	if (fpid == 0) {
2523 		trace_me();
2524 		sleep(20);
2525 		exit(1);
2526 	}
2527 
2528 	/* The first wait() should report the stop from SIGSTOP. */
2529 	wpid = waitpid(fpid, &status, 0);
2530 	ATF_REQUIRE(wpid == fpid);
2531 	ATF_REQUIRE(WIFSTOPPED(status));
2532 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2533 
2534 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2535 
2536 	/* Send a signal without ptrace. */
2537 	ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2538 
2539 	/* The second wait() should report a SIGINT was received. */
2540 	wpid = waitpid(fpid, &status, 0);
2541 	ATF_REQUIRE(wpid == fpid);
2542 	ATF_REQUIRE(WIFSTOPPED(status));
2543 	ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2544 
2545 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2546 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2547 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2548 
2549 	/* Continue the child process with a different signal. */
2550 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2551 
2552 	/*
2553 	 * The last wait() should report having died due to the new
2554 	 * signal, SIGTERM.
2555 	 */
2556 	wpid = waitpid(fpid, &status, 0);
2557 	ATF_REQUIRE(wpid == fpid);
2558 	ATF_REQUIRE(WIFSIGNALED(status));
2559 	ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2560 
2561 	wpid = wait(&status);
2562 	ATF_REQUIRE(wpid == -1);
2563 	ATF_REQUIRE(errno == ECHILD);
2564 }
2565 
2566 /*
2567  * Verify that a signal can be passed through to the child even when there
2568  * was no true signal originally. Such cases arise when a SIGTRAP is
2569  * invented for e.g, system call stops.
2570  */
2571 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2572 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2573 {
2574 	struct ptrace_lwpinfo pl;
2575 	struct rlimit rl;
2576 	pid_t fpid, wpid;
2577 	int status;
2578 
2579 	ATF_REQUIRE((fpid = fork()) != -1);
2580 	if (fpid == 0) {
2581 		trace_me();
2582 		/* SIGTRAP expected to cause exit on syscall entry. */
2583 		rl.rlim_cur = rl.rlim_max = 0;
2584 		ATF_REQUIRE(setrlimit(RLIMIT_CORE, &rl) == 0);
2585 		getpid();
2586 		exit(1);
2587 	}
2588 
2589 	/* The first wait() should report the stop from SIGSTOP. */
2590 	wpid = waitpid(fpid, &status, 0);
2591 	ATF_REQUIRE(wpid == fpid);
2592 	ATF_REQUIRE(WIFSTOPPED(status));
2593 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2594 
2595 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2596 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2597 
2598 	/* The second wait() should report a system call entry for getpid(). */
2599 	wpid = waitpid(fpid, &status, 0);
2600 	ATF_REQUIRE(wpid == fpid);
2601 	ATF_REQUIRE(WIFSTOPPED(status));
2602 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2603 
2604 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2605 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2606 
2607 	/* Continue the child process with a SIGTRAP. */
2608 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2609 
2610 	for (;;) {
2611 		/*
2612 		 * The last wait() should report exit due to SIGTRAP.  In the
2613 		 * meantime, catch and proceed past any syscall stops.
2614 		 */
2615 		wpid = waitpid(fpid, &status, 0);
2616 		ATF_REQUIRE(wpid == fpid);
2617 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2618 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2619 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2620 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2621 		} else {
2622 			ATF_REQUIRE(WIFSIGNALED(status));
2623 			ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2624 			break;
2625 		}
2626 	}
2627 
2628 	wpid = wait(&status);
2629 	ATF_REQUIRE(wpid == -1);
2630 	ATF_REQUIRE(errno == ECHILD);
2631 
2632 }
2633 
2634 /*
2635  * A mixed bag PT_CONTINUE with signal test.
2636  */
2637 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2638 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2639 {
2640 	struct ptrace_lwpinfo pl;
2641 	pid_t fpid, wpid;
2642 	int status;
2643 
2644 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2645 
2646 	ATF_REQUIRE((fpid = fork()) != -1);
2647 	if (fpid == 0) {
2648 		trace_me();
2649 		getpid();
2650 		exit(1);
2651 	}
2652 
2653 	/* The first wait() should report the stop from SIGSTOP. */
2654 	wpid = waitpid(fpid, &status, 0);
2655 	ATF_REQUIRE(wpid == fpid);
2656 	ATF_REQUIRE(WIFSTOPPED(status));
2657 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2658 
2659 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2660 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2661 
2662 	/* The second wait() should report a system call entry for getpid(). */
2663 	wpid = waitpid(fpid, &status, 0);
2664 	ATF_REQUIRE(wpid == fpid);
2665 	ATF_REQUIRE(WIFSTOPPED(status));
2666 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2667 
2668 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2669 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2670 
2671 	/* Continue with the first SIGUSR1. */
2672 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2673 
2674 	/* The next wait() should report a system call exit for getpid(). */
2675 	wpid = waitpid(fpid, &status, 0);
2676 	ATF_REQUIRE(wpid == fpid);
2677 	ATF_REQUIRE(WIFSTOPPED(status));
2678 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2679 
2680 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2681 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2682 
2683 	/* Send an ABRT without ptrace. */
2684 	ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2685 
2686 	/* Continue normally. */
2687 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2688 
2689 	/* The next wait() should report the SIGABRT. */
2690 	wpid = waitpid(fpid, &status, 0);
2691 	ATF_REQUIRE(wpid == fpid);
2692 	ATF_REQUIRE(WIFSTOPPED(status));
2693 	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2694 
2695 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2696 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2697 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2698 
2699 	/* Continue, replacing the SIGABRT with another SIGUSR1. */
2700 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2701 
2702 	for (;;) {
2703 		/*
2704 		 * The last wait() should report exit 2, i.e., a normal _exit
2705 		 * from the signal handler. In the meantime, catch and proceed
2706 		 * past any syscall stops.
2707 		 */
2708 		wpid = waitpid(fpid, &status, 0);
2709 		ATF_REQUIRE(wpid == fpid);
2710 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2711 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2712 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2713 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2714 		} else {
2715 			ATF_REQUIRE(WIFEXITED(status));
2716 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2717 			break;
2718 		}
2719 	}
2720 
2721 	wpid = wait(&status);
2722 	ATF_REQUIRE(wpid == -1);
2723 	ATF_REQUIRE(errno == ECHILD);
2724 
2725 }
2726 
2727 /*
2728  * Verify a signal delivered by ptrace is noticed by kevent(2).
2729  */
2730 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2731 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2732 {
2733 	pid_t fpid, wpid;
2734 	int status, kq, nevents;
2735 	struct kevent kev;
2736 
2737 	ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2738 
2739 	ATF_REQUIRE((fpid = fork()) != -1);
2740 	if (fpid == 0) {
2741 		CHILD_REQUIRE((kq = kqueue()) > 0);
2742 		EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2743 		CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2744 
2745 		trace_me();
2746 
2747 		for (;;) {
2748 			nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2749 			if (nevents == -1 && errno == EINTR)
2750 				continue;
2751 			CHILD_REQUIRE(nevents > 0);
2752 			CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2753 			CHILD_REQUIRE(kev.ident == SIGUSR1);
2754 			break;
2755 		}
2756 
2757 		exit(1);
2758 	}
2759 
2760 	/* The first wait() should report the stop from SIGSTOP. */
2761 	wpid = waitpid(fpid, &status, 0);
2762 	ATF_REQUIRE(wpid == fpid);
2763 	ATF_REQUIRE(WIFSTOPPED(status));
2764 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2765 
2766 	/* Continue with the SIGUSR1. */
2767 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2768 
2769 	/*
2770 	 * The last wait() should report normal exit with code 1.
2771 	 */
2772 	wpid = waitpid(fpid, &status, 0);
2773 	ATF_REQUIRE(wpid == fpid);
2774 	ATF_REQUIRE(WIFEXITED(status));
2775 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2776 
2777 	wpid = wait(&status);
2778 	ATF_REQUIRE(wpid == -1);
2779 	ATF_REQUIRE(errno == ECHILD);
2780 }
2781 
2782 static void *
2783 signal_thread(void *arg)
2784 {
2785 	int err;
2786 	sigset_t sigmask;
2787 
2788 	pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2789 
2790 	/* Wait for this thread to receive a SIGUSR1. */
2791 	do {
2792 		err = sem_wait(&sigusr1_sem);
2793 		CHILD_REQUIRE(err == 0 || errno == EINTR);
2794 	} while (err != 0 && errno == EINTR);
2795 
2796 	/* Free our companion thread from the barrier. */
2797 	pthread_barrier_wait(pbarrier);
2798 
2799 	/*
2800 	 * Swap ignore duties; the next SIGUSR1 should go to the
2801 	 * other thread.
2802 	 */
2803 	CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2804 	CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2805 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2806 
2807 	/* Sync up threads after swapping signal masks. */
2808 	pthread_barrier_wait(pbarrier);
2809 
2810 	/* Wait until our companion has received its SIGUSR1. */
2811 	pthread_barrier_wait(pbarrier);
2812 
2813 	return (NULL);
2814 }
2815 
2816 /*
2817  * Verify that a traced process with blocked signal received the
2818  * signal from kill() once unmasked.
2819  */
2820 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2821 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2822 {
2823 	struct ptrace_lwpinfo pl;
2824 	pid_t fpid, wpid;
2825 	int status, err;
2826 	sigset_t sigmask;
2827 
2828 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2829 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2830 	got_usr1 = 0;
2831 
2832 	ATF_REQUIRE((fpid = fork()) != -1);
2833 	if (fpid == 0) {
2834 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2835 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2836 		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2837 
2838 		trace_me();
2839 		CHILD_REQUIRE(got_usr1 == 0);
2840 
2841 		/* Allow the pending SIGUSR1 in now. */
2842 		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2843 		/* Wait to receive a SIGUSR1. */
2844 		do {
2845 			err = sem_wait(&sigusr1_sem);
2846 			CHILD_REQUIRE(err == 0 || errno == EINTR);
2847 		} while (err != 0 && errno == EINTR);
2848 		CHILD_REQUIRE(got_usr1 == 1);
2849 		exit(1);
2850 	}
2851 
2852 	/* The first wait() should report the stop from SIGSTOP. */
2853 	wpid = waitpid(fpid, &status, 0);
2854 	ATF_REQUIRE(wpid == fpid);
2855 	ATF_REQUIRE(WIFSTOPPED(status));
2856 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2857 	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2858 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2859 
2860 	/* Send blocked SIGUSR1 which should cause a stop. */
2861 	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2862 
2863 	/* Continue the child ignoring the SIGSTOP. */
2864 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2865 
2866 	/* The next wait() should report the kill(SIGUSR1) was received. */
2867 	wpid = waitpid(fpid, &status, 0);
2868 	ATF_REQUIRE(wpid == fpid);
2869 	ATF_REQUIRE(WIFSTOPPED(status));
2870 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2871 	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2872 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2873 
2874 	/* Continue the child, allowing in the SIGUSR1. */
2875 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2876 
2877 	/* The last wait() should report normal exit with code 1. */
2878 	wpid = waitpid(fpid, &status, 0);
2879 	ATF_REQUIRE(wpid == fpid);
2880 	ATF_REQUIRE(WIFEXITED(status));
2881 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2882 
2883 	wpid = wait(&status);
2884 	ATF_REQUIRE(wpid == -1);
2885 	ATF_REQUIRE(errno == ECHILD);
2886 }
2887 
2888 /*
2889  * Verify that a traced process with blocked signal received the
2890  * signal from PT_CONTINUE once unmasked.
2891  */
2892 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
2893 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
2894 {
2895 	struct ptrace_lwpinfo pl;
2896 	pid_t fpid, wpid;
2897 	int status, err;
2898 	sigset_t sigmask;
2899 
2900 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2901 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2902 	got_usr1 = 0;
2903 
2904 	ATF_REQUIRE((fpid = fork()) != -1);
2905 	if (fpid == 0) {
2906 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2907 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2908 		CHILD_REQUIRE(sigprocmask(SIG_BLOCK, &sigmask, NULL) == 0);
2909 
2910 		trace_me();
2911 		CHILD_REQUIRE(got_usr1 == 0);
2912 
2913 		/* Allow the pending SIGUSR1 in now. */
2914 		CHILD_REQUIRE(sigprocmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2915 		/* Wait to receive a SIGUSR1. */
2916 		do {
2917 			err = sem_wait(&sigusr1_sem);
2918 			CHILD_REQUIRE(err == 0 || errno == EINTR);
2919 		} while (err != 0 && errno == EINTR);
2920 
2921 		CHILD_REQUIRE(got_usr1 == 1);
2922 		exit(1);
2923 	}
2924 
2925 	/* The first wait() should report the stop from SIGSTOP. */
2926 	wpid = waitpid(fpid, &status, 0);
2927 	ATF_REQUIRE(wpid == fpid);
2928 	ATF_REQUIRE(WIFSTOPPED(status));
2929 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2930 	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2931 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGSTOP);
2932 
2933 	/* Continue the child replacing SIGSTOP with SIGUSR1. */
2934 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2935 
2936 	/* The next wait() should report the SIGUSR1 was received. */
2937 	wpid = waitpid(fpid, &status, 0);
2938 	ATF_REQUIRE(wpid == fpid);
2939 	ATF_REQUIRE(WIFSTOPPED(status));
2940 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR1);
2941 	ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2942 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGUSR1);
2943 
2944 	/* Continue the child, ignoring the SIGUSR1. */
2945 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2946 
2947 	/* The last wait() should report normal exit with code 1. */
2948 	wpid = waitpid(fpid, &status, 0);
2949 	ATF_REQUIRE(wpid == fpid);
2950 	ATF_REQUIRE(WIFEXITED(status));
2951 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2952 
2953 	wpid = wait(&status);
2954 	ATF_REQUIRE(wpid == -1);
2955 	ATF_REQUIRE(errno == ECHILD);
2956 }
2957 
2958 /*
2959  * Verify that if ptrace stops due to a signal but continues with
2960  * a different signal that the new signal is routed to a thread
2961  * that can accept it, and that the thread is awakened by the signal
2962  * in a timely manner.
2963  */
2964 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2965 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2966 {
2967 	pid_t fpid, wpid;
2968 	int status, err;
2969 	pthread_t t;
2970 	sigset_t sigmask;
2971 	pthread_barrier_t barrier;
2972 
2973 	ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2974 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2975 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2976 
2977 	ATF_REQUIRE((fpid = fork()) != -1);
2978 	if (fpid == 0) {
2979 		CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2980 
2981 		/* The other thread should receive the first SIGUSR1. */
2982 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2983 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2984 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2985 
2986 		trace_me();
2987 
2988 		/* Wait until other thread has received its SIGUSR1. */
2989 		pthread_barrier_wait(&barrier);
2990 
2991 		/*
2992 		 * Swap ignore duties; the next SIGUSR1 should go to this
2993 		 * thread.
2994 		 */
2995 		CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2996 
2997 		/* Sync up threads after swapping signal masks. */
2998 		pthread_barrier_wait(&barrier);
2999 
3000 		/*
3001 		 * Sync up with test code; we're ready for the next SIGUSR1
3002 		 * now.
3003 		 */
3004 		raise(SIGSTOP);
3005 
3006 		/* Wait for this thread to receive a SIGUSR1. */
3007 		do {
3008 			err = sem_wait(&sigusr1_sem);
3009 			CHILD_REQUIRE(err == 0 || errno == EINTR);
3010 		} while (err != 0 && errno == EINTR);
3011 
3012 		/* Free the other thread from the barrier. */
3013 		pthread_barrier_wait(&barrier);
3014 
3015 		CHILD_REQUIRE(pthread_join(t, NULL) == 0);
3016 
3017 		exit(1);
3018 	}
3019 
3020 	/* The first wait() should report the stop from SIGSTOP. */
3021 	wpid = waitpid(fpid, &status, 0);
3022 	ATF_REQUIRE(wpid == fpid);
3023 	ATF_REQUIRE(WIFSTOPPED(status));
3024 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3025 
3026 	/* Continue the child ignoring the SIGSTOP. */
3027 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3028 
3029 	/*
3030 	 * Send a signal without ptrace that either thread will accept (USR2,
3031 	 * in this case).
3032 	 */
3033 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3034 
3035 	/* The second wait() should report a SIGUSR2 was received. */
3036 	wpid = waitpid(fpid, &status, 0);
3037 	ATF_REQUIRE(wpid == fpid);
3038 	ATF_REQUIRE(WIFSTOPPED(status));
3039 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3040 
3041 	/* Continue the child, changing the signal to USR1. */
3042 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3043 
3044 	/* The next wait() should report the stop from SIGSTOP. */
3045 	wpid = waitpid(fpid, &status, 0);
3046 	ATF_REQUIRE(wpid == fpid);
3047 	ATF_REQUIRE(WIFSTOPPED(status));
3048 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3049 
3050 	/* Continue the child ignoring the SIGSTOP. */
3051 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3052 
3053 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
3054 
3055 	/* The next wait() should report a SIGUSR2 was received. */
3056 	wpid = waitpid(fpid, &status, 0);
3057 	ATF_REQUIRE(wpid == fpid);
3058 	ATF_REQUIRE(WIFSTOPPED(status));
3059 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
3060 
3061 	/* Continue the child, changing the signal to USR1. */
3062 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
3063 
3064 	/* The last wait() should report normal exit with code 1. */
3065 	wpid = waitpid(fpid, &status, 0);
3066 	ATF_REQUIRE(wpid == fpid);
3067 	ATF_REQUIRE(WIFEXITED(status));
3068 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3069 
3070 	wpid = wait(&status);
3071 	ATF_REQUIRE(wpid == -1);
3072 	ATF_REQUIRE(errno == ECHILD);
3073 }
3074 
3075 static void *
3076 raise_sigstop_thread(void *arg __unused)
3077 {
3078 
3079 	raise(SIGSTOP);
3080 	return NULL;
3081 }
3082 
3083 static void *
3084 sleep_thread(void *arg __unused)
3085 {
3086 
3087 	sleep(60);
3088 	return NULL;
3089 }
3090 
3091 static void
3092 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3093 {
3094 	pid_t fpid, wpid;
3095 	int status, i;
3096 	cpuset_t setmask;
3097 	cpusetid_t setid;
3098 	pthread_t t;
3099 
3100 	/*
3101 	 * Become the reaper for this process tree. We need to be able to check
3102 	 * that both child and grandchild have died.
3103 	 */
3104 	ATF_REQUIRE(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL) == 0);
3105 
3106 	fpid = fork();
3107 	ATF_REQUIRE(fpid >= 0);
3108 	if (fpid == 0) {
3109 		fpid = fork();
3110 		CHILD_REQUIRE(fpid >= 0);
3111 		if (fpid == 0) {
3112 			trace_me();
3113 
3114 			/* Pin to CPU 0 to serialize thread execution. */
3115 			CPU_ZERO(&setmask);
3116 			CPU_SET(0, &setmask);
3117 			CHILD_REQUIRE(cpuset(&setid) == 0);
3118 			CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3119 			    CPU_WHICH_CPUSET, setid,
3120 			    sizeof(setmask), &setmask) == 0);
3121 
3122 			if (sigstop_from_main_thread) {
3123 				/*
3124 				 * We expect the SIGKILL sent when our parent
3125 				 * dies to be delivered to the new thread.
3126 				 * Raise the SIGSTOP in this thread so the
3127 				 * threads compete.
3128 				 */
3129 				CHILD_REQUIRE(pthread_create(&t, NULL,
3130 				    sleep_thread, NULL) == 0);
3131 				raise(SIGSTOP);
3132 			} else {
3133 				/*
3134 				 * We expect the SIGKILL to be delivered to
3135 				 * this thread. After creating the new thread,
3136 				 * just get off the CPU so the other thread can
3137 				 * raise the SIGSTOP.
3138 				 */
3139 				CHILD_REQUIRE(pthread_create(&t, NULL,
3140 				    raise_sigstop_thread, NULL) == 0);
3141 				sleep(60);
3142 			}
3143 
3144 			exit(0);
3145 		}
3146 		/* First stop is trace_me() immediately after fork. */
3147 		wpid = waitpid(fpid, &status, 0);
3148 		CHILD_REQUIRE(wpid == fpid);
3149 		CHILD_REQUIRE(WIFSTOPPED(status));
3150 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3151 
3152 		CHILD_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3153 
3154 		/* Second stop is from the raise(SIGSTOP). */
3155 		wpid = waitpid(fpid, &status, 0);
3156 		CHILD_REQUIRE(wpid == fpid);
3157 		CHILD_REQUIRE(WIFSTOPPED(status));
3158 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3159 
3160 		/*
3161 		 * Terminate tracing process without detaching. Our child
3162 		 * should be killed.
3163 		 */
3164 		exit(0);
3165 	}
3166 
3167 	/*
3168 	 * We should get a normal exit from our immediate child and a SIGKILL
3169 	 * exit from our grandchild. The latter case is the interesting one.
3170 	 * Our grandchild should not have stopped due to the SIGSTOP that was
3171 	 * left dangling when its parent died.
3172 	 */
3173 	for (i = 0; i < 2; ++i) {
3174 		wpid = wait(&status);
3175 		if (wpid == fpid)
3176 			ATF_REQUIRE(WIFEXITED(status) &&
3177 			    WEXITSTATUS(status) == 0);
3178 		else
3179 			ATF_REQUIRE(WIFSIGNALED(status) &&
3180 			    WTERMSIG(status) == SIGKILL);
3181 	}
3182 }
3183 
3184 /*
3185  * These two tests ensure that if the tracing process exits without detaching
3186  * just after the child received a SIGSTOP, the child is cleanly killed and
3187  * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3188  * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3189  * different threads, the SIGKILL must win.  There are two variants of this
3190  * test, designed to catch the case where the SIGKILL is delivered to the
3191  * younger thread (the first test) and the case where the SIGKILL is delivered
3192  * to the older thread (the second test). This behavior has changed in the
3193  * past, so make no assumption.
3194  */
3195 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3196 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3197 {
3198 
3199 	atf_tc_set_md_var(tc, "require.user", "root");
3200 }
3201 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3202 {
3203 
3204 	terminate_with_pending_sigstop(true);
3205 }
3206 
3207 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3208 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3209 {
3210 
3211 	atf_tc_set_md_var(tc, "require.user", "root");
3212 }
3213 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3214 {
3215 
3216 	terminate_with_pending_sigstop(false);
3217 }
3218 
3219 /*
3220  * Verify that after ptrace() discards a SIGKILL signal, the event mask
3221  * is not modified.
3222  */
3223 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3224 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3225 {
3226 	struct ptrace_lwpinfo pl;
3227 	pid_t fpid, wpid;
3228 	int status, event_mask, new_event_mask;
3229 
3230 	ATF_REQUIRE((fpid = fork()) != -1);
3231 	if (fpid == 0) {
3232 		trace_me();
3233 		raise(SIGSTOP);
3234 		exit(0);
3235 	}
3236 
3237 	/* The first wait() should report the stop from trace_me(). */
3238 	wpid = waitpid(fpid, &status, 0);
3239 	ATF_REQUIRE(wpid == fpid);
3240 	ATF_REQUIRE(WIFSTOPPED(status));
3241 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3242 
3243 	/* Set several unobtrusive event bits. */
3244 	event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3245 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3246 	    sizeof(event_mask)) == 0);
3247 
3248 	/* Send a SIGKILL without using ptrace. */
3249 	ATF_REQUIRE(kill(fpid, SIGKILL) == 0);
3250 
3251 	/* Continue the child ignoring the SIGSTOP. */
3252 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3253 
3254 	/* The next stop should be due to the SIGKILL. */
3255 	wpid = waitpid(fpid, &status, 0);
3256 	ATF_REQUIRE(wpid == fpid);
3257 	ATF_REQUIRE(WIFSTOPPED(status));
3258 	ATF_REQUIRE(WSTOPSIG(status) == SIGKILL);
3259 
3260 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3261 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3262 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGKILL);
3263 
3264 	/* Continue the child ignoring the SIGKILL. */
3265 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3266 
3267 	/* The next wait() should report the stop from SIGSTOP. */
3268 	wpid = waitpid(fpid, &status, 0);
3269 	ATF_REQUIRE(wpid == fpid);
3270 	ATF_REQUIRE(WIFSTOPPED(status));
3271 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3272 
3273 	/* Check the current event mask. It should not have changed. */
3274 	new_event_mask = 0;
3275 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3276 	    sizeof(new_event_mask)) == 0);
3277 	ATF_REQUIRE(event_mask == new_event_mask);
3278 
3279 	/* Continue the child to let it exit. */
3280 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3281 
3282 	/* The last event should be for the child process's exit. */
3283 	wpid = waitpid(fpid, &status, 0);
3284 	ATF_REQUIRE(WIFEXITED(status));
3285 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
3286 
3287 	wpid = wait(&status);
3288 	ATF_REQUIRE(wpid == -1);
3289 	ATF_REQUIRE(errno == ECHILD);
3290 }
3291 
3292 static void *
3293 flock_thread(void *arg)
3294 {
3295 	int fd;
3296 
3297 	fd = *(int *)arg;
3298 	(void)flock(fd, LOCK_EX);
3299 	(void)flock(fd, LOCK_UN);
3300 	return (NULL);
3301 }
3302 
3303 /*
3304  * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3305  * We rely on the fact that the lockf implementation sets SBDRY before blocking
3306  * on a lock. This is a regression test for r318191.
3307  */
3308 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3309 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3310 {
3311 	pthread_barrier_t barrier;
3312 	pthread_barrierattr_t battr;
3313 	char tmpfile[64];
3314 	pid_t child, wpid;
3315 	int error, fd, i, status;
3316 
3317 	ATF_REQUIRE(pthread_barrierattr_init(&battr) == 0);
3318 	ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3319 	    PTHREAD_PROCESS_SHARED) == 0);
3320 	ATF_REQUIRE(pthread_barrier_init(&barrier, &battr, 2) == 0);
3321 
3322 	(void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3323 	fd = mkstemp(tmpfile);
3324 	ATF_REQUIRE(fd >= 0);
3325 
3326 	ATF_REQUIRE((child = fork()) != -1);
3327 	if (child == 0) {
3328 		pthread_t t[2];
3329 		int cfd;
3330 
3331 		error = pthread_barrier_wait(&barrier);
3332 		if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3333 			_exit(1);
3334 
3335 		cfd = open(tmpfile, O_RDONLY);
3336 		if (cfd < 0)
3337 			_exit(1);
3338 
3339 		/*
3340 		 * We want at least two threads blocked on the file lock since
3341 		 * the SIGSTOP from PT_ATTACH may kick one of them out of
3342 		 * sleep.
3343 		 */
3344 		if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3345 			_exit(1);
3346 		if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3347 			_exit(1);
3348 		if (pthread_join(t[0], NULL) != 0)
3349 			_exit(1);
3350 		if (pthread_join(t[1], NULL) != 0)
3351 			_exit(1);
3352 		_exit(0);
3353 	}
3354 
3355 	ATF_REQUIRE(flock(fd, LOCK_EX) == 0);
3356 
3357 	error = pthread_barrier_wait(&barrier);
3358 	ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3359 
3360 	/*
3361 	 * Give the child some time to block. Is there a better way to do this?
3362 	 */
3363 	sleep(1);
3364 
3365 	/*
3366 	 * Attach and give the child 3 seconds to stop.
3367 	 */
3368 	ATF_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) == 0);
3369 	for (i = 0; i < 3; i++) {
3370 		wpid = waitpid(child, &status, WNOHANG);
3371 		if (wpid == child && WIFSTOPPED(status) &&
3372 		    WSTOPSIG(status) == SIGSTOP)
3373 			break;
3374 		sleep(1);
3375 	}
3376 	ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3377 
3378 	ATF_REQUIRE(ptrace(PT_DETACH, child, NULL, 0) == 0);
3379 
3380 	ATF_REQUIRE(flock(fd, LOCK_UN) == 0);
3381 	ATF_REQUIRE(unlink(tmpfile) == 0);
3382 	ATF_REQUIRE(close(fd) == 0);
3383 }
3384 
3385 static void
3386 sigusr1_step_handler(int sig)
3387 {
3388 
3389 	CHILD_REQUIRE(sig == SIGUSR1);
3390 	raise(SIGABRT);
3391 }
3392 
3393 /*
3394  * Verify that PT_STEP with a signal invokes the signal before
3395  * stepping the next instruction (and that the next instruction is
3396  * stepped correctly).
3397  */
3398 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3399 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3400 {
3401 	struct ptrace_lwpinfo pl;
3402 	pid_t fpid, wpid;
3403 	int status;
3404 
3405 	ATF_REQUIRE((fpid = fork()) != -1);
3406 	if (fpid == 0) {
3407 		trace_me();
3408 		signal(SIGUSR1, sigusr1_step_handler);
3409 		raise(SIGABRT);
3410 		exit(1);
3411 	}
3412 
3413 	/* The first wait() should report the stop from SIGSTOP. */
3414 	wpid = waitpid(fpid, &status, 0);
3415 	ATF_REQUIRE(wpid == fpid);
3416 	ATF_REQUIRE(WIFSTOPPED(status));
3417 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3418 
3419 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3420 
3421 	/* The next stop should report the SIGABRT in the child body. */
3422 	wpid = waitpid(fpid, &status, 0);
3423 	ATF_REQUIRE(wpid == fpid);
3424 	ATF_REQUIRE(WIFSTOPPED(status));
3425 	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3426 
3427 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3428 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3429 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3430 
3431 	/* Step the child process inserting SIGUSR1. */
3432 	ATF_REQUIRE(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1) == 0);
3433 
3434 	/* The next stop should report the SIGABRT in the signal handler. */
3435 	wpid = waitpid(fpid, &status, 0);
3436 	ATF_REQUIRE(wpid == fpid);
3437 	ATF_REQUIRE(WIFSTOPPED(status));
3438 	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
3439 
3440 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3441 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3442 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
3443 
3444 	/* Continue the child process discarding the signal. */
3445 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3446 
3447 	/* The next stop should report a trace trap from PT_STEP. */
3448 	wpid = waitpid(fpid, &status, 0);
3449 	ATF_REQUIRE(wpid == fpid);
3450 	ATF_REQUIRE(WIFSTOPPED(status));
3451 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3452 
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 == SIGTRAP);
3456 	ATF_REQUIRE(pl.pl_siginfo.si_code == TRAP_TRACE);
3457 
3458 	/* Continue the child to let it exit. */
3459 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3460 
3461 	/* The last event should be for the child process's exit. */
3462 	wpid = waitpid(fpid, &status, 0);
3463 	ATF_REQUIRE(WIFEXITED(status));
3464 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3465 
3466 	wpid = wait(&status);
3467 	ATF_REQUIRE(wpid == -1);
3468 	ATF_REQUIRE(errno == ECHILD);
3469 }
3470 
3471 #if defined(__amd64__) || defined(__i386__)
3472 /*
3473  * Only x86 both define breakpoint() and have a PC after breakpoint so
3474  * that restarting doesn't retrigger the breakpoint.
3475  */
3476 static void *
3477 continue_thread(void *arg)
3478 {
3479 	breakpoint();
3480 	return (NULL);
3481 }
3482 
3483 static __dead2 void
3484 continue_thread_main(void)
3485 {
3486 	pthread_t threads[2];
3487 
3488 	CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3489 	    NULL) == 0);
3490 	CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3491 	    NULL) == 0);
3492 	CHILD_REQUIRE(pthread_join(threads[0], NULL) == 0);
3493 	CHILD_REQUIRE(pthread_join(threads[1], NULL) == 0);
3494 	exit(1);
3495 }
3496 
3497 /*
3498  * Ensure that PT_CONTINUE clears the status of the thread that
3499  * triggered the stop even if a different thread's LWP was passed to
3500  * PT_CONTINUE.
3501  */
3502 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3503 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3504 {
3505 	struct ptrace_lwpinfo pl;
3506 	pid_t fpid, wpid;
3507 	lwpid_t lwps[2];
3508 	bool hit_break[2];
3509 	int i, j, status;
3510 
3511 	ATF_REQUIRE((fpid = fork()) != -1);
3512 	if (fpid == 0) {
3513 		trace_me();
3514 		continue_thread_main();
3515 	}
3516 
3517 	/* The first wait() should report the stop from SIGSTOP. */
3518 	wpid = waitpid(fpid, &status, 0);
3519 	ATF_REQUIRE(wpid == fpid);
3520 	ATF_REQUIRE(WIFSTOPPED(status));
3521 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
3522 
3523 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3524 	    sizeof(pl)) != -1);
3525 
3526 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
3527 
3528 	/* Continue the child ignoring the SIGSTOP. */
3529 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3530 
3531 	/* One of the new threads should report it's birth. */
3532 	wpid = waitpid(fpid, &status, 0);
3533 	ATF_REQUIRE(wpid == fpid);
3534 	ATF_REQUIRE(WIFSTOPPED(status));
3535 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3536 
3537 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3538 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3539 	    (PL_FLAG_BORN | PL_FLAG_SCX));
3540 	lwps[0] = pl.pl_lwpid;
3541 
3542 	/*
3543 	 * Suspend this thread to ensure both threads are alive before
3544 	 * hitting the breakpoint.
3545 	 */
3546 	ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3547 
3548 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3549 
3550 	/* Second thread should report it's birth. */
3551 	wpid = waitpid(fpid, &status, 0);
3552 	ATF_REQUIRE(wpid == fpid);
3553 	ATF_REQUIRE(WIFSTOPPED(status));
3554 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3555 
3556 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3557 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
3558 	    (PL_FLAG_BORN | PL_FLAG_SCX));
3559 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3560 	lwps[1] = pl.pl_lwpid;
3561 
3562 	/* Resume both threads waiting for breakpoint events. */
3563 	hit_break[0] = hit_break[1] = false;
3564 	ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3565 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3566 
3567 	/* One thread should report a breakpoint. */
3568 	wpid = waitpid(fpid, &status, 0);
3569 	ATF_REQUIRE(wpid == fpid);
3570 	ATF_REQUIRE(WIFSTOPPED(status));
3571 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3572 
3573 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3574 	ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3575 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3576 	    pl.pl_siginfo.si_code == TRAP_BRKPT);
3577 	if (pl.pl_lwpid == lwps[0])
3578 		i = 0;
3579 	else
3580 		i = 1;
3581 	hit_break[i] = true;
3582 
3583 	/*
3584 	 * Resume both threads but pass the other thread's LWPID to
3585 	 * PT_CONTINUE.
3586 	 */
3587 	ATF_REQUIRE(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0) == 0);
3588 
3589 	/*
3590 	 * Will now get two thread exit events and one more breakpoint
3591 	 * event.
3592 	 */
3593 	for (j = 0; j < 3; j++) {
3594 		wpid = waitpid(fpid, &status, 0);
3595 		ATF_REQUIRE(wpid == fpid);
3596 		ATF_REQUIRE(WIFSTOPPED(status));
3597 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
3598 
3599 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3600 		    sizeof(pl)) != -1);
3601 
3602 		if (pl.pl_lwpid == lwps[0])
3603 			i = 0;
3604 		else
3605 			i = 1;
3606 
3607 		ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3608 		if (pl.pl_flags & PL_FLAG_EXITED) {
3609 			ATF_REQUIRE_MSG(hit_break[i],
3610 			    "exited thread did not report breakpoint");
3611 			lwps[i] = 0;
3612 		} else {
3613 			ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3614 			ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGTRAP &&
3615 			    pl.pl_siginfo.si_code == TRAP_BRKPT);
3616 			ATF_REQUIRE_MSG(!hit_break[i],
3617 			    "double breakpoint event");
3618 			hit_break[i] = true;
3619 		}
3620 
3621 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
3622 	}
3623 
3624 	/* Both threads should have exited. */
3625 	ATF_REQUIRE(lwps[0] == 0);
3626 	ATF_REQUIRE(lwps[1] == 0);
3627 
3628 	/* The last event should be for the child process's exit. */
3629 	wpid = waitpid(fpid, &status, 0);
3630 	ATF_REQUIRE(WIFEXITED(status));
3631 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
3632 
3633 	wpid = wait(&status);
3634 	ATF_REQUIRE(wpid == -1);
3635 	ATF_REQUIRE(errno == ECHILD);
3636 }
3637 #endif
3638 
3639 ATF_TP_ADD_TCS(tp)
3640 {
3641 
3642 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
3643 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
3644 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
3645 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
3646 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
3647 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
3648 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
3649 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
3650 	ATF_TP_ADD_TC(tp,
3651 	    ptrace__follow_fork_child_detached_unrelated_debugger);
3652 	ATF_TP_ADD_TC(tp,
3653 	    ptrace__follow_fork_parent_detached_unrelated_debugger);
3654 	ATF_TP_ADD_TC(tp, ptrace__getppid);
3655 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
3656 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
3657 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
3658 	ATF_TP_ADD_TC(tp, ptrace__lwp_events);
3659 	ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
3660 	ATF_TP_ADD_TC(tp, ptrace__siginfo);
3661 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
3662 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
3663 	ATF_TP_ADD_TC(tp, ptrace__event_mask);
3664 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
3665 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
3666 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
3667 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
3668 #endif
3669 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
3670 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
3671 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
3672 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
3673 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
3674 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
3675 	ATF_TP_ADD_TC(tp,
3676 	    ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
3677 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
3678 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
3679 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
3680 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
3681 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
3682 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
3683 	ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
3684 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
3685 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3686 	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
3687 	ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
3688 	ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
3689 	ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
3690 	ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
3691 #if defined(__amd64__) || defined(__i386__)
3692 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
3693 #endif
3694 
3695 	return (atf_no_error());
3696 }
3697