Dump function with "-f"
authorMartin Pieuchot <mpi@openbsd.org>
Mon, 14 Mar 2016 20:16:39 +0000 (21:16 +0100)
committerMartin Pieuchot <mpi@openbsd.org>
Wed, 16 Mar 2016 08:18:26 +0000 (09:18 +0100)
ctf.h
ctfdump.c

diff --git a/ctf.h b/ctf.h
index a2d94cf01f0b5acd15984c0aa6ab7c0e1beb83bf..55e1e2f3f38dff6961d1af957fd75a9c19e15e27 100644 (file)
--- a/ctf.h
+++ b/ctf.h
@@ -82,9 +82,33 @@ struct ctf_enum {
 #define CTF_VERSION            2
 
 #define CTF_MAX_NAME           0x7fffffff
+#define CTF_MAX_VLEN           0x03ff
+
 #define CTF_STRTAB_0           0
 #define CTF_STRTAB_1           1
 
+/*
+ * Info macro.
+ */
+#define CTF_INFO_VLEN(i)       (((i) & CTF_MAX_VLEN))
+#define CTF_INFO_ISROOT(i)     (((i) & 0x0400) >> 10)
+#define CTF_INFO_KIND(i)       (((i) & 0xf800) >> 11)
+#define  CTF_K_UNKNOWN         0
+#define  CTF_K_INTEGER         1
+#define  CTF_K_FLOAT           2
+#define  CTF_K_POINTER         3
+#define  CTF_K_ARRAY           4
+#define  CTF_K_FUNCTION                5
+#define  CTF_K_STRUCT          6
+#define  CTF_K_UNION           7
+#define  CTF_K_ENUM            8
+#define  CTF_K_FORWARD         9
+#define  CTF_K_TYPEDEF         10
+#define  CTF_K_VOLATILE                11
+#define  CTF_K_CONST           12
+#define  CTF_K_RESTRICT                13
+#define  CTF_K_MAX             31
+
 /*
  * Name reference macro.
  */
index 7ccf9bf9cdbf897ed38866d3c88b39cb3f18b317..0474b562fe92169721573ba6087b63e6704d355a 100644 (file)
--- a/ctfdump.c
+++ b/ctfdump.c
@@ -76,6 +76,9 @@ main(int argc, char *argv[])
                case 'd':
                        flags |= DUMP_OBJECT;
                        break;
+               case 'f':
+                       flags |= DUMP_FUNCTION;
+                       break;
                case 'h':
                        flags |= DUMP_HEADER;
                        break;
@@ -434,6 +437,31 @@ ctf_dump(const char *p, size_t size, uint32_t flags)
                }
        }
 
+       if (flags & DUMP_FUNCTION) {
+               unsigned short          *fsp, kind, vlen;
+               size_t                   idx = 0, i = 0;
+               const char              *s;
+               int                      l;
+
+               fsp = (unsigned short *)(data + cth->cth_funcoff);
+               while (fsp < (unsigned short *)(data + cth->cth_typeoff)) {
+                       kind = CTF_INFO_KIND(*fsp);
+                       vlen = CTF_INFO_VLEN(*fsp);
+                       fsp++;
+
+                       if (kind == CTF_K_UNKNOWN && vlen == 0)
+                               continue;
+
+                       l = printf("%u [%zu] FUNC", vlen, i++);
+                       if ((s = elf_idx2sym(&idx, STT_FUNC)) != NULL)
+                               printf(" (%s)", s);
+                       printf(" returns: %u args: (", *fsp++);
+                       while (vlen-- > 0)
+                               printf("%u%s", *fsp++, (vlen > 0) ? ", " : "");
+                       printf(")\n");
+               }
+       }
+
        if (cth->cth_flags & CTF_F_COMPRESS)
                free(data);