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