Path: blob/main/cddl/contrib/opensolaris/lib/libdtrace/common/dt_proc.c
39562 views
/*1* CDDL HEADER START2*3* The contents of this file are subject to the terms of the4* Common Development and Distribution License (the "License").5* You may not use this file except in compliance with the License.6*7* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE8* or http://www.opensolaris.org/os/licensing.9* See the License for the specific language governing permissions10* and limitations under the License.11*12* When distributing Covered Code, include this CDDL HEADER in each13* file and include the License file at usr/src/OPENSOLARIS.LICENSE.14* If applicable, add the following below this CDDL HEADER, with the15* fields enclosed by brackets "[]" replaced with your own identifying16* information: Portions Copyright [yyyy] [name of copyright owner]17*18* CDDL HEADER END19*/2021/*22* Copyright 2010 Sun Microsystems, Inc. All rights reserved.23* Use is subject to license terms.24*/2526/*27* Copyright (c) 2012 by Delphix. All rights reserved.28*/2930/*31* DTrace Process Control32*33* This file provides a set of routines that permit libdtrace and its clients34* to create and grab process handles using libproc, and to share these handles35* between library mechanisms that need libproc access, such as ustack(), and36* client mechanisms that need libproc access, such as dtrace(1M) -c and -p.37* The library provides several mechanisms in the libproc control layer:38*39* Reference Counting: The library code and client code can independently grab40* the same process handles without interfering with one another. Only when41* the reference count drops to zero and the handle is not being cached (see42* below for more information on caching) will Prelease() be called on it.43*44* Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and45* the reference count drops to zero, the handle is not immediately released.46* Instead, libproc handles are maintained on dph_lrulist in order from most-47* recently accessed to least-recently accessed. Idle handles are maintained48* until a pre-defined LRU cache limit is exceeded, permitting repeated calls49* to ustack() to avoid the overhead of releasing and re-grabbing processes.50*51* Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)52* or created by dt_proc_create(), a control thread is created to provide53* callbacks on process exit and symbol table caching on dlopen()s.54*55* MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()56* are provided to synchronize access to the libproc handle between libdtrace57* code and client code and the control thread's use of the ps_prochandle.58*59* NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the60* dtrace_proc_grab/dtrace_proc_create mechanisms. Like all exported libdtrace61* calls, these are assumed to be MT-Unsafe. MT-Safety is ONLY provided for62* synchronization between libdtrace control threads and the client thread.63*64* The ps_prochandles themselves are maintained along with a dt_proc_t struct65* in a hash table indexed by PID. This provides basic locking and reference66* counting. The dt_proc_t is also maintained in LRU order on dph_lrulist.67* The dph_lrucnt and dph_lrulim count the number of cacheable processes and68* the current limit on the number of actively cached entries.69*70* The control thread for a process establishes breakpoints at the rtld_db71* locations of interest, updates mappings and symbol tables at these points,72* and handles exec and fork (by always following the parent). The control73* thread automatically exits when the process dies or control is lost.74*75* A simple notification mechanism is provided for libdtrace clients using76* dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events. If77* such an event occurs, the dt_proc_t itself is enqueued on a notification78* list and the control thread broadcasts to dph_cv. dtrace_sleep() will wake79* up using this condition and will then call the client handler as necessary.80*/8182#include <sys/syscall.h>83#include <sys/wait.h>84#include <strings.h>85#include <signal.h>86#include <assert.h>87#include <errno.h>8889#include <dt_proc.h>90#include <dt_pid.h>91#include <dt_impl.h>9293#include <libproc_compat.h>9495#define IS_SYS_EXEC(w) (w == SYS_execve)96#define IS_SYS_FORK(w) (w == SYS_vfork || w == SYS_fork)9798static dt_bkpt_t *99dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)100{101struct ps_prochandle *P = dpr->dpr_proc;102dt_bkpt_t *dbp;103104assert(DT_MUTEX_HELD(&dpr->dpr_lock));105106if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {107dbp->dbp_func = func;108dbp->dbp_data = data;109dbp->dbp_addr = addr;110111if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)112dbp->dbp_active = B_TRUE;113114dt_list_append(&dpr->dpr_bps, dbp);115}116117return (dbp);118}119120static void121dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)122{123int state = Pstate(dpr->dpr_proc);124dt_bkpt_t *dbp, *nbp;125126assert(DT_MUTEX_HELD(&dpr->dpr_lock));127128for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {129if (delbkpts && dbp->dbp_active &&130state != PS_LOST && state != PS_UNDEAD) {131(void) Pdelbkpt(dpr->dpr_proc,132dbp->dbp_addr, dbp->dbp_instr);133}134nbp = dt_list_next(dbp);135dt_list_delete(&dpr->dpr_bps, dbp);136dt_free(dpr->dpr_hdl, dbp);137}138}139140static void141dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)142{143unsigned long pc;144dt_bkpt_t *dbp;145146assert(DT_MUTEX_HELD(&dpr->dpr_lock));147148proc_regget(dpr->dpr_proc, REG_PC, &pc);149proc_bkptregadj(&pc);150151for (dbp = dt_list_next(&dpr->dpr_bps);152dbp != NULL; dbp = dt_list_next(dbp)) {153if (pc == dbp->dbp_addr)154break;155}156157if (dbp == NULL) {158dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",159(int)dpr->dpr_pid, pc);160return;161}162163dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",164(int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);165166dbp->dbp_func(dtp, dpr, dbp->dbp_data);167(void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);168}169170static void171dt_proc_bpenable(dt_proc_t *dpr)172{173dt_bkpt_t *dbp;174175assert(DT_MUTEX_HELD(&dpr->dpr_lock));176177for (dbp = dt_list_next(&dpr->dpr_bps);178dbp != NULL; dbp = dt_list_next(dbp)) {179if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,180dbp->dbp_addr, &dbp->dbp_instr) == 0)181dbp->dbp_active = B_TRUE;182}183184dt_dprintf("breakpoints enabled\n");185}186187static void188dt_proc_bpdisable(dt_proc_t *dpr)189{190dt_bkpt_t *dbp;191192assert(DT_MUTEX_HELD(&dpr->dpr_lock));193194for (dbp = dt_list_next(&dpr->dpr_bps);195dbp != NULL; dbp = dt_list_next(dbp)) {196if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,197dbp->dbp_addr, dbp->dbp_instr) == 0)198dbp->dbp_active = B_FALSE;199}200201dt_dprintf("breakpoints disabled\n");202}203204static void205dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,206const char *msg)207{208dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));209210if (dprn == NULL) {211dt_dprintf("failed to allocate notification for %d %s\n",212(int)dpr->dpr_pid, msg);213} else {214dprn->dprn_dpr = dpr;215if (msg == NULL)216dprn->dprn_errmsg[0] = '\0';217else218(void) strlcpy(dprn->dprn_errmsg, msg,219sizeof (dprn->dprn_errmsg));220221(void) pthread_mutex_lock(&dph->dph_lock);222223dprn->dprn_next = dph->dph_notify;224dph->dph_notify = dprn;225226(void) pthread_cond_broadcast(&dph->dph_cv);227(void) pthread_mutex_unlock(&dph->dph_lock);228}229}230231/*232* Check to see if the control thread was requested to stop when the victim233* process reached a particular event (why) rather than continuing the victim.234* If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().235* If 'why' is not set, this function returns immediately and does nothing.236*/237static void238dt_proc_stop(dt_proc_t *dpr, uint8_t why)239{240assert(DT_MUTEX_HELD(&dpr->dpr_lock));241assert(why != DT_PROC_STOP_IDLE);242243if (dpr->dpr_stop & why) {244dpr->dpr_stop |= DT_PROC_STOP_IDLE;245dpr->dpr_stop &= ~why;246247(void) pthread_cond_broadcast(&dpr->dpr_cv);248249/*250* We disable breakpoints while stopped to preserve the251* integrity of the program text for both our own disassembly252* and that of the kernel.253*/254dt_proc_bpdisable(dpr);255256while (dpr->dpr_stop & DT_PROC_STOP_IDLE)257(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);258259dt_proc_bpenable(dpr);260}261}262263/*ARGSUSED*/264static void265dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)266{267dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);268dt_proc_stop(dpr, DT_PROC_STOP_MAIN);269}270271static void272dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)273{274rd_event_msg_t rdm;275rd_err_e err;276277if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {278dt_dprintf("pid %d: failed to get %s event message: %s\n",279(int)dpr->dpr_pid, evname, rd_errstr(err));280return;281}282283dt_dprintf("pid %d: rtld event %s type=%d state %d\n",284(int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);285286switch (rdm.type) {287case RD_DLACTIVITY:288if (rdm.u.state != RD_CONSISTENT)289break;290291Pupdate_syms(dpr->dpr_proc);292if (dt_pid_create_probes_module(dtp, dpr) != 0)293dt_proc_notify(dtp, dtp->dt_procs, dpr,294dpr->dpr_errmsg);295296break;297case RD_PREINIT:298Pupdate_syms(dpr->dpr_proc);299dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);300break;301case RD_POSTINIT:302Pupdate_syms(dpr->dpr_proc);303dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);304break;305}306}307308static void309dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)310{311rd_notify_t rdn;312rd_err_e err;313314if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {315dt_dprintf("pid %d: failed to get event address for %s: %s\n",316(int)dpr->dpr_pid, evname, rd_errstr(err));317return;318}319320if (rdn.type != RD_NOTIFY_BPT) {321dt_dprintf("pid %d: event %s has unexpected type %d\n",322(int)dpr->dpr_pid, evname, rdn.type);323return;324}325326(void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,327/* XXX ugly */328(dt_bkpt_f *)dt_proc_rdevent, __DECONST(void *, evname));329}330331/*332* Common code for enabling events associated with the run-time linker after333* attaching to a process or after a victim process completes an exec(2).334*/335static void336dt_proc_attach(dt_proc_t *dpr, int exec)337{338rd_err_e err;339GElf_Sym sym;340341assert(DT_MUTEX_HELD(&dpr->dpr_lock));342343if (exec) {344345dt_proc_bpdestroy(dpr, B_FALSE);346}347if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&348(err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {349dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");350} else {351dt_dprintf("pid %d: failed to enable rtld events: %s\n",352(int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :353"rtld_db agent initialization failed");354}355356Pupdate_maps(dpr->dpr_proc);357358if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,359"a.out", "main", &sym, NULL) == 0) {360(void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,361(dt_bkpt_f *)dt_proc_bpmain, "a.out`main");362} else {363dt_dprintf("pid %d: failed to find a.out`main: %s\n",364(int)dpr->dpr_pid, strerror(errno));365}366}367368typedef struct dt_proc_control_data {369dtrace_hdl_t *dpcd_hdl; /* DTrace handle */370dt_proc_t *dpcd_proc; /* proccess to control */371} dt_proc_control_data_t;372373/*374* Main loop for all victim process control threads. We initialize all the375* appropriate /proc control mechanisms, and then enter a loop waiting for376* the process to stop on an event or die. We process any events by calling377* appropriate subroutines, and exit when the victim dies or we lose control.378*379* The control thread synchronizes the use of dpr_proc with other libdtrace380* threads using dpr_lock. We hold the lock for all of our operations except381* waiting while the process is running: this is accomplished by writing a382* PCWSTOP directive directly to the underlying /proc/<pid>/ctl file. If the383* libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.384*/385static void *386dt_proc_control(void *arg)387{388dt_proc_control_data_t *datap = arg;389dtrace_hdl_t *dtp = datap->dpcd_hdl;390dt_proc_t *dpr = datap->dpcd_proc;391dt_proc_hash_t *dph = dtp->dt_procs;392struct ps_prochandle *P = dpr->dpr_proc;393int pid = dpr->dpr_pid;394int notify = B_FALSE;395396/*397* We disable the POSIX thread cancellation mechanism so that the398* client program using libdtrace can't accidentally cancel our thread.399* dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out400* of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.401*/402(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);403404/*405* Set up the corresponding process for tracing by libdtrace. We want406* to be able to catch breakpoints and efficiently single-step over407* them, and we need to enable librtld_db to watch libdl activity.408*/409(void) pthread_mutex_lock(&dpr->dpr_lock);410411dt_proc_attach(dpr, B_FALSE); /* enable rtld breakpoints */412413/*414* If DT_CLOSE_KILL is set, we created the process; otherwise we415* grabbed it. Check for an appropriate stop request and wait for416* dt_proc_continue.417*/418if (dpr->dpr_close == DT_CLOSE_KILL)419dt_proc_stop(dpr, DT_PROC_STOP_CREATE);420else421dt_proc_stop(dpr, DT_PROC_STOP_GRAB);422423if (Psetrun(P, 0, 0) == -1) {424dt_dprintf("pid %d: failed to set running: %s\n",425(int)dpr->dpr_pid, strerror(errno));426}427428(void) pthread_mutex_unlock(&dpr->dpr_lock);429430/*431* Wait for the process corresponding to this control thread to stop,432* process the event, and then set it running again. We want to sleep433* with dpr_lock *unheld* so that other parts of libdtrace can use the434* ps_prochandle in the meantime (e.g. ustack()). To do this, we write435* a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.436* Once the process stops, we wake up, grab dpr_lock, and then call437* Pwait() (which will return immediately) and do our processing.438*/439while (!dpr->dpr_quit) {440const lwpstatus_t *psp;441442/* Wait for the process to report status. */443proc_wstatus(P);444if (errno == EINTR)445continue; /* check dpr_quit and continue waiting */446447(void) pthread_mutex_lock(&dpr->dpr_lock);448449switch (Pstate(P)) {450case PS_STOP:451psp = proc_getlwpstatus(P);452453dt_dprintf("pid %d: proc stopped showing %d/%d\n",454pid, psp->pr_why, psp->pr_what);455456/*457* If the process stops showing one of the events that458* we are tracing, perform the appropriate response.459* Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and460* PR_JOBCONTROL by design: if one of these conditions461* occurs, we will fall through to Psetrun() but the462* process will remain stopped in the kernel by the463* corresponding mechanism (e.g. job control stop).464*/465if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)466dt_proc_bpmatch(dtp, dpr);467else if (psp->pr_why == PR_SYSENTRY &&468IS_SYS_FORK(psp->pr_what))469dt_proc_bpdisable(dpr);470else if (psp->pr_why == PR_SYSEXIT &&471IS_SYS_FORK(psp->pr_what))472dt_proc_bpenable(dpr);473else if (psp->pr_why == PR_SYSEXIT &&474IS_SYS_EXEC(psp->pr_what))475dt_proc_attach(dpr, B_TRUE);476break;477478case PS_LOST:479dt_dprintf("pid %d: proc lost: %s\n",480pid, strerror(errno));481482dpr->dpr_quit = B_TRUE;483notify = B_TRUE;484break;485486case PS_UNDEAD:487dt_dprintf("pid %d: proc died\n", pid);488dpr->dpr_quit = B_TRUE;489notify = B_TRUE;490break;491}492493if (Pstate(P) != PS_UNDEAD) {494if (dpr->dpr_quit && dpr->dpr_close == DT_CLOSE_KILL) {495/*496* We're about to kill the child, so don't497* bother resuming it. In some cases, such as498* an initialization error, we shouldn't have499* started it in the first place, so letting it500* run could be harmful.501*/502} else if (Psetrun(P, 0, 0) == -1) {503dt_dprintf("pid %d: failed to set running: "504"%s\n", (int)dpr->dpr_pid, strerror(errno));505}506}507508(void) pthread_mutex_unlock(&dpr->dpr_lock);509}510511/*512* If the control thread detected PS_UNDEAD or PS_LOST, then enqueue513* the dt_proc_t structure on the dt_proc_hash_t notification list.514*/515if (notify)516dt_proc_notify(dtp, dph, dpr, NULL);517518/*519* Destroy and remove any remaining breakpoints, set dpr_done and clear520* dpr_tid to indicate the control thread has exited, and notify any521* waiting thread in dt_proc_destroy() that we have succesfully exited.522*/523(void) pthread_mutex_lock(&dpr->dpr_lock);524525dt_proc_bpdestroy(dpr, B_TRUE);526dpr->dpr_done = B_TRUE;527dpr->dpr_tid = 0;528529(void) pthread_cond_broadcast(&dpr->dpr_cv);530(void) pthread_mutex_unlock(&dpr->dpr_lock);531532return (NULL);533}534535/*PRINTFLIKE3*/536static struct ps_prochandle *537dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)538{539va_list ap;540541va_start(ap, format);542dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);543va_end(ap);544545if (dpr->dpr_proc != NULL)546Prelease(dpr->dpr_proc, 0);547548dt_free(dtp, dpr);549(void) dt_set_errno(dtp, EDT_COMPILER);550return (NULL);551}552553dt_proc_t *554dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)555{556dt_proc_hash_t *dph = dtp->dt_procs;557pid_t pid = proc_getpid(P);558dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];559560for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {561if (dpr->dpr_pid == pid)562break;563else564dpp = &dpr->dpr_hash;565}566567assert(dpr != NULL);568assert(dpr->dpr_proc == P);569570if (remove)571*dpp = dpr->dpr_hash; /* remove from pid hash chain */572573return (dpr);574}575576static void577dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)578{579dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);580dt_proc_hash_t *dph = dtp->dt_procs;581dt_proc_notify_t *npr, **npp;582int rflag;583584assert(dpr != NULL);585586switch (dpr->dpr_close) {587case DT_CLOSE_KILL:588dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);589rflag = PRELEASE_KILL;590break;591case DT_CLOSE_RUN:592dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);593rflag = 0;594break;595}596597if (dpr->dpr_tid) {598/*599* Set the dpr_quit flag to tell the daemon thread to exit. We600* send it a SIGCANCEL to poke it out of PCWSTOP or any other601* long-term /proc system call. Our daemon threads have POSIX602* cancellation disabled, so EINTR will be the only effect. We603* then wait for dpr_done to indicate the thread has exited.604*605* We can't use pthread_kill() to send SIGCANCEL because the606* interface forbids it and we can't use pthread_cancel()607* because with cancellation disabled it won't actually608* send SIGCANCEL to the target thread, so we use _lwp_kill()609* to do the job. This is all built on evil knowledge of610* the details of the cancellation mechanism in libc.611*/612(void) pthread_mutex_lock(&dpr->dpr_lock);613dpr->dpr_quit = B_TRUE;614pthread_kill(dpr->dpr_tid, SIGTHR);615616/*617* If the process is currently idling in dt_proc_stop(), re-618* enable breakpoints and poke it into running again.619*/620if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {621dt_proc_bpenable(dpr);622dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;623(void) pthread_cond_broadcast(&dpr->dpr_cv);624}625626while (!dpr->dpr_done)627(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);628629(void) pthread_mutex_unlock(&dpr->dpr_lock);630}631632/*633* Before we free the process structure, remove this dt_proc_t from the634* lookup hash, and then walk the dt_proc_hash_t's notification list635* and remove this dt_proc_t if it is enqueued.636*/637(void) pthread_mutex_lock(&dph->dph_lock);638(void) dt_proc_lookup(dtp, P, B_TRUE);639npp = &dph->dph_notify;640641while ((npr = *npp) != NULL) {642if (npr->dprn_dpr == dpr) {643*npp = npr->dprn_next;644dt_free(dtp, npr);645} else {646npp = &npr->dprn_next;647}648}649650(void) pthread_mutex_unlock(&dph->dph_lock);651652/*653* Remove the dt_proc_list from the LRU list, release the underlying654* libproc handle, and free our dt_proc_t data structure.655*/656if (dpr->dpr_cacheable) {657assert(dph->dph_lrucnt != 0);658dph->dph_lrucnt--;659}660661dt_list_delete(&dph->dph_lrulist, dpr);662Prelease(dpr->dpr_proc, rflag);663dt_free(dtp, dpr);664}665666static int667dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)668{669dt_proc_control_data_t data;670sigset_t nset, oset;671pthread_attr_t a;672int err;673674(void) pthread_mutex_lock(&dpr->dpr_lock);675dpr->dpr_stop |= stop; /* set bit for initial rendezvous */676677(void) pthread_attr_init(&a);678(void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);679680(void) sigfillset(&nset);681(void) sigdelset(&nset, SIGABRT); /* unblocked for assert() */682(void) sigdelset(&nset, SIGUSR1); /* see dt_proc_destroy() */683684data.dpcd_hdl = dtp;685data.dpcd_proc = dpr;686687(void) pthread_sigmask(SIG_SETMASK, &nset, &oset);688err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);689(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);690691/*692* If the control thread was created, then wait on dpr_cv for either693* dpr_done to be set (the victim died or the control thread failed)694* or DT_PROC_STOP_IDLE to be set, indicating that the victim is now695* stopped by /proc and the control thread is at the rendezvous event.696* On success, we return with the process and control thread stopped:697* the caller can then apply dt_proc_continue() to resume both.698*/699if (err == 0) {700while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))701(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);702703/*704* If dpr_done is set, the control thread aborted before it705* reached the rendezvous event. This is either due to PS_LOST706* or PS_UNDEAD (i.e. the process died). We try to provide a707* small amount of useful information to help figure it out.708*/709if (dpr->dpr_done) {710int stat = proc_getwstat(dpr->dpr_proc);711int pid = proc_getpid(dpr->dpr_proc);712if (proc_state(dpr->dpr_proc) == PS_LOST) {713(void) dt_proc_error(dpr->dpr_hdl, dpr,714"failed to control pid %d: process exec'd "715"set-id or unobservable program\n", pid);716} else if (WIFSIGNALED(stat)) {717(void) dt_proc_error(dpr->dpr_hdl, dpr,718"failed to control pid %d: process died "719"from signal %d\n", pid, WTERMSIG(stat));720} else {721(void) dt_proc_error(dpr->dpr_hdl, dpr,722"failed to control pid %d: process exited "723"with status %d\n", pid, WEXITSTATUS(stat));724}725726err = ESRCH; /* cause grab() or create() to fail */727}728} else {729(void) dt_proc_error(dpr->dpr_hdl, dpr,730"failed to create control thread for process-id %d: %s\n",731(int)dpr->dpr_pid, strerror(err));732}733734if (err == 0)735(void) pthread_mutex_unlock(&dpr->dpr_lock);736(void) pthread_attr_destroy(&a);737738return (err);739}740741struct ps_prochandle *742dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,743proc_child_func *pcf, void *child_arg)744{745dt_proc_hash_t *dph = dtp->dt_procs;746dt_proc_t *dpr;747int err;748749if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)750return (NULL); /* errno is set for us */751752(void) pthread_mutex_init(&dpr->dpr_lock, NULL);753(void) pthread_cond_init(&dpr->dpr_cv, NULL);754755if ((err = proc_create(file, argv, dtp->dt_proc_env, pcf, child_arg,756&dpr->dpr_proc)) != 0) {757return (dt_proc_error(dtp, dpr,758"failed to execute %s: %s\n", file, Pcreate_error(err)));759}760761dpr->dpr_hdl = dtp;762dpr->dpr_pid = proc_getpid(dpr->dpr_proc);763dpr->dpr_close = DT_CLOSE_KILL;764765if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)766return (NULL); /* dt_proc_error() has been called for us */767768dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];769dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;770dt_list_prepend(&dph->dph_lrulist, dpr);771772dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);773dpr->dpr_refs++;774775return (dpr->dpr_proc);776}777778struct ps_prochandle *779dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)780{781dt_proc_hash_t *dph = dtp->dt_procs;782uint_t h = pid & (dph->dph_hashlen - 1);783dt_proc_t *dpr, *opr;784int err;785786/*787* Search the hash table for the pid. If it is already grabbed or788* created, move the handle to the front of the lrulist, increment789* the reference count, and return the existing ps_prochandle.790*/791for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {792if (dpr->dpr_pid == pid && !dpr->dpr_stale) {793/*794* If the cached handle was opened read-only and795* this request is for a writeable handle, mark796* the cached handle as stale and open a new handle.797* Since it's stale, unmark it as cacheable.798*/799if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {800dt_dprintf("upgrading pid %d\n", (int)pid);801dpr->dpr_stale = B_TRUE;802dpr->dpr_cacheable = B_FALSE;803dph->dph_lrucnt--;804break;805}806807dt_dprintf("grabbed pid %d (cached)\n", (int)pid);808dt_list_delete(&dph->dph_lrulist, dpr);809dt_list_prepend(&dph->dph_lrulist, dpr);810dpr->dpr_refs++;811return (dpr->dpr_proc);812}813}814815if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)816return (NULL); /* errno is set for us */817818(void) pthread_mutex_init(&dpr->dpr_lock, NULL);819(void) pthread_cond_init(&dpr->dpr_cv, NULL);820821if ((err = proc_attach(pid, flags, &dpr->dpr_proc)) != 0) {822return (dt_proc_error(dtp, dpr,823"failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));824}825826dpr->dpr_hdl = dtp;827dpr->dpr_pid = pid;828dpr->dpr_close = DT_CLOSE_RUN;829830/*831* If we are attempting to grab the process without a monitor832* thread, then mark the process cacheable only if it's being833* grabbed read-only. If we're currently caching more process834* handles than dph_lrulim permits, attempt to find the835* least-recently-used handle that is currently unreferenced and836* release it from the cache. Otherwise we are grabbing the process837* for control: create a control thread for this process and store838* its ID in dpr->dpr_tid.839*/840if (nomonitor || (flags & PGRAB_RDONLY)) {841if (dph->dph_lrucnt >= dph->dph_lrulim) {842for (opr = dt_list_prev(&dph->dph_lrulist);843opr != NULL; opr = dt_list_prev(opr)) {844if (opr->dpr_cacheable && opr->dpr_refs == 0) {845dt_proc_destroy(dtp, opr->dpr_proc);846break;847}848}849}850851if (flags & PGRAB_RDONLY) {852dpr->dpr_cacheable = B_TRUE;853dpr->dpr_rdonly = B_TRUE;854dph->dph_lrucnt++;855}856857} else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)858return (NULL); /* dt_proc_error() has been called for us */859860dpr->dpr_hash = dph->dph_hash[h];861dph->dph_hash[h] = dpr;862dt_list_prepend(&dph->dph_lrulist, dpr);863864dt_dprintf("grabbed pid %d\n", (int)pid);865dpr->dpr_refs++;866867return (dpr->dpr_proc);868}869870void871dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)872{873dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);874dt_proc_hash_t *dph = dtp->dt_procs;875876assert(dpr != NULL);877assert(dpr->dpr_refs != 0);878879if (--dpr->dpr_refs == 0 &&880(!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))881dt_proc_destroy(dtp, P);882}883884void885dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)886{887dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);888889(void) pthread_mutex_lock(&dpr->dpr_lock);890891if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {892dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;893(void) pthread_cond_broadcast(&dpr->dpr_cv);894}895896(void) pthread_mutex_unlock(&dpr->dpr_lock);897}898899void900dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)901{902dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);903int err = pthread_mutex_lock(&dpr->dpr_lock);904assert(err == 0); /* check for recursion */905}906907void908dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)909{910dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);911int err = pthread_mutex_unlock(&dpr->dpr_lock);912assert(err == 0); /* check for unheld lock */913}914915void916dt_proc_init(dtrace_hdl_t *dtp)917{918extern char **environ;919static char *envdef[] = {920"LD_NOLAZYLOAD=1", /* linker lazy loading hides funcs */921NULL922};923char **p;924int i;925926if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +927sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) == NULL)928return;929930(void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);931(void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);932933dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;934dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;935936/*937* Count how big our environment needs to be.938*/939for (i = 1, p = environ; *p != NULL; i++, p++)940continue;941for (p = envdef; *p != NULL; i++, p++)942continue;943944if ((dtp->dt_proc_env = dt_zalloc(dtp, sizeof (char *) * i)) == NULL)945return;946947for (i = 0, p = environ; *p != NULL; i++, p++) {948if ((dtp->dt_proc_env[i] = strdup(*p)) == NULL)949goto err;950}951for (p = envdef; *p != NULL; i++, p++) {952if ((dtp->dt_proc_env[i] = strdup(*p)) == NULL)953goto err;954}955956return;957958err:959while (--i != 0) {960dt_free(dtp, dtp->dt_proc_env[i]);961}962dt_free(dtp, dtp->dt_proc_env);963dtp->dt_proc_env = NULL;964}965966void967dt_proc_fini(dtrace_hdl_t *dtp)968{969dt_proc_hash_t *dph = dtp->dt_procs;970dt_proc_t *dpr;971char **p;972973while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)974dt_proc_destroy(dtp, dpr->dpr_proc);975976dtp->dt_procs = NULL;977dt_free(dtp, dph);978979for (p = dtp->dt_proc_env; *p != NULL; p++)980dt_free(dtp, *p);981982dt_free(dtp, dtp->dt_proc_env);983dtp->dt_proc_env = NULL;984}985986struct ps_prochandle *987dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,988proc_child_func *pcf, void *child_arg)989{990dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");991struct ps_prochandle *P = dt_proc_create(dtp, file, argv, pcf, child_arg);992993if (P != NULL && idp != NULL && idp->di_id == 0) {994idp->di_id = proc_getpid(P); /* $target = created pid */995}996997return (P);998}9991000struct ps_prochandle *1001dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)1002{1003dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");1004struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);10051006if (P != NULL && idp != NULL && idp->di_id == 0)1007idp->di_id = pid; /* $target = grabbed pid */10081009return (P);1010}10111012void1013dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)1014{1015dt_proc_release(dtp, P);1016}10171018void1019dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)1020{1021dt_proc_continue(dtp, P);1022}102310241025