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