t_ptrace_wait.c revision 1.6
1/*	$NetBSD: t_ptrace_wait.c,v 1.6 2017/04/16 12:32:03 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.6 2017/04/16 12:32:03 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
5775#ifdef PTRACE_BREAKPOINT_ASM
5776		PTRACE_BREAKPOINT_ASM;
5777#else
5778		/* port me */
5779#endif
5780
5781		printf("Before exiting of the child process\n");
5782		_exit(exitval);
5783	}
5784	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5785
5786	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5787	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5788
5789	validate_status_stopped(status, sigval);
5790
5791	printf("Before resuming the child process where it left off and "
5792	    "without signal to be sent\n");
5793	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5794
5795	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5796	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5797
5798	validate_status_stopped(status, sigmasked);
5799
5800	printf("Before resuming the child process where it left off and "
5801	    "without signal to be sent\n");
5802	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5803
5804	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5805	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5806
5807	validate_status_exited(status, exitval);
5808
5809	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5810	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5811}
5812
5813#if defined(PT_STEP)
5814ATF_TC(signal4);
5815ATF_TC_HEAD(signal4, tc)
5816{
5817	atf_tc_set_md_var(tc, "descr",
5818	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5819	    "catching single step trap");
5820}
5821
5822ATF_TC_BODY(signal4, tc)
5823{
5824	const int exitval = 5;
5825	const int sigval = SIGSTOP;
5826	const int sigmasked = SIGTRAP;
5827	pid_t child, wpid;
5828#if defined(TWAIT_HAVE_STATUS)
5829	int status;
5830#endif
5831	sigset_t intmask;
5832	int happy;
5833
5834#if defined(__arm__)
5835	/* PT_STEP not supported on arm 32-bit */
5836	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
5837#else
5838	atf_tc_expect_fail("PR kern/52118");
5839#endif
5840
5841	printf("Before forking process PID=%d\n", getpid());
5842	ATF_REQUIRE((child = fork()) != -1);
5843	if (child == 0) {
5844		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5845		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5846
5847		happy = check_happy(100);
5848
5849		sigemptyset(&intmask);
5850		sigaddset(&intmask, sigmasked);
5851		sigprocmask(SIG_BLOCK, &intmask, NULL);
5852
5853		printf("Before raising %s from child\n", strsignal(sigval));
5854		FORKEE_ASSERT(raise(sigval) == 0);
5855
5856		FORKEE_ASSERT_EQ(happy, check_happy(100));
5857
5858		printf("Before exiting of the child process\n");
5859		_exit(exitval);
5860	}
5861	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5862
5863	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5864	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5865
5866	validate_status_stopped(status, sigval);
5867
5868	printf("Before resuming the child process where it left off and "
5869	    "without signal to be sent\n");
5870	ATF_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5871
5872	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5873	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5874
5875	validate_status_stopped(status, sigmasked);
5876
5877	printf("Before resuming the child process where it left off and "
5878	    "without signal to be sent\n");
5879	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5880
5881	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5882	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5883
5884	validate_status_exited(status, exitval);
5885
5886	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5887	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5888}
5889#endif
5890
5891ATF_TC(signal5);
5892ATF_TC_HEAD(signal5, tc)
5893{
5894	atf_tc_set_md_var(tc, "descr",
5895	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5896	    "catching exec() breakpoint");
5897}
5898
5899ATF_TC_BODY(signal5, tc)
5900{
5901	const int exitval = 5;
5902	const int sigval = SIGSTOP;
5903	const int sigmasked = SIGTRAP;
5904	pid_t child, wpid;
5905#if defined(TWAIT_HAVE_STATUS)
5906	int status;
5907#endif
5908	sigset_t intmask;
5909
5910	atf_tc_expect_fail("PR kern/51918");
5911
5912	printf("Before forking process PID=%d\n", getpid());
5913	ATF_REQUIRE((child = fork()) != -1);
5914	if (child == 0) {
5915		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5916		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5917
5918		sigemptyset(&intmask);
5919		sigaddset(&intmask, sigmasked);
5920		sigprocmask(SIG_BLOCK, &intmask, NULL);
5921
5922		printf("Before raising %s from child\n", strsignal(sigval));
5923		FORKEE_ASSERT(raise(sigval) == 0);
5924
5925		printf("Before calling execve(2) from child\n");
5926		execlp("/bin/echo", "/bin/echo", NULL);
5927
5928		printf("Before exiting of the child process\n");
5929		_exit(exitval);
5930	}
5931	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5932
5933	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5934	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5935
5936	validate_status_stopped(status, sigval);
5937
5938	printf("Before resuming the child process where it left off and "
5939	    "without signal to be sent\n");
5940	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5941
5942	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5943	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5944
5945	validate_status_stopped(status, sigmasked);
5946
5947	printf("Before resuming the child process where it left off and "
5948	    "without signal to be sent\n");
5949	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5950
5951	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5952	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5953
5954	validate_status_exited(status, exitval);
5955
5956	printf("Before calling %s() for the child\n", TWAIT_FNAME);
5957	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5958}
5959
5960#if defined(TWAIT_HAVE_PID)
5961ATF_TC(signal6);
5962ATF_TC_HEAD(signal6, tc)
5963{
5964	atf_tc_set_md_var(tc, "timeout", "5");
5965	atf_tc_set_md_var(tc, "descr",
5966	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5967	    "catching PTRACE_FORK breakpoint");
5968}
5969
5970ATF_TC_BODY(signal6, tc)
5971{
5972	const int exitval = 5;
5973	const int exitval2 = 15;
5974	const int sigval = SIGSTOP;
5975	const int sigmasked = SIGTRAP;
5976	pid_t child, child2, wpid;
5977#if defined(TWAIT_HAVE_STATUS)
5978	int status;
5979#endif
5980	sigset_t intmask;
5981	ptrace_state_t state;
5982	const int slen = sizeof(state);
5983	ptrace_event_t event;
5984	const int elen = sizeof(event);
5985
5986	atf_tc_expect_timeout("PR kern/51918");
5987
5988	printf("Before forking process PID=%d\n", getpid());
5989	ATF_REQUIRE((child = fork()) != -1);
5990	if (child == 0) {
5991		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
5992		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5993
5994		sigemptyset(&intmask);
5995		sigaddset(&intmask, sigmasked);
5996		sigprocmask(SIG_BLOCK, &intmask, NULL);
5997
5998		printf("Before raising %s from child\n", strsignal(sigval));
5999		FORKEE_ASSERT(raise(sigval) == 0);
6000
6001		FORKEE_ASSERT((child2 = fork()) != -1);
6002
6003		if (child2 == 0)
6004			_exit(exitval2);
6005
6006		FORKEE_REQUIRE_SUCCESS
6007			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6008
6009		forkee_status_exited(status, exitval2);
6010
6011		printf("Before exiting of the child process\n");
6012		_exit(exitval);
6013	}
6014	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6015
6016	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6017	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6018
6019	validate_status_stopped(status, sigval);
6020
6021	printf("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
6022	event.pe_set_event = PTRACE_FORK;
6023	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6024
6025	printf("Before resuming the child process where it left off and "
6026	    "without signal to be sent\n");
6027	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6028
6029	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6030	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6031
6032	validate_status_stopped(status, sigmasked);
6033
6034	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6035	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6036
6037	child2 = state.pe_other_pid;
6038	printf("Reported PTRACE_FORK event with forkee %d\n", child2);
6039
6040	printf("Before calling %s() for the child2\n", TWAIT_FNAME);
6041	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6042	    child2);
6043
6044	validate_status_stopped(status, SIGTRAP);
6045
6046	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6047	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6048	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6049
6050	printf("Before resuming the forkee process where it left off and "
6051	    "without signal to be sent\n");
6052	ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6053
6054	printf("Before resuming the child process where it left off and "
6055	    "without signal to be sent\n");
6056	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6057
6058	printf("Before calling %s() for the forkee - expected exited\n",
6059	    TWAIT_FNAME);
6060	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6061	    child2);
6062
6063	validate_status_exited(status, exitval2);
6064
6065	printf("Before calling %s() for the forkee - expected no process\n",
6066	    TWAIT_FNAME);
6067	TWAIT_REQUIRE_FAILURE(ECHILD,
6068	    wpid = TWAIT_GENERIC(child2, &status, 0));
6069
6070	printf("Before calling %s() for the child - expected stopped "
6071	    "SIGCHLD\n", TWAIT_FNAME);
6072	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6073
6074	validate_status_stopped(status, SIGCHLD);
6075
6076	printf("Before resuming the child process where it left off and "
6077	    "without signal to be sent\n");
6078	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6079
6080	printf("Before calling %s() for the child - expected exited\n",
6081	    TWAIT_FNAME);
6082	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6083
6084	validate_status_exited(status, exitval);
6085
6086	printf("Before calling %s() for the child - expected no process\n",
6087	    TWAIT_FNAME);
6088	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6089}
6090#endif
6091
6092#if defined(TWAIT_HAVE_PID)
6093ATF_TC(signal7);
6094ATF_TC_HEAD(signal7, tc)
6095{
6096	atf_tc_set_md_var(tc, "descr",
6097	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6098	    "catching PTRACE_VFORK breakpoint");
6099}
6100
6101ATF_TC_BODY(signal7, tc)
6102{
6103	const int exitval = 5;
6104	const int exitval2 = 15;
6105	const int sigval = SIGSTOP;
6106	const int sigmasked = SIGTRAP;
6107	pid_t child, child2, wpid;
6108#if defined(TWAIT_HAVE_STATUS)
6109	int status;
6110#endif
6111	sigset_t intmask;
6112	ptrace_state_t state;
6113	const int slen = sizeof(state);
6114	ptrace_event_t event;
6115	const int elen = sizeof(event);
6116
6117	atf_tc_expect_fail("PR kern/51918 PR kern/51630");
6118
6119	printf("Before forking process PID=%d\n", getpid());
6120	ATF_REQUIRE((child = fork()) != -1);
6121	if (child == 0) {
6122		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6123		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6124
6125		sigemptyset(&intmask);
6126		sigaddset(&intmask, sigmasked);
6127		sigprocmask(SIG_BLOCK, &intmask, NULL);
6128
6129		printf("Before raising %s from child\n", strsignal(sigval));
6130		FORKEE_ASSERT(raise(sigval) == 0);
6131
6132		FORKEE_ASSERT((child2 = fork()) != -1);
6133
6134		if (child2 == 0)
6135			_exit(exitval2);
6136
6137		FORKEE_REQUIRE_SUCCESS
6138			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6139
6140		forkee_status_exited(status, exitval2);
6141
6142		printf("Before exiting of the child process\n");
6143		_exit(exitval);
6144	}
6145	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6146
6147	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6148	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6149
6150	validate_status_stopped(status, sigval);
6151
6152	printf("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
6153	event.pe_set_event = PTRACE_VFORK;
6154	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6155
6156	printf("Before resuming the child process where it left off and "
6157	    "without signal to be sent\n");
6158	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6159
6160	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6161	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6162
6163	validate_status_stopped(status, sigmasked);
6164
6165	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6166	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6167
6168	child2 = state.pe_other_pid;
6169	printf("Reported PTRACE_VFORK event with forkee %d\n", child2);
6170
6171	printf("Before calling %s() for the child2\n", TWAIT_FNAME);
6172	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6173	    child2);
6174
6175	validate_status_stopped(status, SIGTRAP);
6176
6177	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6178	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6179	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6180
6181	printf("Before resuming the forkee process where it left off and "
6182	    "without signal to be sent\n");
6183	ATF_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
6184
6185	printf("Before resuming the child process where it left off and "
6186	    "without signal to be sent\n");
6187	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6188
6189	printf("Before calling %s() for the forkee - expected exited\n",
6190	    TWAIT_FNAME);
6191	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6192	    child2);
6193
6194	validate_status_exited(status, exitval2);
6195
6196	printf("Before calling %s() for the forkee - expected no process\n",
6197	    TWAIT_FNAME);
6198	TWAIT_REQUIRE_FAILURE(ECHILD,
6199	    wpid = TWAIT_GENERIC(child2, &status, 0));
6200
6201	printf("Before calling %s() for the child - expected stopped "
6202	    "SIGCHLD\n", TWAIT_FNAME);
6203	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6204
6205	validate_status_stopped(status, SIGCHLD);
6206
6207	printf("Before resuming the child process where it left off and "
6208	    "without signal to be sent\n");
6209	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6210
6211	printf("Before calling %s() for the child - expected exited\n",
6212	    TWAIT_FNAME);
6213	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6214
6215	validate_status_exited(status, exitval);
6216
6217	printf("Before calling %s() for the child - expected no process\n",
6218	    TWAIT_FNAME);
6219	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6220}
6221#endif
6222
6223ATF_TC(signal8);
6224ATF_TC_HEAD(signal8, tc)
6225{
6226	atf_tc_set_md_var(tc, "descr",
6227	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6228	    "catching PTRACE_VFORK_DONE breakpoint");
6229}
6230
6231ATF_TC_BODY(signal8, tc)
6232{
6233	const int exitval = 5;
6234	const int exitval2 = 15;
6235	const int sigval = SIGSTOP;
6236	const int sigmasked = SIGTRAP;
6237	pid_t child, child2, wpid;
6238#if defined(TWAIT_HAVE_STATUS)
6239	int status;
6240#endif
6241	sigset_t intmask;
6242	ptrace_state_t state;
6243	const int slen = sizeof(state);
6244	ptrace_event_t event;
6245	const int elen = sizeof(event);
6246
6247	atf_tc_expect_fail("PR kern/51918");
6248
6249	printf("Before forking process PID=%d\n", getpid());
6250	ATF_REQUIRE((child = fork()) != -1);
6251	if (child == 0) {
6252		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6253		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6254
6255		sigemptyset(&intmask);
6256		sigaddset(&intmask, sigmasked);
6257		sigprocmask(SIG_BLOCK, &intmask, NULL);
6258
6259		printf("Before raising %s from child\n", strsignal(sigval));
6260		FORKEE_ASSERT(raise(sigval) == 0);
6261
6262		FORKEE_ASSERT((child2 = vfork()) != -1);
6263
6264		if (child2 == 0)
6265			_exit(exitval2);
6266
6267		FORKEE_REQUIRE_SUCCESS
6268			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6269
6270		forkee_status_exited(status, exitval2);
6271
6272		printf("Before exiting of the child process\n");
6273		_exit(exitval);
6274	}
6275	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6276
6277	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6278	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6279
6280	validate_status_stopped(status, sigval);
6281
6282	printf("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
6283	    child);
6284	event.pe_set_event = PTRACE_VFORK_DONE;
6285	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6286
6287	printf("Before resuming the child process where it left off and "
6288	    "without signal to be sent\n");
6289	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6290
6291	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6292	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6293
6294	validate_status_stopped(status, sigmasked);
6295
6296	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6297	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
6298
6299	child2 = state.pe_other_pid;
6300	printf("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
6301
6302	printf("Before resuming the child process where it left off and "
6303	    "without signal to be sent\n");
6304	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6305
6306	printf("Before calling %s() for the child - expected stopped "
6307	    "SIGCHLD\n", TWAIT_FNAME);
6308	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6309
6310	validate_status_stopped(status, SIGCHLD);
6311
6312	printf("Before resuming the child process where it left off and "
6313	    "without signal to be sent\n");
6314	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6315
6316	printf("Before calling %s() for the child - expected exited\n",
6317	    TWAIT_FNAME);
6318	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6319
6320	validate_status_exited(status, exitval);
6321
6322	printf("Before calling %s() for the child - expected no process\n",
6323	    TWAIT_FNAME);
6324	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6325}
6326
6327ATF_TC(signal9);
6328ATF_TC_HEAD(signal9, tc)
6329{
6330	atf_tc_set_md_var(tc, "descr",
6331	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6332	    "catching PTRACE_LWP_CREATE breakpoint");
6333}
6334
6335ATF_TC_BODY(signal9, tc)
6336{
6337	const int exitval = 5;
6338	const int sigval = SIGSTOP;
6339	const int sigmasked = SIGTRAP;
6340	pid_t child, wpid;
6341#if defined(TWAIT_HAVE_STATUS)
6342	int status;
6343#endif
6344	sigset_t intmask;
6345	ptrace_state_t state;
6346	const int slen = sizeof(state);
6347	ptrace_event_t event;
6348	const int elen = sizeof(event);
6349	ucontext_t uc;
6350	lwpid_t lid;
6351	static const size_t ssize = 16*1024;
6352	void *stack;
6353
6354	atf_tc_expect_fail("PR kern/51918");
6355
6356	printf("Before forking process PID=%d\n", getpid());
6357	ATF_REQUIRE((child = fork()) != -1);
6358	if (child == 0) {
6359		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6360		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6361
6362		sigemptyset(&intmask);
6363		sigaddset(&intmask, sigmasked);
6364		sigprocmask(SIG_BLOCK, &intmask, NULL);
6365
6366		printf("Before raising %s from child\n", strsignal(sigval));
6367		FORKEE_ASSERT(raise(sigval) == 0);
6368
6369		printf("Before allocating memory for stack in child\n");
6370		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6371
6372		printf("Before making context for new lwp in child\n");
6373		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6374
6375		printf("Before creating new in child\n");
6376		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6377
6378		printf("Before waiting for lwp %d to exit\n", lid);
6379		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6380
6381		printf("Before verifying that reported %d and running lid %d "
6382		    "are the same\n", lid, the_lwp_id);
6383		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6384
6385		printf("Before exiting of the child process\n");
6386		_exit(exitval);
6387	}
6388	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6389
6390	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6391	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6392
6393	validate_status_stopped(status, sigval);
6394
6395	printf("Set empty EVENT_MASK for the child %d\n", child);
6396	event.pe_set_event = PTRACE_LWP_CREATE;
6397	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6398
6399	printf("Before resuming the child process where it left off and "
6400	    "without signal to be sent\n");
6401	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6402
6403	printf("Before calling %s() for the child - expected stopped "
6404	    "SIGTRAP\n", TWAIT_FNAME);
6405	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6406
6407	validate_status_stopped(status, sigmasked);
6408
6409	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6410
6411	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
6412
6413	lid = state.pe_lwp;
6414	printf("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
6415
6416	printf("Before resuming the child process where it left off and "
6417	    "without signal to be sent\n");
6418	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6419
6420	printf("Before calling %s() for the child - expected exited\n",
6421	    TWAIT_FNAME);
6422	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6423
6424	validate_status_exited(status, exitval);
6425
6426	printf("Before calling %s() for the child - expected no process\n",
6427	    TWAIT_FNAME);
6428	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6429}
6430
6431ATF_TC(signal10);
6432ATF_TC_HEAD(signal10, tc)
6433{
6434	atf_tc_set_md_var(tc, "descr",
6435	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6436	    "catching PTRACE_LWP_EXIT breakpoint");
6437}
6438
6439ATF_TC_BODY(signal10, tc)
6440{
6441	const int exitval = 5;
6442	const int sigval = SIGSTOP;
6443	const int sigmasked = SIGTRAP;
6444	pid_t child, wpid;
6445#if defined(TWAIT_HAVE_STATUS)
6446	int status;
6447#endif
6448	sigset_t intmask;
6449	ptrace_state_t state;
6450	const int slen = sizeof(state);
6451	ptrace_event_t event;
6452	const int elen = sizeof(event);
6453	ucontext_t uc;
6454	lwpid_t lid;
6455	static const size_t ssize = 16*1024;
6456	void *stack;
6457
6458	atf_tc_expect_fail("PR kern/51918");
6459
6460	printf("Before forking process PID=%d\n", getpid());
6461	ATF_REQUIRE((child = fork()) != -1);
6462	if (child == 0) {
6463		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6464		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6465
6466		sigemptyset(&intmask);
6467		sigaddset(&intmask, sigmasked);
6468		sigprocmask(SIG_BLOCK, &intmask, NULL);
6469
6470		printf("Before raising %s from child\n", strsignal(sigval));
6471		FORKEE_ASSERT(raise(sigval) == 0);
6472
6473		printf("Before allocating memory for stack in child\n");
6474		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6475
6476		printf("Before making context for new lwp in child\n");
6477		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6478
6479		printf("Before creating new in child\n");
6480		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6481
6482		printf("Before waiting for lwp %d to exit\n", lid);
6483		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6484
6485		printf("Before verifying that reported %d and running lid %d "
6486		    "are the same\n", lid, the_lwp_id);
6487		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6488
6489		printf("Before exiting of the child process\n");
6490		_exit(exitval);
6491	}
6492	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6493
6494	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6495	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6496
6497	validate_status_stopped(status, sigval);
6498
6499	printf("Set empty EVENT_MASK for the child %d\n", child);
6500	event.pe_set_event = PTRACE_LWP_EXIT;
6501	ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6502
6503	printf("Before resuming the child process where it left off and "
6504	    "without signal to be sent\n");
6505	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6506
6507	printf("Before calling %s() for the child - expected stopped "
6508	    "SIGTRAP\n", TWAIT_FNAME);
6509	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6510
6511	validate_status_stopped(status, sigmasked);
6512
6513	ATF_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6514
6515	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
6516
6517	lid = state.pe_lwp;
6518	printf("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
6519
6520	printf("Before resuming the child process where it left off and "
6521	    "without signal to be sent\n");
6522	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6523
6524	printf("Before calling %s() for the child - expected exited\n",
6525	    TWAIT_FNAME);
6526	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6527
6528	validate_status_exited(status, exitval);
6529
6530	printf("Before calling %s() for the child - expected no process\n",
6531	    TWAIT_FNAME);
6532	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6533}
6534
6535ATF_TC(getsigmask1);
6536ATF_TC_HEAD(getsigmask1, tc)
6537{
6538	atf_tc_set_md_var(tc, "descr",
6539	    "Verify that plain PT_SET_SIGMASK can be called");
6540}
6541
6542ATF_TC_BODY(getsigmask1, tc)
6543{
6544	const int exitval = 5;
6545	const int sigval = SIGSTOP;
6546	pid_t child, wpid;
6547#if defined(TWAIT_HAVE_STATUS)
6548	int status;
6549#endif
6550	sigset_t mask;
6551
6552	printf("Before forking process PID=%d\n", getpid());
6553	ATF_REQUIRE((child = fork()) != -1);
6554	if (child == 0) {
6555		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6556		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6557
6558		printf("Before raising %s from child\n", strsignal(sigval));
6559		FORKEE_ASSERT(raise(sigval) == 0);
6560
6561		printf("Before exiting of the child process\n");
6562		_exit(exitval);
6563	}
6564	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6565
6566	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6567	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6568
6569	validate_status_stopped(status, sigval);
6570
6571	printf("Before calling PT_GET_SIGMASK\n");
6572	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6573
6574	printf("Before resuming the child process where it left off and "
6575	    "without signal to be sent\n");
6576	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6577
6578	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6579	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6580
6581	validate_status_exited(status, exitval);
6582
6583	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6584	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6585}
6586
6587ATF_TC(getsigmask2);
6588ATF_TC_HEAD(getsigmask2, tc)
6589{
6590	atf_tc_set_md_var(tc, "descr",
6591	    "Verify that PT_SET_SIGMASK reports correct mask from tracee");
6592}
6593
6594ATF_TC_BODY(getsigmask2, tc)
6595{
6596	const int exitval = 5;
6597	const int sigval = SIGSTOP;
6598	const int sigmasked = SIGTRAP;
6599	pid_t child, wpid;
6600#if defined(TWAIT_HAVE_STATUS)
6601	int status;
6602#endif
6603	sigset_t mask;
6604	sigset_t expected_mask;
6605	ATF_REQUIRE(sigemptyset(&mask) == 0);
6606	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6607	ATF_REQUIRE(sigaddset(&expected_mask, sigmasked) == 0);
6608
6609	printf("Before forking process PID=%d\n", getpid());
6610	ATF_REQUIRE((child = fork()) != -1);
6611	if (child == 0) {
6612		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6613		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6614
6615		sigaddset(&mask, sigmasked);
6616		sigprocmask(SIG_BLOCK, &mask, NULL);
6617
6618		printf("Before raising %s from child\n", strsignal(sigval));
6619		FORKEE_ASSERT(raise(sigval) == 0);
6620
6621		printf("Before exiting of the child process\n");
6622		_exit(exitval);
6623	}
6624	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6625
6626	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6627	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6628
6629	validate_status_stopped(status, sigval);
6630
6631	printf("Before calling PT_GET_SIGMASK\n");
6632	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6633
6634	ATF_REQUIRE(memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6635
6636	printf("Before resuming the child process where it left off and "
6637	    "without signal to be sent\n");
6638	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6639
6640	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6641	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6642
6643	validate_status_exited(status, exitval);
6644
6645	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6646	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6647}
6648
6649ATF_TC(setsigmask1);
6650ATF_TC_HEAD(setsigmask1, tc)
6651{
6652	atf_tc_set_md_var(tc, "descr",
6653	    "Verify that plain PT_SET_SIGMASK can be called with empty mask");
6654}
6655
6656ATF_TC_BODY(setsigmask1, tc)
6657{
6658	const int exitval = 5;
6659	const int sigval = SIGSTOP;
6660	pid_t child, wpid;
6661#if defined(TWAIT_HAVE_STATUS)
6662	int status;
6663#endif
6664	sigset_t mask;
6665	ATF_REQUIRE(sigemptyset(&mask) == 0);
6666
6667	printf("Before forking process PID=%d\n", getpid());
6668	ATF_REQUIRE((child = fork()) != -1);
6669	if (child == 0) {
6670		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6671		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6672
6673		printf("Before raising %s from child\n", strsignal(sigval));
6674		FORKEE_ASSERT(raise(sigval) == 0);
6675
6676		printf("Before exiting of the child process\n");
6677		_exit(exitval);
6678	}
6679	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6680
6681	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6682	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6683
6684	validate_status_stopped(status, sigval);
6685
6686	printf("Before calling PT_SET_SIGMASK for empty mask\n");
6687	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6688
6689	printf("Before resuming the child process where it left off and "
6690	    "without signal to be sent\n");
6691	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6692
6693	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6694	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6695
6696	validate_status_exited(status, exitval);
6697
6698	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6699	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6700}
6701
6702ATF_TC(setsigmask2);
6703ATF_TC_HEAD(setsigmask2, tc)
6704{
6705	atf_tc_set_md_var(tc, "descr",
6706	    "Verify that sigmask is preserved between PT_GET_SIGMASK and "
6707	    "PT_SET_SIGMASK");
6708}
6709
6710ATF_TC_BODY(setsigmask2, tc)
6711{
6712	const int exitval = 5;
6713	const int sigval = SIGSTOP;
6714	pid_t child, wpid;
6715#if defined(TWAIT_HAVE_STATUS)
6716	int status;
6717#endif
6718	sigset_t new_mask;
6719	sigset_t mask;
6720	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6721	ATF_REQUIRE(sigemptyset(&mask) == 0);
6722	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6723
6724	printf("Before forking process PID=%d\n", getpid());
6725	ATF_REQUIRE((child = fork()) != -1);
6726	if (child == 0) {
6727		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6728		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6729
6730		printf("Before raising %s from child\n", strsignal(sigval));
6731		FORKEE_ASSERT(raise(sigval) == 0);
6732
6733		printf("Before exiting of the child process\n");
6734		_exit(exitval);
6735	}
6736	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6737
6738	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6739	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6740
6741	validate_status_stopped(status, sigval);
6742
6743	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6744	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6745
6746	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6747	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6748
6749	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6750
6751	printf("Before resuming the child process where it left off and "
6752	    "without signal to be sent\n");
6753	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6754
6755	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6756	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6757
6758	validate_status_exited(status, exitval);
6759
6760	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6761	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6762}
6763
6764ATF_TC(setsigmask3);
6765ATF_TC_HEAD(setsigmask3, tc)
6766{
6767	atf_tc_set_md_var(tc, "descr",
6768	    "Verify that sigmask is preserved between PT_GET_SIGMASK, process "
6769	    "resumed and PT_SET_SIGMASK");
6770}
6771
6772ATF_TC_BODY(setsigmask3, tc)
6773{
6774	const int exitval = 5;
6775	const int sigval = SIGSTOP;
6776	pid_t child, wpid;
6777#if defined(TWAIT_HAVE_STATUS)
6778	int status;
6779#endif
6780	sigset_t new_mask;
6781	sigset_t mask;
6782	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6783	ATF_REQUIRE(sigemptyset(&mask) == 0);
6784	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6785
6786	printf("Before forking process PID=%d\n", getpid());
6787	ATF_REQUIRE((child = fork()) != -1);
6788	if (child == 0) {
6789		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6790		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6791
6792		printf("Before raising %s from child\n", strsignal(sigval));
6793		FORKEE_ASSERT(raise(sigval) == 0);
6794
6795		printf("Before raising %s from child\n", strsignal(sigval));
6796		FORKEE_ASSERT(raise(sigval) == 0);
6797
6798		printf("Before exiting of the child process\n");
6799		_exit(exitval);
6800	}
6801	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6802
6803	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6804	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6805
6806	validate_status_stopped(status, sigval);
6807
6808	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6809	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6810
6811	printf("Before resuming the child process where it left off and "
6812	    "without signal to be sent\n");
6813	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6814
6815	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6816	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6817
6818	validate_status_stopped(status, sigval);
6819
6820	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6821	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6822
6823	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6824
6825	printf("Before resuming the child process where it left off and "
6826	    "without signal to be sent\n");
6827	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6828
6829	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6830	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6831
6832	validate_status_exited(status, exitval);
6833
6834	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6835	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6836}
6837
6838ATF_TC(setsigmask4);
6839ATF_TC_HEAD(setsigmask4, tc)
6840{
6841	atf_tc_set_md_var(tc, "descr",
6842	    "Verify that new sigmask is visible in tracee");
6843}
6844
6845ATF_TC_BODY(setsigmask4, tc)
6846{
6847	const int exitval = 5;
6848	const int sigval = SIGSTOP;
6849	pid_t child, wpid;
6850#if defined(TWAIT_HAVE_STATUS)
6851	int status;
6852#endif
6853	sigset_t mask;
6854	sigset_t expected_mask;
6855	ATF_REQUIRE(sigemptyset(&mask) == 0);
6856	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6857	ATF_REQUIRE(sigaddset(&expected_mask, SIGINT) == 0);
6858
6859	printf("Before forking process PID=%d\n", getpid());
6860	ATF_REQUIRE((child = fork()) != -1);
6861	if (child == 0) {
6862		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6863		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6864
6865		printf("Before raising %s from child\n", strsignal(sigval));
6866		FORKEE_ASSERT(raise(sigval) == 0);
6867
6868		sigprocmask(0, NULL, &mask);
6869
6870		FORKEE_ASSERT
6871		    (memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6872
6873		printf("Before exiting of the child process\n");
6874		_exit(exitval);
6875	}
6876	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6877
6878	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6879	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6880
6881	validate_status_stopped(status, sigval);
6882
6883	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6884	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &expected_mask, 0) != -1);
6885
6886	printf("Before resuming the child process where it left off and "
6887	    "without signal to be sent\n");
6888	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6889
6890	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6891	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6892
6893	validate_status_exited(status, exitval);
6894
6895	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6896	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6897}
6898
6899ATF_TC(setsigmask5);
6900ATF_TC_HEAD(setsigmask5, tc)
6901{
6902	atf_tc_set_md_var(tc, "descr",
6903	    "Verify that sigmask cannot be set to SIGKILL");
6904}
6905
6906ATF_TC_BODY(setsigmask5, tc)
6907{
6908	const int exitval = 5;
6909	const int sigval = SIGSTOP;
6910	pid_t child, wpid;
6911#if defined(TWAIT_HAVE_STATUS)
6912	int status;
6913#endif
6914	sigset_t new_mask;
6915	sigset_t mask;
6916	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6917	ATF_REQUIRE(sigemptyset(&mask) == 0);
6918	ATF_REQUIRE(sigaddset(&mask, SIGKILL) == 0);
6919
6920	printf("Before forking process PID=%d\n", getpid());
6921	ATF_REQUIRE((child = fork()) != -1);
6922	if (child == 0) {
6923		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6924		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6925
6926		printf("Before raising %s from child\n", strsignal(sigval));
6927		FORKEE_ASSERT(raise(sigval) == 0);
6928
6929		printf("Before exiting of the child process\n");
6930		_exit(exitval);
6931	}
6932	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6933
6934	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6935	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6936
6937	validate_status_stopped(status, sigval);
6938
6939	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6940	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6941
6942	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6943	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6944
6945	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
6946
6947	printf("Before resuming the child process where it left off and "
6948	    "without signal to be sent\n");
6949	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6950
6951	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6952	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6953
6954	validate_status_exited(status, exitval);
6955
6956	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6957	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6958}
6959
6960ATF_TC(setsigmask6);
6961ATF_TC_HEAD(setsigmask6, tc)
6962{
6963	atf_tc_set_md_var(tc, "descr",
6964	    "Verify that sigmask cannot be set to SIGSTOP");
6965}
6966
6967ATF_TC_BODY(setsigmask6, tc)
6968{
6969	const int exitval = 5;
6970	const int sigval = SIGSTOP;
6971	pid_t child, wpid;
6972#if defined(TWAIT_HAVE_STATUS)
6973	int status;
6974#endif
6975	sigset_t new_mask;
6976	sigset_t mask;
6977	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6978	ATF_REQUIRE(sigemptyset(&mask) == 0);
6979	ATF_REQUIRE(sigaddset(&mask, SIGSTOP) == 0);
6980
6981	printf("Before forking process PID=%d\n", getpid());
6982	ATF_REQUIRE((child = fork()) != -1);
6983	if (child == 0) {
6984		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
6985		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6986
6987		printf("Before raising %s from child\n", strsignal(sigval));
6988		FORKEE_ASSERT(raise(sigval) == 0);
6989
6990		printf("Before exiting of the child process\n");
6991		_exit(exitval);
6992	}
6993	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6994
6995	printf("Before calling %s() for the child\n", TWAIT_FNAME);
6996	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6997
6998	validate_status_stopped(status, sigval);
6999
7000	printf("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
7001	ATF_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
7002
7003	printf("Before calling PT_GET_SIGMASK to store it in new_mask\n");
7004	ATF_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
7005
7006	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
7007
7008	printf("Before resuming the child process where it left off and "
7009	    "without signal to be sent\n");
7010	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7011
7012	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7013	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7014
7015	validate_status_exited(status, exitval);
7016
7017	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7018	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7019}
7020
7021static void
7022lwp_main_stop(void *arg)
7023{
7024	the_lwp_id = _lwp_self();
7025
7026	raise(SIGTRAP);
7027
7028	_lwp_exit();
7029}
7030
7031ATF_TC(suspend1);
7032ATF_TC_HEAD(suspend1, tc)
7033{
7034	atf_tc_set_md_var(tc, "descr",
7035	    "Verify that a thread can be suspended by a debugger and later "
7036	    "resumed by a tracee");
7037}
7038
7039ATF_TC_BODY(suspend1, tc)
7040{
7041	const int exitval = 5;
7042	const int sigval = SIGSTOP;
7043	pid_t child, wpid;
7044#if defined(TWAIT_HAVE_STATUS)
7045	int status;
7046#endif
7047	ucontext_t uc;
7048	lwpid_t lid;
7049	static const size_t ssize = 16*1024;
7050	void *stack;
7051	struct ptrace_lwpinfo pl;
7052	struct ptrace_siginfo psi;
7053	volatile int go = 0;
7054
7055	printf("Before forking process PID=%d\n", getpid());
7056	ATF_REQUIRE((child = fork()) != -1);
7057	if (child == 0) {
7058		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7059		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7060
7061		printf("Before raising %s from child\n", strsignal(sigval));
7062		FORKEE_ASSERT(raise(sigval) == 0);
7063
7064		printf("Before allocating memory for stack in child\n");
7065		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7066
7067		printf("Before making context for new lwp in child\n");
7068		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7069
7070		printf("Before creating new in child\n");
7071		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7072
7073		while (go == 0)
7074			continue;
7075
7076		raise(SIGINT);
7077
7078		FORKEE_ASSERT(_lwp_continue(lid) == 0);
7079
7080		printf("Before waiting for lwp %d to exit\n", lid);
7081		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7082
7083		printf("Before verifying that reported %d and running lid %d "
7084		    "are the same\n", lid, the_lwp_id);
7085		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7086
7087		printf("Before exiting of the child process\n");
7088		_exit(exitval);
7089	}
7090	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7091
7092	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7093	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7094
7095	validate_status_stopped(status, sigval);
7096
7097	printf("Before resuming the child process where it left off and "
7098	    "without signal to be sent\n");
7099	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7100
7101	printf("Before calling %s() for the child - expected stopped "
7102	    "SIGTRAP\n", TWAIT_FNAME);
7103	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7104
7105	validate_status_stopped(status, SIGTRAP);
7106
7107	printf("Before reading siginfo and lwpid_t\n");
7108	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7109
7110	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7111	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7112
7113        printf("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
7114	    child, getpid());
7115	ATF_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
7116
7117	printf("Before resuming the child process where it left off and "
7118	    "without signal to be sent\n");
7119	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7120
7121	printf("Before calling %s() for the child - expected stopped "
7122	    "SIGINT\n", TWAIT_FNAME);
7123	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7124
7125	validate_status_stopped(status, SIGINT);
7126
7127	pl.pl_lwpid = 0;
7128
7129	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7130	while (pl.pl_lwpid != 0) {
7131
7132		ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7133		switch (pl.pl_lwpid) {
7134		case 1:
7135			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7136			break;
7137		case 2:
7138			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7139			break;
7140		}
7141	}
7142
7143	printf("Before resuming the child process where it left off and "
7144	    "without signal to be sent\n");
7145	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7146
7147	printf("Before calling %s() for the child - expected exited\n",
7148	    TWAIT_FNAME);
7149	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7150
7151	validate_status_exited(status, exitval);
7152
7153	printf("Before calling %s() for the child - expected no process\n",
7154	    TWAIT_FNAME);
7155	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7156}
7157
7158ATF_TC(suspend2);
7159ATF_TC_HEAD(suspend2, tc)
7160{
7161	atf_tc_set_md_var(tc, "descr",
7162	    "Verify that the while the only thread within a process is "
7163	    "suspended, the whole process cannot be unstopped");
7164}
7165
7166ATF_TC_BODY(suspend2, tc)
7167{
7168	const int exitval = 5;
7169	const int sigval = SIGSTOP;
7170	pid_t child, wpid;
7171#if defined(TWAIT_HAVE_STATUS)
7172	int status;
7173#endif
7174	struct ptrace_siginfo psi;
7175
7176	printf("Before forking process PID=%d\n", getpid());
7177	ATF_REQUIRE((child = fork()) != -1);
7178	if (child == 0) {
7179		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7180		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7181
7182		printf("Before raising %s from child\n", strsignal(sigval));
7183		FORKEE_ASSERT(raise(sigval) == 0);
7184
7185		printf("Before exiting of the child process\n");
7186		_exit(exitval);
7187	}
7188	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7189
7190	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7191	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7192
7193	validate_status_stopped(status, sigval);
7194
7195	printf("Before reading siginfo and lwpid_t\n");
7196	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7197
7198	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7199	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7200
7201	printf("Before resuming the child process where it left off and "
7202	    "without signal to be sent\n");
7203	ATF_REQUIRE_ERRNO(EDEADLK,
7204	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
7205
7206	printf("Before resuming LWP %d\n", psi.psi_lwpid);
7207	ATF_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7208
7209	printf("Before resuming the child process where it left off and "
7210	    "without signal to be sent\n");
7211	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7212
7213	printf("Before calling %s() for the child - expected exited\n",
7214	    TWAIT_FNAME);
7215	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7216
7217	validate_status_exited(status, exitval);
7218
7219	printf("Before calling %s() for the child - expected no process\n",
7220	    TWAIT_FNAME);
7221	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7222}
7223
7224ATF_TC(resume1);
7225ATF_TC_HEAD(resume1, tc)
7226{
7227	atf_tc_set_md_var(tc, "timeout", "5");
7228	atf_tc_set_md_var(tc, "descr",
7229	    "Verify that a thread can be suspended by a debugger and later "
7230	    "resumed by the debugger");
7231}
7232
7233ATF_TC_BODY(resume1, tc)
7234{
7235	struct msg_fds fds;
7236	const int exitval = 5;
7237	const int sigval = SIGSTOP;
7238	pid_t child, wpid;
7239	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
7240#if defined(TWAIT_HAVE_STATUS)
7241	int status;
7242#endif
7243	ucontext_t uc;
7244	lwpid_t lid;
7245	static const size_t ssize = 16*1024;
7246	void *stack;
7247	struct ptrace_lwpinfo pl;
7248	struct ptrace_siginfo psi;
7249
7250	atf_tc_expect_timeout("PR kern/51995");
7251
7252	ATF_REQUIRE(msg_open(&fds) == 0);
7253
7254	printf("Before forking process PID=%d\n", getpid());
7255	ATF_REQUIRE((child = fork()) != -1);
7256	if (child == 0) {
7257		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7258		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7259
7260		printf("Before raising %s from child\n", strsignal(sigval));
7261		FORKEE_ASSERT(raise(sigval) == 0);
7262
7263		printf("Before allocating memory for stack in child\n");
7264		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7265
7266		printf("Before making context for new lwp in child\n");
7267		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7268
7269		printf("Before creating new in child\n");
7270		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7271
7272		CHILD_TO_PARENT("Message", fds, msg);
7273
7274		raise(SIGINT);
7275
7276		printf("Before waiting for lwp %d to exit\n", lid);
7277		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7278
7279		printf("Before verifying that reported %d and running lid %d "
7280		    "are the same\n", lid, the_lwp_id);
7281		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7282
7283		printf("Before exiting of the child process\n");
7284		_exit(exitval);
7285	}
7286	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7287
7288	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7289	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7290
7291	validate_status_stopped(status, sigval);
7292
7293	printf("Before resuming the child process where it left off and "
7294	    "without signal to be sent\n");
7295	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7296
7297	printf("Before calling %s() for the child - expected stopped "
7298	    "SIGTRAP\n", TWAIT_FNAME);
7299	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7300
7301	validate_status_stopped(status, SIGTRAP);
7302
7303	printf("Before reading siginfo and lwpid_t\n");
7304	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7305
7306	printf("Before suspending LWP %d\n", psi.psi_lwpid);
7307	ATF_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7308
7309	PARENT_FROM_CHILD("Message", fds, msg);
7310
7311	printf("Before resuming the child process where it left off and "
7312	    "without signal to be sent\n");
7313	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7314
7315	printf("Before calling %s() for the child - expected stopped "
7316	    "SIGINT\n", TWAIT_FNAME);
7317	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7318
7319	validate_status_stopped(status, SIGINT);
7320
7321	pl.pl_lwpid = 0;
7322
7323	ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7324	while (pl.pl_lwpid != 0) {
7325		ATF_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7326		switch (pl.pl_lwpid) {
7327		case 1:
7328			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7329			break;
7330		case 2:
7331			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7332			break;
7333		}
7334	}
7335
7336	printf("Before resuming LWP %d\n", psi.psi_lwpid);
7337	ATF_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7338
7339	printf("Before resuming the child process where it left off and "
7340	    "without signal to be sent\n");
7341	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7342
7343	printf("Before calling %s() for the child - expected exited\n",
7344	    TWAIT_FNAME);
7345	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7346
7347	validate_status_exited(status, exitval);
7348
7349	printf("Before calling %s() for the child - expected no process\n",
7350	    TWAIT_FNAME);
7351	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7352
7353	msg_close(&fds);
7354
7355	printf("XXX: Test worked this time but for consistency timeout it\n");
7356	sleep(10);
7357}
7358
7359ATF_TC(syscall1);
7360ATF_TC_HEAD(syscall1, tc)
7361{
7362	atf_tc_set_md_var(tc, "descr",
7363	    "Verify that getpid(2) can be traced with PT_SYSCALL");
7364}
7365
7366ATF_TC_BODY(syscall1, tc)
7367{
7368	const int exitval = 5;
7369	const int sigval = SIGSTOP;
7370	pid_t child, wpid;
7371#if defined(TWAIT_HAVE_STATUS)
7372	int status;
7373#endif
7374	struct ptrace_siginfo info;
7375	memset(&info, 0, sizeof(info));
7376
7377	printf("Before forking process PID=%d\n", getpid());
7378	ATF_REQUIRE((child = fork()) != -1);
7379	if (child == 0) {
7380		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7381		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7382
7383		printf("Before raising %s from child\n", strsignal(sigval));
7384		FORKEE_ASSERT(raise(sigval) == 0);
7385
7386		syscall(SYS_getpid);
7387
7388		printf("Before exiting of the child process\n");
7389		_exit(exitval);
7390	}
7391	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7392
7393	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7394	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7395
7396	validate_status_stopped(status, sigval);
7397
7398	printf("Before resuming the child process where it left off and "
7399	    "without signal to be sent\n");
7400	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7401
7402	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7403	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7404
7405	validate_status_stopped(status, SIGTRAP);
7406
7407	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7408	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7409
7410	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7411	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
7412
7413	printf("Before resuming the child process where it left off and "
7414	    "without signal to be sent\n");
7415	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7416
7417	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7418	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7419
7420	validate_status_stopped(status, SIGTRAP);
7421
7422	printf("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7423	ATF_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7424
7425	printf("Before checking siginfo_t\n");
7426	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7427	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
7428
7429	printf("Before resuming the child process where it left off and "
7430	    "without signal to be sent\n");
7431	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7432
7433	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7434	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7435
7436	validate_status_exited(status, exitval);
7437
7438	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7439	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7440}
7441
7442ATF_TC(syscallemu1);
7443ATF_TC_HEAD(syscallemu1, tc)
7444{
7445	atf_tc_set_md_var(tc, "descr",
7446	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
7447}
7448
7449ATF_TC_BODY(syscallemu1, tc)
7450{
7451	const int exitval = 5;
7452	const int sigval = SIGSTOP;
7453	pid_t child, wpid;
7454#if defined(TWAIT_HAVE_STATUS)
7455	int status;
7456#endif
7457
7458#if defined(__sparc__) && !defined(__sparc64__)
7459	/* syscallemu does not work on sparc (32-bit) */
7460	atf_tc_expect_fail("PR kern/52166");
7461#endif
7462
7463	printf("Before forking process PID=%d\n", getpid());
7464	ATF_REQUIRE((child = fork()) != -1);
7465	if (child == 0) {
7466		printf("Before calling PT_TRACE_ME from child %d\n", getpid());
7467		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7468
7469		printf("Before raising %s from child\n", strsignal(sigval));
7470		FORKEE_ASSERT(raise(sigval) == 0);
7471
7472		syscall(SYS_exit, 100);
7473
7474		printf("Before exiting of the child process\n");
7475		_exit(exitval);
7476	}
7477	printf("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7478
7479	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7480	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7481
7482	validate_status_stopped(status, sigval);
7483
7484	printf("Before resuming the child process where it left off and "
7485	    "without signal to be sent\n");
7486	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7487
7488	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7489	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7490
7491	validate_status_stopped(status, SIGTRAP);
7492
7493	printf("Set SYSCALLEMU for intercepted syscall\n");
7494	ATF_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
7495
7496	printf("Before resuming the child process where it left off and "
7497	    "without signal to be sent\n");
7498	ATF_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7499
7500	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7501	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7502
7503	validate_status_stopped(status, SIGTRAP);
7504
7505	printf("Before resuming the child process where it left off and "
7506	    "without signal to be sent\n");
7507	ATF_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7508
7509	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7510	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7511
7512	validate_status_exited(status, exitval);
7513
7514	printf("Before calling %s() for the child\n", TWAIT_FNAME);
7515	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7516}
7517
7518#include "t_ptrace_amd64_wait.h"
7519#include "t_ptrace_i386_wait.h"
7520#include "t_ptrace_x86_wait.h"
7521
7522ATF_TP_ADD_TCS(tp)
7523{
7524	setvbuf(stdout, NULL, _IONBF, 0);
7525	setvbuf(stderr, NULL, _IONBF, 0);
7526	ATF_TP_ADD_TC(tp, traceme1);
7527	ATF_TP_ADD_TC(tp, traceme2);
7528	ATF_TP_ADD_TC(tp, traceme3);
7529	ATF_TP_ADD_TC(tp, traceme4);
7530
7531	ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
7532	ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
7533	ATF_TP_ADD_TC(tp, attach3);
7534	ATF_TP_ADD_TC(tp, attach4);
7535	ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
7536	ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
7537	ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
7538
7539	ATF_TP_ADD_TC(tp, eventmask1);
7540	ATF_TP_ADD_TC(tp, eventmask2);
7541	ATF_TP_ADD_TC(tp, eventmask3);
7542	ATF_TP_ADD_TC(tp, eventmask4);
7543	ATF_TP_ADD_TC(tp, eventmask5);
7544	ATF_TP_ADD_TC(tp, eventmask6);
7545
7546	ATF_TP_ADD_TC_HAVE_PID(tp, fork1);
7547	ATF_TP_ADD_TC(tp, fork2);
7548
7549	ATF_TP_ADD_TC_HAVE_PID(tp, vfork1);
7550	ATF_TP_ADD_TC(tp, vfork2);
7551
7552	ATF_TP_ADD_TC(tp, vforkdone1);
7553	ATF_TP_ADD_TC(tp, vforkdone2);
7554
7555	ATF_TP_ADD_TC(tp, io_read_d1);
7556	ATF_TP_ADD_TC(tp, io_read_d2);
7557	ATF_TP_ADD_TC(tp, io_read_d3);
7558	ATF_TP_ADD_TC(tp, io_read_d4);
7559
7560	ATF_TP_ADD_TC(tp, io_write_d1);
7561	ATF_TP_ADD_TC(tp, io_write_d2);
7562	ATF_TP_ADD_TC(tp, io_write_d3);
7563	ATF_TP_ADD_TC(tp, io_write_d4);
7564
7565	ATF_TP_ADD_TC(tp, read_d1);
7566	ATF_TP_ADD_TC(tp, read_d2);
7567	ATF_TP_ADD_TC(tp, read_d3);
7568	ATF_TP_ADD_TC(tp, read_d4);
7569
7570	ATF_TP_ADD_TC(tp, write_d1);
7571	ATF_TP_ADD_TC(tp, write_d2);
7572	ATF_TP_ADD_TC(tp, write_d3);
7573	ATF_TP_ADD_TC(tp, write_d4);
7574
7575	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1);
7576	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2);
7577
7578	ATF_TP_ADD_TC(tp, read_d_write_d_handshake1);
7579	ATF_TP_ADD_TC(tp, read_d_write_d_handshake2);
7580
7581	ATF_TP_ADD_TC(tp, io_read_i1);
7582	ATF_TP_ADD_TC(tp, io_read_i2);
7583	ATF_TP_ADD_TC(tp, io_read_i3);
7584	ATF_TP_ADD_TC(tp, io_read_i4);
7585
7586	ATF_TP_ADD_TC(tp, read_i1);
7587	ATF_TP_ADD_TC(tp, read_i2);
7588	ATF_TP_ADD_TC(tp, read_i3);
7589	ATF_TP_ADD_TC(tp, read_i4);
7590
7591	ATF_TP_ADD_TC(tp, io_read_auxv1);
7592
7593	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
7594	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
7595	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
7596	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
7597	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
7598
7599	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
7600	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
7601
7602	ATF_TP_ADD_TC_PT_STEP(tp, step1);
7603	ATF_TP_ADD_TC_PT_STEP(tp, step2);
7604	ATF_TP_ADD_TC_PT_STEP(tp, step3);
7605	ATF_TP_ADD_TC_PT_STEP(tp, step4);
7606
7607	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
7608	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
7609	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
7610	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
7611
7612	ATF_TP_ADD_TC(tp, kill1);
7613	ATF_TP_ADD_TC(tp, kill2);
7614
7615	ATF_TP_ADD_TC(tp, lwpinfo1);
7616	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
7617
7618	ATF_TP_ADD_TC(tp, siginfo1);
7619	ATF_TP_ADD_TC(tp, siginfo2);
7620	ATF_TP_ADD_TC(tp, siginfo3);
7621	ATF_TP_ADD_TC(tp, siginfo4);
7622	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
7623	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
7624
7625	ATF_TP_ADD_TC(tp, lwp_create1);
7626
7627	ATF_TP_ADD_TC(tp, lwp_exit1);
7628
7629	ATF_TP_ADD_TC(tp, signal1);
7630	ATF_TP_ADD_TC(tp, signal2);
7631	ATF_TP_ADD_TC(tp, signal3);
7632	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
7633	ATF_TP_ADD_TC(tp, signal5);
7634	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
7635	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
7636	ATF_TP_ADD_TC(tp, signal8);
7637	ATF_TP_ADD_TC(tp, signal9);
7638	ATF_TP_ADD_TC(tp, signal10);
7639
7640	ATF_TP_ADD_TC(tp, suspend1);
7641	ATF_TP_ADD_TC(tp, suspend2);
7642
7643	ATF_TP_ADD_TC(tp, resume1);
7644
7645	ATF_TP_ADD_TC(tp, getsigmask1);
7646	ATF_TP_ADD_TC(tp, getsigmask2);
7647
7648	ATF_TP_ADD_TC(tp, setsigmask1);
7649	ATF_TP_ADD_TC(tp, setsigmask2);
7650	ATF_TP_ADD_TC(tp, setsigmask3);
7651	ATF_TP_ADD_TC(tp, setsigmask4);
7652	ATF_TP_ADD_TC(tp, setsigmask5);
7653	ATF_TP_ADD_TC(tp, setsigmask6);
7654
7655	ATF_TP_ADD_TC(tp, syscall1);
7656
7657	ATF_TP_ADD_TC(tp, syscallemu1);
7658
7659	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
7660	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
7661	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
7662
7663	return atf_no_error();
7664}
7665