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_release(dtp, p);
698 return (dt_set_errno(dtp, EDT_CANTLOAD));
701 if (arg.dpa_count == 0) {
702 dt_dprintf("no ctf data present\n");
703 dt_proc_unlock(dtp, p);
704 dt_proc_release(dtp, p);
705 return (dt_set_errno(dtp, EDT_CANTLOAD));
708 dmp->dm_libctfp = calloc(arg.dpa_count, sizeof (ctf_file_t *));
709 if (dmp->dm_libctfp == NULL) {
710 dt_proc_unlock(dtp, p);
711 dt_proc_release(dtp, p);
712 return (dt_set_errno(dtp, EDT_NOMEM));
715 dmp->dm_libctfn = calloc(arg.dpa_count, sizeof (char *));
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));
723 dmp->dm_nctflibs = arg.dpa_count;
726 if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
727 dt_proc_unlock(dtp, p);
728 dt_module_unload(dtp, dmp);
729 dt_proc_release(dtp, p);
730 return (dt_set_errno(dtp, EDT_CANTLOAD));
732 assert(arg.dpa_count == dmp->dm_nctflibs);
733 dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
736 dt_proc_unlock(dtp, p);
737 dt_proc_release(dtp, p);
738 dmp->dm_flags |= DT_DM_LOADED;
744 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
746 if (dmp->dm_flags & DT_DM_LOADED)
747 return (0); /* module is already loaded */
749 if (dmp->dm_pid != 0)
750 return (dt_module_load_proc(dtp, dmp));
752 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
753 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
754 dmp->dm_ctdata.cts_flags = 0;
755 dmp->dm_ctdata.cts_data = NULL;
756 dmp->dm_ctdata.cts_size = 0;
757 dmp->dm_ctdata.cts_entsize = 0;
758 dmp->dm_ctdata.cts_offset = 0;
760 dmp->dm_symtab.cts_name = ".symtab";
761 dmp->dm_symtab.cts_type = SHT_SYMTAB;
762 dmp->dm_symtab.cts_flags = 0;
763 dmp->dm_symtab.cts_data = NULL;
764 dmp->dm_symtab.cts_size = 0;
765 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
766 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
767 dmp->dm_symtab.cts_offset = 0;
769 dmp->dm_strtab.cts_name = ".strtab";
770 dmp->dm_strtab.cts_type = SHT_STRTAB;
771 dmp->dm_strtab.cts_flags = 0;
772 dmp->dm_strtab.cts_data = NULL;
773 dmp->dm_strtab.cts_size = 0;
774 dmp->dm_strtab.cts_entsize = 0;
775 dmp->dm_strtab.cts_offset = 0;
778 * Attempt to load the module's CTF section, symbol table section, and
779 * string table section. Note that modules may not contain CTF data:
780 * this will result in a successful load_sect but data of size zero.
781 * We will then fail if dt_module_getctf() is called, as shown below.
783 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
784 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
785 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
786 dt_module_unload(dtp, dmp);
787 return (-1); /* dt_errno is set for us */
791 * Allocate the hash chains and hash buckets for symbol name lookup.
792 * This is relatively simple since the symbol table is of fixed size
793 * and is known in advance. We allocate one extra element since we
794 * use element indices instead of pointers and zero is our sentinel.
797 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
799 dmp->dm_nsymbuckets = _dtrace_strbuckets;
800 dmp->dm_symfree = 1; /* first free element is index 1 */
802 dmp->dm_symbuckets = calloc(dmp->dm_nsymbuckets, sizeof (uint_t));
803 dmp->dm_symchains = calloc(dmp->dm_nsymelems + 1, sizeof (dt_sym_t));
805 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
806 dt_module_unload(dtp, dmp);
807 return (dt_set_errno(dtp, EDT_NOMEM));
811 * Iterate over the symbol table data buffer and insert each symbol
812 * name into the name hash if the name and type are valid. Then
813 * allocate the address map, fill it in, and sort it.
815 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
817 dt_dprintf("hashed %s [%s] (%u symbols)\n",
818 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
820 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
821 dt_module_unload(dtp, dmp);
822 return (dt_set_errno(dtp, EDT_NOMEM));
825 dmp->dm_ops->do_symsort(dmp);
827 dt_dprintf("sorted %s [%s] (%u symbols)\n",
828 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
830 dmp->dm_flags |= DT_DM_LOADED;
835 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
837 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
839 return (dt_module_getctf(dtp, dmp) != NULL);
843 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
850 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
851 return (dmp->dm_ctfp);
853 if (dmp->dm_ops == &dt_modops_64)
854 model = CTF_MODEL_LP64;
856 model = CTF_MODEL_ILP32;
859 * If the data model of the module does not match our program data
860 * model, then do not permit CTF from this module to be opened and
861 * returned to the compiler. If we support mixed data models in the
862 * future for combined kernel/user tracing, this can be removed.
864 if (dtp->dt_conf.dtc_ctfmodel != model) {
865 (void) dt_set_errno(dtp, EDT_DATAMODEL);
869 if (dmp->dm_ctdata.cts_size == 0) {
870 (void) dt_set_errno(dtp, EDT_NOCTF);
874 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
875 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
877 if (dmp->dm_ctfp == NULL) {
878 (void) dt_set_errno(dtp, EDT_CTF);
882 (void) ctf_setmodel(dmp->dm_ctfp, model);
883 ctf_setspecific(dmp->dm_ctfp, dmp);
885 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
886 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
887 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
889 (void) dt_set_errno(dtp, EDT_NOMEM);
893 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
894 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
895 (void) dt_set_errno(dtp, EDT_CTF);
900 dt_dprintf("loaded CTF container for %s (%p)\n",
901 dmp->dm_name, (void *)dmp->dm_ctfp);
903 return (dmp->dm_ctfp);
906 ctf_close(dmp->dm_ctfp);
913 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
917 ctf_close(dmp->dm_ctfp);
921 if (dmp->dm_ctdata.cts_data != NULL) {
922 free(dmp->dm_ctdata.cts_data);
924 if (dmp->dm_symtab.cts_data != NULL) {
925 free(dmp->dm_symtab.cts_data);
927 if (dmp->dm_strtab.cts_data != NULL) {
928 free(dmp->dm_strtab.cts_data);
932 if (dmp->dm_libctfp != NULL) {
933 for (i = 0; i < dmp->dm_nctflibs; i++) {
934 ctf_close(dmp->dm_libctfp[i]);
935 free(dmp->dm_libctfn[i]);
937 free(dmp->dm_libctfp);
938 free(dmp->dm_libctfn);
939 dmp->dm_libctfp = NULL;
940 dmp->dm_nctflibs = 0;
943 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
944 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
945 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
947 if (dmp->dm_symbuckets != NULL) {
948 free(dmp->dm_symbuckets);
949 dmp->dm_symbuckets = NULL;
952 if (dmp->dm_symchains != NULL) {
953 free(dmp->dm_symchains);
954 dmp->dm_symchains = NULL;
957 if (dmp->dm_asmap != NULL) {
959 dmp->dm_asmap = NULL;
961 #if defined(__FreeBSD__)
962 if (dmp->dm_sec_offsets != NULL) {
963 free(dmp->dm_sec_offsets);
964 dmp->dm_sec_offsets = NULL;
968 dmp->dm_nsymbuckets = 0;
969 dmp->dm_nsymelems = 0;
974 dmp->dm_text_size = 0;
976 dmp->dm_data_size = 0;
978 dmp->dm_bss_size = 0;
980 if (dmp->dm_extern != NULL) {
981 dt_idhash_destroy(dmp->dm_extern);
982 dmp->dm_extern = NULL;
985 (void) elf_end(dmp->dm_elf);
990 dmp->dm_flags &= ~DT_DM_LOADED;
994 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
996 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
997 dt_module_t **dmpp = &dtp->dt_mods[h];
999 dt_list_delete(&dtp->dt_modlist, dmp);
1000 assert(dtp->dt_nmods != 0);
1004 * Now remove this module from its hash chain. We expect to always
1005 * find the module on its hash chain, so in this loop we assert that
1006 * we don't run off the end of the list.
1008 while (*dmpp != dmp) {
1009 dmpp = &((*dmpp)->dm_next);
1010 assert(*dmpp != NULL);
1013 *dmpp = dmp->dm_next;
1015 dt_module_unload(dtp, dmp);
1020 * Insert a new external symbol reference into the specified module. The new
1021 * symbol will be marked as undefined and is assigned a symbol index beyond
1022 * any existing cached symbols from this module. We use the ident's di_data
1023 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
1026 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
1027 const char *name, const dtrace_typeinfo_t *tip)
1029 dtrace_syminfo_t *sip;
1033 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
1034 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
1035 (void) dt_set_errno(dtp, EDT_NOMEM);
1039 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
1040 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
1044 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
1045 (void) dt_set_errno(dtp, EDT_NOMEM);
1049 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
1050 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1053 (void) dt_set_errno(dtp, EDT_NOMEM);
1058 sip->dts_object = dmp->dm_name;
1059 sip->dts_name = idp->di_name;
1060 sip->dts_id = idp->di_id;
1063 idp->di_ctfp = tip->dtt_ctfp;
1064 idp->di_type = tip->dtt_type;
1070 dt_module_modelname(dt_module_t *dmp)
1072 if (dmp->dm_ops == &dt_modops_64)
1080 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1084 for (i = 0; i < dmp->dm_nctflibs; i++) {
1085 if (dmp->dm_libctfp[i] == fp)
1094 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1098 for (i = 0; i < dmp->dm_nctflibs; i++) {
1099 if (strcmp(dmp->dm_libctfn[i], name) == 0)
1100 return (dmp->dm_libctfp[i]);
1107 * Update our module cache by adding an entry for the specified module 'name'.
1108 * We create the dt_module_t and populate it using /system/object/<name>/.
1110 * On FreeBSD, the module name is passed as the full module file name,
1111 * including the path.
1115 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1117 dt_module_update(dtrace_hdl_t *dtp, struct kld_file_stat *k_stat)
1120 char fname[MAXPATHLEN];
1132 (void) snprintf(fname, sizeof (fname),
1133 "%s/%s/object", OBJFS_ROOT, name);
1137 char name[MAXPATHLEN];
1138 uintptr_t mapbase, alignmask;
1142 (void) strlcpy(name, k_stat->name, sizeof(name));
1143 (void) strlcpy(fname, k_stat->pathname, sizeof(fname));
1146 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1147 (dmp = dt_module_create(dtp, name)) == NULL) {
1148 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1154 * Since the module can unload out from under us (and /system/object
1155 * will return ENOENT), tell libelf to cook the entire file now and
1156 * then close the underlying file descriptor immediately. If this
1157 * succeeds, we know that we can continue safely using dmp->dm_elf.
1159 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1160 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1163 if (dmp->dm_elf == NULL || err == -1 ||
1164 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1165 dt_dprintf("failed to load %s: %s\n",
1166 fname, elf_errmsg(elf_errno()));
1167 dt_module_destroy(dtp, dmp);
1171 switch (gelf_getclass(dmp->dm_elf)) {
1173 dmp->dm_ops = &dt_modops_32;
1177 dmp->dm_ops = &dt_modops_64;
1181 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1182 dt_module_destroy(dtp, dmp);
1185 #if defined(__FreeBSD__)
1186 mapbase = (uintptr_t)k_stat->address;
1187 gelf_getehdr(dmp->dm_elf, &ehdr);
1188 is_elf_obj = (ehdr.e_type == ET_REL);
1190 dmp->dm_sec_offsets =
1191 malloc(ehdr.e_shnum * sizeof(*dmp->dm_sec_offsets));
1192 if (dmp->dm_sec_offsets == NULL) {
1193 dt_dprintf("failed to allocate memory\n");
1194 dt_module_destroy(dtp, dmp);
1200 * Iterate over the section headers locating various sections of
1201 * interest and use their attributes to flesh out the dt_module_t.
1203 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1204 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1205 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1206 continue; /* skip any malformed sections */
1207 #if defined(__FreeBSD__)
1208 if (sh.sh_size == 0)
1210 if (sh.sh_type == SHT_PROGBITS || sh.sh_type == SHT_NOBITS) {
1211 alignmask = sh.sh_addralign - 1;
1212 mapbase += alignmask;
1213 mapbase &= ~alignmask;
1214 sh.sh_addr = mapbase;
1216 dmp->dm_sec_offsets[elf_ndxscn(sp)] = sh.sh_addr;
1217 mapbase += sh.sh_size;
1220 if (strcmp(s, ".text") == 0) {
1221 dmp->dm_text_size = sh.sh_size;
1222 dmp->dm_text_va = sh.sh_addr;
1223 } else if (strcmp(s, ".data") == 0) {
1224 dmp->dm_data_size = sh.sh_size;
1225 dmp->dm_data_va = sh.sh_addr;
1226 } else if (strcmp(s, ".bss") == 0) {
1227 dmp->dm_bss_size = sh.sh_size;
1228 dmp->dm_bss_va = sh.sh_addr;
1229 } else if (strcmp(s, ".info") == 0 &&
1230 (dp = elf_getdata(sp, NULL)) != NULL) {
1231 bcopy(dp->d_buf, &dmp->dm_info,
1232 MIN(sh.sh_size, sizeof (dmp->dm_info)));
1233 } else if (strcmp(s, ".filename") == 0 &&
1234 (dp = elf_getdata(sp, NULL)) != NULL) {
1235 (void) strlcpy(dmp->dm_file,
1236 dp->d_buf, sizeof (dmp->dm_file));
1240 dmp->dm_flags |= DT_DM_KERNEL;
1242 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1245 * Include .rodata and special sections into .text.
1246 * This depends on default section layout produced by GNU ld
1247 * for ELF objects and libraries:
1248 * [Text][R/O data][R/W data][Dynamic][BSS][Non loadable]
1250 dmp->dm_text_size = dmp->dm_data_va - dmp->dm_text_va;
1251 #if defined(__i386__)
1253 * Find the first load section and figure out the relocation
1254 * offset for the symbols. The kernel module will not need
1255 * relocation, but the kernel linker modules will.
1257 for (i = 0; gelf_getphdr(dmp->dm_elf, i, &ph) != NULL; i++) {
1258 if (ph.p_type == PT_LOAD) {
1259 dmp->dm_reloc_offset = k_stat->address - ph.p_vaddr;
1264 #endif /* illumos */
1266 if (dmp->dm_info.objfs_info_primary)
1267 dmp->dm_flags |= DT_DM_PRIMARY;
1269 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1270 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1274 * Unload all the loaded modules and then refresh the module cache with the
1275 * latest list of loaded modules and their address ranges.
1278 dtrace_update(dtrace_hdl_t *dtp)
1282 #if defined(__FreeBSD__)
1286 for (dmp = dt_list_next(&dtp->dt_modlist);
1287 dmp != NULL; dmp = dt_list_next(dmp))
1288 dt_module_unload(dtp, dmp);
1292 * Open /system/object and attempt to create a libdtrace module for
1293 * each kernel module that is loaded on the current system.
1295 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1296 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1299 while ((dp = readdir(dirp)) != NULL) {
1300 if (dp->d_name[0] != '.')
1301 dt_module_update(dtp, dp->d_name);
1304 (void) closedir(dirp);
1306 #elif defined(__FreeBSD__)
1308 * Use FreeBSD's kernel loader interface to discover what kernel
1309 * modules are loaded and create a libdtrace module for each one.
1311 for (fileid = kldnext(0); fileid > 0; fileid = kldnext(fileid)) {
1312 struct kld_file_stat k_stat;
1313 k_stat.version = sizeof(k_stat);
1314 if (kldstat(fileid, &k_stat) == 0)
1315 dt_module_update(dtp, &k_stat);
1320 * Look up all the macro identifiers and set di_id to the latest value.
1321 * This code collaborates with dt_lex.l on the use of di_id. We will
1322 * need to implement something fancier if we need to support non-ints.
1324 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1325 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1326 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1327 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1328 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1329 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1331 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1333 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1335 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1337 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1340 * Cache the pointers to the modules representing the base executable
1341 * and the run-time linker in the dtrace client handle. Note that on
1342 * x86 krtld is folded into unix, so if we don't find it, use unix
1345 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1346 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1347 if (dtp->dt_rtld == NULL)
1348 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1351 * If this is the first time we are initializing the module list,
1352 * remove the module for genunix from the module list and then move it
1353 * to the front of the module list. We do this so that type and symbol
1354 * queries encounter genunix and thereby optimize for the common case
1355 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1357 if (dtp->dt_exec != NULL &&
1358 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1359 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1360 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1364 static dt_module_t *
1365 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1367 int err = EDT_NOMOD;
1370 switch ((uintptr_t)object) {
1371 case (uintptr_t)DTRACE_OBJ_EXEC:
1374 case (uintptr_t)DTRACE_OBJ_RTLD:
1377 case (uintptr_t)DTRACE_OBJ_CDEFS:
1378 dmp = dtp->dt_cdefs;
1380 case (uintptr_t)DTRACE_OBJ_DDEFS:
1381 dmp = dtp->dt_ddefs;
1384 dmp = dt_module_create(dtp, object);
1389 (void) dt_set_errno(dtp, err);
1395 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1396 * complete symbol information for the matching symbol.
1399 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1400 GElf_Sym *symp, dtrace_syminfo_t *sip)
1407 uint_t mask = 0; /* mask of dt_module flags to match */
1408 uint_t bits = 0; /* flag bits that must be present */
1410 if (object != DTRACE_OBJ_EVERY &&
1411 object != DTRACE_OBJ_KMODS &&
1412 object != DTRACE_OBJ_UMODS) {
1413 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1414 return (-1); /* dt_errno is set for us */
1416 if (dt_module_load(dtp, dmp) == -1)
1417 return (-1); /* dt_errno is set for us */
1421 if (object == DTRACE_OBJ_KMODS)
1422 mask = bits = DT_DM_KERNEL;
1423 else if (object == DTRACE_OBJ_UMODS)
1424 mask = DT_DM_KERNEL;
1426 dmp = dt_list_next(&dtp->dt_modlist);
1433 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1434 if ((dmp->dm_flags & mask) != bits)
1435 continue; /* failed to match required attributes */
1437 if (dt_module_load(dtp, dmp) == -1)
1438 continue; /* failed to load symbol table */
1440 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1442 sip->dts_object = dmp->dm_name;
1443 sip->dts_name = (const char *)
1444 dmp->dm_strtab.cts_data + symp->st_name;
1450 if (dmp->dm_extern != NULL &&
1451 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1453 symp->st_name = (uintptr_t)idp->di_name;
1455 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1457 symp->st_shndx = SHN_UNDEF;
1460 ctf_type_size(idp->di_ctfp, idp->di_type);
1464 sip->dts_object = dmp->dm_name;
1465 sip->dts_name = idp->di_name;
1466 sip->dts_id = idp->di_id;
1473 return (dt_set_errno(dtp, EDT_NOSYM));
1477 * Exported interface to look up a symbol by address. We return the GElf_Sym
1478 * and complete symbol information for the matching symbol.
1481 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1482 GElf_Sym *symp, dtrace_syminfo_t *sip)
1486 const dtrace_vector_t *v = dtp->dt_vector;
1489 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1491 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1492 dmp = dt_list_next(dmp)) {
1493 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1494 addr - dmp->dm_data_va < dmp->dm_data_size ||
1495 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1500 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1502 if (dt_module_load(dtp, dmp) == -1)
1503 return (-1); /* dt_errno is set for us */
1506 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1507 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1511 sip->dts_object = dmp->dm_name;
1514 sip->dts_name = (const char *)
1515 dmp->dm_strtab.cts_data + symp->st_name;
1518 sip->dts_name = NULL;
1527 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1528 dtrace_typeinfo_t *tip)
1530 dtrace_typeinfo_t ti;
1539 uint_t mask = 0; /* mask of dt_module flags to match */
1540 uint_t bits = 0; /* flag bits that must be present */
1542 if (object != DTRACE_OBJ_EVERY &&
1543 object != DTRACE_OBJ_KMODS &&
1544 object != DTRACE_OBJ_UMODS) {
1545 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1546 return (-1); /* dt_errno is set for us */
1548 if (dt_module_load(dtp, dmp) == -1)
1549 return (-1); /* dt_errno is set for us */
1553 if (object == DTRACE_OBJ_KMODS)
1554 mask = bits = DT_DM_KERNEL;
1555 else if (object == DTRACE_OBJ_UMODS)
1556 mask = DT_DM_KERNEL;
1558 dmp = dt_list_next(&dtp->dt_modlist);
1566 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1567 if ((dmp->dm_flags & mask) != bits)
1568 continue; /* failed to match required attributes */
1571 * If we can't load the CTF container, continue on to the next
1572 * module. If our search was scoped to only one module then
1573 * return immediately leaving dt_errno unmodified.
1575 if (dt_module_hasctf(dtp, dmp) == 0) {
1582 * Look up the type in the module's CTF container. If our
1583 * match is a forward declaration tag, save this choice in
1584 * 'tip' and keep going in the hope that we will locate the
1585 * underlying structure definition. Otherwise just return.
1587 if (dmp->dm_pid == 0) {
1588 id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1591 if ((p = strchr(name, '`')) != NULL) {
1594 return (dt_set_errno(dtp, EDT_NOMEM));
1595 p = strchr(buf, '`');
1596 if ((q = strchr(p + 1, '`')) != NULL)
1599 fp = dt_module_getctflib(dtp, dmp, buf);
1600 if (fp == NULL || (id = ctf_lookup_by_name(fp,
1605 for (i = 0; i < dmp->dm_nctflibs; i++) {
1606 fp = dmp->dm_libctfp[i];
1607 id = ctf_lookup_by_name(fp, name);
1613 if (id != CTF_ERR) {
1614 tip->dtt_object = dmp->dm_name;
1617 if (ctf_type_kind(fp, ctf_type_resolve(fp, id)) !=
1626 return (dt_set_errno(dtp, EDT_NOTYPE));
1632 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1633 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1637 tip->dtt_object = NULL;
1638 tip->dtt_ctfp = NULL;
1639 tip->dtt_type = CTF_ERR;
1642 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1643 return (dt_set_errno(dtp, EDT_NOMOD));
1645 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1647 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1650 return (dt_set_errno(dtp, EDT_NOSYM));
1652 tip->dtt_ctfp = idp->di_ctfp;
1653 tip->dtt_type = idp->di_type;
1655 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1656 if (dt_module_getctf(dtp, dmp) == NULL)
1657 return (-1); /* errno is set for us */
1659 tip->dtt_ctfp = dmp->dm_ctfp;
1660 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1662 if (tip->dtt_type == CTF_ERR) {
1663 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1664 return (dt_set_errno(dtp, EDT_CTF));
1668 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1669 tip->dtt_type = DT_FPTR_TYPE(dtp);
1672 tip->dtt_object = dmp->dm_name;
1676 static dtrace_objinfo_t *
1677 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1679 dto->dto_name = dmp->dm_name;
1680 dto->dto_file = dmp->dm_file;
1681 dto->dto_id = dmp->dm_modid;
1684 if (dmp->dm_flags & DT_DM_KERNEL)
1685 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1686 if (dmp->dm_flags & DT_DM_PRIMARY)
1687 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1689 dto->dto_text_va = dmp->dm_text_va;
1690 dto->dto_text_size = dmp->dm_text_size;
1691 dto->dto_data_va = dmp->dm_data_va;
1692 dto->dto_data_size = dmp->dm_data_size;
1693 dto->dto_bss_va = dmp->dm_bss_va;
1694 dto->dto_bss_size = dmp->dm_bss_size;
1700 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1702 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1703 dtrace_objinfo_t dto;
1706 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1707 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1715 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1719 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1720 object == DTRACE_OBJ_UMODS || dto == NULL)
1721 return (dt_set_errno(dtp, EINVAL));
1723 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1724 return (-1); /* dt_errno is set for us */
1726 if (dt_module_load(dtp, dmp) == -1)
1727 return (-1); /* dt_errno is set for us */
1729 (void) dt_module_info(dmp, dto);