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 $
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #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>
49 #include <vfs/procfs/procfs.h>
51 #include <sys/thread2.h>
52 #include <sys/spinlock2.h>
54 /* use the equivalent procfs code */
57 pread (struct proc *procp, unsigned int addr, unsigned int *retval)
64 int page_offset; /* offset into page */
65 vm_offset_t pageno; /* page number */
66 vm_map_entry_t out_entry;
71 /* Map page into kernel space */
73 map = &procp->p_vmspace->vm_map;
75 page_offset = addr - trunc_page(addr);
76 pageno = trunc_page(addr);
79 rv = vm_map_lookup(&tmap, pageno, VM_PROT_READ, &out_entry,
80 &ba, &pindex, &out_prot, &wflags);
87 if (rv != KERN_SUCCESS)
90 vm_map_lookup_done (tmap, out_entry, 0);
92 /* Find space in kernel_map for the page we're interested in */
93 rv = vm_map_find (&kernel_map, object, NULL,
94 IDX_TO_OFF(pindex), &kva, PAGE_SIZE,
96 VM_MAPTYPE_NORMAL, VM_SUBSYS_PROC,
97 VM_PROT_ALL, VM_PROT_ALL, 0);
100 vm_object_reference XXX (object);
102 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
105 bcopy ((caddr_t)kva + page_offset,
106 retval, sizeof *retval);
108 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
115 pwrite (struct proc *procp, unsigned int addr, unsigned int datum)
122 int page_offset; /* offset into page */
123 vm_offset_t pageno; /* page number */
124 vm_map_entry_t out_entry;
128 boolean_t fix_prot = 0;
130 /* Map page into kernel space */
132 map = &procp->p_vmspace->vm_map;
134 page_offset = addr - trunc_page(addr);
135 pageno = trunc_page(addr);
138 * Check the permissions for the area we're interested in.
141 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
142 VM_PROT_WRITE, FALSE) == FALSE) {
144 * If the page was not writable, we make it so.
145 * XXX It is possible a page may *not* be read/executable,
146 * if a process changes that!
149 /* The page isn't writable, so let's try making it so... */
150 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
151 VM_PROT_ALL, 0)) != KERN_SUCCESS)
152 return EFAULT; /* I guess... */
156 * Now we need to get the page. out_entry, out_prot, wflags, and
157 * single_use aren't used. One would think the vm code would be
158 * a *bit* nicer... We use tmap because vm_map_lookup() can
159 * change the map argument.
163 rv = vm_map_lookup(&tmap, pageno, VM_PROT_WRITE, &out_entry,
164 &ba, &pindex, &out_prot, &wflags);
170 if (rv != KERN_SUCCESS)
174 * Okay, we've got the page. Let's release tmap.
176 vm_map_lookup_done (tmap, out_entry, 0);
179 * Fault the page in...
181 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
182 if (rv != KERN_SUCCESS)
185 /* Find space in kernel_map for the page we're interested in */
186 rv = vm_map_find (&kernel_map, object, NULL,
187 IDX_TO_OFF(pindex), &kva, PAGE_SIZE,
189 VM_MAPTYPE_NORMAL, VM_SUBSYS_PROC,
190 VM_PROT_ALL, VM_PROT_ALL, 0);
192 vm_object_reference XXX (object);
194 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
196 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
198 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
202 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
203 VM_PROT_READ|VM_PROT_EXECUTE, 0);
209 * Process debugging system call.
214 sys_ptrace(struct ptrace_args *uap)
216 struct proc *p = curproc;
219 * XXX this obfuscation is to reduce stack usage, but the register
220 * structs may be too large to put on the stack anyway.
223 struct ptrace_io_desc piod;
240 error = copyin(uap->addr, &r.reg, sizeof r.reg);
243 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
247 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
251 error = copyin(uap->addr, &r.piod, sizeof r.piod);
259 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
260 &uap->sysmsg_result);
266 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
269 error = copyout(&r.reg, uap->addr, sizeof r.reg);
272 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
276 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
285 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr,
292 struct ptrace_io_desc *piod;
298 if (req == PT_TRACE_ME) {
302 if ((p = pfind(pid)) == NULL)
305 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
309 if (p->p_flags & P_SYSTEM) {
314 lwkt_gettoken(&p->p_token);
315 /* Can't trace a process that's currently exec'ing. */
316 if ((p->p_flags & P_INEXEC) != 0) {
317 lwkt_reltoken(&p->p_token);
332 if (p->p_pid == curp->p_pid) {
333 lwkt_reltoken(&p->p_token);
339 if (p->p_flags & P_TRACED) {
340 lwkt_reltoken(&p->p_token);
345 if (curp->p_flags & P_TRACED)
346 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
348 lwkt_reltoken(&p->p_token);
353 /* not owned by you, has done setuid (unless you're root) */
354 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
355 (p->p_flags & P_SUGID)) {
356 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
357 lwkt_reltoken(&p->p_token);
363 /* can't trace init when securelevel > 0 */
364 if (securelevel > 0 && p->p_pid == 1) {
365 lwkt_reltoken(&p->p_token);
400 /* not being traced... */
401 if ((p->p_flags & P_TRACED) == 0) {
402 lwkt_reltoken(&p->p_token);
407 /* not being traced by YOU */
408 if (p->p_pptr != curp) {
409 lwkt_reltoken(&p->p_token);
414 /* not currently stopped */
415 if (p->p_stat != SSTOP ||
416 (p->p_flags & P_WAITED) == 0) {
417 lwkt_reltoken(&p->p_token);
426 lwkt_reltoken(&p->p_token);
432 lp = FIRST_LWP_IN_PROC(p);
435 * Single step fixup ala procfs
441 * Actually do the requests
448 /* set my trace flag and "owner" so it can read/write me */
449 p->p_flags |= P_TRACED;
450 p->p_oppid = p->p_pptr->p_pid;
451 lwkt_reltoken(&p->p_token);
456 /* security check done above */
457 p->p_flags |= P_TRACED;
458 p->p_oppid = p->p_pptr->p_pid;
459 proc_reparent(p, curp);
461 goto sendsig; /* in PT_CONTINUE below */
466 /* Zero means do not send any signal */
467 if (data < 0 || data > _SIG_MAXSIG) {
468 lwkt_reltoken(&p->p_token);
475 if (req == PT_STEP) {
476 if ((error = ptrace_single_step (lp))) {
478 lwkt_reltoken(&p->p_token);
484 if (addr != (void *)1) {
485 if ((error = ptrace_set_pc (lp, (u_long)addr))) {
487 lwkt_reltoken(&p->p_token);
494 if (req == PT_DETACH) {
495 /* reset process parent */
496 if (p->p_oppid != p->p_pptr->p_pid) {
499 pp = pfind(p->p_oppid);
501 proc_reparent(p, pp);
506 p->p_flags &= ~(P_TRACED | P_WAITED);
509 /* should we send SIGCHLD? */
514 * Deliver or queue signal. If the process is stopped
515 * force it to be SACTIVE again.
518 if (p->p_stat == SSTOP) {
520 proc_unstop(p, SSTOP);
525 lwkt_reltoken(&p->p_token);
536 * NOTE! uio_offset represents the offset in the target
537 * process. The iov is in the current process (the guy
538 * making the ptrace call) so uio_td must be the current
539 * process (though for a SYSSPACE transfer it doesn't
543 /* write = 0 set above */
544 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
545 iov.iov_len = sizeof(int);
548 uio.uio_offset = (off_t)(uintptr_t)addr;
549 uio.uio_resid = sizeof(int);
550 uio.uio_segflg = UIO_SYSSPACE;
551 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
552 uio.uio_td = curthread;
553 error = procfs_domem(curp, lp, NULL, &uio);
554 if (uio.uio_resid != 0) {
556 * XXX procfs_domem() doesn't currently return ENOSPC,
557 * so I think write() can bogusly return 0.
558 * XXX what happens for short writes? We don't want
559 * to write partial data.
560 * XXX procfs_domem() returns EPERM for other invalid
561 * addresses. Convert this to EINVAL. Does this
562 * clobber returns of EPERM for other reasons?
564 if (error == 0 || error == ENOSPC || error == EPERM)
565 error = EINVAL; /* EOF */
569 lwkt_reltoken(&p->p_token);
575 * NOTE! uio_offset represents the offset in the target
576 * process. The iov is in the current process (the guy
577 * making the ptrace call) so uio_td must be the current
581 iov.iov_base = piod->piod_addr;
582 iov.iov_len = piod->piod_len;
585 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
586 uio.uio_resid = piod->piod_len;
587 uio.uio_segflg = UIO_USERSPACE;
588 uio.uio_td = curthread;
589 switch (piod->piod_op) {
592 uio.uio_rw = UIO_READ;
596 uio.uio_rw = UIO_WRITE;
599 lwkt_reltoken(&p->p_token);
603 error = procfs_domem(curp, lp, NULL, &uio);
604 piod->piod_len -= uio.uio_resid;
605 lwkt_reltoken(&p->p_token);
611 goto sendsig; /* in PT_CONTINUE above */
617 #endif /* PT_SETREGS */
620 /* write = 0 above */
621 #endif /* PT_SETREGS */
622 #if defined(PT_SETREGS) || defined(PT_GETREGS)
623 if (!procfs_validregs(lp)) {
624 lwkt_reltoken(&p->p_token);
629 iov.iov_len = sizeof(struct reg);
633 uio.uio_resid = sizeof(struct reg);
634 uio.uio_segflg = UIO_SYSSPACE;
635 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
636 uio.uio_td = curthread;
637 t = procfs_doregs(curp, lp, NULL, &uio);
638 lwkt_reltoken(&p->p_token);
642 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
648 #endif /* PT_SETFPREGS */
651 /* write = 0 above */
652 #endif /* PT_SETFPREGS */
653 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
654 if (!procfs_validfpregs(lp)) {
655 lwkt_reltoken(&p->p_token);
660 iov.iov_len = sizeof(struct fpreg);
664 uio.uio_resid = sizeof(struct fpreg);
665 uio.uio_segflg = UIO_SYSSPACE;
666 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
667 uio.uio_td = curthread;
668 t = procfs_dofpregs(curp, lp, NULL, &uio);
669 lwkt_reltoken(&p->p_token);
673 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
679 #endif /* PT_SETDBREGS */
682 /* write = 0 above */
683 #endif /* PT_SETDBREGS */
684 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
685 if (!procfs_validdbregs(lp)) {
686 lwkt_reltoken(&p->p_token);
691 iov.iov_len = sizeof(struct dbreg);
695 uio.uio_resid = sizeof(struct dbreg);
696 uio.uio_segflg = UIO_SYSSPACE;
697 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
698 uio.uio_td = curthread;
699 t = procfs_dodbregs(curp, lp, NULL, &uio);
700 lwkt_reltoken(&p->p_token);
704 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
710 lwkt_reltoken(&p->p_token);
717 trace_req(struct proc *p)
725 * Stop a process because of a procfs event. Stay stopped until p->p_step
726 * is cleared (cleared by PIOCCONT in procfs).
731 stopevent(struct proc *p, unsigned int event, unsigned int val)
734 * Set event info. Recheck p_stops in case we are
735 * racing a close() on procfs.
737 spin_lock(&p->p_spin);
738 if ((p->p_stops & event) == 0) {
739 spin_unlock(&p->p_spin);
745 tsleep_interlock(&p->p_step, 0);
746 spin_unlock(&p->p_spin);
749 * Wakeup any PIOCWAITing procs and wait for p_step to
754 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
755 spin_lock(&p->p_spin);
756 if (p->p_step == 0) {
757 spin_unlock(&p->p_spin);
760 tsleep_interlock(&p->p_step, 0);
761 spin_unlock(&p->p_spin);