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/sysent.h>
43 #include <sys/sysproto.h>
45 #include <sys/vnode.h>
46 #include <sys/ptrace.h>
48 #include <sys/malloc.h>
49 #include <sys/signalvar.h>
51 #include <machine/reg.h>
53 #include <security/audit/audit.h>
57 #include <vm/vm_extern.h>
58 #include <vm/vm_map.h>
59 #include <vm/vm_kern.h>
60 #include <vm/vm_object.h>
61 #include <vm/vm_page.h>
62 #include <vm/vm_param.h>
65 #include <sys/procfs.h>
66 #include <machine/fpu.h>
67 #include <compat/ia32/ia32_reg.h>
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_flag & P_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)
216 vm_object_t backing_object, object = NULL;
217 vm_offset_t pageno = 0; /* page number */
219 int error, fault_flags, writing;
222 * Assert that someone has locked this vmspace. (Should be
223 * curthread but we can't assert that.) This keeps the process
224 * from exiting out from under us until this operation completes.
226 KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
232 map = &p->p_vmspace->vm_map;
234 writing = uio->uio_rw == UIO_WRITE;
235 reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
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?
246 int page_offset; /* offset into page */
247 vm_map_entry_t out_entry;
256 uva = (vm_offset_t)uio->uio_offset;
259 * Get the page number of this segment.
261 pageno = trunc_page(uva);
262 page_offset = uva - pageno;
265 * How many bytes to copy
267 len = min(PAGE_SIZE - page_offset, uio->uio_resid);
270 * Fault the page on behalf of the process
272 error = vm_fault(map, pageno, reqprot, fault_flags);
274 if (error == KERN_RESOURCE_SHORTAGE)
282 * Now we need to get the page. out_entry, out_prot, wired,
283 * and single_use aren't used. One would think the vm code
284 * would be a *bit* nicer... We use tmap because
285 * vm_map_lookup() can change the map argument.
288 error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
289 &object, &pindex, &out_prot, &wired);
294 VM_OBJECT_LOCK(object);
295 while ((m = vm_page_lookup(object, pindex)) == NULL &&
297 (backing_object = object->backing_object) != NULL) {
299 * Allow fallback to backing objects if we are reading.
301 VM_OBJECT_LOCK(backing_object);
302 pindex += OFF_TO_IDX(object->backing_object_offset);
303 VM_OBJECT_UNLOCK(object);
304 object = backing_object;
306 VM_OBJECT_UNLOCK(object);
308 vm_map_lookup_done(tmap, out_entry);
314 * Hold the page in memory.
316 vm_page_lock_queues();
318 vm_page_unlock_queues();
321 * We're done with tmap now.
323 vm_map_lookup_done(tmap, out_entry);
326 * Now do the i/o move.
328 error = uiomove_fromphys(&m, page_offset, len, uio);
333 vm_page_lock_queues();
335 vm_page_unlock_queues();
337 } while (error == 0 && uio->uio_resid > 0);
343 * Process debugging system call.
345 #ifndef _SYS_SYSPROTO_H_
356 * This CPP subterfuge is to try and reduce the number of ifdefs in
357 * the body of the code.
358 * COPYIN(uap->addr, &r.reg, sizeof r.reg);
360 * copyin(uap->addr, &r.reg, sizeof r.reg);
362 * copyin(uap->addr, &r.reg32, sizeof r.reg32);
363 * .. except this is done at runtime.
365 #define COPYIN(u, k, s) wrap32 ? \
366 copyin(u, k ## 32, s ## 32) : \
368 #define COPYOUT(k, u, s) wrap32 ? \
369 copyout(k ## 32, u, s ## 32) : \
372 #define COPYIN(u, k, s) copyin(u, k, s)
373 #define COPYOUT(k, u, s) copyout(k, u, s)
376 ptrace(struct thread *td, struct ptrace_args *uap)
379 * XXX this obfuscation is to reduce stack usage, but the register
380 * structs may be too large to put on the stack anyway.
383 struct ptrace_io_desc piod;
384 struct ptrace_lwpinfo pl;
389 struct dbreg32 dbreg32;
390 struct fpreg32 fpreg32;
392 struct ptrace_io_desc32 piod32;
400 if (SV_CURPROC_FLAG(SV_ILP32))
403 AUDIT_ARG_PID(uap->pid);
404 AUDIT_ARG_CMD(uap->req);
405 AUDIT_ARG_VALUE(uap->data);
414 error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
417 error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
420 error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
423 error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
432 error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
438 error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
441 error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
444 error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
447 error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
450 error = copyout(&r.pl, uap->addr, uap->data);
461 * PROC_READ(regs, td2, addr);
463 * proc_read_regs(td2, addr);
465 * proc_read_regs32(td2, addr);
466 * .. except this is done at runtime. There is an additional
467 * complication in that PROC_WRITE disallows 32 bit consumers
468 * from writing to 64 bit address space targets.
470 #define PROC_READ(w, t, a) wrap32 ? \
471 proc_read_ ## w ## 32(t, a) : \
472 proc_read_ ## w (t, a)
473 #define PROC_WRITE(w, t, a) wrap32 ? \
474 (safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
475 proc_write_ ## w (t, a)
477 #define PROC_READ(w, t, a) proc_read_ ## w (t, a)
478 #define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
482 kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
486 struct proc *curp, *p, *pp;
487 struct thread *td2 = NULL;
488 struct ptrace_io_desc *piod = NULL;
489 struct ptrace_lwpinfo *pl;
490 int error, write, tmp, num;
491 int proctree_locked = 0;
492 lwpid_t tid = 0, *buf;
494 int wrap32 = 0, safe = 0;
495 struct ptrace_io_desc32 *piod32 = NULL;
500 /* Lock proctree before locking the process. */
510 sx_xlock(&proctree_lock);
518 if (req == PT_TRACE_ME) {
522 if (pid <= PID_MAX) {
523 if ((p = pfind(pid)) == NULL) {
525 sx_xunlock(&proctree_lock);
529 /* this is slow, should be optimized */
530 sx_slock(&allproc_lock);
531 FOREACH_PROC_IN_SYSTEM(p) {
533 FOREACH_THREAD_IN_PROC(p, td2) {
534 if (td2->td_tid == pid)
538 break; /* proc lock held */
541 sx_sunlock(&allproc_lock);
544 sx_xunlock(&proctree_lock);
551 AUDIT_ARG_PROCESS(p);
553 if ((p->p_flag & P_WEXIT) != 0) {
557 if ((error = p_cansee(td, p)) != 0)
560 if ((error = p_candebug(td, p)) != 0)
564 * System processes can't be debugged.
566 if ((p->p_flag & P_SYSTEM) != 0) {
572 if ((p->p_flag & P_STOPPED_TRACE) != 0) {
573 KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
576 td2 = FIRST_THREAD_IN_PROC(p);
583 * Test if we're a 32 bit client and what the target is.
584 * Set the wrap controls accordingly.
586 if (SV_CURPROC_FLAG(SV_ILP32)) {
587 if (td2->td_proc->p_sysent->sv_flags & SV_ILP32)
602 if (p->p_pid == td->td_proc->p_pid) {
608 if (p->p_flag & P_TRACED) {
613 /* Can't trace an ancestor if you're being traced. */
614 if (curp->p_flag & P_TRACED) {
615 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
628 /* Allow thread to clear single step for itself */
629 if (td->td_tid == tid)
634 /* not being traced... */
635 if ((p->p_flag & P_TRACED) == 0) {
640 /* not being traced by YOU */
641 if (p->p_pptr != td->td_proc) {
646 /* not currently stopped */
647 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
648 p->p_suspcount != p->p_numthreads ||
649 (p->p_flag & P_WAITED) == 0) {
654 if ((p->p_flag & P_STOPPED_TRACE) == 0) {
655 static int count = 0;
657 printf("P_STOPPED_TRACE not set.\n");
664 /* Keep this process around until we finish this request. */
669 * Single step fixup ala procfs
675 * Actually do the requests
678 td->td_retval[0] = 0;
682 /* set my trace flag and "owner" so it can read/write me */
683 p->p_flag |= P_TRACED;
684 p->p_oppid = p->p_pptr->p_pid;
688 /* security check done above */
689 p->p_flag |= P_TRACED;
690 p->p_oppid = p->p_pptr->p_pid;
691 if (p->p_pptr != td->td_proc)
692 proc_reparent(p, td->td_proc);
694 goto sendsig; /* in PT_CONTINUE below */
697 error = ptrace_clear_single_step(td2);
701 error = ptrace_single_step(td2);
705 td2->td_dbgflags |= TDB_SUSPEND;
707 td2->td_flags |= TDF_NEEDSUSPCHK;
712 td2->td_dbgflags &= ~TDB_SUSPEND;
721 /* Zero means do not send any signal */
722 if (data < 0 || data > _SIG_MAXSIG) {
729 error = ptrace_single_step(td2);
734 p->p_stops |= S_PT_SCE;
737 p->p_stops |= S_PT_SCX;
740 p->p_stops |= S_PT_SCE | S_PT_SCX;
744 if (addr != (void *)1) {
745 error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr);
750 if (req == PT_DETACH) {
751 /* reset process parent */
752 if (p->p_oppid != p->p_pptr->p_pid) {
755 PROC_LOCK(p->p_pptr);
756 sigqueue_take(p->p_ksi);
757 PROC_UNLOCK(p->p_pptr);
760 pp = pfind(p->p_oppid);
766 proc_reparent(p, pp);
768 p->p_sigparent = SIGCHLD;
770 p->p_flag &= ~(P_TRACED | P_WAITED);
773 /* should we send SIGCHLD? */
774 /* childproc_continued(p); */
778 if (proctree_locked) {
779 sx_xunlock(&proctree_lock);
784 if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
785 /* deliver or queue signal */
786 td2->td_dbgflags &= ~TDB_XSIG;
789 if (req == PT_DETACH) {
791 FOREACH_THREAD_IN_PROC(p, td3) {
792 td3->td_dbgflags &= ~TDB_SUSPEND;
796 * unsuspend all threads, to not let a thread run,
797 * you should use PT_SUSPEND to suspend it before
798 * continuing process.
801 p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
812 td2->td_dbgflags |= TDB_USERWR;
819 /* write = 0 set above */
820 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
821 iov.iov_len = sizeof(int);
824 uio.uio_offset = (off_t)(uintptr_t)addr;
825 uio.uio_resid = sizeof(int);
826 uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */
827 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
829 error = proc_rwmem(p, &uio);
830 if (uio.uio_resid != 0) {
832 * XXX proc_rwmem() doesn't currently return ENOSPC,
833 * so I think write() can bogusly return 0.
834 * XXX what happens for short writes? We don't want
835 * to write partial data.
836 * XXX proc_rwmem() returns EPERM for other invalid
837 * addresses. Convert this to EINVAL. Does this
838 * clobber returns of EPERM for other reasons?
840 if (error == 0 || error == ENOSPC || error == EPERM)
841 error = EINVAL; /* EOF */
844 td->td_retval[0] = tmp;
852 iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
853 iov.iov_len = piod32->piod_len;
854 uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
855 uio.uio_resid = piod32->piod_len;
860 iov.iov_base = piod->piod_addr;
861 iov.iov_len = piod->piod_len;
862 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
863 uio.uio_resid = piod->piod_len;
867 uio.uio_segflg = UIO_USERSPACE;
870 tmp = wrap32 ? piod32->piod_op : piod->piod_op;
877 uio.uio_rw = UIO_READ;
881 td2->td_dbgflags |= TDB_USERWR;
882 uio.uio_rw = UIO_WRITE;
889 error = proc_rwmem(p, &uio);
892 piod32->piod_len -= uio.uio_resid;
895 piod->piod_len -= uio.uio_resid;
901 goto sendsig; /* in PT_CONTINUE above */
904 td2->td_dbgflags |= TDB_USERWR;
905 error = PROC_WRITE(regs, td2, addr);
909 error = PROC_READ(regs, td2, addr);
913 td2->td_dbgflags |= TDB_USERWR;
914 error = PROC_WRITE(fpregs, td2, addr);
918 error = PROC_READ(fpregs, td2, addr);
922 td2->td_dbgflags |= TDB_USERWR;
923 error = PROC_WRITE(dbregs, td2, addr);
927 error = PROC_READ(dbregs, td2, addr);
931 if (data <= 0 || data > sizeof(*pl)) {
936 pl->pl_lwpid = td2->td_tid;
937 if (td2->td_dbgflags & TDB_XSIG)
938 pl->pl_event = PL_EVENT_SIGNAL;
942 pl->pl_sigmask = td2->td_sigmask;
943 pl->pl_siglist = td2->td_siglist;
947 td->td_retval[0] = p->p_numthreads;
955 num = imin(p->p_numthreads, data);
957 buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
960 FOREACH_THREAD_IN_PROC(p, td2) {
963 buf[tmp++] = td2->td_tid;
966 error = copyout(buf, addr, tmp * sizeof(lwpid_t));
969 td->td_retval[0] = tmp;
974 #ifdef __HAVE_PTRACE_MACHDEP
975 if (req >= PT_FIRSTMACH) {
977 error = cpu_ptrace(td2, req, addr, data);
981 /* Unknown request. */
987 /* Drop our hold on this process now that the request has completed. */
992 sx_xunlock(&proctree_lock);
999 * Stop a process because of a debugging event;
1000 * stay stopped until p->p_step is cleared
1001 * (cleared by PIOCCONT in procfs).
1004 stopevent(struct proc *p, unsigned int event, unsigned int val)
1007 PROC_LOCK_ASSERT(p, MA_OWNED);
1011 p->p_xthread = NULL;
1012 p->p_stype = event; /* Which event caused the stop? */
1013 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
1014 msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
1015 } while (p->p_step);