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>
40 #include <sys/vnode.h>
41 #include <sys/ptrace.h>
47 #include <vm/vm_map.h>
48 #include <vm/vm_page.h>
50 #include <vfs/procfs/procfs.h>
52 #include <sys/thread2.h>
53 #include <sys/spinlock2.h>
55 /* use the equivalent procfs code */
58 pread (struct proc *procp, unsigned int addr, unsigned int *retval)
65 int page_offset; /* offset into page */
66 vm_offset_t pageno; /* page number */
67 vm_map_entry_t out_entry;
72 /* Map page into kernel space */
74 map = &procp->p_vmspace->vm_map;
76 page_offset = addr - trunc_page(addr);
77 pageno = trunc_page(addr);
80 rv = vm_map_lookup(&tmap, pageno, VM_PROT_READ, &out_entry,
81 &ba, &pindex, &out_prot, &wflags);
88 if (rv != KERN_SUCCESS)
91 vm_map_lookup_done (tmap, out_entry, 0);
93 /* Find space in kernel_map for the page we're interested in */
94 rv = vm_map_find (&kernel_map, object, NULL,
95 IDX_TO_OFF(pindex), &kva, PAGE_SIZE,
97 VM_MAPTYPE_NORMAL, VM_SUBSYS_PROC,
98 VM_PROT_ALL, VM_PROT_ALL, 0);
101 vm_object_reference XXX (object);
103 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
106 bcopy ((caddr_t)kva + page_offset,
107 retval, sizeof *retval);
109 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
116 pwrite (struct proc *procp, unsigned int addr, unsigned int datum)
123 int page_offset; /* offset into page */
124 vm_offset_t pageno; /* page number */
125 vm_map_entry_t out_entry;
129 boolean_t fix_prot = 0;
131 /* Map page into kernel space */
133 map = &procp->p_vmspace->vm_map;
135 page_offset = addr - trunc_page(addr);
136 pageno = trunc_page(addr);
139 * Check the permissions for the area we're interested in.
142 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
143 VM_PROT_WRITE, FALSE) == FALSE) {
145 * If the page was not writable, we make it so.
146 * XXX It is possible a page may *not* be read/executable,
147 * if a process changes that!
150 /* The page isn't writable, so let's try making it so... */
151 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
152 VM_PROT_ALL, 0)) != KERN_SUCCESS)
153 return EFAULT; /* I guess... */
157 * Now we need to get the page. out_entry, out_prot, wflags, and
158 * single_use aren't used. One would think the vm code would be
159 * a *bit* nicer... We use tmap because vm_map_lookup() can
160 * change the map argument.
164 rv = vm_map_lookup(&tmap, pageno, VM_PROT_WRITE, &out_entry,
165 &ba, &pindex, &out_prot, &wflags);
171 if (rv != KERN_SUCCESS)
175 * Okay, we've got the page. Let's release tmap.
177 vm_map_lookup_done (tmap, out_entry, 0);
180 * Fault the page in...
182 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
183 if (rv != KERN_SUCCESS)
186 /* Find space in kernel_map for the page we're interested in */
187 rv = vm_map_find (&kernel_map, object, NULL,
188 IDX_TO_OFF(pindex), &kva, PAGE_SIZE,
190 VM_MAPTYPE_NORMAL, VM_SUBSYS_PROC,
191 VM_PROT_ALL, VM_PROT_ALL, 0);
193 vm_object_reference XXX (object);
195 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
197 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
199 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
203 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
204 VM_PROT_READ|VM_PROT_EXECUTE, 0);
210 * Process debugging system call.
215 sys_ptrace(struct ptrace_args *uap)
217 struct proc *p = curproc;
220 * XXX this obfuscation is to reduce stack usage, but the register
221 * structs may be too large to put on the stack anyway.
224 struct ptrace_io_desc piod;
241 error = copyin(uap->addr, &r.reg, sizeof r.reg);
244 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
248 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
252 error = copyin(uap->addr, &r.piod, sizeof r.piod);
260 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
261 &uap->sysmsg_result);
267 (void)copyout(&r.piod, uap->addr, sizeof r.piod);
270 error = copyout(&r.reg, uap->addr, sizeof r.reg);
273 error = copyout(&r.fpreg, uap->addr, sizeof r.fpreg);
277 error = copyout(&r.dbreg, uap->addr, sizeof r.dbreg);
286 kern_ptrace(struct proc *curp, int req, pid_t pid, void *addr,
293 struct ptrace_io_desc *piod;
299 if (req == PT_TRACE_ME) {
303 if ((p = pfind(pid)) == NULL)
306 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
310 if (p->p_flags & P_SYSTEM) {
315 lwkt_gettoken(&p->p_token);
316 /* Can't trace a process that's currently exec'ing. */
317 if ((p->p_flags & P_INEXEC) != 0) {
318 lwkt_reltoken(&p->p_token);
333 if (p->p_pid == curp->p_pid) {
334 lwkt_reltoken(&p->p_token);
340 if (p->p_flags & P_TRACED) {
341 lwkt_reltoken(&p->p_token);
346 if (curp->p_flags & P_TRACED)
347 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
349 lwkt_reltoken(&p->p_token);
354 /* not owned by you, has done setuid (unless you're root) */
355 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
356 (p->p_flags & P_SUGID)) {
357 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
358 lwkt_reltoken(&p->p_token);
364 /* can't trace init when securelevel > 0 */
365 if (securelevel > 0 && p->p_pid == 1) {
366 lwkt_reltoken(&p->p_token);
401 /* not being traced... */
402 if ((p->p_flags & P_TRACED) == 0) {
403 lwkt_reltoken(&p->p_token);
408 /* not being traced by YOU */
409 if (p->p_pptr != curp) {
410 lwkt_reltoken(&p->p_token);
415 /* not currently stopped */
416 if (p->p_stat != SSTOP ||
417 (p->p_flags & P_WAITED) == 0) {
418 lwkt_reltoken(&p->p_token);
427 lwkt_reltoken(&p->p_token);
433 lp = FIRST_LWP_IN_PROC(p);
436 * Single step fixup ala procfs
442 * Actually do the requests
449 /* set my trace flag and "owner" so it can read/write me */
450 p->p_flags |= P_TRACED;
451 p->p_oppid = p->p_pptr->p_pid;
452 lwkt_reltoken(&p->p_token);
457 /* security check done above */
458 p->p_flags |= P_TRACED;
459 p->p_oppid = p->p_pptr->p_pid;
460 proc_reparent(p, curp);
462 goto sendsig; /* in PT_CONTINUE below */
467 /* Zero means do not send any signal */
468 if (data < 0 || data > _SIG_MAXSIG) {
469 lwkt_reltoken(&p->p_token);
476 if (req == PT_STEP) {
477 if ((error = ptrace_single_step (lp))) {
479 lwkt_reltoken(&p->p_token);
485 if (addr != (void *)1) {
486 if ((error = ptrace_set_pc (lp, (u_long)addr))) {
488 lwkt_reltoken(&p->p_token);
495 if (req == PT_DETACH) {
496 /* reset process parent */
497 if (p->p_oppid != p->p_pptr->p_pid) {
500 pp = pfind(p->p_oppid);
502 proc_reparent(p, pp);
507 p->p_flags &= ~(P_TRACED | P_WAITED);
510 /* should we send SIGCHLD? */
515 * Deliver or queue signal. If the process is stopped
516 * force it to be SACTIVE again.
519 if (p->p_stat == SSTOP) {
521 proc_unstop(p, SSTOP);
526 lwkt_reltoken(&p->p_token);
537 * NOTE! uio_offset represents the offset in the target
538 * process. The iov is in the current process (the guy
539 * making the ptrace call) so uio_td must be the current
540 * process (though for a SYSSPACE transfer it doesn't
544 /* write = 0 set above */
545 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
546 iov.iov_len = sizeof(int);
549 uio.uio_offset = (off_t)(uintptr_t)addr;
550 uio.uio_resid = sizeof(int);
551 uio.uio_segflg = UIO_SYSSPACE;
552 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
553 uio.uio_td = curthread;
554 error = procfs_domem(curp, lp, NULL, &uio);
555 if (uio.uio_resid != 0) {
557 * XXX procfs_domem() doesn't currently return ENOSPC,
558 * so I think write() can bogusly return 0.
559 * XXX what happens for short writes? We don't want
560 * to write partial data.
561 * XXX procfs_domem() returns EPERM for other invalid
562 * addresses. Convert this to EINVAL. Does this
563 * clobber returns of EPERM for other reasons?
565 if (error == 0 || error == ENOSPC || error == EPERM)
566 error = EINVAL; /* EOF */
570 lwkt_reltoken(&p->p_token);
576 * NOTE! uio_offset represents the offset in the target
577 * process. The iov is in the current process (the guy
578 * making the ptrace call) so uio_td must be the current
582 iov.iov_base = piod->piod_addr;
583 iov.iov_len = piod->piod_len;
586 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
587 uio.uio_resid = piod->piod_len;
588 uio.uio_segflg = UIO_USERSPACE;
589 uio.uio_td = curthread;
590 switch (piod->piod_op) {
593 uio.uio_rw = UIO_READ;
597 uio.uio_rw = UIO_WRITE;
600 lwkt_reltoken(&p->p_token);
604 error = procfs_domem(curp, lp, NULL, &uio);
605 piod->piod_len -= uio.uio_resid;
606 lwkt_reltoken(&p->p_token);
612 goto sendsig; /* in PT_CONTINUE above */
618 #endif /* PT_SETREGS */
621 /* write = 0 above */
622 #endif /* PT_SETREGS */
623 #if defined(PT_SETREGS) || defined(PT_GETREGS)
624 if (!procfs_validregs(lp)) {
625 lwkt_reltoken(&p->p_token);
630 iov.iov_len = sizeof(struct reg);
634 uio.uio_resid = sizeof(struct reg);
635 uio.uio_segflg = UIO_SYSSPACE;
636 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
637 uio.uio_td = curthread;
638 t = procfs_doregs(curp, lp, NULL, &uio);
639 lwkt_reltoken(&p->p_token);
643 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
649 #endif /* PT_SETFPREGS */
652 /* write = 0 above */
653 #endif /* PT_SETFPREGS */
654 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
655 if (!procfs_validfpregs(lp)) {
656 lwkt_reltoken(&p->p_token);
661 iov.iov_len = sizeof(struct fpreg);
665 uio.uio_resid = sizeof(struct fpreg);
666 uio.uio_segflg = UIO_SYSSPACE;
667 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
668 uio.uio_td = curthread;
669 t = procfs_dofpregs(curp, lp, NULL, &uio);
670 lwkt_reltoken(&p->p_token);
674 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
680 #endif /* PT_SETDBREGS */
683 /* write = 0 above */
684 #endif /* PT_SETDBREGS */
685 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
686 if (!procfs_validdbregs(lp)) {
687 lwkt_reltoken(&p->p_token);
692 iov.iov_len = sizeof(struct dbreg);
696 uio.uio_resid = sizeof(struct dbreg);
697 uio.uio_segflg = UIO_SYSSPACE;
698 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
699 uio.uio_td = curthread;
700 t = procfs_dodbregs(curp, lp, NULL, &uio);
701 lwkt_reltoken(&p->p_token);
705 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
711 lwkt_reltoken(&p->p_token);
718 trace_req(struct proc *p)
726 * Stop a process because of a procfs event. Stay stopped until p->p_step
727 * is cleared (cleared by PIOCCONT in procfs).
732 stopevent(struct proc *p, unsigned int event, unsigned int val)
735 * Set event info. Recheck p_stops in case we are
736 * racing a close() on procfs.
738 spin_lock(&p->p_spin);
739 if ((p->p_stops & event) == 0) {
740 spin_unlock(&p->p_spin);
746 tsleep_interlock(&p->p_step, 0);
747 spin_unlock(&p->p_spin);
750 * Wakeup any PIOCWAITing procs and wait for p_step to
755 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
756 spin_lock(&p->p_spin);
757 if (p->p_step == 0) {
758 spin_unlock(&p->p_spin);
761 tsleep_interlock(&p->p_step, 0);
762 spin_unlock(&p->p_spin);