Skip to contentSkip to conversation

perf tools: Minimal build without libelf dependency (v2)

PATCH Applied Namhyung Kim 6 participants 23 messages Jun 22, 2012 – Jul 6, 2012 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Minimal build without libelf dependency (v2)

Hi,

Current build procedure of perf tools requires a couple of external libraries. Although most of them are optional, elfutils' libelf is mandatory for resolving symbols and build-id of the binaries being profiled. For some reason, it makes embedded guys' life harder who want to build and run perf tools on their boards.

This patchset tries to fix the problem by letting the perf could be built without the libelf. The initial intent was just providing perf record command to gather performance events interested on the system and copies perf.data to development box to be used by (full- fledged) perf report for analysis.

However it's changed to have most of perf commands as is because perf can resolve kernel symbols without libelf and many of perf commands deal (mostly) with kernel events - so no need to restrict capability of perf tools. Therefore, the end result is not so minimalistic actually and the only thing it cannot do is "perf probe".

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

In addition, I changed my mind to keep unrelated configurations - i.g. tui/gui and perf/python support - enabled by default so that it can be configured by a find-grained config method in the future.

To build a minimal perf tools explicitly, pass NO_LIBELF=1 to make. Or, if the system doesn't provide the elfutils it'll detect that and converts to the minimal build mode automatically. The resulting perf report will not display symbol names in userland:

# Samples: 3K of event 'cycles'
# Event count (approx.): 3740267998
#
# Overhead Command Shared Object Symbol

 # ........  .......  .................  ..............................
 #
     99.70%  noploop  noploop            [.] 0x000000000000066d        
      0.10%  noploop  [kernel.kallsyms]  [k] free_pgd_range            
      0.03%  noploop  [kernel.kallsyms]  [k] native_write_msr_safe     
      0.03%  noploop  [kernel.kallsyms]  [k] raise_softirq             
      0.03%  noploop  [kernel.kallsyms]  [k] rb_insert_color       

But perf buildid-list can show the build-id's:

 $ ./perf buildid-list --with-hits
 5eaf1839576cc801053e63300762def90a77a305 [kernel.kallsyms]
 43a7a2b399b6ee2ff29c6bdadbda6bff88712ed4 /home/namhyung/bin/noploop

So it can be packed using perf archive command and copied and analized with full-fledged perf tools on a development machine.

This patchset is based on current tip 32c46e579b68 ("Merge tag 'perf-core-for-mingo' ...") and tested on x86_64 and ARM.

Any comments are welcome.
Namhyung

v2:
* change make option name to NO_LIBELF
* make it default when libelf doesn't exist (by Ingo)
* get rid of elf-minimal.h and use elf.h (by Jiri)

Namhyung Kim (8):
perf evsel: Fix a build failure on cross compilation tools lib traceevent: Make dependency files regeneratable tools lib traceevent: Detect build environment changes perf symbols: Introduce symbol__elf_init()
perf symbols: Do not use ELF's symbol binding constants perf tools: Split out util/symbol-elf.c
perf tools: Support minimal build without libelf perf symbols: Implement poor man's ELF parser

 tools/lib/traceevent/Makefile       |   18 +-
 tools/perf/Makefile                 |   58 ++-
 tools/perf/builtin-buildid-list.c   |    4 +-
 tools/perf/builtin-inject.c         |    5 +-
 tools/perf/builtin-top.c            |    5 +-
 tools/perf/command-list.txt         |    2 +-
 tools/perf/perf.c                   |    2 +
 tools/perf/ui/browsers/map.c        |    5 +-
 tools/perf/util/evsel.c             |    1 -
 tools/perf/util/generate-cmdlist.sh |   15 +
 tools/perf/util/map.c               |    3 +-
 tools/perf/util/symbol-elf.c        |  719 ++++++++++++++++++++++++++++++++++
 tools/perf/util/symbol-minimal.c    |  261 ++++++++++++
 tools/perf/util/symbol.c            |  740 +----------------------------------
 tools/perf/util/symbol.h            |   20 +
 15 files changed, 1110 insertions(+), 748 deletions(-)
 create mode 100644 tools/perf/util/symbol-elf.c
 create mode 100644 tools/perf/util/symbol-minimal.c
Signature · 1 line
1.7.10.2
author maintainer
#1 lore raw

PATCH 1/8 perf evsel: Fix a build failure on cross compilation

From: Namhyung Kim <[email protected]>

The commit c410431cefefd ("perf tools: Reconstruct event with modifiers from perf_event_attr") added the line, but it's broken since it needs to go up 3 directories to get to the kernel root directory, not 2.

However host gcc contains /usr/local/include in its search path, so that it can find the perf_event.h in /usr/include/linux. This is why we didn't notice the problem yet. But when I tried to cross compile, it appears like:

CC util/evsel.o
util/evsel.c:18:44: error: ../../include/linux/perf_event.h: No such file or directory
make: *** [util/evsel.o] Error 1

Looking at the source, it isn't needed at all as evsel.h already included the perf_event.h. So simply removing it would solve the problem.

---
 tools/perf/util/evsel.c |    1 -
 1 file changed, 1 deletion(-)
tools/perf/util/evsel.c +0 -1
index 876f639d69ed..3d1f6968f175 100644
@@ -15,7 +15,6 @@
 #include "cpumap.h"
 #include "thread_map.h"
 #include "target.h"
-#include "../../include/linux/perf_event.h"
 
 #define FD(e, x, y) (*(int *)xyarray__entry(e->fd, x, y))
 #define GROUP_FD(group_fd, cpu) (*(int *)xyarray__entry(group_fd, cpu, 0))
Signature · 1 line
1.7.10.2
author maintainer
#2 lore raw

PATCH 2/8 tools lib traceevent: Make dependency files regeneratable

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Ingo reported that libtraceevent doesn't clean out dependency (.d) files and it can cause a build error when the libgcc package upgraded:

