xref: /netbsd-src/external/cddl/osnet/dist/lib/libdtrace/common/dt_proc.c (revision d909946ca08dceb44d7d0f22ec9488679695d976)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * DTrace Process Control
29  *
30  * This file provides a set of routines that permit libdtrace and its clients
31  * to create and grab process handles using libproc, and to share these handles
32  * between library mechanisms that need libproc access, such as ustack(), and
33  * client mechanisms that need libproc access, such as dtrace(1M) -c and -p.
34  * The library provides several mechanisms in the libproc control layer:
35  *
36  * Reference Counting: The library code and client code can independently grab
37  * the same process handles without interfering with one another.  Only when
38  * the reference count drops to zero and the handle is not being cached (see
39  * below for more information on caching) will Prelease() be called on it.
40  *
41  * Handle Caching: If a handle is grabbed PGRAB_RDONLY (e.g. by ustack()) and
42  * the reference count drops to zero, the handle is not immediately released.
43  * Instead, libproc handles are maintained on dph_lrulist in order from most-
44  * recently accessed to least-recently accessed.  Idle handles are maintained
45  * until a pre-defined LRU cache limit is exceeded, permitting repeated calls
46  * to ustack() to avoid the overhead of releasing and re-grabbing processes.
47  *
48  * Process Control: For processes that are grabbed for control (~PGRAB_RDONLY)
49  * or created by dt_proc_create(), a control thread is created to provide
50  * callbacks on process exit and symbol table caching on dlopen()s.
51  *
52  * MT-Safety: Libproc is not MT-Safe, so dt_proc_lock() and dt_proc_unlock()
53  * are provided to synchronize access to the libproc handle between libdtrace
54  * code and client code and the control thread's use of the ps_prochandle.
55  *
56  * NOTE: MT-Safety is NOT provided for libdtrace itself, or for use of the
57  * dtrace_proc_grab/dtrace_proc_create mechanisms.  Like all exported libdtrace
58  * calls, these are assumed to be MT-Unsafe.  MT-Safety is ONLY provided for
59  * synchronization between libdtrace control threads and the client thread.
60  *
61  * The ps_prochandles themselves are maintained along with a dt_proc_t struct
62  * in a hash table indexed by PID.  This provides basic locking and reference
63  * counting.  The dt_proc_t is also maintained in LRU order on dph_lrulist.
64  * The dph_lrucnt and dph_lrulim count the number of cacheable processes and
65  * the current limit on the number of actively cached entries.
66  *
67  * The control thread for a process establishes breakpoints at the rtld_db
68  * locations of interest, updates mappings and symbol tables at these points,
69  * and handles exec and fork (by always following the parent).  The control
70  * thread automatically exits when the process dies or control is lost.
71  *
72  * A simple notification mechanism is provided for libdtrace clients using
73  * dtrace_handle_proc() for notification of PS_UNDEAD or PS_LOST events.  If
74  * such an event occurs, the dt_proc_t itself is enqueued on a notification
75  * list and the control thread broadcasts to dph_cv.  dtrace_sleep() will wake
76  * up using this condition and will then call the client handler as necessary.
77  */
78 
79 #include <sys/wait.h>
80 #ifdef illumos
81 #include <sys/lwp.h>
82 #endif
83 #include <strings.h>
84 #include <signal.h>
85 #include <assert.h>
86 #include <errno.h>
87 
88 #include <dt_proc.h>
89 #include <dt_pid.h>
90 #include <dt_impl.h>
91 
92 #ifndef illumos
93 #include <sys/syscall.h>
94 #include <libproc_compat.h>
95 #define	SYS_forksys SYS_fork
96 #endif
97 
98 #define	IS_SYS_EXEC(w)	(w == SYS_execve)
99 #define	IS_SYS_FORK(w)	(w == SYS_vfork || w == SYS_forksys)
100 
101 #if !defined(__DECONST) && defined(__UNCONST)
102 #define __DECONST(a, b) __UNCONST(b)
103 #endif
104 
105 static dt_bkpt_t *
106 dt_proc_bpcreate(dt_proc_t *dpr, uintptr_t addr, dt_bkpt_f *func, void *data)
107 {
108 	struct ps_prochandle *P = dpr->dpr_proc;
109 	dt_bkpt_t *dbp;
110 
111 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
112 
113 	if ((dbp = dt_zalloc(dpr->dpr_hdl, sizeof (dt_bkpt_t))) != NULL) {
114 		dbp->dbp_func = func;
115 		dbp->dbp_data = data;
116 		dbp->dbp_addr = addr;
117 
118 		if (Psetbkpt(P, dbp->dbp_addr, &dbp->dbp_instr) == 0)
119 			dbp->dbp_active = B_TRUE;
120 
121 		dt_list_append(&dpr->dpr_bps, dbp);
122 	}
123 
124 	return (dbp);
125 }
126 
127 static void
128 dt_proc_bpdestroy(dt_proc_t *dpr, int delbkpts)
129 {
130 	int state = Pstate(dpr->dpr_proc);
131 	dt_bkpt_t *dbp, *nbp;
132 
133 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
134 
135 	for (dbp = dt_list_next(&dpr->dpr_bps); dbp != NULL; dbp = nbp) {
136 		if (delbkpts && dbp->dbp_active &&
137 		    state != PS_LOST && state != PS_UNDEAD) {
138 			(void) Pdelbkpt(dpr->dpr_proc,
139 			    dbp->dbp_addr, &dbp->dbp_instr);
140 		}
141 		nbp = dt_list_next(dbp);
142 		dt_list_delete(&dpr->dpr_bps, dbp);
143 		dt_free(dpr->dpr_hdl, dbp);
144 	}
145 }
146 
147 static void
148 dt_proc_bpmatch(dtrace_hdl_t *dtp, dt_proc_t *dpr)
149 {
150 #ifdef illumos
151 	const lwpstatus_t *psp = &Pstatus(dpr->dpr_proc)->pr_lwp;
152 #else
153 	unsigned long pc;
154 #endif
155 	dt_bkpt_t *dbp;
156 
157 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
158 
159 #ifndef illumos
160 	proc_regget(dpr->dpr_proc, REG_PC, &pc);
161 	proc_bkptregadj(&pc);
162 #endif
163 
164 	for (dbp = dt_list_next(&dpr->dpr_bps);
165 	    dbp != NULL; dbp = dt_list_next(dbp)) {
166 #ifdef illumos
167 		if (psp->pr_reg[R_PC] == dbp->dbp_addr)
168 			break;
169 #else
170 		if (pc == dbp->dbp_addr)
171 			break;
172 #endif
173 	}
174 
175 	if (dbp == NULL) {
176 		dt_dprintf("pid %d: spurious breakpoint wakeup for %lx\n",
177 #ifdef illumos
178 		    (int)dpr->dpr_pid, (ulong_t)psp->pr_reg[R_PC]);
179 #else
180 		    (int)dpr->dpr_pid, pc);
181 #endif
182 		return;
183 	}
184 
185 	dt_dprintf("pid %d: hit breakpoint at %lx (%lu)\n",
186 	    (int)dpr->dpr_pid, (ulong_t)dbp->dbp_addr, ++dbp->dbp_hits);
187 
188 	dbp->dbp_func(dtp, dpr, dbp->dbp_data);
189 	(void) Pxecbkpt(dpr->dpr_proc, &dbp->dbp_instr);
190 }
191 
192 static void
193 dt_proc_bpenable(dt_proc_t *dpr)
194 {
195 	dt_bkpt_t *dbp;
196 
197 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
198 
199 	for (dbp = dt_list_next(&dpr->dpr_bps);
200 	    dbp != NULL; dbp = dt_list_next(dbp)) {
201 		if (!dbp->dbp_active && Psetbkpt(dpr->dpr_proc,
202 		    dbp->dbp_addr, &dbp->dbp_instr) == 0)
203 			dbp->dbp_active = B_TRUE;
204 	}
205 
206 	dt_dprintf("breakpoints enabled\n");
207 }
208 
209 static void
210 dt_proc_bpdisable(dt_proc_t *dpr)
211 {
212 	dt_bkpt_t *dbp;
213 
214 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
215 
216 	for (dbp = dt_list_next(&dpr->dpr_bps);
217 	    dbp != NULL; dbp = dt_list_next(dbp)) {
218 		if (dbp->dbp_active && Pdelbkpt(dpr->dpr_proc,
219 		    dbp->dbp_addr, &dbp->dbp_instr) == 0)
220 			dbp->dbp_active = B_FALSE;
221 	}
222 
223 	dt_dprintf("breakpoints disabled\n");
224 }
225 
226 static void
227 dt_proc_notify(dtrace_hdl_t *dtp, dt_proc_hash_t *dph, dt_proc_t *dpr,
228     const char *msg)
229 {
230 	dt_proc_notify_t *dprn = dt_alloc(dtp, sizeof (dt_proc_notify_t));
231 
232 	if (dprn == NULL) {
233 		dt_dprintf("failed to allocate notification for %d %s\n",
234 		    (int)dpr->dpr_pid, msg);
235 	} else {
236 		dprn->dprn_dpr = dpr;
237 		if (msg == NULL)
238 			dprn->dprn_errmsg[0] = '\0';
239 		else
240 			(void) strlcpy(dprn->dprn_errmsg, msg,
241 			    sizeof (dprn->dprn_errmsg));
242 
243 		(void) pthread_mutex_lock(&dph->dph_lock);
244 
245 		dprn->dprn_next = dph->dph_notify;
246 		dph->dph_notify = dprn;
247 
248 		(void) pthread_cond_broadcast(&dph->dph_cv);
249 		(void) pthread_mutex_unlock(&dph->dph_lock);
250 	}
251 }
252 
253 /*
254  * Check to see if the control thread was requested to stop when the victim
255  * process reached a particular event (why) rather than continuing the victim.
256  * If 'why' is set in the stop mask, we wait on dpr_cv for dt_proc_continue().
257  * If 'why' is not set, this function returns immediately and does nothing.
258  */
259 static void
260 dt_proc_stop(dt_proc_t *dpr, uint8_t why)
261 {
262 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
263 	assert(why != DT_PROC_STOP_IDLE);
264 
265 	if (dpr->dpr_stop & why) {
266 		dpr->dpr_stop |= DT_PROC_STOP_IDLE;
267 		dpr->dpr_stop &= ~why;
268 
269 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
270 
271 		/*
272 		 * We disable breakpoints while stopped to preserve the
273 		 * integrity of the program text for both our own disassembly
274 		 * and that of the kernel.
275 		 */
276 		dt_proc_bpdisable(dpr);
277 
278 		while (dpr->dpr_stop & DT_PROC_STOP_IDLE)
279 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
280 
281 		dt_proc_bpenable(dpr);
282 	}
283 }
284 
285 /*ARGSUSED*/
286 static void
287 dt_proc_bpmain(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *fname)
288 {
289 	dt_dprintf("pid %d: breakpoint at %s()\n", (int)dpr->dpr_pid, fname);
290 	dt_proc_stop(dpr, DT_PROC_STOP_MAIN);
291 }
292 
293 static void
294 dt_proc_rdevent(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *evname)
295 {
296 	rd_event_msg_t rdm;
297 	rd_err_e err;
298 
299 	if ((err = rd_event_getmsg(dpr->dpr_rtld, &rdm)) != RD_OK) {
300 		dt_dprintf("pid %d: failed to get %s event message: %s\n",
301 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
302 		return;
303 	}
304 
305 	dt_dprintf("pid %d: rtld event %s type=%d state %d\n",
306 	    (int)dpr->dpr_pid, evname, rdm.type, rdm.u.state);
307 
308 	switch (rdm.type) {
309 	case RD_NONE:
310 		break;
311 	case RD_DLACTIVITY:
312 		if (rdm.u.state != RD_CONSISTENT)
313 			break;
314 
315 		Pupdate_syms(dpr->dpr_proc);
316 		if (dt_pid_create_probes_module(dtp, dpr) != 0)
317 			dt_proc_notify(dtp, dtp->dt_procs, dpr,
318 			    dpr->dpr_errmsg);
319 
320 		break;
321 	case RD_PREINIT:
322 		Pupdate_syms(dpr->dpr_proc);
323 		dt_proc_stop(dpr, DT_PROC_STOP_PREINIT);
324 		break;
325 	case RD_POSTINIT:
326 		Pupdate_syms(dpr->dpr_proc);
327 		dt_proc_stop(dpr, DT_PROC_STOP_POSTINIT);
328 		break;
329 	}
330 }
331 
332 static void
333 dt_proc_rdwatch(dt_proc_t *dpr, rd_event_e event, const char *evname)
334 {
335 	rd_notify_t rdn;
336 	rd_err_e err;
337 
338 	if ((err = rd_event_addr(dpr->dpr_rtld, event, &rdn)) != RD_OK) {
339 		dt_dprintf("pid %d: failed to get event address for %s: %s\n",
340 		    (int)dpr->dpr_pid, evname, rd_errstr(err));
341 		return;
342 	}
343 
344 	if (rdn.type != RD_NOTIFY_BPT) {
345 		dt_dprintf("pid %d: event %s has unexpected type %d\n",
346 		    (int)dpr->dpr_pid, evname, rdn.type);
347 		return;
348 	}
349 
350 	(void) dt_proc_bpcreate(dpr, rdn.u.bptaddr,
351 #ifdef illumos
352 	    (dt_bkpt_f *)dt_proc_rdevent, (void *)evname);
353 #else
354 	    /* XXX ugly */
355 	    (dt_bkpt_f *)dt_proc_rdevent, __DECONST(void *, evname));
356 #endif
357 }
358 
359 /*
360  * Common code for enabling events associated with the run-time linker after
361  * attaching to a process or after a victim process completes an exec(2).
362  */
363 static void
364 dt_proc_attach(dt_proc_t *dpr, int exec)
365 {
366 #ifdef illumos
367 	const pstatus_t *psp = Pstatus(dpr->dpr_proc);
368 #endif
369 	rd_err_e err;
370 	GElf_Sym sym;
371 
372 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
373 
374 	if (exec) {
375 #ifdef illumos
376 		if (psp->pr_lwp.pr_errno != 0)
377 			return; /* exec failed: nothing needs to be done */
378 #endif
379 
380 		dt_proc_bpdestroy(dpr, B_FALSE);
381 #ifdef illumos
382 		Preset_maps(dpr->dpr_proc);
383 #endif
384 	}
385 	if ((dpr->dpr_rtld = Prd_agent(dpr->dpr_proc)) != NULL &&
386 	    (err = rd_event_enable(dpr->dpr_rtld, B_TRUE)) == RD_OK) {
387 #ifdef illumos
388 		dt_proc_rdwatch(dpr, RD_PREINIT, "RD_PREINIT");
389 #endif
390 		dt_proc_rdwatch(dpr, RD_POSTINIT, "RD_POSTINIT");
391 #ifdef illumos
392 		dt_proc_rdwatch(dpr, RD_DLACTIVITY, "RD_DLACTIVITY");
393 #endif
394 	} else {
395 		dt_dprintf("pid %d: failed to enable rtld events: %s\n",
396 		    (int)dpr->dpr_pid, dpr->dpr_rtld ? rd_errstr(err) :
397 		    "rtld_db agent initialization failed");
398 	}
399 
400 	Pupdate_maps(dpr->dpr_proc);
401 
402 	if (Pxlookup_by_name(dpr->dpr_proc, LM_ID_BASE,
403 	    "a.out", "main", &sym, NULL) == 0) {
404 		(void) dt_proc_bpcreate(dpr, (uintptr_t)sym.st_value,
405 		    (dt_bkpt_f *)dt_proc_bpmain, "a.out`main");
406 	} else {
407 		dt_dprintf("pid %d: failed to find a.out`main: %s\n",
408 		    (int)dpr->dpr_pid, strerror(errno));
409 	}
410 }
411 
412 /*
413  * Wait for a stopped process to be set running again by some other debugger.
414  * This is typically not required by /proc-based debuggers, since the usual
415  * model is that one debugger controls one victim.  But DTrace, as usual, has
416  * its own needs: the stop() action assumes that prun(1) or some other tool
417  * will be applied to resume the victim process.  This could be solved by
418  * adding a PCWRUN directive to /proc, but that seems like overkill unless
419  * other debuggers end up needing this functionality, so we implement a cheap
420  * equivalent to PCWRUN using the set of existing kernel mechanisms.
421  *
422  * Our intent is really not just to wait for the victim to run, but rather to
423  * wait for it to run and then stop again for a reason other than the current
424  * PR_REQUESTED stop.  Since PCWSTOP/Pstopstatus() can be applied repeatedly
425  * to a stopped process and will return the same result without affecting the
426  * victim, we can just perform these operations repeatedly until Pstate()
427  * changes, the representative LWP ID changes, or the stop timestamp advances.
428  * dt_proc_control() will then rediscover the new state and continue as usual.
429  * When the process is still stopped in the same exact state, we sleep for a
430  * brief interval before waiting again so as not to spin consuming CPU cycles.
431  */
432 static void
433 dt_proc_waitrun(dt_proc_t *dpr)
434 {
435 printf("%s:%s(%d): DOODAD\n",__FUNCTION__,__FILE__,__LINE__);
436 #ifdef DOODAD
437 	struct ps_prochandle *P = dpr->dpr_proc;
438 	const lwpstatus_t *psp = &Pstatus(P)->pr_lwp;
439 
440 	int krflag = psp->pr_flags & (PR_KLC | PR_RLC);
441 	timestruc_t tstamp = psp->pr_tstamp;
442 	lwpid_t lwpid = psp->pr_lwpid;
443 
444 	const long wstop = PCWSTOP;
445 	int pfd = Pctlfd(P);
446 
447 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
448 	assert(psp->pr_flags & PR_STOPPED);
449 	assert(Pstate(P) == PS_STOP);
450 
451 	/*
452 	 * While we are waiting for the victim to run, clear PR_KLC and PR_RLC
453 	 * so that if the libdtrace client is killed, the victim stays stopped.
454 	 * dt_proc_destroy() will also observe this and perform PRELEASE_HANG.
455 	 */
456 	(void) Punsetflags(P, krflag);
457 	Psync(P);
458 
459 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
460 
461 	while (!dpr->dpr_quit) {
462 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
463 			continue; /* check dpr_quit and continue waiting */
464 
465 		(void) pthread_mutex_lock(&dpr->dpr_lock);
466 		(void) Pstopstatus(P, PCNULL, 0);
467 		psp = &Pstatus(P)->pr_lwp;
468 
469 		/*
470 		 * If we've reached a new state, found a new representative, or
471 		 * the stop timestamp has changed, restore PR_KLC/PR_RLC to its
472 		 * original setting and then return with dpr_lock held.
473 		 */
474 		if (Pstate(P) != PS_STOP || psp->pr_lwpid != lwpid ||
475 		    bcmp(&psp->pr_tstamp, &tstamp, sizeof (tstamp)) != 0) {
476 			(void) Psetflags(P, krflag);
477 			Psync(P);
478 			return;
479 		}
480 
481 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
482 		(void) poll(NULL, 0, MILLISEC / 2);
483 	}
484 
485 	(void) pthread_mutex_lock(&dpr->dpr_lock);
486 #endif
487 }
488 
489 typedef struct dt_proc_control_data {
490 	dtrace_hdl_t *dpcd_hdl;			/* DTrace handle */
491 	dt_proc_t *dpcd_proc;			/* proccess to control */
492 } dt_proc_control_data_t;
493 
494 /*
495  * Main loop for all victim process control threads.  We initialize all the
496  * appropriate /proc control mechanisms, and then enter a loop waiting for
497  * the process to stop on an event or die.  We process any events by calling
498  * appropriate subroutines, and exit when the victim dies or we lose control.
499  *
500  * The control thread synchronizes the use of dpr_proc with other libdtrace
501  * threads using dpr_lock.  We hold the lock for all of our operations except
502  * waiting while the process is running: this is accomplished by writing a
503  * PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.  If the
504  * libdtrace client wishes to exit or abort our wait, SIGCANCEL can be used.
505  */
506 static void *
507 dt_proc_control(void *arg)
508 {
509 	dt_proc_control_data_t *datap = arg;
510 	dtrace_hdl_t *dtp = datap->dpcd_hdl;
511 	dt_proc_t *dpr = datap->dpcd_proc;
512 	dt_proc_hash_t *dph = dpr->dpr_hdl->dt_procs;
513 	struct ps_prochandle *P = dpr->dpr_proc;
514 	int pid = dpr->dpr_pid;
515 
516 #ifdef illumos
517 	int pfd = Pctlfd(P);
518 
519 	const long wstop = PCWSTOP;
520 #endif
521 	int notify = B_FALSE;
522 
523 	/*
524 	 * We disable the POSIX thread cancellation mechanism so that the
525 	 * client program using libdtrace can't accidentally cancel our thread.
526 	 * dt_proc_destroy() uses SIGCANCEL explicitly to simply poke us out
527 	 * of PCWSTOP with EINTR, at which point we will see dpr_quit and exit.
528 	 */
529 	(void) pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
530 
531 	/*
532 	 * Set up the corresponding process for tracing by libdtrace.  We want
533 	 * to be able to catch breakpoints and efficiently single-step over
534 	 * them, and we need to enable librtld_db to watch libdl activity.
535 	 */
536 	(void) pthread_mutex_lock(&dpr->dpr_lock);
537 
538 #ifdef illumos
539 	(void) Punsetflags(P, PR_ASYNC);	/* require synchronous mode */
540 	(void) Psetflags(P, PR_BPTADJ);		/* always adjust eip on x86 */
541 	(void) Punsetflags(P, PR_FORK);		/* do not inherit on fork */
542 
543 	(void) Pfault(P, FLTBPT, B_TRUE);	/* always trace breakpoints */
544 	(void) Pfault(P, FLTTRACE, B_TRUE);	/* always trace single-step */
545 
546 	/*
547 	 * We must trace exit from exec() system calls so that if the exec is
548 	 * successful, we can reset our breakpoints and re-initialize libproc.
549 	 */
550 	(void) Psysexit(P, SYS_execve, B_TRUE);
551 
552 	/*
553 	 * We must trace entry and exit for fork() system calls in order to
554 	 * disable our breakpoints temporarily during the fork.  We do not set
555 	 * the PR_FORK flag, so if fork succeeds the child begins executing and
556 	 * does not inherit any other tracing behaviors or a control thread.
557 	 */
558 	(void) Psysentry(P, SYS_vfork, B_TRUE);
559 	(void) Psysexit(P, SYS_vfork, B_TRUE);
560 	(void) Psysentry(P, SYS_forksys, B_TRUE);
561 	(void) Psysexit(P, SYS_forksys, B_TRUE);
562 
563 	Psync(P);				/* enable all /proc changes */
564 #endif
565 	dt_proc_attach(dpr, B_FALSE);		/* enable rtld breakpoints */
566 
567 	/*
568 	 * If PR_KLC is set, we created the process; otherwise we grabbed it.
569 	 * Check for an appropriate stop request and wait for dt_proc_continue.
570 	 */
571 #ifdef illumos
572 	if (Pstatus(P)->pr_flags & PR_KLC)
573 #else
574 	if (proc_getflags(P) & PR_KLC)
575 #endif
576 		dt_proc_stop(dpr, DT_PROC_STOP_CREATE);
577 	else
578 		dt_proc_stop(dpr, DT_PROC_STOP_GRAB);
579 
580 	if (Psetrun(P, 0, 0) == -1) {
581 		dt_dprintf("pid %d: failed to set running: %s\n",
582 		    (int)dpr->dpr_pid, strerror(errno));
583 	}
584 
585 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
586 
587 	/*
588 	 * Wait for the process corresponding to this control thread to stop,
589 	 * process the event, and then set it running again.  We want to sleep
590 	 * with dpr_lock *unheld* so that other parts of libdtrace can use the
591 	 * ps_prochandle in the meantime (e.g. ustack()).  To do this, we write
592 	 * a PCWSTOP directive directly to the underlying /proc/<pid>/ctl file.
593 	 * Once the process stops, we wake up, grab dpr_lock, and then call
594 	 * Pwait() (which will return immediately) and do our processing.
595 	 */
596 	while (!dpr->dpr_quit) {
597 		const lwpstatus_t *psp;
598 
599 #ifdef illumos
600 		if (write(pfd, &wstop, sizeof (wstop)) == -1 && errno == EINTR)
601 			continue; /* check dpr_quit and continue waiting */
602 #else
603 		/* Wait for the process to report status. */
604 		proc_wstatus(P);
605 		if (errno == EINTR)
606 			continue; /* check dpr_quit and continue waiting */
607 #endif
608 
609 		(void) pthread_mutex_lock(&dpr->dpr_lock);
610 
611 #ifdef illumos
612 pwait_locked:
613 		if (Pstopstatus(P, PCNULL, 0) == -1 && errno == EINTR) {
614 			(void) pthread_mutex_unlock(&dpr->dpr_lock);
615 			continue; /* check dpr_quit and continue waiting */
616 		}
617 #endif
618 
619 		switch (Pstate(P)) {
620 		case PS_STOP:
621 #ifdef illumos
622 			psp = &Pstatus(P)->pr_lwp;
623 #else
624 			psp = proc_getlwpstatus(P);
625 #endif
626 
627 			dt_dprintf("pid %d: proc stopped showing %d/%d\n",
628 			    pid, psp->pr_why, psp->pr_what);
629 
630 			/*
631 			 * If the process stops showing PR_REQUESTED, then the
632 			 * DTrace stop() action was applied to it or another
633 			 * debugging utility (e.g. pstop(1)) asked it to stop.
634 			 * In either case, the user's intention is for the
635 			 * process to remain stopped until another external
636 			 * mechanism (e.g. prun(1)) is applied.  So instead of
637 			 * setting the process running ourself, we wait for
638 			 * someone else to do so.  Once that happens, we return
639 			 * to our normal loop waiting for an event of interest.
640 			 */
641 			if (psp->pr_why == PR_REQUESTED) {
642 				dt_proc_waitrun(dpr);
643 				(void) pthread_mutex_unlock(&dpr->dpr_lock);
644 				continue;
645 			}
646 
647 			/*
648 			 * If the process stops showing one of the events that
649 			 * we are tracing, perform the appropriate response.
650 			 * Note that we ignore PR_SUSPENDED, PR_CHECKPOINT, and
651 			 * PR_JOBCONTROL by design: if one of these conditions
652 			 * occurs, we will fall through to Psetrun() but the
653 			 * process will remain stopped in the kernel by the
654 			 * corresponding mechanism (e.g. job control stop).
655 			 */
656 			if (psp->pr_why == PR_FAULTED && psp->pr_what == FLTBPT)
657 				dt_proc_bpmatch(dtp, dpr);
658 			else if (psp->pr_why == PR_SYSENTRY &&
659 			    IS_SYS_FORK(psp->pr_what))
660 				dt_proc_bpdisable(dpr);
661 			else if (psp->pr_why == PR_SYSEXIT &&
662 			    IS_SYS_FORK(psp->pr_what))
663 				dt_proc_bpenable(dpr);
664 			else if (psp->pr_why == PR_SYSEXIT &&
665 			    IS_SYS_EXEC(psp->pr_what))
666 				dt_proc_attach(dpr, B_TRUE);
667 			break;
668 
669 		case PS_LOST:
670 #ifdef illumos
671 			if (Preopen(P) == 0)
672 				goto pwait_locked;
673 #endif
674 
675 			dt_dprintf("pid %d: proc lost: %s\n",
676 			    pid, strerror(errno));
677 
678 			dpr->dpr_quit = B_TRUE;
679 			notify = B_TRUE;
680 			break;
681 
682 		case PS_UNDEAD:
683 			dt_dprintf("pid %d: proc died\n", pid);
684 			dpr->dpr_quit = B_TRUE;
685 			notify = B_TRUE;
686 			break;
687 		}
688 
689 		if (Pstate(P) != PS_UNDEAD && Psetrun(P, 0, 0) == -1) {
690 			dt_dprintf("pid %d: failed to set running: %s\n",
691 			    (int)dpr->dpr_pid, strerror(errno));
692 		}
693 
694 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
695 	}
696 
697 	/*
698 	 * If the control thread detected PS_UNDEAD or PS_LOST, then enqueue
699 	 * the dt_proc_t structure on the dt_proc_hash_t notification list.
700 	 */
701 	if (notify)
702 		dt_proc_notify(dtp, dph, dpr, NULL);
703 
704 	/*
705 	 * Destroy and remove any remaining breakpoints, set dpr_done and clear
706 	 * dpr_tid to indicate the control thread has exited, and notify any
707 	 * waiting thread in dt_proc_destroy() that we have succesfully exited.
708 	 */
709 	(void) pthread_mutex_lock(&dpr->dpr_lock);
710 
711 	dt_proc_bpdestroy(dpr, B_TRUE);
712 	dpr->dpr_done = B_TRUE;
713 	dpr->dpr_tid = 0;
714 
715 	(void) pthread_cond_broadcast(&dpr->dpr_cv);
716 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
717 
718 	return (NULL);
719 }
720 
721 /*PRINTFLIKE3*/
722 static struct ps_prochandle *
723 dt_proc_error(dtrace_hdl_t *dtp, dt_proc_t *dpr, const char *format, ...)
724 {
725 	va_list ap;
726 
727 	va_start(ap, format);
728 	dt_set_errmsg(dtp, NULL, NULL, NULL, 0, format, ap);
729 	va_end(ap);
730 
731 	if (dpr->dpr_proc != NULL)
732 		Prelease(dpr->dpr_proc, 0);
733 
734 	dt_free(dtp, dpr);
735 	(void) dt_set_errno(dtp, EDT_COMPILER);
736 	return (NULL);
737 }
738 
739 dt_proc_t *
740 dt_proc_lookup(dtrace_hdl_t *dtp, struct ps_prochandle *P, int remove)
741 {
742 	dt_proc_hash_t *dph = dtp->dt_procs;
743 #ifdef illumos
744 	pid_t pid = Pstatus(P)->pr_pid;
745 #else
746 	pid_t pid = proc_getpid(P);
747 #endif
748 	dt_proc_t *dpr, **dpp = &dph->dph_hash[pid & (dph->dph_hashlen - 1)];
749 
750 	for (dpr = *dpp; dpr != NULL; dpr = dpr->dpr_hash) {
751 		if (dpr->dpr_pid == pid)
752 			break;
753 		else
754 			dpp = &dpr->dpr_hash;
755 	}
756 
757 	assert(dpr != NULL);
758 	assert(dpr->dpr_proc == P);
759 
760 	if (remove)
761 		*dpp = dpr->dpr_hash; /* remove from pid hash chain */
762 
763 	return (dpr);
764 }
765 
766 static void
767 dt_proc_destroy(dtrace_hdl_t *dtp, struct ps_prochandle *P)
768 {
769 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
770 	dt_proc_hash_t *dph = dtp->dt_procs;
771 	dt_proc_notify_t *npr, **npp;
772 	int rflag;
773 
774 	assert(dpr != NULL);
775 
776 	/*
777 	 * If neither PR_KLC nor PR_RLC is set, then the process is stopped by
778 	 * an external debugger and we were waiting in dt_proc_waitrun().
779 	 * Leave the process in this condition using PRELEASE_HANG.
780 	 */
781 #ifdef illumos
782 	if (!(Pstatus(dpr->dpr_proc)->pr_flags & (PR_KLC | PR_RLC))) {
783 #else
784 	if (!(proc_getflags(dpr->dpr_proc) & (PR_KLC | PR_RLC))) {
785 #endif
786 		dt_dprintf("abandoning pid %d\n", (int)dpr->dpr_pid);
787 		rflag = PRELEASE_HANG;
788 #ifdef illumos
789 	} else if (Pstatus(dpr->dpr_proc)->pr_flags & PR_KLC) {
790 #else
791 	} else if (proc_getflags(dpr->dpr_proc) & PR_KLC) {
792 #endif
793 		dt_dprintf("killing pid %d\n", (int)dpr->dpr_pid);
794 		rflag = PRELEASE_KILL; /* apply kill-on-last-close */
795 	} else {
796 		dt_dprintf("releasing pid %d\n", (int)dpr->dpr_pid);
797 		rflag = 0; /* apply run-on-last-close */
798 	}
799 
800 	if (dpr->dpr_tid) {
801 		/*
802 		 * Set the dpr_quit flag to tell the daemon thread to exit.  We
803 		 * send it a SIGCANCEL to poke it out of PCWSTOP or any other
804 		 * long-term /proc system call.  Our daemon threads have POSIX
805 		 * cancellation disabled, so EINTR will be the only effect.  We
806 		 * then wait for dpr_done to indicate the thread has exited.
807 		 *
808 		 * We can't use pthread_kill() to send SIGCANCEL because the
809 		 * interface forbids it and we can't use pthread_cancel()
810 		 * because with cancellation disabled it won't actually
811 		 * send SIGCANCEL to the target thread, so we use _lwp_kill()
812 		 * to do the job.  This is all built on evil knowledge of
813 		 * the details of the cancellation mechanism in libc.
814 		 */
815 		(void) pthread_mutex_lock(&dpr->dpr_lock);
816 		dpr->dpr_quit = B_TRUE;
817 #ifdef illumos
818 		(void) _lwp_kill(dpr->dpr_tid, SIGCANCEL);
819 #elif defined(__FreeBSD__)
820 		pthread_kill(dpr->dpr_tid, SIGTHR);
821 #else
822 		pthread_cancel(dpr->dpr_tid);
823 #endif
824 
825 		/*
826 		 * If the process is currently idling in dt_proc_stop(), re-
827 		 * enable breakpoints and poke it into running again.
828 		 */
829 		if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
830 			dt_proc_bpenable(dpr);
831 			dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
832 			(void) pthread_cond_broadcast(&dpr->dpr_cv);
833 		}
834 
835 		while (!dpr->dpr_done)
836 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
837 
838 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
839 	}
840 
841 	/*
842 	 * Before we free the process structure, remove this dt_proc_t from the
843 	 * lookup hash, and then walk the dt_proc_hash_t's notification list
844 	 * and remove this dt_proc_t if it is enqueued.
845 	 */
846 	(void) pthread_mutex_lock(&dph->dph_lock);
847 	(void) dt_proc_lookup(dtp, P, B_TRUE);
848 	npp = &dph->dph_notify;
849 
850 	while ((npr = *npp) != NULL) {
851 		if (npr->dprn_dpr == dpr) {
852 			*npp = npr->dprn_next;
853 			dt_free(dtp, npr);
854 		} else {
855 			npp = &npr->dprn_next;
856 		}
857 	}
858 
859 	(void) pthread_mutex_unlock(&dph->dph_lock);
860 
861 	/*
862 	 * Remove the dt_proc_list from the LRU list, release the underlying
863 	 * libproc handle, and free our dt_proc_t data structure.
864 	 */
865 	if (dpr->dpr_cacheable) {
866 		assert(dph->dph_lrucnt != 0);
867 		dph->dph_lrucnt--;
868 	}
869 
870 	dt_list_delete(&dph->dph_lrulist, dpr);
871 	Prelease(dpr->dpr_proc, rflag);
872 	dt_free(dtp, dpr);
873 }
874 
875 static int
876 dt_proc_create_thread(dtrace_hdl_t *dtp, dt_proc_t *dpr, uint_t stop)
877 {
878 	dt_proc_control_data_t data;
879 	sigset_t nset, oset;
880 	pthread_attr_t a;
881 	int err;
882 
883 	(void) pthread_mutex_lock(&dpr->dpr_lock);
884 	dpr->dpr_stop |= stop; /* set bit for initial rendezvous */
885 
886 	(void) pthread_attr_init(&a);
887 	(void) pthread_attr_setdetachstate(&a, PTHREAD_CREATE_DETACHED);
888 
889 	(void) sigfillset(&nset);
890 	(void) sigdelset(&nset, SIGABRT);	/* unblocked for assert() */
891 #ifdef illumos
892 	(void) sigdelset(&nset, SIGCANCEL);	/* see dt_proc_destroy() */
893 #else
894 	(void) sigdelset(&nset, SIGUSR1);	/* see dt_proc_destroy() */
895 #endif
896 
897 	data.dpcd_hdl = dtp;
898 	data.dpcd_proc = dpr;
899 
900 	(void) pthread_sigmask(SIG_SETMASK, &nset, &oset);
901 	err = pthread_create(&dpr->dpr_tid, &a, dt_proc_control, &data);
902 	(void) pthread_sigmask(SIG_SETMASK, &oset, NULL);
903 
904 	/*
905 	 * If the control thread was created, then wait on dpr_cv for either
906 	 * dpr_done to be set (the victim died or the control thread failed)
907 	 * or DT_PROC_STOP_IDLE to be set, indicating that the victim is now
908 	 * stopped by /proc and the control thread is at the rendezvous event.
909 	 * On success, we return with the process and control thread stopped:
910 	 * the caller can then apply dt_proc_continue() to resume both.
911 	 */
912 	if (err == 0) {
913 		while (!dpr->dpr_done && !(dpr->dpr_stop & DT_PROC_STOP_IDLE))
914 			(void) pthread_cond_wait(&dpr->dpr_cv, &dpr->dpr_lock);
915 
916 		/*
917 		 * If dpr_done is set, the control thread aborted before it
918 		 * reached the rendezvous event.  This is either due to PS_LOST
919 		 * or PS_UNDEAD (i.e. the process died).  We try to provide a
920 		 * small amount of useful information to help figure it out.
921 		 */
922 		if (dpr->dpr_done) {
923 #ifdef illumos
924 			const psinfo_t *prp = Ppsinfo(dpr->dpr_proc);
925 			int stat = prp ? prp->pr_wstat : 0;
926 			int pid = dpr->dpr_pid;
927 #else
928 			int stat = proc_getwstat(dpr->dpr_proc);
929 			int pid = proc_getpid(dpr->dpr_proc);
930 #endif
931 			if (proc_state(dpr->dpr_proc) == PS_LOST) {
932 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
933 				    "failed to control pid %d: process exec'd "
934 				    "set-id or unobservable program\n", pid);
935 			} else if (WIFSIGNALED(stat)) {
936 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
937 				    "failed to control pid %d: process died "
938 				    "from signal %d\n", pid, WTERMSIG(stat));
939 			} else {
940 				(void) dt_proc_error(dpr->dpr_hdl, dpr,
941 				    "failed to control pid %d: process exited "
942 				    "with status %d\n", pid, WEXITSTATUS(stat));
943 			}
944 
945 			err = ESRCH; /* cause grab() or create() to fail */
946 		}
947 	} else {
948 		(void) dt_proc_error(dpr->dpr_hdl, dpr,
949 		    "failed to create control thread for process-id %d: %s\n",
950 		    (int)dpr->dpr_pid, strerror(err));
951 	}
952 
953 	if (err == 0)
954 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
955 	(void) pthread_attr_destroy(&a);
956 
957 	return (err);
958 }
959 
960 struct ps_prochandle *
961 dt_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
962     proc_child_func *pcf, void *child_arg)
963 {
964 	dt_proc_hash_t *dph = dtp->dt_procs;
965 	dt_proc_t *dpr;
966 	int err;
967 
968 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
969 		return (NULL); /* errno is set for us */
970 
971 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
972 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
973 
974 #ifdef illumos
975 	if ((dpr->dpr_proc = Pcreate(file, argv, &err, NULL, 0)) == NULL) {
976 #else
977 	if ((err = proc_create(file, argv, pcf, child_arg,
978 	    &dpr->dpr_proc)) != 0) {
979 #endif
980 		return (dt_proc_error(dtp, dpr,
981 		    "failed to execute %s: %s\n", file, Pcreate_error(err)));
982 	}
983 
984 	dpr->dpr_hdl = dtp;
985 #ifdef illumos
986 	dpr->dpr_pid = Pstatus(dpr->dpr_proc)->pr_pid;
987 #else
988 	dpr->dpr_pid = proc_getpid(dpr->dpr_proc);
989 #endif
990 
991 	(void) Punsetflags(dpr->dpr_proc, PR_RLC);
992 	(void) Psetflags(dpr->dpr_proc, PR_KLC);
993 
994 	if (dt_proc_create_thread(dtp, dpr, dtp->dt_prcmode) != 0)
995 		return (NULL); /* dt_proc_error() has been called for us */
996 
997 	dpr->dpr_hash = dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)];
998 	dph->dph_hash[dpr->dpr_pid & (dph->dph_hashlen - 1)] = dpr;
999 	dt_list_prepend(&dph->dph_lrulist, dpr);
1000 
1001 	dt_dprintf("created pid %d\n", (int)dpr->dpr_pid);
1002 	dpr->dpr_refs++;
1003 
1004 	return (dpr->dpr_proc);
1005 }
1006 
1007 struct ps_prochandle *
1008 dt_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags, int nomonitor)
1009 {
1010 	dt_proc_hash_t *dph = dtp->dt_procs;
1011 	uint_t h = pid & (dph->dph_hashlen - 1);
1012 	dt_proc_t *dpr, *opr;
1013 	int err;
1014 
1015 	/*
1016 	 * Search the hash table for the pid.  If it is already grabbed or
1017 	 * created, move the handle to the front of the lrulist, increment
1018 	 * the reference count, and return the existing ps_prochandle.
1019 	 */
1020 	for (dpr = dph->dph_hash[h]; dpr != NULL; dpr = dpr->dpr_hash) {
1021 		if (dpr->dpr_pid == pid && !dpr->dpr_stale) {
1022 			/*
1023 			 * If the cached handle was opened read-only and
1024 			 * this request is for a writeable handle, mark
1025 			 * the cached handle as stale and open a new handle.
1026 			 * Since it's stale, unmark it as cacheable.
1027 			 */
1028 			if (dpr->dpr_rdonly && !(flags & PGRAB_RDONLY)) {
1029 				dt_dprintf("upgrading pid %d\n", (int)pid);
1030 				dpr->dpr_stale = B_TRUE;
1031 				dpr->dpr_cacheable = B_FALSE;
1032 				dph->dph_lrucnt--;
1033 				break;
1034 			}
1035 
1036 			dt_dprintf("grabbed pid %d (cached)\n", (int)pid);
1037 			dt_list_delete(&dph->dph_lrulist, dpr);
1038 			dt_list_prepend(&dph->dph_lrulist, dpr);
1039 			dpr->dpr_refs++;
1040 			return (dpr->dpr_proc);
1041 		}
1042 	}
1043 
1044 	if ((dpr = dt_zalloc(dtp, sizeof (dt_proc_t))) == NULL)
1045 		return (NULL); /* errno is set for us */
1046 
1047 	(void) pthread_mutex_init(&dpr->dpr_lock, NULL);
1048 	(void) pthread_cond_init(&dpr->dpr_cv, NULL);
1049 
1050 #ifdef illumos
1051 	if ((dpr->dpr_proc = Pgrab(pid, flags, &err)) == NULL) {
1052 #else
1053 	if ((err = proc_attach(pid, flags, &dpr->dpr_proc)) != 0) {
1054 #endif
1055 		return (dt_proc_error(dtp, dpr,
1056 		    "failed to grab pid %d: %s\n", (int)pid, Pgrab_error(err)));
1057 	}
1058 
1059 	dpr->dpr_hdl = dtp;
1060 	dpr->dpr_pid = pid;
1061 
1062 	(void) Punsetflags(dpr->dpr_proc, PR_KLC);
1063 	(void) Psetflags(dpr->dpr_proc, PR_RLC);
1064 
1065 	/*
1066 	 * If we are attempting to grab the process without a monitor
1067 	 * thread, then mark the process cacheable only if it's being
1068 	 * grabbed read-only.  If we're currently caching more process
1069 	 * handles than dph_lrulim permits, attempt to find the
1070 	 * least-recently-used handle that is currently unreferenced and
1071 	 * release it from the cache.  Otherwise we are grabbing the process
1072 	 * for control: create a control thread for this process and store
1073 	 * its ID in dpr->dpr_tid.
1074 	 */
1075 	if (nomonitor || (flags & PGRAB_RDONLY)) {
1076 		if (dph->dph_lrucnt >= dph->dph_lrulim) {
1077 			for (opr = dt_list_prev(&dph->dph_lrulist);
1078 			    opr != NULL; opr = dt_list_prev(opr)) {
1079 				if (opr->dpr_cacheable && opr->dpr_refs == 0) {
1080 					dt_proc_destroy(dtp, opr->dpr_proc);
1081 					break;
1082 				}
1083 			}
1084 		}
1085 
1086 		if (flags & PGRAB_RDONLY) {
1087 			dpr->dpr_cacheable = B_TRUE;
1088 			dpr->dpr_rdonly = B_TRUE;
1089 			dph->dph_lrucnt++;
1090 		}
1091 
1092 	} else if (dt_proc_create_thread(dtp, dpr, DT_PROC_STOP_GRAB) != 0)
1093 		return (NULL); /* dt_proc_error() has been called for us */
1094 
1095 	dpr->dpr_hash = dph->dph_hash[h];
1096 	dph->dph_hash[h] = dpr;
1097 	dt_list_prepend(&dph->dph_lrulist, dpr);
1098 
1099 	dt_dprintf("grabbed pid %d\n", (int)pid);
1100 	dpr->dpr_refs++;
1101 
1102 	return (dpr->dpr_proc);
1103 }
1104 
1105 void
1106 dt_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1107 {
1108 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1109 	dt_proc_hash_t *dph = dtp->dt_procs;
1110 
1111 	assert(dpr != NULL);
1112 	assert(dpr->dpr_refs != 0);
1113 
1114 	if (--dpr->dpr_refs == 0 &&
1115 	    (!dpr->dpr_cacheable || dph->dph_lrucnt > dph->dph_lrulim))
1116 		dt_proc_destroy(dtp, P);
1117 }
1118 
1119 void
1120 dt_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1121 {
1122 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1123 
1124 	(void) pthread_mutex_lock(&dpr->dpr_lock);
1125 
1126 	if (dpr->dpr_stop & DT_PROC_STOP_IDLE) {
1127 		dpr->dpr_stop &= ~DT_PROC_STOP_IDLE;
1128 		(void) pthread_cond_broadcast(&dpr->dpr_cv);
1129 	}
1130 
1131 	(void) pthread_mutex_unlock(&dpr->dpr_lock);
1132 }
1133 
1134 void
1135 dt_proc_lock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1136 {
1137 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1138 	int err = pthread_mutex_lock(&dpr->dpr_lock);
1139 	assert(err == 0); /* check for recursion */
1140 }
1141 
1142 void
1143 dt_proc_unlock(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1144 {
1145 	dt_proc_t *dpr = dt_proc_lookup(dtp, P, B_FALSE);
1146 	int err = pthread_mutex_unlock(&dpr->dpr_lock);
1147 	assert(err == 0); /* check for unheld lock */
1148 }
1149 
1150 void
1151 dt_proc_hash_create(dtrace_hdl_t *dtp)
1152 {
1153 	if ((dtp->dt_procs = dt_zalloc(dtp, sizeof (dt_proc_hash_t) +
1154 	    sizeof (dt_proc_t *) * _dtrace_pidbuckets - 1)) != NULL) {
1155 
1156 		(void) pthread_mutex_init(&dtp->dt_procs->dph_lock, NULL);
1157 		(void) pthread_cond_init(&dtp->dt_procs->dph_cv, NULL);
1158 
1159 		dtp->dt_procs->dph_hashlen = _dtrace_pidbuckets;
1160 		dtp->dt_procs->dph_lrulim = _dtrace_pidlrulim;
1161 	}
1162 }
1163 
1164 void
1165 dt_proc_hash_destroy(dtrace_hdl_t *dtp)
1166 {
1167 	dt_proc_hash_t *dph = dtp->dt_procs;
1168 	dt_proc_t *dpr;
1169 
1170 	while ((dpr = dt_list_next(&dph->dph_lrulist)) != NULL)
1171 		dt_proc_destroy(dtp, dpr->dpr_proc);
1172 
1173 	dtp->dt_procs = NULL;
1174 	dt_free(dtp, dph);
1175 }
1176 
1177 struct ps_prochandle *
1178 dtrace_proc_create(dtrace_hdl_t *dtp, const char *file, char *const *argv,
1179     proc_child_func *pcf, void *child_arg)
1180 {
1181 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1182 	struct ps_prochandle *P = dt_proc_create(dtp, file, argv, pcf, child_arg);
1183 
1184 	if (P != NULL && idp != NULL && idp->di_id == 0) {
1185 #ifdef illumos
1186 		idp->di_id = Pstatus(P)->pr_pid; /* $target = created pid */
1187 #else
1188 		idp->di_id = proc_getpid(P); /* $target = created pid */
1189 #endif
1190 	}
1191 
1192 	return (P);
1193 }
1194 
1195 struct ps_prochandle *
1196 dtrace_proc_grab(dtrace_hdl_t *dtp, pid_t pid, int flags)
1197 {
1198 	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");
1199 	struct ps_prochandle *P = dt_proc_grab(dtp, pid, flags, 0);
1200 
1201 	if (P != NULL && idp != NULL && idp->di_id == 0)
1202 		idp->di_id = pid; /* $target = grabbed pid */
1203 
1204 	return (P);
1205 }
1206 
1207 void
1208 dtrace_proc_release(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1209 {
1210 	dt_proc_release(dtp, P);
1211 }
1212 
1213 void
1214 dtrace_proc_continue(dtrace_hdl_t *dtp, struct ps_prochandle *P)
1215 {
1216 	dt_proc_continue(dtp, P);
1217 }
1218