xref: /freebsd-14.2/sys/kern/sys_process.c (revision 54c8baa0)
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/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
46 #include <sys/priv.h>
47 #include <sys/proc.h>
48 #include <sys/vnode.h>
49 #include <sys/ptrace.h>
50 #include <sys/rwlock.h>
51 #include <sys/sx.h>
52 #include <sys/malloc.h>
53 #include <sys/signalvar.h>
54 
55 #include <machine/reg.h>
56 
57 #include <security/audit/audit.h>
58 
59 #include <vm/vm.h>
60 #include <vm/pmap.h>
61 #include <vm/vm_extern.h>
62 #include <vm/vm_map.h>
63 #include <vm/vm_kern.h>
64 #include <vm/vm_object.h>
65 #include <vm/vm_page.h>
66 #include <vm/vm_param.h>
67 
68 #ifdef COMPAT_FREEBSD32
69 #include <sys/procfs.h>
70 #endif
71 
72 /*
73  * Functions implemented using PROC_ACTION():
74  *
75  * proc_read_regs(proc, regs)
76  *	Get the current user-visible register set from the process
77  *	and copy it into the regs structure (<machine/reg.h>).
78  *	The process is stopped at the time read_regs is called.
79  *
80  * proc_write_regs(proc, regs)
81  *	Update the current register set from the passed in regs
82  *	structure.  Take care to avoid clobbering special CPU
83  *	registers or privileged bits in the PSL.
84  *	Depending on the architecture this may have fix-up work to do,
85  *	especially if the IAR or PCW are modified.
86  *	The process is stopped at the time write_regs is called.
87  *
88  * proc_read_fpregs, proc_write_fpregs
89  *	deal with the floating point register set, otherwise as above.
90  *
91  * proc_read_dbregs, proc_write_dbregs
92  *	deal with the processor debug register set, otherwise as above.
93  *
94  * proc_sstep(proc)
95  *	Arrange for the process to trap after executing a single instruction.
96  */
97 
98 #define	PROC_ACTION(action) do {					\
99 	int error;							\
100 									\
101 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);			\
102 	if ((td->td_proc->p_flag & P_INMEM) == 0)			\
103 		error = EIO;						\
104 	else								\
105 		error = (action);					\
106 	return (error);							\
107 } while (0)
108 
109 int
110 proc_read_regs(struct thread *td, struct reg *regs)
111 {
112 
113 	PROC_ACTION(fill_regs(td, regs));
114 }
115 
116 int
117 proc_write_regs(struct thread *td, struct reg *regs)
118 {
119 
120 	PROC_ACTION(set_regs(td, regs));
121 }
122 
123 int
124 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
125 {
126 
127 	PROC_ACTION(fill_dbregs(td, dbregs));
128 }
129 
130 int
131 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
132 {
133 
134 	PROC_ACTION(set_dbregs(td, dbregs));
135 }
136 
137 /*
138  * Ptrace doesn't support fpregs at all, and there are no security holes
139  * or translations for fpregs, so we can just copy them.
140  */
141 int
142 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
143 {
144 
145 	PROC_ACTION(fill_fpregs(td, fpregs));
146 }
147 
148 int
149 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
150 {
151 
152 	PROC_ACTION(set_fpregs(td, fpregs));
153 }
154 
155 #ifdef COMPAT_FREEBSD32
156 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
157 int
158 proc_read_regs32(struct thread *td, struct reg32 *regs32)
159 {
160 
161 	PROC_ACTION(fill_regs32(td, regs32));
162 }
163 
164 int
165 proc_write_regs32(struct thread *td, struct reg32 *regs32)
166 {
167 
168 	PROC_ACTION(set_regs32(td, regs32));
169 }
170 
171 int
172 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
173 {
174 
175 	PROC_ACTION(fill_dbregs32(td, dbregs32));
176 }
177 
178 int
179 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
180 {
181 
182 	PROC_ACTION(set_dbregs32(td, dbregs32));
183 }
184 
185 int
186 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
187 {
188 
189 	PROC_ACTION(fill_fpregs32(td, fpregs32));
190 }
191 
192 int
193 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
194 {
195 
196 	PROC_ACTION(set_fpregs32(td, fpregs32));
197 }
198 #endif
199 
200 int
201 proc_sstep(struct thread *td)
202 {
203 
204 	PROC_ACTION(ptrace_single_step(td));
205 }
206 
207 int
208 proc_rwmem(struct proc *p, struct uio *uio)
209 {
210 	vm_map_t map;
211 	vm_offset_t pageno;		/* page number */
212 	vm_prot_t reqprot;
213 	int error, fault_flags, page_offset, writing;
214 
215 	/*
216 	 * Assert that someone has locked this vmspace.  (Should be
217 	 * curthread but we can't assert that.)  This keeps the process
218 	 * from exiting out from under us until this operation completes.
219 	 */
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 dbreg dbreg;
473 		struct fpreg fpreg;
474 		struct reg reg;
475 		char args[sizeof(td->td_sa.args)];
476 		struct ptrace_sc_ret psr;
477 		int ptevents;
478 	} r;
479 	void *addr;
480 	int error = 0;
481 
482 	AUDIT_ARG_PID(uap->pid);
483 	AUDIT_ARG_CMD(uap->req);
484 	AUDIT_ARG_VALUE(uap->data);
485 	addr = &r;
486 	switch (uap->req) {
487 	case PT_GET_EVENT_MASK:
488 	case PT_LWPINFO:
489 	case PT_GET_SC_ARGS:
490 	case PT_GET_SC_RET:
491 		break;
492 	case PT_GETREGS:
493 		bzero(&r.reg, sizeof(r.reg));
494 		break;
495 	case PT_GETFPREGS:
496 		bzero(&r.fpreg, sizeof(r.fpreg));
497 		break;
498 	case PT_GETDBREGS:
499 		bzero(&r.dbreg, sizeof(r.dbreg));
500 		break;
501 	case PT_SETREGS:
502 		error = copyin(uap->addr, &r.reg, sizeof(r.reg));
503 		break;
504 	case PT_SETFPREGS:
505 		error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
506 		break;
507 	case PT_SETDBREGS:
508 		error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
509 		break;
510 	case PT_SET_EVENT_MASK:
511 		if (uap->data != sizeof(r.ptevents))
512 			error = EINVAL;
513 		else
514 			error = copyin(uap->addr, &r.ptevents, uap->data);
515 		break;
516 	case PT_IO:
517 		error = copyin(uap->addr, &r.piod, sizeof(r.piod));
518 		break;
519 	case PT_VM_ENTRY:
520 		error = copyin(uap->addr, &r.pve, sizeof(r.pve));
521 		break;
522 	default:
523 		addr = uap->addr;
524 		break;
525 	}
526 	if (error)
527 		return (error);
528 
529 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
530 	if (error)
531 		return (error);
532 
533 	switch (uap->req) {
534 	case PT_VM_ENTRY:
535 		error = copyout(&r.pve, uap->addr, sizeof(r.pve));
536 		break;
537 	case PT_IO:
538 		error = copyout(&r.piod, uap->addr, sizeof(r.piod));
539 		break;
540 	case PT_GETREGS:
541 		error = copyout(&r.reg, uap->addr, sizeof(r.reg));
542 		break;
543 	case PT_GETFPREGS:
544 		error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
545 		break;
546 	case PT_GETDBREGS:
547 		error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
548 		break;
549 	case PT_GET_EVENT_MASK:
550 		/* NB: The size in uap->data is validated in kern_ptrace(). */
551 		error = copyout(&r.ptevents, uap->addr, uap->data);
552 		break;
553 	case PT_LWPINFO:
554 		/* NB: The size in uap->data is validated in kern_ptrace(). */
555 		error = copyout(&r.pl, uap->addr, uap->data);
556 		break;
557 	case PT_GET_SC_ARGS:
558 		error = copyout(r.args, uap->addr, MIN(uap->data,
559 		    sizeof(r.args)));
560 		break;
561 	case PT_GET_SC_RET:
562 		error = copyout(&r.psr, uap->addr, MIN(uap->data,
563 		    sizeof(r.psr)));
564 		break;
565 	}
566 
567 	return (error);
568 }
569 
570 #ifdef COMPAT_FREEBSD32
571 /*
572  *   PROC_READ(regs, td2, addr);
573  * becomes either:
574  *   proc_read_regs(td2, addr);
575  * or
576  *   proc_read_regs32(td2, addr);
577  * .. except this is done at runtime.  There is an additional
578  * complication in that PROC_WRITE disallows 32 bit consumers
579  * from writing to 64 bit address space targets.
580  */
581 #define	PROC_READ(w, t, a)	wrap32 ? \
582 	proc_read_ ## w ## 32(t, a) : \
583 	proc_read_ ## w (t, a)
584 #define	PROC_WRITE(w, t, a)	wrap32 ? \
585 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
586 	proc_write_ ## w (t, a)
587 #else
588 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
589 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
590 #endif
591 
592 void
593 proc_set_traced(struct proc *p, bool stop)
594 {
595 
596 	sx_assert(&proctree_lock, SX_XLOCKED);
597 	PROC_LOCK_ASSERT(p, MA_OWNED);
598 	p->p_flag |= P_TRACED;
599 	if (stop)
600 		p->p_flag2 |= P2_PTRACE_FSTP;
601 	p->p_ptevents = PTRACE_DEFAULT;
602 }
603 
604 static int
605 proc_can_ptrace(struct thread *td, struct proc *p)
606 {
607 	PROC_LOCK_ASSERT(p, MA_OWNED);
608 
609 	if ((p->p_flag & P_WEXIT) != 0)
610 		return (ESRCH);
611 
612 	/* not being traced... */
613 	if ((p->p_flag & P_TRACED) == 0)
614 		return (EPERM);
615 
616 	/* not being traced by YOU */
617 	if (p->p_pptr != td->td_proc)
618 		return (EBUSY);
619 
620 	/* not currently stopped */
621 	if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
622 	    p->p_suspcount != p->p_numthreads  ||
623 	    (p->p_flag & P_WAITED) == 0)
624 		return (EBUSY);
625 
626 	return (0);
627 }
628 int
629 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
630 {
631 	struct iovec iov;
632 	struct uio uio;
633 	struct proc *curp, *p, *pp;
634 	struct thread *td2 = NULL, *td3;
635 	struct ptrace_io_desc *piod = NULL;
636 	struct ptrace_lwpinfo *pl;
637 	struct ptrace_sc_ret *psr;
638 	int error, num, tmp;
639 	lwpid_t tid = 0, *buf;
640 #ifdef COMPAT_FREEBSD32
641 	int wrap32 = 0, safe = 0;
642 #endif
643 	bool proctree_locked;
644 
645 	curp = td->td_proc;
646 	proctree_locked = false;
647 
648 	/* Lock proctree before locking the process. */
649 	switch (req) {
650 	case PT_TRACE_ME:
651 	case PT_ATTACH:
652 	case PT_STEP:
653 	case PT_CONTINUE:
654 	case PT_TO_SCE:
655 	case PT_TO_SCX:
656 	case PT_SYSCALL:
657 	case PT_FOLLOW_FORK:
658 	case PT_LWP_EVENTS:
659 	case PT_GET_EVENT_MASK:
660 	case PT_SET_EVENT_MASK:
661 	case PT_DETACH:
662 	case PT_GET_SC_ARGS:
663 		sx_xlock(&proctree_lock);
664 		proctree_locked = true;
665 		break;
666 	default:
667 		break;
668 	}
669 
670 	if (req == PT_TRACE_ME) {
671 		p = td->td_proc;
672 		PROC_LOCK(p);
673 	} else {
674 		if (pid <= PID_MAX) {
675 			if ((p = pfind(pid)) == NULL) {
676 				if (proctree_locked)
677 					sx_xunlock(&proctree_lock);
678 				return (ESRCH);
679 			}
680 		} else {
681 			td2 = tdfind(pid, -1);
682 			if (td2 == NULL) {
683 				if (proctree_locked)
684 					sx_xunlock(&proctree_lock);
685 				return (ESRCH);
686 			}
687 			p = td2->td_proc;
688 			tid = pid;
689 			pid = p->p_pid;
690 		}
691 	}
692 	AUDIT_ARG_PROCESS(p);
693 
694 	if ((p->p_flag & P_WEXIT) != 0) {
695 		error = ESRCH;
696 		goto fail;
697 	}
698 	if ((error = p_cansee(td, p)) != 0)
699 		goto fail;
700 
701 	if ((error = p_candebug(td, p)) != 0)
702 		goto fail;
703 
704 	/*
705 	 * System processes can't be debugged.
706 	 */
707 	if ((p->p_flag & P_SYSTEM) != 0) {
708 		error = EINVAL;
709 		goto fail;
710 	}
711 
712 	if (tid == 0) {
713 		if ((p->p_flag & P_STOPPED_TRACE) != 0) {
714 			KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
715 			td2 = p->p_xthread;
716 		} else {
717 			td2 = FIRST_THREAD_IN_PROC(p);
718 		}
719 		tid = td2->td_tid;
720 	}
721 
722 #ifdef COMPAT_FREEBSD32
723 	/*
724 	 * Test if we're a 32 bit client and what the target is.
725 	 * Set the wrap controls accordingly.
726 	 */
727 	if (SV_CURPROC_FLAG(SV_ILP32)) {
728 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
729 			safe = 1;
730 		wrap32 = 1;
731 	}
732 #endif
733 	/*
734 	 * Permissions check
735 	 */
736 	switch (req) {
737 	case PT_TRACE_ME:
738 		/*
739 		 * Always legal, when there is a parent process which
740 		 * could trace us.  Otherwise, reject.
741 		 */
742 		if ((p->p_flag & P_TRACED) != 0) {
743 			error = EBUSY;
744 			goto fail;
745 		}
746 		if (p->p_pptr == initproc) {
747 			error = EPERM;
748 			goto fail;
749 		}
750 		break;
751 
752 	case PT_ATTACH:
753 		/* Self */
754 		if (p == td->td_proc) {
755 			error = EINVAL;
756 			goto fail;
757 		}
758 
759 		/* Already traced */
760 		if (p->p_flag & P_TRACED) {
761 			error = EBUSY;
762 			goto fail;
763 		}
764 
765 		/* Can't trace an ancestor if you're being traced. */
766 		if (curp->p_flag & P_TRACED) {
767 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
768 				if (pp == p) {
769 					error = EINVAL;
770 					goto fail;
771 				}
772 			}
773 		}
774 
775 		/* OK */
776 		break;
777 
778 	case PT_CLEARSTEP:
779 		/* Allow thread to clear single step for itself */
780 		if (td->td_tid == tid)
781 			break;
782 
783 		/* FALLTHROUGH */
784 	default:
785 		error = proc_can_ptrace(td, p);
786 		if (error != 0)
787 			goto fail;
788 
789 		/* Ok */
790 		break;
791 	}
792 
793 	/* Keep this process around until we finish this request. */
794 	_PHOLD(p);
795 
796 	/*
797 	 * Actually do the requests
798 	 */
799 
800 	td->td_retval[0] = 0;
801 
802 	switch (req) {
803 	case PT_TRACE_ME:
804 		/* set my trace flag and "owner" so it can read/write me */
805 		proc_set_traced(p, false);
806 		if (p->p_flag & P_PPWAIT)
807 			p->p_flag |= P_PPTRACE;
808 		CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
809 		break;
810 
811 	case PT_ATTACH:
812 		/* security check done above */
813 		/*
814 		 * It would be nice if the tracing relationship was separate
815 		 * from the parent relationship but that would require
816 		 * another set of links in the proc struct or for "wait"
817 		 * to scan the entire proc table.  To make life easier,
818 		 * we just re-parent the process we're trying to trace.
819 		 * The old parent is remembered so we can put things back
820 		 * on a "detach".
821 		 */
822 		proc_set_traced(p, true);
823 		proc_reparent(p, td->td_proc, false);
824 		CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
825 		    p->p_oppid);
826 
827 		sx_xunlock(&proctree_lock);
828 		proctree_locked = false;
829 		MPASS(p->p_xthread == NULL);
830 		MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
831 
832 		/*
833 		 * If already stopped due to a stop signal, clear the
834 		 * existing stop before triggering a traced SIGSTOP.
835 		 */
836 		if ((p->p_flag & P_STOPPED_SIG) != 0) {
837 			PROC_SLOCK(p);
838 			p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
839 			thread_unsuspend(p);
840 			PROC_SUNLOCK(p);
841 		}
842 
843 		kern_psignal(p, SIGSTOP);
844 		break;
845 
846 	case PT_CLEARSTEP:
847 		CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
848 		    p->p_pid);
849 		error = ptrace_clear_single_step(td2);
850 		break;
851 
852 	case PT_SETSTEP:
853 		CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
854 		    p->p_pid);
855 		error = ptrace_single_step(td2);
856 		break;
857 
858 	case PT_SUSPEND:
859 		CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
860 		    p->p_pid);
861 		td2->td_dbgflags |= TDB_SUSPEND;
862 		thread_lock(td2);
863 		td2->td_flags |= TDF_NEEDSUSPCHK;
864 		thread_unlock(td2);
865 		break;
866 
867 	case PT_RESUME:
868 		CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
869 		    p->p_pid);
870 		td2->td_dbgflags &= ~TDB_SUSPEND;
871 		break;
872 
873 	case PT_FOLLOW_FORK:
874 		CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
875 		    p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
876 		    data ? "enabled" : "disabled");
877 		if (data)
878 			p->p_ptevents |= PTRACE_FORK;
879 		else
880 			p->p_ptevents &= ~PTRACE_FORK;
881 		break;
882 
883 	case PT_LWP_EVENTS:
884 		CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
885 		    p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
886 		    data ? "enabled" : "disabled");
887 		if (data)
888 			p->p_ptevents |= PTRACE_LWP;
889 		else
890 			p->p_ptevents &= ~PTRACE_LWP;
891 		break;
892 
893 	case PT_GET_EVENT_MASK:
894 		if (data != sizeof(p->p_ptevents)) {
895 			error = EINVAL;
896 			break;
897 		}
898 		CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
899 		    p->p_ptevents);
900 		*(int *)addr = p->p_ptevents;
901 		break;
902 
903 	case PT_SET_EVENT_MASK:
904 		if (data != sizeof(p->p_ptevents)) {
905 			error = EINVAL;
906 			break;
907 		}
908 		tmp = *(int *)addr;
909 		if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
910 		    PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
911 			error = EINVAL;
912 			break;
913 		}
914 		CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
915 		    p->p_pid, p->p_ptevents, tmp);
916 		p->p_ptevents = tmp;
917 		break;
918 
919 	case PT_GET_SC_ARGS:
920 		CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
921 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
922 #ifdef COMPAT_FREEBSD32
923 		    || (wrap32 && !safe)
924 #endif
925 		    ) {
926 			error = EINVAL;
927 			break;
928 		}
929 		bzero(addr, sizeof(td2->td_sa.args));
930 		bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg *
931 		    sizeof(register_t));
932 		break;
933 
934 	case PT_GET_SC_RET:
935 		if ((td2->td_dbgflags & (TDB_SCX)) == 0
936 #ifdef COMPAT_FREEBSD32
937 		    || (wrap32 && !safe)
938 #endif
939 		    ) {
940 			error = EINVAL;
941 			break;
942 		}
943 		psr = addr;
944 		bzero(psr, sizeof(*psr));
945 		psr->sr_error = td2->td_errno;
946 		if (psr->sr_error == 0) {
947 			psr->sr_retval[0] = td2->td_retval[0];
948 			psr->sr_retval[1] = td2->td_retval[1];
949 		}
950 		CTR4(KTR_PTRACE,
951 		    "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
952 		    p->p_pid, psr->sr_error, psr->sr_retval[0],
953 		    psr->sr_retval[1]);
954 		break;
955 
956 	case PT_STEP:
957 	case PT_CONTINUE:
958 	case PT_TO_SCE:
959 	case PT_TO_SCX:
960 	case PT_SYSCALL:
961 	case PT_DETACH:
962 		/* Zero means do not send any signal */
963 		if (data < 0 || data > _SIG_MAXSIG) {
964 			error = EINVAL;
965 			break;
966 		}
967 
968 		switch (req) {
969 		case PT_STEP:
970 			CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
971 			    td2->td_tid, p->p_pid, data);
972 			error = ptrace_single_step(td2);
973 			if (error)
974 				goto out;
975 			break;
976 		case PT_CONTINUE:
977 		case PT_TO_SCE:
978 		case PT_TO_SCX:
979 		case PT_SYSCALL:
980 			if (addr != (void *)1) {
981 				error = ptrace_set_pc(td2,
982 				    (u_long)(uintfptr_t)addr);
983 				if (error)
984 					goto out;
985 			}
986 			switch (req) {
987 			case PT_TO_SCE:
988 				p->p_ptevents |= PTRACE_SCE;
989 				CTR4(KTR_PTRACE,
990 		    "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
991 				    p->p_pid, p->p_ptevents,
992 				    (u_long)(uintfptr_t)addr, data);
993 				break;
994 			case PT_TO_SCX:
995 				p->p_ptevents |= PTRACE_SCX;
996 				CTR4(KTR_PTRACE,
997 		    "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
998 				    p->p_pid, p->p_ptevents,
999 				    (u_long)(uintfptr_t)addr, data);
1000 				break;
1001 			case PT_SYSCALL:
1002 				p->p_ptevents |= PTRACE_SYSCALL;
1003 				CTR4(KTR_PTRACE,
1004 		    "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1005 				    p->p_pid, p->p_ptevents,
1006 				    (u_long)(uintfptr_t)addr, data);
1007 				break;
1008 			case PT_CONTINUE:
1009 				CTR3(KTR_PTRACE,
1010 				    "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1011 				    p->p_pid, (u_long)(uintfptr_t)addr, data);
1012 				break;
1013 			}
1014 			break;
1015 		case PT_DETACH:
1016 			/*
1017 			 * Clear P_TRACED before reparenting
1018 			 * a detached process back to its original
1019 			 * parent.  Otherwise the debugee will be set
1020 			 * as an orphan of the debugger.
1021 			 */
1022 			p->p_flag &= ~(P_TRACED | P_WAITED);
1023 
1024 			/*
1025 			 * Reset the process parent.
1026 			 */
1027 			if (p->p_oppid != p->p_pptr->p_pid) {
1028 				PROC_LOCK(p->p_pptr);
1029 				sigqueue_take(p->p_ksi);
1030 				PROC_UNLOCK(p->p_pptr);
1031 
1032 				pp = proc_realparent(p);
1033 				proc_reparent(p, pp, false);
1034 				if (pp == initproc)
1035 					p->p_sigparent = SIGCHLD;
1036 				CTR3(KTR_PTRACE,
1037 			    "PT_DETACH: pid %d reparented to pid %d, sig %d",
1038 				    p->p_pid, pp->p_pid, data);
1039 			} else {
1040 				CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1041 				    p->p_pid, data);
1042 			}
1043 
1044 			p->p_ptevents = 0;
1045 			FOREACH_THREAD_IN_PROC(p, td3) {
1046 				if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1047 					sigqueue_delete(&td3->td_sigqueue,
1048 					    SIGSTOP);
1049 				}
1050 				td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1051 				    TDB_SUSPEND);
1052 			}
1053 
1054 			if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1055 				sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1056 				p->p_flag2 &= ~P2_PTRACE_FSTP;
1057 			}
1058 
1059 			/* should we send SIGCHLD? */
1060 			/* childproc_continued(p); */
1061 			break;
1062 		}
1063 
1064 		sx_xunlock(&proctree_lock);
1065 		proctree_locked = false;
1066 
1067 	sendsig:
1068 		MPASS(!proctree_locked);
1069 
1070 		/*
1071 		 * Clear the pending event for the thread that just
1072 		 * reported its event (p_xthread).  This may not be
1073 		 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1074 		 * the debugger is resuming a different thread.
1075 		 *
1076 		 * Deliver any pending signal via the reporting thread.
1077 		 */
1078 		MPASS(p->p_xthread != NULL);
1079 		p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1080 		p->p_xthread->td_xsig = data;
1081 		p->p_xthread = NULL;
1082 		p->p_xsig = data;
1083 
1084 		/*
1085 		 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1086 		 * always works immediately, even if another thread is
1087 		 * unsuspended first and attempts to handle a
1088 		 * different signal or if the POSIX.1b style signal
1089 		 * queue cannot accommodate any new signals.
1090 		 */
1091 		if (data == SIGKILL)
1092 			proc_wkilled(p);
1093 
1094 		/*
1095 		 * Unsuspend all threads.  To leave a thread
1096 		 * suspended, use PT_SUSPEND to suspend it before
1097 		 * continuing the process.
1098 		 */
1099 		PROC_SLOCK(p);
1100 		p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
1101 		thread_unsuspend(p);
1102 		PROC_SUNLOCK(p);
1103 		itimer_proc_continue(p);
1104 		kqtimer_proc_continue(p);
1105 		break;
1106 
1107 	case PT_WRITE_I:
1108 	case PT_WRITE_D:
1109 		td2->td_dbgflags |= TDB_USERWR;
1110 		PROC_UNLOCK(p);
1111 		error = 0;
1112 		if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1113 		    sizeof(int)) != sizeof(int))
1114 			error = ENOMEM;
1115 		else
1116 			CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1117 			    p->p_pid, addr, data);
1118 		PROC_LOCK(p);
1119 		break;
1120 
1121 	case PT_READ_I:
1122 	case PT_READ_D:
1123 		PROC_UNLOCK(p);
1124 		error = tmp = 0;
1125 		if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1126 		    sizeof(int)) != sizeof(int))
1127 			error = ENOMEM;
1128 		else
1129 			CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1130 			    p->p_pid, addr, tmp);
1131 		td->td_retval[0] = tmp;
1132 		PROC_LOCK(p);
1133 		break;
1134 
1135 	case PT_IO:
1136 		piod = addr;
1137 		iov.iov_base = piod->piod_addr;
1138 		iov.iov_len = piod->piod_len;
1139 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1140 		uio.uio_resid = piod->piod_len;
1141 		uio.uio_iov = &iov;
1142 		uio.uio_iovcnt = 1;
1143 		uio.uio_segflg = UIO_USERSPACE;
1144 		uio.uio_td = td;
1145 		switch (piod->piod_op) {
1146 		case PIOD_READ_D:
1147 		case PIOD_READ_I:
1148 			CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1149 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1150 			uio.uio_rw = UIO_READ;
1151 			break;
1152 		case PIOD_WRITE_D:
1153 		case PIOD_WRITE_I:
1154 			CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1155 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1156 			td2->td_dbgflags |= TDB_USERWR;
1157 			uio.uio_rw = UIO_WRITE;
1158 			break;
1159 		default:
1160 			error = EINVAL;
1161 			goto out;
1162 		}
1163 		PROC_UNLOCK(p);
1164 		error = proc_rwmem(p, &uio);
1165 		piod->piod_len -= uio.uio_resid;
1166 		PROC_LOCK(p);
1167 		break;
1168 
1169 	case PT_KILL:
1170 		CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1171 		data = SIGKILL;
1172 		goto sendsig;	/* in PT_CONTINUE above */
1173 
1174 	case PT_SETREGS:
1175 		CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1176 		    p->p_pid);
1177 		td2->td_dbgflags |= TDB_USERWR;
1178 		error = PROC_WRITE(regs, td2, addr);
1179 		break;
1180 
1181 	case PT_GETREGS:
1182 		CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1183 		    p->p_pid);
1184 		error = PROC_READ(regs, td2, addr);
1185 		break;
1186 
1187 	case PT_SETFPREGS:
1188 		CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1189 		    p->p_pid);
1190 		td2->td_dbgflags |= TDB_USERWR;
1191 		error = PROC_WRITE(fpregs, td2, addr);
1192 		break;
1193 
1194 	case PT_GETFPREGS:
1195 		CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1196 		    p->p_pid);
1197 		error = PROC_READ(fpregs, td2, addr);
1198 		break;
1199 
1200 	case PT_SETDBREGS:
1201 		CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1202 		    p->p_pid);
1203 		td2->td_dbgflags |= TDB_USERWR;
1204 		error = PROC_WRITE(dbregs, td2, addr);
1205 		break;
1206 
1207 	case PT_GETDBREGS:
1208 		CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1209 		    p->p_pid);
1210 		error = PROC_READ(dbregs, td2, addr);
1211 		break;
1212 
1213 	case PT_LWPINFO:
1214 		if (data <= 0 || data > sizeof(*pl)) {
1215 			error = EINVAL;
1216 			break;
1217 		}
1218 		pl = addr;
1219 		bzero(pl, sizeof(*pl));
1220 		pl->pl_lwpid = td2->td_tid;
1221 		pl->pl_event = PL_EVENT_NONE;
1222 		pl->pl_flags = 0;
1223 		if (td2->td_dbgflags & TDB_XSIG) {
1224 			pl->pl_event = PL_EVENT_SIGNAL;
1225 			if (td2->td_si.si_signo != 0 &&
1226 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1227 			    + sizeof(pl->pl_siginfo)){
1228 				pl->pl_flags |= PL_FLAG_SI;
1229 				pl->pl_siginfo = td2->td_si;
1230 			}
1231 		}
1232 		if (td2->td_dbgflags & TDB_SCE)
1233 			pl->pl_flags |= PL_FLAG_SCE;
1234 		else if (td2->td_dbgflags & TDB_SCX)
1235 			pl->pl_flags |= PL_FLAG_SCX;
1236 		if (td2->td_dbgflags & TDB_EXEC)
1237 			pl->pl_flags |= PL_FLAG_EXEC;
1238 		if (td2->td_dbgflags & TDB_FORK) {
1239 			pl->pl_flags |= PL_FLAG_FORKED;
1240 			pl->pl_child_pid = td2->td_dbg_forked;
1241 			if (td2->td_dbgflags & TDB_VFORK)
1242 				pl->pl_flags |= PL_FLAG_VFORKED;
1243 		} else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1244 		    TDB_VFORK)
1245 			pl->pl_flags |= PL_FLAG_VFORK_DONE;
1246 		if (td2->td_dbgflags & TDB_CHILD)
1247 			pl->pl_flags |= PL_FLAG_CHILD;
1248 		if (td2->td_dbgflags & TDB_BORN)
1249 			pl->pl_flags |= PL_FLAG_BORN;
1250 		if (td2->td_dbgflags & TDB_EXIT)
1251 			pl->pl_flags |= PL_FLAG_EXITED;
1252 		pl->pl_sigmask = td2->td_sigmask;
1253 		pl->pl_siglist = td2->td_siglist;
1254 		strcpy(pl->pl_tdname, td2->td_name);
1255 		if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1256 			pl->pl_syscall_code = td2->td_sa.code;
1257 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1258 		} else {
1259 			pl->pl_syscall_code = 0;
1260 			pl->pl_syscall_narg = 0;
1261 		}
1262 		CTR6(KTR_PTRACE,
1263     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1264 		    td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1265 		    pl->pl_child_pid, pl->pl_syscall_code);
1266 		break;
1267 
1268 	case PT_GETNUMLWPS:
1269 		CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1270 		    p->p_numthreads);
1271 		td->td_retval[0] = p->p_numthreads;
1272 		break;
1273 
1274 	case PT_GETLWPLIST:
1275 		CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1276 		    p->p_pid, data, p->p_numthreads);
1277 		if (data <= 0) {
1278 			error = EINVAL;
1279 			break;
1280 		}
1281 		num = imin(p->p_numthreads, data);
1282 		PROC_UNLOCK(p);
1283 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1284 		tmp = 0;
1285 		PROC_LOCK(p);
1286 		FOREACH_THREAD_IN_PROC(p, td2) {
1287 			if (tmp >= num)
1288 				break;
1289 			buf[tmp++] = td2->td_tid;
1290 		}
1291 		PROC_UNLOCK(p);
1292 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1293 		free(buf, M_TEMP);
1294 		if (!error)
1295 			td->td_retval[0] = tmp;
1296 		PROC_LOCK(p);
1297 		break;
1298 
1299 	case PT_VM_TIMESTAMP:
1300 		CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1301 		    p->p_pid, p->p_vmspace->vm_map.timestamp);
1302 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1303 		break;
1304 
1305 	case PT_VM_ENTRY:
1306 		PROC_UNLOCK(p);
1307 		error = ptrace_vm_entry(td, p, addr);
1308 		PROC_LOCK(p);
1309 		break;
1310 
1311 	default:
1312 #ifdef __HAVE_PTRACE_MACHDEP
1313 		if (req >= PT_FIRSTMACH) {
1314 			PROC_UNLOCK(p);
1315 			error = cpu_ptrace(td2, req, addr, data);
1316 			PROC_LOCK(p);
1317 		} else
1318 #endif
1319 			/* Unknown request. */
1320 			error = EINVAL;
1321 		break;
1322 	}
1323 
1324 out:
1325 	/* Drop our hold on this process now that the request has completed. */
1326 	_PRELE(p);
1327 fail:
1328 	PROC_UNLOCK(p);
1329 	if (proctree_locked)
1330 		sx_xunlock(&proctree_lock);
1331 	return (error);
1332 }
1333 #undef PROC_READ
1334 #undef PROC_WRITE
1335