public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Arnaldo Carvalho de Melo <acme@kernel.org>
To: Ingo Molnar <mingo@kernel.org>
Cc: linux-kernel@vger.kernel.org,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Jiri Olsa <jolsa@redhat.com>, Namhyung Kim <namhyung@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Arnaldo Carvalho de Melo <acme@redhat.com>
Subject: [PATCH 04/12] perf probe: Fix to get correct modname from elf header
Date: Wed,  4 Jan 2017 14:25:01 -0300	[thread overview]
Message-ID: <20170104172509.27350-5-acme@kernel.org> (raw)
In-Reply-To: <20170104172509.27350-1-acme@kernel.org>

From: Masami Hiramatsu <mhiramat@kernel.org>

Since 'perf probe' supports cross-arch probes, it is possible to analyze
different arch kernel image which has different bits-per-long.

In that case, it fails to get the module name because it uses the
MOD_NAME_OFFSET macro based on the host machine bits-per-long, instead
of the target arch bits-per-long.

This fixes above issue by changing modname-offset based on the target
archs bit width. This is ok because linux kernel uses LP64 model on
64bit arch.

E.g. without this (on x86_64, and target module is arm32):

  $ perf probe -m build-arm/fs/configfs/configfs.ko -D configfs_lookup
  p:probe/configfs_lookup :configfs_lookup+0
                          ^-Here is an empty module name.

With this fix, you can see correct module name:

  $ perf probe -m build-arm/fs/configfs/configfs.ko -D configfs_lookup
  p:probe/configfs_lookup configfs:configfs_lookup+0

Signed-off-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Peter Zijlstra <peterz@infradead.org>
Link: http://lkml.kernel.org/r/148337043836.6752.383495516397005695.stgit@devbox
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/probe-event.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)

diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index d281ae2b54e8..8f810961ec78 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -268,21 +268,6 @@ static bool kprobe_warn_out_range(const char *symbol, unsigned long address)
 }
 
 /*
- * NOTE:
- * '.gnu.linkonce.this_module' section of kernel module elf directly
- * maps to 'struct module' from linux/module.h. This section contains
- * actual module name which will be used by kernel after loading it.
- * But, we cannot use 'struct module' here since linux/module.h is not
- * exposed to user-space. Offset of 'name' has remained same from long
- * time, so hardcoding it here.
- */
-#ifdef __LP64__
-#define MOD_NAME_OFFSET 24
-#else
-#define MOD_NAME_OFFSET 12
-#endif
-
-/*
  * @module can be module name of module file path. In case of path,
  * inspect elf and find out what is actual module name.
  * Caller has to free mod_name after using it.
@@ -296,6 +281,7 @@ static char *find_module_name(const char *module)
 	Elf_Data *data;
 	Elf_Scn *sec;
 	char *mod_name = NULL;
+	int name_offset;
 
 	fd = open(module, O_RDONLY);
 	if (fd < 0)
@@ -317,7 +303,21 @@ static char *find_module_name(const char *module)
 	if (!data || !data->d_buf)
 		goto ret_err;
 
-	mod_name = strdup((char *)data->d_buf + MOD_NAME_OFFSET);
+	/*
+	 * NOTE:
+	 * '.gnu.linkonce.this_module' section of kernel module elf directly
+	 * maps to 'struct module' from linux/module.h. This section contains
+	 * actual module name which will be used by kernel after loading it.
+	 * But, we cannot use 'struct module' here since linux/module.h is not
+	 * exposed to user-space. Offset of 'name' has remained same from long
+	 * time, so hardcoding it here.
+	 */
+	if (ehdr.e_ident[EI_CLASS] == ELFCLASS32)
+		name_offset = 12;
+	else	/* expect ELFCLASS64 by default */
+		name_offset = 24;
+
+	mod_name = strdup((char *)data->d_buf + name_offset);
 
 ret_err:
 	elf_end(elf);
-- 
2.9.3

  parent reply	other threads:[~2017-01-04 18:10 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-01-04 17:24 [GIT PULL 00/12] perf/urgent fixes Arnaldo Carvalho de Melo
2017-01-04 17:24 ` [PATCH 01/12] perf sched timehist: Show total scheduling time Arnaldo Carvalho de Melo
2017-01-04 17:24 ` [PATCH 02/12] samples/bpf sock_example: Avoid getting ethhdr from two includes Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 03/12] samples/bpf trace_output_user: Remove duplicate sys/ioctl.h include Arnaldo Carvalho de Melo
2017-01-04 17:25 ` Arnaldo Carvalho de Melo [this message]
2017-01-04 17:25 ` [PATCH 05/12] tools lib subcmd: Add OPT_STRING_OPTARG_SET option Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 06/12] perf record: Make __record_options static Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 07/12] perf record: Fix --switch-output documentation and comment Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 08/12] tools lib traceevent: Fix prev/next_prio for deadline tasks Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 09/12] perf tools: Install tools/lib/traceevent plugins with install-bin Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 10/12] perf symbols: Robustify reading of build-id from sysfs Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 11/12] perf probe: Fix --funcs to show correct symbols for offline module Arnaldo Carvalho de Melo
2017-01-04 17:25 ` [PATCH 12/12] perf probe: Fix to probe on gcc generated symbols for offline kernel Arnaldo Carvalho de Melo
2017-01-05  7:36 ` [GIT PULL 00/12] perf/urgent fixes Ingo Molnar
2017-01-05 15:02   ` Arnaldo Carvalho de Melo

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=20170104172509.27350-5-acme@kernel.org \
    --to=acme@kernel.org \
    --cc=acme@redhat.com \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    /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