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