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