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