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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 /* 26 * Copyright (c) 2013, Joyent, Inc. All rights reserved. 27 */ 28 29 #include <sys/types.h> 30 #ifdef illumos 31 #include <sys/modctl.h> 32 #include <sys/kobj.h> 33 #include <sys/kobj_impl.h> 34 #include <sys/sysmacros.h> 35 #include <sys/elf.h> 36 #include <sys/task.h> 37 #else 38 #include <sys/param.h> 39 #include <sys/linker.h> 40 #include <sys/module.h> 41 #include <sys/stat.h> 42 #endif 43 #ifdef __NetBSD__ 44 #include <sys/sysctl.h> 45 #endif 46 47 #include <unistd.h> 48 #ifdef illumos 49 #include <project.h> 50 #endif 51 #include <strings.h> 52 #include <stdlib.h> 53 #include <libelf.h> 54 #include <limits.h> 55 #include <assert.h> 56 #include <errno.h> 57 #include <dirent.h> 58 #ifndef illumos 59 #include <fcntl.h> 60 #include <libproc_compat.h> 61 #endif 62 63 #include <dt_strtab.h> 64 #include <dt_module.h> 65 #include <dt_impl.h> 66 67 static const char *dt_module_strtab; /* active strtab for qsort callbacks */ 68 69 static void 70 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id) 71 { 72 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree]; 73 uint_t h; 74 75 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1); 76 77 dsp->ds_symid = id; 78 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets; 79 dsp->ds_next = dmp->dm_symbuckets[h]; 80 dmp->dm_symbuckets[h] = dmp->dm_symfree++; 81 } 82 83 static uint_t 84 dt_module_syminit32(dt_module_t *dmp) 85 { 86 #if STT_NUM != (STT_TLS + 1) 87 #error "STT_NUM has grown. update dt_module_syminit32()" 88 #endif 89 90 Elf32_Sym *sym = dmp->dm_symtab.cts_data; 91 const char *base = dmp->dm_strtab.cts_data; 92 size_t ss_size = dmp->dm_strtab.cts_size; 93 uint_t i, n = dmp->dm_nsymelems; 94 uint_t asrsv = 0; 95 96 #if defined(__FreeBSD__) || defined(__NetBSD__) 97 GElf_Ehdr ehdr; 98 int is_elf_obj; 99 100 gelf_getehdr(dmp->dm_elf, &ehdr); 101 is_elf_obj = (ehdr.e_type == ET_REL); 102 #endif 103 104 for (i = 0; i < n; i++, sym++) { 105 const char *name = base + sym->st_name; 106 uchar_t type = ELF32_ST_TYPE(sym->st_info); 107 108 if (type >= STT_NUM || type == STT_SECTION) 109 continue; /* skip sections and unknown types */ 110 111 if (sym->st_name == 0 || sym->st_name >= ss_size) 112 continue; /* skip null or invalid names */ 113 114 if (sym->st_value != 0 && 115 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) { 116 asrsv++; /* reserve space in the address map */ 117 118 #if defined(__FreeBSD__) || defined(__NetBSD__) 119 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset; 120 if (is_elf_obj && sym->st_shndx != SHN_UNDEF && 121 sym->st_shndx < ehdr.e_shnum) 122 sym->st_value += 123 dmp->dm_sec_offsets[sym->st_shndx]; 124 #endif 125 } 126 127 dt_module_symhash_insert(dmp, name, i); 128 } 129 130 return (asrsv); 131 } 132 133 static uint_t 134 dt_module_syminit64(dt_module_t *dmp) 135 { 136 #if STT_NUM != (STT_TLS + 1) 137 #error "STT_NUM has grown. update dt_module_syminit64()" 138 #endif 139 140 Elf64_Sym *sym = dmp->dm_symtab.cts_data; 141 const char *base = dmp->dm_strtab.cts_data; 142 size_t ss_size = dmp->dm_strtab.cts_size; 143 uint_t i, n = dmp->dm_nsymelems; 144 uint_t asrsv = 0; 145 146 #if defined(__FreeBSD__) || defined(__NetBSD__) 147 GElf_Ehdr ehdr; 148 int is_elf_obj; 149 150 gelf_getehdr(dmp->dm_elf, &ehdr); 151 is_elf_obj = (ehdr.e_type == ET_REL); 152 #endif 153 154 for (i = 0; i < n; i++, sym++) { 155 const char *name = base + sym->st_name; 156 uchar_t type = ELF64_ST_TYPE(sym->st_info); 157 158 if (type >= STT_NUM || type == STT_SECTION) 159 continue; /* skip sections and unknown types */ 160 161 if (sym->st_name == 0 || sym->st_name >= ss_size) 162 continue; /* skip null or invalid names */ 163 164 if (sym->st_value != 0 && 165 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) { 166 asrsv++; /* reserve space in the address map */ 167 #if defined(__FreeBSD__) || defined(__NetBSD__) 168 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset; 169 if (is_elf_obj && sym->st_shndx != SHN_UNDEF && 170 sym->st_shndx < ehdr.e_shnum) 171 sym->st_value += 172 dmp->dm_sec_offsets[sym->st_shndx]; 173 #endif 174 } 175 176 dt_module_symhash_insert(dmp, name, i); 177 } 178 179 return (asrsv); 180 } 181 182 /* 183 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort 184 * symbols by value. If values are equal, we prefer the symbol that is 185 * non-zero sized, typed, not weak, or lexically first, in that order. 186 */ 187 static int 188 dt_module_symcomp32(const void *lp, const void *rp) 189 { 190 Elf32_Sym *lhs = *((Elf32_Sym **)lp); 191 Elf32_Sym *rhs = *((Elf32_Sym **)rp); 192 193 if (lhs->st_value != rhs->st_value) 194 return (lhs->st_value > rhs->st_value ? 1 : -1); 195 196 if ((lhs->st_size == 0) != (rhs->st_size == 0)) 197 return (lhs->st_size == 0 ? 1 : -1); 198 199 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) != 200 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE)) 201 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1); 202 203 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) != 204 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK)) 205 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1); 206 207 return (strcmp(dt_module_strtab + lhs->st_name, 208 dt_module_strtab + rhs->st_name)); 209 } 210 211 /* 212 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort 213 * symbols by value. If values are equal, we prefer the symbol that is 214 * non-zero sized, typed, not weak, or lexically first, in that order. 215 */ 216 static int 217 dt_module_symcomp64(const void *lp, const void *rp) 218 { 219 Elf64_Sym *lhs = *((Elf64_Sym **)lp); 220 Elf64_Sym *rhs = *((Elf64_Sym **)rp); 221 222 if (lhs->st_value != rhs->st_value) 223 return (lhs->st_value > rhs->st_value ? 1 : -1); 224 225 if ((lhs->st_size == 0) != (rhs->st_size == 0)) 226 return (lhs->st_size == 0 ? 1 : -1); 227 228 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) != 229 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE)) 230 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1); 231 232 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) != 233 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK)) 234 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1); 235 236 return (strcmp(dt_module_strtab + lhs->st_name, 237 dt_module_strtab + rhs->st_name)); 238 } 239 240 static void 241 dt_module_symsort32(dt_module_t *dmp) 242 { 243 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data; 244 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap; 245 const dt_sym_t *dsp = dmp->dm_symchains + 1; 246 uint_t i, n = dmp->dm_symfree; 247 248 for (i = 1; i < n; i++, dsp++) { 249 Elf32_Sym *sym = symtab + dsp->ds_symid; 250 if (sym->st_value != 0 && 251 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) 252 *sympp++ = sym; 253 } 254 255 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap); 256 assert(dmp->dm_aslen <= dmp->dm_asrsv); 257 258 dt_module_strtab = dmp->dm_strtab.cts_data; 259 qsort(dmp->dm_asmap, dmp->dm_aslen, 260 sizeof (Elf32_Sym *), dt_module_symcomp32); 261 dt_module_strtab = NULL; 262 } 263 264 static void 265 dt_module_symsort64(dt_module_t *dmp) 266 { 267 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data; 268 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap; 269 const dt_sym_t *dsp = dmp->dm_symchains + 1; 270 uint_t i, n = dmp->dm_symfree; 271 272 for (i = 1; i < n; i++, dsp++) { 273 Elf64_Sym *sym = symtab + dsp->ds_symid; 274 if (sym->st_value != 0 && 275 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) 276 *sympp++ = sym; 277 } 278 279 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap); 280 assert(dmp->dm_aslen <= dmp->dm_asrsv); 281 282 dt_module_strtab = dmp->dm_strtab.cts_data; 283 qsort(dmp->dm_asmap, dmp->dm_aslen, 284 sizeof (Elf64_Sym *), dt_module_symcomp64); 285 dt_module_strtab = NULL; 286 } 287 288 static GElf_Sym * 289 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst) 290 { 291 if (dst != NULL) { 292 dst->st_name = src->st_name; 293 dst->st_info = src->st_info; 294 dst->st_other = src->st_other; 295 dst->st_shndx = src->st_shndx; 296 dst->st_value = src->st_value; 297 dst->st_size = src->st_size; 298 } 299 300 return (dst); 301 } 302 303 static GElf_Sym * 304 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst) 305 { 306 if (dst != NULL) 307 bcopy(src, dst, sizeof (GElf_Sym)); 308 309 return (dst); 310 } 311 312 static GElf_Sym * 313 dt_module_symname32(dt_module_t *dmp, const char *name, 314 GElf_Sym *symp, uint_t *idp) 315 { 316 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data; 317 const char *strtab = dmp->dm_strtab.cts_data; 318 319 const Elf32_Sym *sym; 320 const dt_sym_t *dsp; 321 uint_t i, h; 322 323 if (dmp->dm_nsymelems == 0) 324 return (NULL); 325 326 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets; 327 328 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) { 329 dsp = &dmp->dm_symchains[i]; 330 sym = symtab + dsp->ds_symid; 331 332 if (strcmp(name, strtab + sym->st_name) == 0) { 333 if (idp != NULL) 334 *idp = dsp->ds_symid; 335 return (dt_module_symgelf32(sym, symp)); 336 } 337 } 338 339 return (NULL); 340 } 341 342 static GElf_Sym * 343 dt_module_symname64(dt_module_t *dmp, const char *name, 344 GElf_Sym *symp, uint_t *idp) 345 { 346 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data; 347 const char *strtab = dmp->dm_strtab.cts_data; 348 349 const Elf64_Sym *sym; 350 const dt_sym_t *dsp; 351 uint_t i, h; 352 353 if (dmp->dm_nsymelems == 0) 354 return (NULL); 355 356 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets; 357 358 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) { 359 dsp = &dmp->dm_symchains[i]; 360 sym = symtab + dsp->ds_symid; 361 362 if (strcmp(name, strtab + sym->st_name) == 0) { 363 if (idp != NULL) 364 *idp = dsp->ds_symid; 365 return (dt_module_symgelf64(sym, symp)); 366 } 367 } 368 369 return (NULL); 370 } 371 372 static GElf_Sym * 373 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr, 374 GElf_Sym *symp, uint_t *idp) 375 { 376 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap; 377 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data; 378 const Elf32_Sym *sym; 379 380 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1; 381 Elf32_Addr v; 382 383 if (dmp->dm_aslen == 0) 384 return (NULL); 385 386 while (hi - lo > 1) { 387 mid = (lo + hi) / 2; 388 if (addr >= asmap[mid]->st_value) 389 lo = mid; 390 else 391 hi = mid; 392 } 393 394 i = addr < asmap[hi]->st_value ? lo : hi; 395 sym = asmap[i]; 396 v = sym->st_value; 397 398 /* 399 * If the previous entry has the same value, improve our choice. The 400 * order of equal-valued symbols is determined by the comparison func. 401 */ 402 while (i-- != 0 && asmap[i]->st_value == v) 403 sym = asmap[i]; 404 405 if (addr - sym->st_value < MAX(sym->st_size, 1)) { 406 if (idp != NULL) 407 *idp = (uint_t)(sym - symtab); 408 return (dt_module_symgelf32(sym, symp)); 409 } 410 411 return (NULL); 412 } 413 414 static GElf_Sym * 415 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr, 416 GElf_Sym *symp, uint_t *idp) 417 { 418 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap; 419 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data; 420 const Elf64_Sym *sym; 421 422 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1; 423 Elf64_Addr v; 424 425 if (dmp->dm_aslen == 0) 426 return (NULL); 427 428 while (hi - lo > 1) { 429 mid = (lo + hi) / 2; 430 if (addr >= asmap[mid]->st_value) 431 lo = mid; 432 else 433 hi = mid; 434 } 435 436 i = addr < asmap[hi]->st_value ? lo : hi; 437 sym = asmap[i]; 438 v = sym->st_value; 439 440 /* 441 * If the previous entry has the same value, improve our choice. The 442 * order of equal-valued symbols is determined by the comparison func. 443 */ 444 while (i-- != 0 && asmap[i]->st_value == v) 445 sym = asmap[i]; 446 447 if (addr - sym->st_value < MAX(sym->st_size, 1)) { 448 if (idp != NULL) 449 *idp = (uint_t)(sym - symtab); 450 return (dt_module_symgelf64(sym, symp)); 451 } 452 453 return (NULL); 454 } 455 456 static const dt_modops_t dt_modops_32 = { 457 dt_module_syminit32, 458 dt_module_symsort32, 459 dt_module_symname32, 460 dt_module_symaddr32 461 }; 462 463 static const dt_modops_t dt_modops_64 = { 464 dt_module_syminit64, 465 dt_module_symsort64, 466 dt_module_symname64, 467 dt_module_symaddr64 468 }; 469 470 dt_module_t * 471 dt_module_create(dtrace_hdl_t *dtp, const char *name) 472 { 473 long pid; 474 char *eptr; 475 dt_ident_t *idp; 476 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets; 477 dt_module_t *dmp; 478 479 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) { 480 if (strcmp(dmp->dm_name, name) == 0) 481 return (dmp); 482 } 483 484 if ((dmp = malloc(sizeof (dt_module_t))) == NULL) 485 return (NULL); /* caller must handle allocation failure */ 486 487 bzero(dmp, sizeof (dt_module_t)); 488 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name)); 489 dt_list_append(&dtp->dt_modlist, dmp); 490 dmp->dm_next = dtp->dt_mods[h]; 491 dtp->dt_mods[h] = dmp; 492 dtp->dt_nmods++; 493 494 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64) 495 dmp->dm_ops = &dt_modops_64; 496 else 497 dmp->dm_ops = &dt_modops_32; 498 499 /* 500 * Modules for userland processes are special. They always refer to a 501 * specific process and have a copy of their CTF data from a specific 502 * instant in time. Any dt_module_t that begins with 'pid' is a module 503 * for a specific process, much like how any probe description that 504 * begins with 'pid' is special. pid123 refers to process 123. A module 505 * that is just 'pid' refers specifically to pid$target. This is 506 * generally done as D does not currently allow for macros to be 507 * evaluated when working with types. 508 */ 509 if (strncmp(dmp->dm_name, "pid", 3) == 0) { 510 errno = 0; 511 if (dmp->dm_name[3] == '\0') { 512 idp = dt_idhash_lookup(dtp->dt_macros, "target"); 513 if (idp != NULL && idp->di_id != 0) 514 dmp->dm_pid = idp->di_id; 515 } else { 516 pid = strtol(dmp->dm_name + 3, &eptr, 10); 517 if (errno == 0 && *eptr == '\0') 518 dmp->dm_pid = (pid_t)pid; 519 else 520 dt_dprintf("encountered malformed pid " 521 "module: %s\n", dmp->dm_name); 522 } 523 } 524 525 return (dmp); 526 } 527 528 dt_module_t * 529 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name) 530 { 531 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets; 532 dt_module_t *dmp; 533 534 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) { 535 if (strcmp(dmp->dm_name, name) == 0) 536 return (dmp); 537 } 538 539 return (NULL); 540 } 541 542 /*ARGSUSED*/ 543 dt_module_t * 544 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp) 545 { 546 return (ctfp ? ctf_getspecific(ctfp) : NULL); 547 } 548 549 #if defined(__FreeBSD__) || defined(__NetBSD__) 550 dt_kmodule_t * 551 dt_kmodule_lookup(dtrace_hdl_t *dtp, const char *name) 552 { 553 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets; 554 dt_kmodule_t *dkmp; 555 556 for (dkmp = dtp->dt_kmods[h]; dkmp != NULL; dkmp = dkmp->dkm_next) { 557 if (strcmp(dkmp->dkm_name, name) == 0) 558 return (dkmp); 559 } 560 561 return (NULL); 562 } 563 #endif 564 565 static int 566 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp) 567 { 568 const char *s; 569 size_t shstrs; 570 GElf_Shdr sh; 571 Elf_Data *dp; 572 Elf_Scn *sp; 573 574 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) 575 return (dt_set_errno(dtp, EDT_NOTLOADED)); 576 577 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) { 578 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL || 579 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL) 580 continue; /* skip any malformed sections */ 581 582 if (sh.sh_type == ctsp->cts_type && 583 sh.sh_entsize == ctsp->cts_entsize && 584 strcmp(s, ctsp->cts_name) == 0) 585 break; /* section matches specification */ 586 } 587 588 /* 589 * If the section isn't found, return success but leave cts_data set 590 * to NULL and cts_size set to zero for our caller. 591 */ 592 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL) 593 return (0); 594 595 #ifdef illumos 596 ctsp->cts_data = dp->d_buf; 597 #else 598 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL) 599 return (0); 600 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size); 601 #endif 602 ctsp->cts_size = dp->d_size; 603 604 dt_dprintf("loaded %s [%s] (%lu bytes)\n", 605 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size); 606 607 return (0); 608 } 609 610 typedef struct dt_module_cb_arg { 611 struct ps_prochandle *dpa_proc; 612 dtrace_hdl_t *dpa_dtp; 613 dt_module_t *dpa_dmp; 614 uint_t dpa_count; 615 } dt_module_cb_arg_t; 616 617 /* ARGSUSED */ 618 static int 619 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj) 620 { 621 ctf_file_t *fp; 622 dt_module_cb_arg_t *dcp = arg; 623 624 /* Try to grab a ctf container if it exists */ 625 fp = Pname_to_ctf(dcp->dpa_proc, obj); 626 if (fp != NULL) 627 dcp->dpa_count++; 628 return (0); 629 } 630 631 /* ARGSUSED */ 632 static int 633 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj) 634 { 635 ctf_file_t *fp; 636 char buf[MAXPATHLEN], *p; 637 dt_module_cb_arg_t *dcp = arg; 638 int count = dcp->dpa_count; 639 Lmid_t lmid; 640 641 fp = Pname_to_ctf(dcp->dpa_proc, obj); 642 if (fp == NULL) 643 return (0); 644 fp = ctf_dup(fp); 645 if (fp == NULL) 646 return (0); 647 dcp->dpa_dmp->dm_libctfp[count] = fp; 648 /* 649 * While it'd be nice to simply use objname here, because of our prior 650 * actions we'll always get a resolved object name to its on disk file. 651 * Like the pid provider, we need to tell a bit of a lie here. The type 652 * that the user thinks of is in terms of the libraries they requested, 653 * eg. libc.so.1, they don't care about the fact that it's 654 * libc_hwcap.so.1. 655 */ 656 (void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf)); 657 if ((p = strrchr(buf, '/')) == NULL) 658 p = buf; 659 else 660 p++; 661 662 /* 663 * If for some reason we can't find a link map id for this module, which 664 * would be really quite weird. We instead just say the link map id is 665 * zero. 666 */ 667 if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0) 668 lmid = 0; 669 670 if (lmid == 0) 671 dcp->dpa_dmp->dm_libctfn[count] = strdup(p); 672 else 673 (void) asprintf(&dcp->dpa_dmp->dm_libctfn[count], 674 "LM%x`%s", lmid, p); 675 if (dcp->dpa_dmp->dm_libctfn[count] == NULL) 676 return (1); 677 ctf_setspecific(fp, dcp->dpa_dmp); 678 dcp->dpa_count++; 679 return (0); 680 } 681 682 /* 683 * We've been asked to load data that belongs to another process. As such we're 684 * going to pgrab it at this instant, load everything that we might ever care 685 * about, and then drive on. The reason for this is that the process that we're 686 * interested in might be changing. As long as we have grabbed it, then this 687 * can't be a problem for us. 688 * 689 * For now, we're actually going to punt on most things and just try to get CTF 690 * data, nothing else. Basically this is only useful as a source of type 691 * information, we can't go and do the stacktrace lookups, etc. 692 */ 693 static int 694 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp) 695 { 696 struct ps_prochandle *p; 697 dt_module_cb_arg_t arg; 698 699 /* 700 * Note that on success we do not release this hold. We must hold this 701 * for our life time. 702 */ 703 p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE); 704 if (p == NULL) { 705 dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid); 706 return (dt_set_errno(dtp, EDT_CANTLOAD)); 707 } 708 dt_proc_lock(dtp, p); 709 710 arg.dpa_proc = p; 711 arg.dpa_dtp = dtp; 712 arg.dpa_dmp = dmp; 713 arg.dpa_count = 0; 714 if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) { 715 dt_dprintf("failed to iterate objects\n"); 716 dt_proc_unlock(dtp, p); 717 dt_proc_release(dtp, p); 718 return (dt_set_errno(dtp, EDT_CANTLOAD)); 719 } 720 721 if (arg.dpa_count == 0) { 722 dt_dprintf("no ctf data present\n"); 723 dt_proc_unlock(dtp, p); 724 dt_proc_release(dtp, p); 725 return (dt_set_errno(dtp, EDT_CANTLOAD)); 726 } 727 728 dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count); 729 if (dmp->dm_libctfp == NULL) { 730 dt_proc_unlock(dtp, p); 731 dt_proc_release(dtp, p); 732 return (dt_set_errno(dtp, EDT_NOMEM)); 733 } 734 bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count); 735 736 dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count); 737 if (dmp->dm_libctfn == NULL) { 738 free(dmp->dm_libctfp); 739 dt_proc_unlock(dtp, p); 740 dt_proc_release(dtp, p); 741 return (dt_set_errno(dtp, EDT_NOMEM)); 742 } 743 bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count); 744 745 dmp->dm_nctflibs = arg.dpa_count; 746 747 arg.dpa_count = 0; 748 if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) { 749 dt_proc_unlock(dtp, p); 750 dt_module_unload(dtp, dmp); 751 dt_proc_release(dtp, p); 752 return (dt_set_errno(dtp, EDT_CANTLOAD)); 753 } 754 assert(arg.dpa_count == dmp->dm_nctflibs); 755 dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count, 756 (int)dmp->dm_pid); 757 758 dt_proc_unlock(dtp, p); 759 dt_proc_release(dtp, p); 760 dmp->dm_flags |= DT_DM_LOADED; 761 762 return (0); 763 } 764 765 int 766 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp) 767 { 768 if (dmp->dm_flags & DT_DM_LOADED) 769 return (0); /* module is already loaded */ 770 771 if (dmp->dm_pid != 0) 772 return (dt_module_load_proc(dtp, dmp)); 773 774 dmp->dm_ctdata.cts_name = ".SUNW_ctf"; 775 dmp->dm_ctdata.cts_type = SHT_PROGBITS; 776 dmp->dm_ctdata.cts_flags = 0; 777 dmp->dm_ctdata.cts_data = NULL; 778 dmp->dm_ctdata.cts_size = 0; 779 dmp->dm_ctdata.cts_entsize = 0; 780 dmp->dm_ctdata.cts_offset = 0; 781 782 dmp->dm_symtab.cts_name = ".symtab"; 783 dmp->dm_symtab.cts_type = SHT_SYMTAB; 784 dmp->dm_symtab.cts_flags = 0; 785 dmp->dm_symtab.cts_data = NULL; 786 dmp->dm_symtab.cts_size = 0; 787 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ? 788 sizeof (Elf64_Sym) : sizeof (Elf32_Sym); 789 dmp->dm_symtab.cts_offset = 0; 790 791 dmp->dm_strtab.cts_name = ".strtab"; 792 dmp->dm_strtab.cts_type = SHT_STRTAB; 793 dmp->dm_strtab.cts_flags = 0; 794 dmp->dm_strtab.cts_data = NULL; 795 dmp->dm_strtab.cts_size = 0; 796 dmp->dm_strtab.cts_entsize = 0; 797 dmp->dm_strtab.cts_offset = 0; 798 799 /* 800 * Attempt to load the module's CTF section, symbol table section, and 801 * string table section. Note that modules may not contain CTF data: 802 * this will result in a successful load_sect but data of size zero. 803 * We will then fail if dt_module_getctf() is called, as shown below. 804 */ 805 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 || 806 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 || 807 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) { 808 dt_module_unload(dtp, dmp); 809 return (-1); /* dt_errno is set for us */ 810 } 811 812 /* 813 * Allocate the hash chains and hash buckets for symbol name lookup. 814 * This is relatively simple since the symbol table is of fixed size 815 * and is known in advance. We allocate one extra element since we 816 * use element indices instead of pointers and zero is our sentinel. 817 */ 818 dmp->dm_nsymelems = 819 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize; 820 821 dmp->dm_nsymbuckets = _dtrace_strbuckets; 822 dmp->dm_symfree = 1; /* first free element is index 1 */ 823 824 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets); 825 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1); 826 827 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) { 828 dt_module_unload(dtp, dmp); 829 return (dt_set_errno(dtp, EDT_NOMEM)); 830 } 831 832 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets); 833 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1); 834 835 /* 836 * Iterate over the symbol table data buffer and insert each symbol 837 * name into the name hash if the name and type are valid. Then 838 * allocate the address map, fill it in, and sort it. 839 */ 840 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp); 841 842 dt_dprintf("hashed %s [%s] (%u symbols)\n", 843 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1); 844 845 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) { 846 dt_module_unload(dtp, dmp); 847 return (dt_set_errno(dtp, EDT_NOMEM)); 848 } 849 850 dmp->dm_ops->do_symsort(dmp); 851 852 dt_dprintf("sorted %s [%s] (%u symbols)\n", 853 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen); 854 855 dmp->dm_flags |= DT_DM_LOADED; 856 return (0); 857 } 858 859 int 860 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp) 861 { 862 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0) 863 return (1); 864 return (dt_module_getctf(dtp, dmp) != NULL); 865 } 866 867 ctf_file_t * 868 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp) 869 { 870 const char *parent; 871 dt_module_t *pmp; 872 ctf_file_t *pfp; 873 int model; 874 875 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0) 876 return (dmp->dm_ctfp); 877 878 if (dmp->dm_ops == &dt_modops_64) 879 model = CTF_MODEL_LP64; 880 else 881 model = CTF_MODEL_ILP32; 882 883 /* 884 * If the data model of the module does not match our program data 885 * model, then do not permit CTF from this module to be opened and 886 * returned to the compiler. If we support mixed data models in the 887 * future for combined kernel/user tracing, this can be removed. 888 */ 889 if (dtp->dt_conf.dtc_ctfmodel != model) { 890 (void) dt_set_errno(dtp, EDT_DATAMODEL); 891 return (NULL); 892 } 893 894 if (dmp->dm_ctdata.cts_size == 0) { 895 (void) dt_set_errno(dtp, EDT_NOCTF); 896 return (NULL); 897 } 898 899 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata, 900 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr); 901 902 if (dmp->dm_ctfp == NULL) { 903 (void) dt_set_errno(dtp, EDT_CTF); 904 return (NULL); 905 } 906 907 (void) ctf_setmodel(dmp->dm_ctfp, model); 908 ctf_setspecific(dmp->dm_ctfp, dmp); 909 910 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) { 911 if ((pmp = dt_module_create(dtp, parent)) == NULL || 912 (pfp = dt_module_getctf(dtp, pmp)) == NULL) { 913 if (pmp == NULL) 914 (void) dt_set_errno(dtp, EDT_NOMEM); 915 goto err; 916 } 917 918 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) { 919 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp); 920 (void) dt_set_errno(dtp, EDT_CTF); 921 goto err; 922 } 923 } 924 925 dt_dprintf("loaded CTF container for %s (%p)\n", 926 dmp->dm_name, (void *)dmp->dm_ctfp); 927 928 return (dmp->dm_ctfp); 929 930 err: 931 ctf_close(dmp->dm_ctfp); 932 dmp->dm_ctfp = NULL; 933 return (NULL); 934 } 935 936 /*ARGSUSED*/ 937 void 938 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp) 939 { 940 int i; 941 942 ctf_close(dmp->dm_ctfp); 943 dmp->dm_ctfp = NULL; 944 945 #ifndef illumos 946 if (dmp->dm_ctdata.cts_data != NULL) { 947 free(dmp->dm_ctdata.cts_data); 948 } 949 if (dmp->dm_symtab.cts_data != NULL) { 950 free(dmp->dm_symtab.cts_data); 951 } 952 if (dmp->dm_strtab.cts_data != NULL) { 953 free(dmp->dm_strtab.cts_data); 954 } 955 #endif 956 957 if (dmp->dm_libctfp != NULL) { 958 for (i = 0; i < dmp->dm_nctflibs; i++) { 959 ctf_close(dmp->dm_libctfp[i]); 960 free(dmp->dm_libctfn[i]); 961 } 962 free(dmp->dm_libctfp); 963 free(dmp->dm_libctfn); 964 dmp->dm_libctfp = NULL; 965 dmp->dm_nctflibs = 0; 966 } 967 968 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t)); 969 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t)); 970 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t)); 971 972 if (dmp->dm_symbuckets != NULL) { 973 free(dmp->dm_symbuckets); 974 dmp->dm_symbuckets = NULL; 975 } 976 977 if (dmp->dm_symchains != NULL) { 978 free(dmp->dm_symchains); 979 dmp->dm_symchains = NULL; 980 } 981 982 if (dmp->dm_asmap != NULL) { 983 free(dmp->dm_asmap); 984 dmp->dm_asmap = NULL; 985 } 986 #if defined(__FreeBSD__) || defined(__NetBSD__) 987 if (dmp->dm_sec_offsets != NULL) { 988 free(dmp->dm_sec_offsets); 989 dmp->dm_sec_offsets = NULL; 990 } 991 #endif 992 dmp->dm_symfree = 0; 993 dmp->dm_nsymbuckets = 0; 994 dmp->dm_nsymelems = 0; 995 dmp->dm_asrsv = 0; 996 dmp->dm_aslen = 0; 997 998 dmp->dm_text_va = 0; 999 dmp->dm_text_size = 0; 1000 dmp->dm_data_va = 0; 1001 dmp->dm_data_size = 0; 1002 dmp->dm_bss_va = 0; 1003 dmp->dm_bss_size = 0; 1004 1005 if (dmp->dm_extern != NULL) { 1006 dt_idhash_destroy(dmp->dm_extern); 1007 dmp->dm_extern = NULL; 1008 } 1009 1010 (void) elf_end(dmp->dm_elf); 1011 dmp->dm_elf = NULL; 1012 1013 dmp->dm_pid = 0; 1014 1015 dmp->dm_flags &= ~DT_DM_LOADED; 1016 } 1017 1018 void 1019 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp) 1020 { 1021 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets; 1022 dt_module_t **dmpp = &dtp->dt_mods[h]; 1023 1024 dt_list_delete(&dtp->dt_modlist, dmp); 1025 assert(dtp->dt_nmods != 0); 1026 dtp->dt_nmods--; 1027 1028 /* 1029 * Now remove this module from its hash chain. We expect to always 1030 * find the module on its hash chain, so in this loop we assert that 1031 * we don't run off the end of the list. 1032 */ 1033 while (*dmpp != dmp) { 1034 dmpp = &((*dmpp)->dm_next); 1035 assert(*dmpp != NULL); 1036 } 1037 1038 *dmpp = dmp->dm_next; 1039 1040 dt_module_unload(dtp, dmp); 1041 free(dmp); 1042 } 1043 1044 /* 1045 * Insert a new external symbol reference into the specified module. The new 1046 * symbol will be marked as undefined and is assigned a symbol index beyond 1047 * any existing cached symbols from this module. We use the ident's di_data 1048 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol. 1049 */ 1050 dt_ident_t * 1051 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp, 1052 const char *name, const dtrace_typeinfo_t *tip) 1053 { 1054 dtrace_syminfo_t *sip; 1055 dt_ident_t *idp; 1056 uint_t id; 1057 1058 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create( 1059 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) { 1060 (void) dt_set_errno(dtp, EDT_NOMEM); 1061 return (NULL); 1062 } 1063 1064 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) { 1065 (void) dt_set_errno(dtp, EDT_SYMOFLOW); 1066 return (NULL); 1067 } 1068 1069 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) { 1070 (void) dt_set_errno(dtp, EDT_NOMEM); 1071 return (NULL); 1072 } 1073 1074 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id, 1075 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); 1076 1077 if (idp == NULL) { 1078 (void) dt_set_errno(dtp, EDT_NOMEM); 1079 free(sip); 1080 return (NULL); 1081 } 1082 1083 sip->dts_object = dmp->dm_name; 1084 sip->dts_name = idp->di_name; 1085 sip->dts_id = idp->di_id; 1086 1087 idp->di_data = sip; 1088 idp->di_ctfp = tip->dtt_ctfp; 1089 idp->di_type = tip->dtt_type; 1090 1091 return (idp); 1092 } 1093 1094 const char * 1095 dt_module_modelname(dt_module_t *dmp) 1096 { 1097 if (dmp->dm_ops == &dt_modops_64) 1098 return ("64-bit"); 1099 else 1100 return ("32-bit"); 1101 } 1102 1103 /* ARGSUSED */ 1104 int 1105 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp) 1106 { 1107 int i; 1108 1109 for (i = 0; i < dmp->dm_nctflibs; i++) { 1110 if (dmp->dm_libctfp[i] == fp) 1111 return (i); 1112 } 1113 1114 return (-1); 1115 } 1116 1117 /* ARGSUSED */ 1118 ctf_file_t * 1119 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name) 1120 { 1121 int i; 1122 1123 for (i = 0; i < dmp->dm_nctflibs; i++) { 1124 if (strcmp(dmp->dm_libctfn[i], name) == 0) 1125 return (dmp->dm_libctfp[i]); 1126 } 1127 1128 return (NULL); 1129 } 1130 1131 /* 1132 * Update our module cache by adding an entry for the specified module 'name'. 1133 * We create the dt_module_t and populate it using /system/object/<name>/. 1134 * 1135 * On FreeBSD, the module name is passed as the full module file name, 1136 * including the path. 1137 */ 1138 static void 1139 #if defined(illumos) || defined(__NetBSD__) 1140 dt_module_update(dtrace_hdl_t *dtp, const char *name) 1141 #elif defined(__FreeBSD__) 1142 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat) 1143 #endif 1144 { 1145 char fname[MAXPATHLEN]; 1146 struct stat64 st; 1147 int fd, err, bits; 1148 #if defined(__FreeBSD__) 1149 struct module_stat ms; 1150 dt_kmodule_t *dkmp; 1151 uint_t h; 1152 int modid; 1153 #endif 1154 1155 dt_module_t *dmp; 1156 const char *s; 1157 size_t shstrs; 1158 GElf_Shdr sh; 1159 Elf_Data *dp; 1160 Elf_Scn *sp; 1161 1162 #ifdef illumos 1163 (void) snprintf(fname, sizeof (fname), 1164 "%s/%s/object", OBJFS_ROOT, name); 1165 #elif defined(__FreeBSD__) 1166 GElf_Ehdr ehdr; 1167 GElf_Phdr ph; 1168 char name[MAXPATHLEN]; 1169 uintptr_t mapbase, alignmask; 1170 int i = 0; 1171 int is_elf_obj; 1172 1173 (void) strlcpy(name, k_stat->name, sizeof(name)); 1174 (void) strlcpy(fname, k_stat->pathname, sizeof(fname)); 1175 #elif defined(__NetBSD__) 1176 int mib_osrel[2] = { CTL_KERN, KERN_OSRELEASE }; 1177 int mib_mach[2] = { CTL_HW, HW_MACHINE }; 1178 char osrel[64]; 1179 char machine[64]; 1180 size_t len; 1181 1182 if (strcmp("netbsd", name) == 0) { 1183 /* want the kernel, but it is not absolute */ 1184 dt_bootfile(machine, sizeof(machine)); 1185 snprintf(fname, sizeof(fname), "/%s", machine); 1186 } else { 1187 1188 /* build stand module path from system */ 1189 len = sizeof(osrel); 1190 if (sysctl(mib_osrel, 2, osrel, &len, NULL, 0) == -1) { 1191 dt_dprintf("sysctl osrel failed: %s\n", 1192 strerror(errno)); 1193 return; 1194 } 1195 1196 len = sizeof(machine); 1197 if (sysctl(mib_mach, 2, machine, &len, NULL, 0) == -1) { 1198 dt_dprintf("sysctl machine failed: %s\n", 1199 strerror(errno)); 1200 return; 1201 } 1202 1203 (void) snprintf(fname, sizeof (fname), 1204 "/stand/%s/%s/modules/%s/%s.kmod", machine, osrel, name, name); 1205 } 1206 #endif 1207 1208 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 || 1209 (dmp = dt_module_create(dtp, name)) == NULL) { 1210 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno)); 1211 (void) close(fd); 1212 return; 1213 } 1214 1215 /* 1216 * Since the module can unload out from under us (and /system/object 1217 * will return ENOENT), tell libelf to cook the entire file now and 1218 * then close the underlying file descriptor immediately. If this 1219 * succeeds, we know that we can continue safely using dmp->dm_elf. 1220 */ 1221 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL); 1222 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD); 1223 (void) close(fd); 1224 1225 if (dmp->dm_elf == NULL || err == -1 || 1226 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) { 1227 dt_dprintf("failed to load %s: %s\n", 1228 fname, elf_errmsg(elf_errno())); 1229 dt_module_destroy(dtp, dmp); 1230 return; 1231 } 1232 1233 switch (gelf_getclass(dmp->dm_elf)) { 1234 case ELFCLASS32: 1235 dmp->dm_ops = &dt_modops_32; 1236 bits = 32; 1237 break; 1238 case ELFCLASS64: 1239 dmp->dm_ops = &dt_modops_64; 1240 bits = 64; 1241 break; 1242 default: 1243 dt_dprintf("failed to load %s: unknown ELF class\n", fname); 1244 dt_module_destroy(dtp, dmp); 1245 return; 1246 } 1247 #if defined(__FreeBSD__) 1248 mapbase = (uintptr_t)k_stat->address; 1249 gelf_getehdr(dmp->dm_elf, &ehdr); 1250 is_elf_obj = (ehdr.e_type == ET_REL); 1251 if (is_elf_obj) { 1252 dmp->dm_sec_offsets = 1253 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets)); 1254 if (dmp->dm_sec_offsets == NULL) { 1255 dt_dprintf("failed to allocate memory\n"); 1256 dt_module_destroy(dtp, dmp); 1257 return; 1258 } 1259 } 1260 #endif 1261 /* 1262 * Iterate over the section headers locating various sections of 1263 * interest and use their attributes to flesh out the dt_module_t. 1264 */ 1265 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) { 1266 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL || 1267 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL) 1268 continue; /* skip any malformed sections */ 1269 #if defined(__FreeBSD__) 1270 if (sh.sh_size == 0) 1271 continue; 1272 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) { 1273 alignmask = sh.sh_addralign - 1; 1274 mapbase += alignmask; 1275 mapbase &= ~alignmask; 1276 sh.sh_addr = mapbase; 1277 if (is_elf_obj) 1278 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr; 1279 mapbase += sh.sh_size; 1280 } 1281 #endif 1282 if (strcmp(s, ".text") == 0) { 1283 dmp->dm_text_size = sh.sh_size; 1284 dmp->dm_text_va = sh.sh_addr; 1285 } else if (strcmp(s, ".data") == 0) { 1286 dmp->dm_data_size = sh.sh_size; 1287 dmp->dm_data_va = sh.sh_addr; 1288 } else if (strcmp(s, ".bss") == 0) { 1289 dmp->dm_bss_size = sh.sh_size; 1290 dmp->dm_bss_va = sh.sh_addr; 1291 } else if (strcmp(s, ".info") == 0 && 1292 (dp = elf_getdata(sp, NULL)) != NULL) { 1293 bcopy(dp->d_buf, &dmp->dm_info, 1294 MIN(sh.sh_size, sizeof (dmp->dm_info))); 1295 } else if (strcmp(s, ".filename") == 0 && 1296 (dp = elf_getdata(sp, NULL)) != NULL) { 1297 (void) strlcpy(dmp->dm_file, 1298 dp->d_buf, sizeof (dmp->dm_file)); 1299 } 1300 } 1301 1302 dmp->dm_flags |= DT_DM_KERNEL; 1303 #ifdef illumos 1304 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino); 1305 #else 1306 /* 1307 * Include .rodata and special sections into .text. 1308 * This depends on default section layout produced by GNU ld 1309 * for ELF objects and libraries: 1310 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable] 1311 */ 1312 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va; 1313 #if defined(__i386__) && !defined(__NetBSD__) 1314 /* 1315 * Find the first load section and figure out the relocation 1316 * offset for the symbols. The kernel module will not need 1317 * relocation, but the kernel linker modules will. 1318 */ 1319 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) { 1320 if (ph.p_type == PT_LOAD) { 1321 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr; 1322 break; 1323 } 1324 } 1325 #endif 1326 #endif /* illumos */ 1327 1328 if (dmp->dm_info.objfs_info_primary) 1329 dmp->dm_flags |= DT_DM_PRIMARY; 1330 1331 #if defined(__FreeBSD__) 1332 ms.version = sizeof(ms); 1333 for (modid = kldfirstmod(k_stat->id); modid > 0; 1334 modid = modnext(modid)) { 1335 if (modstat(modid, &ms) != 0) { 1336 dt_dprintf("modstat failed for id %d in %s: %s\n", 1337 modid, k_stat->name, strerror(errno)); 1338 continue; 1339 } 1340 if (dt_kmodule_lookup(dtp, ms.name) != NULL) 1341 continue; 1342 1343 dkmp = malloc(sizeof (*dkmp)); 1344 if (dkmp == NULL) { 1345 dt_dprintf("failed to allocate memory\n"); 1346 dt_module_destroy(dtp, dmp); 1347 return; 1348 } 1349 1350 h = dt_strtab_hash(ms.name, NULL) % dtp->dt_modbuckets; 1351 dkmp->dkm_next = dtp->dt_kmods[h]; 1352 dkmp->dkm_name = strdup(ms.name); 1353 dkmp->dkm_module = dmp; 1354 dtp->dt_kmods[h] = dkmp; 1355 } 1356 #endif 1357 1358 dt_dprintf("opened %d-bit module %s (%s) [%d]\n", 1359 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid); 1360 } 1361 1362 /* 1363 * Unload all the loaded modules and then refresh the module cache with the 1364 * latest list of loaded modules and their address ranges. 1365 */ 1366 void 1367 dtrace_update(dtrace_hdl_t *dtp) 1368 { 1369 dt_module_t *dmp; 1370 #ifdef illumos 1371 DIR *dirp; 1372 #elif defined(__FreeBSD__) 1373 int fileid; 1374 #endif 1375 1376 for (dmp = dt_list_next(&dtp->dt_modlist); 1377 dmp != NULL; dmp = dt_list_next(dmp)) 1378 dt_module_unload(dtp, dmp); 1379 1380 #ifdef illumos 1381 /* 1382 * Open /system/object and attempt to create a libdtrace module for 1383 * each kernel module that is loaded on the current system. 1384 */ 1385 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) && 1386 (dirp = opendir(OBJFS_ROOT)) != NULL) { 1387 struct dirent *dp; 1388 1389 while ((dp = readdir(dirp)) != NULL) { 1390 if (dp->d_name[0] != '.') 1391 dt_module_update(dtp, dp->d_name); 1392 } 1393 1394 (void) closedir(dirp); 1395 } 1396 #elif defined(__FreeBSD__) 1397 /* 1398 * Use FreeBSD's kernel loader interface to discover what kernel 1399 * modules are loaded and create a libdtrace module for each one. 1400 */ 1401 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1402 struct kld_file_stat k_stat; 1403 k_stat.version = sizeof(k_stat); 1404 if (kldstat(fileid, &k_stat) == 0) 1405 dt_module_update(dtp, &k_stat); 1406 } 1407 #elif defined(__NetBSD__) 1408 /* XXX just the kernel for now */ 1409 dt_module_update(dtp, "netbsd"); 1410 #endif 1411 1412 /* 1413 * Look up all the macro identifiers and set di_id to the latest value. 1414 * This code collaborates with dt_lex.l on the use of di_id. We will 1415 * need to implement something fancier if we need to support non-ints. 1416 */ 1417 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid(); 1418 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid(); 1419 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid(); 1420 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid(); 1421 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0); 1422 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid(); 1423 #ifdef illumos 1424 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid(); 1425 #endif 1426 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0); 1427 #ifdef illumos 1428 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid(); 1429 #endif 1430 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid(); 1431 1432 /* 1433 * Cache the pointers to the modules representing the base executable 1434 * and the run-time linker in the dtrace client handle. Note that on 1435 * x86 krtld is folded into unix, so if we don't find it, use unix 1436 * instead. 1437 */ 1438 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix"); 1439 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld"); 1440 if (dtp->dt_rtld == NULL) 1441 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix"); 1442 1443 /* 1444 * If this is the first time we are initializing the module list, 1445 * remove the module for genunix from the module list and then move it 1446 * to the front of the module list. We do this so that type and symbol 1447 * queries encounter genunix and thereby optimize for the common case 1448 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below. 1449 */ 1450 if (dtp->dt_exec != NULL && 1451 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) { 1452 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec); 1453 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec); 1454 } 1455 } 1456 1457 static dt_module_t * 1458 dt_module_from_object(dtrace_hdl_t *dtp, const char *object) 1459 { 1460 int err = EDT_NOMOD; 1461 dt_module_t *dmp; 1462 1463 switch ((uintptr_t)object) { 1464 case (uintptr_t)DTRACE_OBJ_EXEC: 1465 dmp = dtp->dt_exec; 1466 break; 1467 case (uintptr_t)DTRACE_OBJ_RTLD: 1468 dmp = dtp->dt_rtld; 1469 break; 1470 case (uintptr_t)DTRACE_OBJ_CDEFS: 1471 dmp = dtp->dt_cdefs; 1472 break; 1473 case (uintptr_t)DTRACE_OBJ_DDEFS: 1474 dmp = dtp->dt_ddefs; 1475 break; 1476 default: 1477 dmp = dt_module_create(dtp, object); 1478 err = EDT_NOMEM; 1479 } 1480 1481 if (dmp == NULL) 1482 (void) dt_set_errno(dtp, err); 1483 1484 return (dmp); 1485 } 1486 1487 /* 1488 * Exported interface to look up a symbol by name. We return the GElf_Sym and 1489 * complete symbol information for the matching symbol. 1490 */ 1491 int 1492 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name, 1493 GElf_Sym *symp, dtrace_syminfo_t *sip) 1494 { 1495 dt_module_t *dmp; 1496 dt_ident_t *idp; 1497 uint_t n, id; 1498 GElf_Sym sym; 1499 1500 uint_t mask = 0; /* mask of dt_module flags to match */ 1501 uint_t bits = 0; /* flag bits that must be present */ 1502 1503 if (object != DTRACE_OBJ_EVERY && 1504 object != DTRACE_OBJ_KMODS && 1505 object != DTRACE_OBJ_UMODS) { 1506 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1507 return (-1); /* dt_errno is set for us */ 1508 1509 if (dt_module_load(dtp, dmp) == -1) 1510 return (-1); /* dt_errno is set for us */ 1511 n = 1; 1512 1513 } else { 1514 if (object == DTRACE_OBJ_KMODS) 1515 mask = bits = DT_DM_KERNEL; 1516 else if (object == DTRACE_OBJ_UMODS) 1517 mask = DT_DM_KERNEL; 1518 1519 dmp = dt_list_next(&dtp->dt_modlist); 1520 n = dtp->dt_nmods; 1521 } 1522 1523 if (symp == NULL) 1524 symp = &sym; 1525 1526 for (; n > 0; n--, dmp = dt_list_next(dmp)) { 1527 if ((dmp->dm_flags & mask) != bits) 1528 continue; /* failed to match required attributes */ 1529 1530 if (dt_module_load(dtp, dmp) == -1) 1531 continue; /* failed to load symbol table */ 1532 1533 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) { 1534 if (sip != NULL) { 1535 sip->dts_object = dmp->dm_name; 1536 sip->dts_name = (const char *) 1537 dmp->dm_strtab.cts_data + symp->st_name; 1538 sip->dts_id = id; 1539 } 1540 return (0); 1541 } 1542 1543 if (dmp->dm_extern != NULL && 1544 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) { 1545 if (symp != &sym) { 1546 symp->st_name = (uintptr_t)idp->di_name; 1547 symp->st_info = 1548 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 1549 symp->st_other = 0; 1550 symp->st_shndx = SHN_UNDEF; 1551 symp->st_value = 0; 1552 symp->st_size = 1553 ctf_type_size(idp->di_ctfp, idp->di_type); 1554 } 1555 1556 if (sip != NULL) { 1557 sip->dts_object = dmp->dm_name; 1558 sip->dts_name = idp->di_name; 1559 sip->dts_id = idp->di_id; 1560 } 1561 1562 return (0); 1563 } 1564 } 1565 1566 return (dt_set_errno(dtp, EDT_NOSYM)); 1567 } 1568 1569 /* 1570 * Exported interface to look up a symbol by address. We return the GElf_Sym 1571 * and complete symbol information for the matching symbol. 1572 */ 1573 int 1574 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr, 1575 GElf_Sym *symp, dtrace_syminfo_t *sip) 1576 { 1577 dt_module_t *dmp; 1578 uint_t id; 1579 const dtrace_vector_t *v = dtp->dt_vector; 1580 1581 if (v != NULL) 1582 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip)); 1583 1584 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL; 1585 dmp = dt_list_next(dmp)) { 1586 if (addr - dmp->dm_text_va < dmp->dm_text_size || 1587 addr - dmp->dm_data_va < dmp->dm_data_size || 1588 addr - dmp->dm_bss_va < dmp->dm_bss_size) 1589 break; 1590 } 1591 1592 if (dmp == NULL) 1593 return (dt_set_errno(dtp, EDT_NOSYMADDR)); 1594 1595 if (dt_module_load(dtp, dmp) == -1) 1596 return (-1); /* dt_errno is set for us */ 1597 1598 if (symp != NULL) { 1599 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL) 1600 return (dt_set_errno(dtp, EDT_NOSYMADDR)); 1601 } 1602 1603 if (sip != NULL) { 1604 sip->dts_object = dmp->dm_name; 1605 1606 if (symp != NULL) { 1607 sip->dts_name = (const char *) 1608 dmp->dm_strtab.cts_data + symp->st_name; 1609 sip->dts_id = id; 1610 } else { 1611 sip->dts_name = NULL; 1612 sip->dts_id = 0; 1613 } 1614 } 1615 1616 return (0); 1617 } 1618 1619 int 1620 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name, 1621 dtrace_typeinfo_t *tip) 1622 { 1623 dtrace_typeinfo_t ti; 1624 dt_module_t *dmp; 1625 int found = 0; 1626 ctf_id_t id = CTF_ERR; // XXX: gcc 1627 uint_t n, i; 1628 int justone; 1629 ctf_file_t *fp = NULL; // XXX: gcc 1630 char *buf, *p, *q; 1631 1632 uint_t mask = 0; /* mask of dt_module flags to match */ 1633 uint_t bits = 0; /* flag bits that must be present */ 1634 1635 if (object != DTRACE_OBJ_EVERY && 1636 object != DTRACE_OBJ_KMODS && 1637 object != DTRACE_OBJ_UMODS) { 1638 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1639 return (-1); /* dt_errno is set for us */ 1640 1641 if (dt_module_load(dtp, dmp) == -1) 1642 return (-1); /* dt_errno is set for us */ 1643 n = 1; 1644 justone = 1; 1645 } else { 1646 if (object == DTRACE_OBJ_KMODS) 1647 mask = bits = DT_DM_KERNEL; 1648 else if (object == DTRACE_OBJ_UMODS) 1649 mask = DT_DM_KERNEL; 1650 1651 dmp = dt_list_next(&dtp->dt_modlist); 1652 n = dtp->dt_nmods; 1653 justone = 0; 1654 } 1655 1656 if (tip == NULL) 1657 tip = &ti; 1658 1659 for (; n > 0; n--, dmp = dt_list_next(dmp)) { 1660 if ((dmp->dm_flags & mask) != bits) 1661 continue; /* failed to match required attributes */ 1662 1663 /* 1664 * If we can't load the CTF container, continue on to the next 1665 * module. If our search was scoped to only one module then 1666 * return immediately leaving dt_errno unmodified. 1667 */ 1668 if (dt_module_hasctf(dtp, dmp) == 0) { 1669 if (justone) 1670 return (-1); 1671 continue; 1672 } 1673 1674 /* 1675 * Look up the type in the module's CTF container. If our 1676 * match is a forward declaration tag, save this choice in 1677 * 'tip' and keep going in the hope that we will locate the 1678 * underlying structure definition. Otherwise just return. 1679 */ 1680 if (dmp->dm_pid == 0) { 1681 id = ctf_lookup_by_name(dmp->dm_ctfp, name); 1682 fp = dmp->dm_ctfp; 1683 } else { 1684 if ((p = strchr(name, '`')) != NULL) { 1685 buf = strdup(name); 1686 if (buf == NULL) 1687 return (dt_set_errno(dtp, EDT_NOMEM)); 1688 p = strchr(buf, '`'); 1689 if ((q = strchr(p + 1, '`')) != NULL) 1690 p = q; 1691 *p = '\0'; 1692 fp = dt_module_getctflib(dtp, dmp, buf); 1693 if (fp == NULL || (id = ctf_lookup_by_name(fp, 1694 p + 1)) == CTF_ERR) 1695 id = CTF_ERR; 1696 free(buf); 1697 } else { 1698 for (i = 0; i < dmp->dm_nctflibs; i++) { 1699 fp = dmp->dm_libctfp[i]; 1700 id = ctf_lookup_by_name(fp, name); 1701 if (id != CTF_ERR) 1702 break; 1703 } 1704 } 1705 } 1706 if (id != CTF_ERR) { 1707 tip->dtt_object = dmp->dm_name; 1708 tip->dtt_ctfp = fp; 1709 tip->dtt_type = id; 1710 if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) != 1711 CTF_K_FORWARD) 1712 return (0); 1713 1714 found++; 1715 } 1716 } 1717 1718 if (found == 0) 1719 return (dt_set_errno(dtp, EDT_NOTYPE)); 1720 1721 return (0); 1722 } 1723 1724 int 1725 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp, 1726 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip) 1727 { 1728 dt_module_t *dmp; 1729 1730 tip->dtt_object = NULL; 1731 tip->dtt_ctfp = NULL; 1732 tip->dtt_type = CTF_ERR; 1733 tip->dtt_flags = 0; 1734 1735 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL) 1736 return (dt_set_errno(dtp, EDT_NOMOD)); 1737 1738 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) { 1739 dt_ident_t *idp = 1740 dt_idhash_lookup(dmp->dm_extern, sip->dts_name); 1741 1742 if (idp == NULL) 1743 return (dt_set_errno(dtp, EDT_NOSYM)); 1744 1745 tip->dtt_ctfp = idp->di_ctfp; 1746 tip->dtt_type = idp->di_type; 1747 1748 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) { 1749 if (dt_module_getctf(dtp, dmp) == NULL) 1750 return (-1); /* errno is set for us */ 1751 1752 tip->dtt_ctfp = dmp->dm_ctfp; 1753 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id); 1754 1755 if (tip->dtt_type == CTF_ERR) { 1756 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp); 1757 return (dt_set_errno(dtp, EDT_CTF)); 1758 } 1759 1760 } else { 1761 tip->dtt_ctfp = DT_FPTR_CTFP(dtp); 1762 tip->dtt_type = DT_FPTR_TYPE(dtp); 1763 } 1764 1765 tip->dtt_object = dmp->dm_name; 1766 return (0); 1767 } 1768 1769 static dtrace_objinfo_t * 1770 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto) 1771 { 1772 dto->dto_name = dmp->dm_name; 1773 dto->dto_file = dmp->dm_file; 1774 dto->dto_id = dmp->dm_modid; 1775 dto->dto_flags = 0; 1776 1777 if (dmp->dm_flags & DT_DM_KERNEL) 1778 dto->dto_flags |= DTRACE_OBJ_F_KERNEL; 1779 if (dmp->dm_flags & DT_DM_PRIMARY) 1780 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY; 1781 1782 dto->dto_text_va = dmp->dm_text_va; 1783 dto->dto_text_size = dmp->dm_text_size; 1784 dto->dto_data_va = dmp->dm_data_va; 1785 dto->dto_data_size = dmp->dm_data_size; 1786 dto->dto_bss_va = dmp->dm_bss_va; 1787 dto->dto_bss_size = dmp->dm_bss_size; 1788 1789 return (dto); 1790 } 1791 1792 int 1793 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data) 1794 { 1795 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist); 1796 dtrace_objinfo_t dto; 1797 int rv; 1798 1799 for (; dmp != NULL; dmp = dt_list_next(dmp)) { 1800 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0) 1801 return (rv); 1802 } 1803 1804 return (0); 1805 } 1806 1807 int 1808 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto) 1809 { 1810 dt_module_t *dmp; 1811 1812 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS || 1813 object == DTRACE_OBJ_UMODS || dto == NULL) 1814 return (dt_set_errno(dtp, EINVAL)); 1815 1816 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1817 return (-1); /* dt_errno is set for us */ 1818 1819 if (dt_module_load(dtp, dmp) == -1) 1820 return (-1); /* dt_errno is set for us */ 1821 1822 (void) dt_module_info(dmp, dto); 1823 return (0); 1824 } 1825