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