]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/sys_process.c
MFV: r329072
[FreeBSD/FreeBSD.git] / sys / kern / sys_process.c
1 /*-
2  * SPDX-License-Identifier: BSD-4-Clause
3  *
4  * Copyright (c) 1994, Sean Eric Fagan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Sean Eric Fagan.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  */
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
36
37 #include "opt_compat.h"
38
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/lock.h>
42 #include <sys/mutex.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/pioctl.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/vnode.h>
50 #include <sys/ptrace.h>
51 #include <sys/rwlock.h>
52 #include <sys/sx.h>
53 #include <sys/malloc.h>
54 #include <sys/signalvar.h>
55
56 #include <machine/reg.h>
57
58 #include <security/audit/audit.h>
59
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 #include <vm/vm_extern.h>
63 #include <vm/vm_map.h>
64 #include <vm/vm_kern.h>
65 #include <vm/vm_object.h>
66 #include <vm/vm_page.h>
67 #include <vm/vm_param.h>
68
69 #ifdef COMPAT_FREEBSD32
70 #include <sys/procfs.h>
71 #include <compat/freebsd32/freebsd32_signal.h>
72
73 struct ptrace_io_desc32 {
74         int             piod_op;
75         uint32_t        piod_offs;
76         uint32_t        piod_addr;
77         uint32_t        piod_len;
78 };
79
80 struct ptrace_vm_entry32 {
81         int             pve_entry;
82         int             pve_timestamp;
83         uint32_t        pve_start;
84         uint32_t        pve_end;
85         uint32_t        pve_offset;
86         u_int           pve_prot;
87         u_int           pve_pathlen;
88         int32_t         pve_fileid;
89         u_int           pve_fsid;
90         uint32_t        pve_path;
91 };
92 #endif
93
94 /*
95  * Functions implemented using PROC_ACTION():
96  *
97  * proc_read_regs(proc, regs)
98  *      Get the current user-visible register set from the process
99  *      and copy it into the regs structure (<machine/reg.h>).
100  *      The process is stopped at the time read_regs is called.
101  *
102  * proc_write_regs(proc, regs)
103  *      Update the current register set from the passed in regs
104  *      structure.  Take care to avoid clobbering special CPU
105  *      registers or privileged bits in the PSL.
106  *      Depending on the architecture this may have fix-up work to do,
107  *      especially if the IAR or PCW are modified.
108  *      The process is stopped at the time write_regs is called.
109  *
110  * proc_read_fpregs, proc_write_fpregs
111  *      deal with the floating point register set, otherwise as above.
112  *
113  * proc_read_dbregs, proc_write_dbregs
114  *      deal with the processor debug register set, otherwise as above.
115  *
116  * proc_sstep(proc)
117  *      Arrange for the process to trap after executing a single instruction.
118  */
119
120 #define PROC_ACTION(action) do {                                        \
121         int error;                                                      \
122                                                                         \
123         PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);                        \
124         if ((td->td_proc->p_flag & P_INMEM) == 0)                       \
125                 error = EIO;                                            \
126         else                                                            \
127                 error = (action);                                       \
128         return (error);                                                 \
129 } while(0)
130
131 int
132 proc_read_regs(struct thread *td, struct reg *regs)
133 {
134
135         PROC_ACTION(fill_regs(td, regs));
136 }
137
138 int
139 proc_write_regs(struct thread *td, struct reg *regs)
140 {
141
142         PROC_ACTION(set_regs(td, regs));
143 }
144
145 int
146 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
147 {
148
149         PROC_ACTION(fill_dbregs(td, dbregs));
150 }
151
152 int
153 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
154 {
155
156         PROC_ACTION(set_dbregs(td, dbregs));
157 }
158
159 /*
160  * Ptrace doesn't support fpregs at all, and there are no security holes
161  * or translations for fpregs, so we can just copy them.
162  */
163 int
164 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
165 {
166
167         PROC_ACTION(fill_fpregs(td, fpregs));
168 }
169
170 int
171 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
172 {
173
174         PROC_ACTION(set_fpregs(td, fpregs));
175 }
176
177 #ifdef COMPAT_FREEBSD32
178 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
179 int
180 proc_read_regs32(struct thread *td, struct reg32 *regs32)
181 {
182
183         PROC_ACTION(fill_regs32(td, regs32));
184 }
185
186 int
187 proc_write_regs32(struct thread *td, struct reg32 *regs32)
188 {
189
190         PROC_ACTION(set_regs32(td, regs32));
191 }
192
193 int
194 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
195 {
196
197         PROC_ACTION(fill_dbregs32(td, dbregs32));
198 }
199
200 int
201 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
202 {
203
204         PROC_ACTION(set_dbregs32(td, dbregs32));
205 }
206
207 int
208 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
209 {
210
211         PROC_ACTION(fill_fpregs32(td, fpregs32));
212 }
213
214 int
215 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
216 {
217
218         PROC_ACTION(set_fpregs32(td, fpregs32));
219 }
220 #endif
221
222 int
223 proc_sstep(struct thread *td)
224 {
225
226         PROC_ACTION(ptrace_single_step(td));
227 }
228
229 int
230 proc_rwmem(struct proc *p, struct uio *uio)
231 {
232         vm_map_t map;
233         vm_offset_t pageno;             /* page number */
234         vm_prot_t reqprot;
235         int error, fault_flags, page_offset, writing;
236
237         /*
238          * Assert that someone has locked this vmspace.  (Should be
239          * curthread but we can't assert that.)  This keeps the process
240          * from exiting out from under us until this operation completes.
241          */
242         PROC_ASSERT_HELD(p);
243         PROC_LOCK_ASSERT(p, MA_NOTOWNED);
244
245         /*
246          * The map we want...
247          */
248         map = &p->p_vmspace->vm_map;
249
250         /*
251          * If we are writing, then we request vm_fault() to create a private
252          * copy of each page.  Since these copies will not be writeable by the
253          * process, we must explicity request that they be dirtied.
254          */
255         writing = uio->uio_rw == UIO_WRITE;
256         reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
257         fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
258
259         /*
260          * Only map in one page at a time.  We don't have to, but it
261          * makes things easier.  This way is trivial - right?
262          */
263         do {
264                 vm_offset_t uva;
265                 u_int len;
266                 vm_page_t m;
267
268                 uva = (vm_offset_t)uio->uio_offset;
269
270                 /*
271                  * Get the page number of this segment.
272                  */
273                 pageno = trunc_page(uva);
274                 page_offset = uva - pageno;
275
276                 /*
277                  * How many bytes to copy
278                  */
279                 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
280
281                 /*
282                  * Fault and hold the page on behalf of the process.
283                  */
284                 error = vm_fault_hold(map, pageno, reqprot, fault_flags, &m);
285                 if (error != KERN_SUCCESS) {
286                         if (error == KERN_RESOURCE_SHORTAGE)
287                                 error = ENOMEM;
288                         else
289                                 error = EFAULT;
290                         break;
291                 }
292
293                 /*
294                  * Now do the i/o move.
295                  */
296                 error = uiomove_fromphys(&m, page_offset, len, uio);
297
298                 /* Make the I-cache coherent for breakpoints. */
299                 if (writing && error == 0) {
300                         vm_map_lock_read(map);
301                         if (vm_map_check_protection(map, pageno, pageno +
302                             PAGE_SIZE, VM_PROT_EXECUTE))
303                                 vm_sync_icache(map, uva, len);
304                         vm_map_unlock_read(map);
305                 }
306
307                 /*
308                  * Release the page.
309                  */
310                 vm_page_lock(m);
311                 vm_page_unhold(m);
312                 vm_page_unlock(m);
313
314         } while (error == 0 && uio->uio_resid > 0);
315
316         return (error);
317 }
318
319 static ssize_t
320 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
321     size_t len, enum uio_rw rw)
322 {
323         struct iovec iov;
324         struct uio uio;
325         ssize_t slen;
326         int error;
327
328         MPASS(len < SSIZE_MAX);
329         slen = (ssize_t)len;
330
331         iov.iov_base = (caddr_t)buf;
332         iov.iov_len = len;
333         uio.uio_iov = &iov;
334         uio.uio_iovcnt = 1;
335         uio.uio_offset = va;
336         uio.uio_resid = slen;
337         uio.uio_segflg = UIO_SYSSPACE;
338         uio.uio_rw = rw;
339         uio.uio_td = td;
340         error = proc_rwmem(p, &uio);
341         if (uio.uio_resid == slen)
342                 return (-1);
343         return (slen - uio.uio_resid);
344 }
345
346 ssize_t
347 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
348     size_t len)
349 {
350
351         return (proc_iop(td, p, va, buf, len, UIO_READ));
352 }
353
354 ssize_t
355 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
356     size_t len)
357 {
358
359         return (proc_iop(td, p, va, buf, len, UIO_WRITE));
360 }
361
362 static int
363 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
364 {
365         struct vattr vattr;
366         vm_map_t map;
367         vm_map_entry_t entry;
368         vm_object_t obj, tobj, lobj;
369         struct vmspace *vm;
370         struct vnode *vp;
371         char *freepath, *fullpath;
372         u_int pathlen;
373         int error, index;
374
375         error = 0;
376         obj = NULL;
377
378         vm = vmspace_acquire_ref(p);
379         map = &vm->vm_map;
380         vm_map_lock_read(map);
381
382         do {
383                 entry = map->header.next;
384                 index = 0;
385                 while (index < pve->pve_entry && entry != &map->header) {
386                         entry = entry->next;
387                         index++;
388                 }
389                 if (index != pve->pve_entry) {
390                         error = EINVAL;
391                         break;
392                 }
393                 while (entry != &map->header &&
394                     (entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
395                         entry = entry->next;
396                         index++;
397                 }
398                 if (entry == &map->header) {
399                         error = ENOENT;
400                         break;
401                 }
402
403                 /* We got an entry. */
404                 pve->pve_entry = index + 1;
405                 pve->pve_timestamp = map->timestamp;
406                 pve->pve_start = entry->start;
407                 pve->pve_end = entry->end - 1;
408                 pve->pve_offset = entry->offset;
409                 pve->pve_prot = entry->protection;
410
411                 /* Backing object's path needed? */
412                 if (pve->pve_pathlen == 0)
413                         break;
414
415                 pathlen = pve->pve_pathlen;
416                 pve->pve_pathlen = 0;
417
418                 obj = entry->object.vm_object;
419                 if (obj != NULL)
420                         VM_OBJECT_RLOCK(obj);
421         } while (0);
422
423         vm_map_unlock_read(map);
424
425         pve->pve_fsid = VNOVAL;
426         pve->pve_fileid = VNOVAL;
427
428         if (error == 0 && obj != NULL) {
429                 lobj = obj;
430                 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
431                         if (tobj != obj)
432                                 VM_OBJECT_RLOCK(tobj);
433                         if (lobj != obj)
434                                 VM_OBJECT_RUNLOCK(lobj);
435                         lobj = tobj;
436                         pve->pve_offset += tobj->backing_object_offset;
437                 }
438                 vp = vm_object_vnode(lobj);
439                 if (vp != NULL)
440                         vref(vp);
441                 if (lobj != obj)
442                         VM_OBJECT_RUNLOCK(lobj);
443                 VM_OBJECT_RUNLOCK(obj);
444
445                 if (vp != NULL) {
446                         freepath = NULL;
447                         fullpath = NULL;
448                         vn_fullpath(td, vp, &fullpath, &freepath);
449                         vn_lock(vp, LK_SHARED | LK_RETRY);
450                         if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
451                                 pve->pve_fileid = vattr.va_fileid;
452                                 pve->pve_fsid = vattr.va_fsid;
453                         }
454                         vput(vp);
455
456                         if (fullpath != NULL) {
457                                 pve->pve_pathlen = strlen(fullpath) + 1;
458                                 if (pve->pve_pathlen <= pathlen) {
459                                         error = copyout(fullpath, pve->pve_path,
460                                             pve->pve_pathlen);
461                                 } else
462                                         error = ENAMETOOLONG;
463                         }
464                         if (freepath != NULL)
465                                 free(freepath, M_TEMP);
466                 }
467         }
468         vmspace_free(vm);
469         if (error == 0)
470                 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
471                     p->p_pid, pve->pve_entry, pve->pve_start);
472
473         return (error);
474 }
475
476 #ifdef COMPAT_FREEBSD32
477 static int
478 ptrace_vm_entry32(struct thread *td, struct proc *p,
479     struct ptrace_vm_entry32 *pve32)
480 {
481         struct ptrace_vm_entry pve;
482         int error;
483
484         pve.pve_entry = pve32->pve_entry;
485         pve.pve_pathlen = pve32->pve_pathlen;
486         pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
487
488         error = ptrace_vm_entry(td, p, &pve);
489         if (error == 0) {
490                 pve32->pve_entry = pve.pve_entry;
491                 pve32->pve_timestamp = pve.pve_timestamp;
492                 pve32->pve_start = pve.pve_start;
493                 pve32->pve_end = pve.pve_end;
494                 pve32->pve_offset = pve.pve_offset;
495                 pve32->pve_prot = pve.pve_prot;
496                 pve32->pve_fileid = pve.pve_fileid;
497                 pve32->pve_fsid = pve.pve_fsid;
498         }
499
500         pve32->pve_pathlen = pve.pve_pathlen;
501         return (error);
502 }
503
504 static void
505 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
506     struct ptrace_lwpinfo32 *pl32)
507 {
508
509         bzero(pl32, sizeof(*pl32));
510         pl32->pl_lwpid = pl->pl_lwpid;
511         pl32->pl_event = pl->pl_event;
512         pl32->pl_flags = pl->pl_flags;
513         pl32->pl_sigmask = pl->pl_sigmask;
514         pl32->pl_siglist = pl->pl_siglist;
515         siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
516         strcpy(pl32->pl_tdname, pl->pl_tdname);
517         pl32->pl_child_pid = pl->pl_child_pid;
518         pl32->pl_syscall_code = pl->pl_syscall_code;
519         pl32->pl_syscall_narg = pl->pl_syscall_narg;
520 }
521 #endif /* COMPAT_FREEBSD32 */
522
523 /*
524  * Process debugging system call.
525  */
526 #ifndef _SYS_SYSPROTO_H_
527 struct ptrace_args {
528         int     req;
529         pid_t   pid;
530         caddr_t addr;
531         int     data;
532 };
533 #endif
534
535 #ifdef COMPAT_FREEBSD32
536 /*
537  * This CPP subterfuge is to try and reduce the number of ifdefs in
538  * the body of the code.
539  *   COPYIN(uap->addr, &r.reg, sizeof r.reg);
540  * becomes either:
541  *   copyin(uap->addr, &r.reg, sizeof r.reg);
542  * or
543  *   copyin(uap->addr, &r.reg32, sizeof r.reg32);
544  * .. except this is done at runtime.
545  */
546 #define COPYIN(u, k, s)         wrap32 ? \
547         copyin(u, k ## 32, s ## 32) : \
548         copyin(u, k, s)
549 #define COPYOUT(k, u, s)        wrap32 ? \
550         copyout(k ## 32, u, s ## 32) : \
551         copyout(k, u, s)
552 #else
553 #define COPYIN(u, k, s)         copyin(u, k, s)
554 #define COPYOUT(k, u, s)        copyout(k, u, s)
555 #endif
556 int
557 sys_ptrace(struct thread *td, struct ptrace_args *uap)
558 {
559         /*
560          * XXX this obfuscation is to reduce stack usage, but the register
561          * structs may be too large to put on the stack anyway.
562          */
563         union {
564                 struct ptrace_io_desc piod;
565                 struct ptrace_lwpinfo pl;
566                 struct ptrace_vm_entry pve;
567                 struct dbreg dbreg;
568                 struct fpreg fpreg;
569                 struct reg reg;
570 #ifdef COMPAT_FREEBSD32
571                 struct dbreg32 dbreg32;
572                 struct fpreg32 fpreg32;
573                 struct reg32 reg32;
574                 struct ptrace_io_desc32 piod32;
575                 struct ptrace_lwpinfo32 pl32;
576                 struct ptrace_vm_entry32 pve32;
577 #endif
578                 char args[nitems(td->td_sa.args) * sizeof(register_t)];
579                 int ptevents;
580         } r;
581         void *addr;
582         int error = 0;
583 #ifdef COMPAT_FREEBSD32
584         int wrap32 = 0;
585
586         if (SV_CURPROC_FLAG(SV_ILP32))
587                 wrap32 = 1;
588 #endif
589         AUDIT_ARG_PID(uap->pid);
590         AUDIT_ARG_CMD(uap->req);
591         AUDIT_ARG_VALUE(uap->data);
592         addr = &r;
593         switch (uap->req) {
594         case PT_GET_EVENT_MASK:
595         case PT_GETREGS:
596         case PT_GETFPREGS:
597         case PT_GETDBREGS:
598         case PT_LWPINFO:
599         case PT_GET_SC_ARGS:
600                 break;
601         case PT_SETREGS:
602                 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
603                 break;
604         case PT_SETFPREGS:
605                 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
606                 break;
607         case PT_SETDBREGS:
608                 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
609                 break;
610         case PT_SET_EVENT_MASK:
611                 if (uap->data != sizeof(r.ptevents))
612                         error = EINVAL;
613                 else
614                         error = copyin(uap->addr, &r.ptevents, uap->data);
615                 break;
616         case PT_IO:
617                 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
618                 break;
619         case PT_VM_ENTRY:
620                 error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
621                 break;
622         default:
623                 addr = uap->addr;
624                 break;
625         }
626         if (error)
627                 return (error);
628
629         error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
630         if (error)
631                 return (error);
632
633         switch (uap->req) {
634         case PT_VM_ENTRY:
635                 error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
636                 break;
637         case PT_IO:
638                 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
639                 break;
640         case PT_GETREGS:
641                 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
642                 break;
643         case PT_GETFPREGS:
644                 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
645                 break;
646         case PT_GETDBREGS:
647                 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
648                 break;
649         case PT_GET_EVENT_MASK:
650                 /* NB: The size in uap->data is validated in kern_ptrace(). */
651                 error = copyout(&r.ptevents, uap->addr, uap->data);
652                 break;
653         case PT_LWPINFO:
654                 /* NB: The size in uap->data is validated in kern_ptrace(). */
655                 error = copyout(&r.pl, uap->addr, uap->data);
656                 break;
657         case PT_GET_SC_ARGS:
658                 error = copyout(r.args, uap->addr, MIN(uap->data,
659                     sizeof(r.args)));
660                 break;
661         }
662
663         return (error);
664 }
665 #undef COPYIN
666 #undef COPYOUT
667
668 #ifdef COMPAT_FREEBSD32
669 /*
670  *   PROC_READ(regs, td2, addr);
671  * becomes either:
672  *   proc_read_regs(td2, addr);
673  * or
674  *   proc_read_regs32(td2, addr);
675  * .. except this is done at runtime.  There is an additional
676  * complication in that PROC_WRITE disallows 32 bit consumers
677  * from writing to 64 bit address space targets.
678  */
679 #define PROC_READ(w, t, a)      wrap32 ? \
680         proc_read_ ## w ## 32(t, a) : \
681         proc_read_ ## w (t, a)
682 #define PROC_WRITE(w, t, a)     wrap32 ? \
683         (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
684         proc_write_ ## w (t, a)
685 #else
686 #define PROC_READ(w, t, a)      proc_read_ ## w (t, a)
687 #define PROC_WRITE(w, t, a)     proc_write_ ## w (t, a)
688 #endif
689
690 void
691 proc_set_traced(struct proc *p, bool stop)
692 {
693
694         PROC_LOCK_ASSERT(p, MA_OWNED);
695         p->p_flag |= P_TRACED;
696         if (stop)
697                 p->p_flag2 |= P2_PTRACE_FSTP;
698         p->p_ptevents = PTRACE_DEFAULT;
699         p->p_oppid = p->p_pptr->p_pid;
700 }
701
702 int
703 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
704 {
705         struct iovec iov;
706         struct uio uio;
707         struct proc *curp, *p, *pp;
708         struct thread *td2 = NULL, *td3;
709         struct ptrace_io_desc *piod = NULL;
710         struct ptrace_lwpinfo *pl;
711         int error, num, tmp;
712         int proctree_locked = 0;
713         lwpid_t tid = 0, *buf;
714 #ifdef COMPAT_FREEBSD32
715         int wrap32 = 0, safe = 0;
716         struct ptrace_io_desc32 *piod32 = NULL;
717         struct ptrace_lwpinfo32 *pl32 = NULL;
718         struct ptrace_lwpinfo plr;
719 #endif
720
721         curp = td->td_proc;
722
723         /* Lock proctree before locking the process. */
724         switch (req) {
725         case PT_TRACE_ME:
726         case PT_ATTACH:
727         case PT_STEP:
728         case PT_CONTINUE:
729         case PT_TO_SCE:
730         case PT_TO_SCX:
731         case PT_SYSCALL:
732         case PT_FOLLOW_FORK:
733         case PT_LWP_EVENTS:
734         case PT_GET_EVENT_MASK:
735         case PT_SET_EVENT_MASK:
736         case PT_DETACH:
737         case PT_GET_SC_ARGS:
738                 sx_xlock(&proctree_lock);
739                 proctree_locked = 1;
740                 break;
741         default:
742                 break;
743         }
744
745         if (req == PT_TRACE_ME) {
746                 p = td->td_proc;
747                 PROC_LOCK(p);
748         } else {
749                 if (pid <= PID_MAX) {
750                         if ((p = pfind(pid)) == NULL) {
751                                 if (proctree_locked)
752                                         sx_xunlock(&proctree_lock);
753                                 return (ESRCH);
754                         }
755                 } else {
756                         td2 = tdfind(pid, -1);
757                         if (td2 == NULL) {
758                                 if (proctree_locked)
759                                         sx_xunlock(&proctree_lock);
760                                 return (ESRCH);
761                         }
762                         p = td2->td_proc;
763                         tid = pid;
764                         pid = p->p_pid;
765                 }
766         }
767         AUDIT_ARG_PROCESS(p);
768
769         if ((p->p_flag & P_WEXIT) != 0) {
770                 error = ESRCH;
771                 goto fail;
772         }
773         if ((error = p_cansee(td, p)) != 0)
774                 goto fail;
775
776         if ((error = p_candebug(td, p)) != 0)
777                 goto fail;
778
779         /*
780          * System processes can't be debugged.
781          */
782         if ((p->p_flag & P_SYSTEM) != 0) {
783                 error = EINVAL;
784                 goto fail;
785         }
786
787         if (tid == 0) {
788                 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
789                         KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
790                         td2 = p->p_xthread;
791                 } else {
792                         td2 = FIRST_THREAD_IN_PROC(p);
793                 }
794                 tid = td2->td_tid;
795         }
796
797 #ifdef COMPAT_FREEBSD32
798         /*
799          * Test if we're a 32 bit client and what the target is.
800          * Set the wrap controls accordingly.
801          */
802         if (SV_CURPROC_FLAG(SV_ILP32)) {
803                 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
804                         safe = 1;
805                 wrap32 = 1;
806         }
807 #endif
808         /*
809          * Permissions check
810          */
811         switch (req) {
812         case PT_TRACE_ME:
813                 /*
814                  * Always legal, when there is a parent process which
815                  * could trace us.  Otherwise, reject.
816                  */
817                 if ((p->p_flag & P_TRACED) != 0) {
818                         error = EBUSY;
819                         goto fail;
820                 }
821                 if (p->p_pptr == initproc) {
822                         error = EPERM;
823                         goto fail;
824                 }
825                 break;
826
827         case PT_ATTACH:
828                 /* Self */
829                 if (p == td->td_proc) {
830                         error = EINVAL;
831                         goto fail;
832                 }
833
834                 /* Already traced */
835                 if (p->p_flag & P_TRACED) {
836                         error = EBUSY;
837                         goto fail;
838                 }
839
840                 /* Can't trace an ancestor if you're being traced. */
841                 if (curp->p_flag & P_TRACED) {
842                         for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
843                                 if (pp == p) {
844                                         error = EINVAL;
845                                         goto fail;
846                                 }
847                         }
848                 }
849
850
851                 /* OK */
852                 break;
853
854         case PT_CLEARSTEP:
855                 /* Allow thread to clear single step for itself */
856                 if (td->td_tid == tid)
857                         break;
858
859                 /* FALLTHROUGH */
860         default:
861                 /* not being traced... */
862                 if ((p->p_flag & P_TRACED) == 0) {
863                         error = EPERM;
864                         goto fail;
865                 }
866
867                 /* not being traced by YOU */
868                 if (p->p_pptr != td->td_proc) {
869                         error = EBUSY;
870                         goto fail;
871                 }
872
873                 /* not currently stopped */
874                 if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
875                     p->p_suspcount != p->p_numthreads  ||
876                     (p->p_flag & P_WAITED) == 0) {
877                         error = EBUSY;
878                         goto fail;
879                 }
880
881                 /* OK */
882                 break;
883         }
884
885         /* Keep this process around until we finish this request. */
886         _PHOLD(p);
887
888 #ifdef FIX_SSTEP
889         /*
890          * Single step fixup ala procfs
891          */
892         FIX_SSTEP(td2);
893 #endif
894
895         /*
896          * Actually do the requests
897          */
898
899         td->td_retval[0] = 0;
900
901         switch (req) {
902         case PT_TRACE_ME:
903                 /* set my trace flag and "owner" so it can read/write me */
904                 proc_set_traced(p, false);
905                 if (p->p_flag & P_PPWAIT)
906                         p->p_flag |= P_PPTRACE;
907                 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
908                 break;
909
910         case PT_ATTACH:
911                 /* security check done above */
912                 /*
913                  * It would be nice if the tracing relationship was separate
914                  * from the parent relationship but that would require
915                  * another set of links in the proc struct or for "wait"
916                  * to scan the entire proc table.  To make life easier,
917                  * we just re-parent the process we're trying to trace.
918                  * The old parent is remembered so we can put things back
919                  * on a "detach".
920                  */
921                 proc_set_traced(p, true);
922                 if (p->p_pptr != td->td_proc) {
923                         proc_reparent(p, td->td_proc);
924                 }
925                 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
926                     p->p_oppid);
927
928                 sx_xunlock(&proctree_lock);
929                 proctree_locked = 0;
930                 MPASS(p->p_xthread == NULL);
931                 MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
932
933                 /*
934                  * If already stopped due to a stop signal, clear the
935                  * existing stop before triggering a traced SIGSTOP.
936                  */
937                 if ((p->p_flag & P_STOPPED_SIG) != 0) {
938                         PROC_SLOCK(p);
939                         p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
940                         thread_unsuspend(p);
941                         PROC_SUNLOCK(p);
942                 }
943
944                 kern_psignal(p, SIGSTOP);
945                 break;
946
947         case PT_CLEARSTEP:
948                 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
949                     p->p_pid);
950                 error = ptrace_clear_single_step(td2);
951                 break;
952
953         case PT_SETSTEP:
954                 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
955                     p->p_pid);
956                 error = ptrace_single_step(td2);
957                 break;
958
959         case PT_SUSPEND:
960                 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
961                     p->p_pid);
962                 td2->td_dbgflags |= TDB_SUSPEND;
963                 thread_lock(td2);
964                 td2->td_flags |= TDF_NEEDSUSPCHK;
965                 thread_unlock(td2);
966                 break;
967
968         case PT_RESUME:
969                 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
970                     p->p_pid);
971                 td2->td_dbgflags &= ~TDB_SUSPEND;
972                 break;
973
974         case PT_FOLLOW_FORK:
975                 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
976                     p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
977                     data ? "enabled" : "disabled");
978                 if (data)
979                         p->p_ptevents |= PTRACE_FORK;
980                 else
981                         p->p_ptevents &= ~PTRACE_FORK;
982                 break;
983
984         case PT_LWP_EVENTS:
985                 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
986                     p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
987                     data ? "enabled" : "disabled");
988                 if (data)
989                         p->p_ptevents |= PTRACE_LWP;
990                 else
991                         p->p_ptevents &= ~PTRACE_LWP;
992                 break;
993
994         case PT_GET_EVENT_MASK:
995                 if (data != sizeof(p->p_ptevents)) {
996                         error = EINVAL;
997                         break;
998                 }
999                 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1000                     p->p_ptevents);
1001                 *(int *)addr = p->p_ptevents;
1002                 break;
1003
1004         case PT_SET_EVENT_MASK:
1005                 if (data != sizeof(p->p_ptevents)) {
1006                         error = EINVAL;
1007                         break;
1008                 }
1009                 tmp = *(int *)addr;
1010                 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1011                     PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1012                         error = EINVAL;
1013                         break;
1014                 }
1015                 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1016                     p->p_pid, p->p_ptevents, tmp);
1017                 p->p_ptevents = tmp;
1018                 break;
1019
1020         case PT_GET_SC_ARGS:
1021                 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1022                 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1023 #ifdef COMPAT_FREEBSD32
1024                     || (wrap32 && !safe)
1025 #endif
1026                     ) {
1027                         error = EINVAL;
1028                         break;
1029                 }
1030                 bzero(addr, sizeof(td2->td_sa.args));
1031 #ifdef COMPAT_FREEBSD32
1032                 if (wrap32)
1033                         for (num = 0; num < nitems(td2->td_sa.args); num++)
1034                                 ((uint32_t *)addr)[num] = (uint32_t)
1035                                     td2->td_sa.args[num];
1036                 else
1037 #endif
1038                         bcopy(td2->td_sa.args, addr, td2->td_sa.narg *
1039                             sizeof(register_t));
1040                 break;
1041                 
1042         case PT_STEP:
1043         case PT_CONTINUE:
1044         case PT_TO_SCE:
1045         case PT_TO_SCX:
1046         case PT_SYSCALL:
1047         case PT_DETACH:
1048                 /* Zero means do not send any signal */
1049                 if (data < 0 || data > _SIG_MAXSIG) {
1050                         error = EINVAL;
1051                         break;
1052                 }
1053
1054                 switch (req) {
1055                 case PT_STEP:
1056                         CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1057                             td2->td_tid, p->p_pid, data);
1058                         error = ptrace_single_step(td2);
1059                         if (error)
1060                                 goto out;
1061                         break;
1062                 case PT_CONTINUE:
1063                 case PT_TO_SCE:
1064                 case PT_TO_SCX:
1065                 case PT_SYSCALL:
1066                         if (addr != (void *)1) {
1067                                 error = ptrace_set_pc(td2,
1068                                     (u_long)(uintfptr_t)addr);
1069                                 if (error)
1070                                         goto out;
1071                         }
1072                         switch (req) {
1073                         case PT_TO_SCE:
1074                                 p->p_ptevents |= PTRACE_SCE;
1075                                 CTR4(KTR_PTRACE,
1076                     "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1077                                     p->p_pid, p->p_ptevents,
1078                                     (u_long)(uintfptr_t)addr, data);
1079                                 break;
1080                         case PT_TO_SCX:
1081                                 p->p_ptevents |= PTRACE_SCX;
1082                                 CTR4(KTR_PTRACE,
1083                     "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1084                                     p->p_pid, p->p_ptevents,
1085                                     (u_long)(uintfptr_t)addr, data);
1086                                 break;
1087                         case PT_SYSCALL:
1088                                 p->p_ptevents |= PTRACE_SYSCALL;
1089                                 CTR4(KTR_PTRACE,
1090                     "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1091                                     p->p_pid, p->p_ptevents,
1092                                     (u_long)(uintfptr_t)addr, data);
1093                                 break;
1094                         case PT_CONTINUE:
1095                                 CTR3(KTR_PTRACE,
1096                                     "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1097                                     p->p_pid, (u_long)(uintfptr_t)addr, data);
1098                                 break;
1099                         }
1100                         break;
1101                 case PT_DETACH:
1102                         /*
1103                          * Reset the process parent.
1104                          *
1105                          * NB: This clears P_TRACED before reparenting
1106                          * a detached process back to its original
1107                          * parent.  Otherwise the debugee will be set
1108                          * as an orphan of the debugger.
1109                          */
1110                         p->p_flag &= ~(P_TRACED | P_WAITED);
1111                         if (p->p_oppid != p->p_pptr->p_pid) {
1112                                 PROC_LOCK(p->p_pptr);
1113                                 sigqueue_take(p->p_ksi);
1114                                 PROC_UNLOCK(p->p_pptr);
1115
1116                                 pp = proc_realparent(p);
1117                                 proc_reparent(p, pp);
1118                                 if (pp == initproc)
1119                                         p->p_sigparent = SIGCHLD;
1120                                 CTR3(KTR_PTRACE,
1121                             "PT_DETACH: pid %d reparented to pid %d, sig %d",
1122                                     p->p_pid, pp->p_pid, data);
1123                         } else
1124                                 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1125                                     p->p_pid, data);
1126                         p->p_oppid = 0;
1127                         p->p_ptevents = 0;
1128                         FOREACH_THREAD_IN_PROC(p, td3) {
1129                                 if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1130                                         sigqueue_delete(&td3->td_sigqueue,
1131                                             SIGSTOP);
1132                                 }
1133                                 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1134                                     TDB_SUSPEND);
1135                         }
1136
1137                         if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1138                                 sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1139                                 p->p_flag2 &= ~P2_PTRACE_FSTP;
1140                         }
1141
1142                         /* should we send SIGCHLD? */
1143                         /* childproc_continued(p); */
1144                         break;
1145                 }
1146
1147                 sx_xunlock(&proctree_lock);
1148                 proctree_locked = 0;
1149
1150         sendsig:
1151                 MPASS(proctree_locked == 0);
1152                 
1153                 /* 
1154                  * Clear the pending event for the thread that just
1155                  * reported its event (p_xthread).  This may not be
1156                  * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1157                  * the debugger is resuming a different thread.
1158                  *
1159                  * Deliver any pending signal via the reporting thread.
1160                  */
1161                 MPASS(p->p_xthread != NULL);
1162                 p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1163                 p->p_xthread->td_xsig = data;
1164                 p->p_xthread = NULL;
1165                 p->p_xsig = data;
1166
1167                 /*
1168                  * P_WKILLED is insurance that a PT_KILL/SIGKILL
1169                  * always works immediately, even if another thread is
1170                  * unsuspended first and attempts to handle a
1171                  * different signal or if the POSIX.1b style signal
1172                  * queue cannot accommodate any new signals.
1173                  */
1174                 if (data == SIGKILL)
1175                         p->p_flag |= P_WKILLED;
1176
1177                 /*
1178                  * Unsuspend all threads.  To leave a thread
1179                  * suspended, use PT_SUSPEND to suspend it before
1180                  * continuing the process.
1181                  */
1182                 PROC_SLOCK(p);
1183                 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
1184                 thread_unsuspend(p);
1185                 PROC_SUNLOCK(p);
1186                 break;
1187
1188         case PT_WRITE_I:
1189         case PT_WRITE_D:
1190                 td2->td_dbgflags |= TDB_USERWR;
1191                 PROC_UNLOCK(p);
1192                 error = 0;
1193                 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1194                     sizeof(int)) != sizeof(int))
1195                         error = ENOMEM;
1196                 else
1197                         CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1198                             p->p_pid, addr, data);
1199                 PROC_LOCK(p);
1200                 break;
1201
1202         case PT_READ_I:
1203         case PT_READ_D:
1204                 PROC_UNLOCK(p);
1205                 error = tmp = 0;
1206                 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1207                     sizeof(int)) != sizeof(int))
1208                         error = ENOMEM;
1209                 else
1210                         CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1211                             p->p_pid, addr, tmp);
1212                 td->td_retval[0] = tmp;
1213                 PROC_LOCK(p);
1214                 break;
1215
1216         case PT_IO:
1217 #ifdef COMPAT_FREEBSD32
1218                 if (wrap32) {
1219                         piod32 = addr;
1220                         iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
1221                         iov.iov_len = piod32->piod_len;
1222                         uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
1223                         uio.uio_resid = piod32->piod_len;
1224                 } else
1225 #endif
1226                 {
1227                         piod = addr;
1228                         iov.iov_base = piod->piod_addr;
1229                         iov.iov_len = piod->piod_len;
1230                         uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1231                         uio.uio_resid = piod->piod_len;
1232                 }
1233                 uio.uio_iov = &iov;
1234                 uio.uio_iovcnt = 1;
1235                 uio.uio_segflg = UIO_USERSPACE;
1236                 uio.uio_td = td;
1237 #ifdef COMPAT_FREEBSD32
1238                 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
1239 #else
1240                 tmp = piod->piod_op;
1241 #endif
1242                 switch (tmp) {
1243                 case PIOD_READ_D:
1244                 case PIOD_READ_I:
1245                         CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1246                             p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1247                         uio.uio_rw = UIO_READ;
1248                         break;
1249                 case PIOD_WRITE_D:
1250                 case PIOD_WRITE_I:
1251                         CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1252                             p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1253                         td2->td_dbgflags |= TDB_USERWR;
1254                         uio.uio_rw = UIO_WRITE;
1255                         break;
1256                 default:
1257                         error = EINVAL;
1258                         goto out;
1259                 }
1260                 PROC_UNLOCK(p);
1261                 error = proc_rwmem(p, &uio);
1262 #ifdef COMPAT_FREEBSD32
1263                 if (wrap32)
1264                         piod32->piod_len -= uio.uio_resid;
1265                 else
1266 #endif
1267                         piod->piod_len -= uio.uio_resid;
1268                 PROC_LOCK(p);
1269                 break;
1270
1271         case PT_KILL:
1272                 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1273                 data = SIGKILL;
1274                 goto sendsig;   /* in PT_CONTINUE above */
1275
1276         case PT_SETREGS:
1277                 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1278                     p->p_pid);
1279                 td2->td_dbgflags |= TDB_USERWR;
1280                 error = PROC_WRITE(regs, td2, addr);
1281                 break;
1282
1283         case PT_GETREGS:
1284                 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1285                     p->p_pid);
1286                 error = PROC_READ(regs, td2, addr);
1287                 break;
1288
1289         case PT_SETFPREGS:
1290                 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1291                     p->p_pid);
1292                 td2->td_dbgflags |= TDB_USERWR;
1293                 error = PROC_WRITE(fpregs, td2, addr);
1294                 break;
1295
1296         case PT_GETFPREGS:
1297                 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1298                     p->p_pid);
1299                 error = PROC_READ(fpregs, td2, addr);
1300                 break;
1301
1302         case PT_SETDBREGS:
1303                 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1304                     p->p_pid);
1305                 td2->td_dbgflags |= TDB_USERWR;
1306                 error = PROC_WRITE(dbregs, td2, addr);
1307                 break;
1308
1309         case PT_GETDBREGS:
1310                 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1311                     p->p_pid);
1312                 error = PROC_READ(dbregs, td2, addr);
1313                 break;
1314
1315         case PT_LWPINFO:
1316                 if (data <= 0 ||
1317 #ifdef COMPAT_FREEBSD32
1318                     (!wrap32 && data > sizeof(*pl)) ||
1319                     (wrap32 && data > sizeof(*pl32))) {
1320 #else
1321                     data > sizeof(*pl)) {
1322 #endif
1323                         error = EINVAL;
1324                         break;
1325                 }
1326 #ifdef COMPAT_FREEBSD32
1327                 if (wrap32) {
1328                         pl = &plr;
1329                         pl32 = addr;
1330                 } else
1331 #endif
1332                 pl = addr;
1333                 bzero(pl, sizeof(*pl));
1334                 pl->pl_lwpid = td2->td_tid;
1335                 pl->pl_event = PL_EVENT_NONE;
1336                 pl->pl_flags = 0;
1337                 if (td2->td_dbgflags & TDB_XSIG) {
1338                         pl->pl_event = PL_EVENT_SIGNAL;
1339                         if (td2->td_si.si_signo != 0 &&
1340 #ifdef COMPAT_FREEBSD32
1341                             ((!wrap32 && data >= offsetof(struct ptrace_lwpinfo,
1342                             pl_siginfo) + sizeof(pl->pl_siginfo)) ||
1343                             (wrap32 && data >= offsetof(struct ptrace_lwpinfo32,
1344                             pl_siginfo) + sizeof(struct siginfo32)))
1345 #else
1346                             data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1347                             + sizeof(pl->pl_siginfo)
1348 #endif
1349                         ){
1350                                 pl->pl_flags |= PL_FLAG_SI;
1351                                 pl->pl_siginfo = td2->td_si;
1352                         }
1353                 }
1354                 if (td2->td_dbgflags & TDB_SCE)
1355                         pl->pl_flags |= PL_FLAG_SCE;
1356                 else if (td2->td_dbgflags & TDB_SCX)
1357                         pl->pl_flags |= PL_FLAG_SCX;
1358                 if (td2->td_dbgflags & TDB_EXEC)
1359                         pl->pl_flags |= PL_FLAG_EXEC;
1360                 if (td2->td_dbgflags & TDB_FORK) {
1361                         pl->pl_flags |= PL_FLAG_FORKED;
1362                         pl->pl_child_pid = td2->td_dbg_forked;
1363                         if (td2->td_dbgflags & TDB_VFORK)
1364                                 pl->pl_flags |= PL_FLAG_VFORKED;
1365                 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1366                     TDB_VFORK)
1367                         pl->pl_flags |= PL_FLAG_VFORK_DONE;
1368                 if (td2->td_dbgflags & TDB_CHILD)
1369                         pl->pl_flags |= PL_FLAG_CHILD;
1370                 if (td2->td_dbgflags & TDB_BORN)
1371                         pl->pl_flags |= PL_FLAG_BORN;
1372                 if (td2->td_dbgflags & TDB_EXIT)
1373                         pl->pl_flags |= PL_FLAG_EXITED;
1374                 pl->pl_sigmask = td2->td_sigmask;
1375                 pl->pl_siglist = td2->td_siglist;
1376                 strcpy(pl->pl_tdname, td2->td_name);
1377                 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1378                         pl->pl_syscall_code = td2->td_sa.code;
1379                         pl->pl_syscall_narg = td2->td_sa.narg;
1380                 } else {
1381                         pl->pl_syscall_code = 0;
1382                         pl->pl_syscall_narg = 0;
1383                 }
1384 #ifdef COMPAT_FREEBSD32
1385                 if (wrap32)
1386                         ptrace_lwpinfo_to32(pl, pl32);
1387 #endif
1388                 CTR6(KTR_PTRACE,
1389     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1390                     td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1391                     pl->pl_child_pid, pl->pl_syscall_code);
1392                 break;
1393
1394         case PT_GETNUMLWPS:
1395                 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1396                     p->p_numthreads);
1397                 td->td_retval[0] = p->p_numthreads;
1398                 break;
1399
1400         case PT_GETLWPLIST:
1401                 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1402                     p->p_pid, data, p->p_numthreads);
1403                 if (data <= 0) {
1404                         error = EINVAL;
1405                         break;
1406                 }
1407                 num = imin(p->p_numthreads, data);
1408                 PROC_UNLOCK(p);
1409                 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1410                 tmp = 0;
1411                 PROC_LOCK(p);
1412                 FOREACH_THREAD_IN_PROC(p, td2) {
1413                         if (tmp >= num)
1414                                 break;
1415                         buf[tmp++] = td2->td_tid;
1416                 }
1417                 PROC_UNLOCK(p);
1418                 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1419                 free(buf, M_TEMP);
1420                 if (!error)
1421                         td->td_retval[0] = tmp;
1422                 PROC_LOCK(p);
1423                 break;
1424
1425         case PT_VM_TIMESTAMP:
1426                 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1427                     p->p_pid, p->p_vmspace->vm_map.timestamp);
1428                 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1429                 break;
1430
1431         case PT_VM_ENTRY:
1432                 PROC_UNLOCK(p);
1433 #ifdef COMPAT_FREEBSD32
1434                 if (wrap32)
1435                         error = ptrace_vm_entry32(td, p, addr);
1436                 else
1437 #endif
1438                 error = ptrace_vm_entry(td, p, addr);
1439                 PROC_LOCK(p);
1440                 break;
1441
1442         default:
1443 #ifdef __HAVE_PTRACE_MACHDEP
1444                 if (req >= PT_FIRSTMACH) {
1445                         PROC_UNLOCK(p);
1446                         error = cpu_ptrace(td2, req, addr, data);
1447                         PROC_LOCK(p);
1448                 } else
1449 #endif
1450                         /* Unknown request. */
1451                         error = EINVAL;
1452                 break;
1453         }
1454
1455 out:
1456         /* Drop our hold on this process now that the request has completed. */
1457         _PRELE(p);
1458 fail:
1459         PROC_UNLOCK(p);
1460         if (proctree_locked)
1461                 sx_xunlock(&proctree_lock);
1462         return (error);
1463 }
1464 #undef PROC_READ
1465 #undef PROC_WRITE
1466
1467 /*
1468  * Stop a process because of a debugging event;
1469  * stay stopped until p->p_step is cleared
1470  * (cleared by PIOCCONT in procfs).
1471  */
1472 void
1473 stopevent(struct proc *p, unsigned int event, unsigned int val)
1474 {
1475
1476         PROC_LOCK_ASSERT(p, MA_OWNED);
1477         p->p_step = 1;
1478         CTR3(KTR_PTRACE, "stopevent: pid %d event %u val %u", p->p_pid, event,
1479             val);
1480         do {
1481                 if (event != S_EXIT)
1482                         p->p_xsig = val;
1483                 p->p_xthread = NULL;
1484                 p->p_stype = event;     /* Which event caused the stop? */
1485                 wakeup(&p->p_stype);    /* Wake up any PIOCWAIT'ing procs */
1486                 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1487         } while (p->p_step);
1488 }