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.
205 sys_ptrace(struct ptrace_args *uap)
207 struct proc *p = curproc;
210 * XXX this obfuscation is to reduce stack usage, but the register
211 * structs may be too large to put on the stack anyway.
214 struct ptrace_io_desc piod;
231 error = copyin(uap->addr, &r.reg, sizeof r.reg);
234 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
238 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
242 error = copyin(uap->addr, &r.piod, sizeof r.piod);
251 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
252 &uap->sysmsg_result);
259 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
262 error = copyout(&r.reg, uap->addr, sizeof r.reg);
265 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
269 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
278 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr, int data, int *res)
284 struct ptrace_io_desc *piod;
289 if (req == PT_TRACE_ME) {
292 if ((p = pfind(pid)) == NULL)
295 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred))
298 /* Can't trace a process that's currently exec'ing. */
299 if ((p->p_flag & P_INEXEC) != 0)
312 if (p->p_pid == curp->p_pid)
316 if (p->p_flag & P_TRACED)
319 if (curp->p_flag & P_TRACED)
320 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
324 /* not owned by you, has done setuid (unless you're root) */
325 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
326 (p->p_flag & P_SUGID)) {
327 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0)
331 /* can't trace init when securelevel > 0 */
332 if (securelevel > 0 && p->p_pid == 1)
365 /* not being traced... */
366 if ((p->p_flag & P_TRACED) == 0)
369 /* not being traced by YOU */
370 if (p->p_pptr != curp)
373 /* not currently stopped */
374 if (p->p_stat != SSTOP ||
375 (p->p_flag & P_WAITED) == 0) {
387 lp = FIRST_LWP_IN_PROC(p);
390 * Single step fixup ala procfs
396 * Actually do the requests
403 /* set my trace flag and "owner" so it can read/write me */
404 p->p_flag |= P_TRACED;
405 p->p_oppid = p->p_pptr->p_pid;
409 /* security check done above */
410 p->p_flag |= P_TRACED;
411 p->p_oppid = p->p_pptr->p_pid;
412 if (p->p_pptr != curp)
413 proc_reparent(p, curp);
415 goto sendsig; /* in PT_CONTINUE below */
420 /* Zero means do not send any signal */
421 if (data < 0 || data > _SIG_MAXSIG)
426 if (req == PT_STEP) {
427 if ((error = ptrace_single_step (lp))) {
433 if (addr != (void *)1) {
434 if ((error = ptrace_set_pc (lp,
435 (u_long)(uintfptr_t)addr))) {
442 if (req == PT_DETACH) {
443 /* reset process parent */
444 if (p->p_oppid != p->p_pptr->p_pid) {
447 pp = pfind(p->p_oppid);
448 proc_reparent(p, pp ? pp : initproc);
451 p->p_flag &= ~(P_TRACED | P_WAITED);
454 /* should we send SIGCHLD? */
459 * Deliver or queue signal. If the process is stopped
460 * force it to be SACTIVE again.
463 if (p->p_stat == SSTOP) {
465 lp->lwp_flag |= LWP_BREAKTSLEEP;
480 * NOTE! uio_offset represents the offset in the target
481 * process. The iov is in the current process (the guy
482 * making the ptrace call) so uio_td must be the current
483 * process (though for a SYSSPACE transfer it doesn't
487 /* write = 0 set above */
488 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
489 iov.iov_len = sizeof(int);
492 uio.uio_offset = (off_t)(uintptr_t)addr;
493 uio.uio_resid = sizeof(int);
494 uio.uio_segflg = UIO_SYSSPACE;
495 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
496 uio.uio_td = curthread;
497 error = procfs_domem(curp, lp, NULL, &uio);
498 if (uio.uio_resid != 0) {
500 * XXX procfs_domem() doesn't currently return ENOSPC,
501 * so I think write() can bogusly return 0.
502 * XXX what happens for short writes? We don't want
503 * to write partial data.
504 * XXX procfs_domem() returns EPERM for other invalid
505 * addresses. Convert this to EINVAL. Does this
506 * clobber returns of EPERM for other reasons?
508 if (error == 0 || error == ENOSPC || error == EPERM)
509 error = EINVAL; /* EOF */
517 * NOTE! uio_offset represents the offset in the target
518 * process. The iov is in the current process (the guy
519 * making the ptrace call) so uio_td must be the current
523 iov.iov_base = piod->piod_addr;
524 iov.iov_len = piod->piod_len;
527 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
528 uio.uio_resid = piod->piod_len;
529 uio.uio_segflg = UIO_USERSPACE;
530 uio.uio_td = curthread;
531 switch (piod->piod_op) {
534 uio.uio_rw = UIO_READ;
538 uio.uio_rw = UIO_WRITE;
543 error = procfs_domem(curp, lp, NULL, &uio);
544 piod->piod_len -= uio.uio_resid;
549 goto sendsig; /* in PT_CONTINUE above */
555 #endif /* PT_SETREGS */
558 /* write = 0 above */
559 #endif /* PT_SETREGS */
560 #if defined(PT_SETREGS) || defined(PT_GETREGS)
561 if (!procfs_validregs(lp)) /* no P_SYSTEM procs please */
565 iov.iov_len = sizeof(struct reg);
569 uio.uio_resid = sizeof(struct reg);
570 uio.uio_segflg = UIO_SYSSPACE;
571 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
572 uio.uio_td = curthread;
573 return (procfs_doregs(curp, lp, NULL, &uio));
575 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
581 #endif /* PT_SETFPREGS */
584 /* write = 0 above */
585 #endif /* PT_SETFPREGS */
586 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
587 if (!procfs_validfpregs(lp)) /* no P_SYSTEM procs please */
591 iov.iov_len = sizeof(struct fpreg);
595 uio.uio_resid = sizeof(struct fpreg);
596 uio.uio_segflg = UIO_SYSSPACE;
597 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
598 uio.uio_td = curthread;
599 return (procfs_dofpregs(curp, lp, NULL, &uio));
601 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
607 #endif /* PT_SETDBREGS */
610 /* write = 0 above */
611 #endif /* PT_SETDBREGS */
612 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
613 if (!procfs_validdbregs(lp)) /* no P_SYSTEM procs please */
617 iov.iov_len = sizeof(struct dbreg);
621 uio.uio_resid = sizeof(struct dbreg);
622 uio.uio_segflg = UIO_SYSSPACE;
623 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
624 uio.uio_td = curthread;
625 return (procfs_dodbregs(curp, lp, NULL, &uio));
627 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
637 trace_req(struct proc *p)
645 * Stop a process because of a procfs event. Stay stopped until p->p_step
646 * is cleared (cleared by PIOCCONT in procfs).
651 stopevent(struct proc *p, unsigned int event, unsigned int val)
657 wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
659 p->p_stype = event; /* Which event caused the stop? */
660 tsleep(&p->p_step, 0, "stopevent", 0);