comet:~/tip/tools/perf> make -j
SUBDIR ../lib/traceevent/
make[1]: *** No rule to make target `/usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/stddef.h',
needed by `event-parse.o'. Stop.
make: *** [../lib/traceevent//libtraceevent.a] Error 2

So this patch makes the .d files depends on the source and header files also, so that it can be re-generated as needed.

NOTE: This code is copied from the GNU make manual page (4.14 Generating Prerequisites Automatically).

---
 tools/lib/traceevent/Makefile |    6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
tools/lib/traceevent/Makefile +5 -1
index 3d69aa9ff51e..68a1d088d77e 100644
@@ -250,8 +250,12 @@ endef
 all_objs := $(sort $(ALL_OBJS))
 all_deps := $(all_objs:%.o=.%.d)
 
+# let .d file also depends on the source and header files
 define check_deps
-		$(CC) -M $(CFLAGS) $< > $@;
+		@set -e; $(RM) $@; \
+		$(CC) -M $(CFLAGS) $< > $@.$$$$; \
+		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
+		$(RM) $@.$$$$
 endef
 
 $(gui_deps): ks_version.h
Signature · 1 line
1.7.10.2
#3 lore raw

Re: [PATCH 2/8] tools lib traceevent: Make dependency files regeneratable

Em Fri, Jun 22, 2012 at 02:37:36PM +0900, Namhyung Kim escreveu:

Quoted 5 lines

From: Namhyung Kim <[email protected]>

Ingo reported that libtraceevent doesn't clean out dependency (.d) files and it can cause a build error when the libgcc package upgraded:

Thanks, applied to perf/core.

- Arnaldo

#4 lore raw

[tip:perf/core] tools lib traceevent: Make dependency files regeneratable

In reply to Namhyung Kim
Commit-ID:  860df5833e461beba4bf9f3304a7919da98fd789
Gitweb:     http://git.kernel.org/tip/860df5833e461beba4bf9f3304a7919da98fd789
Author:     Namhyung Kim <[email protected]>
AuthorDate: Fri, 22 Jun 2012 14:37:36 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Fri, 29 Jun 2012 13:28:12 -0300

tools lib traceevent: Make dependency files regeneratable

Ingo reported that libtraceevent doesn't clean out dependency (.d) files and it can cause a build error when the libgcc package upgraded:

comet:~/tip/tools/perf> make -j
SUBDIR ../lib/traceevent/
make[1]: *** No rule to make target `/usr/lib/gcc/x86_64-redhat-linux/4.7.0/include/stddef.h',
needed by `event-parse.o'. Stop.
make: *** [../lib/traceevent//libtraceevent.a] Error 2

So this patch makes the .d files depends on the source and header files also, so that it can be re-generated as needed.

NOTE: This code is copied from the GNU make manual page (4.14 Generating Prerequisites Automatically).

---
 tools/lib/traceevent/Makefile |    6 +++++-
 1 files changed, 5 insertions(+), 1 deletions(-)
tools/lib/traceevent/Makefile +5 -1
index 34a577e..46c2f6b 100644
@@ -250,8 +250,12 @@ endef
 all_objs := $(sort $(ALL_OBJS))
 all_deps := $(all_objs:%.o=.%.d)
 
+# let .d file also depends on the source and header files
 define check_deps
-		$(CC) -M $(CFLAGS) $< > $@;
+		@set -e; $(RM) $@; \
+		$(CC) -M $(CFLAGS) $< > $@.$$$$; \
+		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
+		$(RM) $@.$$$$
 endef
 
 $(gui_deps): ks_version.h
author maintainer
#5 lore raw

PATCH 3/8 tools lib traceevent: Detect build environment changes

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Cross compiling perf requires setting ARCH and CROSS_COMPILE variables, but libtraceevent couldn't detect the changes so it ends up believing no recompiling is required. Thus the linker failed like:

LINK perf
../lib/traceevent//libtraceevent.a: member ../lib/traceevent//libtraceevent.a(event-parse.o) in archive is not an object
collect2: ld returned 1 exit status
make: *** [perf] Error 1

This patch fixes this by adding TRACEEVENT-CFLAGS file like PERF-CFLAGS to track those changes.

---
 tools/lib/traceevent/Makefile |   12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
tools/lib/traceevent/Makefile +11 -1
index 68a1d088d77e..56daa768b9e3 100644
@@ -207,7 +207,7 @@ libtraceevent.so: $(PEVENT_LIB_OBJS)
 libtraceevent.a: $(PEVENT_LIB_OBJS)
 	$(Q)$(do_build_static_lib)
 
-$(PEVENT_LIB_OBJS): %.o: $(src)/%.c
+$(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS
 	$(Q)$(do_fpic_compile)
 
 define make_version.h
@@ -272,6 +272,16 @@ ifneq ($(dep_includes),)
  include $(dep_includes)
 endif
 
+### Detect environment changes
+TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)
+
+TRACEEVENT-CFLAGS: force
+	@FLAGS='$(TRACK_CFLAGS)'; \
+	    if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \
+		echo 1>&2 "    * new build flags or cross compiler"; \
+		echo "$$FLAGS" >TRACEEVENT-CFLAGS; \
+            fi
+
 tags:	force
 	$(RM) tags
 	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px
Signature · 1 line
1.7.10.2
author maintainer
#6 lore raw

PATCH 4/8 perf symbols: Introduce symbol__elf_init()

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

The symbol__elf_init() is for initializing internal libelf data structure and getting rid of its dependency outside of ELF/symboling handling code.

---
 tools/perf/builtin-buildid-list.c |    4 +---
 tools/perf/util/symbol.c          |    8 +++++++-
 tools/perf/util/symbol.h          |    1 +
 3 files changed, 9 insertions(+), 4 deletions(-)
tools/perf/builtin-buildid-list.c +1 -3
index 6b2bcfbde150..7d6842826a0c 100644
@@ -16,8 +16,6 @@
 #include "util/session.h"
 #include "util/symbol.h"
 
-#include <libelf.h>
-
 static const char *input_name;
 static bool force;
 static bool show_kernel;
@@ -71,7 +69,7 @@ static int perf_session__list_build_ids(void)
 {
 	struct perf_session *session;
 
-	elf_version(EV_CURRENT);
+	symbol__elf_init();
 
 	session = perf_session__new(input_name, O_RDONLY, force, false,
 				    &build_id__mark_dso_hit_ops);
tools/perf/util/symbol.c +7 -1
index 3e2e5ea0f03f..38447ad42ad1 100644
@@ -1424,6 +1424,11 @@ out_close:
 	return err;
 }
 
+void symbol__elf_init(void)
+{
+	elf_version(EV_CURRENT);
+}
+
 static bool dso__build_id_equal(const struct dso *dso, u8 *build_id)
 {
 	return memcmp(dso->build_id, build_id, sizeof(dso->build_id)) == 0;
@@ -2614,7 +2619,8 @@ int symbol__init(void)
 
 	symbol_conf.priv_size = ALIGN(symbol_conf.priv_size, sizeof(u64));
 
-	elf_version(EV_CURRENT);
+	symbol__elf_init();
+
 	if (symbol_conf.sort_by_name)
 		symbol_conf.priv_size += (sizeof(struct symbol_name_rb_node) -
 					  sizeof(struct symbol));
tools/perf/util/symbol.h +1 -0
index af0752b1aca1..295a9aa1cc78 100644
@@ -296,6 +296,7 @@ void machines__destroy_guest_kernel_maps(struct rb_root *machines);
 
 int symbol__init(void);
 void symbol__exit(void);
+void symbol__elf_init(void);
 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
 				    const struct addr_location *al, FILE *fp);
 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
Signature · 1 line
1.7.10.2
author maintainer
#7 lore raw

PATCH 5/8 perf symbols: Do not use ELF's symbol binding constants

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

There's no need to use the ELF's internal value directly. Define and use our own - it's required to eliminated the dependency of libelf.

---
 tools/perf/builtin-top.c     |    5 ++---
 tools/perf/ui/browsers/map.c |    5 ++---
 tools/perf/util/symbol.c     |   40 ++++++++++++++++++++++++++--------------
 tools/perf/util/symbol.h     |    4 ++++
 4 files changed, 34 insertions(+), 20 deletions(-)
tools/perf/builtin-top.c +2 -3
index e3cab5f088f8..e0977175f689 100644
@@ -42,7 +42,6 @@
 #include "util/debug.h"
 
 #include <assert.h>
-#include <elf.h>
 #include <fcntl.h>
 
 #include <stdio.h>
@@ -181,8 +180,8 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
 		    "Please report to [email protected]\n",
 		    ip, map->dso->long_name, dso__symtab_origin(map->dso),
 		    map->start, map->end, sym->start, sym->end,
-		    sym->binding == STB_GLOBAL ? 'g' :
-		    sym->binding == STB_LOCAL  ? 'l' : 'w', sym->name,
+		    sym->binding == SYMBIND_GLOBAL ? 'g' :
+		    sym->binding == SYMBIND_LOCAL  ? 'l' : 'w', sym->name,
 		    err ? "[unknown]" : uts.machine,
 		    err ? "[unknown]" : uts.release, perf_version_string);
 	if (use_browser <= 0)
tools/perf/ui/browsers/map.c +2 -3
index 98851d55a53e..f2059664b23f 100644
@@ -1,5 +1,4 @@
 #include "../libslang.h"
-#include <elf.h>
 #include <newt.h>
 #include <inttypes.h>
 #include <sys/ttydefaults.h>
@@ -61,8 +60,8 @@ static void map_browser__write(struct ui_browser *self, void *nd, int row)
 	ui_browser__set_percent_color(self, 0, current_entry);
 	slsmg_printf("%*" PRIx64 " %*" PRIx64 " %c ",
 		     mb->addrlen, sym->start, mb->addrlen, sym->end,
-		     sym->binding == STB_GLOBAL ? 'g' :
-		     sym->binding == STB_LOCAL  ? 'l' : 'w');
+		     sym->binding == SYMBIND_GLOBAL ? 'g' :
+		     sym->binding == SYMBIND_LOCAL  ? 'l' : 'w');
 	width = self->width - ((mb->addrlen * 2) + 4);
 	if (width > 0)
 		slsmg_write_nstring(sym->name, width);
