import less(1)
[unleashed/tickless.git] / usr / src / lib / libdtrace / common / dt_module.c
blob24bd7f2278317cf623d5e233a28cfc93d64c4e3b
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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>
30 #include <sys/modctl.h>
31 #include <sys/kobj.h>
32 #include <sys/kobj_impl.h>
33 #include <sys/sysmacros.h>
34 #include <sys/elf.h>
35 #include <sys/task.h>
37 #include <unistd.h>
38 #include <project.h>
39 #include <strings.h>
40 #include <stdlib.h>
41 #include <libelf.h>
42 #include <limits.h>
43 #include <assert.h>
44 #include <errno.h>
45 #include <dirent.h>
47 #include <dt_strtab.h>
48 #include <dt_module.h>
49 #include <dt_impl.h>
51 static const char *dt_module_strtab; /* active strtab for qsort callbacks */
53 static void
54 dt_module_symhash_insert(dt_module_t *dmp, const char *name, uint_t id)
56 dt_sym_t *dsp = &dmp->dm_symchains[dmp->dm_symfree];
57 uint_t h;
59 assert(dmp->dm_symfree < dmp->dm_nsymelems + 1);
61 dsp->ds_symid = id;
62 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
63 dsp->ds_next = dmp->dm_symbuckets[h];
64 dmp->dm_symbuckets[h] = dmp->dm_symfree++;
67 static uint_t
68 dt_module_syminit32(dt_module_t *dmp)
70 #if STT_NUM != (STT_TLS + 1)
71 #error "STT_NUM has grown. update dt_module_syminit32()"
72 #endif
74 const Elf32_Sym *sym = dmp->dm_symtab.cts_data;
75 const char *base = dmp->dm_strtab.cts_data;
76 size_t ss_size = dmp->dm_strtab.cts_size;
77 uint_t i, n = dmp->dm_nsymelems;
78 uint_t asrsv = 0;
80 for (i = 0; i < n; i++, sym++) {
81 const char *name = base + sym->st_name;
82 uchar_t type = ELF32_ST_TYPE(sym->st_info);
84 if (type >= STT_NUM || type == STT_SECTION)
85 continue; /* skip sections and unknown types */
87 if (sym->st_name == 0 || sym->st_name >= ss_size)
88 continue; /* skip null or invalid names */
90 if (sym->st_value != 0 &&
91 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
92 asrsv++; /* reserve space in the address map */
94 dt_module_symhash_insert(dmp, name, i);
97 return (asrsv);
100 static uint_t
101 dt_module_syminit64(dt_module_t *dmp)
103 #if STT_NUM != (STT_TLS + 1)
104 #error "STT_NUM has grown. update dt_module_syminit64()"
105 #endif
107 const Elf64_Sym *sym = dmp->dm_symtab.cts_data;
108 const char *base = dmp->dm_strtab.cts_data;
109 size_t ss_size = dmp->dm_strtab.cts_size;
110 uint_t i, n = dmp->dm_nsymelems;
111 uint_t asrsv = 0;
113 for (i = 0; i < n; i++, sym++) {
114 const char *name = base + sym->st_name;
115 uchar_t type = ELF64_ST_TYPE(sym->st_info);
117 if (type >= STT_NUM || type == STT_SECTION)
118 continue; /* skip sections and unknown types */
120 if (sym->st_name == 0 || sym->st_name >= ss_size)
121 continue; /* skip null or invalid names */
123 if (sym->st_value != 0 &&
124 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
125 asrsv++; /* reserve space in the address map */
127 dt_module_symhash_insert(dmp, name, i);
130 return (asrsv);
134 * Sort comparison function for 32-bit symbol address-to-name lookups. We sort
135 * symbols by value. If values are equal, we prefer the symbol that is
136 * non-zero sized, typed, not weak, or lexically first, in that order.
138 static int
139 dt_module_symcomp32(const void *lp, const void *rp)
141 Elf32_Sym *lhs = *((Elf32_Sym **)lp);
142 Elf32_Sym *rhs = *((Elf32_Sym **)rp);
144 if (lhs->st_value != rhs->st_value)
145 return (lhs->st_value > rhs->st_value ? 1 : -1);
147 if ((lhs->st_size == 0) != (rhs->st_size == 0))
148 return (lhs->st_size == 0 ? 1 : -1);
150 if ((ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
151 (ELF32_ST_TYPE(rhs->st_info) == STT_NOTYPE))
152 return (ELF32_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
154 if ((ELF32_ST_BIND(lhs->st_info) == STB_WEAK) !=
155 (ELF32_ST_BIND(rhs->st_info) == STB_WEAK))
156 return (ELF32_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
158 return (strcmp(dt_module_strtab + lhs->st_name,
159 dt_module_strtab + rhs->st_name));
163 * Sort comparison function for 64-bit symbol address-to-name lookups. We sort
164 * symbols by value. If values are equal, we prefer the symbol that is
165 * non-zero sized, typed, not weak, or lexically first, in that order.
167 static int
168 dt_module_symcomp64(const void *lp, const void *rp)
170 Elf64_Sym *lhs = *((Elf64_Sym **)lp);
171 Elf64_Sym *rhs = *((Elf64_Sym **)rp);
173 if (lhs->st_value != rhs->st_value)
174 return (lhs->st_value > rhs->st_value ? 1 : -1);
176 if ((lhs->st_size == 0) != (rhs->st_size == 0))
177 return (lhs->st_size == 0 ? 1 : -1);
179 if ((ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE) !=
180 (ELF64_ST_TYPE(rhs->st_info) == STT_NOTYPE))
181 return (ELF64_ST_TYPE(lhs->st_info) == STT_NOTYPE ? 1 : -1);
183 if ((ELF64_ST_BIND(lhs->st_info) == STB_WEAK) !=
184 (ELF64_ST_BIND(rhs->st_info) == STB_WEAK))
185 return (ELF64_ST_BIND(lhs->st_info) == STB_WEAK ? 1 : -1);
187 return (strcmp(dt_module_strtab + lhs->st_name,
188 dt_module_strtab + rhs->st_name));
191 static void
192 dt_module_symsort32(dt_module_t *dmp)
194 Elf32_Sym *symtab = (Elf32_Sym *)dmp->dm_symtab.cts_data;
195 Elf32_Sym **sympp = (Elf32_Sym **)dmp->dm_asmap;
196 const dt_sym_t *dsp = dmp->dm_symchains + 1;
197 uint_t i, n = dmp->dm_symfree;
199 for (i = 1; i < n; i++, dsp++) {
200 Elf32_Sym *sym = symtab + dsp->ds_symid;
201 if (sym->st_value != 0 &&
202 (ELF32_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
203 *sympp++ = sym;
206 dmp->dm_aslen = (uint_t)(sympp - (Elf32_Sym **)dmp->dm_asmap);
207 assert(dmp->dm_aslen <= dmp->dm_asrsv);
209 dt_module_strtab = dmp->dm_strtab.cts_data;
210 qsort(dmp->dm_asmap, dmp->dm_aslen,
211 sizeof (Elf32_Sym *), dt_module_symcomp32);
212 dt_module_strtab = NULL;
215 static void
216 dt_module_symsort64(dt_module_t *dmp)
218 Elf64_Sym *symtab = (Elf64_Sym *)dmp->dm_symtab.cts_data;
219 Elf64_Sym **sympp = (Elf64_Sym **)dmp->dm_asmap;
220 const dt_sym_t *dsp = dmp->dm_symchains + 1;
221 uint_t i, n = dmp->dm_symfree;
223 for (i = 1; i < n; i++, dsp++) {
224 Elf64_Sym *sym = symtab + dsp->ds_symid;
225 if (sym->st_value != 0 &&
226 (ELF64_ST_BIND(sym->st_info) != STB_LOCAL || sym->st_size))
227 *sympp++ = sym;
230 dmp->dm_aslen = (uint_t)(sympp - (Elf64_Sym **)dmp->dm_asmap);
231 assert(dmp->dm_aslen <= dmp->dm_asrsv);
233 dt_module_strtab = dmp->dm_strtab.cts_data;
234 qsort(dmp->dm_asmap, dmp->dm_aslen,
235 sizeof (Elf64_Sym *), dt_module_symcomp64);
236 dt_module_strtab = NULL;
239 static GElf_Sym *
240 dt_module_symgelf32(const Elf32_Sym *src, GElf_Sym *dst)
242 if (dst != NULL) {
243 dst->st_name = src->st_name;
244 dst->st_info = src->st_info;
245 dst->st_other = src->st_other;
246 dst->st_shndx = src->st_shndx;
247 dst->st_value = src->st_value;
248 dst->st_size = src->st_size;
251 return (dst);
254 static GElf_Sym *
255 dt_module_symgelf64(const Elf64_Sym *src, GElf_Sym *dst)
257 if (dst != NULL)
258 bcopy(src, dst, sizeof (GElf_Sym));
260 return (dst);
263 static GElf_Sym *
264 dt_module_symname32(dt_module_t *dmp, const char *name,
265 GElf_Sym *symp, uint_t *idp)
267 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
268 const char *strtab = dmp->dm_strtab.cts_data;
270 const Elf32_Sym *sym;
271 const dt_sym_t *dsp;
272 uint_t i, h;
274 if (dmp->dm_nsymelems == 0)
275 return (NULL);
277 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
279 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
280 dsp = &dmp->dm_symchains[i];
281 sym = symtab + dsp->ds_symid;
283 if (strcmp(name, strtab + sym->st_name) == 0) {
284 if (idp != NULL)
285 *idp = dsp->ds_symid;
286 return (dt_module_symgelf32(sym, symp));
290 return (NULL);
293 static GElf_Sym *
294 dt_module_symname64(dt_module_t *dmp, const char *name,
295 GElf_Sym *symp, uint_t *idp)
297 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
298 const char *strtab = dmp->dm_strtab.cts_data;
300 const Elf64_Sym *sym;
301 const dt_sym_t *dsp;
302 uint_t i, h;
304 if (dmp->dm_nsymelems == 0)
305 return (NULL);
307 h = dt_strtab_hash(name, NULL) % dmp->dm_nsymbuckets;
309 for (i = dmp->dm_symbuckets[h]; i != 0; i = dsp->ds_next) {
310 dsp = &dmp->dm_symchains[i];
311 sym = symtab + dsp->ds_symid;
313 if (strcmp(name, strtab + sym->st_name) == 0) {
314 if (idp != NULL)
315 *idp = dsp->ds_symid;
316 return (dt_module_symgelf64(sym, symp));
320 return (NULL);
323 static GElf_Sym *
324 dt_module_symaddr32(dt_module_t *dmp, GElf_Addr addr,
325 GElf_Sym *symp, uint_t *idp)
327 const Elf32_Sym **asmap = (const Elf32_Sym **)dmp->dm_asmap;
328 const Elf32_Sym *symtab = dmp->dm_symtab.cts_data;
329 const Elf32_Sym *sym;
331 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
332 Elf32_Addr v;
334 if (dmp->dm_aslen == 0)
335 return (NULL);
337 while (hi - lo > 1) {
338 mid = (lo + hi) / 2;
339 if (addr >= asmap[mid]->st_value)
340 lo = mid;
341 else
342 hi = mid;
345 i = addr < asmap[hi]->st_value ? lo : hi;
346 sym = asmap[i];
347 v = sym->st_value;
350 * If the previous entry has the same value, improve our choice. The
351 * order of equal-valued symbols is determined by the comparison func.
353 while (i-- != 0 && asmap[i]->st_value == v)
354 sym = asmap[i];
356 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
357 if (idp != NULL)
358 *idp = (uint_t)(sym - symtab);
359 return (dt_module_symgelf32(sym, symp));
362 return (NULL);
365 static GElf_Sym *
366 dt_module_symaddr64(dt_module_t *dmp, GElf_Addr addr,
367 GElf_Sym *symp, uint_t *idp)
369 const Elf64_Sym **asmap = (const Elf64_Sym **)dmp->dm_asmap;
370 const Elf64_Sym *symtab = dmp->dm_symtab.cts_data;
371 const Elf64_Sym *sym;
373 uint_t i, mid, lo = 0, hi = dmp->dm_aslen - 1;
374 Elf64_Addr v;
376 if (dmp->dm_aslen == 0)
377 return (NULL);
379 while (hi - lo > 1) {
380 mid = (lo + hi) / 2;
381 if (addr >= asmap[mid]->st_value)
382 lo = mid;
383 else
384 hi = mid;
387 i = addr < asmap[hi]->st_value ? lo : hi;
388 sym = asmap[i];
389 v = sym->st_value;
392 * If the previous entry has the same value, improve our choice. The
393 * order of equal-valued symbols is determined by the comparison func.
395 while (i-- != 0 && asmap[i]->st_value == v)
396 sym = asmap[i];
398 if (addr - sym->st_value < MAX(sym->st_size, 1)) {
399 if (idp != NULL)
400 *idp = (uint_t)(sym - symtab);
401 return (dt_module_symgelf64(sym, symp));
404 return (NULL);
407 static const dt_modops_t dt_modops_32 = {
408 dt_module_syminit32,
409 dt_module_symsort32,
410 dt_module_symname32,
411 dt_module_symaddr32
414 static const dt_modops_t dt_modops_64 = {
415 dt_module_syminit64,
416 dt_module_symsort64,
417 dt_module_symname64,
418 dt_module_symaddr64
421 dt_module_t *
422 dt_module_create(dtrace_hdl_t *dtp, const char *name)
424 long pid;
425 char *eptr;
426 dt_ident_t *idp;
427 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
428 dt_module_t *dmp;
430 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
431 if (strcmp(dmp->dm_name, name) == 0)
432 return (dmp);
435 if ((dmp = malloc(sizeof (dt_module_t))) == NULL)
436 return (NULL); /* caller must handle allocation failure */
438 bzero(dmp, sizeof (dt_module_t));
439 (void) strlcpy(dmp->dm_name, name, sizeof (dmp->dm_name));
440 dt_list_append(&dtp->dt_modlist, dmp);
441 dmp->dm_next = dtp->dt_mods[h];
442 dtp->dt_mods[h] = dmp;
443 dtp->dt_nmods++;
445 if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
446 dmp->dm_ops = &dt_modops_64;
447 else
448 dmp->dm_ops = &dt_modops_32;
451 * Modules for userland processes are special. They always refer to a
452 * specific process and have a copy of their CTF data from a specific
453 * instant in time. Any dt_module_t that begins with 'pid' is a module
454 * for a specific process, much like how any probe description that
455 * begins with 'pid' is special. pid123 refers to process 123. A module
456 * that is just 'pid' refers specifically to pid$target. This is
457 * generally done as D does not currently allow for macros to be
458 * evaluated when working with types.
460 if (strncmp(dmp->dm_name, "pid", 3) == 0) {
461 errno = 0;
462 if (dmp->dm_name[3] == '\0') {
463 idp = dt_idhash_lookup(dtp->dt_macros, "target");
464 if (idp != NULL && idp->di_id != 0)
465 dmp->dm_pid = idp->di_id;
466 } else {
467 pid = strtol(dmp->dm_name + 3, &eptr, 10);
468 if (errno == 0 && *eptr == '\0')
469 dmp->dm_pid = (pid_t)pid;
470 else
471 dt_dprintf("encountered malformed pid "
472 "module: %s\n", dmp->dm_name);
476 return (dmp);
479 dt_module_t *
480 dt_module_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
482 uint_t h = dt_strtab_hash(name, NULL) % dtp->dt_modbuckets;
483 dt_module_t *dmp;
485 for (dmp = dtp->dt_mods[h]; dmp != NULL; dmp = dmp->dm_next) {
486 if (strcmp(dmp->dm_name, name) == 0)
487 return (dmp);
490 return (NULL);
493 /*ARGSUSED*/
494 dt_module_t *
495 dt_module_lookup_by_ctf(dtrace_hdl_t *dtp, ctf_file_t *ctfp)
497 return (ctfp ? ctf_getspecific(ctfp) : NULL);
500 static int
501 dt_module_load_sect(dtrace_hdl_t *dtp, dt_module_t *dmp, ctf_sect_t *ctsp)
503 const char *s;
504 size_t shstrs;
505 GElf_Shdr sh;
506 Elf_Data *dp;
507 Elf_Scn *sp;
509 if (elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1)
510 return (dt_set_errno(dtp, EDT_NOTLOADED));
512 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
513 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
514 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
515 continue; /* skip any malformed sections */
517 if (sh.sh_type == ctsp->cts_type &&
518 sh.sh_entsize == ctsp->cts_entsize &&
519 strcmp(s, ctsp->cts_name) == 0)
520 break; /* section matches specification */
524 * If the section isn't found, return success but leave cts_data set
525 * to NULL and cts_size set to zero for our caller.
527 if (sp == NULL || (dp = elf_getdata(sp, NULL)) == NULL)
528 return (0);
530 ctsp->cts_data = dp->d_buf;
531 ctsp->cts_size = dp->d_size;
533 dt_dprintf("loaded %s [%s] (%lu bytes)\n",
534 dmp->dm_name, ctsp->cts_name, (ulong_t)ctsp->cts_size);
536 return (0);
539 typedef struct dt_module_cb_arg {
540 struct ps_prochandle *dpa_proc;
541 dtrace_hdl_t *dpa_dtp;
542 dt_module_t *dpa_dmp;
543 uint_t dpa_count;
544 } dt_module_cb_arg_t;
546 /* ARGSUSED */
547 static int
548 dt_module_load_proc_count(void *arg, const prmap_t *prmap, const char *obj)
550 ctf_file_t *fp;
551 dt_module_cb_arg_t *dcp = arg;
553 /* Try to grab a ctf container if it exists */
554 fp = Pname_to_ctf(dcp->dpa_proc, obj);
555 if (fp != NULL)
556 dcp->dpa_count++;
557 return (0);
560 /* ARGSUSED */
561 static int
562 dt_module_load_proc_build(void *arg, const prmap_t *prmap, const char *obj)
564 ctf_file_t *fp;
565 char buf[MAXPATHLEN], *p;
566 dt_module_cb_arg_t *dcp = arg;
567 int count = dcp->dpa_count;
568 Lmid_t lmid;
570 fp = Pname_to_ctf(dcp->dpa_proc, obj);
571 if (fp == NULL)
572 return (0);
573 fp = ctf_dup(fp);
574 if (fp == NULL)
575 return (0);
576 dcp->dpa_dmp->dm_libctfp[count] = fp;
578 * While it'd be nice to simply use objname here, because of our prior
579 * actions we'll always get a resolved object name to its on disk file.
580 * Like the pid provider, we need to tell a bit of a lie here. The type
581 * that the user thinks of is in terms of the libraries they requested,
582 * eg. libc.so.1, they don't care about the fact that it's
583 * libc_hwcap.so.1.
585 (void) Pobjname(dcp->dpa_proc, prmap->pr_vaddr, buf, sizeof (buf));
586 if ((p = strrchr(buf, '/')) == NULL)
587 p = buf;
588 else
589 p++;
592 * If for some reason we can't find a link map id for this module, which
593 * would be really quite weird. We instead just say the link map id is
594 * zero.
596 if (Plmid(dcp->dpa_proc, prmap->pr_vaddr, &lmid) != 0)
597 lmid = 0;
599 if (lmid == 0)
600 dcp->dpa_dmp->dm_libctfn[count] = strdup(p);
601 else
602 (void) asprintf(&dcp->dpa_dmp->dm_libctfn[count],
603 "LM%lx`%s", lmid, p);
604 if (dcp->dpa_dmp->dm_libctfn[count] == NULL)
605 return (1);
606 ctf_setspecific(fp, dcp->dpa_dmp);
607 dcp->dpa_count++;
608 return (0);
612 * We've been asked to load data that belongs to another process. As such we're
613 * going to pgrab it at this instant, load everything that we might ever care
614 * about, and then drive on. The reason for this is that the process that we're
615 * interested in might be changing. As long as we have grabbed it, then this
616 * can't be a problem for us.
618 * For now, we're actually going to punt on most things and just try to get CTF
619 * data, nothing else. Basically this is only useful as a source of type
620 * information, we can't go and do the stacktrace lookups, etc.
622 static int
623 dt_module_load_proc(dtrace_hdl_t *dtp, dt_module_t *dmp)
625 struct ps_prochandle *p;
626 dt_module_cb_arg_t arg;
629 * Note that on success we do not release this hold. We must hold this
630 * for our life time.
632 p = dt_proc_grab(dtp, dmp->dm_pid, 0, PGRAB_RDONLY | PGRAB_FORCE);
633 if (p == NULL) {
634 dt_dprintf("failed to grab pid: %d\n", (int)dmp->dm_pid);
635 return (dt_set_errno(dtp, EDT_CANTLOAD));
637 dt_proc_lock(dtp, p);
639 arg.dpa_proc = p;
640 arg.dpa_dtp = dtp;
641 arg.dpa_dmp = dmp;
642 arg.dpa_count = 0;
643 if (Pobject_iter_resolved(p, dt_module_load_proc_count, &arg) != 0) {
644 dt_dprintf("failed to iterate objects\n");
645 dt_proc_release(dtp, p);
646 return (dt_set_errno(dtp, EDT_CANTLOAD));
649 if (arg.dpa_count == 0) {
650 dt_dprintf("no ctf data present\n");
651 dt_proc_unlock(dtp, p);
652 dt_proc_release(dtp, p);
653 return (dt_set_errno(dtp, EDT_CANTLOAD));
656 dmp->dm_libctfp = malloc(sizeof (ctf_file_t *) * arg.dpa_count);
657 if (dmp->dm_libctfp == NULL) {
658 dt_proc_unlock(dtp, p);
659 dt_proc_release(dtp, p);
660 return (dt_set_errno(dtp, EDT_NOMEM));
662 bzero(dmp->dm_libctfp, sizeof (ctf_file_t *) * arg.dpa_count);
664 dmp->dm_libctfn = malloc(sizeof (char *) * arg.dpa_count);
665 if (dmp->dm_libctfn == NULL) {
666 free(dmp->dm_libctfp);
667 dt_proc_unlock(dtp, p);
668 dt_proc_release(dtp, p);
669 return (dt_set_errno(dtp, EDT_NOMEM));
671 bzero(dmp->dm_libctfn, sizeof (char *) * arg.dpa_count);
673 dmp->dm_nctflibs = arg.dpa_count;
675 arg.dpa_count = 0;
676 if (Pobject_iter_resolved(p, dt_module_load_proc_build, &arg) != 0) {
677 dt_proc_unlock(dtp, p);
678 dt_module_unload(dtp, dmp);
679 dt_proc_release(dtp, p);
680 return (dt_set_errno(dtp, EDT_CANTLOAD));
682 assert(arg.dpa_count == dmp->dm_nctflibs);
683 dt_dprintf("loaded %d ctf modules for pid %d\n", arg.dpa_count,
684 (int)dmp->dm_pid);
686 dt_proc_unlock(dtp, p);
687 dt_proc_release(dtp, p);
688 dmp->dm_flags |= DT_DM_LOADED;
690 return (0);
694 dt_module_load(dtrace_hdl_t *dtp, dt_module_t *dmp)
696 if (dmp->dm_flags & DT_DM_LOADED)
697 return (0); /* module is already loaded */
699 if (dmp->dm_pid != 0)
700 return (dt_module_load_proc(dtp, dmp));
702 dmp->dm_ctdata.cts_name = ".SUNW_ctf";
703 dmp->dm_ctdata.cts_type = SHT_PROGBITS;
704 dmp->dm_ctdata.cts_flags = 0;
705 dmp->dm_ctdata.cts_data = NULL;
706 dmp->dm_ctdata.cts_size = 0;
707 dmp->dm_ctdata.cts_entsize = 0;
708 dmp->dm_ctdata.cts_offset = 0;
710 dmp->dm_symtab.cts_name = ".symtab";
711 dmp->dm_symtab.cts_type = SHT_SYMTAB;
712 dmp->dm_symtab.cts_flags = 0;
713 dmp->dm_symtab.cts_data = NULL;
714 dmp->dm_symtab.cts_size = 0;
715 dmp->dm_symtab.cts_entsize = dmp->dm_ops == &dt_modops_64 ?
716 sizeof (Elf64_Sym) : sizeof (Elf32_Sym);
717 dmp->dm_symtab.cts_offset = 0;
719 dmp->dm_strtab.cts_name = ".strtab";
720 dmp->dm_strtab.cts_type = SHT_STRTAB;
721 dmp->dm_strtab.cts_flags = 0;
722 dmp->dm_strtab.cts_data = NULL;
723 dmp->dm_strtab.cts_size = 0;
724 dmp->dm_strtab.cts_entsize = 0;
725 dmp->dm_strtab.cts_offset = 0;
728 * Attempt to load the module's CTF section, symbol table section, and
729 * string table section. Note that modules may not contain CTF data:
730 * this will result in a successful load_sect but data of size zero.
731 * We will then fail if dt_module_getctf() is called, as shown below.
733 if (dt_module_load_sect(dtp, dmp, &dmp->dm_ctdata) == -1 ||
734 dt_module_load_sect(dtp, dmp, &dmp->dm_symtab) == -1 ||
735 dt_module_load_sect(dtp, dmp, &dmp->dm_strtab) == -1) {
736 dt_module_unload(dtp, dmp);
737 return (-1); /* dt_errno is set for us */
741 * Allocate the hash chains and hash buckets for symbol name lookup.
742 * This is relatively simple since the symbol table is of fixed size
743 * and is known in advance. We allocate one extra element since we
744 * use element indices instead of pointers and zero is our sentinel.
746 dmp->dm_nsymelems =
747 dmp->dm_symtab.cts_size / dmp->dm_symtab.cts_entsize;
749 dmp->dm_nsymbuckets = _dtrace_strbuckets;
750 dmp->dm_symfree = 1; /* first free element is index 1 */
752 dmp->dm_symbuckets = malloc(sizeof (uint_t) * dmp->dm_nsymbuckets);
753 dmp->dm_symchains = malloc(sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
755 if (dmp->dm_symbuckets == NULL || dmp->dm_symchains == NULL) {
756 dt_module_unload(dtp, dmp);
757 return (dt_set_errno(dtp, EDT_NOMEM));
760 bzero(dmp->dm_symbuckets, sizeof (uint_t) * dmp->dm_nsymbuckets);
761 bzero(dmp->dm_symchains, sizeof (dt_sym_t) * dmp->dm_nsymelems + 1);
764 * Iterate over the symbol table data buffer and insert each symbol
765 * name into the name hash if the name and type are valid. Then
766 * allocate the address map, fill it in, and sort it.
768 dmp->dm_asrsv = dmp->dm_ops->do_syminit(dmp);
770 dt_dprintf("hashed %s [%s] (%u symbols)\n",
771 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_symfree - 1);
773 if ((dmp->dm_asmap = malloc(sizeof (void *) * dmp->dm_asrsv)) == NULL) {
774 dt_module_unload(dtp, dmp);
775 return (dt_set_errno(dtp, EDT_NOMEM));
778 dmp->dm_ops->do_symsort(dmp);
780 dt_dprintf("sorted %s [%s] (%u symbols)\n",
781 dmp->dm_name, dmp->dm_symtab.cts_name, dmp->dm_aslen);
783 dmp->dm_flags |= DT_DM_LOADED;
784 return (0);
788 dt_module_hasctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
790 if (dmp->dm_pid != 0 && dmp->dm_nctflibs > 0)
791 return (1);
792 return (dt_module_getctf(dtp, dmp) != NULL);
795 ctf_file_t *
796 dt_module_getctf(dtrace_hdl_t *dtp, dt_module_t *dmp)
798 const char *parent;
799 dt_module_t *pmp;
800 ctf_file_t *pfp;
801 int model;
803 if (dmp->dm_ctfp != NULL || dt_module_load(dtp, dmp) != 0)
804 return (dmp->dm_ctfp);
806 if (dmp->dm_ops == &dt_modops_64)
807 model = CTF_MODEL_LP64;
808 else
809 model = CTF_MODEL_ILP32;
812 * If the data model of the module does not match our program data
813 * model, then do not permit CTF from this module to be opened and
814 * returned to the compiler. If we support mixed data models in the
815 * future for combined kernel/user tracing, this can be removed.
817 if (dtp->dt_conf.dtc_ctfmodel != model) {
818 (void) dt_set_errno(dtp, EDT_DATAMODEL);
819 return (NULL);
822 if (dmp->dm_ctdata.cts_size == 0) {
823 (void) dt_set_errno(dtp, EDT_NOCTF);
824 return (NULL);
827 dmp->dm_ctfp = ctf_bufopen(&dmp->dm_ctdata,
828 &dmp->dm_symtab, &dmp->dm_strtab, &dtp->dt_ctferr);
830 if (dmp->dm_ctfp == NULL) {
831 (void) dt_set_errno(dtp, EDT_CTF);
832 return (NULL);
835 (void) ctf_setmodel(dmp->dm_ctfp, model);
836 ctf_setspecific(dmp->dm_ctfp, dmp);
838 if ((parent = ctf_parent_name(dmp->dm_ctfp)) != NULL) {
839 if ((pmp = dt_module_create(dtp, parent)) == NULL ||
840 (pfp = dt_module_getctf(dtp, pmp)) == NULL) {
841 if (pmp == NULL)
842 (void) dt_set_errno(dtp, EDT_NOMEM);
843 goto err;
847 * If the label we claim the parent must have is not actually
848 * present in the parent module, ignore the CTF entirely
849 * rather than acquiring possibly bad type references.
851 if (ctf_label_info(pfp, ctf_parent_label(dmp->dm_ctfp),
852 NULL) == CTF_ERR) {
853 (void) dt_set_errno(dtp, EDT_BADCTF);
854 goto err;
857 if (ctf_import(dmp->dm_ctfp, pfp) == CTF_ERR) {
858 dtp->dt_ctferr = ctf_errno(dmp->dm_ctfp);
859 (void) dt_set_errno(dtp, EDT_CTF);
860 goto err;
864 dt_dprintf("loaded CTF container for %s (%p)\n",
865 dmp->dm_name, (void *)dmp->dm_ctfp);
867 return (dmp->dm_ctfp);
869 err:
870 dt_dprintf("could not load CTF container for %s: %s\n",
871 dmp->dm_name, dtrace_errmsg(dtp, dtrace_errno(dtp)));
872 ctf_close(dmp->dm_ctfp);
873 dmp->dm_ctfp = NULL;
874 return (NULL);
877 /*ARGSUSED*/
878 void
879 dt_module_unload(dtrace_hdl_t *dtp, dt_module_t *dmp)
881 int i;
883 ctf_close(dmp->dm_ctfp);
884 dmp->dm_ctfp = NULL;
886 if (dmp->dm_libctfp != NULL) {
887 for (i = 0; i < dmp->dm_nctflibs; i++) {
888 ctf_close(dmp->dm_libctfp[i]);
889 free(dmp->dm_libctfn[i]);
891 free(dmp->dm_libctfp);
892 free(dmp->dm_libctfn);
893 dmp->dm_libctfp = NULL;
894 dmp->dm_nctflibs = 0;
897 bzero(&dmp->dm_ctdata, sizeof (ctf_sect_t));
898 bzero(&dmp->dm_symtab, sizeof (ctf_sect_t));
899 bzero(&dmp->dm_strtab, sizeof (ctf_sect_t));
901 if (dmp->dm_symbuckets != NULL) {
902 free(dmp->dm_symbuckets);
903 dmp->dm_symbuckets = NULL;
906 if (dmp->dm_symchains != NULL) {
907 free(dmp->dm_symchains);
908 dmp->dm_symchains = NULL;
911 if (dmp->dm_asmap != NULL) {
912 free(dmp->dm_asmap);
913 dmp->dm_asmap = NULL;
916 dmp->dm_symfree = 0;
917 dmp->dm_nsymbuckets = 0;
918 dmp->dm_nsymelems = 0;
919 dmp->dm_asrsv = 0;
920 dmp->dm_aslen = 0;
922 dmp->dm_text_va = (uintptr_t)NULL;
923 dmp->dm_text_size = 0;
924 dmp->dm_data_va = (uintptr_t)NULL;
925 dmp->dm_data_size = 0;
926 dmp->dm_bss_va = (uintptr_t)NULL;
927 dmp->dm_bss_size = 0;
929 if (dmp->dm_extern != NULL) {
930 dt_idhash_destroy(dmp->dm_extern);
931 dmp->dm_extern = NULL;
934 (void) elf_end(dmp->dm_elf);
935 dmp->dm_elf = NULL;
937 dmp->dm_pid = 0;
939 dmp->dm_flags &= ~DT_DM_LOADED;
942 void
943 dt_module_destroy(dtrace_hdl_t *dtp, dt_module_t *dmp)
945 uint_t h = dt_strtab_hash(dmp->dm_name, NULL) % dtp->dt_modbuckets;
946 dt_module_t **dmpp = &dtp->dt_mods[h];
948 dt_list_delete(&dtp->dt_modlist, dmp);
949 assert(dtp->dt_nmods != 0);
950 dtp->dt_nmods--;
953 * Now remove this module from its hash chain. We expect to always
954 * find the module on its hash chain, so in this loop we assert that
955 * we don't run off the end of the list.
957 while (*dmpp != dmp) {
958 dmpp = &((*dmpp)->dm_next);
959 assert(*dmpp != NULL);
962 *dmpp = dmp->dm_next;
964 dt_module_unload(dtp, dmp);
965 free(dmp);
969 * Insert a new external symbol reference into the specified module. The new
970 * symbol will be marked as undefined and is assigned a symbol index beyond
971 * any existing cached symbols from this module. We use the ident's di_data
972 * field to store a pointer to a copy of the dtrace_syminfo_t for this symbol.
974 dt_ident_t *
975 dt_module_extern(dtrace_hdl_t *dtp, dt_module_t *dmp,
976 const char *name, const dtrace_typeinfo_t *tip)
978 dtrace_syminfo_t *sip;
979 dt_ident_t *idp;
980 uint_t id;
982 if (dmp->dm_extern == NULL && (dmp->dm_extern = dt_idhash_create(
983 "extern", NULL, dmp->dm_nsymelems, UINT_MAX)) == NULL) {
984 (void) dt_set_errno(dtp, EDT_NOMEM);
985 return (NULL);
988 if (dt_idhash_nextid(dmp->dm_extern, &id) == -1) {
989 (void) dt_set_errno(dtp, EDT_SYMOFLOW);
990 return (NULL);
993 if ((sip = malloc(sizeof (dtrace_syminfo_t))) == NULL) {
994 (void) dt_set_errno(dtp, EDT_NOMEM);
995 return (NULL);
998 idp = dt_idhash_insert(dmp->dm_extern, name, DT_IDENT_SYMBOL, 0, id,
999 _dtrace_symattr, 0, &dt_idops_thaw, NULL, dtp->dt_gen);
1001 if (idp == NULL) {
1002 (void) dt_set_errno(dtp, EDT_NOMEM);
1003 free(sip);
1004 return (NULL);
1007 sip->dts_object = dmp->dm_name;
1008 sip->dts_name = idp->di_name;
1009 sip->dts_id = idp->di_id;
1011 idp->di_data = sip;
1012 idp->di_ctfp = tip->dtt_ctfp;
1013 idp->di_type = tip->dtt_type;
1015 return (idp);
1018 const char *
1019 dt_module_modelname(dt_module_t *dmp)
1021 if (dmp->dm_ops == &dt_modops_64)
1022 return ("64-bit");
1023 else
1024 return ("32-bit");
1027 /* ARGSUSED */
1029 dt_module_getlibid(dtrace_hdl_t *dtp, dt_module_t *dmp, const ctf_file_t *fp)
1031 int i;
1033 for (i = 0; i < dmp->dm_nctflibs; i++) {
1034 if (dmp->dm_libctfp[i] == fp)
1035 return (i);
1038 return (-1);
1041 /* ARGSUSED */
1042 ctf_file_t *
1043 dt_module_getctflib(dtrace_hdl_t *dtp, dt_module_t *dmp, const char *name)
1045 int i;
1047 for (i = 0; i < dmp->dm_nctflibs; i++) {
1048 if (strcmp(dmp->dm_libctfn[i], name) == 0)
1049 return (dmp->dm_libctfp[i]);
1052 return (NULL);
1056 * Update our module cache by adding an entry for the specified module 'name'.
1057 * We create the dt_module_t and populate it using /system/object/<name>/.
1059 static void
1060 dt_module_update(dtrace_hdl_t *dtp, const char *name)
1062 char fname[MAXPATHLEN];
1063 struct stat64 st;
1064 int fd, err, bits;
1066 dt_module_t *dmp;
1067 const char *s;
1068 size_t shstrs;
1069 GElf_Shdr sh;
1070 Elf_Data *dp;
1071 Elf_Scn *sp;
1073 (void) snprintf(fname, sizeof (fname),
1074 "%s/%s/object", OBJFS_ROOT, name);
1076 if ((fd = open(fname, O_RDONLY)) == -1 || fstat64(fd, &st) == -1 ||
1077 (dmp = dt_module_create(dtp, name)) == NULL) {
1078 dt_dprintf("failed to open %s: %s\n", fname, strerror(errno));
1079 (void) close(fd);
1080 return;
1084 * Since the module can unload out from under us (and /system/object
1085 * will return ENOENT), tell libelf to cook the entire file now and
1086 * then close the underlying file descriptor immediately. If this
1087 * succeeds, we know that we can continue safely using dmp->dm_elf.
1089 dmp->dm_elf = elf_begin(fd, ELF_C_READ, NULL);
1090 err = elf_cntl(dmp->dm_elf, ELF_C_FDREAD);
1091 (void) close(fd);
1093 if (dmp->dm_elf == NULL || err == -1 ||
1094 elf_getshdrstrndx(dmp->dm_elf, &shstrs) == -1) {
1095 dt_dprintf("failed to load %s: %s\n",
1096 fname, elf_errmsg(elf_errno()));
1097 dt_module_destroy(dtp, dmp);
1098 return;
1101 switch (gelf_getclass(dmp->dm_elf)) {
1102 case ELFCLASS32:
1103 dmp->dm_ops = &dt_modops_32;
1104 bits = 32;
1105 break;
1106 case ELFCLASS64:
1107 dmp->dm_ops = &dt_modops_64;
1108 bits = 64;
1109 break;
1110 default:
1111 dt_dprintf("failed to load %s: unknown ELF class\n", fname);
1112 dt_module_destroy(dtp, dmp);
1113 return;
1117 * Iterate over the section headers locating various sections of
1118 * interest and use their attributes to flesh out the dt_module_t.
1120 for (sp = NULL; (sp = elf_nextscn(dmp->dm_elf, sp)) != NULL; ) {
1121 if (gelf_getshdr(sp, &sh) == NULL || sh.sh_type == SHT_NULL ||
1122 (s = elf_strptr(dmp->dm_elf, shstrs, sh.sh_name)) == NULL)
1123 continue; /* skip any malformed sections */
1125 if (strcmp(s, ".text") == 0) {
1126 dmp->dm_text_size = sh.sh_size;
1127 dmp->dm_text_va = sh.sh_addr;
1128 } else if (strcmp(s, ".data") == 0) {
1129 dmp->dm_data_size = sh.sh_size;
1130 dmp->dm_data_va = sh.sh_addr;
1131 } else if (strcmp(s, ".bss") == 0) {
1132 dmp->dm_bss_size = sh.sh_size;
1133 dmp->dm_bss_va = sh.sh_addr;
1134 } else if (strcmp(s, ".info") == 0 &&
1135 (dp = elf_getdata(sp, NULL)) != NULL) {
1136 bcopy(dp->d_buf, &dmp->dm_info,
1137 MIN(sh.sh_size, sizeof (dmp->dm_info)));
1138 } else if (strcmp(s, ".filename") == 0 &&
1139 (dp = elf_getdata(sp, NULL)) != NULL) {
1140 (void) strlcpy(dmp->dm_file,
1141 dp->d_buf, sizeof (dmp->dm_file));
1145 dmp->dm_flags |= DT_DM_KERNEL;
1146 dmp->dm_modid = (int)OBJFS_MODID(st.st_ino);
1148 if (dmp->dm_info.objfs_info_primary)
1149 dmp->dm_flags |= DT_DM_PRIMARY;
1151 dt_dprintf("opened %d-bit module %s (%s) [%d]\n",
1152 bits, dmp->dm_name, dmp->dm_file, dmp->dm_modid);
1156 * Unload all the loaded modules and then refresh the module cache with the
1157 * latest list of loaded modules and their address ranges.
1159 void
1160 dtrace_update(dtrace_hdl_t *dtp)
1162 dt_module_t *dmp;
1163 DIR *dirp;
1165 for (dmp = dt_list_next(&dtp->dt_modlist);
1166 dmp != NULL; dmp = dt_list_next(dmp))
1167 dt_module_unload(dtp, dmp);
1170 * Open /system/object and attempt to create a libdtrace module for
1171 * each kernel module that is loaded on the current system.
1173 if (!(dtp->dt_oflags & DTRACE_O_NOSYS) &&
1174 (dirp = opendir(OBJFS_ROOT)) != NULL) {
1175 struct dirent *dp;
1177 while ((dp = readdir(dirp)) != NULL) {
1178 if (dp->d_name[0] != '.')
1179 dt_module_update(dtp, dp->d_name);
1182 (void) closedir(dirp);
1186 * Look up all the macro identifiers and set di_id to the latest value.
1187 * This code collaborates with dt_lex.l on the use of di_id. We will
1188 * need to implement something fancier if we need to support non-ints.
1190 dt_idhash_lookup(dtp->dt_macros, "egid")->di_id = getegid();
1191 dt_idhash_lookup(dtp->dt_macros, "euid")->di_id = geteuid();
1192 dt_idhash_lookup(dtp->dt_macros, "gid")->di_id = getgid();
1193 dt_idhash_lookup(dtp->dt_macros, "pid")->di_id = getpid();
1194 dt_idhash_lookup(dtp->dt_macros, "pgid")->di_id = getpgid(0);
1195 dt_idhash_lookup(dtp->dt_macros, "ppid")->di_id = getppid();
1196 dt_idhash_lookup(dtp->dt_macros, "projid")->di_id = getprojid();
1197 dt_idhash_lookup(dtp->dt_macros, "sid")->di_id = getsid(0);
1198 dt_idhash_lookup(dtp->dt_macros, "taskid")->di_id = gettaskid();
1199 dt_idhash_lookup(dtp->dt_macros, "uid")->di_id = getuid();
1202 * Cache the pointers to the modules representing the base executable
1203 * and the run-time linker in the dtrace client handle. Note that on
1204 * x86 krtld is folded into unix, so if we don't find it, use unix
1205 * instead.
1207 dtp->dt_exec = dt_module_lookup_by_name(dtp, "genunix");
1208 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "krtld");
1209 if (dtp->dt_rtld == NULL)
1210 dtp->dt_rtld = dt_module_lookup_by_name(dtp, "unix");
1213 * If this is the first time we are initializing the module list,
1214 * remove the module for genunix from the module list and then move it
1215 * to the front of the module list. We do this so that type and symbol
1216 * queries encounter genunix and thereby optimize for the common case
1217 * in dtrace_lookup_by_name() and dtrace_lookup_by_type(), below.
1219 if (dtp->dt_exec != NULL &&
1220 dtp->dt_cdefs == NULL && dtp->dt_ddefs == NULL) {
1221 dt_list_delete(&dtp->dt_modlist, dtp->dt_exec);
1222 dt_list_prepend(&dtp->dt_modlist, dtp->dt_exec);
1226 static dt_module_t *
1227 dt_module_from_object(dtrace_hdl_t *dtp, const char *object)
1229 int err = EDT_NOMOD;
1230 dt_module_t *dmp;
1232 switch ((uintptr_t)object) {
1233 case (uintptr_t)DTRACE_OBJ_EXEC:
1234 dmp = dtp->dt_exec;
1235 break;
1236 case (uintptr_t)DTRACE_OBJ_RTLD:
1237 dmp = dtp->dt_rtld;
1238 break;
1239 case (uintptr_t)DTRACE_OBJ_CDEFS:
1240 dmp = dtp->dt_cdefs;
1241 break;
1242 case (uintptr_t)DTRACE_OBJ_DDEFS:
1243 dmp = dtp->dt_ddefs;
1244 break;
1245 default:
1246 dmp = dt_module_create(dtp, object);
1247 err = EDT_NOMEM;
1250 if (dmp == NULL)
1251 (void) dt_set_errno(dtp, err);
1253 return (dmp);
1257 * Exported interface to look up a symbol by name. We return the GElf_Sym and
1258 * complete symbol information for the matching symbol.
1261 dtrace_lookup_by_name(dtrace_hdl_t *dtp, const char *object, const char *name,
1262 GElf_Sym *symp, dtrace_syminfo_t *sip)
1264 dt_module_t *dmp;
1265 dt_ident_t *idp;
1266 uint_t n, id;
1267 GElf_Sym sym;
1269 uint_t mask = 0; /* mask of dt_module flags to match */
1270 uint_t bits = 0; /* flag bits that must be present */
1272 if (object != DTRACE_OBJ_EVERY &&
1273 object != DTRACE_OBJ_KMODS &&
1274 object != DTRACE_OBJ_UMODS) {
1275 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1276 return (-1); /* dt_errno is set for us */
1278 if (dt_module_load(dtp, dmp) == -1)
1279 return (-1); /* dt_errno is set for us */
1280 n = 1;
1282 } else {
1283 if (object == DTRACE_OBJ_KMODS)
1284 mask = bits = DT_DM_KERNEL;
1285 else if (object == DTRACE_OBJ_UMODS)
1286 mask = DT_DM_KERNEL;
1288 dmp = dt_list_next(&dtp->dt_modlist);
1289 n = dtp->dt_nmods;
1292 if (symp == NULL)
1293 symp = &sym;
1295 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1296 if ((dmp->dm_flags & mask) != bits)
1297 continue; /* failed to match required attributes */
1299 if (dt_module_load(dtp, dmp) == -1)
1300 continue; /* failed to load symbol table */
1302 if (dmp->dm_ops->do_symname(dmp, name, symp, &id) != NULL) {
1303 if (sip != NULL) {
1304 sip->dts_object = dmp->dm_name;
1305 sip->dts_name = (const char *)
1306 dmp->dm_strtab.cts_data + symp->st_name;
1307 sip->dts_id = id;
1309 return (0);
1312 if (dmp->dm_extern != NULL &&
1313 (idp = dt_idhash_lookup(dmp->dm_extern, name)) != NULL) {
1314 if (symp != &sym) {
1315 symp->st_name = (uintptr_t)idp->di_name;
1316 symp->st_info =
1317 GELF_ST_INFO(STB_GLOBAL, STT_NOTYPE);
1318 symp->st_other = 0;
1319 symp->st_shndx = SHN_UNDEF;
1320 symp->st_value = 0;
1321 symp->st_size =
1322 ctf_type_size(idp->di_ctfp, idp->di_type);
1325 if (sip != NULL) {
1326 sip->dts_object = dmp->dm_name;
1327 sip->dts_name = idp->di_name;
1328 sip->dts_id = idp->di_id;
1331 return (0);
1335 return (dt_set_errno(dtp, EDT_NOSYM));
1339 * Exported interface to look up a symbol by address. We return the GElf_Sym
1340 * and complete symbol information for the matching symbol.
1343 dtrace_lookup_by_addr(dtrace_hdl_t *dtp, GElf_Addr addr,
1344 GElf_Sym *symp, dtrace_syminfo_t *sip)
1346 dt_module_t *dmp;
1347 uint_t id;
1348 const dtrace_vector_t *v = dtp->dt_vector;
1350 if (v != NULL)
1351 return (v->dtv_lookup_by_addr(dtp->dt_varg, addr, symp, sip));
1353 for (dmp = dt_list_next(&dtp->dt_modlist); dmp != NULL;
1354 dmp = dt_list_next(dmp)) {
1355 if (addr - dmp->dm_text_va < dmp->dm_text_size ||
1356 addr - dmp->dm_data_va < dmp->dm_data_size ||
1357 addr - dmp->dm_bss_va < dmp->dm_bss_size)
1358 break;
1361 if (dmp == NULL)
1362 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1364 if (dt_module_load(dtp, dmp) == -1)
1365 return (-1); /* dt_errno is set for us */
1367 if (symp != NULL) {
1368 if (dmp->dm_ops->do_symaddr(dmp, addr, symp, &id) == NULL)
1369 return (dt_set_errno(dtp, EDT_NOSYMADDR));
1372 if (sip != NULL) {
1373 sip->dts_object = dmp->dm_name;
1375 if (symp != NULL) {
1376 sip->dts_name = (const char *)
1377 dmp->dm_strtab.cts_data + symp->st_name;
1378 sip->dts_id = id;
1379 } else {
1380 sip->dts_name = NULL;
1381 sip->dts_id = 0;
1385 return (0);
1388 boolean_t
1389 dt_is_forward_decl(ctf_file_t *file, ctf_id_t type)
1391 ctf_id_t kind = ctf_type_kind(file, type);
1393 while ((type = ctf_type_reference(file, type)) != CTF_ERR) {
1394 type = ctf_type_resolve(file, type);
1395 kind = ctf_type_kind(file, type);
1398 return (kind == CTF_K_FORWARD);
1401 void
1402 dt_resolve_forward_decl(ctf_file_t **ctfp, ctf_id_t *type)
1404 char name[DT_TYPE_NAMELEN];
1406 while (dt_is_forward_decl(*ctfp, *type)) {
1407 char *tag = ctf_type_name(*ctfp, *type, name, sizeof (name));
1408 dtrace_typeinfo_t dtt;
1410 if (tag != NULL && dt_type_lookup(tag, &dtt) == 0 &&
1411 (dtt.dtt_ctfp != *ctfp) || dtt.dtt_type != *type) {
1412 *ctfp = dtt.dtt_ctfp;
1413 *type = dtt.dtt_type;
1414 } else {
1415 /* All we have is the forward definition */
1416 break;
1422 * We've been asked to look up something inside a pid related module and it has
1423 * been qualified with a library name. In that case, we may have to split this
1424 * up into the library and the type itself, which will be separated by an '`'
1425 * character. This is complicated further by the fact that the keyword for a
1426 * struct, union, or enum, will precede the library. Hence we may have something
1427 * that looks like "struct libsocket.so.1`msghdr" in name and we need to
1428 * transform that into "libsocket.so.1" and "struct msghdr".
1431 dtrace_lookup_fixup_pidtype(const char *name, char **libp, char **typep)
1433 int len, i;
1434 char *split = NULL, *lib, *buf;
1435 char *base;
1436 char *keywords[] = { "struct ", "union ", "enum ", NULL };
1438 if (name == NULL)
1439 return (-1);
1441 *libp = NULL;
1442 *typep = NULL;
1443 buf = strdup(name);
1444 if (buf == NULL)
1445 return (-1);
1447 i = 0;
1448 lib = buf;
1449 while (keywords[i] != NULL) {
1450 base = keywords[i];
1451 len = strlen(base);
1452 if (strncmp(name, base, len) == 0) {
1453 lib += len;
1454 break;
1456 i++;
1459 split = strchr(buf, '`');
1460 assert(split != NULL);
1461 *split = '\0';
1462 split++;
1463 if (lib == buf) {
1464 *libp = strdup(buf);
1465 *typep = strdup(split);
1466 if (*libp == NULL || *typep == NULL)
1467 goto err;
1468 free(buf);
1469 return (0);
1470 } else {
1471 assert(len > 0);
1472 assert(base != NULL);
1474 *libp = strdup(lib);
1475 if (*libp == NULL)
1476 goto err;
1477 if (asprintf(typep, "%s%s", base, split) == -1)
1478 goto err;
1479 free(buf);
1480 return (0);
1483 err:
1484 free(buf);
1485 free(*libp);
1486 *libp = NULL;
1487 free(*typep);
1488 *typep = NULL;
1489 return (-1);
1493 dtrace_lookup_by_type(dtrace_hdl_t *dtp, const char *object, const char *name,
1494 dtrace_typeinfo_t *tip)
1496 dtrace_typeinfo_t ti;
1497 dt_module_t *dmp;
1498 int found = 0;
1499 ctf_id_t id;
1500 uint_t n, i;
1501 int justone;
1502 ctf_file_t *fp;
1504 uint_t mask = 0; /* mask of dt_module flags to match */
1505 uint_t bits = 0; /* flag bits that must be present */
1507 if (object != DTRACE_OBJ_EVERY &&
1508 object != DTRACE_OBJ_KMODS &&
1509 object != DTRACE_OBJ_UMODS) {
1510 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1511 return (-1); /* dt_errno is set for us */
1513 if (dt_module_load(dtp, dmp) == -1)
1514 return (-1); /* dt_errno is set for us */
1515 n = 1;
1516 justone = 1;
1517 } else {
1518 if (object == DTRACE_OBJ_KMODS)
1519 mask = bits = DT_DM_KERNEL;
1520 else if (object == DTRACE_OBJ_UMODS)
1521 mask = DT_DM_KERNEL;
1523 dmp = dt_list_next(&dtp->dt_modlist);
1524 n = dtp->dt_nmods;
1525 justone = 0;
1528 if (tip == NULL)
1529 tip = &ti;
1531 for (; n > 0; n--, dmp = dt_list_next(dmp)) {
1532 if ((dmp->dm_flags & mask) != bits)
1533 continue; /* failed to match required attributes */
1536 * If we can't load the CTF container, continue on to the next
1537 * module. If our search was scoped to only one module then
1538 * return immediately leaving dt_errno unmodified.
1540 if (dt_module_hasctf(dtp, dmp) == 0) {
1541 if (justone)
1542 return (-1);
1543 continue;
1547 * Look up the type in the module's CTF container. If our
1548 * match is a forward declaration tag, save this choice in
1549 * 'tip' and keep going in the hope that we will locate the
1550 * underlying structure definition. Otherwise just return.
1552 if (dmp->dm_pid == 0) {
1553 id = ctf_lookup_by_name(dmp->dm_ctfp, name);
1554 fp = dmp->dm_ctfp;
1555 } else {
1556 dt_dprintf("Trying to find userland type: %s\n", name);
1557 if (strchr(name, '`') != NULL) {
1558 char *lib, *type;
1559 if (dtrace_lookup_fixup_pidtype(name, &lib,
1560 &type) != 0) {
1561 return (dt_set_errno(dtp, EDT_NOMEM));
1563 fp = dt_module_getctflib(dtp, dmp, lib);
1564 if (fp == NULL || (id = ctf_lookup_by_name(fp,
1565 type)) == CTF_ERR)
1566 id = CTF_ERR;
1567 free(lib);
1568 free(type);
1569 } else {
1570 for (i = 0; i < dmp->dm_nctflibs; i++) {
1571 fp = dmp->dm_libctfp[i];
1572 id = ctf_lookup_by_name(fp, name);
1573 if (id != CTF_ERR)
1574 break;
1578 if (id != CTF_ERR) {
1579 tip->dtt_object = dmp->dm_name;
1580 tip->dtt_ctfp = fp;
1581 tip->dtt_type = id;
1582 if (!dt_is_forward_decl(fp, ctf_type_resolve(fp, id)))
1583 return (0);
1585 found++;
1589 if (found == 0)
1590 return (dt_set_errno(dtp, EDT_NOTYPE));
1592 return (0);
1596 dtrace_symbol_type(dtrace_hdl_t *dtp, const GElf_Sym *symp,
1597 const dtrace_syminfo_t *sip, dtrace_typeinfo_t *tip)
1599 dt_module_t *dmp;
1601 tip->dtt_object = NULL;
1602 tip->dtt_ctfp = NULL;
1603 tip->dtt_type = CTF_ERR;
1604 tip->dtt_flags = 0;
1606 if ((dmp = dt_module_lookup_by_name(dtp, sip->dts_object)) == NULL)
1607 return (dt_set_errno(dtp, EDT_NOMOD));
1609 if (symp->st_shndx == SHN_UNDEF && dmp->dm_extern != NULL) {
1610 dt_ident_t *idp =
1611 dt_idhash_lookup(dmp->dm_extern, sip->dts_name);
1613 if (idp == NULL)
1614 return (dt_set_errno(dtp, EDT_NOSYM));
1616 tip->dtt_ctfp = idp->di_ctfp;
1617 tip->dtt_type = idp->di_type;
1619 } else if (GELF_ST_TYPE(symp->st_info) != STT_FUNC) {
1620 if (dt_module_getctf(dtp, dmp) == NULL)
1621 return (-1); /* errno is set for us */
1623 tip->dtt_ctfp = dmp->dm_ctfp;
1624 tip->dtt_type = ctf_lookup_by_symbol(dmp->dm_ctfp, sip->dts_id);
1626 if (tip->dtt_type == CTF_ERR) {
1627 dtp->dt_ctferr = ctf_errno(tip->dtt_ctfp);
1628 return (dt_set_errno(dtp, EDT_CTF));
1631 } else {
1632 tip->dtt_ctfp = DT_FPTR_CTFP(dtp);
1633 tip->dtt_type = DT_FPTR_TYPE(dtp);
1636 tip->dtt_object = dmp->dm_name;
1637 return (0);
1640 static dtrace_objinfo_t *
1641 dt_module_info(const dt_module_t *dmp, dtrace_objinfo_t *dto)
1643 dto->dto_name = dmp->dm_name;
1644 dto->dto_file = dmp->dm_file;
1645 dto->dto_id = dmp->dm_modid;
1646 dto->dto_flags = 0;
1648 if (dmp->dm_flags & DT_DM_KERNEL)
1649 dto->dto_flags |= DTRACE_OBJ_F_KERNEL;
1650 if (dmp->dm_flags & DT_DM_PRIMARY)
1651 dto->dto_flags |= DTRACE_OBJ_F_PRIMARY;
1653 dto->dto_text_va = dmp->dm_text_va;
1654 dto->dto_text_size = dmp->dm_text_size;
1655 dto->dto_data_va = dmp->dm_data_va;
1656 dto->dto_data_size = dmp->dm_data_size;
1657 dto->dto_bss_va = dmp->dm_bss_va;
1658 dto->dto_bss_size = dmp->dm_bss_size;
1660 return (dto);
1664 dtrace_object_iter(dtrace_hdl_t *dtp, dtrace_obj_f *func, void *data)
1666 const dt_module_t *dmp = dt_list_next(&dtp->dt_modlist);
1667 dtrace_objinfo_t dto;
1668 int rv;
1670 for (; dmp != NULL; dmp = dt_list_next(dmp)) {
1671 if ((rv = (*func)(dtp, dt_module_info(dmp, &dto), data)) != 0)
1672 return (rv);
1675 return (0);
1679 dtrace_object_info(dtrace_hdl_t *dtp, const char *object, dtrace_objinfo_t *dto)
1681 dt_module_t *dmp;
1683 if (object == DTRACE_OBJ_EVERY || object == DTRACE_OBJ_KMODS ||
1684 object == DTRACE_OBJ_UMODS || dto == NULL)
1685 return (dt_set_errno(dtp, EINVAL));
1687 if ((dmp = dt_module_from_object(dtp, object)) == NULL)
1688 return (-1); /* dt_errno is set for us */
1690 if (dt_module_load(dtp, dmp) == -1)
1691 return (-1); /* dt_errno is set for us */
1693 (void) dt_module_info(dmp, dto);
1694 return (0);