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