repo-fixing: fixup #1.
[profile/ivi/murphy.git] / src / core / console-command.c
index 1d58ea2..ef680e2 100644 (file)
@@ -298,6 +298,128 @@ static void debug_show(mrp_console_t *c, void *user_data,
     mrp_debug_dump_config(c->stdout);
 }
 
+#define __GNU_SOURCE
+#include <link.h>
+#include <elf.h>
+
+typedef struct {
+    mrp_console_t *c;
+} list_data_t;
+
+
+#undef __DUMP_ELF_INFO__
+
+#ifdef __DUMP_ELF_IFDO__
+static const char *segment_type(uint32_t type)
+{
+#define T(type) case type: return #type
+    switch (type) {
+        T(PT_NULL);
+        T(PT_LOAD);
+        T(PT_DYNAMIC);
+        T(PT_INTERP);
+        T(PT_NOTE);
+        T(PT_SHLIB);
+        T(PT_PHDR);
+        T(PT_TLS);
+        T(PT_NUM);
+        T(PT_LOOS);
+        T(PT_GNU_EH_FRAME);
+        T(PT_GNU_STACK);
+        T(PT_GNU_RELRO);
+        T(PT_LOPROC);
+        T(PT_HIPROC);
+    default:
+        return "unknown";
+    }
+}
+
+
+static const char *segment_flags(uint32_t flags)
+{
+    static char buf[4];
+
+    buf[0] = (flags & PF_R) ? 'r' : '-';
+    buf[1] = (flags & PF_W) ? 'w' : '-';
+    buf[2] = (flags & PF_X) ? 'x' : '-';
+    buf[3] = '\0';
+
+    return buf;
+}
+
+#endif /* __DUMP_ELF_INFO__ */
+
+static int list_cb(struct dl_phdr_info *info, size_t size, void *data)
+{
+#define P(fmt, args...) fprintf(c->stdout, fmt , ## args)
+#define RELOC(addr) (info->dlpi_addr + addr)
+
+    mrp_console_t    *c = (mrp_console_t *)data;
+    const ElfW(Phdr) *h;
+    int               i;
+    const char       *beg, *end, *s, *func;
+    char              file[512], *p;
+    int               line;
+
+    MRP_UNUSED(size);
+
+#ifdef __DUMP_ELF_INFO__
+    P("%s (@%p)\n",
+      info->dlpi_name && *info->dlpi_name ? info->dlpi_name : "<none>",
+      info->dlpi_addr);
+    P("  %d segments\n", info->dlpi_phnum);
+#endif
+
+    file[sizeof(file) - 1] = '\0';
+
+    for (i = 0; i < info->dlpi_phnum; i++) {
+        h = &info->dlpi_phdr[i];
+#if __DUMP_ELF_INFO__
+        P("  #%d:\n", i);
+        P("       type: 0x%x (%s)\n", h->p_type, segment_type(h->p_type));
+        P("     offset: 0x%lx\n", h->p_offset);
+        P("      vaddr: 0x%lx (0x%lx)\n", h->p_vaddr, RELOC(h->p_vaddr));
+        P("      paddr: 0x%lx (0x%lx)\n", h->p_paddr, RELOC(h->p_paddr));
+        P("     filesz: 0x%lx\n", h->p_filesz);
+        P("      memsz: 0x%lx\n", h->p_memsz);
+        P("      flags: 0x%x (%s)\n", h->p_flags, segment_flags(h->p_flags));
+        P("      align: 0x%lx\n", h->p_align);
+#endif
+        if (h->p_flags & PF_W)
+            continue;
+
+        beg = (const char *)RELOC(h->p_vaddr);
+        end = (const char *)beg + h->p_memsz;
+
+#define PREFIX     "__DEBUG_SITE_"
+#define PREFIX_LEN 13
+        for (s = beg; s < end - PREFIX_LEN; s++) {
+            if (!strncmp(s, PREFIX, PREFIX_LEN)) {
+                s += PREFIX_LEN;
+                if (*s != '\0') {
+                    strncpy(file, s, sizeof(file) - 1);
+                    p = strchr(file, ':');
+
+                    if (p != NULL) {
+                        *p = '\0';
+                        line = (int)strtoul(p + 1, NULL, 10);
+                        func = mrp_debug_site_function(file, line);
+                    }
+                    else
+                        func = NULL;
+
+                    if (func != NULL)
+                        P("  %s@%s\n", func, s);
+                    else
+                        P("  %s\n", s);
+                }
+            }
+        }
+    }
+
+    return 0;
+}
+
 
 static void debug_list(mrp_console_t *c, void *user_data,
                        int argc, char **argv)
@@ -308,7 +430,7 @@ static void debug_list(mrp_console_t *c, void *user_data,
     MRP_UNUSED(argv);
 
     fprintf(c->stdout, "Available debug sites:\n");
-    mrp_debug_dump_sites(c->stdout, 4);
+    dl_iterate_phdr(list_cb, (void *)c);
 }