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*5984Sjhaslam * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <assert.h> 300Sstevel@tonic-gate #include <strings.h> 310Sstevel@tonic-gate #include <stdlib.h> 320Sstevel@tonic-gate #include <stdio.h> 330Sstevel@tonic-gate #include <errno.h> 340Sstevel@tonic-gate #include <ctype.h> 350Sstevel@tonic-gate #include <alloca.h> 360Sstevel@tonic-gate #include <libgen.h> 370Sstevel@tonic-gate #include <stddef.h> 380Sstevel@tonic-gate 390Sstevel@tonic-gate #include <dt_impl.h> 40265Smws #include <dt_program.h> 410Sstevel@tonic-gate #include <dt_pid.h> 420Sstevel@tonic-gate #include <dt_string.h> 430Sstevel@tonic-gate 440Sstevel@tonic-gate typedef struct dt_pid_probe { 451399Sahl dtrace_hdl_t *dpp_dtp; 461399Sahl dt_pcb_t *dpp_pcb; 471399Sahl dt_proc_t *dpp_dpr; 481399Sahl struct ps_prochandle *dpp_pr; 491399Sahl const char *dpp_mod; 501399Sahl char *dpp_func; 511399Sahl const char *dpp_name; 521399Sahl const char *dpp_obj; 531399Sahl uintptr_t dpp_pc; 541399Sahl size_t dpp_size; 551399Sahl Lmid_t dpp_lmid; 561399Sahl uint_t dpp_nmatches; 571399Sahl uint64_t dpp_stret[4]; 581399Sahl GElf_Sym dpp_last; 591399Sahl uint_t dpp_last_taken; 600Sstevel@tonic-gate } dt_pid_probe_t; 610Sstevel@tonic-gate 62630Sahl /* 63630Sahl * Compose the lmid and object name into the canonical representation. We 64630Sahl * omit the lmid for the default link map for convenience. 65630Sahl */ 66630Sahl static void 67630Sahl dt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj) 68630Sahl { 69630Sahl if (lmid == LM_ID_BASE) 70630Sahl (void) strncpy(buf, obj, len); 71630Sahl else 72630Sahl (void) snprintf(buf, len, "LM%lx`%s", lmid, obj); 73630Sahl } 74630Sahl 751399Sahl static int 761399Sahl dt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr, 771399Sahl fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...) 78630Sahl { 79630Sahl va_list ap; 801399Sahl int len; 811399Sahl 821399Sahl if (ftp != NULL) 831399Sahl dt_free(dtp, ftp); 84630Sahl 85630Sahl va_start(ap, fmt); 861399Sahl if (pcb == NULL) { 871399Sahl assert(dpr != NULL); 881399Sahl len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg), 891399Sahl fmt, ap); 901399Sahl assert(len >= 2); 911399Sahl if (dpr->dpr_errmsg[len - 2] == '\n') 921399Sahl dpr->dpr_errmsg[len - 2] = '\0'; 931399Sahl } else { 941399Sahl dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region, 951399Sahl pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap); 961399Sahl } 97630Sahl va_end(ap); 981399Sahl 991399Sahl return (1); 100630Sahl } 101630Sahl 1021399Sahl static int 1030Sstevel@tonic-gate dt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func) 1040Sstevel@tonic-gate { 1051399Sahl dtrace_hdl_t *dtp = pp->dpp_dtp; 1061399Sahl dt_pcb_t *pcb = pp->dpp_pcb; 1071399Sahl dt_proc_t *dpr = pp->dpp_dpr; 1080Sstevel@tonic-gate fasttrap_probe_spec_t *ftp; 1090Sstevel@tonic-gate uint64_t off; 1100Sstevel@tonic-gate char *end; 1110Sstevel@tonic-gate uint_t nmatches = 0; 1120Sstevel@tonic-gate ulong_t sz; 1130Sstevel@tonic-gate int glob, err; 1140Sstevel@tonic-gate int isdash = strcmp("-", func) == 0; 1150Sstevel@tonic-gate pid_t pid; 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate pid = Pstatus(pp->dpp_pr)->pr_pid; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj, 1200Sstevel@tonic-gate func, pp->dpp_name); 1210Sstevel@tonic-gate 1220Sstevel@tonic-gate sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 : 1230Sstevel@tonic-gate (symp->st_size - 1) * sizeof (ftp->ftps_offs[0])); 1240Sstevel@tonic-gate 1251399Sahl if ((ftp = dt_alloc(dtp, sz)) == NULL) { 1261399Sahl dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz); 1271399Sahl return (1); /* errno is set for us */ 1280Sstevel@tonic-gate } 1290Sstevel@tonic-gate 1300Sstevel@tonic-gate ftp->ftps_pid = pid; 1310Sstevel@tonic-gate (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func)); 1320Sstevel@tonic-gate 133630Sahl dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid, 134630Sahl pp->dpp_obj); 1350Sstevel@tonic-gate 1360Sstevel@tonic-gate if (!isdash && gmatch("return", pp->dpp_name)) { 1371399Sahl if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp, 1381399Sahl pp->dpp_stret) < 0) { 1391399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, 1401399Sahl D_PROC_CREATEFAIL, "failed to create return probe " 1411399Sahl "for '%s': %s", func, 1421399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp)))); 1431399Sahl } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate nmatches++; 1460Sstevel@tonic-gate } 1470Sstevel@tonic-gate 1480Sstevel@tonic-gate if (!isdash && gmatch("entry", pp->dpp_name)) { 1491399Sahl if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) { 1501399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, 1511399Sahl D_PROC_CREATEFAIL, "failed to create entry probe " 1521399Sahl "for '%s': %s", func, 1531399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp)))); 1541399Sahl } 1550Sstevel@tonic-gate 1560Sstevel@tonic-gate nmatches++; 1570Sstevel@tonic-gate } 1580Sstevel@tonic-gate 1590Sstevel@tonic-gate glob = strisglob(pp->dpp_name); 1600Sstevel@tonic-gate if (!glob && nmatches == 0) { 1610Sstevel@tonic-gate off = strtoull(pp->dpp_name, &end, 16); 1620Sstevel@tonic-gate if (*end != '\0') { 1631399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME, 1641399Sahl "'%s' is an invalid probe name", pp->dpp_name)); 1650Sstevel@tonic-gate } 1660Sstevel@tonic-gate 1670Sstevel@tonic-gate if (off >= symp->st_size) { 1681399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF, 1691399Sahl "offset 0x%llx outside of function '%s'", 1701399Sahl (u_longlong_t)off, func)); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp, 1740Sstevel@tonic-gate symp, off); 1750Sstevel@tonic-gate 1761399Sahl if (err == DT_PROC_ERR) { 1771399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, 1781399Sahl D_PROC_CREATEFAIL, "failed to create probe at " 1791399Sahl "'%s+0x%llx': %s", func, (u_longlong_t)off, 1801399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp)))); 1811399Sahl } 1821399Sahl 1830Sstevel@tonic-gate if (err == DT_PROC_ALIGN) { 1841399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN, 1851399Sahl "offset 0x%llx is not aligned on an instruction", 1861399Sahl (u_longlong_t)off)); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate nmatches++; 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate } else if (glob && !isdash) { 1920Sstevel@tonic-gate if (dt_pid_create_glob_offset_probes(pp->dpp_pr, 1931399Sahl pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) { 1941399Sahl return (dt_pid_error(dtp, pcb, dpr, ftp, 1951399Sahl D_PROC_CREATEFAIL, 1961399Sahl "failed to create offset probes in '%s': %s", func, 1971399Sahl dtrace_errmsg(dtp, dtrace_errno(dtp)))); 1981399Sahl } 1990Sstevel@tonic-gate 2000Sstevel@tonic-gate nmatches++; 2010Sstevel@tonic-gate } 2020Sstevel@tonic-gate 2030Sstevel@tonic-gate pp->dpp_nmatches += nmatches; 2040Sstevel@tonic-gate 2051399Sahl dt_free(dtp, ftp); 2060Sstevel@tonic-gate 2071399Sahl return (0); 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate static int 2110Sstevel@tonic-gate dt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func) 2120Sstevel@tonic-gate { 2130Sstevel@tonic-gate dt_pid_probe_t *pp = arg; 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate if (symp->st_shndx == SHN_UNDEF) 2160Sstevel@tonic-gate return (0); 2170Sstevel@tonic-gate 2180Sstevel@tonic-gate if (symp->st_size == 0) { 2190Sstevel@tonic-gate dt_dprintf("st_size of %s is zero\n", func); 2200Sstevel@tonic-gate return (0); 2210Sstevel@tonic-gate } 2220Sstevel@tonic-gate 2231399Sahl if (pp->dpp_last_taken == 0 || 2241399Sahl symp->st_value != pp->dpp_last.st_value || 2250Sstevel@tonic-gate symp->st_size != pp->dpp_last.st_size) { 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * Due to 4524008, _init and _fini may have a bloated st_size. 2280Sstevel@tonic-gate * While this bug has been fixed for a while, old binaries 2290Sstevel@tonic-gate * may exist that still exhibit this problem. As a result, we 2300Sstevel@tonic-gate * don't match _init and _fini though we allow users to 2310Sstevel@tonic-gate * specify them explicitly. 2320Sstevel@tonic-gate */ 2330Sstevel@tonic-gate if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0) 2340Sstevel@tonic-gate return (0); 2350Sstevel@tonic-gate 2361399Sahl if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) { 2371399Sahl pp->dpp_last = *symp; 2381399Sahl return (dt_pid_per_sym(pp, symp, func)); 2390Sstevel@tonic-gate } 2400Sstevel@tonic-gate } 2410Sstevel@tonic-gate 2420Sstevel@tonic-gate return (0); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2451399Sahl static int 2460Sstevel@tonic-gate dt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj) 2470Sstevel@tonic-gate { 2480Sstevel@tonic-gate dt_pid_probe_t *pp = arg; 2491399Sahl dtrace_hdl_t *dtp = pp->dpp_dtp; 2501399Sahl dt_pcb_t *pcb = pp->dpp_pcb; 2511399Sahl dt_proc_t *dpr = pp->dpp_dpr; 2520Sstevel@tonic-gate GElf_Sym sym; 2530Sstevel@tonic-gate 2540Sstevel@tonic-gate if (obj == NULL) 2551399Sahl return (0); 2560Sstevel@tonic-gate 2570Sstevel@tonic-gate (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid); 2580Sstevel@tonic-gate 2590Sstevel@tonic-gate if ((pp->dpp_obj = strrchr(obj, '/')) == NULL) 2600Sstevel@tonic-gate pp->dpp_obj = obj; 2610Sstevel@tonic-gate else 2620Sstevel@tonic-gate pp->dpp_obj++; 2630Sstevel@tonic-gate 2640Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym, 2650Sstevel@tonic-gate NULL) == 0) 2660Sstevel@tonic-gate pp->dpp_stret[0] = sym.st_value; 2670Sstevel@tonic-gate else 2680Sstevel@tonic-gate pp->dpp_stret[0] = 0; 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym, 2710Sstevel@tonic-gate NULL) == 0) 2720Sstevel@tonic-gate pp->dpp_stret[1] = sym.st_value; 2730Sstevel@tonic-gate else 2740Sstevel@tonic-gate pp->dpp_stret[1] = 0; 2750Sstevel@tonic-gate 2760Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym, 2770Sstevel@tonic-gate NULL) == 0) 2780Sstevel@tonic-gate pp->dpp_stret[2] = sym.st_value; 2790Sstevel@tonic-gate else 2800Sstevel@tonic-gate pp->dpp_stret[2] = 0; 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym, 2830Sstevel@tonic-gate NULL) == 0) 2840Sstevel@tonic-gate pp->dpp_stret[3] = sym.st_value; 2850Sstevel@tonic-gate else 2860Sstevel@tonic-gate pp->dpp_stret[3] = 0; 2870Sstevel@tonic-gate 2880Sstevel@tonic-gate dt_dprintf("%s stret %llx %llx %llx %llx\n", obj, 2890Sstevel@tonic-gate (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1], 2900Sstevel@tonic-gate (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]); 2910Sstevel@tonic-gate 2920Sstevel@tonic-gate /* 2930Sstevel@tonic-gate * If pp->dpp_func contains any globbing meta-characters, we need 2940Sstevel@tonic-gate * to iterate over the symbol table and compare each function name 2950Sstevel@tonic-gate * against the pattern. 2960Sstevel@tonic-gate */ 2970Sstevel@tonic-gate if (!strisglob(pp->dpp_func)) { 2980Sstevel@tonic-gate /* 2990Sstevel@tonic-gate * If we fail to lookup the symbol, try interpreting the 3000Sstevel@tonic-gate * function as the special "-" function that indicates that the 3010Sstevel@tonic-gate * probe name should be interpreted as a absolute virtual 3020Sstevel@tonic-gate * address. If that fails and we were matching a specific 3030Sstevel@tonic-gate * function in a specific module, report the error, otherwise 3040Sstevel@tonic-gate * just fail silently in the hopes that some other object will 3050Sstevel@tonic-gate * contain the desired symbol. 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, 3080Sstevel@tonic-gate pp->dpp_func, &sym, NULL) != 0) { 3090Sstevel@tonic-gate if (strcmp("-", pp->dpp_func) == 0) { 3100Sstevel@tonic-gate sym.st_name = 0; 3110Sstevel@tonic-gate sym.st_info = 3120Sstevel@tonic-gate GELF_ST_INFO(STB_LOCAL, STT_FUNC); 3130Sstevel@tonic-gate sym.st_other = 0; 3140Sstevel@tonic-gate sym.st_value = 0; 3150Sstevel@tonic-gate sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel == 3160Sstevel@tonic-gate PR_MODEL_ILP32 ? -1U : -1ULL; 3170Sstevel@tonic-gate 3180Sstevel@tonic-gate } else if (!strisglob(pp->dpp_mod)) { 3191399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, 3201399Sahl D_PROC_FUNC, 3211399Sahl "failed to lookup '%s' in module '%s'", 3221399Sahl pp->dpp_func, pp->dpp_mod)); 3230Sstevel@tonic-gate } else { 3241399Sahl return (0); 3250Sstevel@tonic-gate } 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate /* 3290Sstevel@tonic-gate * Only match defined functions of non-zero size. 3300Sstevel@tonic-gate */ 3310Sstevel@tonic-gate if (GELF_ST_TYPE(sym.st_info) != STT_FUNC || 3320Sstevel@tonic-gate sym.st_shndx == SHN_UNDEF || sym.st_size == 0) 3331399Sahl return (0); 3340Sstevel@tonic-gate 3350Sstevel@tonic-gate /* 3360Sstevel@tonic-gate * We don't instrument PLTs -- they're dynamically rewritten, 3370Sstevel@tonic-gate * and, so, inherently dicey to instrument. 3380Sstevel@tonic-gate */ 3390Sstevel@tonic-gate if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL) 3401399Sahl return (0); 3410Sstevel@tonic-gate 3420Sstevel@tonic-gate (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func, 3430Sstevel@tonic-gate DTRACE_FUNCNAMELEN, &sym); 3440Sstevel@tonic-gate 3451399Sahl return (dt_pid_per_sym(pp, &sym, pp->dpp_func)); 3460Sstevel@tonic-gate } else { 3470Sstevel@tonic-gate uint_t nmatches = pp->dpp_nmatches; 3480Sstevel@tonic-gate 3491399Sahl if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB, 3501399Sahl BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1) 3511399Sahl return (1); 3520Sstevel@tonic-gate 3530Sstevel@tonic-gate if (nmatches == pp->dpp_nmatches) { 3540Sstevel@tonic-gate /* 3550Sstevel@tonic-gate * If we didn't match anything in the PR_SYMTAB, try 3560Sstevel@tonic-gate * the PR_DYNSYM. 3570Sstevel@tonic-gate */ 3581399Sahl if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM, 3591399Sahl BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1) 3601399Sahl return (1); 3610Sstevel@tonic-gate } 3620Sstevel@tonic-gate } 3631399Sahl 3641399Sahl return (0); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate static int 3680Sstevel@tonic-gate dt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj) 3690Sstevel@tonic-gate { 3701399Sahl char name[DTRACE_MODNAMELEN]; 3710Sstevel@tonic-gate dt_pid_probe_t *pp = arg; 3720Sstevel@tonic-gate 3731399Sahl if (gmatch(obj, pp->dpp_mod)) 3741399Sahl return (dt_pid_per_mod(pp, pmp, obj)); 3750Sstevel@tonic-gate 3761399Sahl (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid); 3770Sstevel@tonic-gate 3781399Sahl if ((pp->dpp_obj = strrchr(obj, '/')) == NULL) 3791399Sahl pp->dpp_obj = obj; 3801399Sahl else 3811399Sahl pp->dpp_obj++; 3820Sstevel@tonic-gate 3831399Sahl dt_pid_objname(name, sizeof (name), pp->dpp_lmid, obj); 3840Sstevel@tonic-gate 3851399Sahl if (gmatch(name, pp->dpp_mod)) 3861399Sahl return (dt_pid_per_mod(pp, pmp, obj)); 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate return (0); 3890Sstevel@tonic-gate } 3900Sstevel@tonic-gate 3910Sstevel@tonic-gate static const prmap_t * 3920Sstevel@tonic-gate dt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P) 3930Sstevel@tonic-gate { 3940Sstevel@tonic-gate char m[MAXPATHLEN]; 3950Sstevel@tonic-gate Lmid_t lmid = PR_LMID_EVERY; 3960Sstevel@tonic-gate const char *obj; 3970Sstevel@tonic-gate const prmap_t *pmp; 3980Sstevel@tonic-gate 3990Sstevel@tonic-gate /* 4000Sstevel@tonic-gate * Pick apart the link map from the library name. 4010Sstevel@tonic-gate */ 4020Sstevel@tonic-gate if (strchr(pdp->dtpd_mod, '`') != NULL) { 4030Sstevel@tonic-gate char *end; 4040Sstevel@tonic-gate 4050Sstevel@tonic-gate if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 || 4060Sstevel@tonic-gate !isdigit(pdp->dtpd_mod[2])) 4070Sstevel@tonic-gate return (NULL); 4080Sstevel@tonic-gate 4090Sstevel@tonic-gate lmid = strtoul(&pdp->dtpd_mod[2], &end, 16); 4100Sstevel@tonic-gate 4110Sstevel@tonic-gate obj = end + 1; 4120Sstevel@tonic-gate 4130Sstevel@tonic-gate if (*end != '`' || strchr(obj, '`') != NULL) 4140Sstevel@tonic-gate return (NULL); 4150Sstevel@tonic-gate 4160Sstevel@tonic-gate } else { 4170Sstevel@tonic-gate obj = pdp->dtpd_mod; 4180Sstevel@tonic-gate } 4190Sstevel@tonic-gate 4200Sstevel@tonic-gate if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL) 4210Sstevel@tonic-gate return (NULL); 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m)); 4240Sstevel@tonic-gate if ((obj = strrchr(m, '/')) == NULL) 4250Sstevel@tonic-gate obj = &m[0]; 4260Sstevel@tonic-gate else 4270Sstevel@tonic-gate obj++; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate (void) Plmid(P, pmp->pr_vaddr, &lmid); 430630Sahl dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj); 4310Sstevel@tonic-gate 4320Sstevel@tonic-gate return (pmp); 4330Sstevel@tonic-gate } 4340Sstevel@tonic-gate 4350Sstevel@tonic-gate 4361399Sahl static int 4371399Sahl dt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, 4381399Sahl dt_pcb_t *pcb, dt_proc_t *dpr) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate dt_pid_probe_t pp; 4411399Sahl int ret = 0; 4420Sstevel@tonic-gate 4431399Sahl pp.dpp_dtp = dtp; 4441399Sahl pp.dpp_dpr = dpr; 4451399Sahl pp.dpp_pr = dpr->dpr_proc; 4461399Sahl pp.dpp_pcb = pcb; 4470Sstevel@tonic-gate 4480Sstevel@tonic-gate /* 4490Sstevel@tonic-gate * We can only trace dynamically-linked executables (since we've 4500Sstevel@tonic-gate * hidden some magic in ld.so.1 as well as libc.so.1). 4510Sstevel@tonic-gate */ 4520Sstevel@tonic-gate if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) { 4531399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN, 4541399Sahl "process %s is not a dynamically-linked executable", 4551399Sahl &pdp->dtpd_provider[3])); 4560Sstevel@tonic-gate } 4570Sstevel@tonic-gate 4580Sstevel@tonic-gate pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*"; 4590Sstevel@tonic-gate pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*"; 4600Sstevel@tonic-gate pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*"; 4611399Sahl pp.dpp_last_taken = 0; 4620Sstevel@tonic-gate 4630Sstevel@tonic-gate if (strcmp(pp.dpp_func, "-") == 0) { 4640Sstevel@tonic-gate const prmap_t *aout, *pmp; 4650Sstevel@tonic-gate 4660Sstevel@tonic-gate if (pdp->dtpd_mod[0] == '\0') { 4670Sstevel@tonic-gate pp.dpp_mod = pdp->dtpd_mod; 4680Sstevel@tonic-gate (void) strcpy(pdp->dtpd_mod, "a.out"); 4690Sstevel@tonic-gate } else if (strisglob(pp.dpp_mod) || 4700Sstevel@tonic-gate (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL || 4710Sstevel@tonic-gate (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL || 4720Sstevel@tonic-gate aout->pr_vaddr != pmp->pr_vaddr) { 4731399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB, 4741399Sahl "only the a.out module is valid with the " 4751399Sahl "'-' function")); 4760Sstevel@tonic-gate } 4770Sstevel@tonic-gate 4780Sstevel@tonic-gate if (strisglob(pp.dpp_name)) { 4791399Sahl return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME, 4801399Sahl "only individual addresses may be specified " 4811399Sahl "with the '-' function")); 4820Sstevel@tonic-gate } 4830Sstevel@tonic-gate } 4840Sstevel@tonic-gate 4850Sstevel@tonic-gate /* 4860Sstevel@tonic-gate * If pp.dpp_mod contains any globbing meta-characters, we need 4870Sstevel@tonic-gate * to iterate over each module and compare its name against the 4880Sstevel@tonic-gate * pattern. An empty module name is treated as '*'. 4890Sstevel@tonic-gate */ 4900Sstevel@tonic-gate if (strisglob(pp.dpp_mod)) { 4911399Sahl ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp); 4920Sstevel@tonic-gate } else { 4930Sstevel@tonic-gate const prmap_t *pmp; 4940Sstevel@tonic-gate char *obj; 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate /* 4971399Sahl * If we can't find a matching module, don't sweat it -- either 4980Sstevel@tonic-gate * we'll fail the enabling because the probes don't exist or 4990Sstevel@tonic-gate * we'll wait for that module to come along. 5000Sstevel@tonic-gate */ 5010Sstevel@tonic-gate if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) { 5020Sstevel@tonic-gate if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL) 5030Sstevel@tonic-gate obj = pdp->dtpd_mod; 5040Sstevel@tonic-gate else 5050Sstevel@tonic-gate obj++; 5060Sstevel@tonic-gate 5071399Sahl ret = dt_pid_per_mod(&pp, pmp, obj); 5080Sstevel@tonic-gate } 5090Sstevel@tonic-gate } 5100Sstevel@tonic-gate 5111399Sahl return (ret); 5120Sstevel@tonic-gate } 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate static int 5150Sstevel@tonic-gate dt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname) 5160Sstevel@tonic-gate { 5170Sstevel@tonic-gate struct ps_prochandle *P = data; 5180Sstevel@tonic-gate GElf_Sym sym; 5190Sstevel@tonic-gate prsyminfo_t sip; 5200Sstevel@tonic-gate dof_helper_t dh; 5210Sstevel@tonic-gate GElf_Half e_type; 5220Sstevel@tonic-gate const char *mname; 5230Sstevel@tonic-gate const char *syms[] = { "___SUNW_dof", "__SUNW_dof" }; 5241399Sahl int i, fd = -1; 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate /* 5270Sstevel@tonic-gate * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and 5280Sstevel@tonic-gate * __SUNW_dof is for actively-loaded DOF sections. We try to force 5290Sstevel@tonic-gate * in both types of DOF section since the process may not yet have 5300Sstevel@tonic-gate * run the code to instantiate these providers. 5310Sstevel@tonic-gate */ 5320Sstevel@tonic-gate for (i = 0; i < 2; i++) { 5330Sstevel@tonic-gate if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym, 5340Sstevel@tonic-gate &sip) != 0) { 5350Sstevel@tonic-gate continue; 5360Sstevel@tonic-gate } 5370Sstevel@tonic-gate 5380Sstevel@tonic-gate if ((mname = strrchr(oname, '/')) == NULL) 5390Sstevel@tonic-gate mname = oname; 5400Sstevel@tonic-gate else 5410Sstevel@tonic-gate mname++; 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname); 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr + 5460Sstevel@tonic-gate offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) { 5470Sstevel@tonic-gate dt_dprintf("read of ELF header failed"); 5480Sstevel@tonic-gate continue; 5490Sstevel@tonic-gate } 5500Sstevel@tonic-gate 5510Sstevel@tonic-gate dh.dofhp_dof = sym.st_value; 5520Sstevel@tonic-gate dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr; 5530Sstevel@tonic-gate 554630Sahl dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod), 555630Sahl sip.prs_lmid, mname); 5560Sstevel@tonic-gate 5571399Sahl if (fd == -1 && 5581399Sahl (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) { 5590Sstevel@tonic-gate dt_dprintf("pr_open of helper device failed: %s\n", 5600Sstevel@tonic-gate strerror(errno)); 5611399Sahl return (-1); /* errno is set for us */ 5620Sstevel@tonic-gate } 5630Sstevel@tonic-gate 5641399Sahl if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0) 5651399Sahl dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod); 5661399Sahl } 5670Sstevel@tonic-gate 5681399Sahl if (fd != -1) 5691399Sahl (void) pr_close(P, fd); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate return (0); 5720Sstevel@tonic-gate } 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate static int 5751399Sahl dt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, 5761399Sahl dt_pcb_t *pcb, dt_proc_t *dpr) 5770Sstevel@tonic-gate { 5780Sstevel@tonic-gate struct ps_prochandle *P = dpr->dpr_proc; 5791399Sahl int ret = 0; 5800Sstevel@tonic-gate 5810Sstevel@tonic-gate assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 5820Sstevel@tonic-gate 5830Sstevel@tonic-gate (void) Pupdate_maps(P); 5841399Sahl if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) { 5851399Sahl ret = -1; 5861399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT, 5871399Sahl "failed to instantiate probes for pid %d: %s", 5881399Sahl (int)Pstatus(P)->pr_pid, strerror(errno)); 5891399Sahl } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate /* 5920Sstevel@tonic-gate * Put the module name in its canonical form. 5930Sstevel@tonic-gate */ 5940Sstevel@tonic-gate (void) dt_pid_fix_mod(pdp, P); 5950Sstevel@tonic-gate 5961399Sahl return (ret); 5970Sstevel@tonic-gate } 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate static pid_t 6001399Sahl dt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb, 6011399Sahl dt_proc_t *dpr) 6020Sstevel@tonic-gate { 6030Sstevel@tonic-gate pid_t pid; 6040Sstevel@tonic-gate char *c, *last = NULL, *end; 6050Sstevel@tonic-gate 6060Sstevel@tonic-gate for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) { 6070Sstevel@tonic-gate if (!isdigit(*c)) 6080Sstevel@tonic-gate last = c; 6090Sstevel@tonic-gate } 6100Sstevel@tonic-gate 6110Sstevel@tonic-gate if (last == NULL || (*(++last) == '\0')) { 6121399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV, 6131399Sahl "'%s' is not a valid provider", pdp->dtpd_provider); 6141399Sahl return (-1); 6150Sstevel@tonic-gate } 6160Sstevel@tonic-gate 6170Sstevel@tonic-gate errno = 0; 6180Sstevel@tonic-gate pid = strtol(last, &end, 10); 6190Sstevel@tonic-gate 6200Sstevel@tonic-gate if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) { 6211399Sahl (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID, 6221399Sahl "'%s' does not contain a valid pid", pdp->dtpd_provider); 6231399Sahl return (-1); 6240Sstevel@tonic-gate } 6250Sstevel@tonic-gate 6260Sstevel@tonic-gate return (pid); 6270Sstevel@tonic-gate } 6280Sstevel@tonic-gate 6291399Sahl int 6301399Sahl dt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb) 6310Sstevel@tonic-gate { 6320Sstevel@tonic-gate char provname[DTRACE_PROVNAMELEN]; 6330Sstevel@tonic-gate struct ps_prochandle *P; 6340Sstevel@tonic-gate dt_proc_t *dpr; 6351399Sahl pid_t pid; 6361593Sahl int err = 0; 6371399Sahl 6381399Sahl assert(pcb != NULL); 6391399Sahl 6401399Sahl if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1) 6411399Sahl return (-1); 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate if (dtp->dt_ftfd == -1) { 6440Sstevel@tonic-gate if (dtp->dt_fterr == ENOENT) { 6451399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV, 6461399Sahl "pid provider is not installed on this system"); 6470Sstevel@tonic-gate } else { 6481399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV, 6491399Sahl "pid provider is not available: %s", 6501399Sahl strerror(dtp->dt_fterr)); 6510Sstevel@tonic-gate } 6521399Sahl 6531399Sahl return (-1); 6540Sstevel@tonic-gate } 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid); 6570Sstevel@tonic-gate 658*5984Sjhaslam if (gmatch(provname, pdp->dtpd_provider) != 0) { 6591399Sahl if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 6601399Sahl 0)) == NULL) { 6611399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB, 6621399Sahl "failed to grab process %d", (int)pid); 6631399Sahl return (-1); 6641399Sahl } 6650Sstevel@tonic-gate 6660Sstevel@tonic-gate dpr = dt_proc_lookup(dtp, P, 0); 6670Sstevel@tonic-gate assert(dpr != NULL); 6681399Sahl (void) pthread_mutex_lock(&dpr->dpr_lock); 6690Sstevel@tonic-gate 6701399Sahl err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr); 6711399Sahl 6721399Sahl (void) pthread_mutex_unlock(&dpr->dpr_lock); 6731399Sahl dt_proc_release(dtp, P); 674*5984Sjhaslam } 6751399Sahl 676*5984Sjhaslam /* 677*5984Sjhaslam * If it's not strictly a pid provider, we might match a USDT provider. 678*5984Sjhaslam */ 679*5984Sjhaslam if (strcmp(provname, pdp->dtpd_provider) != 0) { 6801399Sahl if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) { 6811399Sahl (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB, 6821399Sahl "failed to grab process %d", (int)pid); 6831399Sahl return (-1); 6841399Sahl } 6851399Sahl 6861399Sahl dpr = dt_proc_lookup(dtp, P, 0); 6871399Sahl assert(dpr != NULL); 6880Sstevel@tonic-gate (void) pthread_mutex_lock(&dpr->dpr_lock); 6890Sstevel@tonic-gate 6900Sstevel@tonic-gate if (!dpr->dpr_usdt) { 6911399Sahl err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr); 6920Sstevel@tonic-gate dpr->dpr_usdt = B_TRUE; 6930Sstevel@tonic-gate } 6940Sstevel@tonic-gate 6950Sstevel@tonic-gate (void) pthread_mutex_unlock(&dpr->dpr_lock); 6960Sstevel@tonic-gate dt_proc_release(dtp, P); 6971399Sahl } 6980Sstevel@tonic-gate 6991399Sahl return (err ? -1 : 0); 7000Sstevel@tonic-gate } 7010Sstevel@tonic-gate 7021399Sahl int 7030Sstevel@tonic-gate dt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr) 7040Sstevel@tonic-gate { 7050Sstevel@tonic-gate dtrace_prog_t *pgp; 7060Sstevel@tonic-gate dt_stmt_t *stp; 7070Sstevel@tonic-gate dtrace_probedesc_t *pdp, pd; 7080Sstevel@tonic-gate pid_t pid; 7091399Sahl int ret = 0, found = B_FALSE; 710*5984Sjhaslam char provname[DTRACE_PROVNAMELEN]; 711*5984Sjhaslam 712*5984Sjhaslam (void) snprintf(provname, sizeof (provname), "pid%d", 713*5984Sjhaslam (int)dpr->dpr_pid); 7140Sstevel@tonic-gate 7150Sstevel@tonic-gate for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL; 7160Sstevel@tonic-gate pgp = dt_list_next(pgp)) { 7170Sstevel@tonic-gate 7180Sstevel@tonic-gate for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; 7190Sstevel@tonic-gate stp = dt_list_next(stp)) { 7200Sstevel@tonic-gate 7210Sstevel@tonic-gate pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe; 7221399Sahl pid = dt_pid_get_pid(pdp, dtp, NULL, dpr); 7231399Sahl if (pid != dpr->dpr_pid) 7240Sstevel@tonic-gate continue; 7250Sstevel@tonic-gate 7260Sstevel@tonic-gate found = B_TRUE; 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate pd = *pdp; 7290Sstevel@tonic-gate 730*5984Sjhaslam if (gmatch(provname, pdp->dtpd_provider) != 0 && 731*5984Sjhaslam dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0) 732*5984Sjhaslam ret = 1; 733*5984Sjhaslam 734*5984Sjhaslam /* 735*5984Sjhaslam * If it's not strictly a pid provider, we might match 736*5984Sjhaslam * a USDT provider. 737*5984Sjhaslam */ 738*5984Sjhaslam if (strcmp(provname, pdp->dtpd_provider) != 0 && 739*5984Sjhaslam dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0) 740*5984Sjhaslam ret = 1; 7410Sstevel@tonic-gate } 7420Sstevel@tonic-gate } 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate if (found) { 7450Sstevel@tonic-gate /* 7460Sstevel@tonic-gate * Give DTrace a shot to the ribs to get it to check 7470Sstevel@tonic-gate * out the newly created probes. 7480Sstevel@tonic-gate */ 7490Sstevel@tonic-gate (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL); 7500Sstevel@tonic-gate } 7511399Sahl 7521399Sahl return (ret); 7530Sstevel@tonic-gate } 754