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/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/limits.h>
42 #include <sys/mutex.h>
43 #include <sys/syscallsubr.h>
44 #include <sys/sysent.h>
45 #include <sys/sysproto.h>
48 #include <sys/vnode.h>
49 #include <sys/ptrace.h>
50 #include <sys/rwlock.h>
52 #include <sys/malloc.h>
53 #include <sys/signalvar.h>
54 #include <sys/caprights.h>
55 #include <sys/filedesc.h>
57 #include <machine/reg.h>
59 #include <security/audit/audit.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>
70 #ifdef COMPAT_FREEBSD32
71 #include <sys/procfs.h>
75 * Functions implemented using PROC_ACTION():
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.
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.
90 * proc_read_fpregs, proc_write_fpregs
91 * deal with the floating point register set, otherwise as above.
93 * proc_read_dbregs, proc_write_dbregs
94 * deal with the processor debug register set, otherwise as above.
97 * Arrange for the process to trap after executing a single instruction.
100 #define PROC_ACTION(action) do { \
103 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
104 if ((td->td_proc->p_flag & P_INMEM) == 0) \
112 proc_read_regs(struct thread *td, struct reg *regs)
115 PROC_ACTION(fill_regs(td, regs));
119 proc_write_regs(struct thread *td, struct reg *regs)
122 PROC_ACTION(set_regs(td, regs));
126 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
129 PROC_ACTION(fill_dbregs(td, dbregs));
133 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
136 PROC_ACTION(set_dbregs(td, dbregs));
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.
144 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
147 PROC_ACTION(fill_fpregs(td, fpregs));
151 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
154 PROC_ACTION(set_fpregs(td, fpregs));
157 #ifdef COMPAT_FREEBSD32
158 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
160 proc_read_regs32(struct thread *td, struct reg32 *regs32)
163 PROC_ACTION(fill_regs32(td, regs32));
167 proc_write_regs32(struct thread *td, struct reg32 *regs32)
170 PROC_ACTION(set_regs32(td, regs32));
174 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
177 PROC_ACTION(fill_dbregs32(td, dbregs32));
181 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
184 PROC_ACTION(set_dbregs32(td, dbregs32));
188 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
191 PROC_ACTION(fill_fpregs32(td, fpregs32));
195 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
198 PROC_ACTION(set_fpregs32(td, fpregs32));
203 proc_sstep(struct thread *td)
206 PROC_ACTION(ptrace_single_step(td));
210 proc_rwmem(struct proc *p, struct uio *uio)
213 vm_offset_t pageno; /* page number */
215 int error, fault_flags, page_offset, writing;
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.
223 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
228 map = &p->p_vmspace->vm_map;
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.
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;
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?
248 uva = (vm_offset_t)uio->uio_offset;
251 * Get the page number of this segment.
253 pageno = trunc_page(uva);
254 page_offset = uva - pageno;
257 * How many bytes to copy
259 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
262 * Fault and hold the page on behalf of the process.
264 error = vm_fault(map, pageno, reqprot, fault_flags, &m);
265 if (error != KERN_SUCCESS) {
266 if (error == KERN_RESOURCE_SHORTAGE)
274 * Now do the i/o move.
276 error = uiomove_fromphys(&m, page_offset, len, uio);
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);
290 vm_page_unwire(m, PQ_ACTIVE);
292 } while (error == 0 && uio->uio_resid > 0);
298 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
299 size_t len, enum uio_rw rw)
305 MPASS(len < SSIZE_MAX);
308 iov.iov_base = (caddr_t)buf;
313 uio.uio_resid = slen;
314 uio.uio_segflg = UIO_SYSSPACE;
318 if (uio.uio_resid == slen)
320 return (slen - uio.uio_resid);
324 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
328 return (proc_iop(td, p, va, buf, len, UIO_READ));
332 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
336 return (proc_iop(td, p, va, buf, len, UIO_WRITE));
340 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
344 vm_map_entry_t entry;
345 vm_object_t obj, tobj, lobj;
348 char *freepath, *fullpath;
355 vm = vmspace_acquire_ref(p);
357 vm_map_lock_read(map);
360 KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
361 ("Submap in map header"));
363 VM_MAP_ENTRY_FOREACH(entry, map) {
364 if (index >= pve->pve_entry &&
365 (entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
369 if (index < pve->pve_entry) {
373 if (entry == &map->header) {
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;
386 /* Backing object's path needed? */
387 if (pve->pve_pathlen == 0)
390 pathlen = pve->pve_pathlen;
391 pve->pve_pathlen = 0;
393 obj = entry->object.vm_object;
395 VM_OBJECT_RLOCK(obj);
398 vm_map_unlock_read(map);
400 pve->pve_fsid = VNOVAL;
401 pve->pve_fileid = VNOVAL;
403 if (error == 0 && obj != NULL) {
405 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
407 VM_OBJECT_RLOCK(tobj);
409 VM_OBJECT_RUNLOCK(lobj);
411 pve->pve_offset += tobj->backing_object_offset;
413 vp = vm_object_vnode(lobj);
417 VM_OBJECT_RUNLOCK(lobj);
418 VM_OBJECT_RUNLOCK(obj);
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;
431 if (fullpath != NULL) {
432 pve->pve_pathlen = strlen(fullpath) + 1;
433 if (pve->pve_pathlen <= pathlen) {
434 error = copyout(fullpath, pve->pve_path,
437 error = ENAMETOOLONG;
439 if (freepath != NULL)
440 free(freepath, M_TEMP);
445 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
446 p->p_pid, pve->pve_entry, pve->pve_start);
452 * Process debugging system call.
454 #ifndef _SYS_SYSPROTO_H_
464 sys_ptrace(struct thread *td, struct ptrace_args *uap)
467 * XXX this obfuscation is to reduce stack usage, but the register
468 * structs may be too large to put on the stack anyway.
471 struct ptrace_io_desc piod;
472 struct ptrace_lwpinfo pl;
473 struct ptrace_vm_entry pve;
474 struct ptrace_coredump pc;
478 char args[sizeof(td->td_sa.args)];
479 struct ptrace_sc_ret psr;
485 AUDIT_ARG_PID(uap->pid);
486 AUDIT_ARG_CMD(uap->req);
487 AUDIT_ARG_VALUE(uap->data);
490 case PT_GET_EVENT_MASK:
496 bzero(&r.reg, sizeof(r.reg));
499 bzero(&r.fpreg, sizeof(r.fpreg));
502 bzero(&r.dbreg, sizeof(r.dbreg));
505 error = copyin(uap->addr, &r.reg, sizeof(r.reg));
508 error = copyin(uap->addr, &r.fpreg, sizeof(r.fpreg));
511 error = copyin(uap->addr, &r.dbreg, sizeof(r.dbreg));
513 case PT_SET_EVENT_MASK:
514 if (uap->data != sizeof(r.ptevents))
517 error = copyin(uap->addr, &r.ptevents, uap->data);
520 error = copyin(uap->addr, &r.piod, sizeof(r.piod));
523 error = copyin(uap->addr, &r.pve, sizeof(r.pve));
526 if (uap->data != sizeof(r.pc))
529 error = copyin(uap->addr, &r.pc, uap->data);
538 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
544 error = copyout(&r.pve, uap->addr, sizeof(r.pve));
547 error = copyout(&r.piod, uap->addr, sizeof(r.piod));
550 error = copyout(&r.reg, uap->addr, sizeof(r.reg));
553 error = copyout(&r.fpreg, uap->addr, sizeof(r.fpreg));
556 error = copyout(&r.dbreg, uap->addr, sizeof(r.dbreg));
558 case PT_GET_EVENT_MASK:
559 /* NB: The size in uap->data is validated in kern_ptrace(). */
560 error = copyout(&r.ptevents, uap->addr, uap->data);
563 /* NB: The size in uap->data is validated in kern_ptrace(). */
564 error = copyout(&r.pl, uap->addr, uap->data);
567 error = copyout(r.args, uap->addr, MIN(uap->data,
571 error = copyout(&r.psr, uap->addr, MIN(uap->data,
579 #ifdef COMPAT_FREEBSD32
581 * PROC_READ(regs, td2, addr);
583 * proc_read_regs(td2, addr);
585 * proc_read_regs32(td2, addr);
586 * .. except this is done at runtime. There is an additional
587 * complication in that PROC_WRITE disallows 32 bit consumers
588 * from writing to 64 bit address space targets.
590 #define PROC_READ(w, t, a) wrap32 ? \
591 proc_read_ ## w ## 32(t, a) : \
592 proc_read_ ## w (t, a)
593 #define PROC_WRITE(w, t, a) wrap32 ? \
594 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
595 proc_write_ ## w (t, a)
597 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
598 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
602 proc_set_traced(struct proc *p, bool stop)
605 sx_assert(&proctree_lock, SX_XLOCKED);
606 PROC_LOCK_ASSERT(p, MA_OWNED);
607 p->p_flag |= P_TRACED;
609 p->p_flag2 |= P2_PTRACE_FSTP;
610 p->p_ptevents = PTRACE_DEFAULT;
614 ptrace_unsuspend(struct proc *p)
616 PROC_LOCK_ASSERT(p, MA_OWNED);
619 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
622 itimer_proc_continue(p);
623 kqtimer_proc_continue(p);
627 proc_can_ptrace(struct thread *td, struct proc *p)
631 PROC_LOCK_ASSERT(p, MA_OWNED);
633 if ((p->p_flag & P_WEXIT) != 0)
636 if ((error = p_cansee(td, p)) != 0)
638 if ((error = p_candebug(td, p)) != 0)
641 /* not being traced... */
642 if ((p->p_flag & P_TRACED) == 0)
645 /* not being traced by YOU */
646 if (p->p_pptr != td->td_proc)
649 /* not currently stopped */
650 if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
651 p->p_suspcount != p->p_numthreads ||
652 (p->p_flag & P_WAITED) == 0)
658 static struct thread *
659 ptrace_sel_coredump_thread(struct proc *p)
663 PROC_LOCK_ASSERT(p, MA_OWNED);
664 MPASS((p->p_flag & P_STOPPED_TRACE) != 0);
666 FOREACH_THREAD_IN_PROC(p, td2) {
667 if ((td2->td_dbgflags & TDB_SSWITCH) != 0)
674 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
678 struct proc *curp, *p, *pp;
679 struct thread *td2 = NULL, *td3;
680 struct ptrace_io_desc *piod = NULL;
681 struct ptrace_lwpinfo *pl;
682 struct ptrace_sc_ret *psr;
684 struct ptrace_coredump *pc;
685 struct thr_coredump_req *tcq;
687 lwpid_t tid = 0, *buf;
688 #ifdef COMPAT_FREEBSD32
689 int wrap32 = 0, safe = 0;
691 bool proctree_locked, p2_req_set;
694 proctree_locked = false;
697 /* Lock proctree before locking the process. */
708 case PT_GET_EVENT_MASK:
709 case PT_SET_EVENT_MASK:
712 sx_xlock(&proctree_lock);
713 proctree_locked = true;
719 if (req == PT_TRACE_ME) {
723 if (pid <= PID_MAX) {
724 if ((p = pfind(pid)) == NULL) {
726 sx_xunlock(&proctree_lock);
730 td2 = tdfind(pid, -1);
733 sx_xunlock(&proctree_lock);
741 AUDIT_ARG_PROCESS(p);
743 if ((p->p_flag & P_WEXIT) != 0) {
747 if ((error = p_cansee(td, p)) != 0)
750 if ((error = p_candebug(td, p)) != 0)
754 * System processes can't be debugged.
756 if ((p->p_flag & P_SYSTEM) != 0) {
762 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
763 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
766 td2 = FIRST_THREAD_IN_PROC(p);
771 #ifdef COMPAT_FREEBSD32
773 * Test if we're a 32 bit client and what the target is.
774 * Set the wrap controls accordingly.
776 if (SV_CURPROC_FLAG(SV_ILP32)) {
777 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
788 * Always legal, when there is a parent process which
789 * could trace us. Otherwise, reject.
791 if ((p->p_flag & P_TRACED) != 0) {
795 if (p->p_pptr == initproc) {
803 if (p == td->td_proc) {
809 if (p->p_flag & P_TRACED) {
814 /* Can't trace an ancestor if you're being traced. */
815 if (curp->p_flag & P_TRACED) {
816 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
828 /* Allow thread to clear single step for itself */
829 if (td->td_tid == tid)
835 * Check for ptrace eligibility before waiting for
838 error = proc_can_ptrace(td, p);
843 * Block parallel ptrace requests. Most important, do
844 * not allow other thread in debugger to continue the
845 * debuggee until coredump finished.
847 while ((p->p_flag2 & P2_PTRACEREQ) != 0) {
849 sx_xunlock(&proctree_lock);
850 error = msleep(&p->p_flag2, &p->p_mtx, PPAUSE | PCATCH |
851 (proctree_locked ? PDROP : 0), "pptrace", 0);
852 if (proctree_locked) {
853 sx_xlock(&proctree_lock);
856 if (error == 0 && td2->td_proc != p)
859 error = proc_can_ptrace(td, p);
869 * Keep this process around and request parallel ptrace()
870 * request to wait until we finish this request.
872 MPASS((p->p_flag2 & P2_PTRACEREQ) == 0);
873 p->p_flag2 |= P2_PTRACEREQ;
878 * Actually do the requests
881 td->td_retval[0] = 0;
885 /* set my trace flag and "owner" so it can read/write me */
886 proc_set_traced(p, false);
887 if (p->p_flag & P_PPWAIT)
888 p->p_flag |= P_PPTRACE;
889 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
893 /* security check done above */
895 * It would be nice if the tracing relationship was separate
896 * from the parent relationship but that would require
897 * another set of links in the proc struct or for "wait"
898 * to scan the entire proc table. To make life easier,
899 * we just re-parent the process we're trying to trace.
900 * The old parent is remembered so we can put things back
903 proc_set_traced(p, true);
904 proc_reparent(p, td->td_proc, false);
905 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
908 sx_xunlock(&proctree_lock);
909 proctree_locked = false;
910 MPASS(p->p_xthread == NULL);
911 MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
914 * If already stopped due to a stop signal, clear the
915 * existing stop before triggering a traced SIGSTOP.
917 if ((p->p_flag & P_STOPPED_SIG) != 0) {
919 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
924 kern_psignal(p, SIGSTOP);
928 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
930 error = ptrace_clear_single_step(td2);
934 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
936 error = ptrace_single_step(td2);
940 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
942 td2->td_dbgflags |= TDB_SUSPEND;
944 td2->td_flags |= TDF_NEEDSUSPCHK;
949 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
951 td2->td_dbgflags &= ~TDB_SUSPEND;
955 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
956 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
957 data ? "enabled" : "disabled");
959 p->p_ptevents |= PTRACE_FORK;
961 p->p_ptevents &= ~PTRACE_FORK;
965 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
966 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
967 data ? "enabled" : "disabled");
969 p->p_ptevents |= PTRACE_LWP;
971 p->p_ptevents &= ~PTRACE_LWP;
974 case PT_GET_EVENT_MASK:
975 if (data != sizeof(p->p_ptevents)) {
979 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
981 *(int *)addr = p->p_ptevents;
984 case PT_SET_EVENT_MASK:
985 if (data != sizeof(p->p_ptevents)) {
990 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
991 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
995 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
996 p->p_pid, p->p_ptevents, tmp);
1000 case PT_GET_SC_ARGS:
1001 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1002 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1003 #ifdef COMPAT_FREEBSD32
1004 || (wrap32 && !safe)
1010 bzero(addr, sizeof(td2->td_sa.args));
1011 bcopy(td2->td_sa.args, addr, td2->td_sa.callp->sy_narg *
1012 sizeof(register_t));
1016 if ((td2->td_dbgflags & (TDB_SCX)) == 0
1017 #ifdef COMPAT_FREEBSD32
1018 || (wrap32 && !safe)
1025 bzero(psr, sizeof(*psr));
1026 psr->sr_error = td2->td_errno;
1027 if (psr->sr_error == 0) {
1028 psr->sr_retval[0] = td2->td_retval[0];
1029 psr->sr_retval[1] = td2->td_retval[1];
1032 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1033 p->p_pid, psr->sr_error, psr->sr_retval[0],
1043 /* Zero means do not send any signal */
1044 if (data < 0 || data > _SIG_MAXSIG) {
1051 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1052 td2->td_tid, p->p_pid, data);
1053 error = ptrace_single_step(td2);
1061 if (addr != (void *)1) {
1062 error = ptrace_set_pc(td2,
1063 (u_long)(uintfptr_t)addr);
1069 p->p_ptevents |= PTRACE_SCE;
1071 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1072 p->p_pid, p->p_ptevents,
1073 (u_long)(uintfptr_t)addr, data);
1076 p->p_ptevents |= PTRACE_SCX;
1078 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1079 p->p_pid, p->p_ptevents,
1080 (u_long)(uintfptr_t)addr, data);
1083 p->p_ptevents |= PTRACE_SYSCALL;
1085 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1086 p->p_pid, p->p_ptevents,
1087 (u_long)(uintfptr_t)addr, data);
1091 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1092 p->p_pid, (u_long)(uintfptr_t)addr, data);
1098 * Clear P_TRACED before reparenting
1099 * a detached process back to its original
1100 * parent. Otherwise the debugee will be set
1101 * as an orphan of the debugger.
1103 p->p_flag &= ~(P_TRACED | P_WAITED);
1106 * Reset the process parent.
1108 if (p->p_oppid != p->p_pptr->p_pid) {
1109 PROC_LOCK(p->p_pptr);
1110 sigqueue_take(p->p_ksi);
1111 PROC_UNLOCK(p->p_pptr);
1113 pp = proc_realparent(p);
1114 proc_reparent(p, pp, false);
1116 p->p_sigparent = SIGCHLD;
1118 "PT_DETACH: pid %d reparented to pid %d, sig %d",
1119 p->p_pid, pp->p_pid, data);
1121 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1126 FOREACH_THREAD_IN_PROC(p, td3) {
1127 if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1128 sigqueue_delete(&td3->td_sigqueue,
1131 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1135 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1136 sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1137 p->p_flag2 &= ~P2_PTRACE_FSTP;
1140 /* should we send SIGCHLD? */
1141 /* childproc_continued(p); */
1145 sx_xunlock(&proctree_lock);
1146 proctree_locked = false;
1149 MPASS(!proctree_locked);
1152 * Clear the pending event for the thread that just
1153 * reported its event (p_xthread). This may not be
1154 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1155 * the debugger is resuming a different thread.
1157 * Deliver any pending signal via the reporting thread.
1159 MPASS(p->p_xthread != NULL);
1160 p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1161 p->p_xthread->td_xsig = data;
1162 p->p_xthread = NULL;
1166 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1167 * always works immediately, even if another thread is
1168 * unsuspended first and attempts to handle a
1169 * different signal or if the POSIX.1b style signal
1170 * queue cannot accommodate any new signals.
1172 if (data == SIGKILL)
1176 * Unsuspend all threads. To leave a thread
1177 * suspended, use PT_SUSPEND to suspend it before
1178 * continuing the process.
1180 ptrace_unsuspend(p);
1185 td2->td_dbgflags |= TDB_USERWR;
1188 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1189 sizeof(int)) != sizeof(int))
1192 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1193 p->p_pid, addr, data);
1201 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1202 sizeof(int)) != sizeof(int))
1205 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1206 p->p_pid, addr, tmp);
1207 td->td_retval[0] = tmp;
1213 iov.iov_base = piod->piod_addr;
1214 iov.iov_len = piod->piod_len;
1215 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1216 uio.uio_resid = piod->piod_len;
1219 uio.uio_segflg = UIO_USERSPACE;
1221 switch (piod->piod_op) {
1224 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1225 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1226 uio.uio_rw = UIO_READ;
1230 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1231 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1232 td2->td_dbgflags |= TDB_USERWR;
1233 uio.uio_rw = UIO_WRITE;
1240 error = proc_rwmem(p, &uio);
1241 piod->piod_len -= uio.uio_resid;
1246 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1248 goto sendsig; /* in PT_CONTINUE above */
1251 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1253 td2->td_dbgflags |= TDB_USERWR;
1254 error = PROC_WRITE(regs, td2, addr);
1258 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1260 error = PROC_READ(regs, td2, addr);
1264 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1266 td2->td_dbgflags |= TDB_USERWR;
1267 error = PROC_WRITE(fpregs, td2, addr);
1271 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1273 error = PROC_READ(fpregs, td2, addr);
1277 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1279 td2->td_dbgflags |= TDB_USERWR;
1280 error = PROC_WRITE(dbregs, td2, addr);
1284 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1286 error = PROC_READ(dbregs, td2, addr);
1290 if (data <= 0 || data > sizeof(*pl)) {
1295 bzero(pl, sizeof(*pl));
1296 pl->pl_lwpid = td2->td_tid;
1297 pl->pl_event = PL_EVENT_NONE;
1299 if (td2->td_dbgflags & TDB_XSIG) {
1300 pl->pl_event = PL_EVENT_SIGNAL;
1301 if (td2->td_si.si_signo != 0 &&
1302 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1303 + sizeof(pl->pl_siginfo)){
1304 pl->pl_flags |= PL_FLAG_SI;
1305 pl->pl_siginfo = td2->td_si;
1308 if (td2->td_dbgflags & TDB_SCE)
1309 pl->pl_flags |= PL_FLAG_SCE;
1310 else if (td2->td_dbgflags & TDB_SCX)
1311 pl->pl_flags |= PL_FLAG_SCX;
1312 if (td2->td_dbgflags & TDB_EXEC)
1313 pl->pl_flags |= PL_FLAG_EXEC;
1314 if (td2->td_dbgflags & TDB_FORK) {
1315 pl->pl_flags |= PL_FLAG_FORKED;
1316 pl->pl_child_pid = td2->td_dbg_forked;
1317 if (td2->td_dbgflags & TDB_VFORK)
1318 pl->pl_flags |= PL_FLAG_VFORKED;
1319 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1321 pl->pl_flags |= PL_FLAG_VFORK_DONE;
1322 if (td2->td_dbgflags & TDB_CHILD)
1323 pl->pl_flags |= PL_FLAG_CHILD;
1324 if (td2->td_dbgflags & TDB_BORN)
1325 pl->pl_flags |= PL_FLAG_BORN;
1326 if (td2->td_dbgflags & TDB_EXIT)
1327 pl->pl_flags |= PL_FLAG_EXITED;
1328 pl->pl_sigmask = td2->td_sigmask;
1329 pl->pl_siglist = td2->td_siglist;
1330 strcpy(pl->pl_tdname, td2->td_name);
1331 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1332 pl->pl_syscall_code = td2->td_sa.code;
1333 pl->pl_syscall_narg = td2->td_sa.callp->sy_narg;
1335 pl->pl_syscall_code = 0;
1336 pl->pl_syscall_narg = 0;
1339 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1340 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1341 pl->pl_child_pid, pl->pl_syscall_code);
1345 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1347 td->td_retval[0] = p->p_numthreads;
1351 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1352 p->p_pid, data, p->p_numthreads);
1357 num = imin(p->p_numthreads, data);
1359 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1362 FOREACH_THREAD_IN_PROC(p, td2) {
1365 buf[tmp++] = td2->td_tid;
1368 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1371 td->td_retval[0] = tmp;
1375 case PT_VM_TIMESTAMP:
1376 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1377 p->p_pid, p->p_vmspace->vm_map.timestamp);
1378 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1383 error = ptrace_vm_entry(td, p, addr);
1389 CTR2(KTR_PTRACE, "PT_COREDUMP: pid %d, fd %d",
1390 p->p_pid, pc->pc_fd);
1392 if ((pc->pc_flags & ~(PC_COMPRESS | PC_ALL)) != 0) {
1398 tcq = malloc(sizeof(*tcq), M_TEMP, M_WAITOK | M_ZERO);
1400 error = fget_write(td, pc->pc_fd, &cap_write_rights, &fp);
1402 goto coredump_cleanup_nofp;
1403 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VREG) {
1405 goto coredump_cleanup;
1409 error = proc_can_ptrace(td, p);
1411 goto coredump_cleanup_locked;
1413 td2 = ptrace_sel_coredump_thread(p);
1416 goto coredump_cleanup_locked;
1418 KASSERT((td2->td_dbgflags & TDB_COREDUMPRQ) == 0,
1419 ("proc %d tid %d req coredump", p->p_pid, td2->td_tid));
1421 tcq->tc_vp = fp->f_vnode;
1422 tcq->tc_limit = pc->pc_limit == 0 ? OFF_MAX : pc->pc_limit;
1423 tcq->tc_flags = SVC_PT_COREDUMP;
1424 if ((pc->pc_flags & PC_COMPRESS) == 0)
1425 tcq->tc_flags |= SVC_NOCOMPRESS;
1426 if ((pc->pc_flags & PC_ALL) != 0)
1427 tcq->tc_flags |= SVC_ALL;
1428 td2->td_coredump = tcq;
1429 td2->td_dbgflags |= TDB_COREDUMPRQ;
1430 thread_run_flash(td2);
1431 while ((td2->td_dbgflags & TDB_COREDUMPRQ) != 0)
1432 msleep(p, &p->p_mtx, PPAUSE, "crdmp", 0);
1433 error = tcq->tc_error;
1434 coredump_cleanup_locked:
1438 coredump_cleanup_nofp:
1444 #ifdef __HAVE_PTRACE_MACHDEP
1445 if (req >= PT_FIRSTMACH) {
1447 error = cpu_ptrace(td2, req, addr, data);
1451 /* Unknown request. */
1456 /* Drop our hold on this process now that the request has completed. */
1460 if ((p->p_flag2 & P2_PTRACEREQ) != 0)
1461 wakeup(&p->p_flag2);
1462 p->p_flag2 &= ~P2_PTRACEREQ;
1465 if (proctree_locked)
1466 sx_xunlock(&proctree_lock);