2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * DTrace Process Control
2N/A *
2N/A * This file provides a set of routines that permit libdtrace and its clients
2N/A * to create and grab process handles using libproc, and to share these handles
2N/A * between library mechanisms that need libproc access, such as ustack(), and
2N/A * client mechanisms that need libproc access, such as dtrace(1M) -c and -p.
2N/A * The library provides several mechanisms in the libproc control layer:
2N/A *
2N/A * Reference Counting: The library code and client code can independently grab
2N/A * the same process handles without interfering with one another. Only when
2N/A * the reference count drops to zero and the handle is not being cached (see
2N/A * below for more information on caching) will Prelease() be called on it.
2N/A *
2N/A * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
2N/A * the reference count drops to zero, the handle is not immediately released.
2N/A * Instead, libproc handles are maintained on dph_lrulist in order from most-
2N/A * recently accessed to least-recently accessed. Idle handles are maintained
2N/A * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
2N/A * to ustack() to avoid the overhead of releasing and re-grabbing processes.
2N/A *
2N/A * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
2N/A * or created by dt_proc_create(), a control thread is created to provide
2N/A * callbacks on process exit and symbol table caching on dlopen()s.
2N/A *
2N/A * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
2N/A * are provided to synchronize access to the libproc handle between libdtrace
2N/A * code and client code and the control thread's use of the ps_prochandle.
2N/A *
2N/A * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
2N/A * dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace
2N/A * calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for
2N/A * synchronization between libdtrace control threads and the client thread.
2N/A *
2N/A * The ps_prochandles themselves are maintained along with a dt_proc_t struct
2N/A * in a hash table indexed by PID. This provides basic locking and reference
2N/A * counting. The dt_proc_t is also maintained in LRU order on dph_lrulist.
2N/A * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
2N/A * the current limit on the number of actively cached entries.
2N/A *
2N/A * The control thread for a process establishes breakpoints at the rtld_db
2N/A * locations of interest, updates mappings and symbol tables at these points,
2N/A * and handles exec and fork (by always following the parent). The control
2N/A * thread automatically exits when the process dies or control is lost.
2N/A *
2N/A * A simple notification mechanism is provided for libdtrace clients using
2N/A * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If
2N/A * such an event occurs, the dt_proc_t itself is enqueued on a notification
2N/A * list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake
2N/A * up using this condition and will then call the client handler as necessary.
2N/A */
2N/A
2N/A#include <sys/wait.h>
2N/A#include <sys/lwp.h>
2N/A#include <strings.h>
2N/A#include <signal.h>
2N/A#include <assert.h>
2N/A#include <errno.h>
2N/A
2N/A#include <dt_proc.h>
2N/A#include <dt_pid.h>
2N/A#include <dt_impl.h>
2N/A
2N/A#define IS_SYS_EXEC(w) (w == SYS_execve)
2N/A#define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_forksys)
2N/A
2N/Astatic dt_bkpt_t *
2N/Adt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
2N/A{
2N/A struct ps_prochandle *P = dpr->dpr_proc;
2N/A dt_bkpt_t *dbp;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
2N/A dbp->dbp_func = func;
2N/A dbp->dbp_data = data;
2N/A dbp->dbp_addr = addr;
2N/A
2N/A if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
2N/A dbp->dbp_active = B_TRUE;
2N/A
2N/A dt_list_append(&dpr->dpr_bps, dbp);
2N/A }
2N/A
2N/A return (dbp);
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
2N/A{
2N/A int state = Pstate(dpr->dpr_proc);
2N/A dt_bkpt_t *dbp, *nbp;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
2N/A if (delbkpts && dbp->dbp_active &&
2N/A state != PS_LOST && state != PS_UNDEAD) {
2N/A (void) Pdelbkpt(dpr->dpr_proc,
2N/A dbp->dbp_addr, dbp->dbp_instr);
2N/A }
2N/A nbp = dt_list_next(dbp);
2N/A dt_list_delete(&dpr->dpr_bps, dbp);
2N/A dt_free(dpr->dpr_hdl, dbp);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
2N/A{
2N/A const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
2N/A dt_bkpt_t *dbp;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A for (dbp = dt_list_next(&dpr->dpr_bps);
2N/A dbp != NULL; dbp = dt_list_next(dbp)) {
2N/A if (psp->pr_reg[R_PC] == dbp->dbp_addr)
2N/A break;
2N/A }
2N/A
2N/A if (dbp == NULL) {
2N/A dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
2N/A (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
2N/A return;
2N/A }
2N/A
2N/A dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
2N/A (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
2N/A
2N/A dbp->dbp_func(dtp, dpr, dbp->dbp_data);
2N/A (void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_bpenable(dt_proc_t *dpr)
2N/A{
2N/A dt_bkpt_t *dbp;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A for (dbp = dt_list_next(&dpr->dpr_bps);
2N/A dbp != NULL; dbp = dt_list_next(dbp)) {
2N/A if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
2N/A dbp->dbp_addr, &dbp->dbp_instr) == 0)
2N/A dbp->dbp_active = B_TRUE;
2N/A }
2N/A
2N/A dt_dprintf("breakpoints enabled\n");
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_bpdisable(dt_proc_t *dpr)
2N/A{
2N/A dt_bkpt_t *dbp;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A for (dbp = dt_list_next(&dpr->dpr_bps);
2N/A dbp != NULL; dbp = dt_list_next(dbp)) {
2N/A if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
2N/A dbp->dbp_addr, dbp->dbp_instr) == 0)
2N/A dbp->dbp_active = B_FALSE;
2N/A }
2N/A
2N/A dt_dprintf("breakpoints disabled\n");
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
2N/A const char *msg)
2N/A{
2N/A dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
2N/A
2N/A if (dprn == NULL) {
2N/A dt_dprintf("failed to allocate notification for %d %s\n",
2N/A (int)dpr->dpr_pid, msg);
2N/A } else {
2N/A dprn->dprn_dpr = dpr;
2N/A if (msg == NULL)
2N/A dprn->dprn_errmsg[0] = '\0';
2N/A else
2N/A (void) strlcpy(dprn->dprn_errmsg, msg,
2N/A sizeof (dprn->dprn_errmsg));
2N/A
2N/A (void) pthread_mutex_lock(&dph->dph_lock);
2N/A
2N/A dprn->dprn_next = dph->dph_notify;
2N/A dph->dph_notify = dprn;
2N/A
2N/A (void) pthread_cond_broadcast(&dph->dph_cv);
2N/A (void) pthread_mutex_unlock(&dph->dph_lock);
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Check to see if the control thread was requested to stop when the victim
2N/A * process reached a particular event (why) rather than continuing the victim.
2N/A * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
2N/A * If 'why' is not set, this function returns immediately and does nothing.
2N/A */
2N/Astatic void
2N/Adt_proc_stop(dt_proc_t *dpr, uint8_t why)
2N/A{
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A assert(why != DT_PROC_STOP_IDLE);
2N/A
2N/A if (dpr->dpr_stop & why) {
2N/A dpr->dpr_stop |= DT_PROC_STOP_IDLE;
2N/A dpr->dpr_stop &= ~why;
2N/A
2N/A (void) pthread_cond_broadcast(&dpr->dpr_cv);
2N/A
2N/A /*
2N/A * We disable breakpoints while stopped to preserve the
2N/A * integrity of the program text for both our own disassembly
2N/A * and that of the kernel.
2N/A */
2N/A dt_proc_bpdisable(dpr);
2N/A
2N/A while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
2N/A (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
2N/A
2N/A dt_proc_bpenable(dpr);
2N/A }
2N/A}
2N/A
2N/A/*ARGSUSED*/
2N/Astatic void
2N/Adt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
2N/A{
2N/A dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
2N/A dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
2N/A{
2N/A rd_event_msg_t rdm;
2N/A rd_err_e err;
2N/A
2N/A if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
2N/A dt_dprintf("pid %d: failed to get %s event message: %s\n",
2N/A (int)dpr->dpr_pid, evname, rd_errstr(err));
2N/A return;
2N/A }
2N/A
2N/A dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
2N/A (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
2N/A
2N/A switch (rdm.type) {
2N/A case RD_DLACTIVITY:
2N/A if (rdm.u.state != RD_CONSISTENT)
2N/A break;
2N/A
2N/A Pupdate_syms(dpr->dpr_proc);
2N/A if (dt_pid_create_probes_module(dtp, dpr) != 0)
2N/A dt_proc_notify(dtp, dtp->dt_procs, dpr,
2N/A dpr->dpr_errmsg);
2N/A
2N/A break;
2N/A case RD_PREINIT:
2N/A Pupdate_syms(dpr->dpr_proc);
2N/A dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
2N/A break;
2N/A case RD_POSTINIT:
2N/A Pupdate_syms(dpr->dpr_proc);
2N/A dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
2N/A break;
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
2N/A{
2N/A rd_notify_t rdn;
2N/A rd_err_e err;
2N/A
2N/A if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
2N/A dt_dprintf("pid %d: failed to get event address for %s: %s\n",
2N/A (int)dpr->dpr_pid, evname, rd_errstr(err));
2N/A return;
2N/A }
2N/A
2N/A if (rdn.type != RD_NOTIFY_BPT) {
2N/A dt_dprintf("pid %d: event %s has unexpected type %d\n",
2N/A (int)dpr->dpr_pid, evname, rdn.type);
2N/A return;
2N/A }
2N/A
2N/A (void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
2N/A (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
2N/A}
2N/A
2N/A/*
2N/A * Common code for enabling events associated with the run-time linker after
2N/A * attaching to a process or after a victim process completes an exec(2).
2N/A */
2N/Astatic void
2N/Adt_proc_attach(dt_proc_t *dpr, int exec)
2N/A{
2N/A const pstatus_t *psp = Pstatus(dpr->dpr_proc);
2N/A rd_err_e err;
2N/A GElf_Sym sym;
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A
2N/A if (exec) {
2N/A if (psp->pr_lwp.pr_errno != 0)
2N/A return; /* exec failed: nothing needs to be done */
2N/A
2N/A dt_proc_bpdestroy(dpr, B_FALSE);
2N/A Preset_maps(dpr->dpr_proc);
2N/A }
2N/A
2N/A if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
2N/A (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
2N/A dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
2N/A dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
2N/A dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
2N/A } else {
2N/A dt_dprintf("pid %d: failed to enable rtld events: %s\n",
2N/A (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
2N/A "rtld_db agent initialization failed");
2N/A }
2N/A
2N/A Pupdate_maps(dpr->dpr_proc);
2N/A
2N/A if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
2N/A "a.out", "main", &sym, NULL) == 0) {
2N/A (void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
2N/A (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
2N/A } else {
2N/A dt_dprintf("pid %d: failed to find a.out`main: %s\n",
2N/A (int)dpr->dpr_pid, strerror(errno));
2N/A }
2N/A}
2N/A
2N/A/*
2N/A * Wait for a stopped process to be set running again by some other debugger.
2N/A * This is typically not required by /proc-based debuggers, since the usual
2N/A * model is that one debugger controls one victim. But DTrace, as usual, has
2N/A * its own needs: the stop() action assumes that prun(1) or some other tool
2N/A * will be applied to resume the victim process. This could be solved by
2N/A * adding a PCWRUN directive to /proc, but that seems like overkill unless
2N/A * other debuggers end up needing this functionality, so we implement a cheap
2N/A * equivalent to PCWRUN using the set of existing kernel mechanisms.
2N/A *
2N/A * Our intent is really not just to wait for the victim to run, but rather to
2N/A * wait for it to run and then stop again for a reason other than the current
2N/A * PR_REQUESTED stop. Since PCWSTOP/Pstopstatus() can be applied repeatedly
2N/A * to a stopped process and will return the same result without affecting the
2N/A * victim, we can just perform these operations repeatedly until Pstate()
2N/A * changes, the representative LWP ID changes, or the stop timestamp advances.
2N/A * dt_proc_control() will then rediscover the new state and continue as usual.
2N/A * When the process is still stopped in the same exact state, we sleep for a
2N/A * brief interval before waiting again so as not to spin consuming CPU cycles.
2N/A */
2N/Astatic void
2N/Adt_proc_waitrun(dt_proc_t *dpr)
2N/A{
2N/A struct ps_prochandle *P = dpr->dpr_proc;
2N/A const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
2N/A
2N/A int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
2N/A timestruc_t tstamp = psp->pr_tstamp;
2N/A lwpid_t lwpid = psp->pr_lwpid;
2N/A
2N/A const long wstop = PCWSTOP;
2N/A int pfd = Pctlfd(P);
2N/A
2N/A assert(MUTEX_HELD(&dpr->dpr_lock));
2N/A assert(psp->pr_flags & PR_STOPPED);
2N/A assert(Pstate(P) == PS_STOP);
2N/A
2N/A /*
2N/A * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
2N/A * so that if the libdtrace client is killed, the victim stays stopped.
2N/A * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
2N/A */
2N/A (void) Punsetflags(P, krflag);
2N/A Psync(P);
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A
2N/A while (!dpr->dpr_quit) {
2N/A if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
2N/A continue; /* check dpr_quit and continue waiting */
2N/A
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A (void) Pstopstatus(P, PCNULL, 0);
2N/A psp = &Pstatus(P)->pr_lwp;
2N/A
2N/A /*
2N/A * If we've reached a new state, found a new representative, or
2N/A * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
2N/A * original setting and then return with dpr_lock held.
2N/A */
2N/A if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
2N/A bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
2N/A (void) Psetflags(P, krflag);
2N/A Psync(P);
2N/A return;
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A (void) poll(NULL, 0, MILLISEC / 2);
2N/A }
2N/A
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A}
2N/A
2N/Atypedef struct dt_proc_control_data {
2N/A dtrace_hdl_t *dpcd_hdl; /* DTrace handle */
2N/A dt_proc_t *dpcd_proc; /* proccess to control */
2N/A} dt_proc_control_data_t;
2N/A
2N/A/*
2N/A * Main loop for all victim process control threads. We initialize all the
2N/A * appropriate /proc control mechanisms, and then enter a loop waiting for
2N/A * the process to stop on an event or die. We process any events by calling
2N/A * appropriate subroutines, and exit when the victim dies or we lose control.
2N/A *
2N/A * The control thread synchronizes the use of dpr_proc with other libdtrace
2N/A * threads using dpr_lock. We hold the lock for all of our operations except
2N/A * waiting while the process is running: this is accomplished by writing a
2N/A * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the
2N/A * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
2N/A */
2N/Astatic void *
2N/Adt_proc_control(void *arg)
2N/A{
2N/A dt_proc_control_data_t *datap = arg;
2N/A dtrace_hdl_t *dtp = datap->dpcd_hdl;
2N/A dt_proc_t *dpr = datap->dpcd_proc;
2N/A dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs;
2N/A struct ps_prochandle *P = dpr->dpr_proc;
2N/A
2N/A int pfd = Pctlfd(P);
2N/A int pid = dpr->dpr_pid;
2N/A
2N/A const long wstop = PCWSTOP;
2N/A int notify = B_FALSE;
2N/A
2N/A /*
2N/A * We disable the POSIX thread cancellation mechanism so that the
2N/A * client program using libdtrace can't accidentally cancel our thread.
2N/A * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
2N/A * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
2N/A */
2N/A (void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
2N/A
2N/A /*
2N/A * Set up the corresponding process for tracing by libdtrace. We want
2N/A * to be able to catch breakpoints and efficiently single-step over
2N/A * them, and we need to enable librtld_db to watch libdl activity.
2N/A */
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A
2N/A (void) Punsetflags(P, PR_ASYNC); /* require synchronous mode */
2N/A (void) Psetflags(P, PR_BPTADJ); /* always adjust eip on x86 */
2N/A (void) Punsetflags(P, PR_FORK); /* do not inherit on fork */
2N/A
2N/A (void) Pfault(P, FLTBPT, B_TRUE); /* always trace breakpoints */
2N/A (void) Pfault(P, FLTTRACE, B_TRUE); /* always trace single-step */
2N/A
2N/A /*
2N/A * We must trace exit from exec() system calls so that if the exec is
2N/A * successful, we can reset our breakpoints and re-initialize libproc.
2N/A */
2N/A (void) Psysexit(P, SYS_execve, B_TRUE);
2N/A
2N/A /*
2N/A * We must trace entry and exit for fork() system calls in order to
2N/A * disable our breakpoints temporarily during the fork. We do not set
2N/A * the PR_FORK flag, so if fork succeeds the child begins executing and
2N/A * does not inherit any other tracing behaviors or a control thread.
2N/A */
2N/A (void) Psysentry(P, SYS_vfork, B_TRUE);
2N/A (void) Psysexit(P, SYS_vfork, B_TRUE);
2N/A (void) Psysentry(P, SYS_forksys, B_TRUE);
2N/A (void) Psysexit(P, SYS_forksys, B_TRUE);
2N/A
2N/A Psync(P); /* enable all /proc changes */
2N/A dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */
2N/A
2N/A /*
2N/A * If PR_KLC is set, we created the process; otherwise we grabbed it.
2N/A * Check for an appropriate stop request and wait for dt_proc_continue.
2N/A */
2N/A if (Pstatus(P)->pr_flags & PR_KLC)
2N/A dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
2N/A else
2N/A dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
2N/A
2N/A if (Psetrun(P, 0, 0) == -1) {
2N/A dt_dprintf("pid %d: failed to set running: %s\n",
2N/A (int)dpr->dpr_pid, strerror(errno));
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A
2N/A /*
2N/A * Wait for the process corresponding to this control thread to stop,
2N/A * process the event, and then set it running again. We want to sleep
2N/A * with dpr_lock *unheld* so that other parts of libdtrace can use the
2N/A * ps_prochandle in the meantime (e.g. ustack()). To do this, we write
2N/A * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
2N/A * Once the process stops, we wake up, grab dpr_lock, and then call
2N/A * Pwait() (which will return immediately) and do our processing.
2N/A */
2N/A while (!dpr->dpr_quit) {
2N/A const lwpstatus_t *psp;
2N/A
2N/A if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
2N/A continue; /* check dpr_quit and continue waiting */
2N/A
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/Apwait_locked:
2N/A if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A continue; /* check dpr_quit and continue waiting */
2N/A }
2N/A
2N/A switch (Pstate(P)) {
2N/A case PS_STOP:
2N/A psp = &Pstatus(P)->pr_lwp;
2N/A
2N/A dt_dprintf("pid %d: proc stopped showing %d/%d\n",
2N/A pid, psp->pr_why, psp->pr_what);
2N/A
2N/A /*
2N/A * If the process stops showing PR_REQUESTED, then the
2N/A * DTrace stop() action was applied to it or another
2N/A * debugging utility (e.g. pstop(1)) asked it to stop.
2N/A * In either case, the user's intention is for the
2N/A * process to remain stopped until another external
2N/A * mechanism (e.g. prun(1)) is applied. So instead of
2N/A * setting the process running ourself, we wait for
2N/A * someone else to do so. Once that happens, we return
2N/A * to our normal loop waiting for an event of interest.
2N/A */
2N/A if (psp->pr_why == PR_REQUESTED) {
2N/A dt_proc_waitrun(dpr);
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A continue;
2N/A }
2N/A
2N/A /*
2N/A * If the process stops showing one of the events that
2N/A * we are tracing, perform the appropriate response.
2N/A * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
2N/A * PR_JOBCONTROL by design: if one of these conditions
2N/A * occurs, we will fall through to Psetrun() but the
2N/A * process will remain stopped in the kernel by the
2N/A * corresponding mechanism (e.g. job control stop).
2N/A */
2N/A if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
2N/A dt_proc_bpmatch(dtp, dpr);
2N/A else if (psp->pr_why == PR_SYSENTRY &&
2N/A IS_SYS_FORK(psp->pr_what))
2N/A dt_proc_bpdisable(dpr);
2N/A else if (psp->pr_why == PR_SYSEXIT &&
2N/A IS_SYS_FORK(psp->pr_what))
2N/A dt_proc_bpenable(dpr);
2N/A else if (psp->pr_why == PR_SYSEXIT &&
2N/A IS_SYS_EXEC(psp->pr_what))
2N/A dt_proc_attach(dpr, B_TRUE);
2N/A break;
2N/A
2N/A case PS_LOST:
2N/A if (Preopen(P) == 0)
2N/A goto pwait_locked;
2N/A
2N/A dt_dprintf("pid %d: proc lost: %s\n",
2N/A pid, strerror(errno));
2N/A
2N/A dpr->dpr_quit = B_TRUE;
2N/A notify = B_TRUE;
2N/A break;
2N/A
2N/A case PS_UNDEAD:
2N/A dt_dprintf("pid %d: proc died\n", pid);
2N/A dpr->dpr_quit = B_TRUE;
2N/A notify = B_TRUE;
2N/A break;
2N/A }
2N/A
2N/A if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
2N/A dt_dprintf("pid %d: failed to set running: %s\n",
2N/A (int)dpr->dpr_pid, strerror(errno));
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A }
2N/A
2N/A /*
2N/A * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
2N/A * the dt_proc_t structure on the dt_proc_hash_t notification list.
2N/A */
2N/A if (notify)
2N/A dt_proc_notify(dtp, dph, dpr, NULL);
2N/A
2N/A /*
2N/A * Destroy and remove any remaining breakpoints, set dpr_done and clear
2N/A * dpr_tid to indicate the control thread has exited, and notify any
2N/A * waiting thread in dt_proc_destroy() that we have succesfully exited.
2N/A */
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A
2N/A dt_proc_bpdestroy(dpr, B_TRUE);
2N/A dpr->dpr_done = B_TRUE;
2N/A dpr->dpr_tid = 0;
2N/A
2N/A (void) pthread_cond_broadcast(&dpr->dpr_cv);
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/A/*PRINTFLIKE3*/
2N/Astatic struct ps_prochandle *
2N/Adt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
2N/A{
2N/A va_list ap;
2N/A
2N/A va_start(ap, format);
2N/A dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
2N/A va_end(ap);
2N/A
2N/A if (dpr->dpr_proc != NULL)
2N/A Prelease(dpr->dpr_proc, 0);
2N/A
2N/A dt_free(dtp, dpr);
2N/A (void) dt_set_errno(dtp, EDT_COMPILER);
2N/A return (NULL);
2N/A}
2N/A
2N/Adt_proc_t *
2N/Adt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
2N/A{
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A pid_t pid = Pstatus(P)->pr_pid;
2N/A dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
2N/A
2N/A for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
2N/A if (dpr->dpr_pid == pid)
2N/A break;
2N/A else
2N/A dpp = &dpr->dpr_hash;
2N/A }
2N/A
2N/A assert(dpr != NULL);
2N/A assert(dpr->dpr_proc == P);
2N/A
2N/A if (remove)
2N/A *dpp = dpr->dpr_hash; /* remove from pid hash chain */
2N/A
2N/A return (dpr);
2N/A}
2N/A
2N/Astatic void
2N/Adt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A dt_proc_notify_t *npr, **npp;
2N/A int rflag;
2N/A
2N/A assert(dpr != NULL);
2N/A
2N/A /*
2N/A * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
2N/A * an external debugger and we were waiting in dt_proc_waitrun().
2N/A * Leave the process in this condition using PRELEASE_HANG.
2N/A */
2N/A if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
2N/A dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
2N/A rflag = PRELEASE_HANG;
2N/A } else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) {
2N/A dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);
2N/A rflag = PRELEASE_KILL; /* apply kill-on-last-close */
2N/A } else {
2N/A dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
2N/A rflag = 0; /* apply run-on-last-close */
2N/A }
2N/A
2N/A if (dpr->dpr_tid) {
2N/A /*
2N/A * Set the dpr_quit flag to tell the daemon thread to exit. We
2N/A * send it a SIGCANCEL to poke it out of PCWSTOP or any other
2N/A * long-term /proc system call. Our daemon threads have POSIX
2N/A * cancellation disabled, so EINTR will be the only effect. We
2N/A * then wait for dpr_done to indicate the thread has exited.
2N/A *
2N/A * We can't use pthread_kill() to send SIGCANCEL because the
2N/A * interface forbids it and we can't use pthread_cancel()
2N/A * because with cancellation disabled it won't actually
2N/A * send SIGCANCEL to the target thread, so we use _lwp_kill()
2N/A * to do the job. This is all built on evil knowledge of
2N/A * the details of the cancellation mechanism in libc.
2N/A */
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A dpr->dpr_quit = B_TRUE;
2N/A (void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
2N/A
2N/A /*
2N/A * If the process is currently idling in dt_proc_stop(), re-
2N/A * enable breakpoints and poke it into running again.
2N/A */
2N/A if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
2N/A dt_proc_bpenable(dpr);
2N/A dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
2N/A (void) pthread_cond_broadcast(&dpr->dpr_cv);
2N/A }
2N/A
2N/A while (!dpr->dpr_done)
2N/A (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A }
2N/A
2N/A /*
2N/A * Before we free the process structure, remove this dt_proc_t from the
2N/A * lookup hash, and then walk the dt_proc_hash_t's notification list
2N/A * and remove this dt_proc_t if it is enqueued.
2N/A */
2N/A (void) pthread_mutex_lock(&dph->dph_lock);
2N/A (void) dt_proc_lookup(dtp, P, B_TRUE);
2N/A npp = &dph->dph_notify;
2N/A
2N/A while ((npr = *npp) != NULL) {
2N/A if (npr->dprn_dpr == dpr) {
2N/A *npp = npr->dprn_next;
2N/A dt_free(dtp, npr);
2N/A } else {
2N/A npp = &npr->dprn_next;
2N/A }
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dph->dph_lock);
2N/A
2N/A /*
2N/A * Remove the dt_proc_list from the LRU list, release the underlying
2N/A * libproc handle, and free our dt_proc_t data structure.
2N/A */
2N/A if (dpr->dpr_cacheable) {
2N/A assert(dph->dph_lrucnt != 0);
2N/A dph->dph_lrucnt--;
2N/A }
2N/A
2N/A dt_list_delete(&dph->dph_lrulist, dpr);
2N/A Prelease(dpr->dpr_proc, rflag);
2N/A dt_free(dtp, dpr);
2N/A}
2N/A
2N/Astatic int
2N/Adt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
2N/A{
2N/A dt_proc_control_data_t data;
2N/A sigset_t nset, oset;
2N/A pthread_attr_t a;
2N/A int err;
2N/A
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
2N/A
2N/A (void) pthread_attr_init(&a);
2N/A (void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
2N/A
2N/A (void) sigfillset(&nset);
2N/A (void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */
2N/A (void) sigdelset(&nset, SIGCANCEL); /* see dt_proc_destroy() */
2N/A
2N/A data.dpcd_hdl = dtp;
2N/A data.dpcd_proc = dpr;
2N/A
2N/A (void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
2N/A err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
2N/A (void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
2N/A
2N/A /*
2N/A * If the control thread was created, then wait on dpr_cv for either
2N/A * dpr_done to be set (the victim died or the control thread failed)
2N/A * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
2N/A * stopped by /proc and the control thread is at the rendezvous event.
2N/A * On success, we return with the process and control thread stopped:
2N/A * the caller can then apply dt_proc_continue() to resume both.
2N/A */
2N/A if (err == 0) {
2N/A while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
2N/A (void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
2N/A
2N/A /*
2N/A * If dpr_done is set, the control thread aborted before it
2N/A * reached the rendezvous event. This is either due to PS_LOST
2N/A * or PS_UNDEAD (i.e. the process died). We try to provide a
2N/A * small amount of useful information to help figure it out.
2N/A */
2N/A if (dpr->dpr_done) {
2N/A const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
2N/A int stat = prp ? prp->pr_wstat : 0;
2N/A int pid = dpr->dpr_pid;
2N/A
2N/A if (Pstate(dpr->dpr_proc) == PS_LOST) {
2N/A (void) dt_proc_error(dpr->dpr_hdl, dpr,
2N/A "failed to control pid %d: process exec'd "
2N/A "set-id or unobservable program\n", pid);
2N/A } else if (WIFSIGNALED(stat)) {
2N/A (void) dt_proc_error(dpr->dpr_hdl, dpr,
2N/A "failed to control pid %d: process died "
2N/A "from signal %d\n", pid, WTERMSIG(stat));
2N/A } else {
2N/A (void) dt_proc_error(dpr->dpr_hdl, dpr,
2N/A "failed to control pid %d: process exited "
2N/A "with status %d\n", pid, WEXITSTATUS(stat));
2N/A }
2N/A
2N/A err = ESRCH; /* cause grab() or create() to fail */
2N/A }
2N/A } else {
2N/A (void) dt_proc_error(dpr->dpr_hdl, dpr,
2N/A "failed to create control thread for process-id %d: %s\n",
2N/A (int)dpr->dpr_pid, strerror(err));
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A (void) pthread_attr_destroy(&a);
2N/A
2N/A return (err);
2N/A}
2N/A
2N/Astruct ps_prochandle *
2N/Adt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
2N/A{
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A dt_proc_t *dpr;
2N/A int err;
2N/A
2N/A if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
2N/A return (NULL); /* errno is set for us */
2N/A
2N/A (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
2N/A (void) pthread_cond_init(&dpr->dpr_cv, NULL);
2N/A
2N/A if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) {
2N/A return (dt_proc_error(dtp, dpr,
2N/A "failed to execute %s: %s\n", file, Pcreate_error(err)));
2N/A }
2N/A
2N/A dpr->dpr_hdl = dtp;
2N/A dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
2N/A
2N/A (void) Punsetflags(dpr->dpr_proc, PR_RLC);
2N/A (void) Psetflags(dpr->dpr_proc, PR_KLC);
2N/A
2N/A if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
2N/A return (NULL); /* dt_proc_error() has been called for us */
2N/A
2N/A dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
2N/A dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
2N/A dt_list_prepend(&dph->dph_lrulist, dpr);
2N/A
2N/A dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
2N/A dpr->dpr_refs++;
2N/A
2N/A return (dpr->dpr_proc);
2N/A}
2N/A
2N/Astruct ps_prochandle *
2N/Adt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
2N/A{
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A uint_t h = pid & (dph->dph_hashlen - 1);
2N/A dt_proc_t *dpr, *opr;
2N/A int err;
2N/A
2N/A /*
2N/A * Search the hash table for the pid. If it is already grabbed or
2N/A * created, move the handle to the front of the lrulist, increment
2N/A * the reference count, and return the existing ps_prochandle.
2N/A */
2N/A for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
2N/A if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
2N/A /*
2N/A * If the cached handle was opened read-only and
2N/A * this request is for a writeable handle, mark
2N/A * the cached handle as stale and open a new handle.
2N/A * Since it's stale, unmark it as cacheable.
2N/A */
2N/A if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
2N/A dt_dprintf("upgrading pid %d\n", (int)pid);
2N/A dpr->dpr_stale = B_TRUE;
2N/A dpr->dpr_cacheable = B_FALSE;
2N/A dph->dph_lrucnt--;
2N/A break;
2N/A }
2N/A
2N/A dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
2N/A dt_list_delete(&dph->dph_lrulist, dpr);
2N/A dt_list_prepend(&dph->dph_lrulist, dpr);
2N/A dpr->dpr_refs++;
2N/A return (dpr->dpr_proc);
2N/A }
2N/A }
2N/A
2N/A if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
2N/A return (NULL); /* errno is set for us */
2N/A
2N/A (void) pthread_mutex_init(&dpr->dpr_lock, NULL);
2N/A (void) pthread_cond_init(&dpr->dpr_cv, NULL);
2N/A
2N/A if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
2N/A return (dt_proc_error(dtp, dpr,
2N/A "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
2N/A }
2N/A
2N/A dpr->dpr_hdl = dtp;
2N/A dpr->dpr_pid = pid;
2N/A
2N/A (void) Punsetflags(dpr->dpr_proc, PR_KLC);
2N/A (void) Psetflags(dpr->dpr_proc, PR_RLC);
2N/A
2N/A /*
2N/A * If we are attempting to grab the process without a monitor
2N/A * thread, then mark the process cacheable only if it's being
2N/A * grabbed read-only. If we're currently caching more process
2N/A * handles than dph_lrulim permits, attempt to find the
2N/A * least-recently-used handle that is currently unreferenced and
2N/A * release it from the cache. Otherwise we are grabbing the process
2N/A * for control: create a control thread for this process and store
2N/A * its ID in dpr->dpr_tid.
2N/A */
2N/A if (nomonitor || (flags & PGRAB_RDONLY)) {
2N/A if (dph->dph_lrucnt >= dph->dph_lrulim) {
2N/A for (opr = dt_list_prev(&dph->dph_lrulist);
2N/A opr != NULL; opr = dt_list_prev(opr)) {
2N/A if (opr->dpr_cacheable && opr->dpr_refs == 0) {
2N/A dt_proc_destroy(dtp, opr->dpr_proc);
2N/A break;
2N/A }
2N/A }
2N/A }
2N/A
2N/A if (flags & PGRAB_RDONLY) {
2N/A dpr->dpr_cacheable = B_TRUE;
2N/A dpr->dpr_rdonly = B_TRUE;
2N/A dph->dph_lrucnt++;
2N/A }
2N/A
2N/A } else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
2N/A return (NULL); /* dt_proc_error() has been called for us */
2N/A
2N/A dpr->dpr_hash = dph->dph_hash[h];
2N/A dph->dph_hash[h] = dpr;
2N/A dt_list_prepend(&dph->dph_lrulist, dpr);
2N/A
2N/A dt_dprintf("grabbed pid %d\n", (int)pid);
2N/A dpr->dpr_refs++;
2N/A
2N/A return (dpr->dpr_proc);
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A
2N/A assert(dpr != NULL);
2N/A assert(dpr->dpr_refs != 0);
2N/A
2N/A if (--dpr->dpr_refs == 0 &&
2N/A (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
2N/A dt_proc_destroy(dtp, P);
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
2N/A
2N/A (void) pthread_mutex_lock(&dpr->dpr_lock);
2N/A
2N/A if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
2N/A dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
2N/A (void) pthread_cond_broadcast(&dpr->dpr_cv);
2N/A }
2N/A
2N/A (void) pthread_mutex_unlock(&dpr->dpr_lock);
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
2N/A int err = pthread_mutex_lock(&dpr->dpr_lock);
2N/A assert(err == 0); /* check for recursion */
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
2N/A int err = pthread_mutex_unlock(&dpr->dpr_lock);
2N/A assert(err == 0); /* check for unheld lock */
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_hash_create(dtrace_hdl_t *dtp)
2N/A{
2N/A if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
2N/A sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) {
2N/A
2N/A (void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
2N/A (void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
2N/A
2N/A dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
2N/A dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Adt_proc_hash_destroy(dtrace_hdl_t *dtp)
2N/A{
2N/A dt_proc_hash_t *dph = dtp->dt_procs;
2N/A dt_proc_t *dpr;
2N/A
2N/A while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
2N/A dt_proc_destroy(dtp, dpr->dpr_proc);
2N/A
2N/A dtp->dt_procs = NULL;
2N/A dt_free(dtp, dph);
2N/A}
2N/A
2N/Astruct ps_prochandle *
2N/Adtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
2N/A{
2N/A dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
2N/A struct ps_prochandle *P = dt_proc_create(dtp, file, argv);
2N/A
2N/A if (P != NULL && idp != NULL && idp->di_id == 0)
2N/A idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
2N/A
2N/A return (P);
2N/A}
2N/A
2N/Astruct ps_prochandle *
2N/Adtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
2N/A{
2N/A dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
2N/A struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
2N/A
2N/A if (P != NULL && idp != NULL && idp->di_id == 0)
2N/A idp->di_id = pid; /* $target = grabbed pid */
2N/A
2N/A return (P);
2N/A}
2N/A
2N/Avoid
2N/Adtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_release(dtp, P);
2N/A}
2N/A
2N/Avoid
2N/Adtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
2N/A{
2N/A dt_proc_continue(dtp, P);
2N/A}