/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
*
* Portions Copyright 2007 Chad Mynhier
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <memory.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <signal.h>
#include <atomic.h>
#include <sys/resource.h>
#include <sys/sysmacros.h>
#include "libproc.h"
#include "Pcontrol.h"
#include "Putil.h"
#include "P32ton.h"
/* of symbol tables */
/*
* Function prototypes for static routines in this module.
*/
static void deadcheck(struct ps_prochandle *);
static void restore_tracing_flags(struct ps_prochandle *);
/*
* /proc/<pid>/as file:
*/
static ssize_t
{
}
static ssize_t
{
}
/*
* This is the library's .init handler.
*/
#pragma init(_libproc_init)
void
_libproc_init(void)
{
(void) sigfillset(&blockable_sigs);
}
void
{
}
/*
* Call set_minfd() once before calling dupfd() several times.
* We assume that the application will not reduce its current file
* descriptor limit lower than 512 once it has set at least that value.
*/
int
set_minfd(void)
{
int fd;
(void) mutex_lock(&minfd_lock);
fd = 256;
fd = 3;
}
(void) mutex_unlock(&minfd_lock);
}
return (fd);
}
int
{
int mfd;
/*
* Make fd be greater than 255 (the 32-bit stdio limit),
* or at least make it greater than 2 so that the
* program will work when spawned by init(1m).
* Also, if dfd is non-zero, dup the fd to be dfd.
*/
if (dfd <= 0)
}
/*
* Mark it close-on-exec so any created process doesn't inherit it.
*/
if (fd >= 0)
return (fd);
}
/*
* Create a new controlled process.
* Leave it stopped on successful exit from exec() or execve().
* Return an opaque pointer to its process control structure.
* Return NULL if process cannot be created (fork()/exec() not successful).
*/
struct ps_prochandle *
char *const *argv, /* argument vector */
char *const *envp, /* environment */
int *perr, /* pointer to error return code */
char *path, /* if non-null, holds exec path name on return */
{
struct ps_prochandle *P;
int fd;
char *fname;
int rc;
int lasterrno = 0;
if (len == 0) /* zero length, no path */
*path = '\0';
return (NULL);
}
free(P);
return (NULL);
}
if (pid == 0) { /* child process */
extern char **environ;
/*
* If running setuid or setgid, reset credentials to normal.
*/
Pcreate_callback(P); /* execute callback (see below) */
(void) pause(); /* wait for PRSABORT from parent */
/*
* This is ugly. There is no execvep() function that takes a
* path and an environment. We cheat here by replacing the
* global 'environ' variable right before we call this.
*/
if (envp)
_exit(127);
}
/*
* Initialize the process structure.
*/
(void) memset(P, 0, sizeof (*P));
P->asfd = -1;
P->ctlfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
P->ops = &P_live_ops;
Pinitsym(P);
/*
*/
procfs_path, (int)pid);
(void) set_minfd();
/*
* Exclusive write open advises others not to interfere.
* There is no reason for any of these open()s to fail.
*/
dprintf("Pcreate: failed to open %s: %s\n",
goto bad;
}
dprintf("Pcreate: failed to open %s: %s\n",
goto bad;
}
dprintf("Pcreate: failed to open %s: %s\n",
goto bad;
}
(void) Pstop(P, 0); /* stop the controlled process */
/*
* Wait for process to sleep in pause().
* If the process has already called pause(), then it should be
* stopped (PR_REQUESTED) while asleep in pause and we are done.
* running again, expecting it to stop when it reaches pause().
* There is no reason for this to fail other than an interrupt.
*/
for (;;) {
break;
Psetrun(P, 0, 0) != 0) { /* can't restart */
else {
dprintf("Pcreate: Psetrun failed: %s\n",
}
goto bad;
}
(void) Pwait(P, 0);
}
/*
* Kick the process off the pause() and catch
* it again on entry to exec() or exit().
*/
goto bad;
}
(void) Pwait(P, 0);
goto bad;
}
/*
* Move the process through instances of failed exec()s
* to reach the point of stopped on successful exec().
*/
/*
* Fetch the exec path name now, before we complete
* the exec(). We may lose the process and be unable
* to get the information later.
*/
/*
* Set the process running and wait for
* it to stop on exit from the exec().
*/
(void) Psetrun(P, 0, 0);
(void) Pwait(P, 0);
Preopen(P) != 0) { /* and we can't get it back */
goto bad;
}
/*
* If the exec() failed, continue the loop, expecting
* there to be more attempts to exec(), based on PATH.
*/
/*
* The exec() failed. Set the process running and
* wait for it to stop on entry to the next exec().
*/
(void) Psetrun(P, 0, 0);
(void) Pwait(P, 0);
continue;
}
break;
}
/*
* The process is stopped on successful exec() or execve().
* Turn off all tracing flags and return success.
*/
#ifndef _LP64
/* We must be a 64-bit process to deal with a 64-bit process */
goto bad;
}
#endif
/*
* Set run-on-last-close so the controlled process
* runs even if we die on a signal.
*/
*perr = 0;
return (P);
}
bad:
*path = '\0';
Pfree(P);
return (NULL);
}
struct ps_prochandle *
const char *file, /* executable file name */
char *const *argv, /* argument vector */
int *perr, /* pointer to error return code */
char *path, /* if non-null, holds exec path name on return */
{
}
/*
* Return a printable string corresponding to a Pcreate() error return.
*/
const char *
{
const char *str;
switch (error) {
case C_FORK:
str = "cannot fork";
break;
case C_PERM:
str = "file is set-id or unreadable";
break;
case C_NOEXEC:
str = "cannot execute file";
break;
case C_INTR:
str = "operation interrupted";
break;
case C_LP64:
str = "program is _LP64, self is not";
break;
case C_STRANGE:
str = "unanticipated system error";
break;
case C_NOENT:
str = "cannot find executable file";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Callback to execute in each child process created with Pcreate() after fork
* but before it execs the new process image. By default, we do nothing, but
* by calling this function we allow the client program to define its own
* version of the function which will interpose on our empty default. This
* may be useful for clients that need to modify signal dispositions, terminal
* attributes, or process group and session properties for each new victim.
*/
/*ARGSUSED*/
void
{
/* nothing to do here */
}
/*
* Grab an existing process.
* Return an opaque pointer to its process control structure.
*
* pid: UNIX process ID.
* flags:
* PGRAB_RETAIN Retain tracing flags (default clears all tracing flags).
* PGRAB_FORCE Grab regardless of whether process is already traced.
* PGRAB_RDONLY Open the address space file O_RDONLY instead of O_RDWR,
* and do not open the process control file.
* PGRAB_NOSTOP Open the process but do not force it to stop.
* perr: pointer to error return code.
*/
struct ps_prochandle *
{
struct ps_prochandle *P;
char *fname;
int rc = 0;
/*
* PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
* and so it implies RETAIN and NOSTOP since both require control.
*/
if (flags & PGRAB_RDONLY)
return (NULL);
}
P->asfd = -1;
P->ctlfd = -1;
P->statfd = -1;
again: /* Come back here if we lose it in the Window of Vulnerability */
if (P->ctlfd >= 0)
if (P->asfd >= 0)
if (P->statfd >= 0)
(void) memset(P, 0, sizeof (*P));
P->ctlfd = -1;
P->asfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
P->ops = &P_live_ops;
Pinitsym(P);
/*
*/
procfs_path, (int)pid);
(void) set_minfd();
/*
* Request exclusive open to avoid grabbing someone else's
* process and to prevent others from interfering afterwards.
* If this fails and the 'PGRAB_FORCE' flag is set, attempt to
* open non-exclusively.
*/
switch (errno) {
case ENOENT:
break;
case EACCES:
case EPERM:
break;
case EMFILE:
break;
case EBUSY:
break;
}
/* FALLTHROUGH */
default:
dprintf("Pgrab: failed to open %s: %s\n",
break;
}
goto err;
}
switch (errno) {
case ENOENT:
break;
case EMFILE:
break;
default:
dprintf("Pgrab: failed to open %s: %s\n",
break;
}
goto err;
}
if (!(flags & PGRAB_RDONLY)) {
switch (errno) {
case ENOENT:
break;
case EMFILE:
break;
default:
dprintf("Pgrab: failed to open %s: %s\n",
break;
}
goto err;
}
}
/*
* We are now in the Window of Vulnerability (WoV). The process may
* and the PCSTOP. We will get EAGAIN in this case and must start over.
* As Pstopstatus will trigger the first read() from a /proc file,
* we also need to handle EOVERFLOW here when 32-bit as an indicator
* that this process is 64-bit. Finally, if the process has become
* a zombie (PS_UNDEAD) while we were trying to grab it, just remain
* silent about this and pretend there was no process.
*/
if (Pstopstatus(P, PCNULL, 0) != 0) {
#ifndef _LP64
goto err;
}
#endif
(void) mutex_destroy(&P->proc_lock);
goto again;
}
else
goto err;
}
/*
* If the process is a system process, we can't control it even as root
*/
goto err;
}
#ifndef _LP64
/*
* We must be a 64-bit process to deal with a 64-bit process
*/
goto err;
}
#endif
/*
* Remember the status for use by Prelease().
*/
/*
* Before stopping the process, make sure we are not grabbing ourselves.
* If we are, make sure we are doing it PGRAB_RDONLY.
*/
/*
* Verify that the process is really ourself:
* Set a magic number, read it through the
* /proc file and see if the results match.
*/
errno = 0;
== sizeof (magic2) &&
magic2 == 0 &&
(magic1 = 0xfeedbeef) &&
== sizeof (magic2) &&
magic2 == 0xfeedbeef &&
!(flags & PGRAB_RDONLY)) {
goto err;
}
}
/*
* If the process is already stopped or has been directed
* to stop via /proc, do not set run-on-last-close.
*/
!(flags & PGRAB_RDONLY)) {
/*
* Mark the process run-on-last-close so
* it runs even if we die from SIGKILL.
*/
(void) mutex_destroy(&P->proc_lock);
goto again;
}
else {
dprintf("Pgrab: failed to set RLC\n");
}
goto err;
}
}
/*
* If a stop directive is pending and the process has not yet stopped,
* then synchronously wait for the stop directive to take effect.
* Limit the time spent waiting for the process to stop by iterating
* at most 10 times. The time-out of 20 ms corresponds to the time
* between sending the stop directive and the process actually stopped
* as measured by DTrace on a slow, busy system. If the process doesn't
* stop voluntarily, clear the PR_DSTOP flag so that the code below
* forces the process to stop.
*/
if (!(flags & PGRAB_RDONLY)) {
int niter = 0;
niter++;
if (flags & PGRAB_NOSTOP)
break;
}
/* Try it harder down below */
}
}
/*
* If the process is not already stopped or directed to stop
* and PGRAB_NOSTOP was not specified, stop the process now.
*/
!(flags & PGRAB_NOSTOP)) {
/*
*/
Pstopstatus(P, PCDSTOP, 0) != 0) ||
#ifndef _LP64
goto err;
}
#endif
(void) mutex_destroy(&P->proc_lock);
goto again;
}
dprintf("Pgrab: failed to PCSTOP\n");
} else {
}
goto err;
}
}
/*
* Process should now either be stopped via /proc or there
* should be an outstanding stop directive.
*/
dprintf("Pgrab: process is not stopped\n");
goto err;
}
#ifndef _LP64
/*
* Test this again now because the 32-bit victim process may
* have exec'd a 64-bit process in the meantime.
*/
goto err;
}
#endif
}
/*
* Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
*/
if (!(flags & PGRAB_RETAIN)) {
Psync(P);
}
*perr = 0;
return (P);
err:
Pfree(P);
return (NULL);
}
/*
* Return a printable string corresponding to a Pgrab() error return.
*/
const char *
{
const char *str;
switch (error) {
case G_NOPROC:
str = "no such process";
break;
case G_NOCORE:
str = "no such core file";
break;
case G_NOPROCORCORE:
str = "no such process or core file";
break;
case G_NOEXEC:
str = "cannot find executable file";
break;
case G_ZOMB:
str = "zombie process";
break;
case G_PERM:
str = "permission denied";
break;
case G_BUSY:
str = "process is traced";
break;
case G_SYS:
str = "system process";
break;
case G_SELF:
str = "attempt to grab self";
break;
case G_INTR:
str = "operation interrupted";
break;
case G_LP64:
str = "program is _LP64, self is not";
break;
case G_FORMAT:
str = "file is not an ELF core file";
break;
case G_ELF:
str = "libelf error";
break;
case G_NOTE:
str = "core file is corrupt or missing required data";
break;
case G_STRANGE:
str = "unanticipated system error";
break;
case G_ISAINVAL:
str = "wrong ELF machine type";
break;
case G_BADLWPS:
str = "bad lwp specification";
break;
case G_NOFD:
str = "too many open files";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Free a process control structure.
* Close the file descriptors but don't do the Prelease logic.
*/
void
{
uint_t i;
extern void __priv_free_info(void *);
#ifdef __sparc
#endif
}
#endif
}
P->ucnelems = 0;
}
(void) mutex_lock(&P->proc_lock);
struct ps_lwphandle *L;
for (i = 0; i < HASHSIZE; i++) {
Lfree_internal(P, L);
}
}
(void) mutex_unlock(&P->proc_lock);
(void) mutex_destroy(&P->proc_lock);
if (P->agentctlfd >= 0)
(void) close(P->agentctlfd);
if (P->agentstatfd >= 0)
(void) close(P->agentstatfd);
if (P->ctlfd >= 0)
if (P->asfd >= 0)
if (P->statfd >= 0)
Preset_maps(P);
/* clear out the structure as a precaution against reuse */
(void) memset(P, 0, sizeof (*P));
P->ctlfd = -1;
P->asfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
free(P);
}
/*
* Return the state of the process, one of the PS_* values.
*/
int
{
return (P->state);
}
/*
* Return the open address space file descriptor for the process.
* Clients must not close this file descriptor, not use it
* after the process is freed.
*/
int
{
return (P->asfd);
}
/*
* Return the open control file descriptor for the process.
* Clients must not close this file descriptor, not use it
* after the process is freed.
*/
int
{
return (P->ctlfd);
}
/*
* Return a pointer to the process psinfo structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Prelease().
*/
const psinfo_t *
{
return (NULL);
}
return (NULL);
return (&P->psinfo);
}
/*
* Return a pointer to the process status structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Prelease().
*/
const pstatus_t *
{
return (&P->status);
}
/*
* Fill in a pointer to a process credentials structure. The ngroups parameter
* is the number of supplementary group entries allocated in the caller's cred
* structure. It should equal zero or one unless extra space has been
* allocated for the group list by the caller.
*/
int
{
return (-1);
}
/*
* Avoid returning more supplementary group data than the
* caller has allocated in their buffer. We expect them to
* check pr_ngroups afterward and potentially call us again.
*/
return (0);
}
return (-1);
}
/*
* Fill in a pointer to a process LDT structure.
* The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
* If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
* Otherwise we return the actual number of LDT entries fetched (<= nldt).
*/
int
{
return (-1);
}
return (nldt);
}
return (-1);
}
#endif /* __i386 */
/*
* Fill in a pointer to a process privilege structure.
*/
{
if (size == 0)
return (PRIV_PRPRIV_SIZE(pp));
return (size);
}
return (-1);
}
/* Return the actual size. */
if (size == 0)
return (P->core->core_priv_size);
return (size);
}
return (-1);
}
int
{
int rc;
long *ctl;
return (-1);
}
return (-1);
rc = -1;
else
rc = 0;
return (rc);
}
void *
{
/* Use default from libc */
return (NULL);
return (P->core->core_privinfo);
}
/*
* Ensure that all cached state is written to the process.
* The cached state is the LWP's signal mask and registers
* and the process's tracing flags.
*/
void
{
int n = 0;
}
#ifdef __i386
/* XX64 we should probably restore REG_GS after this */
if (ctlfd == P->agentctlfd)
/* XX64 */
#endif
}
}
}
}
}
return; /* nothing to do or write failed */
}
/*
* Reopen the /proc file (after PS_LOST).
*/
int
{
int fd;
char *fname;
return (0);
if (P->agentcnt > 0) {
P->agentcnt = 1;
Pdestroy_agent(P);
}
procfs_path, (int)P->pid);
dprintf("Preopen: failed to open %s: %s\n",
if (fd >= 0)
return (-1);
}
dprintf("Preopen: failed to open %s: %s\n",
if (fd >= 0)
return (-1);
}
dprintf("Preopen: failed to open %s: %s\n",
if (fd >= 0)
return (-1);
}
/*
* Set the state to PS_RUN and wait for the process to stop so that
* we re-read the status from the new P->statfd. If this fails, Pwait
* will reset the state to PS_LOST and we fail the reopen. Before
* returning, we also forge a bit of P->status to allow the debugger to
* see that we are PS_LOST following a successful exec.
*/
if (Pwait(P, 0) == -1) {
#ifdef _ILP32
#endif
return (-1);
}
/*
* The process should be stopped on exec (REQUESTED)
* or else should be stopped on exit from exec() (SYSEXIT)
*/
/* fake up stop-on-exit-from-execve */
}
} else {
dprintf("Preopen: expected REQUESTED or "
"SYSEXIT(SYS_execve) stop\n");
}
return (0);
}
/*
* Define all settable flags other than the microstate accounting flags.
*/
/*
* Restore /proc tracing flags to their original values
* in preparation for releasing the process.
* Also called by Pcreate() to clear all tracing flags.
*/
static void
{
long flags;
/* we created this process; clear all tracing flags */
(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
} else {
/* we grabbed the process; restore its tracing flags */
(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
if (flags)
}
}
}
/*
* Release the process. Frees the process control structure.
* flags:
* PRELEASE_CLEAR Clear all tracing flags.
* PRELEASE_RETAIN Retain current tracing flags.
* PRELEASE_HANG Leave the process stopped and abandoned.
* PRELEASE_KILL Terminate the process with SIGKILL.
*/
void
{
dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
(void *)P, (int)P->pid);
Pfree(P);
return;
}
dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
(void *)P, fptr->file_pname);
Pfree(P);
return;
}
dprintf("Prelease: releasing handle %p pid %d\n",
(void *)P, (int)P->pid);
if (P->ctlfd == -1) {
Pfree(P);
return;
}
if (P->agentcnt > 0) {
P->agentcnt = 1;
Pdestroy_agent(P);
}
/*
* Attempt to stop the process.
*/
(void) Pstop(P, 1000);
if (flags & PRELEASE_KILL) {
Pfree(P);
return;
}
/*
* If we lost control, all we can do now is close the files.
* In this case, the last close sets the process running.
*/
Pfree(P);
return;
}
/*
* We didn't lose control; we do more.
*/
Psync(P);
if (flags & PRELEASE_CLEAR)
if (!(flags & PRELEASE_RETAIN))
if (flags & PRELEASE_HANG) {
/* Leave the process stopped and abandoned */
Pfree(P);
return;
}
/*
* Set the process running if we created it or if it was
* not originally stopped or directed to stop via /proc
* or if we were given the PRELEASE_CLEAR flag.
*/
/*
* We do this repeatedly because the process may have
* more than one LWP stopped on an event of interest.
* This makes sure all of them are set running.
*/
do {
break; /* Agent LWP may be stuck */
} while (Pstopstatus(P, PCNULL, 0) == 0 &&
dprintf("Prelease: failed to set process running\n");
}
Pfree(P);
}
/* debugging */
void
{
case PR_REQUESTED:
break;
case PR_SIGNALLED:
break;
case PR_FAULTED:
break;
case PR_SYSENTRY:
break;
case PR_SYSEXIT:
break;
case PR_JOBCONTROL:
break;
case PR_SUSPENDED:
break;
default:
break;
}
if (bits)
}
/* debugging */
static void
{
if (bits)
}
/*
* Wait for the specified process to stop or terminate.
* Or, just get the current status (PCNULL).
* Or, direct it to stop and get the current status (PCDSTOP).
* If the agent LWP exists, do these things to the agent,
* else do these things to the process as a whole.
*/
int
long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
{
int err;
switch (P->state) {
case PS_RUN:
break;
case PS_STOP:
return (0);
break;
case PS_LOST:
return (-1);
}
break;
case PS_UNDEAD:
case PS_DEAD:
case PS_IDLE:
return (-1);
}
break;
default: /* corrupted state */
return (-1);
}
rc = 0;
switch (request) {
case PCSTOP:
break;
case PCWSTOP:
break;
case PCDSTOP:
break;
case PCNULL:
return (0);
break;
default: /* programming error */
return (-1);
}
Psync(P);
if (P->agentstatfd < 0) {
} else {
}
if (err) {
switch (err) {
case EINTR: /* user typed ctl-C */
case ERESTART:
dprintf("Pstopstatus: EINTR\n");
break;
case EAGAIN: /* we lost control of the the process */
case EOVERFLOW:
break;
default: /* check for dead process */
if (_libproc_debug) {
const char *errstr;
switch (request) {
case PCNULL:
errstr = "Pstopstatus PCNULL"; break;
case PCSTOP:
errstr = "Pstopstatus PCSTOP"; break;
case PCDSTOP:
errstr = "Pstopstatus PCDSTOP"; break;
case PCWSTOP:
errstr = "Pstopstatus PCWSTOP"; break;
default:
errstr = "Pstopstatus PC???"; break;
}
}
deadcheck(P);
break;
}
return (-1);
}
}
return (0);
dprintf("Pstopstatus: process is not stopped\n");
return (-1);
}
if (_libproc_debug) /* debugging */
prdump(P);
/*
* If the process was already stopped coming into Pstopstatus(),
* then don't use its PC to set P->sysaddr since it may have been
* changed since the time the process originally stopped.
*/
return (0);
case PR_SYSENTRY:
case PR_SYSEXIT:
&P->sysaddr) == 0)
break;
case PR_REQUESTED:
case PR_SIGNALLED:
case PR_FAULTED:
case PR_JOBCONTROL:
case PR_SUSPENDED:
break;
default:
return (-1);
}
return (0);
}
/*
* Wait for the process to stop for any reason.
*/
int
{
}
/*
* Direct the process to stop; wait for it to stop.
*/
int
{
}
/*
* Direct the process to stop; don't wait.
*/
int
{
return (Pstopstatus(P, PCDSTOP, 0));
}
static void
{
int fd;
void *buf;
if (P->statfd < 0)
else {
if (P->agentstatfd < 0) {
} else {
fd = P->agentstatfd;
}
switch (errno) {
default:
break;
case EINTR:
case ERESTART:
continue;
case EAGAIN:
break;
}
break;
}
}
}
/*
* Get the value of one register from stopped process.
*/
int
{
return (-1);
}
return (-1);
}
return (-1);
}
return (0);
}
/*
* Put value of one register into stopped process.
*/
int
{
return (-1);
}
return (-1);
}
return (0);
}
int
int sig, /* signal to pass to process */
int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
{
2 ]; /* PCRUN */
return (-1);
}
Psync(P); /* flush tracing flags and registers */
}
/* make current signal */
}
P->info_valid = 0; /* will need to update map and file info */
/*
* If we've cached ucontext-list information while we were stopped,
* free it now.
*/
P->ucnelems = 0;
}
/* If it is dead or lost, return the real status, not PS_RUN */
(void) Pstopstatus(P, PCNULL, 0);
return (0);
}
/* If it is not in a jobcontrol stop, issue an error message */
return (-1);
}
/* Otherwise pretend that the job-stopped process is running */
}
return (0);
}
void *buf, /* caller's buffer */
{
}
char *buf, /* caller's buffer */
{
int nbyte;
if (size < 2) {
return (-1);
}
size--; /* ensure trailing null fits in buffer */
*buf = '\0';
}
}
}
return (leng);
}
const void *buf, /* caller's buffer */
{
}
int
{
return (-1);
return (0);
}
int
{
return (-1);
return (0);
}
/*
* Set a breakpoint trap, return original instruction.
*/
int
{
return (-1);
}
/* fetch the old instruction */
/* write the BPT instruction */
return (-1);
/*
* Fail if there was already a breakpoint there from another debugger
* or DTrace's user-level tracing on x86.
*/
return (-1);
}
return (0);
}
/*
* Restore original instruction where a breakpoint was set.
*/
int
{
return (-1);
}
/*
* If the breakpoint instruction we had placed has been overwritten
* with a new instruction, then don't try to replace it with the
* old instruction. Doing do can cause problems with self-modifying
* code -- PLTs for example. If the Pread() fails, we assume that we
* should proceed though most likely the Pwrite() will also fail.
*/
return (0);
return (-1);
return (0);
}
/*
* Common code for Pxecbkpt() and Lxecbkpt().
* Develop the array of requests that will do the job, then
* write them to the specified control file descriptor.
* Return the non-zero errno if the write fails.
*/
static int
int ctlfd, /* process or LWP control file descriptor */
{
long ctl[
2 + /* PCRUN */
1 + /* PCWSTOP */
1 + /* PCCFAULT */
int error = 0;
/* block our signals for the duration */
/* hold posted signals */
/* force tracing of FLTTRACE */
}
/* restore the old instruction */
/* clear current signal and fault; set running w/ single-step */
/* wait for stop, cancel the fault */
/* restore the breakpoint trap */
/* restore fault tracing set */
}
/* restore the hold mask */
return (error);
}
/*
* Step over a breakpoint, i.e., execute the instruction that
* really belongs at the breakpoint location (the current %pc)
* and leave the process stopped at the next instruction.
*/
int
{
return (-1);
}
Psync(P);
if (error != 0) {
return (0);
}
return (0);
return (-1);
}
return (rv);
}
/*
* Install the watchpoint described by wp.
*/
int
{
return (-1);
}
return (-1);
return (0);
}
/*
* Remove the watchpoint described by wp.
*/
int
{
return (-1);
}
return (-1);
return (0);
}
/*
* Common code for Pxecwapt() and Lxecwapt(). Develop the array of requests
* that will do the job, then write them to the specified control file
* descriptor. Return the non-zero errno if the write fails.
*/
static int
int ctlfd, /* process or LWP control file descriptor */
{
long ctl[
2 + /* PCRUN */
1 + /* PCWSTOP */
1 + /* PCCFAULT */
int error = 0;
/*
* Hold all posted signals in the victim process prior to stepping.
*/
/*
* Force tracing of FLTTRACE since we need to single step.
*/
}
/*
* Clear only the current watchpoint by setting pr_wflags to zero.
*/
/*
* Clear the current signal and fault; set running with single-step.
* Then wait for the victim to stop and cancel the FLTTRACE.
*/
/*
* Restore the current watchpoint.
*/
/*
* Restore fault tracing set if we modified it.
*/
}
/*
* Restore the hold mask to the current hold mask (i.e. the one
* before we executed any of the previous operations).
*/
return (error);
}
/*
* Step over a watchpoint, i.e., execute the instruction that was stopped by
* the watchpoint, and then leave the LWP stopped at the next instruction.
*/
int
{
return (-1);
}
Psync(P);
if (error != 0) {
return (0);
}
return (0);
return (-1);
}
return (rv);
}
int
{
int rc;
rc = -1;
} else {
rc = 0;
}
return (rc);
}
int
{
int rc;
rc = -1;
} else {
rc = 0;
}
return (rc);
}
/*
* Common function to allow clients to manipulate the action to be taken
* on receipt of a signal, receipt of machine fault, entry to a system call,
* or exit from a system call. We make use of our private prset_* functions
* in order to make this code be common. The 'which' parameter identifies
* the code for the event of interest (0 means change the entire set), and
* the 'stop' parameter is a boolean indicating whether the process should
* stop when the event of interest occurs. The previous value is returned
* to the caller; -1 is returned if an error occurred.
*/
static int
{
int oldval;
return (-1);
}
return (-1);
}
if (stop) {
if (which == 0) {
} else if (!oldval) {
}
} else {
if (which == 0) {
} else if (oldval) {
}
}
Psync(P);
return (oldval);
}
/*
* Set action on specified signal.
*/
int
{
int oldval;
return (-1);
}
return (oldval);
}
/*
* Set all signal tracing flags.
*/
void
{
return;
Psync(P);
}
/*
* Set action on specified fault.
*/
int
{
}
/*
* Set all machine fault tracing flags.
*/
void
{
return;
Psync(P);
}
/*
* Set action on specified system call entry.
*/
int
{
}
/*
* Set all system call entry tracing flags.
*/
void
{
return;
Psync(P);
}
/*
* Set action on specified system call exit.
*/
int
{
}
/*
* Set all system call exit tracing flags.
*/
void
{
return;
Psync(P);
}
/*
* Utility function to read the contents of a file that contains a
* Returns a malloc()d buffer or NULL on failure.
*/
static prheader_t *
{
int fd;
if (fd >= 0)
return (NULL);
}
/*
* 'size' is just the initial guess at the buffer size.
* It will have to grow if the number of lwps increases
* while we are looking at the process.
* 'size' must be larger than the actual file size.
*/
for (;;) {
break;
rval <= sizeof (prheader_t)) {
break;
}
break;
/* need a bigger buffer */
size *= 2;
}
return (Lhp);
}
/*
* LWP iteration interface.
*/
int
{
long nlwp;
int rv;
switch (P->state) {
case PS_RUN:
(void) Pstopstatus(P, PCNULL, 0);
break;
case PS_STOP:
Psync(P);
break;
case PS_IDLE:
return (-1);
}
/*
* For either live processes or cores, the single LWP case is easy:
* the pstatus_t contains the lwpstatus_t for the only LWP.
*/
/*
* For the core file multi-LWP case, we just iterate through the
* list of LWP structs we read in from the core file.
*/
uint_t i;
break;
}
return (rv);
}
/*
* For the live process multi-LWP case, we have to work a little
*/
return (-1);
nlwp > 0;
break;
}
return (rv);
}
/*
* Extended LWP iteration interface.
* Iterate over all LWPs, active and zombie.
*/
int
{
long nstat;
long ninfo;
int rv;
(void) Pstopstatus(P, PCNULL, 0);
(void) Ppsinfo(P);
Psync(P);
/*
* For either live processes or cores, the single LWP case is easy:
* the pstatus_t contains the lwpstatus_t for the only LWP and
* the psinfo_t contains the lwpsinfo_t for the only LWP.
*/
/*
* For the core file multi-LWP case, we just iterate through the
* list of LWP structs we read in from the core file.
*/
uint_t i;
&lwp->lwp_status;
break;
}
return (rv);
}
/*
* For the live process multi-LWP case, we have to work a little
*/
return (-1);
return (-1);
}
/*
* If we are looking at a running process, or one we do not control,
* the active and zombie lwps in the process may have changed since
* we read the process status structure. If so, just start over.
*/
goto retry;
/*
* To be perfectly safe, prescan the two arrays, checking consistency.
* We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
* same order (the lwp directory order) in their respective files.
* We also rely on there being (possibly) more lwpsinfo_t's than
* lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
*/
/*
* Not a zombie lwp; check for matching lwpids.
*/
goto retry;
nstat--;
}
}
if (nstat != 0)
goto retry;
/*
* Rescan, this time for real.
*/
} else {
}
break;
}
return (rv);
}
{
return (P->core->core_content);
return (CC_CONTENT_ALL);
}
/*
* =================================================================
* The remainder of the functions in this file are for the
* control of individual LWPs in the controlled process.
* =================================================================
*/
/*
* Find an entry in the process hash table for the specified lwpid.
* The entry will either point to an existing struct ps_lwphandle
* or it will point to an empty slot for a new struct ps_lwphandle.
*/
static struct ps_lwphandle **
{
struct ps_lwphandle *L;
break;
return (Lp);
}
/*
* Grab an LWP contained within the controlled process.
* Return an opaque pointer to its LWP control structure.
* perr: pointer to error return code.
*/
struct ps_lwphandle *
{
struct ps_lwphandle *L;
int fd;
char *fname;
int rc = 0;
(void) mutex_lock(&P->proc_lock);
== NULL)
if (rc) {
(void) mutex_unlock(&P->proc_lock);
return (NULL);
}
(void) memset(L, 0, sizeof (*L));
L->lwp_ctlfd = -1;
L->lwp_statfd = -1;
L->lwp_proc = P;
*Lp = L; /* insert into the hash table */
goto err;
}
*perr = 0;
(void) mutex_unlock(&P->proc_lock);
return (L);
}
/*
* Open the /proc/<pid>/lwp/<lwpid> files
*/
(void) set_minfd();
switch (errno) {
case ENOENT:
break;
default:
dprintf("Lgrab: failed to open %s: %s\n",
break;
}
goto err;
}
L->lwp_statfd = fd;
switch (errno) {
case ENOENT:
break;
default:
dprintf("Lgrab: failed to read %s: %s\n",
break;
}
goto err;
}
switch (errno) {
case ENOENT:
break;
default:
dprintf("Lgrab: failed to open %s: %s\n",
break;
}
goto err;
}
L->lwp_state =
== (PR_STOPPED|PR_ISTOP))?
*perr = 0;
(void) mutex_unlock(&P->proc_lock);
return (L);
err:
Lfree_internal(P, L);
(void) mutex_unlock(&P->proc_lock);
return (NULL);
}
/*
* Return a printable string corresponding to an Lgrab() error return.
*/
const char *
{
const char *str;
switch (error) {
case G_NOPROC:
str = "no such LWP";
break;
case G_BUSY:
str = "LWP already grabbed";
break;
case G_STRANGE:
str = "unanticipated system error";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Free an LWP control structure.
*/
void
{
struct ps_prochandle *P = L->lwp_proc;
(void) mutex_lock(&P->proc_lock);
Lfree_internal(P, L);
(void) mutex_unlock(&P->proc_lock);
}
static void
{
if (L->lwp_ctlfd >= 0)
if (L->lwp_statfd >= 0)
(void) close(L->lwp_statfd);
/* clear out the structure as a precaution against reuse */
(void) memset(L, 0, sizeof (*L));
L->lwp_ctlfd = -1;
L->lwp_statfd = -1;
free(L);
}
/*
* Return the state of the process, one of the PS_* values.
*/
int
{
return (L->lwp_state);
}
/*
* Return the open control file descriptor for the LWP.
* Clients must not close this file descriptor, nor use it
* after the LWP is freed.
*/
int
{
return (L->lwp_ctlfd);
}
/*
* Return a pointer to the LWP lwpsinfo structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Lfree().
*/
const lwpsinfo_t *
{
return (NULL);
return (&L->lwp_psinfo);
}
/*
* Return a pointer to the LWP status structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Lfree().
*/
const lwpstatus_t *
{
return (&L->lwp_status);
}
/*
* Given an LWP handle, return the process handle.
*/
struct ps_prochandle *
{
return (L->lwp_proc);
}
/*
* Ensure that all cached state is written to the LWP.
* The cached state is the LWP's signal mask and registers.
*/
void
{
int n = 0;
}
}
return; /* nothing to do or write failed */
}
/*
* Wait for the specified LWP to stop or terminate.
* Or, just get the current status (PCNULL).
* Or, direct it to stop and get the current status (PCDSTOP).
*/
static int
long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
{
int err;
switch (L->lwp_state) {
case PS_RUN:
break;
case PS_STOP:
return (0);
break;
case PS_LOST:
return (-1);
}
break;
case PS_UNDEAD:
case PS_DEAD:
return (-1);
}
break;
default: /* corrupted state */
return (-1);
}
rc = 0;
switch (request) {
case PCSTOP:
break;
case PCWSTOP:
break;
case PCDSTOP:
break;
case PCNULL:
return (0); /* Nothing else to do for cores */
break;
default: /* programming error */
return (-1);
}
Lsync(L);
sizeof (L->lwp_status), (off_t)0) < 0)
if (err) {
switch (err) {
case EINTR: /* user typed ctl-C */
case ERESTART:
dprintf("Lstopstatus: EINTR\n");
break;
case EAGAIN: /* we lost control of the the process */
dprintf("Lstopstatus: EAGAIN\n");
return (-1);
default:
if (_libproc_debug) {
const char *errstr;
switch (request) {
case PCNULL:
errstr = "Lstopstatus PCNULL"; break;
case PCSTOP:
errstr = "Lstopstatus PCSTOP"; break;
case PCDSTOP:
errstr = "Lstopstatus PCDSTOP"; break;
case PCWSTOP:
errstr = "Lstopstatus PCWSTOP"; break;
default:
errstr = "Lstopstatus PC???"; break;
}
}
return (-1);
}
}
!= (PR_STOPPED|PR_ISTOP)) {
return (0);
dprintf("Lstopstatus: LWP is not stopped\n");
return (-1);
}
if (_libproc_debug) /* debugging */
switch (L->lwp_status.pr_why) {
case PR_SYSENTRY:
case PR_SYSEXIT:
case PR_REQUESTED:
case PR_SIGNALLED:
case PR_FAULTED:
case PR_JOBCONTROL:
case PR_SUSPENDED:
break;
default:
return (-1);
}
return (0);
}
/*
* Wait for the LWP to stop for any reason.
*/
int
{
}
/*
* Direct the LWP to stop; wait for it to stop.
*/
int
{
}
/*
* Direct the LWP to stop; don't wait.
*/
int
{
return (Lstopstatus(L, PCDSTOP, 0));
}
/*
* Get the value of one register from stopped LWP.
*/
int
{
return (-1);
}
return (-1);
}
return (0);
}
/*
* Put value of one register into stopped LWP.
*/
int
{
return (-1);
}
return (-1);
}
return (0);
}
int
int sig, /* signal to pass to LWP */
int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
{
2 ]; /* PCRUN */
return (-1);
}
Lsync(L); /* flush registers */
}
/* make current signal */
}
/* Pretend that a job-stopped LWP is running */
return (Lstopstatus(L, PCNULL, 0));
}
return (0);
}
int
{
return (-1);
L->lwp_status.pr_cursig = 0;
return (0);
}
int
{
return (-1);
return (0);
}
/*
* Step over a breakpoint, i.e., execute the instruction that
* really belongs at the breakpoint location (the current %pc)
* and leave the LWP stopped at the next instruction.
*/
int
{
struct ps_prochandle *P = L->lwp_proc;
return (-1);
}
Lsync(L);
if (error != 0) {
return (0);
}
return (0);
return (-1);
}
return (rv);
}
/*
* Step over a watchpoint, i.e., execute the instruction that was stopped by
* the watchpoint, and then leave the LWP stopped at the next instruction.
*/
int
{
struct ps_prochandle *P = L->lwp_proc;
return (-1);
}
Lsync(L);
if (error != 0) {
return (0);
}
return (0);
return (-1);
}
return (rv);
}
int
{
struct ps_prochandle *P = L->lwp_proc;
return (-1);
#ifdef _LP64
} else {
return (-1);
#endif
}
return (0);
}
int
{
struct ps_prochandle *P = L->lwp_proc;
return (-1);
/*
* If the SS_ONSTACK flag is set then this LWP is operating on the
* alternate signal stack. We can recover the original stack from
* pr_oldcontext.
*/
return (0);
return (-1);
#ifdef _LP64
} else {
return (-1);
#endif
}
return (0);
}
int
{
return (-1);
}
return (0);
}
/*
* Add a mapping to the given proc handle. Resizes the array as appropriate and
* manages reference counts on the given file_info_t.
*
* The 'map_relocate' member is used to tell Psort_mappings() that the
* associated file_map pointer needs to be relocated after the mappings have
* been sorted. It is only set for the first mapping, and has no meaning
* outside these two functions.
*/
int
{
return (-1);
}
mp->map_relocate = 0;
}
}
return (0);
}
static int
map_sort(const void *a, const void *b)
{
return (-1);
return (1);
else
return (0);
}
/*
* Sort the current set of mappings. Should be called during target
* initialization after all calls to Padd_mapping() have been made.
*/
void
{
int i;
/*
* Update all the file_map pointers to refer to the new locations.
*/
for (i = 0; i < P->map_count; i++) {
if (mp->map_relocate)
mp->map_relocate = 0;
}
}