When debugging eh_frame issues, the error messages aren't all that helpful
without knowing what file a corresponding .eh_frame[_hdr] section belongs
to.  Prefix debug output strings with the file name.

Based on Josh Poimboeuf's, Steven Rostedt's, and my unwind user sframe
implementation.

Signed-off-by: Jens Remus <[email protected]>
---
 include/linux/eh_frame.h       |  4 ++++
 kernel/unwind/eh_frame.c       | 13 ++++++++----
 kernel/unwind/eh_frame_debug.h | 37 ++++++++++++++++++++++++++++++++++
 3 files changed, 50 insertions(+), 4 deletions(-)

diff --git a/include/linux/eh_frame.h b/include/linux/eh_frame.h
index 740316b19df3..70b4f9d12828 100644
--- a/include/linux/eh_frame.h
+++ b/include/linux/eh_frame.h
@@ -11,6 +11,10 @@
 struct eh_frame_section {
        struct rcu_head rcu;
 
+#ifdef CONFIG_DYNAMIC_DEBUG
+       const char      *filename;
+#endif
+
        unsigned long   eh_frame_hdr_start;
        unsigned long   eh_frame_hdr_end;
        unsigned long   text_start;
diff --git a/kernel/unwind/eh_frame.c b/kernel/unwind/eh_frame.c
index 327849ea4a69..96237a96d263 100644
--- a/kernel/unwind/eh_frame.c
+++ b/kernel/unwind/eh_frame.c
@@ -1207,15 +1207,18 @@ int eh_frame_find(unsigned long ip, struct 
unwind_user_frame *frame)
         * e.g. EINVAL (corrupted) or EFAULT (inaccessible).
         * Keep if ENOENT (not found) or EOPNOTSUPP (unsupported CFI).
         */
-       if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP))
+       if (ret && (ret != -ENOENT && ret != -EOPNOTSUPP)) {
+               dbg_sec("removing bad .eh_frame[_hdr] section\n");
                if (eh_frame_remove_section(sec->eh_frame_hdr_start))
                        dbg("eh_frame_remove_section() failed\n");
+       }
 
        return ret;
 }
 
 static void free_section(struct eh_frame_section *sec)
 {
+       dbg_free(sec);
        kfree(sec);
 }
 
@@ -1342,6 +1345,8 @@ int eh_frame_add_section(unsigned long eh_frame_hdr_start,
        sec->text_start         = text_start;
        sec->text_end           = text_end;
 
+       dbg_init(sec);
+
        ret = eh_frame_read_header(sec);
        if (ret)
                goto err_free;
@@ -1349,8 +1354,8 @@ int eh_frame_add_section(unsigned long eh_frame_hdr_start,
        ret = mtree_insert_range(eh_frame_mt, sec->text_start, sec->text_end - 
1,
                                 sec, GFP_KERNEL_ACCOUNT);
        if (ret) {
-               dbg("mtree_insert_range failed: text=%lx-%lx\n",
-                   sec->text_start, sec->text_end);
+               dbg_sec("mtree_insert_range failed: text=%lx-%lx\n",
+                       sec->text_start, sec->text_end);
                goto err_free;
        }
 
@@ -1372,7 +1377,7 @@ static int __eh_frame_remove_section(struct ma_state *mas,
                                     struct eh_frame_section *sec)
 {
        if (mas_erase(mas) != sec) {
-               dbg("mas_erase failed: text=%lx\n", sec->text_start);
+               dbg_sec("mas_erase failed: text=%lx\n", sec->text_start);
                return -EINVAL;
        }
 
diff --git a/kernel/unwind/eh_frame_debug.h b/kernel/unwind/eh_frame_debug.h
index 5a3e4e7f065a..40a80861d4d4 100644
--- a/kernel/unwind/eh_frame_debug.h
+++ b/kernel/unwind/eh_frame_debug.h
@@ -3,6 +3,7 @@
 #define _EH_FRAME_DEBUG_H
 
 #include <linux/eh_frame.h>
+#include <linux/mm.h>
 #include "eh_frame.h"
 
 #ifdef CONFIG_DYNAMIC_DEBUG
@@ -10,9 +11,45 @@
 #define dbg(fmt, ...)                                                  \
        pr_debug("%s (%d): " fmt, current->comm, current->pid, ##__VA_ARGS__)
 
+#define dbg_sec(fmt, ...)                                              \
+       dbg("%s: " fmt, sec->filename, ##__VA_ARGS__)
+
+static inline void dbg_init(struct eh_frame_section *sec)
+{
+       struct mm_struct *mm = current->mm;
+       struct vm_area_struct *vma;
+       const char *name;
+
+       guard(mmap_read_lock)(mm);
+       vma = vma_lookup(mm, sec->eh_frame_hdr_start);
+       if (!vma)
+               sec->filename = kstrdup("(vma gone???)", GFP_KERNEL_ACCOUNT);
+       else if (vma_is_anonymous(vma))
+                sec->filename = kstrdup("(anonymous)", GFP_KERNEL_ACCOUNT);
+       else if (vma->vm_file)
+               sec->filename = kstrdup_quotable_file(vma->vm_file, 
GFP_KERNEL_ACCOUNT);
+       else if (vma->vm_ops && vma->vm_ops->name && (name = 
vma->vm_ops->name(vma)))
+               sec->filename = kstrdup(name, GFP_KERNEL_ACCOUNT);
+       else if (arch_vma_name(vma))
+               sec->filename = kstrdup(arch_vma_name(vma), GFP_KERNEL_ACCOUNT);
+       else if (!vma->vm_mm)
+               sec->filename = kstrdup("(vdso)", GFP_KERNEL_ACCOUNT);
+       else
+               sec->filename = kstrdup("(vma unknown???)", GFP_KERNEL_ACCOUNT);
+}
+
+static inline void dbg_free(struct eh_frame_section *sec)
+{
+       kfree(sec->filename);
+}
+
 #else /* !CONFIG_DYNAMIC_DEBUG */
 
 #define dbg(args...)                   no_printk(args)
+#define dbg_sec(args...)               no_printk(args)
+
+static inline void dbg_init(struct eh_frame_section *sec) {}
+static inline void dbg_free(struct eh_frame_section *sec) {}
 
 #endif /* !CONFIG_DYNAMIC_DEBUG */
 
-- 
2.53.0


Reply via email to