2005-01-06 23:35:40 +00:00
|
|
|
/*-
|
1994-08-08 13:00:27 +00:00
|
|
|
* Copyright (c) 1994, Sean Eric Fagan
|
|
|
|
* All rights reserved.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
1994-08-08 13:00:27 +00:00
|
|
|
* This product includes software developed by Sean Eric Fagan.
|
|
|
|
* 4. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
1994-05-24 10:09:53 +00:00
|
|
|
*
|
1994-08-08 13:00:27 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
1994-05-24 10:09:53 +00:00
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
1994-08-08 13:00:27 +00:00
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
1994-05-24 10:09:53 +00:00
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2003-06-11 00:56:59 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#include "opt_compat.h"
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/param.h>
|
1994-08-18 22:36:09 +00:00
|
|
|
#include <sys/systm.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
#include <sys/lock.h>
|
|
|
|
#include <sys/mutex.h>
|
2002-09-05 01:02:50 +00:00
|
|
|
#include <sys/syscallsubr.h>
|
2009-03-02 18:43:50 +00:00
|
|
|
#include <sys/sysent.h>
|
1995-11-12 06:43:28 +00:00
|
|
|
#include <sys/sysproto.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/proc.h>
|
1994-08-08 13:00:27 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/ptrace.h>
|
2001-03-28 11:52:56 +00:00
|
|
|
#include <sys/sx.h>
|
2004-07-13 07:25:24 +00:00
|
|
|
#include <sys/malloc.h>
|
2004-11-27 06:51:39 +00:00
|
|
|
#include <sys/signalvar.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1994-08-08 13:00:27 +00:00
|
|
|
#include <machine/reg.h>
|
2001-05-01 08:13:21 +00:00
|
|
|
|
2006-02-14 01:18:31 +00:00
|
|
|
#include <security/audit/audit.h>
|
|
|
|
|
1994-08-08 13:00:27 +00:00
|
|
|
#include <vm/vm.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/pmap.h>
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
#include <vm/vm_extern.h>
|
1995-12-07 12:48:31 +00:00
|
|
|
#include <vm/vm_map.h>
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
#include <vm/vm_kern.h>
|
|
|
|
#include <vm/vm_object.h>
|
1994-08-08 13:00:27 +00:00
|
|
|
#include <vm/vm_page.h>
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
#include <vm/vm_param.h>
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
#include <sys/procfs.h>
|
|
|
|
#include <machine/fpu.h>
|
|
|
|
#include <compat/ia32/ia32_reg.h>
|
|
|
|
|
|
|
|
struct ptrace_io_desc32 {
|
|
|
|
int piod_op;
|
|
|
|
u_int32_t piod_offs;
|
|
|
|
u_int32_t piod_addr;
|
|
|
|
u_int32_t piod_len;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2002-02-21 04:37:55 +00:00
|
|
|
/*
|
|
|
|
* Functions implemented using PROC_ACTION():
|
|
|
|
*
|
|
|
|
* proc_read_regs(proc, regs)
|
|
|
|
* Get the current user-visible register set from the process
|
|
|
|
* and copy it into the regs structure (<machine/reg.h>).
|
|
|
|
* The process is stopped at the time read_regs is called.
|
|
|
|
*
|
|
|
|
* proc_write_regs(proc, regs)
|
|
|
|
* Update the current register set from the passed in regs
|
|
|
|
* structure. Take care to avoid clobbering special CPU
|
|
|
|
* registers or privileged bits in the PSL.
|
|
|
|
* Depending on the architecture this may have fix-up work to do,
|
|
|
|
* especially if the IAR or PCW are modified.
|
|
|
|
* The process is stopped at the time write_regs is called.
|
|
|
|
*
|
|
|
|
* proc_read_fpregs, proc_write_fpregs
|
|
|
|
* deal with the floating point register set, otherwise as above.
|
|
|
|
*
|
|
|
|
* proc_read_dbregs, proc_write_dbregs
|
|
|
|
* deal with the processor debug register set, otherwise as above.
|
|
|
|
*
|
|
|
|
* proc_sstep(proc)
|
|
|
|
* Arrange for the process to trap after executing a single instruction.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define PROC_ACTION(action) do { \
|
2001-10-21 23:57:24 +00:00
|
|
|
int error; \
|
|
|
|
\
|
2003-04-22 20:01:56 +00:00
|
|
|
PROC_LOCK_ASSERT(td->td_proc, MA_OWNED); \
|
2007-09-17 05:31:39 +00:00
|
|
|
if ((td->td_proc->p_flag & P_INMEM) == 0) \
|
2002-02-21 04:37:55 +00:00
|
|
|
error = EIO; \
|
|
|
|
else \
|
|
|
|
error = (action); \
|
2001-10-21 23:57:24 +00:00
|
|
|
return (error); \
|
2002-02-21 04:37:55 +00:00
|
|
|
} while(0)
|
2003-03-19 00:33:38 +00:00
|
|
|
|
2002-02-21 04:37:55 +00:00
|
|
|
int
|
|
|
|
proc_read_regs(struct thread *td, struct reg *regs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_regs(td, regs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_write_regs(struct thread *td, struct reg *regs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_regs(td, regs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_read_dbregs(struct thread *td, struct dbreg *dbregs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_dbregs(td, dbregs));
|
2001-10-21 23:57:24 +00:00
|
|
|
}
|
|
|
|
|
2002-02-21 04:37:55 +00:00
|
|
|
int
|
|
|
|
proc_write_dbregs(struct thread *td, struct dbreg *dbregs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_dbregs(td, dbregs));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Ptrace doesn't support fpregs at all, and there are no security holes
|
|
|
|
* or translations for fpregs, so we can just copy them.
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
proc_read_fpregs(struct thread *td, struct fpreg *fpregs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_fpregs(td, fpregs));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_write_fpregs(struct thread *td, struct fpreg *fpregs)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_fpregs(td, fpregs));
|
|
|
|
}
|
2001-10-21 23:57:24 +00:00
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
/* For 32 bit binaries, we need to expose the 32 bit regs layouts. */
|
|
|
|
int
|
|
|
|
proc_read_regs32(struct thread *td, struct reg32 *regs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_regs32(td, regs32));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_write_regs32(struct thread *td, struct reg32 *regs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_regs32(td, regs32));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_read_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_dbregs32(td, dbregs32));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_write_dbregs32(struct thread *td, struct dbreg32 *dbregs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_dbregs32(td, dbregs32));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_read_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(fill_fpregs32(td, fpregs32));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
proc_write_fpregs32(struct thread *td, struct fpreg32 *fpregs32)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_ACTION(set_fpregs32(td, fpregs32));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2001-10-21 23:57:24 +00:00
|
|
|
int
|
|
|
|
proc_sstep(struct thread *td)
|
|
|
|
{
|
|
|
|
|
2002-02-21 04:37:55 +00:00
|
|
|
PROC_ACTION(ptrace_single_step(td));
|
2001-10-21 23:57:24 +00:00
|
|
|
}
|
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
int
|
|
|
|
proc_rwmem(struct proc *p, struct uio *uio)
|
2001-10-04 16:35:44 +00:00
|
|
|
{
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
vm_map_t map;
|
2003-06-11 06:43:48 +00:00
|
|
|
vm_object_t backing_object, object = NULL;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
vm_offset_t pageno = 0; /* page number */
|
|
|
|
vm_prot_t reqprot;
|
2007-11-08 19:35:36 +00:00
|
|
|
int error, fault_flags, writing;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
* Assert that someone has locked this vmspace. (Should be
|
|
|
|
* curthread but we can't assert that.) This keeps the process
|
|
|
|
* from exiting out from under us until this operation completes.
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
*/
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
KASSERT(p->p_lock >= 1, ("%s: process %p (pid %d) not held", __func__,
|
|
|
|
p, p->p_pid));
|
2004-07-27 03:53:41 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* The map we want...
|
|
|
|
*/
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
map = &p->p_vmspace->vm_map;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
writing = uio->uio_rw == UIO_WRITE;
|
|
|
|
reqprot = writing ? (VM_PROT_WRITE | VM_PROT_OVERRIDE_WRITE) :
|
|
|
|
VM_PROT_READ;
|
2007-11-08 19:35:36 +00:00
|
|
|
fault_flags = writing ? VM_FAULT_DIRTY : VM_FAULT_NORMAL;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* Only map in one page at a time. We don't have to, but it
|
|
|
|
* makes things easier. This way is trivial - right?
|
|
|
|
*/
|
|
|
|
do {
|
|
|
|
vm_map_t tmap;
|
|
|
|
vm_offset_t uva;
|
|
|
|
int page_offset; /* offset into page */
|
|
|
|
vm_map_entry_t out_entry;
|
|
|
|
vm_prot_t out_prot;
|
|
|
|
boolean_t wired;
|
|
|
|
vm_pindex_t pindex;
|
|
|
|
u_int len;
|
|
|
|
vm_page_t m;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
object = NULL;
|
1995-05-30 08:16:23 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
uva = (vm_offset_t)uio->uio_offset;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* Get the page number of this segment.
|
|
|
|
*/
|
|
|
|
pageno = trunc_page(uva);
|
|
|
|
page_offset = uva - pageno;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* How many bytes to copy
|
|
|
|
*/
|
|
|
|
len = min(PAGE_SIZE - page_offset, uio->uio_resid);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fault the page on behalf of the process
|
|
|
|
*/
|
2007-11-08 19:35:36 +00:00
|
|
|
error = vm_fault(map, pageno, reqprot, fault_flags);
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
if (error) {
|
Implement global and per-uid accounting of the anonymous memory. Add
rlimit RLIMIT_SWAP that limits the amount of swap that may be reserved
for the uid.
The accounting information (charge) is associated with either map entry,
or vm object backing the entry, assuming the object is the first one
in the shadow chain and entry does not require COW. Charge is moved
from entry to object on allocation of the object, e.g. during the mmap,
assuming the object is allocated, or on the first page fault on the
entry. It moves back to the entry on forks due to COW setup.
The per-entry granularity of accounting makes the charge process fair
for processes that change uid during lifetime, and decrements charge
for proper uid when region is unmapped.
The interface of vm_pager_allocate(9) is extended by adding struct ucred *,
that is used to charge appropriate uid when allocation if performed by
kernel, e.g. md(4).
Several syscalls, among them is fork(2), may now return ENOMEM when
global or per-uid limits are enforced.
In collaboration with: pho
Reviewed by: alc
Approved by: re (kensmith)
2009-06-23 20:45:22 +00:00
|
|
|
if (error == KERN_RESOURCE_SHORTAGE)
|
|
|
|
error = ENOMEM;
|
|
|
|
else
|
|
|
|
error = EFAULT;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
}
|
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* Now we need to get the page. out_entry, out_prot, wired,
|
|
|
|
* and single_use aren't used. One would think the vm code
|
|
|
|
* would be a *bit* nicer... We use tmap because
|
|
|
|
* vm_map_lookup() can change the map argument.
|
|
|
|
*/
|
|
|
|
tmap = map;
|
|
|
|
error = vm_map_lookup(&tmap, pageno, reqprot, &out_entry,
|
|
|
|
&object, &pindex, &out_prot, &wired);
|
|
|
|
if (error) {
|
|
|
|
error = EFAULT;
|
|
|
|
break;
|
|
|
|
}
|
2003-06-11 06:43:48 +00:00
|
|
|
VM_OBJECT_LOCK(object);
|
|
|
|
while ((m = vm_page_lookup(object, pindex)) == NULL &&
|
|
|
|
!writing &&
|
|
|
|
(backing_object = object->backing_object) != NULL) {
|
|
|
|
/*
|
|
|
|
* Allow fallback to backing objects if we are reading.
|
|
|
|
*/
|
|
|
|
VM_OBJECT_LOCK(backing_object);
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
pindex += OFF_TO_IDX(object->backing_object_offset);
|
2003-06-11 06:43:48 +00:00
|
|
|
VM_OBJECT_UNLOCK(object);
|
|
|
|
object = backing_object;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
}
|
2003-06-11 06:43:48 +00:00
|
|
|
VM_OBJECT_UNLOCK(object);
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
if (m == NULL) {
|
|
|
|
vm_map_lookup_done(tmap, out_entry);
|
2003-08-09 18:01:19 +00:00
|
|
|
error = EFAULT;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
break;
|
|
|
|
}
|
1994-08-08 13:00:27 +00:00
|
|
|
|
|
|
|
/*
|
2003-08-09 18:01:19 +00:00
|
|
|
* Hold the page in memory.
|
1994-08-08 13:00:27 +00:00
|
|
|
*/
|
2002-07-12 17:21:22 +00:00
|
|
|
vm_page_lock_queues();
|
2003-08-09 18:01:19 +00:00
|
|
|
vm_page_hold(m);
|
2002-07-12 17:21:22 +00:00
|
|
|
vm_page_unlock_queues();
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* We're done with tmap now.
|
|
|
|
*/
|
|
|
|
vm_map_lookup_done(tmap, out_entry);
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
|
|
|
* Now do the i/o move.
|
|
|
|
*/
|
2004-03-24 23:35:04 +00:00
|
|
|
error = uiomove_fromphys(&m, page_offset, len, uio);
|
1995-05-30 08:16:23 +00:00
|
|
|
|
2009-10-21 18:38:02 +00:00
|
|
|
/* Make the I-cache coherent for breakpoints. */
|
|
|
|
if (!error && writing && (out_prot & VM_PROT_EXECUTE))
|
|
|
|
vm_sync_icache(map, uva, len);
|
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
2003-08-09 18:01:19 +00:00
|
|
|
* Release the page.
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
*/
|
2002-07-12 17:21:22 +00:00
|
|
|
vm_page_lock_queues();
|
2003-08-09 18:01:19 +00:00
|
|
|
vm_page_unhold(m);
|
2002-07-12 17:21:22 +00:00
|
|
|
vm_page_unlock_queues();
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
} while (error == 0 && uio->uio_resid > 0);
|
1994-08-08 13:00:27 +00:00
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
return (error);
|
1994-08-08 13:00:27 +00:00
|
|
|
}
|
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
|
|
|
* Process debugging system call.
|
|
|
|
*/
|
1995-11-12 06:43:28 +00:00
|
|
|
#ifndef _SYS_SYSPROTO_H_
|
1994-05-24 10:09:53 +00:00
|
|
|
struct ptrace_args {
|
|
|
|
int req;
|
|
|
|
pid_t pid;
|
|
|
|
caddr_t addr;
|
|
|
|
int data;
|
|
|
|
};
|
1995-11-12 06:43:28 +00:00
|
|
|
#endif
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
/*
|
|
|
|
* This CPP subterfuge is to try and reduce the number of ifdefs in
|
|
|
|
* the body of the code.
|
|
|
|
* COPYIN(uap->addr, &r.reg, sizeof r.reg);
|
|
|
|
* becomes either:
|
|
|
|
* copyin(uap->addr, &r.reg, sizeof r.reg);
|
|
|
|
* or
|
|
|
|
* copyin(uap->addr, &r.reg32, sizeof r.reg32);
|
|
|
|
* .. except this is done at runtime.
|
|
|
|
*/
|
|
|
|
#define COPYIN(u, k, s) wrap32 ? \
|
|
|
|
copyin(u, k ## 32, s ## 32) : \
|
|
|
|
copyin(u, k, s)
|
|
|
|
#define COPYOUT(k, u, s) wrap32 ? \
|
|
|
|
copyout(k ## 32, u, s ## 32) : \
|
|
|
|
copyout(k, u, s)
|
|
|
|
#else
|
|
|
|
#define COPYIN(u, k, s) copyin(u, k, s)
|
|
|
|
#define COPYOUT(k, u, s) copyout(k, u, s)
|
|
|
|
#endif
|
1994-05-25 09:21:21 +00:00
|
|
|
int
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
ptrace(struct thread *td, struct ptrace_args *uap)
|
1994-05-24 10:09:53 +00:00
|
|
|
{
|
2002-02-21 04:47:38 +00:00
|
|
|
/*
|
|
|
|
* XXX this obfuscation is to reduce stack usage, but the register
|
|
|
|
* structs may be too large to put on the stack anyway.
|
|
|
|
*/
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
union {
|
2002-03-16 02:40:02 +00:00
|
|
|
struct ptrace_io_desc piod;
|
2004-07-12 05:07:50 +00:00
|
|
|
struct ptrace_lwpinfo pl;
|
2002-02-21 04:47:38 +00:00
|
|
|
struct dbreg dbreg;
|
|
|
|
struct fpreg fpreg;
|
|
|
|
struct reg reg;
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
struct dbreg32 dbreg32;
|
|
|
|
struct fpreg32 fpreg32;
|
|
|
|
struct reg32 reg32;
|
|
|
|
struct ptrace_io_desc32 piod32;
|
|
|
|
#endif
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
} r;
|
2002-09-05 01:02:50 +00:00
|
|
|
void *addr;
|
|
|
|
int error = 0;
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
int wrap32 = 0;
|
2002-09-05 01:02:50 +00:00
|
|
|
|
2009-03-02 18:43:50 +00:00
|
|
|
if (SV_CURPROC_FLAG(SV_ILP32))
|
2005-06-30 07:49:22 +00:00
|
|
|
wrap32 = 1;
|
|
|
|
#endif
|
2009-06-27 13:58:44 +00:00
|
|
|
AUDIT_ARG_PID(uap->pid);
|
|
|
|
AUDIT_ARG_CMD(uap->req);
|
|
|
|
AUDIT_ARG_VALUE(uap->data);
|
2002-09-05 01:02:50 +00:00
|
|
|
addr = &r;
|
|
|
|
switch (uap->req) {
|
|
|
|
case PT_GETREGS:
|
|
|
|
case PT_GETFPREGS:
|
|
|
|
case PT_GETDBREGS:
|
2004-07-12 05:07:50 +00:00
|
|
|
case PT_LWPINFO:
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_SETREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYIN(uap->addr, &r.reg, sizeof r.reg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_SETFPREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYIN(uap->addr, &r.fpreg, sizeof r.fpreg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_SETDBREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYIN(uap->addr, &r.dbreg, sizeof r.dbreg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_IO:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYIN(uap->addr, &r.piod, sizeof r.piod);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
addr = uap->addr;
|
2003-08-15 05:25:06 +00:00
|
|
|
break;
|
2002-09-05 01:02:50 +00:00
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
error = kern_ptrace(td, uap->req, uap->pid, addr, uap->data);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
switch (uap->req) {
|
|
|
|
case PT_IO:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYOUT(&r.piod, uap->addr, sizeof r.piod);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_GETREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYOUT(&r.reg, uap->addr, sizeof r.reg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_GETFPREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYOUT(&r.fpreg, uap->addr, sizeof r.fpreg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
|
|
|
case PT_GETDBREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = COPYOUT(&r.dbreg, uap->addr, sizeof r.dbreg);
|
2002-09-05 01:02:50 +00:00
|
|
|
break;
|
2004-07-12 05:07:50 +00:00
|
|
|
case PT_LWPINFO:
|
|
|
|
error = copyout(&r.pl, uap->addr, uap->data);
|
|
|
|
break;
|
2002-09-05 01:02:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return (error);
|
|
|
|
}
|
2005-06-30 07:49:22 +00:00
|
|
|
#undef COPYIN
|
|
|
|
#undef COPYOUT
|
|
|
|
|
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
/*
|
|
|
|
* PROC_READ(regs, td2, addr);
|
|
|
|
* becomes either:
|
|
|
|
* proc_read_regs(td2, addr);
|
|
|
|
* or
|
|
|
|
* proc_read_regs32(td2, addr);
|
|
|
|
* .. except this is done at runtime. There is an additional
|
|
|
|
* complication in that PROC_WRITE disallows 32 bit consumers
|
|
|
|
* from writing to 64 bit address space targets.
|
|
|
|
*/
|
|
|
|
#define PROC_READ(w, t, a) wrap32 ? \
|
|
|
|
proc_read_ ## w ## 32(t, a) : \
|
|
|
|
proc_read_ ## w (t, a)
|
|
|
|
#define PROC_WRITE(w, t, a) wrap32 ? \
|
|
|
|
(safe ? proc_write_ ## w ## 32(t, a) : EINVAL ) : \
|
|
|
|
proc_write_ ## w (t, a)
|
|
|
|
#else
|
|
|
|
#define PROC_READ(w, t, a) proc_read_ ## w (t, a)
|
|
|
|
#define PROC_WRITE(w, t, a) proc_write_ ## w (t, a)
|
|
|
|
#endif
|
2002-09-05 01:02:50 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
kern_ptrace(struct thread *td, int req, pid_t pid, void *addr, int data)
|
|
|
|
{
|
|
|
|
struct iovec iov;
|
|
|
|
struct uio uio;
|
2002-04-14 17:12:55 +00:00
|
|
|
struct proc *curp, *p, *pp;
|
2004-07-02 09:19:22 +00:00
|
|
|
struct thread *td2 = NULL;
|
2005-06-30 07:49:22 +00:00
|
|
|
struct ptrace_io_desc *piod = NULL;
|
2004-07-12 05:07:50 +00:00
|
|
|
struct ptrace_lwpinfo *pl;
|
2004-07-13 07:25:24 +00:00
|
|
|
int error, write, tmp, num;
|
2002-04-12 21:17:37 +00:00
|
|
|
int proctree_locked = 0;
|
2004-07-13 07:25:24 +00:00
|
|
|
lwpid_t tid = 0, *buf;
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
int wrap32 = 0, safe = 0;
|
|
|
|
struct ptrace_io_desc32 *piod32 = NULL;
|
|
|
|
#endif
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2002-04-14 17:12:55 +00:00
|
|
|
curp = td->td_proc;
|
|
|
|
|
2002-09-05 01:02:50 +00:00
|
|
|
/* Lock proctree before locking the process. */
|
|
|
|
switch (req) {
|
2002-04-12 21:17:37 +00:00
|
|
|
case PT_TRACE_ME:
|
|
|
|
case PT_ATTACH:
|
|
|
|
case PT_STEP:
|
|
|
|
case PT_CONTINUE:
|
2003-10-09 10:17:16 +00:00
|
|
|
case PT_TO_SCE:
|
|
|
|
case PT_TO_SCX:
|
2005-03-18 21:22:28 +00:00
|
|
|
case PT_SYSCALL:
|
2002-04-12 21:17:37 +00:00
|
|
|
case PT_DETACH:
|
|
|
|
sx_xlock(&proctree_lock);
|
|
|
|
proctree_locked = 1;
|
|
|
|
break;
|
|
|
|
default:
|
2002-04-20 21:56:42 +00:00
|
|
|
break;
|
2002-04-12 21:17:37 +00:00
|
|
|
}
|
2003-03-19 00:33:38 +00:00
|
|
|
|
1999-07-01 22:52:40 +00:00
|
|
|
write = 0;
|
2002-09-05 01:02:50 +00:00
|
|
|
if (req == PT_TRACE_ME) {
|
2002-04-12 21:17:37 +00:00
|
|
|
p = td->td_proc;
|
2001-04-24 00:51:53 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
} else {
|
2004-07-02 09:19:22 +00:00
|
|
|
if (pid <= PID_MAX) {
|
|
|
|
if ((p = pfind(pid)) == NULL) {
|
|
|
|
if (proctree_locked)
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* this is slow, should be optimized */
|
|
|
|
sx_slock(&allproc_lock);
|
|
|
|
FOREACH_PROC_IN_SYSTEM(p) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
FOREACH_THREAD_IN_PROC(p, td2) {
|
|
|
|
if (td2->td_tid == pid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (td2 != NULL)
|
|
|
|
break; /* proc lock held */
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
sx_sunlock(&allproc_lock);
|
|
|
|
if (p == NULL) {
|
|
|
|
if (proctree_locked)
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
|
|
|
tid = pid;
|
|
|
|
pid = p->p_pid;
|
2002-04-12 21:17:37 +00:00
|
|
|
}
|
1994-08-08 13:00:27 +00:00
|
|
|
}
|
2009-06-27 13:58:44 +00:00
|
|
|
AUDIT_ARG_PROCESS(p);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
|
|
|
|
if ((p->p_flag & P_WEXIT) != 0) {
|
|
|
|
error = ESRCH;
|
|
|
|
goto fail;
|
|
|
|
}
|
2002-07-20 22:44:39 +00:00
|
|
|
if ((error = p_cansee(td, p)) != 0)
|
2002-04-12 21:17:37 +00:00
|
|
|
goto fail;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
|
2002-05-19 00:14:50 +00:00
|
|
|
if ((error = p_candebug(td, p)) != 0)
|
2002-04-12 21:17:37 +00:00
|
|
|
goto fail;
|
|
|
|
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
/*
|
2002-02-21 04:47:38 +00:00
|
|
|
* System processes can't be debugged.
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
*/
|
|
|
|
if ((p->p_flag & P_SYSTEM) != 0) {
|
2002-04-12 21:17:37 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
}
|
2003-03-19 00:33:38 +00:00
|
|
|
|
2004-07-02 09:19:22 +00:00
|
|
|
if (tid == 0) {
|
2005-12-24 02:59:29 +00:00
|
|
|
if ((p->p_flag & P_STOPPED_TRACE) != 0) {
|
|
|
|
KASSERT(p->p_xthread != NULL, ("NULL p_xthread"));
|
|
|
|
td2 = p->p_xthread;
|
|
|
|
} else {
|
|
|
|
td2 = FIRST_THREAD_IN_PROC(p);
|
|
|
|
}
|
2004-07-02 09:19:22 +00:00
|
|
|
tid = td2->td_tid;
|
|
|
|
}
|
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
/*
|
|
|
|
* Test if we're a 32 bit client and what the target is.
|
|
|
|
* Set the wrap controls accordingly.
|
|
|
|
*/
|
2009-03-02 18:43:50 +00:00
|
|
|
if (SV_CURPROC_FLAG(SV_ILP32)) {
|
|
|
|
if (td2->td_proc->p_sysent->sv_flags & SV_ILP32)
|
2005-06-30 07:49:22 +00:00
|
|
|
safe = 1;
|
|
|
|
wrap32 = 1;
|
|
|
|
}
|
|
|
|
#endif
|
1996-01-24 18:29:00 +00:00
|
|
|
/*
|
|
|
|
* Permissions check
|
|
|
|
*/
|
2002-09-05 01:02:50 +00:00
|
|
|
switch (req) {
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_TRACE_ME:
|
|
|
|
/* Always legal. */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_ATTACH:
|
|
|
|
/* Self */
|
2002-04-12 21:17:37 +00:00
|
|
|
if (p->p_pid == td->td_proc->p_pid) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
2001-04-24 00:51:53 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
|
|
|
/* Already traced */
|
2001-03-07 03:06:18 +00:00
|
|
|
if (p->p_flag & P_TRACED) {
|
2002-04-12 21:17:37 +00:00
|
|
|
error = EBUSY;
|
|
|
|
goto fail;
|
2001-03-07 03:06:18 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
2002-04-14 17:12:55 +00:00
|
|
|
/* Can't trace an ancestor if you're being traced. */
|
|
|
|
if (curp->p_flag & P_TRACED) {
|
|
|
|
for (pp = curp->p_pptr; pp != NULL; pp = pp->p_pptr) {
|
|
|
|
if (pp == p) {
|
|
|
|
error = EINVAL;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
/* OK */
|
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2004-07-13 07:25:24 +00:00
|
|
|
case PT_CLEARSTEP:
|
|
|
|
/* Allow thread to clear single step for itself */
|
|
|
|
if (td->td_tid == tid)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* FALLTHROUGH */
|
2003-08-15 05:25:06 +00:00
|
|
|
default:
|
1996-01-24 18:29:00 +00:00
|
|
|
/* not being traced... */
|
2001-03-07 03:06:18 +00:00
|
|
|
if ((p->p_flag & P_TRACED) == 0) {
|
2002-04-12 21:17:37 +00:00
|
|
|
error = EPERM;
|
|
|
|
goto fail;
|
2001-03-07 03:06:18 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
|
|
|
/* not being traced by YOU */
|
2002-04-12 21:17:37 +00:00
|
|
|
if (p->p_pptr != td->td_proc) {
|
|
|
|
error = EBUSY;
|
|
|
|
goto fail;
|
2000-12-23 19:43:10 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
|
|
|
/* not currently stopped */
|
2005-12-24 02:59:29 +00:00
|
|
|
if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) == 0 ||
|
|
|
|
p->p_suspcount != p->p_numthreads ||
|
2004-07-13 07:25:24 +00:00
|
|
|
(p->p_flag & P_WAITED) == 0) {
|
2002-04-12 21:17:37 +00:00
|
|
|
error = EBUSY;
|
|
|
|
goto fail;
|
2000-12-02 01:32:51 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
2005-12-24 02:59:29 +00:00
|
|
|
if ((p->p_flag & P_STOPPED_TRACE) == 0) {
|
|
|
|
static int count = 0;
|
|
|
|
if (count++ == 0)
|
|
|
|
printf("P_STOPPED_TRACE not set.\n");
|
|
|
|
}
|
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
/* OK */
|
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
/* Keep this process around until we finish this request. */
|
|
|
|
_PHOLD(p);
|
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
#ifdef FIX_SSTEP
|
|
|
|
/*
|
|
|
|
* Single step fixup ala procfs
|
|
|
|
*/
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
FIX_SSTEP(td2);
|
1994-08-08 13:00:27 +00:00
|
|
|
#endif
|
1996-01-24 18:29:00 +00:00
|
|
|
|
1994-05-24 10:09:53 +00:00
|
|
|
/*
|
1996-01-24 18:29:00 +00:00
|
|
|
* Actually do the requests
|
1994-05-24 10:09:53 +00:00
|
|
|
*/
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2001-09-12 08:38:13 +00:00
|
|
|
td->td_retval[0] = 0;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2002-09-05 01:02:50 +00:00
|
|
|
switch (req) {
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_TRACE_ME:
|
|
|
|
/* set my trace flag and "owner" so it can read/write me */
|
1994-08-08 13:00:27 +00:00
|
|
|
p->p_flag |= P_TRACED;
|
1996-01-24 18:29:00 +00:00
|
|
|
p->p_oppid = p->p_pptr->p_pid;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_ATTACH:
|
|
|
|
/* security check done above */
|
2001-03-07 03:06:18 +00:00
|
|
|
p->p_flag |= P_TRACED;
|
1996-01-24 18:29:00 +00:00
|
|
|
p->p_oppid = p->p_pptr->p_pid;
|
2006-10-25 06:18:04 +00:00
|
|
|
if (p->p_pptr != td->td_proc)
|
2002-04-12 21:17:37 +00:00
|
|
|
proc_reparent(p, td->td_proc);
|
2002-09-05 01:02:50 +00:00
|
|
|
data = SIGSTOP;
|
1996-01-24 18:29:00 +00:00
|
|
|
goto sendsig; /* in PT_CONTINUE below */
|
|
|
|
|
2004-07-13 07:25:24 +00:00
|
|
|
case PT_CLEARSTEP:
|
|
|
|
error = ptrace_clear_single_step(td2);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
|
|
|
case PT_SETSTEP:
|
|
|
|
error = ptrace_single_step(td2);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
|
|
|
case PT_SUSPEND:
|
2008-10-15 06:31:37 +00:00
|
|
|
td2->td_dbgflags |= TDB_SUSPEND;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_lock(td2);
|
2008-10-15 06:31:37 +00:00
|
|
|
td2->td_flags |= TDF_NEEDSUSPCHK;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
thread_unlock(td2);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
|
|
|
case PT_RESUME:
|
2008-10-15 06:31:37 +00:00
|
|
|
td2->td_dbgflags &= ~TDB_SUSPEND;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_STEP:
|
|
|
|
case PT_CONTINUE:
|
2003-10-09 10:17:16 +00:00
|
|
|
case PT_TO_SCE:
|
|
|
|
case PT_TO_SCX:
|
2005-03-18 21:22:28 +00:00
|
|
|
case PT_SYSCALL:
|
1994-08-08 13:00:27 +00:00
|
|
|
case PT_DETACH:
|
2003-08-10 23:04:55 +00:00
|
|
|
/* Zero means do not send any signal */
|
|
|
|
if (data < 0 || data > _SIG_MAXSIG) {
|
2002-04-12 21:17:37 +00:00
|
|
|
error = EINVAL;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2002-04-12 21:17:37 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
|
2003-10-09 10:17:16 +00:00
|
|
|
switch (req) {
|
|
|
|
case PT_STEP:
|
2002-02-08 08:56:01 +00:00
|
|
|
error = ptrace_single_step(td2);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
if (error)
|
|
|
|
goto out;
|
2003-10-09 10:17:16 +00:00
|
|
|
break;
|
|
|
|
case PT_TO_SCE:
|
|
|
|
p->p_stops |= S_PT_SCE;
|
|
|
|
break;
|
|
|
|
case PT_TO_SCX:
|
|
|
|
p->p_stops |= S_PT_SCX;
|
|
|
|
break;
|
|
|
|
case PT_SYSCALL:
|
|
|
|
p->p_stops |= S_PT_SCE | S_PT_SCX;
|
|
|
|
break;
|
1996-01-24 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
2002-09-05 01:02:50 +00:00
|
|
|
if (addr != (void *)1) {
|
|
|
|
error = ptrace_set_pc(td2, (u_long)(uintfptr_t)addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
if (error)
|
|
|
|
break;
|
1996-01-24 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
2002-09-05 01:02:50 +00:00
|
|
|
if (req == PT_DETACH) {
|
1996-01-24 18:29:00 +00:00
|
|
|
/* reset process parent */
|
|
|
|
if (p->p_oppid != p->p_pptr->p_pid) {
|
|
|
|
struct proc *pp;
|
|
|
|
|
2005-11-08 23:28:12 +00:00
|
|
|
PROC_LOCK(p->p_pptr);
|
|
|
|
sigqueue_take(p->p_ksi);
|
|
|
|
PROC_UNLOCK(p->p_pptr);
|
|
|
|
|
2002-04-12 21:17:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
1996-01-24 18:29:00 +00:00
|
|
|
pp = pfind(p->p_oppid);
|
2002-02-23 11:12:57 +00:00
|
|
|
if (pp == NULL)
|
2001-05-04 18:13:11 +00:00
|
|
|
pp = initproc;
|
2002-02-23 11:12:57 +00:00
|
|
|
else
|
|
|
|
PROC_UNLOCK(pp);
|
2001-05-04 18:13:11 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
proc_reparent(p, pp);
|
2004-02-19 10:39:42 +00:00
|
|
|
if (pp == initproc)
|
|
|
|
p->p_sigparent = SIGCHLD;
|
2002-04-12 21:17:37 +00:00
|
|
|
}
|
1996-01-24 18:29:00 +00:00
|
|
|
p->p_flag &= ~(P_TRACED | P_WAITED);
|
|
|
|
p->p_oppid = 0;
|
2001-03-07 03:06:18 +00:00
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
/* should we send SIGCHLD? */
|
2005-11-08 23:28:12 +00:00
|
|
|
/* childproc_continued(p); */
|
1996-01-24 18:29:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendsig:
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
if (proctree_locked) {
|
2002-04-12 21:17:37 +00:00
|
|
|
sx_xunlock(&proctree_lock);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
proctree_locked = 0;
|
|
|
|
}
|
2005-12-24 02:59:29 +00:00
|
|
|
p->p_xstat = data;
|
|
|
|
p->p_xthread = NULL;
|
|
|
|
if ((p->p_flag & (P_STOPPED_SIG | P_STOPPED_TRACE)) != 0) {
|
2007-10-09 00:03:39 +00:00
|
|
|
/* deliver or queue signal */
|
2008-10-15 06:31:37 +00:00
|
|
|
td2->td_dbgflags &= ~TDB_XSIG;
|
2007-10-09 00:03:39 +00:00
|
|
|
td2->td_xsig = data;
|
|
|
|
|
2004-07-13 07:25:24 +00:00
|
|
|
if (req == PT_DETACH) {
|
|
|
|
struct thread *td3;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(p, td3) {
|
2008-10-15 06:31:37 +00:00
|
|
|
td3->td_dbgflags &= ~TDB_SUSPEND;
|
Commit 14/14 of sched_lock decomposition.
- Use thread_lock() rather than sched_lock for per-thread scheduling
sychronization.
- Use the per-process spinlock rather than the sched_lock for per-process
scheduling synchronization.
Tested by: kris, current@
Tested on: i386, amd64, ULE, 4BSD, libthr, libkse, PREEMPTION, etc.
Discussed with: kris, attilio, kmacy, jhb, julian, bde (small parts each)
2007-06-05 00:00:57 +00:00
|
|
|
}
|
2004-07-13 07:25:24 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* unsuspend all threads, to not let a thread run,
|
|
|
|
* you should use PT_SUSPEND to suspend it before
|
|
|
|
* continuing process.
|
|
|
|
*/
|
2008-11-05 03:01:23 +00:00
|
|
|
PROC_SLOCK(p);
|
2005-12-24 02:59:29 +00:00
|
|
|
p->p_flag &= ~(P_STOPPED_TRACE|P_STOPPED_SIG|P_WAITED);
|
2005-08-19 13:35:34 +00:00
|
|
|
thread_unsuspend(p);
|
2008-11-05 03:01:23 +00:00
|
|
|
PROC_SUNLOCK(p);
|
2007-10-09 00:03:39 +00:00
|
|
|
} else {
|
|
|
|
if (data)
|
|
|
|
psignal(p, data);
|
2004-07-13 07:25:24 +00:00
|
|
|
}
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
|
|
|
case PT_WRITE_I:
|
|
|
|
case PT_WRITE_D:
|
1996-01-24 18:29:00 +00:00
|
|
|
write = 1;
|
2002-08-25 13:23:09 +00:00
|
|
|
/* FALLTHROUGH */
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_READ_I:
|
|
|
|
case PT_READ_D:
|
2002-04-12 21:17:37 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-07-12 16:48:05 +00:00
|
|
|
tmp = 0;
|
1996-01-24 18:29:00 +00:00
|
|
|
/* write = 0 set above */
|
2002-09-05 01:02:50 +00:00
|
|
|
iov.iov_base = write ? (caddr_t)&data : (caddr_t)&tmp;
|
1996-01-24 18:29:00 +00:00
|
|
|
iov.iov_len = sizeof(int);
|
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
2002-09-05 01:02:50 +00:00
|
|
|
uio.uio_offset = (off_t)(uintptr_t)addr;
|
1996-01-24 18:29:00 +00:00
|
|
|
uio.uio_resid = sizeof(int);
|
2002-02-21 04:47:38 +00:00
|
|
|
uio.uio_segflg = UIO_SYSSPACE; /* i.e.: the uap */
|
1996-01-24 18:29:00 +00:00
|
|
|
uio.uio_rw = write ? UIO_WRITE : UIO_READ;
|
2001-09-12 08:38:13 +00:00
|
|
|
uio.uio_td = td;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
error = proc_rwmem(p, &uio);
|
1996-03-30 15:02:58 +00:00
|
|
|
if (uio.uio_resid != 0) {
|
|
|
|
/*
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
* XXX proc_rwmem() doesn't currently return ENOSPC,
|
1996-03-30 15:02:58 +00:00
|
|
|
* so I think write() can bogusly return 0.
|
|
|
|
* XXX what happens for short writes? We don't want
|
|
|
|
* to write partial data.
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
* XXX proc_rwmem() returns EPERM for other invalid
|
1996-03-30 15:02:58 +00:00
|
|
|
* addresses. Convert this to EINVAL. Does this
|
|
|
|
* clobber returns of EPERM for other reasons?
|
|
|
|
*/
|
|
|
|
if (error == 0 || error == ENOSPC || error == EPERM)
|
|
|
|
error = EINVAL; /* EOF */
|
|
|
|
}
|
2002-07-12 16:48:05 +00:00
|
|
|
if (!write)
|
|
|
|
td->td_retval[0] = tmp;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
break;
|
1994-08-08 13:00:27 +00:00
|
|
|
|
2002-03-16 02:40:02 +00:00
|
|
|
case PT_IO:
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
if (wrap32) {
|
|
|
|
piod32 = addr;
|
|
|
|
iov.iov_base = (void *)(uintptr_t)piod32->piod_addr;
|
|
|
|
iov.iov_len = piod32->piod_len;
|
|
|
|
uio.uio_offset = (off_t)(uintptr_t)piod32->piod_offs;
|
|
|
|
uio.uio_resid = piod32->piod_len;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
piod = addr;
|
|
|
|
iov.iov_base = piod->piod_addr;
|
|
|
|
iov.iov_len = piod->piod_len;
|
|
|
|
uio.uio_offset = (off_t)(uintptr_t)piod->piod_offs;
|
|
|
|
uio.uio_resid = piod->piod_len;
|
|
|
|
}
|
2002-03-16 02:40:02 +00:00
|
|
|
uio.uio_iov = &iov;
|
|
|
|
uio.uio_iovcnt = 1;
|
|
|
|
uio.uio_segflg = UIO_USERSPACE;
|
|
|
|
uio.uio_td = td;
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
tmp = wrap32 ? piod32->piod_op : piod->piod_op;
|
|
|
|
#else
|
|
|
|
tmp = piod->piod_op;
|
|
|
|
#endif
|
|
|
|
switch (tmp) {
|
2002-03-16 02:40:02 +00:00
|
|
|
case PIOD_READ_D:
|
|
|
|
case PIOD_READ_I:
|
|
|
|
uio.uio_rw = UIO_READ;
|
|
|
|
break;
|
|
|
|
case PIOD_WRITE_D:
|
|
|
|
case PIOD_WRITE_I:
|
|
|
|
uio.uio_rw = UIO_WRITE;
|
|
|
|
break;
|
|
|
|
default:
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
error = EINVAL;
|
|
|
|
goto out;
|
2002-03-16 02:40:02 +00:00
|
|
|
}
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
PROC_UNLOCK(p);
|
2002-03-16 02:40:02 +00:00
|
|
|
error = proc_rwmem(p, &uio);
|
2005-06-30 07:49:22 +00:00
|
|
|
#ifdef COMPAT_IA32
|
|
|
|
if (wrap32)
|
|
|
|
piod32->piod_len -= uio.uio_resid;
|
|
|
|
else
|
|
|
|
#endif
|
|
|
|
piod->piod_len -= uio.uio_resid;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
break;
|
2002-03-16 02:40:02 +00:00
|
|
|
|
1994-08-08 13:00:27 +00:00
|
|
|
case PT_KILL:
|
2002-09-05 01:02:50 +00:00
|
|
|
data = SIGKILL;
|
1996-01-24 18:29:00 +00:00
|
|
|
goto sendsig; /* in PT_CONTINUE above */
|
|
|
|
|
|
|
|
case PT_SETREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_WRITE(regs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
|
1994-08-08 13:00:27 +00:00
|
|
|
case PT_GETREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_READ(regs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
1996-01-24 18:29:00 +00:00
|
|
|
|
|
|
|
case PT_SETFPREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_WRITE(fpregs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
|
1996-01-24 18:29:00 +00:00
|
|
|
case PT_GETFPREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_READ(fpregs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
1996-01-24 18:29:00 +00:00
|
|
|
|
1999-07-09 04:16:00 +00:00
|
|
|
case PT_SETDBREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_WRITE(dbregs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2002-03-15 20:17:12 +00:00
|
|
|
|
1999-07-09 04:16:00 +00:00
|
|
|
case PT_GETDBREGS:
|
2005-06-30 07:49:22 +00:00
|
|
|
error = PROC_READ(dbregs, td2, addr);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
1999-07-09 04:16:00 +00:00
|
|
|
|
2004-07-12 05:07:50 +00:00
|
|
|
case PT_LWPINFO:
|
2006-08-20 10:29:08 +00:00
|
|
|
if (data <= 0 || data > sizeof(*pl)) {
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2004-07-12 05:07:50 +00:00
|
|
|
pl = addr;
|
2005-12-24 02:59:29 +00:00
|
|
|
pl->pl_lwpid = td2->td_tid;
|
2008-10-15 06:31:37 +00:00
|
|
|
if (td2->td_dbgflags & TDB_XSIG)
|
2004-07-13 07:25:24 +00:00
|
|
|
pl->pl_event = PL_EVENT_SIGNAL;
|
2005-12-24 02:59:29 +00:00
|
|
|
else
|
|
|
|
pl->pl_event = 0;
|
2006-10-26 21:42:22 +00:00
|
|
|
pl->pl_flags = 0;
|
2006-02-06 09:41:56 +00:00
|
|
|
pl->pl_sigmask = td2->td_sigmask;
|
|
|
|
pl->pl_siglist = td2->td_siglist;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-12 05:07:50 +00:00
|
|
|
|
2004-07-13 07:25:24 +00:00
|
|
|
case PT_GETNUMLWPS:
|
|
|
|
td->td_retval[0] = p->p_numthreads;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
|
|
|
case PT_GETLWPLIST:
|
|
|
|
if (data <= 0) {
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
error = EINVAL;
|
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
}
|
|
|
|
num = imin(p->p_numthreads, data);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
buf = malloc(num * sizeof(lwpid_t), M_TEMP, M_WAITOK);
|
|
|
|
tmp = 0;
|
|
|
|
PROC_LOCK(p);
|
|
|
|
FOREACH_THREAD_IN_PROC(p, td2) {
|
|
|
|
if (tmp >= num)
|
|
|
|
break;
|
|
|
|
buf[tmp++] = td2->td_tid;
|
|
|
|
}
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
error = copyout(buf, addr, tmp * sizeof(lwpid_t));
|
|
|
|
free(buf, M_TEMP);
|
|
|
|
if (!error)
|
2006-10-14 10:30:14 +00:00
|
|
|
td->td_retval[0] = tmp;
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
break;
|
2004-07-13 07:25:24 +00:00
|
|
|
|
1994-08-08 13:00:27 +00:00
|
|
|
default:
|
2003-08-15 05:25:06 +00:00
|
|
|
#ifdef __HAVE_PTRACE_MACHDEP
|
|
|
|
if (req >= PT_FIRSTMACH) {
|
|
|
|
PROC_UNLOCK(p);
|
2004-03-15 18:48:28 +00:00
|
|
|
error = cpu_ptrace(td2, req, addr, data);
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
PROC_LOCK(p);
|
|
|
|
} else
|
2003-08-15 05:25:06 +00:00
|
|
|
#endif
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
/* Unknown request. */
|
|
|
|
error = EINVAL;
|
1994-08-08 13:00:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
Close some races between procfs/ptrace and exit(2):
- Reorder the events in exit(2) slightly so that we trigger the S_EXIT
stop event earlier. After we have signalled that, we set P_WEXIT and
then wait for any processes with a hold on the vmspace via PHOLD to
release it. PHOLD now KASSERT()'s that P_WEXIT is clear when it is
invoked, and PRELE now does a wakeup if P_WEXIT is set and p_lock drops
to zero.
- Change proc_rwmem() to require that the processing read from has its
vmspace held via PHOLD by the caller and get rid of all the junk to
screw around with the vmspace reference count as we no longer need it.
- In ptrace() and pseudofs(), treat a process with P_WEXIT set as if it
doesn't exist.
- Only do one PHOLD in kern_ptrace() now, and do it earlier so it covers
FIX_SSTEP() (since on alpha at least this can end up calling proc_rwmem()
to clear an earlier single-step simualted via a breakpoint). We only
do one to avoid races. Also, by making the EINVAL error for unknown
requests be part of the default: case in the switch, the various
switch cases can now just break out to return which removes a _lot_ of
duplicated PRELE and proc unlocks, etc. Also, it fixes at least one bug
where a LWP ptrace command could return EINVAL with the proc lock still
held.
- Changed the locking for ptrace_single_step(), ptrace_set_pc(), and
ptrace_clear_single_step() to always be called with the proc lock
held (it was a mixed bag previously). Alpha and arm have to drop
the lock while the mess around with breakpoints, but other archs
avoid extra lock release/acquires in ptrace(). I did have to fix a
couple of other consumers in kern_kse and a few other places to
hold the proc lock and PHOLD.
Tested by: ps (1 mostly, but some bits of 2-4 as well)
MFC after: 1 week
2006-02-22 18:57:50 +00:00
|
|
|
out:
|
|
|
|
/* Drop our hold on this process now that the request has completed. */
|
|
|
|
_PRELE(p);
|
2002-04-12 21:17:37 +00:00
|
|
|
fail:
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (proctree_locked)
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
return (error);
|
1994-05-24 10:09:53 +00:00
|
|
|
}
|
2005-06-30 07:49:22 +00:00
|
|
|
#undef PROC_READ
|
|
|
|
#undef PROC_WRITE
|
1994-05-24 10:09:53 +00:00
|
|
|
|
1997-12-06 04:11:14 +00:00
|
|
|
/*
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
* Stop a process because of a debugging event;
|
1997-12-06 04:11:14 +00:00
|
|
|
* stay stopped until p->p_step is cleared
|
|
|
|
* (cleared by PIOCCONT in procfs).
|
|
|
|
*/
|
|
|
|
void
|
Dissociate ptrace from procfs.
Until now, the ptrace syscall was implemented as a wrapper that called
various functions in procfs depending on which ptrace operation was
requested. Most of these functions were themselves wrappers around
procfs_{read,write}_{,db,fp}regs(), with only some extra error checks,
which weren't necessary in the ptrace case anyway.
This commit moves procfs_rwmem() from procfs_mem.c into sys_process.c
(renaming it to proc_rwmem() in the process), and implements ptrace()
directly in terms of procfs_{read,write}_{,db,fp}regs() instead of
having it fake up a struct uio and then call procfs_do{,db,fp}regs().
It also moves the prototypes for procfs_{read,write}_{,db,fp}regs()
and proc_rwmem() from proc.h to ptrace.h, and marks all procfs files
except procfs_machdep.c as "optional procfs" instead of "standard".
2001-10-07 20:08:42 +00:00
|
|
|
stopevent(struct proc *p, unsigned int event, unsigned int val)
|
2001-01-24 11:15:24 +00:00
|
|
|
{
|
|
|
|
|
2003-04-17 22:31:54 +00:00
|
|
|
PROC_LOCK_ASSERT(p, MA_OWNED);
|
1997-12-06 04:11:14 +00:00
|
|
|
p->p_step = 1;
|
|
|
|
do {
|
|
|
|
p->p_xstat = val;
|
2004-07-13 07:25:24 +00:00
|
|
|
p->p_xthread = NULL;
|
1997-12-06 04:11:14 +00:00
|
|
|
p->p_stype = event; /* Which event caused the stop? */
|
|
|
|
wakeup(&p->p_stype); /* Wake up any PIOCWAIT'ing procs */
|
2001-01-24 11:15:24 +00:00
|
|
|
msleep(&p->p_step, &p->p_mtx, PWAIT, "stopevent", 0);
|
1997-12-06 04:11:14 +00:00
|
|
|
} while (p->p_step);
|
|
|
|
}
|