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