t_ptrace_wait.c revision 1.8
1/*	$NetBSD: t_ptrace_wait.c,v 1.8 2017/04/16 13:09:40 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.8 2017/04/16 13:09:40 kamil Exp $");
31
32#include <sys/param.h>
33#include <sys/types.h>
34#include <sys/ptrace.h>
35#include <sys/resource.h>
36#include <sys/stat.h>
37#include <sys/syscall.h>
38#include <sys/sysctl.h>
39#include <sys/wait.h>
40#include <machine/reg.h>
41#include <elf.h>
42#include <err.h>
43#include <errno.h>
44#include <lwp.h>
45#include <sched.h>
46#include <signal.h>
47#include <stdint.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <strings.h>
51#include <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_STEP)\n");
4440			ATF_REQUIRE(ptrace(PT_SETSTEP, child, (void *)1, 0)
4441			    != -1);
4442			ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
4443			    != -1);
4444		} else {
4445			printf("Before resuming the child process where it "
4446			    "left off and without signal to be sent (use "
4447			    "PT_STEP)\n");
4448			ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
4449			    != -1);
4450		}
4451
4452		printf("Before calling %s() for the child\n", TWAIT_FNAME);
4453		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4454		    child);
4455
4456		validate_status_stopped(status, SIGTRAP);
4457
4458		if (setstep) {
4459			ATF_REQUIRE(ptrace(PT_CLEARSTEP, child, (void *)1, 0)
4460			    != -1);
4461		}
4462	}
4463
4464	printf("Before resuming the child process where it left off and "
4465	    "without signal to be sent\n");
4466	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4467
4468	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4469	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4470
4471	validate_status_exited(status, exitval);
4472
4473	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4474	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4475}
4476#endif
4477
4478#if defined(PT_STEP)
4479ATF_TC(step1);
4480ATF_TC_HEAD(step1, tc)
4481{
4482	atf_tc_set_md_var(tc, "descr",
4483	    "Verify single PT_STEP call");
4484}
4485
4486ATF_TC_BODY(step1, tc)
4487{
4488	ptrace_step(1, 0);
4489}
4490#endif
4491
4492#if defined(PT_STEP)
4493ATF_TC(step2);
4494ATF_TC_HEAD(step2, tc)
4495{
4496	atf_tc_set_md_var(tc, "descr",
4497	    "Verify PT_STEP called twice");
4498}
4499
4500ATF_TC_BODY(step2, tc)
4501{
4502	ptrace_step(2, 0);
4503}
4504#endif
4505
4506#if defined(PT_STEP)
4507ATF_TC(step3);
4508ATF_TC_HEAD(step3, tc)
4509{
4510	atf_tc_set_md_var(tc, "descr",
4511	    "Verify PT_STEP called three times");
4512}
4513
4514ATF_TC_BODY(step3, tc)
4515{
4516	ptrace_step(3, 0);
4517}
4518#endif
4519
4520#if defined(PT_STEP)
4521ATF_TC(step4);
4522ATF_TC_HEAD(step4, tc)
4523{
4524	atf_tc_set_md_var(tc, "descr",
4525	    "Verify PT_STEP called four times");
4526}
4527
4528ATF_TC_BODY(step4, tc)
4529{
4530	ptrace_step(4, 0);
4531}
4532#endif
4533
4534#if defined(PT_STEP)
4535ATF_TC(setstep1);
4536ATF_TC_HEAD(setstep1, tc)
4537{
4538	atf_tc_set_md_var(tc, "descr",
4539	    "Verify single PT_SETSTEP call");
4540}
4541
4542ATF_TC_BODY(setstep1, tc)
4543{
4544	ptrace_step(1, 1);
4545}
4546#endif
4547
4548#if defined(PT_STEP)
4549ATF_TC(setstep2);
4550ATF_TC_HEAD(setstep2, tc)
4551{
4552	atf_tc_set_md_var(tc, "descr",
4553	    "Verify PT_SETSTEP called twice");
4554}
4555
4556ATF_TC_BODY(setstep2, tc)
4557{
4558	ptrace_step(2, 1);
4559}
4560#endif
4561
4562#if defined(PT_STEP)
4563ATF_TC(setstep3);
4564ATF_TC_HEAD(setstep3, tc)
4565{
4566	atf_tc_set_md_var(tc, "descr",
4567	    "Verify PT_SETSTEP called three times");
4568}
4569
4570ATF_TC_BODY(setstep3, tc)
4571{
4572	ptrace_step(3, 1);
4573}
4574#endif
4575
4576#if defined(PT_STEP)
4577ATF_TC(setstep4);
4578ATF_TC_HEAD(setstep4, tc)
4579{
4580	atf_tc_set_md_var(tc, "descr",
4581	    "Verify PT_SETSTEP called four times");
4582}
4583
4584ATF_TC_BODY(setstep4, tc)
4585{
4586	ptrace_step(4, 1);
4587}
4588#endif
4589
4590ATF_TC(kill1);
4591ATF_TC_HEAD(kill1, tc)
4592{
4593	atf_tc_set_md_var(tc, "descr",
4594	    "Verify that PT_CONTINUE with SIGKILL terminates child");
4595}
4596
4597ATF_TC_BODY(kill1, tc)
4598{
4599	const int sigval = SIGSTOP, sigsent = SIGKILL;
4600	pid_t child, wpid;
4601#if defined(TWAIT_HAVE_STATUS)
4602	int status;
4603#endif
4604
4605	printf("Before forking process PID=%d\n", getpid());
4606	ATF_REQUIRE((child = fork()) != -1);
4607	if (child == 0) {
4608		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
4609		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4610
4611		printf("Before raising %s from child\n", strsignal(sigval));
4612		FORKEE_ASSERT(raise(sigval) == 0);
4613
4614		/* NOTREACHED */
4615		FORKEE_ASSERTX(0 &&
4616		    "Child should be terminated by a signal from its parent");
4617	}
4618	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4619
4620	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4621	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4622
4623	validate_status_stopped(status, sigval);
4624
4625	printf("Before resuming the child process where it left off and "
4626	    "without signal to be sent\n");
4627	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
4628
4629	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4630	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4631
4632	validate_status_signaled(status, sigsent, 0);
4633
4634	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4635	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4636}
4637
4638ATF_TC(kill2);
4639ATF_TC_HEAD(kill2, tc)
4640{
4641	atf_tc_set_md_var(tc, "descr",
4642	    "Verify that PT_KILL terminates child");
4643}
4644
4645ATF_TC_BODY(kill2, tc)
4646{
4647	const int sigval = SIGSTOP;
4648	pid_t child, wpid;
4649#if defined(TWAIT_HAVE_STATUS)
4650	int status;
4651#endif
4652
4653	printf("Before forking process PID=%d\n", getpid());
4654	ATF_REQUIRE((child = fork()) != -1);
4655	if (child == 0) {
4656		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
4657		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4658
4659		printf("Before raising %s from child\n", strsignal(sigval));
4660		FORKEE_ASSERT(raise(sigval) == 0);
4661
4662		/* NOTREACHED */
4663		FORKEE_ASSERTX(0 &&
4664		    "Child should be terminated by a signal from its parent");
4665	}
4666	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4667
4668	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4669	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4670
4671	validate_status_stopped(status, sigval);
4672
4673	printf("Before resuming the child process where it left off and "
4674	    "without signal to be sent\n");
4675	ATF_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
4676
4677	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4678	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4679
4680	validate_status_signaled(status, SIGKILL, 0);
4681
4682	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4683	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4684}
4685
4686ATF_TC(lwpinfo1);
4687ATF_TC_HEAD(lwpinfo1, tc)
4688{
4689	atf_tc_set_md_var(tc, "descr",
4690	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
4691}
4692
4693ATF_TC_BODY(lwpinfo1, tc)
4694{
4695	const int exitval = 5;
4696	const int sigval = SIGSTOP;
4697	pid_t child, wpid;
4698#if defined(TWAIT_HAVE_STATUS)
4699	int status;
4700#endif
4701	struct ptrace_lwpinfo info = {0, 0};
4702
4703	printf("Before forking process PID=%d\n", getpid());
4704	ATF_REQUIRE((child = fork()) != -1);
4705	if (child == 0) {
4706		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
4707		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4708
4709		printf("Before raising %s from child\n", strsignal(sigval));
4710		FORKEE_ASSERT(raise(sigval) == 0);
4711
4712		printf("Before exiting of the child process\n");
4713		_exit(exitval);
4714	}
4715	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4716
4717	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4718	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4719
4720	validate_status_stopped(status, sigval);
4721
4722	printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
4723	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4724
4725	printf("Assert that there exists a thread\n");
4726	ATF_REQUIRE(info.pl_lwpid > 0);
4727
4728	printf("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
4729	    info.pl_lwpid);
4730	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
4731	    "Received event %d != expected event %d",
4732	    info.pl_event, PL_EVENT_SIGNAL);
4733
4734	printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
4735	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4736
4737	printf("Assert that there are no more lwp threads in child\n");
4738	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
4739
4740	printf("Before resuming the child process where it left off and "
4741	    "without signal to be sent\n");
4742	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4743
4744	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4745	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4746
4747	validate_status_exited(status, exitval);
4748
4749	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4750	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4751}
4752
4753#if defined(TWAIT_HAVE_PID)
4754ATF_TC(lwpinfo2);
4755ATF_TC_HEAD(lwpinfo2, tc)
4756{
4757	atf_tc_set_md_var(tc, "descr",
4758	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
4759	    "tracer)");
4760}
4761
4762ATF_TC_BODY(lwpinfo2, tc)
4763{
4764	struct msg_fds parent_tracee, parent_tracer;
4765	const int exitval_tracee = 5;
4766	const int exitval_tracer = 10;
4767	pid_t tracee, tracer, wpid;
4768	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4769#if defined(TWAIT_HAVE_STATUS)
4770	int status;
4771#endif
4772	struct ptrace_lwpinfo info = {0, 0};
4773
4774	printf("Spawn tracee\n");
4775	ATF_REQUIRE(msg_open(&parent_tracee) == 0);
4776	ATF_REQUIRE(msg_open(&parent_tracer) == 0);
4777	tracee = atf_utils_fork();
4778	if (tracee == 0) {
4779
4780		/* Wait for message from the parent */
4781		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
4782		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
4783
4784		_exit(exitval_tracee);
4785	}
4786	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
4787
4788	printf("Spawn debugger\n");
4789	tracer = atf_utils_fork();
4790	if (tracer == 0) {
4791		/* No IPC to communicate with the child */
4792		printf("Before calling PT_ATTACH from tracee %d\n", getpid());
4793		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
4794
4795		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
4796		FORKEE_REQUIRE_SUCCESS(
4797		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4798
4799		forkee_status_stopped(status, SIGSTOP);
4800
4801		printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
4802		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4803		    != -1);
4804
4805		printf("Assert that there exists a thread\n");
4806		FORKEE_ASSERTX(info.pl_lwpid > 0);
4807
4808		printf("Assert that lwp thread %d received event "
4809		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
4810		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
4811
4812		printf("Before calling ptrace(2) with PT_LWPINFO for child\n");
4813		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4814		    != -1);
4815
4816		printf("Assert that there are no more lwp threads in child\n");
4817		FORKEE_ASSERTX(info.pl_lwpid == 0);
4818
4819		/* Resume tracee with PT_CONTINUE */
4820		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
4821
4822		/* Inform parent that tracer has attached to tracee */
4823		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
4824		/* Wait for parent */
4825		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
4826
4827		/* Wait for tracee and assert that it exited */
4828		FORKEE_REQUIRE_SUCCESS(
4829		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4830
4831		forkee_status_exited(status, exitval_tracee);
4832
4833		printf("Before exiting of the tracer process\n");
4834		_exit(exitval_tracer);
4835	}
4836
4837	printf("Wait for the tracer to attach to the tracee\n");
4838	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
4839
4840	printf("Resume the tracee and let it exit\n");
4841	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
4842
4843	printf("Detect that tracee is zombie\n");
4844	await_zombie(tracee);
4845
4846	printf("Assert that there is no status about tracee - "
4847	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
4848	TWAIT_REQUIRE_SUCCESS(
4849	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
4850
4851	printf("Resume the tracer and let it detect exited tracee\n");
4852	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
4853
4854	printf("Wait for tracer to finish its job and exit - calling %s()\n",
4855	    TWAIT_FNAME);
4856	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
4857	    tracer);
4858
4859	validate_status_exited(status, exitval_tracer);
4860
4861	printf("Wait for tracee to finish its job and exit - calling %s()\n",
4862	    TWAIT_FNAME);
4863	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
4864	    tracee);
4865
4866	validate_status_exited(status, exitval_tracee);
4867
4868	msg_close(&parent_tracer);
4869	msg_close(&parent_tracee);
4870}
4871#endif
4872
4873ATF_TC(siginfo1);
4874ATF_TC_HEAD(siginfo1, tc)
4875{
4876	atf_tc_set_md_var(tc, "descr",
4877	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
4878}
4879
4880ATF_TC_BODY(siginfo1, tc)
4881{
4882	const int exitval = 5;
4883	const int sigval = SIGTRAP;
4884	pid_t child, wpid;
4885#if defined(TWAIT_HAVE_STATUS)
4886	int status;
4887#endif
4888	struct ptrace_siginfo info;
4889	memset(&info, 0, sizeof(info));
4890
4891	printf("Before forking process PID=%d\n", getpid());
4892	ATF_REQUIRE((child = fork()) != -1);
4893	if (child == 0) {
4894		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
4895		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4896
4897		printf("Before raising %s from child\n", strsignal(sigval));
4898		FORKEE_ASSERT(raise(sigval) == 0);
4899
4900		printf("Before exiting of the child process\n");
4901		_exit(exitval);
4902	}
4903	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4904
4905	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4906	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4907
4908	validate_status_stopped(status, sigval);
4909
4910	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4911	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4912
4913	printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
4914	printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4915	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4916	    info.psi_siginfo.si_errno);
4917
4918	printf("Before resuming the child process where it left off and "
4919	    "without signal to be sent\n");
4920	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4921
4922	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4923	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4924
4925	validate_status_exited(status, exitval);
4926
4927	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4928	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4929}
4930
4931ATF_TC(siginfo2);
4932ATF_TC_HEAD(siginfo2, tc)
4933{
4934	atf_tc_set_md_var(tc, "descr",
4935	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
4936	    "modification of SIGINT from tracee");
4937}
4938
4939static int siginfo2_caught = 0;
4940
4941static void
4942siginfo2_sighandler(int sig)
4943{
4944	FORKEE_ASSERT_EQ(sig, SIGINT);
4945
4946	++siginfo2_caught;
4947}
4948
4949ATF_TC_BODY(siginfo2, tc)
4950{
4951	const int exitval = 5;
4952	const int sigval = SIGINT;
4953	pid_t child, wpid;
4954	struct sigaction sa;
4955#if defined(TWAIT_HAVE_STATUS)
4956	int status;
4957#endif
4958	struct ptrace_siginfo info;
4959	memset(&info, 0, sizeof(info));
4960
4961	printf("Before forking process PID=%d\n", getpid());
4962	ATF_REQUIRE((child = fork()) != -1);
4963	if (child == 0) {
4964		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
4965		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4966
4967		sa.sa_handler = siginfo2_sighandler;
4968		sa.sa_flags = SA_SIGINFO;
4969		sigemptyset(&sa.sa_mask);
4970
4971		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
4972
4973		printf("Before raising %s from child\n", strsignal(sigval));
4974		FORKEE_ASSERT(raise(sigval) == 0);
4975
4976		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
4977
4978		printf("Before exiting of the child process\n");
4979		_exit(exitval);
4980	}
4981	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4982
4983	printf("Before calling %s() for the child\n", TWAIT_FNAME);
4984	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4985
4986	validate_status_stopped(status, sigval);
4987
4988	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4989	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4990
4991	printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
4992	printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4993	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4994	    info.psi_siginfo.si_errno);
4995
4996	printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
4997	ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
4998
4999	printf("Before resuming the child process where it left off and "
5000	    "without signal to be sent\n");
5001	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
5002
5003	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5004	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5005
5006	validate_status_exited(status, exitval);
5007
5008	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5009	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5010}
5011
5012ATF_TC(siginfo3);
5013ATF_TC_HEAD(siginfo3, tc)
5014{
5015	atf_tc_set_md_var(tc, "descr",
5016	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
5017	    "setting signal to new value");
5018}
5019
5020static int siginfo3_caught = 0;
5021
5022static void
5023siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
5024{
5025	FORKEE_ASSERT_EQ(sig, SIGTRAP);
5026
5027	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
5028	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
5029
5030	++siginfo3_caught;
5031}
5032
5033ATF_TC_BODY(siginfo3, tc)
5034{
5035	const int exitval = 5;
5036	const int sigval = SIGINT;
5037	const int sigfaked = SIGTRAP;
5038	const int sicodefaked = TRAP_BRKPT;
5039	pid_t child, wpid;
5040	struct sigaction sa;
5041#if defined(TWAIT_HAVE_STATUS)
5042	int status;
5043#endif
5044	struct ptrace_siginfo info;
5045	memset(&info, 0, sizeof(info));
5046
5047	printf("Before forking process PID=%d\n", getpid());
5048	ATF_REQUIRE((child = fork()) != -1);
5049	if (child == 0) {
5050		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5051		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5052
5053		sa.sa_sigaction = siginfo3_sigaction;
5054		sa.sa_flags = SA_SIGINFO;
5055		sigemptyset(&sa.sa_mask);
5056
5057		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
5058
5059		printf("Before raising %s from child\n", strsignal(sigval));
5060		FORKEE_ASSERT(raise(sigval) == 0);
5061
5062		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
5063
5064		printf("Before exiting of the child process\n");
5065		_exit(exitval);
5066	}
5067	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5068
5069	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5070	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5071
5072	validate_status_stopped(status, sigval);
5073
5074	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5075	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5076
5077	printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
5078	printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5079	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5080	    info.psi_siginfo.si_errno);
5081
5082	printf("Before setting new faked signal to signo=%d si_code=%d\n",
5083	    sigfaked, sicodefaked);
5084	info.psi_siginfo.si_signo = sigfaked;
5085	info.psi_siginfo.si_code = sicodefaked;
5086
5087	printf("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
5088	ATF_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
5089
5090	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5091	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5092
5093	printf("Before checking siginfo_t\n");
5094	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
5095	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
5096
5097	printf("Before resuming the child process where it left off and "
5098	    "without signal to be sent\n");
5099	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
5100
5101	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5102	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5103
5104	validate_status_exited(status, exitval);
5105
5106	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5107	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5108}
5109
5110ATF_TC(siginfo4);
5111ATF_TC_HEAD(siginfo4, tc)
5112{
5113	atf_tc_set_md_var(tc, "descr",
5114	    "Detect SIGTRAP TRAP_EXEC from tracee");
5115}
5116
5117ATF_TC_BODY(siginfo4, tc)
5118{
5119	const int sigval = SIGTRAP;
5120	pid_t child, wpid;
5121#if defined(TWAIT_HAVE_STATUS)
5122	int status;
5123#endif
5124
5125	struct ptrace_siginfo info;
5126	memset(&info, 0, sizeof(info));
5127
5128	printf("Before forking process PID=%d\n", getpid());
5129	ATF_REQUIRE((child = fork()) != -1);
5130	if (child == 0) {
5131		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5132		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5133
5134		printf("Before calling execve(2) from child\n");
5135		execlp("/bin/echo", "/bin/echo", NULL);
5136
5137		FORKEE_ASSERT(0 && "Not reached");
5138	}
5139	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5140
5141	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5142	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5143
5144	validate_status_stopped(status, sigval);
5145
5146	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5147	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5148
5149	printf("Signal traced to lwpid=%d\n", info.psi_lwpid);
5150	printf("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5151	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5152	    info.psi_siginfo.si_errno);
5153
5154	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5155	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
5156
5157	printf("Before resuming the child process where it left off and "
5158	    "without signal to be sent\n");
5159	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5160
5161	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5162	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5163
5164	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5165	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5166}
5167
5168#if defined(TWAIT_HAVE_PID)
5169ATF_TC(siginfo5);
5170ATF_TC_HEAD(siginfo5, tc)
5171{
5172	atf_tc_set_md_var(tc, "descr",
5173	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
5174	    "set to PTRACE_FORK and reports correct signal information");
5175}
5176
5177ATF_TC_BODY(siginfo5, tc)
5178{
5179	const int exitval = 5;
5180	const int exitval2 = 15;
5181	const int sigval = SIGSTOP;
5182	pid_t child, child2, wpid;
5183#if defined(TWAIT_HAVE_STATUS)
5184	int status;
5185#endif
5186	ptrace_state_t state;
5187	const int slen = sizeof(state);
5188	ptrace_event_t event;
5189	const int elen = sizeof(event);
5190	struct ptrace_siginfo info;
5191
5192	memset(&info, 0, sizeof(info));
5193
5194	printf("Before forking process PID=%d\n", getpid());
5195	ATF_REQUIRE((child = fork()) != -1);
5196	if (child == 0) {
5197		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5198		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5199
5200		printf("Before raising %s from child\n", strsignal(sigval));
5201		FORKEE_ASSERT(raise(sigval) == 0);
5202
5203		FORKEE_ASSERT((child2 = fork()) != -1);
5204
5205		if (child2 == 0)
5206			_exit(exitval2);
5207
5208		FORKEE_REQUIRE_SUCCESS
5209		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5210
5211		forkee_status_exited(status, exitval2);
5212
5213		printf("Before exiting of the child process\n");
5214		_exit(exitval);
5215	}
5216	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5217
5218	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5219	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5220
5221	validate_status_stopped(status, sigval);
5222
5223	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5224	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5225
5226	printf("Before checking siginfo_t\n");
5227	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5228	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5229
5230	printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
5231	event.pe_set_event = PTRACE_FORK;
5232	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5233
5234	printf("Before resuming the child process where it left off and "
5235	    "without signal to be sent\n");
5236        printf("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
5237               "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
5238               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
5239                "state.pe_other_pid=child)\n", child);
5240	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5241
5242	printf("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
5243	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5244
5245	validate_status_stopped(status, SIGTRAP);
5246
5247	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5248	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5249
5250	printf("Before checking siginfo_t\n");
5251	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5252	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5253
5254	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5255	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5256
5257	child2 = state.pe_other_pid;
5258	printf("Reported PTRACE_FORK event with forkee %d\n", child2);
5259
5260	printf("Before calling %s() for the forkee %d of the child %d\n",
5261	    TWAIT_FNAME, child2, child);
5262	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5263	    child2);
5264
5265	validate_status_stopped(status, SIGTRAP);
5266
5267	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5268	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5269
5270	printf("Before checking siginfo_t\n");
5271	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5272	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5273
5274	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
5275	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5276	ATF_REQUIRE_EQ(state.pe_other_pid, child);
5277
5278	printf("Before resuming the forkee process where it left off and "
5279	    "without signal to be sent\n");
5280	ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
5281
5282	printf("Before resuming the child process where it left off and "
5283	    "without signal to be sent\n");
5284	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5285
5286	printf("Before calling %s() for the forkee - expected exited\n",
5287	    TWAIT_FNAME);
5288	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5289	    child2);
5290
5291	validate_status_exited(status, exitval2);
5292
5293	printf("Before calling %s() for the forkee - expected no process\n",
5294	    TWAIT_FNAME);
5295	TWAIT_REQUIRE_FAILURE(ECHILD,
5296	    wpid = TWAIT_GENERIC(child2, &status, 0));
5297
5298	printf("Before calling %s() for the child - expected stopped "
5299	    "SIGCHLD\n", TWAIT_FNAME);
5300	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5301
5302	validate_status_stopped(status, SIGCHLD);
5303
5304	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5305	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5306
5307	printf("Before checking siginfo_t\n");
5308	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
5309	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
5310
5311	printf("Before resuming the child process where it left off and "
5312	    "without signal to be sent\n");
5313	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5314
5315	printf("Before calling %s() for the child - expected exited\n",
5316	    TWAIT_FNAME);
5317	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5318
5319	validate_status_exited(status, exitval);
5320
5321	printf("Before calling %s() for the child - expected no process\n",
5322	    TWAIT_FNAME);
5323	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5324}
5325#endif
5326
5327#if defined(PT_STEP)
5328ATF_TC(siginfo6);
5329ATF_TC_HEAD(siginfo6, tc)
5330{
5331	atf_tc_set_md_var(tc, "descr",
5332	    "Verify single PT_STEP call with signal information check");
5333}
5334
5335ATF_TC_BODY(siginfo6, tc)
5336{
5337	const int exitval = 5;
5338	const int sigval = SIGSTOP;
5339	pid_t child, wpid;
5340#if defined(TWAIT_HAVE_STATUS)
5341	int status;
5342#endif
5343	int happy;
5344	struct ptrace_siginfo info;
5345
5346#if defined(__arm__)
5347	/* PT_STEP not supported on arm 32-bit */
5348	atf_tc_expect_fail("PR kern/52119");
5349#endif
5350
5351	memset(&info, 0, sizeof(info));
5352
5353	printf("Before forking process PID=%d\n", getpid());
5354	ATF_REQUIRE((child = fork()) != -1);
5355	if (child == 0) {
5356		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5357		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5358
5359		happy = check_happy(100);
5360
5361		printf("Before raising %s from child\n", strsignal(sigval));
5362		FORKEE_ASSERT(raise(sigval) == 0);
5363
5364		FORKEE_ASSERT_EQ(happy, check_happy(100));
5365
5366		printf("Before exiting of the child process\n");
5367		_exit(exitval);
5368	}
5369	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5370
5371	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5372	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5373
5374	validate_status_stopped(status, sigval);
5375
5376	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5377	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5378
5379	printf("Before checking siginfo_t\n");
5380	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5381	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5382
5383	printf("Before resuming the child process where it left off and "
5384	    "without signal to be sent (use PT_STEP)\n");
5385	ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5386
5387	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5388	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5389
5390	validate_status_stopped(status, SIGTRAP);
5391
5392	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5393	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5394
5395	printf("Before checking siginfo_t\n");
5396	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5397	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
5398
5399	printf("Before resuming the child process where it left off and "
5400	    "without signal to be sent\n");
5401	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5402
5403	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5404	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5405
5406	validate_status_exited(status, exitval);
5407
5408	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5409	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5410}
5411#endif
5412
5413volatile lwpid_t the_lwp_id = 0;
5414
5415static void
5416lwp_main_func(void *arg)
5417{
5418	the_lwp_id = _lwp_self();
5419	_lwp_exit();
5420}
5421
5422ATF_TC(lwp_create1);
5423ATF_TC_HEAD(lwp_create1, tc)
5424{
5425	atf_tc_set_md_var(tc, "descr",
5426	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5427	    "EVENT_MASK set to PTRACE_LWP_CREATE");
5428}
5429
5430ATF_TC_BODY(lwp_create1, tc)
5431{
5432	const int exitval = 5;
5433	const int sigval = SIGSTOP;
5434	pid_t child, wpid;
5435#if defined(TWAIT_HAVE_STATUS)
5436	int status;
5437#endif
5438	ptrace_state_t state;
5439	const int slen = sizeof(state);
5440	ptrace_event_t event;
5441	const int elen = sizeof(event);
5442	ucontext_t uc;
5443	lwpid_t lid;
5444	static const size_t ssize = 16*1024;
5445	void *stack;
5446
5447	printf("Before forking process PID=%d\n", getpid());
5448	ATF_REQUIRE((child = fork()) != -1);
5449	if (child == 0) {
5450		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5451		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5452
5453		printf("Before raising %s from child\n", strsignal(sigval));
5454		FORKEE_ASSERT(raise(sigval) == 0);
5455
5456		printf("Before allocating memory for stack in child\n");
5457		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5458
5459		printf("Before making context for new lwp in child\n");
5460		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5461
5462		printf("Before creating new in child\n");
5463		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5464
5465		printf("Before waiting for lwp %d to exit\n", lid);
5466		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5467
5468		printf("Before verifying that reported %d and running lid %d "
5469		    "are the same\n", lid, the_lwp_id);
5470		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5471
5472		printf("Before exiting of the child process\n");
5473		_exit(exitval);
5474	}
5475	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5476
5477	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5478	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5479
5480	validate_status_stopped(status, sigval);
5481
5482	printf("Set empty EVENT_MASK for the child %d\n", child);
5483	event.pe_set_event = PTRACE_LWP_CREATE;
5484	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5485
5486	printf("Before resuming the child process where it left off and "
5487	    "without signal to be sent\n");
5488	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5489
5490	printf("Before calling %s() for the child - expected stopped "
5491	    "SIGTRAP\n", TWAIT_FNAME);
5492	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5493
5494	validate_status_stopped(status, SIGTRAP);
5495
5496	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5497
5498	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
5499
5500	lid = state.pe_lwp;
5501	printf("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
5502
5503	printf("Before resuming the child process where it left off and "
5504	    "without signal to be sent\n");
5505	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5506
5507	printf("Before calling %s() for the child - expected exited\n",
5508	    TWAIT_FNAME);
5509	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5510
5511	validate_status_exited(status, exitval);
5512
5513	printf("Before calling %s() for the child - expected no process\n",
5514	    TWAIT_FNAME);
5515	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5516}
5517
5518ATF_TC(lwp_exit1);
5519ATF_TC_HEAD(lwp_exit1, tc)
5520{
5521	atf_tc_set_md_var(tc, "descr",
5522	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5523	    "EVENT_MASK set to PTRACE_LWP_EXIT");
5524}
5525
5526ATF_TC_BODY(lwp_exit1, tc)
5527{
5528	const int exitval = 5;
5529	const int sigval = SIGSTOP;
5530	pid_t child, wpid;
5531#if defined(TWAIT_HAVE_STATUS)
5532	int status;
5533#endif
5534	ptrace_state_t state;
5535	const int slen = sizeof(state);
5536	ptrace_event_t event;
5537	const int elen = sizeof(event);
5538	ucontext_t uc;
5539	lwpid_t lid;
5540	static const size_t ssize = 16*1024;
5541	void *stack;
5542
5543	printf("Before forking process PID=%d\n", getpid());
5544	ATF_REQUIRE((child = fork()) != -1);
5545	if (child == 0) {
5546		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5547		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5548
5549		printf("Before raising %s from child\n", strsignal(sigval));
5550		FORKEE_ASSERT(raise(sigval) == 0);
5551
5552		printf("Before allocating memory for stack in child\n");
5553		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5554
5555		printf("Before making context for new lwp in child\n");
5556		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5557
5558		printf("Before creating new in child\n");
5559		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5560
5561		printf("Before waiting for lwp %d to exit\n", lid);
5562		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5563
5564		printf("Before verifying that reported %d and running lid %d "
5565		    "are the same\n", lid, the_lwp_id);
5566		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5567
5568		printf("Before exiting of the child process\n");
5569		_exit(exitval);
5570	}
5571	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5572
5573	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5574	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5575
5576	validate_status_stopped(status, sigval);
5577
5578	printf("Set empty EVENT_MASK for the child %d\n", child);
5579	event.pe_set_event = PTRACE_LWP_EXIT;
5580	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5581
5582	printf("Before resuming the child process where it left off and "
5583	    "without signal to be sent\n");
5584	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5585
5586	printf("Before calling %s() for the child - expected stopped "
5587	    "SIGTRAP\n", TWAIT_FNAME);
5588	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5589
5590	validate_status_stopped(status, SIGTRAP);
5591
5592	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5593
5594	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
5595
5596	lid = state.pe_lwp;
5597	printf("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
5598
5599	printf("Before resuming the child process where it left off and "
5600	    "without signal to be sent\n");
5601	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5602
5603	printf("Before calling %s() for the child - expected exited\n",
5604	    TWAIT_FNAME);
5605	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5606
5607	validate_status_exited(status, exitval);
5608
5609	printf("Before calling %s() for the child - expected no process\n",
5610	    TWAIT_FNAME);
5611	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5612}
5613
5614ATF_TC(signal1);
5615ATF_TC_HEAD(signal1, tc)
5616{
5617	atf_tc_set_md_var(tc, "descr",
5618	    "Verify that masking single unrelated signal does not stop tracer "
5619	    "from catching other signals");
5620}
5621
5622ATF_TC_BODY(signal1, tc)
5623{
5624	const int exitval = 5;
5625	const int sigval = SIGSTOP;
5626	const int sigmasked = SIGTRAP;
5627	const int signotmasked = SIGINT;
5628	pid_t child, wpid;
5629#if defined(TWAIT_HAVE_STATUS)
5630	int status;
5631#endif
5632	sigset_t intmask;
5633
5634	printf("Before forking process PID=%d\n", getpid());
5635	ATF_REQUIRE((child = fork()) != -1);
5636	if (child == 0) {
5637		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5638		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5639
5640		sigemptyset(&intmask);
5641		sigaddset(&intmask, sigmasked);
5642		sigprocmask(SIG_BLOCK, &intmask, NULL);
5643
5644		printf("Before raising %s from child\n", strsignal(sigval));
5645		FORKEE_ASSERT(raise(sigval) == 0);
5646
5647		printf("Before raising %s from child\n",
5648		    strsignal(signotmasked));
5649		FORKEE_ASSERT(raise(signotmasked) == 0);
5650
5651		printf("Before exiting of the child process\n");
5652		_exit(exitval);
5653	}
5654	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5655
5656	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5657	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5658
5659	validate_status_stopped(status, sigval);
5660
5661	printf("Before resuming the child process where it left off and "
5662	    "without signal to be sent\n");
5663	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5664
5665	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5666	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5667
5668	validate_status_stopped(status, signotmasked);
5669
5670	printf("Before resuming the child process where it left off and "
5671	    "without signal to be sent\n");
5672	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5673
5674	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5675	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5676
5677	validate_status_exited(status, exitval);
5678
5679	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5680	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5681}
5682
5683ATF_TC(signal2);
5684ATF_TC_HEAD(signal2, tc)
5685{
5686	atf_tc_set_md_var(tc, "descr",
5687	    "Verify that masking SIGTRAP in tracee stops tracer from "
5688	    "catching this raised signal");
5689}
5690
5691ATF_TC_BODY(signal2, tc)
5692{
5693	const int exitval = 5;
5694	const int sigval = SIGSTOP;
5695	const int sigmasked = SIGTRAP;
5696	pid_t child, wpid;
5697#if defined(TWAIT_HAVE_STATUS)
5698	int status;
5699#endif
5700	sigset_t intmask;
5701
5702	printf("Before forking process PID=%d\n", getpid());
5703	ATF_REQUIRE((child = fork()) != -1);
5704	if (child == 0) {
5705		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5706		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5707
5708		sigemptyset(&intmask);
5709		sigaddset(&intmask, sigmasked);
5710		sigprocmask(SIG_BLOCK, &intmask, NULL);
5711
5712		printf("Before raising %s from child\n", strsignal(sigval));
5713		FORKEE_ASSERT(raise(sigval) == 0);
5714
5715		printf("Before raising %s breakpoint from child\n",
5716		    strsignal(sigmasked));
5717		FORKEE_ASSERT(raise(sigmasked) == 0);
5718
5719		printf("Before exiting of the child process\n");
5720		_exit(exitval);
5721	}
5722	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5723
5724	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5725	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5726
5727	validate_status_stopped(status, sigval);
5728
5729	printf("Before resuming the child process where it left off and "
5730	    "without signal to be sent\n");
5731	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5732
5733	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5734	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5735
5736	validate_status_exited(status, exitval);
5737
5738	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5739	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5740}
5741
5742ATF_TC(signal3);
5743ATF_TC_HEAD(signal3, tc)
5744{
5745	atf_tc_set_md_var(tc, "timeout", "5");
5746	atf_tc_set_md_var(tc, "descr",
5747	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5748	    "catching software breakpoints");
5749}
5750
5751ATF_TC_BODY(signal3, tc)
5752{
5753	const int exitval = 5;
5754	const int sigval = SIGSTOP;
5755	const int sigmasked = SIGTRAP;
5756	pid_t child, wpid;
5757#if defined(TWAIT_HAVE_STATUS)
5758	int status;
5759#endif
5760	sigset_t intmask;
5761
5762#if defined(__sparc__) && !defined(__sparc64__)
5763	atf_tc_expect_timeout("PR kern/52167");
5764
5765	// timeout wins, failure still valid
5766	// atf_tc_expect_fail("PR kern/51918");
5767#else
5768	atf_tc_expect_fail("PR kern/51918");
5769#endif
5770
5771	printf("Before forking process PID=%d\n", getpid());
5772	ATF_REQUIRE((child = fork()) != -1);
5773	if (child == 0) {
5774		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5775		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5776
5777		sigemptyset(&intmask);
5778		sigaddset(&intmask, sigmasked);
5779		sigprocmask(SIG_BLOCK, &intmask, NULL);
5780
5781		printf("Before raising %s from child\n", strsignal(sigval));
5782		FORKEE_ASSERT(raise(sigval) == 0);
5783
5784		printf("Before raising software breakpoint from child\n");
5785
5786#ifdef PTRACE_BREAKPOINT_ASM
5787		PTRACE_BREAKPOINT_ASM;
5788#else
5789		/* port me */
5790#endif
5791
5792		printf("Before exiting of the child process\n");
5793		_exit(exitval);
5794	}
5795	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5796
5797	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5798	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5799
5800	validate_status_stopped(status, sigval);
5801
5802	printf("Before resuming the child process where it left off and "
5803	    "without signal to be sent\n");
5804	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5805
5806	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5807	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5808
5809	validate_status_stopped(status, sigmasked);
5810
5811	printf("Before resuming the child process where it left off and "
5812	    "without signal to be sent\n");
5813	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5814
5815	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5816	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5817
5818	validate_status_exited(status, exitval);
5819
5820	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5821	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5822}
5823
5824#if defined(PT_STEP)
5825ATF_TC(signal4);
5826ATF_TC_HEAD(signal4, tc)
5827{
5828	atf_tc_set_md_var(tc, "descr",
5829	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5830	    "catching single step trap");
5831}
5832
5833ATF_TC_BODY(signal4, tc)
5834{
5835	const int exitval = 5;
5836	const int sigval = SIGSTOP;
5837	const int sigmasked = SIGTRAP;
5838	pid_t child, wpid;
5839#if defined(TWAIT_HAVE_STATUS)
5840	int status;
5841#endif
5842	sigset_t intmask;
5843	int happy;
5844
5845#if defined(__arm__)
5846	/* PT_STEP not supported on arm 32-bit */
5847	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
5848#else
5849	atf_tc_expect_fail("PR kern/52118");
5850#endif
5851
5852	printf("Before forking process PID=%d\n", getpid());
5853	ATF_REQUIRE((child = fork()) != -1);
5854	if (child == 0) {
5855		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5856		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5857
5858		happy = check_happy(100);
5859
5860		sigemptyset(&intmask);
5861		sigaddset(&intmask, sigmasked);
5862		sigprocmask(SIG_BLOCK, &intmask, NULL);
5863
5864		printf("Before raising %s from child\n", strsignal(sigval));
5865		FORKEE_ASSERT(raise(sigval) == 0);
5866
5867		FORKEE_ASSERT_EQ(happy, check_happy(100));
5868
5869		printf("Before exiting of the child process\n");
5870		_exit(exitval);
5871	}
5872	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5873
5874	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5875	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5876
5877	validate_status_stopped(status, sigval);
5878
5879	printf("Before resuming the child process where it left off and "
5880	    "without signal to be sent\n");
5881	ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5882
5883	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5884	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5885
5886	validate_status_stopped(status, sigmasked);
5887
5888	printf("Before resuming the child process where it left off and "
5889	    "without signal to be sent\n");
5890	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5891
5892	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5893	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5894
5895	validate_status_exited(status, exitval);
5896
5897	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5898	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5899}
5900#endif
5901
5902ATF_TC(signal5);
5903ATF_TC_HEAD(signal5, tc)
5904{
5905	atf_tc_set_md_var(tc, "descr",
5906	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5907	    "catching exec() breakpoint");
5908}
5909
5910ATF_TC_BODY(signal5, tc)
5911{
5912	const int exitval = 5;
5913	const int sigval = SIGSTOP;
5914	const int sigmasked = SIGTRAP;
5915	pid_t child, wpid;
5916#if defined(TWAIT_HAVE_STATUS)
5917	int status;
5918#endif
5919	sigset_t intmask;
5920
5921	atf_tc_expect_fail("PR kern/51918");
5922
5923	printf("Before forking process PID=%d\n", getpid());
5924	ATF_REQUIRE((child = fork()) != -1);
5925	if (child == 0) {
5926		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5927		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5928
5929		sigemptyset(&intmask);
5930		sigaddset(&intmask, sigmasked);
5931		sigprocmask(SIG_BLOCK, &intmask, NULL);
5932
5933		printf("Before raising %s from child\n", strsignal(sigval));
5934		FORKEE_ASSERT(raise(sigval) == 0);
5935
5936		printf("Before calling execve(2) from child\n");
5937		execlp("/bin/echo", "/bin/echo", NULL);
5938
5939		printf("Before exiting of the child process\n");
5940		_exit(exitval);
5941	}
5942	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5943
5944	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5945	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5946
5947	validate_status_stopped(status, sigval);
5948
5949	printf("Before resuming the child process where it left off and "
5950	    "without signal to be sent\n");
5951	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5952
5953	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5954	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5955
5956	validate_status_stopped(status, sigmasked);
5957
5958	printf("Before resuming the child process where it left off and "
5959	    "without signal to be sent\n");
5960	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5961
5962	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5963	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5964
5965	validate_status_exited(status, exitval);
5966
5967	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5968	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5969}
5970
5971#if defined(TWAIT_HAVE_PID)
5972ATF_TC(signal6);
5973ATF_TC_HEAD(signal6, tc)
5974{
5975	atf_tc_set_md_var(tc, "timeout", "5");
5976	atf_tc_set_md_var(tc, "descr",
5977	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5978	    "catching PTRACE_FORK breakpoint");
5979}
5980
5981ATF_TC_BODY(signal6, tc)
5982{
5983	const int exitval = 5;
5984	const int exitval2 = 15;
5985	const int sigval = SIGSTOP;
5986	const int sigmasked = SIGTRAP;
5987	pid_t child, child2, wpid;
5988#if defined(TWAIT_HAVE_STATUS)
5989	int status;
5990#endif
5991	sigset_t intmask;
5992	ptrace_state_t state;
5993	const int slen = sizeof(state);
5994	ptrace_event_t event;
5995	const int elen = sizeof(event);
5996
5997	atf_tc_expect_timeout("PR kern/51918");
5998
5999	printf("Before forking process PID=%d\n", getpid());
6000	ATF_REQUIRE((child = fork()) != -1);
6001	if (child == 0) {
6002		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6003		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6004
6005		sigemptyset(&intmask);
6006		sigaddset(&intmask, sigmasked);
6007		sigprocmask(SIG_BLOCK, &intmask, NULL);
6008
6009		printf("Before raising %s from child\n", strsignal(sigval));
6010		FORKEE_ASSERT(raise(sigval) == 0);
6011
6012		FORKEE_ASSERT((child2 = fork()) != -1);
6013
6014		if (child2 == 0)
6015			_exit(exitval2);
6016
6017		FORKEE_REQUIRE_SUCCESS
6018			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6019
6020		forkee_status_exited(status, exitval2);
6021
6022		printf("Before exiting of the child process\n");
6023		_exit(exitval);
6024	}
6025	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6026
6027	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6028	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6029
6030	validate_status_stopped(status, sigval);
6031
6032	printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
6033	event.pe_set_event = PTRACE_FORK;
6034	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6035
6036	printf("Before resuming the child process where it left off and "
6037	    "without signal to be sent\n");
6038	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6039
6040	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6041	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6042
6043	validate_status_stopped(status, sigmasked);
6044
6045	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6046	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6047
6048	child2 = state.pe_other_pid;
6049	printf("Reported PTRACE_FORK event with forkee %d\n", child2);
6050
6051	printf("Before calling %s() for the child2\n", TWAIT_FNAME);
6052	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6053	    child2);
6054
6055	validate_status_stopped(status, SIGTRAP);
6056
6057	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6058	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6059	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6060
6061	printf("Before resuming the forkee process where it left off and "
6062	    "without signal to be sent\n");
6063	ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6064
6065	printf("Before resuming the child process where it left off and "
6066	    "without signal to be sent\n");
6067	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6068
6069	printf("Before calling %s() for the forkee - expected exited\n",
6070	    TWAIT_FNAME);
6071	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6072	    child2);
6073
6074	validate_status_exited(status, exitval2);
6075
6076	printf("Before calling %s() for the forkee - expected no process\n",
6077	    TWAIT_FNAME);
6078	TWAIT_REQUIRE_FAILURE(ECHILD,
6079	    wpid = TWAIT_GENERIC(child2, &status, 0));
6080
6081	printf("Before calling %s() for the child - expected stopped "
6082	    "SIGCHLD\n", TWAIT_FNAME);
6083	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6084
6085	validate_status_stopped(status, SIGCHLD);
6086
6087	printf("Before resuming the child process where it left off and "
6088	    "without signal to be sent\n");
6089	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6090
6091	printf("Before calling %s() for the child - expected exited\n",
6092	    TWAIT_FNAME);
6093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6094
6095	validate_status_exited(status, exitval);
6096
6097	printf("Before calling %s() for the child - expected no process\n",
6098	    TWAIT_FNAME);
6099	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6100}
6101#endif
6102
6103#if defined(TWAIT_HAVE_PID)
6104ATF_TC(signal7);
6105ATF_TC_HEAD(signal7, tc)
6106{
6107	atf_tc_set_md_var(tc, "descr",
6108	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6109	    "catching PTRACE_VFORK breakpoint");
6110}
6111
6112ATF_TC_BODY(signal7, tc)
6113{
6114	const int exitval = 5;
6115	const int exitval2 = 15;
6116	const int sigval = SIGSTOP;
6117	const int sigmasked = SIGTRAP;
6118	pid_t child, child2, wpid;
6119#if defined(TWAIT_HAVE_STATUS)
6120	int status;
6121#endif
6122	sigset_t intmask;
6123	ptrace_state_t state;
6124	const int slen = sizeof(state);
6125	ptrace_event_t event;
6126	const int elen = sizeof(event);
6127
6128	atf_tc_expect_fail("PR kern/51918 PR kern/51630");
6129
6130	printf("Before forking process PID=%d\n", getpid());
6131	ATF_REQUIRE((child = fork()) != -1);
6132	if (child == 0) {
6133		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6134		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6135
6136		sigemptyset(&intmask);
6137		sigaddset(&intmask, sigmasked);
6138		sigprocmask(SIG_BLOCK, &intmask, NULL);
6139
6140		printf("Before raising %s from child\n", strsignal(sigval));
6141		FORKEE_ASSERT(raise(sigval) == 0);
6142
6143		FORKEE_ASSERT((child2 = fork()) != -1);
6144
6145		if (child2 == 0)
6146			_exit(exitval2);
6147
6148		FORKEE_REQUIRE_SUCCESS
6149			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6150
6151		forkee_status_exited(status, exitval2);
6152
6153		printf("Before exiting of the child process\n");
6154		_exit(exitval);
6155	}
6156	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6157
6158	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6159	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6160
6161	validate_status_stopped(status, sigval);
6162
6163	printf("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
6164	event.pe_set_event = PTRACE_VFORK;
6165	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6166
6167	printf("Before resuming the child process where it left off and "
6168	    "without signal to be sent\n");
6169	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6170
6171	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6172	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6173
6174	validate_status_stopped(status, sigmasked);
6175
6176	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6177	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6178
6179	child2 = state.pe_other_pid;
6180	printf("Reported PTRACE_VFORK event with forkee %d\n", child2);
6181
6182	printf("Before calling %s() for the child2\n", TWAIT_FNAME);
6183	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6184	    child2);
6185
6186	validate_status_stopped(status, SIGTRAP);
6187
6188	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6189	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6190	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6191
6192	printf("Before resuming the forkee process where it left off and "
6193	    "without signal to be sent\n");
6194	ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6195
6196	printf("Before resuming the child process where it left off and "
6197	    "without signal to be sent\n");
6198	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6199
6200	printf("Before calling %s() for the forkee - expected exited\n",
6201	    TWAIT_FNAME);
6202	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6203	    child2);
6204
6205	validate_status_exited(status, exitval2);
6206
6207	printf("Before calling %s() for the forkee - expected no process\n",
6208	    TWAIT_FNAME);
6209	TWAIT_REQUIRE_FAILURE(ECHILD,
6210	    wpid = TWAIT_GENERIC(child2, &status, 0));
6211
6212	printf("Before calling %s() for the child - expected stopped "
6213	    "SIGCHLD\n", TWAIT_FNAME);
6214	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6215
6216	validate_status_stopped(status, SIGCHLD);
6217
6218	printf("Before resuming the child process where it left off and "
6219	    "without signal to be sent\n");
6220	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6221
6222	printf("Before calling %s() for the child - expected exited\n",
6223	    TWAIT_FNAME);
6224	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6225
6226	validate_status_exited(status, exitval);
6227
6228	printf("Before calling %s() for the child - expected no process\n",
6229	    TWAIT_FNAME);
6230	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6231}
6232#endif
6233
6234ATF_TC(signal8);
6235ATF_TC_HEAD(signal8, tc)
6236{
6237	atf_tc_set_md_var(tc, "descr",
6238	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6239	    "catching PTRACE_VFORK_DONE breakpoint");
6240}
6241
6242ATF_TC_BODY(signal8, tc)
6243{
6244	const int exitval = 5;
6245	const int exitval2 = 15;
6246	const int sigval = SIGSTOP;
6247	const int sigmasked = SIGTRAP;
6248	pid_t child, child2, wpid;
6249#if defined(TWAIT_HAVE_STATUS)
6250	int status;
6251#endif
6252	sigset_t intmask;
6253	ptrace_state_t state;
6254	const int slen = sizeof(state);
6255	ptrace_event_t event;
6256	const int elen = sizeof(event);
6257
6258	atf_tc_expect_fail("PR kern/51918");
6259
6260	printf("Before forking process PID=%d\n", getpid());
6261	ATF_REQUIRE((child = fork()) != -1);
6262	if (child == 0) {
6263		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6264		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6265
6266		sigemptyset(&intmask);
6267		sigaddset(&intmask, sigmasked);
6268		sigprocmask(SIG_BLOCK, &intmask, NULL);
6269
6270		printf("Before raising %s from child\n", strsignal(sigval));
6271		FORKEE_ASSERT(raise(sigval) == 0);
6272
6273		FORKEE_ASSERT((child2 = vfork()) != -1);
6274
6275		if (child2 == 0)
6276			_exit(exitval2);
6277
6278		FORKEE_REQUIRE_SUCCESS
6279			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6280
6281		forkee_status_exited(status, exitval2);
6282
6283		printf("Before exiting of the child process\n");
6284		_exit(exitval);
6285	}
6286	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6287
6288	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6289	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6290
6291	validate_status_stopped(status, sigval);
6292
6293	printf("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
6294	    child);
6295	event.pe_set_event = PTRACE_VFORK_DONE;
6296	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6297
6298	printf("Before resuming the child process where it left off and "
6299	    "without signal to be sent\n");
6300	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6301
6302	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6303	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6304
6305	validate_status_stopped(status, sigmasked);
6306
6307	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6308	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
6309
6310	child2 = state.pe_other_pid;
6311	printf("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
6312
6313	printf("Before resuming the child process where it left off and "
6314	    "without signal to be sent\n");
6315	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6316
6317	printf("Before calling %s() for the child - expected stopped "
6318	    "SIGCHLD\n", TWAIT_FNAME);
6319	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6320
6321	validate_status_stopped(status, SIGCHLD);
6322
6323	printf("Before resuming the child process where it left off and "
6324	    "without signal to be sent\n");
6325	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6326
6327	printf("Before calling %s() for the child - expected exited\n",
6328	    TWAIT_FNAME);
6329	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6330
6331	validate_status_exited(status, exitval);
6332
6333	printf("Before calling %s() for the child - expected no process\n",
6334	    TWAIT_FNAME);
6335	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6336}
6337
6338ATF_TC(signal9);
6339ATF_TC_HEAD(signal9, tc)
6340{
6341	atf_tc_set_md_var(tc, "descr",
6342	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6343	    "catching PTRACE_LWP_CREATE breakpoint");
6344}
6345
6346ATF_TC_BODY(signal9, tc)
6347{
6348	const int exitval = 5;
6349	const int sigval = SIGSTOP;
6350	const int sigmasked = SIGTRAP;
6351	pid_t child, wpid;
6352#if defined(TWAIT_HAVE_STATUS)
6353	int status;
6354#endif
6355	sigset_t intmask;
6356	ptrace_state_t state;
6357	const int slen = sizeof(state);
6358	ptrace_event_t event;
6359	const int elen = sizeof(event);
6360	ucontext_t uc;
6361	lwpid_t lid;
6362	static const size_t ssize = 16*1024;
6363	void *stack;
6364
6365	atf_tc_expect_fail("PR kern/51918");
6366
6367	printf("Before forking process PID=%d\n", getpid());
6368	ATF_REQUIRE((child = fork()) != -1);
6369	if (child == 0) {
6370		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6371		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6372
6373		sigemptyset(&intmask);
6374		sigaddset(&intmask, sigmasked);
6375		sigprocmask(SIG_BLOCK, &intmask, NULL);
6376
6377		printf("Before raising %s from child\n", strsignal(sigval));
6378		FORKEE_ASSERT(raise(sigval) == 0);
6379
6380		printf("Before allocating memory for stack in child\n");
6381		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6382
6383		printf("Before making context for new lwp in child\n");
6384		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6385
6386		printf("Before creating new in child\n");
6387		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6388
6389		printf("Before waiting for lwp %d to exit\n", lid);
6390		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6391
6392		printf("Before verifying that reported %d and running lid %d "
6393		    "are the same\n", lid, the_lwp_id);
6394		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6395
6396		printf("Before exiting of the child process\n");
6397		_exit(exitval);
6398	}
6399	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6400
6401	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6402	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6403
6404	validate_status_stopped(status, sigval);
6405
6406	printf("Set empty EVENT_MASK for the child %d\n", child);
6407	event.pe_set_event = PTRACE_LWP_CREATE;
6408	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6409
6410	printf("Before resuming the child process where it left off and "
6411	    "without signal to be sent\n");
6412	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6413
6414	printf("Before calling %s() for the child - expected stopped "
6415	    "SIGTRAP\n", TWAIT_FNAME);
6416	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6417
6418	validate_status_stopped(status, sigmasked);
6419
6420	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6421
6422	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
6423
6424	lid = state.pe_lwp;
6425	printf("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
6426
6427	printf("Before resuming the child process where it left off and "
6428	    "without signal to be sent\n");
6429	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6430
6431	printf("Before calling %s() for the child - expected exited\n",
6432	    TWAIT_FNAME);
6433	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6434
6435	validate_status_exited(status, exitval);
6436
6437	printf("Before calling %s() for the child - expected no process\n",
6438	    TWAIT_FNAME);
6439	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6440}
6441
6442ATF_TC(signal10);
6443ATF_TC_HEAD(signal10, tc)
6444{
6445	atf_tc_set_md_var(tc, "descr",
6446	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6447	    "catching PTRACE_LWP_EXIT breakpoint");
6448}
6449
6450ATF_TC_BODY(signal10, tc)
6451{
6452	const int exitval = 5;
6453	const int sigval = SIGSTOP;
6454	const int sigmasked = SIGTRAP;
6455	pid_t child, wpid;
6456#if defined(TWAIT_HAVE_STATUS)
6457	int status;
6458#endif
6459	sigset_t intmask;
6460	ptrace_state_t state;
6461	const int slen = sizeof(state);
6462	ptrace_event_t event;
6463	const int elen = sizeof(event);
6464	ucontext_t uc;
6465	lwpid_t lid;
6466	static const size_t ssize = 16*1024;
6467	void *stack;
6468
6469	atf_tc_expect_fail("PR kern/51918");
6470
6471	printf("Before forking process PID=%d\n", getpid());
6472	ATF_REQUIRE((child = fork()) != -1);
6473	if (child == 0) {
6474		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6475		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6476
6477		sigemptyset(&intmask);
6478		sigaddset(&intmask, sigmasked);
6479		sigprocmask(SIG_BLOCK, &intmask, NULL);
6480
6481		printf("Before raising %s from child\n", strsignal(sigval));
6482		FORKEE_ASSERT(raise(sigval) == 0);
6483
6484		printf("Before allocating memory for stack in child\n");
6485		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6486
6487		printf("Before making context for new lwp in child\n");
6488		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6489
6490		printf("Before creating new in child\n");
6491		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6492
6493		printf("Before waiting for lwp %d to exit\n", lid);
6494		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6495
6496		printf("Before verifying that reported %d and running lid %d "
6497		    "are the same\n", lid, the_lwp_id);
6498		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6499
6500		printf("Before exiting of the child process\n");
6501		_exit(exitval);
6502	}
6503	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6504
6505	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6506	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6507
6508	validate_status_stopped(status, sigval);
6509
6510	printf("Set empty EVENT_MASK for the child %d\n", child);
6511	event.pe_set_event = PTRACE_LWP_EXIT;
6512	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6513
6514	printf("Before resuming the child process where it left off and "
6515	    "without signal to be sent\n");
6516	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6517
6518	printf("Before calling %s() for the child - expected stopped "
6519	    "SIGTRAP\n", TWAIT_FNAME);
6520	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6521
6522	validate_status_stopped(status, sigmasked);
6523
6524	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6525
6526	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
6527
6528	lid = state.pe_lwp;
6529	printf("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
6530
6531	printf("Before resuming the child process where it left off and "
6532	    "without signal to be sent\n");
6533	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6534
6535	printf("Before calling %s() for the child - expected exited\n",
6536	    TWAIT_FNAME);
6537	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6538
6539	validate_status_exited(status, exitval);
6540
6541	printf("Before calling %s() for the child - expected no process\n",
6542	    TWAIT_FNAME);
6543	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6544}
6545
6546ATF_TC(getsigmask1);
6547ATF_TC_HEAD(getsigmask1, tc)
6548{
6549	atf_tc_set_md_var(tc, "descr",
6550	    "Verify that plain PT_SET_SIGMASK can be called");
6551}
6552
6553ATF_TC_BODY(getsigmask1, tc)
6554{
6555	const int exitval = 5;
6556	const int sigval = SIGSTOP;
6557	pid_t child, wpid;
6558#if defined(TWAIT_HAVE_STATUS)
6559	int status;
6560#endif
6561	sigset_t mask;
6562
6563	printf("Before forking process PID=%d\n", getpid());
6564	ATF_REQUIRE((child = fork()) != -1);
6565	if (child == 0) {
6566		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6567		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6568
6569		printf("Before raising %s from child\n", strsignal(sigval));
6570		FORKEE_ASSERT(raise(sigval) == 0);
6571
6572		printf("Before exiting of the child process\n");
6573		_exit(exitval);
6574	}
6575	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6576
6577	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6578	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6579
6580	validate_status_stopped(status, sigval);
6581
6582	printf("Before calling PT_GET_SIGMASK\n");
6583	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6584
6585	printf("Before resuming the child process where it left off and "
6586	    "without signal to be sent\n");
6587	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6588
6589	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6590	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6591
6592	validate_status_exited(status, exitval);
6593
6594	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6595	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6596}
6597
6598ATF_TC(getsigmask2);
6599ATF_TC_HEAD(getsigmask2, tc)
6600{
6601	atf_tc_set_md_var(tc, "descr",
6602	    "Verify that PT_SET_SIGMASK reports correct mask from tracee");
6603}
6604
6605ATF_TC_BODY(getsigmask2, tc)
6606{
6607	const int exitval = 5;
6608	const int sigval = SIGSTOP;
6609	const int sigmasked = SIGTRAP;
6610	pid_t child, wpid;
6611#if defined(TWAIT_HAVE_STATUS)
6612	int status;
6613#endif
6614	sigset_t mask;
6615	sigset_t expected_mask;
6616	ATF_REQUIRE(sigemptyset(&mask) == 0);
6617	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6618	ATF_REQUIRE(sigaddset(&expected_mask, sigmasked) == 0);
6619
6620	printf("Before forking process PID=%d\n", getpid());
6621	ATF_REQUIRE((child = fork()) != -1);
6622	if (child == 0) {
6623		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6624		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6625
6626		sigaddset(&mask, sigmasked);
6627		sigprocmask(SIG_BLOCK, &mask, NULL);
6628
6629		printf("Before raising %s from child\n", strsignal(sigval));
6630		FORKEE_ASSERT(raise(sigval) == 0);
6631
6632		printf("Before exiting of the child process\n");
6633		_exit(exitval);
6634	}
6635	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6636
6637	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6638	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6639
6640	validate_status_stopped(status, sigval);
6641
6642	printf("Before calling PT_GET_SIGMASK\n");
6643	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6644
6645	ATF_REQUIRE(memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6646
6647	printf("Before resuming the child process where it left off and "
6648	    "without signal to be sent\n");
6649	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6650
6651	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6652	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6653
6654	validate_status_exited(status, exitval);
6655
6656	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6657	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6658}
6659
6660ATF_TC(setsigmask1);
6661ATF_TC_HEAD(setsigmask1, tc)
6662{
6663	atf_tc_set_md_var(tc, "descr",
6664	    "Verify that plain PT_SET_SIGMASK can be called with empty mask");
6665}
6666
6667ATF_TC_BODY(setsigmask1, tc)
6668{
6669	const int exitval = 5;
6670	const int sigval = SIGSTOP;
6671	pid_t child, wpid;
6672#if defined(TWAIT_HAVE_STATUS)
6673	int status;
6674#endif
6675	sigset_t mask;
6676	ATF_REQUIRE(sigemptyset(&mask) == 0);
6677
6678	printf("Before forking process PID=%d\n", getpid());
6679	ATF_REQUIRE((child = fork()) != -1);
6680	if (child == 0) {
6681		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6682		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6683
6684		printf("Before raising %s from child\n", strsignal(sigval));
6685		FORKEE_ASSERT(raise(sigval) == 0);
6686
6687		printf("Before exiting of the child process\n");
6688		_exit(exitval);
6689	}
6690	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6691
6692	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6693	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6694
6695	validate_status_stopped(status, sigval);
6696
6697	printf("Before calling PT_SET_SIGMASK for empty mask\n");
6698	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6699
6700	printf("Before resuming the child process where it left off and "
6701	    "without signal to be sent\n");
6702	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6703
6704	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6705	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6706
6707	validate_status_exited(status, exitval);
6708
6709	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6710	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6711}
6712
6713ATF_TC(setsigmask2);
6714ATF_TC_HEAD(setsigmask2, tc)
6715{
6716	atf_tc_set_md_var(tc, "descr",
6717	    "Verify that sigmask is preserved between PT_GET_SIGMASK and "
6718	    "PT_SET_SIGMASK");
6719}
6720
6721ATF_TC_BODY(setsigmask2, tc)
6722{
6723	const int exitval = 5;
6724	const int sigval = SIGSTOP;
6725	pid_t child, wpid;
6726#if defined(TWAIT_HAVE_STATUS)
6727	int status;
6728#endif
6729	sigset_t new_mask;
6730	sigset_t mask;
6731	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6732	ATF_REQUIRE(sigemptyset(&mask) == 0);
6733	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6734
6735	printf("Before forking process PID=%d\n", getpid());
6736	ATF_REQUIRE((child = fork()) != -1);
6737	if (child == 0) {
6738		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6739		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6740
6741		printf("Before raising %s from child\n", strsignal(sigval));
6742		FORKEE_ASSERT(raise(sigval) == 0);
6743
6744		printf("Before exiting of the child process\n");
6745		_exit(exitval);
6746	}
6747	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6748
6749	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6750	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6751
6752	validate_status_stopped(status, sigval);
6753
6754	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6755	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6756
6757	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6758	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6759
6760	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6761
6762	printf("Before resuming the child process where it left off and "
6763	    "without signal to be sent\n");
6764	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6765
6766	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6767	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6768
6769	validate_status_exited(status, exitval);
6770
6771	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6772	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6773}
6774
6775ATF_TC(setsigmask3);
6776ATF_TC_HEAD(setsigmask3, tc)
6777{
6778	atf_tc_set_md_var(tc, "descr",
6779	    "Verify that sigmask is preserved between PT_GET_SIGMASK, process "
6780	    "resumed and PT_SET_SIGMASK");
6781}
6782
6783ATF_TC_BODY(setsigmask3, tc)
6784{
6785	const int exitval = 5;
6786	const int sigval = SIGSTOP;
6787	pid_t child, wpid;
6788#if defined(TWAIT_HAVE_STATUS)
6789	int status;
6790#endif
6791	sigset_t new_mask;
6792	sigset_t mask;
6793	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6794	ATF_REQUIRE(sigemptyset(&mask) == 0);
6795	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6796
6797	printf("Before forking process PID=%d\n", getpid());
6798	ATF_REQUIRE((child = fork()) != -1);
6799	if (child == 0) {
6800		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6801		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6802
6803		printf("Before raising %s from child\n", strsignal(sigval));
6804		FORKEE_ASSERT(raise(sigval) == 0);
6805
6806		printf("Before raising %s from child\n", strsignal(sigval));
6807		FORKEE_ASSERT(raise(sigval) == 0);
6808
6809		printf("Before exiting of the child process\n");
6810		_exit(exitval);
6811	}
6812	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6813
6814	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6815	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6816
6817	validate_status_stopped(status, sigval);
6818
6819	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6820	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6821
6822	printf("Before resuming the child process where it left off and "
6823	    "without signal to be sent\n");
6824	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6825
6826	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6827	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6828
6829	validate_status_stopped(status, sigval);
6830
6831	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6832	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6833
6834	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6835
6836	printf("Before resuming the child process where it left off and "
6837	    "without signal to be sent\n");
6838	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6839
6840	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6841	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6842
6843	validate_status_exited(status, exitval);
6844
6845	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6846	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6847}
6848
6849ATF_TC(setsigmask4);
6850ATF_TC_HEAD(setsigmask4, tc)
6851{
6852	atf_tc_set_md_var(tc, "descr",
6853	    "Verify that new sigmask is visible in tracee");
6854}
6855
6856ATF_TC_BODY(setsigmask4, tc)
6857{
6858	const int exitval = 5;
6859	const int sigval = SIGSTOP;
6860	pid_t child, wpid;
6861#if defined(TWAIT_HAVE_STATUS)
6862	int status;
6863#endif
6864	sigset_t mask;
6865	sigset_t expected_mask;
6866	ATF_REQUIRE(sigemptyset(&mask) == 0);
6867	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6868	ATF_REQUIRE(sigaddset(&expected_mask, SIGINT) == 0);
6869
6870	printf("Before forking process PID=%d\n", getpid());
6871	ATF_REQUIRE((child = fork()) != -1);
6872	if (child == 0) {
6873		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6874		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6875
6876		printf("Before raising %s from child\n", strsignal(sigval));
6877		FORKEE_ASSERT(raise(sigval) == 0);
6878
6879		sigprocmask(0, NULL, &mask);
6880
6881		FORKEE_ASSERT
6882		    (memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6883
6884		printf("Before exiting of the child process\n");
6885		_exit(exitval);
6886	}
6887	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6888
6889	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6890	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6891
6892	validate_status_stopped(status, sigval);
6893
6894	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6895	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &expected_mask, 0) != -1);
6896
6897	printf("Before resuming the child process where it left off and "
6898	    "without signal to be sent\n");
6899	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6900
6901	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6902	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6903
6904	validate_status_exited(status, exitval);
6905
6906	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6907	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6908}
6909
6910ATF_TC(setsigmask5);
6911ATF_TC_HEAD(setsigmask5, tc)
6912{
6913	atf_tc_set_md_var(tc, "descr",
6914	    "Verify that sigmask cannot be set to SIGKILL");
6915}
6916
6917ATF_TC_BODY(setsigmask5, tc)
6918{
6919	const int exitval = 5;
6920	const int sigval = SIGSTOP;
6921	pid_t child, wpid;
6922#if defined(TWAIT_HAVE_STATUS)
6923	int status;
6924#endif
6925	sigset_t new_mask;
6926	sigset_t mask;
6927	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6928	ATF_REQUIRE(sigemptyset(&mask) == 0);
6929	ATF_REQUIRE(sigaddset(&mask, SIGKILL) == 0);
6930
6931	printf("Before forking process PID=%d\n", getpid());
6932	ATF_REQUIRE((child = fork()) != -1);
6933	if (child == 0) {
6934		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6935		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6936
6937		printf("Before raising %s from child\n", strsignal(sigval));
6938		FORKEE_ASSERT(raise(sigval) == 0);
6939
6940		printf("Before exiting of the child process\n");
6941		_exit(exitval);
6942	}
6943	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6944
6945	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6946	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6947
6948	validate_status_stopped(status, sigval);
6949
6950	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6951	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6952
6953	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6954	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6955
6956	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
6957
6958	printf("Before resuming the child process where it left off and "
6959	    "without signal to be sent\n");
6960	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6961
6962	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6963	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6964
6965	validate_status_exited(status, exitval);
6966
6967	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6968	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6969}
6970
6971ATF_TC(setsigmask6);
6972ATF_TC_HEAD(setsigmask6, tc)
6973{
6974	atf_tc_set_md_var(tc, "descr",
6975	    "Verify that sigmask cannot be set to SIGSTOP");
6976}
6977
6978ATF_TC_BODY(setsigmask6, tc)
6979{
6980	const int exitval = 5;
6981	const int sigval = SIGSTOP;
6982	pid_t child, wpid;
6983#if defined(TWAIT_HAVE_STATUS)
6984	int status;
6985#endif
6986	sigset_t new_mask;
6987	sigset_t mask;
6988	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6989	ATF_REQUIRE(sigemptyset(&mask) == 0);
6990	ATF_REQUIRE(sigaddset(&mask, SIGSTOP) == 0);
6991
6992	printf("Before forking process PID=%d\n", getpid());
6993	ATF_REQUIRE((child = fork()) != -1);
6994	if (child == 0) {
6995		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6996		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6997
6998		printf("Before raising %s from child\n", strsignal(sigval));
6999		FORKEE_ASSERT(raise(sigval) == 0);
7000
7001		printf("Before exiting of the child process\n");
7002		_exit(exitval);
7003	}
7004	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7005
7006	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7007	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7008
7009	validate_status_stopped(status, sigval);
7010
7011	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
7012	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
7013
7014	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
7015	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
7016
7017	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
7018
7019	printf("Before resuming the child process where it left off and "
7020	    "without signal to be sent\n");
7021	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7022
7023	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7024	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7025
7026	validate_status_exited(status, exitval);
7027
7028	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7029	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7030}
7031
7032static void
7033lwp_main_stop(void *arg)
7034{
7035	the_lwp_id = _lwp_self();
7036
7037	raise(SIGTRAP);
7038
7039	_lwp_exit();
7040}
7041
7042ATF_TC(suspend1);
7043ATF_TC_HEAD(suspend1, tc)
7044{
7045	atf_tc_set_md_var(tc, "descr",
7046	    "Verify that a thread can be suspended by a debugger and later "
7047	    "resumed by a tracee");
7048}
7049
7050ATF_TC_BODY(suspend1, tc)
7051{
7052	const int exitval = 5;
7053	const int sigval = SIGSTOP;
7054	pid_t child, wpid;
7055#if defined(TWAIT_HAVE_STATUS)
7056	int status;
7057#endif
7058	ucontext_t uc;
7059	lwpid_t lid;
7060	static const size_t ssize = 16*1024;
7061	void *stack;
7062	struct ptrace_lwpinfo pl;
7063	struct ptrace_siginfo psi;
7064	volatile int go = 0;
7065
7066	printf("Before forking process PID=%d\n", getpid());
7067	ATF_REQUIRE((child = fork()) != -1);
7068	if (child == 0) {
7069		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7070		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7071
7072		printf("Before raising %s from child\n", strsignal(sigval));
7073		FORKEE_ASSERT(raise(sigval) == 0);
7074
7075		printf("Before allocating memory for stack in child\n");
7076		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7077
7078		printf("Before making context for new lwp in child\n");
7079		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7080
7081		printf("Before creating new in child\n");
7082		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7083
7084		while (go == 0)
7085			continue;
7086
7087		raise(SIGINT);
7088
7089		FORKEE_ASSERT(_lwp_continue(lid) == 0);
7090
7091		printf("Before waiting for lwp %d to exit\n", lid);
7092		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7093
7094		printf("Before verifying that reported %d and running lid %d "
7095		    "are the same\n", lid, the_lwp_id);
7096		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7097
7098		printf("Before exiting of the child process\n");
7099		_exit(exitval);
7100	}
7101	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7102
7103	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7104	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7105
7106	validate_status_stopped(status, sigval);
7107
7108	printf("Before resuming the child process where it left off and "
7109	    "without signal to be sent\n");
7110	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7111
7112	printf("Before calling %s() for the child - expected stopped "
7113	    "SIGTRAP\n", TWAIT_FNAME);
7114	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7115
7116	validate_status_stopped(status, SIGTRAP);
7117
7118	printf("Before reading siginfo and lwpid_t\n");
7119	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7120
7121	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7122	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7123
7124        printf("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
7125	    child, getpid());
7126	ATF_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
7127
7128	printf("Before resuming the child process where it left off and "
7129	    "without signal to be sent\n");
7130	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7131
7132	printf("Before calling %s() for the child - expected stopped "
7133	    "SIGINT\n", TWAIT_FNAME);
7134	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7135
7136	validate_status_stopped(status, SIGINT);
7137
7138	pl.pl_lwpid = 0;
7139
7140	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7141	while (pl.pl_lwpid != 0) {
7142
7143		ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7144		switch (pl.pl_lwpid) {
7145		case 1:
7146			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7147			break;
7148		case 2:
7149			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7150			break;
7151		}
7152	}
7153
7154	printf("Before resuming the child process where it left off and "
7155	    "without signal to be sent\n");
7156	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7157
7158	printf("Before calling %s() for the child - expected exited\n",
7159	    TWAIT_FNAME);
7160	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7161
7162	validate_status_exited(status, exitval);
7163
7164	printf("Before calling %s() for the child - expected no process\n",
7165	    TWAIT_FNAME);
7166	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7167}
7168
7169ATF_TC(suspend2);
7170ATF_TC_HEAD(suspend2, tc)
7171{
7172	atf_tc_set_md_var(tc, "descr",
7173	    "Verify that the while the only thread within a process is "
7174	    "suspended, the whole process cannot be unstopped");
7175}
7176
7177ATF_TC_BODY(suspend2, tc)
7178{
7179	const int exitval = 5;
7180	const int sigval = SIGSTOP;
7181	pid_t child, wpid;
7182#if defined(TWAIT_HAVE_STATUS)
7183	int status;
7184#endif
7185	struct ptrace_siginfo psi;
7186
7187	printf("Before forking process PID=%d\n", getpid());
7188	ATF_REQUIRE((child = fork()) != -1);
7189	if (child == 0) {
7190		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7191		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7192
7193		printf("Before raising %s from child\n", strsignal(sigval));
7194		FORKEE_ASSERT(raise(sigval) == 0);
7195
7196		printf("Before exiting of the child process\n");
7197		_exit(exitval);
7198	}
7199	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7200
7201	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7202	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7203
7204	validate_status_stopped(status, sigval);
7205
7206	printf("Before reading siginfo and lwpid_t\n");
7207	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7208
7209	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7210	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7211
7212	printf("Before resuming the child process where it left off and "
7213	    "without signal to be sent\n");
7214	ATF_REQUIRE_ERRNO(EDEADLK,
7215	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
7216
7217	printf("Before resuming LWP %d\n", psi.psi_lwpid);
7218	ATF_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7219
7220	printf("Before resuming the child process where it left off and "
7221	    "without signal to be sent\n");
7222	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7223
7224	printf("Before calling %s() for the child - expected exited\n",
7225	    TWAIT_FNAME);
7226	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7227
7228	validate_status_exited(status, exitval);
7229
7230	printf("Before calling %s() for the child - expected no process\n",
7231	    TWAIT_FNAME);
7232	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7233}
7234
7235ATF_TC(resume1);
7236ATF_TC_HEAD(resume1, tc)
7237{
7238	atf_tc_set_md_var(tc, "timeout", "5");
7239	atf_tc_set_md_var(tc, "descr",
7240	    "Verify that a thread can be suspended by a debugger and later "
7241	    "resumed by the debugger");
7242}
7243
7244ATF_TC_BODY(resume1, tc)
7245{
7246	struct msg_fds fds;
7247	const int exitval = 5;
7248	const int sigval = SIGSTOP;
7249	pid_t child, wpid;
7250	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
7251#if defined(TWAIT_HAVE_STATUS)
7252	int status;
7253#endif
7254	ucontext_t uc;
7255	lwpid_t lid;
7256	static const size_t ssize = 16*1024;
7257	void *stack;
7258	struct ptrace_lwpinfo pl;
7259	struct ptrace_siginfo psi;
7260
7261	atf_tc_expect_timeout("PR kern/51995");
7262
7263	ATF_REQUIRE(msg_open(&fds) == 0);
7264
7265	printf("Before forking process PID=%d\n", getpid());
7266	ATF_REQUIRE((child = fork()) != -1);
7267	if (child == 0) {
7268		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7269		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7270
7271		printf("Before raising %s from child\n", strsignal(sigval));
7272		FORKEE_ASSERT(raise(sigval) == 0);
7273
7274		printf("Before allocating memory for stack in child\n");
7275		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7276
7277		printf("Before making context for new lwp in child\n");
7278		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7279
7280		printf("Before creating new in child\n");
7281		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7282
7283		CHILD_TO_PARENT("Message", fds, msg);
7284
7285		raise(SIGINT);
7286
7287		printf("Before waiting for lwp %d to exit\n", lid);
7288		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7289
7290		printf("Before verifying that reported %d and running lid %d "
7291		    "are the same\n", lid, the_lwp_id);
7292		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7293
7294		printf("Before exiting of the child process\n");
7295		_exit(exitval);
7296	}
7297	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7298
7299	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7300	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7301
7302	validate_status_stopped(status, sigval);
7303
7304	printf("Before resuming the child process where it left off and "
7305	    "without signal to be sent\n");
7306	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7307
7308	printf("Before calling %s() for the child - expected stopped "
7309	    "SIGTRAP\n", TWAIT_FNAME);
7310	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7311
7312	validate_status_stopped(status, SIGTRAP);
7313
7314	printf("Before reading siginfo and lwpid_t\n");
7315	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7316
7317	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7318	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7319
7320	PARENT_FROM_CHILD("Message", fds, msg);
7321
7322	printf("Before resuming the child process where it left off and "
7323	    "without signal to be sent\n");
7324	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7325
7326	printf("Before calling %s() for the child - expected stopped "
7327	    "SIGINT\n", TWAIT_FNAME);
7328	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7329
7330	validate_status_stopped(status, SIGINT);
7331
7332	pl.pl_lwpid = 0;
7333
7334	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7335	while (pl.pl_lwpid != 0) {
7336		ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7337		switch (pl.pl_lwpid) {
7338		case 1:
7339			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7340			break;
7341		case 2:
7342			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7343			break;
7344		}
7345	}
7346
7347	printf("Before resuming LWP %d\n", psi.psi_lwpid);
7348	ATF_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7349
7350	printf("Before resuming the child process where it left off and "
7351	    "without signal to be sent\n");
7352	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7353
7354	printf("Before calling %s() for the child - expected exited\n",
7355	    TWAIT_FNAME);
7356	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7357
7358	validate_status_exited(status, exitval);
7359
7360	printf("Before calling %s() for the child - expected no process\n",
7361	    TWAIT_FNAME);
7362	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7363
7364	msg_close(&fds);
7365
7366	printf("XXX: Test worked this time but for consistency timeout it\n");
7367	sleep(10);
7368}
7369
7370ATF_TC(syscall1);
7371ATF_TC_HEAD(syscall1, tc)
7372{
7373	atf_tc_set_md_var(tc, "descr",
7374	    "Verify that getpid(2) can be traced with PT_SYSCALL");
7375}
7376
7377ATF_TC_BODY(syscall1, tc)
7378{
7379	const int exitval = 5;
7380	const int sigval = SIGSTOP;
7381	pid_t child, wpid;
7382#if defined(TWAIT_HAVE_STATUS)
7383	int status;
7384#endif
7385	struct ptrace_siginfo info;
7386	memset(&info, 0, sizeof(info));
7387
7388	printf("Before forking process PID=%d\n", getpid());
7389	ATF_REQUIRE((child = fork()) != -1);
7390	if (child == 0) {
7391		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7392		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7393
7394		printf("Before raising %s from child\n", strsignal(sigval));
7395		FORKEE_ASSERT(raise(sigval) == 0);
7396
7397		syscall(SYS_getpid);
7398
7399		printf("Before exiting of the child process\n");
7400		_exit(exitval);
7401	}
7402	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7403
7404	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7405	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7406
7407	validate_status_stopped(status, sigval);
7408
7409	printf("Before resuming the child process where it left off and "
7410	    "without signal to be sent\n");
7411	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7412
7413	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7414	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7415
7416	validate_status_stopped(status, SIGTRAP);
7417
7418	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7419	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7420
7421	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7422	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
7423
7424	printf("Before resuming the child process where it left off and "
7425	    "without signal to be sent\n");
7426	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7427
7428	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7429	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7430
7431	validate_status_stopped(status, SIGTRAP);
7432
7433	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7434	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7435
7436	printf("Before checking siginfo_t\n");
7437	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7438	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
7439
7440	printf("Before resuming the child process where it left off and "
7441	    "without signal to be sent\n");
7442	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7443
7444	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7445	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7446
7447	validate_status_exited(status, exitval);
7448
7449	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7450	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7451}
7452
7453ATF_TC(syscallemu1);
7454ATF_TC_HEAD(syscallemu1, tc)
7455{
7456	atf_tc_set_md_var(tc, "descr",
7457	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
7458}
7459
7460ATF_TC_BODY(syscallemu1, tc)
7461{
7462	const int exitval = 5;
7463	const int sigval = SIGSTOP;
7464	pid_t child, wpid;
7465#if defined(TWAIT_HAVE_STATUS)
7466	int status;
7467#endif
7468
7469#if defined(__sparc__) && !defined(__sparc64__)
7470	/* syscallemu does not work on sparc (32-bit) */
7471	atf_tc_expect_fail("PR kern/52166");
7472#endif
7473
7474	printf("Before forking process PID=%d\n", getpid());
7475	ATF_REQUIRE((child = fork()) != -1);
7476	if (child == 0) {
7477		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7478		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7479
7480		printf("Before raising %s from child\n", strsignal(sigval));
7481		FORKEE_ASSERT(raise(sigval) == 0);
7482
7483		syscall(SYS_exit, 100);
7484
7485		printf("Before exiting of the child process\n");
7486		_exit(exitval);
7487	}
7488	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7489
7490	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7491	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7492
7493	validate_status_stopped(status, sigval);
7494
7495	printf("Before resuming the child process where it left off and "
7496	    "without signal to be sent\n");
7497	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7498
7499	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7500	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7501
7502	validate_status_stopped(status, SIGTRAP);
7503
7504	printf("Set SYSCALLEMU for intercepted syscall\n");
7505	ATF_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
7506
7507	printf("Before resuming the child process where it left off and "
7508	    "without signal to be sent\n");
7509	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7510
7511	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7512	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7513
7514	validate_status_stopped(status, SIGTRAP);
7515
7516	printf("Before resuming the child process where it left off and "
7517	    "without signal to be sent\n");
7518	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7519
7520	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7521	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7522
7523	validate_status_exited(status, exitval);
7524
7525	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7526	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7527}
7528
7529#include "t_ptrace_amd64_wait.h"
7530#include "t_ptrace_i386_wait.h"
7531#include "t_ptrace_x86_wait.h"
7532
7533ATF_TP_ADD_TCS(tp)
7534{
7535	setvbuf(stdout, NULL, _IONBF, 0);
7536	setvbuf(stderr, NULL, _IONBF, 0);
7537	ATF_TP_ADD_TC(tp, traceme1);
7538	ATF_TP_ADD_TC(tp, traceme2);
7539	ATF_TP_ADD_TC(tp, traceme3);
7540	ATF_TP_ADD_TC(tp, traceme4);
7541
7542	ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
7543	ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
7544	ATF_TP_ADD_TC(tp, attach3);
7545	ATF_TP_ADD_TC(tp, attach4);
7546	ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
7547	ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
7548	ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
7549
7550	ATF_TP_ADD_TC(tp, eventmask1);
7551	ATF_TP_ADD_TC(tp, eventmask2);
7552	ATF_TP_ADD_TC(tp, eventmask3);
7553	ATF_TP_ADD_TC(tp, eventmask4);
7554	ATF_TP_ADD_TC(tp, eventmask5);
7555	ATF_TP_ADD_TC(tp, eventmask6);
7556
7557	ATF_TP_ADD_TC_HAVE_PID(tp, fork1);
7558	ATF_TP_ADD_TC(tp, fork2);
7559
7560	ATF_TP_ADD_TC_HAVE_PID(tp, vfork1);
7561	ATF_TP_ADD_TC(tp, vfork2);
7562
7563	ATF_TP_ADD_TC(tp, vforkdone1);
7564	ATF_TP_ADD_TC(tp, vforkdone2);
7565
7566	ATF_TP_ADD_TC(tp, io_read_d1);
7567	ATF_TP_ADD_TC(tp, io_read_d2);
7568	ATF_TP_ADD_TC(tp, io_read_d3);
7569	ATF_TP_ADD_TC(tp, io_read_d4);
7570
7571	ATF_TP_ADD_TC(tp, io_write_d1);
7572	ATF_TP_ADD_TC(tp, io_write_d2);
7573	ATF_TP_ADD_TC(tp, io_write_d3);
7574	ATF_TP_ADD_TC(tp, io_write_d4);
7575
7576	ATF_TP_ADD_TC(tp, read_d1);
7577	ATF_TP_ADD_TC(tp, read_d2);
7578	ATF_TP_ADD_TC(tp, read_d3);
7579	ATF_TP_ADD_TC(tp, read_d4);
7580
7581	ATF_TP_ADD_TC(tp, write_d1);
7582	ATF_TP_ADD_TC(tp, write_d2);
7583	ATF_TP_ADD_TC(tp, write_d3);
7584	ATF_TP_ADD_TC(tp, write_d4);
7585
7586	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1);
7587	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2);
7588
7589	ATF_TP_ADD_TC(tp, read_d_write_d_handshake1);
7590	ATF_TP_ADD_TC(tp, read_d_write_d_handshake2);
7591
7592	ATF_TP_ADD_TC(tp, io_read_i1);
7593	ATF_TP_ADD_TC(tp, io_read_i2);
7594	ATF_TP_ADD_TC(tp, io_read_i3);
7595	ATF_TP_ADD_TC(tp, io_read_i4);
7596
7597	ATF_TP_ADD_TC(tp, read_i1);
7598	ATF_TP_ADD_TC(tp, read_i2);
7599	ATF_TP_ADD_TC(tp, read_i3);
7600	ATF_TP_ADD_TC(tp, read_i4);
7601
7602	ATF_TP_ADD_TC(tp, io_read_auxv1);
7603
7604	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
7605	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
7606	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
7607	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
7608	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
7609
7610	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
7611	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
7612
7613	ATF_TP_ADD_TC_PT_STEP(tp, step1);
7614	ATF_TP_ADD_TC_PT_STEP(tp, step2);
7615	ATF_TP_ADD_TC_PT_STEP(tp, step3);
7616	ATF_TP_ADD_TC_PT_STEP(tp, step4);
7617
7618	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
7619	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
7620	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
7621	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
7622
7623	ATF_TP_ADD_TC(tp, kill1);
7624	ATF_TP_ADD_TC(tp, kill2);
7625
7626	ATF_TP_ADD_TC(tp, lwpinfo1);
7627	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
7628
7629	ATF_TP_ADD_TC(tp, siginfo1);
7630	ATF_TP_ADD_TC(tp, siginfo2);
7631	ATF_TP_ADD_TC(tp, siginfo3);
7632	ATF_TP_ADD_TC(tp, siginfo4);
7633	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
7634	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
7635
7636	ATF_TP_ADD_TC(tp, lwp_create1);
7637
7638	ATF_TP_ADD_TC(tp, lwp_exit1);
7639
7640	ATF_TP_ADD_TC(tp, signal1);
7641	ATF_TP_ADD_TC(tp, signal2);
7642	ATF_TP_ADD_TC(tp, signal3);
7643	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
7644	ATF_TP_ADD_TC(tp, signal5);
7645	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
7646	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
7647	ATF_TP_ADD_TC(tp, signal8);
7648	ATF_TP_ADD_TC(tp, signal9);
7649	ATF_TP_ADD_TC(tp, signal10);
7650
7651	ATF_TP_ADD_TC(tp, suspend1);
7652	ATF_TP_ADD_TC(tp, suspend2);
7653
7654	ATF_TP_ADD_TC(tp, resume1);
7655
7656	ATF_TP_ADD_TC(tp, getsigmask1);
7657	ATF_TP_ADD_TC(tp, getsigmask2);
7658
7659	ATF_TP_ADD_TC(tp, setsigmask1);
7660	ATF_TP_ADD_TC(tp, setsigmask2);
7661	ATF_TP_ADD_TC(tp, setsigmask3);
7662	ATF_TP_ADD_TC(tp, setsigmask4);
7663	ATF_TP_ADD_TC(tp, setsigmask5);
7664	ATF_TP_ADD_TC(tp, setsigmask6);
7665
7666	ATF_TP_ADD_TC(tp, syscall1);
7667
7668	ATF_TP_ADD_TC(tp, syscallemu1);
7669
7670	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
7671	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
7672	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
7673
7674	return atf_no_error();
7675}
7676