tools/perf/util/symbol.c +26 -14
index 38447ad42ad1..db807a8535d1 100644
@@ -114,16 +114,16 @@ static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
 		return SYMBOL_B;
 
 	/* Prefer a non weak symbol over a weak one */
-	a = syma->binding == STB_WEAK;
-	b = symb->binding == STB_WEAK;
+	a = syma->binding == SYMBIND_WEAK;
+	b = symb->binding == SYMBIND_WEAK;
 	if (b && !a)
 		return SYMBOL_A;
 	if (a && !b)
 		return SYMBOL_B;
 
 	/* Prefer a global symbol over a non global one */
-	a = syma->binding == STB_GLOBAL;
-	b = symb->binding == STB_GLOBAL;
+	a = syma->binding == SYMBIND_GLOBAL;
+	b = symb->binding == SYMBIND_GLOBAL;
 	if (a && !b)
 		return SYMBOL_A;
 	if (b && !a)
@@ -259,8 +259,8 @@ static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
 {
 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
 		       sym->start, sym->end,
-		       sym->binding == STB_GLOBAL ? 'g' :
-		       sym->binding == STB_LOCAL  ? 'l' : 'w',
+		       sym->binding == SYMBIND_GLOBAL ? 'g' :
+		       sym->binding == SYMBIND_LOCAL  ? 'l' : 'w',
 		       sym->name);
 }
 
@@ -609,12 +609,12 @@ struct process_kallsyms_args {
 	struct dso *dso;
 };
 
-static u8 kallsyms2elf_type(char type)
+static u8 kallsyms_binding(char type)
 {
 	if (type == 'W')
-		return STB_WEAK;
+		return SYMBIND_WEAK;
 
-	return isupper(type) ? STB_GLOBAL : STB_LOCAL;
+	return isupper(type) ? SYMBIND_GLOBAL : SYMBIND_LOCAL;
 }
 
 static int map__process_kallsym_symbol(void *arg, const char *name,
@@ -628,7 +628,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 		return 0;
 
 	sym = symbol__new(start, end - start + 1,
-			  kallsyms2elf_type(type), name);
+			  kallsyms_binding(type), name);
 	if (sym == NULL)
 		return -ENOMEM;
 	/*
@@ -851,7 +851,7 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		if (len + 2 >= line_len)
 			continue;
 
-		sym = symbol__new(start, size, STB_GLOBAL, line + len);
+		sym = symbol__new(start, size, SYMBIND_GLOBAL, line + len);
 
 		if (sym == NULL)
 			goto out_delete_line;
@@ -1064,7 +1064,7 @@ dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
 				 "%s@plt", elf_sym__name(&sym, symstrs));
 
 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-					STB_GLOBAL, sympltname);
+					SYMBIND_GLOBAL, sympltname);
 			if (!f)
 				goto out_elf_end;
 
@@ -1086,7 +1086,7 @@ dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
 				 "%s@plt", elf_sym__name(&sym, symstrs));
 
 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-					STB_GLOBAL, sympltname);
+					SYMBIND_GLOBAL, sympltname);
 			if (!f)
 				goto out_elf_end;
 
@@ -1157,6 +1157,18 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
 	return -1;
 }
 
+static int elf_sym__binding(const GElf_Sym *sym)
+{
+	switch (GELF_ST_BIND(sym->st_info)) {
+	case STB_GLOBAL:
+		return SYMBIND_GLOBAL;
+	case STB_WEAK:
+		return SYMBIND_WEAK;
+	default:
+		return SYMBIND_LOCAL;
+	}
+}
+
 static int dso__swap_init(struct dso *dso, unsigned char eidata)
 {
 	static unsigned int const endian = 1;
@@ -1390,7 +1402,7 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
 			elf_name = demangled;
 new_symbol:
 		f = symbol__new(sym.st_value, sym.st_size,
-				GELF_ST_BIND(sym.st_info), elf_name);
+				elf_sym__binding(&sym), elf_name);
 		free(demangled);
 		if (!f)
 			goto out_elf_end;
tools/perf/util/symbol.h +4 -0
index 295a9aa1cc78..bd91d2aa8262 100644
@@ -50,6 +50,10 @@ char *strxfrchar(char *s, char from, char to);
 
 #define BUILD_ID_SIZE 20
 
+#define SYMBIND_LOCAL	0
+#define SYMBIND_GLOBAL	1
+#define SYMBIND_WEAK	2
+
 /** struct symbol - symtab entry
  *
  * @ignore - resolvable but tools ignore it (e.g. idle routines)
Signature · 1 line
1.7.10.2
#8 lore raw

Re: [PATCH 5/8] perf symbols: Do not use ELF's symbol binding constants

Em Fri, Jun 22, 2012 at 02:37:39PM +0900, Namhyung Kim escreveu:

Quoted 5 lines

From: Namhyung Kim <[email protected]>

There's no need to use the ELF's internal value directly. Define and use our own - it's required to eliminated the dependency of libelf.

Why don't you set STB_GLOBAL, etc to the expected values when libelf is not present? That way no changes need to be made to symbol.c

Ditto for GELF_ST_BIND.

I.e. keep the subset of libelf.h that we use, providing those definitions on the poor man's libelf.h we should use when the "real thing" is not available.

- Arnaldo

Quoted 185 lines
---
 tools/perf/builtin-top.c     |    5 ++---
 tools/perf/ui/browsers/map.c |    5 ++---
 tools/perf/util/symbol.c     |   40 ++++++++++++++++++++++++++--------------
 tools/perf/util/symbol.h     |    4 ++++
 4 files changed, 34 insertions(+), 20 deletions(-)
tools/perf/builtin-top.c +2 -3
index e3cab5f088f8..e0977175f689 100644
@@ -42,7 +42,6 @@
 #include "util/debug.h"
 
 #include <assert.h>
-#include <elf.h>
 #include <fcntl.h>
 
 #include <stdio.h>
@@ -181,8 +180,8 @@ static void ui__warn_map_erange(struct map *map, struct symbol *sym, u64 ip)
 		    "Please report to [email protected]\n",
 		    ip, map->dso->long_name, dso__symtab_origin(map->dso),
 		    map->start, map->end, sym->start, sym->end,
-		    sym->binding == STB_GLOBAL ? 'g' :
-		    sym->binding == STB_LOCAL  ? 'l' : 'w', sym->name,
+		    sym->binding == SYMBIND_GLOBAL ? 'g' :
+		    sym->binding == SYMBIND_LOCAL  ? 'l' : 'w', sym->name,
 		    err ? "[unknown]" : uts.machine,
 		    err ? "[unknown]" : uts.release, perf_version_string);
 	if (use_browser <= 0)
tools/perf/ui/browsers/map.c +2 -3
index 98851d55a53e..f2059664b23f 100644
@@ -1,5 +1,4 @@
 #include "../libslang.h"
-#include <elf.h>
 #include <newt.h>
 #include <inttypes.h>
 #include <sys/ttydefaults.h>
@@ -61,8 +60,8 @@ static void map_browser__write(struct ui_browser *self, void *nd, int row)
 	ui_browser__set_percent_color(self, 0, current_entry);
 	slsmg_printf("%*" PRIx64 " %*" PRIx64 " %c ",
 		     mb->addrlen, sym->start, mb->addrlen, sym->end,
-		     sym->binding == STB_GLOBAL ? 'g' :
-		     sym->binding == STB_LOCAL  ? 'l' : 'w');
+		     sym->binding == SYMBIND_GLOBAL ? 'g' :
+		     sym->binding == SYMBIND_LOCAL  ? 'l' : 'w');
 	width = self->width - ((mb->addrlen * 2) + 4);
 	if (width > 0)
 		slsmg_write_nstring(sym->name, width);
tools/perf/util/symbol.c +26 -14
index 38447ad42ad1..db807a8535d1 100644
@@ -114,16 +114,16 @@ static int choose_best_symbol(struct symbol *syma, struct symbol *symb)
 		return SYMBOL_B;
 
 	/* Prefer a non weak symbol over a weak one */
