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