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