17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5657b1f3dSraf  * Common Development and Distribution License (the "License").
6657b1f3dSraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21900524f3Sahl 
227c478bd9Sstevel@tonic-gate /*
2353f3aea0SRoger A. Faulkner  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27e5803b76SAdam H. Leventhal /*
28e5803b76SAdam H. Leventhal  * Copyright (c) 2012 by Delphix. All rights reserved.
29e5803b76SAdam H. Leventhal  */
30e5803b76SAdam H. Leventhal 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * DTrace Process Control
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * This file provides a set of routines that permit libdtrace and its clients
357c478bd9Sstevel@tonic-gate  * to create and grab process handles using libproc, and to share these handles
367c478bd9Sstevel@tonic-gate  * between library mechanisms that need libproc access, such as ustack(), and
37*bbf21555SRichard Lowe  * client mechanisms that need libproc access, such as dtrace(8) -c and -p.
387c478bd9Sstevel@tonic-gate  * The library provides several mechanisms in the libproc control layer:
397c478bd9Sstevel@tonic-gate  *
407c478bd9Sstevel@tonic-gate  * Reference Counting: The library code and client code can independently grab
417c478bd9Sstevel@tonic-gate  * the same process handles without interfering with one another.  Only when
427c478bd9Sstevel@tonic-gate  * the reference count drops to zero and the handle is not being cached (see
437c478bd9Sstevel@tonic-gate  * below for more information on caching) will Prelease() be called on it.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
467c478bd9Sstevel@tonic-gate  * the reference count drops to zero, the handle is not immediately released.
477c478bd9Sstevel@tonic-gate  * Instead, libproc handles are maintained on dph_lrulist in order from most-
487c478bd9Sstevel@tonic-gate  * recently accessed to least-recently accessed.  Idle handles are maintained
497c478bd9Sstevel@tonic-gate  * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
507c478bd9Sstevel@tonic-gate  * to ustack() to avoid the overhead of releasing and re-grabbing processes.
517c478bd9Sstevel@tonic-gate  *
527c478bd9Sstevel@tonic-gate  * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
537c478bd9Sstevel@tonic-gate  * or created by dt_proc_create(), a control thread is created to provide
547c478bd9Sstevel@tonic-gate  * callbacks on process exit and symbol table caching on dlopen()s.
557c478bd9Sstevel@tonic-gate  *
567c478bd9Sstevel@tonic-gate  * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
577c478bd9Sstevel@tonic-gate  * are provided to synchronize access to the libproc handle between libdtrace
587c478bd9Sstevel@tonic-gate  * code and client code and the control thread's use of the ps_prochandle.
597c478bd9Sstevel@tonic-gate  *
607c478bd9Sstevel@tonic-gate  * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
617c478bd9Sstevel@tonic-gate  * dtrace_proc_grab/dtrace_proc_create mechanisms.  Like all exported libdtrace
627c478bd9Sstevel@tonic-gate  * calls, these are assumed to be MT-Unsafe.  MT-Safety is ONLY provided for
637c478bd9Sstevel@tonic-gate  * synchronization between libdtrace control threads and the client thread.
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * The ps_prochandles themselves are maintained along with a dt_proc_t struct
667c478bd9Sstevel@tonic-gate  * in a hash table indexed by PID.  This provides basic locking and reference
677c478bd9Sstevel@tonic-gate  * counting.  The dt_proc_t is also maintained in LRU order on dph_lrulist.
687c478bd9Sstevel@tonic-gate  * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
697c478bd9Sstevel@tonic-gate  * the current limit on the number of actively cached entries.
707c478bd9Sstevel@tonic-gate  *
717c478bd9Sstevel@tonic-gate  * The control thread for a process establishes breakpoints at the rtld_db
727c478bd9Sstevel@tonic-gate  * locations of interest, updates mappings and symbol tables at these points,
737c478bd9Sstevel@tonic-gate  * and handles exec and fork (by always following the parent).  The control
747c478bd9Sstevel@tonic-gate  * thread automatically exits when the process dies or control is lost.
757c478bd9Sstevel@tonic-gate  *
767c478bd9Sstevel@tonic-gate  * A simple notification mechanism is provided for libdtrace clients using
777c478bd9Sstevel@tonic-gate  * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events.  If
787c478bd9Sstevel@tonic-gate  * such an event occurs, the dt_proc_t itself is enqueued on a notification
797c478bd9Sstevel@tonic-gate  * list and the control thread broadcasts to dph_cv.  dtrace_sleep() will wake
807c478bd9Sstevel@tonic-gate  * up using this condition and will then call the client handler as necessary.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #include <sys/wait.h>
847c478bd9Sstevel@tonic-gate #include <sys/lwp.h>
857c478bd9Sstevel@tonic-gate #include <strings.h>
867c478bd9Sstevel@tonic-gate #include <signal.h>
877c478bd9Sstevel@tonic-gate #include <assert.h>
887c478bd9Sstevel@tonic-gate #include <errno.h>
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate #include <dt_proc.h>
917c478bd9Sstevel@tonic-gate #include <dt_pid.h>
927c478bd9Sstevel@tonic-gate #include <dt_impl.h>
937c478bd9Sstevel@tonic-gate 
948fd04b83SRoger A. Faulkner #define	IS_SYS_EXEC(w)	(w == SYS_execve)
958fd04b83SRoger A. Faulkner #define	IS_SYS_FORK(w)	(w == SYS_vfork || w == SYS_forksys)
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate static dt_bkpt_t *
dt_proc_bpcreate(dt_proc_t * dpr,uintptr_t addr,dt_bkpt_f * func,void * data)987c478bd9Sstevel@tonic-gate dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
997c478bd9Sstevel@tonic-gate {
1007c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
1017c478bd9Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1027c478bd9Sstevel@tonic-gate 
10353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 	if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
1067c478bd9Sstevel@tonic-gate 		dbp->dbp_func = func;
1077c478bd9Sstevel@tonic-gate 		dbp->dbp_data = data;
1087c478bd9Sstevel@tonic-gate 		dbp->dbp_addr = addr;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
1117c478bd9Sstevel@tonic-gate 			dbp->dbp_active = B_TRUE;
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 		dt_list_append(&dpr->dpr_bps, dbp);
1147c478bd9Sstevel@tonic-gate 	}
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	return (dbp);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate static void
dt_proc_bpdestroy(dt_proc_t * dpr,int delbkpts)1207c478bd9Sstevel@tonic-gate dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
1217c478bd9Sstevel@tonic-gate {
1227c478bd9Sstevel@tonic-gate 	int state = Pstate(dpr->dpr_proc);
1237c478bd9Sstevel@tonic-gate 	dt_bkpt_t *dbp, *nbp;
1247c478bd9Sstevel@tonic-gate 
12553f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
1287c478bd9Sstevel@tonic-gate 		if (delbkpts && dbp->dbp_active &&
1297c478bd9Sstevel@tonic-gate 		    state != PS_LOST && state != PS_UNDEAD) {
1307c478bd9Sstevel@tonic-gate 			(void) Pdelbkpt(dpr->dpr_proc,
1317c478bd9Sstevel@tonic-gate 			    dbp->dbp_addr, dbp->dbp_instr);
1327c478bd9Sstevel@tonic-gate 		}
1337c478bd9Sstevel@tonic-gate 		nbp = dt_list_next(dbp);
1347c478bd9Sstevel@tonic-gate 		dt_list_delete(&dpr->dpr_bps, dbp);
1357c478bd9Sstevel@tonic-gate 		dt_free(dpr->dpr_hdl, dbp);
1367c478bd9Sstevel@tonic-gate 	}
1377c478bd9Sstevel@tonic-gate }
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate static void
dt_proc_bpmatch(dtrace_hdl_t * dtp,dt_proc_t * dpr)1407c478bd9Sstevel@tonic-gate dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
1417c478bd9Sstevel@tonic-gate {
1427c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
1437c478bd9Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1447c478bd9Sstevel@tonic-gate 
14553f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1487c478bd9Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1497c478bd9Sstevel@tonic-gate 		if (psp->pr_reg[R_PC] == dbp->dbp_addr)
1507c478bd9Sstevel@tonic-gate 			break;
1517c478bd9Sstevel@tonic-gate 	}
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	if (dbp == NULL) {
1547c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
1557c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
1567c478bd9Sstevel@tonic-gate 		return;
1577c478bd9Sstevel@tonic-gate 	}
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate 	dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
1607c478bd9Sstevel@tonic-gate 	    (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	dbp->dbp_func(dtp, dpr, dbp->dbp_data);
1637c478bd9Sstevel@tonic-gate 	(void) Pxecbkpt(dpr->dpr_proc, dbp->dbp_instr);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
166dcafa303Sahl static void
dt_proc_bpenable(dt_proc_t * dpr)1677c478bd9Sstevel@tonic-gate dt_proc_bpenable(dt_proc_t *dpr)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1707c478bd9Sstevel@tonic-gate 
17153f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1747c478bd9Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1757c478bd9Sstevel@tonic-gate 		if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
1767c478bd9Sstevel@tonic-gate 		    dbp->dbp_addr, &dbp->dbp_instr) == 0)
1777c478bd9Sstevel@tonic-gate 			dbp->dbp_active = B_TRUE;
1787c478bd9Sstevel@tonic-gate 	}
179900524f3Sahl 
180900524f3Sahl 	dt_dprintf("breakpoints enabled\n");
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
183dcafa303Sahl static void
dt_proc_bpdisable(dt_proc_t * dpr)1847c478bd9Sstevel@tonic-gate dt_proc_bpdisable(dt_proc_t *dpr)
1857c478bd9Sstevel@tonic-gate {
1867c478bd9Sstevel@tonic-gate 	dt_bkpt_t *dbp;
1877c478bd9Sstevel@tonic-gate 
18853f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	for (dbp = dt_list_next(&dpr->dpr_bps);
1917c478bd9Sstevel@tonic-gate 	    dbp != NULL; dbp = dt_list_next(dbp)) {
1927c478bd9Sstevel@tonic-gate 		if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
1937c478bd9Sstevel@tonic-gate 		    dbp->dbp_addr, dbp->dbp_instr) == 0)
1947c478bd9Sstevel@tonic-gate 			dbp->dbp_active = B_FALSE;
1957c478bd9Sstevel@tonic-gate 	}
196900524f3Sahl 
197900524f3Sahl 	dt_dprintf("breakpoints disabled\n");
198900524f3Sahl }
199900524f3Sahl 
200900524f3Sahl static void
dt_proc_notify(dtrace_hdl_t * dtp,dt_proc_hash_t * dph,dt_proc_t * dpr,const char * msg)201900524f3Sahl dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
202900524f3Sahl     const char *msg)
203900524f3Sahl {
204900524f3Sahl 	dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
205900524f3Sahl 
206900524f3Sahl 	if (dprn == NULL) {
207900524f3Sahl 		dt_dprintf("failed to allocate notification for %d %s\n",
208900524f3Sahl 		    (int)dpr->dpr_pid, msg);
209900524f3Sahl 	} else {
210900524f3Sahl 		dprn->dprn_dpr = dpr;
211900524f3Sahl 		if (msg == NULL)
212900524f3Sahl 			dprn->dprn_errmsg[0] = '\0';
213900524f3Sahl 		else
214900524f3Sahl 			(void) strlcpy(dprn->dprn_errmsg, msg,
215900524f3Sahl 			    sizeof (dprn->dprn_errmsg));
216900524f3Sahl 
217900524f3Sahl 		(void) pthread_mutex_lock(&dph->dph_lock);
218900524f3Sahl 
219900524f3Sahl 		dprn->dprn_next = dph->dph_notify;
220900524f3Sahl 		dph->dph_notify = dprn;
221900524f3Sahl 
222900524f3Sahl 		(void) pthread_cond_broadcast(&dph->dph_cv);
223900524f3Sahl 		(void) pthread_mutex_unlock(&dph->dph_lock);
224900524f3Sahl 	}
2257c478bd9Sstevel@tonic-gate }
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate /*
2287c478bd9Sstevel@tonic-gate  * Check to see if the control thread was requested to stop when the victim
2297c478bd9Sstevel@tonic-gate  * process reached a particular event (why) rather than continuing the victim.
2307c478bd9Sstevel@tonic-gate  * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
2317c478bd9Sstevel@tonic-gate  * If 'why' is not set, this function returns immediately and does nothing.
2327c478bd9Sstevel@tonic-gate  */
2337c478bd9Sstevel@tonic-gate static void
dt_proc_stop(dt_proc_t * dpr,uint8_t why)2347c478bd9Sstevel@tonic-gate dt_proc_stop(dt_proc_t *dpr, uint8_t why)
2357c478bd9Sstevel@tonic-gate {
23653f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
2377c478bd9Sstevel@tonic-gate 	assert(why != DT_PROC_STOP_IDLE);
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	if (dpr->dpr_stop & why) {
2407c478bd9Sstevel@tonic-gate 		dpr->dpr_stop |= DT_PROC_STOP_IDLE;
2417c478bd9Sstevel@tonic-gate 		dpr->dpr_stop &= ~why;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
2447c478bd9Sstevel@tonic-gate 
245dcafa303Sahl 		/*
246dcafa303Sahl 		 * We disable breakpoints while stopped to preserve the
247dcafa303Sahl 		 * integrity of the program text for both our own disassembly
248dcafa303Sahl 		 * and that of the kernel.
249dcafa303Sahl 		 */
250dcafa303Sahl 		dt_proc_bpdisable(dpr);
251dcafa303Sahl 
2527c478bd9Sstevel@tonic-gate 		while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
2537c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
254dcafa303Sahl 
255dcafa303Sahl 		dt_proc_bpenable(dpr);
2567c478bd9Sstevel@tonic-gate 	}
2577c478bd9Sstevel@tonic-gate }
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate /*ARGSUSED*/
2607c478bd9Sstevel@tonic-gate static void
dt_proc_bpmain(dtrace_hdl_t * dtp,dt_proc_t * dpr,const char * fname)2617c478bd9Sstevel@tonic-gate dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 	dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
2647c478bd9Sstevel@tonic-gate 	dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate static void
dt_proc_rdevent(dtrace_hdl_t * dtp,dt_proc_t * dpr,const char * evname)2687c478bd9Sstevel@tonic-gate dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
2697c478bd9Sstevel@tonic-gate {
2707c478bd9Sstevel@tonic-gate 	rd_event_msg_t rdm;
2717c478bd9Sstevel@tonic-gate 	rd_err_e err;
2727c478bd9Sstevel@tonic-gate 
2737c478bd9Sstevel@tonic-gate 	if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
2747c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to get %s event message: %s\n",
2757c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
2767c478bd9Sstevel@tonic-gate 		return;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
2807c478bd9Sstevel@tonic-gate 	    (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	switch (rdm.type) {
2837c478bd9Sstevel@tonic-gate 	case RD_DLACTIVITY:
284900524f3Sahl 		if (rdm.u.state != RD_CONSISTENT)
285900524f3Sahl 			break;
286900524f3Sahl 
287900524f3Sahl 		Pupdate_syms(dpr->dpr_proc);
288900524f3Sahl 		if (dt_pid_create_probes_module(dtp, dpr) != 0)
289900524f3Sahl 			dt_proc_notify(dtp, dtp->dt_procs, dpr,
290900524f3Sahl 			    dpr->dpr_errmsg);
291900524f3Sahl 
2927c478bd9Sstevel@tonic-gate 		break;
2937c478bd9Sstevel@tonic-gate 	case RD_PREINIT:
2947c478bd9Sstevel@tonic-gate 		Pupdate_syms(dpr->dpr_proc);
2957c478bd9Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 	case RD_POSTINIT:
2987c478bd9Sstevel@tonic-gate 		Pupdate_syms(dpr->dpr_proc);
2997c478bd9Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
3007c478bd9Sstevel@tonic-gate 		break;
3017c478bd9Sstevel@tonic-gate 	}
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static void
dt_proc_rdwatch(dt_proc_t * dpr,rd_event_e event,const char * evname)3057c478bd9Sstevel@tonic-gate dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	rd_notify_t rdn;
3087c478bd9Sstevel@tonic-gate 	rd_err_e err;
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
3117c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to get event address for %s: %s\n",
3127c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
3137c478bd9Sstevel@tonic-gate 		return;
3147c478bd9Sstevel@tonic-gate 	}
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if (rdn.type != RD_NOTIFY_BPT) {
3177c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: event %s has unexpected type %d\n",
3187c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, evname, rdn.type);
3197c478bd9Sstevel@tonic-gate 		return;
3207c478bd9Sstevel@tonic-gate 	}
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	(void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
3237c478bd9Sstevel@tonic-gate 	    (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate /*
3277c478bd9Sstevel@tonic-gate  * Common code for enabling events associated with the run-time linker after
3287c478bd9Sstevel@tonic-gate  * attaching to a process or after a victim process completes an exec(2).
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate static void
dt_proc_attach(dt_proc_t * dpr,int exec)3317c478bd9Sstevel@tonic-gate dt_proc_attach(dt_proc_t *dpr, int exec)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	const pstatus_t *psp = Pstatus(dpr->dpr_proc);
3347c478bd9Sstevel@tonic-gate 	rd_err_e err;
3357c478bd9Sstevel@tonic-gate 	GElf_Sym sym;
3367c478bd9Sstevel@tonic-gate 
33753f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	if (exec) {
3407c478bd9Sstevel@tonic-gate 		if (psp->pr_lwp.pr_errno != 0)
3417c478bd9Sstevel@tonic-gate 			return; /* exec failed: nothing needs to be done */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 		dt_proc_bpdestroy(dpr, B_FALSE);
3447c478bd9Sstevel@tonic-gate 		Preset_maps(dpr->dpr_proc);
3457c478bd9Sstevel@tonic-gate 	}
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
3487c478bd9Sstevel@tonic-gate 	    (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
3497c478bd9Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
3507c478bd9Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
3517c478bd9Sstevel@tonic-gate 		dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
3527c478bd9Sstevel@tonic-gate 	} else {
3537c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to enable rtld events: %s\n",
3547c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
3557c478bd9Sstevel@tonic-gate 		    "rtld_db agent initialization failed");
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	Pupdate_maps(dpr->dpr_proc);
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate 	if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
3617c478bd9Sstevel@tonic-gate 	    "a.out", "main", &sym, NULL) == 0) {
3627c478bd9Sstevel@tonic-gate 		(void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
3637c478bd9Sstevel@tonic-gate 		    (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
3647c478bd9Sstevel@tonic-gate 	} else {
3657c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to find a.out`main: %s\n",
3667c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(errno));
3677c478bd9Sstevel@tonic-gate 	}
3687c478bd9Sstevel@tonic-gate }
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate /*
3717c478bd9Sstevel@tonic-gate  * Wait for a stopped process to be set running again by some other debugger.
3727c478bd9Sstevel@tonic-gate  * This is typically not required by /proc-based debuggers, since the usual
3737c478bd9Sstevel@tonic-gate  * model is that one debugger controls one victim.  But DTrace, as usual, has
3747c478bd9Sstevel@tonic-gate  * its own needs: the stop() action assumes that prun(1) or some other tool
3757c478bd9Sstevel@tonic-gate  * will be applied to resume the victim process.  This could be solved by
3767c478bd9Sstevel@tonic-gate  * adding a PCWRUN directive to /proc, but that seems like overkill unless
3777c478bd9Sstevel@tonic-gate  * other debuggers end up needing this functionality, so we implement a cheap
3787c478bd9Sstevel@tonic-gate  * equivalent to PCWRUN using the set of existing kernel mechanisms.
3797c478bd9Sstevel@tonic-gate  *
3807c478bd9Sstevel@tonic-gate  * Our intent is really not just to wait for the victim to run, but rather to
3817c478bd9Sstevel@tonic-gate  * wait for it to run and then stop again for a reason other than the current
3827c478bd9Sstevel@tonic-gate  * PR_REQUESTED stop.  Since PCWSTOP/Pstopstatus() can be applied repeatedly
3837c478bd9Sstevel@tonic-gate  * to a stopped process and will return the same result without affecting the
3847c478bd9Sstevel@tonic-gate  * victim, we can just perform these operations repeatedly until Pstate()
3857c478bd9Sstevel@tonic-gate  * changes, the representative LWP ID changes, or the stop timestamp advances.
3867c478bd9Sstevel@tonic-gate  * dt_proc_control() will then rediscover the new state and continue as usual.
3877c478bd9Sstevel@tonic-gate  * When the process is still stopped in the same exact state, we sleep for a
3887c478bd9Sstevel@tonic-gate  * brief interval before waiting again so as not to spin consuming CPU cycles.
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate static void
dt_proc_waitrun(dt_proc_t * dpr)3917c478bd9Sstevel@tonic-gate dt_proc_waitrun(dt_proc_t *dpr)
3927c478bd9Sstevel@tonic-gate {
3937c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
3947c478bd9Sstevel@tonic-gate 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
3977c478bd9Sstevel@tonic-gate 	timestruc_t tstamp = psp->pr_tstamp;
3987c478bd9Sstevel@tonic-gate 	lwpid_t lwpid = psp->pr_lwpid;
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate 	const long wstop = PCWSTOP;
4017c478bd9Sstevel@tonic-gate 	int pfd = Pctlfd(P);
4027c478bd9Sstevel@tonic-gate 
40353f3aea0SRoger A. Faulkner 	assert(MUTEX_HELD(&dpr->dpr_lock));
4047c478bd9Sstevel@tonic-gate 	assert(psp->pr_flags & PR_STOPPED);
4057c478bd9Sstevel@tonic-gate 	assert(Pstate(P) == PS_STOP);
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 	/*
4087c478bd9Sstevel@tonic-gate 	 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
4097c478bd9Sstevel@tonic-gate 	 * so that if the libdtrace client is killed, the victim stays stopped.
4107c478bd9Sstevel@tonic-gate 	 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
4117c478bd9Sstevel@tonic-gate 	 */
4127c478bd9Sstevel@tonic-gate 	(void) Punsetflags(P, krflag);
4137c478bd9Sstevel@tonic-gate 	Psync(P);
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	while (!dpr->dpr_quit) {
4187c478bd9Sstevel@tonic-gate 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
4197c478bd9Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
4227c478bd9Sstevel@tonic-gate 		(void) Pstopstatus(P, PCNULL, 0);
4237c478bd9Sstevel@tonic-gate 		psp = &Pstatus(P)->pr_lwp;
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 		/*
4267c478bd9Sstevel@tonic-gate 		 * If we've reached a new state, found a new representative, or
4277c478bd9Sstevel@tonic-gate 		 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
4287c478bd9Sstevel@tonic-gate 		 * original setting and then return with dpr_lock held.
4297c478bd9Sstevel@tonic-gate 		 */
4307c478bd9Sstevel@tonic-gate 		if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
4317c478bd9Sstevel@tonic-gate 		    bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
4327c478bd9Sstevel@tonic-gate 			(void) Psetflags(P, krflag);
4337c478bd9Sstevel@tonic-gate 			Psync(P);
4347c478bd9Sstevel@tonic-gate 			return;
4357c478bd9Sstevel@tonic-gate 		}
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
4387c478bd9Sstevel@tonic-gate 		(void) poll(NULL, 0, MILLISEC / 2);
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate 
4417c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate typedef struct dt_proc_control_data {
4457c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dpcd_hdl;			/* DTrace handle */
4467c478bd9Sstevel@tonic-gate 	dt_proc_t *dpcd_proc;			/* proccess to control */
4477c478bd9Sstevel@tonic-gate } dt_proc_control_data_t;
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate /*
4507c478bd9Sstevel@tonic-gate  * Main loop for all victim process control threads.  We initialize all the
4517c478bd9Sstevel@tonic-gate  * appropriate /proc control mechanisms, and then enter a loop waiting for
4527c478bd9Sstevel@tonic-gate  * the process to stop on an event or die.  We process any events by calling
4537c478bd9Sstevel@tonic-gate  * appropriate subroutines, and exit when the victim dies or we lose control.
4547c478bd9Sstevel@tonic-gate  *
4557c478bd9Sstevel@tonic-gate  * The control thread synchronizes the use of dpr_proc with other libdtrace
4567c478bd9Sstevel@tonic-gate  * threads using dpr_lock.  We hold the lock for all of our operations except
4577c478bd9Sstevel@tonic-gate  * waiting while the process is running: this is accomplished by writing a
4587c478bd9Sstevel@tonic-gate  * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.  If the
4597c478bd9Sstevel@tonic-gate  * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
4607c478bd9Sstevel@tonic-gate  */
4617c478bd9Sstevel@tonic-gate static void *
dt_proc_control(void * arg)4627c478bd9Sstevel@tonic-gate dt_proc_control(void *arg)
4637c478bd9Sstevel@tonic-gate {
4647c478bd9Sstevel@tonic-gate 	dt_proc_control_data_t *datap = arg;
4657c478bd9Sstevel@tonic-gate 	dtrace_hdl_t *dtp = datap->dpcd_hdl;
4667c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr = datap->dpcd_proc;
467e5803b76SAdam H. Leventhal 	dt_proc_hash_t *dph = dtp->dt_procs;
4687c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = dpr->dpr_proc;
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	int pfd = Pctlfd(P);
4717c478bd9Sstevel@tonic-gate 	int pid = dpr->dpr_pid;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	const long wstop = PCWSTOP;
4747c478bd9Sstevel@tonic-gate 	int notify = B_FALSE;
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate 	/*
4777c478bd9Sstevel@tonic-gate 	 * We disable the POSIX thread cancellation mechanism so that the
4787c478bd9Sstevel@tonic-gate 	 * client program using libdtrace can't accidentally cancel our thread.
4797c478bd9Sstevel@tonic-gate 	 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
4807c478bd9Sstevel@tonic-gate 	 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
4817c478bd9Sstevel@tonic-gate 	 */
4827c478bd9Sstevel@tonic-gate 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	/*
4857c478bd9Sstevel@tonic-gate 	 * Set up the corresponding process for tracing by libdtrace.  We want
4867c478bd9Sstevel@tonic-gate 	 * to be able to catch breakpoints and efficiently single-step over
4877c478bd9Sstevel@tonic-gate 	 * them, and we need to enable librtld_db to watch libdl activity.
4887c478bd9Sstevel@tonic-gate 	 */
4897c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	(void) Punsetflags(P, PR_ASYNC);	/* require synchronous mode */
4927c478bd9Sstevel@tonic-gate 	(void) Psetflags(P, PR_BPTADJ);		/* always adjust eip on x86 */
4937c478bd9Sstevel@tonic-gate 	(void) Punsetflags(P, PR_FORK);		/* do not inherit on fork */
4947c478bd9Sstevel@tonic-gate 
4957c478bd9Sstevel@tonic-gate 	(void) Pfault(P, FLTBPT, B_TRUE);	/* always trace breakpoints */
4967c478bd9Sstevel@tonic-gate 	(void) Pfault(P, FLTTRACE, B_TRUE);	/* always trace single-step */
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	/*
4997c478bd9Sstevel@tonic-gate 	 * We must trace exit from exec() system calls so that if the exec is
5007c478bd9Sstevel@tonic-gate 	 * successful, we can reset our breakpoints and re-initialize libproc.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, SYS_execve, B_TRUE);
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/*
5057c478bd9Sstevel@tonic-gate 	 * We must trace entry and exit for fork() system calls in order to
5067c478bd9Sstevel@tonic-gate 	 * disable our breakpoints temporarily during the fork.  We do not set
5077c478bd9Sstevel@tonic-gate 	 * the PR_FORK flag, so if fork succeeds the child begins executing and
5087c478bd9Sstevel@tonic-gate 	 * does not inherit any other tracing behaviors or a control thread.
5097c478bd9Sstevel@tonic-gate 	 */
5107c478bd9Sstevel@tonic-gate 	(void) Psysentry(P, SYS_vfork, B_TRUE);
5117c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, SYS_vfork, B_TRUE);
512657b1f3dSraf 	(void) Psysentry(P, SYS_forksys, B_TRUE);
513657b1f3dSraf 	(void) Psysexit(P, SYS_forksys, B_TRUE);
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 	Psync(P);				/* enable all /proc changes */
5167c478bd9Sstevel@tonic-gate 	dt_proc_attach(dpr, B_FALSE);		/* enable rtld breakpoints */
5177c478bd9Sstevel@tonic-gate 
5187c478bd9Sstevel@tonic-gate 	/*
5197c478bd9Sstevel@tonic-gate 	 * If PR_KLC is set, we created the process; otherwise we grabbed it.
5207c478bd9Sstevel@tonic-gate 	 * Check for an appropriate stop request and wait for dt_proc_continue.
5217c478bd9Sstevel@tonic-gate 	 */
5227c478bd9Sstevel@tonic-gate 	if (Pstatus(P)->pr_flags & PR_KLC)
5237c478bd9Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
5247c478bd9Sstevel@tonic-gate 	else
5257c478bd9Sstevel@tonic-gate 		dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (Psetrun(P, 0, 0) == -1) {
5287c478bd9Sstevel@tonic-gate 		dt_dprintf("pid %d: failed to set running: %s\n",
5297c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(errno));
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
5337c478bd9Sstevel@tonic-gate 
5347c478bd9Sstevel@tonic-gate 	/*
5357c478bd9Sstevel@tonic-gate 	 * Wait for the process corresponding to this control thread to stop,
5367c478bd9Sstevel@tonic-gate 	 * process the event, and then set it running again.  We want to sleep
5377c478bd9Sstevel@tonic-gate 	 * with dpr_lock *unheld* so that other parts of libdtrace can use the
5387c478bd9Sstevel@tonic-gate 	 * ps_prochandle in the meantime (e.g. ustack()).  To do this, we write
5397c478bd9Sstevel@tonic-gate 	 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
5407c478bd9Sstevel@tonic-gate 	 * Once the process stops, we wake up, grab dpr_lock, and then call
5417c478bd9Sstevel@tonic-gate 	 * Pwait() (which will return immediately) and do our processing.
5427c478bd9Sstevel@tonic-gate 	 */
5437c478bd9Sstevel@tonic-gate 	while (!dpr->dpr_quit) {
5447c478bd9Sstevel@tonic-gate 		const lwpstatus_t *psp;
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
5477c478bd9Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
5507c478bd9Sstevel@tonic-gate pwait_locked:
5517c478bd9Sstevel@tonic-gate 		if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
5527c478bd9Sstevel@tonic-gate 			(void) pthread_mutex_unlock(&dpr->dpr_lock);
5537c478bd9Sstevel@tonic-gate 			continue; /* check dpr_quit and continue waiting */
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 		switch (Pstate(P)) {
5577c478bd9Sstevel@tonic-gate 		case PS_STOP:
5587c478bd9Sstevel@tonic-gate 			psp = &Pstatus(P)->pr_lwp;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 			dt_dprintf("pid %d: proc stopped showing %d/%d\n",
5617c478bd9Sstevel@tonic-gate 			    pid, psp->pr_why, psp->pr_what);
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 			/*
5647c478bd9Sstevel@tonic-gate 			 * If the process stops showing PR_REQUESTED, then the
5657c478bd9Sstevel@tonic-gate 			 * DTrace stop() action was applied to it or another
5667c478bd9Sstevel@tonic-gate 			 * debugging utility (e.g. pstop(1)) asked it to stop.
5677c478bd9Sstevel@tonic-gate 			 * In either case, the user's intention is for the
5687c478bd9Sstevel@tonic-gate 			 * process to remain stopped until another external
5697c478bd9Sstevel@tonic-gate 			 * mechanism (e.g. prun(1)) is applied.  So instead of
5707c478bd9Sstevel@tonic-gate 			 * setting the process running ourself, we wait for
5717c478bd9Sstevel@tonic-gate 			 * someone else to do so.  Once that happens, we return
5727c478bd9Sstevel@tonic-gate 			 * to our normal loop waiting for an event of interest.
5737c478bd9Sstevel@tonic-gate 			 */
5747c478bd9Sstevel@tonic-gate 			if (psp->pr_why == PR_REQUESTED) {
5757c478bd9Sstevel@tonic-gate 				dt_proc_waitrun(dpr);
5767c478bd9Sstevel@tonic-gate 				(void) pthread_mutex_unlock(&dpr->dpr_lock);
5777c478bd9Sstevel@tonic-gate 				continue;
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate 			/*
5817c478bd9Sstevel@tonic-gate 			 * If the process stops showing one of the events that
5827c478bd9Sstevel@tonic-gate 			 * we are tracing, perform the appropriate response.
5837c478bd9Sstevel@tonic-gate 			 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
5847c478bd9Sstevel@tonic-gate 			 * PR_JOBCONTROL by design: if one of these conditions
5857c478bd9Sstevel@tonic-gate 			 * occurs, we will fall through to Psetrun() but the
5867c478bd9Sstevel@tonic-gate 			 * process will remain stopped in the kernel by the
5877c478bd9Sstevel@tonic-gate 			 * corresponding mechanism (e.g. job control stop).
5887c478bd9Sstevel@tonic-gate 			 */
5897c478bd9Sstevel@tonic-gate 			if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
5907c478bd9Sstevel@tonic-gate 				dt_proc_bpmatch(dtp, dpr);
5917c478bd9Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSENTRY &&
5927c478bd9Sstevel@tonic-gate 			    IS_SYS_FORK(psp->pr_what))
5937c478bd9Sstevel@tonic-gate 				dt_proc_bpdisable(dpr);
5947c478bd9Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSEXIT &&
5957c478bd9Sstevel@tonic-gate 			    IS_SYS_FORK(psp->pr_what))
5967c478bd9Sstevel@tonic-gate 				dt_proc_bpenable(dpr);
5977c478bd9Sstevel@tonic-gate 			else if (psp->pr_why == PR_SYSEXIT &&
5987c478bd9Sstevel@tonic-gate 			    IS_SYS_EXEC(psp->pr_what))
5997c478bd9Sstevel@tonic-gate 				dt_proc_attach(dpr, B_TRUE);
6007c478bd9Sstevel@tonic-gate 			break;
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 		case PS_LOST:
6037c478bd9Sstevel@tonic-gate 			if (Preopen(P) == 0)
6047c478bd9Sstevel@tonic-gate 				goto pwait_locked;
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 			dt_dprintf("pid %d: proc lost: %s\n",
6077c478bd9Sstevel@tonic-gate 			    pid, strerror(errno));
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 			dpr->dpr_quit = B_TRUE;
6107c478bd9Sstevel@tonic-gate 			notify = B_TRUE;
6117c478bd9Sstevel@tonic-gate 			break;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 		case PS_UNDEAD:
6147c478bd9Sstevel@tonic-gate 			dt_dprintf("pid %d: proc died\n", pid);
6157c478bd9Sstevel@tonic-gate 			dpr->dpr_quit = B_TRUE;
6167c478bd9Sstevel@tonic-gate 			notify = B_TRUE;
6177c478bd9Sstevel@tonic-gate 			break;
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate 		if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
6217c478bd9Sstevel@tonic-gate 			dt_dprintf("pid %d: failed to set running: %s\n",
6227c478bd9Sstevel@tonic-gate 			    (int)dpr->dpr_pid, strerror(errno));
6237c478bd9Sstevel@tonic-gate 		}
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
6267c478bd9Sstevel@tonic-gate 	}
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
6307c478bd9Sstevel@tonic-gate 	 * the dt_proc_t structure on the dt_proc_hash_t notification list.
6317c478bd9Sstevel@tonic-gate 	 */
632900524f3Sahl 	if (notify)
633900524f3Sahl 		dt_proc_notify(dtp, dph, dpr, NULL);
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	/*
6367c478bd9Sstevel@tonic-gate 	 * Destroy and remove any remaining breakpoints, set dpr_done and clear
6377c478bd9Sstevel@tonic-gate 	 * dpr_tid to indicate the control thread has exited, and notify any
6387c478bd9Sstevel@tonic-gate 	 * waiting thread in dt_proc_destroy() that we have succesfully exited.
6397c478bd9Sstevel@tonic-gate 	 */
6407c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 	dt_proc_bpdestroy(dpr, B_TRUE);
6437c478bd9Sstevel@tonic-gate 	dpr->dpr_done = B_TRUE;
6447c478bd9Sstevel@tonic-gate 	dpr->dpr_tid = 0;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	(void) pthread_cond_broadcast(&dpr->dpr_cv);
647900524f3Sahl 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	return (NULL);
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate /*PRINTFLIKE3*/
6537c478bd9Sstevel@tonic-gate static struct ps_prochandle *
dt_proc_error(dtrace_hdl_t * dtp,dt_proc_t * dpr,const char * format,...)6547c478bd9Sstevel@tonic-gate dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
6557c478bd9Sstevel@tonic-gate {
6567c478bd9Sstevel@tonic-gate 	va_list ap;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	va_start(ap, format);
6597c478bd9Sstevel@tonic-gate 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
6607c478bd9Sstevel@tonic-gate 	va_end(ap);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	if (dpr->dpr_proc != NULL)
6637c478bd9Sstevel@tonic-gate 		Prelease(dpr->dpr_proc, 0);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	dt_free(dtp, dpr);
6667c478bd9Sstevel@tonic-gate 	(void) dt_set_errno(dtp, EDT_COMPILER);
6677c478bd9Sstevel@tonic-gate 	return (NULL);
6687c478bd9Sstevel@tonic-gate }
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate dt_proc_t *
dt_proc_lookup(dtrace_hdl_t * dtp,struct ps_prochandle * P,int remove)6717c478bd9Sstevel@tonic-gate dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
6747c478bd9Sstevel@tonic-gate 	pid_t pid = Pstatus(P)->pr_pid;
6757c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 	for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
6787c478bd9Sstevel@tonic-gate 		if (dpr->dpr_pid == pid)
6797c478bd9Sstevel@tonic-gate 			break;
6807c478bd9Sstevel@tonic-gate 		else
6817c478bd9Sstevel@tonic-gate 			dpp = &dpr->dpr_hash;
6827c478bd9Sstevel@tonic-gate 	}
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 	assert(dpr != NULL);
6857c478bd9Sstevel@tonic-gate 	assert(dpr->dpr_proc == P);
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	if (remove)
6887c478bd9Sstevel@tonic-gate 		*dpp = dpr->dpr_hash; /* remove from pid hash chain */
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	return (dpr);
6917c478bd9Sstevel@tonic-gate }
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate static void
dt_proc_destroy(dtrace_hdl_t * dtp,struct ps_prochandle * P)6947c478bd9Sstevel@tonic-gate dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
6957c478bd9Sstevel@tonic-gate {
696b365acd0Sbmc 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
6977c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
698900524f3Sahl 	dt_proc_notify_t *npr, **npp;
6997c478bd9Sstevel@tonic-gate 	int rflag;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 	assert(dpr != NULL);
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate 	/*
7047c478bd9Sstevel@tonic-gate 	 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
7057c478bd9Sstevel@tonic-gate 	 * an external debugger and we were waiting in dt_proc_waitrun().
7067c478bd9Sstevel@tonic-gate 	 * Leave the process in this condition using PRELEASE_HANG.
7077c478bd9Sstevel@tonic-gate 	 */
7087c478bd9Sstevel@tonic-gate 	if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
7097c478bd9Sstevel@tonic-gate 		dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
7107c478bd9Sstevel@tonic-gate 		rflag = PRELEASE_HANG;
71158dbc507SJonathan Haslam 	} else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) {
71258dbc507SJonathan Haslam 		dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);
71358dbc507SJonathan Haslam 		rflag = PRELEASE_KILL; /* apply kill-on-last-close */
7147c478bd9Sstevel@tonic-gate 	} else {
7157c478bd9Sstevel@tonic-gate 		dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
71658dbc507SJonathan Haslam 		rflag = 0; /* apply run-on-last-close */
7177c478bd9Sstevel@tonic-gate 	}
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	if (dpr->dpr_tid) {
7207c478bd9Sstevel@tonic-gate 		/*
7217c478bd9Sstevel@tonic-gate 		 * Set the dpr_quit flag to tell the daemon thread to exit.  We
7227c478bd9Sstevel@tonic-gate 		 * send it a SIGCANCEL to poke it out of PCWSTOP or any other
7237c478bd9Sstevel@tonic-gate 		 * long-term /proc system call.  Our daemon threads have POSIX
7247c478bd9Sstevel@tonic-gate 		 * cancellation disabled, so EINTR will be the only effect.  We
7257c478bd9Sstevel@tonic-gate 		 * then wait for dpr_done to indicate the thread has exited.
7267c478bd9Sstevel@tonic-gate 		 *
7277c478bd9Sstevel@tonic-gate 		 * We can't use pthread_kill() to send SIGCANCEL because the
7287c478bd9Sstevel@tonic-gate 		 * interface forbids it and we can't use pthread_cancel()
7297c478bd9Sstevel@tonic-gate 		 * because with cancellation disabled it won't actually
7307c478bd9Sstevel@tonic-gate 		 * send SIGCANCEL to the target thread, so we use _lwp_kill()
7317c478bd9Sstevel@tonic-gate 		 * to do the job.  This is all built on evil knowledge of
7327c478bd9Sstevel@tonic-gate 		 * the details of the cancellation mechanism in libc.
7337c478bd9Sstevel@tonic-gate 		 */
7347c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_lock(&dpr->dpr_lock);
7357c478bd9Sstevel@tonic-gate 		dpr->dpr_quit = B_TRUE;
7367c478bd9Sstevel@tonic-gate 		(void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
7377c478bd9Sstevel@tonic-gate 
7381a7c1b72Smws 		/*
7391a7c1b72Smws 		 * If the process is currently idling in dt_proc_stop(), re-
7401a7c1b72Smws 		 * enable breakpoints and poke it into running again.
7411a7c1b72Smws 		 */
7421a7c1b72Smws 		if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
7431a7c1b72Smws 			dt_proc_bpenable(dpr);
7441a7c1b72Smws 			dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
7451a7c1b72Smws 			(void) pthread_cond_broadcast(&dpr->dpr_cv);
7461a7c1b72Smws 		}
7471a7c1b72Smws 
7487c478bd9Sstevel@tonic-gate 		while (!dpr->dpr_done)
7497c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
7527c478bd9Sstevel@tonic-gate 	}
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 	/*
755b365acd0Sbmc 	 * Before we free the process structure, remove this dt_proc_t from the
756b365acd0Sbmc 	 * lookup hash, and then walk the dt_proc_hash_t's notification list
757b365acd0Sbmc 	 * and remove this dt_proc_t if it is enqueued.
7587c478bd9Sstevel@tonic-gate 	 */
7597c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dph->dph_lock);
760b365acd0Sbmc 	(void) dt_proc_lookup(dtp, P, B_TRUE);
7617c478bd9Sstevel@tonic-gate 	npp = &dph->dph_notify;
7627c478bd9Sstevel@tonic-gate 
763900524f3Sahl 	while ((npr = *npp) != NULL) {
764900524f3Sahl 		if (npr->dprn_dpr == dpr) {
765900524f3Sahl 			*npp = npr->dprn_next;
766900524f3Sahl 			dt_free(dtp, npr);
767900524f3Sahl 		} else {
768900524f3Sahl 			npp = &npr->dprn_next;
769900524f3Sahl 		}
7707c478bd9Sstevel@tonic-gate 	}
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dph->dph_lock);
7737c478bd9Sstevel@tonic-gate 
7747c478bd9Sstevel@tonic-gate 	/*
7757c478bd9Sstevel@tonic-gate 	 * Remove the dt_proc_list from the LRU list, release the underlying
7767c478bd9Sstevel@tonic-gate 	 * libproc handle, and free our dt_proc_t data structure.
7777c478bd9Sstevel@tonic-gate 	 */
7787c478bd9Sstevel@tonic-gate 	if (dpr->dpr_cacheable) {
7797c478bd9Sstevel@tonic-gate 		assert(dph->dph_lrucnt != 0);
7807c478bd9Sstevel@tonic-gate 		dph->dph_lrucnt--;
7817c478bd9Sstevel@tonic-gate 	}
7827c478bd9Sstevel@tonic-gate 
7837c478bd9Sstevel@tonic-gate 	dt_list_delete(&dph->dph_lrulist, dpr);
7847c478bd9Sstevel@tonic-gate 	Prelease(dpr->dpr_proc, rflag);
7857c478bd9Sstevel@tonic-gate 	dt_free(dtp, dpr);
7867c478bd9Sstevel@tonic-gate }
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate static int
dt_proc_create_thread(dtrace_hdl_t * dtp,dt_proc_t * dpr,uint_t stop)7897c478bd9Sstevel@tonic-gate dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
7907c478bd9Sstevel@tonic-gate {
7917c478bd9Sstevel@tonic-gate 	dt_proc_control_data_t data;
7927c478bd9Sstevel@tonic-gate 	sigset_t nset, oset;
7937c478bd9Sstevel@tonic-gate 	pthread_attr_t a;
7947c478bd9Sstevel@tonic-gate 	int err;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
7977c478bd9Sstevel@tonic-gate 	dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	(void) pthread_attr_init(&a);
8007c478bd9Sstevel@tonic-gate 	(void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	(void) sigfillset(&nset);
8037c478bd9Sstevel@tonic-gate 	(void) sigdelset(&nset, SIGABRT);	/* unblocked for assert() */
8047c478bd9Sstevel@tonic-gate 	(void) sigdelset(&nset, SIGCANCEL);	/* see dt_proc_destroy() */
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	data.dpcd_hdl = dtp;
8077c478bd9Sstevel@tonic-gate 	data.dpcd_proc = dpr;
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
8107c478bd9Sstevel@tonic-gate 	err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
8117c478bd9Sstevel@tonic-gate 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	/*
8147c478bd9Sstevel@tonic-gate 	 * If the control thread was created, then wait on dpr_cv for either
8157c478bd9Sstevel@tonic-gate 	 * dpr_done to be set (the victim died or the control thread failed)
8167c478bd9Sstevel@tonic-gate 	 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
8177c478bd9Sstevel@tonic-gate 	 * stopped by /proc and the control thread is at the rendezvous event.
8187c478bd9Sstevel@tonic-gate 	 * On success, we return with the process and control thread stopped:
8197c478bd9Sstevel@tonic-gate 	 * the caller can then apply dt_proc_continue() to resume both.
8207c478bd9Sstevel@tonic-gate 	 */
8217c478bd9Sstevel@tonic-gate 	if (err == 0) {
8227c478bd9Sstevel@tonic-gate 		while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
8237c478bd9Sstevel@tonic-gate 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 		/*
8267c478bd9Sstevel@tonic-gate 		 * If dpr_done is set, the control thread aborted before it
8277c478bd9Sstevel@tonic-gate 		 * reached the rendezvous event.  This is either due to PS_LOST
8287c478bd9Sstevel@tonic-gate 		 * or PS_UNDEAD (i.e. the process died).  We try to provide a
8297c478bd9Sstevel@tonic-gate 		 * small amount of useful information to help figure it out.
8307c478bd9Sstevel@tonic-gate 		 */
8317c478bd9Sstevel@tonic-gate 		if (dpr->dpr_done) {
8327c478bd9Sstevel@tonic-gate 			const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
8337c478bd9Sstevel@tonic-gate 			int stat = prp ? prp->pr_wstat : 0;
8347c478bd9Sstevel@tonic-gate 			int pid = dpr->dpr_pid;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 			if (Pstate(dpr->dpr_proc) == PS_LOST) {
8377c478bd9Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8387c478bd9Sstevel@tonic-gate 				    "failed to control pid %d: process exec'd "
8397c478bd9Sstevel@tonic-gate 				    "set-id or unobservable program\n", pid);
8407c478bd9Sstevel@tonic-gate 			} else if (WIFSIGNALED(stat)) {
8417c478bd9Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8427c478bd9Sstevel@tonic-gate 				    "failed to control pid %d: process died "
8437c478bd9Sstevel@tonic-gate 				    "from signal %d\n", pid, WTERMSIG(stat));
8447c478bd9Sstevel@tonic-gate 			} else {
8457c478bd9Sstevel@tonic-gate 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
8467c478bd9Sstevel@tonic-gate 				    "failed to control pid %d: process exited "
8477c478bd9Sstevel@tonic-gate 				    "with status %d\n", pid, WEXITSTATUS(stat));
8487c478bd9Sstevel@tonic-gate 			}
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 			err = ESRCH; /* cause grab() or create() to fail */
8517c478bd9Sstevel@tonic-gate 		}
8527c478bd9Sstevel@tonic-gate 	} else {
8537c478bd9Sstevel@tonic-gate 		(void) dt_proc_error(dpr->dpr_hdl, dpr,
8547c478bd9Sstevel@tonic-gate 		    "failed to create control thread for process-id %d: %s\n",
8557c478bd9Sstevel@tonic-gate 		    (int)dpr->dpr_pid, strerror(err));
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
8597c478bd9Sstevel@tonic-gate 	(void) pthread_attr_destroy(&a);
8607c478bd9Sstevel@tonic-gate 
8617c478bd9Sstevel@tonic-gate 	return (err);
8627c478bd9Sstevel@tonic-gate }
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate struct ps_prochandle *
dt_proc_create(dtrace_hdl_t * dtp,const char * file,char * const * argv)8657c478bd9Sstevel@tonic-gate dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
8667c478bd9Sstevel@tonic-gate {
8677c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
8687c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr;
8697c478bd9Sstevel@tonic-gate 	int err;
8707c478bd9Sstevel@tonic-gate 
8717c478bd9Sstevel@tonic-gate 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
8727c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
8757c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
8767c478bd9Sstevel@tonic-gate 
877e5803b76SAdam H. Leventhal 	dpr->dpr_proc = Pxcreate(file, argv, dtp->dt_proc_env, &err, NULL, 0);
878e5803b76SAdam H. Leventhal 	if (dpr->dpr_proc == NULL) {
8797c478bd9Sstevel@tonic-gate 		return (dt_proc_error(dtp, dpr,
8807c478bd9Sstevel@tonic-gate 		    "failed to execute %s: %s\n", file, Pcreate_error(err)));
8817c478bd9Sstevel@tonic-gate 	}
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	dpr->dpr_hdl = dtp;
8847c478bd9Sstevel@tonic-gate 	dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	(void) Punsetflags(dpr->dpr_proc, PR_RLC);
8877c478bd9Sstevel@tonic-gate 	(void) Psetflags(dpr->dpr_proc, PR_KLC);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
8907c478bd9Sstevel@tonic-gate 		return (NULL); /* dt_proc_error() has been called for us */
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
8937c478bd9Sstevel@tonic-gate 	dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
8947c478bd9Sstevel@tonic-gate 	dt_list_prepend(&dph->dph_lrulist, dpr);
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
8977c478bd9Sstevel@tonic-gate 	dpr->dpr_refs++;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	return (dpr->dpr_proc);
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
9027c478bd9Sstevel@tonic-gate struct ps_prochandle *
dt_proc_grab(dtrace_hdl_t * dtp,pid_t pid,int flags,int nomonitor)9037c478bd9Sstevel@tonic-gate dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
9067c478bd9Sstevel@tonic-gate 	uint_t h = pid & (dph->dph_hashlen - 1);
9077c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr, *opr;
9087c478bd9Sstevel@tonic-gate 	int err;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	/*
9117c478bd9Sstevel@tonic-gate 	 * Search the hash table for the pid.  If it is already grabbed or
9127c478bd9Sstevel@tonic-gate 	 * created, move the handle to the front of the lrulist, increment
9137c478bd9Sstevel@tonic-gate 	 * the reference count, and return the existing ps_prochandle.
9147c478bd9Sstevel@tonic-gate 	 */
9157c478bd9Sstevel@tonic-gate 	for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
9167c478bd9Sstevel@tonic-gate 		if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
9177c478bd9Sstevel@tonic-gate 			/*
9187c478bd9Sstevel@tonic-gate 			 * If the cached handle was opened read-only and
9197c478bd9Sstevel@tonic-gate 			 * this request is for a writeable handle, mark
9207c478bd9Sstevel@tonic-gate 			 * the cached handle as stale and open a new handle.
9217c478bd9Sstevel@tonic-gate 			 * Since it's stale, unmark it as cacheable.
9227c478bd9Sstevel@tonic-gate 			 */
9237c478bd9Sstevel@tonic-gate 			if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
9247c478bd9Sstevel@tonic-gate 				dt_dprintf("upgrading pid %d\n", (int)pid);
9257c478bd9Sstevel@tonic-gate 				dpr->dpr_stale = B_TRUE;
9267c478bd9Sstevel@tonic-gate 				dpr->dpr_cacheable = B_FALSE;
9277c478bd9Sstevel@tonic-gate 				dph->dph_lrucnt--;
9287c478bd9Sstevel@tonic-gate 				break;
9297c478bd9Sstevel@tonic-gate 			}
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 			dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
9327c478bd9Sstevel@tonic-gate 			dt_list_delete(&dph->dph_lrulist, dpr);
9337c478bd9Sstevel@tonic-gate 			dt_list_prepend(&dph->dph_lrulist, dpr);
9347c478bd9Sstevel@tonic-gate 			dpr->dpr_refs++;
9357c478bd9Sstevel@tonic-gate 			return (dpr->dpr_proc);
9367c478bd9Sstevel@tonic-gate 		}
9377c478bd9Sstevel@tonic-gate 	}
9387c478bd9Sstevel@tonic-gate 
9397c478bd9Sstevel@tonic-gate 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
9407c478bd9Sstevel@tonic-gate 		return (NULL); /* errno is set for us */
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
9437c478bd9Sstevel@tonic-gate 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 	if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
9467c478bd9Sstevel@tonic-gate 		return (dt_proc_error(dtp, dpr,
9477c478bd9Sstevel@tonic-gate 		    "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	dpr->dpr_hdl = dtp;
9517c478bd9Sstevel@tonic-gate 	dpr->dpr_pid = pid;
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 	(void) Punsetflags(dpr->dpr_proc, PR_KLC);
9547c478bd9Sstevel@tonic-gate 	(void) Psetflags(dpr->dpr_proc, PR_RLC);
9557c478bd9Sstevel@tonic-gate 
9567c478bd9Sstevel@tonic-gate 	/*
9577c478bd9Sstevel@tonic-gate 	 * If we are attempting to grab the process without a monitor
9587c478bd9Sstevel@tonic-gate 	 * thread, then mark the process cacheable only if it's being
9597c478bd9Sstevel@tonic-gate 	 * grabbed read-only.  If we're currently caching more process
9607c478bd9Sstevel@tonic-gate 	 * handles than dph_lrulim permits, attempt to find the
9617c478bd9Sstevel@tonic-gate 	 * least-recently-used handle that is currently unreferenced and
9627c478bd9Sstevel@tonic-gate 	 * release it from the cache.  Otherwise we are grabbing the process
9637c478bd9Sstevel@tonic-gate 	 * for control: create a control thread for this process and store
9647c478bd9Sstevel@tonic-gate 	 * its ID in dpr->dpr_tid.
9657c478bd9Sstevel@tonic-gate 	 */
9667c478bd9Sstevel@tonic-gate 	if (nomonitor || (flags & PGRAB_RDONLY)) {
9677c478bd9Sstevel@tonic-gate 		if (dph->dph_lrucnt >= dph->dph_lrulim) {
9687c478bd9Sstevel@tonic-gate 			for (opr = dt_list_prev(&dph->dph_lrulist);
9697c478bd9Sstevel@tonic-gate 			    opr != NULL; opr = dt_list_prev(opr)) {
9707c478bd9Sstevel@tonic-gate 				if (opr->dpr_cacheable && opr->dpr_refs == 0) {
9717c478bd9Sstevel@tonic-gate 					dt_proc_destroy(dtp, opr->dpr_proc);
9727c478bd9Sstevel@tonic-gate 					break;
9737c478bd9Sstevel@tonic-gate 				}
9747c478bd9Sstevel@tonic-gate 			}
9757c478bd9Sstevel@tonic-gate 		}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 		if (flags & PGRAB_RDONLY) {
9787c478bd9Sstevel@tonic-gate 			dpr->dpr_cacheable = B_TRUE;
9797c478bd9Sstevel@tonic-gate 			dpr->dpr_rdonly = B_TRUE;
9807c478bd9Sstevel@tonic-gate 			dph->dph_lrucnt++;
9817c478bd9Sstevel@tonic-gate 		}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	} else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
9847c478bd9Sstevel@tonic-gate 		return (NULL); /* dt_proc_error() has been called for us */
9857c478bd9Sstevel@tonic-gate 
9867c478bd9Sstevel@tonic-gate 	dpr->dpr_hash = dph->dph_hash[h];
9877c478bd9Sstevel@tonic-gate 	dph->dph_hash[h] = dpr;
9887c478bd9Sstevel@tonic-gate 	dt_list_prepend(&dph->dph_lrulist, dpr);
9897c478bd9Sstevel@tonic-gate 
9907c478bd9Sstevel@tonic-gate 	dt_dprintf("grabbed pid %d\n", (int)pid);
9917c478bd9Sstevel@tonic-gate 	dpr->dpr_refs++;
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	return (dpr->dpr_proc);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate void
dt_proc_release(dtrace_hdl_t * dtp,struct ps_prochandle * P)9977c478bd9Sstevel@tonic-gate dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
9987c478bd9Sstevel@tonic-gate {
9997c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10007c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	assert(dpr != NULL);
10037c478bd9Sstevel@tonic-gate 	assert(dpr->dpr_refs != 0);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	if (--dpr->dpr_refs == 0 &&
10067c478bd9Sstevel@tonic-gate 	    (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
10077c478bd9Sstevel@tonic-gate 		dt_proc_destroy(dtp, P);
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate void
dt_proc_continue(dtrace_hdl_t * dtp,struct ps_prochandle * P)10117c478bd9Sstevel@tonic-gate dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10127c478bd9Sstevel@tonic-gate {
10137c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_lock(&dpr->dpr_lock);
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 	if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
10187c478bd9Sstevel@tonic-gate 		dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
10197c478bd9Sstevel@tonic-gate 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
10207c478bd9Sstevel@tonic-gate 	}
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
10237c478bd9Sstevel@tonic-gate }
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate void
dt_proc_lock(dtrace_hdl_t * dtp,struct ps_prochandle * P)10267c478bd9Sstevel@tonic-gate dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10277c478bd9Sstevel@tonic-gate {
10287c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10297c478bd9Sstevel@tonic-gate 	int err = pthread_mutex_lock(&dpr->dpr_lock);
10307c478bd9Sstevel@tonic-gate 	assert(err == 0); /* check for recursion */
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate void
dt_proc_unlock(dtrace_hdl_t * dtp,struct ps_prochandle * P)10347c478bd9Sstevel@tonic-gate dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
10377c478bd9Sstevel@tonic-gate 	int err = pthread_mutex_unlock(&dpr->dpr_lock);
10387c478bd9Sstevel@tonic-gate 	assert(err == 0); /* check for unheld lock */
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate void
dt_proc_init(dtrace_hdl_t * dtp)1042e5803b76SAdam H. Leventhal dt_proc_init(dtrace_hdl_t *dtp)
10437c478bd9Sstevel@tonic-gate {
1044e5803b76SAdam H. Leventhal 	extern char **environ;
1045e5803b76SAdam H. Leventhal 	static char *envdef[] = {
1046e5803b76SAdam H. Leventhal 		"LD_NOLAZYLOAD=1",	/* linker lazy loading hides funcs */
1047e5803b76SAdam H. Leventhal 		NULL
1048e5803b76SAdam H. Leventhal 	};
1049e5803b76SAdam H. Leventhal 	char **p;
1050e5803b76SAdam H. Leventhal 	int i;
1051e5803b76SAdam H. Leventhal 
10527c478bd9Sstevel@tonic-gate 	if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
1053e5803b76SAdam H. Leventhal 	    sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) == NULL)
1054e5803b76SAdam H. Leventhal 		return;
1055e5803b76SAdam H. Leventhal 
1056e5803b76SAdam H. Leventhal 	(void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
1057e5803b76SAdam H. Leventhal 	(void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
1058e5803b76SAdam H. Leventhal 
1059e5803b76SAdam H. Leventhal 	dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
1060e5803b76SAdam H. Leventhal 	dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
10617c478bd9Sstevel@tonic-gate 
10627c478bd9Sstevel@tonic-gate 
1063e5803b76SAdam H. Leventhal 	/*
1064e5803b76SAdam H. Leventhal 	 * Count how big our environment needs to be.
1065e5803b76SAdam H. Leventhal 	 */
1066e5803b76SAdam H. Leventhal 	for (i = 1, p = environ; *p != NULL; i++, p++)
1067e5803b76SAdam H. Leventhal 		continue;
1068e5803b76SAdam H. Leventhal 	for (p = envdef; *p != NULL; i++, p++)
1069e5803b76SAdam H. Leventhal 		continue;
1070e5803b76SAdam H. Leventhal 
1071e5803b76SAdam H. Leventhal 	if ((dtp->dt_proc_env = dt_zalloc(dtp, sizeof (char *) * i)) == NULL)
1072e5803b76SAdam H. Leventhal 		return;
1073e5803b76SAdam H. Leventhal 
1074e5803b76SAdam H. Leventhal 	for (i = 0, p = environ; *p != NULL; i++, p++) {
1075e5803b76SAdam H. Leventhal 		if ((dtp->dt_proc_env[i] = strdup(*p)) == NULL)
1076e5803b76SAdam H. Leventhal 			goto err;
10777c478bd9Sstevel@tonic-gate 	}
1078e5803b76SAdam H. Leventhal 	for (p = envdef; *p != NULL; i++, p++) {
1079e5803b76SAdam H. Leventhal 		if ((dtp->dt_proc_env[i] = strdup(*p)) == NULL)
1080e5803b76SAdam H. Leventhal 			goto err;
1081e5803b76SAdam H. Leventhal 	}
1082e5803b76SAdam H. Leventhal 
1083e5803b76SAdam H. Leventhal 	return;
1084e5803b76SAdam H. Leventhal 
1085e5803b76SAdam H. Leventhal err:
1086e5803b76SAdam H. Leventhal 	while (--i != 0) {
1087e5803b76SAdam H. Leventhal 		dt_free(dtp, dtp->dt_proc_env[i]);
1088e5803b76SAdam H. Leventhal 	}
1089e5803b76SAdam H. Leventhal 	dt_free(dtp, dtp->dt_proc_env);
1090e5803b76SAdam H. Leventhal 	dtp->dt_proc_env = NULL;
10917c478bd9Sstevel@tonic-gate }
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate void
dt_proc_fini(dtrace_hdl_t * dtp)1094e5803b76SAdam H. Leventhal dt_proc_fini(dtrace_hdl_t *dtp)
10957c478bd9Sstevel@tonic-gate {
10967c478bd9Sstevel@tonic-gate 	dt_proc_hash_t *dph = dtp->dt_procs;
10977c478bd9Sstevel@tonic-gate 	dt_proc_t *dpr;
1098e5803b76SAdam H. Leventhal 	char **p;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
11017c478bd9Sstevel@tonic-gate 		dt_proc_destroy(dtp, dpr->dpr_proc);
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 	dtp->dt_procs = NULL;
11047c478bd9Sstevel@tonic-gate 	dt_free(dtp, dph);
1105e5803b76SAdam H. Leventhal 
1106e5803b76SAdam H. Leventhal 	for (p = dtp->dt_proc_env; *p != NULL; p++)
1107e5803b76SAdam H. Leventhal 		dt_free(dtp, *p);
1108e5803b76SAdam H. Leventhal 
1109e5803b76SAdam H. Leventhal 	dt_free(dtp, dtp->dt_proc_env);
1110e5803b76SAdam H. Leventhal 	dtp->dt_proc_env = NULL;
11117c478bd9Sstevel@tonic-gate }
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate struct ps_prochandle *
dtrace_proc_create(dtrace_hdl_t * dtp,const char * file,char * const * argv)11147c478bd9Sstevel@tonic-gate dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv)
11157c478bd9Sstevel@tonic-gate {
11167c478bd9Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
11177c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = dt_proc_create(dtp, file, argv);
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	if (P != NULL && idp != NULL && idp->di_id == 0)
11207c478bd9Sstevel@tonic-gate 		idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	return (P);
11237c478bd9Sstevel@tonic-gate }
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate struct ps_prochandle *
dtrace_proc_grab(dtrace_hdl_t * dtp,pid_t pid,int flags)11267c478bd9Sstevel@tonic-gate dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
11297c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
11307c478bd9Sstevel@tonic-gate 
11317c478bd9Sstevel@tonic-gate 	if (P != NULL && idp != NULL && idp->di_id == 0)
11327c478bd9Sstevel@tonic-gate 		idp->di_id = pid; /* $target = grabbed pid */
11337c478bd9Sstevel@tonic-gate 
11347c478bd9Sstevel@tonic-gate 	return (P);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate void
dtrace_proc_release(dtrace_hdl_t * dtp,struct ps_prochandle * P)11387c478bd9Sstevel@tonic-gate dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
11397c478bd9Sstevel@tonic-gate {
11407c478bd9Sstevel@tonic-gate 	dt_proc_release(dtp, P);
11417c478bd9Sstevel@tonic-gate }
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate void
dtrace_proc_continue(dtrace_hdl_t * dtp,struct ps_prochandle * P)11447c478bd9Sstevel@tonic-gate dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
11457c478bd9Sstevel@tonic-gate {
11467c478bd9Sstevel@tonic-gate 	dt_proc_continue(dtp, P);
11477c478bd9Sstevel@tonic-gate }
1148