xref: /freebsd/sys/kern/sys_process.c (revision d59c7ea2701fe7b73b32eef49a7c712ef38de5a0)
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/systm.h>
35 #include <sys/caprights.h>
36 #include <sys/filedesc.h>
37 #include <sys/imgact.h>
38 #include <sys/ktr.h>
39 #include <sys/limits.h>
40 #include <sys/lock.h>
41 #include <sys/malloc.h>
42 #include <sys/mman.h>
43 #include <sys/mutex.h>
44 #include <sys/priv.h>
45 #include <sys/proc.h>
46 #include <sys/ptrace.h>
47 #include <sys/reg.h>
48 #include <sys/rwlock.h>
49 #include <sys/signalvar.h>
50 #include <sys/sleepqueue.h>
51 #include <sys/sx.h>
52 #include <sys/syscallsubr.h>
53 #include <sys/sysent.h>
54 #include <sys/sysproto.h>
55 #include <sys/vnode.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 /* Assert it's safe to unlock a process, e.g. to allocate working memory */
73 #define	PROC_ASSERT_TRACEREQ(p)	MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0)
74 
75 /*
76  * Functions implemented below:
77  *
78  * proc_read_regs(proc, regs)
79  *	Get the current user-visible register set from the process
80  *	and copy it into the regs structure (<machine/reg.h>).
81  *	The process is stopped at the time read_regs is called.
82  *
83  * proc_write_regs(proc, regs)
84  *	Update the current register set from the passed in regs
85  *	structure.  Take care to avoid clobbering special CPU
86  *	registers or privileged bits in the PSL.
87  *	Depending on the architecture this may have fix-up work to do,
88  *	especially if the IAR or PCW are modified.
89  *	The process is stopped at the time write_regs is called.
90  *
91  * proc_read_fpregs, proc_write_fpregs
92  *	deal with the floating point register set, otherwise as above.
93  *
94  * proc_read_dbregs, proc_write_dbregs
95  *	deal with the processor debug register set, otherwise as above.
96  *
97  * proc_sstep(proc)
98  *	Arrange for the process to trap after executing a single instruction.
99  */
100 
101 int
102 proc_read_regs(struct thread *td, struct reg *regs)
103 {
104 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
105 	return (fill_regs(td, regs));
106 }
107 
108 int
109 proc_write_regs(struct thread *td, struct reg *regs)
110 {
111 	int error;
112 
113 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
114 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
115 	if (error != 0)
116 		return (error);
117 	return (set_regs(td, regs));
118 }
119 
120 int
121 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
122 {
123 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
124 	return (fill_dbregs(td, dbregs));
125 }
126 
127 int
128 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
129 {
130 	int error;
131 
132 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
133 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
134 	if (error != 0)
135 		return (error);
136 	return (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
144 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
145 {
146 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
147 	return (fill_fpregs(td, fpregs));
148 }
149 
150 int
151 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
152 {
153 	int error;
154 
155 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
156 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
157 	if (error != 0)
158 		return (error);
159 	return (set_fpregs(td, fpregs));
160 }
161 
162 static struct regset *
163 proc_find_regset(struct thread *td, int note)
164 {
165 	struct regset **regsetp, **regset_end, *regset;
166 	struct sysentvec *sv;
167 
168 	sv = td->td_proc->p_sysent;
169 	regsetp = sv->sv_regset_begin;
170 	if (regsetp == NULL)
171 		return (NULL);
172 	regset_end = sv->sv_regset_end;
173 	MPASS(regset_end != NULL);
174 	for (; regsetp < regset_end; regsetp++) {
175 		regset = *regsetp;
176 		if (regset->note != note)
177 			continue;
178 
179 		return (regset);
180 	}
181 
182 	return (NULL);
183 }
184 
185 static int
186 proc_read_regset(struct thread *td, int note, struct iovec *iov)
187 {
188 	struct regset *regset;
189 	struct proc *p;
190 	void *buf;
191 	size_t size;
192 	int error;
193 
194 	regset = proc_find_regset(td, note);
195 	if (regset == NULL)
196 		return (EINVAL);
197 
198 	if (regset->get == NULL)
199 		return (EINVAL);
200 
201 	size = regset->size;
202 	/*
203 	 * The regset is dynamically sized, e.g. the size could change
204 	 * depending on the hardware, or may have a per-thread size.
205 	 */
206 	if (size == 0) {
207 		if (!regset->get(regset, td, NULL, &size))
208 			return (EINVAL);
209 	}
210 
211 	if (iov->iov_base == NULL) {
212 		iov->iov_len = size;
213 		if (iov->iov_len == 0)
214 			return (EINVAL);
215 
216 		return (0);
217 	}
218 
219 	/* The length is wrong, return an error */
220 	if (iov->iov_len != size)
221 		return (EINVAL);
222 
223 	error = 0;
224 	p = td->td_proc;
225 
226 	/* Drop the proc lock while allocating the temp buffer */
227 	PROC_ASSERT_TRACEREQ(p);
228 	PROC_UNLOCK(p);
229 	buf = malloc(size, M_TEMP, M_WAITOK);
230 	PROC_LOCK(p);
231 
232 	if (!regset->get(regset, td, buf, &size)) {
233 		error = EINVAL;
234 	} else {
235 		KASSERT(size == regset->size || regset->size == 0,
236 		    ("%s: Getter function changed the size", __func__));
237 
238 		iov->iov_len = size;
239 		PROC_UNLOCK(p);
240 		error = copyout(buf, iov->iov_base, size);
241 		PROC_LOCK(p);
242 	}
243 
244 	free(buf, M_TEMP);
245 
246 	return (error);
247 }
248 
249 static int
250 proc_write_regset(struct thread *td, int note, struct iovec *iov)
251 {
252 	struct regset *regset;
253 	struct proc *p;
254 	void *buf;
255 	size_t size;
256 	int error;
257 
258 	regset = proc_find_regset(td, note);
259 	if (regset == NULL)
260 		return (EINVAL);
261 
262 	size = regset->size;
263 	/*
264 	 * The regset is dynamically sized, e.g. the size could change
265 	 * depending on the hardware, or may have a per-thread size.
266 	 */
267 	if (size == 0) {
268 		if (!regset->get(regset, td, NULL, &size))
269 			return (EINVAL);
270 	}
271 
272 	/* The length is wrong, return an error */
273 	if (iov->iov_len != size)
274 		return (EINVAL);
275 
276 	if (regset->set == NULL)
277 		return (EINVAL);
278 
279 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
280 	if (error != 0)
281 		return (error);
282 
283 	p = td->td_proc;
284 
285 	/* Drop the proc lock while allocating the temp buffer */
286 	PROC_ASSERT_TRACEREQ(p);
287 	PROC_UNLOCK(p);
288 	buf = malloc(size, M_TEMP, M_WAITOK);
289 	error = copyin(iov->iov_base, buf, size);
290 	PROC_LOCK(p);
291 
292 	if (error == 0) {
293 		if (!regset->set(regset, td, buf, size)) {
294 			error = EINVAL;
295 		}
296 	}
297 
298 	free(buf, M_TEMP);
299 
300 	return (error);
301 }
302 
303 #ifdef COMPAT_FREEBSD32
304 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
305 int
306 proc_read_regs32(struct thread *td, struct reg32 *regs32)
307 {
308 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
309 	return (fill_regs32(td, regs32));
310 }
311 
312 int
313 proc_write_regs32(struct thread *td, struct reg32 *regs32)
314 {
315 	int error;
316 
317 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
318 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
319 	if (error != 0)
320 		return (error);
321 	return (set_regs32(td, regs32));
322 }
323 
324 int
325 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
326 {
327 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
328 	return (fill_dbregs32(td, dbregs32));
329 }
330 
331 int
332 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
333 {
334 	int error;
335 
336 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
337 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
338 	if (error != 0)
339 		return (error);
340 	return (set_dbregs32(td, dbregs32));
341 }
342 
343 int
344 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
345 {
346 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
347 	return (fill_fpregs32(td, fpregs32));
348 }
349 
350 int
351 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
352 {
353 	int error;
354 
355 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
356 	error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
357 	if (error != 0)
358 		return (error);
359 	return (set_fpregs32(td, fpregs32));
360 }
361 #endif
362 
363 int
364 proc_sstep(struct thread *td)
365 {
366 	PROC_LOCK_ASSERT(td->td_proc, MA_OWNED);
367 	return (ptrace_single_step(td));
368 }
369 
370 static int
371 proc_vmspace_check_access(struct thread *td, struct proc *p, int flags)
372 {
373 	PROC_ASSERT_HELD(p);
374 	if ((flags & PRVM_CHECK_DEBUG) != 0)
375 		return (p_candebug(td, p));
376 	if ((flags & PRVM_CHECK_VISIBILITY) != 0)
377 		return (p_cansee(td, p));
378 	return (0);
379 }
380 
381 int
382 proc_vmspace_ref(struct thread *td, struct proc *p, int flags,
383     struct vmspace **vmp)
384 {
385 	struct vmspace *vm;
386 	int error;
387 
388 	MPASS((flags & ~(PRVM_BLOCK_EXEC | PRVM_CHECK_VISIBILITY |
389 	    PRVM_CHECK_DEBUG)) == 0);
390 	MPASS((flags & (PRVM_CHECK_VISIBILITY | PRVM_CHECK_DEBUG)) !=
391 	    (PRVM_CHECK_VISIBILITY | PRVM_CHECK_DEBUG));
392 
393 	PROC_LOCK(p);
394 	if (p != td->td_proc) {
395 		PROC_ASSERT_HELD(p);
396 
397 		/*
398 		 * Make sure that the vmspace doesn't switch out from
399 		 * under us.
400 		 */
401 		if ((flags & PRVM_BLOCK_EXEC) != 0) {
402 			for (;;) {
403 				if (!execve_block(td, p)) {
404 					PROC_LOCK(p);
405 					continue;
406 				}
407 				error = proc_vmspace_check_access(td, p, flags);
408 				if (error != 0) {
409 					execve_unblock(td, p);
410 					PROC_UNLOCK(p);
411 					return (error);
412 				}
413 				break;
414 			}
415 		} else {
416 			error = proc_vmspace_check_access(td, p, flags);
417 			if (error != 0) {
418 				PROC_UNLOCK(p);
419 				return (error);
420 			}
421 		}
422 	}
423 	vm = vmspace_acquire_ref(p);
424 	if (vm == NULL) {
425 		if (p != td->td_proc && (flags & PRVM_BLOCK_EXEC) != 0)
426 			execve_unblock(td, p);
427 		PROC_UNLOCK(p);
428 		return (ESRCH);
429 	}
430 	PROC_UNLOCK(p);
431 	*vmp = vm;
432 	return (0);
433 }
434 
435 void
436 proc_vmspace_unref(struct thread *td, struct proc *p, int flags,
437     struct vmspace *vm)
438 {
439 	vmspace_free(vm);
440 	if (p != td->td_proc && (flags & PRVM_BLOCK_EXEC) != 0) {
441 		PROC_LOCK(p);
442 		PROC_ASSERT_HELD(p);
443 		execve_unblock(td, p);
444 		PROC_UNLOCK(p);
445 	}
446 }
447 
448 static int
449 vmspace_rwmem(struct vmspace *vm, struct uio *uio)
450 {
451 	vm_map_t map;
452 	vm_offset_t pageno;		/* page number */
453 	vm_prot_t reqprot;
454 	ssize_t orig_resid;
455 	int error, fault_flags, page_offset, writing;
456 
457 	map = &vm->vm_map;
458 
459 	/*
460 	 * If we are writing, then we request vm_fault() to create a private
461 	 * copy of each page.  Since these copies will not be writeable by the
462 	 * process, we must explicitly request that they be dirtied.
463 	 */
464 	writing = uio->uio_rw == UIO_WRITE;
465 	reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
466 	fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
467 
468 	orig_resid = uio->uio_resid;
469 
470 	if (writing) {
471 		error = priv_check(curthread, PRIV_PROC_MEM_WRITE);
472 		if (error != 0)
473 			return (error);
474 	}
475 
476 	/*
477 	 * Only map in one page at a time.  We don't have to, but it
478 	 * makes things easier.  This way is trivial - right?
479 	 */
480 	do {
481 		vm_offset_t uva;
482 		u_int len;
483 		vm_page_t m;
484 
485 		uva = (vm_offset_t)uio->uio_offset;
486 
487 		/*
488 		 * Get the page number of this segment.
489 		 */
490 		pageno = trunc_page(uva);
491 		page_offset = uva - pageno;
492 
493 		/*
494 		 * How many bytes to copy
495 		 */
496 		len = MIN(PAGE_SIZE - page_offset, uio->uio_resid);
497 
498 		/*
499 		 * Fault and hold the page on behalf of the process.
500 		 */
501 		error = vm_fault(map, pageno, reqprot, fault_flags, &m);
502 		if (error != KERN_SUCCESS) {
503 			if (error == KERN_RESOURCE_SHORTAGE)
504 				error = ENOMEM;
505 			else
506 				error = EFAULT;
507 			break;
508 		}
509 
510 		/*
511 		 * Now do the i/o move.
512 		 */
513 		error = uiomove_fromphys(&m, page_offset, len, uio);
514 
515 		/* Make the I-cache coherent for breakpoints. */
516 		if (writing && error == 0) {
517 			vm_map_lock_read(map);
518 			if (vm_map_check_protection(map, pageno, pageno +
519 			    PAGE_SIZE, VM_PROT_EXECUTE))
520 				vm_sync_icache(map, uva, len);
521 			vm_map_unlock_read(map);
522 		}
523 
524 		/*
525 		 * Release the page.
526 		 */
527 		vm_page_unwire(m, PQ_ACTIVE);
528 
529 	} while (error == 0 && uio->uio_resid > 0);
530 	return (uio->uio_resid == orig_resid ? error : 0);
531 }
532 
533 int
534 proc_rwmem(struct proc *p, struct uio *uio, int flags)
535 {
536 	struct vmspace *vm;
537 	struct thread *td;
538 	int error;
539 
540 	td = curthread;
541 	error = proc_vmspace_ref(td, p, flags, &vm);
542 	if (error != 0)
543 		return (error);
544 	error = vmspace_rwmem(vm, uio);
545 	proc_vmspace_unref(td, p, flags, vm);
546 	return (error);
547 }
548 
549 ssize_t
550 vmspace_iop(struct thread *td, struct vmspace *vm, vm_offset_t va, void *buf,
551     size_t len, enum uio_rw rw)
552 {
553 	struct iovec iov;
554 	struct uio uio;
555 	ssize_t slen;
556 	int error;
557 
558 	MPASS(len < SSIZE_MAX);
559 	slen = (ssize_t)len;
560 
561 	iov.iov_base = (caddr_t)buf;
562 	iov.iov_len = len;
563 	uio.uio_iov = &iov;
564 	uio.uio_iovcnt = 1;
565 	uio.uio_offset = va;
566 	uio.uio_resid = slen;
567 	uio.uio_segflg = UIO_SYSSPACE;
568 	uio.uio_rw = rw;
569 	uio.uio_td = td;
570 	error = vmspace_rwmem(vm, &uio);
571 	if (error != 0 || uio.uio_resid == slen)
572 		return (-1);
573 	return (slen - uio.uio_resid);
574 }
575 
576 ssize_t
577 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
578     size_t len)
579 {
580 
581 	return (vmspace_iop(td, p->p_vmspace, va, buf, len, UIO_READ));
582 }
583 
584 ssize_t
585 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
586     size_t len)
587 {
588 
589 	return (vmspace_iop(td, p->p_vmspace, va, buf, len, UIO_WRITE));
590 }
591 
592 static int
593 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
594 {
595 	struct vattr vattr;
596 	vm_map_t map;
597 	vm_map_entry_t entry;
598 	vm_object_t obj, tobj, lobj;
599 	struct vmspace *vm;
600 	struct vnode *vp;
601 	char *freepath, *fullpath;
602 	u_int pathlen;
603 	int error, index;
604 
605 	error = 0;
606 	obj = NULL;
607 
608 	vm = vmspace_acquire_ref(p);
609 	map = &vm->vm_map;
610 	vm_map_lock_read(map);
611 
612 	do {
613 		KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
614 		    ("Submap in map header"));
615 		index = 0;
616 		VM_MAP_ENTRY_FOREACH(entry, map) {
617 			if (index >= pve->pve_entry &&
618 			    (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
619 				break;
620 			index++;
621 		}
622 		if (index < pve->pve_entry) {
623 			error = EINVAL;
624 			break;
625 		}
626 		if (entry == &map->header) {
627 			error = ENOENT;
628 			break;
629 		}
630 
631 		/* We got an entry. */
632 		pve->pve_entry = index + 1;
633 		pve->pve_timestamp = map->timestamp;
634 		pve->pve_start = entry->start;
635 		pve->pve_end = entry->end - 1;
636 		pve->pve_offset = entry->offset;
637 		pve->pve_prot = entry->protection |
638 		    PROT_MAX(entry->max_protection);
639 
640 		/* Backing object's path needed? */
641 		if (pve->pve_pathlen == 0)
642 			break;
643 
644 		pathlen = pve->pve_pathlen;
645 		pve->pve_pathlen = 0;
646 
647 		obj = entry->object.vm_object;
648 		if (obj != NULL)
649 			VM_OBJECT_RLOCK(obj);
650 	} while (0);
651 
652 	vm_map_unlock_read(map);
653 
654 	pve->pve_fsid = VNOVAL;
655 	pve->pve_fileid = VNOVAL;
656 
657 	if (error == 0 && obj != NULL) {
658 		lobj = obj;
659 		for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
660 			if (tobj != obj)
661 				VM_OBJECT_RLOCK(tobj);
662 			if (lobj != obj)
663 				VM_OBJECT_RUNLOCK(lobj);
664 			lobj = tobj;
665 			pve->pve_offset += tobj->backing_object_offset;
666 		}
667 		vp = vm_object_vnode(lobj);
668 		if (vp != NULL)
669 			vref(vp);
670 		if (lobj != obj)
671 			VM_OBJECT_RUNLOCK(lobj);
672 		VM_OBJECT_RUNLOCK(obj);
673 
674 		if (vp != NULL) {
675 			freepath = NULL;
676 			fullpath = NULL;
677 			vn_fullpath(vp, &fullpath, &freepath);
678 			vn_lock(vp, LK_SHARED | LK_RETRY);
679 			if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
680 				pve->pve_fileid = vattr.va_fileid;
681 				pve->pve_fsid = vattr.va_fsid;
682 			}
683 			vput(vp);
684 
685 			if (fullpath != NULL) {
686 				pve->pve_pathlen = strlen(fullpath) + 1;
687 				if (pve->pve_pathlen <= pathlen) {
688 					error = copyout(fullpath, pve->pve_path,
689 					    pve->pve_pathlen);
690 				} else
691 					error = ENAMETOOLONG;
692 			}
693 			if (freepath != NULL)
694 				free(freepath, M_TEMP);
695 		}
696 	}
697 	vmspace_free(vm);
698 	if (error == 0)
699 		CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
700 		    p->p_pid, pve->pve_entry, pve->pve_start);
701 
702 	return (error);
703 }
704 
705 /*
706  * Process debugging system call.
707  */
708 #ifndef _SYS_SYSPROTO_H_
709 struct ptrace_args {
710 	int	req;
711 	pid_t	pid;
712 	caddr_t	addr;
713 	int	data;
714 };
715 #endif
716 
717 int
718 sys_ptrace(struct thread *td, struct ptrace_args *uap)
719 {
720 	/*
721 	 * XXX this obfuscation is to reduce stack usage, but the register
722 	 * structs may be too large to put on the stack anyway.
723 	 */
724 	union {
725 		struct ptrace_io_desc piod;
726 		struct ptrace_lwpinfo pl;
727 		struct ptrace_vm_entry pve;
728 		struct ptrace_coredump pc;
729 		struct ptrace_sc_remote sr;
730 		struct dbreg dbreg;
731 		struct fpreg fpreg;
732 		struct reg reg;
733 		struct iovec vec;
734 		syscallarg_t args[nitems(td->td_sa.args)];
735 		struct ptrace_sc_ret psr;
736 		int ptevents;
737 		struct ptrace_child *children;
738 	} r;
739 	syscallarg_t pscr_args[nitems(td->td_sa.args)];
740 	void *addr;
741 	int error;
742 
743 	if (!allow_ptrace)
744 		return (ENOSYS);
745 	error = 0;
746 
747 	AUDIT_ARG_PID(uap->pid);
748 	AUDIT_ARG_CMD(uap->req);
749 	AUDIT_ARG_VALUE(uap->data);
750 	addr = &r;
751 	switch (uap->req) {
752 	case PT_GET_EVENT_MASK:
753 	case PT_LWPINFO:
754 	case PT_GET_SC_ARGS:
755 	case PT_GET_SC_RET:
756 		break;
757 	case PT_SET_SC_RET:
758 		if (uap->data != sizeof(r.psr))
759 			error = EINVAL;
760 		else
761 			error = copyin(uap->addr, &r.psr, sizeof(r.psr));
762 		break;
763 	case PT_GETREGS:
764 		bzero(&r.reg, sizeof(r.reg));
765 		break;
766 	case PT_GETFPREGS:
767 		bzero(&r.fpreg, sizeof(r.fpreg));
768 		break;
769 	case PT_GETDBREGS:
770 		bzero(&r.dbreg, sizeof(r.dbreg));
771 		break;
772 	case PT_GETREGSET:
773 	case PT_SETREGSET:
774 		error = copyin(uap->addr, &r.vec, sizeof(r.vec));
775 		break;
776 	case PT_SETREGS:
777 		error = copyin(uap->addr, &r.reg, sizeof(r.reg));
778 		break;
779 	case PT_SETFPREGS:
780 		error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
781 		break;
782 	case PT_SETDBREGS:
783 		error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
784 		break;
785 	case PT_SET_EVENT_MASK:
786 		if (uap->data != sizeof(r.ptevents))
787 			error = EINVAL;
788 		else
789 			error = copyin(uap->addr, &r.ptevents, uap->data);
790 		break;
791 	case PT_IO:
792 		error = copyin(uap->addr, &r.piod, sizeof(r.piod));
793 		break;
794 	case PT_VM_ENTRY:
795 		error = copyin(uap->addr, &r.pve, sizeof(r.pve));
796 		break;
797 	case PT_COREDUMP:
798 		if (uap->data != sizeof(r.pc))
799 			error = EINVAL;
800 		else
801 			error = copyin(uap->addr, &r.pc, uap->data);
802 		break;
803 	case PT_SC_REMOTE:
804 		if (uap->data != sizeof(r.sr)) {
805 			error = EINVAL;
806 			break;
807 		}
808 		error = copyin(uap->addr, &r.sr, uap->data);
809 		if (error != 0)
810 			break;
811 		if (r.sr.pscr_nargs > nitems(td->td_sa.args)) {
812 			error = EINVAL;
813 			break;
814 		}
815 		error = copyin(r.sr.pscr_args, pscr_args,
816 		    sizeof(u_long) * r.sr.pscr_nargs);
817 		if (error != 0)
818 			break;
819 		r.sr.pscr_args = pscr_args;
820 		break;
821 	case PT_GET_CHILDREN:
822 		if (uap->addr == NULL)
823 			addr = NULL;
824 		else if (uap->data < 0)
825 			error = EINVAL;
826 		else
827 			addr = &r.children;
828 		break;
829 	case PTINTERNAL_FIRST ... PTINTERNAL_LAST:
830 		error = EINVAL;
831 		break;
832 	default:
833 		addr = uap->addr;
834 		break;
835 	}
836 	if (error != 0)
837 		return (error);
838 
839 	error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
840 	if (error != 0)
841 		return (error);
842 
843 	switch (uap->req) {
844 	case PT_VM_ENTRY:
845 		error = copyout(&r.pve, uap->addr, sizeof(r.pve));
846 		break;
847 	case PT_IO:
848 		error = copyout(&r.piod, uap->addr, sizeof(r.piod));
849 		break;
850 	case PT_GETREGS:
851 		error = copyout(&r.reg, uap->addr, sizeof(r.reg));
852 		break;
853 	case PT_GETFPREGS:
854 		error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
855 		break;
856 	case PT_GETDBREGS:
857 		error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
858 		break;
859 	case PT_GETREGSET:
860 		error = copyout(&r.vec, uap->addr, sizeof(r.vec));
861 		break;
862 	case PT_GET_EVENT_MASK:
863 		/* NB: The size in uap->data is validated in kern_ptrace(). */
864 		error = copyout(&r.ptevents, uap->addr, uap->data);
865 		break;
866 	case PT_LWPINFO:
867 		/* NB: The size in uap->data is validated in kern_ptrace(). */
868 		error = copyout(&r.pl, uap->addr, uap->data);
869 		break;
870 	case PT_GET_SC_ARGS:
871 		error = copyout(r.args, uap->addr, MIN(uap->data,
872 		    sizeof(r.args)));
873 		break;
874 	case PT_GET_SC_RET:
875 		error = copyout(&r.psr, uap->addr, MIN(uap->data,
876 		    sizeof(r.psr)));
877 		break;
878 	case PT_SC_REMOTE:
879 		error = copyout(&r.sr.pscr_ret, uap->addr +
880 		    offsetof(struct ptrace_sc_remote, pscr_ret),
881 		    sizeof(r.sr.pscr_ret));
882 		break;
883 	case PT_GET_CHILDREN:
884 		if (uap->addr != NULL) {
885 			error = copyout(r.children, uap->addr,
886 			    td->td_retval[0] * sizeof(struct ptrace_child));
887 			free(r.children, M_TEMP);
888 		}
889 		break;
890 	}
891 
892 	return (error);
893 }
894 
895 #ifdef COMPAT_FREEBSD32
896 /*
897  *   PROC_READ(regs, td2, addr);
898  * becomes either:
899  *   proc_read_regs(td2, addr);
900  * or
901  *   proc_read_regs32(td2, addr);
902  * .. except this is done at runtime.  There is an additional
903  * complication in that PROC_WRITE disallows 32 bit consumers
904  * from writing to 64 bit address space targets.
905  */
906 #define	PROC_READ(w, t, a)	wrap32 ? \
907 	proc_read_ ## w ## 32(t, a) : \
908 	proc_read_ ## w (t, a)
909 #define	PROC_WRITE(w, t, a)	wrap32 ? \
910 	(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
911 	proc_write_ ## w (t, a)
912 #else
913 #define	PROC_READ(w, t, a)	proc_read_ ## w (t, a)
914 #define	PROC_WRITE(w, t, a)	proc_write_ ## w (t, a)
915 #endif
916 
917 void
918 proc_set_traced(struct proc *p, bool stop)
919 {
920 
921 	sx_assert(&proctree_lock, SX_XLOCKED);
922 	PROC_LOCK_ASSERT(p, MA_OWNED);
923 	p->p_flag |= P_TRACED;
924 	if (stop)
925 		p->p_flag2 |= P2_PTRACE_FSTP;
926 	p->p_ptevents = PTRACE_DEFAULT;
927 }
928 
929 void
930 ptrace_unsuspend(struct proc *p)
931 {
932 	PROC_LOCK_ASSERT(p, MA_OWNED);
933 
934 	PROC_SLOCK(p);
935 	p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
936 	thread_unsuspend(p);
937 	PROC_SUNLOCK(p);
938 	itimer_proc_continue(p);
939 	kqtimer_proc_continue(p);
940 }
941 
942 static int
943 proc_can_ptrace1(struct thread *td, struct proc *p)
944 {
945 	int error;
946 
947 	PROC_LOCK_ASSERT(p, MA_OWNED);
948 
949 	if ((p->p_flag & P_WEXIT) != 0)
950 		return (ESRCH);
951 	if ((error = p_cansee(td, p)) != 0)
952 		return (error);
953 	if ((error = p_candebug(td, p)) != 0)
954 		return (error);
955 	return (0);
956 }
957 
958 static int
959 proc_can_ptrace(struct thread *td, struct proc *p)
960 {
961 	int error;
962 
963 	PROC_LOCK_ASSERT(p, MA_OWNED);
964 
965 	if ((error = proc_can_ptrace1(td, p)) != 0)
966 		return (error);
967 
968 	/* not being traced... */
969 	if ((p->p_flag & P_TRACED) == 0)
970 		return (EPERM);
971 
972 	/* not being traced by YOU */
973 	if (p->p_pptr != td->td_proc)
974 		return (EBUSY);
975 
976 	/* not currently stopped */
977 	if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
978 	    p->p_suspcount != p->p_numthreads  ||
979 	    (p->p_flag & P_WAITED) == 0)
980 		return (EBUSY);
981 
982 	return (0);
983 }
984 
985 static int
986 ptrace_count_children(struct thread *td, struct proc *p, bool count_everything)
987 {
988 	struct proc *pp;
989 	int error, num;
990 
991 	sx_assert(&proctree_lock, SX_LOCKED);
992 	num = 0;
993 	LIST_FOREACH(pp, &p->p_children, p_sibling) {
994 		if (count_everything) {
995 			error = 0;
996 		} else {
997 			PROC_LOCK(pp);
998 			error = p_cansee(td, pp);
999 			PROC_UNLOCK(pp);
1000 		}
1001 		if (error != 0)
1002 			continue;
1003 		num++;
1004 	}
1005 	LIST_FOREACH(pp, &p->p_orphans, p_orphan) {
1006 		if (count_everything) {
1007 			error = 0;
1008 		} else {
1009 			PROC_LOCK(pp);
1010 			error = p_cansee(td, pp);
1011 			PROC_UNLOCK(pp);
1012 		}
1013 		if (error != 0)
1014 			continue;
1015 		num++;
1016 	}
1017 	return (num);
1018 }
1019 
1020 static bool
1021 ptrace_report_child(struct thread *td, struct proc *p, struct proc *pp,
1022     struct ptrace_child *ptc)
1023 {
1024 	sx_assert(&proctree_lock, SX_LOCKED);
1025 
1026 	PROC_LOCK(pp);
1027 	if (p_cansee(td, pp) != 0) {
1028 		PROC_UNLOCK(pp);
1029 		return (false);
1030 	}
1031 	ptc->pid = pp->p_pid;
1032 	if ((pp->p_flag & P_TRACED) != 0) {
1033 		ptc->flags |= PTCHLD_TRACED;
1034 		if (pp->p_pptr == td->td_proc)
1035 			ptc->flags |= PTCHLD_TRACED_BY_ME;
1036 	}
1037 	if ((pp->p_flag & P_WEXIT) != 0)
1038 		ptc->flags |= PTCHLD_EXITED;
1039 	PROC_UNLOCK(pp);
1040 	return (true);
1041 }
1042 
1043 static struct thread *
1044 ptrace_sel_coredump_thread(struct proc *p)
1045 {
1046 	struct thread *td2;
1047 
1048 	PROC_LOCK_ASSERT(p, MA_OWNED);
1049 	MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
1050 
1051 	FOREACH_THREAD_IN_PROC(p, td2) {
1052 		if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
1053 			return (td2);
1054 	}
1055 	return (NULL);
1056 }
1057 
1058 int
1059 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
1060 {
1061 	struct iovec iov;
1062 	struct uio uio;
1063 	struct proc *curp, *p, *pp;
1064 	struct thread *td2 = NULL, *td3;
1065 	struct ptrace_io_desc *piod = NULL;
1066 	struct ptrace_lwpinfo *pl;
1067 	struct ptrace_sc_ret *psr;
1068 	struct ptrace_sc_remote *pscr;
1069 	struct file *fp;
1070 	struct ptrace_coredump *pc;
1071 	struct thr_coredump_req *tcq;
1072 	struct thr_syscall_req *tsr;
1073 	struct ptrace_child *children, *ptc;
1074 	int error, num, num1, tmp;
1075 	lwpid_t tid = 0, *buf;
1076 #ifdef COMPAT_FREEBSD32
1077 	int wrap32 = 0, safe = 0;
1078 #endif
1079 	bool need_can_ptrace, proctree_locked, p2_req_set;
1080 
1081 	curp = td->td_proc;
1082 	proctree_locked = false;
1083 	p2_req_set = false;
1084 
1085 	/* Lock proctree before locking the process. */
1086 	switch (req) {
1087 	case PT_TRACE_ME:
1088 	case PT_ATTACH:
1089 	case PT_STEP:
1090 	case PT_CONTINUE:
1091 	case PT_TO_SCE:
1092 	case PT_TO_SCX:
1093 	case PT_SYSCALL:
1094 	case PT_FOLLOW_FORK:
1095 	case PT_LWP_EVENTS:
1096 	case PT_GET_EVENT_MASK:
1097 	case PT_SET_EVENT_MASK:
1098 	case PT_DETACH:
1099 	case PT_GET_SC_ARGS:
1100 	case PT_GET_CHILDREN:
1101 		sx_xlock(&proctree_lock);
1102 		proctree_locked = true;
1103 		break;
1104 	default:
1105 		break;
1106 	}
1107 
1108 	if (req == PT_TRACE_ME) {
1109 		p = td->td_proc;
1110 		PROC_LOCK(p);
1111 	} else {
1112 		if (pid <= PID_MAX) {
1113 			if ((p = pfind(pid)) == NULL) {
1114 				if (proctree_locked)
1115 					sx_xunlock(&proctree_lock);
1116 				return (ESRCH);
1117 			}
1118 		} else {
1119 			td2 = tdfind(pid, -1);
1120 			if (td2 == NULL) {
1121 				if (proctree_locked)
1122 					sx_xunlock(&proctree_lock);
1123 				return (ESRCH);
1124 			}
1125 			p = td2->td_proc;
1126 			tid = pid;
1127 			pid = p->p_pid;
1128 		}
1129 	}
1130 	AUDIT_ARG_PROCESS(p);
1131 
1132 	error = proc_can_ptrace1(td, p);
1133 	if (error != 0)
1134 		goto fail;
1135 
1136 	/*
1137 	 * System processes can't be debugged.
1138 	 */
1139 	if ((p->p_flag & P_SYSTEM) != 0) {
1140 		error = EINVAL;
1141 		goto fail;
1142 	}
1143 
1144 	if (tid == 0) {
1145 		if ((p->p_flag & P_STOPPED_TRACE) != 0)
1146 			td2 = p->p_xthread;
1147 		if (td2 == NULL)
1148 			td2 = FIRST_THREAD_IN_PROC(p);
1149 		tid = td2->td_tid;
1150 	}
1151 
1152 #ifdef COMPAT_FREEBSD32
1153 	/*
1154 	 * Test if we're a 32 bit client and what the target is.
1155 	 * Set the wrap controls accordingly.
1156 	 */
1157 	if (SV_CURPROC_FLAG(SV_ILP32)) {
1158 		if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
1159 			safe = 1;
1160 		wrap32 = 1;
1161 	}
1162 #endif
1163 	/*
1164 	 * Permissions check
1165 	 */
1166 	need_can_ptrace = true;
1167 	switch (req) {
1168 	case PT_TRACE_ME:
1169 		/*
1170 		 * Always legal, when there is a parent process which
1171 		 * could trace us.  Otherwise, reject.
1172 		 */
1173 		if ((p->p_flag & P_TRACED) != 0) {
1174 			error = EBUSY;
1175 			goto fail;
1176 		}
1177 		if (p->p_pptr == initproc) {
1178 			error = EPERM;
1179 			goto fail;
1180 		}
1181 		break;
1182 
1183 	case PT_ATTACH:
1184 		/* Self */
1185 		if (p == td->td_proc) {
1186 			error = EINVAL;
1187 			goto fail;
1188 		}
1189 
1190 		/* Already traced */
1191 		if (p->p_flag & P_TRACED) {
1192 			error = EBUSY;
1193 			goto fail;
1194 		}
1195 
1196 		/* Can't trace an ancestor if you're being traced. */
1197 		if (curp->p_flag & P_TRACED) {
1198 			for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
1199 				if (pp == p) {
1200 					error = EINVAL;
1201 					goto fail;
1202 				}
1203 			}
1204 		}
1205 
1206 		/* OK */
1207 		break;
1208 
1209 	default:
1210 		/*
1211 		 * Allow thread to clear single step for itself.
1212 		 * PT_GET_CHILDREN on itself does not need P_TRACED.
1213 		 */
1214 		if ((req == PT_CLEARSTEP && td->td_tid == tid) ||
1215 		    (req == PT_GET_CHILDREN && p == curp))
1216 			need_can_ptrace = false;
1217 
1218 		/*
1219 		 * Check for ptrace eligibility before waiting for
1220 		 * holds to drain.
1221 		 */
1222 		if (need_can_ptrace) {
1223 			error = proc_can_ptrace(td, p);
1224 			if (error != 0)
1225 				goto fail;
1226 		}
1227 
1228 		/*
1229 		 * Block parallel ptrace requests.  Most important, do
1230 		 * not allow other thread in debugger to continue the
1231 		 * debuggee until coredump finished.
1232 		 */
1233 		while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
1234 			if (proctree_locked)
1235 				sx_xunlock(&proctree_lock);
1236 			error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
1237 			    (proctree_locked ? PDROP : 0), "pptrace", 0);
1238 			if (proctree_locked) {
1239 				sx_xlock(&proctree_lock);
1240 				PROC_LOCK(p);
1241 			}
1242 			if (error == 0 && td2->td_proc != p)
1243 				error = ESRCH;
1244 			if (error == 0 && need_can_ptrace)
1245 				error = proc_can_ptrace(td, p);
1246 			if (error != 0)
1247 				goto fail;
1248 		}
1249 
1250 		/* Ok */
1251 		break;
1252 	}
1253 
1254 	/*
1255 	 * Keep this process around and request parallel ptrace()
1256 	 * request to wait until we finish this request.
1257 	 */
1258 	MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
1259 	p->p_flag2 |= P2_PTRACEREQ;
1260 	p2_req_set = true;
1261 	_PHOLD(p);
1262 
1263 	/*
1264 	 * Actually do the requests
1265 	 */
1266 
1267 	td->td_retval[0] = 0;
1268 
1269 	switch (req) {
1270 	case PT_TRACE_ME:
1271 		/* set my trace flag and "owner" so it can read/write me */
1272 		proc_set_traced(p, false);
1273 		if (p->p_flag & P_PPWAIT)
1274 			p->p_flag |= P_PPTRACE;
1275 		CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
1276 		break;
1277 
1278 	case PT_ATTACH:
1279 		/* security check done above */
1280 		/*
1281 		 * It would be nice if the tracing relationship was separate
1282 		 * from the parent relationship but that would require
1283 		 * another set of links in the proc struct or for "wait"
1284 		 * to scan the entire proc table.  To make life easier,
1285 		 * we just re-parent the process we're trying to trace.
1286 		 * The old parent is remembered so we can put things back
1287 		 * on a "detach".
1288 		 */
1289 		proc_set_traced(p, true);
1290 		proc_reparent(p, td->td_proc, false);
1291 		CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
1292 		    p->p_oppid);
1293 
1294 		sx_xunlock(&proctree_lock);
1295 		proctree_locked = false;
1296 		MPASS(p->p_xthread == NULL);
1297 		MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
1298 
1299 		/*
1300 		 * If already stopped due to a stop signal, clear the
1301 		 * existing stop before triggering a traced SIGSTOP.
1302 		 */
1303 		if ((p->p_flag & P_STOPPED_SIG) != 0) {
1304 			PROC_SLOCK(p);
1305 			p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
1306 			thread_unsuspend(p);
1307 			PROC_SUNLOCK(p);
1308 		}
1309 
1310 		kern_psignal(p, SIGSTOP);
1311 		break;
1312 
1313 	case PT_CLEARSTEP:
1314 		CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
1315 		    p->p_pid);
1316 		error = ptrace_clear_single_step(td2);
1317 		break;
1318 
1319 	case PT_SETSTEP:
1320 		CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
1321 		    p->p_pid);
1322 		error = ptrace_single_step(td2);
1323 		break;
1324 
1325 	case PT_SUSPEND:
1326 		CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1327 		    p->p_pid);
1328 		td2->td_dbgflags |= TDB_SUSPEND;
1329 		ast_sched(td2, TDA_SUSPEND);
1330 		break;
1331 
1332 	case PT_RESUME:
1333 		CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1334 		    p->p_pid);
1335 		td2->td_dbgflags &= ~TDB_SUSPEND;
1336 		break;
1337 
1338 	case PT_FOLLOW_FORK:
1339 		CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1340 		    p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1341 		    data ? "enabled" : "disabled");
1342 		if (data)
1343 			p->p_ptevents |= PTRACE_FORK;
1344 		else
1345 			p->p_ptevents &= ~PTRACE_FORK;
1346 		break;
1347 
1348 	case PT_LWP_EVENTS:
1349 		CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1350 		    p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1351 		    data ? "enabled" : "disabled");
1352 		if (data)
1353 			p->p_ptevents |= PTRACE_LWP;
1354 		else
1355 			p->p_ptevents &= ~PTRACE_LWP;
1356 		break;
1357 
1358 	case PT_GET_EVENT_MASK:
1359 		if (data != sizeof(p->p_ptevents)) {
1360 			error = EINVAL;
1361 			break;
1362 		}
1363 		CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1364 		    p->p_ptevents);
1365 		*(int *)addr = p->p_ptevents;
1366 		break;
1367 
1368 	case PT_SET_EVENT_MASK:
1369 		if (data != sizeof(p->p_ptevents)) {
1370 			error = EINVAL;
1371 			break;
1372 		}
1373 		tmp = *(int *)addr;
1374 		if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1375 		    PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1376 			error = EINVAL;
1377 			break;
1378 		}
1379 		CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1380 		    p->p_pid, p->p_ptevents, tmp);
1381 		p->p_ptevents = tmp;
1382 		break;
1383 
1384 	case PT_GET_SC_ARGS:
1385 	case PTLINUX_GET_SC_ARGS:
1386 		CTR2(KTR_PTRACE, "%s: pid %d", req == PT_GET_SC_ARGS ?
1387 		    "PT_GET_SC_ARGS" : "PT_LINUX_GET_SC_ARGS", p->p_pid);
1388 		if (((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0 &&
1389 		     td2->td_sa.code == 0)
1390 #ifdef COMPAT_FREEBSD32
1391 		    || (wrap32 && !safe)
1392 #endif
1393 		    ) {
1394 			error = EINVAL;
1395 			break;
1396 		}
1397 		if (req == PT_GET_SC_ARGS) {
1398 			bzero(addr, sizeof(td2->td_sa.args));
1399 			bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg *
1400 			    sizeof(syscallarg_t));
1401 		} else {
1402 			/*
1403 			 * Emulate a Linux bug which which strace(1) depends on:
1404 			 * at initialization it tests whether ptrace works by
1405 			 * calling close(2), or some other single-argument
1406 			 * syscall, _with six arguments_, and then verifies
1407 			 * whether it can fetch them all using this API;
1408 			 * otherwise it bails out.
1409 			 */
1410 			bcopy(td2->td_sa.args, addr, 6 * sizeof(syscallarg_t));
1411 		}
1412 		break;
1413 
1414 	case PT_GET_SC_RET:
1415 		if ((td2->td_dbgflags & TDB_SCX) == 0
1416 #ifdef COMPAT_FREEBSD32
1417 		    || (wrap32 && !safe)
1418 #endif
1419 		    ) {
1420 			error = EINVAL;
1421 			break;
1422 		}
1423 		psr = addr;
1424 		bzero(psr, sizeof(*psr));
1425 		psr->sr_error = td2->td_errno;
1426 		if (psr->sr_error == 0) {
1427 			psr->sr_retval[0] = td2->td_retval[0];
1428 			psr->sr_retval[1] = td2->td_retval[1];
1429 		}
1430 		CTR4(KTR_PTRACE,
1431 		    "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1432 		    p->p_pid, psr->sr_error, psr->sr_retval[0],
1433 		    psr->sr_retval[1]);
1434 		break;
1435 
1436 	case PT_SET_SC_RET:
1437 		if ((td2->td_dbgflags & TDB_SCE) == 0
1438 #ifdef COMPAT_FREEBSD32
1439 		    || (wrap32 && !safe)
1440 #endif
1441 		    ) {
1442 			error = EINVAL;
1443 			break;
1444 		}
1445 		psr = addr;
1446 		td2->td_errno = psr->sr_error;
1447 		if (td2->td_errno == 0) {
1448 			td2->td_retval[0] = psr->sr_retval[0];
1449 			td2->td_retval[1] = psr->sr_retval[1];
1450 		}
1451 		td2->td_dbgflags |= TDB_SET_SC_RET;
1452 		break;
1453 
1454 	case PT_STEP:
1455 	case PT_CONTINUE:
1456 	case PT_TO_SCE:
1457 	case PT_TO_SCX:
1458 	case PT_SYSCALL:
1459 	case PT_DETACH:
1460 		/* Zero means do not send any signal */
1461 		if (data < 0 || data > _SIG_MAXSIG) {
1462 			error = EINVAL;
1463 			break;
1464 		}
1465 
1466 		switch (req) {
1467 		case PT_STEP:
1468 			CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1469 			    td2->td_tid, p->p_pid, data);
1470 			error = ptrace_single_step(td2);
1471 			if (error != 0)
1472 				goto out;
1473 			break;
1474 		case PT_CONTINUE:
1475 		case PT_TO_SCE:
1476 		case PT_TO_SCX:
1477 		case PT_SYSCALL:
1478 			if (addr != (void *)1) {
1479 				error = ptrace_set_pc(td2,
1480 				    (u_long)(uintfptr_t)addr);
1481 				if (error != 0)
1482 					goto out;
1483 				td2->td_dbgflags |= TDB_USERWR;
1484 			}
1485 			switch (req) {
1486 			case PT_TO_SCE:
1487 				p->p_ptevents |= PTRACE_SCE;
1488 				CTR4(KTR_PTRACE,
1489 		    "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1490 				    p->p_pid, p->p_ptevents,
1491 				    (u_long)(uintfptr_t)addr, data);
1492 				break;
1493 			case PT_TO_SCX:
1494 				p->p_ptevents |= PTRACE_SCX;
1495 				CTR4(KTR_PTRACE,
1496 		    "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1497 				    p->p_pid, p->p_ptevents,
1498 				    (u_long)(uintfptr_t)addr, data);
1499 				break;
1500 			case PT_SYSCALL:
1501 				p->p_ptevents |= PTRACE_SYSCALL;
1502 				CTR4(KTR_PTRACE,
1503 		    "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1504 				    p->p_pid, p->p_ptevents,
1505 				    (u_long)(uintfptr_t)addr, data);
1506 				break;
1507 			case PT_CONTINUE:
1508 				CTR3(KTR_PTRACE,
1509 				    "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1510 				    p->p_pid, (u_long)(uintfptr_t)addr, data);
1511 				break;
1512 			}
1513 			break;
1514 		case PT_DETACH:
1515 			/*
1516 			 * Clear P_TRACED before reparenting
1517 			 * a detached process back to its original
1518 			 * parent.  Otherwise the debugee will be set
1519 			 * as an orphan of the debugger.
1520 			 */
1521 			p->p_flag &= ~(P_TRACED | P_WAITED);
1522 
1523 			/*
1524 			 * Reset the process parent.
1525 			 */
1526 			if (p->p_oppid != p->p_pptr->p_pid) {
1527 				PROC_LOCK(p->p_pptr);
1528 				sigqueue_take(p->p_ksi);
1529 				PROC_UNLOCK(p->p_pptr);
1530 
1531 				pp = proc_realparent(p);
1532 				proc_reparent(p, pp, false);
1533 				if (pp == initproc)
1534 					p->p_sigparent = SIGCHLD;
1535 				CTR3(KTR_PTRACE,
1536 			    "PT_DETACH: pid %d reparented to pid %d, sig %d",
1537 				    p->p_pid, pp->p_pid, data);
1538 			} else {
1539 				CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1540 				    p->p_pid, data);
1541 			}
1542 
1543 			p->p_ptevents = 0;
1544 			FOREACH_THREAD_IN_PROC(p, td3) {
1545 				if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1546 					sigqueue_delete(&td3->td_sigqueue,
1547 					    SIGSTOP);
1548 				}
1549 				td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1550 				    TDB_SUSPEND | TDB_BORN);
1551 			}
1552 
1553 			if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1554 				sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1555 				p->p_flag2 &= ~P2_PTRACE_FSTP;
1556 			}
1557 
1558 			/*
1559 			 * Send SIGCHLD and wakeup the parent as needed.  It
1560 			 * may be the case that they had stopped the child
1561 			 * before it got ptraced, and now they're in the middle
1562 			 * of a wait(2) for it to continue.
1563 			 */
1564 			PROC_LOCK(p->p_pptr);
1565 			childproc_continued(p);
1566 			PROC_UNLOCK(p->p_pptr);
1567 			break;
1568 		}
1569 
1570 		sx_xunlock(&proctree_lock);
1571 		proctree_locked = false;
1572 
1573 	sendsig:
1574 		MPASS(!proctree_locked);
1575 
1576 		/*
1577 		 * Clear the pending event for the thread that just
1578 		 * reported its event (p_xthread), if any.  This may
1579 		 * not be the thread passed to PT_CONTINUE, PT_STEP,
1580 		 * etc. if the debugger is resuming a different
1581 		 * thread.  There might be no reporting thread if
1582 		 * the process was just attached.
1583 		 *
1584 		 * Deliver any pending signal via the reporting thread.
1585 		 */
1586 		if (p->p_xthread != NULL) {
1587 			p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1588 			p->p_xthread->td_xsig = data;
1589 			p->p_xthread = NULL;
1590 		}
1591 		p->p_xsig = data;
1592 
1593 		/*
1594 		 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1595 		 * always works immediately, even if another thread is
1596 		 * unsuspended first and attempts to handle a
1597 		 * different signal or if the POSIX.1b style signal
1598 		 * queue cannot accommodate any new signals.
1599 		 */
1600 		if (data == SIGKILL)
1601 			proc_wkilled(p);
1602 
1603 		/*
1604 		 * If the PT_CONTINUE-like operation is attempted on
1605 		 * the thread on sleepq, this is possible only after
1606 		 * the transparent PT_ATTACH.  In this case, if the
1607 		 * caller modified the thread state, e.g. by writing
1608 		 * register file or specifying the pc, make the thread
1609 		 * xstopped by waking it up.
1610 		 */
1611 		if ((td2->td_dbgflags & TDB_USERWR) != 0 &&
1612 		    pt_attach_transparent) {
1613 			thread_lock(td2);
1614 			if (TD_ON_SLEEPQ(td2) &&
1615 			    (td2->td_flags & TDF_SINTR) != 0) {
1616 				td2->td_dbgflags &= ~TDB_USERWR;
1617 				sleepq_abort(td2, EINTR);
1618 			} else {
1619 				thread_unlock(td2);
1620 			}
1621 		}
1622 
1623 		/*
1624 		 * Unsuspend all threads.  To leave a thread
1625 		 * suspended, use PT_SUSPEND to suspend it before
1626 		 * continuing the process.
1627 		 */
1628 		ptrace_unsuspend(p);
1629 		break;
1630 
1631 	case PT_WRITE_I:
1632 	case PT_WRITE_D:
1633 		td2->td_dbgflags |= TDB_USERWR;
1634 		PROC_UNLOCK(p);
1635 		error = 0;
1636 		if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1637 		    sizeof(int)) != sizeof(int))
1638 			error = ENOMEM;
1639 		else
1640 			CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1641 			    p->p_pid, addr, data);
1642 		PROC_LOCK(p);
1643 		break;
1644 
1645 	case PT_READ_I:
1646 	case PT_READ_D:
1647 		PROC_UNLOCK(p);
1648 		error = tmp = 0;
1649 		if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1650 		    sizeof(int)) != sizeof(int))
1651 			error = ENOMEM;
1652 		else
1653 			CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1654 			    p->p_pid, addr, tmp);
1655 		td->td_retval[0] = tmp;
1656 		PROC_LOCK(p);
1657 		break;
1658 
1659 	case PT_IO:
1660 		piod = addr;
1661 		if (piod->piod_len > SSIZE_MAX) {
1662 			error = EINVAL;
1663 			goto out;
1664 		}
1665 		iov.iov_base = piod->piod_addr;
1666 		iov.iov_len = piod->piod_len;
1667 		uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1668 		uio.uio_resid = piod->piod_len;
1669 		uio.uio_iov = &iov;
1670 		uio.uio_iovcnt = 1;
1671 		uio.uio_segflg = UIO_USERSPACE;
1672 		uio.uio_td = td;
1673 		switch (piod->piod_op) {
1674 		case PIOD_READ_D:
1675 		case PIOD_READ_I:
1676 			CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1677 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1678 			uio.uio_rw = UIO_READ;
1679 			break;
1680 		case PIOD_WRITE_D:
1681 		case PIOD_WRITE_I:
1682 			CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1683 			    p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1684 			td2->td_dbgflags |= TDB_USERWR;
1685 			uio.uio_rw = UIO_WRITE;
1686 			break;
1687 		default:
1688 			error = EINVAL;
1689 			goto out;
1690 		}
1691 		PROC_UNLOCK(p);
1692 		error = proc_rwmem(p, &uio, 0);
1693 		piod->piod_len -= uio.uio_resid;
1694 		PROC_LOCK(p);
1695 		break;
1696 
1697 	case PT_KILL:
1698 		CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1699 		data = SIGKILL;
1700 		goto sendsig;	/* in PT_CONTINUE above */
1701 
1702 	case PT_SETREGS:
1703 		CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1704 		    p->p_pid);
1705 		td2->td_dbgflags |= TDB_USERWR;
1706 		error = PROC_WRITE(regs, td2, addr);
1707 		break;
1708 
1709 	case PT_GETREGS:
1710 		CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1711 		    p->p_pid);
1712 		error = PROC_READ(regs, td2, addr);
1713 		break;
1714 
1715 	case PT_SETFPREGS:
1716 		CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1717 		    p->p_pid);
1718 		td2->td_dbgflags |= TDB_USERWR;
1719 		error = PROC_WRITE(fpregs, td2, addr);
1720 		break;
1721 
1722 	case PT_GETFPREGS:
1723 		CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1724 		    p->p_pid);
1725 		error = PROC_READ(fpregs, td2, addr);
1726 		break;
1727 
1728 	case PT_SETDBREGS:
1729 		CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1730 		    p->p_pid);
1731 		td2->td_dbgflags |= TDB_USERWR;
1732 		error = PROC_WRITE(dbregs, td2, addr);
1733 		break;
1734 
1735 	case PT_GETDBREGS:
1736 		CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1737 		    p->p_pid);
1738 		error = PROC_READ(dbregs, td2, addr);
1739 		break;
1740 
1741 	case PT_SETREGSET:
1742 		CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid,
1743 		    p->p_pid);
1744 		error = proc_write_regset(td2, data, addr);
1745 		break;
1746 
1747 	case PT_GETREGSET:
1748 		CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid,
1749 		    p->p_pid);
1750 		error = proc_read_regset(td2, data, addr);
1751 		break;
1752 
1753 	case PT_LWPINFO:
1754 		if (data <= 0 || data > sizeof(*pl)) {
1755 			error = EINVAL;
1756 			break;
1757 		}
1758 		pl = addr;
1759 		bzero(pl, sizeof(*pl));
1760 		pl->pl_lwpid = td2->td_tid;
1761 		pl->pl_event = PL_EVENT_NONE;
1762 		pl->pl_flags = 0;
1763 		if (td2->td_dbgflags & TDB_XSIG) {
1764 			pl->pl_event = PL_EVENT_SIGNAL;
1765 			if (td2->td_si.si_signo != 0 &&
1766 			    data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1767 			    + sizeof(pl->pl_siginfo)){
1768 				pl->pl_flags |= PL_FLAG_SI;
1769 				pl->pl_siginfo = td2->td_si;
1770 			}
1771 		}
1772 		if (td2->td_dbgflags & TDB_SCE)
1773 			pl->pl_flags |= PL_FLAG_SCE;
1774 		else if (td2->td_dbgflags & TDB_SCX)
1775 			pl->pl_flags |= PL_FLAG_SCX;
1776 		if (td2->td_dbgflags & TDB_EXEC)
1777 			pl->pl_flags |= PL_FLAG_EXEC;
1778 		if (td2->td_dbgflags & TDB_FORK) {
1779 			pl->pl_flags |= PL_FLAG_FORKED;
1780 			pl->pl_child_pid = td2->td_dbg_forked;
1781 			if (td2->td_dbgflags & TDB_VFORK)
1782 				pl->pl_flags |= PL_FLAG_VFORKED;
1783 		} else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1784 		    TDB_VFORK)
1785 			pl->pl_flags |= PL_FLAG_VFORK_DONE;
1786 		if (td2->td_dbgflags & TDB_CHILD)
1787 			pl->pl_flags |= PL_FLAG_CHILD;
1788 		if (td2->td_dbgflags & TDB_BORN)
1789 			pl->pl_flags |= PL_FLAG_BORN;
1790 		if (td2->td_dbgflags & TDB_EXIT)
1791 			pl->pl_flags |= PL_FLAG_EXITED;
1792 		pl->pl_sigmask = td2->td_sigmask;
1793 		pl->pl_siglist = td2->td_siglist;
1794 		strcpy(pl->pl_tdname, td2->td_name);
1795 		if (td2->td_sa.code != 0) {
1796 			pl->pl_syscall_code = td2->td_sa.code;
1797 			pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1798 		}
1799 		CTR6(KTR_PTRACE,
1800     "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1801 		    td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1802 		    pl->pl_child_pid, pl->pl_syscall_code);
1803 		break;
1804 
1805 	case PT_GETNUMLWPS:
1806 		CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1807 		    p->p_numthreads);
1808 		td->td_retval[0] = p->p_numthreads;
1809 		break;
1810 
1811 	case PT_GETLWPLIST:
1812 		CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1813 		    p->p_pid, data, p->p_numthreads);
1814 		if (data <= 0) {
1815 			error = EINVAL;
1816 			break;
1817 		}
1818 		num = imin(p->p_numthreads, data);
1819 		PROC_UNLOCK(p);
1820 		buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1821 		tmp = 0;
1822 		PROC_LOCK(p);
1823 		FOREACH_THREAD_IN_PROC(p, td2) {
1824 			if (tmp >= num)
1825 				break;
1826 			buf[tmp++] = td2->td_tid;
1827 		}
1828 		PROC_UNLOCK(p);
1829 		error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1830 		free(buf, M_TEMP);
1831 		if (!error)
1832 			td->td_retval[0] = tmp;
1833 		PROC_LOCK(p);
1834 		break;
1835 
1836 	case PT_VM_TIMESTAMP:
1837 		CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1838 		    p->p_pid, p->p_vmspace->vm_map.timestamp);
1839 		td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1840 		break;
1841 
1842 	case PT_VM_ENTRY:
1843 		PROC_UNLOCK(p);
1844 		error = ptrace_vm_entry(td, p, addr);
1845 		PROC_LOCK(p);
1846 		break;
1847 
1848 	case PT_COREDUMP:
1849 		pc = addr;
1850 		CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1851 		    p->p_pid, pc->pc_fd);
1852 
1853 		if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1854 			error = EINVAL;
1855 			break;
1856 		}
1857 		PROC_UNLOCK(p);
1858 
1859 		tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1860 		fp = NULL;
1861 		error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1862 		if (error != 0)
1863 			goto coredump_cleanup_nofp;
1864 		if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1865 			error = EPIPE;
1866 			goto coredump_cleanup;
1867 		}
1868 
1869 		PROC_LOCK(p);
1870 		error = proc_can_ptrace(td, p);
1871 		if (error != 0)
1872 			goto coredump_cleanup_locked;
1873 
1874 		td2 = ptrace_sel_coredump_thread(p);
1875 		if (td2 == NULL) {
1876 			error = EBUSY;
1877 			goto coredump_cleanup_locked;
1878 		}
1879 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1880 		    TDB_SCREMOTEREQ)) == 0,
1881 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1882 
1883 		tcq->tc_vp = fp->f_vnode;
1884 		tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1885 		tcq->tc_flags = SVC_PT_COREDUMP;
1886 		if ((pc->pc_flags & PC_COMPRESS) == 0)
1887 			tcq->tc_flags |= SVC_NOCOMPRESS;
1888 		if ((pc->pc_flags & PC_ALL) != 0)
1889 			tcq->tc_flags |= SVC_ALL;
1890 		td2->td_remotereq = tcq;
1891 		td2->td_dbgflags |= TDB_COREDUMPREQ;
1892 		thread_run_flash(td2);
1893 		while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0)
1894 			msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1895 		error = tcq->tc_error;
1896 coredump_cleanup_locked:
1897 		PROC_UNLOCK(p);
1898 coredump_cleanup:
1899 		fdrop(fp, td);
1900 coredump_cleanup_nofp:
1901 		free(tcq, M_TEMP);
1902 		PROC_LOCK(p);
1903 		break;
1904 
1905 	case PT_SC_REMOTE:
1906 		pscr = addr;
1907 		CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d",
1908 		    p->p_pid, pscr->pscr_syscall);
1909 		if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) {
1910 			error = EBUSY;
1911 			break;
1912 		}
1913 		PROC_UNLOCK(p);
1914 		MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args));
1915 
1916 		tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP,
1917 		    M_WAITOK | M_ZERO);
1918 
1919 		tsr->ts_sa.code = pscr->pscr_syscall;
1920 		tsr->ts_nargs = pscr->pscr_nargs;
1921 		memcpy(&tsr->ts_sa.args, pscr->pscr_args,
1922 		    sizeof(syscallarg_t) * tsr->ts_nargs);
1923 
1924 		PROC_LOCK(p);
1925 		error = proc_can_ptrace(td, p);
1926 		if (error != 0) {
1927 			free(tsr, M_TEMP);
1928 			break;
1929 		}
1930 		if (td2->td_proc != p) {
1931 			free(tsr, M_TEMP);
1932 			error = ESRCH;
1933 			break;
1934 		}
1935 		KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1936 		    TDB_SCREMOTEREQ)) == 0,
1937 		    ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1938 
1939 		td2->td_remotereq = tsr;
1940 		td2->td_dbgflags |= TDB_SCREMOTEREQ;
1941 		thread_run_flash(td2);
1942 		while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0)
1943 			msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0);
1944 		error = 0;
1945 		memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret));
1946 		free(tsr, M_TEMP);
1947 		break;
1948 
1949 	case PT_GET_CHILDREN:
1950 		PROC_UNLOCK(p);
1951 get_children_repeat:
1952 		/*
1953 		 * If addr != NULL, we should ignore p_cansee() to
1954 		 * allocate enough space for the children array,
1955 		 * because the process is allowed to change visibility
1956 		 * between loops.  But do not count children which
1957 		 * we cannot see when only returning the count, to
1958 		 * avoid a leak of information.
1959 		 */
1960 		num = ptrace_count_children(td, p, addr != NULL);
1961 
1962 		if (addr == NULL) {
1963 			td->td_retval[0] = num;
1964 			PROC_LOCK(p);
1965 			break;
1966 		}
1967 		if (data < num * sizeof(struct ptrace_child)) {
1968 			error = ENOMEM;
1969 			PROC_LOCK(p);
1970 			break;
1971 		}
1972 		sx_xunlock(&proctree_lock);
1973 		children = mallocarray(num, sizeof(struct ptrace_child),
1974 		    M_TEMP, M_WAITOK | M_ZERO);
1975 		sx_xlock(&proctree_lock);
1976 		num1 = ptrace_count_children(td, p, true);
1977 		if (num1 > num) {
1978 			free(children, M_TEMP);
1979 			goto get_children_repeat;
1980 		}
1981 		num = num1;
1982 		num1 = 0;
1983 		LIST_FOREACH(pp, &p->p_children, p_sibling) {
1984 			MPASS(num1 < num);
1985 			ptc = &children[num1];
1986 			if (ptrace_report_child(td, p, pp, ptc))
1987 				num1++;
1988 		}
1989 		LIST_FOREACH(pp, &p->p_orphans, p_orphan) {
1990 			MPASS(num1 < num);
1991 			ptc = &children[num1];
1992 			if (ptrace_report_child(td, p, pp, ptc)) {
1993 				num1++;
1994 				ptc->flags |= PTCHLD_ORPHAN;
1995 			}
1996 		}
1997 		*(struct ptrace_child **)addr = children;
1998 		td->td_retval[0] = num1;
1999 		PROC_LOCK(p);
2000 		break;
2001 
2002 	default:
2003 #ifdef __HAVE_PTRACE_MACHDEP
2004 		if (req >= PT_FIRSTMACH) {
2005 			PROC_UNLOCK(p);
2006 			error = cpu_ptrace(td2, req, addr, data);
2007 			PROC_LOCK(p);
2008 		} else
2009 #endif
2010 			/* Unknown request. */
2011 			error = EINVAL;
2012 		break;
2013 	}
2014 out:
2015 	/* Drop our hold on this process now that the request has completed. */
2016 	_PRELE(p);
2017 fail:
2018 	if (p2_req_set) {
2019 		if ((p->p_flag2 & P2_PTRACEREQ) != 0)
2020 			wakeup(&p->p_flag2);
2021 		p->p_flag2 &= ~P2_PTRACEREQ;
2022 	}
2023 	PROC_UNLOCK(p);
2024 	if (proctree_locked)
2025 		sx_xunlock(&proctree_lock);
2026 	return (error);
2027 }
2028 #undef PROC_READ
2029 #undef PROC_WRITE
2030