2 * SPDX-License-Identifier: BSD-4-Clause
4 * Copyright (c) 1994, Sean Eric Fagan
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/limits.h>
39 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysent.h>
43 #include <sys/sysproto.h>
46 #include <sys/vnode.h>
47 #include <sys/ptrace.h>
48 #include <sys/rwlock.h>
50 #include <sys/malloc.h>
51 #include <sys/signalvar.h>
52 #include <sys/caprights.h>
53 #include <sys/filedesc.h>
55 #include <security/audit/audit.h>
59 #include <vm/vm_extern.h>
60 #include <vm/vm_map.h>
61 #include <vm/vm_kern.h>
62 #include <vm/vm_object.h>
63 #include <vm/vm_page.h>
64 #include <vm/vm_param.h>
66 #ifdef COMPAT_FREEBSD32
67 #include <sys/procfs.h>
70 /* Assert it's safe to unlock a process, e.g. to allocate working memory */
71 #define PROC_ASSERT_TRACEREQ(p) MPASS(((p)->p_flag2 & P2_PTRACEREQ) != 0)
74 * Functions implemented using PROC_ACTION():
76 * proc_read_regs(proc, regs)
77 * Get the current user-visible register set from the process
78 * and copy it into the regs structure (<machine/reg.h>).
79 * The process is stopped at the time read_regs is called.
81 * proc_write_regs(proc, regs)
82 * Update the current register set from the passed in regs
83 * structure. Take care to avoid clobbering special CPU
84 * registers or privileged bits in the PSL.
85 * Depending on the architecture this may have fix-up work to do,
86 * especially if the IAR or PCW are modified.
87 * The process is stopped at the time write_regs is called.
89 * proc_read_fpregs, proc_write_fpregs
90 * deal with the floating point register set, otherwise as above.
92 * proc_read_dbregs, proc_write_dbregs
93 * deal with the processor debug register set, otherwise as above.
96 * Arrange for the process to trap after executing a single instruction.
99 #define PROC_ACTION(action) do { \
102 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
103 if ((td->td_proc->p_flag & P_INMEM) == 0) \
111 proc_read_regs(struct thread *td, struct reg *regs)
114 PROC_ACTION(fill_regs(td, regs));
118 proc_write_regs(struct thread *td, struct reg *regs)
121 PROC_ACTION(set_regs(td, regs));
125 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
128 PROC_ACTION(fill_dbregs(td, dbregs));
132 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
135 PROC_ACTION(set_dbregs(td, dbregs));
139 * Ptrace doesn't support fpregs at all, and there are no security holes
140 * or translations for fpregs, so we can just copy them.
143 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
146 PROC_ACTION(fill_fpregs(td, fpregs));
150 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
153 PROC_ACTION(set_fpregs(td, fpregs));
156 static struct regset *
157 proc_find_regset(struct thread *td, int note)
159 struct regset **regsetp, **regset_end, *regset;
160 struct sysentvec *sv;
162 sv = td->td_proc->p_sysent;
163 regsetp = sv->sv_regset_begin;
166 regset_end = sv->sv_regset_end;
167 MPASS(regset_end != NULL);
168 for (; regsetp < regset_end; regsetp++) {
170 if (regset->note != note)
180 proc_read_regset(struct thread *td, int note, struct iovec *iov)
182 struct regset *regset;
188 regset = proc_find_regset(td, note);
192 if (regset->get == NULL)
197 * The regset is dynamically sized, e.g. the size could change
198 * depending on the hardware, or may have a per-thread size.
201 if (!regset->get(regset, td, NULL, &size))
205 if (iov->iov_base == NULL) {
207 if (iov->iov_len == 0)
213 /* The length is wrong, return an error */
214 if (iov->iov_len != size)
220 /* Drop the proc lock while allocating the temp buffer */
221 PROC_ASSERT_TRACEREQ(p);
223 buf = malloc(size, M_TEMP, M_WAITOK);
226 if (!regset->get(regset, td, buf, &size)) {
229 KASSERT(size == regset->size || regset->size == 0,
230 ("%s: Getter function changed the size", __func__));
234 error = copyout(buf, iov->iov_base, size);
244 proc_write_regset(struct thread *td, int note, struct iovec *iov)
246 struct regset *regset;
252 regset = proc_find_regset(td, note);
258 * The regset is dynamically sized, e.g. the size could change
259 * depending on the hardware, or may have a per-thread size.
262 if (!regset->get(regset, td, NULL, &size))
266 /* The length is wrong, return an error */
267 if (iov->iov_len != size)
270 if (regset->set == NULL)
275 /* Drop the proc lock while allocating the temp buffer */
276 PROC_ASSERT_TRACEREQ(p);
278 buf = malloc(size, M_TEMP, M_WAITOK);
279 error = copyin(iov->iov_base, buf, size);
283 if (!regset->set(regset, td, buf, size)) {
293 #ifdef COMPAT_FREEBSD32
294 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
296 proc_read_regs32(struct thread *td, struct reg32 *regs32)
299 PROC_ACTION(fill_regs32(td, regs32));
303 proc_write_regs32(struct thread *td, struct reg32 *regs32)
306 PROC_ACTION(set_regs32(td, regs32));
310 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
313 PROC_ACTION(fill_dbregs32(td, dbregs32));
317 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
320 PROC_ACTION(set_dbregs32(td, dbregs32));
324 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
327 PROC_ACTION(fill_fpregs32(td, fpregs32));
331 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
334 PROC_ACTION(set_fpregs32(td, fpregs32));
339 proc_sstep(struct thread *td)
342 PROC_ACTION(ptrace_single_step(td));
346 proc_rwmem(struct proc *p, struct uio *uio)
349 vm_offset_t pageno; /* page number */
351 int error, fault_flags, page_offset, writing;
354 * Make sure that the process' vmspace remains live.
358 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
363 map = &p->p_vmspace->vm_map;
366 * If we are writing, then we request vm_fault() to create a private
367 * copy of each page. Since these copies will not be writeable by the
368 * process, we must explicity request that they be dirtied.
370 writing = uio->uio_rw == UIO_WRITE;
371 reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
372 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
375 * Only map in one page at a time. We don't have to, but it
376 * makes things easier. This way is trivial - right?
383 uva = (vm_offset_t)uio->uio_offset;
386 * Get the page number of this segment.
388 pageno = trunc_page(uva);
389 page_offset = uva - pageno;
392 * How many bytes to copy
394 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
397 * Fault and hold the page on behalf of the process.
399 error = vm_fault(map, pageno, reqprot, fault_flags, &m);
400 if (error != KERN_SUCCESS) {
401 if (error == KERN_RESOURCE_SHORTAGE)
409 * Now do the i/o move.
411 error = uiomove_fromphys(&m, page_offset, len, uio);
413 /* Make the I-cache coherent for breakpoints. */
414 if (writing && error == 0) {
415 vm_map_lock_read(map);
416 if (vm_map_check_protection(map, pageno, pageno +
417 PAGE_SIZE, VM_PROT_EXECUTE))
418 vm_sync_icache(map, uva, len);
419 vm_map_unlock_read(map);
425 vm_page_unwire(m, PQ_ACTIVE);
427 } while (error == 0 && uio->uio_resid > 0);
433 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
434 size_t len, enum uio_rw rw)
440 MPASS(len < SSIZE_MAX);
443 iov.iov_base = (caddr_t)buf;
448 uio.uio_resid = slen;
449 uio.uio_segflg = UIO_SYSSPACE;
453 if (uio.uio_resid == slen)
455 return (slen - uio.uio_resid);
459 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
463 return (proc_iop(td, p, va, buf, len, UIO_READ));
467 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
471 return (proc_iop(td, p, va, buf, len, UIO_WRITE));
475 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
479 vm_map_entry_t entry;
480 vm_object_t obj, tobj, lobj;
483 char *freepath, *fullpath;
490 vm = vmspace_acquire_ref(p);
492 vm_map_lock_read(map);
495 KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
496 ("Submap in map header"));
498 VM_MAP_ENTRY_FOREACH(entry, map) {
499 if (index >= pve->pve_entry &&
500 (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
504 if (index < pve->pve_entry) {
508 if (entry == &map->header) {
513 /* We got an entry. */
514 pve->pve_entry = index + 1;
515 pve->pve_timestamp = map->timestamp;
516 pve->pve_start = entry->start;
517 pve->pve_end = entry->end - 1;
518 pve->pve_offset = entry->offset;
519 pve->pve_prot = entry->protection;
521 /* Backing object's path needed? */
522 if (pve->pve_pathlen == 0)
525 pathlen = pve->pve_pathlen;
526 pve->pve_pathlen = 0;
528 obj = entry->object.vm_object;
530 VM_OBJECT_RLOCK(obj);
533 vm_map_unlock_read(map);
535 pve->pve_fsid = VNOVAL;
536 pve->pve_fileid = VNOVAL;
538 if (error == 0 && obj != NULL) {
540 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
542 VM_OBJECT_RLOCK(tobj);
544 VM_OBJECT_RUNLOCK(lobj);
546 pve->pve_offset += tobj->backing_object_offset;
548 vp = vm_object_vnode(lobj);
552 VM_OBJECT_RUNLOCK(lobj);
553 VM_OBJECT_RUNLOCK(obj);
558 vn_fullpath(vp, &fullpath, &freepath);
559 vn_lock(vp, LK_SHARED | LK_RETRY);
560 if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
561 pve->pve_fileid = vattr.va_fileid;
562 pve->pve_fsid = vattr.va_fsid;
566 if (fullpath != NULL) {
567 pve->pve_pathlen = strlen(fullpath) + 1;
568 if (pve->pve_pathlen <= pathlen) {
569 error = copyout(fullpath, pve->pve_path,
572 error = ENAMETOOLONG;
574 if (freepath != NULL)
575 free(freepath, M_TEMP);
580 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
581 p->p_pid, pve->pve_entry, pve->pve_start);
587 * Process debugging system call.
589 #ifndef _SYS_SYSPROTO_H_
599 sys_ptrace(struct thread *td, struct ptrace_args *uap)
602 * XXX this obfuscation is to reduce stack usage, but the register
603 * structs may be too large to put on the stack anyway.
606 struct ptrace_io_desc piod;
607 struct ptrace_lwpinfo pl;
608 struct ptrace_vm_entry pve;
609 struct ptrace_coredump pc;
610 struct ptrace_sc_remote sr;
615 syscallarg_t args[nitems(td->td_sa.args)];
616 struct ptrace_sc_ret psr;
619 syscallarg_t pscr_args[nitems(td->td_sa.args)];
627 AUDIT_ARG_PID(uap->pid);
628 AUDIT_ARG_CMD(uap->req);
629 AUDIT_ARG_VALUE(uap->data);
632 case PT_GET_EVENT_MASK:
638 bzero(&r.reg, sizeof(r.reg));
641 bzero(&r.fpreg, sizeof(r.fpreg));
644 bzero(&r.dbreg, sizeof(r.dbreg));
648 error = copyin(uap->addr, &r.vec, sizeof(r.vec));
651 error = copyin(uap->addr, &r.reg, sizeof(r.reg));
654 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
657 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
659 case PT_SET_EVENT_MASK:
660 if (uap->data != sizeof(r.ptevents))
663 error = copyin(uap->addr, &r.ptevents, uap->data);
666 error = copyin(uap->addr, &r.piod, sizeof(r.piod));
669 error = copyin(uap->addr, &r.pve, sizeof(r.pve));
672 if (uap->data != sizeof(r.pc))
675 error = copyin(uap->addr, &r.pc, uap->data);
678 if (uap->data != sizeof(r.sr)) {
682 error = copyin(uap->addr, &r.sr, uap->data);
685 if (r.sr.pscr_nargs > nitems(td->td_sa.args)) {
689 error = copyin(r.sr.pscr_args, pscr_args,
690 sizeof(u_long) * r.sr.pscr_nargs);
693 r.sr.pscr_args = pscr_args;
702 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
708 error = copyout(&r.pve, uap->addr, sizeof(r.pve));
711 error = copyout(&r.piod, uap->addr, sizeof(r.piod));
714 error = copyout(&r.reg, uap->addr, sizeof(r.reg));
717 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
720 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
723 error = copyout(&r.vec, uap->addr, sizeof(r.vec));
725 case PT_GET_EVENT_MASK:
726 /* NB: The size in uap->data is validated in kern_ptrace(). */
727 error = copyout(&r.ptevents, uap->addr, uap->data);
730 /* NB: The size in uap->data is validated in kern_ptrace(). */
731 error = copyout(&r.pl, uap->addr, uap->data);
734 error = copyout(r.args, uap->addr, MIN(uap->data,
738 error = copyout(&r.psr, uap->addr, MIN(uap->data,
742 error = copyout(&r.sr.pscr_ret, uap->addr +
743 offsetof(struct ptrace_sc_remote, pscr_ret),
744 sizeof(r.sr.pscr_ret));
751 #ifdef COMPAT_FREEBSD32
753 * PROC_READ(regs, td2, addr);
755 * proc_read_regs(td2, addr);
757 * proc_read_regs32(td2, addr);
758 * .. except this is done at runtime. There is an additional
759 * complication in that PROC_WRITE disallows 32 bit consumers
760 * from writing to 64 bit address space targets.
762 #define PROC_READ(w, t, a) wrap32 ? \
763 proc_read_ ## w ## 32(t, a) : \
764 proc_read_ ## w (t, a)
765 #define PROC_WRITE(w, t, a) wrap32 ? \
766 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
767 proc_write_ ## w (t, a)
769 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
770 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
774 proc_set_traced(struct proc *p, bool stop)
777 sx_assert(&proctree_lock, SX_XLOCKED);
778 PROC_LOCK_ASSERT(p, MA_OWNED);
779 p->p_flag |= P_TRACED;
781 p->p_flag2 |= P2_PTRACE_FSTP;
782 p->p_ptevents = PTRACE_DEFAULT;
786 ptrace_unsuspend(struct proc *p)
788 PROC_LOCK_ASSERT(p, MA_OWNED);
791 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
794 itimer_proc_continue(p);
795 kqtimer_proc_continue(p);
799 proc_can_ptrace(struct thread *td, struct proc *p)
803 PROC_LOCK_ASSERT(p, MA_OWNED);
805 if ((p->p_flag & P_WEXIT) != 0)
808 if ((error = p_cansee(td, p)) != 0)
810 if ((error = p_candebug(td, p)) != 0)
813 /* not being traced... */
814 if ((p->p_flag & P_TRACED) == 0)
817 /* not being traced by YOU */
818 if (p->p_pptr != td->td_proc)
821 /* not currently stopped */
822 if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
823 p->p_suspcount != p->p_numthreads ||
824 (p->p_flag & P_WAITED) == 0)
830 static struct thread *
831 ptrace_sel_coredump_thread(struct proc *p)
835 PROC_LOCK_ASSERT(p, MA_OWNED);
836 MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
838 FOREACH_THREAD_IN_PROC(p, td2) {
839 if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
846 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
850 struct proc *curp, *p, *pp;
851 struct thread *td2 = NULL, *td3;
852 struct ptrace_io_desc *piod = NULL;
853 struct ptrace_lwpinfo *pl;
854 struct ptrace_sc_ret *psr;
855 struct ptrace_sc_remote *pscr;
857 struct ptrace_coredump *pc;
858 struct thr_coredump_req *tcq;
859 struct thr_syscall_req *tsr;
861 lwpid_t tid = 0, *buf;
862 #ifdef COMPAT_FREEBSD32
863 int wrap32 = 0, safe = 0;
865 bool proctree_locked, p2_req_set;
868 proctree_locked = false;
871 /* Lock proctree before locking the process. */
882 case PT_GET_EVENT_MASK:
883 case PT_SET_EVENT_MASK:
886 sx_xlock(&proctree_lock);
887 proctree_locked = true;
893 if (req == PT_TRACE_ME) {
897 if (pid <= PID_MAX) {
898 if ((p = pfind(pid)) == NULL) {
900 sx_xunlock(&proctree_lock);
904 td2 = tdfind(pid, -1);
907 sx_xunlock(&proctree_lock);
915 AUDIT_ARG_PROCESS(p);
917 if ((p->p_flag & P_WEXIT) != 0) {
921 if ((error = p_cansee(td, p)) != 0)
924 if ((error = p_candebug(td, p)) != 0)
928 * System processes can't be debugged.
930 if ((p->p_flag & P_SYSTEM) != 0) {
936 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
937 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
940 td2 = FIRST_THREAD_IN_PROC(p);
945 #ifdef COMPAT_FREEBSD32
947 * Test if we're a 32 bit client and what the target is.
948 * Set the wrap controls accordingly.
950 if (SV_CURPROC_FLAG(SV_ILP32)) {
951 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
962 * Always legal, when there is a parent process which
963 * could trace us. Otherwise, reject.
965 if ((p->p_flag & P_TRACED) != 0) {
969 if (p->p_pptr == initproc) {
977 if (p == td->td_proc) {
983 if (p->p_flag & P_TRACED) {
988 /* Can't trace an ancestor if you're being traced. */
989 if (curp->p_flag & P_TRACED) {
990 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
1002 /* Allow thread to clear single step for itself */
1003 if (td->td_tid == tid)
1009 * Check for ptrace eligibility before waiting for
1012 error = proc_can_ptrace(td, p);
1017 * Block parallel ptrace requests. Most important, do
1018 * not allow other thread in debugger to continue the
1019 * debuggee until coredump finished.
1021 while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
1022 if (proctree_locked)
1023 sx_xunlock(&proctree_lock);
1024 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
1025 (proctree_locked ? PDROP : 0), "pptrace", 0);
1026 if (proctree_locked) {
1027 sx_xlock(&proctree_lock);
1030 if (error == 0 && td2->td_proc != p)
1033 error = proc_can_ptrace(td, p);
1043 * Keep this process around and request parallel ptrace()
1044 * request to wait until we finish this request.
1046 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
1047 p->p_flag2 |= P2_PTRACEREQ;
1052 * Actually do the requests
1055 td->td_retval[0] = 0;
1059 /* set my trace flag and "owner" so it can read/write me */
1060 proc_set_traced(p, false);
1061 if (p->p_flag & P_PPWAIT)
1062 p->p_flag |= P_PPTRACE;
1063 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
1067 /* security check done above */
1069 * It would be nice if the tracing relationship was separate
1070 * from the parent relationship but that would require
1071 * another set of links in the proc struct or for "wait"
1072 * to scan the entire proc table. To make life easier,
1073 * we just re-parent the process we're trying to trace.
1074 * The old parent is remembered so we can put things back
1077 proc_set_traced(p, true);
1078 proc_reparent(p, td->td_proc, false);
1079 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
1082 sx_xunlock(&proctree_lock);
1083 proctree_locked = false;
1084 MPASS(p->p_xthread == NULL);
1085 MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
1088 * If already stopped due to a stop signal, clear the
1089 * existing stop before triggering a traced SIGSTOP.
1091 if ((p->p_flag & P_STOPPED_SIG) != 0) {
1093 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
1094 thread_unsuspend(p);
1098 kern_psignal(p, SIGSTOP);
1102 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
1104 error = ptrace_clear_single_step(td2);
1108 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
1110 error = ptrace_single_step(td2);
1114 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1116 td2->td_dbgflags |= TDB_SUSPEND;
1117 ast_sched(td2, TDA_SUSPEND);
1121 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1123 td2->td_dbgflags &= ~TDB_SUSPEND;
1126 case PT_FOLLOW_FORK:
1127 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1128 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1129 data ? "enabled" : "disabled");
1131 p->p_ptevents |= PTRACE_FORK;
1133 p->p_ptevents &= ~PTRACE_FORK;
1137 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1138 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1139 data ? "enabled" : "disabled");
1141 p->p_ptevents |= PTRACE_LWP;
1143 p->p_ptevents &= ~PTRACE_LWP;
1146 case PT_GET_EVENT_MASK:
1147 if (data != sizeof(p->p_ptevents)) {
1151 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1153 *(int *)addr = p->p_ptevents;
1156 case PT_SET_EVENT_MASK:
1157 if (data != sizeof(p->p_ptevents)) {
1162 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1163 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1167 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1168 p->p_pid, p->p_ptevents, tmp);
1169 p->p_ptevents = tmp;
1172 case PT_GET_SC_ARGS:
1173 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1174 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1175 #ifdef COMPAT_FREEBSD32
1176 || (wrap32 && !safe)
1182 bzero(addr, sizeof(td2->td_sa.args));
1183 /* See the explanation in linux_ptrace_get_syscall_info(). */
1184 bcopy(td2->td_sa.args, addr, SV_PROC_ABI(td->td_proc) ==
1185 SV_ABI_LINUX ? sizeof(td2->td_sa.args) :
1186 td2->td_sa.callp->sy_narg * sizeof(syscallarg_t));
1190 if ((td2->td_dbgflags & (TDB_SCX)) == 0
1191 #ifdef COMPAT_FREEBSD32
1192 || (wrap32 && !safe)
1199 bzero(psr, sizeof(*psr));
1200 psr->sr_error = td2->td_errno;
1201 if (psr->sr_error == 0) {
1202 psr->sr_retval[0] = td2->td_retval[0];
1203 psr->sr_retval[1] = td2->td_retval[1];
1206 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1207 p->p_pid, psr->sr_error, psr->sr_retval[0],
1217 /* Zero means do not send any signal */
1218 if (data < 0 || data > _SIG_MAXSIG) {
1225 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1226 td2->td_tid, p->p_pid, data);
1227 error = ptrace_single_step(td2);
1235 if (addr != (void *)1) {
1236 error = ptrace_set_pc(td2,
1237 (u_long)(uintfptr_t)addr);
1243 p->p_ptevents |= PTRACE_SCE;
1245 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1246 p->p_pid, p->p_ptevents,
1247 (u_long)(uintfptr_t)addr, data);
1250 p->p_ptevents |= PTRACE_SCX;
1252 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1253 p->p_pid, p->p_ptevents,
1254 (u_long)(uintfptr_t)addr, data);
1257 p->p_ptevents |= PTRACE_SYSCALL;
1259 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1260 p->p_pid, p->p_ptevents,
1261 (u_long)(uintfptr_t)addr, data);
1265 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1266 p->p_pid, (u_long)(uintfptr_t)addr, data);
1272 * Clear P_TRACED before reparenting
1273 * a detached process back to its original
1274 * parent. Otherwise the debugee will be set
1275 * as an orphan of the debugger.
1277 p->p_flag &= ~(P_TRACED | P_WAITED);
1280 * Reset the process parent.
1282 if (p->p_oppid != p->p_pptr->p_pid) {
1283 PROC_LOCK(p->p_pptr);
1284 sigqueue_take(p->p_ksi);
1285 PROC_UNLOCK(p->p_pptr);
1287 pp = proc_realparent(p);
1288 proc_reparent(p, pp, false);
1290 p->p_sigparent = SIGCHLD;
1292 "PT_DETACH: pid %d reparented to pid %d, sig %d",
1293 p->p_pid, pp->p_pid, data);
1295 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1300 FOREACH_THREAD_IN_PROC(p, td3) {
1301 if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1302 sigqueue_delete(&td3->td_sigqueue,
1305 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1306 TDB_SUSPEND | TDB_BORN);
1309 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1310 sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1311 p->p_flag2 &= ~P2_PTRACE_FSTP;
1314 /* should we send SIGCHLD? */
1315 /* childproc_continued(p); */
1319 sx_xunlock(&proctree_lock);
1320 proctree_locked = false;
1323 MPASS(!proctree_locked);
1326 * Clear the pending event for the thread that just
1327 * reported its event (p_xthread). This may not be
1328 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1329 * the debugger is resuming a different thread.
1331 * Deliver any pending signal via the reporting thread.
1333 MPASS(p->p_xthread != NULL);
1334 p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1335 p->p_xthread->td_xsig = data;
1336 p->p_xthread = NULL;
1340 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1341 * always works immediately, even if another thread is
1342 * unsuspended first and attempts to handle a
1343 * different signal or if the POSIX.1b style signal
1344 * queue cannot accommodate any new signals.
1346 if (data == SIGKILL)
1350 * Unsuspend all threads. To leave a thread
1351 * suspended, use PT_SUSPEND to suspend it before
1352 * continuing the process.
1354 ptrace_unsuspend(p);
1359 td2->td_dbgflags |= TDB_USERWR;
1362 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1363 sizeof(int)) != sizeof(int))
1366 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1367 p->p_pid, addr, data);
1375 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1376 sizeof(int)) != sizeof(int))
1379 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1380 p->p_pid, addr, tmp);
1381 td->td_retval[0] = tmp;
1387 iov.iov_base = piod->piod_addr;
1388 iov.iov_len = piod->piod_len;
1389 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1390 uio.uio_resid = piod->piod_len;
1393 uio.uio_segflg = UIO_USERSPACE;
1395 switch (piod->piod_op) {
1398 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1399 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1400 uio.uio_rw = UIO_READ;
1404 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1405 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1406 td2->td_dbgflags |= TDB_USERWR;
1407 uio.uio_rw = UIO_WRITE;
1414 error = proc_rwmem(p, &uio);
1415 piod->piod_len -= uio.uio_resid;
1420 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1422 goto sendsig; /* in PT_CONTINUE above */
1425 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1427 td2->td_dbgflags |= TDB_USERWR;
1428 error = PROC_WRITE(regs, td2, addr);
1432 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1434 error = PROC_READ(regs, td2, addr);
1438 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1440 td2->td_dbgflags |= TDB_USERWR;
1441 error = PROC_WRITE(fpregs, td2, addr);
1445 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1447 error = PROC_READ(fpregs, td2, addr);
1451 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1453 td2->td_dbgflags |= TDB_USERWR;
1454 error = PROC_WRITE(dbregs, td2, addr);
1458 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1460 error = PROC_READ(dbregs, td2, addr);
1464 CTR2(KTR_PTRACE, "PT_SETREGSET: tid %d (pid %d)", td2->td_tid,
1466 error = proc_write_regset(td2, data, addr);
1470 CTR2(KTR_PTRACE, "PT_GETREGSET: tid %d (pid %d)", td2->td_tid,
1472 error = proc_read_regset(td2, data, addr);
1476 if (data <= 0 || data > sizeof(*pl)) {
1481 bzero(pl, sizeof(*pl));
1482 pl->pl_lwpid = td2->td_tid;
1483 pl->pl_event = PL_EVENT_NONE;
1485 if (td2->td_dbgflags & TDB_XSIG) {
1486 pl->pl_event = PL_EVENT_SIGNAL;
1487 if (td2->td_si.si_signo != 0 &&
1488 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1489 + sizeof(pl->pl_siginfo)){
1490 pl->pl_flags |= PL_FLAG_SI;
1491 pl->pl_siginfo = td2->td_si;
1494 if (td2->td_dbgflags & TDB_SCE)
1495 pl->pl_flags |= PL_FLAG_SCE;
1496 else if (td2->td_dbgflags & TDB_SCX)
1497 pl->pl_flags |= PL_FLAG_SCX;
1498 if (td2->td_dbgflags & TDB_EXEC)
1499 pl->pl_flags |= PL_FLAG_EXEC;
1500 if (td2->td_dbgflags & TDB_FORK) {
1501 pl->pl_flags |= PL_FLAG_FORKED;
1502 pl->pl_child_pid = td2->td_dbg_forked;
1503 if (td2->td_dbgflags & TDB_VFORK)
1504 pl->pl_flags |= PL_FLAG_VFORKED;
1505 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1507 pl->pl_flags |= PL_FLAG_VFORK_DONE;
1508 if (td2->td_dbgflags & TDB_CHILD)
1509 pl->pl_flags |= PL_FLAG_CHILD;
1510 if (td2->td_dbgflags & TDB_BORN)
1511 pl->pl_flags |= PL_FLAG_BORN;
1512 if (td2->td_dbgflags & TDB_EXIT)
1513 pl->pl_flags |= PL_FLAG_EXITED;
1514 pl->pl_sigmask = td2->td_sigmask;
1515 pl->pl_siglist = td2->td_siglist;
1516 strcpy(pl->pl_tdname, td2->td_name);
1517 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1518 pl->pl_syscall_code = td2->td_sa.code;
1519 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1521 pl->pl_syscall_code = 0;
1522 pl->pl_syscall_narg = 0;
1525 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1526 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1527 pl->pl_child_pid, pl->pl_syscall_code);
1531 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1533 td->td_retval[0] = p->p_numthreads;
1537 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1538 p->p_pid, data, p->p_numthreads);
1543 num = imin(p->p_numthreads, data);
1545 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1548 FOREACH_THREAD_IN_PROC(p, td2) {
1551 buf[tmp++] = td2->td_tid;
1554 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1557 td->td_retval[0] = tmp;
1561 case PT_VM_TIMESTAMP:
1562 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1563 p->p_pid, p->p_vmspace->vm_map.timestamp);
1564 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1569 error = ptrace_vm_entry(td, p, addr);
1575 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1576 p->p_pid, pc->pc_fd);
1578 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1584 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1586 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1588 goto coredump_cleanup_nofp;
1589 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1591 goto coredump_cleanup;
1595 error = proc_can_ptrace(td, p);
1597 goto coredump_cleanup_locked;
1599 td2 = ptrace_sel_coredump_thread(p);
1602 goto coredump_cleanup_locked;
1604 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1605 TDB_SCREMOTEREQ)) == 0,
1606 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1608 tcq->tc_vp = fp->f_vnode;
1609 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1610 tcq->tc_flags = SVC_PT_COREDUMP;
1611 if ((pc->pc_flags & PC_COMPRESS) == 0)
1612 tcq->tc_flags |= SVC_NOCOMPRESS;
1613 if ((pc->pc_flags & PC_ALL) != 0)
1614 tcq->tc_flags |= SVC_ALL;
1615 td2->td_remotereq = tcq;
1616 td2->td_dbgflags |= TDB_COREDUMPREQ;
1617 thread_run_flash(td2);
1618 while ((td2->td_dbgflags & TDB_COREDUMPREQ) != 0)
1619 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1620 error = tcq->tc_error;
1621 coredump_cleanup_locked:
1625 coredump_cleanup_nofp:
1632 CTR2(KTR_PTRACE, "PT_SC_REMOTE: pid %d, syscall %d",
1633 p->p_pid, pscr->pscr_syscall);
1634 if ((td2->td_dbgflags & TDB_BOUNDARY) == 0) {
1639 MPASS(pscr->pscr_nargs <= nitems(td->td_sa.args));
1641 tsr = malloc(sizeof(struct thr_syscall_req), M_TEMP,
1644 tsr->ts_sa.code = pscr->pscr_syscall;
1645 tsr->ts_nargs = pscr->pscr_nargs;
1646 memcpy(&tsr->ts_sa.args, pscr->pscr_args,
1647 sizeof(syscallarg_t) * tsr->ts_nargs);
1650 error = proc_can_ptrace(td, p);
1655 if (td2->td_proc != p) {
1660 KASSERT((td2->td_dbgflags & (TDB_COREDUMPREQ |
1661 TDB_SCREMOTEREQ)) == 0,
1662 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1664 td2->td_remotereq = tsr;
1665 td2->td_dbgflags |= TDB_SCREMOTEREQ;
1666 thread_run_flash(td2);
1667 while ((td2->td_dbgflags & TDB_SCREMOTEREQ) != 0)
1668 msleep(p, &p->p_mtx, PPAUSE, "pscrx", 0);
1670 memcpy(&pscr->pscr_ret, &tsr->ts_ret, sizeof(tsr->ts_ret));
1675 #ifdef __HAVE_PTRACE_MACHDEP
1676 if (req >= PT_FIRSTMACH) {
1678 error = cpu_ptrace(td2, req, addr, data);
1682 /* Unknown request. */
1687 /* Drop our hold on this process now that the request has completed. */
1691 if ((p->p_flag2 & P2_PTRACEREQ) != 0)
1692 wakeup(&p->p_flag2);
1693 p->p_flag2 &= ~P2_PTRACEREQ;
1696 if (proctree_locked)
1697 sx_xunlock(&proctree_lock);