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