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