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