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.
27 * Copyright (c) 2016, Pedro Giffuni. All rights reserved.
30 #include <sys/types.h>
32 #include <sys/modctl.h>
34 #include <sys/kobj_impl.h>
35 #include <sys/sysmacros.h>
39 #include <sys/param.h>
40 #include <sys/linker.h>
57 #include <libproc_compat.h>
60 #include <dt_strtab.h>
61 #include <dt_module.h>
64 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
67 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
69 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
72 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
75 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
76 dsp->ds_next = dmp->dm_symbuckets[h];
77 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
81 dt_module_syminit32(dt_module_t *dmp)
83 #if STT_NUM != (STT_TLS + 1)
84 #error "STT_NUM has grown. update dt_module_syminit32()"
87 Elf32_Sym *sym = dmp->dm_symtab.cts_data;
88 const char *base = dmp->dm_strtab.cts_data;
89 size_t ss_size = dmp->dm_strtab.cts_size;
90 uint_t i, n = dmp->dm_nsymelems;
93 #if defined(__FreeBSD__)
97 gelf_getehdr(dmp->dm_elf, &ehdr);
98 is_elf_obj = (ehdr.e_type == ET_REL);
101 for (i = 0; i < n; i++, sym++) {
102 const char *name = base + sym->st_name;
103 uchar_t type = ELF32_ST_TYPE(sym->st_info);
105 if (type >= STT_NUM || type == STT_SECTION)
106 continue; /* skip sections and unknown types */
108 if (sym->st_name == 0 || sym->st_name >= ss_size)
109 continue; /* skip null or invalid names */
111 if (sym->st_value != 0 &&
112 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
113 asrsv++; /* reserve space in the address map */
115 #if defined(__FreeBSD__)
116 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
117 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
118 sym->st_shndx < ehdr.e_shnum)
120 dmp->dm_sec_offsets[sym->st_shndx];
124 dt_module_symhash_insert(dmp, name, i);
131 dt_module_syminit64(dt_module_t *dmp)
133 #if STT_NUM != (STT_TLS + 1)
134 #error "STT_NUM has grown. update dt_module_syminit64()"
137 Elf64_Sym *sym = dmp->dm_symtab.cts_data;
138 const char *base = dmp->dm_strtab.cts_data;
139 size_t ss_size = dmp->dm_strtab.cts_size;
140 uint_t i, n = dmp->dm_nsymelems;
143 #if defined(__FreeBSD__)
147 gelf_getehdr(dmp->dm_elf, &ehdr);
148 is_elf_obj = (ehdr.e_type == ET_REL);
151 for (i = 0; i < n; i++, sym++) {
152 const char *name = base + sym->st_name;
153 uchar_t type = ELF64_ST_TYPE(sym->st_info);
155 if (type >= STT_NUM || type == STT_SECTION)
156 continue; /* skip sections and unknown types */
158 if (sym->st_name == 0 || sym->st_name >= ss_size)
159 continue; /* skip null or invalid names */
161 if (sym->st_value != 0 &&
162 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size)) {
163 asrsv++; /* reserve space in the address map */
164 #if defined(__FreeBSD__)
165 sym->st_value += (Elf_Addr) dmp->dm_reloc_offset;
166 if (is_elf_obj && sym->st_shndx != SHN_UNDEF &&
167 sym->st_shndx < ehdr.e_shnum)
169 dmp->dm_sec_offsets[sym->st_shndx];
173 dt_module_symhash_insert(dmp, name, i);
180 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
181 * symbols by value. If values are equal, we prefer the symbol that is
182 * non-zero sized, typed, not weak, or lexically first, in that order.
185 dt_module_symcomp32(const void *lp, const void *rp)
187 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
188 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
190 if (lhs->st_value != rhs->st_value)
191 return (lhs->st_value > rhs->st_value ? 1 : -1);
193 if ((lhs->st_size == 0) != (rhs->st_size == 0))
194 return (lhs->st_size == 0 ? 1 : -1);
196 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
197 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
198 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
200 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
201 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
202 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
204 return (strcmp(dt_module_strtab + lhs->st_name,
205 dt_module_strtab + rhs->st_name));
209 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
210 * symbols by value. If values are equal, we prefer the symbol that is
211 * non-zero sized, typed, not weak, or lexically first, in that order.
214 dt_module_symcomp64(const void *lp, const void *rp)
216 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
217 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
219 if (lhs->st_value != rhs->st_value)
220 return (lhs->st_value > rhs->st_value ? 1 : -1);
222 if ((lhs->st_size == 0) != (rhs->st_size == 0))
223 return (lhs->st_size == 0 ? 1 : -1);
225 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
226 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
227 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
229 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
230 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
231 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
233 return (strcmp(dt_module_strtab + lhs->st_name,
234 dt_module_strtab + rhs->st_name));
238 dt_module_symsort32(dt_module_t *dmp)
240 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
241 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
242 const dt_sym_t *dsp = dmp->dm_symchains + 1;
243 uint_t i, n = dmp->dm_symfree;
245 for (i = 1; i < n; i++, dsp++) {
246 Elf32_Sym *sym = symtab + dsp->ds_symid;
247 if (sym->st_value != 0 &&
248 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
252 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
253 assert(dmp->dm_aslen <= dmp->dm_asrsv);
255 dt_module_strtab = dmp->dm_strtab.cts_data;
256 qsort(dmp->dm_asmap, dmp->dm_aslen,
257 sizeof (Elf32_Sym *), dt_module_symcomp32);
258 dt_module_strtab = NULL;
262 dt_module_symsort64(dt_module_t *dmp)
264 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
265 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
266 const dt_sym_t *dsp = dmp->dm_symchains + 1;
267 uint_t i, n = dmp->dm_symfree;
269 for (i = 1; i < n; i++, dsp++) {
270 Elf64_Sym *sym = symtab + dsp->ds_symid;
271 if (sym->st_value != 0 &&
272 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
276 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
277 assert(dmp->dm_aslen <= dmp->dm_asrsv);
279 dt_module_strtab = dmp->dm_strtab.cts_data;
280 qsort(dmp->dm_asmap, dmp->dm_aslen,
281 sizeof (Elf64_Sym *), dt_module_symcomp64);
282 dt_module_strtab = NULL;
286 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
289 dst->st_name = src->st_name;
290 dst->st_info = src->st_info;
291 dst->st_other = src->st_other;
292 dst->st_shndx = src->st_shndx;
293 dst->st_value = src->st_value;
294 dst->st_size = src->st_size;
301 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
304 bcopy(src, dst, sizeof (GElf_Sym));
310 dt_module_symname32(dt_module_t *dmp, const char *name,
311 GElf_Sym *symp, uint_t *idp)
313 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
314 const char *strtab = dmp->dm_strtab.cts_data;
316 const Elf32_Sym *sym;
320 if (dmp->dm_nsymelems == 0)
323 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
325 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
326 dsp = &dmp->dm_symchains[i];
327 sym = symtab + dsp->ds_symid;
329 if (strcmp(name, strtab + sym->st_name) == 0) {
331 *idp = dsp->ds_symid;
332 return (dt_module_symgelf32(sym, symp));
340 dt_module_symname64(dt_module_t *dmp, const char *name,
341 GElf_Sym *symp, uint_t *idp)
343 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
344 const char *strtab = dmp->dm_strtab.cts_data;
346 const Elf64_Sym *sym;
350 if (dmp->dm_nsymelems == 0)
353 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
355 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
356 dsp = &dmp->dm_symchains[i];
357 sym = symtab + dsp->ds_symid;
359 if (strcmp(name, strtab + sym->st_name) == 0) {
361 *idp = dsp->ds_symid;
362 return (dt_module_symgelf64(sym, symp));
370 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
371 GElf_Sym *symp, uint_t *idp)
373 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
374 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
375 const Elf32_Sym *sym;
377 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
380 if (dmp->dm_aslen == 0)
383 while (hi - lo > 1) {
385 if (addr >= asmap[mid]->st_value)
391 i = addr < asmap[hi]->st_value ? lo : hi;
396 * If the previous entry has the same value, improve our choice. The
397 * order of equal-valued symbols is determined by the comparison func.
399 while (i-- != 0 && asmap[i]->st_value == v)
402 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
404 *idp = (uint_t)(sym - symtab);
405 return (dt_module_symgelf32(sym, symp));
412 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
413 GElf_Sym *symp, uint_t *idp)
415 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
416 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
417 const Elf64_Sym *sym;
419 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
422 if (dmp->dm_aslen == 0)
425 while (hi - lo > 1) {
427 if (addr >= asmap[mid]->st_value)
433 i = addr < asmap[hi]->st_value ? lo : hi;
438 * If the previous entry has the same value, improve our choice. The
439 * order of equal-valued symbols is determined by the comparison func.
441 while (i-- != 0 && asmap[i]->st_value == v)
444 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
446 *idp = (uint_t)(sym - symtab);
447 return (dt_module_symgelf64(sym, symp));
453 static const dt_modops_t dt_modops_32 = {
460 static const dt_modops_t dt_modops_64 = {
468 dt_module_create(dtrace_hdl_t *dtp, const char *name)
473 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
476 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
477 if (strcmp(dmp->dm_name, name) == 0)
481 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
482 return (NULL); /* caller must handle allocation failure */
484 bzero(dmp, sizeof (dt_module_t));
485 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
486 dt_list_append(&dtp->dt_modlist, dmp);
487 dmp->dm_next = dtp->dt_mods[h];
488 dtp->dt_mods[h] = dmp;
491 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
492 dmp->dm_ops = &dt_modops_64;
494 dmp->dm_ops = &dt_modops_32;
497 * Modules for userland processes are special. They always refer to a
498 * specific process and have a copy of their CTF data from a specific
499 * instant in time. Any dt_module_t that begins with 'pid' is a module
500 * for a specific process, much like how any probe description that
501 * begins with 'pid' is special. pid123 refers to process 123. A module
502 * that is just 'pid' refers specifically to pid$target. This is
503 * generally done as D does not currently allow for macros to be
504 * evaluated when working with types.
506 if (strncmp(dmp->dm_name, "pid", 3) == 0) {
508 if (dmp->dm_name[3] == '\0') {
509 idp = dt_idhash_lookup(dtp->dt_macros, "target");
510 if (idp != NULL && idp->di_id != 0)
511 dmp->dm_pid = idp->di_id;
513 pid = strtol(dmp->dm_name + 3, &eptr, 10);
514 if (errno == 0 && *eptr == '\0')
515 dmp->dm_pid = (pid_t)pid;
517 dt_dprintf("encountered malformed pid "
518 "module: %s\n", dmp->dm_name);
526 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
528 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
531 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
532 if (strcmp(dmp->dm_name, name) == 0)
541 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
543 return (ctfp ? ctf_getspecific(ctfp) : NULL);
547 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
555 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
556 return (dt_set_errno(dtp, EDT_NOTLOADED));
558 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
559 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
560 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
561 continue; /* skip any malformed sections */
563 if (sh.sh_type == ctsp->cts_type &&
564 sh.sh_entsize == ctsp->cts_entsize &&
565 strcmp(s, ctsp->cts_name) == 0)
566 break; /* section matches specification */
570 * If the section isn't found, return success but leave cts_data set
571 * to NULL and cts_size set to zero for our caller.
573 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
577 ctsp->cts_data = dp->d_buf;
579 if ((ctsp->cts_data = malloc(dp->d_size)) == NULL)
581 memcpy(ctsp->cts_data, dp->d_buf, dp->d_size);
583 ctsp->cts_size = dp->d_size;
585 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
586 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
591 typedef struct dt_module_cb_arg {
592 struct ps_prochandle *dpa_proc;
593 dtrace_hdl_t *dpa_dtp;
594 dt_module_t *dpa_dmp;
596 } dt_module_cb_arg_t;
600 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)
603 dt_module_cb_arg_t *dcp = arg;
605 /* Try to grab a ctf container if it exists */
606 fp = Pname_to_ctf(dcp->dpa_proc, obj);
614 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)
617 char buf[MAXPATHLEN], *p;
618 dt_module_cb_arg_t *dcp = arg;
619 int count = dcp->dpa_count;
622 fp = Pname_to_ctf(dcp->dpa_proc, obj);
628 dcp->dpa_dmp->dm_libctfp[count] = fp;
630 * While it'd be nice to simply use objname here, because of our prior
631 * actions we'll always get a resolved object name to its on disk file.
632 * Like the pid provider, we need to tell a bit of a lie here. The type
633 * that the user thinks of is in terms of the libraries they requested,
634 * eg. libc.so.1, they don't care about the fact that it's
637 (void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));
638 if ((p = strrchr(buf, '/')) == NULL)
644 * If for some reason we can't find a link map id for this module, which
645 * would be really quite weird. We instead just say the link map id is
648 if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)
652 dcp->dpa_dmp->dm_libctfn[count] = strdup(p);
654 (void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],
656 if (dcp->dpa_dmp->dm_libctfn[count] == NULL)
658 ctf_setspecific(fp, dcp->dpa_dmp);
664 * We've been asked to load data that belongs to another process. As such we're
665 * going to pgrab it at this instant, load everything that we might ever care
666 * about, and then drive on. The reason for this is that the process that we're
667 * interested in might be changing. As long as we have grabbed it, then this
668 * can't be a problem for us.
670 * For now, we're actually going to punt on most things and just try to get CTF
671 * data, nothing else. Basically this is only useful as a source of type
672 * information, we can't go and do the stacktrace lookups, etc.
675 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)
677 struct ps_prochandle *p;
678 dt_module_cb_arg_t arg;
681 * Note that on success we do not release this hold. We must hold this
684 p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
686 dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);
687 return (dt_set_errno(dtp, EDT_CANTLOAD));
689 dt_proc_lock(dtp, p);
695 if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {
696 dt_dprintf("failed to iterate objects\n");
697 dt_proc_unlock(dtp, p);
698 dt_proc_release(dtp, p);
699 return (dt_set_errno(dtp, EDT_CANTLOAD));
702 if (arg.dpa_count == 0) {
703 dt_dprintf("no ctf data present\n");
704 dt_proc_unlock(dtp, p);
705 dt_proc_release(dtp, p);
706 return (dt_set_errno(dtp, EDT_CANTLOAD));
709 dmp->dm_libctfp = calloc(arg.dpa_count, sizeof (ctf_file_t *));
710 if (dmp->dm_libctfp == NULL) {
711 dt_proc_unlock(dtp, p);
712 dt_proc_release(dtp, p);
713 return (dt_set_errno(dtp, EDT_NOMEM));
716 dmp->dm_libctfn = calloc(arg.dpa_count, sizeof (char *));
717 if (dmp->dm_libctfn == NULL) {
718 free(dmp->dm_libctfp);
719 dt_proc_unlock(dtp, p);
720 dt_proc_release(dtp, p);
721 return (dt_set_errno(dtp, EDT_NOMEM));
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 = calloc(dmp->dm_nsymbuckets, sizeof (uint_t));
804 dmp->dm_symchains = calloc(dmp->dm_nsymelems + 1, sizeof (dt_sym_t));
806 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
807 dt_module_unload(dtp, dmp);
808 return (dt_set_errno(dtp, EDT_NOMEM));
812 * Iterate over the symbol table data buffer and insert each symbol
813 * name into the name hash if the name and type are valid. Then
814 * allocate the address map, fill it in, and sort it.
816 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
818 dt_dprintf("hashed %s [%s] (%u symbols)\n",
819 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
821 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
822 dt_module_unload(dtp, dmp);
823 return (dt_set_errno(dtp, EDT_NOMEM));
826 dmp->dm_ops->do_symsort(dmp);
828 dt_dprintf("sorted %s [%s] (%u symbols)\n",
829 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
831 dmp->dm_flags |= DT_DM_LOADED;
836 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
838 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
840 return (dt_module_getctf(dtp, dmp) != NULL);
844 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
851 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
852 return (dmp->dm_ctfp);
854 if (dmp->dm_ops == &dt_modops_64)
855 model = CTF_MODEL_LP64;
857 model = CTF_MODEL_ILP32;
860 * If the data model of the module does not match our program data
861 * model, then do not permit CTF from this module to be opened and
862 * returned to the compiler. If we support mixed data models in the
863 * future for combined kernel/user tracing, this can be removed.
865 if (dtp->dt_conf.dtc_ctfmodel != model) {
866 (void) dt_set_errno(dtp, EDT_DATAMODEL);
870 if (dmp->dm_ctdata.cts_size == 0) {
871 (void) dt_set_errno(dtp, EDT_NOCTF);
875 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
876 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
878 if (dmp->dm_ctfp == NULL) {
879 (void) dt_set_errno(dtp, EDT_CTF);
883 (void) ctf_setmodel(dmp->dm_ctfp, model);
884 ctf_setspecific(dmp->dm_ctfp, dmp);
886 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
887 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
888 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
890 (void) dt_set_errno(dtp, EDT_NOMEM);
894 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
895 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
896 (void) dt_set_errno(dtp, EDT_CTF);
901 dt_dprintf("loaded CTF container for %s (%p)\n",
902 dmp->dm_name, (void *)dmp->dm_ctfp);
904 return (dmp->dm_ctfp);
907 ctf_close(dmp->dm_ctfp);
914 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
918 ctf_close(dmp->dm_ctfp);
922 if (dmp->dm_ctdata.cts_data != NULL) {
923 free(dmp->dm_ctdata.cts_data);
925 if (dmp->dm_symtab.cts_data != NULL) {
926 free(dmp->dm_symtab.cts_data);
928 if (dmp->dm_strtab.cts_data != NULL) {
929 free(dmp->dm_strtab.cts_data);
933 if (dmp->dm_libctfp != NULL) {
934 for (i = 0; i < dmp->dm_nctflibs; i++) {
935 ctf_close(dmp->dm_libctfp[i]);
936 free(dmp->dm_libctfn[i]);
938 free(dmp->dm_libctfp);
939 free(dmp->dm_libctfn);
940 dmp->dm_libctfp = NULL;
941 dmp->dm_nctflibs = 0;
944 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
945 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
946 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
948 if (dmp->dm_symbuckets != NULL) {
949 free(dmp->dm_symbuckets);
950 dmp->dm_symbuckets = NULL;
953 if (dmp->dm_symchains != NULL) {
954 free(dmp->dm_symchains);
955 dmp->dm_symchains = NULL;
958 if (dmp->dm_asmap != NULL) {
960 dmp->dm_asmap = NULL;
962 #if defined(__FreeBSD__)
963 if (dmp->dm_sec_offsets != NULL) {
964 free(dmp->dm_sec_offsets);
965 dmp->dm_sec_offsets = NULL;
969 dmp->dm_nsymbuckets = 0;
970 dmp->dm_nsymelems = 0;
975 dmp->dm_text_size = 0;
977 dmp->dm_data_size = 0;
979 dmp->dm_bss_size = 0;
981 if (dmp->dm_extern != NULL) {
982 dt_idhash_destroy(dmp->dm_extern);
983 dmp->dm_extern = NULL;
986 (void) elf_end(dmp->dm_elf);
991 dmp->dm_flags &= ~DT_DM_LOADED;
995 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
997 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
998 dt_module_t **dmpp = &dtp->dt_mods[h];
1000 dt_list_delete(&dtp->dt_modlist, dmp);
1001 assert(dtp->dt_nmods != 0);
1005 * Now remove this module from its hash chain. We expect to always
1006 * find the module on its hash chain, so in this loop we assert that
1007 * we don't run off the end of the list.
1009 while (*dmpp != dmp) {
1010 dmpp = &((*dmpp)->dm_next);
1011 assert(*dmpp != NULL);
1014 *dmpp = dmp->dm_next;
1016 dt_module_unload(dtp, dmp);
1021 * Insert a new external symbol reference into the specified module. The new
1022 * symbol will be marked as undefined and is assigned a symbol index beyond
1023 * any existing cached symbols from this module. We use the ident's di_data
1024 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
1027 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
1028 const char *name, const dtrace_typeinfo_t *tip)
1030 dtrace_syminfo_t *sip;
1034 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
1035 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
1036 (void) dt_set_errno(dtp, EDT_NOMEM);
1040 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
1041 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
1045 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
1046 (void) dt_set_errno(dtp, EDT_NOMEM);
1050 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
1051 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1054 (void) dt_set_errno(dtp, EDT_NOMEM);
1059 sip->dts_object = dmp->dm_name;
1060 sip->dts_name = idp->di_name;
1061 sip->dts_id = idp->di_id;
1064 idp->di_ctfp = tip->dtt_ctfp;
1065 idp->di_type = tip->dtt_type;
1071 dt_module_modelname(dt_module_t *dmp)
1073 if (dmp->dm_ops == &dt_modops_64)
1081 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1085 for (i = 0; i < dmp->dm_nctflibs; i++) {
1086 if (dmp->dm_libctfp[i] == fp)
1095 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1099 for (i = 0; i < dmp->dm_nctflibs; i++) {
1100 if (strcmp(dmp->dm_libctfn[i], name) == 0)
1101 return (dmp->dm_libctfp[i]);
1108 * Update our module cache by adding an entry for the specified module 'name'.
1109 * We create the dt_module_t and populate it using /system/object/<name>/.
1111 * On FreeBSD, the module name is passed as the full module file name,
1112 * including the path.
1116 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1118 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
1121 char fname[MAXPATHLEN];
1133 (void) snprintf(fname, sizeof (fname),
1134 "%s/%s/object", OBJFS_ROOT, name);
1138 char name[MAXPATHLEN];
1139 uintptr_t mapbase, alignmask;
1143 (void) strlcpy(name, k_stat->name, sizeof(name));
1144 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
1147 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1148 (dmp = dt_module_create(dtp, name)) == NULL) {
1149 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1155 * Since the module can unload out from under us (and /system/object
1156 * will return ENOENT), tell libelf to cook the entire file now and
1157 * then close the underlying file descriptor immediately. If this
1158 * succeeds, we know that we can continue safely using dmp->dm_elf.
1160 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1161 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1164 if (dmp->dm_elf == NULL || err == -1 ||
1165 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1166 dt_dprintf("failed to load %s: %s\n",
1167 fname, elf_errmsg(elf_errno()));
1168 dt_module_destroy(dtp, dmp);
1172 switch (gelf_getclass(dmp->dm_elf)) {
1174 dmp->dm_ops = &dt_modops_32;
1178 dmp->dm_ops = &dt_modops_64;
1182 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1183 dt_module_destroy(dtp, dmp);
1186 #if defined(__FreeBSD__)
1187 mapbase = (uintptr_t)k_stat->address;
1188 gelf_getehdr(dmp->dm_elf, &ehdr);
1189 is_elf_obj = (ehdr.e_type == ET_REL);
1191 dmp->dm_sec_offsets =
1192 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
1193 if (dmp->dm_sec_offsets == NULL) {
1194 dt_dprintf("failed to allocate memory\n");
1195 dt_module_destroy(dtp, dmp);
1201 * Iterate over the section headers locating various sections of
1202 * interest and use their attributes to flesh out the dt_module_t.
1204 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1205 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1206 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1207 continue; /* skip any malformed sections */
1208 #if defined(__FreeBSD__)
1209 if (sh.sh_size == 0)
1211 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
1212 alignmask = sh.sh_addralign - 1;
1213 mapbase += alignmask;
1214 mapbase &= ~alignmask;
1215 sh.sh_addr = mapbase;
1217 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
1218 mapbase += sh.sh_size;
1221 if (strcmp(s, ".text") == 0) {
1222 dmp->dm_text_size = sh.sh_size;
1223 dmp->dm_text_va = sh.sh_addr;
1224 } else if (strcmp(s, ".data") == 0) {
1225 dmp->dm_data_size = sh.sh_size;
1226 dmp->dm_data_va = sh.sh_addr;
1227 } else if (strcmp(s, ".bss") == 0) {
1228 dmp->dm_bss_size = sh.sh_size;
1229 dmp->dm_bss_va = sh.sh_addr;
1230 } else if (strcmp(s, ".info") == 0 &&
1231 (dp = elf_getdata(sp, NULL)) != NULL) {
1232 bcopy(dp->d_buf, &dmp->dm_info,
1233 MIN(sh.sh_size, sizeof (dmp->dm_info)));
1234 } else if (strcmp(s, ".filename") == 0 &&
1235 (dp = elf_getdata(sp, NULL)) != NULL) {
1236 (void) strlcpy(dmp->dm_file,
1237 dp->d_buf, sizeof (dmp->dm_file));
1241 dmp->dm_flags |= DT_DM_KERNEL;
1243 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1246 * Include .rodata and special sections into .text.
1247 * This depends on default section layout produced by GNU ld
1248 * for ELF objects and libraries:
1249 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1251 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1252 #if defined(__i386__)
1254 * Find the first load section and figure out the relocation
1255 * offset for the symbols. The kernel module will not need
1256 * relocation, but the kernel linker modules will.
1258 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1259 if (ph.p_type == PT_LOAD) {
1260 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1265 #endif /* illumos */
1267 if (dmp->dm_info.objfs_info_primary)
1268 dmp->dm_flags |= DT_DM_PRIMARY;
1270 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1271 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1275 * Unload all the loaded modules and then refresh the module cache with the
1276 * latest list of loaded modules and their address ranges.
1279 dtrace_update(dtrace_hdl_t *dtp)
1283 #if defined(__FreeBSD__)
1287 for (dmp = dt_list_next(&dtp->dt_modlist);
1288 dmp != NULL; dmp = dt_list_next(dmp))
1289 dt_module_unload(dtp, dmp);
1293 * Open /system/object and attempt to create a libdtrace module for
1294 * each kernel module that is loaded on the current system.
1296 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1297 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1300 while ((dp = readdir(dirp)) != NULL) {
1301 if (dp->d_name[0] != '.')
1302 dt_module_update(dtp, dp->d_name);
1305 (void) closedir(dirp);
1307 #elif defined(__FreeBSD__)
1309 * Use FreeBSD's kernel loader interface to discover what kernel
1310 * modules are loaded and create a libdtrace module for each one.
1312 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1313 struct kld_file_stat k_stat;
1314 k_stat.version = sizeof(k_stat);
1315 if (kldstat(fileid, &k_stat) == 0)
1316 dt_module_update(dtp, &k_stat);
1321 * Look up all the macro identifiers and set di_id to the latest value.
1322 * This code collaborates with dt_lex.l on the use of di_id. We will
1323 * need to implement something fancier if we need to support non-ints.
1325 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1326 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1327 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1328 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1329 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1330 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1332 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1334 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1336 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1338 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1341 * Cache the pointers to the modules representing the base executable
1342 * and the run-time linker in the dtrace client handle. Note that on
1343 * x86 krtld is folded into unix, so if we don't find it, use unix
1346 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1347 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1348 if (dtp->dt_rtld == NULL)
1349 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1352 * If this is the first time we are initializing the module list,
1353 * remove the module for genunix from the module list and then move it
1354 * to the front of the module list. We do this so that type and symbol
1355 * queries encounter genunix and thereby optimize for the common case
1356 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1358 if (dtp->dt_exec != NULL &&
1359 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1360 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1361 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1365 static dt_module_t *
1366 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1368 int err = EDT_NOMOD;
1371 switch ((uintptr_t)object) {
1372 case (uintptr_t)DTRACE_OBJ_EXEC:
1375 case (uintptr_t)DTRACE_OBJ_RTLD:
1378 case (uintptr_t)DTRACE_OBJ_CDEFS:
1379 dmp = dtp->dt_cdefs;
1381 case (uintptr_t)DTRACE_OBJ_DDEFS:
1382 dmp = dtp->dt_ddefs;
1385 dmp = dt_module_create(dtp, object);
1390 (void) dt_set_errno(dtp, err);
1396 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1397 * complete symbol information for the matching symbol.
1400 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1401 GElf_Sym *symp, dtrace_syminfo_t *sip)
1408 uint_t mask = 0; /* mask of dt_module flags to match */
1409 uint_t bits = 0; /* flag bits that must be present */
1411 if (object != DTRACE_OBJ_EVERY &&
1412 object != DTRACE_OBJ_KMODS &&
1413 object != DTRACE_OBJ_UMODS) {
1414 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1415 return (-1); /* dt_errno is set for us */
1417 if (dt_module_load(dtp, dmp) == -1)
1418 return (-1); /* dt_errno is set for us */
1422 if (object == DTRACE_OBJ_KMODS)
1423 mask = bits = DT_DM_KERNEL;
1424 else if (object == DTRACE_OBJ_UMODS)
1425 mask = DT_DM_KERNEL;
1427 dmp = dt_list_next(&dtp->dt_modlist);
1434 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1435 if ((dmp->dm_flags & mask) != bits)
1436 continue; /* failed to match required attributes */
1438 if (dt_module_load(dtp, dmp) == -1)
1439 continue; /* failed to load symbol table */
1441 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1443 sip->dts_object = dmp->dm_name;
1444 sip->dts_name = (const char *)
1445 dmp->dm_strtab.cts_data + symp->st_name;
1451 if (dmp->dm_extern != NULL &&
1452 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1454 symp->st_name = (uintptr_t)idp->di_name;
1456 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1458 symp->st_shndx = SHN_UNDEF;
1461 ctf_type_size(idp->di_ctfp, idp->di_type);
1465 sip->dts_object = dmp->dm_name;
1466 sip->dts_name = idp->di_name;
1467 sip->dts_id = idp->di_id;
1474 return (dt_set_errno(dtp, EDT_NOSYM));
1478 * Exported interface to look up a symbol by address. We return the GElf_Sym
1479 * and complete symbol information for the matching symbol.
1482 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1483 GElf_Sym *symp, dtrace_syminfo_t *sip)
1487 const dtrace_vector_t *v = dtp->dt_vector;
1490 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1492 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1493 dmp = dt_list_next(dmp)) {
1494 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1495 addr - dmp->dm_data_va < dmp->dm_data_size ||
1496 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1501 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1503 if (dt_module_load(dtp, dmp) == -1)
1504 return (-1); /* dt_errno is set for us */
1507 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1508 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1512 sip->dts_object = dmp->dm_name;
1515 sip->dts_name = (const char *)
1516 dmp->dm_strtab.cts_data + symp->st_name;
1519 sip->dts_name = NULL;
1528 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1529 dtrace_typeinfo_t *tip)
1531 dtrace_typeinfo_t ti;
1540 uint_t mask = 0; /* mask of dt_module flags to match */
1541 uint_t bits = 0; /* flag bits that must be present */
1543 if (object != DTRACE_OBJ_EVERY &&
1544 object != DTRACE_OBJ_KMODS &&
1545 object != DTRACE_OBJ_UMODS) {
1546 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1547 return (-1); /* dt_errno is set for us */
1549 if (dt_module_load(dtp, dmp) == -1)
1550 return (-1); /* dt_errno is set for us */
1554 if (object == DTRACE_OBJ_KMODS)
1555 mask = bits = DT_DM_KERNEL;
1556 else if (object == DTRACE_OBJ_UMODS)
1557 mask = DT_DM_KERNEL;
1559 dmp = dt_list_next(&dtp->dt_modlist);
1567 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1568 if ((dmp->dm_flags & mask) != bits)
1569 continue; /* failed to match required attributes */
1572 * If we can't load the CTF container, continue on to the next
1573 * module. If our search was scoped to only one module then
1574 * return immediately leaving dt_errno unmodified.
1576 if (dt_module_hasctf(dtp, dmp) == 0) {
1583 * Look up the type in the module's CTF container. If our
1584 * match is a forward declaration tag, save this choice in
1585 * 'tip' and keep going in the hope that we will locate the
1586 * underlying structure definition. Otherwise just return.
1588 if (dmp->dm_pid == 0) {
1589 id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1592 if ((p = strchr(name, '`')) != NULL) {
1595 return (dt_set_errno(dtp, EDT_NOMEM));
1596 p = strchr(buf, '`');
1597 if ((q = strchr(p + 1, '`')) != NULL)
1600 fp = dt_module_getctflib(dtp, dmp, buf);
1601 if (fp == NULL || (id = ctf_lookup_by_name(fp,
1606 for (i = 0; i < dmp->dm_nctflibs; i++) {
1607 fp = dmp->dm_libctfp[i];
1608 id = ctf_lookup_by_name(fp, name);
1614 if (id != CTF_ERR) {
1615 tip->dtt_object = dmp->dm_name;
1618 if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=
1627 return (dt_set_errno(dtp, EDT_NOTYPE));
1633 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1634 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1638 tip->dtt_object = NULL;
1639 tip->dtt_ctfp = NULL;
1640 tip->dtt_type = CTF_ERR;
1643 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1644 return (dt_set_errno(dtp, EDT_NOMOD));
1646 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1648 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1651 return (dt_set_errno(dtp, EDT_NOSYM));
1653 tip->dtt_ctfp = idp->di_ctfp;
1654 tip->dtt_type = idp->di_type;
1656 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1657 if (dt_module_getctf(dtp, dmp) == NULL)
1658 return (-1); /* errno is set for us */
1660 tip->dtt_ctfp = dmp->dm_ctfp;
1661 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1663 if (tip->dtt_type == CTF_ERR) {
1664 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1665 return (dt_set_errno(dtp, EDT_CTF));
1669 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1670 tip->dtt_type = DT_FPTR_TYPE(dtp);
1673 tip->dtt_object = dmp->dm_name;
1677 static dtrace_objinfo_t *
1678 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1680 dto->dto_name = dmp->dm_name;
1681 dto->dto_file = dmp->dm_file;
1682 dto->dto_id = dmp->dm_modid;
1685 if (dmp->dm_flags & DT_DM_KERNEL)
1686 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1687 if (dmp->dm_flags & DT_DM_PRIMARY)
1688 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1690 dto->dto_text_va = dmp->dm_text_va;
1691 dto->dto_text_size = dmp->dm_text_size;
1692 dto->dto_data_va = dmp->dm_data_va;
1693 dto->dto_data_size = dmp->dm_data_size;
1694 dto->dto_bss_va = dmp->dm_bss_va;
1695 dto->dto_bss_size = dmp->dm_bss_size;
1701 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1703 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1704 dtrace_objinfo_t dto;
1707 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1708 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1716 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1720 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1721 object == DTRACE_OBJ_UMODS || dto == NULL)
1722 return (dt_set_errno(dtp, EINVAL));
1724 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1725 return (-1); /* dt_errno is set for us */
1727 if (dt_module_load(dtp, dmp) == -1)
1728 return (-1); /* dt_errno is set for us */
1730 (void) dt_module_info(dmp, dto);