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