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>
39 #include <sys/vnode.h>
40 #include <sys/ptrace.h>
46 #include <vm/vm_map.h>
47 #include <vm/vm_page.h>
50 #include <vfs/procfs/procfs.h>
51 #include <sys/thread2.h>
53 /* use the equivalent procfs code */
56 pread (struct proc *procp, unsigned int addr, unsigned int *retval) {
61 int page_offset; /* offset into page */
62 vm_offset_t pageno; /* page number */
63 vm_map_entry_t out_entry;
68 /* Map page into kernel space */
70 map = &procp->p_vmspace->vm_map;
72 page_offset = addr - trunc_page(addr);
73 pageno = trunc_page(addr);
76 rv = vm_map_lookup (&tmap, pageno, VM_PROT_READ, &out_entry,
77 &object, &pindex, &out_prot, &wired);
79 if (rv != KERN_SUCCESS)
82 vm_map_lookup_done (tmap, out_entry, 0);
84 /* Find space in kernel_map for the page we're interested in */
85 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
89 VM_PROT_ALL, VM_PROT_ALL,
93 vm_object_reference (object);
95 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
98 bcopy ((caddr_t)kva + page_offset,
99 retval, sizeof *retval);
101 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
108 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
113 int page_offset; /* offset into page */
114 vm_offset_t pageno; /* page number */
115 vm_map_entry_t out_entry;
119 boolean_t fix_prot = 0;
121 /* Map page into kernel space */
123 map = &procp->p_vmspace->vm_map;
125 page_offset = addr - trunc_page(addr);
126 pageno = trunc_page(addr);
129 * Check the permissions for the area we're interested in.
132 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
133 VM_PROT_WRITE) == FALSE) {
135 * If the page was not writable, we make it so.
136 * XXX It is possible a page may *not* be read/executable,
137 * if a process changes that!
140 /* The page isn't writable, so let's try making it so... */
141 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
142 VM_PROT_ALL, 0)) != KERN_SUCCESS)
143 return EFAULT; /* I guess... */
147 * Now we need to get the page. out_entry, out_prot, wired, and
148 * single_use aren't used. One would think the vm code would be
149 * a *bit* nicer... We use tmap because vm_map_lookup() can
150 * change the map argument.
154 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
155 &object, &pindex, &out_prot, &wired);
156 if (rv != KERN_SUCCESS) {
161 * Okay, we've got the page. Let's release tmap.
164 vm_map_lookup_done (tmap, out_entry, 0);
167 * Fault the page in...
170 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
171 if (rv != KERN_SUCCESS)
174 /* Find space in kernel_map for the page we're interested in */
175 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
179 VM_PROT_ALL, VM_PROT_ALL,
182 vm_object_reference (object);
184 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
186 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
188 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
192 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
193 VM_PROT_READ|VM_PROT_EXECUTE, 0);
199 * Process debugging system call.
202 sys_ptrace(struct ptrace_args *uap)
204 struct proc *p = curproc;
207 * XXX this obfuscation is to reduce stack usage, but the register
208 * structs may be too large to put on the stack anyway.
211 struct ptrace_io_desc piod;
228 error = copyin(uap->addr, &r.reg, sizeof r.reg);
231 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
235 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
239 error = copyin(uap->addr, &r.piod, sizeof r.piod);
247 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
248 &uap->sysmsg_result);
254 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
257 error = copyout(&r.reg, uap->addr, sizeof r.reg);
260 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
264 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
273 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
279 struct ptrace_io_desc *piod;
284 if (req == PT_TRACE_ME) {
287 if ((p = pfind(pid)) == NULL)
290 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
293 /* Can't trace a process that's currently exec'ing. */
294 if ((p->p_flag & P_INEXEC) != 0)
307 if (p->p_pid == curp->p_pid)
311 if (p->p_flag & P_TRACED)
314 if (curp->p_flag & P_TRACED)
315 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
319 /* not owned by you, has done setuid (unless you're root) */
320 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
321 (p->p_flag & P_SUGID)) {
322 if ((error = suser_cred(curp->p_ucred, 0)) != 0)
326 /* can't trace init when securelevel > 0 */
327 if (securelevel > 0 && p->p_pid == 1)
360 /* not being traced... */
361 if ((p->p_flag & P_TRACED) == 0)
364 /* not being traced by YOU */
365 if (p->p_pptr != curp)
368 /* not currently stopped */
369 if (p->p_stat != SSTOP ||
370 (p->p_flag & P_WAITED) == 0) {
382 lp = FIRST_LWP_IN_PROC(p);
385 * Single step fixup ala procfs
391 * Actually do the requests
398 /* set my trace flag and "owner" so it can read/write me */
399 p->p_flag |= P_TRACED;
400 p->p_oppid = p->p_pptr->p_pid;
404 /* security check done above */
405 p->p_flag |= P_TRACED;
406 p->p_oppid = p->p_pptr->p_pid;
407 if (p->p_pptr != curp)
408 proc_reparent(p, curp);
410 goto sendsig; /* in PT_CONTINUE below */
415 /* Zero means do not send any signal */
416 if (data < 0 || data > _SIG_MAXSIG)
421 if (req == PT_STEP) {
422 if ((error = ptrace_single_step (lp))) {
428 if (addr != (void *)1) {
429 if ((error = ptrace_set_pc (lp,
430 (u_long)(uintfptr_t)addr))) {
437 if (req == PT_DETACH) {
438 /* reset process parent */
439 if (p->p_oppid != p->p_pptr->p_pid) {
442 pp = pfind(p->p_oppid);
443 proc_reparent(p, pp ? pp : initproc);
446 p->p_flag &= ~(P_TRACED | P_WAITED);
449 /* should we send SIGCHLD? */
454 * Deliver or queue signal. If the process is stopped
455 * force it to be SACTIVE again.
458 if (p->p_stat == SSTOP) {
460 lp->lwp_flag |= LWP_BREAKTSLEEP;
475 * NOTE! uio_offset represents the offset in the target
476 * process. The iov is in the current process (the guy
477 * making the ptrace call) so uio_td must be the current
478 * process (though for a SYSSPACE transfer it doesn't
482 /* write = 0 set above */
483 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
484 iov.iov_len = sizeof(int);
487 uio.uio_offset = (off_t)(uintptr_t)addr;
488 uio.uio_resid = sizeof(int);
489 uio.uio_segflg = UIO_SYSSPACE;
490 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
491 uio.uio_td = curthread;
492 error = procfs_domem(curp, lp, NULL, &uio);
493 if (uio.uio_resid != 0) {
495 * XXX procfs_domem() doesn't currently return ENOSPC,
496 * so I think write() can bogusly return 0.
497 * XXX what happens for short writes? We don't want
498 * to write partial data.
499 * XXX procfs_domem() returns EPERM for other invalid
500 * addresses. Convert this to EINVAL. Does this
501 * clobber returns of EPERM for other reasons?
503 if (error == 0 || error == ENOSPC || error == EPERM)
504 error = EINVAL; /* EOF */
512 * NOTE! uio_offset represents the offset in the target
513 * process. The iov is in the current process (the guy
514 * making the ptrace call) so uio_td must be the current
518 iov.iov_base = piod->piod_addr;
519 iov.iov_len = piod->piod_len;
522 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
523 uio.uio_resid = piod->piod_len;
524 uio.uio_segflg = UIO_USERSPACE;
525 uio.uio_td = curthread;
526 switch (piod->piod_op) {
529 uio.uio_rw = UIO_READ;
533 uio.uio_rw = UIO_WRITE;
538 error = procfs_domem(curp, lp, NULL, &uio);
539 piod->piod_len -= uio.uio_resid;
544 goto sendsig; /* in PT_CONTINUE above */
550 #endif /* PT_SETREGS */
553 /* write = 0 above */
554 #endif /* PT_SETREGS */
555 #if defined(PT_SETREGS) || defined(PT_GETREGS)
556 if (!procfs_validregs(lp)) /* no P_SYSTEM procs please */
560 iov.iov_len = sizeof(struct reg);
564 uio.uio_resid = sizeof(struct reg);
565 uio.uio_segflg = UIO_SYSSPACE;
566 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
567 uio.uio_td = curthread;
568 return (procfs_doregs(curp, lp, NULL, &uio));
570 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
576 #endif /* PT_SETFPREGS */
579 /* write = 0 above */
580 #endif /* PT_SETFPREGS */
581 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
582 if (!procfs_validfpregs(lp)) /* no P_SYSTEM procs please */
586 iov.iov_len = sizeof(struct fpreg);
590 uio.uio_resid = sizeof(struct fpreg);
591 uio.uio_segflg = UIO_SYSSPACE;
592 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
593 uio.uio_td = curthread;
594 return (procfs_dofpregs(curp, lp, NULL, &uio));
596 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
602 #endif /* PT_SETDBREGS */
605 /* write = 0 above */
606 #endif /* PT_SETDBREGS */
607 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
608 if (!procfs_validdbregs(lp)) /* no P_SYSTEM procs please */
612 iov.iov_len = sizeof(struct dbreg);
616 uio.uio_resid = sizeof(struct dbreg);
617 uio.uio_segflg = UIO_SYSSPACE;
618 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
619 uio.uio_td = curthread;
620 return (procfs_dodbregs(curp, lp, NULL, &uio));
622 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
632 trace_req(struct proc *p)
640 * Stop a process because of a procfs event. Stay stopped until p->p_step
641 * is cleared (cleared by PIOCCONT in procfs).
646 stopevent(struct proc *p, unsigned int event, unsigned int val)
652 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
654 p->p_stype = event; /* Which event caused the stop? */
655 tsleep(&p->p_step, 0, "stopevent", 0);