-	a = syma->binding == STB_WEAK;
-	b = symb->binding == STB_WEAK;
+	a = syma->binding == SYMBIND_WEAK;
+	b = symb->binding == SYMBIND_WEAK;
 	if (b && !a)
 		return SYMBOL_A;
 	if (a && !b)
 		return SYMBOL_B;
 
 	/* Prefer a global symbol over a non global one */
-	a = syma->binding == STB_GLOBAL;
-	b = symb->binding == STB_GLOBAL;
+	a = syma->binding == SYMBIND_GLOBAL;
+	b = symb->binding == SYMBIND_GLOBAL;
 	if (a && !b)
 		return SYMBOL_A;
 	if (b && !a)
@@ -259,8 +259,8 @@ static size_t symbol__fprintf(struct symbol *sym, FILE *fp)
 {
 	return fprintf(fp, " %" PRIx64 "-%" PRIx64 " %c %s\n",
 		       sym->start, sym->end,
-		       sym->binding == STB_GLOBAL ? 'g' :
-		       sym->binding == STB_LOCAL  ? 'l' : 'w',
+		       sym->binding == SYMBIND_GLOBAL ? 'g' :
+		       sym->binding == SYMBIND_LOCAL  ? 'l' : 'w',
 		       sym->name);
 }
 
@@ -609,12 +609,12 @@ struct process_kallsyms_args {
 	struct dso *dso;
 };
 
