xref: /freebsd/tests/sys/kern/ptrace_test.c (revision af67ca3780733f5961aa33ee9993f62dac9fea78)
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/time.h>
34 #include <sys/ptrace.h>
35 #include <sys/syscall.h>
36 #include <sys/sysctl.h>
37 #include <sys/user.h>
38 #include <sys/wait.h>
39 #include <errno.h>
40 #include <machine/cpufunc.h>
41 #include <pthread.h>
42 #include <semaphore.h>
43 #include <signal.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <unistd.h>
47 #include <atf-c.h>
48 
49 /*
50  * A variant of ATF_REQUIRE that is suitable for use in child
51  * processes.  This only works if the parent process is tripped up by
52  * the early exit and fails some requirement itself.
53  */
54 #define	CHILD_REQUIRE(exp) do {						\
55 		if (!(exp))						\
56 			child_fail_require(__FILE__, __LINE__,		\
57 			    #exp " not met");				\
58 	} while (0)
59 
60 static __dead2 void
61 child_fail_require(const char *file, int line, const char *str)
62 {
63 	char buf[128];
64 
65 	snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
66 	write(2, buf, strlen(buf));
67 	_exit(32);
68 }
69 
70 static void
71 trace_me(void)
72 {
73 
74 	/* Attach the parent process as a tracer of this process. */
75 	CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
76 
77 	/* Trigger a stop. */
78 	raise(SIGSTOP);
79 }
80 
81 static void
82 attach_child(pid_t pid)
83 {
84 	pid_t wpid;
85 	int status;
86 
87 	ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
88 
89 	wpid = waitpid(pid, &status, 0);
90 	ATF_REQUIRE(wpid == pid);
91 	ATF_REQUIRE(WIFSTOPPED(status));
92 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
93 }
94 
95 static void
96 wait_for_zombie(pid_t pid)
97 {
98 
99 	/*
100 	 * Wait for a process to exit.  This is kind of gross, but
101 	 * there is not a better way.
102 	 */
103 	for (;;) {
104 		struct kinfo_proc kp;
105 		size_t len;
106 		int mib[4];
107 
108 		mib[0] = CTL_KERN;
109 		mib[1] = KERN_PROC;
110 		mib[2] = KERN_PROC_PID;
111 		mib[3] = pid;
112 		len = sizeof(kp);
113 		if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
114 			/* The KERN_PROC_PID sysctl fails for zombies. */
115 			ATF_REQUIRE(errno == ESRCH);
116 			break;
117 		}
118 		usleep(5000);
119 	}
120 }
121 
122 /*
123  * Verify that a parent debugger process "sees" the exit of a debugged
124  * process exactly once when attached via PT_TRACE_ME.
125  */
126 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
127 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
128 {
129 	pid_t child, wpid;
130 	int status;
131 
132 	ATF_REQUIRE((child = fork()) != -1);
133 	if (child == 0) {
134 		/* Child process. */
135 		trace_me();
136 
137 		_exit(1);
138 	}
139 
140 	/* Parent process. */
141 
142 	/* The first wait() should report the stop from SIGSTOP. */
143 	wpid = waitpid(child, &status, 0);
144 	ATF_REQUIRE(wpid == child);
145 	ATF_REQUIRE(WIFSTOPPED(status));
146 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
147 
148 	/* Continue the child ignoring the SIGSTOP. */
149 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
150 
151 	/* The second wait() should report the exit status. */
152 	wpid = waitpid(child, &status, 0);
153 	ATF_REQUIRE(wpid == child);
154 	ATF_REQUIRE(WIFEXITED(status));
155 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
156 
157 	/* The child should no longer exist. */
158 	wpid = waitpid(child, &status, 0);
159 	ATF_REQUIRE(wpid == -1);
160 	ATF_REQUIRE(errno == ECHILD);
161 }
162 
163 /*
164  * Verify that a parent debugger process "sees" the exit of a debugged
165  * process exactly once when attached via PT_ATTACH.
166  */
167 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
168 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
169 {
170 	pid_t child, wpid;
171 	int cpipe[2], status;
172 	char c;
173 
174 	ATF_REQUIRE(pipe(cpipe) == 0);
175 	ATF_REQUIRE((child = fork()) != -1);
176 	if (child == 0) {
177 		/* Child process. */
178 		close(cpipe[0]);
179 
180 		/* Wait for the parent to attach. */
181 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
182 
183 		_exit(1);
184 	}
185 	close(cpipe[1]);
186 
187 	/* Parent process. */
188 
189 	/* Attach to the child process. */
190 	attach_child(child);
191 
192 	/* Continue the child ignoring the SIGSTOP. */
193 	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
194 
195 	/* Signal the child to exit. */
196 	close(cpipe[0]);
197 
198 	/* The second wait() should report the exit status. */
199 	wpid = waitpid(child, &status, 0);
200 	ATF_REQUIRE(wpid == child);
201 	ATF_REQUIRE(WIFEXITED(status));
202 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
203 
204 	/* The child should no longer exist. */
205 	wpid = waitpid(child, &status, 0);
206 	ATF_REQUIRE(wpid == -1);
207 	ATF_REQUIRE(errno == ECHILD);
208 }
209 
210 /*
211  * Verify that a parent process "sees" the exit of a debugged process only
212  * after the debugger has seen it.
213  */
214 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
215 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
216 {
217 	pid_t child, debugger, wpid;
218 	int cpipe[2], dpipe[2], status;
219 	char c;
220 
221 	ATF_REQUIRE(pipe(cpipe) == 0);
222 	ATF_REQUIRE((child = fork()) != -1);
223 
224 	if (child == 0) {
225 		/* Child process. */
226 		close(cpipe[0]);
227 
228 		/* Wait for parent to be ready. */
229 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
230 
231 		_exit(1);
232 	}
233 	close(cpipe[1]);
234 
235 	ATF_REQUIRE(pipe(dpipe) == 0);
236 	ATF_REQUIRE((debugger = fork()) != -1);
237 
238 	if (debugger == 0) {
239 		/* Debugger process. */
240 		close(dpipe[0]);
241 
242 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
243 
244 		wpid = waitpid(child, &status, 0);
245 		CHILD_REQUIRE(wpid == child);
246 		CHILD_REQUIRE(WIFSTOPPED(status));
247 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
248 
249 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
250 
251 		/* Signal parent that debugger is attached. */
252 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
253 
254 		/* Wait for parent's failed wait. */
255 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
256 
257 		wpid = waitpid(child, &status, 0);
258 		CHILD_REQUIRE(wpid == child);
259 		CHILD_REQUIRE(WIFEXITED(status));
260 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
261 
262 		_exit(0);
263 	}
264 	close(dpipe[1]);
265 
266 	/* Parent process. */
267 
268 	/* Wait for the debugger to attach to the child. */
269 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
270 
271 	/* Release the child. */
272 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
273 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
274 	close(cpipe[0]);
275 
276 	wait_for_zombie(child);
277 
278 	/*
279 	 * This wait should return a pid of 0 to indicate no status to
280 	 * report.  The parent should see the child as non-exited
281 	 * until the debugger sees the exit.
282 	 */
283 	wpid = waitpid(child, &status, WNOHANG);
284 	ATF_REQUIRE(wpid == 0);
285 
286 	/* Signal the debugger to wait for the child. */
287 	close(dpipe[0]);
288 
289 	/* Wait for the debugger. */
290 	wpid = waitpid(debugger, &status, 0);
291 	ATF_REQUIRE(wpid == debugger);
292 	ATF_REQUIRE(WIFEXITED(status));
293 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
294 
295 	/* The child process should now be ready. */
296 	wpid = waitpid(child, &status, WNOHANG);
297 	ATF_REQUIRE(wpid == child);
298 	ATF_REQUIRE(WIFEXITED(status));
299 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
300 }
301 
302 /*
303  * Verify that a parent process "sees" the exit of a debugged process
304  * only after a non-direct-child debugger has seen it.  In particular,
305  * various wait() calls in the parent must avoid failing with ESRCH by
306  * checking the parent's orphan list for the debugee.
307  */
308 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
309 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
310 {
311 	pid_t child, debugger, fpid, wpid;
312 	int cpipe[2], dpipe[2], status;
313 	char c;
314 
315 	ATF_REQUIRE(pipe(cpipe) == 0);
316 	ATF_REQUIRE((child = fork()) != -1);
317 
318 	if (child == 0) {
319 		/* Child process. */
320 		close(cpipe[0]);
321 
322 		/* Wait for parent to be ready. */
323 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
324 
325 		_exit(1);
326 	}
327 	close(cpipe[1]);
328 
329 	ATF_REQUIRE(pipe(dpipe) == 0);
330 	ATF_REQUIRE((debugger = fork()) != -1);
331 
332 	if (debugger == 0) {
333 		/* Debugger parent. */
334 
335 		/*
336 		 * Fork again and drop the debugger parent so that the
337 		 * debugger is not a child of the main parent.
338 		 */
339 		CHILD_REQUIRE((fpid = fork()) != -1);
340 		if (fpid != 0)
341 			_exit(2);
342 
343 		/* Debugger process. */
344 		close(dpipe[0]);
345 
346 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
347 
348 		wpid = waitpid(child, &status, 0);
349 		CHILD_REQUIRE(wpid == child);
350 		CHILD_REQUIRE(WIFSTOPPED(status));
351 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
352 
353 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
354 
355 		/* Signal parent that debugger is attached. */
356 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
357 
358 		/* Wait for parent's failed wait. */
359 		CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
360 
361 		wpid = waitpid(child, &status, 0);
362 		CHILD_REQUIRE(wpid == child);
363 		CHILD_REQUIRE(WIFEXITED(status));
364 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
365 
366 		_exit(0);
367 	}
368 	close(dpipe[1]);
369 
370 	/* Parent process. */
371 
372 	/* Wait for the debugger parent process to exit. */
373 	wpid = waitpid(debugger, &status, 0);
374 	ATF_REQUIRE(wpid == debugger);
375 	ATF_REQUIRE(WIFEXITED(status));
376 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
377 
378 	/* A WNOHANG wait here should see the non-exited child. */
379 	wpid = waitpid(child, &status, WNOHANG);
380 	ATF_REQUIRE(wpid == 0);
381 
382 	/* Wait for the debugger to attach to the child. */
383 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
384 
385 	/* Release the child. */
386 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
387 	ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
388 	close(cpipe[0]);
389 
390 	wait_for_zombie(child);
391 
392 	/*
393 	 * This wait should return a pid of 0 to indicate no status to
394 	 * report.  The parent should see the child as non-exited
395 	 * until the debugger sees the exit.
396 	 */
397 	wpid = waitpid(child, &status, WNOHANG);
398 	ATF_REQUIRE(wpid == 0);
399 
400 	/* Signal the debugger to wait for the child. */
401 	ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
402 
403 	/* Wait for the debugger. */
404 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
405 	close(dpipe[0]);
406 
407 	/* The child process should now be ready. */
408 	wpid = waitpid(child, &status, WNOHANG);
409 	ATF_REQUIRE(wpid == child);
410 	ATF_REQUIRE(WIFEXITED(status));
411 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
412 }
413 
414 /*
415  * The parent process should always act the same regardless of how the
416  * debugger is attached to it.
417  */
418 static __dead2 void
419 follow_fork_parent(bool use_vfork)
420 {
421 	pid_t fpid, wpid;
422 	int status;
423 
424 	if (use_vfork)
425 		CHILD_REQUIRE((fpid = vfork()) != -1);
426 	else
427 		CHILD_REQUIRE((fpid = fork()) != -1);
428 
429 	if (fpid == 0)
430 		/* Child */
431 		_exit(2);
432 
433 	wpid = waitpid(fpid, &status, 0);
434 	CHILD_REQUIRE(wpid == fpid);
435 	CHILD_REQUIRE(WIFEXITED(status));
436 	CHILD_REQUIRE(WEXITSTATUS(status) == 2);
437 
438 	_exit(1);
439 }
440 
441 /*
442  * Helper routine for follow fork tests.  This waits for two stops
443  * that report both "sides" of a fork.  It returns the pid of the new
444  * child process.
445  */
446 static pid_t
447 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
448 {
449 	struct ptrace_lwpinfo pl;
450 	bool fork_reported[2];
451 	pid_t child, wpid;
452 	int i, status;
453 
454 	fork_reported[0] = false;
455 	fork_reported[1] = false;
456 	child = -1;
457 
458 	/*
459 	 * Each process should report a fork event.  The parent should
460 	 * report a PL_FLAG_FORKED event, and the child should report
461 	 * a PL_FLAG_CHILD event.
462 	 */
463 	for (i = 0; i < 2; i++) {
464 		wpid = wait(&status);
465 		ATF_REQUIRE(wpid > 0);
466 		ATF_REQUIRE(WIFSTOPPED(status));
467 
468 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
469 		    sizeof(pl)) != -1);
470 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
471 		    0);
472 		ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
473 		    (PL_FLAG_FORKED | PL_FLAG_CHILD));
474 		if (pl.pl_flags & PL_FLAG_CHILD) {
475 			ATF_REQUIRE(wpid != parent);
476 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
477 			ATF_REQUIRE(!fork_reported[1]);
478 			if (child == -1)
479 				child = wpid;
480 			else
481 				ATF_REQUIRE(child == wpid);
482 			if (ppl != NULL)
483 				ppl[1] = pl;
484 			fork_reported[1] = true;
485 		} else {
486 			ATF_REQUIRE(wpid == parent);
487 			ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
488 			ATF_REQUIRE(!fork_reported[0]);
489 			if (child == -1)
490 				child = pl.pl_child_pid;
491 			else
492 				ATF_REQUIRE(child == pl.pl_child_pid);
493 			if (ppl != NULL)
494 				ppl[0] = pl;
495 			fork_reported[0] = true;
496 		}
497 	}
498 
499 	return (child);
500 }
501 
502 /*
503  * Verify that a new child process is stopped after a followed fork and
504  * that the traced parent sees the exit of the child after the debugger
505  * when both processes remain attached to the debugger.
506  */
507 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
508 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
509 {
510 	pid_t children[2], fpid, wpid;
511 	int status;
512 
513 	ATF_REQUIRE((fpid = fork()) != -1);
514 	if (fpid == 0) {
515 		trace_me();
516 		follow_fork_parent(false);
517 	}
518 
519 	/* Parent process. */
520 	children[0] = fpid;
521 
522 	/* The first wait() should report the stop from SIGSTOP. */
523 	wpid = waitpid(children[0], &status, 0);
524 	ATF_REQUIRE(wpid == children[0]);
525 	ATF_REQUIRE(WIFSTOPPED(status));
526 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
527 
528 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
529 
530 	/* Continue the child ignoring the SIGSTOP. */
531 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
532 
533 	children[1] = handle_fork_events(children[0], NULL);
534 	ATF_REQUIRE(children[1] > 0);
535 
536 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
537 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
538 
539 	/*
540 	 * The child can't exit until the grandchild reports status, so the
541 	 * grandchild should report its exit first to the debugger.
542 	 */
543 	wpid = wait(&status);
544 	ATF_REQUIRE(wpid == children[1]);
545 	ATF_REQUIRE(WIFEXITED(status));
546 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
547 
548 	wpid = wait(&status);
549 	ATF_REQUIRE(wpid == children[0]);
550 	ATF_REQUIRE(WIFEXITED(status));
551 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
552 
553 	wpid = wait(&status);
554 	ATF_REQUIRE(wpid == -1);
555 	ATF_REQUIRE(errno == ECHILD);
556 }
557 
558 /*
559  * Verify that a new child process is stopped after a followed fork
560  * and that the traced parent sees the exit of the child when the new
561  * child process is detached after it reports its fork.
562  */
563 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
564 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
565 {
566 	pid_t children[2], fpid, wpid;
567 	int status;
568 
569 	ATF_REQUIRE((fpid = fork()) != -1);
570 	if (fpid == 0) {
571 		trace_me();
572 		follow_fork_parent(false);
573 	}
574 
575 	/* Parent process. */
576 	children[0] = fpid;
577 
578 	/* The first wait() should report the stop from SIGSTOP. */
579 	wpid = waitpid(children[0], &status, 0);
580 	ATF_REQUIRE(wpid == children[0]);
581 	ATF_REQUIRE(WIFSTOPPED(status));
582 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
583 
584 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
585 
586 	/* Continue the child ignoring the SIGSTOP. */
587 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
588 
589 	children[1] = handle_fork_events(children[0], NULL);
590 	ATF_REQUIRE(children[1] > 0);
591 
592 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
593 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
594 
595 	/*
596 	 * Should not see any status from the grandchild now, only the
597 	 * child.
598 	 */
599 	wpid = wait(&status);
600 	ATF_REQUIRE(wpid == children[0]);
601 	ATF_REQUIRE(WIFEXITED(status));
602 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
603 
604 	wpid = wait(&status);
605 	ATF_REQUIRE(wpid == -1);
606 	ATF_REQUIRE(errno == ECHILD);
607 }
608 
609 /*
610  * Verify that a new child process is stopped after a followed fork
611  * and that the traced parent sees the exit of the child when the
612  * traced parent is detached after the fork.
613  */
614 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
615 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
616 {
617 	pid_t children[2], fpid, wpid;
618 	int status;
619 
620 	ATF_REQUIRE((fpid = fork()) != -1);
621 	if (fpid == 0) {
622 		trace_me();
623 		follow_fork_parent(false);
624 	}
625 
626 	/* Parent process. */
627 	children[0] = fpid;
628 
629 	/* The first wait() should report the stop from SIGSTOP. */
630 	wpid = waitpid(children[0], &status, 0);
631 	ATF_REQUIRE(wpid == children[0]);
632 	ATF_REQUIRE(WIFSTOPPED(status));
633 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
634 
635 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
636 
637 	/* Continue the child ignoring the SIGSTOP. */
638 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
639 
640 	children[1] = handle_fork_events(children[0], NULL);
641 	ATF_REQUIRE(children[1] > 0);
642 
643 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
644 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
645 
646 	/*
647 	 * The child can't exit until the grandchild reports status, so the
648 	 * grandchild should report its exit first to the debugger.
649 	 *
650 	 * Even though the child process is detached, it is still a
651 	 * child of the debugger, so it will still report it's exit
652 	 * after the grandchild.
653 	 */
654 	wpid = wait(&status);
655 	ATF_REQUIRE(wpid == children[1]);
656 	ATF_REQUIRE(WIFEXITED(status));
657 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
658 
659 	wpid = wait(&status);
660 	ATF_REQUIRE(wpid == children[0]);
661 	ATF_REQUIRE(WIFEXITED(status));
662 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
663 
664 	wpid = wait(&status);
665 	ATF_REQUIRE(wpid == -1);
666 	ATF_REQUIRE(errno == ECHILD);
667 }
668 
669 static void
670 attach_fork_parent(int cpipe[2])
671 {
672 	pid_t fpid;
673 
674 	close(cpipe[0]);
675 
676 	/* Double-fork to disassociate from the debugger. */
677 	CHILD_REQUIRE((fpid = fork()) != -1);
678 	if (fpid != 0)
679 		_exit(3);
680 
681 	/* Send the pid of the disassociated child to the debugger. */
682 	fpid = getpid();
683 	CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
684 
685 	/* Wait for the debugger to attach. */
686 	CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
687 }
688 
689 /*
690  * Verify that a new child process is stopped after a followed fork and
691  * that the traced parent sees the exit of the child after the debugger
692  * when both processes remain attached to the debugger.  In this test
693  * the parent that forks is not a direct child of the debugger.
694  */
695 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
696 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
697 {
698 	pid_t children[2], fpid, wpid;
699 	int cpipe[2], status;
700 
701 	ATF_REQUIRE(pipe(cpipe) == 0);
702 	ATF_REQUIRE((fpid = fork()) != -1);
703 	if (fpid == 0) {
704 		attach_fork_parent(cpipe);
705 		follow_fork_parent(false);
706 	}
707 
708 	/* Parent process. */
709 	close(cpipe[1]);
710 
711 	/* Wait for the direct child to exit. */
712 	wpid = waitpid(fpid, &status, 0);
713 	ATF_REQUIRE(wpid == fpid);
714 	ATF_REQUIRE(WIFEXITED(status));
715 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
716 
717 	/* Read the pid of the fork parent. */
718 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
719 	    sizeof(children[0]));
720 
721 	/* Attach to the fork parent. */
722 	attach_child(children[0]);
723 
724 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
725 
726 	/* Continue the fork parent ignoring the SIGSTOP. */
727 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
728 
729 	/* Signal the fork parent to continue. */
730 	close(cpipe[0]);
731 
732 	children[1] = handle_fork_events(children[0], NULL);
733 	ATF_REQUIRE(children[1] > 0);
734 
735 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
736 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
737 
738 	/*
739 	 * The fork parent can't exit until the child reports status,
740 	 * so the child should report its exit first to the debugger.
741 	 */
742 	wpid = wait(&status);
743 	ATF_REQUIRE(wpid == children[1]);
744 	ATF_REQUIRE(WIFEXITED(status));
745 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
746 
747 	wpid = wait(&status);
748 	ATF_REQUIRE(wpid == children[0]);
749 	ATF_REQUIRE(WIFEXITED(status));
750 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
751 
752 	wpid = wait(&status);
753 	ATF_REQUIRE(wpid == -1);
754 	ATF_REQUIRE(errno == ECHILD);
755 }
756 
757 /*
758  * Verify that a new child process is stopped after a followed fork
759  * and that the traced parent sees the exit of the child when the new
760  * child process is detached after it reports its fork.  In this test
761  * the parent that forks is not a direct child of the debugger.
762  */
763 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
764 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
765 {
766 	pid_t children[2], fpid, wpid;
767 	int cpipe[2], status;
768 
769 	ATF_REQUIRE(pipe(cpipe) == 0);
770 	ATF_REQUIRE((fpid = fork()) != -1);
771 	if (fpid == 0) {
772 		attach_fork_parent(cpipe);
773 		follow_fork_parent(false);
774 	}
775 
776 	/* Parent process. */
777 	close(cpipe[1]);
778 
779 	/* Wait for the direct child to exit. */
780 	wpid = waitpid(fpid, &status, 0);
781 	ATF_REQUIRE(wpid == fpid);
782 	ATF_REQUIRE(WIFEXITED(status));
783 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
784 
785 	/* Read the pid of the fork parent. */
786 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
787 	    sizeof(children[0]));
788 
789 	/* Attach to the fork parent. */
790 	attach_child(children[0]);
791 
792 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
793 
794 	/* Continue the fork parent ignoring the SIGSTOP. */
795 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
796 
797 	/* Signal the fork parent to continue. */
798 	close(cpipe[0]);
799 
800 	children[1] = handle_fork_events(children[0], NULL);
801 	ATF_REQUIRE(children[1] > 0);
802 
803 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
804 	ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
805 
806 	/*
807 	 * Should not see any status from the child now, only the fork
808 	 * parent.
809 	 */
810 	wpid = wait(&status);
811 	ATF_REQUIRE(wpid == children[0]);
812 	ATF_REQUIRE(WIFEXITED(status));
813 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
814 
815 	wpid = wait(&status);
816 	ATF_REQUIRE(wpid == -1);
817 	ATF_REQUIRE(errno == ECHILD);
818 }
819 
820 /*
821  * Verify that a new child process is stopped after a followed fork
822  * and that the traced parent sees the exit of the child when the
823  * traced parent is detached after the fork.  In this test the parent
824  * that forks is not a direct child of the debugger.
825  */
826 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
827 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
828 {
829 	pid_t children[2], fpid, wpid;
830 	int cpipe[2], status;
831 
832 	ATF_REQUIRE(pipe(cpipe) == 0);
833 	ATF_REQUIRE((fpid = fork()) != -1);
834 	if (fpid == 0) {
835 		attach_fork_parent(cpipe);
836 		follow_fork_parent(false);
837 	}
838 
839 	/* Parent process. */
840 	close(cpipe[1]);
841 
842 	/* Wait for the direct child to exit. */
843 	wpid = waitpid(fpid, &status, 0);
844 	ATF_REQUIRE(wpid == fpid);
845 	ATF_REQUIRE(WIFEXITED(status));
846 	ATF_REQUIRE(WEXITSTATUS(status) == 3);
847 
848 	/* Read the pid of the fork parent. */
849 	ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
850 	    sizeof(children[0]));
851 
852 	/* Attach to the fork parent. */
853 	attach_child(children[0]);
854 
855 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
856 
857 	/* Continue the fork parent ignoring the SIGSTOP. */
858 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
859 
860 	/* Signal the fork parent to continue. */
861 	close(cpipe[0]);
862 
863 	children[1] = handle_fork_events(children[0], NULL);
864 	ATF_REQUIRE(children[1] > 0);
865 
866 	ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
867 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
868 
869 	/*
870 	 * Should not see any status from the fork parent now, only
871 	 * the child.
872 	 */
873 	wpid = wait(&status);
874 	ATF_REQUIRE(wpid == children[1]);
875 	ATF_REQUIRE(WIFEXITED(status));
876 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
877 
878 	wpid = wait(&status);
879 	ATF_REQUIRE(wpid == -1);
880 	ATF_REQUIRE(errno == ECHILD);
881 }
882 
883 /*
884  * Verify that a child process does not see an unrelated debugger as its
885  * parent but sees its original parent process.
886  */
887 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
888 ATF_TC_BODY(ptrace__getppid, tc)
889 {
890 	pid_t child, debugger, ppid, wpid;
891 	int cpipe[2], dpipe[2], status;
892 	char c;
893 
894 	ATF_REQUIRE(pipe(cpipe) == 0);
895 	ATF_REQUIRE((child = fork()) != -1);
896 
897 	if (child == 0) {
898 		/* Child process. */
899 		close(cpipe[0]);
900 
901 		/* Wait for parent to be ready. */
902 		CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
903 
904 		/* Report the parent PID to the parent. */
905 		ppid = getppid();
906 		CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
907 		    sizeof(ppid));
908 
909 		_exit(1);
910 	}
911 	close(cpipe[1]);
912 
913 	ATF_REQUIRE(pipe(dpipe) == 0);
914 	ATF_REQUIRE((debugger = fork()) != -1);
915 
916 	if (debugger == 0) {
917 		/* Debugger process. */
918 		close(dpipe[0]);
919 
920 		CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
921 
922 		wpid = waitpid(child, &status, 0);
923 		CHILD_REQUIRE(wpid == child);
924 		CHILD_REQUIRE(WIFSTOPPED(status));
925 		CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
926 
927 		CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
928 
929 		/* Signal parent that debugger is attached. */
930 		CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
931 
932 		/* Wait for traced child to exit. */
933 		wpid = waitpid(child, &status, 0);
934 		CHILD_REQUIRE(wpid == child);
935 		CHILD_REQUIRE(WIFEXITED(status));
936 		CHILD_REQUIRE(WEXITSTATUS(status) == 1);
937 
938 		_exit(0);
939 	}
940 	close(dpipe[1]);
941 
942 	/* Parent process. */
943 
944 	/* Wait for the debugger to attach to the child. */
945 	ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
946 
947 	/* Release the child. */
948 	ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
949 
950 	/* Read the parent PID from the child. */
951 	ATF_REQUIRE(read(cpipe[0], &ppid, sizeof(ppid)) == sizeof(ppid));
952 	close(cpipe[0]);
953 
954 	ATF_REQUIRE(ppid == getpid());
955 
956 	/* Wait for the debugger. */
957 	wpid = waitpid(debugger, &status, 0);
958 	ATF_REQUIRE(wpid == debugger);
959 	ATF_REQUIRE(WIFEXITED(status));
960 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
961 
962 	/* The child process should now be ready. */
963 	wpid = waitpid(child, &status, WNOHANG);
964 	ATF_REQUIRE(wpid == child);
965 	ATF_REQUIRE(WIFEXITED(status));
966 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
967 }
968 
969 /*
970  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
971  * child process created via fork() reports the correct value.
972  */
973 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
974 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
975 {
976 	struct ptrace_lwpinfo pl[2];
977 	pid_t children[2], fpid, wpid;
978 	int status;
979 
980 	ATF_REQUIRE((fpid = fork()) != -1);
981 	if (fpid == 0) {
982 		trace_me();
983 		follow_fork_parent(false);
984 	}
985 
986 	/* Parent process. */
987 	children[0] = fpid;
988 
989 	/* The first wait() should report the stop from SIGSTOP. */
990 	wpid = waitpid(children[0], &status, 0);
991 	ATF_REQUIRE(wpid == children[0]);
992 	ATF_REQUIRE(WIFSTOPPED(status));
993 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
994 
995 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
996 
997 	/* Continue the child ignoring the SIGSTOP. */
998 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
999 
1000 	/* Wait for both halves of the fork event to get reported. */
1001 	children[1] = handle_fork_events(children[0], pl);
1002 	ATF_REQUIRE(children[1] > 0);
1003 
1004 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1005 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1006 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
1007 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1008 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1009 
1010 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1011 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1012 
1013 	/*
1014 	 * The child can't exit until the grandchild reports status, so the
1015 	 * grandchild should report its exit first to the debugger.
1016 	 */
1017 	wpid = wait(&status);
1018 	ATF_REQUIRE(wpid == children[1]);
1019 	ATF_REQUIRE(WIFEXITED(status));
1020 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1021 
1022 	wpid = wait(&status);
1023 	ATF_REQUIRE(wpid == children[0]);
1024 	ATF_REQUIRE(WIFEXITED(status));
1025 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1026 
1027 	wpid = wait(&status);
1028 	ATF_REQUIRE(wpid == -1);
1029 	ATF_REQUIRE(errno == ECHILD);
1030 }
1031 
1032 /*
1033  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1034  * child process created via vfork() reports the correct value.
1035  */
1036 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1037 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1038 {
1039 	struct ptrace_lwpinfo pl[2];
1040 	pid_t children[2], fpid, wpid;
1041 	int status;
1042 
1043 	ATF_REQUIRE((fpid = fork()) != -1);
1044 	if (fpid == 0) {
1045 		trace_me();
1046 		follow_fork_parent(true);
1047 	}
1048 
1049 	/* Parent process. */
1050 	children[0] = fpid;
1051 
1052 	/* The first wait() should report the stop from SIGSTOP. */
1053 	wpid = waitpid(children[0], &status, 0);
1054 	ATF_REQUIRE(wpid == children[0]);
1055 	ATF_REQUIRE(WIFSTOPPED(status));
1056 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1057 
1058 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1059 
1060 	/* Continue the child ignoring the SIGSTOP. */
1061 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1062 
1063 	/* Wait for both halves of the fork event to get reported. */
1064 	children[1] = handle_fork_events(children[0], pl);
1065 	ATF_REQUIRE(children[1] > 0);
1066 
1067 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1068 	ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1069 	ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
1070 	ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
1071 	ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
1072 
1073 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1074 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1075 
1076 	/*
1077 	 * The child can't exit until the grandchild reports status, so the
1078 	 * grandchild should report its exit first to the debugger.
1079 	 */
1080 	wpid = wait(&status);
1081 	ATF_REQUIRE(wpid == children[1]);
1082 	ATF_REQUIRE(WIFEXITED(status));
1083 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1084 
1085 	wpid = wait(&status);
1086 	ATF_REQUIRE(wpid == children[0]);
1087 	ATF_REQUIRE(WIFEXITED(status));
1088 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1089 
1090 	wpid = wait(&status);
1091 	ATF_REQUIRE(wpid == -1);
1092 	ATF_REQUIRE(errno == ECHILD);
1093 }
1094 
1095 static void *
1096 simple_thread(void *arg __unused)
1097 {
1098 
1099 	pthread_exit(NULL);
1100 }
1101 
1102 static __dead2 void
1103 simple_thread_main(void)
1104 {
1105 	pthread_t thread;
1106 
1107 	CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1108 	CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1109 	exit(1);
1110 }
1111 
1112 /*
1113  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1114  * thread reports the correct value.
1115  */
1116 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1117 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1118 {
1119 	struct ptrace_lwpinfo pl;
1120 	pid_t fpid, wpid;
1121 	lwpid_t mainlwp;
1122 	int status;
1123 
1124 	ATF_REQUIRE((fpid = fork()) != -1);
1125 	if (fpid == 0) {
1126 		trace_me();
1127 		simple_thread_main();
1128 	}
1129 
1130 	/* The first wait() should report the stop from SIGSTOP. */
1131 	wpid = waitpid(fpid, &status, 0);
1132 	ATF_REQUIRE(wpid == fpid);
1133 	ATF_REQUIRE(WIFSTOPPED(status));
1134 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1135 
1136 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1137 	    sizeof(pl)) != -1);
1138 	mainlwp = pl.pl_lwpid;
1139 
1140 	/*
1141 	 * Continue the child ignoring the SIGSTOP and tracing all
1142 	 * system call exits.
1143 	 */
1144 	ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1145 
1146 	/*
1147 	 * Wait for the new thread to arrive.  pthread_create() might
1148 	 * invoke any number of system calls.  For now we just wait
1149 	 * for the new thread to arrive and make sure it reports a
1150 	 * valid system call code.  If ptrace grows thread event
1151 	 * reporting then this test can be made more precise.
1152 	 */
1153 	for (;;) {
1154 		wpid = waitpid(fpid, &status, 0);
1155 		ATF_REQUIRE(wpid == fpid);
1156 		ATF_REQUIRE(WIFSTOPPED(status));
1157 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1158 
1159 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1160 		    sizeof(pl)) != -1);
1161 		ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1162 		ATF_REQUIRE(pl.pl_syscall_code != 0);
1163 		if (pl.pl_lwpid != mainlwp)
1164 			/* New thread seen. */
1165 			break;
1166 
1167 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1168 	}
1169 
1170 	/* Wait for the child to exit. */
1171 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1172 	for (;;) {
1173 		wpid = waitpid(fpid, &status, 0);
1174 		ATF_REQUIRE(wpid == fpid);
1175 		if (WIFEXITED(status))
1176 			break;
1177 
1178 		ATF_REQUIRE(WIFSTOPPED(status));
1179 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1180 		ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1181 	}
1182 
1183 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1184 
1185 	wpid = wait(&status);
1186 	ATF_REQUIRE(wpid == -1);
1187 	ATF_REQUIRE(errno == ECHILD);
1188 }
1189 
1190 /*
1191  * Verify that the expected LWP events are reported for a child thread.
1192  */
1193 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1194 ATF_TC_BODY(ptrace__lwp_events, tc)
1195 {
1196 	struct ptrace_lwpinfo pl;
1197 	pid_t fpid, wpid;
1198 	lwpid_t lwps[2];
1199 	int status;
1200 
1201 	ATF_REQUIRE((fpid = fork()) != -1);
1202 	if (fpid == 0) {
1203 		trace_me();
1204 		simple_thread_main();
1205 	}
1206 
1207 	/* The first wait() should report the stop from SIGSTOP. */
1208 	wpid = waitpid(fpid, &status, 0);
1209 	ATF_REQUIRE(wpid == fpid);
1210 	ATF_REQUIRE(WIFSTOPPED(status));
1211 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1212 
1213 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1214 	    sizeof(pl)) != -1);
1215 	lwps[0] = pl.pl_lwpid;
1216 
1217 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1218 
1219 	/* Continue the child ignoring the SIGSTOP. */
1220 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1221 
1222 	/* The first event should be for the child thread's birth. */
1223 	wpid = waitpid(fpid, &status, 0);
1224 	ATF_REQUIRE(wpid == fpid);
1225 	ATF_REQUIRE(WIFSTOPPED(status));
1226 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1227 
1228 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1229 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1230 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1231 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1232 	lwps[1] = pl.pl_lwpid;
1233 
1234 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1235 
1236 	/* The next event should be for the child thread's death. */
1237 	wpid = waitpid(fpid, &status, 0);
1238 	ATF_REQUIRE(wpid == fpid);
1239 	ATF_REQUIRE(WIFSTOPPED(status));
1240 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1241 
1242 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1243 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1244 	    (PL_FLAG_EXITED | PL_FLAG_SCE));
1245 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1246 
1247 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1248 
1249 	/* The last event should be for the child process's exit. */
1250 	wpid = waitpid(fpid, &status, 0);
1251 	ATF_REQUIRE(WIFEXITED(status));
1252 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1253 
1254 	wpid = wait(&status);
1255 	ATF_REQUIRE(wpid == -1);
1256 	ATF_REQUIRE(errno == ECHILD);
1257 }
1258 
1259 static void *
1260 exec_thread(void *arg __unused)
1261 {
1262 
1263 	execl("/usr/bin/true", "true", NULL);
1264 	exit(127);
1265 }
1266 
1267 static __dead2 void
1268 exec_thread_main(void)
1269 {
1270 	pthread_t thread;
1271 
1272 	CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1273 	for (;;)
1274 		sleep(60);
1275 	exit(1);
1276 }
1277 
1278 /*
1279  * Verify that the expected LWP events are reported for a multithreaded
1280  * process that calls execve(2).
1281  */
1282 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1283 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1284 {
1285 	struct ptrace_lwpinfo pl;
1286 	pid_t fpid, wpid;
1287 	lwpid_t lwps[2];
1288 	int status;
1289 
1290 	ATF_REQUIRE((fpid = fork()) != -1);
1291 	if (fpid == 0) {
1292 		trace_me();
1293 		exec_thread_main();
1294 	}
1295 
1296 	/* The first wait() should report the stop from SIGSTOP. */
1297 	wpid = waitpid(fpid, &status, 0);
1298 	ATF_REQUIRE(wpid == fpid);
1299 	ATF_REQUIRE(WIFSTOPPED(status));
1300 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1301 
1302 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1303 	    sizeof(pl)) != -1);
1304 	lwps[0] = pl.pl_lwpid;
1305 
1306 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1307 
1308 	/* Continue the child ignoring the SIGSTOP. */
1309 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1310 
1311 	/* The first event should be for the child thread's birth. */
1312 	wpid = waitpid(fpid, &status, 0);
1313 	ATF_REQUIRE(wpid == fpid);
1314 	ATF_REQUIRE(WIFSTOPPED(status));
1315 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1316 
1317 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1318 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1319 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1320 	ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1321 	lwps[1] = pl.pl_lwpid;
1322 
1323 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1324 
1325 	/*
1326 	 * The next event should be for the main thread's death due to
1327 	 * single threading from execve().
1328 	 */
1329 	wpid = waitpid(fpid, &status, 0);
1330 	ATF_REQUIRE(wpid == fpid);
1331 	ATF_REQUIRE(WIFSTOPPED(status));
1332 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1333 
1334 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1335 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1336 	    (PL_FLAG_EXITED));
1337 	ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1338 
1339 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1340 
1341 	/* The next event should be for the child process's exec. */
1342 	wpid = waitpid(fpid, &status, 0);
1343 	ATF_REQUIRE(WIFSTOPPED(status));
1344 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1345 
1346 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1347 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1348 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1349 	ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1350 
1351 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1352 
1353 	/* The last event should be for the child process's exit. */
1354 	wpid = waitpid(fpid, &status, 0);
1355 	ATF_REQUIRE(WIFEXITED(status));
1356 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1357 
1358 	wpid = wait(&status);
1359 	ATF_REQUIRE(wpid == -1);
1360 	ATF_REQUIRE(errno == ECHILD);
1361 }
1362 
1363 static void
1364 handler(int sig __unused)
1365 {
1366 }
1367 
1368 static void
1369 signal_main(void)
1370 {
1371 
1372 	signal(SIGINFO, handler);
1373 	raise(SIGINFO);
1374 	exit(0);
1375 }
1376 
1377 /*
1378  * Verify that the expected ptrace event is reported for a signal.
1379  */
1380 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1381 ATF_TC_BODY(ptrace__siginfo, tc)
1382 {
1383 	struct ptrace_lwpinfo pl;
1384 	pid_t fpid, wpid;
1385 	int status;
1386 
1387 	ATF_REQUIRE((fpid = fork()) != -1);
1388 	if (fpid == 0) {
1389 		trace_me();
1390 		signal_main();
1391 	}
1392 
1393 	/* The first wait() should report the stop from SIGSTOP. */
1394 	wpid = waitpid(fpid, &status, 0);
1395 	ATF_REQUIRE(wpid == fpid);
1396 	ATF_REQUIRE(WIFSTOPPED(status));
1397 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1398 
1399 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1400 
1401 	/* The next event should be for the SIGINFO. */
1402 	wpid = waitpid(fpid, &status, 0);
1403 	ATF_REQUIRE(WIFSTOPPED(status));
1404 	ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1405 
1406 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1407 	ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1408 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1409 	ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1410 	ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1411 
1412 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1413 
1414 	/* The last event should be for the child process's exit. */
1415 	wpid = waitpid(fpid, &status, 0);
1416 	ATF_REQUIRE(WIFEXITED(status));
1417 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1418 
1419 	wpid = wait(&status);
1420 	ATF_REQUIRE(wpid == -1);
1421 	ATF_REQUIRE(errno == ECHILD);
1422 }
1423 
1424 /*
1425  * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1426  */
1427 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1428 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1429 {
1430 	pid_t fpid, wpid;
1431 	int events, status;
1432 
1433 	ATF_REQUIRE((fpid = fork()) != -1);
1434 	if (fpid == 0) {
1435 		trace_me();
1436 		exec_thread(NULL);
1437 	}
1438 
1439 	/* The first wait() should report the stop from SIGSTOP. */
1440 	wpid = waitpid(fpid, &status, 0);
1441 	ATF_REQUIRE(wpid == fpid);
1442 	ATF_REQUIRE(WIFSTOPPED(status));
1443 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1444 
1445 	events = 0;
1446 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1447 	    sizeof(events)) == 0);
1448 
1449 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1450 
1451 	/* Should get one event at exit. */
1452 	wpid = waitpid(fpid, &status, 0);
1453 	ATF_REQUIRE(WIFEXITED(status));
1454 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1455 
1456 	wpid = wait(&status);
1457 	ATF_REQUIRE(wpid == -1);
1458 	ATF_REQUIRE(errno == ECHILD);
1459 }
1460 
1461 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1462 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1463 {
1464 	struct ptrace_lwpinfo pl;
1465 	pid_t fpid, wpid;
1466 	int events, status;
1467 
1468 	ATF_REQUIRE((fpid = fork()) != -1);
1469 	if (fpid == 0) {
1470 		trace_me();
1471 		exec_thread(NULL);
1472 	}
1473 
1474 	/* The first wait() should report the stop from SIGSTOP. */
1475 	wpid = waitpid(fpid, &status, 0);
1476 	ATF_REQUIRE(wpid == fpid);
1477 	ATF_REQUIRE(WIFSTOPPED(status));
1478 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1479 
1480 	events = PTRACE_EXEC;
1481 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1482 	    sizeof(events)) == 0);
1483 
1484 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1485 
1486 	/* The next event should be for the child process's exec. */
1487 	wpid = waitpid(fpid, &status, 0);
1488 	ATF_REQUIRE(WIFSTOPPED(status));
1489 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1490 
1491 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1492 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1493 	    (PL_FLAG_EXEC | PL_FLAG_SCX));
1494 
1495 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1496 
1497 	/* The last event should be for the child process's exit. */
1498 	wpid = waitpid(fpid, &status, 0);
1499 	ATF_REQUIRE(WIFEXITED(status));
1500 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1501 
1502 	wpid = wait(&status);
1503 	ATF_REQUIRE(wpid == -1);
1504 	ATF_REQUIRE(errno == ECHILD);
1505 }
1506 
1507 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1508 ATF_TC_BODY(ptrace__event_mask, tc)
1509 {
1510 	pid_t fpid, wpid;
1511 	int events, status;
1512 
1513 	ATF_REQUIRE((fpid = fork()) != -1);
1514 	if (fpid == 0) {
1515 		trace_me();
1516 		exit(0);
1517 	}
1518 
1519 	/* The first wait() should report the stop from SIGSTOP. */
1520 	wpid = waitpid(fpid, &status, 0);
1521 	ATF_REQUIRE(wpid == fpid);
1522 	ATF_REQUIRE(WIFSTOPPED(status));
1523 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1524 
1525 	/* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1526 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1527 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1528 	    sizeof(events)) == 0);
1529 	ATF_REQUIRE(events & PTRACE_FORK);
1530 	ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1531 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1532 	    sizeof(events)) == 0);
1533 	ATF_REQUIRE(!(events & PTRACE_FORK));
1534 
1535 	/* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1536 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, 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_LWP);
1540 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, 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_LWP));
1544 
1545 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1546 
1547 	/* Should get one event at exit. */
1548 	wpid = waitpid(fpid, &status, 0);
1549 	ATF_REQUIRE(WIFEXITED(status));
1550 	ATF_REQUIRE(WEXITSTATUS(status) == 0);
1551 
1552 	wpid = wait(&status);
1553 	ATF_REQUIRE(wpid == -1);
1554 	ATF_REQUIRE(errno == ECHILD);
1555 }
1556 
1557 /*
1558  * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1559  */
1560 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1561 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1562 {
1563 	struct ptrace_lwpinfo pl;
1564 	pid_t fpid, wpid;
1565 	int events, status;
1566 
1567 	ATF_REQUIRE((fpid = fork()) != -1);
1568 	if (fpid == 0) {
1569 		trace_me();
1570 		follow_fork_parent(true);
1571 	}
1572 
1573 	/* The first wait() should report the stop from SIGSTOP. */
1574 	wpid = waitpid(fpid, &status, 0);
1575 	ATF_REQUIRE(wpid == fpid);
1576 	ATF_REQUIRE(WIFSTOPPED(status));
1577 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1578 
1579 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1580 	    sizeof(events)) == 0);
1581 	events |= PTRACE_VFORK;
1582 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1583 	    sizeof(events)) == 0);
1584 
1585 	/* Continue the child ignoring the SIGSTOP. */
1586 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1587 
1588 	/* The next event should report the end of the vfork. */
1589 	wpid = wait(&status);
1590 	ATF_REQUIRE(wpid == fpid);
1591 	ATF_REQUIRE(WIFSTOPPED(status));
1592 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1593 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1594 	ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1595 
1596 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1597 
1598 	wpid = wait(&status);
1599 	ATF_REQUIRE(wpid == fpid);
1600 	ATF_REQUIRE(WIFEXITED(status));
1601 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1602 
1603 	wpid = wait(&status);
1604 	ATF_REQUIRE(wpid == -1);
1605 	ATF_REQUIRE(errno == ECHILD);
1606 }
1607 
1608 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1609 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1610 {
1611 	struct ptrace_lwpinfo pl[2];
1612 	pid_t children[2], fpid, wpid;
1613 	int events, status;
1614 
1615 	ATF_REQUIRE((fpid = fork()) != -1);
1616 	if (fpid == 0) {
1617 		trace_me();
1618 		follow_fork_parent(true);
1619 	}
1620 
1621 	/* Parent process. */
1622 	children[0] = fpid;
1623 
1624 	/* The first wait() should report the stop from SIGSTOP. */
1625 	wpid = waitpid(children[0], &status, 0);
1626 	ATF_REQUIRE(wpid == children[0]);
1627 	ATF_REQUIRE(WIFSTOPPED(status));
1628 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1629 
1630 	ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1631 	    sizeof(events)) == 0);
1632 	events |= PTRACE_FORK | PTRACE_VFORK;
1633 	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1634 	    sizeof(events)) == 0);
1635 
1636 	/* Continue the child ignoring the SIGSTOP. */
1637 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1638 
1639 	/* Wait for both halves of the fork event to get reported. */
1640 	children[1] = handle_fork_events(children[0], pl);
1641 	ATF_REQUIRE(children[1] > 0);
1642 
1643 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1644 
1645 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1646 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1647 
1648 	/*
1649 	 * The child can't exit until the grandchild reports status, so the
1650 	 * grandchild should report its exit first to the debugger.
1651 	 */
1652 	wpid = waitpid(children[1], &status, 0);
1653 	ATF_REQUIRE(wpid == children[1]);
1654 	ATF_REQUIRE(WIFEXITED(status));
1655 	ATF_REQUIRE(WEXITSTATUS(status) == 2);
1656 
1657 	/*
1658 	 * The child should report it's vfork() completion before it
1659 	 * exits.
1660 	 */
1661 	wpid = wait(&status);
1662 	ATF_REQUIRE(wpid == children[0]);
1663 	ATF_REQUIRE(WIFSTOPPED(status));
1664 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1665 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1666 	    -1);
1667 	ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1668 
1669 	ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1670 
1671 	wpid = wait(&status);
1672 	ATF_REQUIRE(wpid == children[0]);
1673 	ATF_REQUIRE(WIFEXITED(status));
1674 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
1675 
1676 	wpid = wait(&status);
1677 	ATF_REQUIRE(wpid == -1);
1678 	ATF_REQUIRE(errno == ECHILD);
1679 }
1680 
1681 /*
1682  * XXX: There's nothing inherently platform specific about this test, however a
1683  * userspace visible breakpoint() is a prerequisite.
1684  */
1685  #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1686 /*
1687  * Verify that no more events are reported after PT_KILL except for the
1688  * process exit when stopped due to a breakpoint trap.
1689  */
1690 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1691 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1692 {
1693 	pid_t fpid, wpid;
1694 	int status;
1695 
1696 	ATF_REQUIRE((fpid = fork()) != -1);
1697 	if (fpid == 0) {
1698 		trace_me();
1699 		breakpoint();
1700 		exit(1);
1701 	}
1702 
1703 	/* The first wait() should report the stop from SIGSTOP. */
1704 	wpid = waitpid(fpid, &status, 0);
1705 	ATF_REQUIRE(wpid == fpid);
1706 	ATF_REQUIRE(WIFSTOPPED(status));
1707 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1708 
1709 	/* Continue the child ignoring the SIGSTOP. */
1710 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1711 
1712 	/* The second wait() should report hitting the breakpoint. */
1713 	wpid = waitpid(fpid, &status, 0);
1714 	ATF_REQUIRE(wpid == fpid);
1715 	ATF_REQUIRE(WIFSTOPPED(status));
1716 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1717 
1718 	/* Kill the child process. */
1719 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1720 
1721 	/* The last wait() should report the SIGKILL. */
1722 	wpid = waitpid(fpid, &status, 0);
1723 	ATF_REQUIRE(wpid == fpid);
1724 	ATF_REQUIRE(WIFSIGNALED(status));
1725 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1726 
1727 	wpid = wait(&status);
1728 	ATF_REQUIRE(wpid == -1);
1729 	ATF_REQUIRE(errno == ECHILD);
1730 }
1731 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1732 
1733 /*
1734  * Verify that no more events are reported after PT_KILL except for the
1735  * process exit when stopped inside of a system call.
1736  */
1737 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1738 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1739 {
1740 	struct ptrace_lwpinfo pl;
1741 	pid_t fpid, wpid;
1742 	int status;
1743 
1744 	ATF_REQUIRE((fpid = fork()) != -1);
1745 	if (fpid == 0) {
1746 		trace_me();
1747 		getpid();
1748 		exit(1);
1749 	}
1750 
1751 	/* The first wait() should report the stop from SIGSTOP. */
1752 	wpid = waitpid(fpid, &status, 0);
1753 	ATF_REQUIRE(wpid == fpid);
1754 	ATF_REQUIRE(WIFSTOPPED(status));
1755 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1756 
1757 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
1758 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1759 
1760 	/* The second wait() should report a system call entry for getpid(). */
1761 	wpid = waitpid(fpid, &status, 0);
1762 	ATF_REQUIRE(wpid == fpid);
1763 	ATF_REQUIRE(WIFSTOPPED(status));
1764 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1765 
1766 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1767 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1768 
1769 	/* Kill the child process. */
1770 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1771 
1772 	/* The last wait() should report the SIGKILL. */
1773 	wpid = waitpid(fpid, &status, 0);
1774 	ATF_REQUIRE(wpid == fpid);
1775 	ATF_REQUIRE(WIFSIGNALED(status));
1776 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1777 
1778 	wpid = wait(&status);
1779 	ATF_REQUIRE(wpid == -1);
1780 	ATF_REQUIRE(errno == ECHILD);
1781 }
1782 
1783 /*
1784  * Verify that no more events are reported after PT_KILL except for the
1785  * process exit when killing a multithreaded process.
1786  */
1787 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1788 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1789 {
1790 	struct ptrace_lwpinfo pl;
1791 	pid_t fpid, wpid;
1792 	lwpid_t main_lwp;
1793 	int status;
1794 
1795 	ATF_REQUIRE((fpid = fork()) != -1);
1796 	if (fpid == 0) {
1797 		trace_me();
1798 		simple_thread_main();
1799 	}
1800 
1801 	/* The first wait() should report the stop from SIGSTOP. */
1802 	wpid = waitpid(fpid, &status, 0);
1803 	ATF_REQUIRE(wpid == fpid);
1804 	ATF_REQUIRE(WIFSTOPPED(status));
1805 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1806 
1807 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1808 	    sizeof(pl)) != -1);
1809 	main_lwp = pl.pl_lwpid;
1810 
1811 	ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1812 
1813 	/* Continue the child ignoring the SIGSTOP. */
1814 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1815 
1816 	/* The first event should be for the child thread's birth. */
1817 	wpid = waitpid(fpid, &status, 0);
1818 	ATF_REQUIRE(wpid == fpid);
1819 	ATF_REQUIRE(WIFSTOPPED(status));
1820 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1821 
1822 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1823 	ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1824 	    (PL_FLAG_BORN | PL_FLAG_SCX));
1825 	ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1826 
1827 	/* Kill the child process. */
1828 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1829 
1830 	/* The last wait() should report the SIGKILL. */
1831 	wpid = waitpid(fpid, &status, 0);
1832 	ATF_REQUIRE(wpid == fpid);
1833 	ATF_REQUIRE(WIFSIGNALED(status));
1834 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1835 
1836 	wpid = wait(&status);
1837 	ATF_REQUIRE(wpid == -1);
1838 	ATF_REQUIRE(errno == ECHILD);
1839 }
1840 
1841 static void *
1842 mask_usr1_thread(void *arg)
1843 {
1844 	pthread_barrier_t *pbarrier;
1845 	sigset_t sigmask;
1846 
1847 	pbarrier = (pthread_barrier_t*)arg;
1848 
1849 	sigemptyset(&sigmask);
1850 	sigaddset(&sigmask, SIGUSR1);
1851 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1852 
1853 	/* Sync up with other thread after sigmask updated. */
1854 	pthread_barrier_wait(pbarrier);
1855 
1856 	for (;;)
1857 		sleep(60);
1858 
1859 	return (NULL);
1860 }
1861 
1862 /*
1863  * Verify that the SIGKILL from PT_KILL takes priority over other signals
1864  * and prevents spurious stops due to those other signals.
1865  */
1866 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1867 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1868 {
1869 	pid_t fpid, wpid;
1870 	int status;
1871 	cpuset_t setmask;
1872 	pthread_t t;
1873 	pthread_barrier_t barrier;
1874 
1875 	ATF_REQUIRE((fpid = fork()) != -1);
1876 	if (fpid == 0) {
1877 		/*
1878 		 * Bind to one CPU so only one thread at a time will run. This
1879 		 * test expects that the first thread created (the main thread)
1880 		 * will be unsuspended first and will block the second thread
1881 		 * from running.
1882 		 */
1883 		CPU_ZERO(&setmask);
1884 		CPU_SET(0, &setmask);
1885 		cpusetid_t setid;
1886 		CHILD_REQUIRE(cpuset(&setid) == 0);
1887 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1888 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1889 
1890 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1891 
1892 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1893 		    (void*)&barrier) == 0);
1894 
1895 		sigset_t sigmask;
1896 		sigemptyset(&sigmask);
1897 		sigaddset(&sigmask, SIGUSR2);
1898 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1899 
1900 		/* Sync up with other thread after sigmask updated. */
1901 		pthread_barrier_wait(&barrier);
1902 
1903 		trace_me();
1904 
1905 		for (;;)
1906 			sleep(60);
1907 
1908 		exit(1);
1909 	}
1910 
1911 	/* The first wait() should report the stop from SIGSTOP. */
1912 	wpid = waitpid(fpid, &status, 0);
1913 	ATF_REQUIRE(wpid == fpid);
1914 	ATF_REQUIRE(WIFSTOPPED(status));
1915 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1916 
1917 	/* Continue the child ignoring the SIGSTOP. */
1918 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1919 
1920 	/* Send a signal that only the second thread can handle. */
1921 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1922 
1923 	/* The second wait() should report the SIGUSR2. */
1924 	wpid = waitpid(fpid, &status, 0);
1925 	ATF_REQUIRE(wpid == fpid);
1926 	ATF_REQUIRE(WIFSTOPPED(status));
1927 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1928 
1929 	/* Send a signal that only the first thread can handle. */
1930 	ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1931 
1932 	/* Replace the SIGUSR2 with a kill. */
1933 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1934 
1935 	/* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1936 	wpid = waitpid(fpid, &status, 0);
1937 	ATF_REQUIRE(wpid == fpid);
1938 	ATF_REQUIRE(WIFSIGNALED(status));
1939 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1940 
1941 	wpid = wait(&status);
1942 	ATF_REQUIRE(wpid == -1);
1943 	ATF_REQUIRE(errno == ECHILD);
1944 }
1945 
1946 /*
1947  * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1948  * and prevents spurious stops caused by those events.
1949  */
1950 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1951 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1952 {
1953 	pid_t fpid, wpid;
1954 	int status, i;
1955 	cpuset_t setmask;
1956 	pthread_t t;
1957 	pthread_barrier_t barrier;
1958 	lwpid_t main_lwp;
1959 	struct ptrace_lwpinfo pl;
1960 
1961 	ATF_REQUIRE((fpid = fork()) != -1);
1962 	if (fpid == 0) {
1963 		trace_me();
1964 
1965 		/*
1966 		 * Bind to one CPU so only one thread at a time will run. This
1967 		 * test expects that the first thread created (the main thread)
1968 		 * will be unsuspended first and will block the second thread
1969 		 * from running.
1970 		 */
1971 		CPU_ZERO(&setmask);
1972 		CPU_SET(0, &setmask);
1973 		cpusetid_t setid;
1974 		CHILD_REQUIRE(cpuset(&setid) == 0);
1975 		CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1976 		    CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1977 
1978 		CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1979 
1980 		CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1981 		    (void*)&barrier) == 0);
1982 
1983 		sigset_t sigmask;
1984 		sigemptyset(&sigmask);
1985 		sigaddset(&sigmask, SIGUSR2);
1986 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1987 
1988 		/* Sync up with other thread after sigmask updated. */
1989 		pthread_barrier_wait(&barrier);
1990 
1991 		/* Sync up with the test before doing the getpid(). */
1992 		raise(SIGSTOP);
1993 
1994 		getpid();
1995 		exit(1);
1996 	}
1997 
1998 	/* The first wait() should report the stop from SIGSTOP. */
1999 	wpid = waitpid(fpid, &status, 0);
2000 	ATF_REQUIRE(wpid == fpid);
2001 	ATF_REQUIRE(WIFSTOPPED(status));
2002 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2003 
2004 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2005 	main_lwp = pl.pl_lwpid;
2006 
2007 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2008 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2009 
2010 	/*
2011 	 * Continue until child is done with setup, which is indicated with
2012 	 * SIGSTOP. Ignore system calls in the meantime.
2013 	 */
2014 	for (;;) {
2015 		wpid = waitpid(fpid, &status, 0);
2016 		ATF_REQUIRE(wpid == fpid);
2017 		ATF_REQUIRE(WIFSTOPPED(status));
2018 		if (WSTOPSIG(status) == SIGTRAP) {
2019 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2020 			    sizeof(pl)) != -1);
2021 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2022 		} else {
2023 			ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2024 			break;
2025 		}
2026 		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2027 	}
2028 
2029 	/* Let both threads hit their syscall entries. */
2030 	for (i = 0; i < 2; ++i) {
2031 		ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2032 
2033 		wpid = waitpid(fpid, &status, 0);
2034 		ATF_REQUIRE(wpid == fpid);
2035 		ATF_REQUIRE(WIFSTOPPED(status));
2036 		ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2037 
2038 		ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2039 		    sizeof(pl)) != -1);
2040 		ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2041 
2042 		/*
2043 		 * Prevent the main thread from hitting its syscall exit for
2044 		 * now.
2045 		 */
2046 		if (pl.pl_lwpid == main_lwp)
2047 			ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
2048 
2049 	}
2050 
2051 	/* Send a signal that only the second thread can handle. */
2052 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2053 
2054 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2055 
2056 	/* The second wait() should report the SIGUSR2. */
2057 	wpid = waitpid(fpid, &status, 0);
2058 	ATF_REQUIRE(wpid == fpid);
2059 	ATF_REQUIRE(WIFSTOPPED(status));
2060 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2061 
2062 	/* Allow the main thread to try to finish its system call. */
2063 	ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2064 
2065 	/*
2066 	 * At this point, the main thread is in the middle of a system call and
2067 	 * has been resumed. The second thread has taken a signal which will be
2068 	 * replaced with a SIGKILL. We expect the main thread will get to run
2069 	 * first. It should notice the kill request and exit accordingly and
2070 	 * not stop for the system call exit event.
2071 	 */
2072 
2073 	/* Replace the SIGUSR2 with a kill. */
2074 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2075 
2076 	/* The last wait() should report the SIGKILL (not a syscall exit). */
2077 	wpid = waitpid(fpid, &status, 0);
2078 	ATF_REQUIRE(wpid == fpid);
2079 	ATF_REQUIRE(WIFSIGNALED(status));
2080 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2081 
2082 	wpid = wait(&status);
2083 	ATF_REQUIRE(wpid == -1);
2084 	ATF_REQUIRE(errno == ECHILD);
2085 }
2086 
2087 static void
2088 sigusr1_handler(int sig)
2089 {
2090 
2091 	CHILD_REQUIRE(sig == SIGUSR1);
2092 	_exit(2);
2093 }
2094 
2095 /*
2096  * Verify that even if the signal queue is full for a child process,
2097  * a PT_KILL will kill the process.
2098  */
2099 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2100 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2101 {
2102 	pid_t fpid, wpid;
2103 	int status;
2104 	int max_pending_per_proc;
2105 	size_t len;
2106 	int i;
2107 
2108 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2109 
2110 	ATF_REQUIRE((fpid = fork()) != -1);
2111 	if (fpid == 0) {
2112 		trace_me();
2113 		exit(1);
2114 	}
2115 
2116 	/* The first wait() should report the stop from SIGSTOP. */
2117 	wpid = waitpid(fpid, &status, 0);
2118 	ATF_REQUIRE(wpid == fpid);
2119 	ATF_REQUIRE(WIFSTOPPED(status));
2120 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2121 
2122 	len = sizeof(max_pending_per_proc);
2123 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2124 	    &max_pending_per_proc, &len, NULL, 0) == 0);
2125 
2126 	/* Fill the signal queue. */
2127 	for (i = 0; i < max_pending_per_proc; ++i)
2128 		ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2129 
2130 	/* Kill the child process. */
2131 	ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2132 
2133 	/* The last wait() should report the SIGKILL. */
2134 	wpid = waitpid(fpid, &status, 0);
2135 	ATF_REQUIRE(wpid == fpid);
2136 	ATF_REQUIRE(WIFSIGNALED(status));
2137 	ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2138 
2139 	wpid = wait(&status);
2140 	ATF_REQUIRE(wpid == -1);
2141 	ATF_REQUIRE(errno == ECHILD);
2142 }
2143 
2144 /*
2145  * Verify that when stopped at a system call entry, a signal can be
2146  * requested with PT_CONTINUE which will be delivered once the system
2147  * call is complete.
2148  */
2149 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2150 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2151 {
2152 	struct ptrace_lwpinfo pl;
2153 	pid_t fpid, wpid;
2154 	int status;
2155 
2156 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2157 
2158 	ATF_REQUIRE((fpid = fork()) != -1);
2159 	if (fpid == 0) {
2160 		trace_me();
2161 		getpid();
2162 		exit(1);
2163 	}
2164 
2165 	/* The first wait() should report the stop from SIGSTOP. */
2166 	wpid = waitpid(fpid, &status, 0);
2167 	ATF_REQUIRE(wpid == fpid);
2168 	ATF_REQUIRE(WIFSTOPPED(status));
2169 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2170 
2171 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2172 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2173 
2174 	/* The second wait() should report a system call entry for getpid(). */
2175 	wpid = waitpid(fpid, &status, 0);
2176 	ATF_REQUIRE(wpid == fpid);
2177 	ATF_REQUIRE(WIFSTOPPED(status));
2178 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2179 
2180 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2181 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2182 
2183 	/* Continue the child process with a signal. */
2184 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2185 
2186 	for (;;) {
2187 		/*
2188 		 * The last wait() should report exit 2, i.e., a normal _exit
2189 		 * from the signal handler. In the meantime, catch and proceed
2190 		 * past any syscall stops.
2191 		 */
2192 		wpid = waitpid(fpid, &status, 0);
2193 		ATF_REQUIRE(wpid == fpid);
2194 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2195 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2196 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2197 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2198 		} else {
2199 			ATF_REQUIRE(WIFEXITED(status));
2200 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2201 			break;
2202 		}
2203 	}
2204 
2205 	wpid = wait(&status);
2206 	ATF_REQUIRE(wpid == -1);
2207 	ATF_REQUIRE(errno == ECHILD);
2208 }
2209 
2210 static void
2211 sigusr1_counting_handler(int sig)
2212 {
2213 	static int counter = 0;
2214 
2215 	CHILD_REQUIRE(sig == SIGUSR1);
2216 	counter++;
2217 	if (counter == 2)
2218 		_exit(2);
2219 }
2220 
2221 /*
2222  * Verify that, when continuing from a stop at system call entry and exit,
2223  * a signal can be requested from both stops, and both will be delivered when
2224  * the system call is complete.
2225  */
2226 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2227 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2228 {
2229 	struct ptrace_lwpinfo pl;
2230 	pid_t fpid, wpid;
2231 	int status;
2232 
2233 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2234 
2235 	ATF_REQUIRE((fpid = fork()) != -1);
2236 	if (fpid == 0) {
2237 		trace_me();
2238 		getpid();
2239 		exit(1);
2240 	}
2241 
2242 	/* The first wait() should report the stop from SIGSTOP. */
2243 	wpid = waitpid(fpid, &status, 0);
2244 	ATF_REQUIRE(wpid == fpid);
2245 	ATF_REQUIRE(WIFSTOPPED(status));
2246 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2247 
2248 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2249 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2250 
2251 	/* The second wait() should report a system call entry for getpid(). */
2252 	wpid = waitpid(fpid, &status, 0);
2253 	ATF_REQUIRE(wpid == fpid);
2254 	ATF_REQUIRE(WIFSTOPPED(status));
2255 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2256 
2257 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2258 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2259 
2260 	/* Continue the child process with a signal. */
2261 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2262 
2263 	/* The third wait() should report a system call exit for getpid(). */
2264 	wpid = waitpid(fpid, &status, 0);
2265 	ATF_REQUIRE(wpid == fpid);
2266 	ATF_REQUIRE(WIFSTOPPED(status));
2267 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2268 
2269 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2270 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2271 
2272 	/* Continue the child process with a signal. */
2273 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2274 
2275 	for (;;) {
2276 		/*
2277 		 * The last wait() should report exit 2, i.e., a normal _exit
2278 		 * from the signal handler. In the meantime, catch and proceed
2279 		 * past any syscall stops.
2280 		 */
2281 		wpid = waitpid(fpid, &status, 0);
2282 		ATF_REQUIRE(wpid == fpid);
2283 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2284 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2285 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2286 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2287 		} else {
2288 			ATF_REQUIRE(WIFEXITED(status));
2289 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2290 			break;
2291 		}
2292 	}
2293 
2294 	wpid = wait(&status);
2295 	ATF_REQUIRE(wpid == -1);
2296 	ATF_REQUIRE(errno == ECHILD);
2297 }
2298 
2299 /*
2300  * Verify that even if the signal queue is full for a child process,
2301  * a PT_CONTINUE with a signal will not result in loss of that signal.
2302  */
2303 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2304 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2305 {
2306 	pid_t fpid, wpid;
2307 	int status;
2308 	int max_pending_per_proc;
2309 	size_t len;
2310 	int i;
2311 
2312 	ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2313 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2314 
2315 	ATF_REQUIRE((fpid = fork()) != -1);
2316 	if (fpid == 0) {
2317 		trace_me();
2318 		exit(1);
2319 	}
2320 
2321 	/* The first wait() should report the stop from SIGSTOP. */
2322 	wpid = waitpid(fpid, &status, 0);
2323 	ATF_REQUIRE(wpid == fpid);
2324 	ATF_REQUIRE(WIFSTOPPED(status));
2325 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2326 
2327 	len = sizeof(max_pending_per_proc);
2328 	ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2329 	    &max_pending_per_proc, &len, NULL, 0) == 0);
2330 
2331 	/* Fill the signal queue. */
2332 	for (i = 0; i < max_pending_per_proc; ++i)
2333 		ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2334 
2335 	/* Continue with signal. */
2336 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2337 
2338 	for (;;) {
2339 		wpid = waitpid(fpid, &status, 0);
2340 		ATF_REQUIRE(wpid == fpid);
2341 		if (WIFSTOPPED(status)) {
2342 			ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2343 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2344 		} else {
2345 			/*
2346 			 * The last wait() should report normal _exit from the
2347 			 * SIGUSR1 handler.
2348 			 */
2349 			ATF_REQUIRE(WIFEXITED(status));
2350 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2351 			break;
2352 		}
2353 	}
2354 
2355 	wpid = wait(&status);
2356 	ATF_REQUIRE(wpid == -1);
2357 	ATF_REQUIRE(errno == ECHILD);
2358 }
2359 
2360 /*
2361  * Verify that, after stopping due to a signal, that signal can be
2362  * replaced with another signal.
2363  */
2364 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2365 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2366 {
2367 	struct ptrace_lwpinfo pl;
2368 	pid_t fpid, wpid;
2369 	int status;
2370 
2371 	ATF_REQUIRE((fpid = fork()) != -1);
2372 	if (fpid == 0) {
2373 		trace_me();
2374 		sleep(20);
2375 		exit(1);
2376 	}
2377 
2378 	/* The first wait() should report the stop from SIGSTOP. */
2379 	wpid = waitpid(fpid, &status, 0);
2380 	ATF_REQUIRE(wpid == fpid);
2381 	ATF_REQUIRE(WIFSTOPPED(status));
2382 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2383 
2384 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2385 
2386 	/* Send a signal without ptrace. */
2387 	ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2388 
2389 	/* The second wait() should report a SIGINT was received. */
2390 	wpid = waitpid(fpid, &status, 0);
2391 	ATF_REQUIRE(wpid == fpid);
2392 	ATF_REQUIRE(WIFSTOPPED(status));
2393 	ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2394 
2395 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2396 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2397 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2398 
2399 	/* Continue the child process with a different signal. */
2400 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2401 
2402 	/*
2403 	 * The last wait() should report having died due to the new
2404 	 * signal, SIGTERM.
2405 	 */
2406 	wpid = waitpid(fpid, &status, 0);
2407 	ATF_REQUIRE(wpid == fpid);
2408 	ATF_REQUIRE(WIFSIGNALED(status));
2409 	ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2410 
2411 	wpid = wait(&status);
2412 	ATF_REQUIRE(wpid == -1);
2413 	ATF_REQUIRE(errno == ECHILD);
2414 }
2415 
2416 /*
2417  * Verify that a signal can be passed through to the child even when there
2418  * was no true signal originally. Such cases arise when a SIGTRAP is
2419  * invented for e.g, system call stops.
2420  */
2421 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2422 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2423 {
2424 	struct ptrace_lwpinfo pl;
2425 	pid_t fpid, wpid;
2426 	int status;
2427 
2428 	ATF_REQUIRE((fpid = fork()) != -1);
2429 	if (fpid == 0) {
2430 		trace_me();
2431 		getpid();
2432 		exit(1);
2433 	}
2434 
2435 	/* The first wait() should report the stop from SIGSTOP. */
2436 	wpid = waitpid(fpid, &status, 0);
2437 	ATF_REQUIRE(wpid == fpid);
2438 	ATF_REQUIRE(WIFSTOPPED(status));
2439 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2440 
2441 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2442 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2443 
2444 	/* The second wait() should report a system call entry for getpid(). */
2445 	wpid = waitpid(fpid, &status, 0);
2446 	ATF_REQUIRE(wpid == fpid);
2447 	ATF_REQUIRE(WIFSTOPPED(status));
2448 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2449 
2450 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2451 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2452 
2453 	/* Continue the child process with a SIGTRAP. */
2454 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2455 
2456 	for (;;) {
2457 		/*
2458 		 * The last wait() should report exit due to SIGTRAP.  In the
2459 		 * meantime, catch and proceed past any syscall stops.
2460 		 */
2461 		wpid = waitpid(fpid, &status, 0);
2462 		ATF_REQUIRE(wpid == fpid);
2463 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2464 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2465 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2466 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2467 		} else {
2468 			ATF_REQUIRE(WIFSIGNALED(status));
2469 			ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2470 			break;
2471 		}
2472 	}
2473 
2474 	wpid = wait(&status);
2475 	ATF_REQUIRE(wpid == -1);
2476 	ATF_REQUIRE(errno == ECHILD);
2477 
2478 }
2479 
2480 /*
2481  * A mixed bag PT_CONTINUE with signal test.
2482  */
2483 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2484 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2485 {
2486 	struct ptrace_lwpinfo pl;
2487 	pid_t fpid, wpid;
2488 	int status;
2489 
2490 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2491 
2492 	ATF_REQUIRE((fpid = fork()) != -1);
2493 	if (fpid == 0) {
2494 		trace_me();
2495 		getpid();
2496 		exit(1);
2497 	}
2498 
2499 	/* The first wait() should report the stop from SIGSTOP. */
2500 	wpid = waitpid(fpid, &status, 0);
2501 	ATF_REQUIRE(wpid == fpid);
2502 	ATF_REQUIRE(WIFSTOPPED(status));
2503 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2504 
2505 	/* Continue the child ignoring the SIGSTOP and tracing system calls. */
2506 	ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2507 
2508 	/* The second wait() should report a system call entry for getpid(). */
2509 	wpid = waitpid(fpid, &status, 0);
2510 	ATF_REQUIRE(wpid == fpid);
2511 	ATF_REQUIRE(WIFSTOPPED(status));
2512 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2513 
2514 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2515 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2516 
2517 	/* Continue with the first SIGUSR1. */
2518 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2519 
2520 	/* The next wait() should report a system call exit for getpid(). */
2521 	wpid = waitpid(fpid, &status, 0);
2522 	ATF_REQUIRE(wpid == fpid);
2523 	ATF_REQUIRE(WIFSTOPPED(status));
2524 	ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2525 
2526 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2527 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2528 
2529 	/* Send an ABRT without ptrace. */
2530 	ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2531 
2532 	/* Continue normally. */
2533 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2534 
2535 	/* The next wait() should report the SIGABRT. */
2536 	wpid = waitpid(fpid, &status, 0);
2537 	ATF_REQUIRE(wpid == fpid);
2538 	ATF_REQUIRE(WIFSTOPPED(status));
2539 	ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2540 
2541 	ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2542 	ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2543 	ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2544 
2545 	/* Continue, replacing the SIGABRT with another SIGUSR1. */
2546 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2547 
2548 	for (;;) {
2549 		/*
2550 		 * The last wait() should report exit 2, i.e., a normal _exit
2551 		 * from the signal handler. In the meantime, catch and proceed
2552 		 * past any syscall stops.
2553 		 */
2554 		wpid = waitpid(fpid, &status, 0);
2555 		ATF_REQUIRE(wpid == fpid);
2556 		if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2557 			ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2558 			ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2559 			ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2560 		} else {
2561 			ATF_REQUIRE(WIFEXITED(status));
2562 			ATF_REQUIRE(WEXITSTATUS(status) == 2);
2563 			break;
2564 		}
2565 	}
2566 
2567 	wpid = wait(&status);
2568 	ATF_REQUIRE(wpid == -1);
2569 	ATF_REQUIRE(errno == ECHILD);
2570 
2571 }
2572 
2573 /*
2574  * Verify a signal delivered by ptrace is noticed by kevent(2).
2575  */
2576 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2577 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2578 {
2579 	pid_t fpid, wpid;
2580 	int status, kq, nevents;
2581 	struct kevent kev;
2582 
2583 	ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2584 
2585 	ATF_REQUIRE((fpid = fork()) != -1);
2586 	if (fpid == 0) {
2587 		CHILD_REQUIRE((kq = kqueue()) > 0);
2588 		EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2589 		CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2590 
2591 		trace_me();
2592 
2593 		for (;;) {
2594 			nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2595 			if (nevents == -1 && errno == EINTR)
2596 				continue;
2597 			CHILD_REQUIRE(nevents > 0);
2598 			CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2599 			CHILD_REQUIRE(kev.ident == SIGUSR1);
2600 			break;
2601 		}
2602 
2603 		exit(1);
2604 	}
2605 
2606 	/* The first wait() should report the stop from SIGSTOP. */
2607 	wpid = waitpid(fpid, &status, 0);
2608 	ATF_REQUIRE(wpid == fpid);
2609 	ATF_REQUIRE(WIFSTOPPED(status));
2610 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2611 
2612 	/* Continue with the SIGUSR1. */
2613 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2614 
2615 	/*
2616 	 * The last wait() should report normal exit with code 1.
2617 	 */
2618 	wpid = waitpid(fpid, &status, 0);
2619 	ATF_REQUIRE(wpid == fpid);
2620 	ATF_REQUIRE(WIFEXITED(status));
2621 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2622 
2623 	wpid = wait(&status);
2624 	ATF_REQUIRE(wpid == -1);
2625 	ATF_REQUIRE(errno == ECHILD);
2626 }
2627 
2628 static sem_t sigusr1_sem;
2629 
2630 static void
2631 sigusr1_sempost_handler(int sig __unused)
2632 {
2633 
2634 	CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2635 }
2636 
2637 static void *
2638 signal_thread(void *arg)
2639 {
2640 	int err;
2641 	sigset_t sigmask;
2642 
2643 	pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2644 
2645 	/* Wait for this thread to receive a SIGUSR1. */
2646 	do {
2647 		err = sem_wait(&sigusr1_sem);
2648 		CHILD_REQUIRE(err == 0 || errno == EINTR);
2649 	} while (err != 0 && errno == EINTR);
2650 
2651 	/* Free our companion thread from the barrier. */
2652 	pthread_barrier_wait(pbarrier);
2653 
2654 	/*
2655 	 * Swap ignore duties; the next SIGUSR1 should go to the
2656 	 * other thread.
2657 	 */
2658 	CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2659 	CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2660 	CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2661 
2662 	/* Sync up threads after swapping signal masks. */
2663 	pthread_barrier_wait(pbarrier);
2664 
2665 	/* Wait until our companion has received its SIGUSR1. */
2666 	pthread_barrier_wait(pbarrier);
2667 
2668 	return (NULL);
2669 }
2670 
2671 /*
2672  * Verify that if ptrace stops due to a signal but continues with
2673  * a different signal that the new signal is routed to a thread
2674  * that can accept it, and that that thread is awakened by the signal
2675  * in a timely manner.
2676  */
2677 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2678 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2679 {
2680 	pid_t fpid, wpid;
2681 	int status, err;
2682 	pthread_t t;
2683 	sigset_t sigmask;
2684 	pthread_barrier_t barrier;
2685 
2686 	ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2687 	ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2688 	ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2689 
2690 	ATF_REQUIRE((fpid = fork()) != -1);
2691 	if (fpid == 0) {
2692 		CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2693 
2694 		/* The other thread should receive the first SIGUSR1. */
2695 		CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2696 		CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2697 		CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2698 
2699 		trace_me();
2700 
2701 		/* Wait until other thread has received its SIGUSR1. */
2702 		pthread_barrier_wait(&barrier);
2703 
2704 		/*
2705 		 * Swap ignore duties; the next SIGUSR1 should go to this
2706 		 * thread.
2707 		 */
2708 		CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2709 
2710 		/* Sync up threads after swapping signal masks. */
2711 		pthread_barrier_wait(&barrier);
2712 
2713 		/*
2714 		 * Sync up with test code; we're ready for the next SIGUSR1
2715 		 * now.
2716 		 */
2717 		raise(SIGSTOP);
2718 
2719 		/* Wait for this thread to receive a SIGUSR1. */
2720 		do {
2721 			err = sem_wait(&sigusr1_sem);
2722 			CHILD_REQUIRE(err == 0 || errno == EINTR);
2723 		} while (err != 0 && errno == EINTR);
2724 
2725 		/* Free the other thread from the barrier. */
2726 		pthread_barrier_wait(&barrier);
2727 
2728 		CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2729 
2730 		exit(1);
2731 	}
2732 
2733 	/* The first wait() should report the stop from SIGSTOP. */
2734 	wpid = waitpid(fpid, &status, 0);
2735 	ATF_REQUIRE(wpid == fpid);
2736 	ATF_REQUIRE(WIFSTOPPED(status));
2737 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2738 
2739 	/* Continue the child ignoring the SIGSTOP. */
2740 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2741 
2742 	/*
2743 	 * Send a signal without ptrace that either thread will accept (USR2,
2744 	 * in this case).
2745 	 */
2746 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2747 
2748 	/* The second wait() should report a SIGUSR2 was received. */
2749 	wpid = waitpid(fpid, &status, 0);
2750 	ATF_REQUIRE(wpid == fpid);
2751 	ATF_REQUIRE(WIFSTOPPED(status));
2752 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2753 
2754 	/* Continue the child, changing the signal to USR1. */
2755 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2756 
2757 	/* The next wait() should report the stop from SIGSTOP. */
2758 	wpid = waitpid(fpid, &status, 0);
2759 	ATF_REQUIRE(wpid == fpid);
2760 	ATF_REQUIRE(WIFSTOPPED(status));
2761 	ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2762 
2763 	/* Continue the child ignoring the SIGSTOP. */
2764 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2765 
2766 	ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2767 
2768 	/* The next wait() should report a SIGUSR2 was received. */
2769 	wpid = waitpid(fpid, &status, 0);
2770 	ATF_REQUIRE(wpid == fpid);
2771 	ATF_REQUIRE(WIFSTOPPED(status));
2772 	ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2773 
2774 	/* Continue the child, changing the signal to USR1. */
2775 	ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2776 
2777 	/* The last wait() should report normal exit with code 1. */
2778 	wpid = waitpid(fpid, &status, 0);
2779 	ATF_REQUIRE(wpid == fpid);
2780 	ATF_REQUIRE(WIFEXITED(status));
2781 	ATF_REQUIRE(WEXITSTATUS(status) == 1);
2782 
2783 	wpid = wait(&status);
2784 	ATF_REQUIRE(wpid == -1);
2785 	ATF_REQUIRE(errno == ECHILD);
2786 }
2787 
2788 ATF_TP_ADD_TCS(tp)
2789 {
2790 
2791 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2792 	ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
2793 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
2794 	ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
2795 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
2796 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
2797 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
2798 	ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
2799 	ATF_TP_ADD_TC(tp,
2800 	    ptrace__follow_fork_child_detached_unrelated_debugger);
2801 	ATF_TP_ADD_TC(tp,
2802 	    ptrace__follow_fork_parent_detached_unrelated_debugger);
2803 	ATF_TP_ADD_TC(tp, ptrace__getppid);
2804 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2805 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2806 	ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
2807 	ATF_TP_ADD_TC(tp, ptrace__lwp_events);
2808 	ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
2809 	ATF_TP_ADD_TC(tp, ptrace__siginfo);
2810 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
2811 	ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
2812 	ATF_TP_ADD_TC(tp, ptrace__event_mask);
2813 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2814 	ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2815 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
2816 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2817 #endif
2818 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
2819 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
2820 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
2821 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
2822 	ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
2823 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
2824 	ATF_TP_ADD_TC(tp,
2825 	    ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2826 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2827 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
2828 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2829 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
2830 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
2831 	ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2832 
2833 	return (atf_no_error());
2834 }
2835