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