linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kevin Cernekee <cernekee@chromium.org>
To: rusty@rustcorp.com.au
Cc: akpm@linux-foundation.org, ard.biesheuvel@linaro.org,
	keescook@chromium.org, computersforpeace@gmail.com,
	dianders@chromium.org, linux-kernel@vger.kernel.org
Subject: [PATCH] printk: Make %pS print offsets into modules
Date: Sun, 23 Oct 2016 14:21:53 -0700	[thread overview]
Message-ID: <1477257713-22206-1-git-send-email-cernekee@chromium.org> (raw)

If kallsyms cannot find a symbol for an address, entries like this will
appear in backtraces:

    Call trace:
    [<ffffffbffc1ecd7c>] 0xffffffbffc1ecd7c
    [<ffffffbffc1ef7f0>] 0xffffffbffc1ef7f0
    [<ffffffbffc1f0094>] 0xffffffbffc1f0094

This isn't particularly useful for debugging because modules are not
loaded at fixed addresses.  Instead, print the offset from the module's
base, so that the offending location can be easily located in a
disassembly of the .ko file:

    Call trace:
    [<ffffffbffc1d57f4>] [mwifiex_pcie+0x37f4/0x9000]
    [<ffffffbffc1d60ac>] [mwifiex_pcie+0x40ac/0x9000]
    [<ffffffbffc188fe0>] mwifiex_main_process+0xdc/0x6fc [mwifiex]

Signed-off-by: Kevin Cernekee <cernekee@chromium.org>
---
 include/linux/module.h | 16 ++++++++++++++++
 kernel/kallsyms.c      | 19 ++++++++++++++++++-
 kernel/module.c        | 23 +++++++++++++++++++++++
 3 files changed, 57 insertions(+), 1 deletion(-)


Tested on 4.4 only (so the core_layout / init_layout stuff is untested).


diff --git a/include/linux/module.h b/include/linux/module.h
index 0c3207d26ac0..611d1e71b7c8 100644
--- a/include/linux/module.h
+++ b/include/linux/module.h
@@ -616,6 +616,14 @@ const char *module_address_lookup(unsigned long addr,
 			    unsigned long *offset,
 			    char **modname,
 			    char *namebuf);
+
+/* For kallsyms to ask which module, if any, contains addr.  On success,
+ * returns true and populates module_size, module_offset, and modname. */
+bool module_base_lookup(unsigned long addr,
+			unsigned long *module_size,
+			unsigned long *module_offset,
+			char **modname);
+
 int lookup_module_symbol_name(unsigned long addr, char *symname);
 int lookup_module_symbol_attrs(unsigned long addr, unsigned long *size, unsigned long *offset, char *modname, char *name);
 
@@ -698,6 +706,14 @@ static inline const char *module_address_lookup(unsigned long addr,
 	return NULL;
 }
 
+static inline bool module_base_lookup(unsigned long addr,
+				      unsigned long *module_size,
+				      unsigned long *module_offset,
+				      char **modname)
+{
+	return false;
+}
+
 static inline int lookup_module_symbol_name(unsigned long addr, char *symname)
 {
 	return -ERANGE;
diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c
index fafd1a3ef0da..43b114e6861f 100644
--- a/kernel/kallsyms.c
+++ b/kernel/kallsyms.c
@@ -387,8 +387,25 @@ static int __sprint_symbol(char *buffer, unsigned long address,
 
 	address += symbol_offset;
 	name = kallsyms_lookup(address, &size, &offset, &modname, buffer);
-	if (!name)
+	if (!name) {
+		/*
+		 * Fall back to [module_base+offset/size] if the actual
+		 * symbol name is unavailable.
+		 */
+		if (module_base_lookup(address, &size, &offset, &modname)) {
+			if (add_offset) {
+				return snprintf(buffer, KSYM_SYMBOL_LEN,
+						"[%s+%#lx/%#lx]", modname,
+						address - offset -
+							symbol_offset,
+						size);
+			} else {
+				return snprintf(buffer, KSYM_SYMBOL_LEN,
+						"[%s]", modname);
+			}
+		}
 		return sprintf(buffer, "0x%lx", address - symbol_offset);
+	}
 
 	if (name != buffer)
 		strcpy(buffer, name);
diff --git a/kernel/module.c b/kernel/module.c
index f57dd63186e6..5e09f568c601 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -3899,6 +3899,29 @@ const char *module_address_lookup(unsigned long addr,
 	return ret;
 }
 
+bool module_base_lookup(unsigned long addr,
+			unsigned long *module_size,
+			unsigned long *module_offset,
+			char **modname)
+{
+	struct module *mod;
+
+	preempt_disable();
+
+	mod = __module_address(addr);
+	if (!mod) {
+		preempt_enable();
+		return false;
+	}
+
+	*modname = mod->name;
+	*module_offset = (unsigned long)mod->core_layout.base;
+	*module_size = mod->init_layout.size + mod->core_layout.size;
+
+	preempt_enable();
+	return true;
+}
+
 int lookup_module_symbol_name(unsigned long addr, char *symname)
 {
 	struct module *mod;
-- 
2.8.0.rc3.226.g39d4020

             reply	other threads:[~2016-10-23 21:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-10-23 21:21 Kevin Cernekee [this message]
2016-10-24 14:16 ` [PATCH] printk: Make %pS print offsets into modules Ard Biesheuvel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1477257713-22206-1-git-send-email-cernekee@chromium.org \
    --to=cernekee@chromium.org \
    --cc=akpm@linux-foundation.org \
    --cc=ard.biesheuvel@linaro.org \
    --cc=computersforpeace@gmail.com \
    --cc=dianders@chromium.org \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rusty@rustcorp.com.au \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).