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>
41 #include <sys/mutex.h>
42 #include <sys/syscallsubr.h>
43 #include <sys/sysent.h>
44 #include <sys/sysproto.h>
45 #include <sys/pioctl.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>
55 #include <machine/reg.h>
57 #include <security/audit/audit.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>
68 #ifdef COMPAT_FREEBSD32
69 #include <sys/procfs.h>
70 #include <compat/freebsd32/freebsd32_signal.h>
72 struct ptrace_io_desc32 {
79 struct ptrace_sc_ret32 {
80 uint32_t sr_retval[2];
84 struct ptrace_vm_entry32 {
99 * Functions implemented using PROC_ACTION():
101 * proc_read_regs(proc, regs)
102 * Get the current user-visible register set from the process
103 * and copy it into the regs structure (<machine/reg.h>).
104 * The process is stopped at the time read_regs is called.
106 * proc_write_regs(proc, regs)
107 * Update the current register set from the passed in regs
108 * structure. Take care to avoid clobbering special CPU
109 * registers or privileged bits in the PSL.
110 * Depending on the architecture this may have fix-up work to do,
111 * especially if the IAR or PCW are modified.
112 * The process is stopped at the time write_regs is called.
114 * proc_read_fpregs, proc_write_fpregs
115 * deal with the floating point register set, otherwise as above.
117 * proc_read_dbregs, proc_write_dbregs
118 * deal with the processor debug register set, otherwise as above.
121 * Arrange for the process to trap after executing a single instruction.
124 #define PROC_ACTION(action) do { \
127 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
128 if ((td->td_proc->p_flag & P_INMEM) == 0) \
136 proc_read_regs(struct thread *td, struct reg *regs)
139 PROC_ACTION(fill_regs(td, regs));
143 proc_write_regs(struct thread *td, struct reg *regs)
146 PROC_ACTION(set_regs(td, regs));
150 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
153 PROC_ACTION(fill_dbregs(td, dbregs));
157 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
160 PROC_ACTION(set_dbregs(td, dbregs));
164 * Ptrace doesn't support fpregs at all, and there are no security holes
165 * or translations for fpregs, so we can just copy them.
168 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
171 PROC_ACTION(fill_fpregs(td, fpregs));
175 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
178 PROC_ACTION(set_fpregs(td, fpregs));
181 #ifdef COMPAT_FREEBSD32
182 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
184 proc_read_regs32(struct thread *td, struct reg32 *regs32)
187 PROC_ACTION(fill_regs32(td, regs32));
191 proc_write_regs32(struct thread *td, struct reg32 *regs32)
194 PROC_ACTION(set_regs32(td, regs32));
198 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
201 PROC_ACTION(fill_dbregs32(td, dbregs32));
205 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
208 PROC_ACTION(set_dbregs32(td, dbregs32));
212 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
215 PROC_ACTION(fill_fpregs32(td, fpregs32));
219 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
222 PROC_ACTION(set_fpregs32(td, fpregs32));
227 proc_sstep(struct thread *td)
230 PROC_ACTION(ptrace_single_step(td));
234 proc_rwmem(struct proc *p, struct uio *uio)
237 vm_offset_t pageno; /* page number */
239 int error, fault_flags, page_offset, writing;
242 * Assert that someone has locked this vmspace. (Should be
243 * curthread but we can't assert that.) This keeps the process
244 * from exiting out from under us until this operation completes.
247 PROC_LOCK_ASSERT(p, MA_NOTOWNED);
252 map = &p->p_vmspace->vm_map;
255 * If we are writing, then we request vm_fault() to create a private
256 * copy of each page. Since these copies will not be writeable by the
257 * process, we must explicity request that they be dirtied.
259 writing = uio->uio_rw == UIO_WRITE;
260 reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
261 fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
264 * Only map in one page at a time. We don't have to, but it
265 * makes things easier. This way is trivial - right?
272 uva = (vm_offset_t)uio->uio_offset;
275 * Get the page number of this segment.
277 pageno = trunc_page(uva);
278 page_offset = uva - pageno;
281 * How many bytes to copy
283 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
286 * Fault and hold the page on behalf of the process.
288 error = vm_fault_hold(map, pageno, reqprot, fault_flags, &m);
289 if (error != KERN_SUCCESS) {
290 if (error == KERN_RESOURCE_SHORTAGE)
298 * Now do the i/o move.
300 error = uiomove_fromphys(&m, page_offset, len, uio);
302 /* Make the I-cache coherent for breakpoints. */
303 if (writing && error == 0) {
304 vm_map_lock_read(map);
305 if (vm_map_check_protection(map, pageno, pageno +
306 PAGE_SIZE, VM_PROT_EXECUTE))
307 vm_sync_icache(map, uva, len);
308 vm_map_unlock_read(map);
315 if (vm_page_unwire(m, PQ_ACTIVE) && m->object == NULL)
319 } while (error == 0 && uio->uio_resid > 0);
325 proc_iop(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
326 size_t len, enum uio_rw rw)
332 MPASS(len < SSIZE_MAX);
335 iov.iov_base = (caddr_t)buf;
340 uio.uio_resid = slen;
341 uio.uio_segflg = UIO_SYSSPACE;
345 if (uio.uio_resid == slen)
347 return (slen - uio.uio_resid);
351 proc_readmem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
355 return (proc_iop(td, p, va, buf, len, UIO_READ));
359 proc_writemem(struct thread *td, struct proc *p, vm_offset_t va, void *buf,
363 return (proc_iop(td, p, va, buf, len, UIO_WRITE));
367 ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
371 vm_map_entry_t entry;
372 vm_object_t obj, tobj, lobj;
375 char *freepath, *fullpath;
382 vm = vmspace_acquire_ref(p);
384 vm_map_lock_read(map);
387 entry = map->header.next;
389 while (index < pve->pve_entry && entry != &map->header) {
393 if (index != pve->pve_entry) {
397 KASSERT((map->header.eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
398 ("Submap in map header"));
399 while ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
403 if (entry == &map->header) {
408 /* We got an entry. */
409 pve->pve_entry = index + 1;
410 pve->pve_timestamp = map->timestamp;
411 pve->pve_start = entry->start;
412 pve->pve_end = entry->end - 1;
413 pve->pve_offset = entry->offset;
414 pve->pve_prot = entry->protection;
416 /* Backing object's path needed? */
417 if (pve->pve_pathlen == 0)
420 pathlen = pve->pve_pathlen;
421 pve->pve_pathlen = 0;
423 obj = entry->object.vm_object;
425 VM_OBJECT_RLOCK(obj);
428 vm_map_unlock_read(map);
430 pve->pve_fsid = VNOVAL;
431 pve->pve_fileid = VNOVAL;
433 if (error == 0 && obj != NULL) {
435 for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
437 VM_OBJECT_RLOCK(tobj);
439 VM_OBJECT_RUNLOCK(lobj);
441 pve->pve_offset += tobj->backing_object_offset;
443 vp = vm_object_vnode(lobj);
447 VM_OBJECT_RUNLOCK(lobj);
448 VM_OBJECT_RUNLOCK(obj);
453 vn_fullpath(td, vp, &fullpath, &freepath);
454 vn_lock(vp, LK_SHARED | LK_RETRY);
455 if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
456 pve->pve_fileid = vattr.va_fileid;
457 pve->pve_fsid = vattr.va_fsid;
461 if (fullpath != NULL) {
462 pve->pve_pathlen = strlen(fullpath) + 1;
463 if (pve->pve_pathlen <= pathlen) {
464 error = copyout(fullpath, pve->pve_path,
467 error = ENAMETOOLONG;
469 if (freepath != NULL)
470 free(freepath, M_TEMP);
475 CTR3(KTR_PTRACE, "PT_VM_ENTRY: pid %d, entry %d, start %p",
476 p->p_pid, pve->pve_entry, pve->pve_start);
481 #ifdef COMPAT_FREEBSD32
483 ptrace_vm_entry32(struct thread *td, struct proc *p,
484 struct ptrace_vm_entry32 *pve32)
486 struct ptrace_vm_entry pve;
489 pve.pve_entry = pve32->pve_entry;
490 pve.pve_pathlen = pve32->pve_pathlen;
491 pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
493 error = ptrace_vm_entry(td, p, &pve);
495 pve32->pve_entry = pve.pve_entry;
496 pve32->pve_timestamp = pve.pve_timestamp;
497 pve32->pve_start = pve.pve_start;
498 pve32->pve_end = pve.pve_end;
499 pve32->pve_offset = pve.pve_offset;
500 pve32->pve_prot = pve.pve_prot;
501 pve32->pve_fileid = pve.pve_fileid;
502 pve32->pve_fsid = pve.pve_fsid;
505 pve32->pve_pathlen = pve.pve_pathlen;
510 ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
511 struct ptrace_lwpinfo32 *pl32)
514 bzero(pl32, sizeof(*pl32));
515 pl32->pl_lwpid = pl->pl_lwpid;
516 pl32->pl_event = pl->pl_event;
517 pl32->pl_flags = pl->pl_flags;
518 pl32->pl_sigmask = pl->pl_sigmask;
519 pl32->pl_siglist = pl->pl_siglist;
520 siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
521 strcpy(pl32->pl_tdname, pl->pl_tdname);
522 pl32->pl_child_pid = pl->pl_child_pid;
523 pl32->pl_syscall_code = pl->pl_syscall_code;
524 pl32->pl_syscall_narg = pl->pl_syscall_narg;
528 ptrace_sc_ret_to32(const struct ptrace_sc_ret *psr,
529 struct ptrace_sc_ret32 *psr32)
532 bzero(psr32, sizeof(*psr32));
533 psr32->sr_retval[0] = psr->sr_retval[0];
534 psr32->sr_retval[1] = psr->sr_retval[1];
535 psr32->sr_error = psr->sr_error;
537 #endif /* COMPAT_FREEBSD32 */
540 * Process debugging system call.
542 #ifndef _SYS_SYSPROTO_H_
551 #ifdef COMPAT_FREEBSD32
553 * This CPP subterfuge is to try and reduce the number of ifdefs in
554 * the body of the code.
555 * COPYIN(uap->addr, &r.reg, sizeof r.reg);
557 * copyin(uap->addr, &r.reg, sizeof r.reg);
559 * copyin(uap->addr, &r.reg32, sizeof r.reg32);
560 * .. except this is done at runtime.
562 #define BZERO(a, s) wrap32 ? \
563 bzero(a ## 32, s ## 32) : \
565 #define COPYIN(u, k, s) wrap32 ? \
566 copyin(u, k ## 32, s ## 32) : \
568 #define COPYOUT(k, u, s) wrap32 ? \
569 copyout(k ## 32, u, s ## 32) : \
572 #define BZERO(a, s) bzero(a, s)
573 #define COPYIN(u, k, s) copyin(u, k, s)
574 #define COPYOUT(k, u, s) copyout(k, u, s)
577 sys_ptrace(struct thread *td, struct ptrace_args *uap)
580 * XXX this obfuscation is to reduce stack usage, but the register
581 * structs may be too large to put on the stack anyway.
584 struct ptrace_io_desc piod;
585 struct ptrace_lwpinfo pl;
586 struct ptrace_vm_entry pve;
590 #ifdef COMPAT_FREEBSD32
591 struct dbreg32 dbreg32;
592 struct fpreg32 fpreg32;
594 struct ptrace_io_desc32 piod32;
595 struct ptrace_lwpinfo32 pl32;
596 struct ptrace_vm_entry32 pve32;
598 char args[sizeof(td->td_sa.args)];
599 struct ptrace_sc_ret psr;
604 #ifdef COMPAT_FREEBSD32
607 if (SV_CURPROC_FLAG(SV_ILP32))
610 AUDIT_ARG_PID(uap->pid);
611 AUDIT_ARG_CMD(uap->req);
612 AUDIT_ARG_VALUE(uap->data);
615 case PT_GET_EVENT_MASK:
621 BZERO(&r.reg, sizeof r.reg);
624 BZERO(&r.fpreg, sizeof r.fpreg);
627 BZERO(&r.dbreg, sizeof r.dbreg);
630 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
633 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
636 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
638 case PT_SET_EVENT_MASK:
639 if (uap->data != sizeof(r.ptevents))
642 error = copyin(uap->addr, &r.ptevents, uap->data);
645 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
648 error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
657 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
663 error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
666 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
669 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
672 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
675 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
677 case PT_GET_EVENT_MASK:
678 /* NB: The size in uap->data is validated in kern_ptrace(). */
679 error = copyout(&r.ptevents, uap->addr, uap->data);
682 /* NB: The size in uap->data is validated in kern_ptrace(). */
683 error = copyout(&r.pl, uap->addr, uap->data);
686 error = copyout(r.args, uap->addr, MIN(uap->data,
690 error = copyout(&r.psr, uap->addr, MIN(uap->data,
701 #ifdef COMPAT_FREEBSD32
703 * PROC_READ(regs, td2, addr);
705 * proc_read_regs(td2, addr);
707 * proc_read_regs32(td2, addr);
708 * .. except this is done at runtime. There is an additional
709 * complication in that PROC_WRITE disallows 32 bit consumers
710 * from writing to 64 bit address space targets.
712 #define PROC_READ(w, t, a) wrap32 ? \
713 proc_read_ ## w ## 32(t, a) : \
714 proc_read_ ## w (t, a)
715 #define PROC_WRITE(w, t, a) wrap32 ? \
716 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
717 proc_write_ ## w (t, a)
719 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
720 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
724 proc_set_traced(struct proc *p, bool stop)
727 sx_assert(&proctree_lock, SX_XLOCKED);
728 PROC_LOCK_ASSERT(p, MA_OWNED);
729 p->p_flag |= P_TRACED;
731 p->p_flag2 |= P2_PTRACE_FSTP;
732 p->p_ptevents = PTRACE_DEFAULT;
736 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
740 struct proc *curp, *p, *pp;
741 struct thread *td2 = NULL, *td3;
742 struct ptrace_io_desc *piod = NULL;
743 struct ptrace_lwpinfo *pl;
744 struct ptrace_sc_ret *psr;
746 int proctree_locked = 0;
747 lwpid_t tid = 0, *buf;
748 #ifdef COMPAT_FREEBSD32
749 int wrap32 = 0, safe = 0;
750 struct ptrace_io_desc32 *piod32 = NULL;
751 struct ptrace_lwpinfo32 *pl32 = NULL;
752 struct ptrace_sc_ret32 *psr32 = NULL;
754 struct ptrace_lwpinfo pl;
755 struct ptrace_sc_ret psr;
761 /* Lock proctree before locking the process. */
772 case PT_GET_EVENT_MASK:
773 case PT_SET_EVENT_MASK:
776 sx_xlock(&proctree_lock);
783 if (req == PT_TRACE_ME) {
787 if (pid <= PID_MAX) {
788 if ((p = pfind(pid)) == NULL) {
790 sx_xunlock(&proctree_lock);
794 td2 = tdfind(pid, -1);
797 sx_xunlock(&proctree_lock);
805 AUDIT_ARG_PROCESS(p);
807 if ((p->p_flag & P_WEXIT) != 0) {
811 if ((error = p_cansee(td, p)) != 0)
814 if ((error = p_candebug(td, p)) != 0)
818 * System processes can't be debugged.
820 if ((p->p_flag & P_SYSTEM) != 0) {
826 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
827 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
830 td2 = FIRST_THREAD_IN_PROC(p);
835 #ifdef COMPAT_FREEBSD32
837 * Test if we're a 32 bit client and what the target is.
838 * Set the wrap controls accordingly.
840 if (SV_CURPROC_FLAG(SV_ILP32)) {
841 if (SV_PROC_FLAG(td2->td_proc, SV_ILP32))
852 * Always legal, when there is a parent process which
853 * could trace us. Otherwise, reject.
855 if ((p->p_flag & P_TRACED) != 0) {
859 if (p->p_pptr == initproc) {
867 if (p == td->td_proc) {
873 if (p->p_flag & P_TRACED) {
878 /* Can't trace an ancestor if you're being traced. */
879 if (curp->p_flag & P_TRACED) {
880 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
893 /* Allow thread to clear single step for itself */
894 if (td->td_tid == tid)
899 /* not being traced... */
900 if ((p->p_flag & P_TRACED) == 0) {
905 /* not being traced by YOU */
906 if (p->p_pptr != td->td_proc) {
911 /* not currently stopped */
912 if ((p->p_flag & P_STOPPED_TRACE) == 0 ||
913 p->p_suspcount != p->p_numthreads ||
914 (p->p_flag & P_WAITED) == 0) {
923 /* Keep this process around until we finish this request. */
928 * Single step fixup ala procfs
934 * Actually do the requests
937 td->td_retval[0] = 0;
941 /* set my trace flag and "owner" so it can read/write me */
942 proc_set_traced(p, false);
943 if (p->p_flag & P_PPWAIT)
944 p->p_flag |= P_PPTRACE;
945 CTR1(KTR_PTRACE, "PT_TRACE_ME: pid %d", p->p_pid);
949 /* security check done above */
951 * It would be nice if the tracing relationship was separate
952 * from the parent relationship but that would require
953 * another set of links in the proc struct or for "wait"
954 * to scan the entire proc table. To make life easier,
955 * we just re-parent the process we're trying to trace.
956 * The old parent is remembered so we can put things back
959 proc_set_traced(p, true);
960 if (p->p_pptr != td->td_proc) {
961 proc_reparent(p, td->td_proc, false);
963 CTR2(KTR_PTRACE, "PT_ATTACH: pid %d, oppid %d", p->p_pid,
966 sx_xunlock(&proctree_lock);
968 MPASS(p->p_xthread == NULL);
969 MPASS((p->p_flag & P_STOPPED_TRACE) == 0);
972 * If already stopped due to a stop signal, clear the
973 * existing stop before triggering a traced SIGSTOP.
975 if ((p->p_flag & P_STOPPED_SIG) != 0) {
977 p->p_flag &= ~(P_STOPPED_SIG | P_WAITED);
982 kern_psignal(p, SIGSTOP);
986 CTR2(KTR_PTRACE, "PT_CLEARSTEP: tid %d (pid %d)", td2->td_tid,
988 error = ptrace_clear_single_step(td2);
992 CTR2(KTR_PTRACE, "PT_SETSTEP: tid %d (pid %d)", td2->td_tid,
994 error = ptrace_single_step(td2);
998 CTR2(KTR_PTRACE, "PT_SUSPEND: tid %d (pid %d)", td2->td_tid,
1000 td2->td_dbgflags |= TDB_SUSPEND;
1002 td2->td_flags |= TDF_NEEDSUSPCHK;
1007 CTR2(KTR_PTRACE, "PT_RESUME: tid %d (pid %d)", td2->td_tid,
1009 td2->td_dbgflags &= ~TDB_SUSPEND;
1012 case PT_FOLLOW_FORK:
1013 CTR3(KTR_PTRACE, "PT_FOLLOW_FORK: pid %d %s -> %s", p->p_pid,
1014 p->p_ptevents & PTRACE_FORK ? "enabled" : "disabled",
1015 data ? "enabled" : "disabled");
1017 p->p_ptevents |= PTRACE_FORK;
1019 p->p_ptevents &= ~PTRACE_FORK;
1023 CTR3(KTR_PTRACE, "PT_LWP_EVENTS: pid %d %s -> %s", p->p_pid,
1024 p->p_ptevents & PTRACE_LWP ? "enabled" : "disabled",
1025 data ? "enabled" : "disabled");
1027 p->p_ptevents |= PTRACE_LWP;
1029 p->p_ptevents &= ~PTRACE_LWP;
1032 case PT_GET_EVENT_MASK:
1033 if (data != sizeof(p->p_ptevents)) {
1037 CTR2(KTR_PTRACE, "PT_GET_EVENT_MASK: pid %d mask %#x", p->p_pid,
1039 *(int *)addr = p->p_ptevents;
1042 case PT_SET_EVENT_MASK:
1043 if (data != sizeof(p->p_ptevents)) {
1048 if ((tmp & ~(PTRACE_EXEC | PTRACE_SCE | PTRACE_SCX |
1049 PTRACE_FORK | PTRACE_LWP | PTRACE_VFORK)) != 0) {
1053 CTR3(KTR_PTRACE, "PT_SET_EVENT_MASK: pid %d mask %#x -> %#x",
1054 p->p_pid, p->p_ptevents, tmp);
1055 p->p_ptevents = tmp;
1058 case PT_GET_SC_ARGS:
1059 CTR1(KTR_PTRACE, "PT_GET_SC_ARGS: pid %d", p->p_pid);
1060 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) == 0
1061 #ifdef COMPAT_FREEBSD32
1062 || (wrap32 && !safe)
1068 bzero(addr, sizeof(td2->td_sa.args));
1069 #ifdef COMPAT_FREEBSD32
1071 for (num = 0; num < nitems(td2->td_sa.args); num++)
1072 ((uint32_t *)addr)[num] = (uint32_t)
1073 td2->td_sa.args[num];
1076 bcopy(td2->td_sa.args, addr, td2->td_sa.narg *
1077 sizeof(register_t));
1081 if ((td2->td_dbgflags & (TDB_SCX)) == 0
1082 #ifdef COMPAT_FREEBSD32
1083 || (wrap32 && !safe)
1089 #ifdef COMPAT_FREEBSD32
1096 bzero(psr, sizeof(*psr));
1097 psr->sr_error = td2->td_errno;
1098 if (psr->sr_error == 0) {
1099 psr->sr_retval[0] = td2->td_retval[0];
1100 psr->sr_retval[1] = td2->td_retval[1];
1102 #ifdef COMPAT_FREEBSD32
1104 ptrace_sc_ret_to32(psr, psr32);
1107 "PT_GET_SC_RET: pid %d error %d retval %#lx,%#lx",
1108 p->p_pid, psr->sr_error, psr->sr_retval[0],
1118 /* Zero means do not send any signal */
1119 if (data < 0 || data > _SIG_MAXSIG) {
1126 CTR3(KTR_PTRACE, "PT_STEP: tid %d (pid %d), sig = %d",
1127 td2->td_tid, p->p_pid, data);
1128 error = ptrace_single_step(td2);
1136 if (addr != (void *)1) {
1137 error = ptrace_set_pc(td2,
1138 (u_long)(uintfptr_t)addr);
1144 p->p_ptevents |= PTRACE_SCE;
1146 "PT_TO_SCE: pid %d, events = %#x, PC = %#lx, sig = %d",
1147 p->p_pid, p->p_ptevents,
1148 (u_long)(uintfptr_t)addr, data);
1151 p->p_ptevents |= PTRACE_SCX;
1153 "PT_TO_SCX: pid %d, events = %#x, PC = %#lx, sig = %d",
1154 p->p_pid, p->p_ptevents,
1155 (u_long)(uintfptr_t)addr, data);
1158 p->p_ptevents |= PTRACE_SYSCALL;
1160 "PT_SYSCALL: pid %d, events = %#x, PC = %#lx, sig = %d",
1161 p->p_pid, p->p_ptevents,
1162 (u_long)(uintfptr_t)addr, data);
1166 "PT_CONTINUE: pid %d, PC = %#lx, sig = %d",
1167 p->p_pid, (u_long)(uintfptr_t)addr, data);
1173 * Reset the process parent.
1175 * NB: This clears P_TRACED before reparenting
1176 * a detached process back to its original
1177 * parent. Otherwise the debugee will be set
1178 * as an orphan of the debugger.
1180 p->p_flag &= ~(P_TRACED | P_WAITED);
1181 if (p->p_oppid != p->p_pptr->p_pid) {
1182 PROC_LOCK(p->p_pptr);
1183 sigqueue_take(p->p_ksi);
1184 PROC_UNLOCK(p->p_pptr);
1186 pp = proc_realparent(p);
1187 proc_reparent(p, pp, false);
1189 p->p_sigparent = SIGCHLD;
1191 "PT_DETACH: pid %d reparented to pid %d, sig %d",
1192 p->p_pid, pp->p_pid, data);
1194 CTR2(KTR_PTRACE, "PT_DETACH: pid %d, sig %d",
1197 FOREACH_THREAD_IN_PROC(p, td3) {
1198 if ((td3->td_dbgflags & TDB_FSTP) != 0) {
1199 sigqueue_delete(&td3->td_sigqueue,
1202 td3->td_dbgflags &= ~(TDB_XSIG | TDB_FSTP |
1206 if ((p->p_flag2 & P2_PTRACE_FSTP) != 0) {
1207 sigqueue_delete(&p->p_sigqueue, SIGSTOP);
1208 p->p_flag2 &= ~P2_PTRACE_FSTP;
1211 /* should we send SIGCHLD? */
1212 /* childproc_continued(p); */
1216 sx_xunlock(&proctree_lock);
1217 proctree_locked = 0;
1220 MPASS(proctree_locked == 0);
1223 * Clear the pending event for the thread that just
1224 * reported its event (p_xthread). This may not be
1225 * the thread passed to PT_CONTINUE, PT_STEP, etc. if
1226 * the debugger is resuming a different thread.
1228 * Deliver any pending signal via the reporting thread.
1230 MPASS(p->p_xthread != NULL);
1231 p->p_xthread->td_dbgflags &= ~TDB_XSIG;
1232 p->p_xthread->td_xsig = data;
1233 p->p_xthread = NULL;
1237 * P_WKILLED is insurance that a PT_KILL/SIGKILL
1238 * always works immediately, even if another thread is
1239 * unsuspended first and attempts to handle a
1240 * different signal or if the POSIX.1b style signal
1241 * queue cannot accommodate any new signals.
1243 if (data == SIGKILL)
1247 * Unsuspend all threads. To leave a thread
1248 * suspended, use PT_SUSPEND to suspend it before
1249 * continuing the process.
1252 p->p_flag &= ~(P_STOPPED_TRACE | P_STOPPED_SIG | P_WAITED);
1253 thread_unsuspend(p);
1259 td2->td_dbgflags |= TDB_USERWR;
1262 if (proc_writemem(td, p, (off_t)(uintptr_t)addr, &data,
1263 sizeof(int)) != sizeof(int))
1266 CTR3(KTR_PTRACE, "PT_WRITE: pid %d: %p <= %#x",
1267 p->p_pid, addr, data);
1275 if (proc_readmem(td, p, (off_t)(uintptr_t)addr, &tmp,
1276 sizeof(int)) != sizeof(int))
1279 CTR3(KTR_PTRACE, "PT_READ: pid %d: %p >= %#x",
1280 p->p_pid, addr, tmp);
1281 td->td_retval[0] = tmp;
1286 #ifdef COMPAT_FREEBSD32
1289 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
1290 iov.iov_len = piod32->piod_len;
1291 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
1292 uio.uio_resid = piod32->piod_len;
1297 iov.iov_base = piod->piod_addr;
1298 iov.iov_len = piod->piod_len;
1299 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
1300 uio.uio_resid = piod->piod_len;
1304 uio.uio_segflg = UIO_USERSPACE;
1306 #ifdef COMPAT_FREEBSD32
1307 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
1309 tmp = piod->piod_op;
1314 CTR3(KTR_PTRACE, "PT_IO: pid %d: READ (%p, %#x)",
1315 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1316 uio.uio_rw = UIO_READ;
1320 CTR3(KTR_PTRACE, "PT_IO: pid %d: WRITE (%p, %#x)",
1321 p->p_pid, (uintptr_t)uio.uio_offset, uio.uio_resid);
1322 td2->td_dbgflags |= TDB_USERWR;
1323 uio.uio_rw = UIO_WRITE;
1330 error = proc_rwmem(p, &uio);
1331 #ifdef COMPAT_FREEBSD32
1333 piod32->piod_len -= uio.uio_resid;
1336 piod->piod_len -= uio.uio_resid;
1341 CTR1(KTR_PTRACE, "PT_KILL: pid %d", p->p_pid);
1343 goto sendsig; /* in PT_CONTINUE above */
1346 CTR2(KTR_PTRACE, "PT_SETREGS: tid %d (pid %d)", td2->td_tid,
1348 td2->td_dbgflags |= TDB_USERWR;
1349 error = PROC_WRITE(regs, td2, addr);
1353 CTR2(KTR_PTRACE, "PT_GETREGS: tid %d (pid %d)", td2->td_tid,
1355 error = PROC_READ(regs, td2, addr);
1359 CTR2(KTR_PTRACE, "PT_SETFPREGS: tid %d (pid %d)", td2->td_tid,
1361 td2->td_dbgflags |= TDB_USERWR;
1362 error = PROC_WRITE(fpregs, td2, addr);
1366 CTR2(KTR_PTRACE, "PT_GETFPREGS: tid %d (pid %d)", td2->td_tid,
1368 error = PROC_READ(fpregs, td2, addr);
1372 CTR2(KTR_PTRACE, "PT_SETDBREGS: tid %d (pid %d)", td2->td_tid,
1374 td2->td_dbgflags |= TDB_USERWR;
1375 error = PROC_WRITE(dbregs, td2, addr);
1379 CTR2(KTR_PTRACE, "PT_GETDBREGS: tid %d (pid %d)", td2->td_tid,
1381 error = PROC_READ(dbregs, td2, addr);
1386 #ifdef COMPAT_FREEBSD32
1387 (!wrap32 && data > sizeof(*pl)) ||
1388 (wrap32 && data > sizeof(*pl32))) {
1390 data > sizeof(*pl)) {
1395 #ifdef COMPAT_FREEBSD32
1402 bzero(pl, sizeof(*pl));
1403 pl->pl_lwpid = td2->td_tid;
1404 pl->pl_event = PL_EVENT_NONE;
1406 if (td2->td_dbgflags & TDB_XSIG) {
1407 pl->pl_event = PL_EVENT_SIGNAL;
1408 if (td2->td_si.si_signo != 0 &&
1409 #ifdef COMPAT_FREEBSD32
1410 ((!wrap32 && data >= offsetof(struct ptrace_lwpinfo,
1411 pl_siginfo) + sizeof(pl->pl_siginfo)) ||
1412 (wrap32 && data >= offsetof(struct ptrace_lwpinfo32,
1413 pl_siginfo) + sizeof(struct siginfo32)))
1415 data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
1416 + sizeof(pl->pl_siginfo)
1419 pl->pl_flags |= PL_FLAG_SI;
1420 pl->pl_siginfo = td2->td_si;
1423 if (td2->td_dbgflags & TDB_SCE)
1424 pl->pl_flags |= PL_FLAG_SCE;
1425 else if (td2->td_dbgflags & TDB_SCX)
1426 pl->pl_flags |= PL_FLAG_SCX;
1427 if (td2->td_dbgflags & TDB_EXEC)
1428 pl->pl_flags |= PL_FLAG_EXEC;
1429 if (td2->td_dbgflags & TDB_FORK) {
1430 pl->pl_flags |= PL_FLAG_FORKED;
1431 pl->pl_child_pid = td2->td_dbg_forked;
1432 if (td2->td_dbgflags & TDB_VFORK)
1433 pl->pl_flags |= PL_FLAG_VFORKED;
1434 } else if ((td2->td_dbgflags & (TDB_SCX | TDB_VFORK)) ==
1436 pl->pl_flags |= PL_FLAG_VFORK_DONE;
1437 if (td2->td_dbgflags & TDB_CHILD)
1438 pl->pl_flags |= PL_FLAG_CHILD;
1439 if (td2->td_dbgflags & TDB_BORN)
1440 pl->pl_flags |= PL_FLAG_BORN;
1441 if (td2->td_dbgflags & TDB_EXIT)
1442 pl->pl_flags |= PL_FLAG_EXITED;
1443 pl->pl_sigmask = td2->td_sigmask;
1444 pl->pl_siglist = td2->td_siglist;
1445 strcpy(pl->pl_tdname, td2->td_name);
1446 if ((td2->td_dbgflags & (TDB_SCE | TDB_SCX)) != 0) {
1447 pl->pl_syscall_code = td2->td_sa.code;
1448 pl->pl_syscall_narg = td2->td_sa.narg;
1450 pl->pl_syscall_code = 0;
1451 pl->pl_syscall_narg = 0;
1453 #ifdef COMPAT_FREEBSD32
1455 ptrace_lwpinfo_to32(pl, pl32);
1458 "PT_LWPINFO: tid %d (pid %d) event %d flags %#x child pid %d syscall %d",
1459 td2->td_tid, p->p_pid, pl->pl_event, pl->pl_flags,
1460 pl->pl_child_pid, pl->pl_syscall_code);
1464 CTR2(KTR_PTRACE, "PT_GETNUMLWPS: pid %d: %d threads", p->p_pid,
1466 td->td_retval[0] = p->p_numthreads;
1470 CTR3(KTR_PTRACE, "PT_GETLWPLIST: pid %d: data %d, actual %d",
1471 p->p_pid, data, p->p_numthreads);
1476 num = imin(p->p_numthreads, data);
1478 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1481 FOREACH_THREAD_IN_PROC(p, td2) {
1484 buf[tmp++] = td2->td_tid;
1487 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1490 td->td_retval[0] = tmp;
1494 case PT_VM_TIMESTAMP:
1495 CTR2(KTR_PTRACE, "PT_VM_TIMESTAMP: pid %d: timestamp %d",
1496 p->p_pid, p->p_vmspace->vm_map.timestamp);
1497 td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
1502 #ifdef COMPAT_FREEBSD32
1504 error = ptrace_vm_entry32(td, p, addr);
1507 error = ptrace_vm_entry(td, p, addr);
1512 #ifdef __HAVE_PTRACE_MACHDEP
1513 if (req >= PT_FIRSTMACH) {
1515 error = cpu_ptrace(td2, req, addr, data);
1519 /* Unknown request. */
1525 /* Drop our hold on this process now that the request has completed. */
1529 if (proctree_locked)
1530 sx_xunlock(&proctree_lock);
1537 * Stop a process because of a debugging event;
1538 * stay stopped until p->p_step is cleared
1539 * (cleared by PIOCCONT in procfs).
1542 stopevent(struct proc *p, unsigned int event, unsigned int val)
1545 PROC_LOCK_ASSERT(p, MA_OWNED);
1547 CTR3(KTR_PTRACE, "stopevent: pid %d event %u val %u", p->p_pid, event,
1550 if (event != S_EXIT)
1552 p->p_xthread = NULL;
1553 p->p_stype = event; /* Which event caused the stop? */
1554 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
1555 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1556 } while (p->p_step);