1 /* Textual dumping of CTF data.
2 Copyright (C) 2019-2024 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 #define str_append(s, a) ctf_str_append_noerr (s, a)
25 /* One item to be dumped, in string form. */
27 typedef struct ctf_dump_item
33 /* Cross-call state for dumping. Basically just enough to track the section in
34 use and a list of return strings. */
38 ctf_sect_names_t cds_sect
;
40 ctf_dump_item_t
*cds_current
;
44 /* Cross-call state for ctf_dump_member. */
46 typedef struct ctf_dump_membstate
50 const char *cdm_toplevel_indent
;
51 } ctf_dump_membstate_t
;
54 ctf_dump_append (ctf_dump_state_t
*state
, char *str
)
58 if ((cdi
= malloc (sizeof (struct ctf_dump_item
))) == NULL
)
59 return (ctf_set_errno (state
->cds_fp
, ENOMEM
));
62 ctf_list_append (&state
->cds_items
, cdi
);
67 ctf_dump_free (ctf_dump_state_t
*state
)
69 ctf_dump_item_t
*cdi
, *next_cdi
;
74 for (cdi
= ctf_list_next (&state
->cds_items
); cdi
!= NULL
;
78 next_cdi
= ctf_list_next (cdi
);
83 /* Return a dump for a single type, without member info: but do optionally show
84 the type's references. */
86 #define CTF_FT_REFS 0x2 /* Print referenced types. */
87 #define CTF_FT_BITFIELD 0x4 /* Print :BITS if a bitfield. */
88 #define CTF_FT_ID 0x8 /* Print "ID: " in front of type IDs. */
91 ctf_dump_format_type (ctf_dict_t
*fp
, ctf_id_t id
, int flag
)
94 char *str
= NULL
, *bit
= NULL
, *buf
= NULL
;
96 ctf_set_errno (fp
, 0);
102 int kind
, unsliced_kind
;
104 const char *nonroot_leader
= "";
105 const char *nonroot_trailer
= "";
106 const char *idstr
= "";
109 if (!(flag
& CTF_ADD_ROOT
))
111 nonroot_leader
= "{";
112 nonroot_trailer
= "}";
115 buf
= ctf_type_aname (fp
, id
);
118 if (id
== 0 || ctf_errno (fp
) == ECTF_NONREPRESENTABLE
)
120 ctf_set_errno (fp
, ECTF_NONREPRESENTABLE
);
121 str
= str_append (str
, " (type not represented in CTF)");
128 if (flag
& CTF_FT_ID
)
130 if (asprintf (&bit
, "%s%s0x%lx: (kind %i) ", nonroot_leader
, idstr
,
131 id
, ctf_type_kind (fp
, id
)) < 0)
133 str
= str_append (str
, bit
);
138 str
= str_append (str
, buf
);
143 unsliced_kind
= ctf_type_kind_unsliced (fp
, id
);
144 kind
= ctf_type_kind (fp
, id
);
146 /* Report encodings of everything with an encoding other than enums:
147 base-type enums cannot have a nonzero cte_offset or cte_bits value.
148 (Slices of them can, but they are of kind CTF_K_SLICE.) */
149 if (unsliced_kind
!= CTF_K_ENUM
&& ctf_type_encoding (fp
, id
, &ep
) == 0)
151 if ((ssize_t
) ep
.cte_bits
!= ctf_type_size (fp
, id
) * CHAR_BIT
152 && flag
& CTF_FT_BITFIELD
)
154 if (asprintf (&bit
, ":%i", ep
.cte_bits
) < 0)
156 str
= str_append (str
, bit
);
161 if ((ssize_t
) ep
.cte_bits
!= ctf_type_size (fp
, id
) * CHAR_BIT
162 || ep
.cte_offset
!= 0)
164 const char *slice
= "";
166 if (unsliced_kind
== CTF_K_SLICE
)
169 if (asprintf (&bit
, " [%s0x%x:0x%x]",
170 slice
, ep
.cte_offset
, ep
.cte_bits
) < 0)
172 str
= str_append (str
, bit
);
177 if (asprintf (&bit
, " (format 0x%x)", ep
.cte_format
) < 0)
179 str
= str_append (str
, bit
);
184 size
= ctf_type_size (fp
, id
);
185 if (kind
!= CTF_K_FUNCTION
&& size
>= 0)
187 if (asprintf (&bit
, " (size 0x%lx)", (unsigned long int) size
) < 0)
190 str
= str_append (str
, bit
);
195 align
= ctf_type_align (fp
, id
);
198 if (asprintf (&bit
, " (aligned at 0x%lx)",
199 (unsigned long int) align
) < 0)
202 str
= str_append (str
, bit
);
207 if (nonroot_trailer
[0] != 0)
208 str
= str_append (str
, nonroot_trailer
);
210 /* Just exit after one iteration if we are not showing the types this type
212 if (!(flag
& CTF_FT_REFS
))
215 /* Keep going as long as this type references another. We consider arrays
216 to "reference" their element type. */
218 if (kind
== CTF_K_ARRAY
)
220 if (ctf_array_info (fp
, id
, &ar
) < 0)
222 new_id
= ar
.ctr_contents
;
225 new_id
= ctf_type_reference (fp
, id
);
226 if (new_id
!= CTF_ERR
)
227 str
= str_append (str
, " -> ");
229 while (new_id
!= CTF_ERR
);
231 if (ctf_errno (fp
) != ECTF_NOTREF
)
240 ctf_set_errno (fp
, errno
);
242 ctf_err_warn (fp
, 1, ctf_errno (fp
), _("cannot format name dumping type 0x%lx"),
250 /* Dump one string field from the file header into the cds_items. */
252 ctf_dump_header_strfield (ctf_dict_t
*fp
, ctf_dump_state_t
*state
,
253 const char *name
, uint32_t value
)
258 if (asprintf (&str
, "%s: %s\n", name
, ctf_strptr (fp
, value
)) < 0)
260 ctf_dump_append (state
, str
);
265 return (ctf_set_errno (fp
, errno
));
268 /* Dump one section-offset field from the file header into the cds_items. */
270 ctf_dump_header_sectfield (ctf_dict_t
*fp
, ctf_dump_state_t
*state
,
271 const char *sect
, uint32_t off
, uint32_t nextoff
)
276 if (asprintf (&str
, "%s:\t0x%lx -- 0x%lx (0x%lx bytes)\n", sect
,
277 (unsigned long) off
, (unsigned long) (nextoff
- 1),
278 (unsigned long) (nextoff
- off
)) < 0)
280 ctf_dump_append (state
, str
);
285 return (ctf_set_errno (fp
, errno
));
288 /* Dump the file header into the cds_items. */
290 ctf_dump_header (ctf_dict_t
*fp
, ctf_dump_state_t
*state
)
293 char *flagstr
= NULL
;
294 const ctf_header_t
*hp
= fp
->ctf_header
;
295 const char *vertab
[] =
297 NULL
, "CTF_VERSION_1",
298 "CTF_VERSION_1_UPGRADED_3 (latest format, version 1 type "
301 "CTF_VERSION_3", NULL
303 const char *verstr
= NULL
;
305 if (asprintf (&str
, "Magic number: 0x%x\n", hp
->cth_magic
) < 0)
307 ctf_dump_append (state
, str
);
309 if (hp
->cth_version
<= CTF_VERSION
)
310 verstr
= vertab
[hp
->cth_version
];
313 verstr
= "(not a valid version)";
315 if (asprintf (&str
, "Version: %i (%s)\n", hp
->cth_version
,
318 ctf_dump_append (state
, str
);
320 /* Everything else is only printed if present. */
322 /* The flags are unusual in that they represent the ctf_dict_t *in memory*:
323 flags representing compression, etc, are turned off as the file is
324 decompressed. So we store a copy of the flags before they are changed, for
327 if (fp
->ctf_openflags
> 0)
329 if (asprintf (&flagstr
, "%s%s%s%s%s%s%s",
330 fp
->ctf_openflags
& CTF_F_COMPRESS
331 ? "CTF_F_COMPRESS": "",
332 (fp
->ctf_openflags
& CTF_F_COMPRESS
)
333 && (fp
->ctf_openflags
& ~CTF_F_COMPRESS
)
335 fp
->ctf_openflags
& CTF_F_NEWFUNCINFO
336 ? "CTF_F_NEWFUNCINFO" : "",
337 (fp
->ctf_openflags
& (CTF_F_NEWFUNCINFO
))
338 && (fp
->ctf_openflags
& ~(CTF_F_COMPRESS
| CTF_F_NEWFUNCINFO
))
340 fp
->ctf_openflags
& CTF_F_IDXSORTED
341 ? "CTF_F_IDXSORTED" : "",
342 fp
->ctf_openflags
& (CTF_F_IDXSORTED
)
343 && (fp
->ctf_openflags
& ~(CTF_F_COMPRESS
| CTF_F_NEWFUNCINFO
346 fp
->ctf_openflags
& CTF_F_DYNSTR
347 ? "CTF_F_DYNSTR" : "") < 0)
350 if (asprintf (&str
, "Flags: 0x%x (%s)", fp
->ctf_openflags
, flagstr
) < 0)
353 ctf_dump_append (state
, str
);
356 if (ctf_dump_header_strfield (fp
, state
, "Parent label",
357 hp
->cth_parlabel
) < 0)
360 if (ctf_dump_header_strfield (fp
, state
, "Parent name", hp
->cth_parname
) < 0)
363 if (ctf_dump_header_strfield (fp
, state
, "Compilation unit name",
367 if (ctf_dump_header_sectfield (fp
, state
, "Label section", hp
->cth_lbloff
,
368 hp
->cth_objtoff
) < 0)
371 if (ctf_dump_header_sectfield (fp
, state
, "Data object section",
372 hp
->cth_objtoff
, hp
->cth_funcoff
) < 0)
375 if (ctf_dump_header_sectfield (fp
, state
, "Function info section",
376 hp
->cth_funcoff
, hp
->cth_objtidxoff
) < 0)
379 if (ctf_dump_header_sectfield (fp
, state
, "Object index section",
380 hp
->cth_objtidxoff
, hp
->cth_funcidxoff
) < 0)
383 if (ctf_dump_header_sectfield (fp
, state
, "Function index section",
384 hp
->cth_funcidxoff
, hp
->cth_varoff
) < 0)
387 if (ctf_dump_header_sectfield (fp
, state
, "Variable section",
388 hp
->cth_varoff
, hp
->cth_typeoff
) < 0)
391 if (ctf_dump_header_sectfield (fp
, state
, "Type section",
392 hp
->cth_typeoff
, hp
->cth_stroff
) < 0)
395 if (ctf_dump_header_sectfield (fp
, state
, "String section", hp
->cth_stroff
,
396 hp
->cth_stroff
+ hp
->cth_strlen
+ 1) < 0)
402 return (ctf_set_errno (fp
, errno
));
405 /* Dump a single label into the cds_items. */
408 ctf_dump_label (const char *name
, const ctf_lblinfo_t
*info
,
413 ctf_dump_state_t
*state
= arg
;
415 if (asprintf (&str
, "%s -> ", name
) < 0)
416 return (ctf_set_errno (state
->cds_fp
, errno
));
418 if ((typestr
= ctf_dump_format_type (state
->cds_fp
, info
->ctb_type
,
419 CTF_ADD_ROOT
| CTF_FT_REFS
)) == NULL
)
422 return 0; /* Swallow the error. */
425 str
= str_append (str
, typestr
);
428 ctf_dump_append (state
, str
);
432 /* Dump all the object or function entries into the cds_items. */
435 ctf_dump_objts (ctf_dict_t
*fp
, ctf_dump_state_t
*state
, int functions
)
439 ctf_next_t
*i
= NULL
;
442 if ((functions
&& fp
->ctf_funcidx_names
)
443 || (!functions
&& fp
->ctf_objtidx_names
))
444 str
= str_append (str
, _("Section is indexed.\n"));
445 else if (fp
->ctf_ext_symtab
.cts_data
== NULL
)
446 str
= str_append (str
, _("No symbol table.\n"));
448 while ((id
= ctf_symbol_next (fp
, &i
, &name
, functions
)) != CTF_ERR
)
450 char *typestr
= NULL
;
452 /* Emit the name, if we know it. No trailing space: ctf_dump_format_type
453 has a leading one. */
456 if (asprintf (&str
, "%s -> ", name
) < 0)
462 if ((typestr
= ctf_dump_format_type (state
->cds_fp
, id
,
463 CTF_ADD_ROOT
| CTF_FT_REFS
)) == NULL
)
465 ctf_dump_append (state
, str
);
466 continue; /* Swallow the error. */
469 str
= str_append (str
, typestr
);
471 ctf_dump_append (state
, str
);
475 ctf_set_errno (fp
, ENOMEM
);
476 ctf_next_destroy (i
);
482 /* Dump a single variable into the cds_items. */
484 ctf_dump_var (const char *name
, ctf_id_t type
, void *arg
)
488 ctf_dump_state_t
*state
= arg
;
490 if (asprintf (&str
, "%s -> ", name
) < 0)
491 return (ctf_set_errno (state
->cds_fp
, errno
));
493 if ((typestr
= ctf_dump_format_type (state
->cds_fp
, type
,
494 CTF_ADD_ROOT
| CTF_FT_REFS
)) == NULL
)
497 return 0; /* Swallow the error. */
500 str
= str_append (str
, typestr
);
503 ctf_dump_append (state
, str
);
507 /* Dump a single struct/union member into the string in the membstate. */
509 ctf_dump_member (const char *name
, ctf_id_t id
, unsigned long offset
,
510 int depth
, void *arg
)
512 ctf_dump_membstate_t
*state
= arg
;
513 char *typestr
= NULL
;
516 /* The struct/union itself has already been printed. */
520 if (asprintf (&bit
, "%s%*s", state
->cdm_toplevel_indent
, (depth
-1)*4, "") < 0)
522 *state
->cdm_str
= str_append (*state
->cdm_str
, bit
);
525 if ((typestr
= ctf_dump_format_type (state
->cdm_fp
, id
,
526 CTF_ADD_ROOT
| CTF_FT_BITFIELD
527 | CTF_FT_ID
)) == NULL
)
528 return -1; /* errno is set for us. */
530 if (asprintf (&bit
, "[0x%lx] %s: %s\n", offset
, name
, typestr
) < 0)
533 *state
->cdm_str
= str_append (*state
->cdm_str
, bit
);
544 return (ctf_set_errno (state
->cdm_fp
, errno
));
547 /* Report the number of digits in the hexadecimal representation of a type
551 type_hex_digits (ctf_id_t id
)
558 for (; id
> 0; id
>>= 4, i
++);
562 /* Dump a single type into the cds_items. */
564 ctf_dump_type (ctf_id_t id
, int flag
, void *arg
)
568 ctf_dump_state_t
*state
= arg
;
569 ctf_dump_membstate_t membstate
= { &str
, state
->cds_fp
, NULL
};
572 if (asprintf (&indent
, " %*s", type_hex_digits (id
), "") < 0)
573 return (ctf_set_errno (state
->cds_fp
, ENOMEM
));
575 /* Dump the type itself. */
576 if ((str
= ctf_dump_format_type (state
->cds_fp
, id
,
577 flag
| CTF_FT_REFS
)) == NULL
)
579 str
= str_append (str
, "\n");
581 membstate
.cdm_toplevel_indent
= indent
;
583 /* Member dumping for structs, unions... */
584 if (ctf_type_kind (state
->cds_fp
, id
) == CTF_K_STRUCT
585 || ctf_type_kind (state
->cds_fp
, id
) == CTF_K_UNION
)
587 if ((ctf_type_visit (state
->cds_fp
, id
, ctf_dump_member
, &membstate
)) < 0)
589 if (id
== 0 || ctf_errno (state
->cds_fp
) == ECTF_NONREPRESENTABLE
)
591 ctf_dump_append (state
, str
);
594 ctf_err_warn (state
->cds_fp
, 1, ctf_errno (state
->cds_fp
),
595 _("cannot visit members dumping type 0x%lx"), id
);
600 /* ... and enums, for which we dump the first and last few members and skip
601 the ones in the middle. */
602 if (ctf_type_kind (state
->cds_fp
, id
) == CTF_K_ENUM
)
604 int enum_count
= ctf_member_count (state
->cds_fp
, id
);
605 ctf_next_t
*it
= NULL
;
607 const char *enumerand
;
611 while ((enumerand
= ctf_enum_next (state
->cds_fp
, id
,
612 &it
, &value
)) != NULL
)
615 if ((i
> 5) && (i
< enum_count
- 4))
618 str
= str_append (str
, indent
);
620 if (asprintf (&bit
, "%s: %i\n", enumerand
, value
) < 0)
622 ctf_next_destroy (it
);
625 str
= str_append (str
, bit
);
628 if ((i
== 5) && (enum_count
> 10))
630 str
= str_append (str
, indent
);
631 str
= str_append (str
, "...\n");
634 if (ctf_errno (state
->cds_fp
) != ECTF_NEXT_END
)
636 ctf_err_warn (state
->cds_fp
, 1, ctf_errno (state
->cds_fp
),
637 _("cannot visit enumerands dumping type 0x%lx"), id
);
642 ctf_dump_append (state
, str
);
651 /* Swallow the error: don't cause an error in one type to abort all
658 return ctf_set_errno (state
->cds_fp
, ENOMEM
);
661 /* Dump the string table into the cds_items. */
664 ctf_dump_str (ctf_dict_t
*fp
, ctf_dump_state_t
*state
)
666 const char *s
= fp
->ctf_str
[CTF_STRTAB_0
].cts_strs
;
668 for (; s
< fp
->ctf_str
[CTF_STRTAB_0
].cts_strs
+
669 fp
->ctf_str
[CTF_STRTAB_0
].cts_len
;)
672 if (asprintf (&str
, "0x%lx: %s",
673 (unsigned long) (s
- fp
->ctf_str
[CTF_STRTAB_0
].cts_strs
),
675 return (ctf_set_errno (fp
, errno
));
676 ctf_dump_append (state
, str
);
683 /* Dump a particular section of a CTF file, in textual form. Call with a
684 pointer to a NULL STATE: each call emits a dynamically allocated string
685 containing a description of one entity in the specified section, in order.
686 Only the first call (with a NULL state) may vary SECT. Once the CTF section
687 has been entirely dumped, the call returns NULL and frees and annuls the
688 STATE, ready for another section to be dumped. The returned textual content
689 may span multiple lines: between each call the FUNC is called with one
690 textual line at a time, and should return a suitably decorated line (it can
691 allocate a new one and return it if it likes). */
694 ctf_dump (ctf_dict_t
*fp
, ctf_dump_state_t
**statep
, ctf_sect_names_t sect
,
695 ctf_dump_decorate_f
*func
, void *arg
)
699 ctf_dump_state_t
*state
= NULL
;
703 /* Data collection. Transforming a call-at-a-time iterator into a
704 return-at-a-time iterator in a language without call/cc is annoying. It
705 is easiest to simply collect everything at once and then return it bit
706 by bit. The first call will take (much) longer than otherwise, but the
707 amortized time needed is the same. */
709 if ((*statep
= malloc (sizeof (struct ctf_dump_state
))) == NULL
)
711 ctf_set_errno (fp
, ENOMEM
);
716 memset (state
, 0, sizeof (struct ctf_dump_state
));
718 state
->cds_sect
= sect
;
722 case CTF_SECT_HEADER
:
723 ctf_dump_header (fp
, state
);
726 if (ctf_label_iter (fp
, ctf_dump_label
, state
) < 0)
728 if (ctf_errno (fp
) != ECTF_NOLABELDATA
)
729 goto end
; /* errno is set for us. */
730 ctf_set_errno (fp
, 0);
734 if (ctf_dump_objts (fp
, state
, 0) < 0)
735 goto end
; /* errno is set for us. */
738 if (ctf_dump_objts (fp
, state
, 1) < 0)
739 goto end
; /* errno is set for us. */
742 if (ctf_variable_iter (fp
, ctf_dump_var
, state
) < 0)
743 goto end
; /* errno is set for us. */
746 if (ctf_type_iter_all (fp
, ctf_dump_type
, state
) < 0)
747 goto end
; /* errno is set for us. */
750 ctf_dump_str (fp
, state
);
753 ctf_set_errno (fp
, ECTF_DUMPSECTUNKNOWN
);
761 if (state
->cds_sect
!= sect
)
763 ctf_set_errno (fp
, ECTF_DUMPSECTCHANGED
);
768 if (state
->cds_current
== NULL
)
769 state
->cds_current
= ctf_list_next (&state
->cds_items
);
771 state
->cds_current
= ctf_list_next (state
->cds_current
);
773 if (state
->cds_current
== NULL
)
776 /* Hookery. There is some extra complexity to preserve linefeeds within each
777 item while removing linefeeds at the end. */
783 for (line
= state
->cds_current
->cdi_item
; line
&& *line
; )
788 nline
= strchr (line
, '\n');
792 ret
= func (sect
, line
, arg
);
793 str
= str_append (str
, ret
);
794 str
= str_append (str
, "\n");
809 if (str
[len
-1] == '\n')
814 str
= strdup (state
->cds_current
->cdi_item
);
817 ctf_set_errno (fp
, ENOMEM
);
822 ctf_set_errno (fp
, 0);
826 ctf_dump_free (state
);
828 ctf_set_errno (fp
, 0);