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