2 * Copyright (c) 1994, Sean Eric Fagan
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Sean Eric Fagan.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "opt_compat.h"
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <sys/mutex.h>
41 #include <sys/syscallsubr.h>
42 #include <sys/sysproto.h>
44 #include <sys/vnode.h>
45 #include <sys/ptrace.h>
47 #include <sys/malloc.h>
48 #include <sys/signalvar.h>
50 #include <machine/reg.h>
52 #include <security/audit/audit.h>
56 #include <vm/vm_extern.h>
57 #include <vm/vm_map.h>
58 #include <vm/vm_kern.h>
59 #include <vm/vm_object.h>
60 #include <vm/vm_page.h>
63 #include <sys/procfs.h>
64 #include <machine/fpu.h>
65 #include <compat/ia32/ia32_reg.h>
67 extern struct sysentvec ia32_freebsd_sysvec;
69 struct ptrace_io_desc32 {
78 * Functions implemented using PROC_ACTION():
80 * proc_read_regs(proc, regs)
81 * Get the current user-visible register set from the process
82 * and copy it into the regs structure (<machine/reg.h>).
83 * The process is stopped at the time read_regs is called.
85 * proc_write_regs(proc, regs)
86 * Update the current register set from the passed in regs
87 * structure. Take care to avoid clobbering special CPU
88 * registers or privileged bits in the PSL.
89 * Depending on the architecture this may have fix-up work to do,
90 * especially if the IAR or PCW are modified.
91 * The process is stopped at the time write_regs is called.
93 * proc_read_fpregs, proc_write_fpregs
94 * deal with the floating point register set, otherwise as above.
96 * proc_read_dbregs, proc_write_dbregs
97 * deal with the processor debug register set, otherwise as above.
100 * Arrange for the process to trap after executing a single instruction.
103 #define PROC_ACTION(action) do { \
106 PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
107 if ((td->td_proc->p_sflag & PS_INMEM) == 0) \
115 proc_read_regs(struct thread *td, struct reg *regs)
118 PROC_ACTION(fill_regs(td, regs));
122 proc_write_regs(struct thread *td, struct reg *regs)
125 PROC_ACTION(set_regs(td, regs));
129 proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
132 PROC_ACTION(fill_dbregs(td, dbregs));
136 proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
139 PROC_ACTION(set_dbregs(td, dbregs));
143 * Ptrace doesn't support fpregs at all, and there are no security holes
144 * or translations for fpregs, so we can just copy them.
147 proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
150 PROC_ACTION(fill_fpregs(td, fpregs));
154 proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
157 PROC_ACTION(set_fpregs(td, fpregs));
161 /* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
163 proc_read_regs32(struct thread *td, struct reg32 *regs32)
166 PROC_ACTION(fill_regs32(td, regs32));
170 proc_write_regs32(struct thread *td, struct reg32 *regs32)
173 PROC_ACTION(set_regs32(td, regs32));
177 proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
180 PROC_ACTION(fill_dbregs32(td, dbregs32));
184 proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
187 PROC_ACTION(set_dbregs32(td, dbregs32));
191 proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
194 PROC_ACTION(fill_fpregs32(td, fpregs32));
198 proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
201 PROC_ACTION(set_fpregs32(td, fpregs32));
206 proc_sstep(struct thread *td)
209 PROC_ACTION(ptrace_single_step(td));
213 proc_rwmem(struct proc *p, struct uio *uio)
217 vm_object_t backing_object, object = NULL;
218 vm_offset_t pageno = 0; /* page number */
220 int error, refcnt, writing;
223 * if the vmspace is in the midst of being deallocated or the
224 * process is exiting, don't try to grab anything. The page table
225 * usage in that process can be messed up.
228 if ((p->p_flag & P_WEXIT))
231 if ((refcnt = vm->vm_refcnt) < 1)
233 } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
240 writing = uio->uio_rw == UIO_WRITE;
241 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
245 * Only map in one page at a time. We don't have to, but it
246 * makes things easier. This way is trivial - right?
251 int page_offset; /* offset into page */
252 vm_map_entry_t out_entry;
261 uva = (vm_offset_t)uio->uio_offset;
264 * Get the page number of this segment.
266 pageno = trunc_page(uva);
267 page_offset = uva - pageno;
270 * How many bytes to copy
272 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
275 * Fault the page on behalf of the process
277 error = vm_fault(map, pageno, reqprot, VM_FAULT_NORMAL);
284 * Now we need to get the page. out_entry, out_prot, wired,
285 * and single_use aren't used. One would think the vm code
286 * would be a *bit* nicer... We use tmap because
287 * vm_map_lookup() can change the map argument.
290 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
291 &object, &pindex, &out_prot, &wired);
296 VM_OBJECT_LOCK(object);
297 while ((m = vm_page_lookup(object, pindex)) == NULL &&
299 (backing_object = object->backing_object) != NULL) {
301 * Allow fallback to backing objects if we are reading.
303 VM_OBJECT_LOCK(backing_object);
304 pindex += OFF_TO_IDX(object->backing_object_offset);
305 VM_OBJECT_UNLOCK(object);
306 object = backing_object;
308 VM_OBJECT_UNLOCK(object);
310 vm_map_lookup_done(tmap, out_entry);
316 * Hold the page in memory.
318 vm_page_lock_queues();
320 vm_page_unlock_queues();
323 * We're done with tmap now.
325 vm_map_lookup_done(tmap, out_entry);
328 * Now do the i/o move.
330 error = uiomove_fromphys(&m, page_offset, len, uio);
335 vm_page_lock_queues();
337 vm_page_unlock_queues();
339 } while (error == 0 && uio->uio_resid > 0);
346 * Process debugging system call.
348 #ifndef _SYS_SYSPROTO_H_
359 * This CPP subterfuge is to try and reduce the number of ifdefs in
360 * the body of the code.
361 * COPYIN(uap->addr, &r.reg, sizeof r.reg);
363 * copyin(uap->addr, &r.reg, sizeof r.reg);
365 * copyin(uap->addr, &r.reg32, sizeof r.reg32);
366 * .. except this is done at runtime.
368 #define COPYIN(u, k, s) wrap32 ? \
369 copyin(u, k ## 32, s ## 32) : \
371 #define COPYOUT(k, u, s) wrap32 ? \
372 copyout(k ## 32, u, s ## 32) : \
375 #define COPYIN(u, k, s) copyin(u, k, s)
376 #define COPYOUT(k, u, s) copyout(k, u, s)
382 ptrace(struct thread *td, struct ptrace_args *uap)
385 * XXX this obfuscation is to reduce stack usage, but the register
386 * structs may be too large to put on the stack anyway.
389 struct ptrace_io_desc piod;
390 struct ptrace_lwpinfo pl;
395 struct dbreg32 dbreg32;
396 struct fpreg32 fpreg32;
398 struct ptrace_io_desc32 piod32;
406 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec)
409 AUDIT_ARG(pid, uap->pid);
410 AUDIT_ARG(cmd, uap->req);
411 AUDIT_ARG(addr, uap->addr);
412 AUDIT_ARG(value, uap->data);
421 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
424 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
427 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
430 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
439 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
445 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
448 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
451 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
454 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
457 error = copyout(&r.pl, uap->addr, uap->data);
468 * PROC_READ(regs, td2, addr);
470 * proc_read_regs(td2, addr);
472 * proc_read_regs32(td2, addr);
473 * .. except this is done at runtime. There is an additional
474 * complication in that PROC_WRITE disallows 32 bit consumers
475 * from writing to 64 bit address space targets.
477 #define PROC_READ(w, t, a) wrap32 ? \
478 proc_read_ ## w ## 32(t, a) : \
479 proc_read_ ## w (t, a)
480 #define PROC_WRITE(w, t, a) wrap32 ? \
481 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
482 proc_write_ ## w (t, a)
484 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
485 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
489 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
493 struct proc *curp, *p, *pp;
494 struct thread *td2 = NULL;
495 struct ptrace_io_desc *piod = NULL;
496 struct ptrace_lwpinfo *pl;
497 int error, write, tmp, num;
498 int proctree_locked = 0;
499 lwpid_t tid = 0, *buf;
501 int wrap32 = 0, safe = 0;
502 struct ptrace_io_desc32 *piod32 = NULL;
507 /* Lock proctree before locking the process. */
517 sx_xlock(&proctree_lock);
525 if (req == PT_TRACE_ME) {
529 if (pid <= PID_MAX) {
530 if ((p = pfind(pid)) == NULL) {
532 sx_xunlock(&proctree_lock);
536 /* this is slow, should be optimized */
537 sx_slock(&allproc_lock);
538 FOREACH_PROC_IN_SYSTEM(p) {
540 mtx_lock_spin(&sched_lock);
541 FOREACH_THREAD_IN_PROC(p, td2) {
542 if (td2->td_tid == pid)
545 mtx_unlock_spin(&sched_lock);
547 break; /* proc lock held */
550 sx_sunlock(&allproc_lock);
553 sx_xunlock(&proctree_lock);
560 AUDIT_ARG(process, p);
561 if ((error = p_cansee(td, p)) != 0)
564 if ((error = p_candebug(td, p)) != 0)
568 * System processes can't be debugged.
570 if ((p->p_flag & P_SYSTEM) != 0) {
576 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
577 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
580 td2 = FIRST_THREAD_IN_PROC(p);
587 * Test if we're a 32 bit client and what the target is.
588 * Set the wrap controls accordingly.
590 if (td->td_proc->p_sysent == &ia32_freebsd_sysvec) {
591 if (td2->td_proc->p_sysent == &ia32_freebsd_sysvec)
606 if (p->p_pid == td->td_proc->p_pid) {
612 if (p->p_flag & P_TRACED) {
617 /* Can't trace an ancestor if you're being traced. */
618 if (curp->p_flag & P_TRACED) {
619 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
632 /* Allow thread to clear single step for itself */
633 if (td->td_tid == tid)
638 /* not being traced... */
639 if ((p->p_flag & P_TRACED) == 0) {
644 /* not being traced by YOU */
645 if (p->p_pptr != td->td_proc) {
650 /* not currently stopped */
651 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
652 p->p_suspcount != p->p_numthreads ||
653 (p->p_flag & P_WAITED) == 0) {
658 if ((p->p_flag & P_STOPPED_TRACE) == 0) {
659 static int count = 0;
661 printf("P_STOPPED_TRACE not set.\n");
670 * Single step fixup ala procfs
672 FIX_SSTEP(td2); /* XXXKSE */
676 * Actually do the requests
679 td->td_retval[0] = 0;
683 /* set my trace flag and "owner" so it can read/write me */
684 p->p_flag |= P_TRACED;
685 p->p_oppid = p->p_pptr->p_pid;
687 sx_xunlock(&proctree_lock);
691 /* security check done above */
692 p->p_flag |= P_TRACED;
693 p->p_oppid = p->p_pptr->p_pid;
694 if (p->p_pptr != td->td_proc) {
695 PROC_LOCK(p->p_pptr);
696 sigqueue_take(p->p_ksi);
697 PROC_UNLOCK(p->p_pptr);
698 proc_reparent(p, td->td_proc);
701 goto sendsig; /* in PT_CONTINUE below */
705 error = ptrace_clear_single_step(td2);
714 error = ptrace_single_step(td2);
723 mtx_lock_spin(&sched_lock);
724 td2->td_flags |= TDF_DBSUSPEND;
725 mtx_unlock_spin(&sched_lock);
732 mtx_lock_spin(&sched_lock);
733 td2->td_flags &= ~TDF_DBSUSPEND;
734 mtx_unlock_spin(&sched_lock);
745 /* Zero means do not send any signal */
746 if (data < 0 || data > _SIG_MAXSIG) {
756 error = ptrace_single_step(td2);
764 p->p_stops |= S_PT_SCE;
767 p->p_stops |= S_PT_SCX;
770 p->p_stops |= S_PT_SCE | S_PT_SCX;
774 if (addr != (void *)1) {
776 error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr);
785 if (req == PT_DETACH) {
786 /* reset process parent */
787 if (p->p_oppid != p->p_pptr->p_pid) {
790 PROC_LOCK(p->p_pptr);
791 sigqueue_take(p->p_ksi);
792 PROC_UNLOCK(p->p_pptr);
795 pp = pfind(p->p_oppid);
801 proc_reparent(p, pp);
803 p->p_sigparent = SIGCHLD;
805 p->p_flag &= ~(P_TRACED | P_WAITED);
808 /* should we send SIGCHLD? */
809 /* childproc_continued(p); */
814 sx_xunlock(&proctree_lock);
815 /* deliver or queue signal */
816 mtx_lock_spin(&sched_lock);
817 td2->td_flags &= ~TDF_XSIG;
818 mtx_unlock_spin(&sched_lock);
822 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
823 mtx_lock_spin(&sched_lock);
824 if (req == PT_DETACH) {
826 FOREACH_THREAD_IN_PROC(p, td3)
827 td3->td_flags &= ~TDF_DBSUSPEND;
830 * unsuspend all threads, to not let a thread run,
831 * you should use PT_SUSPEND to suspend it before
832 * continuing process.
834 mtx_unlock_spin(&sched_lock);
836 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
837 mtx_lock_spin(&sched_lock);
839 mtx_unlock_spin(&sched_lock);
856 /* write = 0 set above */
857 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
858 iov.iov_len = sizeof(int);
861 uio.uio_offset = (off_t)(uintptr_t)addr;
862 uio.uio_resid = sizeof(int);
863 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */
864 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
866 error = proc_rwmem(p, &uio);
867 if (uio.uio_resid != 0) {
869 * XXX proc_rwmem() doesn't currently return ENOSPC,
870 * so I think write() can bogusly return 0.
871 * XXX what happens for short writes? We don't want
872 * to write partial data.
873 * XXX proc_rwmem() returns EPERM for other invalid
874 * addresses. Convert this to EINVAL. Does this
875 * clobber returns of EPERM for other reasons?
877 if (error == 0 || error == ENOSPC || error == EPERM)
878 error = EINVAL; /* EOF */
881 td->td_retval[0] = tmp;
889 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
890 iov.iov_len = piod32->piod_len;
891 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
892 uio.uio_resid = piod32->piod_len;
897 iov.iov_base = piod->piod_addr;
898 iov.iov_len = piod->piod_len;
899 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
900 uio.uio_resid = piod->piod_len;
904 uio.uio_segflg = UIO_USERSPACE;
907 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
914 uio.uio_rw = UIO_READ;
918 uio.uio_rw = UIO_WRITE;
923 error = proc_rwmem(p, &uio);
926 piod32->piod_len -= uio.uio_resid;
929 piod->piod_len -= uio.uio_resid;
934 goto sendsig; /* in PT_CONTINUE above */
938 error = PROC_WRITE(regs, td2, addr);
945 error = PROC_READ(regs, td2, addr);
952 error = PROC_WRITE(fpregs, td2, addr);
959 error = PROC_READ(fpregs, td2, addr);
966 error = PROC_WRITE(dbregs, td2, addr);
973 error = PROC_READ(dbregs, td2, addr);
979 if (data == 0 || data > sizeof(*pl))
983 pl->pl_lwpid = td2->td_tid;
984 if (td2->td_flags & TDF_XSIG)
985 pl->pl_event = PL_EVENT_SIGNAL;
988 if (td2->td_pflags & TDP_SA) {
989 pl->pl_flags = PL_FLAG_SA;
990 if (td2->td_upcall && !TD_CAN_UNBIND(td2))
991 pl->pl_flags |= PL_FLAG_BOUND;
995 pl->pl_sigmask = td2->td_sigmask;
996 pl->pl_siglist = td2->td_siglist;
1002 td->td_retval[0] = p->p_numthreads;
1011 num = imin(p->p_numthreads, data);
1013 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
1016 mtx_lock_spin(&sched_lock);
1017 FOREACH_THREAD_IN_PROC(p, td2) {
1020 buf[tmp++] = td2->td_tid;
1022 mtx_unlock_spin(&sched_lock);
1024 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
1027 td->td_retval[0] = num;
1031 #ifdef __HAVE_PTRACE_MACHDEP
1032 if (req >= PT_FIRSTMACH) {
1035 error = cpu_ptrace(td2, req, addr, data);
1043 /* Unknown request. */
1049 if (proctree_locked)
1050 sx_xunlock(&proctree_lock);
1057 * Stop a process because of a debugging event;
1058 * stay stopped until p->p_step is cleared
1059 * (cleared by PIOCCONT in procfs).
1062 stopevent(struct proc *p, unsigned int event, unsigned int val)
1065 PROC_LOCK_ASSERT(p, MA_OWNED);
1069 p->p_xthread = NULL;
1070 p->p_stype = event; /* Which event caused the stop? */
1071 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
1072 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1073 } while (p->p_step);