-static u8 kallsyms2elf_type(char type)
+static u8 kallsyms_binding(char type)
 {
 	if (type == 'W')
-		return STB_WEAK;
+		return SYMBIND_WEAK;
 
-	return isupper(type) ? STB_GLOBAL : STB_LOCAL;
+	return isupper(type) ? SYMBIND_GLOBAL : SYMBIND_LOCAL;
 }
 
 static int map__process_kallsym_symbol(void *arg, const char *name,
@@ -628,7 +628,7 @@ static int map__process_kallsym_symbol(void *arg, const char *name,
 		return 0;
 
 	sym = symbol__new(start, end - start + 1,
-			  kallsyms2elf_type(type), name);
+			  kallsyms_binding(type), name);
 	if (sym == NULL)
 		return -ENOMEM;
 	/*
@@ -851,7 +851,7 @@ static int dso__load_perf_map(struct dso *dso, struct map *map,
 		if (len + 2 >= line_len)
 			continue;
 
-		sym = symbol__new(start, size, STB_GLOBAL, line + len);
+		sym = symbol__new(start, size, SYMBIND_GLOBAL, line + len);
 
 		if (sym == NULL)
 			goto out_delete_line;
@@ -1064,7 +1064,7 @@ dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
 				 "%s@plt", elf_sym__name(&sym, symstrs));
 
 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-					STB_GLOBAL, sympltname);
+					SYMBIND_GLOBAL, sympltname);
 			if (!f)
 				goto out_elf_end;
 
@@ -1086,7 +1086,7 @@ dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
 				 "%s@plt", elf_sym__name(&sym, symstrs));
 
 			f = symbol__new(plt_offset, shdr_plt.sh_entsize,
-					STB_GLOBAL, sympltname);
+					SYMBIND_GLOBAL, sympltname);
 			if (!f)
 				goto out_elf_end;
 
@@ -1157,6 +1157,18 @@ static size_t elf_addr_to_index(Elf *elf, GElf_Addr addr)
 	return -1;
 }
 
+static int elf_sym__binding(const GElf_Sym *sym)
+{
+	switch (GELF_ST_BIND(sym->st_info)) {
+	case STB_GLOBAL:
+		return SYMBIND_GLOBAL;
+	case STB_WEAK:
+		return SYMBIND_WEAK;
+	default:
+		return SYMBIND_LOCAL;
+	}
+}
+
 static int dso__swap_init(struct dso *dso, unsigned char eidata)
 {
 	static unsigned int const endian = 1;
@@ -1390,7 +1402,7 @@ static int dso__load_sym(struct dso *dso, struct map *map, const char *name,
 			elf_name = demangled;
 new_symbol:
 		f = symbol__new(sym.st_value, sym.st_size,
-				GELF_ST_BIND(sym.st_info), elf_name);
+				elf_sym__binding(&sym), elf_name);
 		free(demangled);
 		if (!f)
 			goto out_elf_end;
tools/perf/util/symbol.h +4 -0
index 295a9aa1cc78..bd91d2aa8262 100644
@@ -50,6 +50,10 @@ char *strxfrchar(char *s, char from, char to);
 
 #define BUILD_ID_SIZE 20
 
+#define SYMBIND_LOCAL	0
+#define SYMBIND_GLOBAL	1
+#define SYMBIND_WEAK	2
+
 /** struct symbol - symtab entry
  *
  * @ignore - resolvable but tools ignore it (e.g. idle routines)
Signature · 1 line
1.7.10.2
#9 lore raw

Re: [PATCH 5/8] perf symbols: Do not use ELF's symbol binding constants

2012-06-22 (금), 09:43 -0300, Arnaldo Carvalho de Melo:

Quoted 7 lines

Em Fri, Jun 22, 2012 at 02:37:39PM +0900, Namhyung Kim escreveu:

Quoted 5 lines

From: Namhyung Kim <[email protected]>

There's no need to use the ELF's internal value directly. Define and use our own - it's required to eliminated the dependency of libelf.

Quoted 9 lines

Why don't you set STB_GLOBAL, etc to the expected values when libelf is not present? That way no changes need to be made to symbol.c

Ditto for GELF_ST_BIND.

I.e. keep the subset of libelf.h that we use, providing those definitions on the poor man's libelf.h we should use when the "real thing" is not available.

I just tried to be independent to (lib)elf as much as possible. And I thought that using same macro name might cause a bit of confusion - at least for me - so I wanted to use more descriptive and generic name.

But it's not a big deal. If you insist on using the same name is the better way, I can change it.

Signature · 2 lines
Regards,
Namhyung Kim
#10 lore raw

Re: [PATCH 5/8] perf symbols: Do not use ELF's symbol binding constants

Em Sat, Jun 23, 2012 at 12:19:02AM +0900, Namhyung Kim escreveu:

Quoted 10 lines

2012-06-22 (금), 09:43 -0300, Arnaldo Carvalho de Melo:

Quoted 8 lines

Why don't you set STB_GLOBAL, etc to the expected values when libelf is not present? That way no changes need to be made to symbol.c

Ditto for GELF_ST_BIND.

I.e. keep the subset of libelf.h that we use, providing those definitions on the poor man's libelf.h we should use when the "real thing" is not available.

Quoted 6 lines

I just tried to be independent to (lib)elf as much as possible. And I thought that using same macro name might cause a bit of confusion - at least for me - so I wanted to use more descriptive and generic name.

But it's not a big deal. If you insist on using the same name is the better way, I can change it.

I think its better to use the well know names, libelf is not just one implementation, there are several, and we are interested in people experienced with those APIs to feel at ease when looking at our code :-)

Also as a general practice I try hard to reduce patch size, so restricting the changes to allow building with an alternative, stripped down libelf to the stripped down libelf headers seems like a good path to follow.

- Arnaldo

author maintainer
#11 lore raw

PATCH 6/8 perf tools: Split out util/symbol-elf.c

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Factor out the dependency of ELF handling into separate symbol-elf.c file. It is a preparation of building a minimalistic version perf tools which doesn't depend on the elfutils.

---
 tools/perf/Makefile          |    1 +
 tools/perf/util/symbol-elf.c |  719 +++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/symbol.c     |  732 +-----------------------------------------
 tools/perf/util/symbol.h     |   15 +
 4 files changed, 743 insertions(+), 724 deletions(-)
 create mode 100644 tools/perf/util/symbol-elf.c
tools/perf/Makefile +1 -0
index d698c118a602..0f50a773da08 100644
@@ -354,6 +354,7 @@ LIB_OBJS += $(OUTPUT)util/usage.o
 LIB_OBJS += $(OUTPUT)util/wrapper.o
 LIB_OBJS += $(OUTPUT)util/sigchain.o
 LIB_OBJS += $(OUTPUT)util/symbol.o
+LIB_OBJS += $(OUTPUT)util/symbol-elf.o
 LIB_OBJS += $(OUTPUT)util/color.o
 LIB_OBJS += $(OUTPUT)util/pager.o
 LIB_OBJS += $(OUTPUT)util/header.o
tools/perf/util/symbol-elf.c new file +719 -0

719 lines not on this page · raw message

tools/perf/util/symbol.c +8 -724

799 lines not on this page · raw message

tools/perf/util/symbol.h +15 -0
index bd91d2aa8262..d6ee62a49e8f 100644
@@ -233,6 +233,7 @@ void dso__sort_by_name(struct dso *dso, enum map_type type);
 
 struct dso *__dsos__findnew(struct list_head *head, const char *name);
 
+void dsos__add(struct list_head *head, struct dso *dso);
 int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter);
 int dso__load_vmlinux(struct dso *dso, struct map *map,
 		      const char *vmlinux, symbol_filter_t filter);
@@ -285,11 +286,14 @@ struct symbol *dso__find_symbol_by_name(struct dso *dso, enum map_type type,
 int filename__read_build_id(const char *filename, void *bf, size_t size);
 int sysfs__read_build_id(const char *filename, void *bf, size_t size);
 bool __dsos__read_build_ids(struct list_head *head, bool with_hits);
+bool dso__build_id_equal(const struct dso *dso, u8 *build_id);
 int build_id__sprintf(const u8 *build_id, int len, char *bf);
 int kallsyms__parse(const char *filename, void *arg,
 		    int (*process_symbol)(void *arg, const char *name,
 					  char type, u64 start, u64 end));
 
+struct map *map__new2(u64 start, struct dso *dso, enum map_type type);
+
 void machine__destroy_kernel_maps(struct machine *machine);
 int __machine__create_kernel_maps(struct machine *machine, struct dso *kernel);
 int machine__create_kernel_maps(struct machine *machine);
@@ -301,6 +305,8 @@ void machines__destroy_guest_kernel_maps(struct rb_root *machines);
 int symbol__init(void);
 void symbol__exit(void);
 void symbol__elf_init(void);
+struct symbol *symbol__new(u64 start, u64 len, u8 binding, const char *name);
+void symbols__insert(struct rb_root *symbols, struct symbol *sym);
 size_t symbol__fprintf_symname_offs(const struct symbol *sym,
 				    const struct addr_location *al, FILE *fp);
 size_t symbol__fprintf_symname(const struct symbol *sym, FILE *fp);
@@ -308,4 +314,13 @@ bool symbol_type__is_a(char symbol_type, enum map_type map_type);
 
 size_t machine__fprintf_vmlinux_path(struct machine *machine, FILE *fp);
 
+void symbols__fixup_duplicate(struct rb_root *symbols);
+void symbols__fixup_end(struct rb_root *symbols);
+void __map_groups__fixup_end(struct map_groups *mg, enum map_type type);
+
+int dso__synthesize_plt_symbols(struct dso *dso, char *name, struct map *map,
+				symbol_filter_t filter);
+int dso__load_sym(struct dso *dso, struct map *map, const char *name, int fd,
+		  symbol_filter_t filter, int kmodule, int want_symtab);
+
 #endif /* __PERF_SYMBOL */
Signature · 1 line
1.7.10.2
author maintainer
#12 lore raw

PATCH 7/8 perf tools: Support minimal build without libelf

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Now we have isolated all ELF-specific stuff, it's possible to build without libelf. The output binary can do most of jobs but lacks (user level) symbol information - kernel symbols are still accessable thanks to the kallsyms.

For now, only 'perf probe' command is removed since it depends on libelf/libdw heavily.

---
 tools/perf/Makefile                 |   57 ++++++++++++++++++++++++++---------
 tools/perf/builtin-inject.c         |    5 ++-
 tools/perf/command-list.txt         |    2 +-
 tools/perf/perf.c                   |    2 ++
 tools/perf/util/generate-cmdlist.sh |   15 +++++++++
 tools/perf/util/map.c               |    3 +-
 tools/perf/util/symbol-minimal.c    |   33 ++++++++++++++++++++
 7 files changed, 100 insertions(+), 17 deletions(-)
 create mode 100644 tools/perf/util/symbol-minimal.c
tools/perf/Makefile +43 -14
index 0f50a773da08..34eaaff89b62 100644
@@ -37,7 +37,12 @@ include config/utilities.mak
 #
 # Define NO_NEWT if you do not want TUI support.
 #
+# Define NO_GTK2 if you do not want GTK+ GUI support.
+#
 # Define NO_DEMANGLE if you do not want C++ symbol demangling.
+#
+# Define NO_LIBELF if you do not want libelf dependency (e.g. cross-builds)
+#
 
 $(OUTPUT)PERF-VERSION-FILE: .FORCE-PERF-VERSION-FILE
 	@$(SHELL_PATH) util/PERF-VERSION-GEN $(OUTPUT)
@@ -445,34 +450,57 @@ PYRF_OBJS += $(OUTPUT)util/xyarray.o
 -include config.mak.autogen
 -include config.mak
 
-ifndef NO_DWARF
-FLAGS_DWARF=$(ALL_CFLAGS) -ldw -lelf $(ALL_LDFLAGS) $(EXTLIBS)
-ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y)
-	msg := $(warning No libdw.h found or old libdw.h found or elfutils is older than 0.138, disables dwarf support. Please install new elfutils-devel/libdw-dev);
+ifdef NO_LIBELF
 	NO_DWARF := 1
