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