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