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