10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
51593Sahl * Common Development and Distribution License (the "License").
61593Sahl * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate *
80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate * See the License for the specific language governing permissions
110Sstevel@tonic-gate * and limitations under the License.
120Sstevel@tonic-gate *
130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate *
190Sstevel@tonic-gate * CDDL HEADER END
200Sstevel@tonic-gate */
211399Sahl
220Sstevel@tonic-gate /*
23*11466SRoger.Faulkner@Sun.COM * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <assert.h>
280Sstevel@tonic-gate #include <strings.h>
290Sstevel@tonic-gate #include <stdlib.h>
300Sstevel@tonic-gate #include <stdio.h>
310Sstevel@tonic-gate #include <errno.h>
320Sstevel@tonic-gate #include <ctype.h>
330Sstevel@tonic-gate #include <alloca.h>
340Sstevel@tonic-gate #include <libgen.h>
350Sstevel@tonic-gate #include <stddef.h>
360Sstevel@tonic-gate
370Sstevel@tonic-gate #include <dt_impl.h>
38265Smws #include <dt_program.h>
390Sstevel@tonic-gate #include <dt_pid.h>
400Sstevel@tonic-gate #include <dt_string.h>
410Sstevel@tonic-gate
420Sstevel@tonic-gate typedef struct dt_pid_probe {
431399Sahl dtrace_hdl_t *dpp_dtp;
441399Sahl dt_pcb_t *dpp_pcb;
451399Sahl dt_proc_t *dpp_dpr;
461399Sahl struct ps_prochandle *dpp_pr;
471399Sahl const char *dpp_mod;
481399Sahl char *dpp_func;
491399Sahl const char *dpp_name;
501399Sahl const char *dpp_obj;
511399Sahl uintptr_t dpp_pc;
521399Sahl size_t dpp_size;
531399Sahl Lmid_t dpp_lmid;
541399Sahl uint_t dpp_nmatches;
551399Sahl uint64_t dpp_stret[4];
561399Sahl GElf_Sym dpp_last;
571399Sahl uint_t dpp_last_taken;
580Sstevel@tonic-gate } dt_pid_probe_t;
590Sstevel@tonic-gate
60630Sahl /*
61630Sahl * Compose the lmid and object name into the canonical representation. We
62630Sahl * omit the lmid for the default link map for convenience.
63630Sahl */
64630Sahl static void
dt_pid_objname(char * buf,size_t len,Lmid_t lmid,const char * obj)65630Sahl dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj)
66630Sahl {
67630Sahl if (lmid == LM_ID_BASE)
68630Sahl (void) strncpy(buf, obj, len);
69630Sahl else
70630Sahl (void) snprintf(buf, len, "LM%lx`%s", lmid, obj);
71630Sahl }
72630Sahl
731399Sahl static int
dt_pid_error(dtrace_hdl_t * dtp,dt_pcb_t * pcb,dt_proc_t * dpr,fasttrap_probe_spec_t * ftp,dt_errtag_t tag,const char * fmt,...)741399Sahl dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr,
751399Sahl fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...)
76630Sahl {
77630Sahl va_list ap;
781399Sahl int len;
791399Sahl
801399Sahl if (ftp != NULL)
811399Sahl dt_free(dtp, ftp);
82630Sahl
83630Sahl va_start(ap, fmt);
841399Sahl if (pcb == NULL) {
851399Sahl assert(dpr != NULL);
861399Sahl len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg),
871399Sahl fmt, ap);
881399Sahl assert(len >= 2);
891399Sahl if (dpr->dpr_errmsg[len - 2] == '\n')
901399Sahl dpr->dpr_errmsg[len - 2] = '\0';
911399Sahl } else {
921399Sahl dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region,
931399Sahl pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap);
941399Sahl }
95630Sahl va_end(ap);
961399Sahl
971399Sahl return (1);
98630Sahl }
99630Sahl
1001399Sahl static int
dt_pid_per_sym(dt_pid_probe_t * pp,const GElf_Sym * symp,const char * func)1010Sstevel@tonic-gate dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func)
1020Sstevel@tonic-gate {
1031399Sahl dtrace_hdl_t *dtp = pp->dpp_dtp;
1041399Sahl dt_pcb_t *pcb = pp->dpp_pcb;
1051399Sahl dt_proc_t *dpr = pp->dpp_dpr;
1060Sstevel@tonic-gate fasttrap_probe_spec_t *ftp;
1070Sstevel@tonic-gate uint64_t off;
1080Sstevel@tonic-gate char *end;
1090Sstevel@tonic-gate uint_t nmatches = 0;
1100Sstevel@tonic-gate ulong_t sz;
1110Sstevel@tonic-gate int glob, err;
1120Sstevel@tonic-gate int isdash = strcmp("-", func) == 0;
1130Sstevel@tonic-gate pid_t pid;
1140Sstevel@tonic-gate
1150Sstevel@tonic-gate pid = Pstatus(pp->dpp_pr)->pr_pid;
1160Sstevel@tonic-gate
1170Sstevel@tonic-gate dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj,
1180Sstevel@tonic-gate func, pp->dpp_name);
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 :
1210Sstevel@tonic-gate (symp->st_size - 1) * sizeof (ftp->ftps_offs[0]));
1220Sstevel@tonic-gate
1231399Sahl if ((ftp = dt_alloc(dtp, sz)) == NULL) {
1241399Sahl dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz);
1251399Sahl return (1); /* errno is set for us */
1260Sstevel@tonic-gate }
1270Sstevel@tonic-gate
1280Sstevel@tonic-gate ftp->ftps_pid = pid;
1290Sstevel@tonic-gate (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func));
1300Sstevel@tonic-gate
131630Sahl dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid,
132630Sahl pp->dpp_obj);
1330Sstevel@tonic-gate
1340Sstevel@tonic-gate if (!isdash && gmatch("return", pp->dpp_name)) {
1351399Sahl if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp,
1361399Sahl pp->dpp_stret) < 0) {
1371399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp,
1381399Sahl D_PROC_CREATEFAIL, "failed to create return probe "
1391399Sahl "for '%s': %s", func,
1401399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp))));
1411399Sahl }
1420Sstevel@tonic-gate
1430Sstevel@tonic-gate nmatches++;
1440Sstevel@tonic-gate }
1450Sstevel@tonic-gate
1460Sstevel@tonic-gate if (!isdash && gmatch("entry", pp->dpp_name)) {
1471399Sahl if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) {
1481399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp,
1491399Sahl D_PROC_CREATEFAIL, "failed to create entry probe "
1501399Sahl "for '%s': %s", func,
1511399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp))));
1521399Sahl }
1530Sstevel@tonic-gate
1540Sstevel@tonic-gate nmatches++;
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate
1570Sstevel@tonic-gate glob = strisglob(pp->dpp_name);
1580Sstevel@tonic-gate if (!glob && nmatches == 0) {
1590Sstevel@tonic-gate off = strtoull(pp->dpp_name, &end, 16);
1600Sstevel@tonic-gate if (*end != '\0') {
1611399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME,
1621399Sahl "'%s' is an invalid probe name", pp->dpp_name));
1630Sstevel@tonic-gate }
1640Sstevel@tonic-gate
1650Sstevel@tonic-gate if (off >= symp->st_size) {
1661399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF,
1671399Sahl "offset 0x%llx outside of function '%s'",
1681399Sahl (u_longlong_t)off, func));
1690Sstevel@tonic-gate }
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp,
1720Sstevel@tonic-gate symp, off);
1730Sstevel@tonic-gate
1741399Sahl if (err == DT_PROC_ERR) {
1751399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp,
1761399Sahl D_PROC_CREATEFAIL, "failed to create probe at "
1771399Sahl "'%s+0x%llx': %s", func, (u_longlong_t)off,
1781399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp))));
1791399Sahl }
1801399Sahl
1810Sstevel@tonic-gate if (err == DT_PROC_ALIGN) {
1821399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN,
1831399Sahl "offset 0x%llx is not aligned on an instruction",
1841399Sahl (u_longlong_t)off));
1850Sstevel@tonic-gate }
1860Sstevel@tonic-gate
1870Sstevel@tonic-gate nmatches++;
1880Sstevel@tonic-gate
1890Sstevel@tonic-gate } else if (glob && !isdash) {
1900Sstevel@tonic-gate if (dt_pid_create_glob_offset_probes(pp->dpp_pr,
1911399Sahl pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) {
1921399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp,
1931399Sahl D_PROC_CREATEFAIL,
1941399Sahl "failed to create offset probes in '%s': %s", func,
1951399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp))));
1961399Sahl }
1970Sstevel@tonic-gate
1980Sstevel@tonic-gate nmatches++;
1990Sstevel@tonic-gate }
2000Sstevel@tonic-gate
2010Sstevel@tonic-gate pp->dpp_nmatches += nmatches;
2020Sstevel@tonic-gate
2031399Sahl dt_free(dtp, ftp);
2040Sstevel@tonic-gate
2051399Sahl return (0);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate
2080Sstevel@tonic-gate static int
dt_pid_sym_filt(void * arg,const GElf_Sym * symp,const char * func)2090Sstevel@tonic-gate dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func)
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate dt_pid_probe_t *pp = arg;
2120Sstevel@tonic-gate
2130Sstevel@tonic-gate if (symp->st_shndx == SHN_UNDEF)
2140Sstevel@tonic-gate return (0);
2150Sstevel@tonic-gate
2160Sstevel@tonic-gate if (symp->st_size == 0) {
2170Sstevel@tonic-gate dt_dprintf("st_size of %s is zero\n", func);
2180Sstevel@tonic-gate return (0);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate
2211399Sahl if (pp->dpp_last_taken == 0 ||
2221399Sahl symp->st_value != pp->dpp_last.st_value ||
2230Sstevel@tonic-gate symp->st_size != pp->dpp_last.st_size) {
2240Sstevel@tonic-gate /*
2250Sstevel@tonic-gate * Due to 4524008, _init and _fini may have a bloated st_size.
2260Sstevel@tonic-gate * While this bug has been fixed for a while, old binaries
2270Sstevel@tonic-gate * may exist that still exhibit this problem. As a result, we
2280Sstevel@tonic-gate * don't match _init and _fini though we allow users to
2290Sstevel@tonic-gate * specify them explicitly.
2300Sstevel@tonic-gate */
2310Sstevel@tonic-gate if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0)
2320Sstevel@tonic-gate return (0);
2330Sstevel@tonic-gate
2341399Sahl if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) {
2351399Sahl pp->dpp_last = *symp;
2361399Sahl return (dt_pid_per_sym(pp, symp, func));
2370Sstevel@tonic-gate }
2380Sstevel@tonic-gate }
2390Sstevel@tonic-gate
2400Sstevel@tonic-gate return (0);
2410Sstevel@tonic-gate }
2420Sstevel@tonic-gate
2431399Sahl static int
dt_pid_per_mod(void * arg,const prmap_t * pmp,const char * obj)2440Sstevel@tonic-gate dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj)
2450Sstevel@tonic-gate {
2460Sstevel@tonic-gate dt_pid_probe_t *pp = arg;
2471399Sahl dtrace_hdl_t *dtp = pp->dpp_dtp;
2481399Sahl dt_pcb_t *pcb = pp->dpp_pcb;
2491399Sahl dt_proc_t *dpr = pp->dpp_dpr;
2500Sstevel@tonic-gate GElf_Sym sym;
2510Sstevel@tonic-gate
2520Sstevel@tonic-gate if (obj == NULL)
2531399Sahl return (0);
2540Sstevel@tonic-gate
2550Sstevel@tonic-gate (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
2560Sstevel@tonic-gate
2570Sstevel@tonic-gate if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
2580Sstevel@tonic-gate pp->dpp_obj = obj;
2590Sstevel@tonic-gate else
2600Sstevel@tonic-gate pp->dpp_obj++;
2610Sstevel@tonic-gate
2620Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym,
2630Sstevel@tonic-gate NULL) == 0)
2640Sstevel@tonic-gate pp->dpp_stret[0] = sym.st_value;
2650Sstevel@tonic-gate else
2660Sstevel@tonic-gate pp->dpp_stret[0] = 0;
2670Sstevel@tonic-gate
2680Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym,
2690Sstevel@tonic-gate NULL) == 0)
2700Sstevel@tonic-gate pp->dpp_stret[1] = sym.st_value;
2710Sstevel@tonic-gate else
2720Sstevel@tonic-gate pp->dpp_stret[1] = 0;
2730Sstevel@tonic-gate
2740Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym,
2750Sstevel@tonic-gate NULL) == 0)
2760Sstevel@tonic-gate pp->dpp_stret[2] = sym.st_value;
2770Sstevel@tonic-gate else
2780Sstevel@tonic-gate pp->dpp_stret[2] = 0;
2790Sstevel@tonic-gate
2800Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym,
2810Sstevel@tonic-gate NULL) == 0)
2820Sstevel@tonic-gate pp->dpp_stret[3] = sym.st_value;
2830Sstevel@tonic-gate else
2840Sstevel@tonic-gate pp->dpp_stret[3] = 0;
2850Sstevel@tonic-gate
2860Sstevel@tonic-gate dt_dprintf("%s stret %llx %llx %llx %llx\n", obj,
2870Sstevel@tonic-gate (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1],
2880Sstevel@tonic-gate (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]);
2890Sstevel@tonic-gate
2900Sstevel@tonic-gate /*
2910Sstevel@tonic-gate * If pp->dpp_func contains any globbing meta-characters, we need
2920Sstevel@tonic-gate * to iterate over the symbol table and compare each function name
2930Sstevel@tonic-gate * against the pattern.
2940Sstevel@tonic-gate */
2950Sstevel@tonic-gate if (!strisglob(pp->dpp_func)) {
2960Sstevel@tonic-gate /*
2970Sstevel@tonic-gate * If we fail to lookup the symbol, try interpreting the
2980Sstevel@tonic-gate * function as the special "-" function that indicates that the
2990Sstevel@tonic-gate * probe name should be interpreted as a absolute virtual
3000Sstevel@tonic-gate * address. If that fails and we were matching a specific
3010Sstevel@tonic-gate * function in a specific module, report the error, otherwise
3020Sstevel@tonic-gate * just fail silently in the hopes that some other object will
3030Sstevel@tonic-gate * contain the desired symbol.
3040Sstevel@tonic-gate */
3050Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj,
3060Sstevel@tonic-gate pp->dpp_func, &sym, NULL) != 0) {
3070Sstevel@tonic-gate if (strcmp("-", pp->dpp_func) == 0) {
3080Sstevel@tonic-gate sym.st_name = 0;
3090Sstevel@tonic-gate sym.st_info =
3100Sstevel@tonic-gate GELF_ST_INFO(STB_LOCAL, STT_FUNC);
3110Sstevel@tonic-gate sym.st_other = 0;
3120Sstevel@tonic-gate sym.st_value = 0;
3130Sstevel@tonic-gate sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel ==
3140Sstevel@tonic-gate PR_MODEL_ILP32 ? -1U : -1ULL;
3150Sstevel@tonic-gate
3160Sstevel@tonic-gate } else if (!strisglob(pp->dpp_mod)) {
3171399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL,
3181399Sahl D_PROC_FUNC,
3191399Sahl "failed to lookup '%s' in module '%s'",
3201399Sahl pp->dpp_func, pp->dpp_mod));
3210Sstevel@tonic-gate } else {
3221399Sahl return (0);
3230Sstevel@tonic-gate }
3240Sstevel@tonic-gate }
3250Sstevel@tonic-gate
3260Sstevel@tonic-gate /*
3270Sstevel@tonic-gate * Only match defined functions of non-zero size.
3280Sstevel@tonic-gate */
3290Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != STT_FUNC ||
3300Sstevel@tonic-gate sym.st_shndx == SHN_UNDEF || sym.st_size == 0)
3311399Sahl return (0);
3320Sstevel@tonic-gate
3330Sstevel@tonic-gate /*
3340Sstevel@tonic-gate * We don't instrument PLTs -- they're dynamically rewritten,
3350Sstevel@tonic-gate * and, so, inherently dicey to instrument.
3360Sstevel@tonic-gate */
3370Sstevel@tonic-gate if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL)
3381399Sahl return (0);
3390Sstevel@tonic-gate
3400Sstevel@tonic-gate (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func,
3410Sstevel@tonic-gate DTRACE_FUNCNAMELEN, &sym);
3420Sstevel@tonic-gate
3431399Sahl return (dt_pid_per_sym(pp, &sym, pp->dpp_func));
3440Sstevel@tonic-gate } else {
3450Sstevel@tonic-gate uint_t nmatches = pp->dpp_nmatches;
3460Sstevel@tonic-gate
3471399Sahl if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB,
3481399Sahl BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
3491399Sahl return (1);
3500Sstevel@tonic-gate
3510Sstevel@tonic-gate if (nmatches == pp->dpp_nmatches) {
3520Sstevel@tonic-gate /*
3530Sstevel@tonic-gate * If we didn't match anything in the PR_SYMTAB, try
3540Sstevel@tonic-gate * the PR_DYNSYM.
3550Sstevel@tonic-gate */
3561399Sahl if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM,
3571399Sahl BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1)
3581399Sahl return (1);
3590Sstevel@tonic-gate }
3600Sstevel@tonic-gate }
3611399Sahl
3621399Sahl return (0);
3630Sstevel@tonic-gate }
3640Sstevel@tonic-gate
3650Sstevel@tonic-gate static int
dt_pid_mod_filt(void * arg,const prmap_t * pmp,const char * obj)3660Sstevel@tonic-gate dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj)
3670Sstevel@tonic-gate {
3681399Sahl char name[DTRACE_MODNAMELEN];
3690Sstevel@tonic-gate dt_pid_probe_t *pp = arg;
3700Sstevel@tonic-gate
3711399Sahl if ((pp->dpp_obj = strrchr(obj, '/')) == NULL)
3721399Sahl pp->dpp_obj = obj;
3731399Sahl else
3741399Sahl pp->dpp_obj++;
3750Sstevel@tonic-gate
3767991SJonathan.Haslam@Sun.COM if (gmatch(pp->dpp_obj, pp->dpp_mod))
3777991SJonathan.Haslam@Sun.COM return (dt_pid_per_mod(pp, pmp, obj));
3787991SJonathan.Haslam@Sun.COM
3797991SJonathan.Haslam@Sun.COM (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid);
3807991SJonathan.Haslam@Sun.COM
3817991SJonathan.Haslam@Sun.COM dt_pid_objname(name, sizeof (name), pp->dpp_lmid, pp->dpp_obj);
3820Sstevel@tonic-gate
3831399Sahl if (gmatch(name, pp->dpp_mod))
3841399Sahl return (dt_pid_per_mod(pp, pmp, obj));
3850Sstevel@tonic-gate
3860Sstevel@tonic-gate return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate
3890Sstevel@tonic-gate static const prmap_t *
dt_pid_fix_mod(dtrace_probedesc_t * pdp,struct ps_prochandle * P)3900Sstevel@tonic-gate dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P)
3910Sstevel@tonic-gate {
3920Sstevel@tonic-gate char m[MAXPATHLEN];
3930Sstevel@tonic-gate Lmid_t lmid = PR_LMID_EVERY;
3940Sstevel@tonic-gate const char *obj;
3950Sstevel@tonic-gate const prmap_t *pmp;
3960Sstevel@tonic-gate
3970Sstevel@tonic-gate /*
3980Sstevel@tonic-gate * Pick apart the link map from the library name.
3990Sstevel@tonic-gate */
4000Sstevel@tonic-gate if (strchr(pdp->dtpd_mod, '`') != NULL) {
4010Sstevel@tonic-gate char *end;
4020Sstevel@tonic-gate
4030Sstevel@tonic-gate if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 ||
4040Sstevel@tonic-gate !isdigit(pdp->dtpd_mod[2]))
4050Sstevel@tonic-gate return (NULL);
4060Sstevel@tonic-gate
4070Sstevel@tonic-gate lmid = strtoul(&pdp->dtpd_mod[2], &end, 16);
4080Sstevel@tonic-gate
4090Sstevel@tonic-gate obj = end + 1;
4100Sstevel@tonic-gate
4110Sstevel@tonic-gate if (*end != '`' || strchr(obj, '`') != NULL)
4120Sstevel@tonic-gate return (NULL);
4130Sstevel@tonic-gate
4140Sstevel@tonic-gate } else {
4150Sstevel@tonic-gate obj = pdp->dtpd_mod;
4160Sstevel@tonic-gate }
4170Sstevel@tonic-gate
4180Sstevel@tonic-gate if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL)
4190Sstevel@tonic-gate return (NULL);
4200Sstevel@tonic-gate
4210Sstevel@tonic-gate (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m));
4220Sstevel@tonic-gate if ((obj = strrchr(m, '/')) == NULL)
4230Sstevel@tonic-gate obj = &m[0];
4240Sstevel@tonic-gate else
4250Sstevel@tonic-gate obj++;
4260Sstevel@tonic-gate
4270Sstevel@tonic-gate (void) Plmid(P, pmp->pr_vaddr, &lmid);
428630Sahl dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj);
4290Sstevel@tonic-gate
4300Sstevel@tonic-gate return (pmp);
4310Sstevel@tonic-gate }
4320Sstevel@tonic-gate
4330Sstevel@tonic-gate
4341399Sahl static int
dt_pid_create_pid_probes(dtrace_probedesc_t * pdp,dtrace_hdl_t * dtp,dt_pcb_t * pcb,dt_proc_t * dpr)4351399Sahl dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
4361399Sahl dt_pcb_t *pcb, dt_proc_t *dpr)
4370Sstevel@tonic-gate {
4380Sstevel@tonic-gate dt_pid_probe_t pp;
4391399Sahl int ret = 0;
4400Sstevel@tonic-gate
4411399Sahl pp.dpp_dtp = dtp;
4421399Sahl pp.dpp_dpr = dpr;
4431399Sahl pp.dpp_pr = dpr->dpr_proc;
4441399Sahl pp.dpp_pcb = pcb;
4450Sstevel@tonic-gate
4460Sstevel@tonic-gate /*
4470Sstevel@tonic-gate * We can only trace dynamically-linked executables (since we've
4480Sstevel@tonic-gate * hidden some magic in ld.so.1 as well as libc.so.1).
4490Sstevel@tonic-gate */
4500Sstevel@tonic-gate if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) {
4511399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN,
4521399Sahl "process %s is not a dynamically-linked executable",
4531399Sahl &pdp->dtpd_provider[3]));
4540Sstevel@tonic-gate }
4550Sstevel@tonic-gate
4560Sstevel@tonic-gate pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*";
4570Sstevel@tonic-gate pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*";
4580Sstevel@tonic-gate pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*";
4591399Sahl pp.dpp_last_taken = 0;
4600Sstevel@tonic-gate
4610Sstevel@tonic-gate if (strcmp(pp.dpp_func, "-") == 0) {
4620Sstevel@tonic-gate const prmap_t *aout, *pmp;
4630Sstevel@tonic-gate
4640Sstevel@tonic-gate if (pdp->dtpd_mod[0] == '\0') {
4650Sstevel@tonic-gate pp.dpp_mod = pdp->dtpd_mod;
4660Sstevel@tonic-gate (void) strcpy(pdp->dtpd_mod, "a.out");
4670Sstevel@tonic-gate } else if (strisglob(pp.dpp_mod) ||
4680Sstevel@tonic-gate (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL ||
4690Sstevel@tonic-gate (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL ||
4700Sstevel@tonic-gate aout->pr_vaddr != pmp->pr_vaddr) {
4711399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB,
4721399Sahl "only the a.out module is valid with the "
4731399Sahl "'-' function"));
4740Sstevel@tonic-gate }
4750Sstevel@tonic-gate
4760Sstevel@tonic-gate if (strisglob(pp.dpp_name)) {
4771399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME,
4781399Sahl "only individual addresses may be specified "
4791399Sahl "with the '-' function"));
4800Sstevel@tonic-gate }
4810Sstevel@tonic-gate }
4820Sstevel@tonic-gate
4830Sstevel@tonic-gate /*
4840Sstevel@tonic-gate * If pp.dpp_mod contains any globbing meta-characters, we need
4850Sstevel@tonic-gate * to iterate over each module and compare its name against the
4860Sstevel@tonic-gate * pattern. An empty module name is treated as '*'.
4870Sstevel@tonic-gate */
4880Sstevel@tonic-gate if (strisglob(pp.dpp_mod)) {
4891399Sahl ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp);
4900Sstevel@tonic-gate } else {
4910Sstevel@tonic-gate const prmap_t *pmp;
4920Sstevel@tonic-gate char *obj;
4930Sstevel@tonic-gate
4940Sstevel@tonic-gate /*
4951399Sahl * If we can't find a matching module, don't sweat it -- either
4960Sstevel@tonic-gate * we'll fail the enabling because the probes don't exist or
4970Sstevel@tonic-gate * we'll wait for that module to come along.
4980Sstevel@tonic-gate */
4990Sstevel@tonic-gate if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) {
5000Sstevel@tonic-gate if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL)
5010Sstevel@tonic-gate obj = pdp->dtpd_mod;
5020Sstevel@tonic-gate else
5030Sstevel@tonic-gate obj++;
5040Sstevel@tonic-gate
5051399Sahl ret = dt_pid_per_mod(&pp, pmp, obj);
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate }
5080Sstevel@tonic-gate
5091399Sahl return (ret);
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate static int
dt_pid_usdt_mapping(void * data,const prmap_t * pmp,const char * oname)5130Sstevel@tonic-gate dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate struct ps_prochandle *P = data;
5160Sstevel@tonic-gate GElf_Sym sym;
5170Sstevel@tonic-gate prsyminfo_t sip;
5180Sstevel@tonic-gate dof_helper_t dh;
5190Sstevel@tonic-gate GElf_Half e_type;
5200Sstevel@tonic-gate const char *mname;
5210Sstevel@tonic-gate const char *syms[] = { "___SUNW_dof", "__SUNW_dof" };
5221399Sahl int i, fd = -1;
5230Sstevel@tonic-gate
5240Sstevel@tonic-gate /*
5250Sstevel@tonic-gate * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and
5260Sstevel@tonic-gate * __SUNW_dof is for actively-loaded DOF sections. We try to force
5270Sstevel@tonic-gate * in both types of DOF section since the process may not yet have
5280Sstevel@tonic-gate * run the code to instantiate these providers.
5290Sstevel@tonic-gate */
5300Sstevel@tonic-gate for (i = 0; i < 2; i++) {
5310Sstevel@tonic-gate if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym,
5320Sstevel@tonic-gate &sip) != 0) {
5330Sstevel@tonic-gate continue;
5340Sstevel@tonic-gate }
5350Sstevel@tonic-gate
5360Sstevel@tonic-gate if ((mname = strrchr(oname, '/')) == NULL)
5370Sstevel@tonic-gate mname = oname;
5380Sstevel@tonic-gate else
5390Sstevel@tonic-gate mname++;
5400Sstevel@tonic-gate
5410Sstevel@tonic-gate dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname);
5420Sstevel@tonic-gate
5430Sstevel@tonic-gate if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr +
5440Sstevel@tonic-gate offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) {
5450Sstevel@tonic-gate dt_dprintf("read of ELF header failed");
5460Sstevel@tonic-gate continue;
5470Sstevel@tonic-gate }
5480Sstevel@tonic-gate
5490Sstevel@tonic-gate dh.dofhp_dof = sym.st_value;
5500Sstevel@tonic-gate dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr;
5510Sstevel@tonic-gate
552630Sahl dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod),
553630Sahl sip.prs_lmid, mname);
5540Sstevel@tonic-gate
5551399Sahl if (fd == -1 &&
5561399Sahl (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) {
5570Sstevel@tonic-gate dt_dprintf("pr_open of helper device failed: %s\n",
5580Sstevel@tonic-gate strerror(errno));
5591399Sahl return (-1); /* errno is set for us */
5600Sstevel@tonic-gate }
5610Sstevel@tonic-gate
5621399Sahl if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0)
5631399Sahl dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod);
5641399Sahl }
5650Sstevel@tonic-gate
5661399Sahl if (fd != -1)
5671399Sahl (void) pr_close(P, fd);
5680Sstevel@tonic-gate
5690Sstevel@tonic-gate return (0);
5700Sstevel@tonic-gate }
5710Sstevel@tonic-gate
5720Sstevel@tonic-gate static int
dt_pid_create_usdt_probes(dtrace_probedesc_t * pdp,dtrace_hdl_t * dtp,dt_pcb_t * pcb,dt_proc_t * dpr)5731399Sahl dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp,
5741399Sahl dt_pcb_t *pcb, dt_proc_t *dpr)
5750Sstevel@tonic-gate {
5760Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc;
5771399Sahl int ret = 0;
5780Sstevel@tonic-gate
579*11466SRoger.Faulkner@Sun.COM assert(MUTEX_HELD(&dpr->dpr_lock));
5800Sstevel@tonic-gate
5810Sstevel@tonic-gate (void) Pupdate_maps(P);
5821399Sahl if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) {
5831399Sahl ret = -1;
5841399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT,
5851399Sahl "failed to instantiate probes for pid %d: %s",
5861399Sahl (int)Pstatus(P)->pr_pid, strerror(errno));
5871399Sahl }
5880Sstevel@tonic-gate
5890Sstevel@tonic-gate /*
5900Sstevel@tonic-gate * Put the module name in its canonical form.
5910Sstevel@tonic-gate */
5920Sstevel@tonic-gate (void) dt_pid_fix_mod(pdp, P);
5930Sstevel@tonic-gate
5941399Sahl return (ret);
5950Sstevel@tonic-gate }
5960Sstevel@tonic-gate
5970Sstevel@tonic-gate static pid_t
dt_pid_get_pid(dtrace_probedesc_t * pdp,dtrace_hdl_t * dtp,dt_pcb_t * pcb,dt_proc_t * dpr)5981399Sahl dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb,
5991399Sahl dt_proc_t *dpr)
6000Sstevel@tonic-gate {
6010Sstevel@tonic-gate pid_t pid;
6020Sstevel@tonic-gate char *c, *last = NULL, *end;
6030Sstevel@tonic-gate
6040Sstevel@tonic-gate for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) {
6050Sstevel@tonic-gate if (!isdigit(*c))
6060Sstevel@tonic-gate last = c;
6070Sstevel@tonic-gate }
6080Sstevel@tonic-gate
6090Sstevel@tonic-gate if (last == NULL || (*(++last) == '\0')) {
6101399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV,
6111399Sahl "'%s' is not a valid provider", pdp->dtpd_provider);
6121399Sahl return (-1);
6130Sstevel@tonic-gate }
6140Sstevel@tonic-gate
6150Sstevel@tonic-gate errno = 0;
6160Sstevel@tonic-gate pid = strtol(last, &end, 10);
6170Sstevel@tonic-gate
6180Sstevel@tonic-gate if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) {
6191399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID,
6201399Sahl "'%s' does not contain a valid pid", pdp->dtpd_provider);
6211399Sahl return (-1);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate
6240Sstevel@tonic-gate return (pid);
6250Sstevel@tonic-gate }
6260Sstevel@tonic-gate
6271399Sahl int
dt_pid_create_probes(dtrace_probedesc_t * pdp,dtrace_hdl_t * dtp,dt_pcb_t * pcb)6281399Sahl dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate char provname[DTRACE_PROVNAMELEN];
6310Sstevel@tonic-gate struct ps_prochandle *P;
6320Sstevel@tonic-gate dt_proc_t *dpr;
6331399Sahl pid_t pid;
6341593Sahl int err = 0;
6351399Sahl
6361399Sahl assert(pcb != NULL);
6371399Sahl
6381399Sahl if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1)
6391399Sahl return (-1);
6400Sstevel@tonic-gate
6410Sstevel@tonic-gate if (dtp->dt_ftfd == -1) {
6420Sstevel@tonic-gate if (dtp->dt_fterr == ENOENT) {
6431399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
6441399Sahl "pid provider is not installed on this system");
6450Sstevel@tonic-gate } else {
6461399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV,
6471399Sahl "pid provider is not available: %s",
6481399Sahl strerror(dtp->dt_fterr));
6490Sstevel@tonic-gate }
6501399Sahl
6511399Sahl return (-1);
6520Sstevel@tonic-gate }
6530Sstevel@tonic-gate
6540Sstevel@tonic-gate (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid);
6550Sstevel@tonic-gate
6565984Sjhaslam if (gmatch(provname, pdp->dtpd_provider) != 0) {
6571399Sahl if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE,
6581399Sahl 0)) == NULL) {
6591399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
6601399Sahl "failed to grab process %d", (int)pid);
6611399Sahl return (-1);
6621399Sahl }
6630Sstevel@tonic-gate
6640Sstevel@tonic-gate dpr = dt_proc_lookup(dtp, P, 0);
6650Sstevel@tonic-gate assert(dpr != NULL);
6661399Sahl (void) pthread_mutex_lock(&dpr->dpr_lock);
6670Sstevel@tonic-gate
6686390Sahl if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) {
6696390Sahl /*
6706390Sahl * Alert other retained enablings which may match
6716390Sahl * against the newly created probes.
6726390Sahl */
6736390Sahl (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
6746390Sahl }
6751399Sahl
6761399Sahl (void) pthread_mutex_unlock(&dpr->dpr_lock);
6771399Sahl dt_proc_release(dtp, P);
6785984Sjhaslam }
6791399Sahl
6805984Sjhaslam /*
6815984Sjhaslam * If it's not strictly a pid provider, we might match a USDT provider.
6825984Sjhaslam */
6835984Sjhaslam if (strcmp(provname, pdp->dtpd_provider) != 0) {
6841399Sahl if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) {
6851399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB,
6861399Sahl "failed to grab process %d", (int)pid);
6871399Sahl return (-1);
6881399Sahl }
6891399Sahl
6901399Sahl dpr = dt_proc_lookup(dtp, P, 0);
6911399Sahl assert(dpr != NULL);
6920Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock);
6930Sstevel@tonic-gate
6940Sstevel@tonic-gate if (!dpr->dpr_usdt) {
6951399Sahl err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr);
6960Sstevel@tonic-gate dpr->dpr_usdt = B_TRUE;
6970Sstevel@tonic-gate }
6980Sstevel@tonic-gate
6990Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock);
7000Sstevel@tonic-gate dt_proc_release(dtp, P);
7011399Sahl }
7020Sstevel@tonic-gate
7031399Sahl return (err ? -1 : 0);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate
7061399Sahl int
dt_pid_create_probes_module(dtrace_hdl_t * dtp,dt_proc_t * dpr)7070Sstevel@tonic-gate dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr)
7080Sstevel@tonic-gate {
7090Sstevel@tonic-gate dtrace_prog_t *pgp;
7100Sstevel@tonic-gate dt_stmt_t *stp;
7110Sstevel@tonic-gate dtrace_probedesc_t *pdp, pd;
7120Sstevel@tonic-gate pid_t pid;
7131399Sahl int ret = 0, found = B_FALSE;
7145984Sjhaslam char provname[DTRACE_PROVNAMELEN];
7155984Sjhaslam
7165984Sjhaslam (void) snprintf(provname, sizeof (provname), "pid%d",
7175984Sjhaslam (int)dpr->dpr_pid);
7180Sstevel@tonic-gate
7190Sstevel@tonic-gate for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL;
7200Sstevel@tonic-gate pgp = dt_list_next(pgp)) {
7210Sstevel@tonic-gate
7220Sstevel@tonic-gate for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL;
7230Sstevel@tonic-gate stp = dt_list_next(stp)) {
7240Sstevel@tonic-gate
7250Sstevel@tonic-gate pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe;
7261399Sahl pid = dt_pid_get_pid(pdp, dtp, NULL, dpr);
7271399Sahl if (pid != dpr->dpr_pid)
7280Sstevel@tonic-gate continue;
7290Sstevel@tonic-gate
7300Sstevel@tonic-gate found = B_TRUE;
7310Sstevel@tonic-gate
7320Sstevel@tonic-gate pd = *pdp;
7330Sstevel@tonic-gate
7345984Sjhaslam if (gmatch(provname, pdp->dtpd_provider) != 0 &&
7355984Sjhaslam dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0)
7365984Sjhaslam ret = 1;
7375984Sjhaslam
7385984Sjhaslam /*
7395984Sjhaslam * If it's not strictly a pid provider, we might match
7405984Sjhaslam * a USDT provider.
7415984Sjhaslam */
7425984Sjhaslam if (strcmp(provname, pdp->dtpd_provider) != 0 &&
7435984Sjhaslam dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0)
7445984Sjhaslam ret = 1;
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate }
7470Sstevel@tonic-gate
7480Sstevel@tonic-gate if (found) {
7490Sstevel@tonic-gate /*
7500Sstevel@tonic-gate * Give DTrace a shot to the ribs to get it to check
7510Sstevel@tonic-gate * out the newly created probes.
7520Sstevel@tonic-gate */
7530Sstevel@tonic-gate (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL);
7540Sstevel@tonic-gate }
7551399Sahl
7561399Sahl return (ret);
7570Sstevel@tonic-gate }
758