xref: /onnv-gate/usr/src/lib/libdtrace/common/dt_proc.c (revision 3235:9af05252020c)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3235Sraf  * Common Development and Distribution License (the "License").
6*3235Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211399Sahl 
220Sstevel@tonic-gate /*
231399Sahl  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*
300Sstevel@tonic-gate  * DTrace Process Control
310Sstevel@tonic-gate  *
320Sstevel@tonic-gate  * This file provides a set of routines that permit libdtrace and its clients
330Sstevel@tonic-gate  * to create and grab process handles using libproc, and to share these handles
340Sstevel@tonic-gate  * between library mechanisms that need libproc access, such as ustack(), and
350Sstevel@tonic-gate  * client mechanisms that need libproc access, such as dtrace(1M) -c and -p.
360Sstevel@tonic-gate  * The library provides several mechanisms in the libproc control layer:
370Sstevel@tonic-gate  *
380Sstevel@tonic-gate  * Reference Counting: The library code and client code can independently grab
390Sstevel@tonic-gate  * the same process handles without interfering with one another.  Only when
400Sstevel@tonic-gate  * the reference count drops to zero and the handle is not being cached (see
410Sstevel@tonic-gate  * below for more information on caching) will Prelease() be called on it.
420Sstevel@tonic-gate  *
430Sstevel@tonic-gate  * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
440Sstevel@tonic-gate  * the reference count drops to zero, the handle is not immediately released.
450Sstevel@tonic-gate  * Instead, libproc handles are maintained on dph_lrulist in order from most-
460Sstevel@tonic-gate  * recently accessed to least-recently accessed.  Idle handles are maintained
470Sstevel@tonic-gate  * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
480Sstevel@tonic-gate  * to ustack() to avoid the overhead of releasing and re-grabbing processes.
490Sstevel@tonic-gate  *
500Sstevel@tonic-gate  * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
510Sstevel@tonic-gate  * or created by dt_proc_create(), a control thread is created to provide
520Sstevel@tonic-gate  * callbacks on process exit and symbol table caching on dlopen()s.
530Sstevel@tonic-gate  *
540Sstevel@tonic-gate  * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
550Sstevel@tonic-gate  * are provided to synchronize access to the libproc handle between libdtrace
560Sstevel@tonic-gate  * code and client code and the control thread's use of the ps_prochandle.
570Sstevel@tonic-gate  *
580Sstevel@tonic-gate  * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
590Sstevel@tonic-gate  * dtrace_proc_grab/dtrace_proc_create mechanisms.  Like all exported libdtrace
600Sstevel@tonic-gate  * calls, these are assumed to be MT-Unsafe.  MT-Safety is ONLY provided for
610Sstevel@tonic-gate  * synchronization between libdtrace control threads and the client thread.
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  * The ps_prochandles themselves are maintained along with a dt_proc_t struct
640Sstevel@tonic-gate  * in a hash table indexed by PID.  This provides basic locking and reference
650Sstevel@tonic-gate  * counting.  The dt_proc_t is also maintained in LRU order on dph_lrulist.
660Sstevel@tonic-gate  * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
670Sstevel@tonic-gate  * the current limit on the number of actively cached entries.
680Sstevel@tonic-gate  *
690Sstevel@tonic-gate  * The control thread for a process establishes breakpoints at the rtld_db
700Sstevel@tonic-gate  * locations of interest, updates mappings and symbol tables at these points,
710Sstevel@tonic-gate  * and handles exec and fork (by always following the parent).  The control
720Sstevel@tonic-gate  * thread automatically exits when the process dies or control is lost.
730Sstevel@tonic-gate  *
740Sstevel@tonic-gate  * A simple notification mechanism is provided for libdtrace clients using
750Sstevel@tonic-gate  * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events.  If
760Sstevel@tonic-gate  * such an event occurs, the dt_proc_t itself is enqueued on a notification
770Sstevel@tonic-gate  * list and the control thread broadcasts to dph_cv.  dtrace_sleep() will wake
780Sstevel@tonic-gate  * up using this condition and will then call the client handler as necessary.
790Sstevel@tonic-gate  */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate #include <sys/wait.h>
820Sstevel@tonic-gate #include <sys/lwp.h>
830Sstevel@tonic-gate #include <strings.h>
840Sstevel@tonic-gate #include <signal.h>
850Sstevel@tonic-gate #include <assert.h>
860Sstevel@tonic-gate #include <errno.h>
870Sstevel@tonic-gate 
880Sstevel@tonic-gate #include <dt_proc.h>
890Sstevel@tonic-gate #include <dt_pid.h>
900Sstevel@tonic-gate #include <dt_impl.h>
910Sstevel@tonic-gate 
920Sstevel@tonic-gate #define	IS_SYS_EXEC(w)	(w == SYS_exec || w == SYS_execve)
93*3235Sraf #define	IS_SYS_FORK(w)	(w == SYS_vfork || w == SYS_fork1 ||	\
94*3235Sraf 			w == SYS_forkall || w == SYS_forksys)
950Sstevel@tonic-gate 
960Sstevel@tonic-gate static dt_bkpt_t *
970Sstevel@tonic-gate dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
980Sstevel@tonic-gate {
990Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
1000Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1010Sstevel@tonic-gate 
1020Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
1050Sstevel@tonic-gate 		dbp->dbp_func = func;
1060Sstevel@tonic-gate 		dbp->dbp_data = data;
1070Sstevel@tonic-gate 		dbp->dbp_addr = addr;
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
1100Sstevel@tonic-gate 			dbp->dbp_active = B_TRUE;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 		dt_list_append(&dpr->dpr_bps, dbp);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	return (dbp);
1160Sstevel@tonic-gate }
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate static void
1190Sstevel@tonic-gate dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
1200Sstevel@tonic-gate {
1210Sstevel@tonic-gate 	int state = Pstate(dpr->dpr_proc);
1220Sstevel@tonic-gate 	dt_bkpt_t *dbp, *nbp;
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
1250Sstevel@tonic-gate 
1260Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
1270Sstevel@tonic-gate 		if (delbkpts && dbp->dbp_active &&
1280Sstevel@tonic-gate 		    state != PS_LOST && state != PS_UNDEAD) {
1290Sstevel@tonic-gate 			(void) Pdelbkpt(dpr->dpr_proc,
1300Sstevel@tonic-gate 			    dbp->dbp_addr, dbp->dbp_instr);
1310Sstevel@tonic-gate 		}
1320Sstevel@tonic-gate 		nbp = dt_list_next(dbp);
1330Sstevel@tonic-gate 		dt_list_delete(&dpr->dpr_bps, dbp);
1340Sstevel@tonic-gate 		dt_free(dpr->dpr_hdl, dbp);
1350Sstevel@tonic-gate 	}
1360Sstevel@tonic-gate }
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate static void
1390Sstevel@tonic-gate dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
1400Sstevel@tonic-gate {
1410Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
1420Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
1450Sstevel@tonic-gate 
1460Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1470Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1480Sstevel@tonic-gate 		if (psp->pr_reg[R_PC] == dbp->dbp_addr)
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	if (dbp == NULL) {
1530Sstevel@tonic-gate 		dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
1540Sstevel@tonic-gate 		    (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
1550Sstevel@tonic-gate 		return;
1560Sstevel@tonic-gate 	}
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 	dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
1590Sstevel@tonic-gate 	    (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	dbp->dbp_func(dtp, dpr, dbp->dbp_data);
1620Sstevel@tonic-gate 	(void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate 
1651399Sahl void
1660Sstevel@tonic-gate dt_proc_bpenable(dt_proc_t *dpr)
1670Sstevel@tonic-gate {
1680Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1730Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1740Sstevel@tonic-gate 		if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
1750Sstevel@tonic-gate 		    dbp->dbp_addr, &dbp->dbp_instr) == 0)
1760Sstevel@tonic-gate 			dbp->dbp_active = B_TRUE;
1770Sstevel@tonic-gate 	}
1781399Sahl 
1791399Sahl 	dt_dprintf("breakpoints enabled\n");
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1821399Sahl void
1830Sstevel@tonic-gate dt_proc_bpdisable(dt_proc_t *dpr)
1840Sstevel@tonic-gate {
1850Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1860Sstevel@tonic-gate 
1870Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1900Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1910Sstevel@tonic-gate 		if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
1920Sstevel@tonic-gate 		    dbp->dbp_addr, dbp->dbp_instr) == 0)
1930Sstevel@tonic-gate 			dbp->dbp_active = B_FALSE;
1940Sstevel@tonic-gate 	}
1951399Sahl 
1961399Sahl 	dt_dprintf("breakpoints disabled\n");
1971399Sahl }
1981399Sahl 
1991399Sahl static void
2001399Sahl dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
2011399Sahl     const char *msg)
2021399Sahl {
2031399Sahl 	dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
2041399Sahl 
2051399Sahl 	if (dprn == NULL) {
2061399Sahl 		dt_dprintf("failed to allocate notification for %d %s\n",
2071399Sahl 		    (int)dpr->dpr_pid, msg);
2081399Sahl 	} else {
2091399Sahl 		dprn->dprn_dpr = dpr;
2101399Sahl 		if (msg == NULL)
2111399Sahl 			dprn->dprn_errmsg[0] = '\0';
2121399Sahl 		else
2131399Sahl 			(void) strlcpy(dprn->dprn_errmsg, msg,
2141399Sahl 			    sizeof (dprn->dprn_errmsg));
2151399Sahl 
2161399Sahl 		(void) pthread_mutex_lock(&dph->dph_lock);
2171399Sahl 
2181399Sahl 		dprn->dprn_next = dph->dph_notify;
2191399Sahl 		dph->dph_notify = dprn;
2201399Sahl 
2211399Sahl 		(void) pthread_cond_broadcast(&dph->dph_cv);
2221399Sahl 		(void) pthread_mutex_unlock(&dph->dph_lock);
2231399Sahl 	}
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate /*
2270Sstevel@tonic-gate  * Check to see if the control thread was requested to stop when the victim
2280Sstevel@tonic-gate  * process reached a particular event (why) rather than continuing the victim.
2290Sstevel@tonic-gate  * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
2300Sstevel@tonic-gate  * If 'why' is not set, this function returns immediately and does nothing.
2310Sstevel@tonic-gate  */
2320Sstevel@tonic-gate static void
2330Sstevel@tonic-gate dt_proc_stop(dt_proc_t *dpr, uint8_t why)
2340Sstevel@tonic-gate {
2350Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
2360Sstevel@tonic-gate 	assert(why != DT_PROC_STOP_IDLE);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	if (dpr->dpr_stop & why) {
2390Sstevel@tonic-gate 		dpr->dpr_stop |= DT_PROC_STOP_IDLE;
2400Sstevel@tonic-gate 		dpr->dpr_stop &= ~why;
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
2430Sstevel@tonic-gate 
2440Sstevel@tonic-gate 		while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
2450Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate }
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate /*ARGSUSED*/
2500Sstevel@tonic-gate static void
2510Sstevel@tonic-gate dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
2520Sstevel@tonic-gate {
2530Sstevel@tonic-gate 	dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
2540Sstevel@tonic-gate 	dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
2550Sstevel@tonic-gate }
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate static void
2580Sstevel@tonic-gate dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
2590Sstevel@tonic-gate {
2600Sstevel@tonic-gate 	rd_event_msg_t rdm;
2610Sstevel@tonic-gate 	rd_err_e err;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
2640Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to get %s event message: %s\n",
2650Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
2660Sstevel@tonic-gate 		return;
2670Sstevel@tonic-gate 	}
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
2700Sstevel@tonic-gate 	    (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate 	switch (rdm.type) {
2730Sstevel@tonic-gate 	case RD_DLACTIVITY:
2741399Sahl 		if (rdm.u.state != RD_CONSISTENT)
2751399Sahl 			break;
2761399Sahl 
2771399Sahl 		Pupdate_syms(dpr->dpr_proc);
2781399Sahl 		if (dt_pid_create_probes_module(dtp, dpr) != 0)
2791399Sahl 			dt_proc_notify(dtp, dtp->dt_procs, dpr,
2801399Sahl 			    dpr->dpr_errmsg);
2811399Sahl 
2820Sstevel@tonic-gate 		break;
2830Sstevel@tonic-gate 	case RD_PREINIT:
2840Sstevel@tonic-gate 		Pupdate_syms(dpr->dpr_proc);
2850Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
2860Sstevel@tonic-gate 		break;
2870Sstevel@tonic-gate 	case RD_POSTINIT:
2880Sstevel@tonic-gate 		Pupdate_syms(dpr->dpr_proc);
2890Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
2900Sstevel@tonic-gate 		break;
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate }
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate static void
2950Sstevel@tonic-gate dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
2960Sstevel@tonic-gate {
2970Sstevel@tonic-gate 	rd_notify_t rdn;
2980Sstevel@tonic-gate 	rd_err_e err;
2990Sstevel@tonic-gate 
3000Sstevel@tonic-gate 	if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
3010Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to get event address for %s: %s\n",
3020Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
3030Sstevel@tonic-gate 		return;
3040Sstevel@tonic-gate 	}
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	if (rdn.type != RD_NOTIFY_BPT) {
3070Sstevel@tonic-gate 		dt_dprintf("pid %d: event %s has unexpected type %d\n",
3080Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rdn.type);
3090Sstevel@tonic-gate 		return;
3100Sstevel@tonic-gate 	}
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 	(void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
3130Sstevel@tonic-gate 	    (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate 
3160Sstevel@tonic-gate /*
3170Sstevel@tonic-gate  * Common code for enabling events associated with the run-time linker after
3180Sstevel@tonic-gate  * attaching to a process or after a victim process completes an exec(2).
3190Sstevel@tonic-gate  */
3200Sstevel@tonic-gate static void
3210Sstevel@tonic-gate dt_proc_attach(dt_proc_t *dpr, int exec)
3220Sstevel@tonic-gate {
3230Sstevel@tonic-gate 	const pstatus_t *psp = Pstatus(dpr->dpr_proc);
3240Sstevel@tonic-gate 	rd_err_e err;
3250Sstevel@tonic-gate 	GElf_Sym sym;
3260Sstevel@tonic-gate 
3270Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
3280Sstevel@tonic-gate 
3290Sstevel@tonic-gate 	if (exec) {
3300Sstevel@tonic-gate 		if (psp->pr_lwp.pr_errno != 0)
3310Sstevel@tonic-gate 			return; /* exec failed: nothing needs to be done */
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 		dt_proc_bpdestroy(dpr, B_FALSE);
3340Sstevel@tonic-gate 		Preset_maps(dpr->dpr_proc);
3350Sstevel@tonic-gate 	}
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 	if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
3380Sstevel@tonic-gate 	    (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
3390Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
3400Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
3410Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
3420Sstevel@tonic-gate 	} else {
3430Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to enable rtld events: %s\n",
3440Sstevel@tonic-gate 		    (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
3450Sstevel@tonic-gate 		    "rtld_db agent initialization failed");
3460Sstevel@tonic-gate 	}
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate 	Pupdate_maps(dpr->dpr_proc);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
3510Sstevel@tonic-gate 	    "a.out", "main", &sym, NULL) == 0) {
3520Sstevel@tonic-gate 		(void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
3530Sstevel@tonic-gate 		    (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
3540Sstevel@tonic-gate 	} else {
3550Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to find a.out`main: %s\n",
3560Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(errno));
3570Sstevel@tonic-gate 	}
3580Sstevel@tonic-gate }
3590Sstevel@tonic-gate 
3600Sstevel@tonic-gate /*
3610Sstevel@tonic-gate  * Wait for a stopped process to be set running again by some other debugger.
3620Sstevel@tonic-gate  * This is typically not required by /proc-based debuggers, since the usual
3630Sstevel@tonic-gate  * model is that one debugger controls one victim.  But DTrace, as usual, has
3640Sstevel@tonic-gate  * its own needs: the stop() action assumes that prun(1) or some other tool
3650Sstevel@tonic-gate  * will be applied to resume the victim process.  This could be solved by
3660Sstevel@tonic-gate  * adding a PCWRUN directive to /proc, but that seems like overkill unless
3670Sstevel@tonic-gate  * other debuggers end up needing this functionality, so we implement a cheap
3680Sstevel@tonic-gate  * equivalent to PCWRUN using the set of existing kernel mechanisms.
3690Sstevel@tonic-gate  *
3700Sstevel@tonic-gate  * Our intent is really not just to wait for the victim to run, but rather to
3710Sstevel@tonic-gate  * wait for it to run and then stop again for a reason other than the current
3720Sstevel@tonic-gate  * PR_REQUESTED stop.  Since PCWSTOP/Pstopstatus() can be applied repeatedly
3730Sstevel@tonic-gate  * to a stopped process and will return the same result without affecting the
3740Sstevel@tonic-gate  * victim, we can just perform these operations repeatedly until Pstate()
3750Sstevel@tonic-gate  * changes, the representative LWP ID changes, or the stop timestamp advances.
3760Sstevel@tonic-gate  * dt_proc_control() will then rediscover the new state and continue as usual.
3770Sstevel@tonic-gate  * When the process is still stopped in the same exact state, we sleep for a
3780Sstevel@tonic-gate  * brief interval before waiting again so as not to spin consuming CPU cycles.
3790Sstevel@tonic-gate  */
3800Sstevel@tonic-gate static void
3810Sstevel@tonic-gate dt_proc_waitrun(dt_proc_t *dpr)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
3840Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
3870Sstevel@tonic-gate 	timestruc_t tstamp = psp->pr_tstamp;
3880Sstevel@tonic-gate 	lwpid_t lwpid = psp->pr_lwpid;
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate 	const long wstop = PCWSTOP;
3910Sstevel@tonic-gate 	int pfd = Pctlfd(P);
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
3940Sstevel@tonic-gate 	assert(psp->pr_flags & PR_STOPPED);
3950Sstevel@tonic-gate 	assert(Pstate(P) == PS_STOP);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
3990Sstevel@tonic-gate 	 * so that if the libdtrace client is killed, the victim stays stopped.
4000Sstevel@tonic-gate 	 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	(void) Punsetflags(P, krflag);
4030Sstevel@tonic-gate 	Psync(P);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate 	while (!dpr->dpr_quit) {
4080Sstevel@tonic-gate 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
4090Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
4120Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
4130Sstevel@tonic-gate 		psp = &Pstatus(P)->pr_lwp;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 		/*
4160Sstevel@tonic-gate 		 * If we've reached a new state, found a new representative, or
4170Sstevel@tonic-gate 		 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
4180Sstevel@tonic-gate 		 * original setting and then return with dpr_lock held.
4190Sstevel@tonic-gate 		 */
4200Sstevel@tonic-gate 		if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
4210Sstevel@tonic-gate 		    bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
4220Sstevel@tonic-gate 			(void) Psetflags(P, krflag);
4230Sstevel@tonic-gate 			Psync(P);
4240Sstevel@tonic-gate 			return;
4250Sstevel@tonic-gate 		}
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
4280Sstevel@tonic-gate 		(void) poll(NULL, 0, MILLISEC / 2);
4290Sstevel@tonic-gate 	}
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
4320Sstevel@tonic-gate }
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate typedef struct dt_proc_control_data {
4350Sstevel@tonic-gate 	dtrace_hdl_t *dpcd_hdl;			/* DTrace handle */
4360Sstevel@tonic-gate 	dt_proc_t *dpcd_proc;			/* proccess to control */
4370Sstevel@tonic-gate } dt_proc_control_data_t;
4380Sstevel@tonic-gate 
4390Sstevel@tonic-gate /*
4400Sstevel@tonic-gate  * Main loop for all victim process control threads.  We initialize all the
4410Sstevel@tonic-gate  * appropriate /proc control mechanisms, and then enter a loop waiting for
4420Sstevel@tonic-gate  * the process to stop on an event or die.  We process any events by calling
4430Sstevel@tonic-gate  * appropriate subroutines, and exit when the victim dies or we lose control.
4440Sstevel@tonic-gate  *
4450Sstevel@tonic-gate  * The control thread synchronizes the use of dpr_proc with other libdtrace
4460Sstevel@tonic-gate  * threads using dpr_lock.  We hold the lock for all of our operations except
4470Sstevel@tonic-gate  * waiting while the process is running: this is accomplished by writing a
4480Sstevel@tonic-gate  * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.  If the
4490Sstevel@tonic-gate  * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
4500Sstevel@tonic-gate  */
4510Sstevel@tonic-gate static void *
4520Sstevel@tonic-gate dt_proc_control(void *arg)
4530Sstevel@tonic-gate {
4540Sstevel@tonic-gate 	dt_proc_control_data_t *datap = arg;
4550Sstevel@tonic-gate 	dtrace_hdl_t *dtp = datap->dpcd_hdl;
4560Sstevel@tonic-gate 	dt_proc_t *dpr = datap->dpcd_proc;
4570Sstevel@tonic-gate 	dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs;
4580Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	int pfd = Pctlfd(P);
4610Sstevel@tonic-gate 	int pid = dpr->dpr_pid;
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate 	const long wstop = PCWSTOP;
4640Sstevel@tonic-gate 	int notify = B_FALSE;
4650Sstevel@tonic-gate 
4660Sstevel@tonic-gate 	/*
4670Sstevel@tonic-gate 	 * We disable the POSIX thread cancellation mechanism so that the
4680Sstevel@tonic-gate 	 * client program using libdtrace can't accidentally cancel our thread.
4690Sstevel@tonic-gate 	 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
4700Sstevel@tonic-gate 	 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
4710Sstevel@tonic-gate 	 */
4720Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	/*
4750Sstevel@tonic-gate 	 * Set up the corresponding process for tracing by libdtrace.  We want
4760Sstevel@tonic-gate 	 * to be able to catch breakpoints and efficiently single-step over
4770Sstevel@tonic-gate 	 * them, and we need to enable librtld_db to watch libdl activity.
4780Sstevel@tonic-gate 	 */
4790Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	(void) Punsetflags(P, PR_ASYNC);	/* require synchronous mode */
4820Sstevel@tonic-gate 	(void) Psetflags(P, PR_BPTADJ);		/* always adjust eip on x86 */
4830Sstevel@tonic-gate 	(void) Punsetflags(P, PR_FORK);		/* do not inherit on fork */
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	(void) Pfault(P, FLTBPT, B_TRUE);	/* always trace breakpoints */
4860Sstevel@tonic-gate 	(void) Pfault(P, FLTTRACE, B_TRUE);	/* always trace single-step */
4870Sstevel@tonic-gate 
4880Sstevel@tonic-gate 	/*
4890Sstevel@tonic-gate 	 * We must trace exit from exec() system calls so that if the exec is
4900Sstevel@tonic-gate 	 * successful, we can reset our breakpoints and re-initialize libproc.
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	(void) Psysexit(P, SYS_exec, B_TRUE);
4930Sstevel@tonic-gate 	(void) Psysexit(P, SYS_execve, B_TRUE);
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 	/*
4960Sstevel@tonic-gate 	 * We must trace entry and exit for fork() system calls in order to
4970Sstevel@tonic-gate 	 * disable our breakpoints temporarily during the fork.  We do not set
4980Sstevel@tonic-gate 	 * the PR_FORK flag, so if fork succeeds the child begins executing and
4990Sstevel@tonic-gate 	 * does not inherit any other tracing behaviors or a control thread.
5000Sstevel@tonic-gate 	 */
5010Sstevel@tonic-gate 	(void) Psysentry(P, SYS_vfork, B_TRUE);
5020Sstevel@tonic-gate 	(void) Psysexit(P, SYS_vfork, B_TRUE);
5030Sstevel@tonic-gate 	(void) Psysentry(P, SYS_fork1, B_TRUE);
5040Sstevel@tonic-gate 	(void) Psysexit(P, SYS_fork1, B_TRUE);
5050Sstevel@tonic-gate 	(void) Psysentry(P, SYS_forkall, B_TRUE);
5060Sstevel@tonic-gate 	(void) Psysexit(P, SYS_forkall, B_TRUE);
507*3235Sraf 	(void) Psysentry(P, SYS_forksys, B_TRUE);
508*3235Sraf 	(void) Psysexit(P, SYS_forksys, B_TRUE);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	Psync(P);				/* enable all /proc changes */
5110Sstevel@tonic-gate 	dt_proc_attach(dpr, B_FALSE);		/* enable rtld breakpoints */
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	/*
5140Sstevel@tonic-gate 	 * If PR_KLC is set, we created the process; otherwise we grabbed it.
5150Sstevel@tonic-gate 	 * Check for an appropriate stop request and wait for dt_proc_continue.
5160Sstevel@tonic-gate 	 */
5170Sstevel@tonic-gate 	if (Pstatus(P)->pr_flags & PR_KLC)
5180Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
5190Sstevel@tonic-gate 	else
5200Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	if (Psetrun(P, 0, 0) == -1) {
5230Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to set running: %s\n",
5240Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(errno));
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 	/*
5300Sstevel@tonic-gate 	 * Wait for the process corresponding to this control thread to stop,
5310Sstevel@tonic-gate 	 * process the event, and then set it running again.  We want to sleep
5320Sstevel@tonic-gate 	 * with dpr_lock *unheld* so that other parts of libdtrace can use the
5330Sstevel@tonic-gate 	 * ps_prochandle in the meantime (e.g. ustack()).  To do this, we write
5340Sstevel@tonic-gate 	 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
5350Sstevel@tonic-gate 	 * Once the process stops, we wake up, grab dpr_lock, and then call
5360Sstevel@tonic-gate 	 * Pwait() (which will return immediately) and do our processing.
5370Sstevel@tonic-gate 	 */
5380Sstevel@tonic-gate 	while (!dpr->dpr_quit) {
5390Sstevel@tonic-gate 		const lwpstatus_t *psp;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
5420Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
5430Sstevel@tonic-gate 
5440Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
5450Sstevel@tonic-gate pwait_locked:
5460Sstevel@tonic-gate 		if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
5470Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&dpr->dpr_lock);
5480Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
5490Sstevel@tonic-gate 		}
5500Sstevel@tonic-gate 
5510Sstevel@tonic-gate 		switch (Pstate(P)) {
5520Sstevel@tonic-gate 		case PS_STOP:
5530Sstevel@tonic-gate 			psp = &Pstatus(P)->pr_lwp;
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 			dt_dprintf("pid %d: proc stopped showing %d/%d\n",
5560Sstevel@tonic-gate 			    pid, psp->pr_why, psp->pr_what);
5570Sstevel@tonic-gate 
5580Sstevel@tonic-gate 			/*
5590Sstevel@tonic-gate 			 * If the process stops showing PR_REQUESTED, then the
5600Sstevel@tonic-gate 			 * DTrace stop() action was applied to it or another
5610Sstevel@tonic-gate 			 * debugging utility (e.g. pstop(1)) asked it to stop.
5620Sstevel@tonic-gate 			 * In either case, the user's intention is for the
5630Sstevel@tonic-gate 			 * process to remain stopped until another external
5640Sstevel@tonic-gate 			 * mechanism (e.g. prun(1)) is applied.  So instead of
5650Sstevel@tonic-gate 			 * setting the process running ourself, we wait for
5660Sstevel@tonic-gate 			 * someone else to do so.  Once that happens, we return
5670Sstevel@tonic-gate 			 * to our normal loop waiting for an event of interest.
5680Sstevel@tonic-gate 			 */
5690Sstevel@tonic-gate 			if (psp->pr_why == PR_REQUESTED) {
5700Sstevel@tonic-gate 				dt_proc_waitrun(dpr);
5710Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&dpr->dpr_lock);
5720Sstevel@tonic-gate 				continue;
5730Sstevel@tonic-gate 			}
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 			/*
5760Sstevel@tonic-gate 			 * If the process stops showing one of the events that
5770Sstevel@tonic-gate 			 * we are tracing, perform the appropriate response.
5780Sstevel@tonic-gate 			 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
5790Sstevel@tonic-gate 			 * PR_JOBCONTROL by design: if one of these conditions
5800Sstevel@tonic-gate 			 * occurs, we will fall through to Psetrun() but the
5810Sstevel@tonic-gate 			 * process will remain stopped in the kernel by the
5820Sstevel@tonic-gate 			 * corresponding mechanism (e.g. job control stop).
5830Sstevel@tonic-gate 			 */
5840Sstevel@tonic-gate 			if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
5850Sstevel@tonic-gate 				dt_proc_bpmatch(dtp, dpr);
5860Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSENTRY &&
5870Sstevel@tonic-gate 			    IS_SYS_FORK(psp->pr_what))
5880Sstevel@tonic-gate 				dt_proc_bpdisable(dpr);
5890Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSEXIT &&
5900Sstevel@tonic-gate 			    IS_SYS_FORK(psp->pr_what))
5910Sstevel@tonic-gate 				dt_proc_bpenable(dpr);
5920Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSEXIT &&
5930Sstevel@tonic-gate 			    IS_SYS_EXEC(psp->pr_what))
5940Sstevel@tonic-gate 				dt_proc_attach(dpr, B_TRUE);
5950Sstevel@tonic-gate 			break;
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 		case PS_LOST:
5980Sstevel@tonic-gate 			if (Preopen(P) == 0)
5990Sstevel@tonic-gate 				goto pwait_locked;
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 			dt_dprintf("pid %d: proc lost: %s\n",
6020Sstevel@tonic-gate 			    pid, strerror(errno));
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate 			dpr->dpr_quit = B_TRUE;
6050Sstevel@tonic-gate 			notify = B_TRUE;
6060Sstevel@tonic-gate 			break;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		case PS_UNDEAD:
6090Sstevel@tonic-gate 			dt_dprintf("pid %d: proc died\n", pid);
6100Sstevel@tonic-gate 			dpr->dpr_quit = B_TRUE;
6110Sstevel@tonic-gate 			notify = B_TRUE;
6120Sstevel@tonic-gate 			break;
6130Sstevel@tonic-gate 		}
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 		if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
6160Sstevel@tonic-gate 			dt_dprintf("pid %d: failed to set running: %s\n",
6170Sstevel@tonic-gate 			    (int)dpr->dpr_pid, strerror(errno));
6180Sstevel@tonic-gate 		}
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate 
6230Sstevel@tonic-gate 	/*
6240Sstevel@tonic-gate 	 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
6250Sstevel@tonic-gate 	 * the dt_proc_t structure on the dt_proc_hash_t notification list.
6260Sstevel@tonic-gate 	 */
6271399Sahl 	if (notify)
6281399Sahl 		dt_proc_notify(dtp, dph, dpr, NULL);
6290Sstevel@tonic-gate 
6300Sstevel@tonic-gate 	/*
6310Sstevel@tonic-gate 	 * Destroy and remove any remaining breakpoints, set dpr_done and clear
6320Sstevel@tonic-gate 	 * dpr_tid to indicate the control thread has exited, and notify any
6330Sstevel@tonic-gate 	 * waiting thread in dt_proc_destroy() that we have succesfully exited.
6340Sstevel@tonic-gate 	 */
6350Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	dt_proc_bpdestroy(dpr, B_TRUE);
6380Sstevel@tonic-gate 	dpr->dpr_done = B_TRUE;
6390Sstevel@tonic-gate 	dpr->dpr_tid = 0;
6400Sstevel@tonic-gate 
6411399Sahl 	(void) pthread_cond_broadcast(&dpr->dpr_cv);
6420Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
6430Sstevel@tonic-gate 
6440Sstevel@tonic-gate 	return (NULL);
6450Sstevel@tonic-gate }
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate /*PRINTFLIKE3*/
6480Sstevel@tonic-gate static struct ps_prochandle *
6490Sstevel@tonic-gate dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	va_list ap;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	va_start(ap, format);
6540Sstevel@tonic-gate 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
6550Sstevel@tonic-gate 	va_end(ap);
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (dpr->dpr_proc != NULL)
6580Sstevel@tonic-gate 		Prelease(dpr->dpr_proc, 0);
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	dt_free(dtp, dpr);
6610Sstevel@tonic-gate 	(void) dt_set_errno(dtp, EDT_COMPILER);
6620Sstevel@tonic-gate 	return (NULL);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate 
6650Sstevel@tonic-gate dt_proc_t *
6660Sstevel@tonic-gate dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
6670Sstevel@tonic-gate {
6680Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
6690Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
6700Sstevel@tonic-gate 	dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
6730Sstevel@tonic-gate 		if (dpr->dpr_pid == pid)
6740Sstevel@tonic-gate 			break;
6750Sstevel@tonic-gate 		else
6760Sstevel@tonic-gate 			dpp = &dpr->dpr_hash;
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 
6790Sstevel@tonic-gate 	assert(dpr != NULL);
6800Sstevel@tonic-gate 	assert(dpr->dpr_proc == P);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 	if (remove)
6830Sstevel@tonic-gate 		*dpp = dpr->dpr_hash; /* remove from pid hash chain */
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	return (dpr);
6860Sstevel@tonic-gate }
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate static void
6890Sstevel@tonic-gate dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
6900Sstevel@tonic-gate {
691576Sbmc 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
6920Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
6931399Sahl 	dt_proc_notify_t *npr, **npp;
6940Sstevel@tonic-gate 	int rflag;
6950Sstevel@tonic-gate 
6960Sstevel@tonic-gate 	assert(dpr != NULL);
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	/*
6990Sstevel@tonic-gate 	 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
7000Sstevel@tonic-gate 	 * an external debugger and we were waiting in dt_proc_waitrun().
7010Sstevel@tonic-gate 	 * Leave the process in this condition using PRELEASE_HANG.
7020Sstevel@tonic-gate 	 */
7030Sstevel@tonic-gate 	if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
7040Sstevel@tonic-gate 		dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
7050Sstevel@tonic-gate 		rflag = PRELEASE_HANG;
7060Sstevel@tonic-gate 	} else {
7070Sstevel@tonic-gate 		dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
7080Sstevel@tonic-gate 		rflag = 0; /* apply kill or run-on-last-close */
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	if (dpr->dpr_tid) {
7120Sstevel@tonic-gate 		/*
7130Sstevel@tonic-gate 		 * Set the dpr_quit flag to tell the daemon thread to exit.  We
7140Sstevel@tonic-gate 		 * send it a SIGCANCEL to poke it out of PCWSTOP or any other
7150Sstevel@tonic-gate 		 * long-term /proc system call.  Our daemon threads have POSIX
7160Sstevel@tonic-gate 		 * cancellation disabled, so EINTR will be the only effect.  We
7170Sstevel@tonic-gate 		 * then wait for dpr_done to indicate the thread has exited.
7180Sstevel@tonic-gate 		 *
7190Sstevel@tonic-gate 		 * We can't use pthread_kill() to send SIGCANCEL because the
7200Sstevel@tonic-gate 		 * interface forbids it and we can't use pthread_cancel()
7210Sstevel@tonic-gate 		 * because with cancellation disabled it won't actually
7220Sstevel@tonic-gate 		 * send SIGCANCEL to the target thread, so we use _lwp_kill()
7230Sstevel@tonic-gate 		 * to do the job.  This is all built on evil knowledge of
7240Sstevel@tonic-gate 		 * the details of the cancellation mechanism in libc.
7250Sstevel@tonic-gate 		 */
7260Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
7270Sstevel@tonic-gate 		dpr->dpr_quit = B_TRUE;
7280Sstevel@tonic-gate 		(void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
7290Sstevel@tonic-gate 
730265Smws 		/*
731265Smws 		 * If the process is currently idling in dt_proc_stop(), re-
732265Smws 		 * enable breakpoints and poke it into running again.
733265Smws 		 */
734265Smws 		if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
735265Smws 			dt_proc_bpenable(dpr);
736265Smws 			dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
737265Smws 			(void) pthread_cond_broadcast(&dpr->dpr_cv);
738265Smws 		}
739265Smws 
7400Sstevel@tonic-gate 		while (!dpr->dpr_done)
7410Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
7440Sstevel@tonic-gate 	}
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/*
747576Sbmc 	 * Before we free the process structure, remove this dt_proc_t from the
748576Sbmc 	 * lookup hash, and then walk the dt_proc_hash_t's notification list
749576Sbmc 	 * and remove this dt_proc_t if it is enqueued.
7500Sstevel@tonic-gate 	 */
7510Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dph->dph_lock);
752576Sbmc 	(void) dt_proc_lookup(dtp, P, B_TRUE);
7530Sstevel@tonic-gate 	npp = &dph->dph_notify;
7540Sstevel@tonic-gate 
7551399Sahl 	while ((npr = *npp) != NULL) {
7561399Sahl 		if (npr->dprn_dpr == dpr) {
7571399Sahl 			*npp = npr->dprn_next;
7581399Sahl 			dt_free(dtp, npr);
7591399Sahl 		} else {
7601399Sahl 			npp = &npr->dprn_next;
7611399Sahl 		}
7620Sstevel@tonic-gate 	}
7630Sstevel@tonic-gate 
7640Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dph->dph_lock);
7650Sstevel@tonic-gate 
7660Sstevel@tonic-gate 	/*
7670Sstevel@tonic-gate 	 * Remove the dt_proc_list from the LRU list, release the underlying
7680Sstevel@tonic-gate 	 * libproc handle, and free our dt_proc_t data structure.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	if (dpr->dpr_cacheable) {
7710Sstevel@tonic-gate 		assert(dph->dph_lrucnt != 0);
7720Sstevel@tonic-gate 		dph->dph_lrucnt--;
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	dt_list_delete(&dph->dph_lrulist, dpr);
7760Sstevel@tonic-gate 	Prelease(dpr->dpr_proc, rflag);
7770Sstevel@tonic-gate 	dt_free(dtp, dpr);
7780Sstevel@tonic-gate }
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate static int
7810Sstevel@tonic-gate dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
7820Sstevel@tonic-gate {
7830Sstevel@tonic-gate 	dt_proc_control_data_t data;
7840Sstevel@tonic-gate 	sigset_t nset, oset;
7850Sstevel@tonic-gate 	pthread_attr_t a;
7860Sstevel@tonic-gate 	int err;
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
7890Sstevel@tonic-gate 	dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	(void) pthread_attr_init(&a);
7920Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate 	(void) sigfillset(&nset);
7950Sstevel@tonic-gate 	(void) sigdelset(&nset, SIGABRT);	/* unblocked for assert() */
7960Sstevel@tonic-gate 	(void) sigdelset(&nset, SIGCANCEL);	/* see dt_proc_destroy() */
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	data.dpcd_hdl = dtp;
7990Sstevel@tonic-gate 	data.dpcd_proc = dpr;
8000Sstevel@tonic-gate 
8010Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
8020Sstevel@tonic-gate 	err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
8030Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	/*
8060Sstevel@tonic-gate 	 * If the control thread was created, then wait on dpr_cv for either
8070Sstevel@tonic-gate 	 * dpr_done to be set (the victim died or the control thread failed)
8080Sstevel@tonic-gate 	 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
8090Sstevel@tonic-gate 	 * stopped by /proc and the control thread is at the rendezvous event.
8100Sstevel@tonic-gate 	 * On success, we return with the process and control thread stopped:
8110Sstevel@tonic-gate 	 * the caller can then apply dt_proc_continue() to resume both.
8120Sstevel@tonic-gate 	 */
8130Sstevel@tonic-gate 	if (err == 0) {
8140Sstevel@tonic-gate 		while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
8150Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 		/*
8180Sstevel@tonic-gate 		 * If dpr_done is set, the control thread aborted before it
8190Sstevel@tonic-gate 		 * reached the rendezvous event.  This is either due to PS_LOST
8200Sstevel@tonic-gate 		 * or PS_UNDEAD (i.e. the process died).  We try to provide a
8210Sstevel@tonic-gate 		 * small amount of useful information to help figure it out.
8220Sstevel@tonic-gate 		 */
8230Sstevel@tonic-gate 		if (dpr->dpr_done) {
8240Sstevel@tonic-gate 			const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
8250Sstevel@tonic-gate 			int stat = prp ? prp->pr_wstat : 0;
8260Sstevel@tonic-gate 			int pid = dpr->dpr_pid;
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate 			if (Pstate(dpr->dpr_proc) == PS_LOST) {
8290Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8300Sstevel@tonic-gate 				    "failed to control pid %d: process exec'd "
8310Sstevel@tonic-gate 				    "set-id or unobservable program\n", pid);
8320Sstevel@tonic-gate 			} else if (WIFSIGNALED(stat)) {
8330Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8340Sstevel@tonic-gate 				    "failed to control pid %d: process died "
8350Sstevel@tonic-gate 				    "from signal %d\n", pid, WTERMSIG(stat));
8360Sstevel@tonic-gate 			} else {
8370Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8380Sstevel@tonic-gate 				    "failed to control pid %d: process exited "
8390Sstevel@tonic-gate 				    "with status %d\n", pid, WEXITSTATUS(stat));
8400Sstevel@tonic-gate 			}
8410Sstevel@tonic-gate 
8420Sstevel@tonic-gate 			err = ESRCH; /* cause grab() or create() to fail */
8430Sstevel@tonic-gate 		}
8440Sstevel@tonic-gate 	} else {
8450Sstevel@tonic-gate 		(void) dt_proc_error(dpr->dpr_hdl, dpr,
8460Sstevel@tonic-gate 		    "failed to create control thread for process-id %d: %s\n",
8470Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(err));
8480Sstevel@tonic-gate 	}
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
8510Sstevel@tonic-gate 	(void) pthread_attr_destroy(&a);
8520Sstevel@tonic-gate 
8530Sstevel@tonic-gate 	return (err);
8540Sstevel@tonic-gate }
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate struct ps_prochandle *
8570Sstevel@tonic-gate dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
8580Sstevel@tonic-gate {
8590Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
8600Sstevel@tonic-gate 	dt_proc_t *dpr;
8610Sstevel@tonic-gate 	int err;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
8640Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
8670Sstevel@tonic-gate 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
8680Sstevel@tonic-gate 
8690Sstevel@tonic-gate 	if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) {
8700Sstevel@tonic-gate 		return (dt_proc_error(dtp, dpr,
8710Sstevel@tonic-gate 		    "failed to execute %s: %s\n", file, Pcreate_error(err)));
8720Sstevel@tonic-gate 	}
8730Sstevel@tonic-gate 
8740Sstevel@tonic-gate 	dpr->dpr_hdl = dtp;
8750Sstevel@tonic-gate 	dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
8760Sstevel@tonic-gate 
8770Sstevel@tonic-gate 	(void) Punsetflags(dpr->dpr_proc, PR_RLC);
8780Sstevel@tonic-gate 	(void) Psetflags(dpr->dpr_proc, PR_KLC);
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
8810Sstevel@tonic-gate 		return (NULL); /* dt_proc_error() has been called for us */
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate 	dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
8840Sstevel@tonic-gate 	dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
8850Sstevel@tonic-gate 	dt_list_prepend(&dph->dph_lrulist, dpr);
8860Sstevel@tonic-gate 
8870Sstevel@tonic-gate 	dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
8880Sstevel@tonic-gate 	dpr->dpr_refs++;
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate 	return (dpr->dpr_proc);
8910Sstevel@tonic-gate }
8920Sstevel@tonic-gate 
8930Sstevel@tonic-gate struct ps_prochandle *
8940Sstevel@tonic-gate dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
8970Sstevel@tonic-gate 	uint_t h = pid & (dph->dph_hashlen - 1);
8980Sstevel@tonic-gate 	dt_proc_t *dpr, *opr;
8990Sstevel@tonic-gate 	int err;
9000Sstevel@tonic-gate 
9010Sstevel@tonic-gate 	/*
9020Sstevel@tonic-gate 	 * Search the hash table for the pid.  If it is already grabbed or
9030Sstevel@tonic-gate 	 * created, move the handle to the front of the lrulist, increment
9040Sstevel@tonic-gate 	 * the reference count, and return the existing ps_prochandle.
9050Sstevel@tonic-gate 	 */
9060Sstevel@tonic-gate 	for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
9070Sstevel@tonic-gate 		if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
9080Sstevel@tonic-gate 			/*
9090Sstevel@tonic-gate 			 * If the cached handle was opened read-only and
9100Sstevel@tonic-gate 			 * this request is for a writeable handle, mark
9110Sstevel@tonic-gate 			 * the cached handle as stale and open a new handle.
9120Sstevel@tonic-gate 			 * Since it's stale, unmark it as cacheable.
9130Sstevel@tonic-gate 			 */
9140Sstevel@tonic-gate 			if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
9150Sstevel@tonic-gate 				dt_dprintf("upgrading pid %d\n", (int)pid);
9160Sstevel@tonic-gate 				dpr->dpr_stale = B_TRUE;
9170Sstevel@tonic-gate 				dpr->dpr_cacheable = B_FALSE;
9180Sstevel@tonic-gate 				dph->dph_lrucnt--;
9190Sstevel@tonic-gate 				break;
9200Sstevel@tonic-gate 			}
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 			dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
9230Sstevel@tonic-gate 			dt_list_delete(&dph->dph_lrulist, dpr);
9240Sstevel@tonic-gate 			dt_list_prepend(&dph->dph_lrulist, dpr);
9250Sstevel@tonic-gate 			dpr->dpr_refs++;
9260Sstevel@tonic-gate 			return (dpr->dpr_proc);
9270Sstevel@tonic-gate 		}
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
9310Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
9320Sstevel@tonic-gate 
9330Sstevel@tonic-gate 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
9340Sstevel@tonic-gate 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
9370Sstevel@tonic-gate 		return (dt_proc_error(dtp, dpr,
9380Sstevel@tonic-gate 		    "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
9390Sstevel@tonic-gate 	}
9400Sstevel@tonic-gate 
9410Sstevel@tonic-gate 	dpr->dpr_hdl = dtp;
9420Sstevel@tonic-gate 	dpr->dpr_pid = pid;
9430Sstevel@tonic-gate 
9440Sstevel@tonic-gate 	(void) Punsetflags(dpr->dpr_proc, PR_KLC);
9450Sstevel@tonic-gate 	(void) Psetflags(dpr->dpr_proc, PR_RLC);
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	/*
9480Sstevel@tonic-gate 	 * If we are attempting to grab the process without a monitor
9490Sstevel@tonic-gate 	 * thread, then mark the process cacheable only if it's being
9500Sstevel@tonic-gate 	 * grabbed read-only.  If we're currently caching more process
9510Sstevel@tonic-gate 	 * handles than dph_lrulim permits, attempt to find the
9520Sstevel@tonic-gate 	 * least-recently-used handle that is currently unreferenced and
9530Sstevel@tonic-gate 	 * release it from the cache.  Otherwise we are grabbing the process
9540Sstevel@tonic-gate 	 * for control: create a control thread for this process and store
9550Sstevel@tonic-gate 	 * its ID in dpr->dpr_tid.
9560Sstevel@tonic-gate 	 */
9570Sstevel@tonic-gate 	if (nomonitor || (flags & PGRAB_RDONLY)) {
9580Sstevel@tonic-gate 		if (dph->dph_lrucnt >= dph->dph_lrulim) {
9590Sstevel@tonic-gate 			for (opr = dt_list_prev(&dph->dph_lrulist);
9600Sstevel@tonic-gate 			    opr != NULL; opr = dt_list_prev(opr)) {
9610Sstevel@tonic-gate 				if (opr->dpr_cacheable && opr->dpr_refs == 0) {
9620Sstevel@tonic-gate 					dt_proc_destroy(dtp, opr->dpr_proc);
9630Sstevel@tonic-gate 					break;
9640Sstevel@tonic-gate 				}
9650Sstevel@tonic-gate 			}
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate 		if (flags & PGRAB_RDONLY) {
9690Sstevel@tonic-gate 			dpr->dpr_cacheable = B_TRUE;
9700Sstevel@tonic-gate 			dpr->dpr_rdonly = B_TRUE;
9710Sstevel@tonic-gate 			dph->dph_lrucnt++;
9720Sstevel@tonic-gate 		}
9730Sstevel@tonic-gate 
9740Sstevel@tonic-gate 	} else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
9750Sstevel@tonic-gate 		return (NULL); /* dt_proc_error() has been called for us */
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	dpr->dpr_hash = dph->dph_hash[h];
9780Sstevel@tonic-gate 	dph->dph_hash[h] = dpr;
9790Sstevel@tonic-gate 	dt_list_prepend(&dph->dph_lrulist, dpr);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	dt_dprintf("grabbed pid %d\n", (int)pid);
9820Sstevel@tonic-gate 	dpr->dpr_refs++;
9830Sstevel@tonic-gate 
9840Sstevel@tonic-gate 	return (dpr->dpr_proc);
9850Sstevel@tonic-gate }
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate void
9880Sstevel@tonic-gate dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
9890Sstevel@tonic-gate {
9900Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
9910Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	assert(dpr != NULL);
9940Sstevel@tonic-gate 	assert(dpr->dpr_refs != 0);
9950Sstevel@tonic-gate 
9960Sstevel@tonic-gate 	if (--dpr->dpr_refs == 0 &&
9970Sstevel@tonic-gate 	    (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
9980Sstevel@tonic-gate 		dt_proc_destroy(dtp, P);
9990Sstevel@tonic-gate }
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate void
10020Sstevel@tonic-gate dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10030Sstevel@tonic-gate {
10040Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
10070Sstevel@tonic-gate 
10080Sstevel@tonic-gate 	if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
10090Sstevel@tonic-gate 		dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
10100Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
10110Sstevel@tonic-gate 	}
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
10140Sstevel@tonic-gate }
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate void
10170Sstevel@tonic-gate dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10180Sstevel@tonic-gate {
10190Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10200Sstevel@tonic-gate 	int err = pthread_mutex_lock(&dpr->dpr_lock);
10210Sstevel@tonic-gate 	assert(err == 0); /* check for recursion */
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate void
10250Sstevel@tonic-gate dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10280Sstevel@tonic-gate 	int err = pthread_mutex_unlock(&dpr->dpr_lock);
10290Sstevel@tonic-gate 	assert(err == 0); /* check for unheld lock */
10300Sstevel@tonic-gate }
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate void
10330Sstevel@tonic-gate dt_proc_hash_create(dtrace_hdl_t *dtp)
10340Sstevel@tonic-gate {
10350Sstevel@tonic-gate 	if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
10360Sstevel@tonic-gate 	    sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) {
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 		(void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
10390Sstevel@tonic-gate 		(void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 		dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
10420Sstevel@tonic-gate 		dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
10430Sstevel@tonic-gate 	}
10440Sstevel@tonic-gate }
10450Sstevel@tonic-gate 
10460Sstevel@tonic-gate void
10470Sstevel@tonic-gate dt_proc_hash_destroy(dtrace_hdl_t *dtp)
10480Sstevel@tonic-gate {
10490Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
10500Sstevel@tonic-gate 	dt_proc_t *dpr;
10510Sstevel@tonic-gate 
10520Sstevel@tonic-gate 	while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
10530Sstevel@tonic-gate 		dt_proc_destroy(dtp, dpr->dpr_proc);
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate 	dtp->dt_procs = NULL;
10560Sstevel@tonic-gate 	dt_free(dtp, dph);
10570Sstevel@tonic-gate }
10580Sstevel@tonic-gate 
10590Sstevel@tonic-gate struct ps_prochandle *
10600Sstevel@tonic-gate dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
10630Sstevel@tonic-gate 	struct ps_prochandle *P = dt_proc_create(dtp, file, argv);
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	if (P != NULL && idp != NULL && idp->di_id == 0)
10660Sstevel@tonic-gate 		idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	return (P);
10690Sstevel@tonic-gate }
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate struct ps_prochandle *
10720Sstevel@tonic-gate dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
10730Sstevel@tonic-gate {
10740Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
10750Sstevel@tonic-gate 	struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
10760Sstevel@tonic-gate 
10770Sstevel@tonic-gate 	if (P != NULL && idp != NULL && idp->di_id == 0)
10780Sstevel@tonic-gate 		idp->di_id = pid; /* $target = grabbed pid */
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	return (P);
10810Sstevel@tonic-gate }
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate void
10840Sstevel@tonic-gate dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10850Sstevel@tonic-gate {
10860Sstevel@tonic-gate 	dt_proc_release(dtp, P);
10870Sstevel@tonic-gate }
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate void
10900Sstevel@tonic-gate dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10910Sstevel@tonic-gate {
10920Sstevel@tonic-gate 	dt_proc_continue(dtp, P);
10930Sstevel@tonic-gate }
1094