All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH perf/core] perf-probe: Fix to get correct modname from elf header
@ 2017-01-02 15:20 Masami Hiramatsu
  2017-01-02 17:09 ` Arnaldo Carvalho de Melo
  2017-01-05  7:50 ` [tip:perf/urgent] perf probe: " tip-bot for Masami Hiramatsu
  0 siblings, 2 replies; 3+ messages in thread
From: Masami Hiramatsu @ 2017-01-02 15:20 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Masami Hiramatsu, linux-kernel, Jiri Olsa, Peter Zijlstra,
	Ingo Molnar, Namhyung Kim

Since perf-probe supports cross-arch probe, it is possible to
analyze different arch kernel image which has different
bits-per-long. In that case, it fails to get module name
because perf probe uses MOD_NAME_OFFSET macro based on
host machines bits-per-long, instead of the target archs
bits-per-long.

This fixes above issue by changing modname-offset based on
the target archs bitwidth. 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>
---
 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 d281ae2..8f81096 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);

^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH perf/core] perf-probe: Fix to get correct modname from elf header
  2017-01-02 15:20 [PATCH perf/core] perf-probe: Fix to get correct modname from elf header Masami Hiramatsu
@ 2017-01-02 17:09 ` Arnaldo Carvalho de Melo
  2017-01-05  7:50 ` [tip:perf/urgent] perf probe: " tip-bot for Masami Hiramatsu
  1 sibling, 0 replies; 3+ messages in thread
From: Arnaldo Carvalho de Melo @ 2017-01-02 17:09 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: linux-kernel, Jiri Olsa, Peter Zijlstra, Ingo Molnar,
	Namhyung Kim

Em Tue, Jan 03, 2017 at 12:20:49AM +0900, Masami Hiramatsu escreveu:
> Since perf-probe supports cross-arch probe, it is possible to
> analyze different arch kernel image which has different
> bits-per-long. In that case, it fails to get module name
> because perf probe uses MOD_NAME_OFFSET macro based on
> host machines bits-per-long, instead of the target archs
> bits-per-long.
> 
> This fixes above issue by changing modname-offset based on
> the target archs bitwidth. This is ok because linux kernel
> uses LP64 model on 64bit arch.

Seems simple enough, thanks, applied.

- Arnaldo

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [tip:perf/urgent] perf probe: Fix to get correct modname from elf header
  2017-01-02 15:20 [PATCH perf/core] perf-probe: Fix to get correct modname from elf header Masami Hiramatsu
  2017-01-02 17:09 ` Arnaldo Carvalho de Melo
@ 2017-01-05  7:50 ` tip-bot for Masami Hiramatsu
  1 sibling, 0 replies; 3+ messages in thread
From: tip-bot for Masami Hiramatsu @ 2017-01-05  7:50 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, mingo, mhiramat, peterz, jolsa, acme, tglx,
	namhyung, hpa

Commit-ID:  1f2ed153b916c95a49a1ca9d7107738664224b7f
Gitweb:     http://git.kernel.org/tip/1f2ed153b916c95a49a1ca9d7107738664224b7f
Author:     Masami Hiramatsu <mhiramat@kernel.org>
AuthorDate: Tue, 3 Jan 2017 00:20:49 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Mon, 2 Jan 2017 14:09:17 -0300

perf probe: Fix to get correct modname from elf header

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 d281ae2..8f81096 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);

^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2017-01-05  7:53 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-01-02 15:20 [PATCH perf/core] perf-probe: Fix to get correct modname from elf header Masami Hiramatsu
2017-01-02 17:09 ` Arnaldo Carvalho de Melo
2017-01-05  7:50 ` [tip:perf/urgent] perf probe: " tip-bot for Masami Hiramatsu

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.