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