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_release(dtp, p); 717 return (dt_set_errno(dtp, EDT_CANTLOAD)); 718 } 719 720 if (arg.dpa_count == 0) { 721 dt_dprintf("no ctf data present\n"); 722 dt_proc_unlock(dtp, p); 723 dt_proc_release(dtp, p); 724 return (dt_set_errno(dtp, EDT_CANTLOAD)); 725 } 726 727 dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count); 728 if (dmp->dm_libctfp == NULL) { 729 dt_proc_unlock(dtp, p); 730 dt_proc_release(dtp, p); 731 return (dt_set_errno(dtp, EDT_NOMEM)); 732 } 733 bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count); 734 735 dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count); 736 if (dmp->dm_libctfn == NULL) { 737 free(dmp->dm_libctfp); 738 dt_proc_unlock(dtp, p); 739 dt_proc_release(dtp, p); 740 return (dt_set_errno(dtp, EDT_NOMEM)); 741 } 742 bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count); 743 744 dmp->dm_nctflibs = arg.dpa_count; 745 746 arg.dpa_count = 0; 747 if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) { 748 dt_proc_unlock(dtp, p); 749 dt_module_unload(dtp, dmp); 750 dt_proc_release(dtp, p); 751 return (dt_set_errno(dtp, EDT_CANTLOAD)); 752 } 753 assert(arg.dpa_count == dmp->dm_nctflibs); 754 dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count, 755 (int)dmp->dm_pid); 756 757 dt_proc_unlock(dtp, p); 758 dt_proc_release(dtp, p); 759 dmp->dm_flags |= DT_DM_LOADED; 760 761 return (0); 762 } 763 764 int 765 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp) 766 { 767 if (dmp->dm_flags & DT_DM_LOADED) 768 return (0); /* module is already loaded */ 769 770 if (dmp->dm_pid != 0) 771 return (dt_module_load_proc(dtp, dmp)); 772 773 dmp->dm_ctdata.cts_name = ".SUNW_ctf"; 774 dmp->dm_ctdata.cts_type = SHT_PROGBITS; 775 dmp->dm_ctdata.cts_flags = 0; 776 dmp->dm_ctdata.cts_data = NULL; 777 dmp->dm_ctdata.cts_size = 0; 778 dmp->dm_ctdata.cts_entsize = 0; 779 dmp->dm_ctdata.cts_offset = 0; 780 781 dmp->dm_symtab.cts_name = ".symtab"; 782 dmp->dm_symtab.cts_type = SHT_SYMTAB; 783 dmp->dm_symtab.cts_flags = 0; 784 dmp->dm_symtab.cts_data = NULL; 785 dmp->dm_symtab.cts_size = 0; 786 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ? 787 sizeof (Elf64_Sym) : sizeof (Elf32_Sym); 788 dmp->dm_symtab.cts_offset = 0; 789 790 dmp->dm_strtab.cts_name = ".strtab"; 791 dmp->dm_strtab.cts_type = SHT_STRTAB; 792 dmp->dm_strtab.cts_flags = 0; 793 dmp->dm_strtab.cts_data = NULL; 794 dmp->dm_strtab.cts_size = 0; 795 dmp->dm_strtab.cts_entsize = 0; 796 dmp->dm_strtab.cts_offset = 0; 797 798 /* 799 * Attempt to load the module's CTF section, symbol table section, and 800 * string table section. Note that modules may not contain CTF data: 801 * this will result in a successful load_sect but data of size zero. 802 * We will then fail if dt_module_getctf() is called, as shown below. 803 */ 804 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 || 805 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 || 806 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) { 807 dt_module_unload(dtp, dmp); 808 return (-1); /* dt_errno is set for us */ 809 } 810 811 /* 812 * Allocate the hash chains and hash buckets for symbol name lookup. 813 * This is relatively simple since the symbol table is of fixed size 814 * and is known in advance. We allocate one extra element since we 815 * use element indices instead of pointers and zero is our sentinel. 816 */ 817 dmp->dm_nsymelems = 818 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize; 819 820 dmp->dm_nsymbuckets = _dtrace_strbuckets; 821 dmp->dm_symfree = 1; /* first free element is index 1 */ 822 823 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets); 824 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1); 825 826 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) { 827 dt_module_unload(dtp, dmp); 828 return (dt_set_errno(dtp, EDT_NOMEM)); 829 } 830 831 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets); 832 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1); 833 834 /* 835 * Iterate over the symbol table data buffer and insert each symbol 836 * name into the name hash if the name and type are valid. Then 837 * allocate the address map, fill it in, and sort it. 838 */ 839 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp); 840 841 dt_dprintf("hashed %s [%s] (%u symbols)\n", 842 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1); 843 844 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) { 845 dt_module_unload(dtp, dmp); 846 return (dt_set_errno(dtp, EDT_NOMEM)); 847 } 848 849 dmp->dm_ops->do_symsort(dmp); 850 851 dt_dprintf("sorted %s [%s] (%u symbols)\n", 852 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen); 853 854 dmp->dm_flags |= DT_DM_LOADED; 855 return (0); 856 } 857 858 int 859 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp) 860 { 861 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0) 862 return (1); 863 return (dt_module_getctf(dtp, dmp) != NULL); 864 } 865 866 ctf_file_t * 867 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp) 868 { 869 const char *parent; 870 dt_module_t *pmp; 871 ctf_file_t *pfp; 872 int model; 873 874 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0) 875 return (dmp->dm_ctfp); 876 877 if (dmp->dm_ops == &dt_modops_64) 878 model = CTF_MODEL_LP64; 879 else 880 model = CTF_MODEL_ILP32; 881 882 /* 883 * If the data model of the module does not match our program data 884 * model, then do not permit CTF from this module to be opened and 885 * returned to the compiler. If we support mixed data models in the 886 * future for combined kernel/user tracing, this can be removed. 887 */ 888 if (dtp->dt_conf.dtc_ctfmodel != model) { 889 (void) dt_set_errno(dtp, EDT_DATAMODEL); 890 return (NULL); 891 } 892 893 if (dmp->dm_ctdata.cts_size == 0) { 894 (void) dt_set_errno(dtp, EDT_NOCTF); 895 return (NULL); 896 } 897 898 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata, 899 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr); 900 901 if (dmp->dm_ctfp == NULL) { 902 (void) dt_set_errno(dtp, EDT_CTF); 903 return (NULL); 904 } 905 906 (void) ctf_setmodel(dmp->dm_ctfp, model); 907 ctf_setspecific(dmp->dm_ctfp, dmp); 908 909 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) { 910 if ((pmp = dt_module_create(dtp, parent)) == NULL || 911 (pfp = dt_module_getctf(dtp, pmp)) == NULL) { 912 if (pmp == NULL) 913 (void) dt_set_errno(dtp, EDT_NOMEM); 914 goto err; 915 } 916 917 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) { 918 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp); 919 (void) dt_set_errno(dtp, EDT_CTF); 920 goto err; 921 } 922 } 923 924 dt_dprintf("loaded CTF container for %s (%p)\n", 925 dmp->dm_name, (void *)dmp->dm_ctfp); 926 927 return (dmp->dm_ctfp); 928 929 err: 930 ctf_close(dmp->dm_ctfp); 931 dmp->dm_ctfp = NULL; 932 return (NULL); 933 } 934 935 /*ARGSUSED*/ 936 void 937 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp) 938 { 939 int i; 940 941 ctf_close(dmp->dm_ctfp); 942 dmp->dm_ctfp = NULL; 943 944 #ifndef illumos 945 if (dmp->dm_ctdata.cts_data != NULL) { 946 free(dmp->dm_ctdata.cts_data); 947 } 948 if (dmp->dm_symtab.cts_data != NULL) { 949 free(dmp->dm_symtab.cts_data); 950 } 951 if (dmp->dm_strtab.cts_data != NULL) { 952 free(dmp->dm_strtab.cts_data); 953 } 954 #endif 955 956 if (dmp->dm_libctfp != NULL) { 957 for (i = 0; i < dmp->dm_nctflibs; i++) { 958 ctf_close(dmp->dm_libctfp[i]); 959 free(dmp->dm_libctfn[i]); 960 } 961 free(dmp->dm_libctfp); 962 free(dmp->dm_libctfn); 963 dmp->dm_libctfp = NULL; 964 dmp->dm_nctflibs = 0; 965 } 966 967 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t)); 968 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t)); 969 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t)); 970 971 if (dmp->dm_symbuckets != NULL) { 972 free(dmp->dm_symbuckets); 973 dmp->dm_symbuckets = NULL; 974 } 975 976 if (dmp->dm_symchains != NULL) { 977 free(dmp->dm_symchains); 978 dmp->dm_symchains = NULL; 979 } 980 981 if (dmp->dm_asmap != NULL) { 982 free(dmp->dm_asmap); 983 dmp->dm_asmap = NULL; 984 } 985 #if defined(__FreeBSD__) || defined(__NetBSD__) 986 if (dmp->dm_sec_offsets != NULL) { 987 free(dmp->dm_sec_offsets); 988 dmp->dm_sec_offsets = NULL; 989 } 990 #endif 991 dmp->dm_symfree = 0; 992 dmp->dm_nsymbuckets = 0; 993 dmp->dm_nsymelems = 0; 994 dmp->dm_asrsv = 0; 995 dmp->dm_aslen = 0; 996 997 dmp->dm_text_va = 0; 998 dmp->dm_text_size = 0; 999 dmp->dm_data_va = 0; 1000 dmp->dm_data_size = 0; 1001 dmp->dm_bss_va = 0; 1002 dmp->dm_bss_size = 0; 1003 1004 if (dmp->dm_extern != NULL) { 1005 dt_idhash_destroy(dmp->dm_extern); 1006 dmp->dm_extern = NULL; 1007 } 1008 1009 (void) elf_end(dmp->dm_elf); 1010 dmp->dm_elf = NULL; 1011 1012 dmp->dm_pid = 0; 1013 1014 dmp->dm_flags &= ~DT_DM_LOADED; 1015 } 1016 1017 void 1018 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp) 1019 { 1020 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets; 1021 dt_module_t **dmpp = &dtp->dt_mods[h]; 1022 1023 dt_list_delete(&dtp->dt_modlist, dmp); 1024 assert(dtp->dt_nmods != 0); 1025 dtp->dt_nmods--; 1026 1027 /* 1028 * Now remove this module from its hash chain. We expect to always 1029 * find the module on its hash chain, so in this loop we assert that 1030 * we don't run off the end of the list. 1031 */ 1032 while (*dmpp != dmp) { 1033 dmpp = &((*dmpp)->dm_next); 1034 assert(*dmpp != NULL); 1035 } 1036 1037 *dmpp = dmp->dm_next; 1038 1039 dt_module_unload(dtp, dmp); 1040 free(dmp); 1041 } 1042 1043 /* 1044 * Insert a new external symbol reference into the specified module. The new 1045 * symbol will be marked as undefined and is assigned a symbol index beyond 1046 * any existing cached symbols from this module. We use the ident's di_data 1047 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol. 1048 */ 1049 dt_ident_t * 1050 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp, 1051 const char *name, const dtrace_typeinfo_t *tip) 1052 { 1053 dtrace_syminfo_t *sip; 1054 dt_ident_t *idp; 1055 uint_t id; 1056 1057 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create( 1058 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) { 1059 (void) dt_set_errno(dtp, EDT_NOMEM); 1060 return (NULL); 1061 } 1062 1063 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) { 1064 (void) dt_set_errno(dtp, EDT_SYMOFLOW); 1065 return (NULL); 1066 } 1067 1068 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) { 1069 (void) dt_set_errno(dtp, EDT_NOMEM); 1070 return (NULL); 1071 } 1072 1073 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id, 1074 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen); 1075 1076 if (idp == NULL) { 1077 (void) dt_set_errno(dtp, EDT_NOMEM); 1078 free(sip); 1079 return (NULL); 1080 } 1081 1082 sip->dts_object = dmp->dm_name; 1083 sip->dts_name = idp->di_name; 1084 sip->dts_id = idp->di_id; 1085 1086 idp->di_data = sip; 1087 idp->di_ctfp = tip->dtt_ctfp; 1088 idp->di_type = tip->dtt_type; 1089 1090 return (idp); 1091 } 1092 1093 const char * 1094 dt_module_modelname(dt_module_t *dmp) 1095 { 1096 if (dmp->dm_ops == &dt_modops_64) 1097 return ("64-bit"); 1098 else 1099 return ("32-bit"); 1100 } 1101 1102 /* ARGSUSED */ 1103 int 1104 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp) 1105 { 1106 int i; 1107 1108 for (i = 0; i < dmp->dm_nctflibs; i++) { 1109 if (dmp->dm_libctfp[i] == fp) 1110 return (i); 1111 } 1112 1113 return (-1); 1114 } 1115 1116 /* ARGSUSED */ 1117 ctf_file_t * 1118 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name) 1119 { 1120 int i; 1121 1122 for (i = 0; i < dmp->dm_nctflibs; i++) { 1123 if (strcmp(dmp->dm_libctfn[i], name) == 0) 1124 return (dmp->dm_libctfp[i]); 1125 } 1126 1127 return (NULL); 1128 } 1129 1130 /* 1131 * Update our module cache by adding an entry for the specified module 'name'. 1132 * We create the dt_module_t and populate it using /system/object/<name>/. 1133 * 1134 * On FreeBSD, the module name is passed as the full module file name, 1135 * including the path. 1136 */ 1137 static void 1138 #if defined(illumos) || defined(__NetBSD__) 1139 dt_module_update(dtrace_hdl_t *dtp, const char *name) 1140 #elif defined(__FreeBSD__) 1141 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat) 1142 #endif 1143 { 1144 char fname[MAXPATHLEN]; 1145 struct stat64 st; 1146 int fd, err, bits; 1147 #if defined(__FreeBSD__) 1148 struct module_stat ms; 1149 dt_kmodule_t *dkmp; 1150 uint_t h; 1151 int modid; 1152 #endif 1153 1154 dt_module_t *dmp; 1155 const char *s; 1156 size_t shstrs; 1157 GElf_Shdr sh; 1158 Elf_Data *dp; 1159 Elf_Scn *sp; 1160 1161 #ifdef illumos 1162 (void) snprintf(fname, sizeof (fname), 1163 "%s/%s/object", OBJFS_ROOT, name); 1164 #elif defined(__FreeBSD__) 1165 GElf_Ehdr ehdr; 1166 GElf_Phdr ph; 1167 char name[MAXPATHLEN]; 1168 uintptr_t mapbase, alignmask; 1169 int i = 0; 1170 int is_elf_obj; 1171 1172 (void) strlcpy(name, k_stat->name, sizeof(name)); 1173 (void) strlcpy(fname, k_stat->pathname, sizeof(fname)); 1174 #elif defined(__NetBSD__) 1175 int mib_osrel[2] = { CTL_KERN, KERN_OSRELEASE }; 1176 int mib_mach[2] = { CTL_HW, HW_MACHINE }; 1177 char osrel[64]; 1178 char machine[64]; 1179 size_t len; 1180 1181 if (strcmp("netbsd", name) == 0) { 1182 /* want the kernel */ 1183 dt_bootfile(fname, sizeof(fname)); 1184 } else { 1185 1186 /* build stand module path from system */ 1187 len = sizeof(osrel); 1188 if (sysctl(mib_osrel, 2, osrel, &len, NULL, 0) == -1) { 1189 dt_dprintf("sysctl osrel failed: %s\n", 1190 strerror(errno)); 1191 return; 1192 } 1193 1194 len = sizeof(machine); 1195 if (sysctl(mib_mach, 2, machine, &len, NULL, 0) == -1) { 1196 dt_dprintf("sysctl machine failed: %s\n", 1197 strerror(errno)); 1198 return; 1199 } 1200 1201 (void) snprintf(fname, sizeof (fname), 1202 "/stand/%s/%s/modules/%s/%s.kmod", machine, osrel, name, name); 1203 } 1204 #endif 1205 1206 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 || 1207 (dmp = dt_module_create(dtp, name)) == NULL) { 1208 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno)); 1209 (void) close(fd); 1210 return; 1211 } 1212 1213 /* 1214 * Since the module can unload out from under us (and /system/object 1215 * will return ENOENT), tell libelf to cook the entire file now and 1216 * then close the underlying file descriptor immediately. If this 1217 * succeeds, we know that we can continue safely using dmp->dm_elf. 1218 */ 1219 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL); 1220 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD); 1221 (void) close(fd); 1222 1223 if (dmp->dm_elf == NULL || err == -1 || 1224 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) { 1225 dt_dprintf("failed to load %s: %s\n", 1226 fname, elf_errmsg(elf_errno())); 1227 dt_module_destroy(dtp, dmp); 1228 return; 1229 } 1230 1231 switch (gelf_getclass(dmp->dm_elf)) { 1232 case ELFCLASS32: 1233 dmp->dm_ops = &dt_modops_32; 1234 bits = 32; 1235 break; 1236 case ELFCLASS64: 1237 dmp->dm_ops = &dt_modops_64; 1238 bits = 64; 1239 break; 1240 default: 1241 dt_dprintf("failed to load %s: unknown ELF class\n", fname); 1242 dt_module_destroy(dtp, dmp); 1243 return; 1244 } 1245 #if defined(__FreeBSD__) 1246 mapbase = (uintptr_t)k_stat->address; 1247 gelf_getehdr(dmp->dm_elf, &ehdr); 1248 is_elf_obj = (ehdr.e_type == ET_REL); 1249 if (is_elf_obj) { 1250 dmp->dm_sec_offsets = 1251 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets)); 1252 if (dmp->dm_sec_offsets == NULL) { 1253 dt_dprintf("failed to allocate memory\n"); 1254 dt_module_destroy(dtp, dmp); 1255 return; 1256 } 1257 } 1258 #endif 1259 /* 1260 * Iterate over the section headers locating various sections of 1261 * interest and use their attributes to flesh out the dt_module_t. 1262 */ 1263 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) { 1264 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL || 1265 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL) 1266 continue; /* skip any malformed sections */ 1267 #if defined(__FreeBSD__) 1268 if (sh.sh_size == 0) 1269 continue; 1270 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) { 1271 alignmask = sh.sh_addralign - 1; 1272 mapbase += alignmask; 1273 mapbase &= ~alignmask; 1274 sh.sh_addr = mapbase; 1275 if (is_elf_obj) 1276 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr; 1277 mapbase += sh.sh_size; 1278 } 1279 #endif 1280 if (strcmp(s, ".text") == 0) { 1281 dmp->dm_text_size = sh.sh_size; 1282 dmp->dm_text_va = sh.sh_addr; 1283 } else if (strcmp(s, ".data") == 0) { 1284 dmp->dm_data_size = sh.sh_size; 1285 dmp->dm_data_va = sh.sh_addr; 1286 } else if (strcmp(s, ".bss") == 0) { 1287 dmp->dm_bss_size = sh.sh_size; 1288 dmp->dm_bss_va = sh.sh_addr; 1289 } else if (strcmp(s, ".info") == 0 && 1290 (dp = elf_getdata(sp, NULL)) != NULL) { 1291 bcopy(dp->d_buf, &dmp->dm_info, 1292 MIN(sh.sh_size, sizeof (dmp->dm_info))); 1293 } else if (strcmp(s, ".filename") == 0 && 1294 (dp = elf_getdata(sp, NULL)) != NULL) { 1295 (void) strlcpy(dmp->dm_file, 1296 dp->d_buf, sizeof (dmp->dm_file)); 1297 } 1298 } 1299 1300 dmp->dm_flags |= DT_DM_KERNEL; 1301 #ifdef illumos 1302 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino); 1303 #else 1304 /* 1305 * Include .rodata and special sections into .text. 1306 * This depends on default section layout produced by GNU ld 1307 * for ELF objects and libraries: 1308 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable] 1309 */ 1310 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va; 1311 #if defined(__i386__) && !defined(__NetBSD__) 1312 /* 1313 * Find the first load section and figure out the relocation 1314 * offset for the symbols. The kernel module will not need 1315 * relocation, but the kernel linker modules will. 1316 */ 1317 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) { 1318 if (ph.p_type == PT_LOAD) { 1319 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr; 1320 break; 1321 } 1322 } 1323 #endif 1324 #endif /* illumos */ 1325 1326 if (dmp->dm_info.objfs_info_primary) 1327 dmp->dm_flags |= DT_DM_PRIMARY; 1328 1329 #if defined(__FreeBSD__) 1330 ms.version = sizeof(ms); 1331 for (modid = kldfirstmod(k_stat->id); modid > 0; 1332 modid = modnext(modid)) { 1333 if (modstat(modid, &ms) != 0) { 1334 dt_dprintf("modstat failed for id %d in %s: %s\n", 1335 modid, k_stat->name, strerror(errno)); 1336 continue; 1337 } 1338 if (dt_kmodule_lookup(dtp, ms.name) != NULL) 1339 continue; 1340 1341 dkmp = malloc(sizeof (*dkmp)); 1342 if (dkmp == NULL) { 1343 dt_dprintf("failed to allocate memory\n"); 1344 dt_module_destroy(dtp, dmp); 1345 return; 1346 } 1347 1348 h = dt_strtab_hash(ms.name, NULL) % dtp->dt_modbuckets; 1349 dkmp->dkm_next = dtp->dt_kmods[h]; 1350 dkmp->dkm_name = strdup(ms.name); 1351 dkmp->dkm_module = dmp; 1352 dtp->dt_kmods[h] = dkmp; 1353 } 1354 #endif 1355 1356 dt_dprintf("opened %d-bit module %s (%s) [%d]\n", 1357 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid); 1358 } 1359 1360 /* 1361 * Unload all the loaded modules and then refresh the module cache with the 1362 * latest list of loaded modules and their address ranges. 1363 */ 1364 void 1365 dtrace_update(dtrace_hdl_t *dtp) 1366 { 1367 dt_module_t *dmp; 1368 #ifdef illumos 1369 DIR *dirp; 1370 #elif defined(__FreeBSD__) 1371 int fileid; 1372 #endif 1373 1374 for (dmp = dt_list_next(&dtp->dt_modlist); 1375 dmp != NULL; dmp = dt_list_next(dmp)) 1376 dt_module_unload(dtp, dmp); 1377 1378 #ifdef illumos 1379 /* 1380 * Open /system/object and attempt to create a libdtrace module for 1381 * each kernel module that is loaded on the current system. 1382 */ 1383 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) && 1384 (dirp = opendir(OBJFS_ROOT)) != NULL) { 1385 struct dirent *dp; 1386 1387 while ((dp = readdir(dirp)) != NULL) { 1388 if (dp->d_name[0] != '.') 1389 dt_module_update(dtp, dp->d_name); 1390 } 1391 1392 (void) closedir(dirp); 1393 } 1394 #elif defined(__FreeBSD__) 1395 /* 1396 * Use FreeBSD's kernel loader interface to discover what kernel 1397 * modules are loaded and create a libdtrace module for each one. 1398 */ 1399 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) { 1400 struct kld_file_stat k_stat; 1401 k_stat.version = sizeof(k_stat); 1402 if (kldstat(fileid, &k_stat) == 0) 1403 dt_module_update(dtp, &k_stat); 1404 } 1405 #elif defined(__NetBSD__) 1406 /* XXX just the kernel for now */ 1407 dt_module_update(dtp, "netbsd"); 1408 #endif 1409 1410 /* 1411 * Look up all the macro identifiers and set di_id to the latest value. 1412 * This code collaborates with dt_lex.l on the use of di_id. We will 1413 * need to implement something fancier if we need to support non-ints. 1414 */ 1415 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid(); 1416 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid(); 1417 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid(); 1418 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid(); 1419 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0); 1420 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid(); 1421 #ifdef illumos 1422 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid(); 1423 #endif 1424 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0); 1425 #ifdef illumos 1426 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid(); 1427 #endif 1428 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid(); 1429 1430 /* 1431 * Cache the pointers to the modules representing the base executable 1432 * and the run-time linker in the dtrace client handle. Note that on 1433 * x86 krtld is folded into unix, so if we don't find it, use unix 1434 * instead. 1435 */ 1436 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix"); 1437 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld"); 1438 if (dtp->dt_rtld == NULL) 1439 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix"); 1440 1441 /* 1442 * If this is the first time we are initializing the module list, 1443 * remove the module for genunix from the module list and then move it 1444 * to the front of the module list. We do this so that type and symbol 1445 * queries encounter genunix and thereby optimize for the common case 1446 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below. 1447 */ 1448 if (dtp->dt_exec != NULL && 1449 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) { 1450 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec); 1451 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec); 1452 } 1453 } 1454 1455 static dt_module_t * 1456 dt_module_from_object(dtrace_hdl_t *dtp, const char *object) 1457 { 1458 int err = EDT_NOMOD; 1459 dt_module_t *dmp; 1460 1461 switch ((uintptr_t)object) { 1462 case (uintptr_t)DTRACE_OBJ_EXEC: 1463 dmp = dtp->dt_exec; 1464 break; 1465 case (uintptr_t)DTRACE_OBJ_RTLD: 1466 dmp = dtp->dt_rtld; 1467 break; 1468 case (uintptr_t)DTRACE_OBJ_CDEFS: 1469 dmp = dtp->dt_cdefs; 1470 break; 1471 case (uintptr_t)DTRACE_OBJ_DDEFS: 1472 dmp = dtp->dt_ddefs; 1473 break; 1474 default: 1475 dmp = dt_module_create(dtp, object); 1476 err = EDT_NOMEM; 1477 } 1478 1479 if (dmp == NULL) 1480 (void) dt_set_errno(dtp, err); 1481 1482 return (dmp); 1483 } 1484 1485 /* 1486 * Exported interface to look up a symbol by name. We return the GElf_Sym and 1487 * complete symbol information for the matching symbol. 1488 */ 1489 int 1490 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name, 1491 GElf_Sym *symp, dtrace_syminfo_t *sip) 1492 { 1493 dt_module_t *dmp; 1494 dt_ident_t *idp; 1495 uint_t n, id; 1496 GElf_Sym sym; 1497 1498 uint_t mask = 0; /* mask of dt_module flags to match */ 1499 uint_t bits = 0; /* flag bits that must be present */ 1500 1501 if (object != DTRACE_OBJ_EVERY && 1502 object != DTRACE_OBJ_KMODS && 1503 object != DTRACE_OBJ_UMODS) { 1504 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1505 return (-1); /* dt_errno is set for us */ 1506 1507 if (dt_module_load(dtp, dmp) == -1) 1508 return (-1); /* dt_errno is set for us */ 1509 n = 1; 1510 1511 } else { 1512 if (object == DTRACE_OBJ_KMODS) 1513 mask = bits = DT_DM_KERNEL; 1514 else if (object == DTRACE_OBJ_UMODS) 1515 mask = DT_DM_KERNEL; 1516 1517 dmp = dt_list_next(&dtp->dt_modlist); 1518 n = dtp->dt_nmods; 1519 } 1520 1521 if (symp == NULL) 1522 symp = &sym; 1523 1524 for (; n > 0; n--, dmp = dt_list_next(dmp)) { 1525 if ((dmp->dm_flags & mask) != bits) 1526 continue; /* failed to match required attributes */ 1527 1528 if (dt_module_load(dtp, dmp) == -1) 1529 continue; /* failed to load symbol table */ 1530 1531 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) { 1532 if (sip != NULL) { 1533 sip->dts_object = dmp->dm_name; 1534 sip->dts_name = (const char *) 1535 dmp->dm_strtab.cts_data + symp->st_name; 1536 sip->dts_id = id; 1537 } 1538 return (0); 1539 } 1540 1541 if (dmp->dm_extern != NULL && 1542 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) { 1543 if (symp != &sym) { 1544 symp->st_name = (uintptr_t)idp->di_name; 1545 symp->st_info = 1546 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE); 1547 symp->st_other = 0; 1548 symp->st_shndx = SHN_UNDEF; 1549 symp->st_value = 0; 1550 symp->st_size = 1551 ctf_type_size(idp->di_ctfp, idp->di_type); 1552 } 1553 1554 if (sip != NULL) { 1555 sip->dts_object = dmp->dm_name; 1556 sip->dts_name = idp->di_name; 1557 sip->dts_id = idp->di_id; 1558 } 1559 1560 return (0); 1561 } 1562 } 1563 1564 return (dt_set_errno(dtp, EDT_NOSYM)); 1565 } 1566 1567 /* 1568 * Exported interface to look up a symbol by address. We return the GElf_Sym 1569 * and complete symbol information for the matching symbol. 1570 */ 1571 int 1572 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr, 1573 GElf_Sym *symp, dtrace_syminfo_t *sip) 1574 { 1575 dt_module_t *dmp; 1576 uint_t id; 1577 const dtrace_vector_t *v = dtp->dt_vector; 1578 1579 if (v != NULL) 1580 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip)); 1581 1582 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL; 1583 dmp = dt_list_next(dmp)) { 1584 if (addr - dmp->dm_text_va < dmp->dm_text_size || 1585 addr - dmp->dm_data_va < dmp->dm_data_size || 1586 addr - dmp->dm_bss_va < dmp->dm_bss_size) 1587 break; 1588 } 1589 1590 if (dmp == NULL) 1591 return (dt_set_errno(dtp, EDT_NOSYMADDR)); 1592 1593 if (dt_module_load(dtp, dmp) == -1) 1594 return (-1); /* dt_errno is set for us */ 1595 1596 if (symp != NULL) { 1597 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL) 1598 return (dt_set_errno(dtp, EDT_NOSYMADDR)); 1599 } 1600 1601 if (sip != NULL) { 1602 sip->dts_object = dmp->dm_name; 1603 1604 if (symp != NULL) { 1605 sip->dts_name = (const char *) 1606 dmp->dm_strtab.cts_data + symp->st_name; 1607 sip->dts_id = id; 1608 } else { 1609 sip->dts_name = NULL; 1610 sip->dts_id = 0; 1611 } 1612 } 1613 1614 return (0); 1615 } 1616 1617 int 1618 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name, 1619 dtrace_typeinfo_t *tip) 1620 { 1621 dtrace_typeinfo_t ti; 1622 dt_module_t *dmp; 1623 int found = 0; 1624 ctf_id_t id = CTF_ERR; // XXX: gcc 1625 uint_t n, i; 1626 int justone; 1627 ctf_file_t *fp = NULL; // XXX: gcc 1628 char *buf, *p, *q; 1629 1630 uint_t mask = 0; /* mask of dt_module flags to match */ 1631 uint_t bits = 0; /* flag bits that must be present */ 1632 1633 if (object != DTRACE_OBJ_EVERY && 1634 object != DTRACE_OBJ_KMODS && 1635 object != DTRACE_OBJ_UMODS) { 1636 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1637 return (-1); /* dt_errno is set for us */ 1638 1639 if (dt_module_load(dtp, dmp) == -1) 1640 return (-1); /* dt_errno is set for us */ 1641 n = 1; 1642 justone = 1; 1643 } else { 1644 if (object == DTRACE_OBJ_KMODS) 1645 mask = bits = DT_DM_KERNEL; 1646 else if (object == DTRACE_OBJ_UMODS) 1647 mask = DT_DM_KERNEL; 1648 1649 dmp = dt_list_next(&dtp->dt_modlist); 1650 n = dtp->dt_nmods; 1651 justone = 0; 1652 } 1653 1654 if (tip == NULL) 1655 tip = &ti; 1656 1657 for (; n > 0; n--, dmp = dt_list_next(dmp)) { 1658 if ((dmp->dm_flags & mask) != bits) 1659 continue; /* failed to match required attributes */ 1660 1661 /* 1662 * If we can't load the CTF container, continue on to the next 1663 * module. If our search was scoped to only one module then 1664 * return immediately leaving dt_errno unmodified. 1665 */ 1666 if (dt_module_hasctf(dtp, dmp) == 0) { 1667 if (justone) 1668 return (-1); 1669 continue; 1670 } 1671 1672 /* 1673 * Look up the type in the module's CTF container. If our 1674 * match is a forward declaration tag, save this choice in 1675 * 'tip' and keep going in the hope that we will locate the 1676 * underlying structure definition. Otherwise just return. 1677 */ 1678 if (dmp->dm_pid == 0) { 1679 id = ctf_lookup_by_name(dmp->dm_ctfp, name); 1680 fp = dmp->dm_ctfp; 1681 } else { 1682 if ((p = strchr(name, '`')) != NULL) { 1683 buf = strdup(name); 1684 if (buf == NULL) 1685 return (dt_set_errno(dtp, EDT_NOMEM)); 1686 p = strchr(buf, '`'); 1687 if ((q = strchr(p + 1, '`')) != NULL) 1688 p = q; 1689 *p = '\0'; 1690 fp = dt_module_getctflib(dtp, dmp, buf); 1691 if (fp == NULL || (id = ctf_lookup_by_name(fp, 1692 p + 1)) == CTF_ERR) 1693 id = CTF_ERR; 1694 free(buf); 1695 } else { 1696 for (i = 0; i < dmp->dm_nctflibs; i++) { 1697 fp = dmp->dm_libctfp[i]; 1698 id = ctf_lookup_by_name(fp, name); 1699 if (id != CTF_ERR) 1700 break; 1701 } 1702 } 1703 } 1704 if (id != CTF_ERR) { 1705 tip->dtt_object = dmp->dm_name; 1706 tip->dtt_ctfp = fp; 1707 tip->dtt_type = id; 1708 if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) != 1709 CTF_K_FORWARD) 1710 return (0); 1711 1712 found++; 1713 } 1714 } 1715 1716 if (found == 0) 1717 return (dt_set_errno(dtp, EDT_NOTYPE)); 1718 1719 return (0); 1720 } 1721 1722 int 1723 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp, 1724 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip) 1725 { 1726 dt_module_t *dmp; 1727 1728 tip->dtt_object = NULL; 1729 tip->dtt_ctfp = NULL; 1730 tip->dtt_type = CTF_ERR; 1731 tip->dtt_flags = 0; 1732 1733 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL) 1734 return (dt_set_errno(dtp, EDT_NOMOD)); 1735 1736 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) { 1737 dt_ident_t *idp = 1738 dt_idhash_lookup(dmp->dm_extern, sip->dts_name); 1739 1740 if (idp == NULL) 1741 return (dt_set_errno(dtp, EDT_NOSYM)); 1742 1743 tip->dtt_ctfp = idp->di_ctfp; 1744 tip->dtt_type = idp->di_type; 1745 1746 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) { 1747 if (dt_module_getctf(dtp, dmp) == NULL) 1748 return (-1); /* errno is set for us */ 1749 1750 tip->dtt_ctfp = dmp->dm_ctfp; 1751 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id); 1752 1753 if (tip->dtt_type == CTF_ERR) { 1754 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp); 1755 return (dt_set_errno(dtp, EDT_CTF)); 1756 } 1757 1758 } else { 1759 tip->dtt_ctfp = DT_FPTR_CTFP(dtp); 1760 tip->dtt_type = DT_FPTR_TYPE(dtp); 1761 } 1762 1763 tip->dtt_object = dmp->dm_name; 1764 return (0); 1765 } 1766 1767 static dtrace_objinfo_t * 1768 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto) 1769 { 1770 dto->dto_name = dmp->dm_name; 1771 dto->dto_file = dmp->dm_file; 1772 dto->dto_id = dmp->dm_modid; 1773 dto->dto_flags = 0; 1774 1775 if (dmp->dm_flags & DT_DM_KERNEL) 1776 dto->dto_flags |= DTRACE_OBJ_F_KERNEL; 1777 if (dmp->dm_flags & DT_DM_PRIMARY) 1778 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY; 1779 1780 dto->dto_text_va = dmp->dm_text_va; 1781 dto->dto_text_size = dmp->dm_text_size; 1782 dto->dto_data_va = dmp->dm_data_va; 1783 dto->dto_data_size = dmp->dm_data_size; 1784 dto->dto_bss_va = dmp->dm_bss_va; 1785 dto->dto_bss_size = dmp->dm_bss_size; 1786 1787 return (dto); 1788 } 1789 1790 int 1791 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data) 1792 { 1793 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist); 1794 dtrace_objinfo_t dto; 1795 int rv; 1796 1797 for (; dmp != NULL; dmp = dt_list_next(dmp)) { 1798 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0) 1799 return (rv); 1800 } 1801 1802 return (0); 1803 } 1804 1805 int 1806 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto) 1807 { 1808 dt_module_t *dmp; 1809 1810 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS || 1811 object == DTRACE_OBJ_UMODS || dto == NULL) 1812 return (dt_set_errno(dtp, EINVAL)); 1813 1814 if ((dmp = dt_module_from_object(dtp, object)) == NULL) 1815 return (-1); /* dt_errno is set for us */ 1816 1817 if (dt_module_load(dtp, dmp) == -1) 1818 return (-1); /* dt_errno is set for us */ 1819 1820 (void) dt_module_info(dmp, dto); 1821 return (0); 1822 } 1823