libctf: add linking of the variable section
[deliverable/binutils-gdb.git] / libctf / ctf-dump.c
index 3dac435eba39e0587e4a3aa86c928edfee4d62ef..2a888e136396c6af74564c8053d35ad976b799a5 100644 (file)
@@ -95,7 +95,7 @@ ctf_is_slice (ctf_file_t *fp, ctf_id_t id, ctf_encoding_t *enc)
    type's references.  */
 
 static char *
-ctf_dump_format_type (ctf_file_t *fp, ctf_id_t id)
+ctf_dump_format_type (ctf_file_t *fp, ctf_id_t id, int flag)
 {
   ctf_id_t new_id;
   char *str = NULL, *bit = NULL, *buf = NULL;
@@ -104,8 +104,16 @@ ctf_dump_format_type (ctf_file_t *fp, ctf_id_t id)
   do
     {
       ctf_encoding_t enc;
+      const char *nonroot_leader = "";
+      const char *nonroot_trailer = "";
 
       id = new_id;
+      if (flag == CTF_ADD_NONROOT)
+       {
+         nonroot_leader = "{";
+         nonroot_trailer = "}";
+       }
+
       buf = ctf_type_aname (fp, id);
       if (!buf)
        goto oom;
@@ -115,15 +123,17 @@ ctf_dump_format_type (ctf_file_t *fp, ctf_id_t id)
       if (ctf_is_slice (fp, id, &enc))
        {
          ctf_type_encoding (fp, id, &enc);
-         if (asprintf (&bit, " %lx: [slice 0x%x:0x%x]",
-                       id, enc.cte_offset, enc.cte_bits) < 0)
+         if (asprintf (&bit, " %s%lx: [slice 0x%x:0x%x]%s",
+                       nonroot_leader, id, enc.cte_offset, enc.cte_bits,
+                       nonroot_trailer) < 0)
            goto oom;
        }
       else
        {
-         if (asprintf (&bit, " %lx: %s (size %lx)", id, buf[0] == '\0' ?
-                       "(nameless)" : buf,
-                       (unsigned long) ctf_type_size (fp, id)) < 0)
+         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)
            goto oom;
        }
       free (buf);
@@ -153,6 +163,132 @@ ctf_dump_format_type (ctf_file_t *fp, ctf_id_t id)
   return NULL;
 }
 
+/* Dump one string field from the file header into the cds_items.  */
+static int
+ctf_dump_header_strfield (ctf_file_t *fp, ctf_dump_state_t *state,
+                         const char *name, uint32_t value)
+{
+  char *str;
+  if (value)
+    {
+      if (asprintf (&str, "%s: %s\n", name, ctf_strptr (fp, value)) < 0)
+       goto err;
+      ctf_dump_append (state, str);
+    }
+  return 0;
+
+ err:
+  return (ctf_set_errno (fp, -ENOMEM));
+}
+
+/* Dump one section-offset field from the file header into the cds_items.  */
+static int
+ctf_dump_header_sectfield (ctf_file_t *fp, ctf_dump_state_t *state,
+                          const char *sect, uint32_t off, uint32_t nextoff)
+{
+  char *str;
+  if (nextoff - off)
+    {
+      if (asprintf (&str, "%s:\t0x%lx -- 0x%lx (0x%lx bytes)\n", sect,
+                   (unsigned long) off, (unsigned long) (nextoff - 1),
+                   (unsigned long) (nextoff - off)) < 0)
+       goto err;
+      ctf_dump_append (state, str);
+    }
+  return 0;
+
+ err:
+  return (ctf_set_errno (fp, -ENOMEM));
+}
+
+/* Dump the file header into the cds_items.  */
+static int
+ctf_dump_header (ctf_file_t *fp, ctf_dump_state_t *state)
+{
+  char *str;
+  const ctf_header_t *hp = fp->ctf_header;
+  const char *vertab[] =
+    {
+     NULL, "CTF_VERSION_1",
+     "CTF_VERSION_1_UPGRADED_3 (latest format, version 1 type "
+     "boundaries)",
+     "CTF_VERSION_2",
+     "CTF_VERSION_3", NULL
+    };
+  const char *verstr = NULL;
+
+  if (asprintf (&str, "Magic number: %x\n", hp->cth_magic) < 0)
+      goto err;
+  ctf_dump_append (state, str);
+
+  if (hp->cth_version <= CTF_VERSION)
+    verstr = vertab[hp->cth_version];
+
+  if (verstr == NULL)
+    verstr = "(not a valid version)";
+
+  if (asprintf (&str, "Version: %i (%s)\n", hp->cth_version,
+               verstr) < 0)
+    goto err;
+  ctf_dump_append (state, str);
+
+  /* Everything else is only printed if present.  */
+
+  /* The flags are unusual in that they represent the ctf_file_t *in memory*:
+     flags representing compression, etc, are turned off as the file is
+     decompressed.  So we store a copy of the flags before they are changed, for
+     the dumper.  */
+
+  if (fp->ctf_openflags > 0)
+    {
+      if (fp->ctf_openflags)
+       if (asprintf (&str, "Flags: 0x%x (%s)", fp->ctf_openflags,
+                     fp->ctf_openflags & CTF_F_COMPRESS ? "CTF_F_COMPRESS"
+                                                        : "") < 0)
+       goto err;
+      ctf_dump_append (state, str);
+    }
+
+  if (ctf_dump_header_strfield (fp, state, "Parent label",
+                               hp->cth_parlabel) < 0)
+    goto err;
+
+  if (ctf_dump_header_strfield (fp, state, "Parent name", hp->cth_parname) < 0)
+    goto err;
+
+  if (ctf_dump_header_strfield (fp, state, "Compilation unit name",
+                               hp->cth_cuname) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "Label section", hp->cth_lbloff,
+                                hp->cth_objtoff) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "Data object section",
+                                hp->cth_objtoff, hp->cth_funcoff) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "Function info section",
+                                hp->cth_funcoff, hp->cth_varoff) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "Variable section",
+                                hp->cth_varoff, hp->cth_typeoff) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "Type section",
+                                hp->cth_typeoff, hp->cth_stroff) < 0)
+    goto err;
+
+  if (ctf_dump_header_sectfield (fp, state, "String section", hp->cth_stroff,
+                                hp->cth_stroff + hp->cth_strlen + 1) < 0)
+    goto err;
+
+  return 0;
+ err:
+  return (ctf_set_errno (fp, -ENOMEM));
+}
+
 /* Dump a single label into the cds_items.  */
 
 static int
