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