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>
|
2013-09-19 18:53:42 +00:00
|
|
|
#include <sys/priv.h>
|
1994-05-24 10:09:53 +00:00
|
|
|
#include <sys/proc.h>
|
2013-09-19 18:53:42 +00:00
|
|
|
#include <sys/procctl.h>
|
1994-08-08 13:00:27 +00:00
|
|
|
#include <sys/vnode.h>
|
|
|
|
#include <sys/ptrace.h>
|
2013-03-09 02:32:23 +00:00
|
|
|
#include <sys/rwlock.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
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
#include <sys/procfs.h>
|
2010-07-04 11:48:30 +00:00
|
|
|
#include <compat/freebsd32/freebsd32_signal.h>
|
2005-06-30 07:49:22 +00:00
|
|
|
|
|
|
|
struct ptrace_io_desc32 {
|
|
|
|
int piod_op;
|
2010-06-21 09:55:56 +00:00
|
|
|
uint32_t piod_offs;
|
|
|
|
uint32_t piod_addr;
|
|
|
|
uint32_t piod_len;
|
2005-06-30 07:49:22 +00:00
|
|
|
};
|
2010-02-09 17:20:00 +00:00
|
|
|
|
|
|
|
struct ptrace_vm_entry32 {
|
2010-02-11 18:00:53 +00:00
|
|
|
int pve_entry;
|
|
|
|
int pve_timestamp;
|
2010-02-09 17:20:00 +00:00
|
|
|
uint32_t pve_start;
|
|
|
|
uint32_t pve_end;
|
|
|
|
uint32_t pve_offset;
|
|
|
|
u_int pve_prot;
|
|
|
|
u_int pve_pathlen;
|
2010-02-11 18:00:53 +00:00
|
|
|
int32_t pve_fileid;
|
|
|
|
u_int pve_fsid;
|
2010-02-09 17:20:00 +00:00
|
|
|
uint32_t pve_path;
|
|
|
|
};
|
|
|
|
|
2010-07-04 11:48:30 +00:00
|
|
|
struct ptrace_lwpinfo32 {
|
|
|
|
lwpid_t pl_lwpid; /* LWP described. */
|
|
|
|
int pl_event; /* Event that stopped the LWP. */
|
|
|
|
int pl_flags; /* LWP flags. */
|
|
|
|
sigset_t pl_sigmask; /* LWP signal mask */
|
|
|
|
sigset_t pl_siglist; /* LWP pending signal */
|
|
|
|
struct siginfo32 pl_siginfo; /* siginfo for signal */
|
Add the ability for GDB to printout the thread name along with other
thread specific informations.
In order to do that, and in order to avoid KBI breakage with existing
infrastructure the following semantic is implemented:
- For live programs, a new member to the PT_LWPINFO is added (pl_tdname)
- For cores, a new ELF note is added (NT_THRMISC) that can be used for
storing thread specific, miscellaneous, informations. Right now it is
just popluated with a thread name.
GDB, then, retrieves the correct informations from the corefile via the
BFD interface, as it groks the ELF notes and create appropriate
pseudo-sections.
Sponsored by: Sandvine Incorporated
Tested by: gianni
Discussed with: dim, kan, kib
MFC after: 2 weeks
2010-11-22 14:42:13 +00:00
|
|
|
char pl_tdname[MAXCOMLEN + 1]; /* LWP name. */
|
2011-01-25 10:59:21 +00:00
|
|
|
int pl_child_pid; /* New child pid */
|
2010-07-04 11:48:30 +00:00
|
|
|
};
|
|
|
|
|
2005-06-30 07:49:22 +00:00
|
|
|
#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
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
/* 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;
|
2009-11-26 05:16:07 +00:00
|
|
|
vm_offset_t pageno; /* page number */
|
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_prot_t reqprot;
|
2010-12-20 22:49:31 +00:00
|
|
|
int error, fault_flags, page_offset, 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
|
|
|
|
2010-12-20 22:49:31 +00:00
|
|
|
/*
|
|
|
|
* If we are writing, then we request vm_fault() to create a private
|
|
|
|
* copy of each page. Since these copies will not be writeable by the
|
|
|
|
* process, we must explicity request that they be dirtied.
|
|
|
|
*/
|
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;
|
2009-11-26 05:16:07 +00:00
|
|
|
reqprot = writing ? VM_PROT_COPY | VM_PROT_READ : VM_PROT_READ;
|
2010-12-20 22:49:31 +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_offset_t uva;
|
|
|
|
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
|
|
|
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);
|
|
|
|
|
|
|
|
/*
|
2013-08-05 08:55:35 +00:00
|
|
|
* Fault and hold the page on behalf of the process.
|
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
|
|
|
*/
|
2013-08-05 08:55:35 +00:00
|
|
|
error = vm_fault_hold(map, pageno, reqprot, fault_flags, &m);
|
2010-12-20 22:49:31 +00:00
|
|
|
if (error != KERN_SUCCESS) {
|
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 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. */
|
2010-12-20 22:49:31 +00:00
|
|
|
if (writing && error == 0) {
|
|
|
|
vm_map_lock_read(map);
|
|
|
|
if (vm_map_check_protection(map, pageno, pageno +
|
|
|
|
PAGE_SIZE, VM_PROT_EXECUTE))
|
|
|
|
vm_sync_icache(map, uva, len);
|
|
|
|
vm_map_unlock_read(map);
|
|
|
|
}
|
2009-10-21 18:38:02 +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
|
|
|
/*
|
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
|
|
|
*/
|
2013-08-05 08:55:35 +00:00
|
|
|
vm_page_lock(m);
|
|
|
|
vm_page_unhold(m);
|
|
|
|
vm_page_unlock(m);
|
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
|
|
|
}
|
|
|
|
|
2010-02-09 05:52:35 +00:00
|
|
|
static int
|
|
|
|
ptrace_vm_entry(struct thread *td, struct proc *p, struct ptrace_vm_entry *pve)
|
|
|
|
{
|
2010-02-11 18:00:53 +00:00
|
|
|
struct vattr vattr;
|
2010-02-09 05:52:35 +00:00
|
|
|
vm_map_t map;
|
|
|
|
vm_map_entry_t entry;
|
|
|
|
vm_object_t obj, tobj, lobj;
|
2010-02-11 18:00:53 +00:00
|
|
|
struct vmspace *vm;
|
2010-02-09 05:52:35 +00:00
|
|
|
struct vnode *vp;
|
|
|
|
char *freepath, *fullpath;
|
|
|
|
u_int pathlen;
|
2012-10-22 17:50:54 +00:00
|
|
|
int error, index;
|
2010-02-09 05:52:35 +00:00
|
|
|
|
2010-02-11 18:00:53 +00:00
|
|
|
error = 0;
|
|
|
|
obj = NULL;
|
|
|
|
|
|
|
|
vm = vmspace_acquire_ref(p);
|
|
|
|
map = &vm->vm_map;
|
|
|
|
vm_map_lock_read(map);
|
|
|
|
|
|
|
|
do {
|
|
|
|
entry = map->header.next;
|
|
|
|
index = 0;
|
|
|
|
while (index < pve->pve_entry && entry != &map->header) {
|
2010-02-09 05:52:35 +00:00
|
|
|
entry = entry->next;
|
2010-02-11 18:00:53 +00:00
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (index != pve->pve_entry) {
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (entry != &map->header &&
|
|
|
|
(entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0) {
|
|
|
|
entry = entry->next;
|
|
|
|
index++;
|
|
|
|
}
|
|
|
|
if (entry == &map->header) {
|
|
|
|
error = ENOENT;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We got an entry. */
|
|
|
|
pve->pve_entry = index + 1;
|
|
|
|
pve->pve_timestamp = map->timestamp;
|
|
|
|
pve->pve_start = entry->start;
|
|
|
|
pve->pve_end = entry->end - 1;
|
|
|
|
pve->pve_offset = entry->offset;
|
|
|
|
pve->pve_prot = entry->protection;
|
|
|
|
|
|
|
|
/* Backing object's path needed? */
|
|
|
|
if (pve->pve_pathlen == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
pathlen = pve->pve_pathlen;
|
|
|
|
pve->pve_pathlen = 0;
|
|
|
|
|
|
|
|
obj = entry->object.vm_object;
|
|
|
|
if (obj != NULL)
|
2013-04-08 19:58:32 +00:00
|
|
|
VM_OBJECT_RLOCK(obj);
|
2010-02-11 18:00:53 +00:00
|
|
|
} while (0);
|
|
|
|
|
|
|
|
vm_map_unlock_read(map);
|
|
|
|
vmspace_free(vm);
|
|
|
|
|
2010-02-11 21:10:56 +00:00
|
|
|
pve->pve_fsid = VNOVAL;
|
|
|
|
pve->pve_fileid = VNOVAL;
|
|
|
|
|
2010-02-11 18:00:53 +00:00
|
|
|
if (error == 0 && obj != NULL) {
|
|
|
|
lobj = obj;
|
|
|
|
for (tobj = obj; tobj != NULL; tobj = tobj->backing_object) {
|
|
|
|
if (tobj != obj)
|
2013-04-08 19:58:32 +00:00
|
|
|
VM_OBJECT_RLOCK(tobj);
|
2010-02-11 18:00:53 +00:00
|
|
|
if (lobj != obj)
|
2013-04-08 19:58:32 +00:00
|
|
|
VM_OBJECT_RUNLOCK(lobj);
|
2010-02-11 18:00:53 +00:00
|
|
|
lobj = tobj;
|
|
|
|
pve->pve_offset += tobj->backing_object_offset;
|
|
|
|
}
|
2010-02-09 05:52:35 +00:00
|
|
|
vp = (lobj->type == OBJT_VNODE) ? lobj->handle : NULL;
|
|
|
|
if (vp != NULL)
|
|
|
|
vref(vp);
|
|
|
|
if (lobj != obj)
|
2013-04-08 19:58:32 +00:00
|
|
|
VM_OBJECT_RUNLOCK(lobj);
|
|
|
|
VM_OBJECT_RUNLOCK(obj);
|
2010-02-09 05:52:35 +00:00
|
|
|
|
2010-02-11 18:00:53 +00:00
|
|
|
if (vp != NULL) {
|
|
|
|
freepath = NULL;
|
|
|
|
fullpath = NULL;
|
|
|
|
vn_fullpath(td, vp, &fullpath, &freepath);
|
|
|
|
vn_lock(vp, LK_SHARED | LK_RETRY);
|
|
|
|
if (VOP_GETATTR(vp, &vattr, td->td_ucred) == 0) {
|
|
|
|
pve->pve_fileid = vattr.va_fileid;
|
|
|
|
pve->pve_fsid = vattr.va_fsid;
|
|
|
|
}
|
|
|
|
vput(vp);
|
|
|
|
|
|
|
|
if (fullpath != NULL) {
|
|
|
|
pve->pve_pathlen = strlen(fullpath) + 1;
|
|
|
|
if (pve->pve_pathlen <= pathlen) {
|
|
|
|
error = copyout(fullpath, pve->pve_path,
|
|
|
|
pve->pve_pathlen);
|
|
|
|
} else
|
|
|
|
error = ENAMETOOLONG;
|
|
|
|
}
|
|
|
|
if (freepath != NULL)
|
|
|
|
free(freepath, M_TEMP);
|
|
|
|
}
|
|
|
|
}
|
2010-02-09 05:52:35 +00:00
|
|
|
|
2010-02-11 18:00:53 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2010-02-09 05:52:35 +00:00
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2010-02-11 18:00:53 +00:00
|
|
|
static int
|
|
|
|
ptrace_vm_entry32(struct thread *td, struct proc *p,
|
|
|
|
struct ptrace_vm_entry32 *pve32)
|
|
|
|
{
|
|
|
|
struct ptrace_vm_entry pve;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
pve.pve_entry = pve32->pve_entry;
|
|
|
|
pve.pve_pathlen = pve32->pve_pathlen;
|
|
|
|
pve.pve_path = (void *)(uintptr_t)pve32->pve_path;
|
|
|
|
|
|
|
|
error = ptrace_vm_entry(td, p, &pve);
|
|
|
|
if (error == 0) {
|
|
|
|
pve32->pve_entry = pve.pve_entry;
|
|
|
|
pve32->pve_timestamp = pve.pve_timestamp;
|
|
|
|
pve32->pve_start = pve.pve_start;
|
|
|
|
pve32->pve_end = pve.pve_end;
|
|
|
|
pve32->pve_offset = pve.pve_offset;
|
|
|
|
pve32->pve_prot = pve.pve_prot;
|
|
|
|
pve32->pve_fileid = pve.pve_fileid;
|
|
|
|
pve32->pve_fsid = pve.pve_fsid;
|
2010-02-09 05:52:35 +00:00
|
|
|
}
|
2010-02-11 18:00:53 +00:00
|
|
|
|
|
|
|
pve32->pve_pathlen = pve.pve_pathlen;
|
2010-02-09 05:52:35 +00:00
|
|
|
return (error);
|
|
|
|
}
|
2010-07-04 11:48:30 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
ptrace_lwpinfo_to32(const struct ptrace_lwpinfo *pl,
|
|
|
|
struct ptrace_lwpinfo32 *pl32)
|
|
|
|
{
|
|
|
|
|
|
|
|
pl32->pl_lwpid = pl->pl_lwpid;
|
|
|
|
pl32->pl_event = pl->pl_event;
|
|
|
|
pl32->pl_flags = pl->pl_flags;
|
|
|
|
pl32->pl_sigmask = pl->pl_sigmask;
|
|
|
|
pl32->pl_siglist = pl->pl_siglist;
|
|
|
|
siginfo_to_siginfo32(&pl->pl_siginfo, &pl32->pl_siginfo);
|
Add the ability for GDB to printout the thread name along with other
thread specific informations.
In order to do that, and in order to avoid KBI breakage with existing
infrastructure the following semantic is implemented:
- For live programs, a new member to the PT_LWPINFO is added (pl_tdname)
- For cores, a new ELF note is added (NT_THRMISC) that can be used for
storing thread specific, miscellaneous, informations. Right now it is
just popluated with a thread name.
GDB, then, retrieves the correct informations from the corefile via the
BFD interface, as it groks the ELF notes and create appropriate
pseudo-sections.
Sponsored by: Sandvine Incorporated
Tested by: gianni
Discussed with: dim, kan, kib
MFC after: 2 weeks
2010-11-22 14:42:13 +00:00
|
|
|
strcpy(pl32->pl_tdname, pl->pl_tdname);
|
2011-01-25 10:59:21 +00:00
|
|
|
pl32->pl_child_pid = pl->pl_child_pid;
|
2010-07-04 11:48:30 +00:00
|
|
|
}
|
2010-03-11 14:49:06 +00:00
|
|
|
#endif /* COMPAT_FREEBSD32 */
|
2010-02-09 05:52:35 +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
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
/*
|
|
|
|
* 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
|
2011-09-16 13:58:51 +00:00
|
|
|
sys_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;
|
2010-02-09 05:52:35 +00:00
|
|
|
struct ptrace_vm_entry pve;
|
2002-02-21 04:47:38 +00:00
|
|
|
struct dbreg dbreg;
|
|
|
|
struct fpreg fpreg;
|
|
|
|
struct reg reg;
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
struct dbreg32 dbreg32;
|
|
|
|
struct fpreg32 fpreg32;
|
|
|
|
struct reg32 reg32;
|
|
|
|
struct ptrace_io_desc32 piod32;
|
2010-07-04 11:48:30 +00:00
|
|
|
struct ptrace_lwpinfo32 pl32;
|
2010-02-09 17:20:00 +00:00
|
|
|
struct ptrace_vm_entry32 pve32;
|
2005-06-30 07:49:22 +00:00
|
|
|
#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;
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
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;
|
2010-02-09 05:52:35 +00:00
|
|
|
case PT_VM_ENTRY:
|
|
|
|
error = COPYIN(uap->addr, &r.pve, sizeof r.pve);
|
|
|
|
break;
|
2002-09-05 01:02:50 +00:00
|
|
|
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) {
|
2010-02-09 05:52:35 +00:00
|
|
|
case PT_VM_ENTRY:
|
|
|
|
error = COPYOUT(&r.pve, uap->addr, sizeof r.pve);
|
|
|
|
break;
|
2002-09-05 01:02:50 +00:00
|
|
|
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
|
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2012-07-09 09:24:46 +00:00
|
|
|
struct thread *td2 = NULL, *td3;
|
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;
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
int wrap32 = 0, safe = 0;
|
|
|
|
struct ptrace_io_desc32 *piod32 = NULL;
|
2010-07-04 11:48:30 +00:00
|
|
|
struct ptrace_lwpinfo32 *pl32 = NULL;
|
|
|
|
struct ptrace_lwpinfo plr;
|
2005-06-30 07:49:22 +00:00
|
|
|
#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:
|
2011-01-25 10:59:21 +00:00
|
|
|
case PT_FOLLOW_FORK:
|
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 {
|
2010-10-09 02:50:23 +00:00
|
|
|
td2 = tdfind(pid, -1);
|
|
|
|
if (td2 == NULL) {
|
2004-07-02 09:19:22 +00:00
|
|
|
if (proctree_locked)
|
|
|
|
sx_xunlock(&proctree_lock);
|
|
|
|
return (ESRCH);
|
|
|
|
}
|
2010-10-09 02:50:23 +00:00
|
|
|
p = td2->td_proc;
|
2004-07-02 09:19:22 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
/*
|
|
|
|
* 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)) {
|
2011-01-26 20:03:58 +00:00
|
|
|
if (SV_PROC_FLAG(td2->td_proc, 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;
|
2013-02-07 15:34:22 +00:00
|
|
|
if (p->p_flag & P_PPWAIT)
|
|
|
|
p->p_flag |= P_PPTRACE;
|
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 */
|
2011-06-17 21:44:13 +00:00
|
|
|
/*
|
|
|
|
* It would be nice if the tracing relationship was separate
|
|
|
|
* from the parent relationship but that would require
|
|
|
|
* another set of links in the proc struct or for "wait"
|
|
|
|
* to scan the entire proc table. To make life easier,
|
|
|
|
* we just re-parent the process we're trying to trace.
|
|
|
|
* The old parent is remembered so we can put things back
|
|
|
|
* on a "detach".
|
|
|
|
*/
|
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;
|
2011-06-14 17:09:30 +00:00
|
|
|
if (p->p_pptr != td->td_proc) {
|
2002-04-12 21:17:37 +00:00
|
|
|
proc_reparent(p, td->td_proc);
|
2011-06-14 17:09:30 +00:00
|
|
|
}
|
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
|
|
|
|
2011-01-25 10:59:21 +00:00
|
|
|
case PT_FOLLOW_FORK:
|
|
|
|
if (data)
|
|
|
|
p->p_flag |= P_FOLLOWFORK;
|
|
|
|
else
|
|
|
|
p->p_flag &= ~P_FOLLOWFORK;
|
|
|
|
break;
|
|
|
|
|
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;
|
2010-05-25 21:32:37 +00:00
|
|
|
case PT_CONTINUE:
|
2003-10-09 10:17:16 +00:00
|
|
|
case PT_TO_SCE:
|
|
|
|
case PT_TO_SCX:
|
|
|
|
case PT_SYSCALL:
|
2010-05-25 21:32:37 +00:00
|
|
|
if (addr != (void *)1) {
|
|
|
|
error = ptrace_set_pc(td2,
|
|
|
|
(u_long)(uintfptr_t)addr);
|
|
|
|
if (error)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
switch (req) {
|
|
|
|
case PT_TO_SCE:
|
|
|
|
p->p_stops |= S_PT_SCE;
|
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;
|
2010-05-25 21:32:37 +00:00
|
|
|
case PT_TO_SCX:
|
|
|
|
p->p_stops |= S_PT_SCX;
|
|
|
|
break;
|
|
|
|
case PT_SYSCALL:
|
|
|
|
p->p_stops |= S_PT_SCE | S_PT_SCX;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 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_oppid = 0;
|
2011-06-14 17:09:30 +00:00
|
|
|
p->p_flag &= ~(P_TRACED | P_WAITED | P_FOLLOWFORK);
|
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); */
|
2010-05-25 21:32:37 +00:00
|
|
|
break;
|
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) {
|
2012-07-09 09:24:46 +00:00
|
|
|
FOREACH_THREAD_IN_PROC(p, td3)
|
2008-10-15 06:31:37 +00:00
|
|
|
td3->td_dbgflags &= ~TDB_SUSPEND;
|
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);
|
2012-07-09 09:24:46 +00:00
|
|
|
if (req == PT_ATTACH)
|
|
|
|
kern_psignal(p, data);
|
2007-10-09 00:03:39 +00:00
|
|
|
} else {
|
|
|
|
if (data)
|
2011-09-16 13:58:51 +00:00
|
|
|
kern_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:
|
2010-01-23 11:45:35 +00:00
|
|
|
td2->td_dbgflags |= TDB_USERWR;
|
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:
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
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;
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
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:
|
2010-01-23 11:45:35 +00:00
|
|
|
td2->td_dbgflags |= TDB_USERWR;
|
2002-03-16 02:40:02 +00:00
|
|
|
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);
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2005-06-30 07:49:22 +00:00
|
|
|
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:
|
2010-01-23 11:45:35 +00:00
|
|
|
td2->td_dbgflags |= TDB_USERWR;
|
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:
|
2010-01-23 11:45:35 +00:00
|
|
|
td2->td_dbgflags |= TDB_USERWR;
|
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:
|
2010-01-23 11:45:35 +00:00
|
|
|
td2->td_dbgflags |= TDB_USERWR;
|
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:
|
2010-07-04 11:48:30 +00:00
|
|
|
if (data <= 0 ||
|
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
(!wrap32 && data > sizeof(*pl)) ||
|
|
|
|
(wrap32 && data > sizeof(*pl32))) {
|
|
|
|
#else
|
|
|
|
data > sizeof(*pl)) {
|
|
|
|
#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
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
2010-07-04 11:48:30 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
if (wrap32) {
|
|
|
|
pl = &plr;
|
|
|
|
pl32 = addr;
|
|
|
|
} else
|
|
|
|
#endif
|
2004-07-12 05:07:50 +00:00
|
|
|
pl = addr;
|
2005-12-24 02:59:29 +00:00
|
|
|
pl->pl_lwpid = td2->td_tid;
|
2012-08-08 00:20:30 +00:00
|
|
|
pl->pl_event = PL_EVENT_NONE;
|
2006-10-26 21:42:22 +00:00
|
|
|
pl->pl_flags = 0;
|
2010-07-04 11:48:30 +00:00
|
|
|
if (td2->td_dbgflags & TDB_XSIG) {
|
|
|
|
pl->pl_event = PL_EVENT_SIGNAL;
|
|
|
|
if (td2->td_dbgksi.ksi_signo != 0 &&
|
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
((!wrap32 && data >= offsetof(struct ptrace_lwpinfo,
|
|
|
|
pl_siginfo) + sizeof(pl->pl_siginfo)) ||
|
|
|
|
(wrap32 && data >= offsetof(struct ptrace_lwpinfo32,
|
|
|
|
pl_siginfo) + sizeof(struct siginfo32)))
|
|
|
|
#else
|
|
|
|
data >= offsetof(struct ptrace_lwpinfo, pl_siginfo)
|
|
|
|
+ sizeof(pl->pl_siginfo)
|
|
|
|
#endif
|
|
|
|
){
|
|
|
|
pl->pl_flags |= PL_FLAG_SI;
|
|
|
|
pl->pl_siginfo = td2->td_dbgksi.ksi_info;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((pl->pl_flags & PL_FLAG_SI) == 0)
|
|
|
|
bzero(&pl->pl_siginfo, sizeof(pl->pl_siginfo));
|
Reorganize syscall entry and leave handling.
Extend struct sysvec with three new elements:
sv_fetch_syscall_args - the method to fetch syscall arguments from
usermode into struct syscall_args. The structure is machine-depended
(this might be reconsidered after all architectures are converted).
sv_set_syscall_retval - the method to set a return value for usermode
from the syscall. It is a generalization of
cpu_set_syscall_retval(9) to allow ABIs to override the way to set a
return value.
sv_syscallnames - the table of syscall names.
Use sv_set_syscall_retval in kern_sigsuspend() instead of hardcoding
the call to cpu_set_syscall_retval().
The new functions syscallenter(9) and syscallret(9) are provided that
use sv_*syscall* pointers and contain the common repeated code from
the syscall() implementations for the architecture-specific syscall
trap handlers.
Syscallenter() fetches arguments, calls syscall implementation from
ABI sysent table, and set up return frame. The end of syscall
bookkeeping is done by syscallret().
Take advantage of single place for MI syscall handling code and
implement ptrace_lwpinfo pl_flags PL_FLAG_SCE, PL_FLAG_SCX and
PL_FLAG_EXEC. The SCE and SCX flags notify the debugger that the
thread is stopped at syscall entry or return point respectively. The
EXEC flag augments SCX and notifies debugger that the process address
space was changed by one of exec(2)-family syscalls.
The i386, amd64, sparc64, sun4v, powerpc and ia64 syscall()s are
changed to use syscallenter()/syscallret(). MIPS and arm are not
converted and use the mostly unchanged syscall() implementation.
Reviewed by: jhb, marcel, marius, nwhitehorn, stas
Tested by: marcel (ia64), marius (sparc64), nwhitehorn (powerpc),
stas (mips)
MFC after: 1 month
2010-05-23 18:32:02 +00:00
|
|
|
if (td2->td_dbgflags & TDB_SCE)
|
|
|
|
pl->pl_flags |= PL_FLAG_SCE;
|
|
|
|
else if (td2->td_dbgflags & TDB_SCX)
|
|
|
|
pl->pl_flags |= PL_FLAG_SCX;
|
|
|
|
if (td2->td_dbgflags & TDB_EXEC)
|
|
|
|
pl->pl_flags |= PL_FLAG_EXEC;
|
2011-01-25 10:59:21 +00:00
|
|
|
if (td2->td_dbgflags & TDB_FORK) {
|
|
|
|
pl->pl_flags |= PL_FLAG_FORKED;
|
|
|
|
pl->pl_child_pid = td2->td_dbg_forked;
|
|
|
|
}
|
2012-02-10 00:02:13 +00:00
|
|
|
if (td2->td_dbgflags & TDB_CHILD)
|
|
|
|
pl->pl_flags |= PL_FLAG_CHILD;
|
2006-02-06 09:41:56 +00:00
|
|
|
pl->pl_sigmask = td2->td_sigmask;
|
|
|
|
pl->pl_siglist = td2->td_siglist;
|
Add the ability for GDB to printout the thread name along with other
thread specific informations.
In order to do that, and in order to avoid KBI breakage with existing
infrastructure the following semantic is implemented:
- For live programs, a new member to the PT_LWPINFO is added (pl_tdname)
- For cores, a new ELF note is added (NT_THRMISC) that can be used for
storing thread specific, miscellaneous, informations. Right now it is
just popluated with a thread name.
GDB, then, retrieves the correct informations from the corefile via the
BFD interface, as it groks the ELF notes and create appropriate
pseudo-sections.
Sponsored by: Sandvine Incorporated
Tested by: gianni
Discussed with: dim, kan, kib
MFC after: 2 weeks
2010-11-22 14:42:13 +00:00
|
|
|
strcpy(pl->pl_tdname, td2->td_name);
|
2010-07-04 11:48:30 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
|
|
|
if (wrap32)
|
|
|
|
ptrace_lwpinfo_to32(pl, pl32);
|
|
|
|
#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
|
|
|
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
|
|
|
|
2010-02-09 05:52:35 +00:00
|
|
|
case PT_VM_TIMESTAMP:
|
|
|
|
td->td_retval[0] = p->p_vmspace->vm_map.timestamp;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PT_VM_ENTRY:
|
2010-02-11 18:00:53 +00:00
|
|
|
PROC_UNLOCK(p);
|
2010-03-11 14:49:06 +00:00
|
|
|
#ifdef COMPAT_FREEBSD32
|
2010-02-11 18:00:53 +00:00
|
|
|
if (wrap32)
|
|
|
|
error = ptrace_vm_entry32(td, p, addr);
|
|
|
|
else
|
2010-02-09 17:20:00 +00:00
|
|
|
#endif
|
2010-02-09 05:52:35 +00:00
|
|
|
error = ptrace_vm_entry(td, p, addr);
|
|
|
|
PROC_LOCK(p);
|
|
|
|
break;
|
|
|
|
|
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);
|
|
|
|
}
|
2013-09-19 18:53:42 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
protect_setchild(struct thread *td, struct proc *p, int flags)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_LOCK_ASSERT(p, MA_OWNED);
|
|
|
|
if (p->p_flag & P_SYSTEM || p_cansee(td, p) != 0)
|
|
|
|
return (0);
|
|
|
|
if (flags & PPROT_SET) {
|
|
|
|
p->p_flag |= P_PROTECTED;
|
|
|
|
if (flags & PPROT_INHERIT)
|
|
|
|
p->p_flag2 |= P2_INHERIT_PROTECTED;
|
|
|
|
} else {
|
|
|
|
p->p_flag &= ~P_PROTECTED;
|
|
|
|
p->p_flag2 &= ~P2_INHERIT_PROTECTED;
|
|
|
|
}
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
protect_setchildren(struct thread *td, struct proc *top, int flags)
|
|
|
|
{
|
|
|
|
struct proc *p;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
p = top;
|
|
|
|
ret = 0;
|
|
|
|
sx_assert(&proctree_lock, SX_LOCKED);
|
|
|
|
for (;;) {
|
|
|
|
ret |= protect_setchild(td, p, flags);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
/*
|
|
|
|
* If this process has children, descend to them next,
|
|
|
|
* otherwise do any siblings, and if done with this level,
|
|
|
|
* follow back up the tree (but not past top).
|
|
|
|
*/
|
|
|
|
if (!LIST_EMPTY(&p->p_children))
|
|
|
|
p = LIST_FIRST(&p->p_children);
|
|
|
|
else for (;;) {
|
|
|
|
if (p == top) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
return (ret);
|
|
|
|
}
|
|
|
|
if (LIST_NEXT(p, p_sibling)) {
|
|
|
|
p = LIST_NEXT(p, p_sibling);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p = p->p_pptr;
|
|
|
|
}
|
|
|
|
PROC_LOCK(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
protect_set(struct thread *td, struct proc *p, int flags)
|
|
|
|
{
|
|
|
|
int error, ret;
|
|
|
|
|
|
|
|
switch (PPROT_OP(flags)) {
|
|
|
|
case PPROT_SET:
|
|
|
|
case PPROT_CLEAR:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((PPROT_FLAGS(flags) & ~(PPROT_DESCEND | PPROT_INHERIT)) != 0)
|
|
|
|
return (EINVAL);
|
|
|
|
|
|
|
|
error = priv_check(td, PRIV_VM_MADV_PROTECT);
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
|
|
|
|
if (flags & PPROT_DESCEND)
|
|
|
|
ret = protect_setchildren(td, p, flags);
|
|
|
|
else
|
|
|
|
ret = protect_setchild(td, p, flags);
|
|
|
|
if (ret == 0)
|
|
|
|
return (EPERM);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _SYS_SYSPROTO_H_
|
|
|
|
struct procctl_args {
|
|
|
|
idtype_t idtype;
|
|
|
|
id_t id;
|
|
|
|
int com;
|
|
|
|
void *data;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
/* ARGSUSED */
|
|
|
|
int
|
|
|
|
sys_procctl(struct thread *td, struct procctl_args *uap)
|
|
|
|
{
|
|
|
|
int error, flags;
|
|
|
|
void *data;
|
|
|
|
|
|
|
|
switch (uap->com) {
|
|
|
|
case PROC_SPROTECT:
|
|
|
|
error = copyin(uap->data, &flags, sizeof(flags));
|
|
|
|
if (error)
|
|
|
|
return (error);
|
|
|
|
data = &flags;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (kern_procctl(td, uap->idtype, uap->id, uap->com, data));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
kern_procctl_single(struct thread *td, struct proc *p, int com, void *data)
|
|
|
|
{
|
|
|
|
|
|
|
|
PROC_LOCK_ASSERT(p, MA_OWNED);
|
|
|
|
switch (com) {
|
|
|
|
case PROC_SPROTECT:
|
|
|
|
return (protect_set(td, p, *(int *)data));
|
|
|
|
default:
|
|
|
|
return (EINVAL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
kern_procctl(struct thread *td, idtype_t idtype, id_t id, int com, void *data)
|
|
|
|
{
|
|
|
|
struct pgrp *pg;
|
|
|
|
struct proc *p;
|
|
|
|
int error, first_error, ok;
|
|
|
|
|
|
|
|
sx_slock(&proctree_lock);
|
|
|
|
switch (idtype) {
|
|
|
|
case P_PID:
|
|
|
|
p = pfind(id);
|
|
|
|
if (p == NULL) {
|
|
|
|
error = ESRCH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (p->p_state == PRS_NEW)
|
|
|
|
error = ESRCH;
|
|
|
|
else
|
|
|
|
error = p_cansee(td, p);
|
|
|
|
if (error == 0)
|
|
|
|
error = kern_procctl_single(td, p, com, data);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
break;
|
|
|
|
case P_PGID:
|
|
|
|
/*
|
|
|
|
* Attempt to apply the operation to all members of the
|
|
|
|
* group. Ignore processes in the group that can't be
|
|
|
|
* seen. Ignore errors so long as at least one process is
|
|
|
|
* able to complete the request successfully.
|
|
|
|
*/
|
|
|
|
pg = pgfind(id);
|
|
|
|
if (pg == NULL) {
|
|
|
|
error = ESRCH;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
PGRP_UNLOCK(pg);
|
|
|
|
ok = 0;
|
|
|
|
first_error = 0;
|
|
|
|
LIST_FOREACH(p, &pg->pg_members, p_pglist) {
|
|
|
|
PROC_LOCK(p);
|
|
|
|
if (p->p_state == PRS_NEW || p_cansee(td, p) != 0) {
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
error = kern_procctl_single(td, p, com, data);
|
|
|
|
PROC_UNLOCK(p);
|
|
|
|
if (error == 0)
|
|
|
|
ok = 1;
|
|
|
|
else if (first_error == 0)
|
|
|
|
first_error = error;
|
|
|
|
}
|
|
|
|
if (ok)
|
|
|
|
error = 0;
|
|
|
|
else if (first_error != 0)
|
|
|
|
error = first_error;
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Was not able to see any processes in the
|
|
|
|
* process group.
|
|
|
|
*/
|
|
|
|
error = ESRCH;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
error = EINVAL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
sx_sunlock(&proctree_lock);
|
|
|
|
return (error);
|
|
|
|
}
|