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