t_ptrace_wait.c revision 1.21
1/*	$NetBSD: t_ptrace_wait.c,v 1.21 2017/12/28 09:47:52 kamil Exp $	*/
2
3/*-
4 * Copyright (c) 2016 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#include <sys/cdefs.h>
30__RCSID("$NetBSD: t_ptrace_wait.c,v 1.21 2017/12/28 09:47:52 kamil Exp $");
31
32#include <sys/param.h>
33#include <sys/types.h>
34#include <sys/ptrace.h>
35#include <sys/resource.h>
36#include <sys/stat.h>
37#include <sys/syscall.h>
38#include <sys/sysctl.h>
39#include <sys/wait.h>
40#include <machine/reg.h>
41#include <elf.h>
42#include <err.h>
43#include <errno.h>
44#include <lwp.h>
45#include <sched.h>
46#include <signal.h>
47#include <stdint.h>
48#include <stdio.h>
49#include <stdlib.h>
50#include <strings.h>
51#include <unistd.h>
52
53#include <atf-c.h>
54
55#include "h_macros.h"
56
57#include "t_ptrace_wait.h"
58#include "msg.h"
59
60#define PARENT_TO_CHILD(info, fds, msg) \
61    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	/* pmax kernel 8.99.9 panics */
4315	char *machine;
4316	size_t mlen;
4317	machine = asysctlbyname("hw.machine", &mlen);
4318	ATF_REQUIRE(machine);
4319	if (strcmp(machine, "pmax") == 0) {
4320		atf_tc_expect_fail("PR port-pmax/52871");
4321		ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4322	}
4323	free(machine);
4324
4325	DPRINTF("Before forking process PID=%d\n", getpid());
4326	SYSCALL_REQUIRE((child = fork()) != -1);
4327	if (child == 0) {
4328		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4329		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4330
4331		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4332		FORKEE_ASSERT(raise(sigval) == 0);
4333
4334		DPRINTF("Before exiting of the child process\n");
4335		_exit(exitval);
4336	}
4337	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4338
4339	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4340	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4341
4342	validate_status_stopped(status, sigval);
4343
4344	DPRINTF("Call GETFPREGS for the child process\n");
4345	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
4346
4347	DPRINTF("Before resuming the child process where it left off and "
4348	    "without signal to be sent\n");
4349	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4350
4351	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4352	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4353
4354	validate_status_exited(status, exitval);
4355
4356	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4357	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4358}
4359#endif
4360
4361#if defined(HAVE_FPREGS)
4362ATF_TC(fpregs2);
4363ATF_TC_HEAD(fpregs2, tc)
4364{
4365	atf_tc_set_md_var(tc, "descr",
4366	    "Verify PT_GETFPREGS and PT_SETFPREGS calls without changing "
4367	    "regs");
4368}
4369
4370ATF_TC_BODY(fpregs2, tc)
4371{
4372	const int exitval = 5;
4373	const int sigval = SIGSTOP;
4374	pid_t child, wpid;
4375#if defined(TWAIT_HAVE_STATUS)
4376	int status;
4377#endif
4378	struct fpreg r;
4379
4380	/* pmax kernel 8.99.9 panics */
4381	char *machine;
4382	size_t mlen;
4383	machine = asysctlbyname("hw.machine", &mlen);
4384	ATF_REQUIRE(machine);
4385	if (strcmp(machine, "pmax") == 0) {
4386		atf_tc_expect_fail("PR port-pmax/52871");
4387		ATF_REQUIRE(0 && "In order to get reliable failure, abort");
4388	}
4389	free(machine);
4390
4391	DPRINTF("Before forking process PID=%d\n", getpid());
4392	SYSCALL_REQUIRE((child = fork()) != -1);
4393	if (child == 0) {
4394		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4395		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4396
4397		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4398		FORKEE_ASSERT(raise(sigval) == 0);
4399
4400		DPRINTF("Before exiting of the child process\n");
4401		_exit(exitval);
4402	}
4403	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4404
4405	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4406	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4407
4408	validate_status_stopped(status, sigval);
4409
4410	DPRINTF("Call GETFPREGS for the child process\n");
4411	SYSCALL_REQUIRE(ptrace(PT_GETFPREGS, child, &r, 0) != -1);
4412
4413	DPRINTF("Call SETFPREGS for the child (without changed regs)\n");
4414	SYSCALL_REQUIRE(ptrace(PT_SETFPREGS, child, &r, 0) != -1);
4415
4416	DPRINTF("Before resuming the child process where it left off and "
4417	    "without signal to be sent\n");
4418	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4419
4420	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4421	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4422
4423	validate_status_exited(status, exitval);
4424
4425	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4426	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4427}
4428#endif
4429
4430#if defined(PT_STEP)
4431static void
4432ptrace_step(int N, int setstep)
4433{
4434	const int exitval = 5;
4435	const int sigval = SIGSTOP;
4436	pid_t child, wpid;
4437#if defined(TWAIT_HAVE_STATUS)
4438	int status;
4439#endif
4440	int happy;
4441
4442#if defined(__arm__)
4443	/* PT_STEP not supported on arm 32-bit */
4444	atf_tc_expect_fail("PR kern/52119");
4445#endif
4446
4447	DPRINTF("Before forking process PID=%d\n", getpid());
4448	SYSCALL_REQUIRE((child = fork()) != -1);
4449	if (child == 0) {
4450		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4451		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4452
4453		happy = check_happy(999);
4454
4455		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4456		FORKEE_ASSERT(raise(sigval) == 0);
4457
4458		FORKEE_ASSERT_EQ(happy, check_happy(999));
4459
4460		DPRINTF("Before exiting of the child process\n");
4461		_exit(exitval);
4462	}
4463	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4464
4465	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4466	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4467
4468	validate_status_stopped(status, sigval);
4469
4470	while (N --> 0) {
4471		if (setstep) {
4472			DPRINTF("Before resuming the child process where it "
4473			    "left off and without signal to be sent (use "
4474			    "PT_SETSTEP and PT_CONTINUE)\n");
4475			SYSCALL_REQUIRE(ptrace(PT_SETSTEP, child, 0, 0) != -1);
4476			SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0)
4477			    != -1);
4478		} else {
4479			DPRINTF("Before resuming the child process where it "
4480			    "left off and without signal to be sent (use "
4481			    "PT_STEP)\n");
4482			SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0)
4483			    != -1);
4484		}
4485
4486		DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4487		TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0),
4488		    child);
4489
4490		validate_status_stopped(status, SIGTRAP);
4491
4492		if (setstep) {
4493			SYSCALL_REQUIRE(ptrace(PT_CLEARSTEP, child, 0, 0) != -1);
4494		}
4495	}
4496
4497	DPRINTF("Before resuming the child process where it left off and "
4498	    "without signal to be sent\n");
4499	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4500
4501	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4502	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4503
4504	validate_status_exited(status, exitval);
4505
4506	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4507	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4508}
4509#endif
4510
4511#if defined(PT_STEP)
4512ATF_TC(step1);
4513ATF_TC_HEAD(step1, tc)
4514{
4515	atf_tc_set_md_var(tc, "descr",
4516	    "Verify single PT_STEP call");
4517}
4518
4519ATF_TC_BODY(step1, tc)
4520{
4521	ptrace_step(1, 0);
4522}
4523#endif
4524
4525#if defined(PT_STEP)
4526ATF_TC(step2);
4527ATF_TC_HEAD(step2, tc)
4528{
4529	atf_tc_set_md_var(tc, "descr",
4530	    "Verify PT_STEP called twice");
4531}
4532
4533ATF_TC_BODY(step2, tc)
4534{
4535	ptrace_step(2, 0);
4536}
4537#endif
4538
4539#if defined(PT_STEP)
4540ATF_TC(step3);
4541ATF_TC_HEAD(step3, tc)
4542{
4543	atf_tc_set_md_var(tc, "descr",
4544	    "Verify PT_STEP called three times");
4545}
4546
4547ATF_TC_BODY(step3, tc)
4548{
4549	ptrace_step(3, 0);
4550}
4551#endif
4552
4553#if defined(PT_STEP)
4554ATF_TC(step4);
4555ATF_TC_HEAD(step4, tc)
4556{
4557	atf_tc_set_md_var(tc, "descr",
4558	    "Verify PT_STEP called four times");
4559}
4560
4561ATF_TC_BODY(step4, tc)
4562{
4563	ptrace_step(4, 0);
4564}
4565#endif
4566
4567#if defined(PT_STEP)
4568ATF_TC(setstep1);
4569ATF_TC_HEAD(setstep1, tc)
4570{
4571	atf_tc_set_md_var(tc, "descr",
4572	    "Verify single PT_SETSTEP call");
4573}
4574
4575ATF_TC_BODY(setstep1, tc)
4576{
4577	ptrace_step(1, 1);
4578}
4579#endif
4580
4581#if defined(PT_STEP)
4582ATF_TC(setstep2);
4583ATF_TC_HEAD(setstep2, tc)
4584{
4585	atf_tc_set_md_var(tc, "descr",
4586	    "Verify PT_SETSTEP called twice");
4587}
4588
4589ATF_TC_BODY(setstep2, tc)
4590{
4591	ptrace_step(2, 1);
4592}
4593#endif
4594
4595#if defined(PT_STEP)
4596ATF_TC(setstep3);
4597ATF_TC_HEAD(setstep3, tc)
4598{
4599	atf_tc_set_md_var(tc, "descr",
4600	    "Verify PT_SETSTEP called three times");
4601}
4602
4603ATF_TC_BODY(setstep3, tc)
4604{
4605	ptrace_step(3, 1);
4606}
4607#endif
4608
4609#if defined(PT_STEP)
4610ATF_TC(setstep4);
4611ATF_TC_HEAD(setstep4, tc)
4612{
4613	atf_tc_set_md_var(tc, "descr",
4614	    "Verify PT_SETSTEP called four times");
4615}
4616
4617ATF_TC_BODY(setstep4, tc)
4618{
4619	ptrace_step(4, 1);
4620}
4621#endif
4622
4623ATF_TC(kill1);
4624ATF_TC_HEAD(kill1, tc)
4625{
4626	atf_tc_set_md_var(tc, "descr",
4627	    "Verify that PT_CONTINUE with SIGKILL terminates child");
4628}
4629
4630ATF_TC_BODY(kill1, tc)
4631{
4632	const int sigval = SIGSTOP, sigsent = SIGKILL;
4633	pid_t child, wpid;
4634#if defined(TWAIT_HAVE_STATUS)
4635	int status;
4636#endif
4637
4638	DPRINTF("Before forking process PID=%d\n", getpid());
4639	SYSCALL_REQUIRE((child = fork()) != -1);
4640	if (child == 0) {
4641		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4642		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4643
4644		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4645		FORKEE_ASSERT(raise(sigval) == 0);
4646
4647		/* NOTREACHED */
4648		FORKEE_ASSERTX(0 &&
4649		    "Child should be terminated by a signal from its parent");
4650	}
4651	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4652
4653	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4654	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4655
4656	validate_status_stopped(status, sigval);
4657
4658	DPRINTF("Before resuming the child process where it left off and "
4659	    "without signal to be sent\n");
4660	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigsent) != -1);
4661
4662	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4663	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4664
4665	validate_status_signaled(status, sigsent, 0);
4666
4667	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4668	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4669}
4670
4671ATF_TC(kill2);
4672ATF_TC_HEAD(kill2, tc)
4673{
4674	atf_tc_set_md_var(tc, "descr",
4675	    "Verify that PT_KILL terminates child");
4676}
4677
4678ATF_TC_BODY(kill2, tc)
4679{
4680	const int sigval = SIGSTOP;
4681	pid_t child, wpid;
4682#if defined(TWAIT_HAVE_STATUS)
4683	int status;
4684#endif
4685
4686	DPRINTF("Before forking process PID=%d\n", getpid());
4687	SYSCALL_REQUIRE((child = fork()) != -1);
4688	if (child == 0) {
4689		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4690		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4691
4692		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4693		FORKEE_ASSERT(raise(sigval) == 0);
4694
4695		/* NOTREACHED */
4696		FORKEE_ASSERTX(0 &&
4697		    "Child should be terminated by a signal from its parent");
4698	}
4699	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4700
4701	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4702	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4703
4704	validate_status_stopped(status, sigval);
4705
4706	DPRINTF("Before resuming the child process where it left off and "
4707	    "without signal to be sent\n");
4708	SYSCALL_REQUIRE(ptrace(PT_KILL, child, (void*)1, 0) != -1);
4709
4710	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4711	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4712
4713	validate_status_signaled(status, SIGKILL, 0);
4714
4715	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4716	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4717}
4718
4719ATF_TC(lwpinfo1);
4720ATF_TC_HEAD(lwpinfo1, tc)
4721{
4722	atf_tc_set_md_var(tc, "descr",
4723	    "Verify basic LWPINFO call for single thread (PT_TRACE_ME)");
4724}
4725
4726ATF_TC_BODY(lwpinfo1, tc)
4727{
4728	const int exitval = 5;
4729	const int sigval = SIGSTOP;
4730	pid_t child, wpid;
4731#if defined(TWAIT_HAVE_STATUS)
4732	int status;
4733#endif
4734	struct ptrace_lwpinfo info = {0, 0};
4735
4736	DPRINTF("Before forking process PID=%d\n", getpid());
4737	SYSCALL_REQUIRE((child = fork()) != -1);
4738	if (child == 0) {
4739		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4740		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4741
4742		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4743		FORKEE_ASSERT(raise(sigval) == 0);
4744
4745		DPRINTF("Before exiting of the child process\n");
4746		_exit(exitval);
4747	}
4748	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4749
4750	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4751	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4752
4753	validate_status_stopped(status, sigval);
4754
4755	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4756	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4757
4758	DPRINTF("Assert that there exists a thread\n");
4759	ATF_REQUIRE(info.pl_lwpid > 0);
4760
4761	DPRINTF("Assert that lwp thread %d received event PL_EVENT_SIGNAL\n",
4762	    info.pl_lwpid);
4763	ATF_REQUIRE_EQ_MSG(info.pl_event, PL_EVENT_SIGNAL,
4764	    "Received event %d != expected event %d",
4765	    info.pl_event, PL_EVENT_SIGNAL);
4766
4767	DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4768	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &info, sizeof(info)) != -1);
4769
4770	DPRINTF("Assert that there are no more lwp threads in child\n");
4771	ATF_REQUIRE_EQ(info.pl_lwpid, 0);
4772
4773	DPRINTF("Before resuming the child process where it left off and "
4774	    "without signal to be sent\n");
4775	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4776
4777	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4778	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4779
4780	validate_status_exited(status, exitval);
4781
4782	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4783	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4784}
4785
4786#if defined(TWAIT_HAVE_PID)
4787ATF_TC(lwpinfo2);
4788ATF_TC_HEAD(lwpinfo2, tc)
4789{
4790	atf_tc_set_md_var(tc, "descr",
4791	    "Verify basic LWPINFO call for single thread (PT_ATTACH from "
4792	    "tracer)");
4793}
4794
4795ATF_TC_BODY(lwpinfo2, tc)
4796{
4797	struct msg_fds parent_tracee, parent_tracer;
4798	const int exitval_tracee = 5;
4799	const int exitval_tracer = 10;
4800	pid_t tracee, tracer, wpid;
4801	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
4802#if defined(TWAIT_HAVE_STATUS)
4803	int status;
4804#endif
4805	struct ptrace_lwpinfo info = {0, 0};
4806
4807	DPRINTF("Spawn tracee\n");
4808	SYSCALL_REQUIRE(msg_open(&parent_tracee) == 0);
4809	SYSCALL_REQUIRE(msg_open(&parent_tracer) == 0);
4810	tracee = atf_utils_fork();
4811	if (tracee == 0) {
4812
4813		/* Wait for message from the parent */
4814		CHILD_TO_PARENT("tracee ready", parent_tracee, msg);
4815		CHILD_FROM_PARENT("tracee exit", parent_tracee, msg);
4816
4817		_exit(exitval_tracee);
4818	}
4819	PARENT_FROM_CHILD("tracee ready", parent_tracee, msg);
4820
4821	DPRINTF("Spawn debugger\n");
4822	tracer = atf_utils_fork();
4823	if (tracer == 0) {
4824		/* No IPC to communicate with the child */
4825		DPRINTF("Before calling PT_ATTACH from tracee %d\n", getpid());
4826		FORKEE_ASSERT(ptrace(PT_ATTACH, tracee, NULL, 0) != -1);
4827
4828		/* Wait for tracee and assert that it was stopped w/ SIGSTOP */
4829		FORKEE_REQUIRE_SUCCESS(
4830		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4831
4832		forkee_status_stopped(status, SIGSTOP);
4833
4834		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4835		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4836		    != -1);
4837
4838		DPRINTF("Assert that there exists a thread\n");
4839		FORKEE_ASSERTX(info.pl_lwpid > 0);
4840
4841		DPRINTF("Assert that lwp thread %d received event "
4842		    "PL_EVENT_SIGNAL\n", info.pl_lwpid);
4843		FORKEE_ASSERT_EQ(info.pl_event, PL_EVENT_SIGNAL);
4844
4845		DPRINTF("Before calling ptrace(2) with PT_LWPINFO for child\n");
4846		FORKEE_ASSERT(ptrace(PT_LWPINFO, tracee, &info, sizeof(info))
4847		    != -1);
4848
4849		DPRINTF("Assert that there are no more lwp threads in child\n");
4850		FORKEE_ASSERTX(info.pl_lwpid == 0);
4851
4852		/* Resume tracee with PT_CONTINUE */
4853		FORKEE_ASSERT(ptrace(PT_CONTINUE, tracee, (void *)1, 0) != -1);
4854
4855		/* Inform parent that tracer has attached to tracee */
4856		CHILD_TO_PARENT("tracer ready", parent_tracer, msg);
4857		/* Wait for parent */
4858		CHILD_FROM_PARENT("tracer wait", parent_tracer, msg);
4859
4860		/* Wait for tracee and assert that it exited */
4861		FORKEE_REQUIRE_SUCCESS(
4862		    wpid = TWAIT_GENERIC(tracee, &status, 0), tracee);
4863
4864		forkee_status_exited(status, exitval_tracee);
4865
4866		DPRINTF("Before exiting of the tracer process\n");
4867		_exit(exitval_tracer);
4868	}
4869
4870	DPRINTF("Wait for the tracer to attach to the tracee\n");
4871	PARENT_FROM_CHILD("tracer ready", parent_tracer, msg);
4872
4873	DPRINTF("Resume the tracee and let it exit\n");
4874	PARENT_TO_CHILD("tracee exit", parent_tracee, msg);
4875
4876	DPRINTF("Detect that tracee is zombie\n");
4877	await_zombie(tracee);
4878
4879	DPRINTF("Assert that there is no status about tracee - "
4880	    "Tracer must detect zombie first - calling %s()\n", TWAIT_FNAME);
4881	TWAIT_REQUIRE_SUCCESS(
4882	    wpid = TWAIT_GENERIC(tracee, &status, WNOHANG), 0);
4883
4884	DPRINTF("Resume the tracer and let it detect exited tracee\n");
4885	PARENT_TO_CHILD("tracer wait", parent_tracer, msg);
4886
4887	DPRINTF("Wait for tracer to finish its job and exit - calling %s()\n",
4888	    TWAIT_FNAME);
4889	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracer, &status, 0),
4890	    tracer);
4891
4892	validate_status_exited(status, exitval_tracer);
4893
4894	DPRINTF("Wait for tracee to finish its job and exit - calling %s()\n",
4895	    TWAIT_FNAME);
4896	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(tracee, &status, WNOHANG),
4897	    tracee);
4898
4899	validate_status_exited(status, exitval_tracee);
4900
4901	msg_close(&parent_tracer);
4902	msg_close(&parent_tracee);
4903}
4904#endif
4905
4906ATF_TC(siginfo1);
4907ATF_TC_HEAD(siginfo1, tc)
4908{
4909	atf_tc_set_md_var(tc, "descr",
4910	    "Verify basic PT_GET_SIGINFO call for SIGTRAP from tracee");
4911}
4912
4913ATF_TC_BODY(siginfo1, tc)
4914{
4915	const int exitval = 5;
4916	const int sigval = SIGTRAP;
4917	pid_t child, wpid;
4918#if defined(TWAIT_HAVE_STATUS)
4919	int status;
4920#endif
4921	struct ptrace_siginfo info;
4922	memset(&info, 0, sizeof(info));
4923
4924	DPRINTF("Before forking process PID=%d\n", getpid());
4925	SYSCALL_REQUIRE((child = fork()) != -1);
4926	if (child == 0) {
4927		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4928		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4929
4930		DPRINTF("Before raising %s from child\n", strsignal(sigval));
4931		FORKEE_ASSERT(raise(sigval) == 0);
4932
4933		DPRINTF("Before exiting of the child process\n");
4934		_exit(exitval);
4935	}
4936	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
4937
4938	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4939	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4940
4941	validate_status_stopped(status, sigval);
4942
4943	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
4944	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
4945
4946	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
4947	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
4948	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
4949	    info.psi_siginfo.si_errno);
4950
4951	DPRINTF("Before resuming the child process where it left off and "
4952	    "without signal to be sent\n");
4953	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
4954
4955	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4956	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
4957
4958	validate_status_exited(status, exitval);
4959
4960	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
4961	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
4962}
4963
4964ATF_TC(siginfo2);
4965ATF_TC_HEAD(siginfo2, tc)
4966{
4967	atf_tc_set_md_var(tc, "descr",
4968	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls without "
4969	    "modification of SIGINT from tracee");
4970}
4971
4972static int siginfo2_caught = 0;
4973
4974static void
4975siginfo2_sighandler(int sig)
4976{
4977	FORKEE_ASSERT_EQ(sig, SIGINT);
4978
4979	++siginfo2_caught;
4980}
4981
4982ATF_TC_BODY(siginfo2, tc)
4983{
4984	const int exitval = 5;
4985	const int sigval = SIGINT;
4986	pid_t child, wpid;
4987	struct sigaction sa;
4988#if defined(TWAIT_HAVE_STATUS)
4989	int status;
4990#endif
4991	struct ptrace_siginfo info;
4992	memset(&info, 0, sizeof(info));
4993
4994	DPRINTF("Before forking process PID=%d\n", getpid());
4995	SYSCALL_REQUIRE((child = fork()) != -1);
4996	if (child == 0) {
4997		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
4998		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
4999
5000		sa.sa_handler = siginfo2_sighandler;
5001		sa.sa_flags = SA_SIGINFO;
5002		sigemptyset(&sa.sa_mask);
5003
5004		FORKEE_ASSERT(sigaction(sigval, &sa, NULL) != -1);
5005
5006		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5007		FORKEE_ASSERT(raise(sigval) == 0);
5008
5009		FORKEE_ASSERT_EQ(siginfo2_caught, 1);
5010
5011		DPRINTF("Before exiting of the child process\n");
5012		_exit(exitval);
5013	}
5014	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5015
5016	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5017	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5018
5019	validate_status_stopped(status, sigval);
5020
5021	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5022	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5023
5024	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5025	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5026	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5027	    info.psi_siginfo.si_errno);
5028
5029	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
5030	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
5031
5032	DPRINTF("Before resuming the child process where it left off and "
5033	    "without signal to be sent\n");
5034	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigval) != -1);
5035
5036	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5037	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5038
5039	validate_status_exited(status, exitval);
5040
5041	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5042	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5043}
5044
5045ATF_TC(siginfo3);
5046ATF_TC_HEAD(siginfo3, tc)
5047{
5048	atf_tc_set_md_var(tc, "descr",
5049	    "Verify basic PT_GET_SIGINFO and PT_SET_SIGINFO calls with "
5050	    "setting signal to new value");
5051}
5052
5053static int siginfo3_caught = 0;
5054
5055static void
5056siginfo3_sigaction(int sig, siginfo_t *info, void *ctx)
5057{
5058	FORKEE_ASSERT_EQ(sig, SIGTRAP);
5059
5060	FORKEE_ASSERT_EQ(info->si_signo, SIGTRAP);
5061	FORKEE_ASSERT_EQ(info->si_code, TRAP_BRKPT);
5062
5063	++siginfo3_caught;
5064}
5065
5066ATF_TC_BODY(siginfo3, tc)
5067{
5068	const int exitval = 5;
5069	const int sigval = SIGINT;
5070	const int sigfaked = SIGTRAP;
5071	const int sicodefaked = TRAP_BRKPT;
5072	pid_t child, wpid;
5073	struct sigaction sa;
5074#if defined(TWAIT_HAVE_STATUS)
5075	int status;
5076#endif
5077	struct ptrace_siginfo info;
5078	memset(&info, 0, sizeof(info));
5079
5080	DPRINTF("Before forking process PID=%d\n", getpid());
5081	SYSCALL_REQUIRE((child = fork()) != -1);
5082	if (child == 0) {
5083		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5084		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5085
5086		sa.sa_sigaction = siginfo3_sigaction;
5087		sa.sa_flags = SA_SIGINFO;
5088		sigemptyset(&sa.sa_mask);
5089
5090		FORKEE_ASSERT(sigaction(sigfaked, &sa, NULL) != -1);
5091
5092		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5093		FORKEE_ASSERT(raise(sigval) == 0);
5094
5095		FORKEE_ASSERT_EQ(siginfo3_caught, 1);
5096
5097		DPRINTF("Before exiting of the child process\n");
5098		_exit(exitval);
5099	}
5100	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5101
5102	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5103	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5104
5105	validate_status_stopped(status, sigval);
5106
5107	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5108	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5109
5110	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5111	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5112	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5113	    info.psi_siginfo.si_errno);
5114
5115	DPRINTF("Before setting new faked signal to signo=%d si_code=%d\n",
5116	    sigfaked, sicodefaked);
5117	info.psi_siginfo.si_signo = sigfaked;
5118	info.psi_siginfo.si_code = sicodefaked;
5119
5120	DPRINTF("Before calling ptrace(2) with PT_SET_SIGINFO for child\n");
5121	SYSCALL_REQUIRE(ptrace(PT_SET_SIGINFO, child, &info, sizeof(info)) != -1);
5122
5123	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5124	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5125
5126	DPRINTF("Before checking siginfo_t\n");
5127	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigfaked);
5128	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, sicodefaked);
5129
5130	DPRINTF("Before resuming the child process where it left off and "
5131	    "without signal to be sent\n");
5132	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, sigfaked) != -1);
5133
5134	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5135	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5136
5137	validate_status_exited(status, exitval);
5138
5139	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5140	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5141}
5142
5143ATF_TC(siginfo4);
5144ATF_TC_HEAD(siginfo4, tc)
5145{
5146	atf_tc_set_md_var(tc, "descr",
5147	    "Detect SIGTRAP TRAP_EXEC from tracee");
5148}
5149
5150ATF_TC_BODY(siginfo4, tc)
5151{
5152	const int sigval = SIGTRAP;
5153	pid_t child, wpid;
5154#if defined(TWAIT_HAVE_STATUS)
5155	int status;
5156#endif
5157
5158	struct ptrace_siginfo info;
5159	memset(&info, 0, sizeof(info));
5160
5161	DPRINTF("Before forking process PID=%d\n", getpid());
5162	SYSCALL_REQUIRE((child = fork()) != -1);
5163	if (child == 0) {
5164		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5165		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5166
5167		DPRINTF("Before calling execve(2) from child\n");
5168		execlp("/bin/echo", "/bin/echo", NULL);
5169
5170		FORKEE_ASSERT(0 && "Not reached");
5171	}
5172	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5173
5174	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5175	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5176
5177	validate_status_stopped(status, sigval);
5178
5179	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5180	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5181
5182	DPRINTF("Signal traced to lwpid=%d\n", info.psi_lwpid);
5183	DPRINTF("Signal properties: si_signo=%#x si_code=%#x si_errno=%#x\n",
5184	    info.psi_siginfo.si_signo, info.psi_siginfo.si_code,
5185	    info.psi_siginfo.si_errno);
5186
5187	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5188	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_EXEC);
5189
5190	DPRINTF("Before resuming the child process where it left off and "
5191	    "without signal to be sent\n");
5192	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5193
5194	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5195	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5196
5197	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5198	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5199}
5200
5201#if defined(TWAIT_HAVE_PID)
5202ATF_TC(siginfo5);
5203ATF_TC_HEAD(siginfo5, tc)
5204{
5205	atf_tc_set_md_var(tc, "descr",
5206	    "Verify that fork(2) is intercepted by ptrace(2) with EVENT_MASK "
5207	    "set to PTRACE_FORK and reports correct signal information");
5208}
5209
5210ATF_TC_BODY(siginfo5, tc)
5211{
5212	const int exitval = 5;
5213	const int exitval2 = 15;
5214	const int sigval = SIGSTOP;
5215	pid_t child, child2, wpid;
5216#if defined(TWAIT_HAVE_STATUS)
5217	int status;
5218#endif
5219	ptrace_state_t state;
5220	const int slen = sizeof(state);
5221	ptrace_event_t event;
5222	const int elen = sizeof(event);
5223	struct ptrace_siginfo info;
5224
5225	memset(&info, 0, sizeof(info));
5226
5227	DPRINTF("Before forking process PID=%d\n", getpid());
5228	SYSCALL_REQUIRE((child = fork()) != -1);
5229	if (child == 0) {
5230		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5231		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5232
5233		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5234		FORKEE_ASSERT(raise(sigval) == 0);
5235
5236		FORKEE_ASSERT((child2 = fork()) != -1);
5237
5238		if (child2 == 0)
5239			_exit(exitval2);
5240
5241		FORKEE_REQUIRE_SUCCESS
5242		    (wpid = TWAIT_GENERIC(child2, &status, 0), child2);
5243
5244		forkee_status_exited(status, exitval2);
5245
5246		DPRINTF("Before exiting of the child process\n");
5247		_exit(exitval);
5248	}
5249	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5250
5251	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5252	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5253
5254	validate_status_stopped(status, sigval);
5255
5256	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5257	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5258
5259	DPRINTF("Before checking siginfo_t\n");
5260	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5261	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5262
5263	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
5264	event.pe_set_event = PTRACE_FORK;
5265	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5266
5267	DPRINTF("Before resuming the child process where it left off and "
5268	    "without signal to be sent\n");
5269        DPRINTF("We expect two SIGTRAP events, for child %d (TRAP_CHLD, "
5270               "pe_report_event=PTRACE_FORK, state.pe_other_pid=child2) and "
5271               "for child2 (TRAP_CHLD, pe_report_event=PTRACE_FORK, "
5272                "state.pe_other_pid=child)\n", child);
5273	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5274
5275	DPRINTF("Before calling %s() for the child %d\n", TWAIT_FNAME, child);
5276	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5277
5278	validate_status_stopped(status, SIGTRAP);
5279
5280	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5281	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5282
5283	DPRINTF("Before checking siginfo_t\n");
5284	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5285	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5286
5287	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5288	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5289
5290	child2 = state.pe_other_pid;
5291	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
5292
5293	DPRINTF("Before calling %s() for the forkee %d of the child %d\n",
5294	    TWAIT_FNAME, child2, child);
5295	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5296	    child2);
5297
5298	validate_status_stopped(status, SIGTRAP);
5299
5300	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5301	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5302
5303	DPRINTF("Before checking siginfo_t\n");
5304	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5305	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_CHLD);
5306
5307	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
5308	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
5309	ATF_REQUIRE_EQ(state.pe_other_pid, child);
5310
5311	DPRINTF("Before resuming the forkee process where it left off and "
5312	    "without signal to be sent\n");
5313	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
5314
5315	DPRINTF("Before resuming the child process where it left off and "
5316	    "without signal to be sent\n");
5317	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5318
5319	DPRINTF("Before calling %s() for the forkee - expected exited\n",
5320	    TWAIT_FNAME);
5321	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
5322	    child2);
5323
5324	validate_status_exited(status, exitval2);
5325
5326	DPRINTF("Before calling %s() for the forkee - expected no process\n",
5327	    TWAIT_FNAME);
5328	TWAIT_REQUIRE_FAILURE(ECHILD,
5329	    wpid = TWAIT_GENERIC(child2, &status, 0));
5330
5331	DPRINTF("Before calling %s() for the child - expected stopped "
5332	    "SIGCHLD\n", TWAIT_FNAME);
5333	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5334
5335	validate_status_stopped(status, SIGCHLD);
5336
5337	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5338	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5339
5340	DPRINTF("Before checking siginfo_t\n");
5341	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGCHLD);
5342	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, CLD_EXITED);
5343
5344	DPRINTF("Before resuming the child process where it left off and "
5345	    "without signal to be sent\n");
5346	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5347
5348	DPRINTF("Before calling %s() for the child - expected exited\n",
5349	    TWAIT_FNAME);
5350	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5351
5352	validate_status_exited(status, exitval);
5353
5354	DPRINTF("Before calling %s() for the child - expected no process\n",
5355	    TWAIT_FNAME);
5356	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5357}
5358#endif
5359
5360#if defined(PT_STEP)
5361ATF_TC(siginfo6);
5362ATF_TC_HEAD(siginfo6, tc)
5363{
5364	atf_tc_set_md_var(tc, "descr",
5365	    "Verify single PT_STEP call with signal information check");
5366}
5367
5368ATF_TC_BODY(siginfo6, tc)
5369{
5370	const int exitval = 5;
5371	const int sigval = SIGSTOP;
5372	pid_t child, wpid;
5373#if defined(TWAIT_HAVE_STATUS)
5374	int status;
5375#endif
5376	int happy;
5377	struct ptrace_siginfo info;
5378
5379#if defined(__arm__)
5380	/* PT_STEP not supported on arm 32-bit */
5381	atf_tc_expect_fail("PR kern/52119");
5382#endif
5383
5384	memset(&info, 0, sizeof(info));
5385
5386	DPRINTF("Before forking process PID=%d\n", getpid());
5387	SYSCALL_REQUIRE((child = fork()) != -1);
5388	if (child == 0) {
5389		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5390		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5391
5392		happy = check_happy(100);
5393
5394		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5395		FORKEE_ASSERT(raise(sigval) == 0);
5396
5397		FORKEE_ASSERT_EQ(happy, check_happy(100));
5398
5399		DPRINTF("Before exiting of the child process\n");
5400		_exit(exitval);
5401	}
5402	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5403
5404	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5405	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5406
5407	validate_status_stopped(status, sigval);
5408
5409	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5410	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5411
5412	DPRINTF("Before checking siginfo_t\n");
5413	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, sigval);
5414	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, SI_LWP);
5415
5416	DPRINTF("Before resuming the child process where it left off and "
5417	    "without signal to be sent (use PT_STEP)\n");
5418	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5419
5420	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5421	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5422
5423	validate_status_stopped(status, SIGTRAP);
5424
5425	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
5426	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
5427
5428	DPRINTF("Before checking siginfo_t\n");
5429	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
5430	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_TRACE);
5431
5432	DPRINTF("Before resuming the child process where it left off and "
5433	    "without signal to be sent\n");
5434	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5435
5436	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5437	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5438
5439	validate_status_exited(status, exitval);
5440
5441	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5442	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5443}
5444#endif
5445
5446volatile lwpid_t the_lwp_id = 0;
5447
5448static void
5449lwp_main_func(void *arg)
5450{
5451	the_lwp_id = _lwp_self();
5452	_lwp_exit();
5453}
5454
5455ATF_TC(lwp_create1);
5456ATF_TC_HEAD(lwp_create1, tc)
5457{
5458	atf_tc_set_md_var(tc, "descr",
5459	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5460	    "EVENT_MASK set to PTRACE_LWP_CREATE");
5461}
5462
5463ATF_TC_BODY(lwp_create1, tc)
5464{
5465	const int exitval = 5;
5466	const int sigval = SIGSTOP;
5467	pid_t child, wpid;
5468#if defined(TWAIT_HAVE_STATUS)
5469	int status;
5470#endif
5471	ptrace_state_t state;
5472	const int slen = sizeof(state);
5473	ptrace_event_t event;
5474	const int elen = sizeof(event);
5475	ucontext_t uc;
5476	lwpid_t lid;
5477	static const size_t ssize = 16*1024;
5478	void *stack;
5479
5480	DPRINTF("Before forking process PID=%d\n", getpid());
5481	SYSCALL_REQUIRE((child = fork()) != -1);
5482	if (child == 0) {
5483		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5484		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5485
5486		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5487		FORKEE_ASSERT(raise(sigval) == 0);
5488
5489		DPRINTF("Before allocating memory for stack in child\n");
5490		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5491
5492		DPRINTF("Before making context for new lwp in child\n");
5493		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5494
5495		DPRINTF("Before creating new in child\n");
5496		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5497
5498		DPRINTF("Before waiting for lwp %d to exit\n", lid);
5499		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5500
5501		DPRINTF("Before verifying that reported %d and running lid %d "
5502		    "are the same\n", lid, the_lwp_id);
5503		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5504
5505		DPRINTF("Before exiting of the child process\n");
5506		_exit(exitval);
5507	}
5508	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5509
5510	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5511	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5512
5513	validate_status_stopped(status, sigval);
5514
5515	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
5516	event.pe_set_event = PTRACE_LWP_CREATE;
5517	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5518
5519	DPRINTF("Before resuming the child process where it left off and "
5520	    "without signal to be sent\n");
5521	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5522
5523	DPRINTF("Before calling %s() for the child - expected stopped "
5524	    "SIGTRAP\n", TWAIT_FNAME);
5525	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5526
5527	validate_status_stopped(status, SIGTRAP);
5528
5529	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5530
5531	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
5532
5533	lid = state.pe_lwp;
5534	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
5535
5536	DPRINTF("Before resuming the child process where it left off and "
5537	    "without signal to be sent\n");
5538	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5539
5540	DPRINTF("Before calling %s() for the child - expected exited\n",
5541	    TWAIT_FNAME);
5542	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5543
5544	validate_status_exited(status, exitval);
5545
5546	DPRINTF("Before calling %s() for the child - expected no process\n",
5547	    TWAIT_FNAME);
5548	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5549}
5550
5551ATF_TC(lwp_exit1);
5552ATF_TC_HEAD(lwp_exit1, tc)
5553{
5554	atf_tc_set_md_var(tc, "descr",
5555	    "Verify that 1 LWP creation is intercepted by ptrace(2) with "
5556	    "EVENT_MASK set to PTRACE_LWP_EXIT");
5557}
5558
5559ATF_TC_BODY(lwp_exit1, tc)
5560{
5561	const int exitval = 5;
5562	const int sigval = SIGSTOP;
5563	pid_t child, wpid;
5564#if defined(TWAIT_HAVE_STATUS)
5565	int status;
5566#endif
5567	ptrace_state_t state;
5568	const int slen = sizeof(state);
5569	ptrace_event_t event;
5570	const int elen = sizeof(event);
5571	ucontext_t uc;
5572	lwpid_t lid;
5573	static const size_t ssize = 16*1024;
5574	void *stack;
5575
5576	DPRINTF("Before forking process PID=%d\n", getpid());
5577	SYSCALL_REQUIRE((child = fork()) != -1);
5578	if (child == 0) {
5579		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5580		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5581
5582		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5583		FORKEE_ASSERT(raise(sigval) == 0);
5584
5585		DPRINTF("Before allocating memory for stack in child\n");
5586		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
5587
5588		DPRINTF("Before making context for new lwp in child\n");
5589		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
5590
5591		DPRINTF("Before creating new in child\n");
5592		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
5593
5594		DPRINTF("Before waiting for lwp %d to exit\n", lid);
5595		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
5596
5597		DPRINTF("Before verifying that reported %d and running lid %d "
5598		    "are the same\n", lid, the_lwp_id);
5599		FORKEE_ASSERT_EQ(lid, the_lwp_id);
5600
5601		DPRINTF("Before exiting of the child process\n");
5602		_exit(exitval);
5603	}
5604	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5605
5606	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5607	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5608
5609	validate_status_stopped(status, sigval);
5610
5611	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
5612	event.pe_set_event = PTRACE_LWP_EXIT;
5613	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
5614
5615	DPRINTF("Before resuming the child process where it left off and "
5616	    "without signal to be sent\n");
5617	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5618
5619	DPRINTF("Before calling %s() for the child - expected stopped "
5620	    "SIGTRAP\n", TWAIT_FNAME);
5621	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5622
5623	validate_status_stopped(status, SIGTRAP);
5624
5625	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
5626
5627	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
5628
5629	lid = state.pe_lwp;
5630	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
5631
5632	DPRINTF("Before resuming the child process where it left off and "
5633	    "without signal to be sent\n");
5634	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5635
5636	DPRINTF("Before calling %s() for the child - expected exited\n",
5637	    TWAIT_FNAME);
5638	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5639
5640	validate_status_exited(status, exitval);
5641
5642	DPRINTF("Before calling %s() for the child - expected no process\n",
5643	    TWAIT_FNAME);
5644	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5645}
5646
5647ATF_TC(signal1);
5648ATF_TC_HEAD(signal1, tc)
5649{
5650	atf_tc_set_md_var(tc, "descr",
5651	    "Verify that masking single unrelated signal does not stop tracer "
5652	    "from catching other signals");
5653}
5654
5655ATF_TC_BODY(signal1, tc)
5656{
5657	const int exitval = 5;
5658	const int sigval = SIGSTOP;
5659	const int sigmasked = SIGTRAP;
5660	const int signotmasked = SIGINT;
5661	pid_t child, wpid;
5662#if defined(TWAIT_HAVE_STATUS)
5663	int status;
5664#endif
5665	sigset_t intmask;
5666
5667	DPRINTF("Before forking process PID=%d\n", getpid());
5668	SYSCALL_REQUIRE((child = fork()) != -1);
5669	if (child == 0) {
5670		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5671		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5672
5673		sigemptyset(&intmask);
5674		sigaddset(&intmask, sigmasked);
5675		sigprocmask(SIG_BLOCK, &intmask, NULL);
5676
5677		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5678		FORKEE_ASSERT(raise(sigval) == 0);
5679
5680		DPRINTF("Before raising %s from child\n",
5681		    strsignal(signotmasked));
5682		FORKEE_ASSERT(raise(signotmasked) == 0);
5683
5684		DPRINTF("Before exiting of the child process\n");
5685		_exit(exitval);
5686	}
5687	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5688
5689	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5690	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5691
5692	validate_status_stopped(status, sigval);
5693
5694	DPRINTF("Before resuming the child process where it left off and "
5695	    "without signal to be sent\n");
5696	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5697
5698	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5699	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5700
5701	validate_status_stopped(status, signotmasked);
5702
5703	DPRINTF("Before resuming the child process where it left off and "
5704	    "without signal to be sent\n");
5705	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5706
5707	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5708	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5709
5710	validate_status_exited(status, exitval);
5711
5712	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5713	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5714}
5715
5716ATF_TC(signal2);
5717ATF_TC_HEAD(signal2, tc)
5718{
5719	atf_tc_set_md_var(tc, "descr",
5720	    "Verify that masking SIGTRAP in tracee stops tracer from "
5721	    "catching this raised signal");
5722}
5723
5724ATF_TC_BODY(signal2, tc)
5725{
5726	const int exitval = 5;
5727	const int sigval = SIGSTOP;
5728	const int sigmasked = SIGTRAP;
5729	pid_t child, wpid;
5730#if defined(TWAIT_HAVE_STATUS)
5731	int status;
5732#endif
5733	sigset_t intmask;
5734
5735	DPRINTF("Before forking process PID=%d\n", getpid());
5736	SYSCALL_REQUIRE((child = fork()) != -1);
5737	if (child == 0) {
5738		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5739		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5740
5741		sigemptyset(&intmask);
5742		sigaddset(&intmask, sigmasked);
5743		sigprocmask(SIG_BLOCK, &intmask, NULL);
5744
5745		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5746		FORKEE_ASSERT(raise(sigval) == 0);
5747
5748		DPRINTF("Before raising %s breakpoint from child\n",
5749		    strsignal(sigmasked));
5750		FORKEE_ASSERT(raise(sigmasked) == 0);
5751
5752		DPRINTF("Before exiting of the child process\n");
5753		_exit(exitval);
5754	}
5755	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5756
5757	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5758	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5759
5760	validate_status_stopped(status, sigval);
5761
5762	DPRINTF("Before resuming the child process where it left off and "
5763	    "without signal to be sent\n");
5764	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5765
5766	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5767	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5768
5769	validate_status_exited(status, exitval);
5770
5771	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5772	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5773}
5774
5775ATF_TC(signal3);
5776ATF_TC_HEAD(signal3, tc)
5777{
5778	atf_tc_set_md_var(tc, "timeout", "5");
5779	atf_tc_set_md_var(tc, "descr",
5780	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5781	    "catching software breakpoints");
5782}
5783
5784ATF_TC_BODY(signal3, tc)
5785{
5786	const int exitval = 5;
5787	const int sigval = SIGSTOP;
5788	const int sigmasked = SIGTRAP;
5789	pid_t child, wpid;
5790#if defined(TWAIT_HAVE_STATUS)
5791	int status;
5792#endif
5793	sigset_t intmask;
5794
5795	atf_tc_expect_fail("PR kern/51918");
5796
5797	// This test breaks now on some ports, temporarily disable it
5798	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
5799
5800#if defined(__sparc__)
5801	atf_tc_expect_timeout("PR kern/52167");
5802
5803	// timeout wins, failure still valid
5804	// atf_tc_expect_fail("PR kern/51918");
5805#endif
5806
5807	DPRINTF("Before forking process PID=%d\n", getpid());
5808	SYSCALL_REQUIRE((child = fork()) != -1);
5809	if (child == 0) {
5810		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5811		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5812
5813		sigemptyset(&intmask);
5814		sigaddset(&intmask, sigmasked);
5815		sigprocmask(SIG_BLOCK, &intmask, NULL);
5816
5817		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5818		FORKEE_ASSERT(raise(sigval) == 0);
5819
5820		DPRINTF("Before raising software breakpoint from child\n");
5821
5822#ifdef PTRACE_BREAKPOINT_ASM
5823		PTRACE_BREAKPOINT_ASM;
5824#else
5825		/* port me */
5826#endif
5827
5828		DPRINTF("Before exiting of the child process\n");
5829		_exit(exitval);
5830	}
5831	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5832
5833	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5834	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5835
5836	validate_status_stopped(status, sigval);
5837
5838	DPRINTF("Before resuming the child process where it left off and "
5839	    "without signal to be sent\n");
5840	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5841
5842	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5843	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5844
5845	validate_status_stopped(status, sigmasked);
5846
5847	DPRINTF("Before resuming the child process where it left off and "
5848	    "without signal to be sent\n");
5849	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5850
5851	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5852	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5853
5854	validate_status_exited(status, exitval);
5855
5856	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5857	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5858}
5859
5860#if defined(PT_STEP)
5861ATF_TC(signal4);
5862ATF_TC_HEAD(signal4, tc)
5863{
5864	atf_tc_set_md_var(tc, "descr",
5865	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5866	    "catching single step trap");
5867}
5868
5869ATF_TC_BODY(signal4, tc)
5870{
5871	const int exitval = 5;
5872	const int sigval = SIGSTOP;
5873	const int sigmasked = SIGTRAP;
5874	pid_t child, wpid;
5875#if defined(TWAIT_HAVE_STATUS)
5876	int status;
5877#endif
5878	sigset_t intmask;
5879	int happy;
5880
5881#if defined(__arm__)
5882	/* PT_STEP not supported on arm 32-bit */
5883	atf_tc_expect_fail("PR kern/51918 PR kern/52119");
5884#endif
5885
5886	DPRINTF("Before forking process PID=%d\n", getpid());
5887	SYSCALL_REQUIRE((child = fork()) != -1);
5888	if (child == 0) {
5889		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5890		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5891
5892		happy = check_happy(100);
5893
5894		sigemptyset(&intmask);
5895		sigaddset(&intmask, sigmasked);
5896		sigprocmask(SIG_BLOCK, &intmask, NULL);
5897
5898		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5899		FORKEE_ASSERT(raise(sigval) == 0);
5900
5901		FORKEE_ASSERT_EQ(happy, check_happy(100));
5902
5903		DPRINTF("Before exiting of the child process\n");
5904		_exit(exitval);
5905	}
5906	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5907
5908	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5909	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5910
5911	validate_status_stopped(status, sigval);
5912
5913	DPRINTF("Before resuming the child process where it left off and "
5914	    "without signal to be sent\n");
5915	SYSCALL_REQUIRE(ptrace(PT_STEP, child, (void *)1, 0) != -1);
5916
5917	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5918	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5919
5920	validate_status_stopped(status, sigmasked);
5921
5922	DPRINTF("Before resuming the child process where it left off and "
5923	    "without signal to be sent\n");
5924	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5925
5926	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5927	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5928
5929	validate_status_exited(status, exitval);
5930
5931	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5932	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
5933}
5934#endif
5935
5936ATF_TC(signal5);
5937ATF_TC_HEAD(signal5, tc)
5938{
5939	atf_tc_set_md_var(tc, "descr",
5940	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
5941	    "catching exec() breakpoint");
5942}
5943
5944ATF_TC_BODY(signal5, tc)
5945{
5946	const int exitval = 5;
5947	const int sigval = SIGSTOP;
5948	const int sigmasked = SIGTRAP;
5949	pid_t child, wpid;
5950#if defined(TWAIT_HAVE_STATUS)
5951	int status;
5952#endif
5953	sigset_t intmask;
5954
5955	atf_tc_expect_fail("wrong signal");
5956
5957	DPRINTF("Before forking process PID=%d\n", getpid());
5958	SYSCALL_REQUIRE((child = fork()) != -1);
5959	if (child == 0) {
5960		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
5961		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
5962
5963		sigemptyset(&intmask);
5964		sigaddset(&intmask, sigmasked);
5965		sigprocmask(SIG_BLOCK, &intmask, NULL);
5966
5967		DPRINTF("Before raising %s from child\n", strsignal(sigval));
5968		FORKEE_ASSERT(raise(sigval) == 0);
5969
5970		DPRINTF("Before calling execve(2) from child\n");
5971		execlp("/bin/echo", "/bin/echo", NULL);
5972
5973		DPRINTF("Before exiting of the child process\n");
5974		_exit(exitval);
5975	}
5976	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
5977
5978	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5979	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5980
5981	validate_status_stopped(status, sigval);
5982
5983	DPRINTF("Before resuming the child process where it left off and "
5984	    "without signal to be sent\n");
5985	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5986
5987	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5988	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5989
5990	validate_status_stopped(status, sigmasked);
5991
5992	DPRINTF("Before resuming the child process where it left off and "
5993	    "without signal to be sent\n");
5994	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
5995
5996	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
5997	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
5998
5999	validate_status_exited(status, exitval);
6000
6001	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6002	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6003}
6004
6005#if defined(TWAIT_HAVE_PID)
6006ATF_TC(signal6);
6007ATF_TC_HEAD(signal6, tc)
6008{
6009	atf_tc_set_md_var(tc, "timeout", "5");
6010	atf_tc_set_md_var(tc, "descr",
6011	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6012	    "catching PTRACE_FORK breakpoint");
6013}
6014
6015ATF_TC_BODY(signal6, tc)
6016{
6017	const int exitval = 5;
6018	const int exitval2 = 15;
6019	const int sigval = SIGSTOP;
6020	const int sigmasked = SIGTRAP;
6021	pid_t child, child2, wpid;
6022#if defined(TWAIT_HAVE_STATUS)
6023	int status;
6024#endif
6025	sigset_t intmask;
6026	ptrace_state_t state;
6027	const int slen = sizeof(state);
6028	ptrace_event_t event;
6029	const int elen = sizeof(event);
6030
6031	atf_tc_expect_timeout("PR kern/51918");
6032
6033	DPRINTF("Before forking process PID=%d\n", getpid());
6034	SYSCALL_REQUIRE((child = fork()) != -1);
6035	if (child == 0) {
6036		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6037		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6038
6039		sigemptyset(&intmask);
6040		sigaddset(&intmask, sigmasked);
6041		sigprocmask(SIG_BLOCK, &intmask, NULL);
6042
6043		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6044		FORKEE_ASSERT(raise(sigval) == 0);
6045
6046		FORKEE_ASSERT((child2 = fork()) != -1);
6047
6048		if (child2 == 0)
6049			_exit(exitval2);
6050
6051		FORKEE_REQUIRE_SUCCESS
6052			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6053
6054		forkee_status_exited(status, exitval2);
6055
6056		DPRINTF("Before exiting of the child process\n");
6057		_exit(exitval);
6058	}
6059	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6060
6061	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6062	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6063
6064	validate_status_stopped(status, sigval);
6065
6066	DPRINTF("Enable PTRACE_FORK in EVENT_MASK for the child %d\n", child);
6067	event.pe_set_event = PTRACE_FORK;
6068	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6069
6070	DPRINTF("Before resuming the child process where it left off and "
6071	    "without signal to be sent\n");
6072	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6073
6074	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6075	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6076
6077	validate_status_stopped(status, sigmasked);
6078
6079	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6080	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6081
6082	child2 = state.pe_other_pid;
6083	DPRINTF("Reported PTRACE_FORK event with forkee %d\n", child2);
6084
6085	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
6086	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6087	    child2);
6088
6089	validate_status_stopped(status, SIGTRAP);
6090
6091	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6092	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_FORK);
6093	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6094
6095	DPRINTF("Before resuming the forkee process where it left off and "
6096	    "without signal to be sent\n");
6097	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
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 forkee - expected exited\n",
6104	    TWAIT_FNAME);
6105	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6106	    child2);
6107
6108	validate_status_exited(status, exitval2);
6109
6110	DPRINTF("Before calling %s() for the forkee - expected no process\n",
6111	    TWAIT_FNAME);
6112	TWAIT_REQUIRE_FAILURE(ECHILD,
6113	    wpid = TWAIT_GENERIC(child2, &status, 0));
6114
6115	DPRINTF("Before calling %s() for the child - expected stopped "
6116	    "SIGCHLD\n", TWAIT_FNAME);
6117	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6118
6119	validate_status_stopped(status, SIGCHLD);
6120
6121	DPRINTF("Before resuming the child process where it left off and "
6122	    "without signal to be sent\n");
6123	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6124
6125	DPRINTF("Before calling %s() for the child - expected exited\n",
6126	    TWAIT_FNAME);
6127	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6128
6129	validate_status_exited(status, exitval);
6130
6131	DPRINTF("Before calling %s() for the child - expected no process\n",
6132	    TWAIT_FNAME);
6133	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6134}
6135#endif
6136
6137#if defined(TWAIT_HAVE_PID)
6138ATF_TC(signal7);
6139ATF_TC_HEAD(signal7, tc)
6140{
6141	atf_tc_set_md_var(tc, "descr",
6142	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6143	    "catching PTRACE_VFORK breakpoint");
6144}
6145
6146ATF_TC_BODY(signal7, tc)
6147{
6148	const int exitval = 5;
6149	const int exitval2 = 15;
6150	const int sigval = SIGSTOP;
6151	const int sigmasked = SIGTRAP;
6152	pid_t child, child2, wpid;
6153#if defined(TWAIT_HAVE_STATUS)
6154	int status;
6155#endif
6156	sigset_t intmask;
6157	ptrace_state_t state;
6158	const int slen = sizeof(state);
6159	ptrace_event_t event;
6160	const int elen = sizeof(event);
6161
6162	atf_tc_expect_fail("PR kern/51918 PR kern/51630");
6163
6164	DPRINTF("Before forking process PID=%d\n", getpid());
6165	SYSCALL_REQUIRE((child = fork()) != -1);
6166	if (child == 0) {
6167		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6168		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6169
6170		sigemptyset(&intmask);
6171		sigaddset(&intmask, sigmasked);
6172		sigprocmask(SIG_BLOCK, &intmask, NULL);
6173
6174		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6175		FORKEE_ASSERT(raise(sigval) == 0);
6176
6177		FORKEE_ASSERT((child2 = fork()) != -1);
6178
6179		if (child2 == 0)
6180			_exit(exitval2);
6181
6182		FORKEE_REQUIRE_SUCCESS
6183			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6184
6185		forkee_status_exited(status, exitval2);
6186
6187		DPRINTF("Before exiting of the child process\n");
6188		_exit(exitval);
6189	}
6190	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6191
6192	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6193	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6194
6195	validate_status_stopped(status, sigval);
6196
6197	DPRINTF("Enable PTRACE_VFORK in EVENT_MASK for the child %d\n", child);
6198	event.pe_set_event = PTRACE_VFORK;
6199	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1 || errno == ENOTSUP);
6200
6201	DPRINTF("Before resuming the child process where it left off and "
6202	    "without signal to be sent\n");
6203	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6204
6205	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6206	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6207
6208	validate_status_stopped(status, sigmasked);
6209
6210	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6211	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6212
6213	child2 = state.pe_other_pid;
6214	DPRINTF("Reported PTRACE_VFORK event with forkee %d\n", child2);
6215
6216	DPRINTF("Before calling %s() for the child2\n", TWAIT_FNAME);
6217	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6218	    child2);
6219
6220	validate_status_stopped(status, SIGTRAP);
6221
6222	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child2, &state, slen) != -1);
6223	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK);
6224	ATF_REQUIRE_EQ(state.pe_other_pid, child);
6225
6226	DPRINTF("Before resuming the forkee process where it left off and "
6227	    "without signal to be sent\n");
6228	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child2, (void *)1, 0) != -1);
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 forkee - expected exited\n",
6235	    TWAIT_FNAME);
6236	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child2, &status, 0),
6237	    child2);
6238
6239	validate_status_exited(status, exitval2);
6240
6241	DPRINTF("Before calling %s() for the forkee - expected no process\n",
6242	    TWAIT_FNAME);
6243	TWAIT_REQUIRE_FAILURE(ECHILD,
6244	    wpid = TWAIT_GENERIC(child2, &status, 0));
6245
6246	DPRINTF("Before calling %s() for the child - expected stopped "
6247	    "SIGCHLD\n", TWAIT_FNAME);
6248	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6249
6250	validate_status_stopped(status, SIGCHLD);
6251
6252	DPRINTF("Before resuming the child process where it left off and "
6253	    "without signal to be sent\n");
6254	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6255
6256	DPRINTF("Before calling %s() for the child - expected exited\n",
6257	    TWAIT_FNAME);
6258	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6259
6260	validate_status_exited(status, exitval);
6261
6262	DPRINTF("Before calling %s() for the child - expected no process\n",
6263	    TWAIT_FNAME);
6264	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6265}
6266#endif
6267
6268ATF_TC(signal8);
6269ATF_TC_HEAD(signal8, tc)
6270{
6271	atf_tc_set_md_var(tc, "descr",
6272	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6273	    "catching PTRACE_VFORK_DONE breakpoint");
6274}
6275
6276ATF_TC_BODY(signal8, tc)
6277{
6278	const int exitval = 5;
6279	const int exitval2 = 15;
6280	const int sigval = SIGSTOP;
6281	const int sigmasked = SIGTRAP;
6282	pid_t child, child2, wpid;
6283#if defined(TWAIT_HAVE_STATUS)
6284	int status;
6285#endif
6286	sigset_t intmask;
6287	ptrace_state_t state;
6288	const int slen = sizeof(state);
6289	ptrace_event_t event;
6290	const int elen = sizeof(event);
6291
6292	atf_tc_expect_fail("PR kern/51918");
6293
6294	DPRINTF("Before forking process PID=%d\n", getpid());
6295	SYSCALL_REQUIRE((child = fork()) != -1);
6296	if (child == 0) {
6297		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6298		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6299
6300		sigemptyset(&intmask);
6301		sigaddset(&intmask, sigmasked);
6302		sigprocmask(SIG_BLOCK, &intmask, NULL);
6303
6304		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6305		FORKEE_ASSERT(raise(sigval) == 0);
6306
6307		FORKEE_ASSERT((child2 = vfork()) != -1);
6308
6309		if (child2 == 0)
6310			_exit(exitval2);
6311
6312		FORKEE_REQUIRE_SUCCESS
6313			(wpid = TWAIT_GENERIC(child2, &status, 0), child2);
6314
6315		forkee_status_exited(status, exitval2);
6316
6317		DPRINTF("Before exiting of the child process\n");
6318		_exit(exitval);
6319	}
6320	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6321
6322	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6323	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6324
6325	validate_status_stopped(status, sigval);
6326
6327	DPRINTF("Enable PTRACE_VFORK_DONE in EVENT_MASK for the child %d\n",
6328	    child);
6329	event.pe_set_event = PTRACE_VFORK_DONE;
6330	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6331
6332	DPRINTF("Before resuming the child process where it left off and "
6333	    "without signal to be sent\n");
6334	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6335
6336	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6337	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6338
6339	validate_status_stopped(status, sigmasked);
6340
6341	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6342	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_VFORK_DONE);
6343
6344	child2 = state.pe_other_pid;
6345	DPRINTF("Reported PTRACE_VFORK_DONE event with forkee %d\n", child2);
6346
6347	DPRINTF("Before resuming the child process where it left off and "
6348	    "without signal to be sent\n");
6349	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6350
6351	DPRINTF("Before calling %s() for the child - expected stopped "
6352	    "SIGCHLD\n", TWAIT_FNAME);
6353	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6354
6355	validate_status_stopped(status, SIGCHLD);
6356
6357	DPRINTF("Before resuming the child process where it left off and "
6358	    "without signal to be sent\n");
6359	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6360
6361	DPRINTF("Before calling %s() for the child - expected exited\n",
6362	    TWAIT_FNAME);
6363	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6364
6365	validate_status_exited(status, exitval);
6366
6367	DPRINTF("Before calling %s() for the child - expected no process\n",
6368	    TWAIT_FNAME);
6369	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6370}
6371
6372ATF_TC(signal9);
6373ATF_TC_HEAD(signal9, tc)
6374{
6375	atf_tc_set_md_var(tc, "descr",
6376	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6377	    "catching PTRACE_LWP_CREATE breakpoint");
6378}
6379
6380ATF_TC_BODY(signal9, tc)
6381{
6382	const int exitval = 5;
6383	const int sigval = SIGSTOP;
6384	const int sigmasked = SIGTRAP;
6385	pid_t child, wpid;
6386#if defined(TWAIT_HAVE_STATUS)
6387	int status;
6388#endif
6389	sigset_t intmask;
6390	ptrace_state_t state;
6391	const int slen = sizeof(state);
6392	ptrace_event_t event;
6393	const int elen = sizeof(event);
6394	ucontext_t uc;
6395	lwpid_t lid;
6396	static const size_t ssize = 16*1024;
6397	void *stack;
6398
6399	atf_tc_expect_fail("PR kern/51918");
6400
6401	DPRINTF("Before forking process PID=%d\n", getpid());
6402	SYSCALL_REQUIRE((child = fork()) != -1);
6403	if (child == 0) {
6404		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6405		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6406
6407		sigemptyset(&intmask);
6408		sigaddset(&intmask, sigmasked);
6409		sigprocmask(SIG_BLOCK, &intmask, NULL);
6410
6411		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6412		FORKEE_ASSERT(raise(sigval) == 0);
6413
6414		DPRINTF("Before allocating memory for stack in child\n");
6415		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6416
6417		DPRINTF("Before making context for new lwp in child\n");
6418		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6419
6420		DPRINTF("Before creating new in child\n");
6421		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6422
6423		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6424		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6425
6426		DPRINTF("Before verifying that reported %d and running lid %d "
6427		    "are the same\n", lid, the_lwp_id);
6428		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6429
6430		DPRINTF("Before exiting of the child process\n");
6431		_exit(exitval);
6432	}
6433	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6434
6435	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6436	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6437
6438	validate_status_stopped(status, sigval);
6439
6440	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6441	event.pe_set_event = PTRACE_LWP_CREATE;
6442	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6443
6444	DPRINTF("Before resuming the child process where it left off and "
6445	    "without signal to be sent\n");
6446	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6447
6448	DPRINTF("Before calling %s() for the child - expected stopped "
6449	    "SIGTRAP\n", TWAIT_FNAME);
6450	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6451
6452	validate_status_stopped(status, sigmasked);
6453
6454	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6455
6456	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_CREATE);
6457
6458	lid = state.pe_lwp;
6459	DPRINTF("Reported PTRACE_LWP_CREATE event with lid %d\n", lid);
6460
6461	DPRINTF("Before resuming the child process where it left off and "
6462	    "without signal to be sent\n");
6463	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6464
6465	DPRINTF("Before calling %s() for the child - expected exited\n",
6466	    TWAIT_FNAME);
6467	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6468
6469	validate_status_exited(status, exitval);
6470
6471	DPRINTF("Before calling %s() for the child - expected no process\n",
6472	    TWAIT_FNAME);
6473	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6474}
6475
6476ATF_TC(signal10);
6477ATF_TC_HEAD(signal10, tc)
6478{
6479	atf_tc_set_md_var(tc, "descr",
6480	    "Verify that masking SIGTRAP in tracee does not stop tracer from "
6481	    "catching PTRACE_LWP_EXIT breakpoint");
6482}
6483
6484ATF_TC_BODY(signal10, tc)
6485{
6486	const int exitval = 5;
6487	const int sigval = SIGSTOP;
6488	const int sigmasked = SIGTRAP;
6489	pid_t child, wpid;
6490#if defined(TWAIT_HAVE_STATUS)
6491	int status;
6492#endif
6493	sigset_t intmask;
6494	ptrace_state_t state;
6495	const int slen = sizeof(state);
6496	ptrace_event_t event;
6497	const int elen = sizeof(event);
6498	ucontext_t uc;
6499	lwpid_t lid;
6500	static const size_t ssize = 16*1024;
6501	void *stack;
6502
6503	atf_tc_expect_fail("PR kern/51918");
6504
6505	DPRINTF("Before forking process PID=%d\n", getpid());
6506	SYSCALL_REQUIRE((child = fork()) != -1);
6507	if (child == 0) {
6508		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6509		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6510
6511		sigemptyset(&intmask);
6512		sigaddset(&intmask, sigmasked);
6513		sigprocmask(SIG_BLOCK, &intmask, NULL);
6514
6515		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6516		FORKEE_ASSERT(raise(sigval) == 0);
6517
6518		DPRINTF("Before allocating memory for stack in child\n");
6519		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
6520
6521		DPRINTF("Before making context for new lwp in child\n");
6522		_lwp_makecontext(&uc, lwp_main_func, NULL, NULL, stack, ssize);
6523
6524		DPRINTF("Before creating new in child\n");
6525		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
6526
6527		DPRINTF("Before waiting for lwp %d to exit\n", lid);
6528		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
6529
6530		DPRINTF("Before verifying that reported %d and running lid %d "
6531		    "are the same\n", lid, the_lwp_id);
6532		FORKEE_ASSERT_EQ(lid, the_lwp_id);
6533
6534		DPRINTF("Before exiting of the child process\n");
6535		_exit(exitval);
6536	}
6537	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6538
6539	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6540	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6541
6542	validate_status_stopped(status, sigval);
6543
6544	DPRINTF("Set empty EVENT_MASK for the child %d\n", child);
6545	event.pe_set_event = PTRACE_LWP_EXIT;
6546	SYSCALL_REQUIRE(ptrace(PT_SET_EVENT_MASK, child, &event, elen) != -1);
6547
6548	DPRINTF("Before resuming the child process where it left off and "
6549	    "without signal to be sent\n");
6550	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6551
6552	DPRINTF("Before calling %s() for the child - expected stopped "
6553	    "SIGTRAP\n", TWAIT_FNAME);
6554	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6555
6556	validate_status_stopped(status, sigmasked);
6557
6558	SYSCALL_REQUIRE(ptrace(PT_GET_PROCESS_STATE, child, &state, slen) != -1);
6559
6560	ATF_REQUIRE_EQ(state.pe_report_event, PTRACE_LWP_EXIT);
6561
6562	lid = state.pe_lwp;
6563	DPRINTF("Reported PTRACE_LWP_EXIT event with lid %d\n", lid);
6564
6565	DPRINTF("Before resuming the child process where it left off and "
6566	    "without signal to be sent\n");
6567	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6568
6569	DPRINTF("Before calling %s() for the child - expected exited\n",
6570	    TWAIT_FNAME);
6571	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6572
6573	validate_status_exited(status, exitval);
6574
6575	DPRINTF("Before calling %s() for the child - expected no process\n",
6576	    TWAIT_FNAME);
6577	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6578}
6579
6580ATF_TC(getsigmask1);
6581ATF_TC_HEAD(getsigmask1, tc)
6582{
6583	atf_tc_set_md_var(tc, "descr",
6584	    "Verify that plain PT_SET_SIGMASK can be called");
6585}
6586
6587ATF_TC_BODY(getsigmask1, tc)
6588{
6589	const int exitval = 5;
6590	const int sigval = SIGSTOP;
6591	pid_t child, wpid;
6592#if defined(TWAIT_HAVE_STATUS)
6593	int status;
6594#endif
6595	sigset_t mask;
6596
6597	DPRINTF("Before forking process PID=%d\n", getpid());
6598	SYSCALL_REQUIRE((child = fork()) != -1);
6599	if (child == 0) {
6600		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6601		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6602
6603		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6604		FORKEE_ASSERT(raise(sigval) == 0);
6605
6606		DPRINTF("Before exiting of the child process\n");
6607		_exit(exitval);
6608	}
6609	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6610
6611	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6612	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6613
6614	validate_status_stopped(status, sigval);
6615
6616	DPRINTF("Before calling PT_GET_SIGMASK\n");
6617	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6618
6619	DPRINTF("Before resuming the child process where it left off and "
6620	    "without signal to be sent\n");
6621	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6622
6623	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6624	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6625
6626	validate_status_exited(status, exitval);
6627
6628	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6629	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6630}
6631
6632ATF_TC(getsigmask2);
6633ATF_TC_HEAD(getsigmask2, tc)
6634{
6635	atf_tc_set_md_var(tc, "descr",
6636	    "Verify that PT_SET_SIGMASK reports correct mask from tracee");
6637}
6638
6639ATF_TC_BODY(getsigmask2, tc)
6640{
6641	const int exitval = 5;
6642	const int sigval = SIGSTOP;
6643	const int sigmasked = SIGTRAP;
6644	pid_t child, wpid;
6645#if defined(TWAIT_HAVE_STATUS)
6646	int status;
6647#endif
6648	sigset_t mask;
6649	sigset_t expected_mask;
6650	ATF_REQUIRE(sigemptyset(&mask) == 0);
6651	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6652	ATF_REQUIRE(sigaddset(&expected_mask, sigmasked) == 0);
6653
6654	DPRINTF("Before forking process PID=%d\n", getpid());
6655	SYSCALL_REQUIRE((child = fork()) != -1);
6656	if (child == 0) {
6657		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6658		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6659
6660		sigaddset(&mask, sigmasked);
6661		sigprocmask(SIG_BLOCK, &mask, NULL);
6662
6663		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6664		FORKEE_ASSERT(raise(sigval) == 0);
6665
6666		DPRINTF("Before exiting of the child process\n");
6667		_exit(exitval);
6668	}
6669	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6670
6671	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6672	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6673
6674	validate_status_stopped(status, sigval);
6675
6676	DPRINTF("Before calling PT_GET_SIGMASK\n");
6677	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &mask, 0) != -1);
6678
6679	ATF_REQUIRE(memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6680
6681	DPRINTF("Before resuming the child process where it left off and "
6682	    "without signal to be sent\n");
6683	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6684
6685	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6686	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6687
6688	validate_status_exited(status, exitval);
6689
6690	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6691	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6692}
6693
6694ATF_TC(setsigmask1);
6695ATF_TC_HEAD(setsigmask1, tc)
6696{
6697	atf_tc_set_md_var(tc, "descr",
6698	    "Verify that plain PT_SET_SIGMASK can be called with empty mask");
6699}
6700
6701ATF_TC_BODY(setsigmask1, tc)
6702{
6703	const int exitval = 5;
6704	const int sigval = SIGSTOP;
6705	pid_t child, wpid;
6706#if defined(TWAIT_HAVE_STATUS)
6707	int status;
6708#endif
6709	sigset_t mask;
6710	ATF_REQUIRE(sigemptyset(&mask) == 0);
6711
6712	DPRINTF("Before forking process PID=%d\n", getpid());
6713	SYSCALL_REQUIRE((child = fork()) != -1);
6714	if (child == 0) {
6715		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6716		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6717
6718		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6719		FORKEE_ASSERT(raise(sigval) == 0);
6720
6721		DPRINTF("Before exiting of the child process\n");
6722		_exit(exitval);
6723	}
6724	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6725
6726	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6727	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6728
6729	validate_status_stopped(status, sigval);
6730
6731	DPRINTF("Before calling PT_SET_SIGMASK for empty mask\n");
6732	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6733
6734	DPRINTF("Before resuming the child process where it left off and "
6735	    "without signal to be sent\n");
6736	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6737
6738	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6739	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6740
6741	validate_status_exited(status, exitval);
6742
6743	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6744	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6745}
6746
6747ATF_TC(setsigmask2);
6748ATF_TC_HEAD(setsigmask2, tc)
6749{
6750	atf_tc_set_md_var(tc, "descr",
6751	    "Verify that sigmask is preserved between PT_GET_SIGMASK and "
6752	    "PT_SET_SIGMASK");
6753}
6754
6755ATF_TC_BODY(setsigmask2, tc)
6756{
6757	const int exitval = 5;
6758	const int sigval = SIGSTOP;
6759	pid_t child, wpid;
6760#if defined(TWAIT_HAVE_STATUS)
6761	int status;
6762#endif
6763	sigset_t new_mask;
6764	sigset_t mask;
6765	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6766	ATF_REQUIRE(sigemptyset(&mask) == 0);
6767	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6768
6769	DPRINTF("Before forking process PID=%d\n", getpid());
6770	SYSCALL_REQUIRE((child = fork()) != -1);
6771	if (child == 0) {
6772		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6773		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6774
6775		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6776		FORKEE_ASSERT(raise(sigval) == 0);
6777
6778		DPRINTF("Before exiting of the child process\n");
6779		_exit(exitval);
6780	}
6781	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6782
6783	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6784	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6785
6786	validate_status_stopped(status, sigval);
6787
6788	DPRINTF("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6789	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6790
6791	DPRINTF("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6792	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6793
6794	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6795
6796	DPRINTF("Before resuming the child process where it left off and "
6797	    "without signal to be sent\n");
6798	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6799
6800	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6801	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6802
6803	validate_status_exited(status, exitval);
6804
6805	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6806	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6807}
6808
6809ATF_TC(setsigmask3);
6810ATF_TC_HEAD(setsigmask3, tc)
6811{
6812	atf_tc_set_md_var(tc, "descr",
6813	    "Verify that sigmask is preserved between PT_GET_SIGMASK, process "
6814	    "resumed and PT_SET_SIGMASK");
6815}
6816
6817ATF_TC_BODY(setsigmask3, tc)
6818{
6819	const int exitval = 5;
6820	const int sigval = SIGSTOP;
6821	pid_t child, wpid;
6822#if defined(TWAIT_HAVE_STATUS)
6823	int status;
6824#endif
6825	sigset_t new_mask;
6826	sigset_t mask;
6827	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6828	ATF_REQUIRE(sigemptyset(&mask) == 0);
6829	ATF_REQUIRE(sigaddset(&mask, SIGINT) == 0);
6830
6831	DPRINTF("Before forking process PID=%d\n", getpid());
6832	SYSCALL_REQUIRE((child = fork()) != -1);
6833	if (child == 0) {
6834		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6835		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6836
6837		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6838		FORKEE_ASSERT(raise(sigval) == 0);
6839
6840		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6841		FORKEE_ASSERT(raise(sigval) == 0);
6842
6843		DPRINTF("Before exiting of the child process\n");
6844		_exit(exitval);
6845	}
6846	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6847
6848	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6849	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6850
6851	validate_status_stopped(status, sigval);
6852
6853	DPRINTF("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6854	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6855
6856	DPRINTF("Before resuming the child process where it left off and "
6857	    "without signal to be sent\n");
6858	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6859
6860	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6861	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6862
6863	validate_status_stopped(status, sigval);
6864
6865	DPRINTF("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6866	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6867
6868	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) == 0);
6869
6870	DPRINTF("Before resuming the child process where it left off and "
6871	    "without signal to be sent\n");
6872	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6873
6874	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6875	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6876
6877	validate_status_exited(status, exitval);
6878
6879	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6880	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6881}
6882
6883ATF_TC(setsigmask4);
6884ATF_TC_HEAD(setsigmask4, tc)
6885{
6886	atf_tc_set_md_var(tc, "descr",
6887	    "Verify that new sigmask is visible in tracee");
6888}
6889
6890ATF_TC_BODY(setsigmask4, tc)
6891{
6892	const int exitval = 5;
6893	const int sigval = SIGSTOP;
6894	pid_t child, wpid;
6895#if defined(TWAIT_HAVE_STATUS)
6896	int status;
6897#endif
6898	sigset_t mask;
6899	sigset_t expected_mask;
6900	ATF_REQUIRE(sigemptyset(&mask) == 0);
6901	ATF_REQUIRE(sigemptyset(&expected_mask) == 0);
6902	ATF_REQUIRE(sigaddset(&expected_mask, SIGINT) == 0);
6903
6904	DPRINTF("Before forking process PID=%d\n", getpid());
6905	SYSCALL_REQUIRE((child = fork()) != -1);
6906	if (child == 0) {
6907		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6908		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6909
6910		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6911		FORKEE_ASSERT(raise(sigval) == 0);
6912
6913		sigprocmask(0, NULL, &mask);
6914
6915		FORKEE_ASSERT
6916		    (memcmp(&mask, &expected_mask, sizeof(sigset_t)) == 0);
6917
6918		DPRINTF("Before exiting of the child process\n");
6919		_exit(exitval);
6920	}
6921	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6922
6923	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6924	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6925
6926	validate_status_stopped(status, sigval);
6927
6928	DPRINTF("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6929	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &expected_mask, 0) != -1);
6930
6931	DPRINTF("Before resuming the child process where it left off and "
6932	    "without signal to be sent\n");
6933	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6934
6935	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6936	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6937
6938	validate_status_exited(status, exitval);
6939
6940	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6941	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
6942}
6943
6944ATF_TC(setsigmask5);
6945ATF_TC_HEAD(setsigmask5, tc)
6946{
6947	atf_tc_set_md_var(tc, "descr",
6948	    "Verify that sigmask cannot be set to SIGKILL");
6949}
6950
6951ATF_TC_BODY(setsigmask5, tc)
6952{
6953	const int exitval = 5;
6954	const int sigval = SIGSTOP;
6955	pid_t child, wpid;
6956#if defined(TWAIT_HAVE_STATUS)
6957	int status;
6958#endif
6959	sigset_t new_mask;
6960	sigset_t mask;
6961	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
6962	ATF_REQUIRE(sigemptyset(&mask) == 0);
6963	ATF_REQUIRE(sigaddset(&mask, SIGKILL) == 0);
6964
6965	DPRINTF("Before forking process PID=%d\n", getpid());
6966	SYSCALL_REQUIRE((child = fork()) != -1);
6967	if (child == 0) {
6968		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
6969		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
6970
6971		DPRINTF("Before raising %s from child\n", strsignal(sigval));
6972		FORKEE_ASSERT(raise(sigval) == 0);
6973
6974		DPRINTF("Before exiting of the child process\n");
6975		_exit(exitval);
6976	}
6977	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
6978
6979	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6980	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6981
6982	validate_status_stopped(status, sigval);
6983
6984	DPRINTF("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
6985	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
6986
6987	DPRINTF("Before calling PT_GET_SIGMASK to store it in new_mask\n");
6988	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
6989
6990	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
6991
6992	DPRINTF("Before resuming the child process where it left off and "
6993	    "without signal to be sent\n");
6994	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
6995
6996	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
6997	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
6998
6999	validate_status_exited(status, exitval);
7000
7001	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7002	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7003}
7004
7005ATF_TC(setsigmask6);
7006ATF_TC_HEAD(setsigmask6, tc)
7007{
7008	atf_tc_set_md_var(tc, "descr",
7009	    "Verify that sigmask cannot be set to SIGSTOP");
7010}
7011
7012ATF_TC_BODY(setsigmask6, tc)
7013{
7014	const int exitval = 5;
7015	const int sigval = SIGSTOP;
7016	pid_t child, wpid;
7017#if defined(TWAIT_HAVE_STATUS)
7018	int status;
7019#endif
7020	sigset_t new_mask;
7021	sigset_t mask;
7022	ATF_REQUIRE(sigemptyset(&new_mask) == 0);
7023	ATF_REQUIRE(sigemptyset(&mask) == 0);
7024	ATF_REQUIRE(sigaddset(&mask, SIGSTOP) == 0);
7025
7026	DPRINTF("Before forking process PID=%d\n", getpid());
7027	SYSCALL_REQUIRE((child = fork()) != -1);
7028	if (child == 0) {
7029		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7030		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7031
7032		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7033		FORKEE_ASSERT(raise(sigval) == 0);
7034
7035		DPRINTF("Before exiting of the child process\n");
7036		_exit(exitval);
7037	}
7038	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7039
7040	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7041	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7042
7043	validate_status_stopped(status, sigval);
7044
7045	DPRINTF("Before calling PT_SET_SIGMASK for new mask with SIGINT\n");
7046	SYSCALL_REQUIRE(ptrace(PT_SET_SIGMASK, child, &mask, 0) != -1);
7047
7048	DPRINTF("Before calling PT_GET_SIGMASK to store it in new_mask\n");
7049	SYSCALL_REQUIRE(ptrace(PT_GET_SIGMASK, child, &new_mask, 0) != -1);
7050
7051	ATF_REQUIRE(memcmp(&mask, &new_mask, sizeof(sigset_t)) != 0);
7052
7053	DPRINTF("Before resuming the child process where it left off and "
7054	    "without signal to be sent\n");
7055	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7056
7057	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7058	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7059
7060	validate_status_exited(status, exitval);
7061
7062	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7063	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7064}
7065
7066static void
7067lwp_main_stop(void *arg)
7068{
7069	the_lwp_id = _lwp_self();
7070
7071	raise(SIGTRAP);
7072
7073	_lwp_exit();
7074}
7075
7076ATF_TC(suspend1);
7077ATF_TC_HEAD(suspend1, tc)
7078{
7079	atf_tc_set_md_var(tc, "descr",
7080	    "Verify that a thread can be suspended by a debugger and later "
7081	    "resumed by a tracee");
7082}
7083
7084ATF_TC_BODY(suspend1, tc)
7085{
7086	const int exitval = 5;
7087	const int sigval = SIGSTOP;
7088	pid_t child, wpid;
7089#if defined(TWAIT_HAVE_STATUS)
7090	int status;
7091#endif
7092	ucontext_t uc;
7093	lwpid_t lid;
7094	static const size_t ssize = 16*1024;
7095	void *stack;
7096	struct ptrace_lwpinfo pl;
7097	struct ptrace_siginfo psi;
7098	volatile int go = 0;
7099
7100	// Feature pending for refactoring
7101	atf_tc_expect_fail("PR kern/51995");
7102
7103	// Hangs with qemu
7104	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
7105
7106	DPRINTF("Before forking process PID=%d\n", getpid());
7107	SYSCALL_REQUIRE((child = fork()) != -1);
7108	if (child == 0) {
7109		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7110		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7111
7112		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7113		FORKEE_ASSERT(raise(sigval) == 0);
7114
7115		DPRINTF("Before allocating memory for stack in child\n");
7116		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7117
7118		DPRINTF("Before making context for new lwp in child\n");
7119		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7120
7121		DPRINTF("Before creating new in child\n");
7122		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7123
7124		while (go == 0)
7125			continue;
7126
7127		raise(SIGINT);
7128
7129		FORKEE_ASSERT(_lwp_continue(lid) == 0);
7130
7131		DPRINTF("Before waiting for lwp %d to exit\n", lid);
7132		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7133
7134		DPRINTF("Before verifying that reported %d and running lid %d "
7135		    "are the same\n", lid, the_lwp_id);
7136		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7137
7138		DPRINTF("Before exiting of the child process\n");
7139		_exit(exitval);
7140	}
7141	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7142
7143	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7144	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7145
7146	validate_status_stopped(status, sigval);
7147
7148	DPRINTF("Before resuming the child process where it left off and "
7149	    "without signal to be sent\n");
7150	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7151
7152	DPRINTF("Before calling %s() for the child - expected stopped "
7153	    "SIGTRAP\n", TWAIT_FNAME);
7154	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7155
7156	validate_status_stopped(status, SIGTRAP);
7157
7158	DPRINTF("Before reading siginfo and lwpid_t\n");
7159	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7160
7161	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
7162	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7163
7164        DPRINTF("Write new go to tracee (PID=%d) from tracer (PID=%d)\n",
7165	    child, getpid());
7166	SYSCALL_REQUIRE(ptrace(PT_WRITE_D, child, __UNVOLATILE(&go), 1) != -1);
7167
7168	DPRINTF("Before resuming the child process where it left off and "
7169	    "without signal to be sent\n");
7170	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7171
7172	DPRINTF("Before calling %s() for the child - expected stopped "
7173	    "SIGINT\n", TWAIT_FNAME);
7174	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7175
7176	validate_status_stopped(status, SIGINT);
7177
7178	pl.pl_lwpid = 0;
7179
7180	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7181	while (pl.pl_lwpid != 0) {
7182
7183		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7184		switch (pl.pl_lwpid) {
7185		case 1:
7186			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7187			break;
7188		case 2:
7189			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7190			break;
7191		}
7192	}
7193
7194	DPRINTF("Before resuming the child process where it left off and "
7195	    "without signal to be sent\n");
7196	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7197
7198	DPRINTF("Before calling %s() for the child - expected exited\n",
7199	    TWAIT_FNAME);
7200	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7201
7202	validate_status_exited(status, exitval);
7203
7204	DPRINTF("Before calling %s() for the child - expected no process\n",
7205	    TWAIT_FNAME);
7206	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7207}
7208
7209ATF_TC(suspend2);
7210ATF_TC_HEAD(suspend2, tc)
7211{
7212	atf_tc_set_md_var(tc, "descr",
7213	    "Verify that the while the only thread within a process is "
7214	    "suspended, the whole process cannot be unstopped");
7215}
7216
7217ATF_TC_BODY(suspend2, tc)
7218{
7219	const int exitval = 5;
7220	const int sigval = SIGSTOP;
7221	pid_t child, wpid;
7222#if defined(TWAIT_HAVE_STATUS)
7223	int status;
7224#endif
7225	struct ptrace_siginfo psi;
7226
7227	// Feature pending for refactoring
7228	atf_tc_expect_fail("PR kern/51995");
7229
7230	// Hangs with qemu
7231	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
7232
7233	DPRINTF("Before forking process PID=%d\n", getpid());
7234	SYSCALL_REQUIRE((child = fork()) != -1);
7235	if (child == 0) {
7236		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7237		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7238
7239		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7240		FORKEE_ASSERT(raise(sigval) == 0);
7241
7242		DPRINTF("Before exiting of the child process\n");
7243		_exit(exitval);
7244	}
7245	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7246
7247	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7248	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7249
7250	validate_status_stopped(status, sigval);
7251
7252	DPRINTF("Before reading siginfo and lwpid_t\n");
7253	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7254
7255	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
7256	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7257
7258	DPRINTF("Before resuming the child process where it left off and "
7259	    "without signal to be sent\n");
7260	ATF_REQUIRE_ERRNO(EDEADLK,
7261	    ptrace(PT_CONTINUE, child, (void *)1, 0) == -1);
7262
7263	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
7264	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7265
7266	DPRINTF("Before resuming the child process where it left off and "
7267	    "without signal to be sent\n");
7268	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7269
7270	DPRINTF("Before calling %s() for the child - expected exited\n",
7271	    TWAIT_FNAME);
7272	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7273
7274	validate_status_exited(status, exitval);
7275
7276	DPRINTF("Before calling %s() for the child - expected no process\n",
7277	    TWAIT_FNAME);
7278	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7279}
7280
7281ATF_TC(resume1);
7282ATF_TC_HEAD(resume1, tc)
7283{
7284	atf_tc_set_md_var(tc, "timeout", "5");
7285	atf_tc_set_md_var(tc, "descr",
7286	    "Verify that a thread can be suspended by a debugger and later "
7287	    "resumed by the debugger");
7288}
7289
7290ATF_TC_BODY(resume1, tc)
7291{
7292	struct msg_fds fds;
7293	const int exitval = 5;
7294	const int sigval = SIGSTOP;
7295	pid_t child, wpid;
7296	uint8_t msg = 0xde; /* dummy message for IPC based on pipe(2) */
7297#if defined(TWAIT_HAVE_STATUS)
7298	int status;
7299#endif
7300	ucontext_t uc;
7301	lwpid_t lid;
7302	static const size_t ssize = 16*1024;
7303	void *stack;
7304	struct ptrace_lwpinfo pl;
7305	struct ptrace_siginfo psi;
7306
7307	// Feature pending for refactoring
7308	atf_tc_expect_fail("PR kern/51995");
7309
7310	// Hangs with qemu
7311	ATF_REQUIRE(0 && "In order to get reliable failure, abort");
7312
7313	SYSCALL_REQUIRE(msg_open(&fds) == 0);
7314
7315	DPRINTF("Before forking process PID=%d\n", getpid());
7316	SYSCALL_REQUIRE((child = fork()) != -1);
7317	if (child == 0) {
7318		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7319		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7320
7321		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7322		FORKEE_ASSERT(raise(sigval) == 0);
7323
7324		DPRINTF("Before allocating memory for stack in child\n");
7325		FORKEE_ASSERT((stack = malloc(ssize)) != NULL);
7326
7327		DPRINTF("Before making context for new lwp in child\n");
7328		_lwp_makecontext(&uc, lwp_main_stop, NULL, NULL, stack, ssize);
7329
7330		DPRINTF("Before creating new in child\n");
7331		FORKEE_ASSERT(_lwp_create(&uc, 0, &lid) == 0);
7332
7333		CHILD_TO_PARENT("Message", fds, msg);
7334
7335		raise(SIGINT);
7336
7337		DPRINTF("Before waiting for lwp %d to exit\n", lid);
7338		FORKEE_ASSERT(_lwp_wait(lid, NULL) == 0);
7339
7340		DPRINTF("Before verifying that reported %d and running lid %d "
7341		    "are the same\n", lid, the_lwp_id);
7342		FORKEE_ASSERT_EQ(lid, the_lwp_id);
7343
7344		DPRINTF("Before exiting of the child process\n");
7345		_exit(exitval);
7346	}
7347	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7348
7349	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7350	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7351
7352	validate_status_stopped(status, sigval);
7353
7354	DPRINTF("Before resuming the child process where it left off and "
7355	    "without signal to be sent\n");
7356	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7357
7358	DPRINTF("Before calling %s() for the child - expected stopped "
7359	    "SIGTRAP\n", TWAIT_FNAME);
7360	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7361
7362	validate_status_stopped(status, SIGTRAP);
7363
7364	DPRINTF("Before reading siginfo and lwpid_t\n");
7365	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &psi, sizeof(psi)) != -1);
7366
7367	DPRINTF("Before suspending LWP %d\n", psi.psi_lwpid);
7368	SYSCALL_REQUIRE(ptrace(PT_SUSPEND, child, NULL, psi.psi_lwpid) != -1);
7369
7370	PARENT_FROM_CHILD("Message", fds, msg);
7371
7372	DPRINTF("Before resuming the child process where it left off and "
7373	    "without signal to be sent\n");
7374	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7375
7376	DPRINTF("Before calling %s() for the child - expected stopped "
7377	    "SIGINT\n", TWAIT_FNAME);
7378	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7379
7380	validate_status_stopped(status, SIGINT);
7381
7382	pl.pl_lwpid = 0;
7383
7384	SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7385	while (pl.pl_lwpid != 0) {
7386		SYSCALL_REQUIRE(ptrace(PT_LWPINFO, child, &pl, sizeof(pl)) != -1);
7387		switch (pl.pl_lwpid) {
7388		case 1:
7389			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
7390			break;
7391		case 2:
7392			ATF_REQUIRE_EQ(pl.pl_event, PL_EVENT_SUSPENDED);
7393			break;
7394		}
7395	}
7396
7397	DPRINTF("Before resuming LWP %d\n", psi.psi_lwpid);
7398	SYSCALL_REQUIRE(ptrace(PT_RESUME, child, NULL, psi.psi_lwpid) != -1);
7399
7400	DPRINTF("Before resuming the child process where it left off and "
7401	    "without signal to be sent\n");
7402	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7403
7404	DPRINTF("Before calling %s() for the child - expected exited\n",
7405	    TWAIT_FNAME);
7406	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7407
7408	validate_status_exited(status, exitval);
7409
7410	DPRINTF("Before calling %s() for the child - expected no process\n",
7411	    TWAIT_FNAME);
7412	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7413
7414	msg_close(&fds);
7415
7416	DPRINTF("XXX: Test worked this time but for consistency timeout it\n");
7417	sleep(10);
7418}
7419
7420ATF_TC(syscall1);
7421ATF_TC_HEAD(syscall1, tc)
7422{
7423	atf_tc_set_md_var(tc, "descr",
7424	    "Verify that getpid(2) can be traced with PT_SYSCALL");
7425}
7426
7427ATF_TC_BODY(syscall1, tc)
7428{
7429	const int exitval = 5;
7430	const int sigval = SIGSTOP;
7431	pid_t child, wpid;
7432#if defined(TWAIT_HAVE_STATUS)
7433	int status;
7434#endif
7435	struct ptrace_siginfo info;
7436	memset(&info, 0, sizeof(info));
7437
7438	DPRINTF("Before forking process PID=%d\n", getpid());
7439	SYSCALL_REQUIRE((child = fork()) != -1);
7440	if (child == 0) {
7441		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7442		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7443
7444		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7445		FORKEE_ASSERT(raise(sigval) == 0);
7446
7447		syscall(SYS_getpid);
7448
7449		DPRINTF("Before exiting of the child process\n");
7450		_exit(exitval);
7451	}
7452	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7453
7454	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7455	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7456
7457	validate_status_stopped(status, sigval);
7458
7459	DPRINTF("Before resuming the child process where it left off and "
7460	    "without signal to be sent\n");
7461	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7462
7463	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7464	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7465
7466	validate_status_stopped(status, SIGTRAP);
7467
7468	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7469	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7470
7471	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7472	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCE);
7473
7474	DPRINTF("Before resuming the child process where it left off and "
7475	    "without signal to be sent\n");
7476	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7477
7478	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7479	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7480
7481	validate_status_stopped(status, SIGTRAP);
7482
7483	DPRINTF("Before calling ptrace(2) with PT_GET_SIGINFO for child\n");
7484	SYSCALL_REQUIRE(ptrace(PT_GET_SIGINFO, child, &info, sizeof(info)) != -1);
7485
7486	DPRINTF("Before checking siginfo_t\n");
7487	ATF_REQUIRE_EQ(info.psi_siginfo.si_signo, SIGTRAP);
7488	ATF_REQUIRE_EQ(info.psi_siginfo.si_code, TRAP_SCX);
7489
7490	DPRINTF("Before resuming the child process where it left off and "
7491	    "without signal to be sent\n");
7492	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7493
7494	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7495	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7496
7497	validate_status_exited(status, exitval);
7498
7499	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7500	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7501}
7502
7503ATF_TC(syscallemu1);
7504ATF_TC_HEAD(syscallemu1, tc)
7505{
7506	atf_tc_set_md_var(tc, "descr",
7507	    "Verify that exit(2) can be intercepted with PT_SYSCALLEMU");
7508}
7509
7510ATF_TC_BODY(syscallemu1, tc)
7511{
7512	const int exitval = 5;
7513	const int sigval = SIGSTOP;
7514	pid_t child, wpid;
7515#if defined(TWAIT_HAVE_STATUS)
7516	int status;
7517#endif
7518
7519#if defined(__sparc__) && !defined(__sparc64__)
7520	/* syscallemu does not work on sparc (32-bit) */
7521	atf_tc_expect_fail("PR kern/52166");
7522#endif
7523
7524	DPRINTF("Before forking process PID=%d\n", getpid());
7525	SYSCALL_REQUIRE((child = fork()) != -1);
7526	if (child == 0) {
7527		DPRINTF("Before calling PT_TRACE_ME from child %d\n", getpid());
7528		FORKEE_ASSERT(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
7529
7530		DPRINTF("Before raising %s from child\n", strsignal(sigval));
7531		FORKEE_ASSERT(raise(sigval) == 0);
7532
7533		syscall(SYS_exit, 100);
7534
7535		DPRINTF("Before exiting of the child process\n");
7536		_exit(exitval);
7537	}
7538	DPRINTF("Parent process PID=%d, child's PID=%d\n", getpid(), child);
7539
7540	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7541	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7542
7543	validate_status_stopped(status, sigval);
7544
7545	DPRINTF("Before resuming the child process where it left off and "
7546	    "without signal to be sent\n");
7547	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7548
7549	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7550	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7551
7552	validate_status_stopped(status, SIGTRAP);
7553
7554	DPRINTF("Set SYSCALLEMU for intercepted syscall\n");
7555	SYSCALL_REQUIRE(ptrace(PT_SYSCALLEMU, child, (void *)1, 0) != -1);
7556
7557	DPRINTF("Before resuming the child process where it left off and "
7558	    "without signal to be sent\n");
7559	SYSCALL_REQUIRE(ptrace(PT_SYSCALL, child, (void *)1, 0) != -1);
7560
7561	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7562	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7563
7564	validate_status_stopped(status, SIGTRAP);
7565
7566	DPRINTF("Before resuming the child process where it left off and "
7567	    "without signal to be sent\n");
7568	SYSCALL_REQUIRE(ptrace(PT_CONTINUE, child, (void *)1, 0) != -1);
7569
7570	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7571	TWAIT_REQUIRE_SUCCESS(wpid = TWAIT_GENERIC(child, &status, 0), child);
7572
7573	validate_status_exited(status, exitval);
7574
7575	DPRINTF("Before calling %s() for the child\n", TWAIT_FNAME);
7576	TWAIT_REQUIRE_FAILURE(ECHILD, wpid = TWAIT_GENERIC(child, &status, 0));
7577}
7578
7579#include "t_ptrace_amd64_wait.h"
7580#include "t_ptrace_i386_wait.h"
7581#include "t_ptrace_x86_wait.h"
7582
7583ATF_TP_ADD_TCS(tp)
7584{
7585	setvbuf(stdout, NULL, _IONBF, 0);
7586	setvbuf(stderr, NULL, _IONBF, 0);
7587	ATF_TP_ADD_TC(tp, traceme1);
7588	ATF_TP_ADD_TC(tp, traceme2);
7589	ATF_TP_ADD_TC(tp, traceme3);
7590	ATF_TP_ADD_TC(tp, traceme4);
7591
7592	ATF_TP_ADD_TC_HAVE_PID(tp, attach1);
7593	ATF_TP_ADD_TC_HAVE_PID(tp, attach2);
7594	ATF_TP_ADD_TC(tp, attach3);
7595	ATF_TP_ADD_TC(tp, attach4);
7596	ATF_TP_ADD_TC_HAVE_PID(tp, attach5);
7597	ATF_TP_ADD_TC_HAVE_PID(tp, attach6);
7598	ATF_TP_ADD_TC_HAVE_PID(tp, attach7);
7599
7600	ATF_TP_ADD_TC(tp, eventmask1);
7601	ATF_TP_ADD_TC(tp, eventmask2);
7602	ATF_TP_ADD_TC(tp, eventmask3);
7603	ATF_TP_ADD_TC(tp, eventmask4);
7604	ATF_TP_ADD_TC(tp, eventmask5);
7605	ATF_TP_ADD_TC(tp, eventmask6);
7606
7607	ATF_TP_ADD_TC_HAVE_PID(tp, fork1);
7608	ATF_TP_ADD_TC(tp, fork2);
7609
7610	ATF_TP_ADD_TC_HAVE_PID(tp, vfork1);
7611	ATF_TP_ADD_TC(tp, vfork2);
7612
7613	ATF_TP_ADD_TC(tp, vforkdone1);
7614	ATF_TP_ADD_TC(tp, vforkdone2);
7615
7616	ATF_TP_ADD_TC(tp, io_read_d1);
7617	ATF_TP_ADD_TC(tp, io_read_d2);
7618	ATF_TP_ADD_TC(tp, io_read_d3);
7619	ATF_TP_ADD_TC(tp, io_read_d4);
7620
7621	ATF_TP_ADD_TC(tp, io_write_d1);
7622	ATF_TP_ADD_TC(tp, io_write_d2);
7623	ATF_TP_ADD_TC(tp, io_write_d3);
7624	ATF_TP_ADD_TC(tp, io_write_d4);
7625
7626	ATF_TP_ADD_TC(tp, read_d1);
7627	ATF_TP_ADD_TC(tp, read_d2);
7628	ATF_TP_ADD_TC(tp, read_d3);
7629	ATF_TP_ADD_TC(tp, read_d4);
7630
7631	ATF_TP_ADD_TC(tp, write_d1);
7632	ATF_TP_ADD_TC(tp, write_d2);
7633	ATF_TP_ADD_TC(tp, write_d3);
7634	ATF_TP_ADD_TC(tp, write_d4);
7635
7636	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake1);
7637	ATF_TP_ADD_TC(tp, io_read_d_write_d_handshake2);
7638
7639	ATF_TP_ADD_TC(tp, read_d_write_d_handshake1);
7640	ATF_TP_ADD_TC(tp, read_d_write_d_handshake2);
7641
7642	ATF_TP_ADD_TC(tp, io_read_i1);
7643	ATF_TP_ADD_TC(tp, io_read_i2);
7644	ATF_TP_ADD_TC(tp, io_read_i3);
7645	ATF_TP_ADD_TC(tp, io_read_i4);
7646
7647	ATF_TP_ADD_TC(tp, read_i1);
7648	ATF_TP_ADD_TC(tp, read_i2);
7649	ATF_TP_ADD_TC(tp, read_i3);
7650	ATF_TP_ADD_TC(tp, read_i4);
7651
7652	ATF_TP_ADD_TC(tp, io_read_auxv1);
7653
7654	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs1);
7655	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs2);
7656	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs3);
7657	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs4);
7658	ATF_TP_ADD_TC_HAVE_GPREGS(tp, regs5);
7659
7660	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs1);
7661	ATF_TP_ADD_TC_HAVE_FPREGS(tp, fpregs2);
7662
7663	ATF_TP_ADD_TC_PT_STEP(tp, step1);
7664	ATF_TP_ADD_TC_PT_STEP(tp, step2);
7665	ATF_TP_ADD_TC_PT_STEP(tp, step3);
7666	ATF_TP_ADD_TC_PT_STEP(tp, step4);
7667
7668	ATF_TP_ADD_TC_PT_STEP(tp, setstep1);
7669	ATF_TP_ADD_TC_PT_STEP(tp, setstep2);
7670	ATF_TP_ADD_TC_PT_STEP(tp, setstep3);
7671	ATF_TP_ADD_TC_PT_STEP(tp, setstep4);
7672
7673	ATF_TP_ADD_TC(tp, kill1);
7674	ATF_TP_ADD_TC(tp, kill2);
7675
7676	ATF_TP_ADD_TC(tp, lwpinfo1);
7677	ATF_TP_ADD_TC_HAVE_PID(tp, lwpinfo2);
7678
7679	ATF_TP_ADD_TC(tp, siginfo1);
7680	ATF_TP_ADD_TC(tp, siginfo2);
7681	ATF_TP_ADD_TC(tp, siginfo3);
7682	ATF_TP_ADD_TC(tp, siginfo4);
7683	ATF_TP_ADD_TC_HAVE_PID(tp, siginfo5);
7684	ATF_TP_ADD_TC_PT_STEP(tp, siginfo6);
7685
7686	ATF_TP_ADD_TC(tp, lwp_create1);
7687
7688	ATF_TP_ADD_TC(tp, lwp_exit1);
7689
7690	ATF_TP_ADD_TC(tp, signal1);
7691	ATF_TP_ADD_TC(tp, signal2);
7692	ATF_TP_ADD_TC(tp, signal3);
7693	ATF_TP_ADD_TC_PT_STEP(tp, signal4);
7694	ATF_TP_ADD_TC(tp, signal5);
7695	ATF_TP_ADD_TC_HAVE_PID(tp, signal6);
7696	ATF_TP_ADD_TC_HAVE_PID(tp, signal7);
7697	ATF_TP_ADD_TC(tp, signal8);
7698	ATF_TP_ADD_TC(tp, signal9);
7699	ATF_TP_ADD_TC(tp, signal10);
7700
7701	ATF_TP_ADD_TC(tp, suspend1);
7702	ATF_TP_ADD_TC(tp, suspend2);
7703
7704	ATF_TP_ADD_TC(tp, resume1);
7705
7706	ATF_TP_ADD_TC(tp, getsigmask1);
7707	ATF_TP_ADD_TC(tp, getsigmask2);
7708
7709	ATF_TP_ADD_TC(tp, setsigmask1);
7710	ATF_TP_ADD_TC(tp, setsigmask2);
7711	ATF_TP_ADD_TC(tp, setsigmask3);
7712	ATF_TP_ADD_TC(tp, setsigmask4);
7713	ATF_TP_ADD_TC(tp, setsigmask5);
7714	ATF_TP_ADD_TC(tp, setsigmask6);
7715
7716	ATF_TP_ADD_TC(tp, syscall1);
7717
7718	ATF_TP_ADD_TC(tp, syscallemu1);
7719
7720	ATF_TP_ADD_TCS_PTRACE_WAIT_AMD64();
7721	ATF_TP_ADD_TCS_PTRACE_WAIT_I386();
7722	ATF_TP_ADD_TCS_PTRACE_WAIT_X86();
7723
7724	return atf_no_error();
7725}
7726