xref: /freebsd/cddl/contrib/opensolaris/lib/libdtrace/common/dt_pid.c (revision ea43014585ec4131d1d7a7f027110f12781c99cc)
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  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
28  */
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 #if defined(sun)
37 #include <alloca.h>
38 #endif
39 #include <libgen.h>
40 #include <stddef.h>
41 #include <sys/sysmacros.h>
42 
43 #include <dt_impl.h>
44 #include <dt_program.h>
45 #include <dt_pid.h>
46 #include <dt_string.h>
47 #if !defined(sun)
48 #include <libproc_compat.h>
49 #endif
50 #include <dt_module.h>
51 
52 typedef struct dt_pid_probe {
53 	dtrace_hdl_t *dpp_dtp;
54 	dt_pcb_t *dpp_pcb;
55 	dt_proc_t *dpp_dpr;
56 	struct ps_prochandle *dpp_pr;
57 	const char *dpp_mod;
58 	char *dpp_func;
59 	const char *dpp_name;
60 	const char *dpp_obj;
61 	uintptr_t dpp_pc;
62 	size_t dpp_size;
63 	Lmid_t dpp_lmid;
64 	uint_t dpp_nmatches;
65 	uint64_t dpp_stret[4];
66 	GElf_Sym dpp_last;
67 	uint_t dpp_last_taken;
68 } dt_pid_probe_t;
69 
70 /*
71  * Compose the lmid and object name into the canonical representation. We
72  * omit the lmid for the default link map for convenience.
73  */
74 static void
75 dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
76 {
77 #if defined(sun)
78 	if (lmid == LM_ID_BASE)
79 		(void) strncpy(buf, obj, len);
80 	else
81 		(void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
82 #else
83 	(void) strncpy(buf, obj, len);
84 #endif
85 }
86 
87 static int
88 dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
89     fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
90 {
91 	va_list ap;
92 	int len;
93 
94 	if (ftp != NULL)
95 		dt_free(dtp, ftp);
96 
97 	va_start(ap, fmt);
98 	if (pcb == NULL) {
99 		assert(dpr != NULL);
100 		len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
101 		    fmt, ap);
102 		assert(len >= 2);
103 		if (dpr->dpr_errmsg[len - 2] == '\n')
104 			dpr->dpr_errmsg[len - 2] = '\0';
105 	} else {
106 		dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
107 		    pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
108 	}
109 	va_end(ap);
110 
111 	return (1);
112 }
113 
114 static int
115 dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
116 {
117 	dtrace_hdl_t *dtp = pp->dpp_dtp;
118 	dt_pcb_t *pcb = pp->dpp_pcb;
119 	dt_proc_t *dpr = pp->dpp_dpr;
120 	fasttrap_probe_spec_t *ftp;
121 	uint64_t off;
122 	char *end;
123 	uint_t nmatches = 0;
124 	ulong_t sz;
125 	int glob, err;
126 	int isdash = strcmp("-", func) == 0;
127 	pid_t pid;
128 
129 #if defined(sun)
130 	pid = Pstatus(pp->dpp_pr)->pr_pid;
131 #else
132 	pid = proc_getpid(pp->dpp_pr);
133 #endif
134 
135 	dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
136 	    func, pp->dpp_name);
137 
138 	sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
139 	    (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
140 
141 	if ((ftp = dt_alloc(dtp, sz)) == NULL) {
142 		dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
143 		return (1); /* errno is set for us */
144 	}
145 
146 	ftp->ftps_pid = pid;
147 	(void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
148 
149 	dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
150 	    pp->dpp_obj);
151 
152 	if (!isdash && gmatch("return", pp->dpp_name)) {
153 		if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp,
154 		    pp->dpp_stret) < 0) {
155 			return (dt_pid_error(dtp, pcb, dpr, ftp,
156 			    D_PROC_CREATEFAIL, "failed to create return probe "
157 			    "for '%s': %s", func,
158 			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
159 		}
160 
161 		nmatches++;
162 	}
163 
164 	if (!isdash && gmatch("entry", pp->dpp_name)) {
165 		if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
166 			return (dt_pid_error(dtp, pcb, dpr, ftp,
167 			    D_PROC_CREATEFAIL, "failed to create entry probe "
168 			    "for '%s': %s", func,
169 			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
170 		}
171 
172 		nmatches++;
173 	}
174 
175 	glob = strisglob(pp->dpp_name);
176 	if (!glob && nmatches == 0) {
177 		off = strtoull(pp->dpp_name, &end, 16);
178 		if (*end != '\0') {
179 			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
180 			    "'%s' is an invalid probe name", pp->dpp_name));
181 		}
182 
183 		if (off >= symp->st_size) {
184 			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
185 			    "offset 0x%llx outside of function '%s'",
186 			    (u_longlong_t)off, func));
187 		}
188 
189 		err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
190 		    symp, off);
191 
192 		if (err == DT_PROC_ERR) {
193 			return (dt_pid_error(dtp, pcb, dpr, ftp,
194 			    D_PROC_CREATEFAIL, "failed to create probe at "
195 			    "'%s+0x%llx': %s", func, (u_longlong_t)off,
196 			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
197 		}
198 
199 		if (err == DT_PROC_ALIGN) {
200 			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
201 			    "offset 0x%llx is not aligned on an instruction",
202 			    (u_longlong_t)off));
203 		}
204 
205 		nmatches++;
206 
207 	} else if (glob && !isdash) {
208 		if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
209 		    pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
210 			return (dt_pid_error(dtp, pcb, dpr, ftp,
211 			    D_PROC_CREATEFAIL,
212 			    "failed to create offset probes in '%s': %s", func,
213 			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
214 		}
215 
216 		nmatches++;
217 	}
218 
219 	pp->dpp_nmatches += nmatches;
220 
221 	dt_free(dtp, ftp);
222 
223 	return (0);
224 }
225 
226 static int
227 dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
228 {
229 	dt_pid_probe_t *pp = arg;
230 
231 	if (symp->st_shndx == SHN_UNDEF)
232 		return (0);
233 
234 	if (symp->st_size == 0) {
235 		dt_dprintf("st_size of %s is zero\n", func);
236 		return (0);
237 	}
238 
239 	if (pp->dpp_last_taken == 0 ||
240 	    symp->st_value != pp->dpp_last.st_value ||
241 	    symp->st_size != pp->dpp_last.st_size) {
242 		/*
243 		 * Due to 4524008, _init and _fini may have a bloated st_size.
244 		 * While this bug has been fixed for a while, old binaries
245 		 * may exist that still exhibit this problem. As a result, we
246 		 * don't match _init and _fini though we allow users to
247 		 * specify them explicitly.
248 		 */
249 		if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
250 			return (0);
251 
252 		if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
253 			pp->dpp_last = *symp;
254 			return (dt_pid_per_sym(pp, symp, func));
255 		}
256 	}
257 
258 	return (0);
259 }
260 
261 static int
262 dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
263 {
264 	dt_pid_probe_t *pp = arg;
265 	dtrace_hdl_t *dtp = pp->dpp_dtp;
266 	dt_pcb_t *pcb = pp->dpp_pcb;
267 	dt_proc_t *dpr = pp->dpp_dpr;
268 	GElf_Sym sym;
269 
270 	if (obj == NULL)
271 		return (0);
272 
273 #if defined(sun)
274 	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
275 #endif
276 
277 
278 	if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
279 		pp->dpp_obj = obj;
280 	else
281 		pp->dpp_obj++;
282 #if defined(sun)
283 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
284 	    NULL) == 0)
285 		pp->dpp_stret[0] = sym.st_value;
286 	else
287 		pp->dpp_stret[0] = 0;
288 
289 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
290 	    NULL) == 0)
291 		pp->dpp_stret[1] = sym.st_value;
292 	else
293 		pp->dpp_stret[1] = 0;
294 
295 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
296 	    NULL) == 0)
297 		pp->dpp_stret[2] = sym.st_value;
298 	else
299 		pp->dpp_stret[2] = 0;
300 
301 	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
302 	    NULL) == 0)
303 		pp->dpp_stret[3] = sym.st_value;
304 	else
305 		pp->dpp_stret[3] = 0;
306 #else
307 	pp->dpp_stret[0] = 0;
308 	pp->dpp_stret[1] = 0;
309 	pp->dpp_stret[2] = 0;
310 	pp->dpp_stret[3] = 0;
311 #endif
312 
313 	dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
314 	    (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
315 	    (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
316 
317 	/*
318 	 * If pp->dpp_func contains any globbing meta-characters, we need
319 	 * to iterate over the symbol table and compare each function name
320 	 * against the pattern.
321 	 */
322 	if (!strisglob(pp->dpp_func)) {
323 		/*
324 		 * If we fail to lookup the symbol, try interpreting the
325 		 * function as the special "-" function that indicates that the
326 		 * probe name should be interpreted as a absolute virtual
327 		 * address. If that fails and we were matching a specific
328 		 * function in a specific module, report the error, otherwise
329 		 * just fail silently in the hopes that some other object will
330 		 * contain the desired symbol.
331 		 */
332 		if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
333 		    pp->dpp_func, &sym, NULL) != 0) {
334 			if (strcmp("-", pp->dpp_func) == 0) {
335 				sym.st_name = 0;
336 				sym.st_info =
337 				    GELF_ST_INFO(STB_LOCAL, STT_FUNC);
338 				sym.st_other = 0;
339 				sym.st_value = 0;
340 #if defined(sun)
341 				sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
342 				    PR_MODEL_ILP32 ? -1U : -1ULL;
343 #else
344 				sym.st_size = ~((Elf64_Xword) 0);
345 #endif
346 
347 			} else if (!strisglob(pp->dpp_mod)) {
348 				return (dt_pid_error(dtp, pcb, dpr, NULL,
349 				    D_PROC_FUNC,
350 				    "failed to lookup '%s' in module '%s'",
351 				    pp->dpp_func, pp->dpp_mod));
352 			} else {
353 				return (0);
354 			}
355 		}
356 
357 		/*
358 		 * Only match defined functions of non-zero size.
359 		 */
360 		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
361 		    sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
362 			return (0);
363 
364 		/*
365 		 * We don't instrument PLTs -- they're dynamically rewritten,
366 		 * and, so, inherently dicey to instrument.
367 		 */
368 #ifdef DOODAD
369 		if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
370 			return (0);
371 #endif
372 
373 		(void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
374 		    DTRACE_FUNCNAMELEN, &sym);
375 
376 		return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
377 	} else {
378 		uint_t nmatches = pp->dpp_nmatches;
379 
380 		if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
381 		    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
382 			return (1);
383 
384 		if (nmatches == pp->dpp_nmatches) {
385 			/*
386 			 * If we didn't match anything in the PR_SYMTAB, try
387 			 * the PR_DYNSYM.
388 			 */
389 			if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
390 			    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
391 				return (1);
392 		}
393 	}
394 
395 	return (0);
396 }
397 
398 static int
399 dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
400 {
401 	char name[DTRACE_MODNAMELEN];
402 	dt_pid_probe_t *pp = arg;
403 
404 	if (gmatch(obj, pp->dpp_mod))
405 		return (dt_pid_per_mod(pp, pmp, obj));
406 
407 #if defined(sun)
408 	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
409 #else
410 	pp->dpp_lmid = 0;
411 #endif
412 
413 	if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
414 		pp->dpp_obj = obj;
415 	else
416 		pp->dpp_obj++;
417 
418 	if (gmatch(pp->dpp_obj, pp->dpp_mod))
419 		return (dt_pid_per_mod(pp, pmp, obj));
420 
421 #if defined(sun)
422 	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
423 #endif
424 
425 	dt_pid_objname(name, sizeof (name), pp->dpp_lmid, pp->dpp_obj);
426 
427 	if (gmatch(name, pp->dpp_mod))
428 		return (dt_pid_per_mod(pp, pmp, obj));
429 
430 	return (0);
431 }
432 
433 static const prmap_t *
434 dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
435 {
436 	char m[MAXPATHLEN];
437 #if defined(sun)
438 	Lmid_t lmid = PR_LMID_EVERY;
439 #else
440 	Lmid_t lmid = 0;
441 #endif
442 	const char *obj;
443 	const prmap_t *pmp;
444 
445 #if defined(sun)
446 	/*
447 	 * Pick apart the link map from the library name.
448 	 */
449 	if (strchr(pdp->dtpd_mod, '`') != NULL) {
450 		char *end;
451 
452 		if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
453 		    !isdigit(pdp->dtpd_mod[2]))
454 			return (NULL);
455 
456 		lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
457 
458 		obj = end + 1;
459 
460 		if (*end != '`' || strchr(obj, '`') != NULL)
461 			return (NULL);
462 
463 	} else {
464 		obj = pdp->dtpd_mod;
465 	}
466 #else
467 	obj = pdp->dtpd_mod;
468 #endif
469 
470 	if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
471 		return (NULL);
472 
473 #if defined(sun)
474 	(void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
475 	if ((obj = strrchr(m, '/')) == NULL)
476 		obj = &m[0];
477 	else
478 		obj++;
479 
480 	(void) Plmid(P, pmp->pr_vaddr, &lmid);
481 #endif
482 
483 	dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
484 
485 	return (pmp);
486 }
487 
488 
489 static int
490 dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
491     dt_pcb_t *pcb, dt_proc_t *dpr)
492 {
493 	dt_pid_probe_t pp;
494 	int ret = 0;
495 
496 	pp.dpp_dtp = dtp;
497 	pp.dpp_dpr = dpr;
498 	pp.dpp_pr = dpr->dpr_proc;
499 	pp.dpp_pcb = pcb;
500 
501 #ifdef DOODAD
502 	/*
503 	 * We can only trace dynamically-linked executables (since we've
504 	 * hidden some magic in ld.so.1 as well as libc.so.1).
505 	 */
506 	if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
507 		return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
508 		    "process %s is not a dynamically-linked executable",
509 		    &pdp->dtpd_provider[3]));
510 	}
511 #endif
512 
513 	pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
514 	pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
515 	pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
516 	pp.dpp_last_taken = 0;
517 
518 	if (strcmp(pp.dpp_func, "-") == 0) {
519 		const prmap_t *aout, *pmp;
520 
521 		if (pdp->dtpd_mod[0] == '\0') {
522 			pp.dpp_mod = pdp->dtpd_mod;
523 			(void) strcpy(pdp->dtpd_mod, "a.out");
524 		} else if (strisglob(pp.dpp_mod) ||
525 		    (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
526 		    (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
527 		    aout->pr_vaddr != pmp->pr_vaddr) {
528 			return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
529 			    "only the a.out module is valid with the "
530 			    "'-' function"));
531 		}
532 
533 		if (strisglob(pp.dpp_name)) {
534 			return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
535 			    "only individual addresses may be specified "
536 			    "with the '-' function"));
537 		}
538 	}
539 
540 	/*
541 	 * If pp.dpp_mod contains any globbing meta-characters, we need
542 	 * to iterate over each module and compare its name against the
543 	 * pattern. An empty module name is treated as '*'.
544 	 */
545 	if (strisglob(pp.dpp_mod)) {
546 		ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
547 	} else {
548 		const prmap_t *pmp;
549 		char *obj;
550 
551 		/*
552 		 * If we can't find a matching module, don't sweat it -- either
553 		 * we'll fail the enabling because the probes don't exist or
554 		 * we'll wait for that module to come along.
555 		 */
556 		if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
557 			if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
558 				obj = pdp->dtpd_mod;
559 			else
560 				obj++;
561 
562 			ret = dt_pid_per_mod(&pp, pmp, obj);
563 		}
564 	}
565 
566 	return (ret);
567 }
568 
569 static int
570 dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
571 {
572 	struct ps_prochandle *P = data;
573 	GElf_Sym sym;
574 #if defined(sun)
575 	prsyminfo_t sip;
576 #endif
577 	dof_helper_t dh;
578 	GElf_Half e_type;
579 	const char *mname;
580 	const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
581 	int i, fd = -1;
582 
583 	/*
584 	 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
585 	 * __SUNW_dof is for actively-loaded DOF sections. We try to force
586 	 * in both types of DOF section since the process may not yet have
587 	 * run the code to instantiate these providers.
588 	 */
589 	for (i = 0; i < 2; i++) {
590 		if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
591 		    &sip) != 0) {
592 			continue;
593 		}
594 
595 		if ((mname = strrchr(oname, '/')) == NULL)
596 			mname = oname;
597 		else
598 			mname++;
599 
600 		dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
601 
602 		if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
603 		    offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
604 			dt_dprintf("read of ELF header failed");
605 			continue;
606 		}
607 
608 		dh.dofhp_dof = sym.st_value;
609 		dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
610 
611 		dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
612 #if defined(sun)
613 		    sip.prs_lmid, mname);
614 #else
615 		    0, mname);
616 #endif
617 
618 #if defined(sun)
619 		if (fd == -1 &&
620 		    (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
621 			dt_dprintf("pr_open of helper device failed: %s\n",
622 			    strerror(errno));
623 			return (-1); /* errno is set for us */
624 		}
625 
626 		if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
627 			dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
628 #endif
629 	}
630 
631 #if defined(sun)
632 	if (fd != -1)
633 		(void) pr_close(P, fd);
634 #endif
635 
636 	return (0);
637 }
638 
639 static int
640 dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
641     dt_pcb_t *pcb, dt_proc_t *dpr)
642 {
643 	struct ps_prochandle *P = dpr->dpr_proc;
644 	int ret = 0;
645 
646 	assert(DT_MUTEX_HELD(&dpr->dpr_lock));
647 #if defined(sun)
648 	(void) Pupdate_maps(P);
649 	if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
650 		ret = -1;
651 		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
652 		    "failed to instantiate probes for pid %d: %s",
653 #if defined(sun)
654 		    (int)Pstatus(P)->pr_pid, strerror(errno));
655 #else
656 		    (int)proc_getpid(P), strerror(errno));
657 #endif
658 	}
659 #else
660 	ret = 0;
661 #endif
662 
663 	/*
664 	 * Put the module name in its canonical form.
665 	 */
666 	(void) dt_pid_fix_mod(pdp, P);
667 
668 	return (ret);
669 }
670 
671 static pid_t
672 dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
673     dt_proc_t *dpr)
674 {
675 	pid_t pid;
676 	char *c, *last = NULL, *end;
677 
678 	for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
679 		if (!isdigit(*c))
680 			last = c;
681 	}
682 
683 	if (last == NULL || (*(++last) == '\0')) {
684 		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
685 		    "'%s' is not a valid provider", pdp->dtpd_provider);
686 		return (-1);
687 	}
688 
689 	errno = 0;
690 	pid = strtol(last, &end, 10);
691 
692 	if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
693 		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
694 		    "'%s' does not contain a valid pid", pdp->dtpd_provider);
695 		return (-1);
696 	}
697 
698 	return (pid);
699 }
700 
701 int
702 dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
703 {
704 	char provname[DTRACE_PROVNAMELEN];
705 	struct ps_prochandle *P;
706 	dt_proc_t *dpr;
707 	pid_t pid;
708 	int err = 0;
709 
710 	assert(pcb != NULL);
711 
712 	if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
713 		return (-1);
714 
715 	if (dtp->dt_ftfd == -1) {
716 		if (dtp->dt_fterr == ENOENT) {
717 			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
718 			    "pid provider is not installed on this system");
719 		} else {
720 			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
721 			    "pid provider is not available: %s",
722 			    strerror(dtp->dt_fterr));
723 		}
724 
725 		return (-1);
726 	}
727 
728 	(void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
729 
730 	if (gmatch(provname, pdp->dtpd_provider) != 0) {
731 		if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
732 		    0)) == NULL) {
733 			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
734 			    "failed to grab process %d", (int)pid);
735 			return (-1);
736 		}
737 
738 		dpr = dt_proc_lookup(dtp, P, 0);
739 		assert(dpr != NULL);
740 		(void) pthread_mutex_lock(&dpr->dpr_lock);
741 
742 		if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
743 			/*
744 			 * Alert other retained enablings which may match
745 			 * against the newly created probes.
746 			 */
747 			(void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
748 		}
749 
750 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
751 		dt_proc_release(dtp, P);
752 	}
753 
754 	/*
755 	 * If it's not strictly a pid provider, we might match a USDT provider.
756 	 */
757 	if (strcmp(provname, pdp->dtpd_provider) != 0) {
758 		if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
759 			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
760 			    "failed to grab process %d", (int)pid);
761 			return (-1);
762 		}
763 
764 		dpr = dt_proc_lookup(dtp, P, 0);
765 		assert(dpr != NULL);
766 		(void) pthread_mutex_lock(&dpr->dpr_lock);
767 
768 		if (!dpr->dpr_usdt) {
769 			err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
770 			dpr->dpr_usdt = B_TRUE;
771 		}
772 
773 		(void) pthread_mutex_unlock(&dpr->dpr_lock);
774 		dt_proc_release(dtp, P);
775 	}
776 
777 	return (err ? -1 : 0);
778 }
779 
780 int
781 dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
782 {
783 	dtrace_enable_io_t args;
784 	dtrace_prog_t *pgp;
785 	dt_stmt_t *stp;
786 	dtrace_probedesc_t *pdp, pd;
787 	pid_t pid;
788 	int ret = 0, found = B_FALSE;
789 	char provname[DTRACE_PROVNAMELEN];
790 
791 	(void) snprintf(provname, sizeof (provname), "pid%d",
792 	    (int)dpr->dpr_pid);
793 
794 	for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
795 	    pgp = dt_list_next(pgp)) {
796 
797 		for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
798 		    stp = dt_list_next(stp)) {
799 
800 			pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
801 			pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
802 			if (pid != dpr->dpr_pid)
803 				continue;
804 
805 			found = B_TRUE;
806 
807 			pd = *pdp;
808 
809 			if (gmatch(provname, pdp->dtpd_provider) != 0 &&
810 			    dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
811 				ret = 1;
812 
813 			/*
814 			 * If it's not strictly a pid provider, we might match
815 			 * a USDT provider.
816 			 */
817 			if (strcmp(provname, pdp->dtpd_provider) != 0 &&
818 			    dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
819 				ret = 1;
820 		}
821 	}
822 
823 	if (found) {
824 		/*
825 		 * Give DTrace a shot to the ribs to get it to check
826 		 * out the newly created probes.
827 		 */
828 		args.dof = NULL;
829 		args.n_matched = 0;
830 		(void) dt_ioctl(dtp, DTRACEIOC_ENABLE, &args);
831 	}
832 
833 	return (ret);
834 }
835 
836 /*
837  * libdtrace has a backroom deal with us to ask us for type information on
838  * behalf of pid provider probes when fasttrap doesn't return any type
839  * information. Instead we'll look up the module and see if there is type
840  * information available. However, if there is no type information available due
841  * to a lack of CTF data, then we want to make sure that DTrace still carries on
842  * in face of that. As such we don't have a meaningful exit code about failure.
843  * We emit information about why we failed to the dtrace debug log so someone
844  * can figure it out by asking nicely for DTRACE_DEBUG.
845  */
846 void
847 dt_pid_get_types(dtrace_hdl_t *dtp, const dtrace_probedesc_t *pdp,
848     dtrace_argdesc_t *adp, int *nargs)
849 {
850 	dt_module_t *dmp;
851 	ctf_file_t *fp;
852 	ctf_funcinfo_t f;
853 	ctf_id_t argv[32];
854 	GElf_Sym sym;
855 #if defined(sun)
856 	prsyminfo_t si;
857 #else
858 	void *si;
859 #endif
860 	struct ps_prochandle *p;
861 	int i, args;
862 	char buf[DTRACE_ARGTYPELEN];
863 	const char *mptr;
864 	char *eptr;
865 	int ret = 0;
866 	int argc = sizeof (argv) / sizeof (ctf_id_t);
867 	Lmid_t lmid;
868 
869 	/* Set up a potential outcome */
870 	args = *nargs;
871 	*nargs = 0;
872 
873 	/*
874 	 * If we don't have an entry or return probe then we can just stop right
875 	 * now as we don't have arguments for offset probes.
876 	 */
877 	if (strcmp(pdp->dtpd_name, "entry") != 0 &&
878 	    strcmp(pdp->dtpd_name, "return") != 0)
879 		return;
880 
881 	dmp = dt_module_create(dtp, pdp->dtpd_provider);
882 	if (dmp == NULL) {
883 		dt_dprintf("failed to find module for %s\n",
884 		    pdp->dtpd_provider);
885 		return;
886 	}
887 	if (dt_module_load(dtp, dmp) != 0) {
888 		dt_dprintf("failed to load module for %s\n",
889 		    pdp->dtpd_provider);
890 		return;
891 	}
892 
893 	/*
894 	 * We may be working with a module that doesn't have ctf. If that's the
895 	 * case then we just return now and move on with life.
896 	 */
897 	fp = dt_module_getctflib(dtp, dmp, pdp->dtpd_mod);
898 	if (fp == NULL) {
899 		dt_dprintf("no ctf container for  %s\n",
900 		    pdp->dtpd_mod);
901 		return;
902 	}
903 	p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
904 	if (p == NULL) {
905 		dt_dprintf("failed to grab pid\n");
906 		return;
907 	}
908 	dt_proc_lock(dtp, p);
909 
910 	/*
911 	 * Check to see if the D module has a link map ID and separate that out
912 	 * for properly interrogating libproc.
913 	 */
914 	if ((mptr = strchr(pdp->dtpd_mod, '`')) != NULL) {
915 		if (strlen(pdp->dtpd_mod) < 3) {
916 			dt_dprintf("found weird modname with linkmap, "
917 			    "aborting: %s\n", pdp->dtpd_mod);
918 			goto out;
919 		}
920 		if (pdp->dtpd_mod[0] != 'L' || pdp->dtpd_mod[1] != 'M') {
921 			dt_dprintf("missing leading 'LM', "
922 			    "aborting: %s\n", pdp->dtpd_mod);
923 			goto out;
924 		}
925 		errno = 0;
926 		lmid = strtol(pdp->dtpd_mod + 2, &eptr, 16);
927 		if (errno == ERANGE || eptr != mptr) {
928 			dt_dprintf("failed to parse out lmid, aborting: %s\n",
929 			    pdp->dtpd_mod);
930 			goto out;
931 		}
932 		mptr++;
933 	} else {
934 		mptr = pdp->dtpd_mod;
935 		lmid = 0;
936 	}
937 
938 	if (Pxlookup_by_name(p, lmid, mptr, pdp->dtpd_func,
939 	    &sym, &si) != 0) {
940 		dt_dprintf("failed to find function %s in %s`%s\n",
941 		    pdp->dtpd_func, pdp->dtpd_provider, pdp->dtpd_mod);
942 		goto out;
943 	}
944 #if defined(sun)
945 	if (ctf_func_info(fp, si.prs_id, &f) == CTF_ERR) {
946 		dt_dprintf("failed to get ctf information for %s in %s`%s\n",
947 		    pdp->dtpd_func, pdp->dtpd_provider, pdp->dtpd_mod);
948 		goto out;
949 	}
950 #endif
951 
952 	(void) snprintf(buf, sizeof (buf), "%s`%s", pdp->dtpd_provider,
953 	    pdp->dtpd_mod);
954 
955 	if (strcmp(pdp->dtpd_name, "return") == 0) {
956 		if (args < 2)
957 			goto out;
958 
959 		bzero(adp, sizeof (dtrace_argdesc_t));
960 		adp->dtargd_ndx = 0;
961 		adp->dtargd_id = pdp->dtpd_id;
962 		adp->dtargd_mapping = adp->dtargd_ndx;
963 		/*
964 		 * We explicitly leave out the library here, we only care that
965 		 * it is some int. We are assuming that there is no ctf
966 		 * container in here that is lying about what an int is.
967 		 */
968 		(void) snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
969 		    "user %s`%s", pdp->dtpd_provider, "int");
970 		adp++;
971 		bzero(adp, sizeof (dtrace_argdesc_t));
972 		adp->dtargd_ndx = 1;
973 		adp->dtargd_id = pdp->dtpd_id;
974 		adp->dtargd_mapping = adp->dtargd_ndx;
975 		ret = snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
976 		    "userland ");
977 		(void) ctf_type_qname(fp, f.ctc_return, adp->dtargd_native +
978 		    ret, DTRACE_ARGTYPELEN - ret, buf);
979 		*nargs = 2;
980 #if defined(sun)
981 	} else {
982 		if (ctf_func_args(fp, si.prs_id, argc, argv) == CTF_ERR)
983 			goto out;
984 
985 		*nargs = MIN(args, f.ctc_argc);
986 		for (i = 0; i < *nargs; i++, adp++) {
987 			bzero(adp, sizeof (dtrace_argdesc_t));
988 			adp->dtargd_ndx = i;
989 			adp->dtargd_id = pdp->dtpd_id;
990 			adp->dtargd_mapping = adp->dtargd_ndx;
991 			ret = snprintf(adp->dtargd_native, DTRACE_ARGTYPELEN,
992 			    "userland ");
993 			(void) ctf_type_qname(fp, argv[i], adp->dtargd_native +
994 			    ret, DTRACE_ARGTYPELEN - ret, buf);
995 		}
996 #endif
997 	}
998 out:
999 	dt_proc_unlock(dtp, p);
1000 	dt_proc_release(dtp, p);
1001 }
1002