dt_pid.c revision 178479
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/* 23178479Sjb * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24178479Sjb * Use is subject to license terms. 25178479Sjb */ 26178479Sjb 27178479Sjb#pragma ident "%Z%%M% %I% %E% SMI" 28178479Sjb 29178479Sjb#include <assert.h> 30178479Sjb#include <strings.h> 31178479Sjb#include <stdlib.h> 32178479Sjb#include <stdio.h> 33178479Sjb#include <errno.h> 34178479Sjb#include <ctype.h> 35178479Sjb#if defined(sun) 36178479Sjb#include <alloca.h> 37178479Sjb#endif 38178479Sjb#include <libgen.h> 39178479Sjb#include <stddef.h> 40178479Sjb 41178479Sjb#include <dt_impl.h> 42178479Sjb#include <dt_program.h> 43178479Sjb#include <dt_pid.h> 44178479Sjb#include <dt_string.h> 45178479Sjb 46178479Sjbtypedef struct dt_pid_probe { 47178479Sjb dtrace_hdl_t *dpp_dtp; 48178479Sjb dt_pcb_t *dpp_pcb; 49178479Sjb dt_proc_t *dpp_dpr; 50178479Sjb struct ps_prochandle *dpp_pr; 51178479Sjb const char *dpp_mod; 52178479Sjb char *dpp_func; 53178479Sjb const char *dpp_name; 54178479Sjb const char *dpp_obj; 55178479Sjb uintptr_t dpp_pc; 56178479Sjb size_t dpp_size; 57178479Sjb Lmid_t dpp_lmid; 58178479Sjb uint_t dpp_nmatches; 59178479Sjb uint64_t dpp_stret[4]; 60178479Sjb GElf_Sym dpp_last; 61178479Sjb uint_t dpp_last_taken; 62178479Sjb} dt_pid_probe_t; 63178479Sjb 64178479Sjb/* 65178479Sjb * Compose the lmid and object name into the canonical representation. We 66178479Sjb * omit the lmid for the default link map for convenience. 67178479Sjb */ 68178479Sjbstatic void 69178479Sjbdt_pid_objname(char *buf, size_t len, Lmid_t lmid, const char *obj) 70178479Sjb{ 71178479Sjb#if defined(sun) 72178479Sjb if (lmid == LM_ID_BASE) 73178479Sjb (void) strncpy(buf, obj, len); 74178479Sjb else 75178479Sjb (void) snprintf(buf, len, "LM%lx`%s", lmid, obj); 76178479Sjb#else 77178479Sjb (void) strncpy(buf, obj, len); 78178479Sjb#endif 79178479Sjb} 80178479Sjb 81178479Sjbstatic int 82178479Sjbdt_pid_error(dtrace_hdl_t *dtp, dt_pcb_t *pcb, dt_proc_t *dpr, 83178479Sjb fasttrap_probe_spec_t *ftp, dt_errtag_t tag, const char *fmt, ...) 84178479Sjb{ 85178479Sjb va_list ap; 86178479Sjb int len; 87178479Sjb 88178479Sjb if (ftp != NULL) 89178479Sjb dt_free(dtp, ftp); 90178479Sjb 91178479Sjb va_start(ap, fmt); 92178479Sjb if (pcb == NULL) { 93178479Sjb assert(dpr != NULL); 94178479Sjb len = vsnprintf(dpr->dpr_errmsg, sizeof (dpr->dpr_errmsg), 95178479Sjb fmt, ap); 96178479Sjb assert(len >= 2); 97178479Sjb if (dpr->dpr_errmsg[len - 2] == '\n') 98178479Sjb dpr->dpr_errmsg[len - 2] = '\0'; 99178479Sjb } else { 100178479Sjb dt_set_errmsg(dtp, dt_errtag(tag), pcb->pcb_region, 101178479Sjb pcb->pcb_filetag, pcb->pcb_fileptr ? yylineno : 0, fmt, ap); 102178479Sjb } 103178479Sjb va_end(ap); 104178479Sjb 105178479Sjb return (1); 106178479Sjb} 107178479Sjb 108178479Sjbstatic int 109178479Sjbdt_pid_per_sym(dt_pid_probe_t *pp, const GElf_Sym *symp, const char *func) 110178479Sjb{ 111178479Sjb dtrace_hdl_t *dtp = pp->dpp_dtp; 112178479Sjb dt_pcb_t *pcb = pp->dpp_pcb; 113178479Sjb dt_proc_t *dpr = pp->dpp_dpr; 114178479Sjb fasttrap_probe_spec_t *ftp; 115178479Sjb uint64_t off; 116178479Sjb char *end; 117178479Sjb uint_t nmatches = 0; 118178479Sjb ulong_t sz; 119178479Sjb int glob, err; 120178479Sjb int isdash = strcmp("-", func) == 0; 121178479Sjb pid_t pid; 122178479Sjb 123178479Sjb#if defined(sun) 124178479Sjb pid = Pstatus(pp->dpp_pr)->pr_pid; 125178479Sjb#else 126178479Sjb pid = proc_getpid(pp->dpp_pr); 127178479Sjb#endif 128178479Sjb 129178479Sjb dt_dprintf("creating probe pid%d:%s:%s:%s\n", (int)pid, pp->dpp_obj, 130178479Sjb func, pp->dpp_name); 131178479Sjb 132178479Sjb sz = sizeof (fasttrap_probe_spec_t) + (isdash ? 4 : 133178479Sjb (symp->st_size - 1) * sizeof (ftp->ftps_offs[0])); 134178479Sjb 135178479Sjb if ((ftp = dt_alloc(dtp, sz)) == NULL) { 136178479Sjb dt_dprintf("proc_per_sym: dt_alloc(%lu) failed\n", sz); 137178479Sjb return (1); /* errno is set for us */ 138178479Sjb } 139178479Sjb 140178479Sjb ftp->ftps_pid = pid; 141178479Sjb (void) strncpy(ftp->ftps_func, func, sizeof (ftp->ftps_func)); 142178479Sjb 143178479Sjb dt_pid_objname(ftp->ftps_mod, sizeof (ftp->ftps_mod), pp->dpp_lmid, 144178479Sjb pp->dpp_obj); 145178479Sjb 146178479Sjb if (!isdash && gmatch("return", pp->dpp_name)) { 147178479Sjb#ifdef DOODAD 148178479Sjb if (dt_pid_create_return_probe(pp->dpp_pr, dtp, ftp, symp, 149178479Sjb pp->dpp_stret) < 0) { 150178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, 151178479Sjb D_PROC_CREATEFAIL, "failed to create return probe " 152178479Sjb "for '%s': %s", func, 153178479Sjb dtrace_errmsg(dtp, dtrace_errno(dtp)))); 154178479Sjb } 155178479Sjb#endif 156178479Sjb 157178479Sjb nmatches++; 158178479Sjb } 159178479Sjb 160178479Sjb if (!isdash && gmatch("entry", pp->dpp_name)) { 161178479Sjb#ifdef DOODAD 162178479Sjb if (dt_pid_create_entry_probe(pp->dpp_pr, dtp, ftp, symp) < 0) { 163178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, 164178479Sjb D_PROC_CREATEFAIL, "failed to create entry probe " 165178479Sjb "for '%s': %s", func, 166178479Sjb dtrace_errmsg(dtp, dtrace_errno(dtp)))); 167178479Sjb } 168178479Sjb#endif 169178479Sjb 170178479Sjb nmatches++; 171178479Sjb } 172178479Sjb 173178479Sjb glob = strisglob(pp->dpp_name); 174178479Sjb if (!glob && nmatches == 0) { 175178479Sjb off = strtoull(pp->dpp_name, &end, 16); 176178479Sjb if (*end != '\0') { 177178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_NAME, 178178479Sjb "'%s' is an invalid probe name", pp->dpp_name)); 179178479Sjb } 180178479Sjb 181178479Sjb if (off >= symp->st_size) { 182178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_OFF, 183178479Sjb "offset 0x%llx outside of function '%s'", 184178479Sjb (u_longlong_t)off, func)); 185178479Sjb } 186178479Sjb 187178479Sjb#ifdef DOODAD 188178479Sjb err = dt_pid_create_offset_probe(pp->dpp_pr, pp->dpp_dtp, ftp, 189178479Sjb symp, off); 190178479Sjb#endif 191178479Sjb 192178479Sjb if (err == DT_PROC_ERR) { 193178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, 194178479Sjb D_PROC_CREATEFAIL, "failed to create probe at " 195178479Sjb "'%s+0x%llx': %s", func, (u_longlong_t)off, 196178479Sjb dtrace_errmsg(dtp, dtrace_errno(dtp)))); 197178479Sjb } 198178479Sjb 199178479Sjb if (err == DT_PROC_ALIGN) { 200178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, D_PROC_ALIGN, 201178479Sjb "offset 0x%llx is not aligned on an instruction", 202178479Sjb (u_longlong_t)off)); 203178479Sjb } 204178479Sjb 205178479Sjb nmatches++; 206178479Sjb 207178479Sjb } else if (glob && !isdash) { 208178479Sjb#ifdef DOODAD 209178479Sjb if (dt_pid_create_glob_offset_probes(pp->dpp_pr, 210178479Sjb pp->dpp_dtp, ftp, symp, pp->dpp_name) < 0) { 211178479Sjb return (dt_pid_error(dtp, pcb, dpr, ftp, 212178479Sjb D_PROC_CREATEFAIL, 213178479Sjb "failed to create offset probes in '%s': %s", func, 214178479Sjb dtrace_errmsg(dtp, dtrace_errno(dtp)))); 215178479Sjb } 216178479Sjb#endif 217178479Sjb 218178479Sjb nmatches++; 219178479Sjb } 220178479Sjb 221178479Sjb pp->dpp_nmatches += nmatches; 222178479Sjb 223178479Sjb dt_free(dtp, ftp); 224178479Sjb 225178479Sjb return (0); 226178479Sjb} 227178479Sjb 228178479Sjbstatic int 229178479Sjbdt_pid_sym_filt(void *arg, const GElf_Sym *symp, const char *func) 230178479Sjb{ 231178479Sjb dt_pid_probe_t *pp = arg; 232178479Sjb 233178479Sjb if (symp->st_shndx == SHN_UNDEF) 234178479Sjb return (0); 235178479Sjb 236178479Sjb if (symp->st_size == 0) { 237178479Sjb dt_dprintf("st_size of %s is zero\n", func); 238178479Sjb return (0); 239178479Sjb } 240178479Sjb 241178479Sjb if (pp->dpp_last_taken == 0 || 242178479Sjb symp->st_value != pp->dpp_last.st_value || 243178479Sjb symp->st_size != pp->dpp_last.st_size) { 244178479Sjb /* 245178479Sjb * Due to 4524008, _init and _fini may have a bloated st_size. 246178479Sjb * While this bug has been fixed for a while, old binaries 247178479Sjb * may exist that still exhibit this problem. As a result, we 248178479Sjb * don't match _init and _fini though we allow users to 249178479Sjb * specify them explicitly. 250178479Sjb */ 251178479Sjb if (strcmp(func, "_init") == 0 || strcmp(func, "_fini") == 0) 252178479Sjb return (0); 253178479Sjb 254178479Sjb if ((pp->dpp_last_taken = gmatch(func, pp->dpp_func)) != 0) { 255178479Sjb pp->dpp_last = *symp; 256178479Sjb return (dt_pid_per_sym(pp, symp, func)); 257178479Sjb } 258178479Sjb } 259178479Sjb 260178479Sjb return (0); 261178479Sjb} 262178479Sjb 263178479Sjbstatic int 264178479Sjbdt_pid_per_mod(void *arg, const prmap_t *pmp, const char *obj) 265178479Sjb{ 266178479Sjb dt_pid_probe_t *pp = arg; 267178479Sjb dtrace_hdl_t *dtp = pp->dpp_dtp; 268178479Sjb dt_pcb_t *pcb = pp->dpp_pcb; 269178479Sjb dt_proc_t *dpr = pp->dpp_dpr; 270178479Sjb GElf_Sym sym; 271178479Sjb 272178479Sjb if (obj == NULL) 273178479Sjb return (0); 274178479Sjb 275178479Sjb#if defined(sun) 276178479Sjb (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid); 277178479Sjb#endif 278178479Sjb 279178479Sjb 280178479Sjb if ((pp->dpp_obj = strrchr(obj, '/')) == NULL) 281178479Sjb pp->dpp_obj = obj; 282178479Sjb else 283178479Sjb pp->dpp_obj++; 284178479Sjb 285178479Sjb#if defined(sun) 286178479Sjb if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret1", &sym, 287178479Sjb NULL) == 0) 288178479Sjb pp->dpp_stret[0] = sym.st_value; 289178479Sjb else 290178479Sjb pp->dpp_stret[0] = 0; 291178479Sjb 292178479Sjb if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret2", &sym, 293178479Sjb NULL) == 0) 294178479Sjb pp->dpp_stret[1] = sym.st_value; 295178479Sjb else 296178479Sjb pp->dpp_stret[1] = 0; 297178479Sjb 298178479Sjb if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret4", &sym, 299178479Sjb NULL) == 0) 300178479Sjb pp->dpp_stret[2] = sym.st_value; 301178479Sjb else 302178479Sjb pp->dpp_stret[2] = 0; 303178479Sjb 304178479Sjb if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, ".stret8", &sym, 305178479Sjb NULL) == 0) 306178479Sjb pp->dpp_stret[3] = sym.st_value; 307178479Sjb else 308178479Sjb pp->dpp_stret[3] = 0; 309178479Sjb#else 310178479Sjb if (proc_name2sym(pp->dpp_pr, obj, ".stret1", &sym) == 0) 311178479Sjb pp->dpp_stret[0] = sym.st_value; 312178479Sjb else 313178479Sjb pp->dpp_stret[0] = 0; 314178479Sjb 315178479Sjb if (proc_name2sym(pp->dpp_pr, obj, ".stret2", &sym) == 0) 316178479Sjb pp->dpp_stret[1] = sym.st_value; 317178479Sjb else 318178479Sjb pp->dpp_stret[1] = 0; 319178479Sjb 320178479Sjb if (proc_name2sym(pp->dpp_pr, obj, ".stret4", &sym) == 0) 321178479Sjb pp->dpp_stret[2] = sym.st_value; 322178479Sjb else 323178479Sjb pp->dpp_stret[2] = 0; 324178479Sjb 325178479Sjb if (proc_name2sym(pp->dpp_pr, obj, ".stret8", &sym) == 0) 326178479Sjb pp->dpp_stret[3] = sym.st_value; 327178479Sjb else 328178479Sjb pp->dpp_stret[3] = 0; 329178479Sjb#endif 330178479Sjb 331178479Sjb dt_dprintf("%s stret %llx %llx %llx %llx\n", obj, 332178479Sjb (u_longlong_t)pp->dpp_stret[0], (u_longlong_t)pp->dpp_stret[1], 333178479Sjb (u_longlong_t)pp->dpp_stret[2], (u_longlong_t)pp->dpp_stret[3]); 334178479Sjb 335178479Sjb /* 336178479Sjb * If pp->dpp_func contains any globbing meta-characters, we need 337178479Sjb * to iterate over the symbol table and compare each function name 338178479Sjb * against the pattern. 339178479Sjb */ 340178479Sjb if (!strisglob(pp->dpp_func)) { 341178479Sjb /* 342178479Sjb * If we fail to lookup the symbol, try interpreting the 343178479Sjb * function as the special "-" function that indicates that the 344178479Sjb * probe name should be interpreted as a absolute virtual 345178479Sjb * address. If that fails and we were matching a specific 346178479Sjb * function in a specific module, report the error, otherwise 347178479Sjb * just fail silently in the hopes that some other object will 348178479Sjb * contain the desired symbol. 349178479Sjb */ 350178479Sjb#if defined(sun) 351178479Sjb if (Pxlookup_by_name(pp->dpp_pr, pp->dpp_lmid, obj, 352178479Sjb pp->dpp_func, &sym, NULL) != 0) { 353178479Sjb#else 354178479Sjb if (proc_name2sym(pp->dpp_pr, obj, pp->dpp_func, &sym) != 0) { 355178479Sjb#endif 356178479Sjb if (strcmp("-", pp->dpp_func) == 0) { 357178479Sjb sym.st_name = 0; 358178479Sjb sym.st_info = 359178479Sjb GELF_ST_INFO(STB_LOCAL, STT_FUNC); 360178479Sjb sym.st_other = 0; 361178479Sjb sym.st_value = 0; 362178479Sjb#if defined(sun) 363178479Sjb sym.st_size = Pstatus(pp->dpp_pr)->pr_dmodel == 364178479Sjb PR_MODEL_ILP32 ? -1U : -1ULL; 365178479Sjb#else 366178479Sjb sym.st_size = ~((Elf64_Xword) 0); 367178479Sjb#endif 368178479Sjb 369178479Sjb } else if (!strisglob(pp->dpp_mod)) { 370178479Sjb return (dt_pid_error(dtp, pcb, dpr, NULL, 371178479Sjb D_PROC_FUNC, 372178479Sjb "failed to lookup '%s' in module '%s'", 373178479Sjb pp->dpp_func, pp->dpp_mod)); 374178479Sjb } else { 375178479Sjb return (0); 376178479Sjb } 377178479Sjb } 378178479Sjb 379178479Sjb /* 380178479Sjb * Only match defined functions of non-zero size. 381178479Sjb */ 382178479Sjb if (GELF_ST_TYPE(sym.st_info) != STT_FUNC || 383178479Sjb sym.st_shndx == SHN_UNDEF || sym.st_size == 0) 384178479Sjb return (0); 385178479Sjb 386178479Sjb /* 387178479Sjb * We don't instrument PLTs -- they're dynamically rewritten, 388178479Sjb * and, so, inherently dicey to instrument. 389178479Sjb */ 390178479Sjb#ifdef DOODAD 391178479Sjb if (Ppltdest(pp->dpp_pr, sym.st_value) != NULL) 392178479Sjb return (0); 393178479Sjb#endif 394178479Sjb 395178479Sjb#if defined(sun) 396178479Sjb (void) Plookup_by_addr(pp->dpp_pr, sym.st_value, pp->dpp_func, 397178479Sjb#else 398178479Sjb (void) proc_addr2sym(pp->dpp_pr, sym.st_value, pp->dpp_func, 399178479Sjb#endif 400178479Sjb DTRACE_FUNCNAMELEN, &sym); 401178479Sjb 402178479Sjb return (dt_pid_per_sym(pp, &sym, pp->dpp_func)); 403178479Sjb } else { 404178479Sjb#ifdef DOODAD 405178479Sjb uint_t nmatches = pp->dpp_nmatches; 406178479Sjb 407178479Sjb if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_SYMTAB, 408178479Sjb BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1) 409178479Sjb return (1); 410178479Sjb 411178479Sjb if (nmatches == pp->dpp_nmatches) { 412178479Sjb /* 413178479Sjb * If we didn't match anything in the PR_SYMTAB, try 414178479Sjb * the PR_DYNSYM. 415178479Sjb */ 416178479Sjb if (Psymbol_iter_by_addr(pp->dpp_pr, obj, PR_DYNSYM, 417178479Sjb BIND_ANY | TYPE_FUNC, dt_pid_sym_filt, pp) == 1) 418178479Sjb return (1); 419178479Sjb } 420178479Sjb#endif 421178479Sjb } 422178479Sjb 423178479Sjb return (0); 424178479Sjb} 425178479Sjb 426178479Sjbstatic int 427178479Sjbdt_pid_mod_filt(void *arg, const prmap_t *pmp, const char *obj) 428178479Sjb{ 429178479Sjb char name[DTRACE_MODNAMELEN]; 430178479Sjb dt_pid_probe_t *pp = arg; 431178479Sjb 432178479Sjb if (gmatch(obj, pp->dpp_mod)) 433178479Sjb return (dt_pid_per_mod(pp, pmp, obj)); 434178479Sjb 435178479Sjb#if defined(sun) 436178479Sjb (void) Plmid(pp->dpp_pr, pmp->pr_vaddr, &pp->dpp_lmid); 437178479Sjb#else 438178479Sjb pp->dpp_lmid = 0; 439178479Sjb#endif 440178479Sjb 441178479Sjb if ((pp->dpp_obj = strrchr(obj, '/')) == NULL) 442178479Sjb pp->dpp_obj = obj; 443178479Sjb else 444178479Sjb pp->dpp_obj++; 445178479Sjb 446178479Sjb dt_pid_objname(name, sizeof (name), pp->dpp_lmid, obj); 447178479Sjb 448178479Sjb if (gmatch(name, pp->dpp_mod)) 449178479Sjb return (dt_pid_per_mod(pp, pmp, obj)); 450178479Sjb 451178479Sjb return (0); 452178479Sjb} 453178479Sjb 454178479Sjbstatic const prmap_t * 455178479Sjbdt_pid_fix_mod(dtrace_probedesc_t *pdp, struct ps_prochandle *P) 456178479Sjb{ 457178479Sjb#ifdef DOODAD 458178479Sjb char m[MAXPATHLEN]; 459178479Sjb Lmid_t lmid = PR_LMID_EVERY; 460178479Sjb const char *obj; 461178479Sjb#endif 462178479Sjb const prmap_t *pmp; 463178479Sjb 464178479Sjb#ifdef DOODAD 465178479Sjb /* 466178479Sjb * Pick apart the link map from the library name. 467178479Sjb */ 468178479Sjb if (strchr(pdp->dtpd_mod, '`') != NULL) { 469178479Sjb char *end; 470178479Sjb 471178479Sjb if (strncmp(pdp->dtpd_mod, "LM", 2) != 0 || 472178479Sjb !isdigit(pdp->dtpd_mod[2])) 473178479Sjb return (NULL); 474178479Sjb 475178479Sjb lmid = strtoul(&pdp->dtpd_mod[2], &end, 16); 476178479Sjb 477178479Sjb obj = end + 1; 478178479Sjb 479178479Sjb if (*end != '`' || strchr(obj, '`') != NULL) 480178479Sjb return (NULL); 481178479Sjb 482178479Sjb } else { 483178479Sjb obj = pdp->dtpd_mod; 484178479Sjb } 485178479Sjb 486178479Sjb if ((pmp = Plmid_to_map(P, lmid, obj)) == NULL) 487178479Sjb return (NULL); 488178479Sjb 489178479Sjb (void) Pobjname(P, pmp->pr_vaddr, m, sizeof (m)); 490178479Sjb if ((obj = strrchr(m, '/')) == NULL) 491178479Sjb obj = &m[0]; 492178479Sjb else 493178479Sjb obj++; 494178479Sjb 495178479Sjb (void) Plmid(P, pmp->pr_vaddr, &lmid); 496178479Sjb 497178479Sjb dt_pid_objname(pdp->dtpd_mod, sizeof (pdp->dtpd_mod), lmid, obj); 498178479Sjb#else 499178479Sjbpmp = NULL; 500178479Sjb#endif 501178479Sjb 502178479Sjb return (pmp); 503178479Sjb} 504178479Sjb 505178479Sjb 506178479Sjbstatic int 507178479Sjbdt_pid_create_pid_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, 508178479Sjb dt_pcb_t *pcb, dt_proc_t *dpr) 509178479Sjb{ 510178479Sjb dt_pid_probe_t pp; 511178479Sjb int ret = 0; 512178479Sjb 513178479Sjb pp.dpp_dtp = dtp; 514178479Sjb pp.dpp_dpr = dpr; 515178479Sjb pp.dpp_pr = dpr->dpr_proc; 516178479Sjb pp.dpp_pcb = pcb; 517178479Sjb 518178479Sjb#ifdef DOODAD 519178479Sjb /* 520178479Sjb * We can only trace dynamically-linked executables (since we've 521178479Sjb * hidden some magic in ld.so.1 as well as libc.so.1). 522178479Sjb */ 523178479Sjb if (Pname_to_map(pp.dpp_pr, PR_OBJ_LDSO) == NULL) { 524178479Sjb return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_DYN, 525178479Sjb "process %s is not a dynamically-linked executable", 526178479Sjb &pdp->dtpd_provider[3])); 527178479Sjb } 528178479Sjb#endif 529178479Sjb 530178479Sjb pp.dpp_mod = pdp->dtpd_mod[0] != '\0' ? pdp->dtpd_mod : "*"; 531178479Sjb pp.dpp_func = pdp->dtpd_func[0] != '\0' ? pdp->dtpd_func : "*"; 532178479Sjb pp.dpp_name = pdp->dtpd_name[0] != '\0' ? pdp->dtpd_name : "*"; 533178479Sjb pp.dpp_last_taken = 0; 534178479Sjb 535178479Sjb if (strcmp(pp.dpp_func, "-") == 0) { 536178479Sjb const prmap_t *aout, *pmp; 537178479Sjb 538178479Sjb if (pdp->dtpd_mod[0] == '\0') { 539178479Sjb pp.dpp_mod = pdp->dtpd_mod; 540178479Sjb (void) strcpy(pdp->dtpd_mod, "a.out"); 541178479Sjb } else if (strisglob(pp.dpp_mod) || 542178479Sjb#if defined(sun) 543178479Sjb (aout = Pname_to_map(pp.dpp_pr, "a.out")) == NULL || 544178479Sjb (pmp = Pname_to_map(pp.dpp_pr, pp.dpp_mod)) == NULL || 545178479Sjb#else 546178479Sjb (aout = proc_name2map(pp.dpp_pr, "a.out")) == NULL || 547178479Sjb (pmp = proc_name2map(pp.dpp_pr, pp.dpp_mod)) == NULL || 548178479Sjb#endif 549178479Sjb aout->pr_vaddr != pmp->pr_vaddr) { 550178479Sjb return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_LIB, 551178479Sjb "only the a.out module is valid with the " 552178479Sjb "'-' function")); 553178479Sjb } 554178479Sjb 555178479Sjb if (strisglob(pp.dpp_name)) { 556178479Sjb return (dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_NAME, 557178479Sjb "only individual addresses may be specified " 558178479Sjb "with the '-' function")); 559178479Sjb } 560178479Sjb } 561178479Sjb 562178479Sjb /* 563178479Sjb * If pp.dpp_mod contains any globbing meta-characters, we need 564178479Sjb * to iterate over each module and compare its name against the 565178479Sjb * pattern. An empty module name is treated as '*'. 566178479Sjb */ 567178479Sjb#ifdef DOODAD 568178479Sjb if (strisglob(pp.dpp_mod)) { 569178479Sjb ret = Pobject_iter(pp.dpp_pr, dt_pid_mod_filt, &pp); 570178479Sjb } else { 571178479Sjb const prmap_t *pmp; 572178479Sjb char *obj; 573178479Sjb 574178479Sjb /* 575178479Sjb * If we can't find a matching module, don't sweat it -- either 576178479Sjb * we'll fail the enabling because the probes don't exist or 577178479Sjb * we'll wait for that module to come along. 578178479Sjb */ 579178479Sjb if ((pmp = dt_pid_fix_mod(pdp, pp.dpp_pr)) != NULL) { 580178479Sjb if ((obj = strchr(pdp->dtpd_mod, '`')) == NULL) 581178479Sjb obj = pdp->dtpd_mod; 582178479Sjb else 583178479Sjb obj++; 584178479Sjb 585178479Sjb ret = dt_pid_per_mod(&pp, pmp, obj); 586178479Sjb } 587178479Sjb } 588178479Sjb#endif 589178479Sjb 590178479Sjb return (ret); 591178479Sjb} 592178479Sjb 593178479Sjbstatic int 594178479Sjbdt_pid_usdt_mapping(void *data, const prmap_t *pmp, const char *oname) 595178479Sjb{ 596178479Sjb struct ps_prochandle *P = data; 597178479Sjb GElf_Sym sym; 598178479Sjb#if defined(sun) 599178479Sjb prsyminfo_t sip; 600178479Sjb#endif 601178479Sjb dof_helper_t dh; 602178479Sjb GElf_Half e_type; 603178479Sjb const char *mname; 604178479Sjb const char *syms[] = { "___SUNW_dof", "__SUNW_dof" }; 605178479Sjb int i, fd = -1; 606178479Sjb 607178479Sjb /* 608178479Sjb * The symbol ___SUNW_dof is for lazy-loaded DOF sections, and 609178479Sjb * __SUNW_dof is for actively-loaded DOF sections. We try to force 610178479Sjb * in both types of DOF section since the process may not yet have 611178479Sjb * run the code to instantiate these providers. 612178479Sjb */ 613178479Sjb for (i = 0; i < 2; i++) { 614178479Sjb#if defined(sun) 615178479Sjb if (Pxlookup_by_name(P, PR_LMID_EVERY, oname, syms[i], &sym, 616178479Sjb &sip) != 0) { 617178479Sjb#else 618178479Sjb if (proc_name2sym(P, oname, syms[i], &sym) != 0) { 619178479Sjb#endif 620178479Sjb continue; 621178479Sjb } 622178479Sjb 623178479Sjb if ((mname = strrchr(oname, '/')) == NULL) 624178479Sjb mname = oname; 625178479Sjb else 626178479Sjb mname++; 627178479Sjb 628178479Sjb dt_dprintf("lookup of %s succeeded for %s\n", syms[i], mname); 629178479Sjb 630178479Sjb#ifdef DOODAD 631178479Sjb if (Pread(P, &e_type, sizeof (e_type), pmp->pr_vaddr + 632178479Sjb offsetof(Elf64_Ehdr, e_type)) != sizeof (e_type)) { 633178479Sjb dt_dprintf("read of ELF header failed"); 634178479Sjb continue; 635178479Sjb } 636178479Sjb#endif 637178479Sjb 638178479Sjb dh.dofhp_dof = sym.st_value; 639178479Sjb dh.dofhp_addr = (e_type == ET_EXEC) ? 0 : pmp->pr_vaddr; 640178479Sjb 641178479Sjb dt_pid_objname(dh.dofhp_mod, sizeof (dh.dofhp_mod), 642178479Sjb#if defined(sun) 643178479Sjb sip.prs_lmid, mname); 644178479Sjb#else 645178479Sjb 0, mname); 646178479Sjb#endif 647178479Sjb 648178479Sjb#ifdef DOODAD 649178479Sjb if (fd == -1 && 650178479Sjb (fd = pr_open(P, "/dev/dtrace/helper", O_RDWR, 0)) < 0) { 651178479Sjb dt_dprintf("pr_open of helper device failed: %s\n", 652178479Sjb strerror(errno)); 653178479Sjb return (-1); /* errno is set for us */ 654178479Sjb } 655178479Sjb 656178479Sjb if (pr_ioctl(P, fd, DTRACEHIOC_ADDDOF, &dh, sizeof (dh)) < 0) 657178479Sjb dt_dprintf("DOF was rejected for %s\n", dh.dofhp_mod); 658178479Sjb#endif 659178479Sjb } 660178479Sjb 661178479Sjb#ifdef DOODAD 662178479Sjb if (fd != -1) 663178479Sjb (void) pr_close(P, fd); 664178479Sjb#endif 665178479Sjb 666178479Sjb return (0); 667178479Sjb} 668178479Sjb 669178479Sjbstatic int 670178479Sjbdt_pid_create_usdt_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, 671178479Sjb dt_pcb_t *pcb, dt_proc_t *dpr) 672178479Sjb{ 673178479Sjb struct ps_prochandle *P = dpr->dpr_proc; 674178479Sjb int ret = 0; 675178479Sjb 676178479Sjb assert(DT_MUTEX_HELD(&dpr->dpr_lock)); 677178479Sjb 678178479Sjb#ifdef DOODAD 679178479Sjb (void) Pupdate_maps(P); 680178479Sjb if (Pobject_iter(P, dt_pid_usdt_mapping, P) != 0) { 681178479Sjb ret = -1; 682178479Sjb (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_USDT, 683178479Sjb "failed to instantiate probes for pid %d: %s", 684178479Sjb#if defined(sun) 685178479Sjb (int)Pstatus(P)->pr_pid, strerror(errno)); 686178479Sjb#else 687178479Sjb (int)proc_getpid(P), strerror(errno)); 688178479Sjb#endif 689178479Sjb } 690178479Sjb#endif 691178479Sjb 692178479Sjb /* 693178479Sjb * Put the module name in its canonical form. 694178479Sjb */ 695178479Sjb (void) dt_pid_fix_mod(pdp, P); 696178479Sjb 697178479Sjb return (ret); 698178479Sjb} 699178479Sjb 700178479Sjbstatic pid_t 701178479Sjbdt_pid_get_pid(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb, 702178479Sjb dt_proc_t *dpr) 703178479Sjb{ 704178479Sjb pid_t pid; 705178479Sjb char *c, *last = NULL, *end; 706178479Sjb 707178479Sjb for (c = &pdp->dtpd_provider[0]; *c != '\0'; c++) { 708178479Sjb if (!isdigit(*c)) 709178479Sjb last = c; 710178479Sjb } 711178479Sjb 712178479Sjb if (last == NULL || (*(++last) == '\0')) { 713178479Sjb (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPROV, 714178479Sjb "'%s' is not a valid provider", pdp->dtpd_provider); 715178479Sjb return (-1); 716178479Sjb } 717178479Sjb 718178479Sjb errno = 0; 719178479Sjb pid = strtol(last, &end, 10); 720178479Sjb 721178479Sjb if (errno != 0 || end == last || end[0] != '\0' || pid <= 0) { 722178479Sjb (void) dt_pid_error(dtp, pcb, dpr, NULL, D_PROC_BADPID, 723178479Sjb "'%s' does not contain a valid pid", pdp->dtpd_provider); 724178479Sjb return (-1); 725178479Sjb } 726178479Sjb 727178479Sjb return (pid); 728178479Sjb} 729178479Sjb 730178479Sjbint 731178479Sjbdt_pid_create_probes(dtrace_probedesc_t *pdp, dtrace_hdl_t *dtp, dt_pcb_t *pcb) 732178479Sjb{ 733178479Sjb char provname[DTRACE_PROVNAMELEN]; 734178479Sjb struct ps_prochandle *P; 735178479Sjb dt_proc_t *dpr; 736178479Sjb pid_t pid; 737178479Sjb int err = 0; 738178479Sjb 739178479Sjb assert(pcb != NULL); 740178479Sjb 741178479Sjb if ((pid = dt_pid_get_pid(pdp, dtp, pcb, NULL)) == -1) 742178479Sjb return (-1); 743178479Sjb 744178479Sjb if (dtp->dt_ftfd == -1) { 745178479Sjb if (dtp->dt_fterr == ENOENT) { 746178479Sjb (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV, 747178479Sjb "pid provider is not installed on this system"); 748178479Sjb } else { 749178479Sjb (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_NODEV, 750178479Sjb "pid provider is not available: %s", 751178479Sjb strerror(dtp->dt_fterr)); 752178479Sjb } 753178479Sjb 754178479Sjb return (-1); 755178479Sjb } 756178479Sjb 757178479Sjb (void) snprintf(provname, sizeof (provname), "pid%d", (int)pid); 758178479Sjb 759178479Sjb if (gmatch(provname, pdp->dtpd_provider) != 0) { 760178479Sjb if ((P = dt_proc_grab(dtp, pid, PGRAB_RDONLY | PGRAB_FORCE, 761178479Sjb 0)) == NULL) { 762178479Sjb (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB, 763178479Sjb "failed to grab process %d", (int)pid); 764178479Sjb return (-1); 765178479Sjb } 766178479Sjb 767178479Sjb dpr = dt_proc_lookup(dtp, P, 0); 768178479Sjb assert(dpr != NULL); 769178479Sjb (void) pthread_mutex_lock(&dpr->dpr_lock); 770178479Sjb 771178479Sjb if ((err = dt_pid_create_pid_probes(pdp, dtp, pcb, dpr)) == 0) { 772178479Sjb /* 773178479Sjb * Alert other retained enablings which may match 774178479Sjb * against the newly created probes. 775178479Sjb */ 776178479Sjb (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, NULL); 777178479Sjb } 778178479Sjb 779178479Sjb (void) pthread_mutex_unlock(&dpr->dpr_lock); 780178479Sjb dt_proc_release(dtp, P); 781178479Sjb } 782178479Sjb 783178479Sjb /* 784178479Sjb * If it's not strictly a pid provider, we might match a USDT provider. 785178479Sjb */ 786178479Sjb if (strcmp(provname, pdp->dtpd_provider) != 0) { 787178479Sjb if ((P = dt_proc_grab(dtp, pid, 0, 1)) == NULL) { 788178479Sjb (void) dt_pid_error(dtp, pcb, NULL, NULL, D_PROC_GRAB, 789178479Sjb "failed to grab process %d", (int)pid); 790178479Sjb return (-1); 791178479Sjb } 792178479Sjb 793178479Sjb dpr = dt_proc_lookup(dtp, P, 0); 794178479Sjb assert(dpr != NULL); 795178479Sjb (void) pthread_mutex_lock(&dpr->dpr_lock); 796178479Sjb 797178479Sjb if (!dpr->dpr_usdt) { 798178479Sjb err = dt_pid_create_usdt_probes(pdp, dtp, pcb, dpr); 799178479Sjb dpr->dpr_usdt = B_TRUE; 800178479Sjb } 801178479Sjb 802178479Sjb (void) pthread_mutex_unlock(&dpr->dpr_lock); 803178479Sjb dt_proc_release(dtp, P); 804178479Sjb } 805178479Sjb 806178479Sjb return (err ? -1 : 0); 807178479Sjb} 808178479Sjb 809178479Sjbint 810178479Sjbdt_pid_create_probes_module(dtrace_hdl_t *dtp, dt_proc_t *dpr) 811178479Sjb{ 812178479Sjb dtrace_enable_io_t args; 813178479Sjb dtrace_prog_t *pgp; 814178479Sjb dt_stmt_t *stp; 815178479Sjb dtrace_probedesc_t *pdp, pd; 816178479Sjb pid_t pid; 817178479Sjb int ret = 0, found = B_FALSE; 818178479Sjb char provname[DTRACE_PROVNAMELEN]; 819178479Sjb 820178479Sjb (void) snprintf(provname, sizeof (provname), "pid%d", 821178479Sjb (int)dpr->dpr_pid); 822178479Sjb 823178479Sjb for (pgp = dt_list_next(&dtp->dt_programs); pgp != NULL; 824178479Sjb pgp = dt_list_next(pgp)) { 825178479Sjb 826178479Sjb for (stp = dt_list_next(&pgp->dp_stmts); stp != NULL; 827178479Sjb stp = dt_list_next(stp)) { 828178479Sjb 829178479Sjb pdp = &stp->ds_desc->dtsd_ecbdesc->dted_probe; 830178479Sjb pid = dt_pid_get_pid(pdp, dtp, NULL, dpr); 831178479Sjb if (pid != dpr->dpr_pid) 832178479Sjb continue; 833178479Sjb 834178479Sjb found = B_TRUE; 835178479Sjb 836178479Sjb pd = *pdp; 837178479Sjb 838178479Sjb if (gmatch(provname, pdp->dtpd_provider) != 0 && 839178479Sjb dt_pid_create_pid_probes(&pd, dtp, NULL, dpr) != 0) 840178479Sjb ret = 1; 841178479Sjb 842178479Sjb /* 843178479Sjb * If it's not strictly a pid provider, we might match 844178479Sjb * a USDT provider. 845178479Sjb */ 846178479Sjb if (strcmp(provname, pdp->dtpd_provider) != 0 && 847178479Sjb dt_pid_create_usdt_probes(&pd, dtp, NULL, dpr) != 0) 848178479Sjb ret = 1; 849178479Sjb } 850178479Sjb } 851178479Sjb 852178479Sjb if (found) { 853178479Sjb /* 854178479Sjb * Give DTrace a shot to the ribs to get it to check 855178479Sjb * out the newly created probes. 856178479Sjb */ 857178479Sjb args.dof = NULL; 858178479Sjb args.n_matched = 0; 859178479Sjb (void) dt_ioctl(dtp, DTRACEIOC_ENABLE, &args); 860178479Sjb } 861178479Sjb 862178479Sjb return (ret); 863178479Sjb} 864