-endif # Dwarf support
-endif # NO_DWARF
-
--include arch/$(ARCH)/Makefile
-
-ifneq ($(OUTPUT),)
-	BASIC_CFLAGS += -I$(OUTPUT)
-endif
-
+	NO_DEMANGLE := 1
+else
 FLAGS_LIBELF=$(ALL_CFLAGS) $(ALL_LDFLAGS) $(EXTLIBS)
 ifneq ($(call try-cc,$(SOURCE_LIBELF),$(FLAGS_LIBELF)),y)
 	FLAGS_GLIBC=$(ALL_CFLAGS) $(ALL_LDFLAGS)
 	ifneq ($(call try-cc,$(SOURCE_GLIBC),$(FLAGS_GLIBC)),y)
 		msg := $(error No gnu/libc-version.h found, please install glibc-dev[el]/glibc-static);
 	else
-		msg := $(error No libelf.h/libelf found, please install libelf-dev/elfutils-libelf-devel);
+		NO_LIBELF := 1
+		NO_DWARF := 1
+		NO_DEMANGLE := 1
 	endif
 endif
+endif # NO_LIBELF
+
+-include arch/$(ARCH)/Makefile
+
+ifneq ($(OUTPUT),)
+	BASIC_CFLAGS += -I$(OUTPUT)
+endif
+
+ifdef NO_LIBELF
+BASIC_CFLAGS += -DNO_LIBELF_SUPPORT
+
+EXTLIBS := $(filter-out -lelf,$(EXTLIBS))
+
+# Remove ELF/DWARF dependent codes
+LIB_OBJS := $(filter-out $(OUTPUT)util/symbol-elf.o,$(LIB_OBJS))
+LIB_OBJS := $(filter-out $(OUTPUT)util/dwarf-aux.o,$(LIB_OBJS))
+LIB_OBJS := $(filter-out $(OUTPUT)util/probe-event.o,$(LIB_OBJS))
+LIB_OBJS := $(filter-out $(OUTPUT)util/probe-finder.o,$(LIB_OBJS))
+
+BUILTIN_OBJS := $(filter-out $(OUTPUT)builtin-probe.o,$(BUILTIN_OBJS))
+
+# Use minimal symbol handling
+LIB_OBJS += $(OUTPUT)util/symbol-minimal.o
+
+else # NO_LIBELF
 
 ifneq ($(call try-cc,$(SOURCE_ELF_MMAP),$(FLAGS_COMMON)),y)
 	BASIC_CFLAGS += -DLIBELF_NO_MMAP
 endif
 
+FLAGS_DWARF=$(ALL_CFLAGS) -ldw -lelf $(ALL_LDFLAGS) $(EXTLIBS)
+ifneq ($(call try-cc,$(SOURCE_DWARF),$(FLAGS_DWARF)),y)
+	msg := $(warning No libdw.h found or old libdw.h found or elfutils is older than 0.138, disables dwarf support. Please install new elfutils-devel/libdw-dev);
+	NO_DWARF := 1
+endif # Dwarf support
+
 ifndef NO_DWARF
 ifeq ($(origin PERF_HAVE_DWARF_REGS), undefined)
 	msg := $(warning DWARF register mappings have not been defined for architecture $(ARCH), DWARF support disabled);
@@ -483,6 +511,7 @@ else
 	LIB_OBJS += $(OUTPUT)util/dwarf-aux.o
 endif # PERF_HAVE_DWARF_REGS
 endif # NO_DWARF
+endif # NO_LIBELF
 
 ifdef NO_NEWT
 	BASIC_CFLAGS += -DNO_NEWT_SUPPORT
tools/perf/builtin-inject.c +4 -1
index 3beab489afc5..64d8ba2fb7bc 100644
@@ -191,10 +191,13 @@ static int perf_event__inject_buildid(struct perf_tool *tool,
 				 * If this fails, too bad, let the other side
 				 * account this as unresolved.
 				 */
-			} else
+			} else {
+#ifndef NO_LIBELF_SUPPORT
 				pr_warning("no symbols found in %s, maybe "
 					   "install a debug package?\n",
 					   al.map->dso->long_name);
+#endif
+			}
 		}
 	}
 
tools/perf/command-list.txt +1 -1
index d695fe40fbff..0303ec692274 100644
@@ -18,7 +18,7 @@ perf-stat			mainporcelain common
 perf-timechart			mainporcelain common
 perf-top			mainporcelain common
 perf-script			mainporcelain common
-perf-probe			mainporcelain common
+perf-probe			mainporcelain full
 perf-kmem			mainporcelain common
 perf-lock			mainporcelain common
 perf-kvm			mainporcelain common
tools/perf/perf.c +2 -0
index 2b2e225a4d4c..a1450ccac291 100644
@@ -313,7 +313,9 @@ static void handle_internal_command(int argc, const char **argv)
 		{ "version",	cmd_version,	0 },
 		{ "script",	cmd_script,	0 },
 		{ "sched",	cmd_sched,	0 },
+#ifndef NO_LIBELF_SUPPORT
 		{ "probe",	cmd_probe,	0 },
+#endif
 		{ "kmem",	cmd_kmem,	0 },
 		{ "lock",	cmd_lock,	0 },
 		{ "kvm",	cmd_kvm,	0 },
tools/perf/util/generate-cmdlist.sh +15 -0
index f06f6fd148f8..389590c1ad21 100755
@@ -21,4 +21,19 @@ do
 	    p
      }' "Documentation/perf-$cmd.txt"
 done
+
+echo "#ifndef NO_LIBELF_SUPPORT"
+sed -n -e 's/^perf-\([^ 	]*\)[ 	].* full.*/\1/p' command-list.txt |
+sort |
+while read cmd
+do
+     sed -n '
+     /^NAME/,/perf-'"$cmd"'/H
+     ${
+            x
+            s/.*perf-'"$cmd"' - \(.*\)/  {"'"$cmd"'", "\1"},/
+	    p
+     }' "Documentation/perf-$cmd.txt"
+done
+echo "#endif /* NO_LIBELF_SUPPORT */"
 echo "};"
tools/perf/util/map.c +2 -1
index 35ae56864e4f..f29ac8b80f4e 100644
@@ -135,6 +135,7 @@ int map__load(struct map *self, symbol_filter_t filter)
 		pr_warning(", continuing without symbols\n");
 		return -1;
 	} else if (nr == 0) {
+#ifndef NO_LIBELF_SUPPORT
 		const size_t len = strlen(name);
 		const size_t real_len = len - sizeof(DSO__DELETED);
 
@@ -147,7 +148,7 @@ int map__load(struct map *self, symbol_filter_t filter)
 			pr_warning("no symbols found in %s, maybe install "
 				   "a debug package?\n", name);
 		}
