dt_pid.c revision 210767
1178479Sjb/*
2178479Sjb * CDDL HEADER START
3178479Sjb *
4178479Sjb * The contents of this file are subject to the terms of the
5178479Sjb * Common Development and Distribution License (the "License").
6178479Sjb * You may not use this file except in compliance with the License.
7178479Sjb *
8178479Sjb * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9178479Sjb * or http://www.opensolaris.org/os/licensing.
10178479Sjb * See the License for the specific language governing permissions
11178479Sjb * and limitations under the License.
12178479Sjb *
13178479Sjb * When distributing Covered Code, include this CDDL HEADER in each
14178479Sjb * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15178479Sjb * If applicable, add the following below this CDDL HEADER, with the
16178479Sjb * fields enclosed by brackets "[]" replaced with your own identifying
17178479Sjb * information: Portions Copyright [yyyy] [name of copyright owner]
18178479Sjb *
19178479Sjb * CDDL HEADER END
20178479Sjb */
21178479Sjb
22178479Sjb/*
23210767Srpaulo * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24178479Sjb * Use is subject to license terms.
25178479Sjb */
26178479Sjb
27178479Sjb#include <assert.h>
28178479Sjb#include <strings.h>
29178479Sjb#include <stdlib.h>
30178479Sjb#include <stdio.h>
31178479Sjb#include <errno.h>
32178479Sjb#include <ctype.h>
33178568Sjb#if defined(sun)
34178479Sjb#include <alloca.h>
35178568Sjb#endif
36178479Sjb#include <libgen.h>
37178479Sjb#include <stddef.h>
38178479Sjb
39178479Sjb#include <dt_impl.h>
40178479Sjb#include <dt_program.h>
41178479Sjb#include <dt_pid.h>
42178479Sjb#include <dt_string.h>
43178479Sjb
44178479Sjbtypedef struct dt_pid_probe {
45178479Sjb	dtrace_hdl_t *dpp_dtp;
46178479Sjb	dt_pcb_t *dpp_pcb;
47178479Sjb	dt_proc_t *dpp_dpr;
48178479Sjb	struct ps_prochandle *dpp_pr;
49178479Sjb	const char *dpp_mod;
50178479Sjb	char *dpp_func;
51178479Sjb	const char *dpp_name;
52178479Sjb	const char *dpp_obj;
53178479Sjb	uintptr_t dpp_pc;
54178479Sjb	size_t dpp_size;
55178479Sjb	Lmid_t dpp_lmid;
56178479Sjb	uint_t dpp_nmatches;
57178479Sjb	uint64_t dpp_stret[4];
58178479Sjb	GElf_Sym dpp_last;
59178479Sjb	uint_t dpp_last_taken;
60178479Sjb} dt_pid_probe_t;
61178479Sjb
62178479Sjb/*
63178479Sjb * Compose the lmid and object name into the canonical representation. We
64178479Sjb * omit the lmid for the default link map for convenience.
65178479Sjb */
66178479Sjbstatic void
67178479Sjbdt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
68178479Sjb{
69178568Sjb#if defined(sun)
70178479Sjb	if (lmid == LM_ID_BASE)
71178479Sjb		(void) strncpy(buf, obj, len);
72178479Sjb	else
73178479Sjb		(void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
74178568Sjb#else
75178568Sjb	(void) strncpy(buf, obj, len);
76178568Sjb#endif
77178479Sjb}
78178479Sjb
79178479Sjbstatic int
80178479Sjbdt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
81178479Sjb    fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
82178479Sjb{
83178479Sjb	va_list ap;
84178479Sjb	int len;
85178479Sjb
86178479Sjb	if (ftp != NULL)
87178479Sjb		dt_free(dtp, ftp);
88178479Sjb
89178479Sjb	va_start(ap, fmt);
90178479Sjb	if (pcb == NULL) {
91178479Sjb		assert(dpr != NULL);
92178479Sjb		len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
93178479Sjb		    fmt, ap);
94178479Sjb		assert(len >= 2);
95178479Sjb		if (dpr->dpr_errmsg[len - 2] == '\n')
96178479Sjb			dpr->dpr_errmsg[len - 2] = '\0';
97178479Sjb	} else {
98178479Sjb		dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
99178479Sjb		    pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
100178479Sjb	}
101178479Sjb	va_end(ap);
102178479Sjb
103178479Sjb	return (1);
104178479Sjb}
105178479Sjb
106178479Sjbstatic int
107178479Sjbdt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
108178479Sjb{
109178479Sjb	dtrace_hdl_t *dtp = pp->dpp_dtp;
110178479Sjb	dt_pcb_t *pcb = pp->dpp_pcb;
111178479Sjb	dt_proc_t *dpr = pp->dpp_dpr;
112178479Sjb	fasttrap_probe_spec_t *ftp;
113178479Sjb	uint64_t off;
114178479Sjb	char *end;
115178479Sjb	uint_t nmatches = 0;
116178479Sjb	ulong_t sz;
117178479Sjb	int glob, err;
118178479Sjb	int isdash = strcmp("-", func) == 0;
119178479Sjb	pid_t pid;
120178479Sjb
121178568Sjb#if defined(sun)
122178479Sjb	pid = Pstatus(pp->dpp_pr)->pr_pid;
123178568Sjb#else
124178568Sjb	pid = proc_getpid(pp->dpp_pr);
125178568Sjb#endif
126178479Sjb
127178479Sjb	dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
128178479Sjb	    func, pp->dpp_name);
129178479Sjb
130178479Sjb	sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
131178479Sjb	    (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
132178479Sjb
133178479Sjb	if ((ftp = dt_alloc(dtp, sz)) == NULL) {
134178479Sjb		dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
135178479Sjb		return (1); /* errno is set for us */
136178479Sjb	}
137178479Sjb
138178479Sjb	ftp->ftps_pid = pid;
139178479Sjb	(void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
140178479Sjb
141178479Sjb	dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
142178479Sjb	    pp->dpp_obj);
143178479Sjb
144178479Sjb	if (!isdash && gmatch("return", pp->dpp_name)) {
145178568Sjb#ifdef DOODAD
146178479Sjb		if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp,
147178479Sjb		    pp->dpp_stret) < 0) {
148178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp,
149178479Sjb			    D_PROC_CREATEFAIL, "failed to create return probe "
150178479Sjb			    "for '%s': %s", func,
151178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
152178479Sjb		}
153178568Sjb#endif
154178479Sjb
155178479Sjb		nmatches++;
156178479Sjb	}
157178479Sjb
158178479Sjb	if (!isdash && gmatch("entry", pp->dpp_name)) {
159178568Sjb#ifdef DOODAD
160178479Sjb		if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
161178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp,
162178479Sjb			    D_PROC_CREATEFAIL, "failed to create entry probe "
163178479Sjb			    "for '%s': %s", func,
164178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
165178479Sjb		}
166178568Sjb#endif
167178479Sjb
168178479Sjb		nmatches++;
169178479Sjb	}
170178479Sjb
171178479Sjb	glob = strisglob(pp->dpp_name);
172178479Sjb	if (!glob && nmatches == 0) {
173178479Sjb		off = strtoull(pp->dpp_name, &end, 16);
174178479Sjb		if (*end != '\0') {
175178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
176178479Sjb			    "'%s' is an invalid probe name", pp->dpp_name));
177178479Sjb		}
178178479Sjb
179178479Sjb		if (off >= symp->st_size) {
180178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
181178479Sjb			    "offset 0x%llx outside of function '%s'",
182178479Sjb			    (u_longlong_t)off, func));
183178479Sjb		}
184178479Sjb
185178568Sjb#ifdef DOODAD
186178479Sjb		err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
187178479Sjb		    symp, off);
188178568Sjb#endif
189178479Sjb
190178479Sjb		if (err == DT_PROC_ERR) {
191178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp,
192178479Sjb			    D_PROC_CREATEFAIL, "failed to create probe at "
193178479Sjb			    "'%s+0x%llx': %s", func, (u_longlong_t)off,
194178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
195178479Sjb		}
196178479Sjb
197178479Sjb		if (err == DT_PROC_ALIGN) {
198178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
199178479Sjb			    "offset 0x%llx is not aligned on an instruction",
200178479Sjb			    (u_longlong_t)off));
201178479Sjb		}
202178479Sjb
203178479Sjb		nmatches++;
204178479Sjb
205178479Sjb	} else if (glob && !isdash) {
206178568Sjb#ifdef DOODAD
207178479Sjb		if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
208178479Sjb		    pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
209178479Sjb			return (dt_pid_error(dtp, pcb, dpr, ftp,
210178479Sjb			    D_PROC_CREATEFAIL,
211178479Sjb			    "failed to create offset probes in '%s': %s", func,
212178479Sjb			    dtrace_errmsg(dtp, dtrace_errno(dtp))));
213178479Sjb		}
214178568Sjb#endif
215178479Sjb
216178479Sjb		nmatches++;
217178479Sjb	}
218178479Sjb
219178479Sjb	pp->dpp_nmatches += nmatches;
220178479Sjb
221178479Sjb	dt_free(dtp, ftp);
222178479Sjb
223178479Sjb	return (0);
224178479Sjb}
225178479Sjb
226178479Sjbstatic int
227178479Sjbdt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
228178479Sjb{
229178479Sjb	dt_pid_probe_t *pp = arg;
230178479Sjb
231178479Sjb	if (symp->st_shndx == SHN_UNDEF)
232178479Sjb		return (0);
233178479Sjb
234178479Sjb	if (symp->st_size == 0) {
235178479Sjb		dt_dprintf("st_size of %s is zero\n", func);
236178479Sjb		return (0);
237178479Sjb	}
238178479Sjb
239178479Sjb	if (pp->dpp_last_taken == 0 ||
240178479Sjb	    symp->st_value != pp->dpp_last.st_value ||
241178479Sjb	    symp->st_size != pp->dpp_last.st_size) {
242178479Sjb		/*
243178479Sjb		 * Due to 4524008, _init and _fini may have a bloated st_size.
244178479Sjb		 * While this bug has been fixed for a while, old binaries
245178479Sjb		 * may exist that still exhibit this problem. As a result, we
246178479Sjb		 * don't match _init and _fini though we allow users to
247178479Sjb		 * specify them explicitly.
248178479Sjb		 */
249178479Sjb		if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
250178479Sjb			return (0);
251178479Sjb
252178479Sjb		if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
253178479Sjb			pp->dpp_last = *symp;
254178479Sjb			return (dt_pid_per_sym(pp, symp, func));
255178479Sjb		}
256178479Sjb	}
257178479Sjb
258178479Sjb	return (0);
259178479Sjb}
260178479Sjb
261178479Sjbstatic int
262178479Sjbdt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
263178479Sjb{
264178479Sjb	dt_pid_probe_t *pp = arg;
265178479Sjb	dtrace_hdl_t *dtp = pp->dpp_dtp;
266178479Sjb	dt_pcb_t *pcb = pp->dpp_pcb;
267178479Sjb	dt_proc_t *dpr = pp->dpp_dpr;
268178479Sjb	GElf_Sym sym;
269178479Sjb
270178479Sjb	if (obj == NULL)
271178479Sjb		return (0);
272178479Sjb
273178568Sjb#if defined(sun)
274178479Sjb	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
275178568Sjb#endif
276178568Sjb
277178479Sjb
278178479Sjb	if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
279178479Sjb		pp->dpp_obj = obj;
280178479Sjb	else
281178479Sjb		pp->dpp_obj++;
282178479Sjb
283178568Sjb#if defined(sun)
284178479Sjb	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
285178479Sjb	    NULL) == 0)
286178479Sjb		pp->dpp_stret[0] = sym.st_value;
287178479Sjb	else
288178479Sjb		pp->dpp_stret[0] = 0;
289178479Sjb
290178479Sjb	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
291178479Sjb	    NULL) == 0)
292178479Sjb		pp->dpp_stret[1] = sym.st_value;
293178479Sjb	else
294178479Sjb		pp->dpp_stret[1] = 0;
295178479Sjb
296178479Sjb	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
297178479Sjb	    NULL) == 0)
298178479Sjb		pp->dpp_stret[2] = sym.st_value;
299178479Sjb	else
300178479Sjb		pp->dpp_stret[2] = 0;
301178479Sjb
302178479Sjb	if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
303178479Sjb	    NULL) == 0)
304178479Sjb		pp->dpp_stret[3] = sym.st_value;
305178479Sjb	else
306178479Sjb		pp->dpp_stret[3] = 0;
307178568Sjb#else
308178568Sjb	if (proc_name2sym(pp->dpp_pr, obj, ".stret1", &sym) == 0)
309178568Sjb		pp->dpp_stret[0] = sym.st_value;
310178568Sjb	else
311178568Sjb		pp->dpp_stret[0] = 0;
312178479Sjb
313178568Sjb	if (proc_name2sym(pp->dpp_pr, obj, ".stret2", &sym) == 0)
314178568Sjb		pp->dpp_stret[1] = sym.st_value;
315178568Sjb	else
316178568Sjb		pp->dpp_stret[1] = 0;
317178568Sjb
318178568Sjb	if (proc_name2sym(pp->dpp_pr, obj, ".stret4", &sym) == 0)
319178568Sjb		pp->dpp_stret[2] = sym.st_value;
320178568Sjb	else
321178568Sjb		pp->dpp_stret[2] = 0;
322178568Sjb
323178568Sjb	if (proc_name2sym(pp->dpp_pr, obj, ".stret8", &sym) == 0)
324178568Sjb		pp->dpp_stret[3] = sym.st_value;
325178568Sjb	else
326178568Sjb		pp->dpp_stret[3] = 0;
327178568Sjb#endif
328178568Sjb
329178479Sjb	dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
330178479Sjb	    (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
331178479Sjb	    (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
332178479Sjb
333178479Sjb	/*
334178479Sjb	 * If pp->dpp_func contains any globbing meta-characters, we need
335178479Sjb	 * to iterate over the symbol table and compare each function name
336178479Sjb	 * against the pattern.
337178479Sjb	 */
338178479Sjb	if (!strisglob(pp->dpp_func)) {
339178479Sjb		/*
340178479Sjb		 * If we fail to lookup the symbol, try interpreting the
341178479Sjb		 * function as the special "-" function that indicates that the
342178479Sjb		 * probe name should be interpreted as a absolute virtual
343178479Sjb		 * address. If that fails and we were matching a specific
344178479Sjb		 * function in a specific module, report the error, otherwise
345178479Sjb		 * just fail silently in the hopes that some other object will
346178479Sjb		 * contain the desired symbol.
347178479Sjb		 */
348178568Sjb#if defined(sun)
349178479Sjb		if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
350178479Sjb		    pp->dpp_func, &sym, NULL) != 0) {
351178568Sjb#else
352178568Sjb		if (proc_name2sym(pp->dpp_pr, obj, pp->dpp_func, &sym) != 0) {
353178568Sjb#endif
354178479Sjb			if (strcmp("-", pp->dpp_func) == 0) {
355178479Sjb				sym.st_name = 0;
356178479Sjb				sym.st_info =
357178479Sjb				    GELF_ST_INFO(STB_LOCAL, STT_FUNC);
358178479Sjb				sym.st_other = 0;
359178479Sjb				sym.st_value = 0;
360178568Sjb#if defined(sun)
361178479Sjb				sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
362178479Sjb				    PR_MODEL_ILP32 ? -1U : -1ULL;
363178568Sjb#else
364178568Sjb				sym.st_size = ~((Elf64_Xword) 0);
365178568Sjb#endif
366178479Sjb
367178479Sjb			} else if (!strisglob(pp->dpp_mod)) {
368178479Sjb				return (dt_pid_error(dtp, pcb, dpr, NULL,
369178479Sjb				    D_PROC_FUNC,
370178479Sjb				    "failed to lookup '%s' in module '%s'",
371178479Sjb				    pp->dpp_func, pp->dpp_mod));
372178479Sjb			} else {
373178479Sjb				return (0);
374178479Sjb			}
375178479Sjb		}
376178479Sjb
377178479Sjb		/*
378178479Sjb		 * Only match defined functions of non-zero size.
379178479Sjb		 */
380178479Sjb		if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
381178479Sjb		    sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
382178479Sjb			return (0);
383178479Sjb
384178479Sjb		/*
385178479Sjb		 * We don't instrument PLTs -- they're dynamically rewritten,
386178479Sjb		 * and, so, inherently dicey to instrument.
387178479Sjb		 */
388178568Sjb#ifdef DOODAD
389178479Sjb		if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
390178479Sjb			return (0);
391178568Sjb#endif
392178479Sjb
393178568Sjb#if defined(sun)
394178479Sjb		(void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
395178568Sjb#else
396178568Sjb		(void) proc_addr2sym(pp->dpp_pr, sym.st_value, pp->dpp_func,
397178568Sjb#endif
398178479Sjb		    DTRACE_FUNCNAMELEN, &sym);
399178479Sjb
400178479Sjb		return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
401178479Sjb	} else {
402178568Sjb#ifdef DOODAD
403178479Sjb		uint_t nmatches = pp->dpp_nmatches;
404178479Sjb
405178479Sjb		if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
406178479Sjb		    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
407178479Sjb			return (1);
408178479Sjb
409178479Sjb		if (nmatches == pp->dpp_nmatches) {
410178479Sjb			/*
411178479Sjb			 * If we didn't match anything in the PR_SYMTAB, try
412178479Sjb			 * the PR_DYNSYM.
413178479Sjb			 */
414178479Sjb			if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
415178479Sjb			    BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
416178479Sjb				return (1);
417178479Sjb		}
418178568Sjb#endif
419178479Sjb	}
420178479Sjb
421178479Sjb	return (0);
422178479Sjb}
423178479Sjb
424178479Sjbstatic int
425178479Sjbdt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
426178479Sjb{
427178479Sjb	char name[DTRACE_MODNAMELEN];
428178479Sjb	dt_pid_probe_t *pp = arg;
429178479Sjb
430178479Sjb	if (gmatch(obj, pp->dpp_mod))
431178479Sjb		return (dt_pid_per_mod(pp, pmp, obj));
432178479Sjb
433178568Sjb#if defined(sun)
434178479Sjb	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
435178568Sjb#else
436178568Sjb	pp->dpp_lmid = 0;
437178568Sjb#endif
438178479Sjb
439178479Sjb	if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
440178479Sjb		pp->dpp_obj = obj;
441178479Sjb	else
442178479Sjb		pp->dpp_obj++;
443178479Sjb
444210767Srpaulo	if (gmatch(pp->dpp_obj, pp->dpp_mod))
445210767Srpaulo		return (dt_pid_per_mod(pp, pmp, obj));
446178479Sjb
447210767Srpaulo	(void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
448210767Srpaulo
449210767Srpaulo	dt_pid_objname(name, sizeof (name), pp->dpp_lmid, pp->dpp_obj);
450210767Srpaulo
451178479Sjb	if (gmatch(name, pp->dpp_mod))
452178479Sjb		return (dt_pid_per_mod(pp, pmp, obj));
453178479Sjb
454178479Sjb	return (0);
455178479Sjb}
456178479Sjb
457178479Sjbstatic const prmap_t *
458178479Sjbdt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
459178479Sjb{
460178568Sjb#ifdef DOODAD
461178479Sjb	char m[MAXPATHLEN];
462178479Sjb	Lmid_t lmid = PR_LMID_EVERY;
463178479Sjb	const char *obj;
464178568Sjb#endif
465178479Sjb	const prmap_t *pmp;
466178479Sjb
467178568Sjb#ifdef DOODAD
468178479Sjb	/*
469178479Sjb	 * Pick apart the link map from the library name.
470178479Sjb	 */
471178479Sjb	if (strchr(pdp->dtpd_mod, '`') != NULL) {
472178479Sjb		char *end;
473178479Sjb
474178479Sjb		if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
475178479Sjb		    !isdigit(pdp->dtpd_mod[2]))
476178479Sjb			return (NULL);
477178479Sjb
478178479Sjb		lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
479178479Sjb
480178479Sjb		obj = end + 1;
481178479Sjb
482178479Sjb		if (*end != '`' || strchr(obj, '`') != NULL)
483178479Sjb			return (NULL);
484178479Sjb
485178479Sjb	} else {
486178479Sjb		obj = pdp->dtpd_mod;
487178479Sjb	}
488178479Sjb
489178479Sjb	if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
490178479Sjb		return (NULL);
491178479Sjb
492178479Sjb	(void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
493178479Sjb	if ((obj = strrchr(m, '/')) == NULL)
494178479Sjb		obj = &m[0];
495178479Sjb	else
496178479Sjb		obj++;
497178479Sjb
498178479Sjb	(void) Plmid(P, pmp->pr_vaddr, &lmid);
499178568Sjb
500178479Sjb	dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
501178568Sjb#else
502178568Sjbpmp = NULL;
503178568Sjb#endif
504178479Sjb
505178479Sjb	return (pmp);
506178479Sjb}
507178479Sjb
508178479Sjb
509178479Sjbstatic int
510178479Sjbdt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
511178479Sjb    dt_pcb_t *pcb, dt_proc_t *dpr)
512178479Sjb{
513178479Sjb	dt_pid_probe_t pp;
514178479Sjb	int ret = 0;
515178479Sjb
516178479Sjb	pp.dpp_dtp = dtp;
517178479Sjb	pp.dpp_dpr = dpr;
518178479Sjb	pp.dpp_pr = dpr->dpr_proc;
519178479Sjb	pp.dpp_pcb = pcb;
520178479Sjb
521178568Sjb#ifdef DOODAD
522178479Sjb	/*
523178479Sjb	 * We can only trace dynamically-linked executables (since we've
524178479Sjb	 * hidden some magic in ld.so.1 as well as libc.so.1).
525178479Sjb	 */
526178479Sjb	if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
527178479Sjb		return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
528178479Sjb		    "process %s is not a dynamically-linked executable",
529178479Sjb		    &pdp->dtpd_provider[3]));
530178479Sjb	}
531178568Sjb#endif
532178479Sjb
533178479Sjb	pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
534178479Sjb	pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
535178479Sjb	pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
536178479Sjb	pp.dpp_last_taken = 0;
537178479Sjb
538178479Sjb	if (strcmp(pp.dpp_func, "-") == 0) {
539178479Sjb		const prmap_t *aout, *pmp;
540178479Sjb
541178479Sjb		if (pdp->dtpd_mod[0] == '\0') {
542178479Sjb			pp.dpp_mod = pdp->dtpd_mod;
543178479Sjb			(void) strcpy(pdp->dtpd_mod, "a.out");
544178479Sjb		} else if (strisglob(pp.dpp_mod) ||
545178568Sjb#if defined(sun)
546178479Sjb		    (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
547178479Sjb		    (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
548178568Sjb#else
549178568Sjb		    (aout = proc_name2map(pp.dpp_pr, "a.out")) == NULL ||
550178568Sjb		    (pmp = proc_name2map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
551178568Sjb#endif
552178479Sjb		    aout->pr_vaddr != pmp->pr_vaddr) {
553178479Sjb			return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
554178479Sjb			    "only the a.out module is valid with the "
555178479Sjb			    "'-' function"));
556178479Sjb		}
557178479Sjb
558178479Sjb		if (strisglob(pp.dpp_name)) {
559178479Sjb			return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
560178479Sjb			    "only individual addresses may be specified "
561178479Sjb			    "with the '-' function"));
562178479Sjb		}
563178479Sjb	}
564178479Sjb
565178479Sjb	/*
566178479Sjb	 * If pp.dpp_mod contains any globbing meta-characters, we need
567178479Sjb	 * to iterate over each module and compare its name against the
568178479Sjb	 * pattern. An empty module name is treated as '*'.
569178479Sjb	 */
570178568Sjb#ifdef DOODAD
571178479Sjb	if (strisglob(pp.dpp_mod)) {
572178479Sjb		ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
573178479Sjb	} else {
574178479Sjb		const prmap_t *pmp;
575178479Sjb		char *obj;
576178479Sjb
577178479Sjb		/*
578178479Sjb		 * If we can't find a matching module, don't sweat it -- either
579178479Sjb		 * we'll fail the enabling because the probes don't exist or
580178479Sjb		 * we'll wait for that module to come along.
581178479Sjb		 */
582178479Sjb		if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
583178479Sjb			if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
584178479Sjb				obj = pdp->dtpd_mod;
585178479Sjb			else
586178479Sjb				obj++;
587178479Sjb
588178479Sjb			ret = dt_pid_per_mod(&pp, pmp, obj);
589178479Sjb		}
590178479Sjb	}
591178568Sjb#endif
592178479Sjb
593178479Sjb	return (ret);
594178479Sjb}
595178479Sjb
596178479Sjbstatic int
597178479Sjbdt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
598178479Sjb{
599178479Sjb	struct ps_prochandle *P = data;
600178479Sjb	GElf_Sym sym;
601178568Sjb#if defined(sun)
602178479Sjb	prsyminfo_t sip;
603178568Sjb#endif
604178479Sjb	dof_helper_t dh;
605178479Sjb	GElf_Half e_type;
606178479Sjb	const char *mname;
607178479Sjb	const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
608178479Sjb	int i, fd = -1;
609178479Sjb
610178479Sjb	/*
611178479Sjb	 * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
612178479Sjb	 * __SUNW_dof is for actively-loaded DOF sections. We try to force
613178479Sjb	 * in both types of DOF section since the process may not yet have
614178479Sjb	 * run the code to instantiate these providers.
615178479Sjb	 */
616178479Sjb	for (i = 0; i < 2; i++) {
617178568Sjb#if defined(sun)
618178479Sjb		if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
619178479Sjb		    &sip) != 0) {
620178568Sjb#else
621178568Sjb		if (proc_name2sym(P, oname, syms[i], &sym) != 0) {
622178568Sjb#endif
623178479Sjb			continue;
624178479Sjb		}
625178479Sjb
626178479Sjb		if ((mname = strrchr(oname, '/')) == NULL)
627178479Sjb			mname = oname;
628178479Sjb		else
629178479Sjb			mname++;
630178479Sjb
631178479Sjb		dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
632178479Sjb
633178568Sjb#ifdef DOODAD
634178479Sjb		if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
635178479Sjb		    offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
636178479Sjb			dt_dprintf("read of ELF header failed");
637178479Sjb			continue;
638178479Sjb		}
639178568Sjb#endif
640178479Sjb
641178479Sjb		dh.dofhp_dof = sym.st_value;
642178479Sjb		dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
643178479Sjb
644178479Sjb		dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
645178568Sjb#if defined(sun)
646178479Sjb		    sip.prs_lmid, mname);
647178568Sjb#else
648178568Sjb		    0, mname);
649178568Sjb#endif
650178479Sjb
651178568Sjb#ifdef DOODAD
652178479Sjb		if (fd == -1 &&
653178479Sjb		    (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
654178479Sjb			dt_dprintf("pr_open of helper device failed: %s\n",
655178479Sjb			    strerror(errno));
656178479Sjb			return (-1); /* errno is set for us */
657178479Sjb		}
658178479Sjb
659178479Sjb		if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
660178479Sjb			dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
661178568Sjb#endif
662178479Sjb	}
663178479Sjb
664178568Sjb#ifdef DOODAD
665178479Sjb	if (fd != -1)
666178479Sjb		(void) pr_close(P, fd);
667178568Sjb#endif
668178479Sjb
669178479Sjb	return (0);
670178479Sjb}
671178479Sjb
672178479Sjbstatic int
673178479Sjbdt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
674178479Sjb    dt_pcb_t *pcb, dt_proc_t *dpr)
675178479Sjb{
676178479Sjb	struct ps_prochandle *P = dpr->dpr_proc;
677178479Sjb	int ret = 0;
678178479Sjb
679210767Srpaulo	assert(MUTEX_HELD(&dpr->dpr_lock));
680178479Sjb
681178568Sjb#ifdef DOODAD
682178479Sjb	(void) Pupdate_maps(P);
683178479Sjb	if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
684178479Sjb		ret = -1;
685178479Sjb		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
686178479Sjb		    "failed to instantiate probes for pid %d: %s",
687178568Sjb#if defined(sun)
688178479Sjb		    (int)Pstatus(P)->pr_pid, strerror(errno));
689178568Sjb#else
690178568Sjb		    (int)proc_getpid(P), strerror(errno));
691178568Sjb#endif
692178479Sjb	}
693178568Sjb#endif
694178479Sjb
695178479Sjb	/*
696178479Sjb	 * Put the module name in its canonical form.
697178479Sjb	 */
698178479Sjb	(void) dt_pid_fix_mod(pdp, P);
699178479Sjb
700178479Sjb	return (ret);
701178479Sjb}
702178479Sjb
703178479Sjbstatic pid_t
704178479Sjbdt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
705178479Sjb    dt_proc_t *dpr)
706178479Sjb{
707178479Sjb	pid_t pid;
708178479Sjb	char *c, *last = NULL, *end;
709178479Sjb
710178479Sjb	for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
711178479Sjb		if (!isdigit(*c))
712178479Sjb			last = c;
713178479Sjb	}
714178479Sjb
715178479Sjb	if (last == NULL || (*(++last) == '\0')) {
716178479Sjb		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
717178479Sjb		    "'%s' is not a valid provider", pdp->dtpd_provider);
718178479Sjb		return (-1);
719178479Sjb	}
720178479Sjb
721178479Sjb	errno = 0;
722178479Sjb	pid = strtol(last, &end, 10);
723178479Sjb
724178479Sjb	if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
725178479Sjb		(void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
726178479Sjb		    "'%s' does not contain a valid pid", pdp->dtpd_provider);
727178479Sjb		return (-1);
728178479Sjb	}
729178479Sjb
730178479Sjb	return (pid);
731178479Sjb}
732178479Sjb
733178479Sjbint
734178479Sjbdt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
735178479Sjb{
736178479Sjb	char provname[DTRACE_PROVNAMELEN];
737178479Sjb	struct ps_prochandle *P;
738178479Sjb	dt_proc_t *dpr;
739178479Sjb	pid_t pid;
740178479Sjb	int err = 0;
741178479Sjb
742178479Sjb	assert(pcb != NULL);
743178479Sjb
744178479Sjb	if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
745178479Sjb		return (-1);
746178479Sjb
747178479Sjb	if (dtp->dt_ftfd == -1) {
748178479Sjb		if (dtp->dt_fterr == ENOENT) {
749178479Sjb			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
750178479Sjb			    "pid provider is not installed on this system");
751178479Sjb		} else {
752178479Sjb			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
753178479Sjb			    "pid provider is not available: %s",
754178479Sjb			    strerror(dtp->dt_fterr));
755178479Sjb		}
756178479Sjb
757178479Sjb		return (-1);
758178479Sjb	}
759178479Sjb
760178479Sjb	(void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
761178479Sjb
762178479Sjb	if (gmatch(provname, pdp->dtpd_provider) != 0) {
763178479Sjb		if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
764178479Sjb		    0)) == NULL) {
765178479Sjb			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
766178479Sjb			    "failed to grab process %d", (int)pid);
767178479Sjb			return (-1);
768178479Sjb		}
769178479Sjb
770178479Sjb		dpr = dt_proc_lookup(dtp, P, 0);
771178479Sjb		assert(dpr != NULL);
772178479Sjb		(void) pthread_mutex_lock(&dpr->dpr_lock);
773178479Sjb
774178568Sjb		if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
775178568Sjb			/*
776178568Sjb			 * Alert other retained enablings which may match
777178568Sjb			 * against the newly created probes.
778178568Sjb			 */
779178568Sjb			(void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
780178568Sjb		}
781178479Sjb
782178479Sjb		(void) pthread_mutex_unlock(&dpr->dpr_lock);
783178479Sjb		dt_proc_release(dtp, P);
784178479Sjb	}
785178479Sjb
786178479Sjb	/*
787178479Sjb	 * If it's not strictly a pid provider, we might match a USDT provider.
788178479Sjb	 */
789178479Sjb	if (strcmp(provname, pdp->dtpd_provider) != 0) {
790178479Sjb		if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
791178479Sjb			(void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
792178479Sjb			    "failed to grab process %d", (int)pid);
793178479Sjb			return (-1);
794178479Sjb		}
795178479Sjb
796178479Sjb		dpr = dt_proc_lookup(dtp, P, 0);
797178479Sjb		assert(dpr != NULL);
798178479Sjb		(void) pthread_mutex_lock(&dpr->dpr_lock);
799178479Sjb
800178479Sjb		if (!dpr->dpr_usdt) {
801178479Sjb			err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
802178479Sjb			dpr->dpr_usdt = B_TRUE;
803178479Sjb		}
804178479Sjb
805178479Sjb		(void) pthread_mutex_unlock(&dpr->dpr_lock);
806178479Sjb		dt_proc_release(dtp, P);
807178479Sjb	}
808178479Sjb
809178479Sjb	return (err ? -1 : 0);
810178479Sjb}
811178479Sjb
812178479Sjbint
813178479Sjbdt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
814178479Sjb{
815178568Sjb	dtrace_enable_io_t args;
816178479Sjb	dtrace_prog_t *pgp;
817178479Sjb	dt_stmt_t *stp;
818178479Sjb	dtrace_probedesc_t *pdp, pd;
819178479Sjb	pid_t pid;
820178479Sjb	int ret = 0, found = B_FALSE;
821178479Sjb	char provname[DTRACE_PROVNAMELEN];
822178479Sjb
823178479Sjb	(void) snprintf(provname, sizeof (provname), "pid%d",
824178479Sjb	    (int)dpr->dpr_pid);
825178479Sjb
826178479Sjb	for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
827178479Sjb	    pgp = dt_list_next(pgp)) {
828178479Sjb
829178479Sjb		for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
830178479Sjb		    stp = dt_list_next(stp)) {
831178479Sjb
832178479Sjb			pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
833178479Sjb			pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
834178479Sjb			if (pid != dpr->dpr_pid)
835178479Sjb				continue;
836178479Sjb
837178479Sjb			found = B_TRUE;
838178479Sjb
839178479Sjb			pd = *pdp;
840178479Sjb
841178479Sjb			if (gmatch(provname, pdp->dtpd_provider) != 0 &&
842178479Sjb			    dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
843178479Sjb				ret = 1;
844178479Sjb
845178479Sjb			/*
846178479Sjb			 * If it's not strictly a pid provider, we might match
847178479Sjb			 * a USDT provider.
848178479Sjb			 */
849178479Sjb			if (strcmp(provname, pdp->dtpd_provider) != 0 &&
850178479Sjb			    dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
851178479Sjb				ret = 1;
852178479Sjb		}
853178479Sjb	}
854178479Sjb
855178479Sjb	if (found) {
856178479Sjb		/*
857178479Sjb		 * Give DTrace a shot to the ribs to get it to check
858178479Sjb		 * out the newly created probes.
859178479Sjb		 */
860178568Sjb		args.dof = NULL;
861178568Sjb		args.n_matched = 0;
862178568Sjb		(void) dt_ioctl(dtp, DTRACEIOC_ENABLE, &args);
863178479Sjb	}
864178479Sjb
865178479Sjb	return (ret);
866178479Sjb}
867