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