/* Textual dumping of CTF data.
- Copyright (C) 2019 Free Software Foundation, Inc.
+ Copyright (C) 2019-2020 Free Software Foundation, Inc.
This file is part of libctf.
#include <ctf-impl.h>
#include <string.h>
+#define str_append(s, a) ctf_str_append_noerr (s, a)
+
/* One item to be dumped, in string form. */
typedef struct ctf_dump_item
}
}
-/* Slices need special handling to distinguish them from their referenced
- type. */
-
-static int
-ctf_is_slice (ctf_file_t *fp, ctf_id_t id, ctf_encoding_t *enc)
-{
- int kind = ctf_type_kind (fp, id);
-
- return (((kind == CTF_K_INTEGER) || (kind == CTF_K_ENUM)
- || (kind == CTF_K_FLOAT))
- && ctf_type_reference (fp, id) != CTF_ERR
- && ctf_type_encoding (fp, id, enc) == 0);
-}
-
/* Return a dump for a single type, without member info: but do show the
type's references. */
{
if (id == 0 || ctf_errno (fp) == ECTF_NONREPRESENTABLE)
{
- str = ctf_str_append (str, " (type not represented in CTF)");
+ str = str_append (str, " (type not represented in CTF)");
ctf_set_errno (fp, ECTF_NOTREF);
break;
}
goto err;
}
- /* Slices get a different print representation. */
+ if (asprintf (&bit, " %s%lx: ", nonroot_leader, id) < 0)
+ goto oom;
+ str = str_append (str, bit);
+ free (bit);
+ bit = NULL;
- if (ctf_is_slice (fp, id, &enc))
+ if (buf[0] != '\0')
+ {
+ str = str_append (str, buf);
+ str = str_append (str, " ");
+ }
+
+ free (buf);
+ buf = NULL;
+
+ /* Slices get a different print representation. */
+ if (ctf_type_kind_unsliced (fp, id) == CTF_K_SLICE)
{
ctf_type_encoding (fp, id, &enc);
- if (asprintf (&bit, " %s%lx: [slice 0x%x:0x%x]%s",
- nonroot_leader, id, enc.cte_offset, enc.cte_bits,
- nonroot_trailer) < 0)
+ if (asprintf (&bit, "[slice 0x%x:0x%x] ",
+ enc.cte_offset, enc.cte_bits) < 0)
goto oom;
}
- else
+ else if (ctf_type_kind (fp, id) == CTF_K_INTEGER)
{
- if (asprintf (&bit, " %s%lx: %s (size 0x%lx)%s", nonroot_leader,
- id, buf[0] == '\0' ? "(nameless)" : buf,
- (unsigned long) ctf_type_size (fp, id),
- nonroot_trailer) < 0)
+ ctf_type_encoding (fp, id, &enc);
+ if (asprintf (&bit, "[0x%x:0x%x] ",
+ enc.cte_offset, enc.cte_bits) < 0)
goto oom;
}
- free (buf);
- buf = NULL;
- str = ctf_str_append (str, bit);
+ str = str_append (str, bit);
+ free (bit);
+ bit = NULL;
+
+ if (asprintf (&bit, "(size 0x%lx)%s",
+ (unsigned long) ctf_type_size (fp, id),
+ nonroot_trailer) < 0)
+ goto oom;
+
+ str = str_append (str, bit);
free (bit);
bit = NULL;
new_id = ctf_type_reference (fp, id);
if (new_id != CTF_ERR)
- str = ctf_str_append (str, " ->");
+ str = str_append (str, " ->");
} while (new_id != CTF_ERR);
if (ctf_errno (fp) != ECTF_NOTREF)
oom:
ctf_set_errno (fp, errno);
err:
+ ctf_err_warn (fp, 1, 0, _("cannot format name dumping type 0x%lx"), id);
free (buf);
free (str);
free (bit);
CTF_ADD_ROOT)) == NULL)
{
free (str);
- return -1; /* errno is set for us. */
+ return 0; /* Swallow the error. */
}
- str = ctf_str_append (str, typestr);
+ str = str_append (str, typestr);
free (typestr);
ctf_dump_append (state, str);
CTF_ADD_ROOT)) == NULL)
{
free (str);
- return -1; /* errno is set for us. */
+ return 0; /* Swallow the error. */
}
- str = ctf_str_append (str, typestr);
+ str = str_append (str, typestr);
free (typestr);
ctf_dump_append (state, str);
for (i = 0; i < fp->ctf_nsyms; i++)
{
char *str;
- char *bit;
- const char *err;
+ char *bit = NULL;
const char *sym_name;
ctf_funcinfo_t fi;
ctf_id_t type;
- size_t j;
- ctf_id_t *args;
if ((type = ctf_func_info (state->cds_fp, i, &fi)) == CTF_ERR)
switch (ctf_errno (state->cds_fp))
case ECTF_NOFUNCDAT:
continue;
}
- if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
- return (ctf_set_errno (fp, ENOMEM));
- /* Return type. */
- if ((str = ctf_type_aname (state->cds_fp, type)) == NULL)
+ /* Return type and all args. */
+ if ((bit = ctf_type_aname (state->cds_fp, type)) == NULL)
{
- err = "look up return type";
- goto err;
+ ctf_err_warn (fp, 1, ctf_errno (state->cds_fp),
+ _("cannot look up return type dumping function type "
+ "for symbol 0x%li"), (unsigned long) i);
+ free (bit);
+ return -1; /* errno is set for us. */
}
- str = ctf_str_append (str, " ");
-
- /* Function name. */
+ /* Replace in the returned string, dropping in the function name. */
sym_name = ctf_lookup_symbol_name (fp, i);
- if (sym_name[0] == '\0')
- {
- if (asprintf (&bit, "0x%lx ", (unsigned long) i) < 0)
- goto oom;
- }
- else
+ if (sym_name[0] != '\0')
{
- if (asprintf (&bit, "%s (0x%lx) ", sym_name, (unsigned long) i) < 0)
+ char *retstar;
+ char *new_bit;
+ char *walk;
+
+ new_bit = malloc (strlen (bit) + 1 + strlen (sym_name));
+ if (!new_bit)
goto oom;
- }
- str = ctf_str_append (str, bit);
- str = ctf_str_append (str, " (");
- free (bit);
- /* Function arguments. */
+ /* See ctf_type_aname. */
+ retstar = strstr (bit, "(*) (");
+ if (!ctf_assert (fp, retstar))
+ goto assert_err;
+ retstar += 2; /* After the '*' */
- if (ctf_func_args (state->cds_fp, i, fi.ctc_argc, args) < 0)
- {
- err = "look up argument type";
- goto err;
- }
+ /* C is not good at search-and-replace. */
+ walk = new_bit;
+ memcpy (walk, bit, retstar - bit);
+ walk += (retstar - bit);
+ strcpy (walk, sym_name);
+ walk += strlen (sym_name);
+ strcpy (walk, retstar);
- for (j = 0; j < fi.ctc_argc; j++)
- {
- if ((bit = ctf_type_aname (state->cds_fp, args[j])) == NULL)
- {
- err = "look up argument type name";
- goto err;
- }
- str = ctf_str_append (str, bit);
- if ((j < fi.ctc_argc - 1) || (fi.ctc_flags & CTF_FUNC_VARARG))
- str = ctf_str_append (str, ", ");
free (bit);
+ bit = new_bit;
}
- if (fi.ctc_flags & CTF_FUNC_VARARG)
- str = ctf_str_append (str, "...");
- str = ctf_str_append (str, ")");
+ if (asprintf (&str, "Symbol 0x%lx: %s", (unsigned long) i, bit) < 0)
+ goto oom;
+ free (bit);
- free (args);
ctf_dump_append (state, str);
continue;
oom:
- free (args);
- free (str);
+ free (bit);
return (ctf_set_errno (fp, errno));
- err:
- ctf_dprintf ("Cannot %s dumping function type for symbol 0x%li: %s\n",
- err, (unsigned long) i,
- ctf_errmsg (ctf_errno (state->cds_fp)));
- free (args);
- free (str);
+
+ assert_err:
+ free (bit);
return -1; /* errno is set for us. */
}
return 0;
CTF_ADD_ROOT)) == NULL)
{
free (str);
- return -1; /* errno is set for us. */
+ return 0; /* Swallow the error. */
}
- str = ctf_str_append (str, typestr);
+ str = str_append (str, typestr);
free (typestr);
ctf_dump_append (state, str);
char *typestr = NULL;
char *bit = NULL;
ctf_encoding_t ep;
+ int has_encoding = 0;
ssize_t i;
for (i = 0; i < depth; i++)
- *state->cdm_str = ctf_str_append (*state->cdm_str, " ");
+ *state->cdm_str = str_append (*state->cdm_str, " ");
if ((typestr = ctf_type_aname (state->cdm_fp, id)) == NULL)
{
offset) < 0)
goto oom;
- *state->cdm_str = ctf_str_append (*state->cdm_str, bit);
+ *state->cdm_str = str_append (*state->cdm_str, bit);
free (typestr);
free (bit);
return 0;
}
- goto oom;
+ return -1; /* errno is set for us. */
+ }
+
+ if (ctf_type_encoding (state->cdm_fp, id, &ep) == 0)
+ {
+ has_encoding = 1;
+ ctf_type_encoding (state->cdm_fp, id, &ep);
+
+ if (asprintf (&bit, " [0x%lx] (ID 0x%lx) (kind %i) %s%s%s:%i "
+ "(aligned at 0x%lx", offset, id,
+ ctf_type_kind (state->cdm_fp, id), typestr,
+ (name[0] != 0 && typestr[0] != 0) ? " " : "", name,
+ ep.cte_bits, (unsigned long) ctf_type_align (state->cdm_fp,
+ id)) < 0)
+ goto oom;
+ }
+ else
+ {
+ if (asprintf (&bit, " [0x%lx] (ID 0x%lx) (kind %i) %s%s%s "
+ "(aligned at 0x%lx", offset, id,
+ ctf_type_kind (state->cdm_fp, id), typestr,
+ (name[0] != 0 && typestr[0] != 0) ? " " : "", name,
+ (unsigned long) ctf_type_align (state->cdm_fp, id)) < 0)
+ goto oom;
}
- if (asprintf (&bit, " [0x%lx] (ID 0x%lx) (kind %i) %s %s (aligned at 0x%lx",
- offset, id, ctf_type_kind (state->cdm_fp, id), typestr, name,
- (unsigned long) ctf_type_align (state->cdm_fp, id)) < 0)
- goto oom;
- *state->cdm_str = ctf_str_append (*state->cdm_str, bit);
+ *state->cdm_str = str_append (*state->cdm_str, bit);
free (typestr);
free (bit);
typestr = NULL;
bit = NULL;
- if ((ctf_type_kind (state->cdm_fp, id) == CTF_K_INTEGER)
- || (ctf_type_kind (state->cdm_fp, id) == CTF_K_FLOAT)
- || (ctf_is_slice (state->cdm_fp, id, &ep) == CTF_K_ENUM))
+ if (has_encoding)
{
- ctf_type_encoding (state->cdm_fp, id, &ep);
if (asprintf (&bit, ", format 0x%x, offset:bits 0x%x:0x%x", ep.cte_format,
ep.cte_offset, ep.cte_bits) < 0)
goto oom;
- *state->cdm_str = ctf_str_append (*state->cdm_str, bit);
+ *state->cdm_str = str_append (*state->cdm_str, bit);
free (bit);
bit = NULL;
}
- *state->cdm_str = ctf_str_append (*state->cdm_str, ")\n");
+ *state->cdm_str = str_append (*state->cdm_str, ")\n");
return 0;
oom:
ctf_dump_type (ctf_id_t id, int flag, void *arg)
{
char *str;
- const char *err;
ctf_dump_state_t *state = arg;
ctf_dump_membstate_t membstate = { &str, state->cds_fp };
size_t len;
if ((str = ctf_dump_format_type (state->cds_fp, id, flag)) == NULL)
- {
- err = "format type";
- goto err;
- }
+ goto err;
- str = ctf_str_append (str, "\n");
+ str = str_append (str, "\n");
if ((ctf_type_visit (state->cds_fp, id, ctf_dump_member, &membstate)) < 0)
{
if (id == 0 || ctf_errno (state->cds_fp) == ECTF_NONREPRESENTABLE)
ctf_dump_append (state, str);
return 0;
}
- err = "visit members";
+ ctf_err_warn (state->cds_fp, 1, ctf_errno (state->cds_fp),
+ _("cannot visit members dumping type 0x%lx"), id);
goto err;
}
return 0;
err:
- ctf_dprintf ("Cannot %s dumping type 0x%lx: %s\n", err, id,
- ctf_errmsg (ctf_errno (state->cds_fp)));
free (str);
- return -1; /* errno is set for us. */
+ return 0; /* Swallow the error. */
}
/* Dump the string table into the cds_items. */
nline[0] = '\0';
ret = func (sect, line, arg);
- str = ctf_str_append (str, ret);
- str = ctf_str_append (str, "\n");
+ str = str_append (str, ret);
+ str = str_append (str, "\n");
if (ret != line)
free (ret);
str[len-1] = '\0';
}
else
- str = strdup (state->cds_current->cdi_item);
+ {
+ str = strdup (state->cds_current->cdi_item);
+ if (!str)
+ {
+ ctf_set_errno (fp, ENOMEM);
+ return str;
+ }
+ }
ctf_set_errno (fp, 0);
return str;