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
31 * $FreeBSD: src/sys/kern/sys_process.c,v 1.51.2.6 2003/01/08 03:06:45 kan Exp $
32 * $DragonFly: src/sys/kern/sys_process.c,v 1.30 2007/02/19 01:14:23 corecode Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/sysproto.h>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
51 #include <vfs/procfs/procfs.h>
52 #include <sys/thread2.h>
54 /* use the equivalent procfs code */
57 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
62 int page_offset; /* offset into page */
63 vm_offset_t pageno; /* page number */
64 vm_map_entry_t out_entry;
69 /* Map page into kernel space */
71 map = &procp->p_vmspace->vm_map;
73 page_offset = addr - trunc_page(addr);
74 pageno = trunc_page(addr);
77 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
78 &object, &pindex, &out_prot, &wired);
80 if (rv != KERN_SUCCESS)
83 vm_map_lookup_done (tmap, out_entry, 0);
85 /* Find space in kernel_map for the page we're interested in */
86 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
90 VM_PROT_ALL, VM_PROT_ALL,
94 vm_object_reference (object);
96 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
99 bcopy ((caddr_t)kva + page_offset,
100 retval, sizeof *retval);
102 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
109 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
114 int page_offset; /* offset into page */
115 vm_offset_t pageno; /* page number */
116 vm_map_entry_t out_entry;
120 boolean_t fix_prot = 0;
122 /* Map page into kernel space */
124 map = &procp->p_vmspace->vm_map;
126 page_offset = addr - trunc_page(addr);
127 pageno = trunc_page(addr);
130 * Check the permissions for the area we're interested in.
133 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
134 VM_PROT_WRITE) == FALSE) {
136 * If the page was not writable, we make it so.
137 * XXX It is possible a page may *not* be read/executable,
138 * if a process changes that!
141 /* The page isn't writable, so let's try making it so... */
142 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
143 VM_PROT_ALL, 0)) != KERN_SUCCESS)
144 return EFAULT; /* I guess... */
148 * Now we need to get the page. out_entry, out_prot, wired, and
149 * single_use aren't used. One would think the vm code would be
150 * a *bit* nicer... We use tmap because vm_map_lookup() can
151 * change the map argument.
155 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
156 &object, &pindex, &out_prot, &wired);
157 if (rv != KERN_SUCCESS) {
162 * Okay, we've got the page. Let's release tmap.
165 vm_map_lookup_done (tmap, out_entry, 0);
168 * Fault the page in...
171 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
172 if (rv != KERN_SUCCESS)
175 /* Find space in kernel_map for the page we're interested in */
176 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
180 VM_PROT_ALL, VM_PROT_ALL,
183 vm_object_reference (object);
185 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
187 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
189 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
193 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
194 VM_PROT_READ|VM_PROT_EXECUTE, 0);
200 * Process debugging system call.
203 sys_ptrace(struct ptrace_args *uap)
205 struct proc *p = curproc;
208 * XXX this obfuscation is to reduce stack usage, but the register
209 * structs may be too large to put on the stack anyway.
212 struct ptrace_io_desc piod;
229 error = copyin(uap->addr, &r.reg, sizeof r.reg);
232 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
236 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
240 error = copyin(uap->addr, &r.piod, sizeof r.piod);
248 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
249 &uap->sysmsg_result);
255 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
258 error = copyout(&r.reg, uap->addr, sizeof r.reg);
261 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
265 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
274 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
280 struct ptrace_io_desc *piod;
285 if (req == PT_TRACE_ME) {
288 if ((p = pfind(pid)) == NULL)
291 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
294 /* Can't trace a process that's currently exec'ing. */
295 if ((p->p_flag & P_INEXEC) != 0)
308 if (p->p_pid == curp->p_pid)
312 if (p->p_flag & P_TRACED)
315 if (curp->p_flag & P_TRACED)
316 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
320 /* not owned by you, has done setuid (unless you're root) */
321 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
322 (p->p_flag & P_SUGID)) {
323 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0)
327 /* can't trace init when securelevel > 0 */
328 if (securelevel > 0 && p->p_pid == 1)
361 /* not being traced... */
362 if ((p->p_flag & P_TRACED) == 0)
365 /* not being traced by YOU */
366 if (p->p_pptr != curp)
369 /* not currently stopped */
370 if (p->p_stat != SSTOP ||
371 (p->p_flag & P_WAITED) == 0) {
383 lp = FIRST_LWP_IN_PROC(p);
386 * Single step fixup ala procfs
392 * Actually do the requests
399 /* set my trace flag and "owner" so it can read/write me */
400 p->p_flag |= P_TRACED;
401 p->p_oppid = p->p_pptr->p_pid;
405 /* security check done above */
406 p->p_flag |= P_TRACED;
407 p->p_oppid = p->p_pptr->p_pid;
408 if (p->p_pptr != curp)
409 proc_reparent(p, curp);
411 goto sendsig; /* in PT_CONTINUE below */
416 /* Zero means do not send any signal */
417 if (data < 0 || data > _SIG_MAXSIG)
422 if (req == PT_STEP) {
423 if ((error = ptrace_single_step (lp))) {
429 if (addr != (void *)1) {
430 if ((error = ptrace_set_pc (lp,
431 (u_long)(uintfptr_t)addr))) {
438 if (req == PT_DETACH) {
439 /* reset process parent */
440 if (p->p_oppid != p->p_pptr->p_pid) {
443 pp = pfind(p->p_oppid);
444 proc_reparent(p, pp ? pp : initproc);
447 p->p_flag &= ~(P_TRACED | P_WAITED);
450 /* should we send SIGCHLD? */
455 * Deliver or queue signal. If the process is stopped
456 * force it to be SACTIVE again.
459 if (p->p_stat == SSTOP) {
461 lp->lwp_flag |= LWP_BREAKTSLEEP;
476 * NOTE! uio_offset represents the offset in the target
477 * process. The iov is in the current process (the guy
478 * making the ptrace call) so uio_td must be the current
479 * process (though for a SYSSPACE transfer it doesn't
483 /* write = 0 set above */
484 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
485 iov.iov_len = sizeof(int);
488 uio.uio_offset = (off_t)(uintptr_t)addr;
489 uio.uio_resid = sizeof(int);
490 uio.uio_segflg = UIO_SYSSPACE;
491 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
492 uio.uio_td = curthread;
493 error = procfs_domem(curp, lp, NULL, &uio);
494 if (uio.uio_resid != 0) {
496 * XXX procfs_domem() doesn't currently return ENOSPC,
497 * so I think write() can bogusly return 0.
498 * XXX what happens for short writes? We don't want
499 * to write partial data.
500 * XXX procfs_domem() returns EPERM for other invalid
501 * addresses. Convert this to EINVAL. Does this
502 * clobber returns of EPERM for other reasons?
504 if (error == 0 || error == ENOSPC || error == EPERM)
505 error = EINVAL; /* EOF */
513 * NOTE! uio_offset represents the offset in the target
514 * process. The iov is in the current process (the guy
515 * making the ptrace call) so uio_td must be the current
519 iov.iov_base = piod->piod_addr;
520 iov.iov_len = piod->piod_len;
523 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
524 uio.uio_resid = piod->piod_len;
525 uio.uio_segflg = UIO_USERSPACE;
526 uio.uio_td = curthread;
527 switch (piod->piod_op) {
530 uio.uio_rw = UIO_READ;
534 uio.uio_rw = UIO_WRITE;
539 error = procfs_domem(curp, lp, NULL, &uio);
540 piod->piod_len -= uio.uio_resid;
545 goto sendsig; /* in PT_CONTINUE above */
551 #endif /* PT_SETREGS */
554 /* write = 0 above */
555 #endif /* PT_SETREGS */
556 #if defined(PT_SETREGS) || defined(PT_GETREGS)
557 if (!procfs_validregs(lp)) /* no P_SYSTEM procs please */
561 iov.iov_len = sizeof(struct reg);
565 uio.uio_resid = sizeof(struct reg);
566 uio.uio_segflg = UIO_SYSSPACE;
567 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
568 uio.uio_td = curthread;
569 return (procfs_doregs(curp, lp, NULL, &uio));
571 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
577 #endif /* PT_SETFPREGS */
580 /* write = 0 above */
581 #endif /* PT_SETFPREGS */
582 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
583 if (!procfs_validfpregs(lp)) /* no P_SYSTEM procs please */
587 iov.iov_len = sizeof(struct fpreg);
591 uio.uio_resid = sizeof(struct fpreg);
592 uio.uio_segflg = UIO_SYSSPACE;
593 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
594 uio.uio_td = curthread;
595 return (procfs_dofpregs(curp, lp, NULL, &uio));
597 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
603 #endif /* PT_SETDBREGS */
606 /* write = 0 above */
607 #endif /* PT_SETDBREGS */
608 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
609 if (!procfs_validdbregs(lp)) /* no P_SYSTEM procs please */
613 iov.iov_len = sizeof(struct dbreg);
617 uio.uio_resid = sizeof(struct dbreg);
618 uio.uio_segflg = UIO_SYSSPACE;
619 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
620 uio.uio_td = curthread;
621 return (procfs_dodbregs(curp, lp, NULL, &uio));
623 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
633 trace_req(struct proc *p)
641 * Stop a process because of a procfs event. Stay stopped until p->p_step
642 * is cleared (cleared by PIOCCONT in procfs).
647 stopevent(struct proc *p, unsigned int event, unsigned int val)
653 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
655 p->p_stype = event; /* Which event caused the stop? */
656 tsleep(&p->p_step, 0, "stopevent", 0);