-
+#endif
 		return -1;
 	}
 	/*
tools/perf/util/symbol-minimal.c new file +33 -0
new file mode 100644
index 000000000000..fa37f3bb88d1
@@ -0,0 +1,33 @@
+#include "symbol.h"
+
+
+int filename__read_build_id(const char *filename __used, void *bf __used,
+			    size_t size __used)
+{
+	return -1;
+}
+
+int sysfs__read_build_id(const char *filename __used, void *build_id __used,
+			 size_t size __used)
+{
+	return -1;
+}
+
+int dso__synthesize_plt_symbols(struct dso *dso __used, char *name __used,
+				struct map *map __used,
+				symbol_filter_t filter __used)
+{
+	return 0;
+}
+
+int dso__load_sym(struct dso *dso __used, struct map *map __used,
+		  const char *name __used, int fd __used,
+		  symbol_filter_t filter __used, int kmodule __used,
+		  int want_symtab __used)
+{
+	return 0;
+}
+
+void symbol__elf_init(void)
+{
+}
Signature · 1 line
1.7.10.2
author maintainer
#13 lore raw

PATCH 8/8 perf symbols: Implement poor man's ELF parser

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Implement a minimal elf parser for getting build-id. It assumes that required elf.h header is provided by libc header on the system and the parser only looks for PT_NOTE program header to check build-id.

---
 tools/perf/util/symbol-minimal.c |  242 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 235 insertions(+), 7 deletions(-)
tools/perf/util/symbol-minimal.c +235 -7
index fa37f3bb88d1..9f3c681ca008 100644
@@ -1,18 +1,240 @@
 #include "symbol.h"
 
+#include <elf.h>
+#include <stdio.h>
+#include <fcntl.h>
+#include <string.h>
+#include <byteswap.h>
+#include <sys/stat.h>
 
-int filename__read_build_id(const char *filename __used, void *bf __used,
-			    size_t size __used)
+
+static bool check_need_swap(int file_endian)
 {
-	return -1;
+	const int data = 1;
+	u8 *check = (u8 *)&data;
+	int host_endian;
+
+	if (check[0] == 1)
+		host_endian = ELFDATA2LSB;
+	else
+		host_endian = ELFDATA2MSB;
+
+	return host_endian != file_endian;
 }
 
-int sysfs__read_build_id(const char *filename __used, void *build_id __used,
-			 size_t size __used)
+#define NOTE_ALIGN(sz) (((sz) + 3) & ~3)
+
+#define NT_GNU_BUILD_ID	3
+
+static int read_build_id(void *note_data, size_t note_len, void *bf,
+			 size_t size, bool need_swap)
 {
+	struct {
+		u32 n_namesz;
+		u32 n_descsz;
+		u32 n_type;
+	} *nhdr;
+	void *ptr;
+
+	ptr = note_data;
+	while (ptr < (note_data + note_len)) {
+		const char *name;
+		size_t namesz, descsz;
+
+		nhdr = ptr;
+		if (need_swap) {
+			nhdr->n_namesz = bswap_32(nhdr->n_namesz);
+			nhdr->n_descsz = bswap_32(nhdr->n_descsz);
+			nhdr->n_type = bswap_32(nhdr->n_type);
+		}
+
+		namesz = NOTE_ALIGN(nhdr->n_namesz);
+		descsz = NOTE_ALIGN(nhdr->n_descsz);
+
+		ptr += sizeof(*nhdr);
+		name = ptr;
+		ptr += namesz;
+		if (nhdr->n_type == NT_GNU_BUILD_ID &&
+		    nhdr->n_namesz == sizeof("GNU")) {
+			if (memcmp(name, "GNU", sizeof("GNU")) == 0) {
+				size_t sz = min(size, descsz);
+				memcpy(bf, ptr, sz);
+				memset(bf + sz, 0, size - sz);
+				return 0;
+			}
+		}
+		ptr += descsz;
+	}
+
 	return -1;
 }
 
+/*
+ * Just try PT_NOTE header otherwise fails
+ */
+int filename__read_build_id(const char *filename, void *bf, size_t size)
+{
+	FILE *fp;
+	int ret = -1;
+	bool need_swap = false;
+	u8 e_ident[EI_NIDENT];
+	size_t buf_size;
+	void *buf;
+	int i;
+
+	fp = fopen(filename, "r");
+	if (fp == NULL)
+		return -1;
+
+	if (fread(e_ident, sizeof(e_ident), 1, fp) != 1)
+		goto out;
+
+	if (memcmp(e_ident, ELFMAG, SELFMAG) ||
+	    e_ident[EI_VERSION] != EV_CURRENT)
+		goto out;
+
+	need_swap = check_need_swap(e_ident[EI_DATA]);
+
+	/* for simplicity */
+	fseek(fp, 0, SEEK_SET);
+
+	if (e_ident[EI_CLASS] == ELFCLASS32) {
+		Elf32_Ehdr ehdr;
+		Elf32_Phdr *phdr;
+
+		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
+			goto out;
+
+		if (need_swap) {
+			ehdr.e_phoff = bswap_32(ehdr.e_phoff);
+			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
+			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
+		}
+
+		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
+		buf = malloc(buf_size);
+		if (buf == NULL)
+			goto out;
+
+		fseek(fp, ehdr.e_phoff, SEEK_SET);
+		if (fread(buf, buf_size, 1, fp) != 1)
+			goto out_free;
+
+		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
+			void *tmp;
+
+			if (need_swap) {
+				phdr->p_type = bswap_32(phdr->p_type);
+				phdr->p_offset = bswap_32(phdr->p_offset);
+				phdr->p_filesz = bswap_32(phdr->p_filesz);
+			}
+
+			if (phdr->p_type != PT_NOTE)
+				continue;
+
+			buf_size = phdr->p_filesz;
+			tmp = realloc(buf, buf_size);
+			if (tmp == NULL)
+				goto out_free;
+
+			buf = tmp;
+			fseek(fp, phdr->p_offset, SEEK_SET);
+			if (fread(buf, buf_size, 1, fp) != 1)
+				goto out_free;
+
+			ret = read_build_id(buf, buf_size, bf, size, need_swap);
+			if (ret == 0)
+				ret = size;
+			break;
+		}
+	} else {
+		Elf64_Ehdr ehdr;
+		Elf64_Phdr *phdr;
+
+		if (fread(&ehdr, sizeof(ehdr), 1, fp) != 1)
+			goto out;
+
+		if (need_swap) {
+			ehdr.e_phoff = bswap_64(ehdr.e_phoff);
+			ehdr.e_phentsize = bswap_16(ehdr.e_phentsize);
+			ehdr.e_phnum = bswap_16(ehdr.e_phnum);
+		}
+
+		buf_size = ehdr.e_phentsize * ehdr.e_phnum;
+		buf = malloc(buf_size);
+		if (buf == NULL)
+			goto out;
+
+		fseek(fp, ehdr.e_phoff, SEEK_SET);
+		if (fread(buf, buf_size, 1, fp) != 1)
+			goto out_free;
+
+		for (i = 0, phdr = buf; i < ehdr.e_phnum; i++, phdr++) {
+			void *tmp;
+
+			if (need_swap) {
+				phdr->p_type = bswap_32(phdr->p_type);
+				phdr->p_offset = bswap_64(phdr->p_offset);
+				phdr->p_filesz = bswap_64(phdr->p_filesz);
+			}
+
+			if (phdr->p_type != PT_NOTE)
+				continue;
+
+			buf_size = phdr->p_filesz;
+			tmp = realloc(buf, buf_size);
+			if (tmp == NULL)
+				goto out_free;
+
+			buf = tmp;
+			fseek(fp, phdr->p_offset, SEEK_SET);
+			if (fread(buf, buf_size, 1, fp) != 1)
+				goto out_free;
+
+			ret = read_build_id(buf, buf_size, bf, size, need_swap);
+			if (ret == 0)
+				ret = size;
+			break;
+		}
+	}
+out_free:
+	free(buf);
+out:
+	fclose(fp);
+	return ret;
+}
+
+int sysfs__read_build_id(const char *filename, void *build_id, size_t size)
+{
+	int fd;
+	int ret = -1;
+	struct stat stbuf;
+	size_t buf_size;
+	void *buf;
+
+	fd = open(filename, O_RDONLY);
+	if (fd < 0)
+		return -1;
+
+	if (fstat(fd, &stbuf) < 0)
+		goto out;
+
+	buf_size = stbuf.st_size;
+	buf = malloc(buf_size);
+	if (buf == NULL)
+		goto out;
+
+	if (read(fd, buf, buf_size) != (ssize_t) buf_size)
+		goto out_free;
+
+	ret = read_build_id(buf, buf_size, build_id, size, false);
+out_free:
+	free(buf);
+out:
+	close(fd);
+	return ret;
+}
+
 int dso__synthesize_plt_symbols(struct dso *dso __used, char *name __used,
 				struct map *map __used,
 				symbol_filter_t filter __used)
