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