]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tests/sys/kern/ptrace_test.c
ptrace_test: Add more debug output on test failures
[FreeBSD/FreeBSD.git] / tests / sys / kern / ptrace_test.c
1 /*-
2  * Copyright (c) 2015 John Baldwin <jhb@FreeBSD.org>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23  * SUCH DAMAGE.
24  */
25
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
28
29 #include <sys/types.h>
30 #include <sys/cpuset.h>
31 #include <sys/event.h>
32 #include <sys/file.h>
33 #include <sys/time.h>
34 #include <sys/procctl.h>
35 #include <sys/procdesc.h>
36 #define _WANT_MIPS_REGNUM
37 #include <sys/ptrace.h>
38 #include <sys/queue.h>
39 #include <sys/runq.h>
40 #include <sys/syscall.h>
41 #include <sys/sysctl.h>
42 #include <sys/user.h>
43 #include <sys/wait.h>
44 #include <errno.h>
45 #include <machine/cpufunc.h>
46 #include <pthread.h>
47 #include <sched.h>
48 #include <semaphore.h>
49 #include <signal.h>
50 #include <stdarg.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <unistd.h>
54 #include <atf-c.h>
55
56 /*
57  * Architectures with a user-visible breakpoint().
58  */
59 #if defined(__aarch64__) || defined(__amd64__) || defined(__arm__) ||   \
60     defined(__i386__) || defined(__mips__) || defined(__riscv) ||       \
61     defined(__sparc64__)
62 #define HAVE_BREAKPOINT
63 #endif
64
65 /*
66  * Adjust PC to skip over a breakpoint when stopped for a breakpoint trap.
67  */
68 #ifdef HAVE_BREAKPOINT
69 #if defined(__aarch64__)
70 #define SKIP_BREAK(reg) ((reg)->elr += 4)
71 #elif defined(__amd64__) || defined(__i386__)
72 #define SKIP_BREAK(reg)
73 #elif defined(__arm__)
74 #define SKIP_BREAK(reg) ((reg)->r_pc += 4)
75 #elif defined(__mips__)
76 #define SKIP_BREAK(reg) ((reg)->r_regs[PC] += 4)
77 #elif defined(__riscv)
78 #define SKIP_BREAK(reg) ((reg)->sepc += 4)
79 #elif defined(__sparc64__)
80 #define SKIP_BREAK(reg) do {                                            \
81         (reg)->r_tpc = (reg)->r_tnpc + 4;                               \
82         (reg)->r_tnpc += 8;                                             \
83 } while (0)
84 #endif
85 #endif
86
87 /*
88  * A variant of ATF_REQUIRE that is suitable for use in child
89  * processes.  This only works if the parent process is tripped up by
90  * the early exit and fails some requirement itself.
91  */
92 #define CHILD_REQUIRE(exp) do {                         \
93         if (!(exp))                                     \
94                 child_fail_require(__FILE__, __LINE__,  \
95                     #exp " not met\n");                 \
96 } while (0)
97
98 #define CHILD_REQUIRE_EQ(actual, expected) do {                 \
99         __typeof__(expected) _e = expected;                     \
100         __typeof__(actual) _a = actual;                         \
101         if (_e != _a)                                           \
102                 child_fail_require(__FILE__, __LINE__, #actual  \
103                     " (%jd) == " #expected " (%jd) not met\n",  \
104                     (intmax_t)_a, (intmax_t)_e);                \
105 } while (0)
106
107 static __dead2 void
108 child_fail_require(const char *file, int line, const char *fmt, ...)
109 {
110         va_list ap;
111         char buf[1024];
112
113         /* Use write() not fprintf() to avoid possible duplicate output. */
114         snprintf(buf, sizeof(buf), "%s:%d: ", file, line);
115         write(STDERR_FILENO, buf, strlen(buf));
116         va_start(ap, fmt);
117         vsnprintf(buf, sizeof(buf), fmt, ap);
118         write(STDERR_FILENO, buf, strlen(buf));
119         va_end(ap);
120
121         _exit(32);
122 }
123
124 #define REQUIRE_EQ(actual, expected) do {                               \
125         __typeof__(expected) _e = expected;                             \
126         __typeof__(actual) _a = actual;                                 \
127         ATF_REQUIRE_MSG(_e == _a, #actual " (%jd) == "                  \
128             #expected " (%jd) not met", (intmax_t)_a, (intmax_t)_e);    \
129 } while (0)
130
131 static void
132 trace_me(void)
133 {
134
135         /* Attach the parent process as a tracer of this process. */
136         CHILD_REQUIRE(ptrace(PT_TRACE_ME, 0, NULL, 0) != -1);
137
138         /* Trigger a stop. */
139         raise(SIGSTOP);
140 }
141
142 static void
143 attach_child(pid_t pid)
144 {
145         pid_t wpid;
146         int status;
147
148         REQUIRE_EQ(ptrace(PT_ATTACH, pid, NULL, 0), 0);
149
150         wpid = waitpid(pid, &status, 0);
151         REQUIRE_EQ(wpid, pid);
152         ATF_REQUIRE(WIFSTOPPED(status));
153         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
154 }
155
156 static void
157 wait_for_zombie(pid_t pid)
158 {
159
160         /*
161          * Wait for a process to exit.  This is kind of gross, but
162          * there is not a better way.
163          *
164          * Prior to r325719, the kern.proc.pid.<pid> sysctl failed
165          * with ESRCH.  After that change, a valid struct kinfo_proc
166          * is returned for zombies with ki_stat set to SZOMB.
167          */
168         for (;;) {
169                 struct kinfo_proc kp;
170                 size_t len;
171                 int mib[4];
172
173                 mib[0] = CTL_KERN;
174                 mib[1] = KERN_PROC;
175                 mib[2] = KERN_PROC_PID;
176                 mib[3] = pid;
177                 len = sizeof(kp);
178                 if (sysctl(mib, nitems(mib), &kp, &len, NULL, 0) == -1) {
179                         REQUIRE_EQ(errno, ESRCH);
180                         break;
181                 }
182                 if (kp.ki_stat == SZOMB)
183                         break;
184                 usleep(5000);
185         }
186 }
187
188 /*
189  * Verify that a parent debugger process "sees" the exit of a debugged
190  * process exactly once when attached via PT_TRACE_ME.
191  */
192 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_trace_me);
193 ATF_TC_BODY(ptrace__parent_wait_after_trace_me, tc)
194 {
195         pid_t child, wpid;
196         int status;
197
198         ATF_REQUIRE((child = fork()) != -1);
199         if (child == 0) {
200                 /* Child process. */
201                 trace_me();
202
203                 _exit(1);
204         }
205
206         /* Parent process. */
207
208         /* The first wait() should report the stop from SIGSTOP. */
209         wpid = waitpid(child, &status, 0);
210         REQUIRE_EQ(wpid, child);
211         ATF_REQUIRE(WIFSTOPPED(status));
212         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
213
214         /* Continue the child ignoring the SIGSTOP. */
215         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
216
217         /* The second wait() should report the exit status. */
218         wpid = waitpid(child, &status, 0);
219         REQUIRE_EQ(wpid, child);
220         ATF_REQUIRE(WIFEXITED(status));
221         REQUIRE_EQ(WEXITSTATUS(status), 1);
222
223         /* The child should no longer exist. */
224         wpid = waitpid(child, &status, 0);
225         REQUIRE_EQ(wpid, -1);
226         REQUIRE_EQ(errno, ECHILD);
227 }
228
229 /*
230  * Verify that a parent debugger process "sees" the exit of a debugged
231  * process exactly once when attached via PT_ATTACH.
232  */
233 ATF_TC_WITHOUT_HEAD(ptrace__parent_wait_after_attach);
234 ATF_TC_BODY(ptrace__parent_wait_after_attach, tc)
235 {
236         pid_t child, wpid;
237         int cpipe[2], status;
238         char c;
239
240         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
241                 atf_tc_skip("https://bugs.freebsd.org/244055");
242
243         REQUIRE_EQ(pipe(cpipe), 0);
244         ATF_REQUIRE((child = fork()) != -1);
245         if (child == 0) {
246                 /* Child process. */
247                 close(cpipe[0]);
248
249                 /* Wait for the parent to attach. */
250                 CHILD_REQUIRE_EQ(0, read(cpipe[1], &c, sizeof(c)));
251
252                 _exit(1);
253         }
254         close(cpipe[1]);
255
256         /* Parent process. */
257
258         /* Attach to the child process. */
259         attach_child(child);
260
261         /* Continue the child ignoring the SIGSTOP. */
262         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
263
264         /* Signal the child to exit. */
265         close(cpipe[0]);
266
267         /* The second wait() should report the exit status. */
268         wpid = waitpid(child, &status, 0);
269         REQUIRE_EQ(wpid, child);
270         ATF_REQUIRE(WIFEXITED(status));
271         REQUIRE_EQ(WEXITSTATUS(status), 1);
272
273         /* The child should no longer exist. */
274         wpid = waitpid(child, &status, 0);
275         REQUIRE_EQ(wpid, -1);
276         REQUIRE_EQ(errno, ECHILD);
277 }
278
279 /*
280  * Verify that a parent process "sees" the exit of a debugged process only
281  * after the debugger has seen it.
282  */
283 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_child_debugger);
284 ATF_TC_BODY(ptrace__parent_sees_exit_after_child_debugger, tc)
285 {
286         pid_t child, debugger, wpid;
287         int cpipe[2], dpipe[2], status;
288         char c;
289
290         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
291                 atf_tc_skip("https://bugs.freebsd.org/239399");
292
293         REQUIRE_EQ(pipe(cpipe), 0);
294         ATF_REQUIRE((child = fork()) != -1);
295
296         if (child == 0) {
297                 /* Child process. */
298                 close(cpipe[0]);
299
300                 /* Wait for parent to be ready. */
301                 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)), sizeof(c));
302
303                 _exit(1);
304         }
305         close(cpipe[1]);
306
307         REQUIRE_EQ(pipe(dpipe), 0);
308         ATF_REQUIRE((debugger = fork()) != -1);
309
310         if (debugger == 0) {
311                 /* Debugger process. */
312                 close(dpipe[0]);
313
314                 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
315
316                 wpid = waitpid(child, &status, 0);
317                 CHILD_REQUIRE_EQ(wpid, child);
318                 CHILD_REQUIRE(WIFSTOPPED(status));
319                 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
320
321                 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
322
323                 /* Signal parent that debugger is attached. */
324                 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)), sizeof(c));
325
326                 /* Wait for parent's failed wait. */
327                 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)), 0);
328
329                 wpid = waitpid(child, &status, 0);
330                 CHILD_REQUIRE_EQ(wpid, child);
331                 CHILD_REQUIRE(WIFEXITED(status));
332                 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
333
334                 _exit(0);
335         }
336         close(dpipe[1]);
337
338         /* Parent process. */
339
340         /* Wait for the debugger to attach to the child. */
341         REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), sizeof(c));
342
343         /* Release the child. */
344         REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), sizeof(c));
345         REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
346         close(cpipe[0]);
347
348         wait_for_zombie(child);
349
350         /*
351          * This wait should return a pid of 0 to indicate no status to
352          * report.  The parent should see the child as non-exited
353          * until the debugger sees the exit.
354          */
355         wpid = waitpid(child, &status, WNOHANG);
356         REQUIRE_EQ(wpid, 0);
357
358         /* Signal the debugger to wait for the child. */
359         close(dpipe[0]);
360
361         /* Wait for the debugger. */
362         wpid = waitpid(debugger, &status, 0);
363         REQUIRE_EQ(wpid, debugger);
364         ATF_REQUIRE(WIFEXITED(status));
365         REQUIRE_EQ(WEXITSTATUS(status), 0);
366
367         /* The child process should now be ready. */
368         wpid = waitpid(child, &status, WNOHANG);
369         REQUIRE_EQ(wpid, child);
370         ATF_REQUIRE(WIFEXITED(status));
371         REQUIRE_EQ(WEXITSTATUS(status), 1);
372 }
373
374 /*
375  * Verify that a parent process "sees" the exit of a debugged process
376  * only after a non-direct-child debugger has seen it.  In particular,
377  * various wait() calls in the parent must avoid failing with ESRCH by
378  * checking the parent's orphan list for the debugee.
379  */
380 ATF_TC_WITHOUT_HEAD(ptrace__parent_sees_exit_after_unrelated_debugger);
381 ATF_TC_BODY(ptrace__parent_sees_exit_after_unrelated_debugger, tc)
382 {
383         pid_t child, debugger, fpid, wpid;
384         int cpipe[2], dpipe[2], status;
385         char c;
386
387         REQUIRE_EQ(pipe(cpipe), 0);
388         ATF_REQUIRE((child = fork()) != -1);
389
390         if (child == 0) {
391                 /* Child process. */
392                 close(cpipe[0]);
393
394                 /* Wait for parent to be ready. */
395                 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)), sizeof(c));
396
397                 _exit(1);
398         }
399         close(cpipe[1]);
400
401         REQUIRE_EQ(pipe(dpipe), 0);
402         ATF_REQUIRE((debugger = fork()) != -1);
403
404         if (debugger == 0) {
405                 /* Debugger parent. */
406
407                 /*
408                  * Fork again and drop the debugger parent so that the
409                  * debugger is not a child of the main parent.
410                  */
411                 CHILD_REQUIRE((fpid = fork()) != -1);
412                 if (fpid != 0)
413                         _exit(2);
414
415                 /* Debugger process. */
416                 close(dpipe[0]);
417
418                 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
419
420                 wpid = waitpid(child, &status, 0);
421                 CHILD_REQUIRE_EQ(wpid, child);
422                 CHILD_REQUIRE(WIFSTOPPED(status));
423                 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
424
425                 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
426
427                 /* Signal parent that debugger is attached. */
428                 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)), sizeof(c));
429
430                 /* Wait for parent's failed wait. */
431                 CHILD_REQUIRE_EQ(read(dpipe[1], &c, sizeof(c)), sizeof(c));
432
433                 wpid = waitpid(child, &status, 0);
434                 CHILD_REQUIRE_EQ(wpid, child);
435                 CHILD_REQUIRE(WIFEXITED(status));
436                 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
437
438                 _exit(0);
439         }
440         close(dpipe[1]);
441
442         /* Parent process. */
443
444         /* Wait for the debugger parent process to exit. */
445         wpid = waitpid(debugger, &status, 0);
446         REQUIRE_EQ(wpid, debugger);
447         ATF_REQUIRE(WIFEXITED(status));
448         REQUIRE_EQ(WEXITSTATUS(status), 2);
449
450         /* A WNOHANG wait here should see the non-exited child. */
451         wpid = waitpid(child, &status, WNOHANG);
452         REQUIRE_EQ(wpid, 0);
453
454         /* Wait for the debugger to attach to the child. */
455         REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), sizeof(c));
456
457         /* Release the child. */
458         REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), sizeof(c));
459         REQUIRE_EQ(read(cpipe[0], &c, sizeof(c)), 0);
460         close(cpipe[0]);
461
462         wait_for_zombie(child);
463
464         /*
465          * This wait should return a pid of 0 to indicate no status to
466          * report.  The parent should see the child as non-exited
467          * until the debugger sees the exit.
468          */
469         wpid = waitpid(child, &status, WNOHANG);
470         REQUIRE_EQ(wpid, 0);
471
472         /* Signal the debugger to wait for the child. */
473         REQUIRE_EQ(write(dpipe[0], &c, sizeof(c)), sizeof(c));
474
475         /* Wait for the debugger. */
476         REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), 0);
477         close(dpipe[0]);
478
479         /* The child process should now be ready. */
480         wpid = waitpid(child, &status, WNOHANG);
481         REQUIRE_EQ(wpid, child);
482         ATF_REQUIRE(WIFEXITED(status));
483         REQUIRE_EQ(WEXITSTATUS(status), 1);
484 }
485
486 /*
487  * Make sure that we can collect the exit status of an orphaned process.
488  */
489 ATF_TC_WITHOUT_HEAD(ptrace__parent_exits_before_child);
490 ATF_TC_BODY(ptrace__parent_exits_before_child, tc)
491 {
492         ssize_t n;
493         int cpipe1[2], cpipe2[2], gcpipe[2], status;
494         pid_t child, gchild;
495
496         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
497                 atf_tc_skip("https://bugs.freebsd.org/244056");
498
499         REQUIRE_EQ(pipe(cpipe1), 0);
500         REQUIRE_EQ(pipe(cpipe2), 0);
501         REQUIRE_EQ(pipe(gcpipe), 0);
502
503         REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
504
505         ATF_REQUIRE((child = fork()) != -1);
506         if (child == 0) {
507                 CHILD_REQUIRE((gchild = fork()) != -1);
508                 if (gchild == 0) {
509                         status = 1;
510                         do {
511                                 n = read(gcpipe[0], &status, sizeof(status));
512                         } while (n == -1 && errno == EINTR);
513                         _exit(status);
514                 }
515
516                 CHILD_REQUIRE(write(cpipe1[1], &gchild, sizeof(gchild)) ==
517                     sizeof(gchild));
518                 CHILD_REQUIRE(read(cpipe2[0], &status, sizeof(status)) ==
519                     sizeof(status));
520                 _exit(status);
521         }
522
523         REQUIRE_EQ(read(cpipe1[0], &gchild, sizeof(gchild)), sizeof(gchild));
524
525         REQUIRE_EQ(ptrace(PT_ATTACH, gchild, NULL, 0), 0);
526
527         status = 0;
528         REQUIRE_EQ(write(cpipe2[1], &status, sizeof(status)), sizeof(status));
529         REQUIRE_EQ(waitpid(child, &status, 0), child);
530         ATF_REQUIRE(WIFEXITED(status));
531         REQUIRE_EQ(WEXITSTATUS(status), 0);
532
533         status = 0;
534         REQUIRE_EQ(write(gcpipe[1], &status, sizeof(status)), sizeof(status));
535         REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
536         ATF_REQUIRE(WIFSTOPPED(status));
537         REQUIRE_EQ(ptrace(PT_DETACH, gchild, (caddr_t)1, 0), 0);
538         REQUIRE_EQ(waitpid(gchild, &status, 0), gchild);
539         ATF_REQUIRE(WIFEXITED(status));
540         REQUIRE_EQ(WEXITSTATUS(status), 0);
541
542         REQUIRE_EQ(close(cpipe1[0]), 0);
543         REQUIRE_EQ(close(cpipe1[1]), 0);
544         REQUIRE_EQ(close(cpipe2[0]), 0);
545         REQUIRE_EQ(close(cpipe2[1]), 0);
546         REQUIRE_EQ(close(gcpipe[0]), 0);
547         REQUIRE_EQ(close(gcpipe[1]), 0);
548 }
549
550 /*
551  * The parent process should always act the same regardless of how the
552  * debugger is attached to it.
553  */
554 static __dead2 void
555 follow_fork_parent(bool use_vfork)
556 {
557         pid_t fpid, wpid;
558         int status;
559
560         if (use_vfork)
561                 CHILD_REQUIRE((fpid = vfork()) != -1);
562         else
563                 CHILD_REQUIRE((fpid = fork()) != -1);
564
565         if (fpid == 0)
566                 /* Child */
567                 _exit(2);
568
569         wpid = waitpid(fpid, &status, 0);
570         CHILD_REQUIRE_EQ(wpid, fpid);
571         CHILD_REQUIRE(WIFEXITED(status));
572         CHILD_REQUIRE_EQ(WEXITSTATUS(status), 2);
573
574         _exit(1);
575 }
576
577 /*
578  * Helper routine for follow fork tests.  This waits for two stops
579  * that report both "sides" of a fork.  It returns the pid of the new
580  * child process.
581  */
582 static pid_t
583 handle_fork_events(pid_t parent, struct ptrace_lwpinfo *ppl)
584 {
585         struct ptrace_lwpinfo pl;
586         bool fork_reported[2];
587         pid_t child, wpid;
588         int i, status;
589
590         fork_reported[0] = false;
591         fork_reported[1] = false;
592         child = -1;
593         
594         /*
595          * Each process should report a fork event.  The parent should
596          * report a PL_FLAG_FORKED event, and the child should report
597          * a PL_FLAG_CHILD event.
598          */
599         for (i = 0; i < 2; i++) {
600                 wpid = wait(&status);
601                 ATF_REQUIRE(wpid > 0);
602                 ATF_REQUIRE(WIFSTOPPED(status));
603
604                 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
605                     sizeof(pl)) != -1);
606                 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
607                     0);
608                 ATF_REQUIRE((pl.pl_flags & (PL_FLAG_FORKED | PL_FLAG_CHILD)) !=
609                     (PL_FLAG_FORKED | PL_FLAG_CHILD));
610                 if (pl.pl_flags & PL_FLAG_CHILD) {
611                         ATF_REQUIRE(wpid != parent);
612                         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
613                         ATF_REQUIRE(!fork_reported[1]);
614                         if (child == -1)
615                                 child = wpid;
616                         else
617                                 REQUIRE_EQ(child, wpid);
618                         if (ppl != NULL)
619                                 ppl[1] = pl;
620                         fork_reported[1] = true;
621                 } else {
622                         REQUIRE_EQ(wpid, parent);
623                         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
624                         ATF_REQUIRE(!fork_reported[0]);
625                         if (child == -1)
626                                 child = pl.pl_child_pid;
627                         else
628                                 REQUIRE_EQ(child, pl.pl_child_pid);
629                         if (ppl != NULL)
630                                 ppl[0] = pl;
631                         fork_reported[0] = true;
632                 }
633         }
634
635         return (child);
636 }
637
638 /*
639  * Verify that a new child process is stopped after a followed fork and
640  * that the traced parent sees the exit of the child after the debugger
641  * when both processes remain attached to the debugger.
642  */
643 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached);
644 ATF_TC_BODY(ptrace__follow_fork_both_attached, tc)
645 {
646         pid_t children[2], fpid, wpid;
647         int status;
648
649         ATF_REQUIRE((fpid = fork()) != -1);
650         if (fpid == 0) {
651                 trace_me();
652                 follow_fork_parent(false);
653         }
654
655         /* Parent process. */
656         children[0] = fpid;
657
658         /* The first wait() should report the stop from SIGSTOP. */
659         wpid = waitpid(children[0], &status, 0);
660         REQUIRE_EQ(wpid, children[0]);
661         ATF_REQUIRE(WIFSTOPPED(status));
662         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
663
664         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
665
666         /* Continue the child ignoring the SIGSTOP. */
667         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
668
669         children[1] = handle_fork_events(children[0], NULL);
670         ATF_REQUIRE(children[1] > 0);
671
672         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
673         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
674
675         /*
676          * The child can't exit until the grandchild reports status, so the
677          * grandchild should report its exit first to the debugger.
678          */
679         wpid = wait(&status);
680         REQUIRE_EQ(wpid, children[1]);
681         ATF_REQUIRE(WIFEXITED(status));
682         REQUIRE_EQ(WEXITSTATUS(status), 2);
683
684         wpid = wait(&status);
685         REQUIRE_EQ(wpid, children[0]);
686         ATF_REQUIRE(WIFEXITED(status));
687         REQUIRE_EQ(WEXITSTATUS(status), 1);
688
689         wpid = wait(&status);
690         REQUIRE_EQ(wpid, -1);
691         REQUIRE_EQ(errno, ECHILD);
692 }
693
694 /*
695  * Verify that a new child process is stopped after a followed fork
696  * and that the traced parent sees the exit of the child when the new
697  * child process is detached after it reports its fork.
698  */
699 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached);
700 ATF_TC_BODY(ptrace__follow_fork_child_detached, tc)
701 {
702         pid_t children[2], fpid, wpid;
703         int status;
704
705         ATF_REQUIRE((fpid = fork()) != -1);
706         if (fpid == 0) {
707                 trace_me();
708                 follow_fork_parent(false);
709         }
710
711         /* Parent process. */
712         children[0] = fpid;
713
714         /* The first wait() should report the stop from SIGSTOP. */
715         wpid = waitpid(children[0], &status, 0);
716         REQUIRE_EQ(wpid, children[0]);
717         ATF_REQUIRE(WIFSTOPPED(status));
718         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
719
720         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
721
722         /* Continue the child ignoring the SIGSTOP. */
723         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
724
725         children[1] = handle_fork_events(children[0], NULL);
726         ATF_REQUIRE(children[1] > 0);
727
728         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
729         ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
730
731         /*
732          * Should not see any status from the grandchild now, only the
733          * child.
734          */
735         wpid = wait(&status);
736         REQUIRE_EQ(wpid, children[0]);
737         ATF_REQUIRE(WIFEXITED(status));
738         REQUIRE_EQ(WEXITSTATUS(status), 1);
739
740         wpid = wait(&status);
741         REQUIRE_EQ(wpid, -1);
742         REQUIRE_EQ(errno, ECHILD);
743 }
744
745 /*
746  * Verify that a new child process is stopped after a followed fork
747  * and that the traced parent sees the exit of the child when the
748  * traced parent is detached after the fork.
749  */
750 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached);
751 ATF_TC_BODY(ptrace__follow_fork_parent_detached, tc)
752 {
753         pid_t children[2], fpid, wpid;
754         int status;
755
756         ATF_REQUIRE((fpid = fork()) != -1);
757         if (fpid == 0) {
758                 trace_me();
759                 follow_fork_parent(false);
760         }
761
762         /* Parent process. */
763         children[0] = fpid;
764
765         /* The first wait() should report the stop from SIGSTOP. */
766         wpid = waitpid(children[0], &status, 0);
767         REQUIRE_EQ(wpid, children[0]);
768         ATF_REQUIRE(WIFSTOPPED(status));
769         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
770
771         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
772
773         /* Continue the child ignoring the SIGSTOP. */
774         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
775
776         children[1] = handle_fork_events(children[0], NULL);
777         ATF_REQUIRE(children[1] > 0);
778
779         ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
780         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
781
782         /*
783          * The child can't exit until the grandchild reports status, so the
784          * grandchild should report its exit first to the debugger.
785          *
786          * Even though the child process is detached, it is still a
787          * child of the debugger, so it will still report it's exit
788          * after the grandchild.
789          */
790         wpid = wait(&status);
791         REQUIRE_EQ(wpid, children[1]);
792         ATF_REQUIRE(WIFEXITED(status));
793         REQUIRE_EQ(WEXITSTATUS(status), 2);
794
795         wpid = wait(&status);
796         REQUIRE_EQ(wpid, children[0]);
797         ATF_REQUIRE(WIFEXITED(status));
798         REQUIRE_EQ(WEXITSTATUS(status), 1);
799
800         wpid = wait(&status);
801         REQUIRE_EQ(wpid, -1);
802         REQUIRE_EQ(errno, ECHILD);
803 }
804
805 static void
806 attach_fork_parent(int cpipe[2])
807 {
808         pid_t fpid;
809
810         close(cpipe[0]);
811
812         /* Double-fork to disassociate from the debugger. */
813         CHILD_REQUIRE((fpid = fork()) != -1);
814         if (fpid != 0)
815                 _exit(3);
816         
817         /* Send the pid of the disassociated child to the debugger. */
818         fpid = getpid();
819         CHILD_REQUIRE_EQ(write(cpipe[1], &fpid, sizeof(fpid)), sizeof(fpid));
820
821         /* Wait for the debugger to attach. */
822         CHILD_REQUIRE_EQ(read(cpipe[1], &fpid, sizeof(fpid)), 0);
823 }
824
825 /*
826  * Verify that a new child process is stopped after a followed fork and
827  * that the traced parent sees the exit of the child after the debugger
828  * when both processes remain attached to the debugger.  In this test
829  * the parent that forks is not a direct child of the debugger.
830  */
831 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_both_attached_unrelated_debugger);
832 ATF_TC_BODY(ptrace__follow_fork_both_attached_unrelated_debugger, tc)
833 {
834         pid_t children[2], fpid, wpid;
835         int cpipe[2], status;
836
837         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
838                 atf_tc_skip("https://bugs.freebsd.org/239397");
839
840         REQUIRE_EQ(pipe(cpipe), 0);
841         ATF_REQUIRE((fpid = fork()) != -1);
842         if (fpid == 0) {
843                 attach_fork_parent(cpipe);
844                 follow_fork_parent(false);
845         }
846
847         /* Parent process. */
848         close(cpipe[1]);
849
850         /* Wait for the direct child to exit. */
851         wpid = waitpid(fpid, &status, 0);
852         REQUIRE_EQ(wpid, fpid);
853         ATF_REQUIRE(WIFEXITED(status));
854         REQUIRE_EQ(WEXITSTATUS(status), 3);
855
856         /* Read the pid of the fork parent. */
857         REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
858             sizeof(children[0]));
859
860         /* Attach to the fork parent. */
861         attach_child(children[0]);
862
863         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
864
865         /* Continue the fork parent ignoring the SIGSTOP. */
866         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
867
868         /* Signal the fork parent to continue. */
869         close(cpipe[0]);
870
871         children[1] = handle_fork_events(children[0], NULL);
872         ATF_REQUIRE(children[1] > 0);
873
874         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
875         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
876
877         /*
878          * The fork parent can't exit until the child reports status,
879          * so the child should report its exit first to the debugger.
880          */
881         wpid = wait(&status);
882         REQUIRE_EQ(wpid, children[1]);
883         ATF_REQUIRE(WIFEXITED(status));
884         REQUIRE_EQ(WEXITSTATUS(status), 2);
885
886         wpid = wait(&status);
887         REQUIRE_EQ(wpid, children[0]);
888         ATF_REQUIRE(WIFEXITED(status));
889         REQUIRE_EQ(WEXITSTATUS(status), 1);
890
891         wpid = wait(&status);
892         REQUIRE_EQ(wpid, -1);
893         REQUIRE_EQ(errno, ECHILD);
894 }
895
896 /*
897  * Verify that a new child process is stopped after a followed fork
898  * and that the traced parent sees the exit of the child when the new
899  * child process is detached after it reports its fork.  In this test
900  * the parent that forks is not a direct child of the debugger.
901  */
902 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_child_detached_unrelated_debugger);
903 ATF_TC_BODY(ptrace__follow_fork_child_detached_unrelated_debugger, tc)
904 {
905         pid_t children[2], fpid, wpid;
906         int cpipe[2], status;
907
908         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
909                 atf_tc_skip("https://bugs.freebsd.org/239292");
910
911         REQUIRE_EQ(pipe(cpipe), 0);
912         ATF_REQUIRE((fpid = fork()) != -1);
913         if (fpid == 0) {
914                 attach_fork_parent(cpipe);
915                 follow_fork_parent(false);
916         }
917
918         /* Parent process. */
919         close(cpipe[1]);
920
921         /* Wait for the direct child to exit. */
922         wpid = waitpid(fpid, &status, 0);
923         REQUIRE_EQ(wpid, fpid);
924         ATF_REQUIRE(WIFEXITED(status));
925         REQUIRE_EQ(WEXITSTATUS(status), 3);
926
927         /* Read the pid of the fork parent. */
928         REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
929             sizeof(children[0]));
930
931         /* Attach to the fork parent. */
932         attach_child(children[0]);
933
934         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
935
936         /* Continue the fork parent ignoring the SIGSTOP. */
937         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
938
939         /* Signal the fork parent to continue. */
940         close(cpipe[0]);
941
942         children[1] = handle_fork_events(children[0], NULL);
943         ATF_REQUIRE(children[1] > 0);
944
945         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
946         ATF_REQUIRE(ptrace(PT_DETACH, children[1], (caddr_t)1, 0) != -1);
947
948         /*
949          * Should not see any status from the child now, only the fork
950          * parent.
951          */
952         wpid = wait(&status);
953         REQUIRE_EQ(wpid, children[0]);
954         ATF_REQUIRE(WIFEXITED(status));
955         REQUIRE_EQ(WEXITSTATUS(status), 1);
956
957         wpid = wait(&status);
958         REQUIRE_EQ(wpid, -1);
959         REQUIRE_EQ(errno, ECHILD);
960 }
961
962 /*
963  * Verify that a new child process is stopped after a followed fork
964  * and that the traced parent sees the exit of the child when the
965  * traced parent is detached after the fork.  In this test the parent
966  * that forks is not a direct child of the debugger.
967  */
968 ATF_TC_WITHOUT_HEAD(ptrace__follow_fork_parent_detached_unrelated_debugger);
969 ATF_TC_BODY(ptrace__follow_fork_parent_detached_unrelated_debugger, tc)
970 {
971         pid_t children[2], fpid, wpid;
972         int cpipe[2], status;
973
974         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
975                 atf_tc_skip("https://bugs.freebsd.org/239425");
976
977         REQUIRE_EQ(pipe(cpipe), 0);
978         ATF_REQUIRE((fpid = fork()) != -1);
979         if (fpid == 0) {
980                 attach_fork_parent(cpipe);
981                 follow_fork_parent(false);
982         }
983
984         /* Parent process. */
985         close(cpipe[1]);
986
987         /* Wait for the direct child to exit. */
988         wpid = waitpid(fpid, &status, 0);
989         REQUIRE_EQ(wpid, fpid);
990         ATF_REQUIRE(WIFEXITED(status));
991         REQUIRE_EQ(WEXITSTATUS(status), 3);
992
993         /* Read the pid of the fork parent. */
994         REQUIRE_EQ(read(cpipe[0], &children[0], sizeof(children[0])),
995             sizeof(children[0]));
996
997         /* Attach to the fork parent. */
998         attach_child(children[0]);
999
1000         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1001
1002         /* Continue the fork parent ignoring the SIGSTOP. */
1003         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1004
1005         /* Signal the fork parent to continue. */
1006         close(cpipe[0]);
1007
1008         children[1] = handle_fork_events(children[0], NULL);
1009         ATF_REQUIRE(children[1] > 0);
1010
1011         ATF_REQUIRE(ptrace(PT_DETACH, children[0], (caddr_t)1, 0) != -1);
1012         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1013
1014         /*
1015          * Should not see any status from the fork parent now, only
1016          * the child.
1017          */
1018         wpid = wait(&status);
1019         REQUIRE_EQ(wpid, children[1]);
1020         ATF_REQUIRE(WIFEXITED(status));
1021         REQUIRE_EQ(WEXITSTATUS(status), 2);
1022
1023         wpid = wait(&status);
1024         REQUIRE_EQ(wpid, -1);
1025         REQUIRE_EQ(errno, ECHILD);
1026 }
1027
1028 /*
1029  * Verify that a child process does not see an unrelated debugger as its
1030  * parent but sees its original parent process.
1031  */
1032 ATF_TC_WITHOUT_HEAD(ptrace__getppid);
1033 ATF_TC_BODY(ptrace__getppid, tc)
1034 {
1035         pid_t child, debugger, ppid, wpid;
1036         int cpipe[2], dpipe[2], status;
1037         char c;
1038
1039         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
1040                 atf_tc_skip("https://bugs.freebsd.org/240510");
1041
1042         REQUIRE_EQ(pipe(cpipe), 0);
1043         ATF_REQUIRE((child = fork()) != -1);
1044
1045         if (child == 0) {
1046                 /* Child process. */
1047                 close(cpipe[0]);
1048
1049                 /* Wait for parent to be ready. */
1050                 CHILD_REQUIRE_EQ(read(cpipe[1], &c, sizeof(c)), sizeof(c));
1051
1052                 /* Report the parent PID to the parent. */
1053                 ppid = getppid();
1054                 CHILD_REQUIRE(write(cpipe[1], &ppid, sizeof(ppid)) ==
1055                     sizeof(ppid));
1056
1057                 _exit(1);
1058         }
1059         close(cpipe[1]);
1060
1061         REQUIRE_EQ(pipe(dpipe), 0);
1062         ATF_REQUIRE((debugger = fork()) != -1);
1063
1064         if (debugger == 0) {
1065                 /* Debugger process. */
1066                 close(dpipe[0]);
1067
1068                 CHILD_REQUIRE(ptrace(PT_ATTACH, child, NULL, 0) != -1);
1069
1070                 wpid = waitpid(child, &status, 0);
1071                 CHILD_REQUIRE_EQ(wpid, child);
1072                 CHILD_REQUIRE(WIFSTOPPED(status));
1073                 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1074
1075                 CHILD_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
1076
1077                 /* Signal parent that debugger is attached. */
1078                 CHILD_REQUIRE_EQ(write(dpipe[1], &c, sizeof(c)), sizeof(c));
1079
1080                 /* Wait for traced child to exit. */
1081                 wpid = waitpid(child, &status, 0);
1082                 CHILD_REQUIRE_EQ(wpid, child);
1083                 CHILD_REQUIRE(WIFEXITED(status));
1084                 CHILD_REQUIRE_EQ(WEXITSTATUS(status), 1);
1085
1086                 _exit(0);
1087         }
1088         close(dpipe[1]);
1089
1090         /* Parent process. */
1091
1092         /* Wait for the debugger to attach to the child. */
1093         REQUIRE_EQ(read(dpipe[0], &c, sizeof(c)), sizeof(c));
1094
1095         /* Release the child. */
1096         REQUIRE_EQ(write(cpipe[0], &c, sizeof(c)), sizeof(c));
1097
1098         /* Read the parent PID from the child. */
1099         REQUIRE_EQ(read(cpipe[0], &ppid, sizeof(ppid)), sizeof(ppid));
1100         close(cpipe[0]);
1101
1102         REQUIRE_EQ(ppid, getpid());
1103
1104         /* Wait for the debugger. */
1105         wpid = waitpid(debugger, &status, 0);
1106         REQUIRE_EQ(wpid, debugger);
1107         ATF_REQUIRE(WIFEXITED(status));
1108         REQUIRE_EQ(WEXITSTATUS(status), 0);
1109
1110         /* The child process should now be ready. */
1111         wpid = waitpid(child, &status, WNOHANG);
1112         REQUIRE_EQ(wpid, child);
1113         ATF_REQUIRE(WIFEXITED(status));
1114         REQUIRE_EQ(WEXITSTATUS(status), 1);
1115 }
1116
1117 /*
1118  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1119  * child process created via fork() reports the correct value.
1120  */
1121 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_fork);
1122 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_fork, tc)
1123 {
1124         struct ptrace_lwpinfo pl[2];
1125         pid_t children[2], fpid, wpid;
1126         int status;
1127
1128         ATF_REQUIRE((fpid = fork()) != -1);
1129         if (fpid == 0) {
1130                 trace_me();
1131                 follow_fork_parent(false);
1132         }
1133
1134         /* Parent process. */
1135         children[0] = fpid;
1136
1137         /* The first wait() should report the stop from SIGSTOP. */
1138         wpid = waitpid(children[0], &status, 0);
1139         REQUIRE_EQ(wpid, children[0]);
1140         ATF_REQUIRE(WIFSTOPPED(status));
1141         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1142
1143         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1144
1145         /* Continue the child ignoring the SIGSTOP. */
1146         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1147
1148         /* Wait for both halves of the fork event to get reported. */
1149         children[1] = handle_fork_events(children[0], pl);
1150         ATF_REQUIRE(children[1] > 0);
1151
1152         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1153         ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1154         REQUIRE_EQ(pl[0].pl_syscall_code, SYS_fork);
1155         REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1156         REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1157
1158         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1159         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1160
1161         /*
1162          * The child can't exit until the grandchild reports status, so the
1163          * grandchild should report its exit first to the debugger.
1164          */
1165         wpid = wait(&status);
1166         REQUIRE_EQ(wpid, children[1]);
1167         ATF_REQUIRE(WIFEXITED(status));
1168         REQUIRE_EQ(WEXITSTATUS(status), 2);
1169
1170         wpid = wait(&status);
1171         REQUIRE_EQ(wpid, children[0]);
1172         ATF_REQUIRE(WIFEXITED(status));
1173         REQUIRE_EQ(WEXITSTATUS(status), 1);
1174
1175         wpid = wait(&status);
1176         REQUIRE_EQ(wpid, -1);
1177         REQUIRE_EQ(errno, ECHILD);
1178 }
1179
1180 /*
1181  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1182  * child process created via vfork() reports the correct value.
1183  */
1184 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_vfork);
1185 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_vfork, tc)
1186 {
1187         struct ptrace_lwpinfo pl[2];
1188         pid_t children[2], fpid, wpid;
1189         int status;
1190
1191         ATF_REQUIRE((fpid = fork()) != -1);
1192         if (fpid == 0) {
1193                 trace_me();
1194                 follow_fork_parent(true);
1195         }
1196
1197         /* Parent process. */
1198         children[0] = fpid;
1199
1200         /* The first wait() should report the stop from SIGSTOP. */
1201         wpid = waitpid(children[0], &status, 0);
1202         REQUIRE_EQ(wpid, children[0]);
1203         ATF_REQUIRE(WIFSTOPPED(status));
1204         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1205
1206         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, children[0], NULL, 1) != -1);
1207
1208         /* Continue the child ignoring the SIGSTOP. */
1209         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1210
1211         /* Wait for both halves of the fork event to get reported. */
1212         children[1] = handle_fork_events(children[0], pl);
1213         ATF_REQUIRE(children[1] > 0);
1214
1215         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_SCX) != 0);
1216         ATF_REQUIRE((pl[1].pl_flags & PL_FLAG_SCX) != 0);
1217         REQUIRE_EQ(pl[0].pl_syscall_code, SYS_vfork);
1218         REQUIRE_EQ(pl[0].pl_syscall_code, pl[1].pl_syscall_code);
1219         REQUIRE_EQ(pl[0].pl_syscall_narg, pl[1].pl_syscall_narg);
1220
1221         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1222         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1223
1224         /*
1225          * The child can't exit until the grandchild reports status, so the
1226          * grandchild should report its exit first to the debugger.
1227          */
1228         wpid = wait(&status);
1229         REQUIRE_EQ(wpid, children[1]);
1230         ATF_REQUIRE(WIFEXITED(status));
1231         REQUIRE_EQ(WEXITSTATUS(status), 2);
1232
1233         wpid = wait(&status);
1234         REQUIRE_EQ(wpid, children[0]);
1235         ATF_REQUIRE(WIFEXITED(status));
1236         REQUIRE_EQ(WEXITSTATUS(status), 1);
1237
1238         wpid = wait(&status);
1239         REQUIRE_EQ(wpid, -1);
1240         REQUIRE_EQ(errno, ECHILD);
1241 }
1242
1243 static void *
1244 simple_thread(void *arg __unused)
1245 {
1246
1247         pthread_exit(NULL);
1248 }
1249
1250 static __dead2 void
1251 simple_thread_main(void)
1252 {
1253         pthread_t thread;
1254
1255         CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, simple_thread, NULL), 0);
1256         CHILD_REQUIRE_EQ(pthread_join(thread, NULL), 0);
1257         exit(1);
1258 }
1259
1260 /*
1261  * Verify that pl_syscall_code in struct ptrace_lwpinfo for a new
1262  * thread reports the correct value.
1263  */
1264 ATF_TC_WITHOUT_HEAD(ptrace__new_child_pl_syscall_code_thread);
1265 ATF_TC_BODY(ptrace__new_child_pl_syscall_code_thread, tc)
1266 {
1267         struct ptrace_lwpinfo pl;
1268         pid_t fpid, wpid;
1269         lwpid_t mainlwp;
1270         int status;
1271
1272         ATF_REQUIRE((fpid = fork()) != -1);
1273         if (fpid == 0) {
1274                 trace_me();
1275                 simple_thread_main();
1276         }
1277
1278         /* The first wait() should report the stop from SIGSTOP. */
1279         wpid = waitpid(fpid, &status, 0);
1280         REQUIRE_EQ(wpid, fpid);
1281         ATF_REQUIRE(WIFSTOPPED(status));
1282         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1283
1284         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1285             sizeof(pl)) != -1);
1286         mainlwp = pl.pl_lwpid;
1287
1288         /*
1289          * Continue the child ignoring the SIGSTOP and tracing all
1290          * system call exits.
1291          */
1292         ATF_REQUIRE(ptrace(PT_TO_SCX, fpid, (caddr_t)1, 0) != -1);
1293
1294         /*
1295          * Wait for the new thread to arrive.  pthread_create() might
1296          * invoke any number of system calls.  For now we just wait
1297          * for the new thread to arrive and make sure it reports a
1298          * valid system call code.  If ptrace grows thread event
1299          * reporting then this test can be made more precise.
1300          */
1301         for (;;) {
1302                 wpid = waitpid(fpid, &status, 0);
1303                 REQUIRE_EQ(wpid, fpid);
1304                 ATF_REQUIRE(WIFSTOPPED(status));
1305                 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1306
1307                 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1308                     sizeof(pl)) != -1);
1309                 ATF_REQUIRE((pl.pl_flags & PL_FLAG_SCX) != 0);
1310                 ATF_REQUIRE(pl.pl_syscall_code != 0);
1311                 if (pl.pl_lwpid != mainlwp)
1312                         /* New thread seen. */
1313                         break;
1314
1315                 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1316         }
1317
1318         /* Wait for the child to exit. */
1319         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1320         for (;;) {
1321                 wpid = waitpid(fpid, &status, 0);
1322                 REQUIRE_EQ(wpid, fpid);
1323                 if (WIFEXITED(status))
1324                         break;
1325                 
1326                 ATF_REQUIRE(WIFSTOPPED(status));
1327                 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1328                 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1329         }
1330
1331         REQUIRE_EQ(WEXITSTATUS(status), 1);
1332
1333         wpid = wait(&status);
1334         REQUIRE_EQ(wpid, -1);
1335         REQUIRE_EQ(errno, ECHILD);
1336 }
1337
1338 /*
1339  * Verify that the expected LWP events are reported for a child thread.
1340  */
1341 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events);
1342 ATF_TC_BODY(ptrace__lwp_events, tc)
1343 {
1344         struct ptrace_lwpinfo pl;
1345         pid_t fpid, wpid;
1346         lwpid_t lwps[2];
1347         int status;
1348
1349         ATF_REQUIRE((fpid = fork()) != -1);
1350         if (fpid == 0) {
1351                 trace_me();
1352                 simple_thread_main();
1353         }
1354
1355         /* The first wait() should report the stop from SIGSTOP. */
1356         wpid = waitpid(fpid, &status, 0);
1357         REQUIRE_EQ(wpid, fpid);
1358         ATF_REQUIRE(WIFSTOPPED(status));
1359         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1360
1361         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1362             sizeof(pl)) != -1);
1363         lwps[0] = pl.pl_lwpid;
1364
1365         REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1366
1367         /* Continue the child ignoring the SIGSTOP. */
1368         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1369
1370         /* The first event should be for the child thread's birth. */
1371         wpid = waitpid(fpid, &status, 0);
1372         REQUIRE_EQ(wpid, fpid);
1373         ATF_REQUIRE(WIFSTOPPED(status));
1374         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1375
1376         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1377         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1378             (PL_FLAG_BORN | PL_FLAG_SCX));
1379         ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1380         lwps[1] = pl.pl_lwpid;
1381
1382         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1383
1384         /* The next event should be for the child thread's death. */
1385         wpid = waitpid(fpid, &status, 0);
1386         REQUIRE_EQ(wpid, fpid);
1387         ATF_REQUIRE(WIFSTOPPED(status));
1388         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1389
1390         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1391         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1392             (PL_FLAG_EXITED | PL_FLAG_SCE));
1393         REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1394
1395         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1396
1397         /* The last event should be for the child process's exit. */
1398         wpid = waitpid(fpid, &status, 0);
1399         ATF_REQUIRE(WIFEXITED(status));
1400         REQUIRE_EQ(WEXITSTATUS(status), 1);
1401
1402         wpid = wait(&status);
1403         REQUIRE_EQ(wpid, -1);
1404         REQUIRE_EQ(errno, ECHILD);
1405 }
1406
1407 static void *
1408 exec_thread(void *arg __unused)
1409 {
1410
1411         execl("/usr/bin/true", "true", NULL);
1412         exit(127);
1413 }
1414
1415 static __dead2 void
1416 exec_thread_main(void)
1417 {
1418         pthread_t thread;
1419
1420         CHILD_REQUIRE_EQ(pthread_create(&thread, NULL, exec_thread, NULL), 0);
1421         for (;;)
1422                 sleep(60);
1423         exit(1);
1424 }
1425
1426 /*
1427  * Verify that the expected LWP events are reported for a multithreaded
1428  * process that calls execve(2).
1429  */
1430 ATF_TC_WITHOUT_HEAD(ptrace__lwp_events_exec);
1431 ATF_TC_BODY(ptrace__lwp_events_exec, tc)
1432 {
1433         struct ptrace_lwpinfo pl;
1434         pid_t fpid, wpid;
1435         lwpid_t lwps[2];
1436         int status;
1437
1438         ATF_REQUIRE((fpid = fork()) != -1);
1439         if (fpid == 0) {
1440                 trace_me();
1441                 exec_thread_main();
1442         }
1443
1444         /* The first wait() should report the stop from SIGSTOP. */
1445         wpid = waitpid(fpid, &status, 0);
1446         REQUIRE_EQ(wpid, fpid);
1447         ATF_REQUIRE(WIFSTOPPED(status));
1448         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1449
1450         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1451             sizeof(pl)) != -1);
1452         lwps[0] = pl.pl_lwpid;
1453
1454         REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1455
1456         /* Continue the child ignoring the SIGSTOP. */
1457         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1458
1459         /* The first event should be for the child thread's birth. */
1460         wpid = waitpid(fpid, &status, 0);
1461         REQUIRE_EQ(wpid, fpid);
1462         ATF_REQUIRE(WIFSTOPPED(status));
1463         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1464
1465         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1466         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1467             (PL_FLAG_BORN | PL_FLAG_SCX));
1468         ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
1469         lwps[1] = pl.pl_lwpid;
1470
1471         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1472
1473         /*
1474          * The next event should be for the main thread's death due to
1475          * single threading from execve().
1476          */
1477         wpid = waitpid(fpid, &status, 0);
1478         REQUIRE_EQ(wpid, fpid);
1479         ATF_REQUIRE(WIFSTOPPED(status));
1480         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1481
1482         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1483         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXITED | PL_FLAG_SCE)),
1484             (PL_FLAG_EXITED));
1485         REQUIRE_EQ(pl.pl_lwpid, lwps[0]);
1486
1487         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1488
1489         /* The next event should be for the child process's exec. */
1490         wpid = waitpid(fpid, &status, 0);
1491         ATF_REQUIRE(WIFSTOPPED(status));
1492         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1493
1494         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1495         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1496             (PL_FLAG_EXEC | PL_FLAG_SCX));
1497         REQUIRE_EQ(pl.pl_lwpid, lwps[1]);
1498
1499         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1500
1501         /* The last event should be for the child process's exit. */
1502         wpid = waitpid(fpid, &status, 0);
1503         ATF_REQUIRE(WIFEXITED(status));
1504         REQUIRE_EQ(WEXITSTATUS(status), 0);
1505
1506         wpid = wait(&status);
1507         REQUIRE_EQ(wpid, -1);
1508         REQUIRE_EQ(errno, ECHILD);
1509 }
1510
1511 static void
1512 handler(int sig __unused)
1513 {
1514 }
1515
1516 static void
1517 signal_main(void)
1518 {
1519
1520         signal(SIGINFO, handler);
1521         raise(SIGINFO);
1522         exit(0);
1523 }
1524
1525 /*
1526  * Verify that the expected ptrace event is reported for a signal.
1527  */
1528 ATF_TC_WITHOUT_HEAD(ptrace__siginfo);
1529 ATF_TC_BODY(ptrace__siginfo, tc)
1530 {
1531         struct ptrace_lwpinfo pl;
1532         pid_t fpid, wpid;
1533         int status;
1534
1535         ATF_REQUIRE((fpid = fork()) != -1);
1536         if (fpid == 0) {
1537                 trace_me();
1538                 signal_main();
1539         }
1540
1541         /* The first wait() should report the stop from SIGSTOP. */
1542         wpid = waitpid(fpid, &status, 0);
1543         REQUIRE_EQ(wpid, fpid);
1544         ATF_REQUIRE(WIFSTOPPED(status));
1545         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1546
1547         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1548
1549         /* The next event should be for the SIGINFO. */
1550         wpid = waitpid(fpid, &status, 0);
1551         ATF_REQUIRE(WIFSTOPPED(status));
1552         REQUIRE_EQ(WSTOPSIG(status), SIGINFO);
1553
1554         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1555         REQUIRE_EQ(pl.pl_event, PL_EVENT_SIGNAL);
1556         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
1557         REQUIRE_EQ(pl.pl_siginfo.si_code, SI_LWP);
1558         REQUIRE_EQ(pl.pl_siginfo.si_pid, wpid);
1559
1560         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1561
1562         /* The last event should be for the child process's exit. */
1563         wpid = waitpid(fpid, &status, 0);
1564         ATF_REQUIRE(WIFEXITED(status));
1565         REQUIRE_EQ(WEXITSTATUS(status), 0);
1566
1567         wpid = wait(&status);
1568         REQUIRE_EQ(wpid, -1);
1569         REQUIRE_EQ(errno, ECHILD);
1570 }
1571
1572 /*
1573  * Verify that the expected ptrace events are reported for PTRACE_EXEC.
1574  */
1575 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_disable);
1576 ATF_TC_BODY(ptrace__ptrace_exec_disable, tc)
1577 {
1578         pid_t fpid, wpid;
1579         int events, status;
1580
1581         ATF_REQUIRE((fpid = fork()) != -1);
1582         if (fpid == 0) {
1583                 trace_me();
1584                 exec_thread(NULL);
1585         }
1586
1587         /* The first wait() should report the stop from SIGSTOP. */
1588         wpid = waitpid(fpid, &status, 0);
1589         REQUIRE_EQ(wpid, fpid);
1590         ATF_REQUIRE(WIFSTOPPED(status));
1591         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1592
1593         events = 0;
1594         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1595             sizeof(events)) == 0);
1596
1597         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1598
1599         /* Should get one event at exit. */
1600         wpid = waitpid(fpid, &status, 0);
1601         ATF_REQUIRE(WIFEXITED(status));
1602         REQUIRE_EQ(WEXITSTATUS(status), 0);
1603
1604         wpid = wait(&status);
1605         REQUIRE_EQ(wpid, -1);
1606         REQUIRE_EQ(errno, ECHILD);
1607 }
1608
1609 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_exec_enable);
1610 ATF_TC_BODY(ptrace__ptrace_exec_enable, tc)
1611 {
1612         struct ptrace_lwpinfo pl;
1613         pid_t fpid, wpid;
1614         int events, status;
1615
1616         ATF_REQUIRE((fpid = fork()) != -1);
1617         if (fpid == 0) {
1618                 trace_me();
1619                 exec_thread(NULL);
1620         }
1621
1622         /* The first wait() should report the stop from SIGSTOP. */
1623         wpid = waitpid(fpid, &status, 0);
1624         REQUIRE_EQ(wpid, fpid);
1625         ATF_REQUIRE(WIFSTOPPED(status));
1626         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1627
1628         events = PTRACE_EXEC;
1629         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1630             sizeof(events)) == 0);
1631
1632         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1633
1634         /* The next event should be for the child process's exec. */
1635         wpid = waitpid(fpid, &status, 0);
1636         ATF_REQUIRE(WIFSTOPPED(status));
1637         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1638
1639         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1640         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_EXEC | PL_FLAG_SCX)),
1641             (PL_FLAG_EXEC | PL_FLAG_SCX));
1642
1643         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1644
1645         /* The last event should be for the child process's exit. */
1646         wpid = waitpid(fpid, &status, 0);
1647         ATF_REQUIRE(WIFEXITED(status));
1648         REQUIRE_EQ(WEXITSTATUS(status), 0);
1649
1650         wpid = wait(&status);
1651         REQUIRE_EQ(wpid, -1);
1652         REQUIRE_EQ(errno, ECHILD);
1653 }
1654
1655 ATF_TC_WITHOUT_HEAD(ptrace__event_mask);
1656 ATF_TC_BODY(ptrace__event_mask, tc)
1657 {
1658         pid_t fpid, wpid;
1659         int events, status;
1660
1661         ATF_REQUIRE((fpid = fork()) != -1);
1662         if (fpid == 0) {
1663                 trace_me();
1664                 exit(0);
1665         }
1666
1667         /* The first wait() should report the stop from SIGSTOP. */
1668         wpid = waitpid(fpid, &status, 0);
1669         REQUIRE_EQ(wpid, fpid);
1670         ATF_REQUIRE(WIFSTOPPED(status));
1671         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1672
1673         /* PT_FOLLOW_FORK should toggle the state of PTRACE_FORK. */
1674         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 1) != -1);
1675         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1676             sizeof(events)) == 0);
1677         ATF_REQUIRE(events & PTRACE_FORK);
1678         ATF_REQUIRE(ptrace(PT_FOLLOW_FORK, fpid, NULL, 0) != -1);
1679         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1680             sizeof(events)) == 0);
1681         ATF_REQUIRE(!(events & PTRACE_FORK));
1682
1683         /* PT_LWP_EVENTS should toggle the state of PTRACE_LWP. */
1684         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 1) != -1);
1685         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1686             sizeof(events)) == 0);
1687         ATF_REQUIRE(events & PTRACE_LWP);
1688         ATF_REQUIRE(ptrace(PT_LWP_EVENTS, fpid, NULL, 0) != -1);
1689         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1690             sizeof(events)) == 0);
1691         ATF_REQUIRE(!(events & PTRACE_LWP));
1692
1693         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1694
1695         /* Should get one event at exit. */
1696         wpid = waitpid(fpid, &status, 0);
1697         ATF_REQUIRE(WIFEXITED(status));
1698         REQUIRE_EQ(WEXITSTATUS(status), 0);
1699
1700         wpid = wait(&status);
1701         REQUIRE_EQ(wpid, -1);
1702         REQUIRE_EQ(errno, ECHILD);
1703 }
1704
1705 /*
1706  * Verify that the expected ptrace events are reported for PTRACE_VFORK.
1707  */
1708 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork);
1709 ATF_TC_BODY(ptrace__ptrace_vfork, tc)
1710 {
1711         struct ptrace_lwpinfo pl;
1712         pid_t fpid, wpid;
1713         int events, status;
1714
1715         ATF_REQUIRE((fpid = fork()) != -1);
1716         if (fpid == 0) {
1717                 trace_me();
1718                 follow_fork_parent(true);
1719         }
1720
1721         /* The first wait() should report the stop from SIGSTOP. */
1722         wpid = waitpid(fpid, &status, 0);
1723         REQUIRE_EQ(wpid, fpid);
1724         ATF_REQUIRE(WIFSTOPPED(status));
1725         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1726
1727         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
1728             sizeof(events)) == 0);
1729         events |= PTRACE_VFORK;
1730         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
1731             sizeof(events)) == 0);
1732         
1733         /* Continue the child ignoring the SIGSTOP. */
1734         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1735
1736         /* The next event should report the end of the vfork. */
1737         wpid = wait(&status);
1738         REQUIRE_EQ(wpid, fpid);
1739         ATF_REQUIRE(WIFSTOPPED(status));
1740         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1741         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1742         ATF_REQUIRE((pl.pl_flags & PL_FLAG_VFORK_DONE) != 0);
1743
1744         ATF_REQUIRE(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0) != -1);
1745
1746         wpid = wait(&status);
1747         REQUIRE_EQ(wpid, fpid);
1748         ATF_REQUIRE(WIFEXITED(status));
1749         REQUIRE_EQ(WEXITSTATUS(status), 1);
1750
1751         wpid = wait(&status);
1752         REQUIRE_EQ(wpid, -1);
1753         REQUIRE_EQ(errno, ECHILD);
1754 }
1755
1756 ATF_TC_WITHOUT_HEAD(ptrace__ptrace_vfork_follow);
1757 ATF_TC_BODY(ptrace__ptrace_vfork_follow, tc)
1758 {
1759         struct ptrace_lwpinfo pl[2];
1760         pid_t children[2], fpid, wpid;
1761         int events, status;
1762
1763         ATF_REQUIRE((fpid = fork()) != -1);
1764         if (fpid == 0) {
1765                 trace_me();
1766                 follow_fork_parent(true);
1767         }
1768
1769         /* Parent process. */
1770         children[0] = fpid;
1771
1772         /* The first wait() should report the stop from SIGSTOP. */
1773         wpid = waitpid(children[0], &status, 0);
1774         REQUIRE_EQ(wpid, children[0]);
1775         ATF_REQUIRE(WIFSTOPPED(status));
1776         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1777
1778         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, children[0], (caddr_t)&events,
1779             sizeof(events)) == 0);
1780         events |= PTRACE_FORK | PTRACE_VFORK;
1781         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, children[0], (caddr_t)&events,
1782             sizeof(events)) == 0);
1783
1784         /* Continue the child ignoring the SIGSTOP. */
1785         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1786
1787         /* Wait for both halves of the fork event to get reported. */
1788         children[1] = handle_fork_events(children[0], pl);
1789         ATF_REQUIRE(children[1] > 0);
1790
1791         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORKED) != 0);
1792
1793         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1794         ATF_REQUIRE(ptrace(PT_CONTINUE, children[1], (caddr_t)1, 0) != -1);
1795
1796         /*
1797          * The child can't exit until the grandchild reports status, so the
1798          * grandchild should report its exit first to the debugger.
1799          */
1800         wpid = waitpid(children[1], &status, 0);
1801         REQUIRE_EQ(wpid, children[1]);
1802         ATF_REQUIRE(WIFEXITED(status));
1803         REQUIRE_EQ(WEXITSTATUS(status), 2);
1804
1805         /*
1806          * The child should report it's vfork() completion before it
1807          * exits.
1808          */
1809         wpid = wait(&status);
1810         REQUIRE_EQ(wpid, children[0]);
1811         ATF_REQUIRE(WIFSTOPPED(status));
1812         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1813         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl[0], sizeof(pl[0])) !=
1814             -1);
1815         ATF_REQUIRE((pl[0].pl_flags & PL_FLAG_VFORK_DONE) != 0);
1816
1817         ATF_REQUIRE(ptrace(PT_CONTINUE, children[0], (caddr_t)1, 0) != -1);
1818
1819         wpid = wait(&status);
1820         REQUIRE_EQ(wpid, children[0]);
1821         ATF_REQUIRE(WIFEXITED(status));
1822         REQUIRE_EQ(WEXITSTATUS(status), 1);
1823
1824         wpid = wait(&status);
1825         REQUIRE_EQ(wpid, -1);
1826         REQUIRE_EQ(errno, ECHILD);
1827 }
1828
1829 #ifdef HAVE_BREAKPOINT
1830 /*
1831  * Verify that no more events are reported after PT_KILL except for the
1832  * process exit when stopped due to a breakpoint trap.
1833  */
1834 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_breakpoint);
1835 ATF_TC_BODY(ptrace__PT_KILL_breakpoint, tc)
1836 {
1837         pid_t fpid, wpid;
1838         int status;
1839
1840         ATF_REQUIRE((fpid = fork()) != -1);
1841         if (fpid == 0) {
1842                 trace_me();
1843                 breakpoint();
1844                 exit(1);
1845         }
1846
1847         /* The first wait() should report the stop from SIGSTOP. */
1848         wpid = waitpid(fpid, &status, 0);
1849         REQUIRE_EQ(wpid, fpid);
1850         ATF_REQUIRE(WIFSTOPPED(status));
1851         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1852
1853         /* Continue the child ignoring the SIGSTOP. */
1854         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1855
1856         /* The second wait() should report hitting the breakpoint. */
1857         wpid = waitpid(fpid, &status, 0);
1858         REQUIRE_EQ(wpid, fpid);
1859         ATF_REQUIRE(WIFSTOPPED(status));
1860         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1861
1862         /* Kill the child process. */
1863         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1864
1865         /* The last wait() should report the SIGKILL. */
1866         wpid = waitpid(fpid, &status, 0);
1867         REQUIRE_EQ(wpid, fpid);
1868         ATF_REQUIRE(WIFSIGNALED(status));
1869         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1870
1871         wpid = wait(&status);
1872         REQUIRE_EQ(wpid, -1);
1873         REQUIRE_EQ(errno, ECHILD);
1874 }
1875 #endif /* HAVE_BREAKPOINT */
1876
1877 /*
1878  * Verify that no more events are reported after PT_KILL except for the
1879  * process exit when stopped inside of a system call.
1880  */
1881 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_system_call);
1882 ATF_TC_BODY(ptrace__PT_KILL_system_call, tc)
1883 {
1884         struct ptrace_lwpinfo pl;
1885         pid_t fpid, wpid;
1886         int status;
1887
1888         ATF_REQUIRE((fpid = fork()) != -1);
1889         if (fpid == 0) {
1890                 trace_me();
1891                 getpid();
1892                 exit(1);
1893         }
1894
1895         /* The first wait() should report the stop from SIGSTOP. */
1896         wpid = waitpid(fpid, &status, 0);
1897         REQUIRE_EQ(wpid, fpid);
1898         ATF_REQUIRE(WIFSTOPPED(status));
1899         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1900
1901         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
1902         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
1903
1904         /* The second wait() should report a system call entry for getpid(). */
1905         wpid = waitpid(fpid, &status, 0);
1906         REQUIRE_EQ(wpid, fpid);
1907         ATF_REQUIRE(WIFSTOPPED(status));
1908         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1909
1910         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1911         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
1912
1913         /* Kill the child process. */
1914         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1915
1916         /* The last wait() should report the SIGKILL. */
1917         wpid = waitpid(fpid, &status, 0);
1918         REQUIRE_EQ(wpid, fpid);
1919         ATF_REQUIRE(WIFSIGNALED(status));
1920         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1921
1922         wpid = wait(&status);
1923         REQUIRE_EQ(wpid, -1);
1924         REQUIRE_EQ(errno, ECHILD);
1925 }
1926
1927 /*
1928  * Verify that no more events are reported after PT_KILL except for the
1929  * process exit when killing a multithreaded process.
1930  */
1931 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_threads);
1932 ATF_TC_BODY(ptrace__PT_KILL_threads, tc)
1933 {
1934         struct ptrace_lwpinfo pl;
1935         pid_t fpid, wpid;
1936         lwpid_t main_lwp;
1937         int status;
1938
1939         ATF_REQUIRE((fpid = fork()) != -1);
1940         if (fpid == 0) {
1941                 trace_me();
1942                 simple_thread_main();
1943         }
1944
1945         /* The first wait() should report the stop from SIGSTOP. */
1946         wpid = waitpid(fpid, &status, 0);
1947         REQUIRE_EQ(wpid, fpid);
1948         ATF_REQUIRE(WIFSTOPPED(status));
1949         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
1950
1951         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
1952             sizeof(pl)) != -1);
1953         main_lwp = pl.pl_lwpid;
1954
1955         REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
1956
1957         /* Continue the child ignoring the SIGSTOP. */
1958         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
1959
1960         /* The first event should be for the child thread's birth. */
1961         wpid = waitpid(fpid, &status, 0);
1962         REQUIRE_EQ(wpid, fpid);
1963         ATF_REQUIRE(WIFSTOPPED(status));
1964         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
1965
1966         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
1967         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
1968             (PL_FLAG_BORN | PL_FLAG_SCX));
1969         ATF_REQUIRE(pl.pl_lwpid != main_lwp);
1970
1971         /* Kill the child process. */
1972         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
1973
1974         /* The last wait() should report the SIGKILL. */
1975         wpid = waitpid(fpid, &status, 0);
1976         REQUIRE_EQ(wpid, fpid);
1977         ATF_REQUIRE(WIFSIGNALED(status));
1978         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
1979
1980         wpid = wait(&status);
1981         REQUIRE_EQ(wpid, -1);
1982         REQUIRE_EQ(errno, ECHILD);
1983 }
1984
1985 static void *
1986 mask_usr1_thread(void *arg)
1987 {
1988         pthread_barrier_t *pbarrier;
1989         sigset_t sigmask;
1990
1991         pbarrier = (pthread_barrier_t*)arg;
1992
1993         sigemptyset(&sigmask);
1994         sigaddset(&sigmask, SIGUSR1);
1995         CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
1996
1997         /* Sync up with other thread after sigmask updated. */
1998         pthread_barrier_wait(pbarrier);
1999
2000         for (;;)
2001                 sleep(60);
2002
2003         return (NULL);
2004 }
2005
2006 /*
2007  * Verify that the SIGKILL from PT_KILL takes priority over other signals
2008  * and prevents spurious stops due to those other signals.
2009  */
2010 ATF_TC(ptrace__PT_KILL_competing_signal);
2011 ATF_TC_HEAD(ptrace__PT_KILL_competing_signal, tc)
2012 {
2013
2014         atf_tc_set_md_var(tc, "require.user", "root");
2015 }
2016 ATF_TC_BODY(ptrace__PT_KILL_competing_signal, tc)
2017 {
2018         pid_t fpid, wpid;
2019         int status;
2020         cpuset_t setmask;
2021         pthread_t t;
2022         pthread_barrier_t barrier;
2023         struct sched_param sched_param;
2024
2025         ATF_REQUIRE((fpid = fork()) != -1);
2026         if (fpid == 0) {
2027                 /* Bind to one CPU so only one thread at a time will run. */
2028                 CPU_ZERO(&setmask);
2029                 CPU_SET(0, &setmask);
2030                 cpusetid_t setid;
2031                 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2032                 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2033                     CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2034
2035                 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2036
2037                 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2038                     (void*)&barrier) == 0);
2039
2040                 /*
2041                  * Give the main thread higher priority. The test always
2042                  * assumes that, if both threads are able to run, the main
2043                  * thread runs first.
2044                  */
2045                 sched_param.sched_priority =
2046                     (sched_get_priority_max(SCHED_FIFO) +
2047                     sched_get_priority_min(SCHED_FIFO)) / 2;
2048                 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2049                     SCHED_FIFO, &sched_param) == 0);
2050                 sched_param.sched_priority -= RQ_PPQ;
2051                 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2052                     &sched_param) == 0);
2053
2054                 sigset_t sigmask;
2055                 sigemptyset(&sigmask);
2056                 sigaddset(&sigmask, SIGUSR2);
2057                 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2058
2059                 /* Sync up with other thread after sigmask updated. */
2060                 pthread_barrier_wait(&barrier);
2061
2062                 trace_me();
2063
2064                 for (;;)
2065                         sleep(60);
2066
2067                 exit(1);
2068         }
2069
2070         /* The first wait() should report the stop from SIGSTOP. */
2071         wpid = waitpid(fpid, &status, 0);
2072         REQUIRE_EQ(wpid, fpid);
2073         ATF_REQUIRE(WIFSTOPPED(status));
2074         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2075
2076         /* Continue the child ignoring the SIGSTOP. */
2077         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2078
2079         /* Send a signal that only the second thread can handle. */
2080         REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2081
2082         /* The second wait() should report the SIGUSR2. */
2083         wpid = waitpid(fpid, &status, 0);
2084         REQUIRE_EQ(wpid, fpid);
2085         ATF_REQUIRE(WIFSTOPPED(status));
2086         REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2087
2088         /* Send a signal that only the first thread can handle. */
2089         REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2090
2091         /* Replace the SIGUSR2 with a kill. */
2092         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2093
2094         /* The last wait() should report the SIGKILL (not the SIGUSR signal). */
2095         wpid = waitpid(fpid, &status, 0);
2096         REQUIRE_EQ(wpid, fpid);
2097         ATF_REQUIRE(WIFSIGNALED(status));
2098         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2099
2100         wpid = wait(&status);
2101         REQUIRE_EQ(wpid, -1);
2102         REQUIRE_EQ(errno, ECHILD);
2103 }
2104
2105 /*
2106  * Verify that the SIGKILL from PT_KILL takes priority over other stop events
2107  * and prevents spurious stops caused by those events.
2108  */
2109 ATF_TC(ptrace__PT_KILL_competing_stop);
2110 ATF_TC_HEAD(ptrace__PT_KILL_competing_stop, tc)
2111 {
2112
2113         atf_tc_set_md_var(tc, "require.user", "root");
2114 }
2115 ATF_TC_BODY(ptrace__PT_KILL_competing_stop, tc)
2116 {
2117         pid_t fpid, wpid;
2118         int status;
2119         cpuset_t setmask;
2120         pthread_t t;
2121         pthread_barrier_t barrier;
2122         lwpid_t main_lwp;
2123         struct ptrace_lwpinfo pl;
2124         struct sched_param sched_param;
2125
2126         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
2127                 atf_tc_skip("https://bugs.freebsd.org/220841");
2128
2129         ATF_REQUIRE((fpid = fork()) != -1);
2130         if (fpid == 0) {
2131                 trace_me();
2132
2133                 /* Bind to one CPU so only one thread at a time will run. */
2134                 CPU_ZERO(&setmask);
2135                 CPU_SET(0, &setmask);
2136                 cpusetid_t setid;
2137                 CHILD_REQUIRE_EQ(cpuset(&setid), 0);
2138                 CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
2139                     CPU_WHICH_CPUSET, setid, sizeof(setmask), &setmask) == 0);
2140
2141                 CHILD_REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
2142
2143                 CHILD_REQUIRE(pthread_create(&t, NULL, mask_usr1_thread,
2144                     (void*)&barrier) == 0);
2145
2146                 /*
2147                  * Give the main thread higher priority. The test always
2148                  * assumes that, if both threads are able to run, the main
2149                  * thread runs first.
2150                  */
2151                 sched_param.sched_priority =
2152                     (sched_get_priority_max(SCHED_FIFO) +
2153                     sched_get_priority_min(SCHED_FIFO)) / 2;
2154                 CHILD_REQUIRE(pthread_setschedparam(pthread_self(),
2155                     SCHED_FIFO, &sched_param) == 0);
2156                 sched_param.sched_priority -= RQ_PPQ;
2157                 CHILD_REQUIRE(pthread_setschedparam(t, SCHED_FIFO,
2158                     &sched_param) == 0);
2159
2160                 sigset_t sigmask;
2161                 sigemptyset(&sigmask);
2162                 sigaddset(&sigmask, SIGUSR2);
2163                 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2164
2165                 /* Sync up with other thread after sigmask updated. */
2166                 pthread_barrier_wait(&barrier);
2167
2168                 /* Sync up with the test before doing the getpid(). */
2169                 raise(SIGSTOP);
2170
2171                 getpid();
2172                 exit(1);
2173         }
2174
2175         /* The first wait() should report the stop from SIGSTOP. */
2176         wpid = waitpid(fpid, &status, 0);
2177         REQUIRE_EQ(wpid, fpid);
2178         ATF_REQUIRE(WIFSTOPPED(status));
2179         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2180
2181         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2182         main_lwp = pl.pl_lwpid;
2183
2184         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2185         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2186
2187         /*
2188          * Continue until child is done with setup, which is indicated with
2189          * SIGSTOP. Ignore system calls in the meantime.
2190          */
2191         for (;;) {
2192                 wpid = waitpid(fpid, &status, 0);
2193                 REQUIRE_EQ(wpid, fpid);
2194                 ATF_REQUIRE(WIFSTOPPED(status));
2195                 if (WSTOPSIG(status) == SIGTRAP) {
2196                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2197                             sizeof(pl)) != -1);
2198                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2199                 } else {
2200                         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2201                         break;
2202                 }
2203                 REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2204         }
2205
2206         /* Proceed, allowing main thread to hit syscall entry for getpid(). */
2207         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2208
2209         wpid = waitpid(fpid, &status, 0);
2210         REQUIRE_EQ(wpid, fpid);
2211         ATF_REQUIRE(WIFSTOPPED(status));
2212         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2213
2214         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2215             sizeof(pl)) != -1);
2216         REQUIRE_EQ(pl.pl_lwpid, main_lwp);
2217         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2218         /* Prevent the main thread from hitting its syscall exit for now. */
2219         REQUIRE_EQ(ptrace(PT_SUSPEND, main_lwp, 0, 0), 0);
2220
2221         /*
2222          * Proceed, allowing second thread to hit syscall exit for
2223          * pthread_barrier_wait().
2224          */
2225         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2226
2227         wpid = waitpid(fpid, &status, 0);
2228         REQUIRE_EQ(wpid, fpid);
2229         ATF_REQUIRE(WIFSTOPPED(status));
2230         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2231
2232         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
2233             sizeof(pl)) != -1);
2234         ATF_REQUIRE(pl.pl_lwpid != main_lwp);
2235         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2236
2237         /* Send a signal that only the second thread can handle. */
2238         REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2239
2240         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2241
2242         /* The next wait() should report the SIGUSR2. */
2243         wpid = waitpid(fpid, &status, 0);
2244         REQUIRE_EQ(wpid, fpid);
2245         ATF_REQUIRE(WIFSTOPPED(status));
2246         REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2247
2248         /* Allow the main thread to try to finish its system call. */
2249         REQUIRE_EQ(ptrace(PT_RESUME, main_lwp, 0, 0), 0);
2250
2251         /*
2252          * At this point, the main thread is in the middle of a system call and
2253          * has been resumed. The second thread has taken a SIGUSR2 which will
2254          * be replaced with a SIGKILL below. The main thread will get to run
2255          * first. It should notice the kill request (even though the signal
2256          * replacement occurred in the other thread) and exit accordingly.  It
2257          * should not stop for the system call exit event.
2258          */
2259
2260         /* Replace the SIGUSR2 with a kill. */
2261         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2262
2263         /* The last wait() should report the SIGKILL (not a syscall exit). */
2264         wpid = waitpid(fpid, &status, 0);
2265         REQUIRE_EQ(wpid, fpid);
2266         ATF_REQUIRE(WIFSIGNALED(status));
2267         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2268
2269         wpid = wait(&status);
2270         REQUIRE_EQ(wpid, -1);
2271         REQUIRE_EQ(errno, ECHILD);
2272 }
2273
2274 static void
2275 sigusr1_handler(int sig)
2276 {
2277
2278         CHILD_REQUIRE_EQ(sig, SIGUSR1);
2279         _exit(2);
2280 }
2281
2282 /*
2283  * Verify that even if the signal queue is full for a child process,
2284  * a PT_KILL will kill the process.
2285  */
2286 ATF_TC_WITHOUT_HEAD(ptrace__PT_KILL_with_signal_full_sigqueue);
2287 ATF_TC_BODY(ptrace__PT_KILL_with_signal_full_sigqueue, tc)
2288 {
2289         pid_t fpid, wpid;
2290         int status;
2291         int max_pending_per_proc;
2292         size_t len;
2293         int i;
2294
2295         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2296
2297         ATF_REQUIRE((fpid = fork()) != -1);
2298         if (fpid == 0) {
2299                 trace_me();
2300                 exit(1);
2301         }
2302
2303         /* The first wait() should report the stop from SIGSTOP. */
2304         wpid = waitpid(fpid, &status, 0);
2305         REQUIRE_EQ(wpid, fpid);
2306         ATF_REQUIRE(WIFSTOPPED(status));
2307         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2308
2309         len = sizeof(max_pending_per_proc);
2310         ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2311             &max_pending_per_proc, &len, NULL, 0) == 0);
2312
2313         /* Fill the signal queue. */
2314         for (i = 0; i < max_pending_per_proc; ++i)
2315                 REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2316
2317         /* Kill the child process. */
2318         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
2319
2320         /* The last wait() should report the SIGKILL. */
2321         wpid = waitpid(fpid, &status, 0);
2322         REQUIRE_EQ(wpid, fpid);
2323         ATF_REQUIRE(WIFSIGNALED(status));
2324         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
2325
2326         wpid = wait(&status);
2327         REQUIRE_EQ(wpid, -1);
2328         REQUIRE_EQ(errno, ECHILD);
2329 }
2330
2331 /*
2332  * Verify that when stopped at a system call entry, a signal can be
2333  * requested with PT_CONTINUE which will be delivered once the system
2334  * call is complete.
2335  */
2336 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry);
2337 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry, tc)
2338 {
2339         struct ptrace_lwpinfo pl;
2340         pid_t fpid, wpid;
2341         int status;
2342
2343         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2344
2345         ATF_REQUIRE((fpid = fork()) != -1);
2346         if (fpid == 0) {
2347                 trace_me();
2348                 getpid();
2349                 exit(1);
2350         }
2351
2352         /* The first wait() should report the stop from SIGSTOP. */
2353         wpid = waitpid(fpid, &status, 0);
2354         REQUIRE_EQ(wpid, fpid);
2355         ATF_REQUIRE(WIFSTOPPED(status));
2356         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2357
2358         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2359         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2360
2361         /* The second wait() should report a system call entry for getpid(). */
2362         wpid = waitpid(fpid, &status, 0);
2363         REQUIRE_EQ(wpid, fpid);
2364         ATF_REQUIRE(WIFSTOPPED(status));
2365         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2366
2367         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2368         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2369
2370         /* Continue the child process with a signal. */
2371         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2372
2373         for (;;) {
2374                 /*
2375                  * The last wait() should report exit 2, i.e., a normal _exit
2376                  * from the signal handler. In the meantime, catch and proceed
2377                  * past any syscall stops.
2378                  */
2379                 wpid = waitpid(fpid, &status, 0);
2380                 REQUIRE_EQ(wpid, fpid);
2381                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2382                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2383                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2384                         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2385                 } else {
2386                         ATF_REQUIRE(WIFEXITED(status));
2387                         REQUIRE_EQ(WEXITSTATUS(status), 2);
2388                         break;
2389                 }
2390         }
2391
2392         wpid = wait(&status);
2393         REQUIRE_EQ(wpid, -1);
2394         REQUIRE_EQ(errno, ECHILD);
2395 }
2396
2397 static void
2398 sigusr1_counting_handler(int sig)
2399 {
2400         static int counter = 0;
2401
2402         CHILD_REQUIRE_EQ(sig, SIGUSR1);
2403         counter++;
2404         if (counter == 2)
2405                 _exit(2);
2406 }
2407
2408 /*
2409  * Verify that, when continuing from a stop at system call entry and exit,
2410  * a signal can be requested from both stops, and both will be delivered when
2411  * the system call is complete.
2412  */
2413 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
2414 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit, tc)
2415 {
2416         struct ptrace_lwpinfo pl;
2417         pid_t fpid, wpid;
2418         int status;
2419
2420         ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2421
2422         ATF_REQUIRE((fpid = fork()) != -1);
2423         if (fpid == 0) {
2424                 trace_me();
2425                 getpid();
2426                 exit(1);
2427         }
2428
2429         /* The first wait() should report the stop from SIGSTOP. */
2430         wpid = waitpid(fpid, &status, 0);
2431         REQUIRE_EQ(wpid, fpid);
2432         ATF_REQUIRE(WIFSTOPPED(status));
2433         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2434
2435         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2436         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2437
2438         /* The second wait() should report a system call entry for getpid(). */
2439         wpid = waitpid(fpid, &status, 0);
2440         REQUIRE_EQ(wpid, fpid);
2441         ATF_REQUIRE(WIFSTOPPED(status));
2442         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2443
2444         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2445         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2446
2447         /* Continue the child process with a signal. */
2448         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2449
2450         /* The third wait() should report a system call exit for getpid(). */
2451         wpid = waitpid(fpid, &status, 0);
2452         REQUIRE_EQ(wpid, fpid);
2453         ATF_REQUIRE(WIFSTOPPED(status));
2454         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2455
2456         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2457         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2458
2459         /* Continue the child process with a signal. */
2460         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2461
2462         for (;;) {
2463                 /*
2464                  * The last wait() should report exit 2, i.e., a normal _exit
2465                  * from the signal handler. In the meantime, catch and proceed
2466                  * past any syscall stops.
2467                  */
2468                 wpid = waitpid(fpid, &status, 0);
2469                 REQUIRE_EQ(wpid, fpid);
2470                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2471                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2472                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2473                         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2474                 } else {
2475                         ATF_REQUIRE(WIFEXITED(status));
2476                         REQUIRE_EQ(WEXITSTATUS(status), 2);
2477                         break;
2478                 }
2479         }
2480
2481         wpid = wait(&status);
2482         REQUIRE_EQ(wpid, -1);
2483         REQUIRE_EQ(errno, ECHILD);
2484 }
2485
2486 /*
2487  * Verify that even if the signal queue is full for a child process,
2488  * a PT_CONTINUE with a signal will not result in loss of that signal.
2489  */
2490 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_full_sigqueue);
2491 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_full_sigqueue, tc)
2492 {
2493         pid_t fpid, wpid;
2494         int status;
2495         int max_pending_per_proc;
2496         size_t len;
2497         int i;
2498
2499         ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2500         ATF_REQUIRE(signal(SIGUSR1, sigusr1_handler) != SIG_ERR);
2501
2502         ATF_REQUIRE((fpid = fork()) != -1);
2503         if (fpid == 0) {
2504                 trace_me();
2505                 exit(1);
2506         }
2507
2508         /* The first wait() should report the stop from SIGSTOP. */
2509         wpid = waitpid(fpid, &status, 0);
2510         REQUIRE_EQ(wpid, fpid);
2511         ATF_REQUIRE(WIFSTOPPED(status));
2512         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2513
2514         len = sizeof(max_pending_per_proc);
2515         ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2516             &max_pending_per_proc, &len, NULL, 0) == 0);
2517
2518         /* Fill the signal queue. */
2519         for (i = 0; i < max_pending_per_proc; ++i)
2520                 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2521
2522         /* Continue with signal. */
2523         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2524
2525         for (;;) {
2526                 wpid = waitpid(fpid, &status, 0);
2527                 REQUIRE_EQ(wpid, fpid);
2528                 if (WIFSTOPPED(status)) {
2529                         REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2530                         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2531                 } else {
2532                         /*
2533                          * The last wait() should report normal _exit from the
2534                          * SIGUSR1 handler.
2535                          */
2536                         ATF_REQUIRE(WIFEXITED(status));
2537                         REQUIRE_EQ(WEXITSTATUS(status), 2);
2538                         break;
2539                 }
2540         }
2541
2542         wpid = wait(&status);
2543         REQUIRE_EQ(wpid, -1);
2544         REQUIRE_EQ(errno, ECHILD);
2545 }
2546
2547 static sem_t sigusr1_sem;
2548 static int got_usr1;
2549
2550 static void
2551 sigusr1_sempost_handler(int sig __unused)
2552 {
2553
2554         got_usr1++;
2555         CHILD_REQUIRE_EQ(sem_post(&sigusr1_sem), 0);
2556 }
2557
2558 /*
2559  * Verify that even if the signal queue is full for a child process,
2560  * and the signal is masked, a PT_CONTINUE with a signal will not
2561  * result in loss of that signal.
2562  */
2563 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
2564 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue, tc)
2565 {
2566         struct ptrace_lwpinfo pl;
2567         pid_t fpid, wpid;
2568         int status, err;
2569         int max_pending_per_proc;
2570         size_t len;
2571         int i;
2572         sigset_t sigmask;
2573
2574         ATF_REQUIRE(signal(SIGUSR2, handler) != SIG_ERR);
2575         REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2576         ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2577
2578         got_usr1 = 0;
2579         ATF_REQUIRE((fpid = fork()) != -1);
2580         if (fpid == 0) {
2581                 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2582                 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2583                 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2584
2585                 trace_me();
2586                 CHILD_REQUIRE_EQ(got_usr1, 0);
2587
2588                 /* Allow the pending SIGUSR1 in now. */
2589                 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2590                 /* Wait to receive the SIGUSR1. */
2591                 do {
2592                         err = sem_wait(&sigusr1_sem);
2593                         CHILD_REQUIRE(err == 0 || errno == EINTR);
2594                 } while (err != 0 && errno == EINTR);
2595                 CHILD_REQUIRE_EQ(got_usr1, 1);
2596                 exit(1);
2597         }
2598
2599         /* The first wait() should report the stop from SIGSTOP. */
2600         wpid = waitpid(fpid, &status, 0);
2601         REQUIRE_EQ(wpid, fpid);
2602         ATF_REQUIRE(WIFSTOPPED(status));
2603         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2604
2605         len = sizeof(max_pending_per_proc);
2606         ATF_REQUIRE(sysctlbyname("kern.sigqueue.max_pending_per_proc",
2607             &max_pending_per_proc, &len, NULL, 0) == 0);
2608
2609         /* Fill the signal queue. */
2610         for (i = 0; i < max_pending_per_proc; ++i)
2611                 REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
2612
2613         /* Continue with signal. */
2614         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2615
2616         /* Collect and ignore all of the SIGUSR2. */
2617         for (i = 0; i < max_pending_per_proc; ++i) {
2618                 wpid = waitpid(fpid, &status, 0);
2619                 REQUIRE_EQ(wpid, fpid);
2620                 ATF_REQUIRE(WIFSTOPPED(status));
2621                 REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
2622                 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2623         }
2624
2625         /* Now our PT_CONTINUE'd SIGUSR1 should cause a stop after unmask. */
2626         wpid = waitpid(fpid, &status, 0);
2627         REQUIRE_EQ(wpid, fpid);
2628         ATF_REQUIRE(WIFSTOPPED(status));
2629         REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
2630         ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2631         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
2632
2633         /* Continue the child, ignoring the SIGUSR1. */
2634         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2635
2636         /* The last wait() should report exit after receiving SIGUSR1. */
2637         wpid = waitpid(fpid, &status, 0);
2638         REQUIRE_EQ(wpid, fpid);
2639         ATF_REQUIRE(WIFEXITED(status));
2640         REQUIRE_EQ(WEXITSTATUS(status), 1);
2641
2642         wpid = wait(&status);
2643         REQUIRE_EQ(wpid, -1);
2644         REQUIRE_EQ(errno, ECHILD);
2645 }
2646
2647 /*
2648  * Verify that, after stopping due to a signal, that signal can be
2649  * replaced with another signal.
2650  */
2651 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_change_sig);
2652 ATF_TC_BODY(ptrace__PT_CONTINUE_change_sig, tc)
2653 {
2654         struct ptrace_lwpinfo pl;
2655         pid_t fpid, wpid;
2656         int status;
2657
2658         ATF_REQUIRE((fpid = fork()) != -1);
2659         if (fpid == 0) {
2660                 trace_me();
2661                 sleep(20);
2662                 exit(1);
2663         }
2664
2665         /* The first wait() should report the stop from SIGSTOP. */
2666         wpid = waitpid(fpid, &status, 0);
2667         REQUIRE_EQ(wpid, fpid);
2668         ATF_REQUIRE(WIFSTOPPED(status));
2669         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2670
2671         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2672
2673         /* Send a signal without ptrace. */
2674         REQUIRE_EQ(kill(fpid, SIGINT), 0);
2675
2676         /* The second wait() should report a SIGINT was received. */
2677         wpid = waitpid(fpid, &status, 0);
2678         REQUIRE_EQ(wpid, fpid);
2679         ATF_REQUIRE(WIFSTOPPED(status));
2680         REQUIRE_EQ(WSTOPSIG(status), SIGINT);
2681
2682         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2683         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2684         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGINT);
2685
2686         /* Continue the child process with a different signal. */
2687         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTERM), 0);
2688
2689         /*
2690          * The last wait() should report having died due to the new
2691          * signal, SIGTERM.
2692          */
2693         wpid = waitpid(fpid, &status, 0);
2694         REQUIRE_EQ(wpid, fpid);
2695         ATF_REQUIRE(WIFSIGNALED(status));
2696         REQUIRE_EQ(WTERMSIG(status), SIGTERM);
2697
2698         wpid = wait(&status);
2699         REQUIRE_EQ(wpid, -1);
2700         REQUIRE_EQ(errno, ECHILD);
2701 }
2702
2703 /*
2704  * Verify that a signal can be passed through to the child even when there
2705  * was no true signal originally. Such cases arise when a SIGTRAP is
2706  * invented for e.g, system call stops.
2707  */
2708 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
2709 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigtrap_system_call_entry, tc)
2710 {
2711         struct ptrace_lwpinfo pl;
2712         struct rlimit rl;
2713         pid_t fpid, wpid;
2714         int status;
2715
2716         ATF_REQUIRE((fpid = fork()) != -1);
2717         if (fpid == 0) {
2718                 trace_me();
2719                 /* SIGTRAP expected to cause exit on syscall entry. */
2720                 rl.rlim_cur = rl.rlim_max = 0;
2721                 REQUIRE_EQ(setrlimit(RLIMIT_CORE, &rl), 0);
2722                 getpid();
2723                 exit(1);
2724         }
2725
2726         /* The first wait() should report the stop from SIGSTOP. */
2727         wpid = waitpid(fpid, &status, 0);
2728         REQUIRE_EQ(wpid, fpid);
2729         ATF_REQUIRE(WIFSTOPPED(status));
2730         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2731
2732         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2733         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2734
2735         /* The second wait() should report a system call entry for getpid(). */
2736         wpid = waitpid(fpid, &status, 0);
2737         REQUIRE_EQ(wpid, fpid);
2738         ATF_REQUIRE(WIFSTOPPED(status));
2739         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2740
2741         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2742         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2743
2744         /* Continue the child process with a SIGTRAP. */
2745         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGTRAP), 0);
2746
2747         for (;;) {
2748                 /*
2749                  * The last wait() should report exit due to SIGTRAP.  In the
2750                  * meantime, catch and proceed past any syscall stops.
2751                  */
2752                 wpid = waitpid(fpid, &status, 0);
2753                 REQUIRE_EQ(wpid, fpid);
2754                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2755                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2756                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2757                         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2758                 } else {
2759                         ATF_REQUIRE(WIFSIGNALED(status));
2760                         REQUIRE_EQ(WTERMSIG(status), SIGTRAP);
2761                         break;
2762                 }
2763         }
2764
2765         wpid = wait(&status);
2766         REQUIRE_EQ(wpid, -1);
2767         REQUIRE_EQ(errno, ECHILD);
2768 }
2769
2770 /*
2771  * A mixed bag PT_CONTINUE with signal test.
2772  */
2773 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_mix);
2774 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_mix, tc)
2775 {
2776         struct ptrace_lwpinfo pl;
2777         pid_t fpid, wpid;
2778         int status;
2779
2780         ATF_REQUIRE(signal(SIGUSR1, sigusr1_counting_handler) != SIG_ERR);
2781
2782         ATF_REQUIRE((fpid = fork()) != -1);
2783         if (fpid == 0) {
2784                 trace_me();
2785                 getpid();
2786                 exit(1);
2787         }
2788
2789         /* The first wait() should report the stop from SIGSTOP. */
2790         wpid = waitpid(fpid, &status, 0);
2791         REQUIRE_EQ(wpid, fpid);
2792         ATF_REQUIRE(WIFSTOPPED(status));
2793         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2794
2795         /* Continue the child ignoring the SIGSTOP and tracing system calls. */
2796         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
2797
2798         /* The second wait() should report a system call entry for getpid(). */
2799         wpid = waitpid(fpid, &status, 0);
2800         REQUIRE_EQ(wpid, fpid);
2801         ATF_REQUIRE(WIFSTOPPED(status));
2802         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2803
2804         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2805         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
2806
2807         /* Continue with the first SIGUSR1. */
2808         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2809
2810         /* The next wait() should report a system call exit for getpid(). */
2811         wpid = waitpid(fpid, &status, 0);
2812         REQUIRE_EQ(wpid, fpid);
2813         ATF_REQUIRE(WIFSTOPPED(status));
2814         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
2815
2816         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2817         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
2818
2819         /* Send an ABRT without ptrace. */
2820         REQUIRE_EQ(kill(fpid, SIGABRT), 0);
2821
2822         /* Continue normally. */
2823         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2824
2825         /* The next wait() should report the SIGABRT. */
2826         wpid = waitpid(fpid, &status, 0);
2827         REQUIRE_EQ(wpid, fpid);
2828         ATF_REQUIRE(WIFSTOPPED(status));
2829         REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
2830
2831         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2832         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
2833         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
2834
2835         /* Continue, replacing the SIGABRT with another SIGUSR1. */
2836         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2837
2838         for (;;) {
2839                 /*
2840                  * The last wait() should report exit 2, i.e., a normal _exit
2841                  * from the signal handler. In the meantime, catch and proceed
2842                  * past any syscall stops.
2843                  */
2844                 wpid = waitpid(fpid, &status, 0);
2845                 REQUIRE_EQ(wpid, fpid);
2846                 if (WIFSTOPPED(status) && WSTOPSIG(status) == SIGTRAP) {
2847                         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
2848                         ATF_REQUIRE(pl.pl_flags & (PL_FLAG_SCE | PL_FLAG_SCX));
2849                         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
2850                 } else {
2851                         ATF_REQUIRE(WIFEXITED(status));
2852                         REQUIRE_EQ(WEXITSTATUS(status), 2);
2853                         break;
2854                 }
2855         }
2856
2857         wpid = wait(&status);
2858         REQUIRE_EQ(wpid, -1);
2859         REQUIRE_EQ(errno, ECHILD);
2860 }
2861
2862 /*
2863  * Verify a signal delivered by ptrace is noticed by kevent(2).
2864  */
2865 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_kqueue);
2866 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_kqueue, tc)
2867 {
2868         pid_t fpid, wpid;
2869         int status, kq, nevents;
2870         struct kevent kev;
2871
2872         ATF_REQUIRE(signal(SIGUSR1, SIG_IGN) != SIG_ERR);
2873
2874         ATF_REQUIRE((fpid = fork()) != -1);
2875         if (fpid == 0) {
2876                 CHILD_REQUIRE((kq = kqueue()) > 0);
2877                 EV_SET(&kev, SIGUSR1, EVFILT_SIGNAL, EV_ADD, 0, 0, 0);
2878                 CHILD_REQUIRE_EQ(kevent(kq, &kev, 1, NULL, 0, NULL), 0);
2879
2880                 trace_me();
2881
2882                 for (;;) {
2883                         nevents = kevent(kq, NULL, 0, &kev, 1, NULL);
2884                         if (nevents == -1 && errno == EINTR)
2885                                 continue;
2886                         CHILD_REQUIRE(nevents > 0);
2887                         CHILD_REQUIRE_EQ(kev.filter, EVFILT_SIGNAL);
2888                         CHILD_REQUIRE_EQ(kev.ident, SIGUSR1);
2889                         break;
2890                 }
2891
2892                 exit(1);
2893         }
2894
2895         /* The first wait() should report the stop from SIGSTOP. */
2896         wpid = waitpid(fpid, &status, 0);
2897         REQUIRE_EQ(wpid, fpid);
2898         ATF_REQUIRE(WIFSTOPPED(status));
2899         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2900
2901         /* Continue with the SIGUSR1. */
2902         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
2903
2904         /*
2905          * The last wait() should report normal exit with code 1.
2906          */
2907         wpid = waitpid(fpid, &status, 0);
2908         REQUIRE_EQ(wpid, fpid);
2909         ATF_REQUIRE(WIFEXITED(status));
2910         REQUIRE_EQ(WEXITSTATUS(status), 1);
2911
2912         wpid = wait(&status);
2913         REQUIRE_EQ(wpid, -1);
2914         REQUIRE_EQ(errno, ECHILD);
2915 }
2916
2917 static void *
2918 signal_thread(void *arg)
2919 {
2920         int err;
2921         sigset_t sigmask;
2922
2923         pthread_barrier_t *pbarrier = (pthread_barrier_t*)arg;
2924
2925         /* Wait for this thread to receive a SIGUSR1. */
2926         do {
2927                 err = sem_wait(&sigusr1_sem);
2928                 CHILD_REQUIRE(err == 0 || errno == EINTR);
2929         } while (err != 0 && errno == EINTR);
2930
2931         /* Free our companion thread from the barrier. */
2932         pthread_barrier_wait(pbarrier);
2933
2934         /*
2935          * Swap ignore duties; the next SIGUSR1 should go to the
2936          * other thread.
2937          */
2938         CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2939         CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2940         CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
2941
2942         /* Sync up threads after swapping signal masks. */
2943         pthread_barrier_wait(pbarrier);
2944
2945         /* Wait until our companion has received its SIGUSR1. */
2946         pthread_barrier_wait(pbarrier);
2947
2948         return (NULL);
2949 }
2950
2951 /*
2952  * Verify that a traced process with blocked signal received the
2953  * signal from kill() once unmasked.
2954  */
2955 ATF_TC_WITHOUT_HEAD(ptrace__killed_with_sigmask);
2956 ATF_TC_BODY(ptrace__killed_with_sigmask, tc)
2957 {
2958         struct ptrace_lwpinfo pl;
2959         pid_t fpid, wpid;
2960         int status, err;
2961         sigset_t sigmask;
2962
2963         REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
2964         ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
2965         got_usr1 = 0;
2966
2967         ATF_REQUIRE((fpid = fork()) != -1);
2968         if (fpid == 0) {
2969                 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
2970                 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
2971                 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
2972
2973                 trace_me();
2974                 CHILD_REQUIRE_EQ(got_usr1, 0);
2975
2976                 /* Allow the pending SIGUSR1 in now. */
2977                 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
2978                 /* Wait to receive a SIGUSR1. */
2979                 do {
2980                         err = sem_wait(&sigusr1_sem);
2981                         CHILD_REQUIRE(err == 0 || errno == EINTR);
2982                 } while (err != 0 && errno == EINTR);
2983                 CHILD_REQUIRE_EQ(got_usr1, 1);
2984                 exit(1);
2985         }
2986
2987         /* The first wait() should report the stop from SIGSTOP. */
2988         wpid = waitpid(fpid, &status, 0);
2989         REQUIRE_EQ(wpid, fpid);
2990         ATF_REQUIRE(WIFSTOPPED(status));
2991         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
2992         ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
2993         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
2994
2995         /* Send blocked SIGUSR1 which should cause a stop. */
2996         REQUIRE_EQ(kill(fpid, SIGUSR1), 0);
2997
2998         /* Continue the child ignoring the SIGSTOP. */
2999         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3000
3001         /* The next wait() should report the kill(SIGUSR1) was received. */
3002         wpid = waitpid(fpid, &status, 0);
3003         REQUIRE_EQ(wpid, fpid);
3004         ATF_REQUIRE(WIFSTOPPED(status));
3005         REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
3006         ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3007         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
3008
3009         /* Continue the child, allowing in the SIGUSR1. */
3010         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3011
3012         /* The last wait() should report normal exit with code 1. */
3013         wpid = waitpid(fpid, &status, 0);
3014         REQUIRE_EQ(wpid, fpid);
3015         ATF_REQUIRE(WIFEXITED(status));
3016         REQUIRE_EQ(WEXITSTATUS(status), 1);
3017
3018         wpid = wait(&status);
3019         REQUIRE_EQ(wpid, -1);
3020         REQUIRE_EQ(errno, ECHILD);
3021 }
3022
3023 /*
3024  * Verify that a traced process with blocked signal received the
3025  * signal from PT_CONTINUE once unmasked.
3026  */
3027 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_sigmask);
3028 ATF_TC_BODY(ptrace__PT_CONTINUE_with_sigmask, tc)
3029 {
3030         struct ptrace_lwpinfo pl;
3031         pid_t fpid, wpid;
3032         int status, err;
3033         sigset_t sigmask;
3034
3035         REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3036         ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3037         got_usr1 = 0;
3038
3039         ATF_REQUIRE((fpid = fork()) != -1);
3040         if (fpid == 0) {
3041                 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3042                 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3043                 CHILD_REQUIRE_EQ(sigprocmask(SIG_BLOCK, &sigmask, NULL), 0);
3044
3045                 trace_me();
3046                 CHILD_REQUIRE_EQ(got_usr1, 0);
3047
3048                 /* Allow the pending SIGUSR1 in now. */
3049                 CHILD_REQUIRE_EQ(sigprocmask(SIG_UNBLOCK, &sigmask, NULL), 0);
3050                 /* Wait to receive a SIGUSR1. */
3051                 do {
3052                         err = sem_wait(&sigusr1_sem);
3053                         CHILD_REQUIRE(err == 0 || errno == EINTR);
3054                 } while (err != 0 && errno == EINTR);
3055
3056                 CHILD_REQUIRE_EQ(got_usr1, 1);
3057                 exit(1);
3058         }
3059
3060         /* The first wait() should report the stop from SIGSTOP. */
3061         wpid = waitpid(fpid, &status, 0);
3062         REQUIRE_EQ(wpid, fpid);
3063         ATF_REQUIRE(WIFSTOPPED(status));
3064         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3065         ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3066         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGSTOP);
3067
3068         /* Continue the child replacing SIGSTOP with SIGUSR1. */
3069         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3070
3071         /* The next wait() should report the SIGUSR1 was received. */
3072         wpid = waitpid(fpid, &status, 0);
3073         REQUIRE_EQ(wpid, fpid);
3074         ATF_REQUIRE(WIFSTOPPED(status));
3075         REQUIRE_EQ(WSTOPSIG(status), SIGUSR1);
3076         ATF_REQUIRE(ptrace(PT_LWPINFO, fpid, (caddr_t)&pl, sizeof(pl)) != -1);
3077         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGUSR1);
3078
3079         /* Continue the child, ignoring the SIGUSR1. */
3080         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3081
3082         /* The last wait() should report normal exit with code 1. */
3083         wpid = waitpid(fpid, &status, 0);
3084         REQUIRE_EQ(wpid, fpid);
3085         ATF_REQUIRE(WIFEXITED(status));
3086         REQUIRE_EQ(WEXITSTATUS(status), 1);
3087
3088         wpid = wait(&status);
3089         REQUIRE_EQ(wpid, -1);
3090         REQUIRE_EQ(errno, ECHILD);
3091 }
3092
3093 /*
3094  * Verify that if ptrace stops due to a signal but continues with
3095  * a different signal that the new signal is routed to a thread
3096  * that can accept it, and that the thread is awakened by the signal
3097  * in a timely manner.
3098  */
3099 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_with_signal_thread_sigmask);
3100 ATF_TC_BODY(ptrace__PT_CONTINUE_with_signal_thread_sigmask, tc)
3101 {
3102         pid_t fpid, wpid;
3103         int status, err;
3104         pthread_t t;
3105         sigset_t sigmask;
3106         pthread_barrier_t barrier;
3107
3108         REQUIRE_EQ(pthread_barrier_init(&barrier, NULL, 2), 0);
3109         REQUIRE_EQ(sem_init(&sigusr1_sem, 0, 0), 0);
3110         ATF_REQUIRE(signal(SIGUSR1, sigusr1_sempost_handler) != SIG_ERR);
3111
3112         ATF_REQUIRE((fpid = fork()) != -1);
3113         if (fpid == 0) {
3114                 CHILD_REQUIRE_EQ(pthread_create(&t, NULL, signal_thread,
3115                     (void *)&barrier), 0);
3116
3117                 /* The other thread should receive the first SIGUSR1. */
3118                 CHILD_REQUIRE_EQ(sigemptyset(&sigmask), 0);
3119                 CHILD_REQUIRE_EQ(sigaddset(&sigmask, SIGUSR1), 0);
3120                 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_BLOCK, &sigmask, NULL), 0);
3121
3122                 trace_me();
3123
3124                 /* Wait until other thread has received its SIGUSR1. */
3125                 pthread_barrier_wait(&barrier);
3126
3127                 /*
3128                  * Swap ignore duties; the next SIGUSR1 should go to this
3129                  * thread.
3130                  */
3131                 CHILD_REQUIRE_EQ(pthread_sigmask(SIG_UNBLOCK, &sigmask, NULL),
3132                     0);
3133
3134                 /* Sync up threads after swapping signal masks. */
3135                 pthread_barrier_wait(&barrier);
3136
3137                 /*
3138                  * Sync up with test code; we're ready for the next SIGUSR1
3139                  * now.
3140                  */
3141                 raise(SIGSTOP);
3142
3143                 /* Wait for this thread to receive a SIGUSR1. */
3144                 do {
3145                         err = sem_wait(&sigusr1_sem);
3146                         CHILD_REQUIRE(err == 0 || errno == EINTR);
3147                 } while (err != 0 && errno == EINTR);
3148
3149                 /* Free the other thread from the barrier. */
3150                 pthread_barrier_wait(&barrier);
3151
3152                 CHILD_REQUIRE_EQ(pthread_join(t, NULL), 0);
3153
3154                 exit(1);
3155         }
3156
3157         /* The first wait() should report the stop from SIGSTOP. */
3158         wpid = waitpid(fpid, &status, 0);
3159         REQUIRE_EQ(wpid, fpid);
3160         ATF_REQUIRE(WIFSTOPPED(status));
3161         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3162
3163         /* Continue the child ignoring the SIGSTOP. */
3164         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3165
3166         /*
3167          * Send a signal without ptrace that either thread will accept (USR2,
3168          * in this case).
3169          */
3170         REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3171
3172         /* The second wait() should report a SIGUSR2 was received. */
3173         wpid = waitpid(fpid, &status, 0);
3174         REQUIRE_EQ(wpid, fpid);
3175         ATF_REQUIRE(WIFSTOPPED(status));
3176         REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3177
3178         /* Continue the child, changing the signal to USR1. */
3179         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3180
3181         /* The next wait() should report the stop from SIGSTOP. */
3182         wpid = waitpid(fpid, &status, 0);
3183         REQUIRE_EQ(wpid, fpid);
3184         ATF_REQUIRE(WIFSTOPPED(status));
3185         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3186
3187         /* Continue the child ignoring the SIGSTOP. */
3188         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3189
3190         REQUIRE_EQ(kill(fpid, SIGUSR2), 0);
3191
3192         /* The next wait() should report a SIGUSR2 was received. */
3193         wpid = waitpid(fpid, &status, 0);
3194         REQUIRE_EQ(wpid, fpid);
3195         ATF_REQUIRE(WIFSTOPPED(status));
3196         REQUIRE_EQ(WSTOPSIG(status), SIGUSR2);
3197
3198         /* Continue the child, changing the signal to USR1. */
3199         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, SIGUSR1), 0);
3200
3201         /* The last wait() should report normal exit with code 1. */
3202         wpid = waitpid(fpid, &status, 0);
3203         REQUIRE_EQ(wpid, fpid);
3204         ATF_REQUIRE(WIFEXITED(status));
3205         REQUIRE_EQ(WEXITSTATUS(status), 1);
3206
3207         wpid = wait(&status);
3208         REQUIRE_EQ(wpid, -1);
3209         REQUIRE_EQ(errno, ECHILD);
3210 }
3211
3212 static void *
3213 raise_sigstop_thread(void *arg __unused)
3214 {
3215
3216         raise(SIGSTOP);
3217         return NULL;
3218 }
3219
3220 static void *
3221 sleep_thread(void *arg __unused)
3222 {
3223
3224         sleep(60);
3225         return NULL;
3226 }
3227
3228 static void
3229 terminate_with_pending_sigstop(bool sigstop_from_main_thread)
3230 {
3231         pid_t fpid, wpid;
3232         int status, i;
3233         cpuset_t setmask;
3234         cpusetid_t setid;
3235         pthread_t t;
3236
3237         /*
3238          * Become the reaper for this process tree. We need to be able to check
3239          * that both child and grandchild have died.
3240          */
3241         REQUIRE_EQ(procctl(P_PID, getpid(), PROC_REAP_ACQUIRE, NULL), 0);
3242
3243         fpid = fork();
3244         ATF_REQUIRE(fpid >= 0);
3245         if (fpid == 0) {
3246                 fpid = fork();
3247                 CHILD_REQUIRE(fpid >= 0);
3248                 if (fpid == 0) {
3249                         trace_me();
3250
3251                         /* Pin to CPU 0 to serialize thread execution. */
3252                         CPU_ZERO(&setmask);
3253                         CPU_SET(0, &setmask);
3254                         CHILD_REQUIRE_EQ(cpuset(&setid), 0);
3255                         CHILD_REQUIRE(cpuset_setaffinity(CPU_LEVEL_CPUSET,
3256                             CPU_WHICH_CPUSET, setid,
3257                             sizeof(setmask), &setmask) == 0);
3258
3259                         if (sigstop_from_main_thread) {
3260                                 /*
3261                                  * We expect the SIGKILL sent when our parent
3262                                  * dies to be delivered to the new thread.
3263                                  * Raise the SIGSTOP in this thread so the
3264                                  * threads compete.
3265                                  */
3266                                 CHILD_REQUIRE(pthread_create(&t, NULL,
3267                                     sleep_thread, NULL) == 0);
3268                                 raise(SIGSTOP);
3269                         } else {
3270                                 /*
3271                                  * We expect the SIGKILL to be delivered to
3272                                  * this thread. After creating the new thread,
3273                                  * just get off the CPU so the other thread can
3274                                  * raise the SIGSTOP.
3275                                  */
3276                                 CHILD_REQUIRE(pthread_create(&t, NULL,
3277                                     raise_sigstop_thread, NULL) == 0);
3278                                 sleep(60);
3279                         }
3280
3281                         exit(0);
3282                 }
3283                 /* First stop is trace_me() immediately after fork. */
3284                 wpid = waitpid(fpid, &status, 0);
3285                 CHILD_REQUIRE_EQ(wpid, fpid);
3286                 CHILD_REQUIRE(WIFSTOPPED(status));
3287                 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3288
3289                 CHILD_REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3290
3291                 /* Second stop is from the raise(SIGSTOP). */
3292                 wpid = waitpid(fpid, &status, 0);
3293                 CHILD_REQUIRE_EQ(wpid, fpid);
3294                 CHILD_REQUIRE(WIFSTOPPED(status));
3295                 CHILD_REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3296
3297                 /*
3298                  * Terminate tracing process without detaching. Our child
3299                  * should be killed.
3300                  */
3301                 exit(0);
3302         }
3303
3304         /*
3305          * We should get a normal exit from our immediate child and a SIGKILL
3306          * exit from our grandchild. The latter case is the interesting one.
3307          * Our grandchild should not have stopped due to the SIGSTOP that was
3308          * left dangling when its parent died.
3309          */
3310         for (i = 0; i < 2; ++i) {
3311                 wpid = wait(&status);
3312                 if (wpid == fpid) {
3313                         ATF_REQUIRE(WIFEXITED(status));
3314                         REQUIRE_EQ(WEXITSTATUS(status), 0);
3315                 } else {
3316                         ATF_REQUIRE(WIFSIGNALED(status));
3317                         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3318                 }
3319         }
3320 }
3321
3322 /*
3323  * These two tests ensure that if the tracing process exits without detaching
3324  * just after the child received a SIGSTOP, the child is cleanly killed and
3325  * doesn't go to sleep due to the SIGSTOP. The parent's death will send a
3326  * SIGKILL to the child. If the SIGKILL and the SIGSTOP are handled by
3327  * different threads, the SIGKILL must win.  There are two variants of this
3328  * test, designed to catch the case where the SIGKILL is delivered to the
3329  * younger thread (the first test) and the case where the SIGKILL is delivered
3330  * to the older thread (the second test). This behavior has changed in the
3331  * past, so make no assumption.
3332  */
3333 ATF_TC(ptrace__parent_terminate_with_pending_sigstop1);
3334 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop1, tc)
3335 {
3336
3337         atf_tc_set_md_var(tc, "require.user", "root");
3338 }
3339 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop1, tc)
3340 {
3341
3342         terminate_with_pending_sigstop(true);
3343 }
3344
3345 ATF_TC(ptrace__parent_terminate_with_pending_sigstop2);
3346 ATF_TC_HEAD(ptrace__parent_terminate_with_pending_sigstop2, tc)
3347 {
3348
3349         atf_tc_set_md_var(tc, "require.user", "root");
3350 }
3351 ATF_TC_BODY(ptrace__parent_terminate_with_pending_sigstop2, tc)
3352 {
3353
3354         terminate_with_pending_sigstop(false);
3355 }
3356
3357 /*
3358  * Verify that after ptrace() discards a SIGKILL signal, the event mask
3359  * is not modified.
3360  */
3361 ATF_TC_WITHOUT_HEAD(ptrace__event_mask_sigkill_discard);
3362 ATF_TC_BODY(ptrace__event_mask_sigkill_discard, tc)
3363 {
3364         struct ptrace_lwpinfo pl;
3365         pid_t fpid, wpid;
3366         int status, event_mask, new_event_mask;
3367
3368         ATF_REQUIRE((fpid = fork()) != -1);
3369         if (fpid == 0) {
3370                 trace_me();
3371                 raise(SIGSTOP);
3372                 exit(0);
3373         }
3374
3375         /* The first wait() should report the stop from trace_me(). */
3376         wpid = waitpid(fpid, &status, 0);
3377         REQUIRE_EQ(wpid, fpid);
3378         ATF_REQUIRE(WIFSTOPPED(status));
3379         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3380
3381         /* Set several unobtrusive event bits. */
3382         event_mask = PTRACE_EXEC | PTRACE_FORK | PTRACE_LWP;
3383         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, wpid, (caddr_t)&event_mask,
3384             sizeof(event_mask)) == 0);
3385
3386         /* Send a SIGKILL without using ptrace. */
3387         REQUIRE_EQ(kill(fpid, SIGKILL), 0);
3388
3389         /* Continue the child ignoring the SIGSTOP. */
3390         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3391
3392         /* The next stop should be due to the SIGKILL. */
3393         wpid = waitpid(fpid, &status, 0);
3394         REQUIRE_EQ(wpid, fpid);
3395         ATF_REQUIRE(WIFSTOPPED(status));
3396         REQUIRE_EQ(WSTOPSIG(status), SIGKILL);
3397
3398         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3399         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3400         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGKILL);
3401
3402         /* Continue the child ignoring the SIGKILL. */
3403         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3404
3405         /* The next wait() should report the stop from SIGSTOP. */
3406         wpid = waitpid(fpid, &status, 0);
3407         REQUIRE_EQ(wpid, fpid);
3408         ATF_REQUIRE(WIFSTOPPED(status));
3409         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3410
3411         /* Check the current event mask. It should not have changed. */
3412         new_event_mask = 0;
3413         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, wpid, (caddr_t)&new_event_mask,
3414             sizeof(new_event_mask)) == 0);
3415         REQUIRE_EQ(event_mask, new_event_mask);
3416
3417         /* Continue the child to let it exit. */
3418         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3419
3420         /* The last event should be for the child process's exit. */
3421         wpid = waitpid(fpid, &status, 0);
3422         ATF_REQUIRE(WIFEXITED(status));
3423         REQUIRE_EQ(WEXITSTATUS(status), 0);
3424
3425         wpid = wait(&status);
3426         REQUIRE_EQ(wpid, -1);
3427         REQUIRE_EQ(errno, ECHILD);
3428 }
3429
3430 static void *
3431 flock_thread(void *arg)
3432 {
3433         int fd;
3434
3435         fd = *(int *)arg;
3436         (void)flock(fd, LOCK_EX);
3437         (void)flock(fd, LOCK_UN);
3438         return (NULL);
3439 }
3440
3441 /*
3442  * Verify that PT_ATTACH will suspend threads sleeping in an SBDRY section.
3443  * We rely on the fact that the lockf implementation sets SBDRY before blocking
3444  * on a lock. This is a regression test for r318191.
3445  */
3446 ATF_TC_WITHOUT_HEAD(ptrace__PT_ATTACH_with_SBDRY_thread);
3447 ATF_TC_BODY(ptrace__PT_ATTACH_with_SBDRY_thread, tc)
3448 {
3449         pthread_barrier_t barrier;
3450         pthread_barrierattr_t battr;
3451         char tmpfile[64];
3452         pid_t child, wpid;
3453         int error, fd, i, status;
3454
3455         REQUIRE_EQ(pthread_barrierattr_init(&battr), 0);
3456         ATF_REQUIRE(pthread_barrierattr_setpshared(&battr,
3457             PTHREAD_PROCESS_SHARED) == 0);
3458         REQUIRE_EQ(pthread_barrier_init(&barrier, &battr, 2), 0);
3459
3460         (void)snprintf(tmpfile, sizeof(tmpfile), "./ptrace.XXXXXX");
3461         fd = mkstemp(tmpfile);
3462         ATF_REQUIRE(fd >= 0);
3463
3464         ATF_REQUIRE((child = fork()) != -1);
3465         if (child == 0) {
3466                 pthread_t t[2];
3467                 int cfd;
3468
3469                 error = pthread_barrier_wait(&barrier);
3470                 if (error != 0 && error != PTHREAD_BARRIER_SERIAL_THREAD)
3471                         _exit(1);
3472
3473                 cfd = open(tmpfile, O_RDONLY);
3474                 if (cfd < 0)
3475                         _exit(1);
3476
3477                 /*
3478                  * We want at least two threads blocked on the file lock since
3479                  * the SIGSTOP from PT_ATTACH may kick one of them out of
3480                  * sleep.
3481                  */
3482                 if (pthread_create(&t[0], NULL, flock_thread, &cfd) != 0)
3483                         _exit(1);
3484                 if (pthread_create(&t[1], NULL, flock_thread, &cfd) != 0)
3485                         _exit(1);
3486                 if (pthread_join(t[0], NULL) != 0)
3487                         _exit(1);
3488                 if (pthread_join(t[1], NULL) != 0)
3489                         _exit(1);
3490                 _exit(0);
3491         }
3492
3493         REQUIRE_EQ(flock(fd, LOCK_EX), 0);
3494
3495         error = pthread_barrier_wait(&barrier);
3496         ATF_REQUIRE(error == 0 || error == PTHREAD_BARRIER_SERIAL_THREAD);
3497
3498         /*
3499          * Give the child some time to block. Is there a better way to do this?
3500          */
3501         sleep(1);
3502
3503         /*
3504          * Attach and give the child 3 seconds to stop.
3505          */
3506         REQUIRE_EQ(ptrace(PT_ATTACH, child, NULL, 0), 0);
3507         for (i = 0; i < 3; i++) {
3508                 wpid = waitpid(child, &status, WNOHANG);
3509                 if (wpid == child && WIFSTOPPED(status) &&
3510                     WSTOPSIG(status) == SIGSTOP)
3511                         break;
3512                 sleep(1);
3513         }
3514         ATF_REQUIRE_MSG(i < 3, "failed to stop child process after PT_ATTACH");
3515
3516         REQUIRE_EQ(ptrace(PT_DETACH, child, NULL, 0), 0);
3517
3518         REQUIRE_EQ(flock(fd, LOCK_UN), 0);
3519         REQUIRE_EQ(unlink(tmpfile), 0);
3520         REQUIRE_EQ(close(fd), 0);
3521 }
3522
3523 static void
3524 sigusr1_step_handler(int sig)
3525 {
3526
3527         CHILD_REQUIRE_EQ(sig, SIGUSR1);
3528         raise(SIGABRT);
3529 }
3530
3531 /*
3532  * Verify that PT_STEP with a signal invokes the signal before
3533  * stepping the next instruction (and that the next instruction is
3534  * stepped correctly).
3535  */
3536 ATF_TC_WITHOUT_HEAD(ptrace__PT_STEP_with_signal);
3537 ATF_TC_BODY(ptrace__PT_STEP_with_signal, tc)
3538 {
3539         struct ptrace_lwpinfo pl;
3540         pid_t fpid, wpid;
3541         int status;
3542
3543         ATF_REQUIRE((fpid = fork()) != -1);
3544         if (fpid == 0) {
3545                 trace_me();
3546                 signal(SIGUSR1, sigusr1_step_handler);
3547                 raise(SIGABRT);
3548                 exit(1);
3549         }
3550
3551         /* The first wait() should report the stop from SIGSTOP. */
3552         wpid = waitpid(fpid, &status, 0);
3553         REQUIRE_EQ(wpid, fpid);
3554         ATF_REQUIRE(WIFSTOPPED(status));
3555         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3556
3557         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3558
3559         /* The next stop should report the SIGABRT in the child body. */
3560         wpid = waitpid(fpid, &status, 0);
3561         REQUIRE_EQ(wpid, fpid);
3562         ATF_REQUIRE(WIFSTOPPED(status));
3563         REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3564
3565         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3566         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3567         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3568
3569         /* Step the child process inserting SIGUSR1. */
3570         REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, SIGUSR1), 0);
3571
3572         /* The next stop should report the SIGABRT in the signal handler. */
3573         wpid = waitpid(fpid, &status, 0);
3574         REQUIRE_EQ(wpid, fpid);
3575         ATF_REQUIRE(WIFSTOPPED(status));
3576         REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3577
3578         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3579         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3580         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3581
3582         /* Continue the child process discarding the signal. */
3583         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3584
3585         /* The next stop should report a trace trap from PT_STEP. */
3586         wpid = waitpid(fpid, &status, 0);
3587         REQUIRE_EQ(wpid, fpid);
3588         ATF_REQUIRE(WIFSTOPPED(status));
3589         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3590
3591         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3592         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3593         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3594         REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3595
3596         /* Continue the child to let it exit. */
3597         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3598
3599         /* The last event should be for the child process's exit. */
3600         wpid = waitpid(fpid, &status, 0);
3601         ATF_REQUIRE(WIFEXITED(status));
3602         REQUIRE_EQ(WEXITSTATUS(status), 1);
3603
3604         wpid = wait(&status);
3605         REQUIRE_EQ(wpid, -1);
3606         REQUIRE_EQ(errno, ECHILD);
3607 }
3608
3609 #ifdef HAVE_BREAKPOINT
3610 /*
3611  * Verify that a SIGTRAP event with the TRAP_BRKPT code is reported
3612  * for a breakpoint trap.
3613  */
3614 ATF_TC_WITHOUT_HEAD(ptrace__breakpoint_siginfo);
3615 ATF_TC_BODY(ptrace__breakpoint_siginfo, tc)
3616 {
3617         struct ptrace_lwpinfo pl;
3618         pid_t fpid, wpid;
3619         int status;
3620
3621         ATF_REQUIRE((fpid = fork()) != -1);
3622         if (fpid == 0) {
3623                 trace_me();
3624                 breakpoint();
3625                 exit(1);
3626         }
3627
3628         /* The first wait() should report the stop from SIGSTOP. */
3629         wpid = waitpid(fpid, &status, 0);
3630         REQUIRE_EQ(wpid, fpid);
3631         ATF_REQUIRE(WIFSTOPPED(status));
3632         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3633
3634         /* Continue the child ignoring the SIGSTOP. */
3635         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3636
3637         /* The second wait() should report hitting the breakpoint. */
3638         wpid = waitpid(fpid, &status, 0);
3639         REQUIRE_EQ(wpid, fpid);
3640         ATF_REQUIRE(WIFSTOPPED(status));
3641         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3642
3643         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3644         ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3645         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3646         REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3647
3648         /* Kill the child process. */
3649         REQUIRE_EQ(ptrace(PT_KILL, fpid, 0, 0), 0);
3650
3651         /* The last wait() should report the SIGKILL. */
3652         wpid = waitpid(fpid, &status, 0);
3653         REQUIRE_EQ(wpid, fpid);
3654         ATF_REQUIRE(WIFSIGNALED(status));
3655         REQUIRE_EQ(WTERMSIG(status), SIGKILL);
3656
3657         wpid = wait(&status);
3658         REQUIRE_EQ(wpid, -1);
3659         REQUIRE_EQ(errno, ECHILD);
3660 }
3661 #endif /* HAVE_BREAKPOINT */
3662
3663 /*
3664  * Verify that a SIGTRAP event with the TRAP_TRACE code is reported
3665  * for a single-step trap from PT_STEP.
3666  */
3667 ATF_TC_WITHOUT_HEAD(ptrace__step_siginfo);
3668 ATF_TC_BODY(ptrace__step_siginfo, tc)
3669 {
3670         struct ptrace_lwpinfo pl;
3671         pid_t fpid, wpid;
3672         int status;
3673
3674         ATF_REQUIRE((fpid = fork()) != -1);
3675         if (fpid == 0) {
3676                 trace_me();
3677                 exit(1);
3678         }
3679
3680         /* The first wait() should report the stop from SIGSTOP. */
3681         wpid = waitpid(fpid, &status, 0);
3682         REQUIRE_EQ(wpid, fpid);
3683         ATF_REQUIRE(WIFSTOPPED(status));
3684         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3685
3686         /* Step the child ignoring the SIGSTOP. */
3687         REQUIRE_EQ(ptrace(PT_STEP, fpid, (caddr_t)1, 0), 0);
3688
3689         /* The second wait() should report a single-step trap. */
3690         wpid = waitpid(fpid, &status, 0);
3691         REQUIRE_EQ(wpid, fpid);
3692         ATF_REQUIRE(WIFSTOPPED(status));
3693         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3694
3695         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3696         ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3697         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3698         REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_TRACE);
3699
3700         /* Continue the child process. */
3701         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3702
3703         /* The last event should be for the child process's exit. */
3704         wpid = waitpid(fpid, &status, 0);
3705         ATF_REQUIRE(WIFEXITED(status));
3706         REQUIRE_EQ(WEXITSTATUS(status), 1);
3707
3708         wpid = wait(&status);
3709         REQUIRE_EQ(wpid, -1);
3710         REQUIRE_EQ(errno, ECHILD);
3711 }
3712
3713 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
3714 static void *
3715 continue_thread(void *arg __unused)
3716 {
3717         breakpoint();
3718         return (NULL);
3719 }
3720
3721 static __dead2 void
3722 continue_thread_main(void)
3723 {
3724         pthread_t threads[2];
3725
3726         CHILD_REQUIRE(pthread_create(&threads[0], NULL, continue_thread,
3727             NULL) == 0);
3728         CHILD_REQUIRE(pthread_create(&threads[1], NULL, continue_thread,
3729             NULL) == 0);
3730         CHILD_REQUIRE_EQ(pthread_join(threads[0], NULL), 0);
3731         CHILD_REQUIRE_EQ(pthread_join(threads[1], NULL), 0);
3732         exit(1);
3733 }
3734
3735 /*
3736  * Ensure that PT_CONTINUE clears the status of the thread that
3737  * triggered the stop even if a different thread's LWP was passed to
3738  * PT_CONTINUE.
3739  */
3740 ATF_TC_WITHOUT_HEAD(ptrace__PT_CONTINUE_different_thread);
3741 ATF_TC_BODY(ptrace__PT_CONTINUE_different_thread, tc)
3742 {
3743         struct ptrace_lwpinfo pl;
3744         pid_t fpid, wpid;
3745         lwpid_t lwps[2];
3746         bool hit_break[2];
3747         struct reg reg;
3748         int i, j, status;
3749
3750         ATF_REQUIRE((fpid = fork()) != -1);
3751         if (fpid == 0) {
3752                 trace_me();
3753                 continue_thread_main();
3754         }
3755
3756         /* The first wait() should report the stop from SIGSTOP. */
3757         wpid = waitpid(fpid, &status, 0);
3758         REQUIRE_EQ(wpid, fpid);
3759         ATF_REQUIRE(WIFSTOPPED(status));
3760         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3761
3762         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3763             sizeof(pl)) != -1);
3764
3765         REQUIRE_EQ(ptrace(PT_LWP_EVENTS, wpid, NULL, 1), 0);
3766
3767         /* Continue the child ignoring the SIGSTOP. */
3768         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3769
3770         /* One of the new threads should report it's birth. */
3771         wpid = waitpid(fpid, &status, 0);
3772         REQUIRE_EQ(wpid, fpid);
3773         ATF_REQUIRE(WIFSTOPPED(status));
3774         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3775
3776         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3777         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3778             (PL_FLAG_BORN | PL_FLAG_SCX));
3779         lwps[0] = pl.pl_lwpid;
3780
3781         /*
3782          * Suspend this thread to ensure both threads are alive before
3783          * hitting the breakpoint.
3784          */
3785         ATF_REQUIRE(ptrace(PT_SUSPEND, lwps[0], NULL, 0) != -1);
3786
3787         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3788
3789         /* Second thread should report it's birth. */
3790         wpid = waitpid(fpid, &status, 0);
3791         REQUIRE_EQ(wpid, fpid);
3792         ATF_REQUIRE(WIFSTOPPED(status));
3793         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3794
3795         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3796         REQUIRE_EQ((pl.pl_flags & (PL_FLAG_BORN | PL_FLAG_SCX)),
3797             (PL_FLAG_BORN | PL_FLAG_SCX));
3798         ATF_REQUIRE(pl.pl_lwpid != lwps[0]);
3799         lwps[1] = pl.pl_lwpid;
3800
3801         /* Resume both threads waiting for breakpoint events. */
3802         hit_break[0] = hit_break[1] = false;
3803         ATF_REQUIRE(ptrace(PT_RESUME, lwps[0], NULL, 0) != -1);
3804         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3805
3806         /* One thread should report a breakpoint. */
3807         wpid = waitpid(fpid, &status, 0);
3808         REQUIRE_EQ(wpid, fpid);
3809         ATF_REQUIRE(WIFSTOPPED(status));
3810         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3811
3812         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3813         ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3814         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3815         REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3816         if (pl.pl_lwpid == lwps[0])
3817                 i = 0;
3818         else
3819                 i = 1;
3820         hit_break[i] = true;
3821         ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)&reg, 0) != -1);
3822         SKIP_BREAK(&reg);
3823         ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)&reg, 0) != -1);
3824
3825         /*
3826          * Resume both threads but pass the other thread's LWPID to
3827          * PT_CONTINUE.
3828          */
3829         REQUIRE_EQ(ptrace(PT_CONTINUE, lwps[i ^ 1], (caddr_t)1, 0), 0);
3830
3831         /*
3832          * Will now get two thread exit events and one more breakpoint
3833          * event.
3834          */
3835         for (j = 0; j < 3; j++) {
3836                 wpid = waitpid(fpid, &status, 0);
3837                 REQUIRE_EQ(wpid, fpid);
3838                 ATF_REQUIRE(WIFSTOPPED(status));
3839                 REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3840
3841                 ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl,
3842                     sizeof(pl)) != -1);
3843                 
3844                 if (pl.pl_lwpid == lwps[0])
3845                         i = 0;
3846                 else
3847                         i = 1;
3848
3849                 ATF_REQUIRE_MSG(lwps[i] != 0, "event for exited thread");
3850                 if (pl.pl_flags & PL_FLAG_EXITED) {
3851                         ATF_REQUIRE_MSG(hit_break[i],
3852                             "exited thread did not report breakpoint");
3853                         lwps[i] = 0;
3854                 } else {
3855                         ATF_REQUIRE((pl.pl_flags & PL_FLAG_SI) != 0);
3856                         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGTRAP);
3857                         REQUIRE_EQ(pl.pl_siginfo.si_code, TRAP_BRKPT);
3858                         ATF_REQUIRE_MSG(!hit_break[i],
3859                             "double breakpoint event");
3860                         hit_break[i] = true;
3861                         ATF_REQUIRE(ptrace(PT_GETREGS, pl.pl_lwpid, (caddr_t)&reg,
3862                             0) != -1);
3863                         SKIP_BREAK(&reg);
3864                         ATF_REQUIRE(ptrace(PT_SETREGS, pl.pl_lwpid, (caddr_t)&reg,
3865                             0) != -1);
3866                 }
3867
3868                 REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3869         }
3870
3871         /* Both threads should have exited. */
3872         REQUIRE_EQ(lwps[0], 0);
3873         REQUIRE_EQ(lwps[1], 0);
3874
3875         /* The last event should be for the child process's exit. */
3876         wpid = waitpid(fpid, &status, 0);
3877         ATF_REQUIRE(WIFEXITED(status));
3878         REQUIRE_EQ(WEXITSTATUS(status), 1);
3879
3880         wpid = wait(&status);
3881         REQUIRE_EQ(wpid, -1);
3882         REQUIRE_EQ(errno, ECHILD);
3883 }
3884 #endif
3885
3886 /*
3887  * Verify that PT_LWPINFO doesn't return stale siginfo.
3888  */
3889 ATF_TC_WITHOUT_HEAD(ptrace__PT_LWPINFO_stale_siginfo);
3890 ATF_TC_BODY(ptrace__PT_LWPINFO_stale_siginfo, tc)
3891 {
3892         struct ptrace_lwpinfo pl;
3893         pid_t fpid, wpid;
3894         int events, status;
3895
3896         ATF_REQUIRE((fpid = fork()) != -1);
3897         if (fpid == 0) {
3898                 trace_me();
3899                 raise(SIGABRT);
3900                 exit(1);
3901         }
3902
3903         /* The first wait() should report the stop from SIGSTOP. */
3904         wpid = waitpid(fpid, &status, 0);
3905         REQUIRE_EQ(wpid, fpid);
3906         ATF_REQUIRE(WIFSTOPPED(status));
3907         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3908
3909         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3910
3911         /* The next stop should report the SIGABRT in the child body. */
3912         wpid = waitpid(fpid, &status, 0);
3913         REQUIRE_EQ(wpid, fpid);
3914         ATF_REQUIRE(WIFSTOPPED(status));
3915         REQUIRE_EQ(WSTOPSIG(status), SIGABRT);
3916
3917         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3918         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SI);
3919         REQUIRE_EQ(pl.pl_siginfo.si_signo, SIGABRT);
3920
3921         /*
3922          * Continue the process ignoring the signal, but enabling
3923          * syscall traps.
3924          */
3925         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
3926
3927         /*
3928          * The next stop should report a system call entry from
3929          * exit().  PL_FLAGS_SI should not be set.
3930          */
3931         wpid = waitpid(fpid, &status, 0);
3932         REQUIRE_EQ(wpid, fpid);
3933         ATF_REQUIRE(WIFSTOPPED(status));
3934         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3935
3936         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3937         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
3938         REQUIRE_EQ((pl.pl_flags & PL_FLAG_SI), 0);
3939
3940         /* Disable syscall tracing and continue the child to let it exit. */
3941         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
3942             sizeof(events)) == 0);
3943         events &= ~PTRACE_SYSCALL;
3944         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
3945             sizeof(events)) == 0);
3946         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
3947
3948         /* The last event should be for the child process's exit. */
3949         wpid = waitpid(fpid, &status, 0);
3950         ATF_REQUIRE(WIFEXITED(status));
3951         REQUIRE_EQ(WEXITSTATUS(status), 1);
3952
3953         wpid = wait(&status);
3954         REQUIRE_EQ(wpid, -1);
3955         REQUIRE_EQ(errno, ECHILD);
3956 }
3957
3958 /*
3959  * A simple test of PT_GET_SC_ARGS and PT_GET_SC_RET.
3960  */
3961 ATF_TC_WITHOUT_HEAD(ptrace__syscall_args);
3962 ATF_TC_BODY(ptrace__syscall_args, tc)
3963 {
3964         struct ptrace_lwpinfo pl;
3965         struct ptrace_sc_ret psr;
3966         pid_t fpid, wpid;
3967         register_t args[2];
3968         int events, status;
3969
3970         ATF_REQUIRE((fpid = fork()) != -1);
3971         if (fpid == 0) {
3972                 trace_me();
3973                 kill(getpid(), 0);
3974                 close(3);
3975                 exit(1);
3976         }
3977
3978         /* The first wait() should report the stop from SIGSTOP. */
3979         wpid = waitpid(fpid, &status, 0);
3980         REQUIRE_EQ(wpid, fpid);
3981         ATF_REQUIRE(WIFSTOPPED(status));
3982         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
3983
3984         /*
3985          * Continue the process ignoring the signal, but enabling
3986          * syscall traps.
3987          */
3988         REQUIRE_EQ(ptrace(PT_SYSCALL, fpid, (caddr_t)1, 0), 0);
3989
3990         /*
3991          * The next stop should be the syscall entry from getpid().
3992          */
3993         wpid = waitpid(fpid, &status, 0);
3994         REQUIRE_EQ(wpid, fpid);
3995         ATF_REQUIRE(WIFSTOPPED(status));
3996         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
3997
3998         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
3999         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4000         REQUIRE_EQ(pl.pl_syscall_code, SYS_getpid);
4001
4002         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4003
4004         /*
4005          * The next stop should be the syscall exit from getpid().
4006          */
4007         wpid = waitpid(fpid, &status, 0);
4008         REQUIRE_EQ(wpid, fpid);
4009         ATF_REQUIRE(WIFSTOPPED(status));
4010         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4011
4012         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4013         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4014         REQUIRE_EQ(pl.pl_syscall_code, SYS_getpid);
4015
4016         ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4017             sizeof(psr)) != -1);
4018         REQUIRE_EQ(psr.sr_error, 0);
4019         REQUIRE_EQ(psr.sr_retval[0], wpid);
4020
4021         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4022
4023         /*
4024          * The next stop should be the syscall entry from kill().
4025          */
4026         wpid = waitpid(fpid, &status, 0);
4027         REQUIRE_EQ(wpid, fpid);
4028         ATF_REQUIRE(WIFSTOPPED(status));
4029         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4030
4031         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4032         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4033         REQUIRE_EQ(pl.pl_syscall_code, SYS_kill);
4034         REQUIRE_EQ(pl.pl_syscall_narg, 2);
4035
4036         ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4037             sizeof(args)) != -1);
4038         REQUIRE_EQ(args[0], wpid);
4039         REQUIRE_EQ(args[1], 0);
4040
4041         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4042
4043         /*
4044          * The next stop should be the syscall exit from kill().
4045          */
4046         wpid = waitpid(fpid, &status, 0);
4047         REQUIRE_EQ(wpid, fpid);
4048         ATF_REQUIRE(WIFSTOPPED(status));
4049         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4050
4051         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4052         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4053         REQUIRE_EQ(pl.pl_syscall_code, SYS_kill);
4054
4055         ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4056             sizeof(psr)) != -1);
4057         REQUIRE_EQ(psr.sr_error, 0);
4058
4059         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4060
4061         /*
4062          * The next stop should be the syscall entry from close().
4063          */
4064         wpid = waitpid(fpid, &status, 0);
4065         REQUIRE_EQ(wpid, fpid);
4066         ATF_REQUIRE(WIFSTOPPED(status));
4067         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4068
4069         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4070         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCE);
4071         REQUIRE_EQ(pl.pl_syscall_code, SYS_close);
4072         REQUIRE_EQ(pl.pl_syscall_narg, 1);
4073
4074         ATF_REQUIRE(ptrace(PT_GET_SC_ARGS, wpid, (caddr_t)args,
4075             sizeof(args)) != -1);
4076         REQUIRE_EQ(args[0], 3);
4077
4078         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4079
4080         /*
4081          * The next stop should be the syscall exit from close().
4082          */
4083         wpid = waitpid(fpid, &status, 0);
4084         REQUIRE_EQ(wpid, fpid);
4085         ATF_REQUIRE(WIFSTOPPED(status));
4086         REQUIRE_EQ(WSTOPSIG(status), SIGTRAP);
4087
4088         ATF_REQUIRE(ptrace(PT_LWPINFO, wpid, (caddr_t)&pl, sizeof(pl)) != -1);
4089         ATF_REQUIRE(pl.pl_flags & PL_FLAG_SCX);
4090         REQUIRE_EQ(pl.pl_syscall_code, SYS_close);
4091
4092         ATF_REQUIRE(ptrace(PT_GET_SC_RET, wpid, (caddr_t)&psr,
4093             sizeof(psr)) != -1);
4094         REQUIRE_EQ(psr.sr_error, EBADF);
4095
4096         /* Disable syscall tracing and continue the child to let it exit. */
4097         ATF_REQUIRE(ptrace(PT_GET_EVENT_MASK, fpid, (caddr_t)&events,
4098             sizeof(events)) == 0);
4099         events &= ~PTRACE_SYSCALL;
4100         ATF_REQUIRE(ptrace(PT_SET_EVENT_MASK, fpid, (caddr_t)&events,
4101             sizeof(events)) == 0);
4102         REQUIRE_EQ(ptrace(PT_CONTINUE, fpid, (caddr_t)1, 0), 0);
4103
4104         /* The last event should be for the child process's exit. */
4105         wpid = waitpid(fpid, &status, 0);
4106         ATF_REQUIRE(WIFEXITED(status));
4107         REQUIRE_EQ(WEXITSTATUS(status), 1);
4108
4109         wpid = wait(&status);
4110         REQUIRE_EQ(wpid, -1);
4111         REQUIRE_EQ(errno, ECHILD);
4112 }
4113
4114 /*
4115  * Verify that when the process is traced that it isn't reparent
4116  * to the init process when we close all process descriptors.
4117  */
4118 ATF_TC(ptrace__proc_reparent);
4119 ATF_TC_HEAD(ptrace__proc_reparent, tc)
4120 {
4121
4122         atf_tc_set_md_var(tc, "timeout", "2");
4123 }
4124 ATF_TC_BODY(ptrace__proc_reparent, tc)
4125 {
4126         pid_t traced, debuger, wpid;
4127         int pd, status;
4128
4129         traced = pdfork(&pd, 0);
4130         ATF_REQUIRE(traced >= 0);
4131         if (traced == 0) {
4132                 raise(SIGSTOP);
4133                 exit(0);
4134         }
4135         ATF_REQUIRE(pd >= 0);
4136
4137         debuger = fork();
4138         ATF_REQUIRE(debuger >= 0);
4139         if (debuger == 0) {
4140                 /* The traced process is reparented to debuger. */
4141                 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4142                 wpid = waitpid(traced, &status, 0);
4143                 REQUIRE_EQ(wpid, traced);
4144                 ATF_REQUIRE(WIFSTOPPED(status));
4145                 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4146                 REQUIRE_EQ(close(pd), 0);
4147                 REQUIRE_EQ(ptrace(PT_DETACH, traced, (caddr_t)1, 0), 0);
4148
4149                 /* We closed pd so we should not have any child. */
4150                 wpid = wait(&status);
4151                 REQUIRE_EQ(wpid, -1);
4152                 REQUIRE_EQ(errno, ECHILD);
4153
4154                 exit(0);
4155         }
4156
4157         REQUIRE_EQ(close(pd), 0);
4158         wpid = waitpid(debuger, &status, 0);
4159         REQUIRE_EQ(wpid, debuger);
4160         REQUIRE_EQ(WEXITSTATUS(status), 0);
4161
4162         /* Check if we still have any child. */
4163         wpid = wait(&status);
4164         REQUIRE_EQ(wpid, -1);
4165         REQUIRE_EQ(errno, ECHILD);
4166 }
4167
4168 /*
4169  * Ensure that traced processes created with pdfork(2) are visible to
4170  * waitid(P_ALL).
4171  */
4172 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_wait_child);
4173 ATF_TC_BODY(ptrace__procdesc_wait_child, tc)
4174 {
4175         pid_t child, wpid;
4176         int pd, status;
4177
4178         child = pdfork(&pd, 0);
4179         ATF_REQUIRE(child >= 0);
4180
4181         if (child == 0) {
4182                 trace_me();
4183                 (void)raise(SIGSTOP);
4184                 exit(0);
4185         }
4186
4187         wpid = waitpid(child, &status, 0);
4188         REQUIRE_EQ(wpid, child);
4189         ATF_REQUIRE(WIFSTOPPED(status));
4190         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4191
4192         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4193
4194         wpid = wait(&status);
4195         REQUIRE_EQ(wpid, child);
4196         ATF_REQUIRE(WIFSTOPPED(status));
4197         REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4198
4199         ATF_REQUIRE(ptrace(PT_CONTINUE, child, (caddr_t)1, 0) != -1);
4200
4201         /*
4202          * If process was created by pdfork, the return code have to
4203          * be collected through process descriptor.
4204          */
4205         wpid = wait(&status);
4206         REQUIRE_EQ(wpid, -1);
4207         REQUIRE_EQ(errno, ECHILD);
4208
4209         ATF_REQUIRE(close(pd) != -1);
4210 }
4211
4212 /*
4213  * Ensure that traced processes created with pdfork(2) are not visible
4214  * after returning to parent - waitid(P_ALL).
4215  */
4216 ATF_TC_WITHOUT_HEAD(ptrace__procdesc_reparent_wait_child);
4217 ATF_TC_BODY(ptrace__procdesc_reparent_wait_child, tc)
4218 {
4219         pid_t traced, debuger, wpid;
4220         int pd, status;
4221
4222         if (atf_tc_get_config_var_as_bool_wd(tc, "ci", false))
4223                 atf_tc_skip("https://bugs.freebsd.org/243605");
4224
4225         traced = pdfork(&pd, 0);
4226         ATF_REQUIRE(traced >= 0);
4227         if (traced == 0) {
4228                 raise(SIGSTOP);
4229                 exit(0);
4230         }
4231         ATF_REQUIRE(pd >= 0);
4232
4233         debuger = fork();
4234         ATF_REQUIRE(debuger >= 0);
4235         if (debuger == 0) {
4236                 /* The traced process is reparented to debuger. */
4237                 REQUIRE_EQ(ptrace(PT_ATTACH, traced, 0, 0), 0);
4238                 wpid = waitpid(traced, &status, 0);
4239                 REQUIRE_EQ(wpid, traced);
4240                 ATF_REQUIRE(WIFSTOPPED(status));
4241                 REQUIRE_EQ(WSTOPSIG(status), SIGSTOP);
4242
4243                 /* Allow process to die. */
4244                 REQUIRE_EQ(ptrace(PT_CONTINUE, traced, (caddr_t)1, 0), 0);
4245                 wpid = waitpid(traced, &status, 0);
4246                 REQUIRE_EQ(wpid, traced);
4247                 ATF_REQUIRE(WIFEXITED(status));
4248                 REQUIRE_EQ(WEXITSTATUS(status), 0);
4249
4250                 /* Reparent back to the orginal process. */
4251                 REQUIRE_EQ(close(pd), 0);
4252                 exit(0);
4253         }
4254
4255         wpid = waitpid(debuger, &status, 0);
4256         REQUIRE_EQ(wpid, debuger);
4257         REQUIRE_EQ(WEXITSTATUS(status), 0);
4258
4259         /*
4260          * We have a child but it has a process descriptori
4261          * so we should not be able to collect it process.
4262          */
4263         wpid = wait(&status);
4264         REQUIRE_EQ(wpid, -1);
4265         REQUIRE_EQ(errno, ECHILD);
4266
4267         REQUIRE_EQ(close(pd), 0);
4268 }
4269
4270 ATF_TP_ADD_TCS(tp)
4271 {
4272
4273         ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_trace_me);
4274         ATF_TP_ADD_TC(tp, ptrace__parent_wait_after_attach);
4275         ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_child_debugger);
4276         ATF_TP_ADD_TC(tp, ptrace__parent_sees_exit_after_unrelated_debugger);
4277         ATF_TP_ADD_TC(tp, ptrace__parent_exits_before_child);
4278         ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached);
4279         ATF_TP_ADD_TC(tp, ptrace__follow_fork_child_detached);
4280         ATF_TP_ADD_TC(tp, ptrace__follow_fork_parent_detached);
4281         ATF_TP_ADD_TC(tp, ptrace__follow_fork_both_attached_unrelated_debugger);
4282         ATF_TP_ADD_TC(tp,
4283             ptrace__follow_fork_child_detached_unrelated_debugger);
4284         ATF_TP_ADD_TC(tp,
4285             ptrace__follow_fork_parent_detached_unrelated_debugger);
4286         ATF_TP_ADD_TC(tp, ptrace__getppid);
4287         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_fork);
4288         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_vfork);
4289         ATF_TP_ADD_TC(tp, ptrace__new_child_pl_syscall_code_thread);
4290         ATF_TP_ADD_TC(tp, ptrace__lwp_events);
4291         ATF_TP_ADD_TC(tp, ptrace__lwp_events_exec);
4292         ATF_TP_ADD_TC(tp, ptrace__siginfo);
4293         ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_disable);
4294         ATF_TP_ADD_TC(tp, ptrace__ptrace_exec_enable);
4295         ATF_TP_ADD_TC(tp, ptrace__event_mask);
4296         ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork);
4297         ATF_TP_ADD_TC(tp, ptrace__ptrace_vfork_follow);
4298 #ifdef HAVE_BREAKPOINT
4299         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_breakpoint);
4300 #endif
4301         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_system_call);
4302         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_threads);
4303         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_signal);
4304         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_competing_stop);
4305         ATF_TP_ADD_TC(tp, ptrace__PT_KILL_with_signal_full_sigqueue);
4306         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_system_call_entry);
4307         ATF_TP_ADD_TC(tp,
4308             ptrace__PT_CONTINUE_with_signal_system_call_entry_and_exit);
4309         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_full_sigqueue);
4310         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_masked_full_sigqueue);
4311         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_change_sig);
4312         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigtrap_system_call_entry);
4313         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_mix);
4314         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_kqueue);
4315         ATF_TP_ADD_TC(tp, ptrace__killed_with_sigmask);
4316         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_sigmask);
4317         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_with_signal_thread_sigmask);
4318         ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop1);
4319         ATF_TP_ADD_TC(tp, ptrace__parent_terminate_with_pending_sigstop2);
4320         ATF_TP_ADD_TC(tp, ptrace__event_mask_sigkill_discard);
4321         ATF_TP_ADD_TC(tp, ptrace__PT_ATTACH_with_SBDRY_thread);
4322         ATF_TP_ADD_TC(tp, ptrace__PT_STEP_with_signal);
4323 #ifdef HAVE_BREAKPOINT
4324         ATF_TP_ADD_TC(tp, ptrace__breakpoint_siginfo);
4325 #endif
4326         ATF_TP_ADD_TC(tp, ptrace__step_siginfo);
4327 #if defined(HAVE_BREAKPOINT) && defined(SKIP_BREAK)
4328         ATF_TP_ADD_TC(tp, ptrace__PT_CONTINUE_different_thread);
4329 #endif
4330         ATF_TP_ADD_TC(tp, ptrace__PT_LWPINFO_stale_siginfo);
4331         ATF_TP_ADD_TC(tp, ptrace__syscall_args);
4332         ATF_TP_ADD_TC(tp, ptrace__proc_reparent);
4333         ATF_TP_ADD_TC(tp, ptrace__procdesc_wait_child);
4334         ATF_TP_ADD_TC(tp, ptrace__procdesc_reparent_wait_child);
4335
4336         return (atf_no_error());
4337 }