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