xref: /titanic_44/usr/src/lib/libdtrace/common/dt_pid.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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 #include <assert.h>
30 #include <strings.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <alloca.h>
36 #include <libgen.h>
37 #include <stddef.h>
38 
39 #include <dt_impl.h>
40 #include <dt_pid.h>
41 #include <dt_string.h>
42 
43 typedef struct dt_pid_probe {
44 	dtrace_hdl_t		*dpp_dtp;
45 	struct ps_prochandle	*dpp_pr;
46 	const char		*dpp_mod;
47 	char			*dpp_func;
48 	const char		*dpp_name;
49 	const char		*dpp_obj;
50 	uintptr_t		dpp_pc;
51 	size_t			dpp_size;
52 	Lmid_t			dpp_lmid;
53 	uint_t			dpp_nmatches;
54 	uint64_t		dpp_stret[4];
55 	GElf_Sym		dpp_last;
56 	uint_t			dpp_last_taken;
57 } dt_pid_probe_t;
58 
59 static void
60 dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
61 {
62 	fasttrap_probe_spec_t *ftp;
63 	uint64_t off;
64 	char *end;
65 	uint_t nmatches = 0;
66 	ulong_t sz;
67 	int glob, err;
68 	int isdash = strcmp("-", func) == 0;
69 	pid_t pid;
70 
71 	pid = Pstatus(pp->dpp_pr)->pr_pid;
72 
73 	dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
74 	    func, pp->dpp_name);
75 
76 	sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
77 	    (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
78 
79 	if (sz < 4000) {
80 		ftp = alloca(sz);
81 		sz = 0;
82 	} else if ((ftp = malloc(sz)) == NULL) {
83 		dt_dprintf("proc_per_sym: malloc(%lu) failed\n", sz);
84 		return;
85 	}
86 
87 	ftp->ftps_pid = pid;
88 	(void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
89 
90 	if (pp->dpp_lmid == 0) {
91 		(void) strncpy(ftp->ftps_mod, pp->dpp_obj,
92 		    sizeof (ftp->ftps_mod));
93 	} else {
94 		(void) snprintf(ftp->ftps_mod, sizeof (ftp->ftps_mod),
95 		    "LM%lx`%s", pp->dpp_lmid, pp->dpp_obj);
96 	}
97 
98 	if (!isdash && gmatch("return", pp->dpp_name)) {
99 		if (dt_pid_create_return_probe(pp->dpp_pr, pp->dpp_dtp,
100 		    ftp, symp, pp->dpp_stret) < 0)
101 			goto create_err;
102 
103 		nmatches++;
104 	}
105 
106 	if (!isdash && gmatch("entry", pp->dpp_name)) {
107 		if (dt_pid_create_entry_probe(pp->dpp_pr, pp->dpp_dtp,
108 		    ftp, symp) < 0)
109 			goto create_err;
110 
111 		nmatches++;
112 	}
113 
114 	glob = strisglob(pp->dpp_name);
115 	if (!glob && nmatches == 0) {
116 		off = strtoull(pp->dpp_name, &end, 16);
117 		if (*end != '\0') {
118 			if (sz != 0)
119 				free(ftp);
120 			dt_proc_release(pp->dpp_dtp, pp->dpp_pr);
121 			xyerror(D_PROC_NAME, "'%s' is an invalid probe name\n",
122 			    pp->dpp_name);
123 		}
124 
125 		if (off >= symp->st_size) {
126 			char buf[DTRACE_FUNCNAMELEN];
127 			/*
128 			 * We need to copy the function name to the stack
129 			 * because 'func' may be freed by virtue of calling
130 			 * dt_proc_release() on the libproc handle.
131 			 */
132 			(void) strncpy(buf, func, sizeof (buf));
133 			if (sz != 0)
134 				free(ftp);
135 			dt_proc_release(pp->dpp_dtp, pp->dpp_pr);
136 			xyerror(D_PROC_OFF, "offset 0x%llx outside of "
137 			    "function '%s'\n", (u_longlong_t)off, buf);
138 		}
139 
140 		err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
141 		    symp, off);
142 
143 		if (err == DT_PROC_ERR)
144 			goto create_err;
145 		if (err == DT_PROC_ALIGN) {
146 			if (sz != 0)
147 				free(ftp);
148 			dt_proc_release(pp->dpp_dtp, pp->dpp_pr);
149 			xyerror(D_PROC_ALIGN, "offset 0x%llx is not aligned "
150 			    "on an instruction\n", (u_longlong_t)off);
151 		}
152 
153 		nmatches++;
154 
155 	} else if (glob && !isdash) {
156 		if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
157 		    pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0)
158 			goto create_err;
159 
160 		nmatches++;
161 	}
162 
163 	pp->dpp_nmatches += nmatches;
164 
165 	if (sz != 0)
166 		free(ftp);
167 	return;
168 
169 create_err:
170 	if (sz != 0)
171 		free(ftp);
172 
173 	dt_proc_release(pp->dpp_dtp, pp->dpp_pr);
174 	xyerror(D_PROC_CREATEFAIL, "failed to create probe in process %d: %s\n",
175 	    (int)pid, dtrace_errmsg(pp->dpp_dtp, dtrace_errno(pp->dpp_dtp)));
176 }
177 
178 static int
179 dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
180 {
181 	dt_pid_probe_t *pp = arg;
182 
183 	if (symp->st_shndx == SHN_UNDEF)
184 		return (0);
185 
186 	if (symp->st_size == 0) {
187 		dt_dprintf("st_size of %s is zero\n", func);
188 		return (0);
189 	}
190 
191 	if (symp->st_value != pp->dpp_last.st_value ||
192 	    symp->st_size != pp->dpp_last.st_size) {
193 		/*
194 		 * Due to 4524008, _init and _fini may have a bloated st_size.
195 		 * While this bug has been fixed for a while, old binaries
196 		 * may exist that still exhibit this problem. As a result, we
197 		 * don't match _init and _fini though we allow users to
198 		 * specify them explicitly.
199 		 */
200 		if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
201 			return (0);
202 
203 		if (gmatch(func, pp->dpp_func)) {
204 			dt_pid_per_sym(pp, symp, func);
205 			pp->dpp_last_taken = 1;
206 		}
207 
208 		pp->dpp_last = *symp;
209 	}
210 
211 	return (0);
212 }
213 
214 static void
215 dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
216 {
217 	dt_pid_probe_t *pp = arg;
218 	GElf_Sym sym;
219 
220 	if (obj == NULL)
221 		return;
222 
223 	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
224 
225 	if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
226 		pp->dpp_obj = obj;
227 	else
228 		pp->dpp_obj++;
229 
230 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
231 	    NULL) == 0)
232 		pp->dpp_stret[0] = sym.st_value;
233 	else
234 		pp->dpp_stret[0] = 0;
235 
236 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
237 	    NULL) == 0)
238 		pp->dpp_stret[1] = sym.st_value;
239 	else
240 		pp->dpp_stret[1] = 0;
241 
242 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
243 	    NULL) == 0)
244 		pp->dpp_stret[2] = sym.st_value;
245 	else
246 		pp->dpp_stret[2] = 0;
247 
248 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
249 	    NULL) == 0)
250 		pp->dpp_stret[3] = sym.st_value;
251 	else
252 		pp->dpp_stret[3] = 0;
253 
254 	dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
255 	    (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
256 	    (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
257 
258 	/*
259 	 * If pp->dpp_func contains any globbing meta-characters, we need
260 	 * to iterate over the symbol table and compare each function name
261 	 * against the pattern.
262 	 */
263 	if (!strisglob(pp->dpp_func)) {
264 		/*
265 		 * If we fail to lookup the symbol, try interpreting the
266 		 * function as the special "-" function that indicates that the
267 		 * probe name should be interpreted as a absolute virtual
268 		 * address. If that fails and we were matching a specific
269 		 * function in a specific module, report the error, otherwise
270 		 * just fail silently in the hopes that some other object will
271 		 * contain the desired symbol.
272 		 */
273 		if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
274 		    pp->dpp_func, &sym, NULL) != 0) {
275 			if (strcmp("-", pp->dpp_func) == 0) {
276 				sym.st_name = 0;
277 				sym.st_info =
278 				    GELF_ST_INFO(STB_LOCAL, STT_FUNC);
279 				sym.st_other = 0;
280 				sym.st_value = 0;
281 				sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
282 				    PR_MODEL_ILP32 ? -1U : -1ULL;
283 
284 			} else if (!strisglob(pp->dpp_mod)) {
285 				dt_proc_release(pp->dpp_dtp, pp->dpp_pr);
286 				xyerror(D_PROC_FUNC, "failed to lookup '%s'\n",
287 				    pp->dpp_func);
288 			} else {
289 				return;
290 			}
291 		}
292 
293 		/*
294 		 * Only match defined functions of non-zero size.
295 		 */
296 		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
297 		    sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
298 			return;
299 
300 		/*
301 		 * We don't instrument PLTs -- they're dynamically rewritten,
302 		 * and, so, inherently dicey to instrument.
303 		 */
304 		if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
305 			return;
306 
307 		(void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
308 		    DTRACE_FUNCNAMELEN, &sym);
309 
310 		dt_pid_per_sym(pp, &sym, pp->dpp_func);
311 	} else {
312 		uint_t nmatches = pp->dpp_nmatches;
313 
314 		(void) Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
315 		    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp);
316 
317 		if (nmatches == pp->dpp_nmatches) {
318 			/*
319 			 * If we didn't match anything in the PR_SYMTAB, try
320 			 * the PR_DYNSYM.
321 			 */
322 			(void) Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
323 			    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp);
324 		}
325 	}
326 }
327 
328 static int
329 dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
330 {
331 	dt_pid_probe_t *pp = arg;
332 
333 	if (gmatch(obj, pp->dpp_mod)) {
334 		dt_pid_per_mod(pp, pmp, obj);
335 	} else {
336 		char name[DTRACE_MODNAMELEN];
337 
338 		(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
339 
340 		if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
341 			pp->dpp_obj = obj;
342 		else
343 			pp->dpp_obj++;
344 
345 		(void) snprintf(name, sizeof (name), "LM%lx`%s",
346 		    pp->dpp_lmid, obj);
347 
348 		if (gmatch(name, pp->dpp_mod))
349 			dt_pid_per_mod(pp, pmp, obj);
350 	}
351 
352 	return (0);
353 }
354 
355 static const prmap_t *
356 dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
357 {
358 	char m[MAXPATHLEN];
359 	Lmid_t lmid = PR_LMID_EVERY;
360 	const char *obj;
361 	const prmap_t *pmp;
362 
363 	/*
364 	 * Pick apart the link map from the library name.
365 	 */
366 	if (strchr(pdp->dtpd_mod, '`') != NULL) {
367 		char *end;
368 
369 		if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
370 		    !isdigit(pdp->dtpd_mod[2]))
371 			return (NULL);
372 
373 		lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
374 
375 		obj = end + 1;
376 
377 		if (*end != '`' || strchr(obj, '`') != NULL)
378 			return (NULL);
379 
380 	} else {
381 		obj = pdp->dtpd_mod;
382 	}
383 
384 	if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
385 		return (NULL);
386 
387 	(void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
388 	if ((obj = strrchr(m, '/')) == NULL)
389 		obj = &m[0];
390 	else
391 		obj++;
392 
393 	(void) Plmid(P, pmp->pr_vaddr, &lmid);
394 	if (lmid == LM_ID_BASE)
395 		(void) strcpy(pdp->dtpd_mod, obj);
396 	else
397 		(void) snprintf(pdp->dtpd_mod, sizeof (pdp->dtpd_mod),
398 		    "LM%lx`%s", lmid, obj);
399 
400 	return (pmp);
401 }
402 
403 
404 static void
405 dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, pid_t pid)
406 {
407 	dt_pid_probe_t pp;
408 
409 	pp.dpp_pr = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 0);
410 	if (pp.dpp_pr == NULL)
411 		longjmp(dtp->dt_pcb->pcb_jmpbuf, EDT_COMPILER);
412 
413 	/*
414 	 * We can only trace dynamically-linked executables (since we've
415 	 * hidden some magic in ld.so.1 as well as libc.so.1).
416 	 */
417 	if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
418 		dt_proc_release(dtp, pp.dpp_pr);
419 		xyerror(D_PROC_DYN, "process %s is not a dynamically-linked "
420 		    "executable\n", &pdp->dtpd_provider[3]);
421 	}
422 
423 	pp.dpp_dtp = dtp;
424 	pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
425 	pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
426 	pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
427 
428 	if (strcmp(pp.dpp_func, "-") == 0) {
429 		const prmap_t *aout, *pmp;
430 
431 		if (pdp->dtpd_mod[0] == '\0') {
432 			pp.dpp_mod = pdp->dtpd_mod;
433 			(void) strcpy(pdp->dtpd_mod, "a.out");
434 		} else if (strisglob(pp.dpp_mod) ||
435 		    (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
436 		    (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
437 		    aout->pr_vaddr != pmp->pr_vaddr) {
438 			dt_proc_release(dtp, pp.dpp_pr);
439 			xyerror(D_PROC_LIB, "only the a.out module is valid "
440 			    "with the '-' function\n");
441 		}
442 
443 		if (strisglob(pp.dpp_name)) {
444 			dt_proc_release(dtp, pp.dpp_pr);
445 			xyerror(D_PROC_NAME, "only individual addresses may "
446 			    "be specified with the '-' function\n");
447 		}
448 	}
449 
450 	/*
451 	 * If pp.dpp_mod contains any globbing meta-characters, we need
452 	 * to iterate over each module and compare its name against the
453 	 * pattern. An empty module name is treated as '*'.
454 	 */
455 	if (strisglob(pp.dpp_mod)) {
456 		(void) Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
457 	} else {
458 		const prmap_t *pmp;
459 		char *obj;
460 
461 		/*
462 		 * If can't find a matching module, don't sweat it -- either
463 		 * we'll fail the enabling because the probes don't exist or
464 		 * we'll wait for that module to come along.
465 		 */
466 		if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
467 			if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
468 				obj = pdp->dtpd_mod;
469 			else
470 				obj++;
471 
472 			dt_pid_per_mod(&pp, pmp, obj);
473 		}
474 	}
475 
476 	dt_proc_release(dtp, pp.dpp_pr);
477 }
478 
479 static int
480 dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
481 {
482 	struct ps_prochandle *P = data;
483 	GElf_Sym sym;
484 	prsyminfo_t sip;
485 	int fd;
486 	dof_helper_t dh;
487 	GElf_Half e_type;
488 	const char *mname;
489 	const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
490 	int i;
491 
492 	/*
493 	 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
494 	 * __SUNW_dof is for actively-loaded DOF sections. We try to force
495 	 * in both types of DOF section since the process may not yet have
496 	 * run the code to instantiate these providers.
497 	 */
498 	for (i = 0; i < 2; i++) {
499 		if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
500 		    &sip) != 0) {
501 			continue;
502 		}
503 
504 		if ((mname = strrchr(oname, '/')) == NULL)
505 			mname = oname;
506 		else
507 			mname++;
508 
509 		dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
510 
511 		if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
512 		    offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
513 			dt_dprintf("read of ELF header failed");
514 			continue;
515 		}
516 
517 		dh.dofhp_dof = sym.st_value;
518 		dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
519 
520 		if (sip.prs_lmid == 0) {
521 			(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
522 			    "%s", mname);
523 		} else {
524 			(void) snprintf(dh.dofhp_mod, sizeof (dh.dofhp_mod),
525 			    "LM%lx`%s", sip.prs_lmid, mname);
526 		}
527 
528 		if ((fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
529 			dt_dprintf("pr_open of helper device failed: %s\n",
530 			    strerror(errno));
531 			return (errno);
532 		}
533 
534 		(void) pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh));
535 
536 		if (pr_close(P, fd) != 0)
537 			return (errno);
538 	}
539 
540 	return (0);
541 }
542 
543 static int
544 dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dt_proc_t *dpr)
545 {
546 	struct ps_prochandle *P = dpr->dpr_proc;
547 	int err;
548 
549 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
550 
551 	(void) Pupdate_maps(P);
552 	err = Pobject_iter(P, dt_pid_usdt_mapping, P);
553 
554 	/*
555 	 * Put the module name in its canonical form.
556 	 */
557 	(void) dt_pid_fix_mod(pdp, P);
558 
559 	return (err);
560 }
561 
562 static pid_t
563 dt_pid_get_pid(dtrace_probedesc_t *pdp, int *errp)
564 {
565 	pid_t pid;
566 	char *c, *last = NULL, *end;
567 
568 	for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
569 		if (!isdigit(*c))
570 			last = c;
571 	}
572 
573 	if (last == NULL || (*(++last) == '\0')) {
574 		if (errp != NULL) {
575 			*errp = D_PROC_BADPROV;
576 			return (-1);
577 		}
578 		xyerror(D_PROC_BADPROV, "%s is not a valid provider\n",
579 		    pdp->dtpd_provider);
580 	}
581 
582 	errno = 0;
583 	pid = strtol(last, &end, 10);
584 
585 	if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
586 		if (errp != NULL) {
587 			*errp = D_PROC_BADPID;
588 			return (-1);
589 		}
590 		xyerror(D_PROC_BADPID, "%s does not contain a valid pid\n",
591 		    pdp->dtpd_provider);
592 	}
593 
594 	if (errp != NULL)
595 		*errp = 0;
596 
597 	return (pid);
598 }
599 
600 void
601 dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp)
602 {
603 	pid_t pid = dt_pid_get_pid(pdp, NULL);
604 	char provname[DTRACE_PROVNAMELEN];
605 	struct ps_prochandle *P;
606 	dt_proc_t *dpr;
607 	int err = 0;
608 
609 	if (dtp->dt_ftfd == -1) {
610 		if (dtp->dt_fterr == ENOENT) {
611 			xyerror(D_PROC_NODEV, "pid provider is not "
612 			    "installed on this system\n");
613 		} else {
614 			xyerror(D_PROC_NODEV, "pid provider is not "
615 			    "available: %s\n", strerror(dtp->dt_fterr));
616 		}
617 	}
618 
619 	(void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
620 
621 	if (strcmp(provname, pdp->dtpd_provider) == 0) {
622 		dt_pid_create_pid_probes(pdp, dtp, pid);
623 	} else {
624 		if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL)
625 			longjmp(dtp->dt_pcb->pcb_jmpbuf, EDT_COMPILER);
626 
627 		dpr = dt_proc_lookup(dtp, P, 0);
628 		assert(dpr != NULL);
629 
630 		(void) pthread_mutex_lock(&dpr->dpr_lock);
631 
632 		if (!dpr->dpr_usdt) {
633 			err = dt_pid_create_usdt_probes(pdp, dpr);
634 			dpr->dpr_usdt = B_TRUE;
635 		}
636 
637 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
638 
639 		dt_proc_release(dtp, P);
640 
641 		if (err != 0)
642 			xyerror(D_PROC_USDT, "failed to instantiate probes "
643 			    "for PID %d: %s", (int)pid, strerror(err));
644 	}
645 }
646 
647 void
648 dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
649 {
650 	dtrace_prog_t *pgp;
651 	dt_stmt_t *stp;
652 	char provname[DTRACE_PROVNAMELEN];
653 	dtrace_probedesc_t *pdp, pd;
654 	pid_t pid;
655 	int err;
656 	int found = B_FALSE;
657 
658 	for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
659 	    pgp = dt_list_next(pgp)) {
660 
661 		for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
662 		    stp = dt_list_next(stp)) {
663 
664 			pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
665 			pid = dt_pid_get_pid(pdp, &err);
666 			if (err != 0 || pid != dpr->dpr_pid)
667 				continue;
668 
669 			found = B_TRUE;
670 
671 			pd = *pdp;
672 
673 			(void) snprintf(provname, sizeof (provname), "pid%d",
674 			    (int)pid);
675 
676 			if (strcmp(provname, pdp->dtpd_provider) == 0)
677 				dt_pid_create_pid_probes(&pd, dtp, pid);
678 			else
679 				(void) dt_pid_create_usdt_probes(&pd, dpr);
680 		}
681 	}
682 
683 	if (found) {
684 		/*
685 		 * Give DTrace a shot to the ribs to get it to check
686 		 * out the newly created probes.
687 		 */
688 		(void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
689 	}
690 }
691