@@ -20,11 +242,17 @@ int dso__synthesize_plt_symbols(struct dso *dso __used, char *name __used,
 	return 0;
 }
 
-int dso__load_sym(struct dso *dso __used, struct map *map __used,
-		  const char *name __used, int fd __used,
+int dso__load_sym(struct dso *dso, struct map *map __used,
+		  const char *name, int fd __used,
 		  symbol_filter_t filter __used, int kmodule __used,
 		  int want_symtab __used)
 {
+	unsigned char *build_id[BUILD_ID_SIZE];
+
+	if (filename__read_build_id(name, build_id, BUILD_ID_SIZE) > 0) {
+		dso__set_build_id(dso, build_id);
+		return 1;
+	}
 	return 0;
 }
 
Signature · 1 line
1.7.10.2
#14 lore raw
In reply to Namhyung Kim
Quoted 3 lines · Namhyung Kim

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

Why? the very first versions didn't know about any of that nonsense :-) It works just fine as long as you don't go change binaries around.

That said, you did the work already, so no objection, just saying builtids aren't that important.

#15 lore raw

Hi Peter,

2012-06-22 (금), 11:47 +0200, Peter Zijlstra:

Quoted 10 lines · Namhyung Kim
Quoted 3 lines

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

Why? the very first versions didn't know about any of that nonsense :-) It works just fine as long as you don't go change binaries around.

That said, you did the work already, so no objection, just saying builtids aren't that important.

I'm not sure I understood you correctly. But 'perf record' needs to know about the build-id's to save them to perf.data for 'perf report' later. And 'perf archive' also needs to know about them to select necessary binaries for the session.

Signature · 2 lines
Regards,
Namhyung Kim
#16 lore raw
Quoted 11 lines · Namhyung Kim

Hi Peter,

2012-06-22 (금), 11:47 +0200, Peter Zijlstra:

Quoted 8 lines · Namhyung Kim
Quoted 3 lines

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

Why? the very first versions didn't know about any of that nonsense :-) It works just fine as long as you don't go change binaries around.

Quoted 8 lines
Quoted 2 lines

That said, you did the work already, so no objection, just saying builtids aren't that important.

I'm not sure I understood you correctly. But 'perf record' needs to know about the build-id's to save them to perf.data for 'perf report' later. And 'perf archive' also needs to know about them to select necessary binaries for the session.

And build-id's are not required for report (-B option for record).

Also, the intent is for a small footprint binary for embedded systems. On such a system I would expect binaries and libraries to be stripped, so no point in running perf-archive.

David

#17 lore raw

2012-06-22 (금), 09:18 -0600, David Ahern:

Quoted 19 lines · Namhyung Kim
Quoted 9 lines

2012-06-22 (금), 11:47 +0200, Peter Zijlstra:

Quoted 8 lines · Namhyung Kim
Quoted 3 lines

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

Why? the very first versions didn't know about any of that nonsense :-) It works just fine as long as you don't go change binaries around.

Quoted 8 lines
Quoted 2 lines

That said, you did the work already, so no objection, just saying builtids aren't that important.

I'm not sure I understood you correctly. But 'perf record' needs to know about the build-id's to save them to perf.data for 'perf report' later. And 'perf archive' also needs to know about them to select necessary binaries for the session.

Quoted 6 lines

And build-id's are not required for report (-B option for record).

Also, the intent is for a small footprint binary for embedded systems. On such a system I would expect binaries and libraries to be stripped, so no point in running perf-archive.

But is there a chance that binaries on host still contains symbol (and debug) information even for those cases?

Signature · 2 lines
Regards,
Namhyung Kim
#18 lore raw
Quoted 10 lines · Namhyung Kim
Quoted 6 lines

And build-id's are not required for report (-B option for record).

Also, the intent is for a small footprint binary for embedded systems. On such a system I would expect binaries and libraries to be stripped, so no point in running perf-archive.

But is there a chance that binaries on host still contains symbol (and debug) information even for those cases?

AFAIK embedded systems do not have the luxury of symbols; the goal is as small as possible. All debugging information is left on the build servers for off line analysis. That's certainly true for the product I work on.

David

#19 lore raw

Em Fri, Jun 22, 2012 at 09:35:37AM -0600, David Ahern escreveu:

Quoted 7 lines · Namhyung Kim
Quoted 5 lines
Quoted 5 lines

And build-id's are not required for report (-B option for record).

Also, the intent is for a small footprint binary for embedded systems. On such a system I would expect binaries and libraries to be stripped, so no point in running perf-archive.

Quoted 7 lines
Quoted 2 lines

But is there a chance that binaries on host still contains symbol (and debug) information even for those cases?

AFAIK embedded systems do not have the luxury of symbols; the goal is as small as possible. All debugging information is left on the build servers for off line analysis. That's certainly true for the product I work on.

I think at least the .dymsym symbol table is present in DSOs, aka libraries, otherwise how will they dynamicly link?

- Arnaldo

#20 lore raw
Quoted 5 lines · Arnaldo Carvalho de Melo

I think at least the .dymsym symbol table is present in DSOs, aka libraries, otherwise how will they dynamicly link?

- Arnaldo

yes. I meant debug information wise - everything is stripped during packaging phase, so onbox analysis is severely limited in address to symbol resolutions.

David

author maintainer
#21 lore raw

Hi, David

Quoted 8 lines · David Ahern
Quoted 3 lines · Arnaldo Carvalho de Melo

I think at least the .dymsym symbol table is present in DSOs, aka libraries, otherwise how will they dynamicly link?

yes. I meant debug information wise - everything is stripped during packaging phase, so onbox analysis is severely limited in address to symbol resolutions.

Right, I was saying about a case of the onbox recording and off-line analysis - I hope I didn't use vague words here. And I thought that supporting build-id is helpful (not crucial, though) for the case too.

Thanks,
Namhyung

#22 lore raw
In reply to David Ahern

Em Fri, Jun 22, 2012 at 09:18:34AM -0600, David Ahern escreveu:

Quoted 18 lines · Namhyung Kim
Quoted 9 lines

2012-06-22 (금), 11:47 +0200, Peter Zijlstra:

Quoted 8 lines · Namhyung Kim
Quoted 3 lines

And then I realized that the perf record needs to know about the build-id's anyway. :( So I implemented a poor man's version of elf parser only for parsing the build-id info.

Why? the very first versions didn't know about any of that nonsense :-) It works just fine as long as you don't go change binaries around.

Quoted 7 lines
Quoted 2 lines

That said, you did the work already, so no objection, just saying builtids aren't that important.

I'm not sure I understood you correctly. But 'perf record' needs to know about the build-id's to save them to perf.data for 'perf report' later. And 'perf archive' also needs to know about them to select necessary binaries for the session.

Quoted 5 lines

And build-id's are not required for report (-B option for record).

Also, the intent is for a small footprint binary for embedded systems. On such a system I would expect binaries and libraries to be stripped, so no point in running perf-archive.

Right, build ids are not a strict requirement, its just a safeguard, when available, to prevent WTF moments at post processing time (report, annotate, etc) if different binaries are used to resolve symbols.

- Arnaldo