xref: /netbsd-src/tests/lib/libc/sys/t_ptrace_wait.c (revision fc4f42693f9b1c31f39f9cf50af1bf2010325808)
1 /*	$NetBSD: t_ptrace_wait.c,v 1.31 2018/04/11 01:52:59 kamil Exp $	*/
2 
3 /*-
4  * Copyright (c) 2016 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  * POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __RCSID("$NetBSD: t_ptrace_wait.c,v 1.31 2018/04/11 01:52:59 kamil Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/ptrace.h>
35 #include <sys/resource.h>
36 #include <sys/stat.h>
37 #include <sys/syscall.h>
38 #include <sys/sysctl.h>
39 #include <sys/wait.h>
40 #include <machine/reg.h>
41 #include <elf.h>
42 #include <err.h>
43 #include <errno.h>
44 #include <lwp.h>
45 #include <sched.h>
46 #include <signal.h>
47 #include <stdint.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <strings.h>
51 #include <time.h>
52 #include <unistd.h>
53 
54 #include <atf-c.h>
55 
56 #include "h_macros.h"
57 
58 #include "t_ptrace_wait.h"
59 #include "msg.h"
60 
61 #define PARENT_TO_CHILD(info, fds, msg) \
62     SYSCALL_REQUIRE(msg_write_child(info " to child " # fds, &fds, &msg, sizeof(msg)) == 0)
63 
64 #define CHILD_FROM_PARENT(info, fds, msg) \
65     FORKEE_ASSERT(msg_read_parent(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
66 
67 #define CHILD_TO_PARENT(info, fds, msg) \
68     FORKEE_ASSERT(msg_write_parent(info " to parent " # fds, &fds, &msg, sizeof(msg)) == 0)
69 
70 #define PARENT_FROM_CHILD(info, fds, msg) \
71     SYSCALL_REQUIRE(msg_read_child(info " from parent " # fds, &fds, &msg, sizeof(msg)) == 0)
72 
73 #define SYSCALL_REQUIRE(expr) ATF_REQUIRE_MSG(expr, "%s: %s", # expr, \
74     strerror(errno))
75 #define SYSCALL_REQUIRE_ERRNO(res, exp) ATF_REQUIRE_MSG(res == exp, \
76     "%d(%s) != %d", res, strerror(res), exp)
77 
78 static int debug = 0;
79 
80 #define DPRINTF(a, ...)	do  \
81 	if (debug) printf(a,  ##__VA_ARGS__); \
82     while (/*CONSTCOND*/0)
83 
84 
85 ATF_TC(traceme1);
86 ATF_TC_HEAD(traceme1, tc)
87 {
88 	atf_tc_set_md_var(tc, "descr",
89 	    "Verify SIGSTOP followed by _exit(2) in a child");
90 }
91 
92 ATF_TC_BODY(traceme1, tc)
93 {
94 	const int exitval = 5;
95 	const int sigval = SIGSTOP;
96 	pid_t child, wpid;
97 #if defined(TWAIT_HAVE_STATUS)
98 	int status;
99 #endif
100 
101 	DPRINTF("Before forking process PID=%d\n", getpid());
102 	SYSCALL_REQUIRE((child = fork()) != -1);
103 	if (child == 0) {
104 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
105 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
106 
107 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
108 		FORKEE_ASSERT(raise(sigval) == 0);
109 
110 		DPRINTF("Before exiting of the child process\n");
111 		_exit(exitval);
112 	}
113 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
114 
115 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
116 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
117 
118 	validate_status_stopped(status, sigval);
119 
120 	DPRINTF("Before resuming the child process where it left off and "
121 	    "without signal to be sent\n");
122 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
123 
124 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
125 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
126 
127 	validate_status_exited(status, exitval);
128 
129 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
130 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
131 }
132 
133 ATF_TC(traceme2);
134 ATF_TC_HEAD(traceme2, tc)
135 {
136 	atf_tc_set_md_var(tc, "descr",
137 	    "Verify that a signal emitted by a tracer to a child is caught by "
138 	    "a signal handler");
139 }
140 
141 static int traceme2_caught = 0;
142 
143 static void
144 traceme2_sighandler(int sig)
145 {
146 	FORKEE_ASSERT_EQ(sig, SIGINT);
147 
148 	++traceme2_caught;
149 }
150 
151 ATF_TC_BODY(traceme2, tc)
152 {
153 	const int exitval = 5;
154 	const int sigval = SIGSTOP, sigsent = SIGINT;
155 	pid_t child, wpid;
156 	struct sigaction sa;
157 #if defined(TWAIT_HAVE_STATUS)
158 	int status;
159 #endif
160 
161 	DPRINTF("Before forking process PID=%d\n", getpid());
162 	SYSCALL_REQUIRE((child = fork()) != -1);
163 	if (child == 0) {
164 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
165 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
166 
167 		sa.sa_handler = traceme2_sighandler;
168 		sa.sa_flags = SA_SIGINFO;
169 		sigemptyset(&sa.sa_mask);
170 
171 		FORKEE_ASSERT(sigaction(sigsent, &sa, NULL) != -1);
172 
173 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
174 		FORKEE_ASSERT(raise(sigval) == 0);
175 
176 		FORKEE_ASSERT_EQ(traceme2_caught, 1);
177 
178 		DPRINTF("Before exiting of the child process\n");
179 		_exit(exitval);
180 	}
181 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
182 
183 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
184 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
185 
186 	validate_status_stopped(status, sigval);
187 
188 	DPRINTF("Before resuming the child process where it left off and with "
189 	    "signal %s to be sent\n", strsignal(sigsent));
190 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
191 
192 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
193 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
194 
195 	validate_status_exited(status, exitval);
196 
197 	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
198 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
199 }
200 
201 ATF_TC(traceme3);
202 ATF_TC_HEAD(traceme3, tc)
203 {
204 	atf_tc_set_md_var(tc, "descr",
205 	    "Verify SIGSTOP followed by termination by a signal in a child");
206 }
207 
208 ATF_TC_BODY(traceme3, tc)
209 {
210 	const int sigval = SIGSTOP, sigsent = SIGINT /* Without core-dump */;
211 	pid_t child, wpid;
212 #if defined(TWAIT_HAVE_STATUS)
213 	int status;
214 #endif
215 
216 	DPRINTF("Before forking process PID=%d\n", getpid());
217 	SYSCALL_REQUIRE((child = fork()) != -1);
218 	if (child == 0) {
219 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
220 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
221 
222 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
223 		FORKEE_ASSERT(raise(sigval) == 0);
224 
225 		/* NOTREACHED */
226 		FORKEE_ASSERTX(0 &&
227 		    "Child should be terminated by a signal from its parent");
228 	}
229 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
230 
231 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
232 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
233 
234 	validate_status_stopped(status, sigval);
235 
236 	DPRINTF("Before resuming the child process where it left off and with "
237 	    "signal %s to be sent\n", strsignal(sigsent));
238 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
239 
240 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
241 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
242 
243 	validate_status_signaled(status, sigsent, 0);
244 
245 	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
246 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
247 }
248 
249 ATF_TC(traceme4);
250 ATF_TC_HEAD(traceme4, tc)
251 {
252 	atf_tc_set_md_var(tc, "descr",
253 	    "Verify SIGSTOP followed by SIGCONT and _exit(2) in a child");
254 }
255 
256 ATF_TC_BODY(traceme4, tc)
257 {
258 	const int exitval = 5;
259 	const int sigval = SIGSTOP, sigsent = SIGCONT;
260 	pid_t child, wpid;
261 #if defined(TWAIT_HAVE_STATUS)
262 	int status;
263 #endif
264 
265 	DPRINTF("Before forking process PID=%d\n", getpid());
266 	SYSCALL_REQUIRE((child = fork()) != -1);
267 	if (child == 0) {
268 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
269 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
270 
271 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
272 		FORKEE_ASSERT(raise(sigval) == 0);
273 
274 		DPRINTF("Before raising %s from child\n", strsignal(sigsent));
275 		FORKEE_ASSERT(raise(sigsent) == 0);
276 
277 		DPRINTF("Before exiting of the child process\n");
278 		_exit(exitval);
279 	}
280 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(),child);
281 
282 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
283 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
284 
285 	validate_status_stopped(status, sigval);
286 
287 	DPRINTF("Before resuming the child process where it left off and "
288 	    "without signal to be sent\n");
289 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
290 
291 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
292 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
293 
294 	validate_status_stopped(status, sigsent);
295 
296 	DPRINTF("Before resuming the child process where it left off and "
297 	    "without signal to be sent\n");
298 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
299 
300 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
301 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
302 
303 	validate_status_exited(status, exitval);
304 
305 	DPRINTF("Before calling %s() for the exited child\n", TWAIT_FNAME);
306 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
307 }
308 
309 #if defined(TWAIT_HAVE_PID)
310 ATF_TC(attach1);
311 ATF_TC_HEAD(attach1, tc)
312 {
313 	atf_tc_set_md_var(tc, "descr",
314 	    "Assert that tracer sees process termination before the parent");
315 }
316 
317 static void
318 attach1_raw(bool raw)
319 {
320 	struct msg_fds parent_tracee, parent_tracer;
321 	const int exitval_tracee = 5;
322 	const int exitval_tracer = 10;
323 	pid_t tracee, tracer, wpid;
324 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
325 #if defined(TWAIT_HAVE_STATUS)
326 	int status;
327 #endif
328 
329 	DPRINTF("Spawn tracee\n");
330 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
331 	tracee = atf_utils_fork();
332 	if (tracee == 0) {
333 		// Wait for parent to let us exit
334 		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
335 		_exit(exitval_tracee);
336 	}
337 
338 	DPRINTF("Spawn debugger\n");
339 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
340 	tracer = atf_utils_fork();
341 	if (tracer == 0) {
342 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
343 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
344 
345 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
346 		FORKEE_REQUIRE_SUCCESS(
347 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
348 
349 		forkee_status_stopped(status, SIGSTOP);
350 
351 		/* Resume tracee with PT_CONTINUE */
352 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
353 
354 		/* Inform parent that tracer has attached to tracee */
355 		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
356 
357 		/* Wait for parent to tell use that tracee should have exited */
358 		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
359 
360 		/* Wait for tracee and assert that it exited */
361 		FORKEE_REQUIRE_SUCCESS(
362 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
363 
364 		forkee_status_exited(status, exitval_tracee);
365 		DPRINTF("Tracee %d exited with %d\n", tracee, exitval_tracee);
366 
367 		DPRINTF("Before exiting of the tracer process\n");
368 		_exit(exitval_tracer);
369 	}
370 
371 	DPRINTF("Wait for the tracer to attach to the tracee\n");
372 	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
373 
374 	DPRINTF("Resume the tracee and let it exit\n");
375 	PARENT_TO_CHILD("exit tracee", parent_tracee,  msg);
376 
377 	DPRINTF("Detect that tracee is zombie\n");
378 	if (raw)
379 		await_zombie_raw(tracee, 0);
380 	else
381 		await_zombie(tracee);
382 
383 	DPRINTF("Assert that there is no status about tracee %d - "
384 	    "Tracer must detect zombie first - calling %s()\n", tracee,
385 	    TWAIT_FNAME);
386 	TWAIT_REQUIRE_SUCCESS(
387 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
388 
389 	DPRINTF("Tell the tracer child should have exited\n");
390 	PARENT_TO_CHILD("wait for tracee exit", parent_tracer,  msg);
391 	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
392 	    TWAIT_FNAME);
393 
394 	DPRINTF("Wait from tracer child to complete waiting for tracee\n");
395 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
396 	    tracer);
397 
398 	validate_status_exited(status, exitval_tracer);
399 
400 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
401 	    TWAIT_FNAME);
402 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
403 	    tracee);
404 
405 	validate_status_exited(status, exitval_tracee);
406 
407 	msg_close(&parent_tracer);
408 	msg_close(&parent_tracee);
409 }
410 
411 ATF_TC_BODY(attach1, tc)
412 {
413 
414 	/* Reuse this test with race1 */
415 	attach1_raw(false);
416 }
417 
418 #endif
419 
420 #if defined(TWAIT_HAVE_PID)
421 ATF_TC(attach2);
422 ATF_TC_HEAD(attach2, tc)
423 {
424 	atf_tc_set_md_var(tc, "descr",
425 	    "Assert that any tracer sees process termination before its "
426 	    "parent");
427 }
428 
429 ATF_TC_BODY(attach2, tc)
430 {
431 	struct msg_fds parent_tracer, parent_tracee;
432 	const int exitval_tracee = 5;
433 	const int exitval_tracer1 = 10, exitval_tracer2 = 20;
434 	pid_t tracee, tracer, wpid;
435 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
436 #if defined(TWAIT_HAVE_STATUS)
437 	int status;
438 #endif
439 
440 	DPRINTF("Spawn tracee\n");
441 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
442 	tracee = atf_utils_fork();
443 	if (tracee == 0) {
444 		/* Wait for message from the parent */
445 		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
446 		_exit(exitval_tracee);
447 	}
448 
449 	DPRINTF("Spawn debugger\n");
450 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
451 	tracer = atf_utils_fork();
452 	if (tracer == 0) {
453 		/* Fork again and drop parent to reattach to PID 1 */
454 		tracer = atf_utils_fork();
455 		if (tracer != 0)
456 			_exit(exitval_tracer1);
457 
458 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
459 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
460 
461 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
462 		FORKEE_REQUIRE_SUCCESS(
463 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
464 
465 		forkee_status_stopped(status, SIGSTOP);
466 
467 		/* Resume tracee with PT_CONTINUE */
468 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
469 
470 		/* Inform parent that tracer has attached to tracee */
471 		CHILD_TO_PARENT("Message 1", parent_tracer, msg);
472 		CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
473 
474 		/* Wait for tracee and assert that it exited */
475 		FORKEE_REQUIRE_SUCCESS(
476 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
477 
478 		forkee_status_exited(status, exitval_tracee);
479 
480 		DPRINTF("Before exiting of the tracer process\n");
481 		_exit(exitval_tracer2);
482 	}
483 	DPRINTF("Wait for the tracer process (direct child) to exit calling "
484 	    "%s()\n", TWAIT_FNAME);
485 	TWAIT_REQUIRE_SUCCESS(
486 	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
487 
488 	validate_status_exited(status, exitval_tracer1);
489 
490 	DPRINTF("Wait for the non-exited tracee process with %s()\n",
491 	    TWAIT_FNAME);
492 	TWAIT_REQUIRE_SUCCESS(
493 	    wpid = TWAIT_GENERIC(tracee, NULL, WNOHANG), 0);
494 
495 	DPRINTF("Wait for the tracer to attach to the tracee\n");
496 	PARENT_FROM_CHILD("Message 1", parent_tracer, msg);
497 	DPRINTF("Resume the tracee and let it exit\n");
498 	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
499 
500 	DPRINTF("Detect that tracee is zombie\n");
501 	await_zombie(tracee);
502 
503 	DPRINTF("Assert that there is no status about tracee - "
504 	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
505 	TWAIT_REQUIRE_SUCCESS(
506 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
507 
508 	DPRINTF("Resume the tracer and let it detect exited tracee\n");
509 	PARENT_TO_CHILD("Message 2", parent_tracer, msg);
510 
511 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
512 	    TWAIT_FNAME);
513 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, 0),
514 	    tracee);
515 
516 	validate_status_exited(status, exitval_tracee);
517 
518 	msg_close(&parent_tracer);
519 	msg_close(&parent_tracee);
520 }
521 #endif
522 
523 ATF_TC(attach3);
524 ATF_TC_HEAD(attach3, tc)
525 {
526 	atf_tc_set_md_var(tc, "descr",
527 	    "Assert that tracer parent can PT_ATTACH to its child");
528 }
529 
530 ATF_TC_BODY(attach3, tc)
531 {
532 	struct msg_fds parent_tracee;
533 	const int exitval_tracee = 5;
534 	pid_t tracee, wpid;
535 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
536 #if defined(TWAIT_HAVE_STATUS)
537 	int status;
538 #endif
539 
540 	DPRINTF("Spawn tracee\n");
541 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
542 	tracee = atf_utils_fork();
543 	if (tracee == 0) {
544 		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
545 		DPRINTF("Parent should now attach to tracee\n");
546 
547 		CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
548 		/* Wait for message from the parent */
549 		_exit(exitval_tracee);
550 	}
551 	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
552 
553 	DPRINTF("Before calling PT_ATTACH for tracee %d\n", tracee);
554 	SYSCALL_REQUIRE(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
555 
556 	DPRINTF("Wait for the stopped tracee process with %s()\n",
557 	    TWAIT_FNAME);
558 	TWAIT_REQUIRE_SUCCESS(
559 	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
560 
561 	validate_status_stopped(status, SIGSTOP);
562 
563 	DPRINTF("Resume tracee with PT_CONTINUE\n");
564 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
565 
566 	DPRINTF("Let the tracee exit now\n");
567 	PARENT_TO_CHILD("Message 2", parent_tracee, msg);
568 
569 	DPRINTF("Wait for tracee to exit with %s()\n", TWAIT_FNAME);
570 	TWAIT_REQUIRE_SUCCESS(
571 	    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
572 
573 	validate_status_exited(status, exitval_tracee);
574 
575 	DPRINTF("Before calling %s() for tracee\n", TWAIT_FNAME);
576 	TWAIT_REQUIRE_FAILURE(ECHILD,
577 	    wpid = TWAIT_GENERIC(tracee, &status, 0));
578 
579 	msg_close(&parent_tracee);
580 }
581 
582 ATF_TC(attach4);
583 ATF_TC_HEAD(attach4, tc)
584 {
585 	atf_tc_set_md_var(tc, "descr",
586 	    "Assert that tracer child can PT_ATTACH to its parent");
587 }
588 
589 ATF_TC_BODY(attach4, tc)
590 {
591 	struct msg_fds parent_tracee;
592 	const int exitval_tracer = 5;
593 	pid_t tracer, wpid;
594 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
595 #if defined(TWAIT_HAVE_STATUS)
596 	int status;
597 #endif
598 
599 	DPRINTF("Spawn tracer\n");
600 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
601 	tracer = atf_utils_fork();
602 	if (tracer == 0) {
603 
604 		/* Wait for message from the parent */
605 		CHILD_FROM_PARENT("Message 1", parent_tracee, msg);
606 
607 		DPRINTF("Attach to parent PID %d with PT_ATTACH from child\n",
608 		    getppid());
609 		FORKEE_ASSERT(ptrace(PT_ATTACH, getppid(), NULL, 0) != -1);
610 
611 		DPRINTF("Wait for the stopped parent process with %s()\n",
612 		    TWAIT_FNAME);
613 		FORKEE_REQUIRE_SUCCESS(
614 		    wpid = TWAIT_GENERIC(getppid(), &status, 0), getppid());
615 
616 		forkee_status_stopped(status, SIGSTOP);
617 
618 		DPRINTF("Resume parent with PT_DETACH\n");
619 		FORKEE_ASSERT(ptrace(PT_DETACH, getppid(), (void *)1, 0)
620 		    != -1);
621 
622 		/* Tell parent we are ready */
623 		CHILD_TO_PARENT("Message 1", parent_tracee, msg);
624 
625 		_exit(exitval_tracer);
626 	}
627 
628 	DPRINTF("Wait for the tracer to become ready\n");
629 	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
630 	DPRINTF("Allow the tracer to exit now\n");
631 	PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
632 
633 	DPRINTF("Wait for tracer to exit with %s()\n", TWAIT_FNAME);
634 	TWAIT_REQUIRE_SUCCESS(
635 	    wpid = TWAIT_GENERIC(tracer, &status, 0), tracer);
636 
637 	validate_status_exited(status, exitval_tracer);
638 
639 	DPRINTF("Before calling %s() for tracer\n", TWAIT_FNAME);
640 	TWAIT_REQUIRE_FAILURE(ECHILD,
641 	    wpid = TWAIT_GENERIC(tracer, &status, 0));
642 
643 	msg_close(&parent_tracee);
644 }
645 
646 #if defined(TWAIT_HAVE_PID)
647 ATF_TC(attach5);
648 ATF_TC_HEAD(attach5, tc)
649 {
650 	atf_tc_set_md_var(tc, "descr",
651 	    "Assert that tracer sees its parent when attached to tracer "
652 	    "(check getppid(2))");
653 }
654 
655 ATF_TC_BODY(attach5, tc)
656 {
657 	struct msg_fds parent_tracer, parent_tracee;
658 	const int exitval_tracee = 5;
659 	const int exitval_tracer = 10;
660 	pid_t parent, tracee, tracer, wpid;
661 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
662 #if defined(TWAIT_HAVE_STATUS)
663 	int status;
664 #endif
665 
666 	DPRINTF("Spawn tracee\n");
667 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
668 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
669 	tracee = atf_utils_fork();
670 	if (tracee == 0) {
671 		parent = getppid();
672 
673 		/* Emit message to the parent */
674 		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
675 		CHILD_FROM_PARENT("exit tracee", parent_tracee, msg);
676 
677 		FORKEE_ASSERT_EQ(parent, getppid());
678 
679 		_exit(exitval_tracee);
680 	}
681 	DPRINTF("Wait for child to record its parent identifier (pid)\n");
682 	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
683 
684 	DPRINTF("Spawn debugger\n");
685 	tracer = atf_utils_fork();
686 	if (tracer == 0) {
687 		/* No IPC to communicate with the child */
688 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
689 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
690 
691 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
692 		FORKEE_REQUIRE_SUCCESS(
693 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
694 
695 		forkee_status_stopped(status, SIGSTOP);
696 
697 		/* Resume tracee with PT_CONTINUE */
698 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
699 
700 		/* Inform parent that tracer has attached to tracee */
701 		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
702 
703 		/* Wait for parent to tell use that tracee should have exited */
704 		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
705 
706 		/* Wait for tracee and assert that it exited */
707 		FORKEE_REQUIRE_SUCCESS(
708 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
709 
710 		forkee_status_exited(status, exitval_tracee);
711 
712 		DPRINTF("Before exiting of the tracer process\n");
713 		_exit(exitval_tracer);
714 	}
715 
716 	DPRINTF("Wait for the tracer to attach to the tracee\n");
717 	PARENT_FROM_CHILD("tracer ready",  parent_tracer, msg);
718 
719 	DPRINTF("Resume the tracee and let it exit\n");
720 	PARENT_TO_CHILD("exit tracee",  parent_tracee, msg);
721 
722 	DPRINTF("Detect that tracee is zombie\n");
723 	await_zombie(tracee);
724 
725 	DPRINTF("Assert that there is no status about tracee - "
726 	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
727 	TWAIT_REQUIRE_SUCCESS(
728 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
729 
730 	DPRINTF("Tell the tracer child should have exited\n");
731 	PARENT_TO_CHILD("wait for tracee exit",  parent_tracer, msg);
732 
733 	DPRINTF("Wait from tracer child to complete waiting for tracee\n");
734 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
735 	    tracer);
736 
737 	validate_status_exited(status, exitval_tracer);
738 
739 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
740 	    TWAIT_FNAME);
741 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
742 	    tracee);
743 
744 	validate_status_exited(status, exitval_tracee);
745 
746 	msg_close(&parent_tracer);
747 	msg_close(&parent_tracee);
748 }
749 #endif
750 
751 #if defined(TWAIT_HAVE_PID)
752 ATF_TC(attach6);
753 ATF_TC_HEAD(attach6, tc)
754 {
755 	atf_tc_set_md_var(tc, "descr",
756 	    "Assert that tracer sees its parent when attached to tracer "
757 	    "(check sysctl(7) and struct kinfo_proc2)");
758 }
759 
760 ATF_TC_BODY(attach6, tc)
761 {
762 	struct msg_fds parent_tracee, parent_tracer;
763 	const int exitval_tracee = 5;
764 	const int exitval_tracer = 10;
765 	pid_t parent, tracee, tracer, wpid;
766 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
767 #if defined(TWAIT_HAVE_STATUS)
768 	int status;
769 #endif
770 	int name[CTL_MAXNAME];
771 	struct kinfo_proc2 kp;
772 	size_t len = sizeof(kp);
773 	unsigned int namelen;
774 
775 	DPRINTF("Spawn tracee\n");
776 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
777 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
778 	tracee = atf_utils_fork();
779 	if (tracee == 0) {
780 		parent = getppid();
781 
782 		/* Emit message to the parent */
783 		CHILD_TO_PARENT("Message 1", parent_tracee, msg);
784 		CHILD_FROM_PARENT("Message 2", parent_tracee, msg);
785 
786 		namelen = 0;
787 		name[namelen++] = CTL_KERN;
788 		name[namelen++] = KERN_PROC2;
789 		name[namelen++] = KERN_PROC_PID;
790 		name[namelen++] = getpid();
791 		name[namelen++] = len;
792 		name[namelen++] = 1;
793 
794 		FORKEE_ASSERT(sysctl(name, namelen, &kp, &len, NULL, 0) == 0);
795 		FORKEE_ASSERT_EQ(parent, kp.p_ppid);
796 
797 		_exit(exitval_tracee);
798 	}
799 
800 	DPRINTF("Wait for child to record its parent identifier (pid)\n");
801 	PARENT_FROM_CHILD("Message 1", parent_tracee, msg);
802 
803 	DPRINTF("Spawn debugger\n");
804 	tracer = atf_utils_fork();
805 	if (tracer == 0) {
806 		/* No IPC to communicate with the child */
807 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
808 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
809 
810 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
811 		FORKEE_REQUIRE_SUCCESS(
812 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
813 
814 		forkee_status_stopped(status, SIGSTOP);
815 
816 		/* Resume tracee with PT_CONTINUE */
817 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
818 
819 		/* Inform parent that tracer has attached to tracee */
820 		CHILD_TO_PARENT("Message 1", parent_tracer, msg);
821 
822 		CHILD_FROM_PARENT("Message 2", parent_tracer, msg);
823 
824 		/* Wait for tracee and assert that it exited */
825 		FORKEE_REQUIRE_SUCCESS(
826 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
827 
828 		forkee_status_exited(status, exitval_tracee);
829 
830 		DPRINTF("Before exiting of the tracer process\n");
831 		_exit(exitval_tracer);
832 	}
833 
834 	DPRINTF("Wait for the tracer to attach to the tracee\n");
835 	PARENT_FROM_CHILD("Message 1", parent_tracer, msg);
836 
837 	DPRINTF("Resume the tracee and let it exit\n");
838 	PARENT_TO_CHILD("Message 1", parent_tracee, msg);
839 
840 	DPRINTF("Detect that tracee is zombie\n");
841 	await_zombie(tracee);
842 
843 	DPRINTF("Assert that there is no status about tracee - "
844 	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
845 	TWAIT_REQUIRE_SUCCESS(
846 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
847 
848 	DPRINTF("Resume the tracer and let it detect exited tracee\n");
849 	PARENT_TO_CHILD("Message 2", parent_tracer, msg);
850 
851 	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
852 	    TWAIT_FNAME);
853 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
854 	    tracer);
855 
856 	validate_status_exited(status, exitval_tracer);
857 
858 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
859 	    TWAIT_FNAME);
860 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
861 	    tracee);
862 
863 	validate_status_exited(status, exitval_tracee);
864 
865 	msg_close(&parent_tracee);
866 	msg_close(&parent_tracer);
867 }
868 #endif
869 
870 #if defined(TWAIT_HAVE_PID)
871 ATF_TC(attach7);
872 ATF_TC_HEAD(attach7, tc)
873 {
874 	atf_tc_set_md_var(tc, "descr",
875 	    "Assert that tracer sees its parent when attached to tracer "
876 	    "(check /proc/curproc/status 3rd column)");
877 }
878 
879 ATF_TC_BODY(attach7, tc)
880 {
881 	struct msg_fds parent_tracee, parent_tracer;
882 	int rv;
883 	const int exitval_tracee = 5;
884 	const int exitval_tracer = 10;
885 	pid_t parent, tracee, tracer, wpid;
886 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
887 #if defined(TWAIT_HAVE_STATUS)
888 	int status;
889 #endif
890 	FILE *fp;
891 	struct stat st;
892 	const char *fname = "/proc/curproc/status";
893 	char s_executable[MAXPATHLEN];
894 	int s_pid, s_ppid;
895 	/*
896 	 * Format:
897 	 *  EXECUTABLE PID PPID ...
898 	 */
899 
900 	SYSCALL_REQUIRE((rv = stat(fname, &st)) == 0 || (errno == ENOENT));
901 	if (rv != 0) {
902 		atf_tc_skip("/proc/curproc/status not found");
903 	}
904 
905 	DPRINTF("Spawn tracee\n");
906 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
907 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
908 	tracee = atf_utils_fork();
909 	if (tracee == 0) {
910 		parent = getppid();
911 
912 		// Wait for parent to let us exit
913 		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
914 		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
915 
916 		FORKEE_ASSERT((fp = fopen(fname, "r")) != NULL);
917 		fscanf(fp, "%s %d %d", s_executable, &s_pid, &s_ppid);
918 		FORKEE_ASSERT(fclose(fp) == 0);
919 		FORKEE_ASSERT_EQ(parent, s_ppid);
920 
921 		_exit(exitval_tracee);
922 	}
923 
924 	DPRINTF("Wait for child to record its parent identifier (pid)\n");
925 	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
926 
927 	DPRINTF("Spawn debugger\n");
928 	tracer = atf_utils_fork();
929 	if (tracer == 0) {
930 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
931 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
932 
933 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
934 		FORKEE_REQUIRE_SUCCESS(
935 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
936 
937 		forkee_status_stopped(status, SIGSTOP);
938 
939 		/* Resume tracee with PT_CONTINUE */
940 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
941 
942 		/* Inform parent that tracer has attached to tracee */
943 		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
944 
945 		/* Wait for parent to tell use that tracee should have exited */
946 		CHILD_FROM_PARENT("wait for tracee exit", parent_tracer, msg);
947 
948 		/* Wait for tracee and assert that it exited */
949 		FORKEE_REQUIRE_SUCCESS(
950 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
951 
952 		forkee_status_exited(status, exitval_tracee);
953 
954 		DPRINTF("Before exiting of the tracer process\n");
955 		_exit(exitval_tracer);
956 	}
957 	DPRINTF("Wait for the tracer to attach to the tracee\n");
958 	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
959 	DPRINTF("Resume the tracee and let it exit\n");
960 	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
961 
962 	DPRINTF("Detect that tracee is zombie\n");
963 	await_zombie(tracee);
964 
965 	DPRINTF("Assert that there is no status about tracee - "
966 	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
967 	TWAIT_REQUIRE_SUCCESS(
968 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
969 
970 	DPRINTF("Resume the tracer and let it detect exited tracee\n");
971 	PARENT_TO_CHILD("Message 2", parent_tracer, msg);
972 
973 	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
974 	    TWAIT_FNAME);
975 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
976 	    tracer);
977 
978 	validate_status_exited(status, exitval_tracer);
979 
980 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
981 	    TWAIT_FNAME);
982 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
983 	    tracee);
984 
985 	validate_status_exited(status, exitval_tracee);
986 
987 	msg_close(&parent_tracee);
988 	msg_close(&parent_tracer);
989 }
990 #endif
991 
992 ATF_TC(eventmask1);
993 ATF_TC_HEAD(eventmask1, tc)
994 {
995 	atf_tc_set_md_var(tc, "descr",
996 	    "Verify that empty EVENT_MASK is preserved");
997 }
998 
999 ATF_TC_BODY(eventmask1, tc)
1000 {
1001 	const int exitval = 5;
1002 	const int sigval = SIGSTOP;
1003 	pid_t child, wpid;
1004 #if defined(TWAIT_HAVE_STATUS)
1005 	int status;
1006 #endif
1007 	ptrace_event_t set_event, get_event;
1008 	const int len = sizeof(ptrace_event_t);
1009 
1010 	DPRINTF("Before forking process PID=%d\n", getpid());
1011 	SYSCALL_REQUIRE((child = fork()) != -1);
1012 	if (child == 0) {
1013 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1014 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1015 
1016 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1017 		FORKEE_ASSERT(raise(sigval) == 0);
1018 
1019 		DPRINTF("Before exiting of the child process\n");
1020 		_exit(exitval);
1021 	}
1022 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1023 
1024 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1025 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1026 
1027 	validate_status_stopped(status, sigval);
1028 
1029 	set_event.pe_set_event = 0;
1030 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1031 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1032 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1033 
1034 	DPRINTF("Before resuming the child process where it left off and "
1035 	    "without signal to be sent\n");
1036 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1037 
1038 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1039 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1040 
1041 	validate_status_exited(status, exitval);
1042 
1043 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1044 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1045 }
1046 
1047 ATF_TC(eventmask2);
1048 ATF_TC_HEAD(eventmask2, tc)
1049 {
1050 	atf_tc_set_md_var(tc, "descr",
1051 	    "Verify that PTRACE_FORK in EVENT_MASK is preserved");
1052 }
1053 
1054 ATF_TC_BODY(eventmask2, tc)
1055 {
1056 	const int exitval = 5;
1057 	const int sigval = SIGSTOP;
1058 	pid_t child, wpid;
1059 #if defined(TWAIT_HAVE_STATUS)
1060 	int status;
1061 #endif
1062 	ptrace_event_t set_event, get_event;
1063 	const int len = sizeof(ptrace_event_t);
1064 
1065 	DPRINTF("Before forking process PID=%d\n", getpid());
1066 	SYSCALL_REQUIRE((child = fork()) != -1);
1067 	if (child == 0) {
1068 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1069 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1070 
1071 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1072 		FORKEE_ASSERT(raise(sigval) == 0);
1073 
1074 		DPRINTF("Before exiting of the child process\n");
1075 		_exit(exitval);
1076 	}
1077 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1078 
1079 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1080 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1081 
1082 	validate_status_stopped(status, sigval);
1083 
1084 	set_event.pe_set_event = PTRACE_FORK;
1085 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1086 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1087 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1088 
1089 	DPRINTF("Before resuming the child process where it left off and "
1090 	    "without signal to be sent\n");
1091 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1092 
1093 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1094 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1095 
1096 	validate_status_exited(status, exitval);
1097 
1098 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1099 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1100 }
1101 
1102 ATF_TC(eventmask3);
1103 ATF_TC_HEAD(eventmask3, tc)
1104 {
1105 	atf_tc_set_md_var(tc, "descr",
1106 	    "Verify that PTRACE_VFORK in EVENT_MASK is preserved");
1107 }
1108 
1109 ATF_TC_BODY(eventmask3, tc)
1110 {
1111 	const int exitval = 5;
1112 	const int sigval = SIGSTOP;
1113 	pid_t child, wpid;
1114 #if defined(TWAIT_HAVE_STATUS)
1115 	int status;
1116 #endif
1117 	ptrace_event_t set_event, get_event;
1118 	const int len = sizeof(ptrace_event_t);
1119 
1120 	atf_tc_expect_fail("PR kern/51630");
1121 
1122 	DPRINTF("Before forking process PID=%d\n", getpid());
1123 	SYSCALL_REQUIRE((child = fork()) != -1);
1124 	if (child == 0) {
1125 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1126 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1127 
1128 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1129 		FORKEE_ASSERT(raise(sigval) == 0);
1130 
1131 		DPRINTF("Before exiting of the child process\n");
1132 		_exit(exitval);
1133 	}
1134 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1135 
1136 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1137 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1138 
1139 	validate_status_stopped(status, sigval);
1140 
1141 	set_event.pe_set_event = PTRACE_VFORK;
1142 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1 || errno == ENOTSUP);
1143 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1144 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1145 
1146 	DPRINTF("Before resuming the child process where it left off and "
1147 	    "without signal to be sent\n");
1148 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1149 
1150 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1151 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1152 
1153 	validate_status_exited(status, exitval);
1154 
1155 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1156 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1157 }
1158 
1159 ATF_TC(eventmask4);
1160 ATF_TC_HEAD(eventmask4, tc)
1161 {
1162 	atf_tc_set_md_var(tc, "descr",
1163 	    "Verify that PTRACE_VFORK_DONE in EVENT_MASK is preserved");
1164 }
1165 
1166 ATF_TC_BODY(eventmask4, tc)
1167 {
1168 	const int exitval = 5;
1169 	const int sigval = SIGSTOP;
1170 	pid_t child, wpid;
1171 #if defined(TWAIT_HAVE_STATUS)
1172 	int status;
1173 #endif
1174 	ptrace_event_t set_event, get_event;
1175 	const int len = sizeof(ptrace_event_t);
1176 
1177 	DPRINTF("Before forking process PID=%d\n", getpid());
1178 	SYSCALL_REQUIRE((child = fork()) != -1);
1179 	if (child == 0) {
1180 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1181 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1182 
1183 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1184 		FORKEE_ASSERT(raise(sigval) == 0);
1185 
1186 		DPRINTF("Before exiting of the child process\n");
1187 		_exit(exitval);
1188 	}
1189 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1190 
1191 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1192 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1193 
1194 	validate_status_stopped(status, sigval);
1195 
1196 	set_event.pe_set_event = PTRACE_VFORK_DONE;
1197 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1198 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1199 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1200 
1201 	DPRINTF("Before resuming the child process where it left off and "
1202 	    "without signal to be sent\n");
1203 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1204 
1205 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1206 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1207 
1208 	validate_status_exited(status, exitval);
1209 
1210 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1211 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1212 }
1213 
1214 ATF_TC(eventmask5);
1215 ATF_TC_HEAD(eventmask5, tc)
1216 {
1217 	atf_tc_set_md_var(tc, "descr",
1218 	    "Verify that PTRACE_LWP_CREATE in EVENT_MASK is preserved");
1219 }
1220 
1221 ATF_TC_BODY(eventmask5, tc)
1222 {
1223 	const int exitval = 5;
1224 	const int sigval = SIGSTOP;
1225 	pid_t child, wpid;
1226 #if defined(TWAIT_HAVE_STATUS)
1227 	int status;
1228 #endif
1229 	ptrace_event_t set_event, get_event;
1230 	const int len = sizeof(ptrace_event_t);
1231 
1232 	DPRINTF("Before forking process PID=%d\n", getpid());
1233 	SYSCALL_REQUIRE((child = fork()) != -1);
1234 	if (child == 0) {
1235 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1236 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1237 
1238 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1239 		FORKEE_ASSERT(raise(sigval) == 0);
1240 
1241 		DPRINTF("Before exiting of the child process\n");
1242 		_exit(exitval);
1243 	}
1244 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1245 
1246 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1247 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1248 
1249 	validate_status_stopped(status, sigval);
1250 
1251 	set_event.pe_set_event = PTRACE_LWP_CREATE;
1252 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1253 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1254 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1255 
1256 	DPRINTF("Before resuming the child process where it left off and "
1257 	    "without signal to be sent\n");
1258 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1259 
1260 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1261 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1262 
1263 	validate_status_exited(status, exitval);
1264 
1265 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1266 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1267 }
1268 
1269 ATF_TC(eventmask6);
1270 ATF_TC_HEAD(eventmask6, tc)
1271 {
1272 	atf_tc_set_md_var(tc, "descr",
1273 	    "Verify that PTRACE_LWP_EXIT in EVENT_MASK is preserved");
1274 }
1275 
1276 ATF_TC_BODY(eventmask6, tc)
1277 {
1278 	const int exitval = 5;
1279 	const int sigval = SIGSTOP;
1280 	pid_t child, wpid;
1281 #if defined(TWAIT_HAVE_STATUS)
1282 	int status;
1283 #endif
1284 	ptrace_event_t set_event, get_event;
1285 	const int len = sizeof(ptrace_event_t);
1286 
1287 	DPRINTF("Before forking process PID=%d\n", getpid());
1288 	SYSCALL_REQUIRE((child = fork()) != -1);
1289 	if (child == 0) {
1290 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1291 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1292 
1293 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1294 		FORKEE_ASSERT(raise(sigval) == 0);
1295 
1296 		DPRINTF("Before exiting of the child process\n");
1297 		_exit(exitval);
1298 	}
1299 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1300 
1301 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1302 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1303 
1304 	validate_status_stopped(status, sigval);
1305 
1306 	set_event.pe_set_event = PTRACE_LWP_EXIT;
1307 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &set_event, len) != -1);
1308 	SYSCALL_REQUIRE(ptrace(PT_GET_EVENT_MASK, child, &get_event, len) != -1);
1309 	ATF_REQUIRE(memcmp(&set_event, &get_event, len) == 0);
1310 
1311 	DPRINTF("Before resuming the child process where it left off and "
1312 	    "without signal to be sent\n");
1313 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1314 
1315 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1316 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1317 
1318 	validate_status_exited(status, exitval);
1319 
1320 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1321 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1322 }
1323 
1324 static void
1325 fork_test(pid_t (*fn)(void), bool trackfork, bool trackvfork,
1326           bool trackvforkdone)
1327 {
1328 	const int exitval = 5;
1329 	const int exitval2 = 15;
1330 	const int sigval = SIGSTOP;
1331 	pid_t child, child2 = 0, wpid;
1332 #if defined(TWAIT_HAVE_STATUS)
1333 	int status;
1334 #endif
1335 	ptrace_state_t state;
1336 	const int slen = sizeof(state);
1337 	ptrace_event_t event;
1338 	const int elen = sizeof(event);
1339 
1340 	if (trackvfork) {
1341 		atf_tc_expect_fail("PR kern/51630");
1342 	}
1343 
1344 	DPRINTF("Before forking process PID=%d\n", getpid());
1345 	SYSCALL_REQUIRE((child = fork()) != -1);
1346 	if (child == 0) {
1347 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1348 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1349 
1350 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1351 		FORKEE_ASSERT(raise(sigval) == 0);
1352 
1353 		FORKEE_ASSERT((child2 = (fn)()) != -1);
1354 
1355 		if (child2 == 0)
1356 			_exit(exitval2);
1357 
1358 		FORKEE_REQUIRE_SUCCESS
1359 		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
1360 
1361 		forkee_status_exited(status, exitval2);
1362 
1363 		DPRINTF("Before exiting of the child process\n");
1364 		_exit(exitval);
1365 	}
1366 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1367 
1368 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1369 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1370 
1371 	validate_status_stopped(status, sigval);
1372 
1373 	DPRINTF("Set 0%s%s%s in EVENT_MASK for the child %d\n",
1374 	        trackfork ? "|PTRACE_FORK" : "",
1375 	        trackvfork ? "|PTRACE_VFORK" : "",
1376 	        trackvforkdone ? "|PTRACE_VFORK_DONE" : "", child);
1377 	event.pe_set_event = 0;
1378 	if (trackfork)
1379 		event.pe_set_event |= PTRACE_FORK;
1380 	if (trackvfork)
1381 		event.pe_set_event |= PTRACE_VFORK;
1382 	if (trackvforkdone)
1383 		event.pe_set_event |= PTRACE_VFORK_DONE;
1384 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
1385 
1386 	DPRINTF("Before resuming the child process where it left off and "
1387 	    "without signal to be sent\n");
1388 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1389 
1390 #if defined(TWAIT_HAVE_PID)
1391 	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1392 		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1393 		        child);
1394 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1395 		                      child);
1396 
1397 		validate_status_stopped(status, SIGTRAP);
1398 
1399 		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1400 		                       slen) != -1);
1401 		if (trackfork && fn == fork) {
1402 			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1403 			       PTRACE_FORK);
1404 		}
1405 		if (trackvfork && fn == vfork) {
1406 			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1407 			       PTRACE_VFORK);
1408 		}
1409 
1410 		child2 = state.pe_other_pid;
1411 		DPRINTF("Reported ptrace event with forkee %d\n", child2);
1412 
1413 		DPRINTF("Before calling %s() for the forkee %d of the child "
1414 		        "%d\n", TWAIT_FNAME, child2, child);
1415 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1416 		    child2);
1417 
1418 		validate_status_stopped(status, SIGTRAP);
1419 
1420 		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state,
1421 		                       slen) != -1);
1422 		if (trackfork && fn == fork) {
1423 			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_FORK,
1424 			       PTRACE_FORK);
1425 		}
1426 		if (trackvfork && fn == vfork) {
1427 			ATF_REQUIRE_EQ(state.pe_report_event & PTRACE_VFORK,
1428 			       PTRACE_VFORK);
1429 		}
1430 
1431 		ATF_REQUIRE_EQ(state.pe_other_pid, child);
1432 
1433 		DPRINTF("Before resuming the forkee process where it left off "
1434 		    "and without signal to be sent\n");
1435 		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0)
1436 		                != -1);
1437 
1438 		DPRINTF("Before resuming the child process where it left off "
1439 		        "and without signal to be sent\n");
1440 		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1441 	}
1442 #endif
1443 
1444 	if (trackvforkdone && fn == vfork) {
1445 		DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME,
1446 		        child);
1447 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
1448 		                      child);
1449 
1450 		validate_status_stopped(status, SIGTRAP);
1451 
1452 		SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state,
1453 		                       slen) != -1);
1454 		ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
1455 
1456 		child2 = state.pe_other_pid;
1457 		DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n",
1458 		        child2);
1459 
1460 		DPRINTF("Before resuming the child process where it left off "
1461 		        "and without signal to be sent\n");
1462 		SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1463 	}
1464 
1465 #if defined(TWAIT_HAVE_PID)
1466 	if ((trackfork && fn == fork) || (trackvfork && fn == vfork)) {
1467 		DPRINTF("Before calling %s() for the forkee - expected exited"
1468 		        "\n", TWAIT_FNAME);
1469 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
1470 		    child2);
1471 
1472 		validate_status_exited(status, exitval2);
1473 
1474 		DPRINTF("Before calling %s() for the forkee - expected no "
1475 		        "process\n", TWAIT_FNAME);
1476 		TWAIT_REQUIRE_FAILURE(ECHILD,
1477 		    wpid = TWAIT_GENERIC(child2, &status, 0));
1478 	}
1479 #endif
1480 
1481 	DPRINTF("Before calling %s() for the child - expected stopped "
1482 	    "SIGCHLD\n", TWAIT_FNAME);
1483 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1484 
1485 	validate_status_stopped(status, SIGCHLD);
1486 
1487 	DPRINTF("Before resuming the child process where it left off and "
1488 	    "without signal to be sent\n");
1489 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1490 
1491 	DPRINTF("Before calling %s() for the child - expected exited\n",
1492 	    TWAIT_FNAME);
1493 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1494 
1495 	validate_status_exited(status, exitval);
1496 
1497 	DPRINTF("Before calling %s() for the child - expected no process\n",
1498 	    TWAIT_FNAME);
1499 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1500 }
1501 
1502 ATF_TC(fork1);
1503 ATF_TC_HEAD(fork1, tc)
1504 {
1505 	atf_tc_set_md_var(tc, "descr",
1506 	    "Verify that fork(2) is not intercepted by ptrace(2) with empty "
1507 	    "EVENT_MASK");
1508 }
1509 
1510 ATF_TC_BODY(fork1, tc)
1511 {
1512 
1513 	fork_test(fork, false, false, false);
1514 }
1515 
1516 #if defined(TWAIT_HAVE_PID)
1517 ATF_TC(fork2);
1518 ATF_TC_HEAD(fork2, tc)
1519 {
1520 	atf_tc_set_md_var(tc, "descr",
1521 	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
1522 	    "set to PTRACE_FORK");
1523 }
1524 
1525 ATF_TC_BODY(fork2, tc)
1526 {
1527 
1528 	fork_test(fork, true, false, false);
1529 }
1530 #endif
1531 
1532 #if defined(TWAIT_HAVE_PID)
1533 ATF_TC(fork3);
1534 ATF_TC_HEAD(fork3, tc)
1535 {
1536 	atf_tc_set_md_var(tc, "descr",
1537 	    "Verify that fork(2) is not intercepted by ptrace(2) with "
1538 	    "EVENT_MASK set to PTRACE_VFORK");
1539 }
1540 
1541 ATF_TC_BODY(fork3, tc)
1542 {
1543 
1544 	fork_test(fork, false, true, false);
1545 }
1546 #endif
1547 
1548 #if defined(TWAIT_HAVE_PID)
1549 ATF_TC(fork4);
1550 ATF_TC_HEAD(fork4, tc)
1551 {
1552 	atf_tc_set_md_var(tc, "descr",
1553 	    "Verify that fork(2) is intercepted by ptrace(2) with "
1554 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK");
1555 }
1556 
1557 ATF_TC_BODY(fork4, tc)
1558 {
1559 
1560 	fork_test(fork, true, true, false);
1561 }
1562 #endif
1563 
1564 ATF_TC(fork5);
1565 ATF_TC_HEAD(fork5, tc)
1566 {
1567 	atf_tc_set_md_var(tc, "descr",
1568 	    "Verify that fork(2) is not intercepted by ptrace(2) with "
1569 	    "EVENT_MASK set to PTRACE_VFORK_DONE");
1570 }
1571 
1572 ATF_TC_BODY(fork5, tc)
1573 {
1574 
1575 	fork_test(fork, false, false, true);
1576 }
1577 
1578 #if defined(TWAIT_HAVE_PID)
1579 ATF_TC(fork6);
1580 ATF_TC_HEAD(fork6, tc)
1581 {
1582 	atf_tc_set_md_var(tc, "descr",
1583 	    "Verify that fork(2) is intercepted by ptrace(2) with "
1584 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK_DONE");
1585 }
1586 
1587 ATF_TC_BODY(fork6, tc)
1588 {
1589 
1590 	fork_test(fork, true, false, true);
1591 }
1592 #endif
1593 
1594 #if defined(TWAIT_HAVE_PID)
1595 ATF_TC(fork7);
1596 ATF_TC_HEAD(fork7, tc)
1597 {
1598 	atf_tc_set_md_var(tc, "descr",
1599 	    "Verify that fork(2) is not intercepted by ptrace(2) with "
1600 	    "EVENT_MASK set to PTRACE_VFORK|PTRACE_VFORK_DONE");
1601 }
1602 
1603 ATF_TC_BODY(fork7, tc)
1604 {
1605 
1606 	fork_test(fork, false, true, true);
1607 }
1608 #endif
1609 
1610 #if defined(TWAIT_HAVE_PID)
1611 ATF_TC(fork8);
1612 ATF_TC_HEAD(fork8, tc)
1613 {
1614 	atf_tc_set_md_var(tc, "descr",
1615 	    "Verify that fork(2) is intercepted by ptrace(2) with "
1616 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK|PTRACE_VFORK_DONE");
1617 }
1618 
1619 ATF_TC_BODY(fork8, tc)
1620 {
1621 
1622 	fork_test(fork, true, true, true);
1623 }
1624 #endif
1625 
1626 
1627 ATF_TC(vfork1);
1628 ATF_TC_HEAD(vfork1, tc)
1629 {
1630 	atf_tc_set_md_var(tc, "descr",
1631 	    "Verify that vfork(2) is not intercepted by ptrace(2) with empty "
1632 	    "EVENT_MASK");
1633 }
1634 
1635 ATF_TC_BODY(vfork1, tc)
1636 {
1637 
1638 	fork_test(vfork, false, false, false);
1639 }
1640 
1641 #if defined(TWAIT_HAVE_PID)
1642 ATF_TC(vfork2);
1643 ATF_TC_HEAD(vfork2, tc)
1644 {
1645 	atf_tc_set_md_var(tc, "descr",
1646 	    "Verify that vfork(2) is not intercepted by ptrace(2) with "
1647 	    "EVENT_MASK set to PTRACE_FORK");
1648 }
1649 
1650 ATF_TC_BODY(vfork2, tc)
1651 {
1652 
1653 	fork_test(vfork, true, false, false);
1654 }
1655 #endif
1656 
1657 #if defined(TWAIT_HAVE_PID)
1658 ATF_TC(vfork3);
1659 ATF_TC_HEAD(vfork3, tc)
1660 {
1661 	atf_tc_set_md_var(tc, "descr",
1662 	    "Verify that vfork(2) is intercepted by ptrace(2) with "
1663 	    "EVENT_MASK set to PTRACE_VFORK");
1664 }
1665 
1666 ATF_TC_BODY(vfork3, tc)
1667 {
1668 
1669 	fork_test(vfork, false, true, false);
1670 }
1671 #endif
1672 
1673 #if defined(TWAIT_HAVE_PID)
1674 ATF_TC(vfork4);
1675 ATF_TC_HEAD(vfork4, tc)
1676 {
1677 	atf_tc_set_md_var(tc, "descr",
1678 	    "Verify that vfork(2) is intercepted by ptrace(2) with "
1679 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK");
1680 }
1681 
1682 ATF_TC_BODY(vfork4, tc)
1683 {
1684 
1685 	fork_test(vfork, true, true, false);
1686 }
1687 #endif
1688 
1689 ATF_TC(vfork5);
1690 ATF_TC_HEAD(vfork5, tc)
1691 {
1692 	atf_tc_set_md_var(tc, "descr",
1693 	    "Verify that vfork(2) is intercepted by ptrace(2) with "
1694 	    "EVENT_MASK set to PTRACE_VFORK_DONE");
1695 }
1696 
1697 ATF_TC_BODY(vfork5, tc)
1698 {
1699 
1700 	fork_test(vfork, false, false, true);
1701 }
1702 
1703 #if defined(TWAIT_HAVE_PID)
1704 ATF_TC(vfork6);
1705 ATF_TC_HEAD(vfork6, tc)
1706 {
1707 	atf_tc_set_md_var(tc, "descr",
1708 	    "Verify that vfork(2) is intercepted by ptrace(2) with "
1709 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK_DONE");
1710 }
1711 
1712 ATF_TC_BODY(vfork6, tc)
1713 {
1714 
1715 	fork_test(vfork, true, false, true);
1716 }
1717 #endif
1718 
1719 #if defined(TWAIT_HAVE_PID)
1720 ATF_TC(vfork7);
1721 ATF_TC_HEAD(vfork7, tc)
1722 {
1723 	atf_tc_set_md_var(tc, "descr",
1724 	    "Verify that vfork(2) is not intercepted by ptrace(2) with "
1725 	    "EVENT_MASK set to PTRACE_VFORK|PTRACE_VFORK_DONE");
1726 }
1727 
1728 ATF_TC_BODY(vfork7, tc)
1729 {
1730 
1731 	fork_test(vfork, false, true, true);
1732 }
1733 #endif
1734 
1735 #if defined(TWAIT_HAVE_PID)
1736 ATF_TC(vfork8);
1737 ATF_TC_HEAD(vfork8, tc)
1738 {
1739 	atf_tc_set_md_var(tc, "descr",
1740 	    "Verify that vfork(2) is intercepted by ptrace(2) with "
1741 	    "EVENT_MASK set to PTRACE_FORK|PTRACE_VFORK|PTRACE_VFORK_DONE");
1742 }
1743 
1744 ATF_TC_BODY(vfork8, tc)
1745 {
1746 
1747 	fork_test(vfork, true, true, true);
1748 }
1749 #endif
1750 
1751 ATF_TC(io_read_d1);
1752 ATF_TC_HEAD(io_read_d1, tc)
1753 {
1754 	atf_tc_set_md_var(tc, "descr",
1755 	    "Verify PT_IO with PIOD_READ_D and len = sizeof(uint8_t)");
1756 }
1757 
1758 ATF_TC_BODY(io_read_d1, tc)
1759 {
1760 	const int exitval = 5;
1761 	const int sigval = SIGSTOP;
1762 	pid_t child, wpid;
1763 	uint8_t lookup_me = 0;
1764 	const uint8_t magic = 0xab;
1765 	struct ptrace_io_desc io = {
1766 		.piod_op = PIOD_READ_D,
1767 		.piod_offs = &lookup_me,
1768 		.piod_addr = &lookup_me,
1769 		.piod_len = sizeof(lookup_me)
1770 	};
1771 #if defined(TWAIT_HAVE_STATUS)
1772 	int status;
1773 #endif
1774 
1775 	DPRINTF("Before forking process PID=%d\n", getpid());
1776 	SYSCALL_REQUIRE((child = fork()) != -1);
1777 	if (child == 0) {
1778 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1779 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1780 
1781 		lookup_me = magic;
1782 
1783 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1784 		FORKEE_ASSERT(raise(sigval) == 0);
1785 
1786 		DPRINTF("Before exiting of the child process\n");
1787 		_exit(exitval);
1788 	}
1789 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1790 
1791 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1792 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1793 
1794 	validate_status_stopped(status, sigval);
1795 
1796 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
1797 	    child, getpid());
1798 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
1799 
1800 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
1801 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic);
1802 
1803 	DPRINTF("Before resuming the child process where it left off and "
1804 	    "without signal to be sent\n");
1805 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1806 
1807 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1808 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1809 
1810 	validate_status_exited(status, exitval);
1811 
1812 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1813 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1814 }
1815 
1816 ATF_TC(io_read_d2);
1817 ATF_TC_HEAD(io_read_d2, tc)
1818 {
1819 	atf_tc_set_md_var(tc, "descr",
1820 	    "Verify PT_IO with PIOD_READ_D and len = sizeof(uint16_t)");
1821 }
1822 
1823 ATF_TC_BODY(io_read_d2, tc)
1824 {
1825 	const int exitval = 5;
1826 	const int sigval = SIGSTOP;
1827 	pid_t child, wpid;
1828 	uint16_t lookup_me = 0;
1829 	const uint16_t magic = 0x1234;
1830 	struct ptrace_io_desc io = {
1831 		.piod_op = PIOD_READ_D,
1832 		.piod_offs = &lookup_me,
1833 		.piod_addr = &lookup_me,
1834 		.piod_len = sizeof(lookup_me)
1835 	};
1836 #if defined(TWAIT_HAVE_STATUS)
1837 	int status;
1838 #endif
1839 
1840 	DPRINTF("Before forking process PID=%d\n", getpid());
1841 	SYSCALL_REQUIRE((child = fork()) != -1);
1842 	if (child == 0) {
1843 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1844 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1845 
1846 		lookup_me = magic;
1847 
1848 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1849 		FORKEE_ASSERT(raise(sigval) == 0);
1850 
1851 		DPRINTF("Before exiting of the child process\n");
1852 		_exit(exitval);
1853 	}
1854 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1855 
1856 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1857 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1858 
1859 	validate_status_stopped(status, sigval);
1860 
1861 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
1862 	    child, getpid());
1863 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
1864 
1865 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
1866 	    "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic);
1867 
1868 	DPRINTF("Before resuming the child process where it left off and "
1869 	    "without signal to be sent\n");
1870 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1871 
1872 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1873 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1874 
1875 	validate_status_exited(status, exitval);
1876 
1877 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1878 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1879 }
1880 
1881 ATF_TC(io_read_d3);
1882 ATF_TC_HEAD(io_read_d3, tc)
1883 {
1884 	atf_tc_set_md_var(tc, "descr",
1885 	    "Verify PT_IO with PIOD_READ_D and len = sizeof(uint32_t)");
1886 }
1887 
1888 ATF_TC_BODY(io_read_d3, tc)
1889 {
1890 	const int exitval = 5;
1891 	const int sigval = SIGSTOP;
1892 	pid_t child, wpid;
1893 	uint32_t lookup_me = 0;
1894 	const uint32_t magic = 0x1234abcd;
1895 	struct ptrace_io_desc io = {
1896 		.piod_op = PIOD_READ_D,
1897 		.piod_offs = &lookup_me,
1898 		.piod_addr = &lookup_me,
1899 		.piod_len = sizeof(lookup_me)
1900 	};
1901 #if defined(TWAIT_HAVE_STATUS)
1902 	int status;
1903 #endif
1904 
1905 	DPRINTF("Before forking process PID=%d\n", getpid());
1906 	SYSCALL_REQUIRE((child = fork()) != -1);
1907 	if (child == 0) {
1908 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1909 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1910 
1911 		lookup_me = magic;
1912 
1913 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1914 		FORKEE_ASSERT(raise(sigval) == 0);
1915 
1916 		DPRINTF("Before exiting of the child process\n");
1917 		_exit(exitval);
1918 	}
1919 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1920 
1921 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1922 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1923 
1924 	validate_status_stopped(status, sigval);
1925 
1926 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
1927 	    child, getpid());
1928 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
1929 
1930 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
1931 	    "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic);
1932 
1933 	DPRINTF("Before resuming the child process where it left off and "
1934 	    "without signal to be sent\n");
1935 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
1936 
1937 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1938 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1939 
1940 	validate_status_exited(status, exitval);
1941 
1942 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1943 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
1944 }
1945 
1946 ATF_TC(io_read_d4);
1947 ATF_TC_HEAD(io_read_d4, tc)
1948 {
1949 	atf_tc_set_md_var(tc, "descr",
1950 	    "Verify PT_IO with PIOD_READ_D and len = sizeof(uint64_t)");
1951 }
1952 
1953 ATF_TC_BODY(io_read_d4, tc)
1954 {
1955 	const int exitval = 5;
1956 	const int sigval = SIGSTOP;
1957 	pid_t child, wpid;
1958 	uint64_t lookup_me = 0;
1959 	const uint64_t magic = 0x1234abcd9876dcfa;
1960 	struct ptrace_io_desc io = {
1961 		.piod_op = PIOD_READ_D,
1962 		.piod_offs = &lookup_me,
1963 		.piod_addr = &lookup_me,
1964 		.piod_len = sizeof(lookup_me)
1965 	};
1966 #if defined(TWAIT_HAVE_STATUS)
1967 	int status;
1968 #endif
1969 
1970 	DPRINTF("Before forking process PID=%d\n", getpid());
1971 	SYSCALL_REQUIRE((child = fork()) != -1);
1972 	if (child == 0) {
1973 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
1974 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
1975 
1976 		lookup_me = magic;
1977 
1978 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
1979 		FORKEE_ASSERT(raise(sigval) == 0);
1980 
1981 		DPRINTF("Before exiting of the child process\n");
1982 		_exit(exitval);
1983 	}
1984 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
1985 
1986 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
1987 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
1988 
1989 	validate_status_stopped(status, sigval);
1990 
1991 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
1992 	    child, getpid());
1993 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
1994 
1995 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
1996 	    "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic);
1997 
1998 	DPRINTF("Before resuming the child process where it left off and "
1999 	    "without signal to be sent\n");
2000 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2001 
2002 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2003 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2004 
2005 	validate_status_exited(status, exitval);
2006 
2007 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2008 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2009 }
2010 
2011 ATF_TC(io_write_d1);
2012 ATF_TC_HEAD(io_write_d1, tc)
2013 {
2014 	atf_tc_set_md_var(tc, "descr",
2015 	    "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint8_t)");
2016 }
2017 
2018 ATF_TC_BODY(io_write_d1, tc)
2019 {
2020 	const int exitval = 5;
2021 	const int sigval = SIGSTOP;
2022 	pid_t child, wpid;
2023 	uint8_t lookup_me = 0;
2024 	const uint8_t magic = 0xab;
2025 	struct ptrace_io_desc io = {
2026 		.piod_op = PIOD_WRITE_D,
2027 		.piod_offs = &lookup_me,
2028 		.piod_addr = &lookup_me,
2029 		.piod_len = sizeof(lookup_me)
2030 	};
2031 #if defined(TWAIT_HAVE_STATUS)
2032 	int status;
2033 #endif
2034 
2035 	DPRINTF("Before forking process PID=%d\n", getpid());
2036 	SYSCALL_REQUIRE((child = fork()) != -1);
2037 	if (child == 0) {
2038 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2039 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2040 
2041 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2042 		FORKEE_ASSERT(raise(sigval) == 0);
2043 
2044 		FORKEE_ASSERT_EQ(lookup_me, magic);
2045 
2046 		DPRINTF("Before exiting of the child process\n");
2047 		_exit(exitval);
2048 	}
2049 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2050 
2051 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2052 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2053 
2054 	validate_status_stopped(status, sigval);
2055 
2056 	lookup_me = magic;
2057 
2058 	DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
2059 	    child, getpid());
2060 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2061 
2062 	DPRINTF("Before resuming the child process where it left off and "
2063 	    "without signal to be sent\n");
2064 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2065 
2066 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2067 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2068 
2069 	validate_status_exited(status, exitval);
2070 
2071 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2072 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2073 }
2074 
2075 ATF_TC(io_write_d2);
2076 ATF_TC_HEAD(io_write_d2, tc)
2077 {
2078 	atf_tc_set_md_var(tc, "descr",
2079 	    "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint16_t)");
2080 }
2081 
2082 ATF_TC_BODY(io_write_d2, tc)
2083 {
2084 	const int exitval = 5;
2085 	const int sigval = SIGSTOP;
2086 	pid_t child, wpid;
2087 	uint16_t lookup_me = 0;
2088 	const uint16_t magic = 0xab12;
2089 	struct ptrace_io_desc io = {
2090 		.piod_op = PIOD_WRITE_D,
2091 		.piod_offs = &lookup_me,
2092 		.piod_addr = &lookup_me,
2093 		.piod_len = sizeof(lookup_me)
2094 	};
2095 #if defined(TWAIT_HAVE_STATUS)
2096 	int status;
2097 #endif
2098 
2099 	DPRINTF("Before forking process PID=%d\n", getpid());
2100 	SYSCALL_REQUIRE((child = fork()) != -1);
2101 	if (child == 0) {
2102 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2103 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2104 
2105 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2106 		FORKEE_ASSERT(raise(sigval) == 0);
2107 
2108 		FORKEE_ASSERT_EQ(lookup_me, magic);
2109 
2110 		DPRINTF("Before exiting of the child process\n");
2111 		_exit(exitval);
2112 	}
2113 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2114 
2115 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2116 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2117 
2118 	validate_status_stopped(status, sigval);
2119 
2120 	lookup_me = magic;
2121 
2122 	DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
2123 	    child, getpid());
2124 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2125 
2126 	DPRINTF("Before resuming the child process where it left off and "
2127 	    "without signal to be sent\n");
2128 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2129 
2130 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2131 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2132 
2133 	validate_status_exited(status, exitval);
2134 
2135 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2136 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2137 }
2138 
2139 ATF_TC(io_write_d3);
2140 ATF_TC_HEAD(io_write_d3, tc)
2141 {
2142 	atf_tc_set_md_var(tc, "descr",
2143 	    "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint32_t)");
2144 }
2145 
2146 ATF_TC_BODY(io_write_d3, tc)
2147 {
2148 	const int exitval = 5;
2149 	const int sigval = SIGSTOP;
2150 	pid_t child, wpid;
2151 	uint32_t lookup_me = 0;
2152 	const uint32_t magic = 0xab127643;
2153 	struct ptrace_io_desc io = {
2154 		.piod_op = PIOD_WRITE_D,
2155 		.piod_offs = &lookup_me,
2156 		.piod_addr = &lookup_me,
2157 		.piod_len = sizeof(lookup_me)
2158 	};
2159 #if defined(TWAIT_HAVE_STATUS)
2160 	int status;
2161 #endif
2162 
2163 	DPRINTF("Before forking process PID=%d\n", getpid());
2164 	SYSCALL_REQUIRE((child = fork()) != -1);
2165 	if (child == 0) {
2166 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2167 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2168 
2169 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2170 		FORKEE_ASSERT(raise(sigval) == 0);
2171 
2172 		FORKEE_ASSERT_EQ(lookup_me, magic);
2173 
2174 		DPRINTF("Before exiting of the child process\n");
2175 		_exit(exitval);
2176 	}
2177 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2178 
2179 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2180 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2181 
2182 	validate_status_stopped(status, sigval);
2183 
2184 	lookup_me = magic;
2185 
2186 	DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
2187 	    child, getpid());
2188 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2189 
2190 	DPRINTF("Before resuming the child process where it left off and "
2191 	    "without signal to be sent\n");
2192 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2193 
2194 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2195 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2196 
2197 	validate_status_exited(status, exitval);
2198 
2199 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2200 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2201 }
2202 
2203 ATF_TC(io_write_d4);
2204 ATF_TC_HEAD(io_write_d4, tc)
2205 {
2206 	atf_tc_set_md_var(tc, "descr",
2207 	    "Verify PT_IO with PIOD_WRITE_D and len = sizeof(uint64_t)");
2208 }
2209 
2210 ATF_TC_BODY(io_write_d4, tc)
2211 {
2212 	const int exitval = 5;
2213 	const int sigval = SIGSTOP;
2214 	pid_t child, wpid;
2215 	uint64_t lookup_me = 0;
2216 	const uint64_t magic = 0xab12764376490123;
2217 	struct ptrace_io_desc io = {
2218 		.piod_op = PIOD_WRITE_D,
2219 		.piod_offs = &lookup_me,
2220 		.piod_addr = &lookup_me,
2221 		.piod_len = sizeof(lookup_me)
2222 	};
2223 #if defined(TWAIT_HAVE_STATUS)
2224 	int status;
2225 #endif
2226 
2227 	DPRINTF("Before forking process PID=%d\n", getpid());
2228 	SYSCALL_REQUIRE((child = fork()) != -1);
2229 	if (child == 0) {
2230 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2231 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2232 
2233 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2234 		FORKEE_ASSERT(raise(sigval) == 0);
2235 
2236 		FORKEE_ASSERT_EQ(lookup_me, magic);
2237 
2238 		DPRINTF("Before exiting of the child process\n");
2239 		_exit(exitval);
2240 	}
2241 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2242 
2243 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2244 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2245 
2246 	validate_status_stopped(status, sigval);
2247 
2248 	lookup_me = magic;
2249 
2250 	DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
2251 	    child, getpid());
2252 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2253 
2254 	DPRINTF("Before resuming the child process where it left off and "
2255 	    "without signal to be sent\n");
2256 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2257 
2258 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2259 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2260 
2261 	validate_status_exited(status, exitval);
2262 
2263 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2264 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2265 }
2266 
2267 ATF_TC(io_read_auxv1);
2268 ATF_TC_HEAD(io_read_auxv1, tc)
2269 {
2270 	atf_tc_set_md_var(tc, "descr",
2271 	    "Verify PT_READ_AUXV called for tracee");
2272 }
2273 
2274 ATF_TC_BODY(io_read_auxv1, tc)
2275 {
2276 	const int exitval = 5;
2277 	const int sigval = SIGSTOP;
2278 	pid_t child, wpid;
2279 #if defined(TWAIT_HAVE_STATUS)
2280 	int status;
2281 #endif
2282 	AuxInfo ai[100], *aip;
2283 	struct ptrace_io_desc io = {
2284 		.piod_op = PIOD_READ_AUXV,
2285 		.piod_offs = 0,
2286 		.piod_addr = ai,
2287 		.piod_len = sizeof(ai)
2288 	};
2289 
2290 	DPRINTF("Before forking process PID=%d\n", getpid());
2291 	SYSCALL_REQUIRE((child = fork()) != -1);
2292 	if (child == 0) {
2293 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2294 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2295 
2296 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2297 		FORKEE_ASSERT(raise(sigval) == 0);
2298 
2299 		DPRINTF("Before exiting of the child process\n");
2300 		_exit(exitval);
2301 	}
2302 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2303 
2304 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2305 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2306 
2307 	validate_status_stopped(status, sigval);
2308 
2309 	DPRINTF("Read new AUXV from tracee (PID=%d) by tracer (PID=%d)\n",
2310 	    child, getpid());
2311 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
2312 
2313 	DPRINTF("Asserting that AUXV length (%zu) is > 0\n", io.piod_len);
2314 	ATF_REQUIRE(io.piod_len > 0);
2315 
2316 	for (aip = ai; aip->a_type != AT_NULL; aip++)
2317 		DPRINTF("a_type=%#llx a_v=%#llx\n",
2318 		    (long long int)aip->a_type, (long long int)aip->a_v);
2319 
2320 	DPRINTF("Before resuming the child process where it left off and "
2321 	    "without signal to be sent\n");
2322 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2323 
2324 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2325 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2326 
2327 	validate_status_exited(status, exitval);
2328 
2329 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2330 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2331 }
2332 
2333 ATF_TC(read_d1);
2334 ATF_TC_HEAD(read_d1, tc)
2335 {
2336 	atf_tc_set_md_var(tc, "descr",
2337 	    "Verify PT_READ_D called once");
2338 }
2339 
2340 ATF_TC_BODY(read_d1, tc)
2341 {
2342 	const int exitval = 5;
2343 	const int sigval = SIGSTOP;
2344 	pid_t child, wpid;
2345 	int lookup_me = 0;
2346 	const int magic = (int)random();
2347 #if defined(TWAIT_HAVE_STATUS)
2348 	int status;
2349 #endif
2350 
2351 	DPRINTF("Before forking process PID=%d\n", getpid());
2352 	SYSCALL_REQUIRE((child = fork()) != -1);
2353 	if (child == 0) {
2354 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2355 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2356 
2357 		lookup_me = magic;
2358 
2359 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2360 		FORKEE_ASSERT(raise(sigval) == 0);
2361 
2362 		DPRINTF("Before exiting of the child process\n");
2363 		_exit(exitval);
2364 	}
2365 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2366 
2367 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2368 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2369 
2370 	validate_status_stopped(status, sigval);
2371 
2372 	DPRINTF("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
2373 	    child, getpid());
2374 	errno = 0;
2375 	lookup_me = ptrace(PT_READ_D, child, &lookup_me, 0);
2376 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2377 
2378 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
2379 	    "got value %#x != expected %#x", lookup_me, magic);
2380 
2381 	DPRINTF("Before resuming the child process where it left off and "
2382 	    "without signal to be sent\n");
2383 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2384 
2385 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2386 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2387 
2388 	validate_status_exited(status, exitval);
2389 
2390 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2391 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2392 }
2393 
2394 ATF_TC(read_d2);
2395 ATF_TC_HEAD(read_d2, tc)
2396 {
2397 	atf_tc_set_md_var(tc, "descr",
2398 	    "Verify PT_READ_D called twice");
2399 }
2400 
2401 ATF_TC_BODY(read_d2, tc)
2402 {
2403 	const int exitval = 5;
2404 	const int sigval = SIGSTOP;
2405 	pid_t child, wpid;
2406 	int lookup_me1 = 0;
2407 	int lookup_me2 = 0;
2408 	const int magic1 = (int)random();
2409 	const int magic2 = (int)random();
2410 #if defined(TWAIT_HAVE_STATUS)
2411 	int status;
2412 #endif
2413 
2414 	DPRINTF("Before forking process PID=%d\n", getpid());
2415 	SYSCALL_REQUIRE((child = fork()) != -1);
2416 	if (child == 0) {
2417 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2418 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2419 
2420 		lookup_me1 = magic1;
2421 		lookup_me2 = magic2;
2422 
2423 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2424 		FORKEE_ASSERT(raise(sigval) == 0);
2425 
2426 		DPRINTF("Before exiting of the child process\n");
2427 		_exit(exitval);
2428 	}
2429 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2430 
2431 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2432 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2433 
2434 	validate_status_stopped(status, sigval);
2435 
2436 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
2437 	    child, getpid());
2438 	errno = 0;
2439 	lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
2440 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2441 
2442 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
2443 	    "got value %#x != expected %#x", lookup_me1, magic1);
2444 
2445 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
2446 	    child, getpid());
2447 	errno = 0;
2448 	lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
2449 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2450 
2451 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
2452 	    "got value %#x != expected %#x", lookup_me2, magic2);
2453 
2454 	DPRINTF("Before resuming the child process where it left off and "
2455 	    "without signal to be sent\n");
2456 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2457 
2458 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2459 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2460 
2461 	validate_status_exited(status, exitval);
2462 
2463 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2464 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2465 }
2466 
2467 ATF_TC(read_d3);
2468 ATF_TC_HEAD(read_d3, tc)
2469 {
2470 	atf_tc_set_md_var(tc, "descr",
2471 	    "Verify PT_READ_D called three times");
2472 }
2473 
2474 ATF_TC_BODY(read_d3, tc)
2475 {
2476 	const int exitval = 5;
2477 	const int sigval = SIGSTOP;
2478 	pid_t child, wpid;
2479 	int lookup_me1 = 0;
2480 	int lookup_me2 = 0;
2481 	int lookup_me3 = 0;
2482 	const int magic1 = (int)random();
2483 	const int magic2 = (int)random();
2484 	const int magic3 = (int)random();
2485 #if defined(TWAIT_HAVE_STATUS)
2486 	int status;
2487 #endif
2488 
2489 	DPRINTF("Before forking process PID=%d\n", getpid());
2490 	SYSCALL_REQUIRE((child = fork()) != -1);
2491 	if (child == 0) {
2492 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2493 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2494 
2495 		lookup_me1 = magic1;
2496 		lookup_me2 = magic2;
2497 		lookup_me3 = magic3;
2498 
2499 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2500 		FORKEE_ASSERT(raise(sigval) == 0);
2501 
2502 		DPRINTF("Before exiting of the child process\n");
2503 		_exit(exitval);
2504 	}
2505 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2506 
2507 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2508 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2509 
2510 	validate_status_stopped(status, sigval);
2511 
2512 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
2513 	    child, getpid());
2514 	errno = 0;
2515 	lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
2516 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2517 
2518 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
2519 	    "got value %#x != expected %#x", lookup_me1, magic1);
2520 
2521 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
2522 	    child, getpid());
2523 	errno = 0;
2524 	lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
2525 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2526 
2527 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
2528 	    "got value %#x != expected %#x", lookup_me2, magic2);
2529 
2530 	DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
2531 	    child, getpid());
2532 	errno = 0;
2533 	lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0);
2534 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2535 
2536 	ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
2537 	    "got value %#x != expected %#x", lookup_me3, magic3);
2538 
2539 	DPRINTF("Before resuming the child process where it left off and "
2540 	    "without signal to be sent\n");
2541 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2542 
2543 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2544 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2545 
2546 	validate_status_exited(status, exitval);
2547 
2548 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2549 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2550 }
2551 
2552 ATF_TC(read_d4);
2553 ATF_TC_HEAD(read_d4, tc)
2554 {
2555 	atf_tc_set_md_var(tc, "descr",
2556 	    "Verify PT_READ_D called four times");
2557 }
2558 
2559 ATF_TC_BODY(read_d4, tc)
2560 {
2561 	const int exitval = 5;
2562 	const int sigval = SIGSTOP;
2563 	pid_t child, wpid;
2564 	int lookup_me1 = 0;
2565 	int lookup_me2 = 0;
2566 	int lookup_me3 = 0;
2567 	int lookup_me4 = 0;
2568 	const int magic1 = (int)random();
2569 	const int magic2 = (int)random();
2570 	const int magic3 = (int)random();
2571 	const int magic4 = (int)random();
2572 #if defined(TWAIT_HAVE_STATUS)
2573 	int status;
2574 #endif
2575 
2576 	DPRINTF("Before forking process PID=%d\n", getpid());
2577 	SYSCALL_REQUIRE((child = fork()) != -1);
2578 	if (child == 0) {
2579 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2580 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2581 
2582 		lookup_me1 = magic1;
2583 		lookup_me2 = magic2;
2584 		lookup_me3 = magic3;
2585 		lookup_me4 = magic4;
2586 
2587 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2588 		FORKEE_ASSERT(raise(sigval) == 0);
2589 
2590 		DPRINTF("Before exiting of the child process\n");
2591 		_exit(exitval);
2592 	}
2593 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2594 
2595 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2596 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2597 
2598 	validate_status_stopped(status, sigval);
2599 
2600 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
2601 	    child, getpid());
2602 	errno = 0;
2603 	lookup_me1 = ptrace(PT_READ_D, child, &lookup_me1, 0);
2604 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2605 
2606 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
2607 	    "got value %#x != expected %#x", lookup_me1, magic1);
2608 
2609 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
2610 	    child, getpid());
2611 	errno = 0;
2612 	lookup_me2 = ptrace(PT_READ_D, child, &lookup_me2, 0);
2613 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2614 
2615 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
2616 	    "got value %#x != expected %#x", lookup_me2, magic2);
2617 
2618 	DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
2619 	    child, getpid());
2620 	errno = 0;
2621 	lookup_me3 = ptrace(PT_READ_D, child, &lookup_me3, 0);
2622 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2623 
2624 	ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
2625 	    "got value %#x != expected %#x", lookup_me3, magic3);
2626 
2627 	DPRINTF("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n",
2628 	    child, getpid());
2629 	errno = 0;
2630 	lookup_me4 = ptrace(PT_READ_D, child, &lookup_me4, 0);
2631 	SYSCALL_REQUIRE_ERRNO(errno, 0);
2632 
2633 	ATF_REQUIRE_EQ_MSG(lookup_me4, magic4,
2634 	    "got value %#x != expected %#x", lookup_me4, magic4);
2635 
2636 	DPRINTF("Before resuming the child process where it left off and "
2637 	    "without signal to be sent\n");
2638 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2639 
2640 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2641 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2642 
2643 	validate_status_exited(status, exitval);
2644 
2645 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2646 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2647 }
2648 
2649 ATF_TC(write_d1);
2650 ATF_TC_HEAD(write_d1, tc)
2651 {
2652 	atf_tc_set_md_var(tc, "descr",
2653 	    "Verify PT_WRITE_D called once");
2654 }
2655 
2656 ATF_TC_BODY(write_d1, tc)
2657 {
2658 	const int exitval = 5;
2659 	const int sigval = SIGSTOP;
2660 	pid_t child, wpid;
2661 	int lookup_me = 0;
2662 	const int magic = (int)random();
2663 #if defined(TWAIT_HAVE_STATUS)
2664 	int status;
2665 #endif
2666 
2667 	DPRINTF("Before forking process PID=%d\n", getpid());
2668 	SYSCALL_REQUIRE((child = fork()) != -1);
2669 	if (child == 0) {
2670 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2671 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2672 
2673 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2674 		FORKEE_ASSERT(raise(sigval) == 0);
2675 
2676 		FORKEE_ASSERT_EQ(lookup_me, magic);
2677 
2678 		DPRINTF("Before exiting of the child process\n");
2679 		_exit(exitval);
2680 	}
2681 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2682 
2683 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2684 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2685 
2686 	validate_status_stopped(status, sigval);
2687 
2688 	DPRINTF("Write new lookup_me to tracee (PID=%d) from tracer (PID=%d)\n",
2689 	    child, getpid());
2690 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me, magic) != -1);
2691 
2692 	DPRINTF("Before resuming the child process where it left off and "
2693 	    "without signal to be sent\n");
2694 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2695 
2696 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2697 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2698 
2699 	validate_status_exited(status, exitval);
2700 
2701 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2702 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2703 }
2704 
2705 ATF_TC(write_d2);
2706 ATF_TC_HEAD(write_d2, tc)
2707 {
2708 	atf_tc_set_md_var(tc, "descr",
2709 	    "Verify PT_WRITE_D called twice");
2710 }
2711 
2712 ATF_TC_BODY(write_d2, tc)
2713 {
2714 	const int exitval = 5;
2715 	const int sigval = SIGSTOP;
2716 	pid_t child, wpid;
2717 	int lookup_me1 = 0;
2718 	int lookup_me2 = 0;
2719 	const int magic1 = (int)random();
2720 	const int magic2 = (int)random();
2721 #if defined(TWAIT_HAVE_STATUS)
2722 	int status;
2723 #endif
2724 
2725 	DPRINTF("Before forking process PID=%d\n", getpid());
2726 	SYSCALL_REQUIRE((child = fork()) != -1);
2727 	if (child == 0) {
2728 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2729 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2730 
2731 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2732 		FORKEE_ASSERT(raise(sigval) == 0);
2733 
2734 		FORKEE_ASSERT_EQ(lookup_me1, magic1);
2735 		FORKEE_ASSERT_EQ(lookup_me2, magic2);
2736 
2737 		DPRINTF("Before exiting of the child process\n");
2738 		_exit(exitval);
2739 	}
2740 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2741 
2742 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2743 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2744 
2745 	validate_status_stopped(status, sigval);
2746 
2747 	DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
2748 	    child, getpid());
2749 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
2750 
2751 	DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
2752 	    child, getpid());
2753 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
2754 
2755 	DPRINTF("Before resuming the child process where it left off and "
2756 	    "without signal to be sent\n");
2757 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2758 
2759 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2760 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2761 
2762 	validate_status_exited(status, exitval);
2763 
2764 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2765 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2766 }
2767 
2768 ATF_TC(write_d3);
2769 ATF_TC_HEAD(write_d3, tc)
2770 {
2771 	atf_tc_set_md_var(tc, "descr",
2772 	    "Verify PT_WRITE_D called three times");
2773 }
2774 
2775 ATF_TC_BODY(write_d3, tc)
2776 {
2777 	const int exitval = 5;
2778 	const int sigval = SIGSTOP;
2779 	pid_t child, wpid;
2780 	int lookup_me1 = 0;
2781 	int lookup_me2 = 0;
2782 	int lookup_me3 = 0;
2783 	const int magic1 = (int)random();
2784 	const int magic2 = (int)random();
2785 	const int magic3 = (int)random();
2786 #if defined(TWAIT_HAVE_STATUS)
2787 	int status;
2788 #endif
2789 
2790 	DPRINTF("Before forking process PID=%d\n", getpid());
2791 	SYSCALL_REQUIRE((child = fork()) != -1);
2792 	if (child == 0) {
2793 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2794 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2795 
2796 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2797 		FORKEE_ASSERT(raise(sigval) == 0);
2798 
2799 		FORKEE_ASSERT_EQ(lookup_me1, magic1);
2800 		FORKEE_ASSERT_EQ(lookup_me2, magic2);
2801 		FORKEE_ASSERT_EQ(lookup_me3, magic3);
2802 
2803 		DPRINTF("Before exiting of the child process\n");
2804 		_exit(exitval);
2805 	}
2806 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2807 
2808 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2809 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2810 
2811 	validate_status_stopped(status, sigval);
2812 
2813 	DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
2814 	    child, getpid());
2815 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
2816 
2817 	DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
2818 	    child, getpid());
2819 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
2820 
2821 	DPRINTF("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n",
2822 	    child, getpid());
2823 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1);
2824 
2825 	DPRINTF("Before resuming the child process where it left off and "
2826 	    "without signal to be sent\n");
2827 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2828 
2829 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2830 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2831 
2832 	validate_status_exited(status, exitval);
2833 
2834 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2835 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2836 }
2837 
2838 ATF_TC(write_d4);
2839 ATF_TC_HEAD(write_d4, tc)
2840 {
2841 	atf_tc_set_md_var(tc, "descr",
2842 	    "Verify PT_WRITE_D called four times");
2843 }
2844 
2845 ATF_TC_BODY(write_d4, tc)
2846 {
2847 	const int exitval = 5;
2848 	const int sigval = SIGSTOP;
2849 	pid_t child, wpid;
2850 	int lookup_me1 = 0;
2851 	int lookup_me2 = 0;
2852 	int lookup_me3 = 0;
2853 	int lookup_me4 = 0;
2854 	const int magic1 = (int)random();
2855 	const int magic2 = (int)random();
2856 	const int magic3 = (int)random();
2857 	const int magic4 = (int)random();
2858 #if defined(TWAIT_HAVE_STATUS)
2859 	int status;
2860 #endif
2861 
2862 	DPRINTF("Before forking process PID=%d\n", getpid());
2863 	SYSCALL_REQUIRE((child = fork()) != -1);
2864 	if (child == 0) {
2865 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2866 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2867 
2868 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2869 		FORKEE_ASSERT(raise(sigval) == 0);
2870 
2871 		FORKEE_ASSERT_EQ(lookup_me1, magic1);
2872 		FORKEE_ASSERT_EQ(lookup_me2, magic2);
2873 		FORKEE_ASSERT_EQ(lookup_me3, magic3);
2874 		FORKEE_ASSERT_EQ(lookup_me4, magic4);
2875 
2876 		DPRINTF("Before exiting of the child process\n");
2877 		_exit(exitval);
2878 	}
2879 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2880 
2881 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2882 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2883 
2884 	validate_status_stopped(status, sigval);
2885 
2886 	DPRINTF("Write new lookup_me1 to tracee (PID=%d) from tracer (PID=%d)\n",
2887 	    child, getpid());
2888 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me1, magic1) != -1);
2889 
2890 	DPRINTF("Write new lookup_me2 to tracee (PID=%d) from tracer (PID=%d)\n",
2891 	    child, getpid());
2892 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me2, magic2) != -1);
2893 
2894 	DPRINTF("Write new lookup_me3 to tracee (PID=%d) from tracer (PID=%d)\n",
2895 	    child, getpid());
2896 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me3, magic3) != -1);
2897 
2898 	DPRINTF("Write new lookup_me4 to tracee (PID=%d) from tracer (PID=%d)\n",
2899 	    child, getpid());
2900 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, &lookup_me4, magic4) != -1);
2901 
2902 	DPRINTF("Before resuming the child process where it left off and "
2903 	    "without signal to be sent\n");
2904 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2905 
2906 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2907 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2908 
2909 	validate_status_exited(status, exitval);
2910 
2911 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2912 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2913 }
2914 
2915 ATF_TC(io_read_d_write_d_handshake1);
2916 ATF_TC_HEAD(io_read_d_write_d_handshake1, tc)
2917 {
2918 	atf_tc_set_md_var(tc, "descr",
2919 	    "Verify PT_IO with PIOD_READ_D and PIOD_WRITE_D handshake");
2920 }
2921 
2922 ATF_TC_BODY(io_read_d_write_d_handshake1, tc)
2923 {
2924 	const int exitval = 5;
2925 	const int sigval = SIGSTOP;
2926 	pid_t child, wpid;
2927 	uint8_t lookup_me_fromtracee = 0;
2928 	const uint8_t magic_fromtracee = (uint8_t)random();
2929 	uint8_t lookup_me_totracee = 0;
2930 	const uint8_t magic_totracee = (uint8_t)random();
2931 	struct ptrace_io_desc io_fromtracee = {
2932 		.piod_op = PIOD_READ_D,
2933 		.piod_offs = &lookup_me_fromtracee,
2934 		.piod_addr = &lookup_me_fromtracee,
2935 		.piod_len = sizeof(lookup_me_fromtracee)
2936 	};
2937 	struct ptrace_io_desc io_totracee = {
2938 		.piod_op = PIOD_WRITE_D,
2939 		.piod_offs = &lookup_me_totracee,
2940 		.piod_addr = &lookup_me_totracee,
2941 		.piod_len = sizeof(lookup_me_totracee)
2942 	};
2943 #if defined(TWAIT_HAVE_STATUS)
2944 	int status;
2945 #endif
2946 
2947 	DPRINTF("Before forking process PID=%d\n", getpid());
2948 	SYSCALL_REQUIRE((child = fork()) != -1);
2949 	if (child == 0) {
2950 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
2951 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
2952 
2953 		lookup_me_fromtracee = magic_fromtracee;
2954 
2955 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
2956 		FORKEE_ASSERT(raise(sigval) == 0);
2957 
2958 		FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
2959 
2960 		DPRINTF("Before exiting of the child process\n");
2961 		_exit(exitval);
2962 	}
2963 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
2964 
2965 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2966 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2967 
2968 	validate_status_stopped(status, sigval);
2969 
2970 	DPRINTF("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
2971 	    child, getpid());
2972 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1);
2973 
2974 	ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
2975 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee,
2976 	    magic_fromtracee);
2977 
2978 	lookup_me_totracee = magic_totracee;
2979 
2980 	DPRINTF("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n",
2981 	    child, getpid());
2982 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1);
2983 
2984 	ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee,
2985 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee,
2986 	    magic_totracee);
2987 
2988 	DPRINTF("Before resuming the child process where it left off and "
2989 	    "without signal to be sent\n");
2990 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
2991 
2992 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2993 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
2994 
2995 	validate_status_exited(status, exitval);
2996 
2997 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
2998 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
2999 }
3000 
3001 ATF_TC(io_read_d_write_d_handshake2);
3002 ATF_TC_HEAD(io_read_d_write_d_handshake2, tc)
3003 {
3004 	atf_tc_set_md_var(tc, "descr",
3005 	    "Verify PT_IO with PIOD_WRITE_D and PIOD_READ_D handshake");
3006 }
3007 
3008 ATF_TC_BODY(io_read_d_write_d_handshake2, tc)
3009 {
3010 	const int exitval = 5;
3011 	const int sigval = SIGSTOP;
3012 	pid_t child, wpid;
3013 	uint8_t lookup_me_fromtracee = 0;
3014 	const uint8_t magic_fromtracee = (uint8_t)random();
3015 	uint8_t lookup_me_totracee = 0;
3016 	const uint8_t magic_totracee = (uint8_t)random();
3017 	struct ptrace_io_desc io_fromtracee = {
3018 		.piod_op = PIOD_READ_D,
3019 		.piod_offs = &lookup_me_fromtracee,
3020 		.piod_addr = &lookup_me_fromtracee,
3021 		.piod_len = sizeof(lookup_me_fromtracee)
3022 	};
3023 	struct ptrace_io_desc io_totracee = {
3024 		.piod_op = PIOD_WRITE_D,
3025 		.piod_offs = &lookup_me_totracee,
3026 		.piod_addr = &lookup_me_totracee,
3027 		.piod_len = sizeof(lookup_me_totracee)
3028 	};
3029 #if defined(TWAIT_HAVE_STATUS)
3030 	int status;
3031 #endif
3032 
3033 	DPRINTF("Before forking process PID=%d\n", getpid());
3034 	SYSCALL_REQUIRE((child = fork()) != -1);
3035 	if (child == 0) {
3036 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3037 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3038 
3039 		lookup_me_fromtracee = magic_fromtracee;
3040 
3041 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3042 		FORKEE_ASSERT(raise(sigval) == 0);
3043 
3044 		FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
3045 
3046 		DPRINTF("Before exiting of the child process\n");
3047 		_exit(exitval);
3048 	}
3049 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3050 
3051 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3052 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3053 
3054 	validate_status_stopped(status, sigval);
3055 
3056 	lookup_me_totracee = magic_totracee;
3057 
3058 	DPRINTF("Write lookup_me_totracee to PID=%d by tracer (PID=%d)\n",
3059 	    child, getpid());
3060 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_totracee, 0) != -1);
3061 
3062 	ATF_REQUIRE_EQ_MSG(lookup_me_totracee, magic_totracee,
3063 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me_totracee,
3064 	    magic_totracee);
3065 
3066 	DPRINTF("Read lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
3067 	    child, getpid());
3068 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io_fromtracee, 0) != -1);
3069 
3070 	ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
3071 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me_fromtracee,
3072 	    magic_fromtracee);
3073 
3074 	DPRINTF("Before resuming the child process where it left off and "
3075 	    "without signal to be sent\n");
3076 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3077 
3078 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3079 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3080 
3081 	validate_status_exited(status, exitval);
3082 
3083 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3084 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3085 }
3086 
3087 ATF_TC(read_d_write_d_handshake1);
3088 ATF_TC_HEAD(read_d_write_d_handshake1, tc)
3089 {
3090 	atf_tc_set_md_var(tc, "descr",
3091 	    "Verify PT_READ_D with PT_WRITE_D handshake");
3092 }
3093 
3094 ATF_TC_BODY(read_d_write_d_handshake1, tc)
3095 {
3096 	const int exitval = 5;
3097 	const int sigval = SIGSTOP;
3098 	pid_t child, wpid;
3099 	int lookup_me_fromtracee = 0;
3100 	const int magic_fromtracee = (int)random();
3101 	int lookup_me_totracee = 0;
3102 	const int magic_totracee = (int)random();
3103 #if defined(TWAIT_HAVE_STATUS)
3104 	int status;
3105 #endif
3106 
3107 	DPRINTF("Before forking process PID=%d\n", getpid());
3108 	SYSCALL_REQUIRE((child = fork()) != -1);
3109 	if (child == 0) {
3110 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3111 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3112 
3113 		lookup_me_fromtracee = magic_fromtracee;
3114 
3115 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3116 		FORKEE_ASSERT(raise(sigval) == 0);
3117 
3118 		FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
3119 
3120 		DPRINTF("Before exiting of the child process\n");
3121 		_exit(exitval);
3122 	}
3123 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3124 
3125 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3126 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3127 
3128 	validate_status_stopped(status, sigval);
3129 
3130 	DPRINTF("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
3131 	    child, getpid());
3132 	errno = 0;
3133 	lookup_me_fromtracee =
3134 	    ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0);
3135 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3136 
3137 	ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
3138 	    "got value %#x != expected %#x", lookup_me_fromtracee,
3139 	    magic_fromtracee);
3140 
3141 	DPRINTF("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n",
3142 	    child, getpid());
3143 	ATF_REQUIRE
3144 	    (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee)
3145 	    != -1);
3146 
3147 	DPRINTF("Before resuming the child process where it left off and "
3148 	    "without signal to be sent\n");
3149 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3150 
3151 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3152 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3153 
3154 	validate_status_exited(status, exitval);
3155 
3156 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3157 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3158 }
3159 
3160 ATF_TC(read_d_write_d_handshake2);
3161 ATF_TC_HEAD(read_d_write_d_handshake2, tc)
3162 {
3163 	atf_tc_set_md_var(tc, "descr",
3164 	    "Verify PT_WRITE_D with PT_READ_D handshake");
3165 }
3166 
3167 ATF_TC_BODY(read_d_write_d_handshake2, tc)
3168 {
3169 	const int exitval = 5;
3170 	const int sigval = SIGSTOP;
3171 	pid_t child, wpid;
3172 	int lookup_me_fromtracee = 0;
3173 	const int magic_fromtracee = (int)random();
3174 	int lookup_me_totracee = 0;
3175 	const int magic_totracee = (int)random();
3176 #if defined(TWAIT_HAVE_STATUS)
3177 	int status;
3178 #endif
3179 
3180 	DPRINTF("Before forking process PID=%d\n", getpid());
3181 	SYSCALL_REQUIRE((child = fork()) != -1);
3182 	if (child == 0) {
3183 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3184 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3185 
3186 		lookup_me_fromtracee = magic_fromtracee;
3187 
3188 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3189 		FORKEE_ASSERT(raise(sigval) == 0);
3190 
3191 		FORKEE_ASSERT_EQ(lookup_me_totracee, magic_totracee);
3192 
3193 		DPRINTF("Before exiting of the child process\n");
3194 		_exit(exitval);
3195 	}
3196 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3197 
3198 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3199 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3200 
3201 	validate_status_stopped(status, sigval);
3202 
3203 	DPRINTF("Write new lookup_me_totracee to PID=%d from tracer (PID=%d)\n",
3204 	    child, getpid());
3205 	ATF_REQUIRE
3206 	    (ptrace(PT_WRITE_D, child, &lookup_me_totracee, magic_totracee)
3207 	    != -1);
3208 
3209 	DPRINTF("Read new lookup_me_fromtracee PID=%d by tracer (PID=%d)\n",
3210 	    child, getpid());
3211 	errno = 0;
3212 	lookup_me_fromtracee =
3213 	    ptrace(PT_READ_D, child, &lookup_me_fromtracee, 0);
3214 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3215 
3216 	ATF_REQUIRE_EQ_MSG(lookup_me_fromtracee, magic_fromtracee,
3217 	    "got value %#x != expected %#x", lookup_me_fromtracee,
3218 	    magic_fromtracee);
3219 
3220 	DPRINTF("Before resuming the child process where it left off and "
3221 	    "without signal to be sent\n");
3222 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3223 
3224 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3225 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3226 
3227 	validate_status_exited(status, exitval);
3228 
3229 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3230 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3231 }
3232 
3233 /* These dummy functions are used to be copied with ptrace(2) calls */
3234 static int __used
3235 dummy_fn1(int a, int b, int c, int d)
3236 {
3237 
3238 	a *= 1;
3239 	b += 2;
3240 	c -= 3;
3241 	d /= 4;
3242 
3243 	return a + b * c - d;
3244 }
3245 
3246 static int __used
3247 dummy_fn2(int a, int b, int c, int d)
3248 {
3249 
3250 	a *= 4;
3251 	b += 3;
3252 	c -= 2;
3253 	d /= 1;
3254 
3255 	return a + b * c - d;
3256 }
3257 
3258 static int __used
3259 dummy_fn3(int a, int b, int c, int d)
3260 {
3261 
3262 	a *= 10;
3263 	b += 20;
3264 	c -= 30;
3265 	d /= 40;
3266 
3267 	return a + b * c - d;
3268 }
3269 
3270 static int __used
3271 dummy_fn4(int a, int b, int c, int d)
3272 {
3273 
3274 	a *= 40;
3275 	b += 30;
3276 	c -= 20;
3277 	d /= 10;
3278 
3279 	return a + b * c - d;
3280 }
3281 
3282 ATF_TC(io_read_i1);
3283 ATF_TC_HEAD(io_read_i1, tc)
3284 {
3285 	atf_tc_set_md_var(tc, "descr",
3286 	    "Verify PT_IO with PIOD_READ_I and len = sizeof(uint8_t)");
3287 }
3288 
3289 ATF_TC_BODY(io_read_i1, tc)
3290 {
3291 	const int exitval = 5;
3292 	const int sigval = SIGSTOP;
3293 	pid_t child, wpid;
3294 	uint8_t lookup_me = 0;
3295 	uint8_t magic;
3296 	memcpy(&magic, dummy_fn1, sizeof(magic));
3297 	struct ptrace_io_desc io = {
3298 		.piod_op = PIOD_READ_I,
3299 		.piod_offs = dummy_fn1,
3300 		.piod_addr = &lookup_me,
3301 		.piod_len = sizeof(lookup_me)
3302 	};
3303 #if defined(TWAIT_HAVE_STATUS)
3304 	int status;
3305 #endif
3306 
3307 	DPRINTF("Before forking process PID=%d\n", getpid());
3308 	SYSCALL_REQUIRE((child = fork()) != -1);
3309 	if (child == 0) {
3310 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3311 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3312 
3313 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3314 		FORKEE_ASSERT(raise(sigval) == 0);
3315 
3316 		DPRINTF("Before exiting of the child process\n");
3317 		_exit(exitval);
3318 	}
3319 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3320 
3321 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3322 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3323 
3324 	validate_status_stopped(status, sigval);
3325 
3326 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
3327 	    child, getpid());
3328 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
3329 
3330 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
3331 	    "got value %" PRIx8 " != expected %" PRIx8, lookup_me, magic);
3332 
3333 	DPRINTF("Before resuming the child process where it left off and "
3334 	    "without signal to be sent\n");
3335 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3336 
3337 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3338 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3339 
3340 	validate_status_exited(status, exitval);
3341 
3342 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3343 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3344 }
3345 
3346 ATF_TC(io_read_i2);
3347 ATF_TC_HEAD(io_read_i2, tc)
3348 {
3349 	atf_tc_set_md_var(tc, "descr",
3350 	    "Verify PT_IO with PIOD_READ_I and len = sizeof(uint16_t)");
3351 }
3352 
3353 ATF_TC_BODY(io_read_i2, tc)
3354 {
3355 	const int exitval = 5;
3356 	const int sigval = SIGSTOP;
3357 	pid_t child, wpid;
3358 	uint16_t lookup_me = 0;
3359 	uint16_t magic;
3360 	memcpy(&magic, dummy_fn1, sizeof(magic));
3361 	struct ptrace_io_desc io = {
3362 		.piod_op = PIOD_READ_I,
3363 		.piod_offs = dummy_fn1,
3364 		.piod_addr = &lookup_me,
3365 		.piod_len = sizeof(lookup_me)
3366 	};
3367 #if defined(TWAIT_HAVE_STATUS)
3368 	int status;
3369 #endif
3370 
3371 	DPRINTF("Before forking process PID=%d\n", getpid());
3372 	SYSCALL_REQUIRE((child = fork()) != -1);
3373 	if (child == 0) {
3374 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3375 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3376 
3377 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3378 		FORKEE_ASSERT(raise(sigval) == 0);
3379 
3380 		DPRINTF("Before exiting of the child process\n");
3381 		_exit(exitval);
3382 	}
3383 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3384 
3385 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3386 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3387 
3388 	validate_status_stopped(status, sigval);
3389 
3390 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
3391 	    child, getpid());
3392 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
3393 
3394 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
3395 	    "got value %" PRIx16 " != expected %" PRIx16, lookup_me, magic);
3396 
3397 	DPRINTF("Before resuming the child process where it left off and "
3398 	    "without signal to be sent\n");
3399 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3400 
3401 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3402 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3403 
3404 	validate_status_exited(status, exitval);
3405 
3406 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3407 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3408 }
3409 
3410 ATF_TC(io_read_i3);
3411 ATF_TC_HEAD(io_read_i3, tc)
3412 {
3413 	atf_tc_set_md_var(tc, "descr",
3414 	    "Verify PT_IO with PIOD_READ_I and len = sizeof(uint32_t)");
3415 }
3416 
3417 ATF_TC_BODY(io_read_i3, tc)
3418 {
3419 	const int exitval = 5;
3420 	const int sigval = SIGSTOP;
3421 	pid_t child, wpid;
3422 	uint32_t lookup_me = 0;
3423 	uint32_t magic;
3424 	memcpy(&magic, dummy_fn1, sizeof(magic));
3425 	struct ptrace_io_desc io = {
3426 		.piod_op = PIOD_READ_I,
3427 		.piod_offs = dummy_fn1,
3428 		.piod_addr = &lookup_me,
3429 		.piod_len = sizeof(lookup_me)
3430 	};
3431 #if defined(TWAIT_HAVE_STATUS)
3432 	int status;
3433 #endif
3434 
3435 	DPRINTF("Before forking process PID=%d\n", getpid());
3436 	SYSCALL_REQUIRE((child = fork()) != -1);
3437 	if (child == 0) {
3438 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3439 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3440 
3441 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3442 		FORKEE_ASSERT(raise(sigval) == 0);
3443 
3444 		DPRINTF("Before exiting of the child process\n");
3445 		_exit(exitval);
3446 	}
3447 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3448 
3449 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3450 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3451 
3452 	validate_status_stopped(status, sigval);
3453 
3454 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
3455 	    child, getpid());
3456 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
3457 
3458 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
3459 	    "got value %" PRIx32 " != expected %" PRIx32, lookup_me, magic);
3460 
3461 	DPRINTF("Before resuming the child process where it left off and "
3462 	    "without signal to be sent\n");
3463 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3464 
3465 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3466 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3467 
3468 	validate_status_exited(status, exitval);
3469 
3470 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3471 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3472 }
3473 
3474 ATF_TC(io_read_i4);
3475 ATF_TC_HEAD(io_read_i4, tc)
3476 {
3477 	atf_tc_set_md_var(tc, "descr",
3478 	    "Verify PT_IO with PIOD_READ_I and len = sizeof(uint64_t)");
3479 }
3480 
3481 ATF_TC_BODY(io_read_i4, tc)
3482 {
3483 	const int exitval = 5;
3484 	const int sigval = SIGSTOP;
3485 	pid_t child, wpid;
3486 	uint64_t lookup_me = 0;
3487 	uint64_t magic;
3488 	memcpy(&magic, dummy_fn1, sizeof(magic));
3489 	struct ptrace_io_desc io = {
3490 		.piod_op = PIOD_READ_I,
3491 		.piod_offs = dummy_fn1,
3492 		.piod_addr = &lookup_me,
3493 		.piod_len = sizeof(lookup_me)
3494 	};
3495 #if defined(TWAIT_HAVE_STATUS)
3496 	int status;
3497 #endif
3498 
3499 	DPRINTF("Before forking process PID=%d\n", getpid());
3500 	SYSCALL_REQUIRE((child = fork()) != -1);
3501 	if (child == 0) {
3502 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3503 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3504 
3505 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3506 		FORKEE_ASSERT(raise(sigval) == 0);
3507 
3508 		DPRINTF("Before exiting of the child process\n");
3509 		_exit(exitval);
3510 	}
3511 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3512 
3513 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3514 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3515 
3516 	validate_status_stopped(status, sigval);
3517 
3518 	DPRINTF("Read lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
3519 	    child, getpid());
3520 	SYSCALL_REQUIRE(ptrace(PT_IO, child, &io, 0) != -1);
3521 
3522 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
3523 	    "got value %" PRIx64 " != expected %" PRIx64, lookup_me, magic);
3524 
3525 	DPRINTF("Before resuming the child process where it left off and "
3526 	    "without signal to be sent\n");
3527 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3528 
3529 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3530 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3531 
3532 	validate_status_exited(status, exitval);
3533 
3534 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3535 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3536 }
3537 
3538 ATF_TC(read_i1);
3539 ATF_TC_HEAD(read_i1, tc)
3540 {
3541 	atf_tc_set_md_var(tc, "descr",
3542 	    "Verify PT_READ_I called once");
3543 }
3544 
3545 ATF_TC_BODY(read_i1, tc)
3546 {
3547 	const int exitval = 5;
3548 	const int sigval = SIGSTOP;
3549 	pid_t child, wpid;
3550 	int lookup_me = 0;
3551 	int magic;
3552 	memcpy(&magic, dummy_fn1, sizeof(magic));
3553 #if defined(TWAIT_HAVE_STATUS)
3554 	int status;
3555 #endif
3556 
3557 	DPRINTF("Before forking process PID=%d\n", getpid());
3558 	SYSCALL_REQUIRE((child = fork()) != -1);
3559 	if (child == 0) {
3560 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3561 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3562 
3563 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3564 		FORKEE_ASSERT(raise(sigval) == 0);
3565 
3566 		DPRINTF("Before exiting of the child process\n");
3567 		_exit(exitval);
3568 	}
3569 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3570 
3571 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3572 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3573 
3574 	validate_status_stopped(status, sigval);
3575 
3576 	DPRINTF("Read new lookup_me from tracee (PID=%d) by tracer (PID=%d)\n",
3577 	    child, getpid());
3578 	errno = 0;
3579 	lookup_me = ptrace(PT_READ_I, child, dummy_fn1, 0);
3580 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3581 
3582 	ATF_REQUIRE_EQ_MSG(lookup_me, magic,
3583 	    "got value %#x != expected %#x", lookup_me, magic);
3584 
3585 	DPRINTF("Before resuming the child process where it left off and "
3586 	    "without signal to be sent\n");
3587 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3588 
3589 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3590 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3591 
3592 	validate_status_exited(status, exitval);
3593 
3594 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3595 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3596 }
3597 
3598 ATF_TC(read_i2);
3599 ATF_TC_HEAD(read_i2, tc)
3600 {
3601 	atf_tc_set_md_var(tc, "descr",
3602 	    "Verify PT_READ_I called twice");
3603 }
3604 
3605 ATF_TC_BODY(read_i2, tc)
3606 {
3607 	const int exitval = 5;
3608 	const int sigval = SIGSTOP;
3609 	pid_t child, wpid;
3610 	int lookup_me1 = 0;
3611 	int lookup_me2 = 0;
3612 	int magic1;
3613 	int magic2;
3614 	memcpy(&magic1, dummy_fn1, sizeof(magic1));
3615 	memcpy(&magic2, dummy_fn2, sizeof(magic2));
3616 #if defined(TWAIT_HAVE_STATUS)
3617 	int status;
3618 #endif
3619 
3620 	DPRINTF("Before forking process PID=%d\n", getpid());
3621 	SYSCALL_REQUIRE((child = fork()) != -1);
3622 	if (child == 0) {
3623 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3624 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3625 
3626 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3627 		FORKEE_ASSERT(raise(sigval) == 0);
3628 
3629 		DPRINTF("Before exiting of the child process\n");
3630 		_exit(exitval);
3631 	}
3632 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3633 
3634 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3635 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3636 
3637 	validate_status_stopped(status, sigval);
3638 
3639 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
3640 	    child, getpid());
3641 	errno = 0;
3642 	lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
3643 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3644 
3645 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
3646 	    "got value %#x != expected %#x", lookup_me1, magic1);
3647 
3648 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
3649 	    child, getpid());
3650 	errno = 0;
3651 	lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
3652 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3653 
3654 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
3655 	    "got value %#x != expected %#x", lookup_me2, magic2);
3656 
3657 	DPRINTF("Before resuming the child process where it left off and "
3658 	    "without signal to be sent\n");
3659 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3660 
3661 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3662 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3663 
3664 	validate_status_exited(status, exitval);
3665 
3666 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3667 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3668 }
3669 
3670 ATF_TC(read_i3);
3671 ATF_TC_HEAD(read_i3, tc)
3672 {
3673 	atf_tc_set_md_var(tc, "descr",
3674 	    "Verify PT_READ_I called three times");
3675 }
3676 
3677 ATF_TC_BODY(read_i3, tc)
3678 {
3679 	const int exitval = 5;
3680 	const int sigval = SIGSTOP;
3681 	pid_t child, wpid;
3682 	int lookup_me1 = 0;
3683 	int lookup_me2 = 0;
3684 	int lookup_me3 = 0;
3685 	int magic1;
3686 	int magic2;
3687 	int magic3;
3688 	memcpy(&magic1, dummy_fn1, sizeof(magic1));
3689 	memcpy(&magic2, dummy_fn2, sizeof(magic2));
3690 	memcpy(&magic3, dummy_fn3, sizeof(magic3));
3691 #if defined(TWAIT_HAVE_STATUS)
3692 	int status;
3693 #endif
3694 
3695 	DPRINTF("Before forking process PID=%d\n", getpid());
3696 	SYSCALL_REQUIRE((child = fork()) != -1);
3697 	if (child == 0) {
3698 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3699 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3700 
3701 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3702 		FORKEE_ASSERT(raise(sigval) == 0);
3703 
3704 		DPRINTF("Before exiting of the child process\n");
3705 		_exit(exitval);
3706 	}
3707 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3708 
3709 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3710 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3711 
3712 	validate_status_stopped(status, sigval);
3713 
3714 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
3715 	    child, getpid());
3716 	errno = 0;
3717 	lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
3718 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3719 
3720 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
3721 	    "got value %#x != expected %#x", lookup_me1, magic1);
3722 
3723 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
3724 	    child, getpid());
3725 	errno = 0;
3726 	lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
3727 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3728 
3729 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
3730 	    "got value %#x != expected %#x", lookup_me2, magic2);
3731 
3732 	DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
3733 	    child, getpid());
3734 	errno = 0;
3735 	lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0);
3736 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3737 
3738 	ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
3739 	    "got value %#x != expected %#x", lookup_me3, magic3);
3740 
3741 	DPRINTF("Before resuming the child process where it left off and "
3742 	    "without signal to be sent\n");
3743 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3744 
3745 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3746 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3747 
3748 	validate_status_exited(status, exitval);
3749 
3750 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3751 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3752 }
3753 
3754 ATF_TC(read_i4);
3755 ATF_TC_HEAD(read_i4, tc)
3756 {
3757 	atf_tc_set_md_var(tc, "descr",
3758 	    "Verify PT_READ_I called four times");
3759 }
3760 
3761 ATF_TC_BODY(read_i4, tc)
3762 {
3763 	const int exitval = 5;
3764 	const int sigval = SIGSTOP;
3765 	pid_t child, wpid;
3766 	int lookup_me1 = 0;
3767 	int lookup_me2 = 0;
3768 	int lookup_me3 = 0;
3769 	int lookup_me4 = 0;
3770 	int magic1;
3771 	int magic2;
3772 	int magic3;
3773 	int magic4;
3774 	memcpy(&magic1, dummy_fn1, sizeof(magic1));
3775 	memcpy(&magic2, dummy_fn2, sizeof(magic2));
3776 	memcpy(&magic3, dummy_fn3, sizeof(magic3));
3777 	memcpy(&magic4, dummy_fn4, sizeof(magic4));
3778 #if defined(TWAIT_HAVE_STATUS)
3779 	int status;
3780 #endif
3781 
3782 	DPRINTF("Before forking process PID=%d\n", getpid());
3783 	SYSCALL_REQUIRE((child = fork()) != -1);
3784 	if (child == 0) {
3785 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3786 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3787 
3788 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3789 		FORKEE_ASSERT(raise(sigval) == 0);
3790 
3791 		DPRINTF("Before exiting of the child process\n");
3792 		_exit(exitval);
3793 	}
3794 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3795 
3796 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3797 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3798 
3799 	validate_status_stopped(status, sigval);
3800 
3801 	DPRINTF("Read new lookup_me1 from tracee (PID=%d) by tracer (PID=%d)\n",
3802 	    child, getpid());
3803 	errno = 0;
3804 	lookup_me1 = ptrace(PT_READ_I, child, dummy_fn1, 0);
3805 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3806 
3807 	ATF_REQUIRE_EQ_MSG(lookup_me1, magic1,
3808 	    "got value %#x != expected %#x", lookup_me1, magic1);
3809 
3810 	DPRINTF("Read new lookup_me2 from tracee (PID=%d) by tracer (PID=%d)\n",
3811 	    child, getpid());
3812 	errno = 0;
3813 	lookup_me2 = ptrace(PT_READ_I, child, dummy_fn2, 0);
3814 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3815 
3816 	ATF_REQUIRE_EQ_MSG(lookup_me2, magic2,
3817 	    "got value %#x != expected %#x", lookup_me2, magic2);
3818 
3819 	DPRINTF("Read new lookup_me3 from tracee (PID=%d) by tracer (PID=%d)\n",
3820 	    child, getpid());
3821 	errno = 0;
3822 	lookup_me3 = ptrace(PT_READ_I, child, dummy_fn3, 0);
3823 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3824 
3825 	ATF_REQUIRE_EQ_MSG(lookup_me3, magic3,
3826 	    "got value %#x != expected %#x", lookup_me3, magic3);
3827 
3828 	DPRINTF("Read new lookup_me4 from tracee (PID=%d) by tracer (PID=%d)\n",
3829 	    child, getpid());
3830 	errno = 0;
3831 	lookup_me4 = ptrace(PT_READ_I, child, dummy_fn4, 0);
3832 	SYSCALL_REQUIRE_ERRNO(errno, 0);
3833 
3834 	ATF_REQUIRE_EQ_MSG(lookup_me4, magic4,
3835 	    "got value %#x != expected %#x", lookup_me4, magic4);
3836 
3837 	DPRINTF("Before resuming the child process where it left off and "
3838 	    "without signal to be sent\n");
3839 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3840 
3841 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3842 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3843 
3844 	validate_status_exited(status, exitval);
3845 
3846 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3847 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3848 }
3849 
3850 #if defined(HAVE_GPREGS)
3851 ATF_TC(regs1);
3852 ATF_TC_HEAD(regs1, tc)
3853 {
3854 	atf_tc_set_md_var(tc, "descr",
3855 	    "Verify plain PT_GETREGS call without further steps");
3856 }
3857 
3858 ATF_TC_BODY(regs1, tc)
3859 {
3860 	const int exitval = 5;
3861 	const int sigval = SIGSTOP;
3862 	pid_t child, wpid;
3863 #if defined(TWAIT_HAVE_STATUS)
3864 	int status;
3865 #endif
3866 	struct reg r;
3867 
3868 	DPRINTF("Before forking process PID=%d\n", getpid());
3869 	SYSCALL_REQUIRE((child = fork()) != -1);
3870 	if (child == 0) {
3871 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3872 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3873 
3874 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3875 		FORKEE_ASSERT(raise(sigval) == 0);
3876 
3877 		DPRINTF("Before exiting of the child process\n");
3878 		_exit(exitval);
3879 	}
3880 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3881 
3882 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3883 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3884 
3885 	validate_status_stopped(status, sigval);
3886 
3887 	DPRINTF("Call GETREGS for the child process\n");
3888 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
3889 
3890 	DPRINTF("Before resuming the child process where it left off and "
3891 	    "without signal to be sent\n");
3892 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3893 
3894 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3895 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3896 
3897 	validate_status_exited(status, exitval);
3898 
3899 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3900 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3901 }
3902 #endif
3903 
3904 #if defined(HAVE_GPREGS)
3905 ATF_TC(regs2);
3906 ATF_TC_HEAD(regs2, tc)
3907 {
3908 	atf_tc_set_md_var(tc, "descr",
3909 	    "Verify plain PT_GETREGS call and retrieve PC");
3910 }
3911 
3912 ATF_TC_BODY(regs2, tc)
3913 {
3914 	const int exitval = 5;
3915 	const int sigval = SIGSTOP;
3916 	pid_t child, wpid;
3917 #if defined(TWAIT_HAVE_STATUS)
3918 	int status;
3919 #endif
3920 	struct reg r;
3921 
3922 	DPRINTF("Before forking process PID=%d\n", getpid());
3923 	SYSCALL_REQUIRE((child = fork()) != -1);
3924 	if (child == 0) {
3925 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3926 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3927 
3928 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3929 		FORKEE_ASSERT(raise(sigval) == 0);
3930 
3931 		DPRINTF("Before exiting of the child process\n");
3932 		_exit(exitval);
3933 	}
3934 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3935 
3936 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3937 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3938 
3939 	validate_status_stopped(status, sigval);
3940 
3941 	DPRINTF("Call GETREGS for the child process\n");
3942 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
3943 
3944 	DPRINTF("Retrieved PC=%" PRIxREGISTER "\n", PTRACE_REG_PC(&r));
3945 
3946 	DPRINTF("Before resuming the child process where it left off and "
3947 	    "without signal to be sent\n");
3948 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
3949 
3950 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3951 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3952 
3953 	validate_status_exited(status, exitval);
3954 
3955 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3956 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
3957 }
3958 #endif
3959 
3960 #if defined(HAVE_GPREGS)
3961 ATF_TC(regs3);
3962 ATF_TC_HEAD(regs3, tc)
3963 {
3964 	atf_tc_set_md_var(tc, "descr",
3965 	    "Verify plain PT_GETREGS call and retrieve SP");
3966 }
3967 
3968 ATF_TC_BODY(regs3, tc)
3969 {
3970 	const int exitval = 5;
3971 	const int sigval = SIGSTOP;
3972 	pid_t child, wpid;
3973 #if defined(TWAIT_HAVE_STATUS)
3974 	int status;
3975 #endif
3976 	struct reg r;
3977 
3978 	DPRINTF("Before forking process PID=%d\n", getpid());
3979 	SYSCALL_REQUIRE((child = fork()) != -1);
3980 	if (child == 0) {
3981 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
3982 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
3983 
3984 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
3985 		FORKEE_ASSERT(raise(sigval) == 0);
3986 
3987 		DPRINTF("Before exiting of the child process\n");
3988 		_exit(exitval);
3989 	}
3990 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
3991 
3992 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
3993 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
3994 
3995 	validate_status_stopped(status, sigval);
3996 
3997 	DPRINTF("Call GETREGS for the child process\n");
3998 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
3999 
4000 	DPRINTF("Retrieved SP=%" PRIxREGISTER "\n", PTRACE_REG_SP(&r));
4001 
4002 	DPRINTF("Before resuming the child process where it left off and "
4003 	    "without signal to be sent\n");
4004 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4005 
4006 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4007 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4008 
4009 	validate_status_exited(status, exitval);
4010 
4011 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4012 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4013 }
4014 #endif
4015 
4016 #if defined(HAVE_GPREGS)
4017 ATF_TC(regs4);
4018 ATF_TC_HEAD(regs4, tc)
4019 {
4020 	atf_tc_set_md_var(tc, "descr",
4021 	    "Verify plain PT_GETREGS call and retrieve INTRV");
4022 }
4023 
4024 ATF_TC_BODY(regs4, tc)
4025 {
4026 	const int exitval = 5;
4027 	const int sigval = SIGSTOP;
4028 	pid_t child, wpid;
4029 #if defined(TWAIT_HAVE_STATUS)
4030 	int status;
4031 #endif
4032 	struct reg r;
4033 
4034 	DPRINTF("Before forking process PID=%d\n", getpid());
4035 	SYSCALL_REQUIRE((child = fork()) != -1);
4036 	if (child == 0) {
4037 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4038 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4039 
4040 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4041 		FORKEE_ASSERT(raise(sigval) == 0);
4042 
4043 		DPRINTF("Before exiting of the child process\n");
4044 		_exit(exitval);
4045 	}
4046 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4047 
4048 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4049 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4050 
4051 	validate_status_stopped(status, sigval);
4052 
4053 	DPRINTF("Call GETREGS for the child process\n");
4054 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
4055 
4056 	DPRINTF("Retrieved INTRV=%" PRIxREGISTER "\n", PTRACE_REG_INTRV(&r));
4057 
4058 	DPRINTF("Before resuming the child process where it left off and "
4059 	    "without signal to be sent\n");
4060 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4061 
4062 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4063 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4064 
4065 	validate_status_exited(status, exitval);
4066 
4067 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4068 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4069 }
4070 #endif
4071 
4072 #if defined(HAVE_GPREGS)
4073 ATF_TC(regs5);
4074 ATF_TC_HEAD(regs5, tc)
4075 {
4076 	atf_tc_set_md_var(tc, "descr",
4077 	    "Verify PT_GETREGS and PT_SETREGS calls without changing regs");
4078 }
4079 
4080 ATF_TC_BODY(regs5, tc)
4081 {
4082 	const int exitval = 5;
4083 	const int sigval = SIGSTOP;
4084 	pid_t child, wpid;
4085 #if defined(TWAIT_HAVE_STATUS)
4086 	int status;
4087 #endif
4088 	struct reg r;
4089 
4090 	DPRINTF("Before forking process PID=%d\n", getpid());
4091 	SYSCALL_REQUIRE((child = fork()) != -1);
4092 	if (child == 0) {
4093 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4094 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4095 
4096 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4097 		FORKEE_ASSERT(raise(sigval) == 0);
4098 
4099 		DPRINTF("Before exiting of the child process\n");
4100 		_exit(exitval);
4101 	}
4102 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4103 
4104 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4105 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4106 
4107 	validate_status_stopped(status, sigval);
4108 
4109 	DPRINTF("Call GETREGS for the child process\n");
4110 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
4111 
4112 	DPRINTF("Call SETREGS for the child process (without changed regs)\n");
4113 	SYSCALL_REQUIRE(ptrace(PT_GETREGS, child, &r, 0) != -1);
4114 
4115 	DPRINTF("Before resuming the child process where it left off and "
4116 	    "without signal to be sent\n");
4117 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4118 
4119 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4120 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4121 
4122 	validate_status_exited(status, exitval);
4123 
4124 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4125 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4126 }
4127 #endif
4128 
4129 #if defined(HAVE_FPREGS)
4130 ATF_TC(fpregs1);
4131 ATF_TC_HEAD(fpregs1, tc)
4132 {
4133 	atf_tc_set_md_var(tc, "descr",
4134 	    "Verify plain PT_GETFPREGS call without further steps");
4135 }
4136 
4137 ATF_TC_BODY(fpregs1, tc)
4138 {
4139 	const int exitval = 5;
4140 	const int sigval = SIGSTOP;
4141 	pid_t child, wpid;
4142 #if defined(TWAIT_HAVE_STATUS)
4143 	int status;
4144 #endif
4145 	struct fpreg r;
4146 
4147 	DPRINTF("Before forking process PID=%d\n", getpid());
4148 	SYSCALL_REQUIRE((child = fork()) != -1);
4149 	if (child == 0) {
4150 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4151 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4152 
4153 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4154 		FORKEE_ASSERT(raise(sigval) == 0);
4155 
4156 		DPRINTF("Before exiting of the child process\n");
4157 		_exit(exitval);
4158 	}
4159 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4160 
4161 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4162 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4163 
4164 	validate_status_stopped(status, sigval);
4165 
4166 	DPRINTF("Call GETFPREGS for the child process\n");
4167 	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
4168 
4169 	DPRINTF("Before resuming the child process where it left off and "
4170 	    "without signal to be sent\n");
4171 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4172 
4173 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4174 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4175 
4176 	validate_status_exited(status, exitval);
4177 
4178 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4179 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4180 }
4181 #endif
4182 
4183 #if defined(HAVE_FPREGS)
4184 ATF_TC(fpregs2);
4185 ATF_TC_HEAD(fpregs2, tc)
4186 {
4187 	atf_tc_set_md_var(tc, "descr",
4188 	    "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
4189 	    "regs");
4190 }
4191 
4192 ATF_TC_BODY(fpregs2, tc)
4193 {
4194 	const int exitval = 5;
4195 	const int sigval = SIGSTOP;
4196 	pid_t child, wpid;
4197 #if defined(TWAIT_HAVE_STATUS)
4198 	int status;
4199 #endif
4200 	struct fpreg r;
4201 
4202 	DPRINTF("Before forking process PID=%d\n", getpid());
4203 	SYSCALL_REQUIRE((child = fork()) != -1);
4204 	if (child == 0) {
4205 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4206 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4207 
4208 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4209 		FORKEE_ASSERT(raise(sigval) == 0);
4210 
4211 		DPRINTF("Before exiting of the child process\n");
4212 		_exit(exitval);
4213 	}
4214 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4215 
4216 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4217 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4218 
4219 	validate_status_stopped(status, sigval);
4220 
4221 	DPRINTF("Call GETFPREGS for the child process\n");
4222 	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
4223 
4224 	DPRINTF("Call SETFPREGS for the child (without changed regs)\n");
4225 	SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
4226 
4227 	DPRINTF("Before resuming the child process where it left off and "
4228 	    "without signal to be sent\n");
4229 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4230 
4231 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4232 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4233 
4234 	validate_status_exited(status, exitval);
4235 
4236 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4237 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4238 }
4239 #endif
4240 
4241 #if defined(PT_STEP)
4242 static void
4243 ptrace_step(int N, int setstep)
4244 {
4245 	const int exitval = 5;
4246 	const int sigval = SIGSTOP;
4247 	pid_t child, wpid;
4248 #if defined(TWAIT_HAVE_STATUS)
4249 	int status;
4250 #endif
4251 	int happy;
4252 
4253 #if defined(__arm__)
4254 	/* PT_STEP not supported on arm 32-bit */
4255 	atf_tc_expect_fail("PR kern/52119");
4256 #endif
4257 
4258 	DPRINTF("Before forking process PID=%d\n", getpid());
4259 	SYSCALL_REQUIRE((child = fork()) != -1);
4260 	if (child == 0) {
4261 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4262 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4263 
4264 		happy = check_happy(999);
4265 
4266 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4267 		FORKEE_ASSERT(raise(sigval) == 0);
4268 
4269 		FORKEE_ASSERT_EQ(happy, check_happy(999));
4270 
4271 		DPRINTF("Before exiting of the child process\n");
4272 		_exit(exitval);
4273 	}
4274 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4275 
4276 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4277 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4278 
4279 	validate_status_stopped(status, sigval);
4280 
4281 	while (N --> 0) {
4282 		if (setstep) {
4283 			DPRINTF("Before resuming the child process where it "
4284 			    "left off and without signal to be sent (use "
4285 			    "PT_SETSTEP and PT_CONTINUE)\n");
4286 			SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1);
4287 			SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
4288 			    != -1);
4289 		} else {
4290 			DPRINTF("Before resuming the child process where it "
4291 			    "left off and without signal to be sent (use "
4292 			    "PT_STEP)\n");
4293 			SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
4294 			    != -1);
4295 		}
4296 
4297 		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4298 		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4299 		    child);
4300 
4301 		validate_status_stopped(status, SIGTRAP);
4302 
4303 		if (setstep) {
4304 			SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1);
4305 		}
4306 	}
4307 
4308 	DPRINTF("Before resuming the child process where it left off and "
4309 	    "without signal to be sent\n");
4310 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4311 
4312 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4313 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4314 
4315 	validate_status_exited(status, exitval);
4316 
4317 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4318 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4319 }
4320 #endif
4321 
4322 #if defined(PT_STEP)
4323 ATF_TC(step1);
4324 ATF_TC_HEAD(step1, tc)
4325 {
4326 	atf_tc_set_md_var(tc, "descr",
4327 	    "Verify single PT_STEP call");
4328 }
4329 
4330 ATF_TC_BODY(step1, tc)
4331 {
4332 	ptrace_step(1, 0);
4333 }
4334 #endif
4335 
4336 #if defined(PT_STEP)
4337 ATF_TC(step2);
4338 ATF_TC_HEAD(step2, tc)
4339 {
4340 	atf_tc_set_md_var(tc, "descr",
4341 	    "Verify PT_STEP called twice");
4342 }
4343 
4344 ATF_TC_BODY(step2, tc)
4345 {
4346 	ptrace_step(2, 0);
4347 }
4348 #endif
4349 
4350 #if defined(PT_STEP)
4351 ATF_TC(step3);
4352 ATF_TC_HEAD(step3, tc)
4353 {
4354 	atf_tc_set_md_var(tc, "descr",
4355 	    "Verify PT_STEP called three times");
4356 }
4357 
4358 ATF_TC_BODY(step3, tc)
4359 {
4360 	ptrace_step(3, 0);
4361 }
4362 #endif
4363 
4364 #if defined(PT_STEP)
4365 ATF_TC(step4);
4366 ATF_TC_HEAD(step4, tc)
4367 {
4368 	atf_tc_set_md_var(tc, "descr",
4369 	    "Verify PT_STEP called four times");
4370 }
4371 
4372 ATF_TC_BODY(step4, tc)
4373 {
4374 	ptrace_step(4, 0);
4375 }
4376 #endif
4377 
4378 #if defined(PT_STEP)
4379 ATF_TC(setstep1);
4380 ATF_TC_HEAD(setstep1, tc)
4381 {
4382 	atf_tc_set_md_var(tc, "descr",
4383 	    "Verify single PT_SETSTEP call");
4384 }
4385 
4386 ATF_TC_BODY(setstep1, tc)
4387 {
4388 	ptrace_step(1, 1);
4389 }
4390 #endif
4391 
4392 #if defined(PT_STEP)
4393 ATF_TC(setstep2);
4394 ATF_TC_HEAD(setstep2, tc)
4395 {
4396 	atf_tc_set_md_var(tc, "descr",
4397 	    "Verify PT_SETSTEP called twice");
4398 }
4399 
4400 ATF_TC_BODY(setstep2, tc)
4401 {
4402 	ptrace_step(2, 1);
4403 }
4404 #endif
4405 
4406 #if defined(PT_STEP)
4407 ATF_TC(setstep3);
4408 ATF_TC_HEAD(setstep3, tc)
4409 {
4410 	atf_tc_set_md_var(tc, "descr",
4411 	    "Verify PT_SETSTEP called three times");
4412 }
4413 
4414 ATF_TC_BODY(setstep3, tc)
4415 {
4416 	ptrace_step(3, 1);
4417 }
4418 #endif
4419 
4420 #if defined(PT_STEP)
4421 ATF_TC(setstep4);
4422 ATF_TC_HEAD(setstep4, tc)
4423 {
4424 	atf_tc_set_md_var(tc, "descr",
4425 	    "Verify PT_SETSTEP called four times");
4426 }
4427 
4428 ATF_TC_BODY(setstep4, tc)
4429 {
4430 	ptrace_step(4, 1);
4431 }
4432 #endif
4433 
4434 ATF_TC(kill1);
4435 ATF_TC_HEAD(kill1, tc)
4436 {
4437 	atf_tc_set_md_var(tc, "descr",
4438 	    "Verify that PT_CONTINUE with SIGKILL terminates child");
4439 }
4440 
4441 ATF_TC_BODY(kill1, tc)
4442 {
4443 	const int sigval = SIGSTOP, sigsent = SIGKILL;
4444 	pid_t child, wpid;
4445 #if defined(TWAIT_HAVE_STATUS)
4446 	int status;
4447 #endif
4448 
4449 	DPRINTF("Before forking process PID=%d\n", getpid());
4450 	SYSCALL_REQUIRE((child = fork()) != -1);
4451 	if (child == 0) {
4452 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4453 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4454 
4455 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4456 		FORKEE_ASSERT(raise(sigval) == 0);
4457 
4458 		/* NOTREACHED */
4459 		FORKEE_ASSERTX(0 &&
4460 		    "Child should be terminated by a signal from its parent");
4461 	}
4462 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4463 
4464 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4465 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4466 
4467 	validate_status_stopped(status, sigval);
4468 
4469 	DPRINTF("Before resuming the child process where it left off and "
4470 	    "without signal to be sent\n");
4471 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
4472 
4473 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4474 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4475 
4476 	validate_status_signaled(status, sigsent, 0);
4477 
4478 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4479 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4480 }
4481 
4482 ATF_TC(kill2);
4483 ATF_TC_HEAD(kill2, tc)
4484 {
4485 	atf_tc_set_md_var(tc, "descr",
4486 	    "Verify that PT_KILL terminates child");
4487 }
4488 
4489 ATF_TC_BODY(kill2, tc)
4490 {
4491 	const int sigval = SIGSTOP;
4492 	pid_t child, wpid;
4493 #if defined(TWAIT_HAVE_STATUS)
4494 	int status;
4495 #endif
4496 
4497 	DPRINTF("Before forking process PID=%d\n", getpid());
4498 	SYSCALL_REQUIRE((child = fork()) != -1);
4499 	if (child == 0) {
4500 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4501 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4502 
4503 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4504 		FORKEE_ASSERT(raise(sigval) == 0);
4505 
4506 		/* NOTREACHED */
4507 		FORKEE_ASSERTX(0 &&
4508 		    "Child should be terminated by a signal from its parent");
4509 	}
4510 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4511 
4512 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4513 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4514 
4515 	validate_status_stopped(status, sigval);
4516 
4517 	DPRINTF("Before resuming the child process where it left off and "
4518 	    "without signal to be sent\n");
4519 	SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
4520 
4521 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4522 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4523 
4524 	validate_status_signaled(status, SIGKILL, 0);
4525 
4526 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4527 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4528 }
4529 
4530 ATF_TC(lwpinfo1);
4531 ATF_TC_HEAD(lwpinfo1, tc)
4532 {
4533 	atf_tc_set_md_var(tc, "descr",
4534 	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
4535 }
4536 
4537 ATF_TC_BODY(lwpinfo1, tc)
4538 {
4539 	const int exitval = 5;
4540 	const int sigval = SIGSTOP;
4541 	pid_t child, wpid;
4542 #if defined(TWAIT_HAVE_STATUS)
4543 	int status;
4544 #endif
4545 	struct ptrace_lwpinfo info = {0, 0};
4546 
4547 	DPRINTF("Before forking process PID=%d\n", getpid());
4548 	SYSCALL_REQUIRE((child = fork()) != -1);
4549 	if (child == 0) {
4550 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4551 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4552 
4553 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4554 		FORKEE_ASSERT(raise(sigval) == 0);
4555 
4556 		DPRINTF("Before exiting of the child process\n");
4557 		_exit(exitval);
4558 	}
4559 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4560 
4561 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4562 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4563 
4564 	validate_status_stopped(status, sigval);
4565 
4566 	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4567 	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4568 
4569 	DPRINTF("Assert that there exists a thread\n");
4570 	ATF_REQUIRE(info.pl_lwpid > 0);
4571 
4572 	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
4573 	    info.pl_lwpid);
4574 	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
4575 	    "Received event %d != expected event %d",
4576 	    info.pl_event, PL_EVENT_SIGNAL);
4577 
4578 	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4579 	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4580 
4581 	DPRINTF("Assert that there are no more lwp threads in child\n");
4582 	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
4583 
4584 	DPRINTF("Before resuming the child process where it left off and "
4585 	    "without signal to be sent\n");
4586 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4587 
4588 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4589 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4590 
4591 	validate_status_exited(status, exitval);
4592 
4593 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4594 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4595 }
4596 
4597 #if defined(TWAIT_HAVE_PID)
4598 ATF_TC(lwpinfo2);
4599 ATF_TC_HEAD(lwpinfo2, tc)
4600 {
4601 	atf_tc_set_md_var(tc, "descr",
4602 	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
4603 	    "tracer)");
4604 }
4605 
4606 ATF_TC_BODY(lwpinfo2, tc)
4607 {
4608 	struct msg_fds parent_tracee, parent_tracer;
4609 	const int exitval_tracee = 5;
4610 	const int exitval_tracer = 10;
4611 	pid_t tracee, tracer, wpid;
4612 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4613 #if defined(TWAIT_HAVE_STATUS)
4614 	int status;
4615 #endif
4616 	struct ptrace_lwpinfo info = {0, 0};
4617 
4618 	DPRINTF("Spawn tracee\n");
4619 	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
4620 	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
4621 	tracee = atf_utils_fork();
4622 	if (tracee == 0) {
4623 
4624 		/* Wait for message from the parent */
4625 		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
4626 		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
4627 
4628 		_exit(exitval_tracee);
4629 	}
4630 	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
4631 
4632 	DPRINTF("Spawn debugger\n");
4633 	tracer = atf_utils_fork();
4634 	if (tracer == 0) {
4635 		/* No IPC to communicate with the child */
4636 		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
4637 		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
4638 
4639 		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
4640 		FORKEE_REQUIRE_SUCCESS(
4641 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4642 
4643 		forkee_status_stopped(status, SIGSTOP);
4644 
4645 		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4646 		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4647 		    != -1);
4648 
4649 		DPRINTF("Assert that there exists a thread\n");
4650 		FORKEE_ASSERTX(info.pl_lwpid > 0);
4651 
4652 		DPRINTF("Assert that lwp thread %d received event "
4653 		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
4654 		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
4655 
4656 		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4657 		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4658 		    != -1);
4659 
4660 		DPRINTF("Assert that there are no more lwp threads in child\n");
4661 		FORKEE_ASSERTX(info.pl_lwpid == 0);
4662 
4663 		/* Resume tracee with PT_CONTINUE */
4664 		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
4665 
4666 		/* Inform parent that tracer has attached to tracee */
4667 		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
4668 		/* Wait for parent */
4669 		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
4670 
4671 		/* Wait for tracee and assert that it exited */
4672 		FORKEE_REQUIRE_SUCCESS(
4673 		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4674 
4675 		forkee_status_exited(status, exitval_tracee);
4676 
4677 		DPRINTF("Before exiting of the tracer process\n");
4678 		_exit(exitval_tracer);
4679 	}
4680 
4681 	DPRINTF("Wait for the tracer to attach to the tracee\n");
4682 	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
4683 
4684 	DPRINTF("Resume the tracee and let it exit\n");
4685 	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
4686 
4687 	DPRINTF("Detect that tracee is zombie\n");
4688 	await_zombie(tracee);
4689 
4690 	DPRINTF("Assert that there is no status about tracee - "
4691 	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
4692 	TWAIT_REQUIRE_SUCCESS(
4693 	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
4694 
4695 	DPRINTF("Resume the tracer and let it detect exited tracee\n");
4696 	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
4697 
4698 	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
4699 	    TWAIT_FNAME);
4700 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
4701 	    tracer);
4702 
4703 	validate_status_exited(status, exitval_tracer);
4704 
4705 	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
4706 	    TWAIT_FNAME);
4707 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
4708 	    tracee);
4709 
4710 	validate_status_exited(status, exitval_tracee);
4711 
4712 	msg_close(&parent_tracer);
4713 	msg_close(&parent_tracee);
4714 }
4715 #endif
4716 
4717 ATF_TC(siginfo1);
4718 ATF_TC_HEAD(siginfo1, tc)
4719 {
4720 	atf_tc_set_md_var(tc, "descr",
4721 	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
4722 }
4723 
4724 ATF_TC_BODY(siginfo1, tc)
4725 {
4726 	const int exitval = 5;
4727 	const int sigval = SIGTRAP;
4728 	pid_t child, wpid;
4729 #if defined(TWAIT_HAVE_STATUS)
4730 	int status;
4731 #endif
4732 	struct ptrace_siginfo info;
4733 	memset(&info, 0, sizeof(info));
4734 
4735 	DPRINTF("Before forking process PID=%d\n", getpid());
4736 	SYSCALL_REQUIRE((child = fork()) != -1);
4737 	if (child == 0) {
4738 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4739 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4740 
4741 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4742 		FORKEE_ASSERT(raise(sigval) == 0);
4743 
4744 		DPRINTF("Before exiting of the child process\n");
4745 		_exit(exitval);
4746 	}
4747 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4748 
4749 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4750 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4751 
4752 	validate_status_stopped(status, sigval);
4753 
4754 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4755 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4756 
4757 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4758 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4759 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4760 	    info.psi_siginfo.si_errno);
4761 
4762 	DPRINTF("Before resuming the child process where it left off and "
4763 	    "without signal to be sent\n");
4764 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4765 
4766 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4767 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4768 
4769 	validate_status_exited(status, exitval);
4770 
4771 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4772 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4773 }
4774 
4775 ATF_TC(siginfo2);
4776 ATF_TC_HEAD(siginfo2, tc)
4777 {
4778 	atf_tc_set_md_var(tc, "descr",
4779 	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
4780 	    "modification of SIGINT from tracee");
4781 }
4782 
4783 static int siginfo2_caught = 0;
4784 
4785 static void
4786 siginfo2_sighandler(int sig)
4787 {
4788 	FORKEE_ASSERT_EQ(sig, SIGINT);
4789 
4790 	++siginfo2_caught;
4791 }
4792 
4793 ATF_TC_BODY(siginfo2, tc)
4794 {
4795 	const int exitval = 5;
4796 	const int sigval = SIGINT;
4797 	pid_t child, wpid;
4798 	struct sigaction sa;
4799 #if defined(TWAIT_HAVE_STATUS)
4800 	int status;
4801 #endif
4802 	struct ptrace_siginfo info;
4803 	memset(&info, 0, sizeof(info));
4804 
4805 	DPRINTF("Before forking process PID=%d\n", getpid());
4806 	SYSCALL_REQUIRE((child = fork()) != -1);
4807 	if (child == 0) {
4808 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4809 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4810 
4811 		sa.sa_handler = siginfo2_sighandler;
4812 		sa.sa_flags = SA_SIGINFO;
4813 		sigemptyset(&sa.sa_mask);
4814 
4815 		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
4816 
4817 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4818 		FORKEE_ASSERT(raise(sigval) == 0);
4819 
4820 		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
4821 
4822 		DPRINTF("Before exiting of the child process\n");
4823 		_exit(exitval);
4824 	}
4825 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4826 
4827 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4828 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4829 
4830 	validate_status_stopped(status, sigval);
4831 
4832 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4833 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4834 
4835 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4836 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4837 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4838 	    info.psi_siginfo.si_errno);
4839 
4840 	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
4841 	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
4842 
4843 	DPRINTF("Before resuming the child process where it left off and "
4844 	    "without signal to be sent\n");
4845 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
4846 
4847 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4848 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4849 
4850 	validate_status_exited(status, exitval);
4851 
4852 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4853 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4854 }
4855 
4856 ATF_TC(siginfo3);
4857 ATF_TC_HEAD(siginfo3, tc)
4858 {
4859 	atf_tc_set_md_var(tc, "descr",
4860 	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
4861 	    "setting signal to new value");
4862 }
4863 
4864 static int siginfo3_caught = 0;
4865 
4866 static void
4867 siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
4868 {
4869 	FORKEE_ASSERT_EQ(sig, SIGTRAP);
4870 
4871 	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
4872 	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
4873 
4874 	++siginfo3_caught;
4875 }
4876 
4877 ATF_TC_BODY(siginfo3, tc)
4878 {
4879 	const int exitval = 5;
4880 	const int sigval = SIGINT;
4881 	const int sigfaked = SIGTRAP;
4882 	const int sicodefaked = TRAP_BRKPT;
4883 	pid_t child, wpid;
4884 	struct sigaction sa;
4885 #if defined(TWAIT_HAVE_STATUS)
4886 	int status;
4887 #endif
4888 	struct ptrace_siginfo info;
4889 	memset(&info, 0, sizeof(info));
4890 
4891 	DPRINTF("Before forking process PID=%d\n", getpid());
4892 	SYSCALL_REQUIRE((child = fork()) != -1);
4893 	if (child == 0) {
4894 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4895 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4896 
4897 		sa.sa_sigaction = siginfo3_sigaction;
4898 		sa.sa_flags = SA_SIGINFO;
4899 		sigemptyset(&sa.sa_mask);
4900 
4901 		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
4902 
4903 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4904 		FORKEE_ASSERT(raise(sigval) == 0);
4905 
4906 		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
4907 
4908 		DPRINTF("Before exiting of the child process\n");
4909 		_exit(exitval);
4910 	}
4911 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4912 
4913 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4914 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4915 
4916 	validate_status_stopped(status, sigval);
4917 
4918 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4919 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4920 
4921 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4922 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4923 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4924 	    info.psi_siginfo.si_errno);
4925 
4926 	DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n",
4927 	    sigfaked, sicodefaked);
4928 	info.psi_siginfo.si_signo = sigfaked;
4929 	info.psi_siginfo.si_code = sicodefaked;
4930 
4931 	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
4932 	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
4933 
4934 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4935 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4936 
4937 	DPRINTF("Before checking siginfo_t\n");
4938 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
4939 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
4940 
4941 	DPRINTF("Before resuming the child process where it left off and "
4942 	    "without signal to be sent\n");
4943 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
4944 
4945 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4946 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4947 
4948 	validate_status_exited(status, exitval);
4949 
4950 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4951 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4952 }
4953 
4954 ATF_TC(siginfo4);
4955 ATF_TC_HEAD(siginfo4, tc)
4956 {
4957 	atf_tc_set_md_var(tc, "descr",
4958 	    "Detect SIGTRAP TRAP_EXEC from tracee");
4959 }
4960 
4961 ATF_TC_BODY(siginfo4, tc)
4962 {
4963 	const int sigval = SIGTRAP;
4964 	pid_t child, wpid;
4965 #if defined(TWAIT_HAVE_STATUS)
4966 	int status;
4967 #endif
4968 
4969 	struct ptrace_siginfo info;
4970 	memset(&info, 0, sizeof(info));
4971 
4972 	DPRINTF("Before forking process PID=%d\n", getpid());
4973 	SYSCALL_REQUIRE((child = fork()) != -1);
4974 	if (child == 0) {
4975 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4976 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4977 
4978 		DPRINTF("Before calling execve(2) from child\n");
4979 		execlp("/bin/echo", "/bin/echo", NULL);
4980 
4981 		FORKEE_ASSERT(0 && "Not reached");
4982 	}
4983 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4984 
4985 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4986 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4987 
4988 	validate_status_stopped(status, sigval);
4989 
4990 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4991 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4992 
4993 	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4994 	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4995 	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4996 	    info.psi_siginfo.si_errno);
4997 
4998 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
4999 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
5000 
5001 	DPRINTF("Before resuming the child process where it left off and "
5002 	    "without signal to be sent\n");
5003 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5004 
5005 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5006 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5007 
5008 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5009 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5010 }
5011 
5012 #if defined(TWAIT_HAVE_PID)
5013 ATF_TC(siginfo5);
5014 ATF_TC_HEAD(siginfo5, tc)
5015 {
5016 	atf_tc_set_md_var(tc, "descr",
5017 	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
5018 	    "set to PTRACE_FORK and reports correct signal information");
5019 }
5020 
5021 ATF_TC_BODY(siginfo5, tc)
5022 {
5023 	const int exitval = 5;
5024 	const int exitval2 = 15;
5025 	const int sigval = SIGSTOP;
5026 	pid_t child, child2, wpid;
5027 #if defined(TWAIT_HAVE_STATUS)
5028 	int status;
5029 #endif
5030 	ptrace_state_t state;
5031 	const int slen = sizeof(state);
5032 	ptrace_event_t event;
5033 	const int elen = sizeof(event);
5034 	struct ptrace_siginfo info;
5035 
5036 	memset(&info, 0, sizeof(info));
5037 
5038 	DPRINTF("Before forking process PID=%d\n", getpid());
5039 	SYSCALL_REQUIRE((child = fork()) != -1);
5040 	if (child == 0) {
5041 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5042 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5043 
5044 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5045 		FORKEE_ASSERT(raise(sigval) == 0);
5046 
5047 		FORKEE_ASSERT((child2 = fork()) != -1);
5048 
5049 		if (child2 == 0)
5050 			_exit(exitval2);
5051 
5052 		FORKEE_REQUIRE_SUCCESS
5053 		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5054 
5055 		forkee_status_exited(status, exitval2);
5056 
5057 		DPRINTF("Before exiting of the child process\n");
5058 		_exit(exitval);
5059 	}
5060 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5061 
5062 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5063 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5064 
5065 	validate_status_stopped(status, sigval);
5066 
5067 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5068 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5069 
5070 	DPRINTF("Before checking siginfo_t\n");
5071 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5072 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5073 
5074 	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
5075 	event.pe_set_event = PTRACE_FORK;
5076 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5077 
5078 	DPRINTF("Before resuming the child process where it left off and "
5079 	    "without signal to be sent\n");
5080         DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
5081                "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
5082                "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
5083                 "state.pe_other_pid=child)\n", child);
5084 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5085 
5086 	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
5087 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5088 
5089 	validate_status_stopped(status, SIGTRAP);
5090 
5091 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5092 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5093 
5094 	DPRINTF("Before checking siginfo_t\n");
5095 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5096 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5097 
5098 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5099 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5100 
5101 	child2 = state.pe_other_pid;
5102 	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
5103 
5104 	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
5105 	    TWAIT_FNAME, child2, child);
5106 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5107 	    child2);
5108 
5109 	validate_status_stopped(status, SIGTRAP);
5110 
5111 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5112 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5113 
5114 	DPRINTF("Before checking siginfo_t\n");
5115 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5116 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5117 
5118 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
5119 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5120 	ATF_REQUIRE_EQ(state.pe_other_pid, child);
5121 
5122 	DPRINTF("Before resuming the forkee process where it left off and "
5123 	    "without signal to be sent\n");
5124 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
5125 
5126 	DPRINTF("Before resuming the child process where it left off and "
5127 	    "without signal to be sent\n");
5128 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5129 
5130 	DPRINTF("Before calling %s() for the forkee - expected exited\n",
5131 	    TWAIT_FNAME);
5132 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5133 	    child2);
5134 
5135 	validate_status_exited(status, exitval2);
5136 
5137 	DPRINTF("Before calling %s() for the forkee - expected no process\n",
5138 	    TWAIT_FNAME);
5139 	TWAIT_REQUIRE_FAILURE(ECHILD,
5140 	    wpid = TWAIT_GENERIC(child2, &status, 0));
5141 
5142 	DPRINTF("Before calling %s() for the child - expected stopped "
5143 	    "SIGCHLD\n", TWAIT_FNAME);
5144 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5145 
5146 	validate_status_stopped(status, SIGCHLD);
5147 
5148 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5149 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5150 
5151 	DPRINTF("Before checking siginfo_t\n");
5152 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
5153 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
5154 
5155 	DPRINTF("Before resuming the child process where it left off and "
5156 	    "without signal to be sent\n");
5157 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5158 
5159 	DPRINTF("Before calling %s() for the child - expected exited\n",
5160 	    TWAIT_FNAME);
5161 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5162 
5163 	validate_status_exited(status, exitval);
5164 
5165 	DPRINTF("Before calling %s() for the child - expected no process\n",
5166 	    TWAIT_FNAME);
5167 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5168 }
5169 #endif
5170 
5171 #if defined(PT_STEP)
5172 ATF_TC(siginfo6);
5173 ATF_TC_HEAD(siginfo6, tc)
5174 {
5175 	atf_tc_set_md_var(tc, "descr",
5176 	    "Verify single PT_STEP call with signal information check");
5177 }
5178 
5179 ATF_TC_BODY(siginfo6, tc)
5180 {
5181 	const int exitval = 5;
5182 	const int sigval = SIGSTOP;
5183 	pid_t child, wpid;
5184 #if defined(TWAIT_HAVE_STATUS)
5185 	int status;
5186 #endif
5187 	int happy;
5188 	struct ptrace_siginfo info;
5189 
5190 #if defined(__arm__)
5191 	/* PT_STEP not supported on arm 32-bit */
5192 	atf_tc_expect_fail("PR kern/52119");
5193 #endif
5194 
5195 	memset(&info, 0, sizeof(info));
5196 
5197 	DPRINTF("Before forking process PID=%d\n", getpid());
5198 	SYSCALL_REQUIRE((child = fork()) != -1);
5199 	if (child == 0) {
5200 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5201 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5202 
5203 		happy = check_happy(100);
5204 
5205 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5206 		FORKEE_ASSERT(raise(sigval) == 0);
5207 
5208 		FORKEE_ASSERT_EQ(happy, check_happy(100));
5209 
5210 		DPRINTF("Before exiting of the child process\n");
5211 		_exit(exitval);
5212 	}
5213 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5214 
5215 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5216 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5217 
5218 	validate_status_stopped(status, sigval);
5219 
5220 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5221 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5222 
5223 	DPRINTF("Before checking siginfo_t\n");
5224 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5225 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5226 
5227 	DPRINTF("Before resuming the child process where it left off and "
5228 	    "without signal to be sent (use PT_STEP)\n");
5229 	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5230 
5231 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5232 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5233 
5234 	validate_status_stopped(status, SIGTRAP);
5235 
5236 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5237 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5238 
5239 	DPRINTF("Before checking siginfo_t\n");
5240 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5241 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
5242 
5243 	DPRINTF("Before resuming the child process where it left off and "
5244 	    "without signal to be sent\n");
5245 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5246 
5247 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5248 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5249 
5250 	validate_status_exited(status, exitval);
5251 
5252 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5253 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5254 }
5255 #endif
5256 
5257 volatile lwpid_t the_lwp_id = 0;
5258 
5259 static void
5260 lwp_main_func(void *arg)
5261 {
5262 	the_lwp_id = _lwp_self();
5263 	_lwp_exit();
5264 }
5265 
5266 ATF_TC(lwp_create1);
5267 ATF_TC_HEAD(lwp_create1, tc)
5268 {
5269 	atf_tc_set_md_var(tc, "descr",
5270 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5271 	    "EVENT_MASK set to PTRACE_LWP_CREATE");
5272 }
5273 
5274 ATF_TC_BODY(lwp_create1, tc)
5275 {
5276 	const int exitval = 5;
5277 	const int sigval = SIGSTOP;
5278 	pid_t child, wpid;
5279 #if defined(TWAIT_HAVE_STATUS)
5280 	int status;
5281 #endif
5282 	ptrace_state_t state;
5283 	const int slen = sizeof(state);
5284 	ptrace_event_t event;
5285 	const int elen = sizeof(event);
5286 	ucontext_t uc;
5287 	lwpid_t lid;
5288 	static const size_t ssize = 16*1024;
5289 	void *stack;
5290 
5291 	DPRINTF("Before forking process PID=%d\n", getpid());
5292 	SYSCALL_REQUIRE((child = fork()) != -1);
5293 	if (child == 0) {
5294 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5295 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5296 
5297 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5298 		FORKEE_ASSERT(raise(sigval) == 0);
5299 
5300 		DPRINTF("Before allocating memory for stack in child\n");
5301 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5302 
5303 		DPRINTF("Before making context for new lwp in child\n");
5304 		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5305 
5306 		DPRINTF("Before creating new in child\n");
5307 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5308 
5309 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
5310 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5311 
5312 		DPRINTF("Before verifying that reported %d and running lid %d "
5313 		    "are the same\n", lid, the_lwp_id);
5314 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5315 
5316 		DPRINTF("Before exiting of the child process\n");
5317 		_exit(exitval);
5318 	}
5319 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5320 
5321 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5322 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5323 
5324 	validate_status_stopped(status, sigval);
5325 
5326 	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
5327 	event.pe_set_event = PTRACE_LWP_CREATE;
5328 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5329 
5330 	DPRINTF("Before resuming the child process where it left off and "
5331 	    "without signal to be sent\n");
5332 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5333 
5334 	DPRINTF("Before calling %s() for the child - expected stopped "
5335 	    "SIGTRAP\n", TWAIT_FNAME);
5336 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5337 
5338 	validate_status_stopped(status, SIGTRAP);
5339 
5340 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5341 
5342 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
5343 
5344 	lid = state.pe_lwp;
5345 	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
5346 
5347 	DPRINTF("Before resuming the child process where it left off and "
5348 	    "without signal to be sent\n");
5349 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5350 
5351 	DPRINTF("Before calling %s() for the child - expected exited\n",
5352 	    TWAIT_FNAME);
5353 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5354 
5355 	validate_status_exited(status, exitval);
5356 
5357 	DPRINTF("Before calling %s() for the child - expected no process\n",
5358 	    TWAIT_FNAME);
5359 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5360 }
5361 
5362 ATF_TC(lwp_exit1);
5363 ATF_TC_HEAD(lwp_exit1, tc)
5364 {
5365 	atf_tc_set_md_var(tc, "descr",
5366 	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5367 	    "EVENT_MASK set to PTRACE_LWP_EXIT");
5368 }
5369 
5370 ATF_TC_BODY(lwp_exit1, tc)
5371 {
5372 	const int exitval = 5;
5373 	const int sigval = SIGSTOP;
5374 	pid_t child, wpid;
5375 #if defined(TWAIT_HAVE_STATUS)
5376 	int status;
5377 #endif
5378 	ptrace_state_t state;
5379 	const int slen = sizeof(state);
5380 	ptrace_event_t event;
5381 	const int elen = sizeof(event);
5382 	ucontext_t uc;
5383 	lwpid_t lid;
5384 	static const size_t ssize = 16*1024;
5385 	void *stack;
5386 
5387 	DPRINTF("Before forking process PID=%d\n", getpid());
5388 	SYSCALL_REQUIRE((child = fork()) != -1);
5389 	if (child == 0) {
5390 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5391 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5392 
5393 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5394 		FORKEE_ASSERT(raise(sigval) == 0);
5395 
5396 		DPRINTF("Before allocating memory for stack in child\n");
5397 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5398 
5399 		DPRINTF("Before making context for new lwp in child\n");
5400 		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5401 
5402 		DPRINTF("Before creating new in child\n");
5403 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5404 
5405 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
5406 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5407 
5408 		DPRINTF("Before verifying that reported %d and running lid %d "
5409 		    "are the same\n", lid, the_lwp_id);
5410 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5411 
5412 		DPRINTF("Before exiting of the child process\n");
5413 		_exit(exitval);
5414 	}
5415 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5416 
5417 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5418 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5419 
5420 	validate_status_stopped(status, sigval);
5421 
5422 	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
5423 	event.pe_set_event = PTRACE_LWP_EXIT;
5424 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5425 
5426 	DPRINTF("Before resuming the child process where it left off and "
5427 	    "without signal to be sent\n");
5428 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5429 
5430 	DPRINTF("Before calling %s() for the child - expected stopped "
5431 	    "SIGTRAP\n", TWAIT_FNAME);
5432 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5433 
5434 	validate_status_stopped(status, SIGTRAP);
5435 
5436 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5437 
5438 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
5439 
5440 	lid = state.pe_lwp;
5441 	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
5442 
5443 	DPRINTF("Before resuming the child process where it left off and "
5444 	    "without signal to be sent\n");
5445 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5446 
5447 	DPRINTF("Before calling %s() for the child - expected exited\n",
5448 	    TWAIT_FNAME);
5449 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5450 
5451 	validate_status_exited(status, exitval);
5452 
5453 	DPRINTF("Before calling %s() for the child - expected no process\n",
5454 	    TWAIT_FNAME);
5455 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5456 }
5457 
5458 ATF_TC(signal1);
5459 ATF_TC_HEAD(signal1, tc)
5460 {
5461 	atf_tc_set_md_var(tc, "descr",
5462 	    "Verify that masking single unrelated signal does not stop tracer "
5463 	    "from catching other signals");
5464 }
5465 
5466 ATF_TC_BODY(signal1, tc)
5467 {
5468 	const int exitval = 5;
5469 	const int sigval = SIGSTOP;
5470 	const int sigmasked = SIGTRAP;
5471 	const int signotmasked = SIGINT;
5472 	pid_t child, wpid;
5473 #if defined(TWAIT_HAVE_STATUS)
5474 	int status;
5475 #endif
5476 	sigset_t intmask;
5477 
5478 	DPRINTF("Before forking process PID=%d\n", getpid());
5479 	SYSCALL_REQUIRE((child = fork()) != -1);
5480 	if (child == 0) {
5481 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5482 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5483 
5484 		sigemptyset(&intmask);
5485 		sigaddset(&intmask, sigmasked);
5486 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5487 
5488 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5489 		FORKEE_ASSERT(raise(sigval) == 0);
5490 
5491 		DPRINTF("Before raising %s from child\n",
5492 		    strsignal(signotmasked));
5493 		FORKEE_ASSERT(raise(signotmasked) == 0);
5494 
5495 		DPRINTF("Before exiting of the child process\n");
5496 		_exit(exitval);
5497 	}
5498 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5499 
5500 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5501 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5502 
5503 	validate_status_stopped(status, sigval);
5504 
5505 	DPRINTF("Before resuming the child process where it left off and "
5506 	    "without signal to be sent\n");
5507 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5508 
5509 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5510 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5511 
5512 	validate_status_stopped(status, signotmasked);
5513 
5514 	DPRINTF("Before resuming the child process where it left off and "
5515 	    "without signal to be sent\n");
5516 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5517 
5518 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5519 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5520 
5521 	validate_status_exited(status, exitval);
5522 
5523 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5524 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5525 }
5526 
5527 ATF_TC(signal2);
5528 ATF_TC_HEAD(signal2, tc)
5529 {
5530 	atf_tc_set_md_var(tc, "descr",
5531 	    "Verify that masking SIGTRAP in tracee stops tracer from "
5532 	    "catching this raised signal");
5533 }
5534 
5535 ATF_TC_BODY(signal2, tc)
5536 {
5537 	const int exitval = 5;
5538 	const int sigval = SIGSTOP;
5539 	const int sigmasked = SIGTRAP;
5540 	pid_t child, wpid;
5541 #if defined(TWAIT_HAVE_STATUS)
5542 	int status;
5543 #endif
5544 	sigset_t intmask;
5545 
5546 	DPRINTF("Before forking process PID=%d\n", getpid());
5547 	SYSCALL_REQUIRE((child = fork()) != -1);
5548 	if (child == 0) {
5549 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5550 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5551 
5552 		sigemptyset(&intmask);
5553 		sigaddset(&intmask, sigmasked);
5554 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5555 
5556 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5557 		FORKEE_ASSERT(raise(sigval) == 0);
5558 
5559 		DPRINTF("Before raising %s breakpoint from child\n",
5560 		    strsignal(sigmasked));
5561 		FORKEE_ASSERT(raise(sigmasked) == 0);
5562 
5563 		DPRINTF("Before exiting of the child process\n");
5564 		_exit(exitval);
5565 	}
5566 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5567 
5568 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5569 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5570 
5571 	validate_status_stopped(status, sigval);
5572 
5573 	DPRINTF("Before resuming the child process where it left off and "
5574 	    "without signal to be sent\n");
5575 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5576 
5577 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5578 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5579 
5580 	validate_status_exited(status, exitval);
5581 
5582 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5583 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5584 }
5585 
5586 ATF_TC(signal3);
5587 ATF_TC_HEAD(signal3, tc)
5588 {
5589 	atf_tc_set_md_var(tc, "timeout", "5");
5590 	atf_tc_set_md_var(tc, "descr",
5591 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5592 	    "catching software breakpoints");
5593 }
5594 
5595 ATF_TC_BODY(signal3, tc)
5596 {
5597 	const int exitval = 5;
5598 	const int sigval = SIGSTOP;
5599 	const int sigmasked = SIGTRAP;
5600 	pid_t child, wpid;
5601 #if defined(TWAIT_HAVE_STATUS)
5602 	int status;
5603 #endif
5604 	sigset_t intmask;
5605 
5606 	atf_tc_expect_fail("PR kern/51918");
5607 
5608 	// This test breaks now on some ports, temporarily disable it
5609 	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
5610 
5611 #if defined(__sparc__)
5612 	atf_tc_expect_timeout("PR kern/52167");
5613 
5614 	// timeout wins, failure still valid
5615 	// atf_tc_expect_fail("PR kern/51918");
5616 #endif
5617 
5618 	DPRINTF("Before forking process PID=%d\n", getpid());
5619 	SYSCALL_REQUIRE((child = fork()) != -1);
5620 	if (child == 0) {
5621 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5622 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5623 
5624 		sigemptyset(&intmask);
5625 		sigaddset(&intmask, sigmasked);
5626 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5627 
5628 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5629 		FORKEE_ASSERT(raise(sigval) == 0);
5630 
5631 		DPRINTF("Before raising software breakpoint from child\n");
5632 
5633 #ifdef PTRACE_BREAKPOINT_ASM
5634 		PTRACE_BREAKPOINT_ASM;
5635 #else
5636 		/* port me */
5637 #endif
5638 
5639 		DPRINTF("Before exiting of the child process\n");
5640 		_exit(exitval);
5641 	}
5642 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5643 
5644 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5645 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5646 
5647 	validate_status_stopped(status, sigval);
5648 
5649 	DPRINTF("Before resuming the child process where it left off and "
5650 	    "without signal to be sent\n");
5651 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5652 
5653 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5654 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5655 
5656 	validate_status_stopped(status, sigmasked);
5657 
5658 	DPRINTF("Before resuming the child process where it left off and "
5659 	    "without signal to be sent\n");
5660 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5661 
5662 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5663 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5664 
5665 	validate_status_exited(status, exitval);
5666 
5667 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5668 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5669 }
5670 
5671 #if defined(PT_STEP)
5672 ATF_TC(signal4);
5673 ATF_TC_HEAD(signal4, tc)
5674 {
5675 	atf_tc_set_md_var(tc, "descr",
5676 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5677 	    "catching single step trap");
5678 }
5679 
5680 ATF_TC_BODY(signal4, tc)
5681 {
5682 	const int exitval = 5;
5683 	const int sigval = SIGSTOP;
5684 	const int sigmasked = SIGTRAP;
5685 	pid_t child, wpid;
5686 #if defined(TWAIT_HAVE_STATUS)
5687 	int status;
5688 #endif
5689 	sigset_t intmask;
5690 	int happy;
5691 
5692 #if defined(__arm__)
5693 	/* PT_STEP not supported on arm 32-bit */
5694 	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
5695 #endif
5696 
5697 	DPRINTF("Before forking process PID=%d\n", getpid());
5698 	SYSCALL_REQUIRE((child = fork()) != -1);
5699 	if (child == 0) {
5700 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5701 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5702 
5703 		happy = check_happy(100);
5704 
5705 		sigemptyset(&intmask);
5706 		sigaddset(&intmask, sigmasked);
5707 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5708 
5709 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5710 		FORKEE_ASSERT(raise(sigval) == 0);
5711 
5712 		FORKEE_ASSERT_EQ(happy, check_happy(100));
5713 
5714 		DPRINTF("Before exiting of the child process\n");
5715 		_exit(exitval);
5716 	}
5717 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5718 
5719 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5720 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5721 
5722 	validate_status_stopped(status, sigval);
5723 
5724 	DPRINTF("Before resuming the child process where it left off and "
5725 	    "without signal to be sent\n");
5726 	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5727 
5728 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5729 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5730 
5731 	validate_status_stopped(status, sigmasked);
5732 
5733 	DPRINTF("Before resuming the child process where it left off and "
5734 	    "without signal to be sent\n");
5735 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5736 
5737 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5738 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5739 
5740 	validate_status_exited(status, exitval);
5741 
5742 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5743 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5744 }
5745 #endif
5746 
5747 ATF_TC(signal5);
5748 ATF_TC_HEAD(signal5, tc)
5749 {
5750 	atf_tc_set_md_var(tc, "descr",
5751 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5752 	    "catching exec() breakpoint");
5753 }
5754 
5755 ATF_TC_BODY(signal5, tc)
5756 {
5757 	const int exitval = 5;
5758 	const int sigval = SIGSTOP;
5759 	const int sigmasked = SIGTRAP;
5760 	pid_t child, wpid;
5761 #if defined(TWAIT_HAVE_STATUS)
5762 	int status;
5763 #endif
5764 	sigset_t intmask;
5765 
5766 	atf_tc_expect_fail("wrong signal");
5767 
5768 	DPRINTF("Before forking process PID=%d\n", getpid());
5769 	SYSCALL_REQUIRE((child = fork()) != -1);
5770 	if (child == 0) {
5771 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5772 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5773 
5774 		sigemptyset(&intmask);
5775 		sigaddset(&intmask, sigmasked);
5776 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5777 
5778 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5779 		FORKEE_ASSERT(raise(sigval) == 0);
5780 
5781 		DPRINTF("Before calling execve(2) from child\n");
5782 		execlp("/bin/echo", "/bin/echo", NULL);
5783 
5784 		DPRINTF("Before exiting of the child process\n");
5785 		_exit(exitval);
5786 	}
5787 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5788 
5789 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5790 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5791 
5792 	validate_status_stopped(status, sigval);
5793 
5794 	DPRINTF("Before resuming the child process where it left off and "
5795 	    "without signal to be sent\n");
5796 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5797 
5798 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5799 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5800 
5801 	validate_status_stopped(status, sigmasked);
5802 
5803 	DPRINTF("Before resuming the child process where it left off and "
5804 	    "without signal to be sent\n");
5805 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5806 
5807 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5808 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5809 
5810 	validate_status_exited(status, exitval);
5811 
5812 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5813 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5814 }
5815 
5816 #if defined(TWAIT_HAVE_PID)
5817 ATF_TC(signal6);
5818 ATF_TC_HEAD(signal6, tc)
5819 {
5820 	atf_tc_set_md_var(tc, "timeout", "5");
5821 	atf_tc_set_md_var(tc, "descr",
5822 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5823 	    "catching PTRACE_FORK breakpoint");
5824 }
5825 
5826 ATF_TC_BODY(signal6, tc)
5827 {
5828 	const int exitval = 5;
5829 	const int exitval2 = 15;
5830 	const int sigval = SIGSTOP;
5831 	const int sigmasked = SIGTRAP;
5832 	pid_t child, child2, wpid;
5833 #if defined(TWAIT_HAVE_STATUS)
5834 	int status;
5835 #endif
5836 	sigset_t intmask;
5837 	ptrace_state_t state;
5838 	const int slen = sizeof(state);
5839 	ptrace_event_t event;
5840 	const int elen = sizeof(event);
5841 
5842 	atf_tc_expect_timeout("PR kern/51918");
5843 
5844 	DPRINTF("Before forking process PID=%d\n", getpid());
5845 	SYSCALL_REQUIRE((child = fork()) != -1);
5846 	if (child == 0) {
5847 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5848 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5849 
5850 		sigemptyset(&intmask);
5851 		sigaddset(&intmask, sigmasked);
5852 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5853 
5854 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5855 		FORKEE_ASSERT(raise(sigval) == 0);
5856 
5857 		FORKEE_ASSERT((child2 = fork()) != -1);
5858 
5859 		if (child2 == 0)
5860 			_exit(exitval2);
5861 
5862 		FORKEE_REQUIRE_SUCCESS
5863 			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5864 
5865 		forkee_status_exited(status, exitval2);
5866 
5867 		DPRINTF("Before exiting of the child process\n");
5868 		_exit(exitval);
5869 	}
5870 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5871 
5872 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5873 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5874 
5875 	validate_status_stopped(status, sigval);
5876 
5877 	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
5878 	event.pe_set_event = PTRACE_FORK;
5879 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5880 
5881 	DPRINTF("Before resuming the child process where it left off and "
5882 	    "without signal to be sent\n");
5883 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5884 
5885 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5886 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5887 
5888 	validate_status_stopped(status, sigmasked);
5889 
5890 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5891 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5892 
5893 	child2 = state.pe_other_pid;
5894 	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
5895 
5896 	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
5897 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5898 	    child2);
5899 
5900 	validate_status_stopped(status, SIGTRAP);
5901 
5902 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
5903 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5904 	ATF_REQUIRE_EQ(state.pe_other_pid, child);
5905 
5906 	DPRINTF("Before resuming the forkee process where it left off and "
5907 	    "without signal to be sent\n");
5908 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
5909 
5910 	DPRINTF("Before resuming the child process where it left off and "
5911 	    "without signal to be sent\n");
5912 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5913 
5914 	DPRINTF("Before calling %s() for the forkee - expected exited\n",
5915 	    TWAIT_FNAME);
5916 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5917 	    child2);
5918 
5919 	validate_status_exited(status, exitval2);
5920 
5921 	DPRINTF("Before calling %s() for the forkee - expected no process\n",
5922 	    TWAIT_FNAME);
5923 	TWAIT_REQUIRE_FAILURE(ECHILD,
5924 	    wpid = TWAIT_GENERIC(child2, &status, 0));
5925 
5926 	DPRINTF("Before calling %s() for the child - expected stopped "
5927 	    "SIGCHLD\n", TWAIT_FNAME);
5928 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5929 
5930 	validate_status_stopped(status, SIGCHLD);
5931 
5932 	DPRINTF("Before resuming the child process where it left off and "
5933 	    "without signal to be sent\n");
5934 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5935 
5936 	DPRINTF("Before calling %s() for the child - expected exited\n",
5937 	    TWAIT_FNAME);
5938 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5939 
5940 	validate_status_exited(status, exitval);
5941 
5942 	DPRINTF("Before calling %s() for the child - expected no process\n",
5943 	    TWAIT_FNAME);
5944 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5945 }
5946 #endif
5947 
5948 #if defined(TWAIT_HAVE_PID)
5949 ATF_TC(signal7);
5950 ATF_TC_HEAD(signal7, tc)
5951 {
5952 	atf_tc_set_md_var(tc, "descr",
5953 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5954 	    "catching PTRACE_VFORK breakpoint");
5955 }
5956 
5957 ATF_TC_BODY(signal7, tc)
5958 {
5959 	const int exitval = 5;
5960 	const int exitval2 = 15;
5961 	const int sigval = SIGSTOP;
5962 	const int sigmasked = SIGTRAP;
5963 	pid_t child, child2, wpid;
5964 #if defined(TWAIT_HAVE_STATUS)
5965 	int status;
5966 #endif
5967 	sigset_t intmask;
5968 	ptrace_state_t state;
5969 	const int slen = sizeof(state);
5970 	ptrace_event_t event;
5971 	const int elen = sizeof(event);
5972 
5973 	atf_tc_expect_fail("PR kern/51918 PR kern/51630");
5974 
5975 	DPRINTF("Before forking process PID=%d\n", getpid());
5976 	SYSCALL_REQUIRE((child = fork()) != -1);
5977 	if (child == 0) {
5978 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5979 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5980 
5981 		sigemptyset(&intmask);
5982 		sigaddset(&intmask, sigmasked);
5983 		sigprocmask(SIG_BLOCK, &intmask, NULL);
5984 
5985 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5986 		FORKEE_ASSERT(raise(sigval) == 0);
5987 
5988 		FORKEE_ASSERT((child2 = fork()) != -1);
5989 
5990 		if (child2 == 0)
5991 			_exit(exitval2);
5992 
5993 		FORKEE_REQUIRE_SUCCESS
5994 			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5995 
5996 		forkee_status_exited(status, exitval2);
5997 
5998 		DPRINTF("Before exiting of the child process\n");
5999 		_exit(exitval);
6000 	}
6001 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6002 
6003 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6004 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6005 
6006 	validate_status_stopped(status, sigval);
6007 
6008 	DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
6009 	event.pe_set_event = PTRACE_VFORK;
6010 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP);
6011 
6012 	DPRINTF("Before resuming the child process where it left off and "
6013 	    "without signal to be sent\n");
6014 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6015 
6016 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6017 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6018 
6019 	validate_status_stopped(status, sigmasked);
6020 
6021 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6022 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6023 
6024 	child2 = state.pe_other_pid;
6025 	DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2);
6026 
6027 	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
6028 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6029 	    child2);
6030 
6031 	validate_status_stopped(status, SIGTRAP);
6032 
6033 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6034 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6035 	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6036 
6037 	DPRINTF("Before resuming the forkee process where it left off and "
6038 	    "without signal to be sent\n");
6039 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6040 
6041 	DPRINTF("Before resuming the child process where it left off and "
6042 	    "without signal to be sent\n");
6043 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6044 
6045 	DPRINTF("Before calling %s() for the forkee - expected exited\n",
6046 	    TWAIT_FNAME);
6047 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6048 	    child2);
6049 
6050 	validate_status_exited(status, exitval2);
6051 
6052 	DPRINTF("Before calling %s() for the forkee - expected no process\n",
6053 	    TWAIT_FNAME);
6054 	TWAIT_REQUIRE_FAILURE(ECHILD,
6055 	    wpid = TWAIT_GENERIC(child2, &status, 0));
6056 
6057 	DPRINTF("Before calling %s() for the child - expected stopped "
6058 	    "SIGCHLD\n", TWAIT_FNAME);
6059 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6060 
6061 	validate_status_stopped(status, SIGCHLD);
6062 
6063 	DPRINTF("Before resuming the child process where it left off and "
6064 	    "without signal to be sent\n");
6065 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6066 
6067 	DPRINTF("Before calling %s() for the child - expected exited\n",
6068 	    TWAIT_FNAME);
6069 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6070 
6071 	validate_status_exited(status, exitval);
6072 
6073 	DPRINTF("Before calling %s() for the child - expected no process\n",
6074 	    TWAIT_FNAME);
6075 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6076 }
6077 #endif
6078 
6079 ATF_TC(signal8);
6080 ATF_TC_HEAD(signal8, tc)
6081 {
6082 	atf_tc_set_md_var(tc, "descr",
6083 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6084 	    "catching PTRACE_VFORK_DONE breakpoint");
6085 }
6086 
6087 ATF_TC_BODY(signal8, tc)
6088 {
6089 	const int exitval = 5;
6090 	const int exitval2 = 15;
6091 	const int sigval = SIGSTOP;
6092 	const int sigmasked = SIGTRAP;
6093 	pid_t child, child2, wpid;
6094 #if defined(TWAIT_HAVE_STATUS)
6095 	int status;
6096 #endif
6097 	sigset_t intmask;
6098 	ptrace_state_t state;
6099 	const int slen = sizeof(state);
6100 	ptrace_event_t event;
6101 	const int elen = sizeof(event);
6102 
6103 	atf_tc_expect_fail("PR kern/51918");
6104 
6105 	DPRINTF("Before forking process PID=%d\n", getpid());
6106 	SYSCALL_REQUIRE((child = fork()) != -1);
6107 	if (child == 0) {
6108 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6109 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6110 
6111 		sigemptyset(&intmask);
6112 		sigaddset(&intmask, sigmasked);
6113 		sigprocmask(SIG_BLOCK, &intmask, NULL);
6114 
6115 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6116 		FORKEE_ASSERT(raise(sigval) == 0);
6117 
6118 		FORKEE_ASSERT((child2 = vfork()) != -1);
6119 
6120 		if (child2 == 0)
6121 			_exit(exitval2);
6122 
6123 		FORKEE_REQUIRE_SUCCESS
6124 			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6125 
6126 		forkee_status_exited(status, exitval2);
6127 
6128 		DPRINTF("Before exiting of the child process\n");
6129 		_exit(exitval);
6130 	}
6131 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6132 
6133 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6134 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6135 
6136 	validate_status_stopped(status, sigval);
6137 
6138 	DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
6139 	    child);
6140 	event.pe_set_event = PTRACE_VFORK_DONE;
6141 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6142 
6143 	DPRINTF("Before resuming the child process where it left off and "
6144 	    "without signal to be sent\n");
6145 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6146 
6147 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6148 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6149 
6150 	validate_status_stopped(status, sigmasked);
6151 
6152 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6153 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
6154 
6155 	child2 = state.pe_other_pid;
6156 	DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
6157 
6158 	DPRINTF("Before resuming the child process where it left off and "
6159 	    "without signal to be sent\n");
6160 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6161 
6162 	DPRINTF("Before calling %s() for the child - expected stopped "
6163 	    "SIGCHLD\n", TWAIT_FNAME);
6164 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6165 
6166 	validate_status_stopped(status, SIGCHLD);
6167 
6168 	DPRINTF("Before resuming the child process where it left off and "
6169 	    "without signal to be sent\n");
6170 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6171 
6172 	DPRINTF("Before calling %s() for the child - expected exited\n",
6173 	    TWAIT_FNAME);
6174 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6175 
6176 	validate_status_exited(status, exitval);
6177 
6178 	DPRINTF("Before calling %s() for the child - expected no process\n",
6179 	    TWAIT_FNAME);
6180 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6181 }
6182 
6183 ATF_TC(signal9);
6184 ATF_TC_HEAD(signal9, tc)
6185 {
6186 	atf_tc_set_md_var(tc, "descr",
6187 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6188 	    "catching PTRACE_LWP_CREATE breakpoint");
6189 }
6190 
6191 ATF_TC_BODY(signal9, tc)
6192 {
6193 	const int exitval = 5;
6194 	const int sigval = SIGSTOP;
6195 	const int sigmasked = SIGTRAP;
6196 	pid_t child, wpid;
6197 #if defined(TWAIT_HAVE_STATUS)
6198 	int status;
6199 #endif
6200 	sigset_t intmask;
6201 	ptrace_state_t state;
6202 	const int slen = sizeof(state);
6203 	ptrace_event_t event;
6204 	const int elen = sizeof(event);
6205 	ucontext_t uc;
6206 	lwpid_t lid;
6207 	static const size_t ssize = 16*1024;
6208 	void *stack;
6209 
6210 	atf_tc_expect_fail("PR kern/51918");
6211 
6212 	DPRINTF("Before forking process PID=%d\n", getpid());
6213 	SYSCALL_REQUIRE((child = fork()) != -1);
6214 	if (child == 0) {
6215 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6216 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6217 
6218 		sigemptyset(&intmask);
6219 		sigaddset(&intmask, sigmasked);
6220 		sigprocmask(SIG_BLOCK, &intmask, NULL);
6221 
6222 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6223 		FORKEE_ASSERT(raise(sigval) == 0);
6224 
6225 		DPRINTF("Before allocating memory for stack in child\n");
6226 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6227 
6228 		DPRINTF("Before making context for new lwp in child\n");
6229 		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6230 
6231 		DPRINTF("Before creating new in child\n");
6232 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6233 
6234 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6235 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6236 
6237 		DPRINTF("Before verifying that reported %d and running lid %d "
6238 		    "are the same\n", lid, the_lwp_id);
6239 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6240 
6241 		DPRINTF("Before exiting of the child process\n");
6242 		_exit(exitval);
6243 	}
6244 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6245 
6246 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6247 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6248 
6249 	validate_status_stopped(status, sigval);
6250 
6251 	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6252 	event.pe_set_event = PTRACE_LWP_CREATE;
6253 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6254 
6255 	DPRINTF("Before resuming the child process where it left off and "
6256 	    "without signal to be sent\n");
6257 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6258 
6259 	DPRINTF("Before calling %s() for the child - expected stopped "
6260 	    "SIGTRAP\n", TWAIT_FNAME);
6261 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6262 
6263 	validate_status_stopped(status, sigmasked);
6264 
6265 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6266 
6267 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
6268 
6269 	lid = state.pe_lwp;
6270 	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
6271 
6272 	DPRINTF("Before resuming the child process where it left off and "
6273 	    "without signal to be sent\n");
6274 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6275 
6276 	DPRINTF("Before calling %s() for the child - expected exited\n",
6277 	    TWAIT_FNAME);
6278 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6279 
6280 	validate_status_exited(status, exitval);
6281 
6282 	DPRINTF("Before calling %s() for the child - expected no process\n",
6283 	    TWAIT_FNAME);
6284 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6285 }
6286 
6287 ATF_TC(signal10);
6288 ATF_TC_HEAD(signal10, tc)
6289 {
6290 	atf_tc_set_md_var(tc, "descr",
6291 	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6292 	    "catching PTRACE_LWP_EXIT breakpoint");
6293 }
6294 
6295 ATF_TC_BODY(signal10, tc)
6296 {
6297 	const int exitval = 5;
6298 	const int sigval = SIGSTOP;
6299 	const int sigmasked = SIGTRAP;
6300 	pid_t child, wpid;
6301 #if defined(TWAIT_HAVE_STATUS)
6302 	int status;
6303 #endif
6304 	sigset_t intmask;
6305 	ptrace_state_t state;
6306 	const int slen = sizeof(state);
6307 	ptrace_event_t event;
6308 	const int elen = sizeof(event);
6309 	ucontext_t uc;
6310 	lwpid_t lid;
6311 	static const size_t ssize = 16*1024;
6312 	void *stack;
6313 
6314 	atf_tc_expect_fail("PR kern/51918");
6315 
6316 	DPRINTF("Before forking process PID=%d\n", getpid());
6317 	SYSCALL_REQUIRE((child = fork()) != -1);
6318 	if (child == 0) {
6319 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6320 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6321 
6322 		sigemptyset(&intmask);
6323 		sigaddset(&intmask, sigmasked);
6324 		sigprocmask(SIG_BLOCK, &intmask, NULL);
6325 
6326 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6327 		FORKEE_ASSERT(raise(sigval) == 0);
6328 
6329 		DPRINTF("Before allocating memory for stack in child\n");
6330 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6331 
6332 		DPRINTF("Before making context for new lwp in child\n");
6333 		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6334 
6335 		DPRINTF("Before creating new in child\n");
6336 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6337 
6338 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6339 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6340 
6341 		DPRINTF("Before verifying that reported %d and running lid %d "
6342 		    "are the same\n", lid, the_lwp_id);
6343 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6344 
6345 		DPRINTF("Before exiting of the child process\n");
6346 		_exit(exitval);
6347 	}
6348 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6349 
6350 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6351 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6352 
6353 	validate_status_stopped(status, sigval);
6354 
6355 	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6356 	event.pe_set_event = PTRACE_LWP_EXIT;
6357 	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6358 
6359 	DPRINTF("Before resuming the child process where it left off and "
6360 	    "without signal to be sent\n");
6361 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6362 
6363 	DPRINTF("Before calling %s() for the child - expected stopped "
6364 	    "SIGTRAP\n", TWAIT_FNAME);
6365 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6366 
6367 	validate_status_stopped(status, sigmasked);
6368 
6369 	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6370 
6371 	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
6372 
6373 	lid = state.pe_lwp;
6374 	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
6375 
6376 	DPRINTF("Before resuming the child process where it left off and "
6377 	    "without signal to be sent\n");
6378 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6379 
6380 	DPRINTF("Before calling %s() for the child - expected exited\n",
6381 	    TWAIT_FNAME);
6382 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6383 
6384 	validate_status_exited(status, exitval);
6385 
6386 	DPRINTF("Before calling %s() for the child - expected no process\n",
6387 	    TWAIT_FNAME);
6388 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6389 }
6390 
6391 static void
6392 lwp_main_stop(void *arg)
6393 {
6394 	the_lwp_id = _lwp_self();
6395 
6396 	raise(SIGTRAP);
6397 
6398 	_lwp_exit();
6399 }
6400 
6401 ATF_TC(suspend1);
6402 ATF_TC_HEAD(suspend1, tc)
6403 {
6404 	atf_tc_set_md_var(tc, "descr",
6405 	    "Verify that a thread can be suspended by a debugger and later "
6406 	    "resumed by a tracee");
6407 }
6408 
6409 ATF_TC_BODY(suspend1, tc)
6410 {
6411 	const int exitval = 5;
6412 	const int sigval = SIGSTOP;
6413 	pid_t child, wpid;
6414 #if defined(TWAIT_HAVE_STATUS)
6415 	int status;
6416 #endif
6417 	ucontext_t uc;
6418 	lwpid_t lid;
6419 	static const size_t ssize = 16*1024;
6420 	void *stack;
6421 	struct ptrace_lwpinfo pl;
6422 	struct ptrace_siginfo psi;
6423 	volatile int go = 0;
6424 
6425 	// Feature pending for refactoring
6426 	atf_tc_expect_fail("PR kern/51995");
6427 
6428 	// Hangs with qemu
6429 	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6430 
6431 	DPRINTF("Before forking process PID=%d\n", getpid());
6432 	SYSCALL_REQUIRE((child = fork()) != -1);
6433 	if (child == 0) {
6434 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6435 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6436 
6437 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6438 		FORKEE_ASSERT(raise(sigval) == 0);
6439 
6440 		DPRINTF("Before allocating memory for stack in child\n");
6441 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6442 
6443 		DPRINTF("Before making context for new lwp in child\n");
6444 		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
6445 
6446 		DPRINTF("Before creating new in child\n");
6447 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6448 
6449 		while (go == 0)
6450 			continue;
6451 
6452 		raise(SIGINT);
6453 
6454 		FORKEE_ASSERT(_lwp_continue(lid) == 0);
6455 
6456 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6457 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6458 
6459 		DPRINTF("Before verifying that reported %d and running lid %d "
6460 		    "are the same\n", lid, the_lwp_id);
6461 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6462 
6463 		DPRINTF("Before exiting of the child process\n");
6464 		_exit(exitval);
6465 	}
6466 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6467 
6468 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6469 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6470 
6471 	validate_status_stopped(status, sigval);
6472 
6473 	DPRINTF("Before resuming the child process where it left off and "
6474 	    "without signal to be sent\n");
6475 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6476 
6477 	DPRINTF("Before calling %s() for the child - expected stopped "
6478 	    "SIGTRAP\n", TWAIT_FNAME);
6479 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6480 
6481 	validate_status_stopped(status, SIGTRAP);
6482 
6483 	DPRINTF("Before reading siginfo and lwpid_t\n");
6484 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6485 
6486 	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6487 	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6488 
6489         DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
6490 	    child, getpid());
6491 	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
6492 
6493 	DPRINTF("Before resuming the child process where it left off and "
6494 	    "without signal to be sent\n");
6495 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6496 
6497 	DPRINTF("Before calling %s() for the child - expected stopped "
6498 	    "SIGINT\n", TWAIT_FNAME);
6499 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6500 
6501 	validate_status_stopped(status, SIGINT);
6502 
6503 	pl.pl_lwpid = 0;
6504 
6505 	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6506 	while (pl.pl_lwpid != 0) {
6507 
6508 		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6509 		switch (pl.pl_lwpid) {
6510 		case 1:
6511 			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
6512 			break;
6513 		case 2:
6514 			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
6515 			break;
6516 		}
6517 	}
6518 
6519 	DPRINTF("Before resuming the child process where it left off and "
6520 	    "without signal to be sent\n");
6521 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6522 
6523 	DPRINTF("Before calling %s() for the child - expected exited\n",
6524 	    TWAIT_FNAME);
6525 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6526 
6527 	validate_status_exited(status, exitval);
6528 
6529 	DPRINTF("Before calling %s() for the child - expected no process\n",
6530 	    TWAIT_FNAME);
6531 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6532 }
6533 
6534 ATF_TC(suspend2);
6535 ATF_TC_HEAD(suspend2, tc)
6536 {
6537 	atf_tc_set_md_var(tc, "descr",
6538 	    "Verify that the while the only thread within a process is "
6539 	    "suspended, the whole process cannot be unstopped");
6540 }
6541 
6542 ATF_TC_BODY(suspend2, tc)
6543 {
6544 	const int exitval = 5;
6545 	const int sigval = SIGSTOP;
6546 	pid_t child, wpid;
6547 #if defined(TWAIT_HAVE_STATUS)
6548 	int status;
6549 #endif
6550 	struct ptrace_siginfo psi;
6551 
6552 	// Feature pending for refactoring
6553 	atf_tc_expect_fail("PR kern/51995");
6554 
6555 	// Hangs with qemu
6556 	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6557 
6558 	DPRINTF("Before forking process PID=%d\n", getpid());
6559 	SYSCALL_REQUIRE((child = fork()) != -1);
6560 	if (child == 0) {
6561 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6562 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6563 
6564 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6565 		FORKEE_ASSERT(raise(sigval) == 0);
6566 
6567 		DPRINTF("Before exiting of the child process\n");
6568 		_exit(exitval);
6569 	}
6570 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6571 
6572 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6573 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6574 
6575 	validate_status_stopped(status, sigval);
6576 
6577 	DPRINTF("Before reading siginfo and lwpid_t\n");
6578 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6579 
6580 	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6581 	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6582 
6583 	DPRINTF("Before resuming the child process where it left off and "
6584 	    "without signal to be sent\n");
6585 	ATF_REQUIRE_ERRNO(EDEADLK,
6586 	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
6587 
6588 	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
6589 	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
6590 
6591 	DPRINTF("Before resuming the child process where it left off and "
6592 	    "without signal to be sent\n");
6593 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6594 
6595 	DPRINTF("Before calling %s() for the child - expected exited\n",
6596 	    TWAIT_FNAME);
6597 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6598 
6599 	validate_status_exited(status, exitval);
6600 
6601 	DPRINTF("Before calling %s() for the child - expected no process\n",
6602 	    TWAIT_FNAME);
6603 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6604 }
6605 
6606 ATF_TC(resume1);
6607 ATF_TC_HEAD(resume1, tc)
6608 {
6609 	atf_tc_set_md_var(tc, "timeout", "5");
6610 	atf_tc_set_md_var(tc, "descr",
6611 	    "Verify that a thread can be suspended by a debugger and later "
6612 	    "resumed by the debugger");
6613 }
6614 
6615 ATF_TC_BODY(resume1, tc)
6616 {
6617 	struct msg_fds fds;
6618 	const int exitval = 5;
6619 	const int sigval = SIGSTOP;
6620 	pid_t child, wpid;
6621 	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
6622 #if defined(TWAIT_HAVE_STATUS)
6623 	int status;
6624 #endif
6625 	ucontext_t uc;
6626 	lwpid_t lid;
6627 	static const size_t ssize = 16*1024;
6628 	void *stack;
6629 	struct ptrace_lwpinfo pl;
6630 	struct ptrace_siginfo psi;
6631 
6632 	// Feature pending for refactoring
6633 	atf_tc_expect_fail("PR kern/51995");
6634 
6635 	// Hangs with qemu
6636 	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
6637 
6638 	SYSCALL_REQUIRE(msg_open(&fds) == 0);
6639 
6640 	DPRINTF("Before forking process PID=%d\n", getpid());
6641 	SYSCALL_REQUIRE((child = fork()) != -1);
6642 	if (child == 0) {
6643 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6644 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6645 
6646 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6647 		FORKEE_ASSERT(raise(sigval) == 0);
6648 
6649 		DPRINTF("Before allocating memory for stack in child\n");
6650 		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6651 
6652 		DPRINTF("Before making context for new lwp in child\n");
6653 		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
6654 
6655 		DPRINTF("Before creating new in child\n");
6656 		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6657 
6658 		CHILD_TO_PARENT("Message", fds, msg);
6659 
6660 		raise(SIGINT);
6661 
6662 		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6663 		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6664 
6665 		DPRINTF("Before verifying that reported %d and running lid %d "
6666 		    "are the same\n", lid, the_lwp_id);
6667 		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6668 
6669 		DPRINTF("Before exiting of the child process\n");
6670 		_exit(exitval);
6671 	}
6672 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6673 
6674 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6675 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6676 
6677 	validate_status_stopped(status, sigval);
6678 
6679 	DPRINTF("Before resuming the child process where it left off and "
6680 	    "without signal to be sent\n");
6681 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6682 
6683 	DPRINTF("Before calling %s() for the child - expected stopped "
6684 	    "SIGTRAP\n", TWAIT_FNAME);
6685 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6686 
6687 	validate_status_stopped(status, SIGTRAP);
6688 
6689 	DPRINTF("Before reading siginfo and lwpid_t\n");
6690 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
6691 
6692 	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
6693 	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
6694 
6695 	PARENT_FROM_CHILD("Message", fds, msg);
6696 
6697 	DPRINTF("Before resuming the child process where it left off and "
6698 	    "without signal to be sent\n");
6699 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6700 
6701 	DPRINTF("Before calling %s() for the child - expected stopped "
6702 	    "SIGINT\n", TWAIT_FNAME);
6703 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6704 
6705 	validate_status_stopped(status, SIGINT);
6706 
6707 	pl.pl_lwpid = 0;
6708 
6709 	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6710 	while (pl.pl_lwpid != 0) {
6711 		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
6712 		switch (pl.pl_lwpid) {
6713 		case 1:
6714 			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
6715 			break;
6716 		case 2:
6717 			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
6718 			break;
6719 		}
6720 	}
6721 
6722 	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
6723 	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
6724 
6725 	DPRINTF("Before resuming the child process where it left off and "
6726 	    "without signal to be sent\n");
6727 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6728 
6729 	DPRINTF("Before calling %s() for the child - expected exited\n",
6730 	    TWAIT_FNAME);
6731 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6732 
6733 	validate_status_exited(status, exitval);
6734 
6735 	DPRINTF("Before calling %s() for the child - expected no process\n",
6736 	    TWAIT_FNAME);
6737 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6738 
6739 	msg_close(&fds);
6740 
6741 	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
6742 	sleep(10);
6743 }
6744 
6745 ATF_TC(syscall1);
6746 ATF_TC_HEAD(syscall1, tc)
6747 {
6748 	atf_tc_set_md_var(tc, "descr",
6749 	    "Verify that getpid(2) can be traced with PT_SYSCALL");
6750 }
6751 
6752 ATF_TC_BODY(syscall1, tc)
6753 {
6754 	const int exitval = 5;
6755 	const int sigval = SIGSTOP;
6756 	pid_t child, wpid;
6757 #if defined(TWAIT_HAVE_STATUS)
6758 	int status;
6759 #endif
6760 	struct ptrace_siginfo info;
6761 	memset(&info, 0, sizeof(info));
6762 
6763 	DPRINTF("Before forking process PID=%d\n", getpid());
6764 	SYSCALL_REQUIRE((child = fork()) != -1);
6765 	if (child == 0) {
6766 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6767 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6768 
6769 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6770 		FORKEE_ASSERT(raise(sigval) == 0);
6771 
6772 		syscall(SYS_getpid);
6773 
6774 		DPRINTF("Before exiting of the child process\n");
6775 		_exit(exitval);
6776 	}
6777 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6778 
6779 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6780 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6781 
6782 	validate_status_stopped(status, sigval);
6783 
6784 	DPRINTF("Before resuming the child process where it left off and "
6785 	    "without signal to be sent\n");
6786 	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6787 
6788 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6789 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6790 
6791 	validate_status_stopped(status, SIGTRAP);
6792 
6793 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
6794 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
6795 
6796 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
6797 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
6798 
6799 	DPRINTF("Before resuming the child process where it left off and "
6800 	    "without signal to be sent\n");
6801 	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6802 
6803 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6804 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6805 
6806 	validate_status_stopped(status, SIGTRAP);
6807 
6808 	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
6809 	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
6810 
6811 	DPRINTF("Before checking siginfo_t\n");
6812 	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
6813 	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
6814 
6815 	DPRINTF("Before resuming the child process where it left off and "
6816 	    "without signal to be sent\n");
6817 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6818 
6819 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6820 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6821 
6822 	validate_status_exited(status, exitval);
6823 
6824 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6825 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6826 }
6827 
6828 ATF_TC(syscallemu1);
6829 ATF_TC_HEAD(syscallemu1, tc)
6830 {
6831 	atf_tc_set_md_var(tc, "descr",
6832 	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
6833 }
6834 
6835 ATF_TC_BODY(syscallemu1, tc)
6836 {
6837 	const int exitval = 5;
6838 	const int sigval = SIGSTOP;
6839 	pid_t child, wpid;
6840 #if defined(TWAIT_HAVE_STATUS)
6841 	int status;
6842 #endif
6843 
6844 #if defined(__sparc__) && !defined(__sparc64__)
6845 	/* syscallemu does not work on sparc (32-bit) */
6846 	atf_tc_expect_fail("PR kern/52166");
6847 #endif
6848 
6849 	DPRINTF("Before forking process PID=%d\n", getpid());
6850 	SYSCALL_REQUIRE((child = fork()) != -1);
6851 	if (child == 0) {
6852 		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6853 		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6854 
6855 		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6856 		FORKEE_ASSERT(raise(sigval) == 0);
6857 
6858 		syscall(SYS_exit, 100);
6859 
6860 		DPRINTF("Before exiting of the child process\n");
6861 		_exit(exitval);
6862 	}
6863 	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6864 
6865 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6866 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6867 
6868 	validate_status_stopped(status, sigval);
6869 
6870 	DPRINTF("Before resuming the child process where it left off and "
6871 	    "without signal to be sent\n");
6872 	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6873 
6874 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6875 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6876 
6877 	validate_status_stopped(status, SIGTRAP);
6878 
6879 	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
6880 	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
6881 
6882 	DPRINTF("Before resuming the child process where it left off and "
6883 	    "without signal to be sent\n");
6884 	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
6885 
6886 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6887 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6888 
6889 	validate_status_stopped(status, SIGTRAP);
6890 
6891 	DPRINTF("Before resuming the child process where it left off and "
6892 	    "without signal to be sent\n");
6893 	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6894 
6895 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6896 	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6897 
6898 	validate_status_exited(status, exitval);
6899 
6900 	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6901 	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6902 }
6903 
6904 #if defined(TWAIT_HAVE_PID)
6905 ATF_TC(race1);
6906 ATF_TC_HEAD(race1, tc)
6907 {
6908 	atf_tc_set_md_var(tc, "descr",
6909 	    "Assert that await_zombie() in attach1 always finds a single "
6910 	    "process and no other error is reported");
6911 }
6912 
6913 ATF_TC_BODY(race1, tc)
6914 {
6915 	time_t start, end;
6916 	double diff;
6917 	unsigned long N = 0;
6918 
6919 	/* Reuse this test with attach1 */
6920 
6921 	start = time(NULL);
6922 	while (true) {
6923 		DPRINTF("Step: %lu\n", N);
6924 		attach1_raw(true);
6925 		end = time(NULL);
6926 		diff = difftime(end, start);
6927 		if (diff >= 5.0)
6928 			break;
6929 		++N;
6930 	}
6931 	DPRINTF("Iterations: %lu\n", N);
6932 }
6933 #endif
6934 
6935 #include "t_ptrace_amd64_wait.h"
6936 #include "t_ptrace_i386_wait.h"
6937 #include "t_ptrace_x86_wait.h"
6938 
6939 ATF_TP_ADD_TCS(tp)
6940 {
6941 	setvbuf(stdout, NULL, _IONBF, 0);
6942 	setvbuf(stderr, NULL, _IONBF, 0);
6943 	ATF_TP_ADD_TC(tp, traceme1);
6944 	ATF_TP_ADD_TC(tp, traceme2);
6945 	ATF_TP_ADD_TC(tp, traceme3);
6946 	ATF_TP_ADD_TC(tp, traceme4);
6947 
6948 	ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
6949 	ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
6950 	ATF_TP_ADD_TC(tp, attach3);
6951 	ATF_TP_ADD_TC(tp, attach4);
6952 	ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
6953 	ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
6954 	ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
6955 
6956 	ATF_TP_ADD_TC(tp, eventmask1);
6957 	ATF_TP_ADD_TC(tp, eventmask2);
6958 	ATF_TP_ADD_TC(tp, eventmask3);
6959 	ATF_TP_ADD_TC(tp, eventmask4);
6960 	ATF_TP_ADD_TC(tp, eventmask5);
6961 	ATF_TP_ADD_TC(tp, eventmask6);
6962 
6963 	ATF_TP_ADD_TC(tp, fork1);
6964 	ATF_TP_ADD_TC_HAVE_PID(tp, fork2);
6965 	ATF_TP_ADD_TC_HAVE_PID(tp, fork3);
6966 	ATF_TP_ADD_TC_HAVE_PID(tp, fork4);
6967 	ATF_TP_ADD_TC(tp, fork5);
6968 	ATF_TP_ADD_TC_HAVE_PID(tp, fork6);
6969 	ATF_TP_ADD_TC_HAVE_PID(tp, fork7);
6970 	ATF_TP_ADD_TC_HAVE_PID(tp, fork8);
6971 
6972 	ATF_TP_ADD_TC(tp, vfork1);
6973 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork2);
6974 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork3);
6975 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork4);
6976 	ATF_TP_ADD_TC(tp, vfork5);
6977 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork6);
6978 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork7);
6979 	ATF_TP_ADD_TC_HAVE_PID(tp, vfork8);
6980 
6981 	ATF_TP_ADD_TC(tp, io_read_d1);
6982 	ATF_TP_ADD_TC(tp, io_read_d2);
6983 	ATF_TP_ADD_TC(tp, io_read_d3);
6984 	ATF_TP_ADD_TC(tp, io_read_d4);
6985 
6986 	ATF_TP_ADD_TC(tp, io_write_d1);
6987 	ATF_TP_ADD_TC(tp, io_write_d2);
6988 	ATF_TP_ADD_TC(tp, io_write_d3);
6989 	ATF_TP_ADD_TC(tp, io_write_d4);
6990 
6991 	ATF_TP_ADD_TC(tp, read_d1);
6992 	ATF_TP_ADD_TC(tp, read_d2);
6993 	ATF_TP_ADD_TC(tp, read_d3);
6994 	ATF_TP_ADD_TC(tp, read_d4);
6995 
6996 	ATF_TP_ADD_TC(tp, write_d1);
6997 	ATF_TP_ADD_TC(tp, write_d2);
6998 	ATF_TP_ADD_TC(tp, write_d3);
6999 	ATF_TP_ADD_TC(tp, write_d4);
7000 
7001 	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1);
7002 	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2);
7003 
7004 	ATF_TP_ADD_TC(tp, read_d_write_d_handshake1);
7005 	ATF_TP_ADD_TC(tp, read_d_write_d_handshake2);
7006 
7007 	ATF_TP_ADD_TC(tp, io_read_i1);
7008 	ATF_TP_ADD_TC(tp, io_read_i2);
7009 	ATF_TP_ADD_TC(tp, io_read_i3);
7010 	ATF_TP_ADD_TC(tp, io_read_i4);
7011 
7012 	ATF_TP_ADD_TC(tp, read_i1);
7013 	ATF_TP_ADD_TC(tp, read_i2);
7014 	ATF_TP_ADD_TC(tp, read_i3);
7015 	ATF_TP_ADD_TC(tp, read_i4);
7016 
7017 	ATF_TP_ADD_TC(tp, io_read_auxv1);
7018 
7019 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
7020 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
7021 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
7022 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
7023 	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
7024 
7025 	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
7026 	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
7027 
7028 	ATF_TP_ADD_TC_PT_STEP(tp, step1);
7029 	ATF_TP_ADD_TC_PT_STEP(tp, step2);
7030 	ATF_TP_ADD_TC_PT_STEP(tp, step3);
7031 	ATF_TP_ADD_TC_PT_STEP(tp, step4);
7032 
7033 	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
7034 	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
7035 	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
7036 	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
7037 
7038 	ATF_TP_ADD_TC(tp, kill1);
7039 	ATF_TP_ADD_TC(tp, kill2);
7040 
7041 	ATF_TP_ADD_TC(tp, lwpinfo1);
7042 	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
7043 
7044 	ATF_TP_ADD_TC(tp, siginfo1);
7045 	ATF_TP_ADD_TC(tp, siginfo2);
7046 	ATF_TP_ADD_TC(tp, siginfo3);
7047 	ATF_TP_ADD_TC(tp, siginfo4);
7048 	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
7049 	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
7050 
7051 	ATF_TP_ADD_TC(tp, lwp_create1);
7052 
7053 	ATF_TP_ADD_TC(tp, lwp_exit1);
7054 
7055 	ATF_TP_ADD_TC(tp, signal1);
7056 	ATF_TP_ADD_TC(tp, signal2);
7057 	ATF_TP_ADD_TC(tp, signal3);
7058 	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
7059 	ATF_TP_ADD_TC(tp, signal5);
7060 	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
7061 	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
7062 	ATF_TP_ADD_TC(tp, signal8);
7063 	ATF_TP_ADD_TC(tp, signal9);
7064 	ATF_TP_ADD_TC(tp, signal10);
7065 
7066 	ATF_TP_ADD_TC(tp, suspend1);
7067 	ATF_TP_ADD_TC(tp, suspend2);
7068 
7069 	ATF_TP_ADD_TC(tp, resume1);
7070 
7071 	ATF_TP_ADD_TC(tp, syscall1);
7072 
7073 	ATF_TP_ADD_TC(tp, syscallemu1);
7074 
7075 	ATF_TP_ADD_TC_HAVE_PID(tp, race1);
7076 
7077 	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
7078 	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
7079 	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
7080 
7081 	return atf_no_error();
7082 }
7083