1 /* Type handling functions.
2 Copyright (C) 2019-2020 Free Software Foundation, Inc.
4 This file is part of libctf.
6 libctf is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 See the GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; see the file COPYING. If not see
18 <http://www.gnu.org/licenses/>. */
23 /* Determine whether a type is a parent or a child. */
26 ctf_type_isparent (ctf_file_t
*fp
, ctf_id_t id
)
28 return (LCTF_TYPE_ISPARENT (fp
, id
));
32 ctf_type_ischild (ctf_file_t
* fp
, ctf_id_t id
)
34 return (LCTF_TYPE_ISCHILD (fp
, id
));
37 /* Iterate over the members of a STRUCT or UNION. We pass the name, member
38 type, and offset of each member to the specified callback function. */
41 ctf_member_iter (ctf_file_t
*fp
, ctf_id_t type
, ctf_member_f
*func
, void *arg
)
46 ssize_t size
, increment
;
50 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
51 return -1; /* errno is set for us. */
53 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
54 return -1; /* errno is set for us. */
56 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
57 kind
= LCTF_INFO_KIND (fp
, tp
->ctt_info
);
59 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
60 return (ctf_set_errno (ofp
, ECTF_NOTSOU
));
62 if ((dtd
= ctf_dynamic_type (fp
, type
)) == NULL
)
64 if (size
< CTF_LSTRUCT_THRESH
)
66 const ctf_member_t
*mp
= (const ctf_member_t
*) ((uintptr_t) tp
+
69 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, mp
++)
71 const char *name
= ctf_strptr (fp
, mp
->ctm_name
);
72 if ((rc
= func (name
, mp
->ctm_type
, mp
->ctm_offset
, arg
)) != 0)
78 const ctf_lmember_t
*lmp
= (const ctf_lmember_t
*) ((uintptr_t) tp
+
81 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, lmp
++)
83 const char *name
= ctf_strptr (fp
, lmp
->ctlm_name
);
84 if ((rc
= func (name
, lmp
->ctlm_type
,
85 (unsigned long) CTF_LMEM_OFFSET (lmp
), arg
)) != 0)
94 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
95 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
97 if ((rc
= func (dmd
->dmd_name
, dmd
->dmd_type
,
98 dmd
->dmd_offset
, arg
)) != 0)
106 /* Iterate over the members of an ENUM. We pass the string name and associated
107 integer value of each enum element to the specified callback function. */
110 ctf_enum_iter (ctf_file_t
*fp
, ctf_id_t type
, ctf_enum_f
*func
, void *arg
)
112 ctf_file_t
*ofp
= fp
;
113 const ctf_type_t
*tp
;
114 const ctf_enum_t
*ep
;
120 if ((type
= ctf_type_resolve_unsliced (fp
, type
)) == CTF_ERR
)
121 return -1; /* errno is set for us. */
123 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
124 return -1; /* errno is set for us. */
126 if (LCTF_INFO_KIND (fp
, tp
->ctt_info
) != CTF_K_ENUM
)
127 return (ctf_set_errno (ofp
, ECTF_NOTENUM
));
129 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
131 if ((dtd
= ctf_dynamic_type (ofp
, type
)) == NULL
)
133 ep
= (const ctf_enum_t
*) ((uintptr_t) tp
+ increment
);
135 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, ep
++)
137 const char *name
= ctf_strptr (fp
, ep
->cte_name
);
138 if ((rc
= func (name
, ep
->cte_value
, arg
)) != 0)
146 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
147 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
149 if ((rc
= func (dmd
->dmd_name
, dmd
->dmd_value
, arg
)) != 0)
157 /* Iterate over every root (user-visible) type in the given CTF container.
158 We pass the type ID of each type to the specified callback function. */
161 ctf_type_iter (ctf_file_t
*fp
, ctf_type_f
*func
, void *arg
)
163 ctf_id_t id
, max
= fp
->ctf_typemax
;
164 int rc
, child
= (fp
->ctf_flags
& LCTF_CHILD
);
166 for (id
= 1; id
<= max
; id
++)
168 const ctf_type_t
*tp
= LCTF_INDEX_TO_TYPEPTR (fp
, id
);
169 if (LCTF_INFO_ISROOT (fp
, tp
->ctt_info
)
170 && (rc
= func (LCTF_INDEX_TO_TYPE (fp
, id
, child
), arg
)) != 0)
177 /* Iterate over every type in the given CTF container, user-visible or not.
178 We pass the type ID of each type to the specified callback function. */
181 ctf_type_iter_all (ctf_file_t
*fp
, ctf_type_all_f
*func
, void *arg
)
183 ctf_id_t id
, max
= fp
->ctf_typemax
;
184 int rc
, child
= (fp
->ctf_flags
& LCTF_CHILD
);
186 for (id
= 1; id
<= max
; id
++)
188 const ctf_type_t
*tp
= LCTF_INDEX_TO_TYPEPTR (fp
, id
);
189 if ((rc
= func (LCTF_INDEX_TO_TYPE (fp
, id
, child
),
190 LCTF_INFO_ISROOT(fp
, tp
->ctt_info
)
191 ? CTF_ADD_ROOT
: CTF_ADD_NONROOT
, arg
) != 0))
198 /* Iterate over every variable in the given CTF container, in arbitrary order.
199 We pass the name of each variable to the specified callback function. */
202 ctf_variable_iter (ctf_file_t
*fp
, ctf_variable_f
*func
, void *arg
)
206 if ((fp
->ctf_flags
& LCTF_CHILD
) && (fp
->ctf_parent
== NULL
))
207 return ECTF_NOPARENT
;
209 if (!(fp
->ctf_flags
& LCTF_RDWR
))
212 for (i
= 0; i
< fp
->ctf_nvars
; i
++)
213 if ((rc
= func (ctf_strptr (fp
, fp
->ctf_vars
[i
].ctv_name
),
214 fp
->ctf_vars
[i
].ctv_type
, arg
)) != 0)
221 for (dvd
= ctf_list_next (&fp
->ctf_dvdefs
); dvd
!= NULL
;
222 dvd
= ctf_list_next (dvd
))
224 if ((rc
= func (dvd
->dvd_name
, dvd
->dvd_type
, arg
)) != 0)
232 /* Follow a given type through the graph for TYPEDEF, VOLATILE, CONST, and
233 RESTRICT nodes until we reach a "base" type node. This is useful when
234 we want to follow a type ID to a node that has members or a size. To guard
235 against infinite loops, we implement simplified cycle detection and check
236 each link against itself, the previous node, and the topmost node.
238 Does not drill down through slices to their contained type. */
241 ctf_type_resolve (ctf_file_t
*fp
, ctf_id_t type
)
243 ctf_id_t prev
= type
, otype
= type
;
244 ctf_file_t
*ofp
= fp
;
245 const ctf_type_t
*tp
;
248 return (ctf_set_errno (ofp
, ECTF_NONREPRESENTABLE
));
250 while ((tp
= ctf_lookup_by_id (&fp
, type
)) != NULL
)
252 switch (LCTF_INFO_KIND (fp
, tp
->ctt_info
))
258 if (tp
->ctt_type
== type
|| tp
->ctt_type
== otype
259 || tp
->ctt_type
== prev
)
261 ctf_dprintf ("type %ld cycle detected\n", otype
);
262 return (ctf_set_errno (ofp
, ECTF_CORRUPT
));
271 return (ctf_set_errno (ofp
, ECTF_NONREPRESENTABLE
));
274 return CTF_ERR
; /* errno is set for us. */
277 /* Like ctf_type_resolve(), but traverse down through slices to their contained
281 ctf_type_resolve_unsliced (ctf_file_t
*fp
, ctf_id_t type
)
283 const ctf_type_t
*tp
;
285 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
288 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
289 return CTF_ERR
; /* errno is set for us. */
291 if ((LCTF_INFO_KIND (fp
, tp
->ctt_info
)) == CTF_K_SLICE
)
292 return ctf_type_reference (fp
, type
);
296 /* Look up a name in the given name table, in the appropriate hash given the
297 kind of the identifier. The name is a raw, undecorated identifier. */
299 ctf_id_t
ctf_lookup_by_rawname (ctf_file_t
*fp
, int kind
, const char *name
)
301 return ctf_lookup_by_rawhash (fp
, ctf_name_table (fp
, kind
), name
);
304 /* Look up a name in the given name table, in the appropriate hash given the
305 readability state of the dictionary. The name is a raw, undecorated
308 ctf_id_t
ctf_lookup_by_rawhash (ctf_file_t
*fp
, ctf_names_t
*np
, const char *name
)
312 if (fp
->ctf_flags
& LCTF_RDWR
)
313 id
= (ctf_id_t
) ctf_dynhash_lookup (np
->ctn_writable
, name
);
315 id
= ctf_hash_lookup_type (np
->ctn_readonly
, fp
, name
);
319 /* Lookup the given type ID and return its name as a new dynamically-allocated
323 ctf_type_aname (ctf_file_t
*fp
, ctf_id_t type
)
326 ctf_decl_node_t
*cdp
;
327 ctf_decl_prec_t prec
, lp
, rp
;
332 if (fp
== NULL
&& type
== CTF_ERR
)
333 return NULL
; /* Simplify caller code by permitting CTF_ERR. */
336 ctf_decl_push (&cd
, fp
, type
);
341 ctf_set_errno (fp
, cd
.cd_err
);
345 /* If the type graph's order conflicts with lexical precedence order
346 for pointers or arrays, then we need to surround the declarations at
347 the corresponding lexical precedence with parentheses. This can
348 result in either a parenthesized pointer (*) as in int (*)() or
349 int (*)[], or in a parenthesized pointer and array as in int (*[])(). */
351 ptr
= cd
.cd_order
[CTF_PREC_POINTER
] > CTF_PREC_POINTER
;
352 arr
= cd
.cd_order
[CTF_PREC_ARRAY
] > CTF_PREC_ARRAY
;
354 rp
= arr
? CTF_PREC_ARRAY
: ptr
? CTF_PREC_POINTER
: -1;
355 lp
= ptr
? CTF_PREC_POINTER
: arr
? CTF_PREC_ARRAY
: -1;
357 k
= CTF_K_POINTER
; /* Avoid leading whitespace (see below). */
359 for (prec
= CTF_PREC_BASE
; prec
< CTF_PREC_MAX
; prec
++)
361 for (cdp
= ctf_list_next (&cd
.cd_nodes
[prec
]);
362 cdp
!= NULL
; cdp
= ctf_list_next (cdp
))
364 ctf_file_t
*rfp
= fp
;
365 const ctf_type_t
*tp
= ctf_lookup_by_id (&rfp
, cdp
->cd_type
);
366 const char *name
= ctf_strptr (rfp
, tp
->ctt_name
);
368 if (k
!= CTF_K_POINTER
&& k
!= CTF_K_ARRAY
)
369 ctf_decl_sprintf (&cd
, " ");
373 ctf_decl_sprintf (&cd
, "(");
377 switch (cdp
->cd_kind
)
382 /* Integers, floats, and typedefs must always be named types. */
386 ctf_set_errno (fp
, ECTF_CORRUPT
);
391 ctf_decl_sprintf (&cd
, "%s", name
);
394 ctf_decl_sprintf (&cd
, "*");
397 ctf_decl_sprintf (&cd
, "[%u]", cdp
->cd_n
);
400 ctf_decl_sprintf (&cd
, "()");
404 ctf_decl_sprintf (&cd
, "struct %s", name
);
407 ctf_decl_sprintf (&cd
, "union %s", name
);
410 ctf_decl_sprintf (&cd
, "enum %s", name
);
413 ctf_decl_sprintf (&cd
, "volatile");
416 ctf_decl_sprintf (&cd
, "const");
419 ctf_decl_sprintf (&cd
, "restrict");
422 /* No representation: just changes encoding of contained type,
423 which is not in any case printed. Skip it. */
431 ctf_decl_sprintf (&cd
, ")");
435 (void) ctf_set_errno (fp
, ENOMEM
);
437 buf
= ctf_decl_buf (&cd
);
443 /* Lookup the given type ID and print a string name for it into buf. Return
444 the actual number of bytes (not including \0) needed to format the name. */
447 ctf_type_lname (ctf_file_t
*fp
, ctf_id_t type
, char *buf
, size_t len
)
449 char *str
= ctf_type_aname (fp
, type
);
453 return CTF_ERR
; /* errno is set for us. */
456 snprintf (buf
, len
, "%s", str
);
460 (void) ctf_set_errno (fp
, ECTF_NAMELEN
);
465 /* Lookup the given type ID and print a string name for it into buf. If buf
466 is too small, return NULL: the ECTF_NAMELEN error is set on 'fp' for us. */
469 ctf_type_name (ctf_file_t
*fp
, ctf_id_t type
, char *buf
, size_t len
)
471 ssize_t rv
= ctf_type_lname (fp
, type
, buf
, len
);
472 return (rv
>= 0 && (size_t) rv
< len
? buf
: NULL
);
475 /* Lookup the given type ID and return its raw, unadorned, undecorated name.
476 The name will live as long as its ctf_file_t does. */
479 ctf_type_name_raw (ctf_file_t
*fp
, ctf_id_t type
)
481 const ctf_type_t
*tp
;
483 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
484 return NULL
; /* errno is set for us. */
486 return ctf_strraw (fp
, tp
->ctt_name
);
489 /* Lookup the given type ID and return its raw, unadorned, undecorated name as a
490 new dynamically-allocated string. */
493 ctf_type_aname_raw (ctf_file_t
*fp
, ctf_id_t type
)
495 const char *name
= ctf_type_name_raw (fp
, type
);
498 return strdup (name
);
503 /* Resolve the type down to a base type node, and then return the size
504 of the type storage in bytes. */
507 ctf_type_size (ctf_file_t
*fp
, ctf_id_t type
)
509 const ctf_type_t
*tp
;
513 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
514 return -1; /* errno is set for us. */
516 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
517 return -1; /* errno is set for us. */
519 switch (LCTF_INFO_KIND (fp
, tp
->ctt_info
))
522 return fp
->ctf_dmodel
->ctd_pointer
;
525 return 0; /* Function size is only known by symtab. */
528 return fp
->ctf_dmodel
->ctd_int
;
531 /* ctf_add_array() does not directly encode the element size, but
532 requires the user to multiply to determine the element size.
534 If ctf_get_ctt_size() returns nonzero, then use the recorded
537 if ((size
= ctf_get_ctt_size (fp
, tp
, NULL
, NULL
)) > 0)
540 if (ctf_array_info (fp
, type
, &ar
) < 0
541 || (size
= ctf_type_size (fp
, ar
.ctr_contents
)) < 0)
542 return -1; /* errno is set for us. */
544 return size
* ar
.ctr_nelems
;
546 default: /* including slices of enums, etc */
547 return (ctf_get_ctt_size (fp
, tp
, NULL
, NULL
));
551 /* Resolve the type down to a base type node, and then return the alignment
552 needed for the type storage in bytes.
554 XXX may need arch-dependent attention. */
557 ctf_type_align (ctf_file_t
*fp
, ctf_id_t type
)
559 const ctf_type_t
*tp
;
560 ctf_file_t
*ofp
= fp
;
563 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
564 return -1; /* errno is set for us. */
566 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
567 return -1; /* errno is set for us. */
569 kind
= LCTF_INFO_KIND (fp
, tp
->ctt_info
);
574 return fp
->ctf_dmodel
->ctd_pointer
;
579 if (ctf_array_info (fp
, type
, &r
) < 0)
580 return -1; /* errno is set for us. */
581 return (ctf_type_align (fp
, r
.ctr_contents
));
590 if ((dtd
= ctf_dynamic_type (ofp
, type
)) == NULL
)
592 uint32_t n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
);
593 ssize_t size
, increment
;
596 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
597 vmp
= (unsigned char *) tp
+ increment
;
599 if (kind
== CTF_K_STRUCT
)
600 n
= MIN (n
, 1); /* Only use first member for structs. */
602 if (size
< CTF_LSTRUCT_THRESH
)
604 const ctf_member_t
*mp
= vmp
;
605 for (; n
!= 0; n
--, mp
++)
607 ssize_t am
= ctf_type_align (fp
, mp
->ctm_type
);
608 align
= MAX (align
, (size_t) am
);
613 const ctf_lmember_t
*lmp
= vmp
;
614 for (; n
!= 0; n
--, lmp
++)
616 ssize_t am
= ctf_type_align (fp
, lmp
->ctlm_type
);
617 align
= MAX (align
, (size_t) am
);
625 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
626 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
628 ssize_t am
= ctf_type_align (fp
, dmd
->dmd_type
);
629 align
= MAX (align
, (size_t) am
);
630 if (kind
== CTF_K_STRUCT
)
639 return fp
->ctf_dmodel
->ctd_int
;
641 default: /* including slices of enums, etc */
642 return (ctf_get_ctt_size (fp
, tp
, NULL
, NULL
));
646 /* Return the kind (CTF_K_* constant) for the specified type ID. */
649 ctf_type_kind_unsliced (ctf_file_t
*fp
, ctf_id_t type
)
651 const ctf_type_t
*tp
;
653 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
654 return -1; /* errno is set for us. */
656 return (LCTF_INFO_KIND (fp
, tp
->ctt_info
));
659 /* Return the kind (CTF_K_* constant) for the specified type ID.
660 Slices are considered to be of the same kind as the type sliced. */
663 ctf_type_kind (ctf_file_t
*fp
, ctf_id_t type
)
667 if ((kind
= ctf_type_kind_unsliced (fp
, type
)) < 0)
670 if (kind
== CTF_K_SLICE
)
672 if ((type
= ctf_type_reference (fp
, type
)) == CTF_ERR
)
674 kind
= ctf_type_kind_unsliced (fp
, type
);
680 /* If the type is one that directly references another type (such as POINTER),
681 then return the ID of the type to which it refers. */
684 ctf_type_reference (ctf_file_t
*fp
, ctf_id_t type
)
686 ctf_file_t
*ofp
= fp
;
687 const ctf_type_t
*tp
;
689 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
690 return CTF_ERR
; /* errno is set for us. */
692 switch (LCTF_INFO_KIND (fp
, tp
->ctt_info
))
700 /* Slices store their type in an unusual place. */
704 const ctf_slice_t
*sp
;
706 if ((dtd
= ctf_dynamic_type (ofp
, type
)) == NULL
)
710 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
711 sp
= (const ctf_slice_t
*) ((uintptr_t) tp
+ increment
);
714 sp
= &dtd
->dtd_u
.dtu_slice
;
719 return (ctf_set_errno (ofp
, ECTF_NOTREF
));
723 /* Find a pointer to type by looking in fp->ctf_ptrtab. If we can't find a
724 pointer to the given type, see if we can compute a pointer to the type
725 resulting from resolving the type down to its base type and use that
726 instead. This helps with cases where the CTF data includes "struct foo *"
727 but not "foo_t *" and the user accesses "foo_t *" in the debugger.
729 XXX what about parent containers? */
732 ctf_type_pointer (ctf_file_t
*fp
, ctf_id_t type
)
734 ctf_file_t
*ofp
= fp
;
737 if (ctf_lookup_by_id (&fp
, type
) == NULL
)
738 return CTF_ERR
; /* errno is set for us. */
740 if ((ntype
= fp
->ctf_ptrtab
[LCTF_TYPE_TO_INDEX (fp
, type
)]) != 0)
741 return (LCTF_INDEX_TO_TYPE (fp
, ntype
, (fp
->ctf_flags
& LCTF_CHILD
)));
743 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
744 return (ctf_set_errno (ofp
, ECTF_NOTYPE
));
746 if (ctf_lookup_by_id (&fp
, type
) == NULL
)
747 return (ctf_set_errno (ofp
, ECTF_NOTYPE
));
749 if ((ntype
= fp
->ctf_ptrtab
[LCTF_TYPE_TO_INDEX (fp
, type
)]) != 0)
750 return (LCTF_INDEX_TO_TYPE (fp
, ntype
, (fp
->ctf_flags
& LCTF_CHILD
)));
752 return (ctf_set_errno (ofp
, ECTF_NOTYPE
));
755 /* Return the encoding for the specified INTEGER or FLOAT. */
758 ctf_type_encoding (ctf_file_t
*fp
, ctf_id_t type
, ctf_encoding_t
*ep
)
760 ctf_file_t
*ofp
= fp
;
762 const ctf_type_t
*tp
;
766 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
767 return -1; /* errno is set for us. */
769 if ((dtd
= ctf_dynamic_type (ofp
, type
)) != NULL
)
771 switch (LCTF_INFO_KIND (fp
, tp
->ctt_info
))
775 *ep
= dtd
->dtd_u
.dtu_enc
;
779 const ctf_slice_t
*slice
;
780 ctf_encoding_t underlying_en
;
783 slice
= &dtd
->dtd_u
.dtu_slice
;
784 underlying
= ctf_type_resolve (fp
, slice
->cts_type
);
785 data
= ctf_type_encoding (fp
, underlying
, &underlying_en
);
787 ep
->cte_format
= underlying_en
.cte_format
;
788 ep
->cte_offset
= slice
->cts_offset
;
789 ep
->cte_bits
= slice
->cts_bits
;
793 return (ctf_set_errno (ofp
, ECTF_NOTINTFP
));
798 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
800 switch (LCTF_INFO_KIND (fp
, tp
->ctt_info
))
803 data
= *(const uint32_t *) ((uintptr_t) tp
+ increment
);
804 ep
->cte_format
= CTF_INT_ENCODING (data
);
805 ep
->cte_offset
= CTF_INT_OFFSET (data
);
806 ep
->cte_bits
= CTF_INT_BITS (data
);
809 data
= *(const uint32_t *) ((uintptr_t) tp
+ increment
);
810 ep
->cte_format
= CTF_FP_ENCODING (data
);
811 ep
->cte_offset
= CTF_FP_OFFSET (data
);
812 ep
->cte_bits
= CTF_FP_BITS (data
);
816 const ctf_slice_t
*slice
;
817 ctf_encoding_t underlying_en
;
820 slice
= (ctf_slice_t
*) ((uintptr_t) tp
+ increment
);
821 underlying
= ctf_type_resolve (fp
, slice
->cts_type
);
822 data
= ctf_type_encoding (fp
, underlying
, &underlying_en
);
824 ep
->cte_format
= underlying_en
.cte_format
;
825 ep
->cte_offset
= slice
->cts_offset
;
826 ep
->cte_bits
= slice
->cts_bits
;
830 return (ctf_set_errno (ofp
, ECTF_NOTINTFP
));
837 ctf_type_cmp (ctf_file_t
*lfp
, ctf_id_t ltype
, ctf_file_t
*rfp
,
844 else if (ltype
> rtype
)
852 if (LCTF_TYPE_ISPARENT (lfp
, ltype
) && lfp
->ctf_parent
!= NULL
)
853 lfp
= lfp
->ctf_parent
;
855 if (LCTF_TYPE_ISPARENT (rfp
, rtype
) && rfp
->ctf_parent
!= NULL
)
856 rfp
= rfp
->ctf_parent
;
867 /* Return a boolean value indicating if two types are compatible. This function
868 returns true if the two types are the same, or if they (or their ultimate
869 base type) have the same encoding properties, or (for structs / unions /
870 enums / forward declarations) if they have the same name and (for structs /
871 unions) member count. */
874 ctf_type_compat (ctf_file_t
*lfp
, ctf_id_t ltype
,
875 ctf_file_t
*rfp
, ctf_id_t rtype
)
877 const ctf_type_t
*ltp
, *rtp
;
878 ctf_encoding_t le
, re
;
880 uint32_t lkind
, rkind
;
883 if (ctf_type_cmp (lfp
, ltype
, rfp
, rtype
) == 0)
886 ltype
= ctf_type_resolve (lfp
, ltype
);
887 lkind
= ctf_type_kind (lfp
, ltype
);
889 rtype
= ctf_type_resolve (rfp
, rtype
);
890 rkind
= ctf_type_kind (rfp
, rtype
);
892 ltp
= ctf_lookup_by_id (&lfp
, ltype
);
893 rtp
= ctf_lookup_by_id (&rfp
, rtype
);
895 if (ltp
!= NULL
&& rtp
!= NULL
)
896 same_names
= (strcmp (ctf_strptr (lfp
, ltp
->ctt_name
),
897 ctf_strptr (rfp
, rtp
->ctt_name
)) == 0);
899 if (((lkind
== CTF_K_ENUM
) && (rkind
== CTF_K_INTEGER
)) ||
900 ((rkind
== CTF_K_ENUM
) && (lkind
== CTF_K_INTEGER
)))
910 memset (&le
, 0, sizeof (le
));
911 memset (&re
, 0, sizeof (re
));
912 return (ctf_type_encoding (lfp
, ltype
, &le
) == 0
913 && ctf_type_encoding (rfp
, rtype
, &re
) == 0
914 && memcmp (&le
, &re
, sizeof (ctf_encoding_t
)) == 0);
916 return (ctf_type_compat (lfp
, ctf_type_reference (lfp
, ltype
),
917 rfp
, ctf_type_reference (rfp
, rtype
)));
919 return (ctf_array_info (lfp
, ltype
, &la
) == 0
920 && ctf_array_info (rfp
, rtype
, &ra
) == 0
921 && la
.ctr_nelems
== ra
.ctr_nelems
922 && ctf_type_compat (lfp
, la
.ctr_contents
, rfp
, ra
.ctr_contents
)
923 && ctf_type_compat (lfp
, la
.ctr_index
, rfp
, ra
.ctr_index
));
926 return (same_names
&& (ctf_type_size (lfp
, ltype
)
927 == ctf_type_size (rfp
, rtype
)));
930 int lencoded
, rencoded
;
931 lencoded
= ctf_type_encoding (lfp
, ltype
, &le
);
932 rencoded
= ctf_type_encoding (rfp
, rtype
, &re
);
934 if ((lencoded
!= rencoded
) ||
935 ((lencoded
== 0) && memcmp (&le
, &re
, sizeof (ctf_encoding_t
)) != 0))
940 return same_names
; /* No other checks required for these type kinds. */
942 return 0; /* Should not get here since we did a resolve. */
946 /* Return the type and offset for a given member of a STRUCT or UNION. */
949 ctf_member_info (ctf_file_t
*fp
, ctf_id_t type
, const char *name
,
952 ctf_file_t
*ofp
= fp
;
953 const ctf_type_t
*tp
;
955 ssize_t size
, increment
;
958 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
959 return -1; /* errno is set for us. */
961 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
962 return -1; /* errno is set for us. */
964 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
965 kind
= LCTF_INFO_KIND (fp
, tp
->ctt_info
);
967 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
968 return (ctf_set_errno (ofp
, ECTF_NOTSOU
));
970 if ((dtd
= ctf_dynamic_type (fp
, type
)) == NULL
)
972 if (size
< CTF_LSTRUCT_THRESH
)
974 const ctf_member_t
*mp
= (const ctf_member_t
*) ((uintptr_t) tp
+
977 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, mp
++)
979 if (strcmp (ctf_strptr (fp
, mp
->ctm_name
), name
) == 0)
981 mip
->ctm_type
= mp
->ctm_type
;
982 mip
->ctm_offset
= mp
->ctm_offset
;
989 const ctf_lmember_t
*lmp
= (const ctf_lmember_t
*) ((uintptr_t) tp
+
992 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, lmp
++)
994 if (strcmp (ctf_strptr (fp
, lmp
->ctlm_name
), name
) == 0)
996 mip
->ctm_type
= lmp
->ctlm_type
;
997 mip
->ctm_offset
= (unsigned long) CTF_LMEM_OFFSET (lmp
);
1007 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1008 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1010 if (strcmp (dmd
->dmd_name
, name
) == 0)
1012 mip
->ctm_type
= dmd
->dmd_type
;
1013 mip
->ctm_offset
= dmd
->dmd_offset
;
1019 return (ctf_set_errno (ofp
, ECTF_NOMEMBNAM
));
1022 /* Return the array type, index, and size information for the specified ARRAY. */
1025 ctf_array_info (ctf_file_t
*fp
, ctf_id_t type
, ctf_arinfo_t
*arp
)
1027 ctf_file_t
*ofp
= fp
;
1028 const ctf_type_t
*tp
;
1029 const ctf_array_t
*ap
;
1030 const ctf_dtdef_t
*dtd
;
1033 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1034 return -1; /* errno is set for us. */
1036 if (LCTF_INFO_KIND (fp
, tp
->ctt_info
) != CTF_K_ARRAY
)
1037 return (ctf_set_errno (ofp
, ECTF_NOTARRAY
));
1039 if ((dtd
= ctf_dynamic_type (ofp
, type
)) != NULL
)
1041 *arp
= dtd
->dtd_u
.dtu_arr
;
1045 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
1047 ap
= (const ctf_array_t
*) ((uintptr_t) tp
+ increment
);
1048 arp
->ctr_contents
= ap
->cta_contents
;
1049 arp
->ctr_index
= ap
->cta_index
;
1050 arp
->ctr_nelems
= ap
->cta_nelems
;
1055 /* Convert the specified value to the corresponding enum tag name, if a
1056 matching name can be found. Otherwise NULL is returned. */
1059 ctf_enum_name (ctf_file_t
*fp
, ctf_id_t type
, int value
)
1061 ctf_file_t
*ofp
= fp
;
1062 const ctf_type_t
*tp
;
1063 const ctf_enum_t
*ep
;
1064 const ctf_dtdef_t
*dtd
;
1068 if ((type
= ctf_type_resolve_unsliced (fp
, type
)) == CTF_ERR
)
1069 return NULL
; /* errno is set for us. */
1071 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1072 return NULL
; /* errno is set for us. */
1074 if (LCTF_INFO_KIND (fp
, tp
->ctt_info
) != CTF_K_ENUM
)
1076 (void) ctf_set_errno (ofp
, ECTF_NOTENUM
);
1080 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
1082 if ((dtd
= ctf_dynamic_type (ofp
, type
)) == NULL
)
1084 ep
= (const ctf_enum_t
*) ((uintptr_t) tp
+ increment
);
1086 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, ep
++)
1088 if (ep
->cte_value
== value
)
1089 return (ctf_strptr (fp
, ep
->cte_name
));
1096 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1097 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1099 if (dmd
->dmd_value
== value
)
1100 return dmd
->dmd_name
;
1104 (void) ctf_set_errno (ofp
, ECTF_NOENUMNAM
);
1108 /* Convert the specified enum tag name to the corresponding value, if a
1109 matching name can be found. Otherwise CTF_ERR is returned. */
1112 ctf_enum_value (ctf_file_t
* fp
, ctf_id_t type
, const char *name
, int *valp
)
1114 ctf_file_t
*ofp
= fp
;
1115 const ctf_type_t
*tp
;
1116 const ctf_enum_t
*ep
;
1117 const ctf_dtdef_t
*dtd
;
1121 if ((type
= ctf_type_resolve_unsliced (fp
, type
)) == CTF_ERR
)
1122 return -1; /* errno is set for us. */
1124 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1125 return -1; /* errno is set for us. */
1127 if (LCTF_INFO_KIND (fp
, tp
->ctt_info
) != CTF_K_ENUM
)
1129 (void) ctf_set_errno (ofp
, ECTF_NOTENUM
);
1133 (void) ctf_get_ctt_size (fp
, tp
, NULL
, &increment
);
1135 ep
= (const ctf_enum_t
*) ((uintptr_t) tp
+ increment
);
1137 if ((dtd
= ctf_dynamic_type (ofp
, type
)) == NULL
)
1139 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, ep
++)
1141 if (strcmp (ctf_strptr (fp
, ep
->cte_name
), name
) == 0)
1144 *valp
= ep
->cte_value
;
1153 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1154 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1156 if (strcmp (dmd
->dmd_name
, name
) == 0)
1159 *valp
= dmd
->dmd_value
;
1165 (void) ctf_set_errno (ofp
, ECTF_NOENUMNAM
);
1169 /* Given a type ID relating to a function type, return info on return types and
1170 arg counts for that function. */
1173 ctf_func_type_info (ctf_file_t
*fp
, ctf_id_t type
, ctf_funcinfo_t
*fip
)
1175 const ctf_type_t
*tp
;
1177 const uint32_t *args
;
1178 const ctf_dtdef_t
*dtd
;
1179 ssize_t size
, increment
;
1181 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
1182 return -1; /* errno is set for us. */
1184 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1185 return -1; /* errno is set for us. */
1187 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
1188 kind
= LCTF_INFO_KIND (fp
, tp
->ctt_info
);
1190 if (kind
!= CTF_K_FUNCTION
)
1191 return (ctf_set_errno (fp
, ECTF_NOTFUNC
));
1193 fip
->ctc_return
= tp
->ctt_type
;
1195 fip
->ctc_argc
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
);
1197 if ((dtd
= ctf_dynamic_type (fp
, type
)) == NULL
)
1198 args
= (uint32_t *) ((uintptr_t) tp
+ increment
);
1200 args
= dtd
->dtd_u
.dtu_argv
;
1202 if (fip
->ctc_argc
!= 0 && args
[fip
->ctc_argc
- 1] == 0)
1204 fip
->ctc_flags
|= CTF_FUNC_VARARG
;
1211 /* Given a type ID relating to a function type, return the arguments for the
1215 ctf_func_type_args (ctf_file_t
*fp
, ctf_id_t type
, uint32_t argc
, ctf_id_t
*argv
)
1217 const ctf_type_t
*tp
;
1218 const uint32_t *args
;
1219 const ctf_dtdef_t
*dtd
;
1220 ssize_t size
, increment
;
1223 if (ctf_func_type_info (fp
, type
, &f
) < 0)
1224 return -1; /* errno is set for us. */
1226 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
1227 return -1; /* errno is set for us. */
1229 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1230 return -1; /* errno is set for us. */
1232 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
1234 if ((dtd
= ctf_dynamic_type (fp
, type
)) == NULL
)
1235 args
= (uint32_t *) ((uintptr_t) tp
+ increment
);
1237 args
= dtd
->dtd_u
.dtu_argv
;
1239 for (argc
= MIN (argc
, f
.ctc_argc
); argc
!= 0; argc
--)
1245 /* Recursively visit the members of any type. This function is used as the
1246 engine for ctf_type_visit, below. We resolve the input type, recursively
1247 invoke ourself for each type member if the type is a struct or union, and
1248 then invoke the callback function on the current type. If any callback
1249 returns non-zero, we abort and percolate the error code back up to the top. */
1252 ctf_type_rvisit (ctf_file_t
*fp
, ctf_id_t type
, ctf_visit_f
*func
,
1253 void *arg
, const char *name
, unsigned long offset
, int depth
)
1255 ctf_id_t otype
= type
;
1256 const ctf_type_t
*tp
;
1257 const ctf_dtdef_t
*dtd
;
1258 ssize_t size
, increment
;
1262 if ((type
= ctf_type_resolve (fp
, type
)) == CTF_ERR
)
1263 return -1; /* errno is set for us. */
1265 if ((tp
= ctf_lookup_by_id (&fp
, type
)) == NULL
)
1266 return -1; /* errno is set for us. */
1268 if ((rc
= func (name
, otype
, offset
, depth
, arg
)) != 0)
1271 kind
= LCTF_INFO_KIND (fp
, tp
->ctt_info
);
1273 if (kind
!= CTF_K_STRUCT
&& kind
!= CTF_K_UNION
)
1276 (void) ctf_get_ctt_size (fp
, tp
, &size
, &increment
);
1278 if ((dtd
= ctf_dynamic_type (fp
, type
)) == NULL
)
1280 if (size
< CTF_LSTRUCT_THRESH
)
1282 const ctf_member_t
*mp
= (const ctf_member_t
*) ((uintptr_t) tp
+
1285 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, mp
++)
1287 if ((rc
= ctf_type_rvisit (fp
, mp
->ctm_type
,
1288 func
, arg
, ctf_strptr (fp
,
1290 offset
+ mp
->ctm_offset
,
1297 const ctf_lmember_t
*lmp
= (const ctf_lmember_t
*) ((uintptr_t) tp
+
1300 for (n
= LCTF_INFO_VLEN (fp
, tp
->ctt_info
); n
!= 0; n
--, lmp
++)
1302 if ((rc
= ctf_type_rvisit (fp
, lmp
->ctlm_type
,
1303 func
, arg
, ctf_strptr (fp
,
1305 offset
+ (unsigned long) CTF_LMEM_OFFSET (lmp
),
1315 for (dmd
= ctf_list_next (&dtd
->dtd_u
.dtu_members
);
1316 dmd
!= NULL
; dmd
= ctf_list_next (dmd
))
1318 if ((rc
= ctf_type_rvisit (fp
, dmd
->dmd_type
, func
, arg
,
1319 dmd
->dmd_name
, dmd
->dmd_offset
,
1328 /* Recursively visit the members of any type. We pass the name, member
1329 type, and offset of each member to the specified callback function. */
1331 ctf_type_visit (ctf_file_t
*fp
, ctf_id_t type
, ctf_visit_f
*func
, void *arg
)
1333 return (ctf_type_rvisit (fp
, type
, func
, arg
, "", 0, 0));