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