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