Skip to contentSkip to conversation

perf symbols: Allow symbol alias when loading map for symbol name

PATCH In review Namhyung Kim 2 participants 7 messages Mar 4, 2015 – Mar 6, 2015 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/2 perf symbols: Allow symbol alias when loading map for symbol name

When perf probe tries to add a probe in a binary using symbol name, it sometimes failed since some symbols were discard during loading dso. When it resolves an address to symbol, it'd be better to have just one symbol at given address. But for finding address from symbol, it'd be better to keep all names (including aliases).

Add and propagate a new allow_alias argument to dso (and map) load functions so that it can keep those duplicate symbol aliases.

Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/machine.c        | 2 +-
 tools/perf/util/map.c            | 6 +++---
 tools/perf/util/map.h            | 8 +++++++-
 tools/perf/util/symbol-elf.c     | 5 +++--
 tools/perf/util/symbol-minimal.c | 2 +-
 tools/perf/util/symbol.c         | 8 +++++---
 tools/perf/util/symbol.h         | 5 +++--
 7 files changed, 23 insertions(+), 13 deletions(-)
tools/perf/util/machine.c +1 -1
index 24f8c978cfd4..01ba9b635e4f 100644
@@ -1128,7 +1128,7 @@ static int machine__process_kernel_mmap_event(struct machine *machine,
 			 * preload dso of guest kernel and modules
 			 */
 			dso__load(kernel, machine->vmlinux_maps[MAP__FUNCTION],
-				  NULL);
+				  NULL, false);
 		}
 	}
 	return 0;
tools/perf/util/map.c +3 -3
index 62ca9f2607d5..711e0728520b 100644
@@ -248,7 +248,7 @@ void map__fixup_end(struct map *map)
 
 #define DSO__DELETED "(deleted)"
 