@@ -166,7 +302,8 @@ ctf_dump_label (const char *name, const ctf_lblinfo_t *info,
   if (asprintf (&str, "%s -> ", name) < 0)
     return (ctf_set_errno (state->cds_fp, ENOMEM));
 
-  if ((typestr = ctf_dump_format_type (state->cds_fp, info->ctb_type)) == NULL)
+  if ((typestr = ctf_dump_format_type (state->cds_fp, info->ctb_type,
+                                      CTF_ADD_ROOT)) == NULL)
     {
       free (str);
       return -1;                       /* errno is set for us.  */
@@ -222,7 +359,8 @@ ctf_dump_objts (ctf_file_t *fp, ctf_dump_state_t *state)
        }
 
       /* Variable type.  */
-      if ((typestr = ctf_dump_format_type (state->cds_fp, type)) == NULL)
+      if ((typestr = ctf_dump_format_type (state->cds_fp, type,
+                                          CTF_ADD_ROOT)) == NULL)
        {
          free (str);
          return -1;                    /* errno is set for us.  */
@@ -263,7 +401,8 @@ ctf_dump_funcs (ctf_file_t *fp, ctf_dump_state_t *state)
          case ECTF_NOSYMTAB:
            return -1;
          case ECTF_NOTDATA:
-         case ECTF_NOTYPEDAT:
+         case ECTF_NOTFUNC:
+         case ECTF_NOFUNCDAT:
            continue;
          }
       if ((args = calloc (fi.ctc_argc, sizeof (ctf_id_t))) == NULL)
@@ -280,12 +419,12 @@ ctf_dump_funcs (ctf_file_t *fp, ctf_dump_state_t *state)
       sym_name = ctf_lookup_symbol_name (fp, i);
       if (sym_name[0] == '\0')
        {
-         if (asprintf (&bit, "%lx ", (unsigned long) i) < 0)
+         if (asprintf (&bit, "0x%lx ", (unsigned long) i) < 0)
            goto oom;
        }
       else
        {
-         if (asprintf (&bit, "%s (%lx) ", sym_name, (unsigned long) i) < 0)
+         if (asprintf (&bit, "%s (0x%lx) ", sym_name, (unsigned long) i) < 0)
            goto oom;
        }
       str = ctf_str_append (str, bit);
@@ -338,7 +477,8 @@ ctf_dump_var (const char *name, ctf_id_t type, void *arg)
   if (asprintf (&str, "%s -> ", name) < 0)
     return (ctf_set_errno (state->cds_fp, ENOMEM));
 
-  if ((typestr = ctf_dump_format_type (state->cds_fp, type)) == NULL)
+  if ((typestr = ctf_dump_format_type (state->cds_fp, type,
+                                      CTF_ADD_ROOT)) == NULL)
     {
       free (str);
       return -1;                       /* errno is set for us.  */
@@ -403,14 +543,14 @@ ctf_dump_member (const char *name, ctf_id_t id, unsigned long offset,
 /* Dump a single type into the cds_items.  */
 
 static int
-ctf_dump_type (ctf_id_t id, void *arg)
+ctf_dump_type (ctf_id_t id, int flag, void *arg)
 {
   char *str;
   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)) == NULL)
+  if ((str = ctf_dump_format_type (state->cds_fp, id, flag)) == NULL)
     goto err;
 
   str = ctf_str_append (str, "\n");
@@ -492,8 +632,7 @@ ctf_dump (ctf_file_t *fp, ctf_dump_state_t **statep, ctf_sect_names_t sect,
       switch (sect)
        {
        case CTF_SECT_HEADER:
-         /* Nothing doable (yet): entire header is discarded after read-phase.  */
-         str = strdup ("");
+         ctf_dump_header (fp, state);
          break;
        case CTF_SECT_LABEL:
          if (ctf_label_iter (fp, ctf_dump_label, state) < 0)
@@ -516,7 +655,7 @@ ctf_dump (ctf_file_t *fp, ctf_dump_state_t **statep, ctf_sect_names_t sect,
            goto end;                   /* errno is set for us.  */
          break;
        case CTF_SECT_TYPE:
-         if (ctf_type_iter (fp, ctf_dump_type, state) < 0)
+         if (ctf_type_iter_all (fp, ctf_dump_type, state) < 0)
            goto end;                   /* errno is set for us.  */
          break;
        case CTF_SECT_STR:
This page took 0.025386 seconds and 4 git commands to generate.