4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
26 * Copyright (c) 2013, Joyent, Inc. All rights reserved.
29 #include <sys/types.h>
30 #include <sys/modctl.h>
32 #include <sys/kobj_impl.h>
33 #include <sys/sysmacros.h>
47 #include <dt_strtab.h>
48 #include <dt_module.h>
51 static const char *dt_module_strtab
; /* active strtab for qsort callbacks */
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
];
59 assert(dmp
->dm_symfree
< dmp
->dm_nsymelems
+ 1);
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
++;
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()"
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
;
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
);
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()"
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
;
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
);
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.
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.
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
));
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
))
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
;
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
))
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
;
240 dt_module_symgelf32(const Elf32_Sym
*src
, GElf_Sym
*dst
)
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
;
255 dt_module_symgelf64(const Elf64_Sym
*src
, GElf_Sym
*dst
)
258 bcopy(src
, dst
, sizeof (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
;
274 if (dmp
->dm_nsymelems
== 0)
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) {
285 *idp
= dsp
->ds_symid
;
286 return (dt_module_symgelf32(sym
, symp
));
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
;
304 if (dmp
->dm_nsymelems
== 0)
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) {
315 *idp
= dsp
->ds_symid
;
316 return (dt_module_symgelf64(sym
, symp
));
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;
334 if (dmp
->dm_aslen
== 0)
337 while (hi
- lo
> 1) {
339 if (addr
>= asmap
[mid
]->st_value
)
345 i
= addr
< asmap
[hi
]->st_value
? lo
: hi
;
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
)
356 if (addr
- sym
->st_value
< MAX(sym
->st_size
, 1)) {
358 *idp
= (uint_t
)(sym
- symtab
);
359 return (dt_module_symgelf32(sym
, symp
));
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;
376 if (dmp
->dm_aslen
== 0)
379 while (hi
- lo
> 1) {
381 if (addr
>= asmap
[mid
]->st_value
)
387 i
= addr
< asmap
[hi
]->st_value
? lo
: hi
;
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
)
398 if (addr
- sym
->st_value
< MAX(sym
->st_size
, 1)) {
400 *idp
= (uint_t
)(sym
- symtab
);
401 return (dt_module_symgelf64(sym
, symp
));
407 static const dt_modops_t dt_modops_32
= {
414 static const dt_modops_t dt_modops_64
= {
422 dt_module_create(dtrace_hdl_t
*dtp
, const char *name
)
427 uint_t h
= dt_strtab_hash(name
, NULL
) % dtp
->dt_modbuckets
;
430 for (dmp
= dtp
->dt_mods
[h
]; dmp
!= NULL
; dmp
= dmp
->dm_next
) {
431 if (strcmp(dmp
->dm_name
, name
) == 0)
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
;
445 if (dtp
->dt_conf
.dtc_ctfmodel
== CTF_MODEL_LP64
)
446 dmp
->dm_ops
= &dt_modops_64
;
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) {
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
;
467 pid
= strtol(dmp
->dm_name
+ 3, &eptr
, 10);
468 if (errno
== 0 && *eptr
== '\0')
469 dmp
->dm_pid
= (pid_t
)pid
;
471 dt_dprintf("encountered malformed pid "
472 "module: %s\n", dmp
->dm_name
);
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
;
485 for (dmp
= dtp
->dt_mods
[h
]; dmp
!= NULL
; dmp
= dmp
->dm_next
) {
486 if (strcmp(dmp
->dm_name
, name
) == 0)
495 dt_module_lookup_by_ctf(dtrace_hdl_t
*dtp
, ctf_file_t
*ctfp
)
497 return (ctfp
? ctf_getspecific(ctfp
) : NULL
);
501 dt_module_load_sect(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
, ctf_sect_t
*ctsp
)
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
)
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
);
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
;
544 } dt_module_cb_arg_t
;
548 dt_module_load_proc_count(void *arg
, const prmap_t
*prmap
, const char *obj
)
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
);
562 dt_module_load_proc_build(void *arg
, const prmap_t
*prmap
, const char *obj
)
565 char buf
[MAXPATHLEN
], *p
;
566 dt_module_cb_arg_t
*dcp
= arg
;
567 int count
= dcp
->dpa_count
;
570 fp
= Pname_to_ctf(dcp
->dpa_proc
, obj
);
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
585 (void) Pobjname(dcp
->dpa_proc
, prmap
->pr_vaddr
, buf
, sizeof (buf
));
586 if ((p
= strrchr(buf
, '/')) == NULL
)
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
596 if (Plmid(dcp
->dpa_proc
, prmap
->pr_vaddr
, &lmid
) != 0)
600 dcp
->dpa_dmp
->dm_libctfn
[count
] = strdup(p
);
602 (void) asprintf(&dcp
->dpa_dmp
->dm_libctfn
[count
],
603 "LM%lx`%s", lmid
, p
);
604 if (dcp
->dpa_dmp
->dm_libctfn
[count
] == NULL
)
606 ctf_setspecific(fp
, dcp
->dpa_dmp
);
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.
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
632 p
= dt_proc_grab(dtp
, dmp
->dm_pid
, 0, PGRAB_RDONLY
| PGRAB_FORCE
);
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
);
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
;
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
,
686 dt_proc_unlock(dtp
, p
);
687 dt_proc_release(dtp
, p
);
688 dmp
->dm_flags
|= DT_DM_LOADED
;
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.
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
;
788 dt_module_hasctf(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
)
790 if (dmp
->dm_pid
!= 0 && dmp
->dm_nctflibs
> 0)
792 return (dt_module_getctf(dtp
, dmp
) != NULL
);
796 dt_module_getctf(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
)
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
;
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
);
822 if (dmp
->dm_ctdata
.cts_size
== 0) {
823 (void) dt_set_errno(dtp
, EDT_NOCTF
);
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
);
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
) {
842 (void) dt_set_errno(dtp
, EDT_NOMEM
);
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
),
853 (void) dt_set_errno(dtp
, EDT_BADCTF
);
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
);
864 dt_dprintf("loaded CTF container for %s (%p)\n",
865 dmp
->dm_name
, (void *)dmp
->dm_ctfp
);
867 return (dmp
->dm_ctfp
);
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
);
879 dt_module_unload(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
)
883 ctf_close(dmp
->dm_ctfp
);
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
) {
913 dmp
->dm_asmap
= NULL
;
917 dmp
->dm_nsymbuckets
= 0;
918 dmp
->dm_nsymelems
= 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
);
939 dmp
->dm_flags
&= ~DT_DM_LOADED
;
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);
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
);
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.
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
;
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
);
988 if (dt_idhash_nextid(dmp
->dm_extern
, &id
) == -1) {
989 (void) dt_set_errno(dtp
, EDT_SYMOFLOW
);
993 if ((sip
= malloc(sizeof (dtrace_syminfo_t
))) == NULL
) {
994 (void) dt_set_errno(dtp
, EDT_NOMEM
);
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
);
1002 (void) dt_set_errno(dtp
, EDT_NOMEM
);
1007 sip
->dts_object
= dmp
->dm_name
;
1008 sip
->dts_name
= idp
->di_name
;
1009 sip
->dts_id
= idp
->di_id
;
1012 idp
->di_ctfp
= tip
->dtt_ctfp
;
1013 idp
->di_type
= tip
->dtt_type
;
1019 dt_module_modelname(dt_module_t
*dmp
)
1021 if (dmp
->dm_ops
== &dt_modops_64
)
1029 dt_module_getlibid(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
, const ctf_file_t
*fp
)
1033 for (i
= 0; i
< dmp
->dm_nctflibs
; i
++) {
1034 if (dmp
->dm_libctfp
[i
] == fp
)
1043 dt_module_getctflib(dtrace_hdl_t
*dtp
, dt_module_t
*dmp
, const char *name
)
1047 for (i
= 0; i
< dmp
->dm_nctflibs
; i
++) {
1048 if (strcmp(dmp
->dm_libctfn
[i
], name
) == 0)
1049 return (dmp
->dm_libctfp
[i
]);
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>/.
1060 dt_module_update(dtrace_hdl_t
*dtp
, const char *name
)
1062 char fname
[MAXPATHLEN
];
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
));
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
);
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
);
1101 switch (gelf_getclass(dmp
->dm_elf
)) {
1103 dmp
->dm_ops
= &dt_modops_32
;
1107 dmp
->dm_ops
= &dt_modops_64
;
1111 dt_dprintf("failed to load %s: unknown ELF class\n", fname
);
1112 dt_module_destroy(dtp
, dmp
);
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.
1160 dtrace_update(dtrace_hdl_t
*dtp
)
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
) {
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
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
;
1232 switch ((uintptr_t)object
) {
1233 case (uintptr_t)DTRACE_OBJ_EXEC
:
1236 case (uintptr_t)DTRACE_OBJ_RTLD
:
1239 case (uintptr_t)DTRACE_OBJ_CDEFS
:
1240 dmp
= dtp
->dt_cdefs
;
1242 case (uintptr_t)DTRACE_OBJ_DDEFS
:
1243 dmp
= dtp
->dt_ddefs
;
1246 dmp
= dt_module_create(dtp
, object
);
1251 (void) dt_set_errno(dtp
, err
);
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
)
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 */
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
);
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
) {
1304 sip
->dts_object
= dmp
->dm_name
;
1305 sip
->dts_name
= (const char *)
1306 dmp
->dm_strtab
.cts_data
+ symp
->st_name
;
1312 if (dmp
->dm_extern
!= NULL
&&
1313 (idp
= dt_idhash_lookup(dmp
->dm_extern
, name
)) != NULL
) {
1315 symp
->st_name
= (uintptr_t)idp
->di_name
;
1317 GELF_ST_INFO(STB_GLOBAL
, STT_NOTYPE
);
1319 symp
->st_shndx
= SHN_UNDEF
;
1322 ctf_type_size(idp
->di_ctfp
, idp
->di_type
);
1326 sip
->dts_object
= dmp
->dm_name
;
1327 sip
->dts_name
= idp
->di_name
;
1328 sip
->dts_id
= idp
->di_id
;
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
)
1348 const dtrace_vector_t
*v
= dtp
->dt_vector
;
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
)
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 */
1368 if (dmp
->dm_ops
->do_symaddr(dmp
, addr
, symp
, &id
) == NULL
)
1369 return (dt_set_errno(dtp
, EDT_NOSYMADDR
));
1373 sip
->dts_object
= dmp
->dm_name
;
1376 sip
->dts_name
= (const char *)
1377 dmp
->dm_strtab
.cts_data
+ symp
->st_name
;
1380 sip
->dts_name
= NULL
;
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
);
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
;
1415 /* All we have is the forward definition */
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
)
1434 char *split
= NULL
, *lib
, *buf
;
1436 char *keywords
[] = { "struct ", "union ", "enum ", NULL
};
1449 while (keywords
[i
] != NULL
) {
1452 if (strncmp(name
, base
, len
) == 0) {
1459 split
= strchr(buf
, '`');
1460 assert(split
!= NULL
);
1464 *libp
= strdup(buf
);
1465 *typep
= strdup(split
);
1466 if (*libp
== NULL
|| *typep
== NULL
)
1472 assert(base
!= NULL
);
1474 *libp
= strdup(lib
);
1477 if (asprintf(typep
, "%s%s", base
, split
) == -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
;
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 */
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
);
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) {
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
);
1556 dt_dprintf("Trying to find userland type: %s\n", name
);
1557 if (strchr(name
, '`') != NULL
) {
1559 if (dtrace_lookup_fixup_pidtype(name
, &lib
,
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
,
1570 for (i
= 0; i
< dmp
->dm_nctflibs
; i
++) {
1571 fp
= dmp
->dm_libctfp
[i
];
1572 id
= ctf_lookup_by_name(fp
, name
);
1578 if (id
!= CTF_ERR
) {
1579 tip
->dtt_object
= dmp
->dm_name
;
1582 if (!dt_is_forward_decl(fp
, ctf_type_resolve(fp
, id
)))
1590 return (dt_set_errno(dtp
, EDT_NOTYPE
));
1596 dtrace_symbol_type(dtrace_hdl_t
*dtp
, const GElf_Sym
*symp
,
1597 const dtrace_syminfo_t
*sip
, dtrace_typeinfo_t
*tip
)
1601 tip
->dtt_object
= NULL
;
1602 tip
->dtt_ctfp
= NULL
;
1603 tip
->dtt_type
= CTF_ERR
;
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
) {
1611 dt_idhash_lookup(dmp
->dm_extern
, sip
->dts_name
);
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
));
1632 tip
->dtt_ctfp
= DT_FPTR_CTFP(dtp
);
1633 tip
->dtt_type
= DT_FPTR_TYPE(dtp
);
1636 tip
->dtt_object
= dmp
->dm_name
;
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
;
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
;
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
;
1670 for (; dmp
!= NULL
; dmp
= dt_list_next(dmp
)) {
1671 if ((rv
= (*func
)(dtp
, dt_module_info(dmp
, &dto
), data
)) != 0)
1679 dtrace_object_info(dtrace_hdl_t
*dtp
, const char *object
, dtrace_objinfo_t
*dto
)
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
);