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