-int map__load(struct map *map, symbol_filter_t filter)
+int __map__load(struct map *map, symbol_filter_t filter, bool allow_alias)
 {
 	const char *name = map->dso->long_name;
 	int nr;
@@ -256,7 +256,7 @@ int map__load(struct map *map, symbol_filter_t filter)
 	if (dso__loaded(map->dso, map->type))
 		return 0;
 
-	nr = dso__load(map->dso, map, filter);
+	nr = dso__load(map->dso, map, filter, allow_alias);
 	if (nr < 0) {
 		if (map->dso->has_build_id) {
 			char sbuild_id[BUILD_ID_SIZE * 2 + 1];
@@ -304,7 +304,7 @@ struct symbol *map__find_symbol(struct map *map, u64 addr,
 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
 					symbol_filter_t filter)
 {
-	if (map__load(map, filter) < 0)
+	if (__map__load(map, filter, true) < 0)
 		return NULL;
 
 	if (!dso__sorted_by_name(map->dso, map->type))
tools/perf/util/map.h +7 -1
index 0e42438b1e59..ba156076c25b 100644
@@ -149,7 +149,13 @@ size_t map__fprintf_dsoname(struct map *map, FILE *fp);
 int map__fprintf_srcline(struct map *map, u64 addr, const char *prefix,
 			 FILE *fp);
 
-int map__load(struct map *map, symbol_filter_t filter);
+int __map__load(struct map *map, symbol_filter_t filter, bool allow_alias);
+
+static inline int map__load(struct map *map, symbol_filter_t filter)
+{
+	return __map__load(map, filter, false);
+}
+
 struct symbol *map__find_symbol(struct map *map,
 				u64 addr, symbol_filter_t filter);
 struct symbol *map__find_symbol_by_name(struct map *map, const char *name,
tools/perf/util/symbol-elf.c +3 -2
index ada16762fac2..fb630f8fc45c 100644
@@ -754,7 +754,7 @@ static bool want_demangle(bool is_kernel_sym)
 
 int dso__load_sym(struct dso *dso, struct map *map,
 		  struct symsrc *syms_ss, struct symsrc *runtime_ss,
-		  symbol_filter_t filter, int kmodule)
+		  symbol_filter_t filter, int kmodule, bool allow_alias)
 {
 	struct kmap *kmap = dso->kernel ? map__kmap(map) : NULL;
 	struct map *curr_map = map;
@@ -1048,7 +1048,8 @@ new_symbol:
 	 * For misannotated, zeroed, ASM function sizes.
 	 */
 	if (nr > 0) {
-		symbols__fixup_duplicate(&dso->symbols[map->type]);
+		if (!allow_alias)
+			symbols__fixup_duplicate(&dso->symbols[map->type]);
 		symbols__fixup_end(&dso->symbols[map->type]);
 		if (kmap) {
 			/*
tools/perf/util/symbol-minimal.c +1 -1
index d7efb03b3f9a..fefeeb32697e 100644
@@ -334,7 +334,7 @@ int dso__load_sym(struct dso *dso, struct map *map __maybe_unused,
 		  struct symsrc *ss,
 		  struct symsrc *runtime_ss __maybe_unused,
 		  symbol_filter_t filter __maybe_unused,
-		  int kmodule __maybe_unused)
+		  int kmodule __maybe_unused, bool allow_alias __maybe_unused)
 {
 	unsigned char *build_id[BUILD_ID_SIZE];
 	int ret;
tools/perf/util/symbol.c +5 -3
index a69066865a55..de72a163758d 100644
@@ -1345,7 +1345,8 @@ static bool dso__is_compatible_symtab_type(struct dso *dso, bool kmod,
 	}
 }
 
-int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
+int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter,
+	      bool allow_alias)
 {
 	char *name;
 	int ret = -1;
@@ -1458,7 +1459,8 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
 		runtime_ss = syms_ss;
 
 	if (syms_ss)
-		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod);
+		ret = dso__load_sym(dso, map, syms_ss, runtime_ss, filter, kmod,
+				    allow_alias);
 	else
 		ret = -1;
 
@@ -1516,7 +1518,7 @@ int dso__load_vmlinux(struct dso *dso, struct map *map,
 	if (symsrc__init(&ss, dso, symfs_vmlinux, symtab_type))
 		return -1;
 
-	err = dso__load_sym(dso, map, &ss, &ss, filter, 0);
+	err = dso__load_sym(dso, map, &ss, &ss, filter, 0, false);
 	symsrc__destroy(&ss);
 
 	if (err > 0) {
tools/perf/util/symbol.h +3 -2
index 1650dcb3a67b..82990384f546 100644
@@ -218,7 +218,8 @@ int symsrc__init(struct symsrc *ss, struct dso *dso, const char *name,
 bool symsrc__has_symtab(struct symsrc *ss);
 bool symsrc__possibly_runtime(struct symsrc *ss);
 
-int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
+int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter,
+	      bool allow_alias);
 int dso__load_vmlinux(struct dso *dso, struct map *map,
 		      const char *vmlinux, bool vmlinux_allocated,
 		      symbol_filter_t filter);
@@ -262,7 +263,7 @@ bool symbol__is_idle(struct symbol *sym);
 
 int dso__load_sym(struct dso *dso, struct map *map, struct symsrc *syms_ss,
 		  struct symsrc *runtime_ss, symbol_filter_t filter,
-		  int kmodule);
+		  int kmodule, bool allow_alias);
 int dso__synthesize_plt_symbols(struct dso *dso, struct symsrc *ss,
 				struct map *map, symbol_filter_t filter);
 
Signature · 1 line
2.3.1
author maintainer
#1 lore raw

PATCH 2/2 perf probe: Allow weak symbols to be probed

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/probe-event.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +2 -4
index 1c570c2fa7cc..12b7d018106e 100644
@@ -2339,8 +2339,7 @@ static int find_probe_functions(struct map *map, char *name)
 	struct symbol *sym;
 
 	map__for_each_symbol_by_name(map, name, sym) {
-		if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
-			found++;
+		found++;
 	}
 
 	return found;
@@ -2708,8 +2707,7 @@ static struct strfilter *available_func_filter;
 static int filter_available_functions(struct map *map __maybe_unused,
 				      struct symbol *sym)
 {
-	if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
-	    strfilter__compare(available_func_filter, sym->name))
+	if (strfilter__compare(available_func_filter, sym->name))
 		return 0;
 	return 1;
 }
Signature · 1 line
2.3.1
#2 lore raw

Re: [RFC/PATCH 2/2] perf probe: Allow weak symbols to be probed

Quoted 7 lines · Namhyung Kim

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
Quoted 8 lines
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Hmm, even on my previous series, I got an error with calloc and waitpid.

$ ./perf probe -x /usr/lib64/libc-2.17.so -vvV calloc
probe-definition(0): calloc
symbol:calloc file:(null) line:0 offset:0 return:0 lazy:(null)
0 arguments
Open Debuginfo file: /usr/lib/debug/usr/lib64/libc-2.17.so.debug
Searching variables at calloc
Failed to find the address of calloc
  Error: Failed to show vars. Reason: No such file or directory (Code: -2)

However, it seems that calloc is loaded as a symbol.

$ ./perf probe -x /usr/lib64/libc-2.17.so -vvvvV calloc
...
symbol__new: __xstat64 0xe7340-0xe7385
symbol__new: calloc 0x80a90-0x80d2a
symbol__new: msgget 0xf7940-0xf7961
...

FYI, without these patches, I see the same result (calloc is loaded)

Thank you,

Quoted 32 lines
Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/probe-event.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +2 -4
index 1c570c2fa7cc..12b7d018106e 100644
@@ -2339,8 +2339,7 @@ static int find_probe_functions(struct map *map, char *name)
 	struct symbol *sym;
 
 	map__for_each_symbol_by_name(map, name, sym) {
-		if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
-			found++;
+		found++;
 	}
 
 	return found;
@@ -2708,8 +2707,7 @@ static struct strfilter *available_func_filter;
 static int filter_available_functions(struct map *map __maybe_unused,
 				      struct symbol *sym)
 {
-	if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
-	    strfilter__compare(available_func_filter, sym->name))
+	if (strfilter__compare(available_func_filter, sym->name))
 		return 0;
 	return 1;
 }
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author maintainer
#3 lore raw

Re: [RFC/PATCH 2/2] perf probe: Allow weak symbols to be probed

Hi Masami,

Quoted 30 lines · Masami Hiramatsu
Quoted 7 lines · Namhyung Kim

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
Quoted 8 lines
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Hmm, even on my previous series, I got an error with calloc and waitpid.

$ ./perf probe -x /usr/lib64/libc-2.17.so -vvV calloc
probe-definition(0): calloc
symbol:calloc file:(null) line:0 offset:0 return:0 lazy:(null)
0 arguments
Open Debuginfo file: /usr/lib/debug/usr/lib64/libc-2.17.so.debug
Searching variables at calloc
Failed to find the address of calloc
  Error: Failed to show vars. Reason: No such file or directory (Code: -2)

However, it seems that calloc is loaded as a symbol.

Quoted 8 lines
$ ./perf probe -x /usr/lib64/libc-2.17.so -vvvvV calloc
...
symbol__new: __xstat64 0xe7340-0xe7385
symbol__new: calloc 0x80a90-0x80d2a
symbol__new: msgget 0xf7940-0xf7961
...

FYI, without these patches, I see the same result (calloc is loaded)

I'm bit confused with the English ;-). So you mean that now you *can* probe calloc and waitpid with this patch, right?

Thanks,
Namhyung

#4 lore raw

Re: [RFC/PATCH 2/2] perf probe: Allow weak symbols to be probed

Quoted 40 lines · Namhyung Kim

Hi Masami,

Quoted 37 lines · Masami Hiramatsu
Quoted 7 lines · Namhyung Kim

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
Quoted 8 lines
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Hmm, even on my previous series, I got an error with calloc and waitpid.

$ ./perf probe -x /usr/lib64/libc-2.17.so -vvV calloc
probe-definition(0): calloc
symbol:calloc file:(null) line:0 offset:0 return:0 lazy:(null)
0 arguments
Open Debuginfo file: /usr/lib/debug/usr/lib64/libc-2.17.so.debug
Searching variables at calloc
Failed to find the address of calloc
  Error: Failed to show vars. Reason: No such file or directory (Code: -2)

However, it seems that calloc is loaded as a symbol.

$ ./perf probe -x /usr/lib64/libc-2.17.so -vvvvV calloc
...
symbol__new: __xstat64 0xe7340-0xe7385
symbol__new: calloc 0x80a90-0x80d2a
symbol__new: msgget 0xf7940-0xf7961
...
Quoted 4 lines
Quoted 1 line

FYI, without these patches, I see the same result (calloc is loaded)

I'm bit confused with the English ;-). So you mean that now you *can* probe calloc and waitpid with this patch, right?

Ah, sorry for confusing you. I meant that I couldn't probe it even with your patch. I'm not sure why, since the calloc symbol is created as above message, but can't find in the map...

Thank you,

Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
#5 lore raw

Re: [RFC/PATCH 2/2] perf probe: Allow weak symbols to be probed

In reply to Namhyung Kim
Quoted 27 lines · Namhyung Kim

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/probe-event.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +0 -0
index 1c570c2fa7cc..12b7d018106e 100644

tools/perf/util/probe-event.c static int find_probe_functions(struct map *map, char *name) · line 2343

Quoted 7 lines
tools/perf/util/probe-event.c +1 -2
@@ -2339,8 +2339,7 @@ static int find_probe_functions(struct map *map, char *name)
 	struct symbol *sym;
 
 	map__for_each_symbol_by_name(map, name, sym) {
-		if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
-			found++;
+		found++;
 	}

Ah, I've found this is the magic...
Here, we need another fix on my series.

tools/perf/util/probe-event.c +2 -4
index 22392b06..f9c1e53 100644
@@ -309,10 +309,8 @@ static int find_alternative_probe_point(struct debuginfo *d
 
        /* Find the address of given function */
        map__for_each_symbol_by_name(map, pp->function, sym) {
-               if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) {
-                       address = sym->start;
-                       break;
-               }
+               address = sym->start;
+               break;
        }
        if (!address) {
                ret = -ENOENT;

-------

With this fix, I could get variables on waitpid and calloc.
-----

# ./perf probe -x /lib64/libc-2.17.so -V waitpid
Available variables at waitpid
        @<__libc_waitpid+0>
                __pid_t pid
                int     oldtype
                int     options
                int*    stat_loc
-----

I'll update and include it my series.

Thank you!

Quoted 13 lines
tools/perf/util/probe-event.c +1 -2
 	return found;
@@ -2708,8 +2707,7 @@ static struct strfilter *available_func_filter;
 static int filter_available_functions(struct map *map __maybe_unused,
 				      struct symbol *sym)
 {
-	if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
-	    strfilter__compare(available_func_filter, sym->name))
+	if (strfilter__compare(available_func_filter, sym->name))
 		return 0;
 	return 1;
 }
Signature · 4 lines
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center
Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]
author maintainer
#6 lore raw

Re: [RFC/PATCH 2/2] perf probe: Allow weak symbols to be probed

Quoted 30 lines · Masami Hiramatsu
Quoted 29 lines · Namhyung Kim

It currently prevents adding probes in weak symbols. But there're cases that given name is an only weak symbol so that we cannot add probe.

  $ perf probe -x /usr/lib/libc.so.6 -a calloc
  Failed to find symbol calloc in /usr/lib/libc-2.21.so
    Error: Failed to add events.
  $ nm /usr/lib/libc.so.6 | grep calloc
  000000000007b1f0 t __calloc
  000000000007b1f0 T __libc_calloc
  000000000007b1f0 W calloc

This change will result in duplicate probes when strong and weak symbols co-exist in a binary. But I think it's not a big problem since probes at the weak symbol will never be hit anyway.

Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/probe-event.c | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)
tools/perf/util/probe-event.c +0 -0
index 1c570c2fa7cc..12b7d018106e 100644
@@ -2339,8 +2339,7 @@ static int find_probe_functions(struct map *map, char *name)
      struct symbol *sym;
 
Quoted 8 lines

tools/perf/util/probe-event.c static int find_probe_functions(struct map *map, char *name) · line 2343

Quoted 5 lines
tools/perf/util/probe-event.c +1 -2
      map__for_each_symbol_by_name(map, name, sym) {
-             if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL)
-                     found++;
+             found++;
      }

Ah, I've found this is the magic...
Here, we need another fix on my series.

Oops, right. I didn't base on your patch so missed this function.

Thanks,
Namhyung

Quoted 57 lines
tools/perf/util/probe-event.c +2 -5
index 22392b06..f9c1e53 100644
@@ -309,10 +309,8 @@ static int find_alternative_probe_point(struct debuginfo *d
 
        /* Find the address of given function */
        map__for_each_symbol_by_name(map, pp->function, sym) {
-               if (sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) {
-                       address = sym->start;
-                       break;
-               }
+               address = sym->start;
+               break;
        }
        if (!address) {
                ret = -ENOENT;
-------

With this fix, I could get variables on waitpid and calloc.
-----

# ./perf probe -x /lib64/libc-2.17.so -V waitpid
Available variables at waitpid
        @<__libc_waitpid+0>
                __pid_t pid
                int     oldtype
                int     options
                int*    stat_loc
-----

I'll update and include it my series.

Thank you!

tools/perf/util/probe-event.c static struct strfilter *available_func_filter; · line 2713

Quoted 13 lines
tools/perf/util/probe-event.c +1 -2
      return found;
@@ -2708,8 +2707,7 @@ static struct strfilter *available_func_filter;
 static int filter_available_functions(struct map *map __maybe_unused,
                                    struct symbol *sym)
 {
-     if ((sym->binding == STB_GLOBAL || sym->binding == STB_LOCAL) &&
-         strfilter__compare(available_func_filter, sym->name))
+     if (strfilter__compare(available_func_filter, sym->name))
              return 0;
      return 1;
 }

--
Masami HIRAMATSU
Software Platform Research Dept. Linux Technology Research Center Hitachi, Ltd., Yokohama Research Laboratory
E-mail: [email protected]

Signature · 2 lines
Thanks,
Namhyung