]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - tests/sys/kern/ptrace_test.c
MFC r313992, r314075, r314118, r315484:
[FreeBSD/stable/10.git] / tests / sys / kern / ptrace_test.c
1 /*-
2  * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31 #include <sys/cpuset.h>
32 #include <sys/event.h>
33 #include <sys/time.h>
34 #include <sys/ptrace.h>
35 #include <sys/queue.h>
36 #include <sys/runq.h>
37 #include <sys/syscall.h>
38 #include <sys/sysctl.h>
39 #include <sys/user.h>
40 #include <sys/wait.h>
41 #include <errno.h>
42 #include <machine/cpufunc.h>
43 #include <pthread.h>
44 #include <sched.h>
45 #include <semaphore.h>
46 #include <signal.h>
47 #include <stdio.h>
48 #include <stdlib.h>
49 #include <unistd.h>
50 #include <atf-c.h>
51
52 /*
53  * A variant of ATF_REQUIRE that is suitable for use in child
54  * processes.  This only works if the parent process is tripped up by
55  * the early exit and fails some requirement itself.
56  */
57 #define CHILD_REQUIRE(exp) do {                                         \
58                 if (!(exp))                                             \
59                         child_fail_require(__FILE__, __LINE__,          \
60                             #exp " not met");                           \
61         } while (0)
62
63 static __dead2 void
64 child_fail_require(const char *file, int line, const char *str)
65 {
66         char buf[128];
67
68         snprintf(buf, sizeof(buf), "%s:%d: %s\n", file, line, str);
69         write(2, buf, strlen(buf));
70         _exit(32);
71 }
72
73 static void
74 trace_me(void)
75 {
76
77         /* Attach the parent process as a tracer of this process. */
78         CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
79
80         /* Trigger a stop. */
81         raise(SIGSTOP);
82 }
83
84 static void
85 attach_child(pid_t pid)
86 {
87         pid_t wpid;
88         int status;
89
90         ATF_REQUIRE(ptrace(PT_ATTACH, pid, NULL, 0) == 0);
91
92         wpid = waitpid(pid, &status, 0);
93         ATF_REQUIRE(wpid == pid);
94         ATF_REQUIRE(WIFSTOPPED(status));
95         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
96 }
97
98 static void
99 wait_for_zombie(pid_t pid)
100 {
101
102         /*
103          * Wait for a process to exit.  This is kind of gross, but
104          * there is not a better way.
105          */
106         for (;;) {
107                 struct kinfo_proc kp;
108                 size_t len;
109                 int mib[4];
110
111                 mib[0] = CTL_KERN;
112                 mib[1] = KERN_PROC;
113                 mib[2] = KERN_PROC_PID;
114                 mib[3] = pid;
115                 len = sizeof(kp);
116                 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
117                         /* The KERN_PROC_PID sysctl fails for zombies. */
118                         ATF_REQUIRE(errno == ESRCH);
119                         break;
120                 }
121                 usleep(5000);
122         }
123 }
124
125 /*
126  * Verify that a parent debugger process "sees" the exit of a debugged
127  * process exactly once when attached via PT_TRACE_ME.
128  */
129 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
130 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
131 {
132         pid_t child, wpid;
133         int status;
134
135         ATF_REQUIRE((child = fork()) != -1);
136         if (child == 0) {
137                 /* Child process. */
138                 trace_me();
139
140                 exit(1);
141         }
142
143         /* Parent process. */
144
145         /* The first wait() should report the stop from SIGSTOP. */
146         wpid = waitpid(child, &status, 0);
147         ATF_REQUIRE(wpid == child);
148         ATF_REQUIRE(WIFSTOPPED(status));
149         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
150
151         /* Continue the child ignoring the SIGSTOP. */
152         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
153
154         /* The second wait() should report the exit status. */
155         wpid = waitpid(child, &status, 0);
156         ATF_REQUIRE(wpid == child);
157         ATF_REQUIRE(WIFEXITED(status));
158         ATF_REQUIRE(WEXITSTATUS(status) == 1);
159
160         /* The child should no longer exist. */
161         wpid = waitpid(child, &status, 0);
162         ATF_REQUIRE(wpid == -1);
163         ATF_REQUIRE(errno == ECHILD);
164 }
165
166 /*
167  * Verify that a parent debugger process "sees" the exit of a debugged
168  * process exactly once when attached via PT_ATTACH.
169  */
170 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
171 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
172 {
173         pid_t child, wpid;
174         int cpipe[2], status;
175         char c;
176
177         ATF_REQUIRE(pipe(cpipe) == 0);
178         ATF_REQUIRE((child = fork()) != -1);
179         if (child == 0) {
180                 /* Child process. */
181                 close(cpipe[0]);
182
183                 /* Wait for the parent to attach. */
184                 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == 0);
185
186                 exit(1);
187         }
188         close(cpipe[1]);
189
190         /* Parent process. */
191
192         /* Attach to the child process. */
193         attach_child(child);
194
195         /* Continue the child ignoring the SIGSTOP. */
196         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
197
198         /* Signal the child to exit. */
199         close(cpipe[0]);
200
201         /* The second wait() should report the exit status. */
202         wpid = waitpid(child, &status, 0);
203         ATF_REQUIRE(wpid == child);
204         ATF_REQUIRE(WIFEXITED(status));
205         ATF_REQUIRE(WEXITSTATUS(status) == 1);
206
207         /* The child should no longer exist. */
208         wpid = waitpid(child, &status, 0);
209         ATF_REQUIRE(wpid == -1);
210         ATF_REQUIRE(errno == ECHILD);
211 }
212
213 /*
214  * Verify that a parent process "sees" the exit of a debugged process only
215  * after the debugger has seen it.
216  */
217 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
218 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
219 {
220         pid_t child, debugger, wpid;
221         int cpipe[2], dpipe[2], status;
222         char c;
223
224         ATF_REQUIRE(pipe(cpipe) == 0);
225         ATF_REQUIRE((child = fork()) != -1);
226
227         if (child == 0) {
228                 /* Child process. */
229                 close(cpipe[0]);
230
231                 /* Wait for parent to be ready. */
232                 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
233
234                 exit(1);
235         }
236         close(cpipe[1]);
237
238         ATF_REQUIRE(pipe(dpipe) == 0);
239         ATF_REQUIRE((debugger = fork()) != -1);
240
241         if (debugger == 0) {
242                 /* Debugger process. */
243                 close(dpipe[0]);
244
245                 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
246
247                 wpid = waitpid(child, &status, 0);
248                 CHILD_REQUIRE(wpid == child);
249                 CHILD_REQUIRE(WIFSTOPPED(status));
250                 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
251
252                 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
253
254                 /* Signal parent that debugger is attached. */
255                 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
256
257                 /* Wait for parent's failed wait. */
258                 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == 0);
259
260                 wpid = waitpid(child, &status, 0);
261                 CHILD_REQUIRE(wpid == child);
262                 CHILD_REQUIRE(WIFEXITED(status));
263                 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
264
265                 exit(0);
266         }
267         close(dpipe[1]);
268
269         /* Parent process. */
270
271         /* Wait for the debugger to attach to the child. */
272         ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
273
274         /* Release the child. */
275         ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
276         ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
277         close(cpipe[0]);
278
279         wait_for_zombie(child);
280
281         /*
282          * This wait should return a pid of 0 to indicate no status to
283          * report.  The parent should see the child as non-exited
284          * until the debugger sees the exit.
285          */
286         wpid = waitpid(child, &status, WNOHANG);
287         ATF_REQUIRE(wpid == 0);
288
289         /* Signal the debugger to wait for the child. */
290         close(dpipe[0]);
291
292         /* Wait for the debugger. */
293         wpid = waitpid(debugger, &status, 0);
294         ATF_REQUIRE(wpid == debugger);
295         ATF_REQUIRE(WIFEXITED(status));
296         ATF_REQUIRE(WEXITSTATUS(status) == 0);
297
298         /* The child process should now be ready. */
299         wpid = waitpid(child, &status, WNOHANG);
300         ATF_REQUIRE(wpid == child);
301         ATF_REQUIRE(WIFEXITED(status));
302         ATF_REQUIRE(WEXITSTATUS(status) == 1);
303 }
304
305 /*
306  * Verify that a parent process "sees" the exit of a debugged process
307  * only after a non-direct-child debugger has seen it.  In particular,
308  * various wait() calls in the parent must avoid failing with ESRCH by
309  * checking the parent's orphan list for the debugee.
310  */
311 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
312 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
313 {
314         pid_t child, debugger, fpid, wpid;
315         int cpipe[2], dpipe[2], status;
316         char c;
317
318         ATF_REQUIRE(pipe(cpipe) == 0);
319         ATF_REQUIRE((child = fork()) != -1);
320
321         if (child == 0) {
322                 /* Child process. */
323                 close(cpipe[0]);
324
325                 /* Wait for parent to be ready. */
326                 CHILD_REQUIRE(read(cpipe[1], &c, sizeof(c)) == sizeof(c));
327
328                 exit(1);
329         }
330         close(cpipe[1]);
331
332         ATF_REQUIRE(pipe(dpipe) == 0);
333         ATF_REQUIRE((debugger = fork()) != -1);
334
335         if (debugger == 0) {
336                 /* Debugger parent. */
337
338                 /*
339                  * Fork again and drop the debugger parent so that the
340                  * debugger is not a child of the main parent.
341                  */
342                 CHILD_REQUIRE((fpid = fork()) != -1);
343                 if (fpid != 0)
344                         exit(2);
345
346                 /* Debugger process. */
347                 close(dpipe[0]);
348
349                 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
350
351                 wpid = waitpid(child, &status, 0);
352                 CHILD_REQUIRE(wpid == child);
353                 CHILD_REQUIRE(WIFSTOPPED(status));
354                 CHILD_REQUIRE(WSTOPSIG(status) == SIGSTOP);
355
356                 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
357
358                 /* Signal parent that debugger is attached. */
359                 CHILD_REQUIRE(write(dpipe[1], &c, sizeof(c)) == sizeof(c));
360
361                 /* Wait for parent's failed wait. */
362                 CHILD_REQUIRE(read(dpipe[1], &c, sizeof(c)) == sizeof(c));
363
364                 wpid = waitpid(child, &status, 0);
365                 CHILD_REQUIRE(wpid == child);
366                 CHILD_REQUIRE(WIFEXITED(status));
367                 CHILD_REQUIRE(WEXITSTATUS(status) == 1);
368
369                 exit(0);
370         }
371         close(dpipe[1]);
372
373         /* Parent process. */
374
375         /* Wait for the debugger parent process to exit. */
376         wpid = waitpid(debugger, &status, 0);
377         ATF_REQUIRE(wpid == debugger);
378         ATF_REQUIRE(WIFEXITED(status));
379         ATF_REQUIRE(WEXITSTATUS(status) == 2);
380
381         /* A WNOHANG wait here should see the non-exited child. */
382         wpid = waitpid(child, &status, WNOHANG);
383         ATF_REQUIRE(wpid == 0);
384
385         /* Wait for the debugger to attach to the child. */
386         ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == sizeof(c));
387
388         /* Release the child. */
389         ATF_REQUIRE(write(cpipe[0], &c, sizeof(c)) == sizeof(c));
390         ATF_REQUIRE(read(cpipe[0], &c, sizeof(c)) == 0);
391         close(cpipe[0]);
392
393         wait_for_zombie(child);
394
395         /*
396          * This wait should return a pid of 0 to indicate no status to
397          * report.  The parent should see the child as non-exited
398          * until the debugger sees the exit.
399          */
400         wpid = waitpid(child, &status, WNOHANG);
401         ATF_REQUIRE(wpid == 0);
402
403         /* Signal the debugger to wait for the child. */
404         ATF_REQUIRE(write(dpipe[0], &c, sizeof(c)) == sizeof(c));
405
406         /* Wait for the debugger. */
407         ATF_REQUIRE(read(dpipe[0], &c, sizeof(c)) == 0);
408         close(dpipe[0]);
409
410         /* The child process should now be ready. */
411         wpid = waitpid(child, &status, WNOHANG);
412         ATF_REQUIRE(wpid == child);
413         ATF_REQUIRE(WIFEXITED(status));
414         ATF_REQUIRE(WEXITSTATUS(status) == 1);
415 }
416
417 /*
418  * The parent process should always act the same regardless of how the
419  * debugger is attached to it.
420  */
421 static __dead2 void
422 follow_fork_parent(bool use_vfork)
423 {
424         pid_t fpid, wpid;
425         int status;
426
427         if (use_vfork)
428                 CHILD_REQUIRE((fpid = vfork()) != -1);
429         else
430                 CHILD_REQUIRE((fpid = fork()) != -1);
431
432         if (fpid == 0)
433                 /* Child */
434                 exit(2);
435
436         wpid = waitpid(fpid, &status, 0);
437         CHILD_REQUIRE(wpid == fpid);
438         CHILD_REQUIRE(WIFEXITED(status));
439         CHILD_REQUIRE(WEXITSTATUS(status) == 2);
440
441         exit(1);
442 }
443
444 /*
445  * Helper routine for follow fork tests.  This waits for two stops
446  * that report both "sides" of a fork.  It returns the pid of the new
447  * child process.
448  */
449 static pid_t
450 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
451 {
452         struct ptrace_lwpinfo pl;
453         bool fork_reported[2];
454         pid_t child, wpid;
455         int i, status;
456
457         fork_reported[0] = false;
458         fork_reported[1] = false;
459         child = -1;
460         
461         /*
462          * Each process should report a fork event.  The parent should
463          * report a PL_FLAG_FORKED event, and the child should report
464          * a PL_FLAG_CHILD event.
465          */
466         for (i = 0; i < 2; i++) {
467                 wpid = wait(&status);
468                 ATF_REQUIRE(wpid > 0);
469                 ATF_REQUIRE(WIFSTOPPED(status));
470
471                 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
472                     sizeof(pl)) != -1);
473                 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
474                     0);
475                 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
476                     (PL_FLAG_FORKED | PL_FLAG_CHILD));
477                 if (pl.pl_flags & PL_FLAG_CHILD) {
478                         ATF_REQUIRE(wpid != parent);
479                         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
480                         ATF_REQUIRE(!fork_reported[1]);
481                         if (child == -1)
482                                 child = wpid;
483                         else
484                                 ATF_REQUIRE(child == wpid);
485                         if (ppl != NULL)
486                                 ppl[1] = pl;
487                         fork_reported[1] = true;
488                 } else {
489                         ATF_REQUIRE(wpid == parent);
490                         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
491                         ATF_REQUIRE(!fork_reported[0]);
492                         if (child == -1)
493                                 child = pl.pl_child_pid;
494                         else
495                                 ATF_REQUIRE(child == pl.pl_child_pid);
496                         if (ppl != NULL)
497                                 ppl[0] = pl;
498                         fork_reported[0] = true;
499                 }
500         }
501
502         return (child);
503 }
504
505 /*
506  * Verify that a new child process is stopped after a followed fork and
507  * that the traced parent sees the exit of the child after the debugger
508  * when both processes remain attached to the debugger.
509  */
510 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
511 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
512 {
513         pid_t children[0], fpid, wpid;
514         int status;
515
516         ATF_REQUIRE((fpid = fork()) != -1);
517         if (fpid == 0) {
518                 trace_me();
519                 follow_fork_parent(false);
520         }
521
522         /* Parent process. */
523         children[0] = fpid;
524
525         /* The first wait() should report the stop from SIGSTOP. */
526         wpid = waitpid(children[0], &status, 0);
527         ATF_REQUIRE(wpid == children[0]);
528         ATF_REQUIRE(WIFSTOPPED(status));
529         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
530
531         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
532
533         /* Continue the child ignoring the SIGSTOP. */
534         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
535
536         children[1] = handle_fork_events(children[0], NULL);
537         ATF_REQUIRE(children[1] > 0);
538
539         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
540         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
541
542         /*
543          * The child can't exit until the grandchild reports status, so the
544          * grandchild should report its exit first to the debugger.
545          */
546         wpid = wait(&status);
547         ATF_REQUIRE(wpid == children[1]);
548         ATF_REQUIRE(WIFEXITED(status));
549         ATF_REQUIRE(WEXITSTATUS(status) == 2);
550
551         wpid = wait(&status);
552         ATF_REQUIRE(wpid == children[0]);
553         ATF_REQUIRE(WIFEXITED(status));
554         ATF_REQUIRE(WEXITSTATUS(status) == 1);
555
556         wpid = wait(&status);
557         ATF_REQUIRE(wpid == -1);
558         ATF_REQUIRE(errno == ECHILD);
559 }
560
561 /*
562  * Verify that a new child process is stopped after a followed fork
563  * and that the traced parent sees the exit of the child when the new
564  * child process is detached after it reports its fork.
565  */
566 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
567 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
568 {
569         pid_t children[0], fpid, wpid;
570         int status;
571
572         ATF_REQUIRE((fpid = fork()) != -1);
573         if (fpid == 0) {
574                 trace_me();
575                 follow_fork_parent(false);
576         }
577
578         /* Parent process. */
579         children[0] = fpid;
580
581         /* The first wait() should report the stop from SIGSTOP. */
582         wpid = waitpid(children[0], &status, 0);
583         ATF_REQUIRE(wpid == children[0]);
584         ATF_REQUIRE(WIFSTOPPED(status));
585         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
586
587         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
588
589         /* Continue the child ignoring the SIGSTOP. */
590         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
591
592         children[1] = handle_fork_events(children[0], NULL);
593         ATF_REQUIRE(children[1] > 0);
594
595         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
596         ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
597
598         /*
599          * Should not see any status from the grandchild now, only the
600          * child.
601          */
602         wpid = wait(&status);
603         ATF_REQUIRE(wpid == children[0]);
604         ATF_REQUIRE(WIFEXITED(status));
605         ATF_REQUIRE(WEXITSTATUS(status) == 1);
606
607         wpid = wait(&status);
608         ATF_REQUIRE(wpid == -1);
609         ATF_REQUIRE(errno == ECHILD);
610 }
611
612 /*
613  * Verify that a new child process is stopped after a followed fork
614  * and that the traced parent sees the exit of the child when the
615  * traced parent is detached after the fork.
616  */
617 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
618 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
619 {
620         pid_t children[0], fpid, wpid;
621         int status;
622
623         ATF_REQUIRE((fpid = fork()) != -1);
624         if (fpid == 0) {
625                 trace_me();
626                 follow_fork_parent(false);
627         }
628
629         /* Parent process. */
630         children[0] = fpid;
631
632         /* The first wait() should report the stop from SIGSTOP. */
633         wpid = waitpid(children[0], &status, 0);
634         ATF_REQUIRE(wpid == children[0]);
635         ATF_REQUIRE(WIFSTOPPED(status));
636         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
637
638         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
639
640         /* Continue the child ignoring the SIGSTOP. */
641         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
642
643         children[1] = handle_fork_events(children[0], NULL);
644         ATF_REQUIRE(children[1] > 0);
645
646         ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
647         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
648
649         /*
650          * The child can't exit until the grandchild reports status, so the
651          * grandchild should report its exit first to the debugger.
652          *
653          * Even though the child process is detached, it is still a
654          * child of the debugger, so it will still report it's exit
655          * after the grandchild.
656          */
657         wpid = wait(&status);
658         ATF_REQUIRE(wpid == children[1]);
659         ATF_REQUIRE(WIFEXITED(status));
660         ATF_REQUIRE(WEXITSTATUS(status) == 2);
661
662         wpid = wait(&status);
663         ATF_REQUIRE(wpid == children[0]);
664         ATF_REQUIRE(WIFEXITED(status));
665         ATF_REQUIRE(WEXITSTATUS(status) == 1);
666
667         wpid = wait(&status);
668         ATF_REQUIRE(wpid == -1);
669         ATF_REQUIRE(errno == ECHILD);
670 }
671
672 static void
673 attach_fork_parent(int cpipe[2])
674 {
675         pid_t fpid;
676
677         close(cpipe[0]);
678
679         /* Double-fork to disassociate from the debugger. */
680         CHILD_REQUIRE((fpid = fork()) != -1);
681         if (fpid != 0)
682                 exit(3);
683         
684         /* Send the pid of the disassociated child to the debugger. */
685         fpid = getpid();
686         CHILD_REQUIRE(write(cpipe[1], &fpid, sizeof(fpid)) == sizeof(fpid));
687
688         /* Wait for the debugger to attach. */
689         CHILD_REQUIRE(read(cpipe[1], &fpid, sizeof(fpid)) == 0);
690 }
691
692 /*
693  * Verify that a new child process is stopped after a followed fork and
694  * that the traced parent sees the exit of the child after the debugger
695  * when both processes remain attached to the debugger.  In this test
696  * the parent that forks is not a direct child of the debugger.
697  */
698 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
699 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
700 {
701         pid_t children[0], fpid, wpid;
702         int cpipe[2], status;
703
704         ATF_REQUIRE(pipe(cpipe) == 0);
705         ATF_REQUIRE((fpid = fork()) != -1);
706         if (fpid == 0) {
707                 attach_fork_parent(cpipe);
708                 follow_fork_parent(false);
709         }
710
711         /* Parent process. */
712         close(cpipe[1]);
713
714         /* Wait for the direct child to exit. */
715         wpid = waitpid(fpid, &status, 0);
716         ATF_REQUIRE(wpid == fpid);
717         ATF_REQUIRE(WIFEXITED(status));
718         ATF_REQUIRE(WEXITSTATUS(status) == 3);
719
720         /* Read the pid of the fork parent. */
721         ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
722             sizeof(children[0]));
723
724         /* Attach to the fork parent. */
725         attach_child(children[0]);
726
727         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
728
729         /* Continue the fork parent ignoring the SIGSTOP. */
730         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
731
732         /* Signal the fork parent to continue. */
733         close(cpipe[0]);
734
735         children[1] = handle_fork_events(children[0], NULL);
736         ATF_REQUIRE(children[1] > 0);
737
738         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
739         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
740
741         /*
742          * The fork parent can't exit until the child reports status,
743          * so the child should report its exit first to the debugger.
744          */
745         wpid = wait(&status);
746         ATF_REQUIRE(wpid == children[1]);
747         ATF_REQUIRE(WIFEXITED(status));
748         ATF_REQUIRE(WEXITSTATUS(status) == 2);
749
750         wpid = wait(&status);
751         ATF_REQUIRE(wpid == children[0]);
752         ATF_REQUIRE(WIFEXITED(status));
753         ATF_REQUIRE(WEXITSTATUS(status) == 1);
754
755         wpid = wait(&status);
756         ATF_REQUIRE(wpid == -1);
757         ATF_REQUIRE(errno == ECHILD);
758 }
759
760 /*
761  * Verify that a new child process is stopped after a followed fork
762  * and that the traced parent sees the exit of the child when the new
763  * child process is detached after it reports its fork.  In this test
764  * the parent that forks is not a direct child of the debugger.
765  */
766 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
767 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
768 {
769         pid_t children[0], fpid, wpid;
770         int cpipe[2], status;
771
772         ATF_REQUIRE(pipe(cpipe) == 0);
773         ATF_REQUIRE((fpid = fork()) != -1);
774         if (fpid == 0) {
775                 attach_fork_parent(cpipe);
776                 follow_fork_parent(false);
777         }
778
779         /* Parent process. */
780         close(cpipe[1]);
781
782         /* Wait for the direct child to exit. */
783         wpid = waitpid(fpid, &status, 0);
784         ATF_REQUIRE(wpid == fpid);
785         ATF_REQUIRE(WIFEXITED(status));
786         ATF_REQUIRE(WEXITSTATUS(status) == 3);
787
788         /* Read the pid of the fork parent. */
789         ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
790             sizeof(children[0]));
791
792         /* Attach to the fork parent. */
793         attach_child(children[0]);
794
795         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
796
797         /* Continue the fork parent ignoring the SIGSTOP. */
798         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
799
800         /* Signal the fork parent to continue. */
801         close(cpipe[0]);
802
803         children[1] = handle_fork_events(children[0], NULL);
804         ATF_REQUIRE(children[1] > 0);
805
806         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
807         ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
808
809         /*
810          * Should not see any status from the child now, only the fork
811          * parent.
812          */
813         wpid = wait(&status);
814         ATF_REQUIRE(wpid == children[0]);
815         ATF_REQUIRE(WIFEXITED(status));
816         ATF_REQUIRE(WEXITSTATUS(status) == 1);
817
818         wpid = wait(&status);
819         ATF_REQUIRE(wpid == -1);
820         ATF_REQUIRE(errno == ECHILD);
821 }
822
823 /*
824  * Verify that a new child process is stopped after a followed fork
825  * and that the traced parent sees the exit of the child when the
826  * traced parent is detached after the fork.  In this test the parent
827  * that forks is not a direct child of the debugger.
828  */
829 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
830 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
831 {
832         pid_t children[0], fpid, wpid;
833         int cpipe[2], status;
834
835         ATF_REQUIRE(pipe(cpipe) == 0);
836         ATF_REQUIRE((fpid = fork()) != -1);
837         if (fpid == 0) {
838                 attach_fork_parent(cpipe);
839                 follow_fork_parent(false);
840         }
841
842         /* Parent process. */
843         close(cpipe[1]);
844
845         /* Wait for the direct child to exit. */
846         wpid = waitpid(fpid, &status, 0);
847         ATF_REQUIRE(wpid == fpid);
848         ATF_REQUIRE(WIFEXITED(status));
849         ATF_REQUIRE(WEXITSTATUS(status) == 3);
850
851         /* Read the pid of the fork parent. */
852         ATF_REQUIRE(read(cpipe[0], &children[0], sizeof(children[0])) ==
853             sizeof(children[0]));
854
855         /* Attach to the fork parent. */
856         attach_child(children[0]);
857
858         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
859
860         /* Continue the fork parent ignoring the SIGSTOP. */
861         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
862
863         /* Signal the fork parent to continue. */
864         close(cpipe[0]);
865
866         children[1] = handle_fork_events(children[0], NULL);
867         ATF_REQUIRE(children[1] > 0);
868
869         ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
870         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
871
872         /*
873          * Should not see any status from the fork parent now, only
874          * the child.
875          */
876         wpid = wait(&status);
877         ATF_REQUIRE(wpid == children[1]);
878         ATF_REQUIRE(WIFEXITED(status));
879         ATF_REQUIRE(WEXITSTATUS(status) == 2);
880
881         wpid = wait(&status);
882         ATF_REQUIRE(wpid == -1);
883         ATF_REQUIRE(errno == ECHILD);
884 }
885
886 /*
887  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
888  * child process created via fork() reports the correct value.
889  */
890 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
891 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
892 {
893         struct ptrace_lwpinfo pl[2];
894         pid_t children[2], fpid, wpid;
895         int status;
896
897         ATF_REQUIRE((fpid = fork()) != -1);
898         if (fpid == 0) {
899                 trace_me();
900                 follow_fork_parent(false);
901         }
902
903         /* Parent process. */
904         children[0] = fpid;
905
906         /* The first wait() should report the stop from SIGSTOP. */
907         wpid = waitpid(children[0], &status, 0);
908         ATF_REQUIRE(wpid == children[0]);
909         ATF_REQUIRE(WIFSTOPPED(status));
910         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
911
912         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
913
914         /* Continue the child ignoring the SIGSTOP. */
915         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
916
917         /* Wait for both halves of the fork event to get reported. */
918         children[1] = handle_fork_events(children[0], pl);
919         ATF_REQUIRE(children[1] > 0);
920
921         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
922         ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
923         ATF_REQUIRE(pl[0].pl_syscall_code == SYS_fork);
924         ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
925         ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
926
927         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
928         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
929
930         /*
931          * The child can't exit until the grandchild reports status, so the
932          * grandchild should report its exit first to the debugger.
933          */
934         wpid = wait(&status);
935         ATF_REQUIRE(wpid == children[1]);
936         ATF_REQUIRE(WIFEXITED(status));
937         ATF_REQUIRE(WEXITSTATUS(status) == 2);
938
939         wpid = wait(&status);
940         ATF_REQUIRE(wpid == children[0]);
941         ATF_REQUIRE(WIFEXITED(status));
942         ATF_REQUIRE(WEXITSTATUS(status) == 1);
943
944         wpid = wait(&status);
945         ATF_REQUIRE(wpid == -1);
946         ATF_REQUIRE(errno == ECHILD);
947 }
948
949 /*
950  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
951  * child process created via vfork() reports the correct value.
952  */
953 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
954 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
955 {
956         struct ptrace_lwpinfo pl[2];
957         pid_t children[2], fpid, wpid;
958         int status;
959
960         ATF_REQUIRE((fpid = fork()) != -1);
961         if (fpid == 0) {
962                 trace_me();
963                 follow_fork_parent(true);
964         }
965
966         /* Parent process. */
967         children[0] = fpid;
968
969         /* The first wait() should report the stop from SIGSTOP. */
970         wpid = waitpid(children[0], &status, 0);
971         ATF_REQUIRE(wpid == children[0]);
972         ATF_REQUIRE(WIFSTOPPED(status));
973         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
974
975         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
976
977         /* Continue the child ignoring the SIGSTOP. */
978         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
979
980         /* Wait for both halves of the fork event to get reported. */
981         children[1] = handle_fork_events(children[0], pl);
982         ATF_REQUIRE(children[1] > 0);
983
984         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
985         ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
986         ATF_REQUIRE(pl[0].pl_syscall_code == SYS_vfork);
987         ATF_REQUIRE(pl[0].pl_syscall_code == pl[1].pl_syscall_code);
988         ATF_REQUIRE(pl[0].pl_syscall_narg == pl[1].pl_syscall_narg);
989
990         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
991         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
992
993         /*
994          * The child can't exit until the grandchild reports status, so the
995          * grandchild should report its exit first to the debugger.
996          */
997         wpid = wait(&status);
998         ATF_REQUIRE(wpid == children[1]);
999         ATF_REQUIRE(WIFEXITED(status));
1000         ATF_REQUIRE(WEXITSTATUS(status) == 2);
1001
1002         wpid = wait(&status);
1003         ATF_REQUIRE(wpid == children[0]);
1004         ATF_REQUIRE(WIFEXITED(status));
1005         ATF_REQUIRE(WEXITSTATUS(status) == 1);
1006
1007         wpid = wait(&status);
1008         ATF_REQUIRE(wpid == -1);
1009         ATF_REQUIRE(errno == ECHILD);
1010 }
1011
1012 static void *
1013 simple_thread(void *arg __unused)
1014 {
1015
1016         pthread_exit(NULL);
1017 }
1018
1019 static __dead2 void
1020 simple_thread_main(void)
1021 {
1022         pthread_t thread;
1023
1024         CHILD_REQUIRE(pthread_create(&thread, NULL, simple_thread, NULL) == 0);
1025         CHILD_REQUIRE(pthread_join(thread, NULL) == 0);
1026         exit(1);
1027 }
1028
1029 /*
1030  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1031  * thread reports the correct value.
1032  */
1033 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1034 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1035 {
1036         struct ptrace_lwpinfo pl;
1037         pid_t fpid, wpid;
1038         lwpid_t mainlwp;
1039         int status;
1040
1041         ATF_REQUIRE((fpid = fork()) != -1);
1042         if (fpid == 0) {
1043                 trace_me();
1044                 simple_thread_main();
1045         }
1046
1047         /* The first wait() should report the stop from SIGSTOP. */
1048         wpid = waitpid(fpid, &status, 0);
1049         ATF_REQUIRE(wpid == fpid);
1050         ATF_REQUIRE(WIFSTOPPED(status));
1051         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1052
1053         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1054             sizeof(pl)) != -1);
1055         mainlwp = pl.pl_lwpid;
1056
1057         /*
1058          * Continue the child ignoring the SIGSTOP and tracing all
1059          * system call exits.
1060          */
1061         ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1062
1063         /*
1064          * Wait for the new thread to arrive.  pthread_create() might
1065          * invoke any number of system calls.  For now we just wait
1066          * for the new thread to arrive and make sure it reports a
1067          * valid system call code.  If ptrace grows thread event
1068          * reporting then this test can be made more precise.
1069          */
1070         for (;;) {
1071                 wpid = waitpid(fpid, &status, 0);
1072                 ATF_REQUIRE(wpid == fpid);
1073                 ATF_REQUIRE(WIFSTOPPED(status));
1074                 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1075                 
1076                 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1077                     sizeof(pl)) != -1);
1078                 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1079                 ATF_REQUIRE(pl.pl_syscall_code != 0);
1080                 if (pl.pl_lwpid != mainlwp)
1081                         /* New thread seen. */
1082                         break;
1083
1084                 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1085         }
1086
1087         /* Wait for the child to exit. */
1088         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1089         for (;;) {
1090                 wpid = waitpid(fpid, &status, 0);
1091                 ATF_REQUIRE(wpid == fpid);
1092                 if (WIFEXITED(status))
1093                         break;
1094                 
1095                 ATF_REQUIRE(WIFSTOPPED(status));
1096                 ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1097                 ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1098         }
1099                 
1100         ATF_REQUIRE(WEXITSTATUS(status) == 1);
1101
1102         wpid = wait(&status);
1103         ATF_REQUIRE(wpid == -1);
1104         ATF_REQUIRE(errno == ECHILD);
1105 }
1106
1107 /*
1108  * Verify that the expected LWP events are reported for a child thread.
1109  */
1110 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1111 ATF_TC_BODY(ptrace__lwp_events, tc)
1112 {
1113         struct ptrace_lwpinfo pl;
1114         pid_t fpid, wpid;
1115         lwpid_t lwps[2];
1116         int status;
1117
1118         ATF_REQUIRE((fpid = fork()) != -1);
1119         if (fpid == 0) {
1120                 trace_me();
1121                 simple_thread_main();
1122         }
1123
1124         /* The first wait() should report the stop from SIGSTOP. */
1125         wpid = waitpid(fpid, &status, 0);
1126         ATF_REQUIRE(wpid == fpid);
1127         ATF_REQUIRE(WIFSTOPPED(status));
1128         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1129
1130         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1131             sizeof(pl)) != -1);
1132         lwps[0] = pl.pl_lwpid;
1133
1134         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1135
1136         /* Continue the child ignoring the SIGSTOP. */
1137         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1138
1139         /* The first event should be for the child thread's birth. */
1140         wpid = waitpid(fpid, &status, 0);
1141         ATF_REQUIRE(wpid == fpid);
1142         ATF_REQUIRE(WIFSTOPPED(status));
1143         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1144                 
1145         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1146         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1147             (PL_FLAG_BORN | PL_FLAG_SCX));
1148         ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1149         lwps[1] = pl.pl_lwpid;
1150
1151         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1152
1153         /* The next event should be for the child thread's death. */
1154         wpid = waitpid(fpid, &status, 0);
1155         ATF_REQUIRE(wpid == fpid);
1156         ATF_REQUIRE(WIFSTOPPED(status));
1157         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1158                 
1159         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1160         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1161             (PL_FLAG_EXITED | PL_FLAG_SCE));
1162         ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1163
1164         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1165
1166         /* The last event should be for the child process's exit. */
1167         wpid = waitpid(fpid, &status, 0);
1168         ATF_REQUIRE(WIFEXITED(status));
1169         ATF_REQUIRE(WEXITSTATUS(status) == 1);
1170
1171         wpid = wait(&status);
1172         ATF_REQUIRE(wpid == -1);
1173         ATF_REQUIRE(errno == ECHILD);
1174 }
1175
1176 static void *
1177 exec_thread(void *arg __unused)
1178 {
1179
1180         execl("/usr/bin/true", "true", NULL);
1181         exit(127);
1182 }
1183
1184 static __dead2 void
1185 exec_thread_main(void)
1186 {
1187         pthread_t thread;
1188
1189         CHILD_REQUIRE(pthread_create(&thread, NULL, exec_thread, NULL) == 0);
1190         for (;;)
1191                 sleep(60);
1192         exit(1);
1193 }
1194
1195 /*
1196  * Verify that the expected LWP events are reported for a multithreaded
1197  * process that calls execve(2).
1198  */
1199 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1200 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1201 {
1202         struct ptrace_lwpinfo pl;
1203         pid_t fpid, wpid;
1204         lwpid_t lwps[2];
1205         int status;
1206
1207         ATF_REQUIRE((fpid = fork()) != -1);
1208         if (fpid == 0) {
1209                 trace_me();
1210                 exec_thread_main();
1211         }
1212
1213         /* The first wait() should report the stop from SIGSTOP. */
1214         wpid = waitpid(fpid, &status, 0);
1215         ATF_REQUIRE(wpid == fpid);
1216         ATF_REQUIRE(WIFSTOPPED(status));
1217         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1218
1219         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1220             sizeof(pl)) != -1);
1221         lwps[0] = pl.pl_lwpid;
1222
1223         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1224
1225         /* Continue the child ignoring the SIGSTOP. */
1226         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1227
1228         /* The first event should be for the child thread's birth. */
1229         wpid = waitpid(fpid, &status, 0);
1230         ATF_REQUIRE(wpid == fpid);
1231         ATF_REQUIRE(WIFSTOPPED(status));
1232         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1233                 
1234         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1235         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1236             (PL_FLAG_BORN | PL_FLAG_SCX));
1237         ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1238         lwps[1] = pl.pl_lwpid;
1239
1240         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1241
1242         /*
1243          * The next event should be for the main thread's death due to
1244          * single threading from execve().
1245          */
1246         wpid = waitpid(fpid, &status, 0);
1247         ATF_REQUIRE(wpid == fpid);
1248         ATF_REQUIRE(WIFSTOPPED(status));
1249         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1250
1251         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1252         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)) ==
1253             (PL_FLAG_EXITED));
1254         ATF_REQUIRE(pl.pl_lwpid == lwps[0]);
1255
1256         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1257
1258         /* The next event should be for the child process's exec. */
1259         wpid = waitpid(fpid, &status, 0);
1260         ATF_REQUIRE(WIFSTOPPED(status));
1261         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1262
1263         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1264         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1265             (PL_FLAG_EXEC | PL_FLAG_SCX));
1266         ATF_REQUIRE(pl.pl_lwpid == lwps[1]);
1267
1268         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1269
1270         /* The last event should be for the child process's exit. */
1271         wpid = waitpid(fpid, &status, 0);
1272         ATF_REQUIRE(WIFEXITED(status));
1273         ATF_REQUIRE(WEXITSTATUS(status) == 0);
1274
1275         wpid = wait(&status);
1276         ATF_REQUIRE(wpid == -1);
1277         ATF_REQUIRE(errno == ECHILD);
1278 }
1279
1280 static void
1281 handler(int sig __unused)
1282 {
1283 }
1284
1285 static void
1286 signal_main(void)
1287 {
1288
1289         signal(SIGINFO, handler);
1290         raise(SIGINFO);
1291         exit(0);
1292 }
1293
1294 /*
1295  * Verify that the expected ptrace event is reported for a signal.
1296  */
1297 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1298 ATF_TC_BODY(ptrace__siginfo, tc)
1299 {
1300         struct ptrace_lwpinfo pl;
1301         pid_t fpid, wpid;
1302         int status;
1303
1304         ATF_REQUIRE((fpid = fork()) != -1);
1305         if (fpid == 0) {
1306                 trace_me();
1307                 signal_main();
1308         }
1309
1310         /* The first wait() should report the stop from SIGSTOP. */
1311         wpid = waitpid(fpid, &status, 0);
1312         ATF_REQUIRE(wpid == fpid);
1313         ATF_REQUIRE(WIFSTOPPED(status));
1314         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1315
1316         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1317
1318         /* The next event should be for the SIGINFO. */
1319         wpid = waitpid(fpid, &status, 0);
1320         ATF_REQUIRE(WIFSTOPPED(status));
1321         ATF_REQUIRE(WSTOPSIG(status) == SIGINFO);
1322
1323         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1324         ATF_REQUIRE(pl.pl_event == PL_EVENT_SIGNAL);
1325         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1326         ATF_REQUIRE(pl.pl_siginfo.si_code == SI_LWP);
1327         ATF_REQUIRE(pl.pl_siginfo.si_pid == wpid);
1328
1329         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1330
1331         /* The last event should be for the child process's exit. */
1332         wpid = waitpid(fpid, &status, 0);
1333         ATF_REQUIRE(WIFEXITED(status));
1334         ATF_REQUIRE(WEXITSTATUS(status) == 0);
1335
1336         wpid = wait(&status);
1337         ATF_REQUIRE(wpid == -1);
1338         ATF_REQUIRE(errno == ECHILD);
1339 }
1340
1341 /*
1342  * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1343  */
1344 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1345 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1346 {
1347         pid_t fpid, wpid;
1348         int events, status;
1349
1350         ATF_REQUIRE((fpid = fork()) != -1);
1351         if (fpid == 0) {
1352                 trace_me();
1353                 exec_thread(NULL);
1354         }
1355
1356         /* The first wait() should report the stop from SIGSTOP. */
1357         wpid = waitpid(fpid, &status, 0);
1358         ATF_REQUIRE(wpid == fpid);
1359         ATF_REQUIRE(WIFSTOPPED(status));
1360         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1361
1362         events = 0;
1363         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1364             sizeof(events)) == 0);
1365
1366         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1367
1368         /* Should get one event at exit. */
1369         wpid = waitpid(fpid, &status, 0);
1370         ATF_REQUIRE(WIFEXITED(status));
1371         ATF_REQUIRE(WEXITSTATUS(status) == 0);
1372
1373         wpid = wait(&status);
1374         ATF_REQUIRE(wpid == -1);
1375         ATF_REQUIRE(errno == ECHILD);
1376 }
1377
1378 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1379 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1380 {
1381         struct ptrace_lwpinfo pl;
1382         pid_t fpid, wpid;
1383         int events, status;
1384
1385         ATF_REQUIRE((fpid = fork()) != -1);
1386         if (fpid == 0) {
1387                 trace_me();
1388                 exec_thread(NULL);
1389         }
1390
1391         /* The first wait() should report the stop from SIGSTOP. */
1392         wpid = waitpid(fpid, &status, 0);
1393         ATF_REQUIRE(wpid == fpid);
1394         ATF_REQUIRE(WIFSTOPPED(status));
1395         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1396
1397         events = PTRACE_EXEC;
1398         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1399             sizeof(events)) == 0);
1400
1401         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1402
1403         /* The next event should be for the child process's exec. */
1404         wpid = waitpid(fpid, &status, 0);
1405         ATF_REQUIRE(WIFSTOPPED(status));
1406         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1407
1408         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1409         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)) ==
1410             (PL_FLAG_EXEC | PL_FLAG_SCX));
1411
1412         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1413
1414         /* The last event should be for the child process's exit. */
1415         wpid = waitpid(fpid, &status, 0);
1416         ATF_REQUIRE(WIFEXITED(status));
1417         ATF_REQUIRE(WEXITSTATUS(status) == 0);
1418
1419         wpid = wait(&status);
1420         ATF_REQUIRE(wpid == -1);
1421         ATF_REQUIRE(errno == ECHILD);
1422 }
1423
1424 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1425 ATF_TC_BODY(ptrace__event_mask, tc)
1426 {
1427         pid_t fpid, wpid;
1428         int events, status;
1429
1430         ATF_REQUIRE((fpid = fork()) != -1);
1431         if (fpid == 0) {
1432                 trace_me();
1433                 exit(0);
1434         }
1435
1436         /* The first wait() should report the stop from SIGSTOP. */
1437         wpid = waitpid(fpid, &status, 0);
1438         ATF_REQUIRE(wpid == fpid);
1439         ATF_REQUIRE(WIFSTOPPED(status));
1440         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1441
1442         /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1443         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1444         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1445             sizeof(events)) == 0);
1446         ATF_REQUIRE(events & PTRACE_FORK);
1447         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1448         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1449             sizeof(events)) == 0);
1450         ATF_REQUIRE(!(events & PTRACE_FORK));
1451
1452         /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1453         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1454         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1455             sizeof(events)) == 0);
1456         ATF_REQUIRE(events & PTRACE_LWP);
1457         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1458         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1459             sizeof(events)) == 0);
1460         ATF_REQUIRE(!(events & PTRACE_LWP));
1461
1462         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1463
1464         /* Should get one event at exit. */
1465         wpid = waitpid(fpid, &status, 0);
1466         ATF_REQUIRE(WIFEXITED(status));
1467         ATF_REQUIRE(WEXITSTATUS(status) == 0);
1468
1469         wpid = wait(&status);
1470         ATF_REQUIRE(wpid == -1);
1471         ATF_REQUIRE(errno == ECHILD);
1472 }
1473
1474 /*
1475  * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1476  */
1477 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1478 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1479 {
1480         struct ptrace_lwpinfo pl;
1481         pid_t fpid, wpid;
1482         int events, status;
1483
1484         ATF_REQUIRE((fpid = fork()) != -1);
1485         if (fpid == 0) {
1486                 trace_me();
1487                 follow_fork_parent(true);
1488         }
1489
1490         /* The first wait() should report the stop from SIGSTOP. */
1491         wpid = waitpid(fpid, &status, 0);
1492         ATF_REQUIRE(wpid == fpid);
1493         ATF_REQUIRE(WIFSTOPPED(status));
1494         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1495
1496         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1497             sizeof(events)) == 0);
1498         events |= PTRACE_VFORK;
1499         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1500             sizeof(events)) == 0);
1501         
1502         /* Continue the child ignoring the SIGSTOP. */
1503         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1504
1505         /* The next event should report the end of the vfork. */
1506         wpid = wait(&status);
1507         ATF_REQUIRE(wpid == fpid);
1508         ATF_REQUIRE(WIFSTOPPED(status));
1509         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1510         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1511         ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1512
1513         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1514
1515         wpid = wait(&status);
1516         ATF_REQUIRE(wpid == fpid);
1517         ATF_REQUIRE(WIFEXITED(status));
1518         ATF_REQUIRE(WEXITSTATUS(status) == 1);
1519
1520         wpid = wait(&status);
1521         ATF_REQUIRE(wpid == -1);
1522         ATF_REQUIRE(errno == ECHILD);
1523 }
1524
1525 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1526 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1527 {
1528         struct ptrace_lwpinfo pl[2];
1529         pid_t children[2], fpid, wpid;
1530         int events, status;
1531
1532         ATF_REQUIRE((fpid = fork()) != -1);
1533         if (fpid == 0) {
1534                 trace_me();
1535                 follow_fork_parent(true);
1536         }
1537
1538         /* Parent process. */
1539         children[0] = fpid;
1540
1541         /* The first wait() should report the stop from SIGSTOP. */
1542         wpid = waitpid(children[0], &status, 0);
1543         ATF_REQUIRE(wpid == children[0]);
1544         ATF_REQUIRE(WIFSTOPPED(status));
1545         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1546
1547         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1548             sizeof(events)) == 0);
1549         events |= PTRACE_FORK | PTRACE_VFORK;
1550         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1551             sizeof(events)) == 0);
1552
1553         /* Continue the child ignoring the SIGSTOP. */
1554         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1555
1556         /* Wait for both halves of the fork event to get reported. */
1557         children[1] = handle_fork_events(children[0], pl);
1558         ATF_REQUIRE(children[1] > 0);
1559
1560         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1561
1562         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1563         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1564
1565         /*
1566          * The child can't exit until the grandchild reports status, so the
1567          * grandchild should report its exit first to the debugger.
1568          */
1569         wpid = waitpid(children[1], &status, 0);
1570         ATF_REQUIRE(wpid == children[1]);
1571         ATF_REQUIRE(WIFEXITED(status));
1572         ATF_REQUIRE(WEXITSTATUS(status) == 2);
1573
1574         /*
1575          * The child should report it's vfork() completion before it
1576          * exits.
1577          */
1578         wpid = wait(&status);
1579         ATF_REQUIRE(wpid == children[0]);
1580         ATF_REQUIRE(WIFSTOPPED(status));
1581         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1582         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1583             -1);
1584         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1585
1586         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1587
1588         wpid = wait(&status);
1589         ATF_REQUIRE(wpid == children[0]);
1590         ATF_REQUIRE(WIFEXITED(status));
1591         ATF_REQUIRE(WEXITSTATUS(status) == 1);
1592
1593         wpid = wait(&status);
1594         ATF_REQUIRE(wpid == -1);
1595         ATF_REQUIRE(errno == ECHILD);
1596 }
1597
1598 /*
1599  * XXX: There's nothing inherently platform specific about this test, however a
1600  * userspace visible breakpoint() is a prerequisite.
1601  */
1602  #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
1603 /*
1604  * Verify that no more events are reported after PT_KILL except for the
1605  * process exit when stopped due to a breakpoint trap.
1606  */
1607 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1608 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1609 {
1610         pid_t fpid, wpid;
1611         int status;
1612
1613         ATF_REQUIRE((fpid = fork()) != -1);
1614         if (fpid == 0) {
1615                 trace_me();
1616                 breakpoint();
1617                 exit(1);
1618         }
1619
1620         /* The first wait() should report the stop from SIGSTOP. */
1621         wpid = waitpid(fpid, &status, 0);
1622         ATF_REQUIRE(wpid == fpid);
1623         ATF_REQUIRE(WIFSTOPPED(status));
1624         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1625
1626         /* Continue the child ignoring the SIGSTOP. */
1627         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1628
1629         /* The second wait() should report hitting the breakpoint. */
1630         wpid = waitpid(fpid, &status, 0);
1631         ATF_REQUIRE(wpid == fpid);
1632         ATF_REQUIRE(WIFSTOPPED(status));
1633         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1634
1635         /* Kill the child process. */
1636         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1637
1638         /* The last wait() should report the SIGKILL. */
1639         wpid = waitpid(fpid, &status, 0);
1640         ATF_REQUIRE(wpid == fpid);
1641         ATF_REQUIRE(WIFSIGNALED(status));
1642         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1643
1644         wpid = wait(&status);
1645         ATF_REQUIRE(wpid == -1);
1646         ATF_REQUIRE(errno == ECHILD);
1647 }
1648 #endif /* defined(__amd64__) || defined(__i386__) || defined(__sparc64__) */
1649
1650 /*
1651  * Verify that no more events are reported after PT_KILL except for the
1652  * process exit when stopped inside of a system call.
1653  */
1654 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1655 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1656 {
1657         struct ptrace_lwpinfo pl;
1658         pid_t fpid, wpid;
1659         int status;
1660
1661         ATF_REQUIRE((fpid = fork()) != -1);
1662         if (fpid == 0) {
1663                 trace_me();
1664                 getpid();
1665                 exit(1);
1666         }
1667
1668         /* The first wait() should report the stop from SIGSTOP. */
1669         wpid = waitpid(fpid, &status, 0);
1670         ATF_REQUIRE(wpid == fpid);
1671         ATF_REQUIRE(WIFSTOPPED(status));
1672         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1673
1674         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1675         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1676
1677         /* The second wait() should report a system call entry for getpid(). */
1678         wpid = waitpid(fpid, &status, 0);
1679         ATF_REQUIRE(wpid == fpid);
1680         ATF_REQUIRE(WIFSTOPPED(status));
1681         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1682
1683         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1684         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1685
1686         /* Kill the child process. */
1687         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1688
1689         /* The last wait() should report the SIGKILL. */
1690         wpid = waitpid(fpid, &status, 0);
1691         ATF_REQUIRE(wpid == fpid);
1692         ATF_REQUIRE(WIFSIGNALED(status));
1693         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1694
1695         wpid = wait(&status);
1696         ATF_REQUIRE(wpid == -1);
1697         ATF_REQUIRE(errno == ECHILD);
1698 }
1699
1700 /*
1701  * Verify that no more events are reported after PT_KILL except for the
1702  * process exit when killing a multithreaded process.
1703  */
1704 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1705 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1706 {
1707         struct ptrace_lwpinfo pl;
1708         pid_t fpid, wpid;
1709         lwpid_t main_lwp;
1710         int status;
1711
1712         ATF_REQUIRE((fpid = fork()) != -1);
1713         if (fpid == 0) {
1714                 trace_me();
1715                 simple_thread_main();
1716         }
1717
1718         /* The first wait() should report the stop from SIGSTOP. */
1719         wpid = waitpid(fpid, &status, 0);
1720         ATF_REQUIRE(wpid == fpid);
1721         ATF_REQUIRE(WIFSTOPPED(status));
1722         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1723
1724         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1725             sizeof(pl)) != -1);
1726         main_lwp = pl.pl_lwpid;
1727
1728         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, wpid, NULL, 1) == 0);
1729
1730         /* Continue the child ignoring the SIGSTOP. */
1731         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1732
1733         /* The first event should be for the child thread's birth. */
1734         wpid = waitpid(fpid, &status, 0);
1735         ATF_REQUIRE(wpid == fpid);
1736         ATF_REQUIRE(WIFSTOPPED(status));
1737         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1738                 
1739         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1740         ATF_REQUIRE((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)) ==
1741             (PL_FLAG_BORN | PL_FLAG_SCX));
1742         ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1743
1744         /* Kill the child process. */
1745         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1746
1747         /* The last wait() should report the SIGKILL. */
1748         wpid = waitpid(fpid, &status, 0);
1749         ATF_REQUIRE(wpid == fpid);
1750         ATF_REQUIRE(WIFSIGNALED(status));
1751         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1752
1753         wpid = wait(&status);
1754         ATF_REQUIRE(wpid == -1);
1755         ATF_REQUIRE(errno == ECHILD);
1756 }
1757
1758 static void *
1759 mask_usr1_thread(void *arg)
1760 {
1761         pthread_barrier_t *pbarrier;
1762         sigset_t sigmask;
1763
1764         pbarrier = (pthread_barrier_t*)arg;
1765
1766         sigemptyset(&sigmask);
1767         sigaddset(&sigmask, SIGUSR1);
1768         CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1769
1770         /* Sync up with other thread after sigmask updated. */
1771         pthread_barrier_wait(pbarrier);
1772
1773         for (;;)
1774                 sleep(60);
1775
1776         return (NULL);
1777 }
1778
1779 /*
1780  * Verify that the SIGKILL from PT_KILL takes priority over other signals
1781  * and prevents spurious stops due to those other signals.
1782  */
1783 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_signal);
1784 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
1785 {
1786         pid_t fpid, wpid;
1787         int status;
1788         cpuset_t setmask;
1789         pthread_t t;
1790         pthread_barrier_t barrier;
1791         struct sched_param sched_param;
1792
1793         ATF_REQUIRE((fpid = fork()) != -1);
1794         if (fpid == 0) {
1795                 /* Bind to one CPU so only one thread at a time will run. */
1796                 CPU_ZERO(&setmask);
1797                 CPU_SET(0, &setmask);
1798                 cpusetid_t setid;
1799                 CHILD_REQUIRE(cpuset(&setid) == 0);
1800                 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1801                     CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1802
1803                 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1804
1805                 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1806                     (void*)&barrier) == 0);
1807
1808                 /*
1809                  * Give the main thread higher priority. The test always
1810                  * assumes that, if both threads are able to run, the main
1811                  * thread runs first.
1812                  */
1813                 sched_param.sched_priority =
1814                     (sched_get_priority_max(SCHED_FIFO) +
1815                     sched_get_priority_min(SCHED_FIFO)) / 2;
1816                 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1817                     SCHED_FIFO, &sched_param) == 0);
1818                 sched_param.sched_priority -= RQ_PPQ;
1819                 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1820                     &sched_param) == 0);
1821
1822                 sigset_t sigmask;
1823                 sigemptyset(&sigmask);
1824                 sigaddset(&sigmask, SIGUSR2);
1825                 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1826
1827                 /* Sync up with other thread after sigmask updated. */
1828                 pthread_barrier_wait(&barrier);
1829
1830                 trace_me();
1831
1832                 for (;;)
1833                         sleep(60);
1834
1835                 exit(1);
1836         }
1837
1838         /* The first wait() should report the stop from SIGSTOP. */
1839         wpid = waitpid(fpid, &status, 0);
1840         ATF_REQUIRE(wpid == fpid);
1841         ATF_REQUIRE(WIFSTOPPED(status));
1842         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1843
1844         /* Continue the child ignoring the SIGSTOP. */
1845         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
1846
1847         /* Send a signal that only the second thread can handle. */
1848         ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1849
1850         /* The second wait() should report the SIGUSR2. */
1851         wpid = waitpid(fpid, &status, 0);
1852         ATF_REQUIRE(wpid == fpid);
1853         ATF_REQUIRE(WIFSTOPPED(status));
1854         ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
1855
1856         /* Send a signal that only the first thread can handle. */
1857         ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
1858
1859         /* Replace the SIGUSR2 with a kill. */
1860         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
1861
1862         /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
1863         wpid = waitpid(fpid, &status, 0);
1864         ATF_REQUIRE(wpid == fpid);
1865         ATF_REQUIRE(WIFSIGNALED(status));
1866         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
1867
1868         wpid = wait(&status);
1869         ATF_REQUIRE(wpid == -1);
1870         ATF_REQUIRE(errno == ECHILD);
1871 }
1872
1873 /*
1874  * Verify that the SIGKILL from PT_KILL takes priority over other stop events
1875  * and prevents spurious stops caused by those events.
1876  */
1877 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_competing_stop);
1878 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
1879 {
1880         pid_t fpid, wpid;
1881         int status;
1882         cpuset_t setmask;
1883         pthread_t t;
1884         pthread_barrier_t barrier;
1885         lwpid_t main_lwp;
1886         struct ptrace_lwpinfo pl;
1887         struct sched_param sched_param;
1888
1889         ATF_REQUIRE((fpid = fork()) != -1);
1890         if (fpid == 0) {
1891                 trace_me();
1892
1893                 /* Bind to one CPU so only one thread at a time will run. */
1894                 CPU_ZERO(&setmask);
1895                 CPU_SET(0, &setmask);
1896                 cpusetid_t setid;
1897                 CHILD_REQUIRE(cpuset(&setid) == 0);
1898                 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
1899                     CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
1900
1901                 CHILD_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
1902
1903                 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
1904                     (void*)&barrier) == 0);
1905
1906                 /*
1907                  * Give the main thread higher priority. The test always
1908                  * assumes that, if both threads are able to run, the main
1909                  * thread runs first.
1910                  */
1911                 sched_param.sched_priority =
1912                     (sched_get_priority_max(SCHED_FIFO) +
1913                     sched_get_priority_min(SCHED_FIFO)) / 2;
1914                 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
1915                     SCHED_FIFO, &sched_param) == 0);
1916                 sched_param.sched_priority -= RQ_PPQ;
1917                 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
1918                     &sched_param) == 0);
1919
1920                 sigset_t sigmask;
1921                 sigemptyset(&sigmask);
1922                 sigaddset(&sigmask, SIGUSR2);
1923                 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
1924
1925                 /* Sync up with other thread after sigmask updated. */
1926                 pthread_barrier_wait(&barrier);
1927
1928                 /* Sync up with the test before doing the getpid(). */
1929                 raise(SIGSTOP);
1930
1931                 getpid();
1932                 exit(1);
1933         }
1934
1935         /* The first wait() should report the stop from SIGSTOP. */
1936         wpid = waitpid(fpid, &status, 0);
1937         ATF_REQUIRE(wpid == fpid);
1938         ATF_REQUIRE(WIFSTOPPED(status));
1939         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1940
1941         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1942         main_lwp = pl.pl_lwpid;
1943
1944         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1945         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1946
1947         /*
1948          * Continue until child is done with setup, which is indicated with
1949          * SIGSTOP. Ignore system calls in the meantime.
1950          */
1951         for (;;) {
1952                 wpid = waitpid(fpid, &status, 0);
1953                 ATF_REQUIRE(wpid == fpid);
1954                 ATF_REQUIRE(WIFSTOPPED(status));
1955                 if (WSTOPSIG(status) == SIGTRAP) {
1956                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1957                             sizeof(pl)) != -1);
1958                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
1959                 } else {
1960                         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
1961                         break;
1962                 }
1963                 ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1964         }
1965
1966         /* Proceed, allowing main thread to hit syscall entry for getpid(). */
1967         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1968
1969         wpid = waitpid(fpid, &status, 0);
1970         ATF_REQUIRE(wpid == fpid);
1971         ATF_REQUIRE(WIFSTOPPED(status));
1972         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1973
1974         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1975             sizeof(pl)) != -1);
1976         ATF_REQUIRE(pl.pl_lwpid == main_lwp);
1977         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1978         /* Prevent the main thread from hitting its syscall exit for now. */
1979         ATF_REQUIRE(ptrace(PT_SUSPEND, main_lwp, 0, 0) == 0);
1980
1981         /*
1982          * Proceed, allowing second thread to hit syscall exit for
1983          * pthread_barrier_wait().
1984          */
1985         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
1986
1987         wpid = waitpid(fpid, &status, 0);
1988         ATF_REQUIRE(wpid == fpid);
1989         ATF_REQUIRE(WIFSTOPPED(status));
1990         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
1991
1992         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1993             sizeof(pl)) != -1);
1994         ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1995         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
1996
1997         /* Send a signal that only the second thread can handle. */
1998         ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
1999
2000         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2001
2002         /* The next wait() should report the SIGUSR2. */
2003         wpid = waitpid(fpid, &status, 0);
2004         ATF_REQUIRE(wpid == fpid);
2005         ATF_REQUIRE(WIFSTOPPED(status));
2006         ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2007
2008         /* Allow the main thread to try to finish its system call. */
2009         ATF_REQUIRE(ptrace(PT_RESUME, main_lwp, 0, 0) == 0);
2010
2011         /*
2012          * At this point, the main thread is in the middle of a system call and
2013          * has been resumed. The second thread has taken a SIGUSR2 which will
2014          * be replaced with a SIGKILL below. The main thread will get to run
2015          * first. It should notice the kill request (even though the signal
2016          * replacement occurred in the other thread) and exit accordingly.  It
2017          * should not stop for the system call exit event.
2018          */
2019
2020         /* Replace the SIGUSR2 with a kill. */
2021         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2022
2023         /* The last wait() should report the SIGKILL (not a syscall exit). */
2024         wpid = waitpid(fpid, &status, 0);
2025         ATF_REQUIRE(wpid == fpid);
2026         ATF_REQUIRE(WIFSIGNALED(status));
2027         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2028
2029         wpid = wait(&status);
2030         ATF_REQUIRE(wpid == -1);
2031         ATF_REQUIRE(errno == ECHILD);
2032 }
2033
2034 static void
2035 sigusr1_handler(int sig)
2036 {
2037
2038         CHILD_REQUIRE(sig == SIGUSR1);
2039         _exit(2);
2040 }
2041
2042 /*
2043  * Verify that even if the signal queue is full for a child process,
2044  * a PT_KILL will kill the process.
2045  */
2046 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2047 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2048 {
2049         pid_t fpid, wpid;
2050         int status;
2051         int max_pending_per_proc;
2052         size_t len;
2053         int i;
2054
2055         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2056
2057         ATF_REQUIRE((fpid = fork()) != -1);
2058         if (fpid == 0) {
2059                 trace_me();
2060                 exit(1);
2061         }
2062
2063         /* The first wait() should report the stop from SIGSTOP. */
2064         wpid = waitpid(fpid, &status, 0);
2065         ATF_REQUIRE(wpid == fpid);
2066         ATF_REQUIRE(WIFSTOPPED(status));
2067         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2068
2069         len = sizeof(max_pending_per_proc);
2070         ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2071             &max_pending_per_proc, &len, NULL, 0) == 0);
2072
2073         /* Fill the signal queue. */
2074         for (i = 0; i < max_pending_per_proc; ++i)
2075                 ATF_REQUIRE(kill(fpid, SIGUSR1) == 0);
2076
2077         /* Kill the child process. */
2078         ATF_REQUIRE(ptrace(PT_KILL, fpid, 0, 0) == 0);
2079
2080         /* The last wait() should report the SIGKILL. */
2081         wpid = waitpid(fpid, &status, 0);
2082         ATF_REQUIRE(wpid == fpid);
2083         ATF_REQUIRE(WIFSIGNALED(status));
2084         ATF_REQUIRE(WTERMSIG(status) == SIGKILL);
2085
2086         wpid = wait(&status);
2087         ATF_REQUIRE(wpid == -1);
2088         ATF_REQUIRE(errno == ECHILD);
2089 }
2090
2091 /*
2092  * Verify that when stopped at a system call entry, a signal can be
2093  * requested with PT_CONTINUE which will be delivered once the system
2094  * call is complete.
2095  */
2096 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2097 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2098 {
2099         struct ptrace_lwpinfo pl;
2100         pid_t fpid, wpid;
2101         int status;
2102
2103         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2104
2105         ATF_REQUIRE((fpid = fork()) != -1);
2106         if (fpid == 0) {
2107                 trace_me();
2108                 getpid();
2109                 exit(1);
2110         }
2111
2112         /* The first wait() should report the stop from SIGSTOP. */
2113         wpid = waitpid(fpid, &status, 0);
2114         ATF_REQUIRE(wpid == fpid);
2115         ATF_REQUIRE(WIFSTOPPED(status));
2116         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2117
2118         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2119         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2120
2121         /* The second wait() should report a system call entry for getpid(). */
2122         wpid = waitpid(fpid, &status, 0);
2123         ATF_REQUIRE(wpid == fpid);
2124         ATF_REQUIRE(WIFSTOPPED(status));
2125         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2126
2127         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2128         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2129
2130         /* Continue the child process with a signal. */
2131         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2132
2133         for (;;) {
2134                 /*
2135                  * The last wait() should report exit 2, i.e., a normal _exit
2136                  * from the signal handler. In the meantime, catch and proceed
2137                  * past any syscall stops.
2138                  */
2139                 wpid = waitpid(fpid, &status, 0);
2140                 ATF_REQUIRE(wpid == fpid);
2141                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2142                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2143                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2144                         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2145                 } else {
2146                         ATF_REQUIRE(WIFEXITED(status));
2147                         ATF_REQUIRE(WEXITSTATUS(status) == 2);
2148                         break;
2149                 }
2150         }
2151
2152         wpid = wait(&status);
2153         ATF_REQUIRE(wpid == -1);
2154         ATF_REQUIRE(errno == ECHILD);
2155 }
2156
2157 static void
2158 sigusr1_counting_handler(int sig)
2159 {
2160         static int counter = 0;
2161
2162         CHILD_REQUIRE(sig == SIGUSR1);
2163         counter++;
2164         if (counter == 2)
2165                 _exit(2);
2166 }
2167
2168 /*
2169  * Verify that, when continuing from a stop at system call entry and exit,
2170  * a signal can be requested from both stops, and both will be delivered when
2171  * the system call is complete.
2172  */
2173 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2174 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2175 {
2176         struct ptrace_lwpinfo pl;
2177         pid_t fpid, wpid;
2178         int status;
2179
2180         ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2181
2182         ATF_REQUIRE((fpid = fork()) != -1);
2183         if (fpid == 0) {
2184                 trace_me();
2185                 getpid();
2186                 exit(1);
2187         }
2188
2189         /* The first wait() should report the stop from SIGSTOP. */
2190         wpid = waitpid(fpid, &status, 0);
2191         ATF_REQUIRE(wpid == fpid);
2192         ATF_REQUIRE(WIFSTOPPED(status));
2193         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2194
2195         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2196         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2197
2198         /* The second wait() should report a system call entry for getpid(). */
2199         wpid = waitpid(fpid, &status, 0);
2200         ATF_REQUIRE(wpid == fpid);
2201         ATF_REQUIRE(WIFSTOPPED(status));
2202         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2203
2204         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2205         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2206
2207         /* Continue the child process with a signal. */
2208         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2209
2210         /* The third wait() should report a system call exit for getpid(). */
2211         wpid = waitpid(fpid, &status, 0);
2212         ATF_REQUIRE(wpid == fpid);
2213         ATF_REQUIRE(WIFSTOPPED(status));
2214         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2215
2216         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2217         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2218
2219         /* Continue the child process with a signal. */
2220         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2221
2222         for (;;) {
2223                 /*
2224                  * The last wait() should report exit 2, i.e., a normal _exit
2225                  * from the signal handler. In the meantime, catch and proceed
2226                  * past any syscall stops.
2227                  */
2228                 wpid = waitpid(fpid, &status, 0);
2229                 ATF_REQUIRE(wpid == fpid);
2230                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2231                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2232                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2233                         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2234                 } else {
2235                         ATF_REQUIRE(WIFEXITED(status));
2236                         ATF_REQUIRE(WEXITSTATUS(status) == 2);
2237                         break;
2238                 }
2239         }
2240
2241         wpid = wait(&status);
2242         ATF_REQUIRE(wpid == -1);
2243         ATF_REQUIRE(errno == ECHILD);
2244 }
2245
2246 /*
2247  * Verify that even if the signal queue is full for a child process,
2248  * a PT_CONTINUE with a signal will not result in loss of that signal.
2249  */
2250 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2251 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2252 {
2253         pid_t fpid, wpid;
2254         int status;
2255         int max_pending_per_proc;
2256         size_t len;
2257         int i;
2258
2259         ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2260         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2261
2262         ATF_REQUIRE((fpid = fork()) != -1);
2263         if (fpid == 0) {
2264                 trace_me();
2265                 exit(1);
2266         }
2267
2268         /* The first wait() should report the stop from SIGSTOP. */
2269         wpid = waitpid(fpid, &status, 0);
2270         ATF_REQUIRE(wpid == fpid);
2271         ATF_REQUIRE(WIFSTOPPED(status));
2272         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2273
2274         len = sizeof(max_pending_per_proc);
2275         ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2276             &max_pending_per_proc, &len, NULL, 0) == 0);
2277
2278         /* Fill the signal queue. */
2279         for (i = 0; i < max_pending_per_proc; ++i)
2280                 ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2281
2282         /* Continue with signal. */
2283         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2284
2285         for (;;) {
2286                 wpid = waitpid(fpid, &status, 0);
2287                 ATF_REQUIRE(wpid == fpid);
2288                 if (WIFSTOPPED(status)) {
2289                         ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2290                         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2291                 } else {
2292                         /*
2293                          * The last wait() should report normal _exit from the
2294                          * SIGUSR1 handler.
2295                          */
2296                         ATF_REQUIRE(WIFEXITED(status));
2297                         ATF_REQUIRE(WEXITSTATUS(status) == 2);
2298                         break;
2299                 }
2300         }
2301
2302         wpid = wait(&status);
2303         ATF_REQUIRE(wpid == -1);
2304         ATF_REQUIRE(errno == ECHILD);
2305 }
2306
2307 /*
2308  * Verify that, after stopping due to a signal, that signal can be
2309  * replaced with another signal.
2310  */
2311 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2312 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2313 {
2314         struct ptrace_lwpinfo pl;
2315         pid_t fpid, wpid;
2316         int status;
2317
2318         ATF_REQUIRE((fpid = fork()) != -1);
2319         if (fpid == 0) {
2320                 trace_me();
2321                 sleep(20);
2322                 exit(1);
2323         }
2324
2325         /* The first wait() should report the stop from SIGSTOP. */
2326         wpid = waitpid(fpid, &status, 0);
2327         ATF_REQUIRE(wpid == fpid);
2328         ATF_REQUIRE(WIFSTOPPED(status));
2329         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2330
2331         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2332
2333         /* Send a signal without ptrace. */
2334         ATF_REQUIRE(kill(fpid, SIGINT) == 0);
2335
2336         /* The second wait() should report a SIGINT was received. */
2337         wpid = waitpid(fpid, &status, 0);
2338         ATF_REQUIRE(wpid == fpid);
2339         ATF_REQUIRE(WIFSTOPPED(status));
2340         ATF_REQUIRE(WSTOPSIG(status) == SIGINT);
2341
2342         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2343         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2344         ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGINT);
2345
2346         /* Continue the child process with a different signal. */
2347         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM) == 0);
2348
2349         /*
2350          * The last wait() should report having died due to the new
2351          * signal, SIGTERM.
2352          */
2353         wpid = waitpid(fpid, &status, 0);
2354         ATF_REQUIRE(wpid == fpid);
2355         ATF_REQUIRE(WIFSIGNALED(status));
2356         ATF_REQUIRE(WTERMSIG(status) == SIGTERM);
2357
2358         wpid = wait(&status);
2359         ATF_REQUIRE(wpid == -1);
2360         ATF_REQUIRE(errno == ECHILD);
2361 }
2362
2363 /*
2364  * Verify that a signal can be passed through to the child even when there
2365  * was no true signal originally. Such cases arise when a SIGTRAP is
2366  * invented for e.g, system call stops.
2367  */
2368 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2369 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2370 {
2371         struct ptrace_lwpinfo pl;
2372         pid_t fpid, wpid;
2373         int status;
2374
2375         ATF_REQUIRE((fpid = fork()) != -1);
2376         if (fpid == 0) {
2377                 trace_me();
2378                 getpid();
2379                 exit(1);
2380         }
2381
2382         /* The first wait() should report the stop from SIGSTOP. */
2383         wpid = waitpid(fpid, &status, 0);
2384         ATF_REQUIRE(wpid == fpid);
2385         ATF_REQUIRE(WIFSTOPPED(status));
2386         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2387
2388         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2389         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2390
2391         /* The second wait() should report a system call entry for getpid(). */
2392         wpid = waitpid(fpid, &status, 0);
2393         ATF_REQUIRE(wpid == fpid);
2394         ATF_REQUIRE(WIFSTOPPED(status));
2395         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2396
2397         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2398         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2399
2400         /* Continue the child process with a SIGTRAP. */
2401         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP) == 0);
2402
2403         for (;;) {
2404                 /*
2405                  * The last wait() should report exit due to SIGTRAP.  In the
2406                  * meantime, catch and proceed past any syscall stops.
2407                  */
2408                 wpid = waitpid(fpid, &status, 0);
2409                 ATF_REQUIRE(wpid == fpid);
2410                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2411                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2412                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2413                         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2414                 } else {
2415                         ATF_REQUIRE(WIFSIGNALED(status));
2416                         ATF_REQUIRE(WTERMSIG(status) == SIGTRAP);
2417                         break;
2418                 }
2419         }
2420
2421         wpid = wait(&status);
2422         ATF_REQUIRE(wpid == -1);
2423         ATF_REQUIRE(errno == ECHILD);
2424
2425 }
2426
2427 /*
2428  * A mixed bag PT_CONTINUE with signal test.
2429  */
2430 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2431 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2432 {
2433         struct ptrace_lwpinfo pl;
2434         pid_t fpid, wpid;
2435         int status;
2436
2437         ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2438
2439         ATF_REQUIRE((fpid = fork()) != -1);
2440         if (fpid == 0) {
2441                 trace_me();
2442                 getpid();
2443                 exit(1);
2444         }
2445
2446         /* The first wait() should report the stop from SIGSTOP. */
2447         wpid = waitpid(fpid, &status, 0);
2448         ATF_REQUIRE(wpid == fpid);
2449         ATF_REQUIRE(WIFSTOPPED(status));
2450         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2451
2452         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2453         ATF_REQUIRE(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0) == 0);
2454
2455         /* The second wait() should report a system call entry for getpid(). */
2456         wpid = waitpid(fpid, &status, 0);
2457         ATF_REQUIRE(wpid == fpid);
2458         ATF_REQUIRE(WIFSTOPPED(status));
2459         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2460
2461         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2462         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2463
2464         /* Continue with the first SIGUSR1. */
2465         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2466
2467         /* The next wait() should report a system call exit for getpid(). */
2468         wpid = waitpid(fpid, &status, 0);
2469         ATF_REQUIRE(wpid == fpid);
2470         ATF_REQUIRE(WIFSTOPPED(status));
2471         ATF_REQUIRE(WSTOPSIG(status) == SIGTRAP);
2472
2473         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2474         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2475
2476         /* Send an ABRT without ptrace. */
2477         ATF_REQUIRE(kill(fpid, SIGABRT) == 0);
2478
2479         /* Continue normally. */
2480         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2481
2482         /* The next wait() should report the SIGABRT. */
2483         wpid = waitpid(fpid, &status, 0);
2484         ATF_REQUIRE(wpid == fpid);
2485         ATF_REQUIRE(WIFSTOPPED(status));
2486         ATF_REQUIRE(WSTOPSIG(status) == SIGABRT);
2487
2488         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2489         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2490         ATF_REQUIRE(pl.pl_siginfo.si_signo == SIGABRT);
2491
2492         /* Continue, replacing the SIGABRT with another SIGUSR1. */
2493         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2494
2495         for (;;) {
2496                 /*
2497                  * The last wait() should report exit 2, i.e., a normal _exit
2498                  * from the signal handler. In the meantime, catch and proceed
2499                  * past any syscall stops.
2500                  */
2501                 wpid = waitpid(fpid, &status, 0);
2502                 ATF_REQUIRE(wpid == fpid);
2503                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2504                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2505                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2506                         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2507                 } else {
2508                         ATF_REQUIRE(WIFEXITED(status));
2509                         ATF_REQUIRE(WEXITSTATUS(status) == 2);
2510                         break;
2511                 }
2512         }
2513
2514         wpid = wait(&status);
2515         ATF_REQUIRE(wpid == -1);
2516         ATF_REQUIRE(errno == ECHILD);
2517
2518 }
2519
2520 /*
2521  * Verify a signal delivered by ptrace is noticed by kevent(2).
2522  */
2523 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2524 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2525 {
2526         pid_t fpid, wpid;
2527         int status, kq, nevents;
2528         struct kevent kev;
2529
2530         ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2531
2532         ATF_REQUIRE((fpid = fork()) != -1);
2533         if (fpid == 0) {
2534                 CHILD_REQUIRE((kq = kqueue()) > 0);
2535                 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2536                 CHILD_REQUIRE(kevent(kq, &kev, 1, NULL, 0, NULL) == 0);
2537
2538                 trace_me();
2539
2540                 for (;;) {
2541                         nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2542                         if (nevents == -1 && errno == EINTR)
2543                                 continue;
2544                         CHILD_REQUIRE(nevents > 0);
2545                         CHILD_REQUIRE(kev.filter == EVFILT_SIGNAL);
2546                         CHILD_REQUIRE(kev.ident == SIGUSR1);
2547                         break;
2548                 }
2549
2550                 exit(1);
2551         }
2552
2553         /* The first wait() should report the stop from SIGSTOP. */
2554         wpid = waitpid(fpid, &status, 0);
2555         ATF_REQUIRE(wpid == fpid);
2556         ATF_REQUIRE(WIFSTOPPED(status));
2557         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2558
2559         /* Continue with the SIGUSR1. */
2560         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2561
2562         /*
2563          * The last wait() should report normal exit with code 1.
2564          */
2565         wpid = waitpid(fpid, &status, 0);
2566         ATF_REQUIRE(wpid == fpid);
2567         ATF_REQUIRE(WIFEXITED(status));
2568         ATF_REQUIRE(WEXITSTATUS(status) == 1);
2569
2570         wpid = wait(&status);
2571         ATF_REQUIRE(wpid == -1);
2572         ATF_REQUIRE(errno == ECHILD);
2573 }
2574
2575 static sem_t sigusr1_sem;
2576
2577 static void
2578 sigusr1_sempost_handler(int sig __unused)
2579 {
2580
2581         CHILD_REQUIRE(sem_post(&sigusr1_sem) == 0);
2582 }
2583
2584 static void *
2585 signal_thread(void *arg)
2586 {
2587         int err;
2588         sigset_t sigmask;
2589
2590         pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2591
2592         /* Wait for this thread to receive a SIGUSR1. */
2593         do {
2594                 err = sem_wait(&sigusr1_sem);
2595                 CHILD_REQUIRE(err == 0 || errno == EINTR);
2596         } while (err != 0 && errno == EINTR);
2597
2598         /* Free our companion thread from the barrier. */
2599         pthread_barrier_wait(pbarrier);
2600
2601         /*
2602          * Swap ignore duties; the next SIGUSR1 should go to the
2603          * other thread.
2604          */
2605         CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2606         CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2607         CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2608
2609         /* Sync up threads after swapping signal masks. */
2610         pthread_barrier_wait(pbarrier);
2611
2612         /* Wait until our companion has received its SIGUSR1. */
2613         pthread_barrier_wait(pbarrier);
2614
2615         return (NULL);
2616 }
2617
2618 /*
2619  * Verify that if ptrace stops due to a signal but continues with
2620  * a different signal that the new signal is routed to a thread
2621  * that can accept it, and that that thread is awakened by the signal
2622  * in a timely manner.
2623  */
2624 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2625 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
2626 {
2627         pid_t fpid, wpid;
2628         int status, err;
2629         pthread_t t;
2630         sigset_t sigmask;
2631         pthread_barrier_t barrier;
2632
2633         ATF_REQUIRE(pthread_barrier_init(&barrier, NULL, 2) == 0);
2634         ATF_REQUIRE(sem_init(&sigusr1_sem, 0, 0) == 0);
2635         ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2636
2637         ATF_REQUIRE((fpid = fork()) != -1);
2638         if (fpid == 0) {
2639                 CHILD_REQUIRE(pthread_create(&t, NULL, signal_thread, (void*)&barrier) == 0);
2640
2641                 /* The other thread should receive the first SIGUSR1. */
2642                 CHILD_REQUIRE(sigemptyset(&sigmask) == 0);
2643                 CHILD_REQUIRE(sigaddset(&sigmask, SIGUSR1) == 0);
2644                 CHILD_REQUIRE(pthread_sigmask(SIG_BLOCK, &sigmask, NULL) == 0);
2645
2646                 trace_me();
2647
2648                 /* Wait until other thread has received its SIGUSR1. */
2649                 pthread_barrier_wait(&barrier);
2650
2651                 /*
2652                  * Swap ignore duties; the next SIGUSR1 should go to this
2653                  * thread.
2654                  */
2655                 CHILD_REQUIRE(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL) == 0);
2656
2657                 /* Sync up threads after swapping signal masks. */
2658                 pthread_barrier_wait(&barrier);
2659
2660                 /*
2661                  * Sync up with test code; we're ready for the next SIGUSR1
2662                  * now.
2663                  */
2664                 raise(SIGSTOP);
2665
2666                 /* Wait for this thread to receive a SIGUSR1. */
2667                 do {
2668                         err = sem_wait(&sigusr1_sem);
2669                         CHILD_REQUIRE(err == 0 || errno == EINTR);
2670                 } while (err != 0 && errno == EINTR);
2671
2672                 /* Free the other thread from the barrier. */
2673                 pthread_barrier_wait(&barrier);
2674
2675                 CHILD_REQUIRE(pthread_join(t, NULL) == 0);
2676
2677                 exit(1);
2678         }
2679
2680         /* The first wait() should report the stop from SIGSTOP. */
2681         wpid = waitpid(fpid, &status, 0);
2682         ATF_REQUIRE(wpid == fpid);
2683         ATF_REQUIRE(WIFSTOPPED(status));
2684         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2685
2686         /* Continue the child ignoring the SIGSTOP. */
2687         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2688
2689         /*
2690          * Send a signal without ptrace that either thread will accept (USR2,
2691          * in this case).
2692          */
2693         ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2694         
2695         /* The second wait() should report a SIGUSR2 was received. */
2696         wpid = waitpid(fpid, &status, 0);
2697         ATF_REQUIRE(wpid == fpid);
2698         ATF_REQUIRE(WIFSTOPPED(status));
2699         ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2700
2701         /* Continue the child, changing the signal to USR1. */
2702         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2703
2704         /* The next wait() should report the stop from SIGSTOP. */
2705         wpid = waitpid(fpid, &status, 0);
2706         ATF_REQUIRE(wpid == fpid);
2707         ATF_REQUIRE(WIFSTOPPED(status));
2708         ATF_REQUIRE(WSTOPSIG(status) == SIGSTOP);
2709
2710         /* Continue the child ignoring the SIGSTOP. */
2711         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) == 0);
2712
2713         ATF_REQUIRE(kill(fpid, SIGUSR2) == 0);
2714
2715         /* The next wait() should report a SIGUSR2 was received. */
2716         wpid = waitpid(fpid, &status, 0);
2717         ATF_REQUIRE(wpid == fpid);
2718         ATF_REQUIRE(WIFSTOPPED(status));
2719         ATF_REQUIRE(WSTOPSIG(status) == SIGUSR2);
2720
2721         /* Continue the child, changing the signal to USR1. */
2722         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1) == 0);
2723
2724         /* The last wait() should report normal exit with code 1. */
2725         wpid = waitpid(fpid, &status, 0);
2726         ATF_REQUIRE(wpid == fpid);
2727         ATF_REQUIRE(WIFEXITED(status));
2728         ATF_REQUIRE(WEXITSTATUS(status) == 1);
2729
2730         wpid = wait(&status);
2731         ATF_REQUIRE(wpid == -1);
2732         ATF_REQUIRE(errno == ECHILD);
2733 }
2734
2735 ATF_TP_ADD_TCS(tp)
2736 {
2737
2738         ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
2739         ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
2740         ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
2741         ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
2742         ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
2743         ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
2744         ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
2745         ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
2746         ATF_TP_ADD_TC(tp,
2747             ptrace__follow_fork_child_detached_unrelated_debugger);
2748         ATF_TP_ADD_TC(tp,
2749             ptrace__follow_fork_parent_detached_unrelated_debugger);
2750         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
2751         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
2752         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
2753         ATF_TP_ADD_TC(tp, ptrace__lwp_events);
2754         ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
2755         ATF_TP_ADD_TC(tp, ptrace__siginfo);
2756         ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
2757         ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
2758         ATF_TP_ADD_TC(tp, ptrace__event_mask);
2759         ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
2760         ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
2761 #if defined(__amd64__) || defined(__i386__) || defined(__sparc64__)
2762         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
2763 #endif
2764         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
2765         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
2766         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
2767         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
2768         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
2769         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
2770         ATF_TP_ADD_TC(tp,
2771             ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2772         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2773         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
2774         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2775         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
2776         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
2777         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
2778
2779         return (atf_no_error());
2780 }