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/mplock2.h>
55 #include <sys/spinlock2.h>
57 /* use the equivalent procfs code */
60 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 &object
, &pindex
, &out_prot
, &wired
);
83 if (rv
!= KERN_SUCCESS
)
86 vm_map_lookup_done (tmap
, out_entry
, 0);
88 /* Find space in kernel_map for the page we're interested in */
89 rv
= vm_map_find (&kernel_map
, object
, IDX_TO_OFF(pindex
),
93 VM_PROT_ALL
, VM_PROT_ALL
,
97 vm_object_reference (object
);
99 rv
= vm_map_wire (&kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
102 bcopy ((caddr_t
)kva
+ page_offset
,
103 retval
, sizeof *retval
);
105 vm_map_remove (&kernel_map
, kva
, kva
+ PAGE_SIZE
);
112 pwrite (struct proc
*procp
, unsigned int addr
, unsigned int datum
) {
117 int page_offset
; /* offset into page */
118 vm_offset_t pageno
; /* page number */
119 vm_map_entry_t out_entry
;
123 boolean_t fix_prot
= 0;
125 /* Map page into kernel space */
127 map
= &procp
->p_vmspace
->vm_map
;
129 page_offset
= addr
- trunc_page(addr
);
130 pageno
= trunc_page(addr
);
133 * Check the permissions for the area we're interested in.
136 if (vm_map_check_protection (map
, pageno
, pageno
+ PAGE_SIZE
,
137 VM_PROT_WRITE
) == FALSE
) {
139 * If the page was not writable, we make it so.
140 * XXX It is possible a page may *not* be read/executable,
141 * if a process changes that!
144 /* The page isn't writable, so let's try making it so... */
145 if ((rv
= vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
146 VM_PROT_ALL
, 0)) != KERN_SUCCESS
)
147 return EFAULT
; /* I guess... */
151 * Now we need to get the page. out_entry, out_prot, wired, and
152 * single_use aren't used. One would think the vm code would be
153 * a *bit* nicer... We use tmap because vm_map_lookup() can
154 * change the map argument.
158 rv
= vm_map_lookup (&tmap
, pageno
, VM_PROT_WRITE
, &out_entry
,
159 &object
, &pindex
, &out_prot
, &wired
);
160 if (rv
!= KERN_SUCCESS
) {
165 * Okay, we've got the page. Let's release tmap.
168 vm_map_lookup_done (tmap
, out_entry
, 0);
171 * Fault the page in...
174 rv
= vm_fault(map
, pageno
, VM_PROT_WRITE
|VM_PROT_READ
, FALSE
);
175 if (rv
!= KERN_SUCCESS
)
178 /* Find space in kernel_map for the page we're interested in */
179 rv
= vm_map_find (&kernel_map
, object
, IDX_TO_OFF(pindex
),
181 PAGE_SIZE
, PAGE_SIZE
,
182 0, VM_MAPTYPE_NORMAL
,
183 VM_PROT_ALL
, VM_PROT_ALL
,
186 vm_object_reference (object
);
188 rv
= vm_map_wire (&kernel_map
, kva
, kva
+ PAGE_SIZE
, 0);
190 bcopy (&datum
, (caddr_t
)kva
+ page_offset
, sizeof datum
);
192 vm_map_remove (&kernel_map
, kva
, kva
+ PAGE_SIZE
);
196 vm_map_protect (map
, pageno
, pageno
+ PAGE_SIZE
,
197 VM_PROT_READ
|VM_PROT_EXECUTE
, 0);
203 * Process debugging system call.
208 sys_ptrace(struct ptrace_args
*uap
)
210 struct proc
*p
= curproc
;
213 * XXX this obfuscation is to reduce stack usage, but the register
214 * structs may be too large to put on the stack anyway.
217 struct ptrace_io_desc piod
;
234 error
= copyin(uap
->addr
, &r
.reg
, sizeof r
.reg
);
237 error
= copyin(uap
->addr
, &r
.fpreg
, sizeof r
.fpreg
);
241 error
= copyin(uap
->addr
, &r
.dbreg
, sizeof r
.dbreg
);
245 error
= copyin(uap
->addr
, &r
.piod
, sizeof r
.piod
);
254 error
= kern_ptrace(p
, uap
->req
, uap
->pid
, addr
, uap
->data
,
255 &uap
->sysmsg_result
);
262 (void)copyout(&r
.piod
, uap
->addr
, sizeof r
.piod
);
265 error
= copyout(&r
.reg
, uap
->addr
, sizeof r
.reg
);
268 error
= copyout(&r
.fpreg
, uap
->addr
, sizeof r
.fpreg
);
272 error
= copyout(&r
.dbreg
, uap
->addr
, sizeof r
.dbreg
);
281 kern_ptrace(struct proc
*curp
, int req
, pid_t pid
, void *addr
, int data
, int *res
)
287 struct ptrace_io_desc
*piod
;
292 if (req
== PT_TRACE_ME
) {
295 if ((p
= pfind(pid
)) == NULL
)
298 if (!PRISON_CHECK(curp
->p_ucred
, p
->p_ucred
))
301 /* Can't trace a process that's currently exec'ing. */
302 if ((p
->p_flag
& P_INEXEC
) != 0)
315 if (p
->p_pid
== curp
->p_pid
)
319 if (p
->p_flag
& P_TRACED
)
322 if (curp
->p_flag
& P_TRACED
)
323 for (pp
= curp
->p_pptr
; pp
!= NULL
; pp
= pp
->p_pptr
)
327 /* not owned by you, has done setuid (unless you're root) */
328 if ((p
->p_ucred
->cr_ruid
!= curp
->p_ucred
->cr_ruid
) ||
329 (p
->p_flag
& P_SUGID
)) {
330 if ((error
= priv_check_cred(curp
->p_ucred
, PRIV_ROOT
, 0)) != 0)
334 /* can't trace init when securelevel > 0 */
335 if (securelevel
> 0 && p
->p_pid
== 1)
368 /* not being traced... */
369 if ((p
->p_flag
& P_TRACED
) == 0)
372 /* not being traced by YOU */
373 if (p
->p_pptr
!= curp
)
376 /* not currently stopped */
377 if (p
->p_stat
!= SSTOP
||
378 (p
->p_flag
& P_WAITED
) == 0) {
390 lp
= FIRST_LWP_IN_PROC(p
);
393 * Single step fixup ala procfs
399 * Actually do the requests
406 /* set my trace flag and "owner" so it can read/write me */
407 p
->p_flag
|= P_TRACED
;
408 p
->p_oppid
= p
->p_pptr
->p_pid
;
412 /* security check done above */
413 p
->p_flag
|= P_TRACED
;
414 p
->p_oppid
= p
->p_pptr
->p_pid
;
415 if (p
->p_pptr
!= curp
)
416 proc_reparent(p
, curp
);
418 goto sendsig
; /* in PT_CONTINUE below */
423 /* Zero means do not send any signal */
424 if (data
< 0 || data
> _SIG_MAXSIG
)
429 if (req
== PT_STEP
) {
430 if ((error
= ptrace_single_step (lp
))) {
436 if (addr
!= (void *)1) {
437 if ((error
= ptrace_set_pc (lp
,
438 (u_long
)(uintfptr_t
)addr
))) {
445 if (req
== PT_DETACH
) {
446 /* reset process parent */
447 if (p
->p_oppid
!= p
->p_pptr
->p_pid
) {
450 pp
= pfind(p
->p_oppid
);
451 proc_reparent(p
, pp
? pp
: initproc
);
454 p
->p_flag
&= ~(P_TRACED
| P_WAITED
);
457 /* should we send SIGCHLD? */
462 * Deliver or queue signal. If the process is stopped
463 * force it to be SACTIVE again.
466 if (p
->p_stat
== SSTOP
) {
468 lp
->lwp_flag
|= LWP_BREAKTSLEEP
;
483 * NOTE! uio_offset represents the offset in the target
484 * process. The iov is in the current process (the guy
485 * making the ptrace call) so uio_td must be the current
486 * process (though for a SYSSPACE transfer it doesn't
490 /* write = 0 set above */
491 iov
.iov_base
= write
? (caddr_t
)&data
: (caddr_t
)&tmp
;
492 iov
.iov_len
= sizeof(int);
495 uio
.uio_offset
= (off_t
)(uintptr_t)addr
;
496 uio
.uio_resid
= sizeof(int);
497 uio
.uio_segflg
= UIO_SYSSPACE
;
498 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
499 uio
.uio_td
= curthread
;
500 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
501 if (uio
.uio_resid
!= 0) {
503 * XXX procfs_domem() doesn't currently return ENOSPC,
504 * so I think write() can bogusly return 0.
505 * XXX what happens for short writes? We don't want
506 * to write partial data.
507 * XXX procfs_domem() returns EPERM for other invalid
508 * addresses. Convert this to EINVAL. Does this
509 * clobber returns of EPERM for other reasons?
511 if (error
== 0 || error
== ENOSPC
|| error
== EPERM
)
512 error
= EINVAL
; /* EOF */
520 * NOTE! uio_offset represents the offset in the target
521 * process. The iov is in the current process (the guy
522 * making the ptrace call) so uio_td must be the current
526 iov
.iov_base
= piod
->piod_addr
;
527 iov
.iov_len
= piod
->piod_len
;
530 uio
.uio_offset
= (off_t
)(uintptr_t)piod
->piod_offs
;
531 uio
.uio_resid
= piod
->piod_len
;
532 uio
.uio_segflg
= UIO_USERSPACE
;
533 uio
.uio_td
= curthread
;
534 switch (piod
->piod_op
) {
537 uio
.uio_rw
= UIO_READ
;
541 uio
.uio_rw
= UIO_WRITE
;
546 error
= procfs_domem(curp
, lp
, NULL
, &uio
);
547 piod
->piod_len
-= uio
.uio_resid
;
552 goto sendsig
; /* in PT_CONTINUE above */
558 #endif /* PT_SETREGS */
561 /* write = 0 above */
562 #endif /* PT_SETREGS */
563 #if defined(PT_SETREGS) || defined(PT_GETREGS)
564 if (!procfs_validregs(lp
)) /* no P_SYSTEM procs please */
568 iov
.iov_len
= sizeof(struct reg
);
572 uio
.uio_resid
= sizeof(struct reg
);
573 uio
.uio_segflg
= UIO_SYSSPACE
;
574 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
575 uio
.uio_td
= curthread
;
576 return (procfs_doregs(curp
, lp
, NULL
, &uio
));
578 #endif /* defined(PT_SETREGS) || defined(PT_GETREGS) */
584 #endif /* PT_SETFPREGS */
587 /* write = 0 above */
588 #endif /* PT_SETFPREGS */
589 #if defined(PT_SETFPREGS) || defined(PT_GETFPREGS)
590 if (!procfs_validfpregs(lp
)) /* no P_SYSTEM procs please */
594 iov
.iov_len
= sizeof(struct fpreg
);
598 uio
.uio_resid
= sizeof(struct fpreg
);
599 uio
.uio_segflg
= UIO_SYSSPACE
;
600 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
601 uio
.uio_td
= curthread
;
602 return (procfs_dofpregs(curp
, lp
, NULL
, &uio
));
604 #endif /* defined(PT_SETFPREGS) || defined(PT_GETFPREGS) */
610 #endif /* PT_SETDBREGS */
613 /* write = 0 above */
614 #endif /* PT_SETDBREGS */
615 #if defined(PT_SETDBREGS) || defined(PT_GETDBREGS)
616 if (!procfs_validdbregs(lp
)) /* no P_SYSTEM procs please */
620 iov
.iov_len
= sizeof(struct dbreg
);
624 uio
.uio_resid
= sizeof(struct dbreg
);
625 uio
.uio_segflg
= UIO_SYSSPACE
;
626 uio
.uio_rw
= write
? UIO_WRITE
: UIO_READ
;
627 uio
.uio_td
= curthread
;
628 return (procfs_dodbregs(curp
, lp
, NULL
, &uio
));
630 #endif /* defined(PT_SETDBREGS) || defined(PT_GETDBREGS) */
640 trace_req(struct proc
*p
)
648 * Stop a process because of a procfs event. Stay stopped until p->p_step
649 * is cleared (cleared by PIOCCONT in procfs).
654 stopevent(struct proc
*p
, unsigned int event
, unsigned int val
)
657 * Set event info. Recheck p_stops in case we are
658 * racing a close() on procfs.
660 spin_lock_wr(&p
->p_spin
);
661 if ((p
->p_stops
& event
) == 0) {
662 spin_unlock_wr(&p
->p_spin
);
668 tsleep_interlock(&p
->p_step
, 0);
669 spin_unlock_wr(&p
->p_spin
);
672 * Wakeup any PIOCWAITing procs and wait for p_step to
677 tsleep(&p
->p_step
, PINTERLOCKED
, "stopevent", 0);
678 spin_lock_wr(&p
->p_spin
);
679 if (p
->p_step
== 0) {
680 spin_unlock_wr(&p
->p_spin
);
683 tsleep_interlock(&p
->p_step
, 0);
684 spin_unlock_wr(&p
->p_spin
);