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