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