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>
53 #include <sys/thread2.h>
54 #include <sys/spinlock2.h>
56 /* use the equivalent procfs code */
59 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 &object, &pindex, &out_prot, &wired);
82 if (rv != KERN_SUCCESS)
85 vm_map_lookup_done (tmap, out_entry, 0);
87 /* Find space in kernel_map for the page we're interested in */
88 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
92 VM_PROT_ALL, VM_PROT_ALL,
96 vm_object_reference (object);
98 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
101 bcopy ((caddr_t)kva + page_offset,
102 retval, sizeof *retval);
104 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
111 pwrite (struct proc *procp, unsigned int addr, unsigned int datum) {
116 int page_offset; /* offset into page */
117 vm_offset_t pageno; /* page number */
118 vm_map_entry_t out_entry;
122 boolean_t fix_prot = 0;
124 /* Map page into kernel space */
126 map = &procp->p_vmspace->vm_map;
128 page_offset = addr - trunc_page(addr);
129 pageno = trunc_page(addr);
132 * Check the permissions for the area we're interested in.
135 if (vm_map_check_protection (map, pageno, pageno + PAGE_SIZE,
136 VM_PROT_WRITE, FALSE) == FALSE) {
138 * If the page was not writable, we make it so.
139 * XXX It is possible a page may *not* be read/executable,
140 * if a process changes that!
143 /* The page isn't writable, so let's try making it so... */
144 if ((rv = vm_map_protect (map, pageno, pageno + PAGE_SIZE,
145 VM_PROT_ALL, 0)) != KERN_SUCCESS)
146 return EFAULT; /* I guess... */
150 * Now we need to get the page. out_entry, out_prot, wired, and
151 * single_use aren't used. One would think the vm code would be
152 * a *bit* nicer... We use tmap because vm_map_lookup() can
153 * change the map argument.
157 rv = vm_map_lookup (&tmap, pageno, VM_PROT_WRITE, &out_entry,
158 &object, &pindex, &out_prot, &wired);
159 if (rv != KERN_SUCCESS) {
164 * Okay, we've got the page. Let's release tmap.
167 vm_map_lookup_done (tmap, out_entry, 0);
170 * Fault the page in...
173 rv = vm_fault(map, pageno, VM_PROT_WRITE|VM_PROT_READ, FALSE);
174 if (rv != KERN_SUCCESS)
177 /* Find space in kernel_map for the page we're interested in */
178 rv = vm_map_find (&kernel_map, object, IDX_TO_OFF(pindex),
180 PAGE_SIZE, PAGE_SIZE,
181 0, VM_MAPTYPE_NORMAL,
182 VM_PROT_ALL, VM_PROT_ALL,
185 vm_object_reference (object);
187 rv = vm_map_wire (&kernel_map, kva, kva + PAGE_SIZE, 0);
189 bcopy (&datum, (caddr_t)kva + page_offset, sizeof datum);
191 vm_map_remove (&kernel_map, kva, kva + PAGE_SIZE);
195 vm_map_protect (map, pageno, pageno + PAGE_SIZE,
196 VM_PROT_READ|VM_PROT_EXECUTE, 0);
202 * Process debugging system call.
207 sys_ptrace(struct ptrace_args *uap)
209 struct proc *p = curproc;
212 * XXX this obfuscation is to reduce stack usage, but the register
213 * structs may be too large to put on the stack anyway.
216 struct ptrace_io_desc piod;
233 error = copyin(uap->addr, &r.reg, sizeof r.reg);
236 error = copyin(uap->addr, &r.fpreg, sizeof r.fpreg);
240 error = copyin(uap->addr, &r.dbreg, sizeof r.dbreg);
244 error = copyin(uap->addr, &r.piod, sizeof r.piod);
252 error = kern_ptrace(p, uap->req, uap->pid, addr, uap->data,
253 &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,
285 struct ptrace_io_desc *piod;
290 lwkt_gettoken(&proc_token);
293 if (req == PT_TRACE_ME) {
297 if ((p = pfind(pid)) == NULL) {
298 lwkt_reltoken(&proc_token);
302 if (!PRISON_CHECK(curp->p_ucred, p->p_ucred)) {
304 lwkt_reltoken(&proc_token);
308 lwkt_gettoken(&p->p_token);
309 /* Can't trace a process that's currently exec'ing. */
310 if ((p->p_flag & P_INEXEC) != 0) {
311 lwkt_reltoken(&p->p_token);
313 lwkt_reltoken(&proc_token);
327 if (p->p_pid == curp->p_pid) {
328 lwkt_reltoken(&p->p_token);
330 lwkt_reltoken(&proc_token);
335 if (p->p_flag & P_TRACED) {
336 lwkt_reltoken(&p->p_token);
338 lwkt_reltoken(&proc_token);
342 if (curp->p_flag & P_TRACED)
343 for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr)
345 lwkt_reltoken(&p->p_token);
347 lwkt_reltoken(&proc_token);
351 /* not owned by you, has done setuid (unless you're root) */
352 if ((p->p_ucred->cr_ruid != curp->p_ucred->cr_ruid) ||
353 (p->p_flag & P_SUGID)) {
354 if ((error = priv_check_cred(curp->p_ucred, PRIV_ROOT, 0)) != 0) {
355 lwkt_reltoken(&p->p_token);
357 lwkt_reltoken(&proc_token);
362 /* can't trace init when securelevel > 0 */
363 if (securelevel > 0 && p->p_pid == 1) {
364 lwkt_reltoken(&p->p_token);
366 lwkt_reltoken(&proc_token);
400 /* not being traced... */
401 if ((p->p_flag & P_TRACED) == 0) {
402 lwkt_reltoken(&p->p_token);
404 lwkt_reltoken(&proc_token);
408 /* not being traced by YOU */
409 if (p->p_pptr != curp) {
410 lwkt_reltoken(&p->p_token);
412 lwkt_reltoken(&proc_token);
416 /* not currently stopped */
417 if (p->p_stat != SSTOP ||
418 (p->p_flag & P_WAITED) == 0) {
419 lwkt_reltoken(&p->p_token);
421 lwkt_reltoken(&proc_token);
429 lwkt_reltoken(&p->p_token);
431 lwkt_reltoken(&proc_token);
436 lp = FIRST_LWP_IN_PROC(p);
439 * Single step fixup ala procfs
445 * Actually do the requests
452 /* set my trace flag and "owner" so it can read/write me */
453 p->p_flag |= P_TRACED;
454 p->p_oppid = p->p_pptr->p_pid;
455 lwkt_reltoken(&p->p_token);
457 lwkt_reltoken(&proc_token);
461 /* security check done above */
462 p->p_flag |= P_TRACED;
463 p->p_oppid = p->p_pptr->p_pid;
464 if (p->p_pptr != curp)
465 proc_reparent(p, curp);
467 goto sendsig; /* in PT_CONTINUE below */
472 /* Zero means do not send any signal */
473 if (data < 0 || data > _SIG_MAXSIG) {
474 lwkt_reltoken(&p->p_token);
476 lwkt_reltoken(&proc_token);
482 if (req == PT_STEP) {
483 if ((error = ptrace_single_step (lp))) {
485 lwkt_reltoken(&p->p_token);
487 lwkt_reltoken(&proc_token);
492 if (addr != (void *)1) {
493 if ((error = ptrace_set_pc (lp,
494 (u_long)(uintfptr_t)addr))) {
496 lwkt_reltoken(&p->p_token);
498 lwkt_reltoken(&proc_token);
504 if (req == PT_DETACH) {
505 /* reset process parent */
506 if (p->p_oppid != p->p_pptr->p_pid) {
509 pp = pfind(p->p_oppid);
510 proc_reparent(p, pp ? pp : initproc);
513 p->p_flag &= ~(P_TRACED | P_WAITED);
516 /* should we send SIGCHLD? */
521 * Deliver or queue signal. If the process is stopped
522 * force it to be SACTIVE again.
525 if (p->p_stat == SSTOP) {
527 lp->lwp_flag |= LWP_BREAKTSLEEP;
533 lwkt_reltoken(&p->p_token);
535 lwkt_reltoken(&proc_token);
545 * NOTE! uio_offset represents the offset in the target
546 * process. The iov is in the current process (the guy
547 * making the ptrace call) so uio_td must be the current
548 * process (though for a SYSSPACE transfer it doesn't
552 /* write = 0 set above */
553 iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
554 iov.iov_len = sizeof(int);
557 uio.uio_offset = (off_t)(uintptr_t)addr;
558 uio.uio_resid = sizeof(int);
559 uio.uio_segflg = UIO_SYSSPACE;
560 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
561 uio.uio_td = curthread;
562 error = procfs_domem(curp, lp, NULL, &uio);
563 if (uio.uio_resid != 0) {
565 * XXX procfs_domem() doesn't currently return ENOSPC,
566 * so I think write() can bogusly return 0.
567 * XXX what happens for short writes? We don't want
568 * to write partial data.
569 * XXX procfs_domem() returns EPERM for other invalid
570 * addresses. Convert this to EINVAL. Does this
571 * clobber returns of EPERM for other reasons?
573 if (error == 0 || error == ENOSPC || error == EPERM)
574 error = EINVAL; /* EOF */
578 lwkt_reltoken(&p->p_token);
580 lwkt_reltoken(&proc_token);
585 * NOTE! uio_offset represents the offset in the target
586 * process. The iov is in the current process (the guy
587 * making the ptrace call) so uio_td must be the current
591 iov.iov_base = piod->piod_addr;
592 iov.iov_len = piod->piod_len;
595 uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
596 uio.uio_resid = piod->piod_len;
597 uio.uio_segflg = UIO_USERSPACE;
598 uio.uio_td = curthread;
599 switch (piod->piod_op) {
602 uio.uio_rw = UIO_READ;
606 uio.uio_rw = UIO_WRITE;
609 lwkt_reltoken(&p->p_token);
611 lwkt_reltoken(&proc_token);
614 error = procfs_domem(curp, lp, NULL, &uio);
615 piod->piod_len -= uio.uio_resid;
616 lwkt_reltoken(&p->p_token);
618 lwkt_reltoken(&proc_token);
623 goto sendsig; /* in PT_CONTINUE above */
629 #endif /* PT_SETREGS */
632 /* write = 0 above */
633 #endif /* PT_SETREGS */
634 #if defined(PT_SETREGS) || defined(PT_GETREGS)
635 if (!procfs_validregs(lp)) { /* no P_SYSTEM procs please */
636 lwkt_reltoken(&p->p_token);
638 lwkt_reltoken(&proc_token);
642 iov.iov_len = sizeof(struct reg);
646 uio.uio_resid = sizeof(struct reg);
647 uio.uio_segflg = UIO_SYSSPACE;
648 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
649 uio.uio_td = curthread;
650 t = procfs_doregs(curp, lp, NULL, &uio);
651 lwkt_reltoken(&p->p_token);
653 lwkt_reltoken(&proc_token);
656 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
662 #endif /* PT_SETFPREGS */
665 /* write = 0 above */
666 #endif /* PT_SETFPREGS */
667 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
668 if (!procfs_validfpregs(lp)) { /* no P_SYSTEM procs please */
669 lwkt_reltoken(&p->p_token);
671 lwkt_reltoken(&proc_token);
675 iov.iov_len = sizeof(struct fpreg);
679 uio.uio_resid = sizeof(struct fpreg);
680 uio.uio_segflg = UIO_SYSSPACE;
681 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
682 uio.uio_td = curthread;
683 t = procfs_dofpregs(curp, lp, NULL, &uio);
684 lwkt_reltoken(&p->p_token);
686 lwkt_reltoken(&proc_token);
689 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
695 #endif /* PT_SETDBREGS */
698 /* write = 0 above */
699 #endif /* PT_SETDBREGS */
700 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
701 if (!procfs_validdbregs(lp)) { /* no P_SYSTEM procs please */
702 lwkt_reltoken(&p->p_token);
704 lwkt_reltoken(&proc_token);
708 iov.iov_len = sizeof(struct dbreg);
712 uio.uio_resid = sizeof(struct dbreg);
713 uio.uio_segflg = UIO_SYSSPACE;
714 uio.uio_rw = write ? UIO_WRITE : UIO_READ;
715 uio.uio_td = curthread;
716 t = procfs_dodbregs(curp, lp, NULL, &uio);
717 lwkt_reltoken(&p->p_token);
719 lwkt_reltoken(&proc_token);
722 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
728 lwkt_reltoken(&p->p_token);
730 lwkt_reltoken(&proc_token);
736 trace_req(struct proc *p)
744 * Stop a process because of a procfs event. Stay stopped until p->p_step
745 * is cleared (cleared by PIOCCONT in procfs).
750 stopevent(struct proc *p, unsigned int event, unsigned int val)
753 * Set event info. Recheck p_stops in case we are
754 * racing a close() on procfs.
756 spin_lock(&p->p_spin);
757 if ((p->p_stops & event) == 0) {
758 spin_unlock(&p->p_spin);
764 tsleep_interlock(&p->p_step, 0);
765 spin_unlock(&p->p_spin);
768 * Wakeup any PIOCWAITing procs and wait for p_step to
773 tsleep(&p->p_step, PINTERLOCKED, "stopevent", 0);
774 spin_lock(&p->p_spin);
775 if (p->p_step == 0) {
776 spin_unlock(&p->p_spin);
779 tsleep_interlock(&p->p_step, 0);
780 spin_unlock(&p->p_spin);