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.
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.
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]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 #include <sys/types.h>
31 #include <sys/modctl.h>
33 #include <sys/kobj_impl.h>
34 #include <sys/sysmacros.h>
38 #include <sys/param.h>
39 #include <sys/linker.h>
56 #include <libproc_compat.h>
59 #include <dt_strtab.h>
60 #include <dt_module.h>
63 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
66 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
68 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
71 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
74 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
75 dsp->ds_next = dmp->dm_symbuckets[h];
76 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
80 dt_module_syminit32(dt_module_t *dmp)
82 #if STT_NUM != (STT_TLS + 1)
83 #error "STT_NUM has grown. update dt_module_syminit32()"
86 Elf32_Sym *sym = dmp->dm_symtab.cts_data;
87 const char *base = dmp->dm_strtab.cts_data;
88 size_t ss_size = dmp->dm_strtab.cts_size;
89 uint_t i, n = dmp->dm_nsymelems;
92 #if defined(__FreeBSD__)
96 gelf_getehdr(dmp->dm_elf, &ehdr);
97 is_elf_obj = (ehdr.e_type == ET_REL);
100 for (i = 0; i < n; i++, sym++) {
101 const char *name = base + sym->st_name;
102 uchar_t type = ELF32_ST_TYPE(sym->st_info);
104 if (type >= STT_NUM || type == STT_SECTION)
105 continue; /* skip sections and unknown types */
107 if (sym->st_name == 0 || sym->st_name >= ss_size)
108 continue; /* skip null or invalid names */
110 if (sym->st_value != 0 &&
111 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
112 asrsv++; /* reserve space in the address map */
114 #if defined(__FreeBSD__)
115 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
116 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
117 sym->st_shndx < ehdr.e_shnum)
119 dmp->dm_sec_offsets[sym->st_shndx];
123 dt_module_symhash_insert(dmp, name, i);
130 dt_module_syminit64(dt_module_t *dmp)
132 #if STT_NUM != (STT_TLS + 1)
133 #error "STT_NUM has grown. update dt_module_syminit64()"
136 Elf64_Sym *sym = dmp->dm_symtab.cts_data;
137 const char *base = dmp->dm_strtab.cts_data;
138 size_t ss_size = dmp->dm_strtab.cts_size;
139 uint_t i, n = dmp->dm_nsymelems;
142 #if defined(__FreeBSD__)
146 gelf_getehdr(dmp->dm_elf, &ehdr);
147 is_elf_obj = (ehdr.e_type == ET_REL);
150 for (i = 0; i < n; i++, sym++) {
151 const char *name = base + sym->st_name;
152 uchar_t type = ELF64_ST_TYPE(sym->st_info);
154 if (type >= STT_NUM || type == STT_SECTION)
155 continue; /* skip sections and unknown types */
157 if (sym->st_name == 0 || sym->st_name >= ss_size)
158 continue; /* skip null or invalid names */
160 if (sym->st_value != 0 &&
161 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
162 asrsv++; /* reserve space in the address map */
163 #if defined(__FreeBSD__)
164 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
165 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
166 sym->st_shndx < ehdr.e_shnum)
168 dmp->dm_sec_offsets[sym->st_shndx];
172 dt_module_symhash_insert(dmp, name, i);
179 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
180 * symbols by value. If values are equal, we prefer the symbol that is
181 * non-zero sized, typed, not weak, or lexically first, in that order.
184 dt_module_symcomp32(const void *lp, const void *rp)
186 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
187 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
189 if (lhs->st_value != rhs->st_value)
190 return (lhs->st_value > rhs->st_value ? 1 : -1);
192 if ((lhs->st_size == 0) != (rhs->st_size == 0))
193 return (lhs->st_size == 0 ? 1 : -1);
195 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
196 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
197 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
199 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
200 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
201 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
203 return (strcmp(dt_module_strtab + lhs->st_name,
204 dt_module_strtab + rhs->st_name));
208 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
209 * symbols by value. If values are equal, we prefer the symbol that is
210 * non-zero sized, typed, not weak, or lexically first, in that order.
213 dt_module_symcomp64(const void *lp, const void *rp)
215 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
216 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
218 if (lhs->st_value != rhs->st_value)
219 return (lhs->st_value > rhs->st_value ? 1 : -1);
221 if ((lhs->st_size == 0) != (rhs->st_size == 0))
222 return (lhs->st_size == 0 ? 1 : -1);
224 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
225 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
226 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
228 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
229 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
230 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
232 return (strcmp(dt_module_strtab + lhs->st_name,
233 dt_module_strtab + rhs->st_name));
237 dt_module_symsort32(dt_module_t *dmp)
239 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
240 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
241 const dt_sym_t *dsp = dmp->dm_symchains + 1;
242 uint_t i, n = dmp->dm_symfree;
244 for (i = 1; i < n; i++, dsp++) {
245 Elf32_Sym *sym = symtab + dsp->ds_symid;
246 if (sym->st_value != 0 &&
247 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
251 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
252 assert(dmp->dm_aslen <= dmp->dm_asrsv);
254 dt_module_strtab = dmp->dm_strtab.cts_data;
255 qsort(dmp->dm_asmap, dmp->dm_aslen,
256 sizeof (Elf32_Sym *), dt_module_symcomp32);
257 dt_module_strtab = NULL;
261 dt_module_symsort64(dt_module_t *dmp)
263 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
264 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
265 const dt_sym_t *dsp = dmp->dm_symchains + 1;
266 uint_t i, n = dmp->dm_symfree;
268 for (i = 1; i < n; i++, dsp++) {
269 Elf64_Sym *sym = symtab + dsp->ds_symid;
270 if (sym->st_value != 0 &&
271 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
275 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
276 assert(dmp->dm_aslen <= dmp->dm_asrsv);
278 dt_module_strtab = dmp->dm_strtab.cts_data;
279 qsort(dmp->dm_asmap, dmp->dm_aslen,
280 sizeof (Elf64_Sym *), dt_module_symcomp64);
281 dt_module_strtab = NULL;
285 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
288 dst->st_name = src->st_name;
289 dst->st_info = src->st_info;
290 dst->st_other = src->st_other;
291 dst->st_shndx = src->st_shndx;
292 dst->st_value = src->st_value;
293 dst->st_size = src->st_size;
300 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
303 bcopy(src, dst, sizeof (GElf_Sym));
309 dt_module_symname32(dt_module_t *dmp, const char *name,
310 GElf_Sym *symp, uint_t *idp)
312 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
313 const char *strtab = dmp->dm_strtab.cts_data;
315 const Elf32_Sym *sym;
319 if (dmp->dm_nsymelems == 0)
322 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
324 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
325 dsp = &dmp->dm_symchains[i];
326 sym = symtab + dsp->ds_symid;
328 if (strcmp(name, strtab + sym->st_name) == 0) {
330 *idp = dsp->ds_symid;
331 return (dt_module_symgelf32(sym, symp));
339 dt_module_symname64(dt_module_t *dmp, const char *name,
340 GElf_Sym *symp, uint_t *idp)
342 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
343 const char *strtab = dmp->dm_strtab.cts_data;
345 const Elf64_Sym *sym;
349 if (dmp->dm_nsymelems == 0)
352 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
354 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
355 dsp = &dmp->dm_symchains[i];
356 sym = symtab + dsp->ds_symid;
358 if (strcmp(name, strtab + sym->st_name) == 0) {
360 *idp = dsp->ds_symid;
361 return (dt_module_symgelf64(sym, symp));
369 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
370 GElf_Sym *symp, uint_t *idp)
372 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
373 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
374 const Elf32_Sym *sym;
376 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
379 if (dmp->dm_aslen == 0)
382 while (hi - lo > 1) {
384 if (addr >= asmap[mid]->st_value)
390 i = addr < asmap[hi]->st_value ? lo : hi;
395 * If the previous entry has the same value, improve our choice. The
396 * order of equal-valued symbols is determined by the comparison func.
398 while (i-- != 0 && asmap[i]->st_value == v)
401 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
403 *idp = (uint_t)(sym - symtab);
404 return (dt_module_symgelf32(sym, symp));
411 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
412 GElf_Sym *symp, uint_t *idp)
414 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
415 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
416 const Elf64_Sym *sym;
418 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
421 if (dmp->dm_aslen == 0)
424 while (hi - lo > 1) {
426 if (addr >= asmap[mid]->st_value)
432 i = addr < asmap[hi]->st_value ? lo : hi;
437 * If the previous entry has the same value, improve our choice. The
438 * order of equal-valued symbols is determined by the comparison func.
440 while (i-- != 0 && asmap[i]->st_value == v)
443 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
445 *idp = (uint_t)(sym - symtab);
446 return (dt_module_symgelf64(sym, symp));
452 static const dt_modops_t dt_modops_32 = {
459 static const dt_modops_t dt_modops_64 = {
467 dt_module_create(dtrace_hdl_t *dtp, const char *name)
472 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
475 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
476 if (strcmp(dmp->dm_name, name) == 0)
480 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
481 return (NULL); /* caller must handle allocation failure */
483 bzero(dmp, sizeof (dt_module_t));
484 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
485 dt_list_append(&dtp->dt_modlist, dmp);
486 dmp->dm_next = dtp->dt_mods[h];
487 dtp->dt_mods[h] = dmp;
490 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
491 dmp->dm_ops = &dt_modops_64;
493 dmp->dm_ops = &dt_modops_32;
496 * Modules for userland processes are special. They always refer to a
497 * specific process and have a copy of their CTF data from a specific
498 * instant in time. Any dt_module_t that begins with 'pid' is a module
499 * for a specific process, much like how any probe description that
500 * begins with 'pid' is special. pid123 refers to process 123. A module
501 * that is just 'pid' refers specifically to pid$target. This is
502 * generally done as D does not currently allow for macros to be
503 * evaluated when working with types.
505 if (strncmp(dmp->dm_name, "pid", 3) == 0) {
507 if (dmp->dm_name[3] == '\0') {
508 idp = dt_idhash_lookup(dtp->dt_macros, "target");
509 if (idp != NULL && idp->di_id != 0)
510 dmp->dm_pid = idp->di_id;
512 pid = strtol(dmp->dm_name + 3, &eptr, 10);
513 if (errno == 0 && *eptr == '\0')
514 dmp->dm_pid = (pid_t)pid;
516 dt_dprintf("encountered malformed pid "
517 "module: %s\n", dmp->dm_name);
525 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
527 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
530 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
531 if (strcmp(dmp->dm_name, name) == 0)
540 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
542 return (ctfp ? ctf_getspecific(ctfp) : NULL);
546 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
554 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
555 return (dt_set_errno(dtp, EDT_NOTLOADED));
557 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
558 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
559 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
560 continue; /* skip any malformed sections */
562 if (sh.sh_type == ctsp->cts_type &&
563 sh.sh_entsize == ctsp->cts_entsize &&
564 strcmp(s, ctsp->cts_name) == 0)
565 break; /* section matches specification */
569 * If the section isn't found, return success but leave cts_data set
570 * to NULL and cts_size set to zero for our caller.
572 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
576 ctsp->cts_data = dp->d_buf;
578 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
580 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
582 ctsp->cts_size = dp->d_size;
584 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
585 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
590 typedef struct dt_module_cb_arg {
591 struct ps_prochandle *dpa_proc;
592 dtrace_hdl_t *dpa_dtp;
593 dt_module_t *dpa_dmp;
595 } dt_module_cb_arg_t;
599 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)
602 dt_module_cb_arg_t *dcp = arg;
604 /* Try to grab a ctf container if it exists */
605 fp = Pname_to_ctf(dcp->dpa_proc, obj);
613 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)
616 char buf[MAXPATHLEN], *p;
617 dt_module_cb_arg_t *dcp = arg;
618 int count = dcp->dpa_count;
621 fp = Pname_to_ctf(dcp->dpa_proc, obj);
627 dcp->dpa_dmp->dm_libctfp[count] = fp;
629 * While it'd be nice to simply use objname here, because of our prior
630 * actions we'll always get a resolved object name to its on disk file.
631 * Like the pid provider, we need to tell a bit of a lie here. The type
632 * that the user thinks of is in terms of the libraries they requested,
633 * eg. libc.so.1, they don't care about the fact that it's
636 (void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));
637 if ((p = strrchr(buf, '/')) == NULL)
643 * If for some reason we can't find a link map id for this module, which
644 * would be really quite weird. We instead just say the link map id is
647 if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)
651 dcp->dpa_dmp->dm_libctfn[count] = strdup(p);
653 (void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],
655 if (dcp->dpa_dmp->dm_libctfn[count] == NULL)
657 ctf_setspecific(fp, dcp->dpa_dmp);
663 * We've been asked to load data that belongs to another process. As such we're
664 * going to pgrab it at this instant, load everything that we might ever care
665 * about, and then drive on. The reason for this is that the process that we're
666 * interested in might be changing. As long as we have grabbed it, then this
667 * can't be a problem for us.
669 * For now, we're actually going to punt on most things and just try to get CTF
670 * data, nothing else. Basically this is only useful as a source of type
671 * information, we can't go and do the stacktrace lookups, etc.
674 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)
676 struct ps_prochandle *p;
677 dt_module_cb_arg_t arg;
680 * Note that on success we do not release this hold. We must hold this
683 p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
685 dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);
686 return (dt_set_errno(dtp, EDT_CANTLOAD));
688 dt_proc_lock(dtp, p);
694 if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {
695 dt_dprintf("failed to iterate objects\n");
696 dt_proc_release(dtp, p);
697 return (dt_set_errno(dtp, EDT_CANTLOAD));
700 if (arg.dpa_count == 0) {
701 dt_dprintf("no ctf data present\n");
702 dt_proc_unlock(dtp, p);
703 dt_proc_release(dtp, p);
704 return (dt_set_errno(dtp, EDT_CANTLOAD));
707 dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count);
708 if (dmp->dm_libctfp == NULL) {
709 dt_proc_unlock(dtp, p);
710 dt_proc_release(dtp, p);
711 return (dt_set_errno(dtp, EDT_NOMEM));
713 bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count);
715 dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count);
716 if (dmp->dm_libctfn == NULL) {
717 free(dmp->dm_libctfp);
718 dt_proc_unlock(dtp, p);
719 dt_proc_release(dtp, p);
720 return (dt_set_errno(dtp, EDT_NOMEM));
722 bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count);
724 dmp->dm_nctflibs = arg.dpa_count;
727 if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
728 dt_proc_unlock(dtp, p);
729 dt_module_unload(dtp, dmp);
730 dt_proc_release(dtp, p);
731 return (dt_set_errno(dtp, EDT_CANTLOAD));
733 assert(arg.dpa_count == dmp->dm_nctflibs);
734 dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
737 dt_proc_unlock(dtp, p);
738 dt_proc_release(dtp, p);
739 dmp->dm_flags |= DT_DM_LOADED;
745 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
747 if (dmp->dm_flags & DT_DM_LOADED)
748 return (0); /* module is already loaded */
750 if (dmp->dm_pid != 0)
751 return (dt_module_load_proc(dtp, dmp));
753 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
754 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
755 dmp->dm_ctdata.cts_flags = 0;
756 dmp->dm_ctdata.cts_data = NULL;
757 dmp->dm_ctdata.cts_size = 0;
758 dmp->dm_ctdata.cts_entsize = 0;
759 dmp->dm_ctdata.cts_offset = 0;
761 dmp->dm_symtab.cts_name = ".symtab";
762 dmp->dm_symtab.cts_type = SHT_SYMTAB;
763 dmp->dm_symtab.cts_flags = 0;
764 dmp->dm_symtab.cts_data = NULL;
765 dmp->dm_symtab.cts_size = 0;
766 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
767 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
768 dmp->dm_symtab.cts_offset = 0;
770 dmp->dm_strtab.cts_name = ".strtab";
771 dmp->dm_strtab.cts_type = SHT_STRTAB;
772 dmp->dm_strtab.cts_flags = 0;
773 dmp->dm_strtab.cts_data = NULL;
774 dmp->dm_strtab.cts_size = 0;
775 dmp->dm_strtab.cts_entsize = 0;
776 dmp->dm_strtab.cts_offset = 0;
779 * Attempt to load the module's CTF section, symbol table section, and
780 * string table section. Note that modules may not contain CTF data:
781 * this will result in a successful load_sect but data of size zero.
782 * We will then fail if dt_module_getctf() is called, as shown below.
784 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
785 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
786 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
787 dt_module_unload(dtp, dmp);
788 return (-1); /* dt_errno is set for us */
792 * Allocate the hash chains and hash buckets for symbol name lookup.
793 * This is relatively simple since the symbol table is of fixed size
794 * and is known in advance. We allocate one extra element since we
795 * use element indices instead of pointers and zero is our sentinel.
798 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
800 dmp->dm_nsymbuckets = _dtrace_strbuckets;
801 dmp->dm_symfree = 1; /* first free element is index 1 */
803 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
804 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
806 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
807 dt_module_unload(dtp, dmp);
808 return (dt_set_errno(dtp, EDT_NOMEM));
811 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
812 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
815 * Iterate over the symbol table data buffer and insert each symbol
816 * name into the name hash if the name and type are valid. Then
817 * allocate the address map, fill it in, and sort it.
819 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
821 dt_dprintf("hashed %s [%s] (%u symbols)\n",
822 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
824 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
825 dt_module_unload(dtp, dmp);
826 return (dt_set_errno(dtp, EDT_NOMEM));
829 dmp->dm_ops->do_symsort(dmp);
831 dt_dprintf("sorted %s [%s] (%u symbols)\n",
832 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
834 dmp->dm_flags |= DT_DM_LOADED;
839 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
841 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
843 return (dt_module_getctf(dtp, dmp) != NULL);
847 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
854 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
855 return (dmp->dm_ctfp);
857 if (dmp->dm_ops == &dt_modops_64)
858 model = CTF_MODEL_LP64;
860 model = CTF_MODEL_ILP32;
863 * If the data model of the module does not match our program data
864 * model, then do not permit CTF from this module to be opened and
865 * returned to the compiler. If we support mixed data models in the
866 * future for combined kernel/user tracing, this can be removed.
868 if (dtp->dt_conf.dtc_ctfmodel != model) {
869 (void) dt_set_errno(dtp, EDT_DATAMODEL);
873 if (dmp->dm_ctdata.cts_size == 0) {
874 (void) dt_set_errno(dtp, EDT_NOCTF);
878 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
879 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
881 if (dmp->dm_ctfp == NULL) {
882 (void) dt_set_errno(dtp, EDT_CTF);
886 (void) ctf_setmodel(dmp->dm_ctfp, model);
887 ctf_setspecific(dmp->dm_ctfp, dmp);
889 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
890 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
891 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
893 (void) dt_set_errno(dtp, EDT_NOMEM);
897 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
898 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
899 (void) dt_set_errno(dtp, EDT_CTF);
904 dt_dprintf("loaded CTF container for %s (%p)\n",
905 dmp->dm_name, (void *)dmp->dm_ctfp);
907 return (dmp->dm_ctfp);
910 ctf_close(dmp->dm_ctfp);
917 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
921 ctf_close(dmp->dm_ctfp);
925 if (dmp->dm_ctdata.cts_data != NULL) {
926 free(dmp->dm_ctdata.cts_data);
928 if (dmp->dm_symtab.cts_data != NULL) {
929 free(dmp->dm_symtab.cts_data);
931 if (dmp->dm_strtab.cts_data != NULL) {
932 free(dmp->dm_strtab.cts_data);
936 if (dmp->dm_libctfp != NULL) {
937 for (i = 0; i < dmp->dm_nctflibs; i++) {
938 ctf_close(dmp->dm_libctfp[i]);
939 free(dmp->dm_libctfn[i]);
941 free(dmp->dm_libctfp);
942 free(dmp->dm_libctfn);
943 dmp->dm_libctfp = NULL;
944 dmp->dm_nctflibs = 0;
947 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
948 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
949 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
951 if (dmp->dm_symbuckets != NULL) {
952 free(dmp->dm_symbuckets);
953 dmp->dm_symbuckets = NULL;
956 if (dmp->dm_symchains != NULL) {
957 free(dmp->dm_symchains);
958 dmp->dm_symchains = NULL;
961 if (dmp->dm_asmap != NULL) {
963 dmp->dm_asmap = NULL;
965 #if defined(__FreeBSD__)
966 if (dmp->dm_sec_offsets != NULL) {
967 free(dmp->dm_sec_offsets);
968 dmp->dm_sec_offsets = NULL;
972 dmp->dm_nsymbuckets = 0;
973 dmp->dm_nsymelems = 0;
978 dmp->dm_text_size = 0;
980 dmp->dm_data_size = 0;
982 dmp->dm_bss_size = 0;
984 if (dmp->dm_extern != NULL) {
985 dt_idhash_destroy(dmp->dm_extern);
986 dmp->dm_extern = NULL;
989 (void) elf_end(dmp->dm_elf);
994 dmp->dm_flags &= ~DT_DM_LOADED;
998 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
1000 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
1001 dt_module_t **dmpp = &dtp->dt_mods[h];
1003 dt_list_delete(&dtp->dt_modlist, dmp);
1004 assert(dtp->dt_nmods != 0);
1008 * Now remove this module from its hash chain. We expect to always
1009 * find the module on its hash chain, so in this loop we assert that
1010 * we don't run off the end of the list.
1012 while (*dmpp != dmp) {
1013 dmpp = &((*dmpp)->dm_next);
1014 assert(*dmpp != NULL);
1017 *dmpp = dmp->dm_next;
1019 dt_module_unload(dtp, dmp);
1024 * Insert a new external symbol reference into the specified module. The new
1025 * symbol will be marked as undefined and is assigned a symbol index beyond
1026 * any existing cached symbols from this module. We use the ident's di_data
1027 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
1030 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
1031 const char *name, const dtrace_typeinfo_t *tip)
1033 dtrace_syminfo_t *sip;
1037 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
1038 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
1039 (void) dt_set_errno(dtp, EDT_NOMEM);
1043 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
1044 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
1048 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
1049 (void) dt_set_errno(dtp, EDT_NOMEM);
1053 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
1054 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1057 (void) dt_set_errno(dtp, EDT_NOMEM);
1062 sip->dts_object = dmp->dm_name;
1063 sip->dts_name = idp->di_name;
1064 sip->dts_id = idp->di_id;
1067 idp->di_ctfp = tip->dtt_ctfp;
1068 idp->di_type = tip->dtt_type;
1074 dt_module_modelname(dt_module_t *dmp)
1076 if (dmp->dm_ops == &dt_modops_64)
1084 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1088 for (i = 0; i < dmp->dm_nctflibs; i++) {
1089 if (dmp->dm_libctfp[i] == fp)
1098 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1102 for (i = 0; i < dmp->dm_nctflibs; i++) {
1103 if (strcmp(dmp->dm_libctfn[i], name) == 0)
1104 return (dmp->dm_libctfp[i]);
1111 * Update our module cache by adding an entry for the specified module 'name'.
1112 * We create the dt_module_t and populate it using /system/object/<name>/.
1114 * On FreeBSD, the module name is passed as the full module file name,
1115 * including the path.
1119 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1121 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
1124 char fname[MAXPATHLEN];
1136 (void) snprintf(fname, sizeof (fname),
1137 "%s/%s/object", OBJFS_ROOT, name);
1141 char name[MAXPATHLEN];
1142 uintptr_t mapbase, alignmask;
1146 (void) strlcpy(name, k_stat->name, sizeof(name));
1147 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
1150 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1151 (dmp = dt_module_create(dtp, name)) == NULL) {
1152 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1158 * Since the module can unload out from under us (and /system/object
1159 * will return ENOENT), tell libelf to cook the entire file now and
1160 * then close the underlying file descriptor immediately. If this
1161 * succeeds, we know that we can continue safely using dmp->dm_elf.
1163 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1164 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1167 if (dmp->dm_elf == NULL || err == -1 ||
1168 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1169 dt_dprintf("failed to load %s: %s\n",
1170 fname, elf_errmsg(elf_errno()));
1171 dt_module_destroy(dtp, dmp);
1175 switch (gelf_getclass(dmp->dm_elf)) {
1177 dmp->dm_ops = &dt_modops_32;
1181 dmp->dm_ops = &dt_modops_64;
1185 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1186 dt_module_destroy(dtp, dmp);
1189 #if defined(__FreeBSD__)
1190 mapbase = (uintptr_t)k_stat->address;
1191 gelf_getehdr(dmp->dm_elf, &ehdr);
1192 is_elf_obj = (ehdr.e_type == ET_REL);
1194 dmp->dm_sec_offsets =
1195 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
1196 if (dmp->dm_sec_offsets == NULL) {
1197 dt_dprintf("failed to allocate memory\n");
1198 dt_module_destroy(dtp, dmp);
1204 * Iterate over the section headers locating various sections of
1205 * interest and use their attributes to flesh out the dt_module_t.
1207 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1208 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1209 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1210 continue; /* skip any malformed sections */
1211 #if defined(__FreeBSD__)
1212 if (sh.sh_size == 0)
1214 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
1215 alignmask = sh.sh_addralign - 1;
1216 mapbase += alignmask;
1217 mapbase &= ~alignmask;
1218 sh.sh_addr = mapbase;
1220 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
1221 mapbase += sh.sh_size;
1224 if (strcmp(s, ".text") == 0) {
1225 dmp->dm_text_size = sh.sh_size;
1226 dmp->dm_text_va = sh.sh_addr;
1227 } else if (strcmp(s, ".data") == 0) {
1228 dmp->dm_data_size = sh.sh_size;
1229 dmp->dm_data_va = sh.sh_addr;
1230 } else if (strcmp(s, ".bss") == 0) {
1231 dmp->dm_bss_size = sh.sh_size;
1232 dmp->dm_bss_va = sh.sh_addr;
1233 } else if (strcmp(s, ".info") == 0 &&
1234 (dp = elf_getdata(sp, NULL)) != NULL) {
1235 bcopy(dp->d_buf, &dmp->dm_info,
1236 MIN(sh.sh_size, sizeof (dmp->dm_info)));
1237 } else if (strcmp(s, ".filename") == 0 &&
1238 (dp = elf_getdata(sp, NULL)) != NULL) {
1239 (void) strlcpy(dmp->dm_file,
1240 dp->d_buf, sizeof (dmp->dm_file));
1244 dmp->dm_flags |= DT_DM_KERNEL;
1246 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1249 * Include .rodata and special sections into .text.
1250 * This depends on default section layout produced by GNU ld
1251 * for ELF objects and libraries:
1252 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1254 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1255 #if defined(__i386__)
1257 * Find the first load section and figure out the relocation
1258 * offset for the symbols. The kernel module will not need
1259 * relocation, but the kernel linker modules will.
1261 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1262 if (ph.p_type == PT_LOAD) {
1263 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1268 #endif /* illumos */
1270 if (dmp->dm_info.objfs_info_primary)
1271 dmp->dm_flags |= DT_DM_PRIMARY;
1273 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1274 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1278 * Unload all the loaded modules and then refresh the module cache with the
1279 * latest list of loaded modules and their address ranges.
1282 dtrace_update(dtrace_hdl_t *dtp)
1286 #if defined(__FreeBSD__)
1290 for (dmp = dt_list_next(&dtp->dt_modlist);
1291 dmp != NULL; dmp = dt_list_next(dmp))
1292 dt_module_unload(dtp, dmp);
1296 * Open /system/object and attempt to create a libdtrace module for
1297 * each kernel module that is loaded on the current system.
1299 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1300 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1303 while ((dp = readdir(dirp)) != NULL) {
1304 if (dp->d_name[0] != '.')
1305 dt_module_update(dtp, dp->d_name);
1308 (void) closedir(dirp);
1310 #elif defined(__FreeBSD__)
1312 * Use FreeBSD's kernel loader interface to discover what kernel
1313 * modules are loaded and create a libdtrace module for each one.
1315 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1316 struct kld_file_stat k_stat;
1317 k_stat.version = sizeof(k_stat);
1318 if (kldstat(fileid, &k_stat) == 0)
1319 dt_module_update(dtp, &k_stat);
1324 * Look up all the macro identifiers and set di_id to the latest value.
1325 * This code collaborates with dt_lex.l on the use of di_id. We will
1326 * need to implement something fancier if we need to support non-ints.
1328 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1329 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1330 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1331 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1332 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1333 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1335 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1337 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1339 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1341 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1344 * Cache the pointers to the modules representing the base executable
1345 * and the run-time linker in the dtrace client handle. Note that on
1346 * x86 krtld is folded into unix, so if we don't find it, use unix
1349 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1350 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1351 if (dtp->dt_rtld == NULL)
1352 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1355 * If this is the first time we are initializing the module list,
1356 * remove the module for genunix from the module list and then move it
1357 * to the front of the module list. We do this so that type and symbol
1358 * queries encounter genunix and thereby optimize for the common case
1359 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1361 if (dtp->dt_exec != NULL &&
1362 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1363 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1364 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1368 static dt_module_t *
1369 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1371 int err = EDT_NOMOD;
1374 switch ((uintptr_t)object) {
1375 case (uintptr_t)DTRACE_OBJ_EXEC:
1378 case (uintptr_t)DTRACE_OBJ_RTLD:
1381 case (uintptr_t)DTRACE_OBJ_CDEFS:
1382 dmp = dtp->dt_cdefs;
1384 case (uintptr_t)DTRACE_OBJ_DDEFS:
1385 dmp = dtp->dt_ddefs;
1388 dmp = dt_module_create(dtp, object);
1393 (void) dt_set_errno(dtp, err);
1399 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1400 * complete symbol information for the matching symbol.
1403 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1404 GElf_Sym *symp, dtrace_syminfo_t *sip)
1411 uint_t mask = 0; /* mask of dt_module flags to match */
1412 uint_t bits = 0; /* flag bits that must be present */
1414 if (object != DTRACE_OBJ_EVERY &&
1415 object != DTRACE_OBJ_KMODS &&
1416 object != DTRACE_OBJ_UMODS) {
1417 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1418 return (-1); /* dt_errno is set for us */
1420 if (dt_module_load(dtp, dmp) == -1)
1421 return (-1); /* dt_errno is set for us */
1425 if (object == DTRACE_OBJ_KMODS)
1426 mask = bits = DT_DM_KERNEL;
1427 else if (object == DTRACE_OBJ_UMODS)
1428 mask = DT_DM_KERNEL;
1430 dmp = dt_list_next(&dtp->dt_modlist);
1437 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1438 if ((dmp->dm_flags & mask) != bits)
1439 continue; /* failed to match required attributes */
1441 if (dt_module_load(dtp, dmp) == -1)
1442 continue; /* failed to load symbol table */
1444 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1446 sip->dts_object = dmp->dm_name;
1447 sip->dts_name = (const char *)
1448 dmp->dm_strtab.cts_data + symp->st_name;
1454 if (dmp->dm_extern != NULL &&
1455 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1457 symp->st_name = (uintptr_t)idp->di_name;
1459 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1461 symp->st_shndx = SHN_UNDEF;
1464 ctf_type_size(idp->di_ctfp, idp->di_type);
1468 sip->dts_object = dmp->dm_name;
1469 sip->dts_name = idp->di_name;
1470 sip->dts_id = idp->di_id;
1477 return (dt_set_errno(dtp, EDT_NOSYM));
1481 * Exported interface to look up a symbol by address. We return the GElf_Sym
1482 * and complete symbol information for the matching symbol.
1485 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1486 GElf_Sym *symp, dtrace_syminfo_t *sip)
1490 const dtrace_vector_t *v = dtp->dt_vector;
1493 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1495 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1496 dmp = dt_list_next(dmp)) {
1497 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1498 addr - dmp->dm_data_va < dmp->dm_data_size ||
1499 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1504 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1506 if (dt_module_load(dtp, dmp) == -1)
1507 return (-1); /* dt_errno is set for us */
1510 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1511 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1515 sip->dts_object = dmp->dm_name;
1518 sip->dts_name = (const char *)
1519 dmp->dm_strtab.cts_data + symp->st_name;
1522 sip->dts_name = NULL;
1531 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1532 dtrace_typeinfo_t *tip)
1534 dtrace_typeinfo_t ti;
1543 uint_t mask = 0; /* mask of dt_module flags to match */
1544 uint_t bits = 0; /* flag bits that must be present */
1546 if (object != DTRACE_OBJ_EVERY &&
1547 object != DTRACE_OBJ_KMODS &&
1548 object != DTRACE_OBJ_UMODS) {
1549 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1550 return (-1); /* dt_errno is set for us */
1552 if (dt_module_load(dtp, dmp) == -1)
1553 return (-1); /* dt_errno is set for us */
1557 if (object == DTRACE_OBJ_KMODS)
1558 mask = bits = DT_DM_KERNEL;
1559 else if (object == DTRACE_OBJ_UMODS)
1560 mask = DT_DM_KERNEL;
1562 dmp = dt_list_next(&dtp->dt_modlist);
1570 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1571 if ((dmp->dm_flags & mask) != bits)
1572 continue; /* failed to match required attributes */
1575 * If we can't load the CTF container, continue on to the next
1576 * module. If our search was scoped to only one module then
1577 * return immediately leaving dt_errno unmodified.
1579 if (dt_module_hasctf(dtp, dmp) == 0) {
1586 * Look up the type in the module's CTF container. If our
1587 * match is a forward declaration tag, save this choice in
1588 * 'tip' and keep going in the hope that we will locate the
1589 * underlying structure definition. Otherwise just return.
1591 if (dmp->dm_pid == 0) {
1592 id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1595 if ((p = strchr(name, '`')) != NULL) {
1598 return (dt_set_errno(dtp, EDT_NOMEM));
1599 p = strchr(buf, '`');
1600 if ((q = strchr(p + 1, '`')) != NULL)
1603 fp = dt_module_getctflib(dtp, dmp, buf);
1604 if (fp == NULL || (id = ctf_lookup_by_name(fp,
1609 for (i = 0; i < dmp->dm_nctflibs; i++) {
1610 fp = dmp->dm_libctfp[i];
1611 id = ctf_lookup_by_name(fp, name);
1617 if (id != CTF_ERR) {
1618 tip->dtt_object = dmp->dm_name;
1621 if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=
1630 return (dt_set_errno(dtp, EDT_NOTYPE));
1636 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1637 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1641 tip->dtt_object = NULL;
1642 tip->dtt_ctfp = NULL;
1643 tip->dtt_type = CTF_ERR;
1646 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1647 return (dt_set_errno(dtp, EDT_NOMOD));
1649 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1651 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1654 return (dt_set_errno(dtp, EDT_NOSYM));
1656 tip->dtt_ctfp = idp->di_ctfp;
1657 tip->dtt_type = idp->di_type;
1659 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1660 if (dt_module_getctf(dtp, dmp) == NULL)
1661 return (-1); /* errno is set for us */
1663 tip->dtt_ctfp = dmp->dm_ctfp;
1664 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1666 if (tip->dtt_type == CTF_ERR) {
1667 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1668 return (dt_set_errno(dtp, EDT_CTF));
1672 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1673 tip->dtt_type = DT_FPTR_TYPE(dtp);
1676 tip->dtt_object = dmp->dm_name;
1680 static dtrace_objinfo_t *
1681 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1683 dto->dto_name = dmp->dm_name;
1684 dto->dto_file = dmp->dm_file;
1685 dto->dto_id = dmp->dm_modid;
1688 if (dmp->dm_flags & DT_DM_KERNEL)
1689 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1690 if (dmp->dm_flags & DT_DM_PRIMARY)
1691 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1693 dto->dto_text_va = dmp->dm_text_va;
1694 dto->dto_text_size = dmp->dm_text_size;
1695 dto->dto_data_va = dmp->dm_data_va;
1696 dto->dto_data_size = dmp->dm_data_size;
1697 dto->dto_bss_va = dmp->dm_bss_va;
1698 dto->dto_bss_size = dmp->dm_bss_size;
1704 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1706 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1707 dtrace_objinfo_t dto;
1710 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1711 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1719 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1723 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1724 object == DTRACE_OBJ_UMODS || dto == NULL)
1725 return (dt_set_errno(dtp, EINVAL));
1727 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1728 return (-1); /* dt_errno is set for us */
1730 if (dt_module_load(dtp, dmp) == -1)
1731 return (-1); /* dt_errno is set for us */
1733 (void) dt_module_info(dmp, dto);