From 62eb93905b3b43cea407cfbc061cc7b40ae1c6e9 Mon Sep 17 00:00:00 2001 From: Arnaldo Carvalho de Melo Date: Mon, 18 May 2009 14:28:47 -0300 Subject: [PATCH] perf_counter: Implement dso__load using libelf Signed-off-by: Arnaldo Carvalho de Melo Acked-by: Peter Zijlstra Cc: Paul Mackerras Cc: Corey Ashford Cc: Marcelo Tosatti Cc: Thomas Gleixner LKML-Reference: Signed-off-by: Ingo Molnar --- Documentation/perf_counter/Makefile | 2 +- Documentation/perf_counter/builtin-report.c | 122 +++++++++++++++++++++++++++- 2 files changed, 121 insertions(+), 3 deletions(-) diff --git a/Documentation/perf_counter/Makefile b/Documentation/perf_counter/Makefile index 49c601e10692..6bffa86af6be 100644 --- a/Documentation/perf_counter/Makefile +++ b/Documentation/perf_counter/Makefile @@ -160,7 +160,7 @@ uname_V := $(shell sh -c 'uname -v 2>/dev/null || echo not') # CFLAGS and LDFLAGS are for the users to override from the command line. CFLAGS = -g -O2 -Wall -LDFLAGS = -lpthread -lrt +LDFLAGS = -lpthread -lrt -lelf ALL_CFLAGS = $(CFLAGS) ALL_LDFLAGS = $(LDFLAGS) STRIP ?= strip diff --git a/Documentation/perf_counter/builtin-report.c b/Documentation/perf_counter/builtin-report.c index 864f68f06a9e..ad2f327a6576 100644 --- a/Documentation/perf_counter/builtin-report.c +++ b/Documentation/perf_counter/builtin-report.c @@ -8,6 +8,9 @@ #include #include #include +#include +#include +#include #include #include #include @@ -195,10 +198,123 @@ static struct symbol *dso__find_symbol(struct dso *self, uint64_t ip) return NULL; } +/** + * elf_symtab__for_each_symbol - iterate thru all the symbols + * + * @self: struct elf_symtab instance to iterate + * @index: uint32_t index + * @sym: GElf_Sym iterator + */ +#define elf_symtab__for_each_symbol(syms, nr_syms, index, sym) \ + for (index = 0, gelf_getsym(syms, index, &sym);\ + index < nr_syms; \ + index++, gelf_getsym(syms, index, &sym)) + +static inline uint8_t elf_sym__type(const GElf_Sym *sym) +{ + return GELF_ST_TYPE(sym->st_info); +} + +static inline bool elf_sym__is_function(const GElf_Sym *sym) +{ + return elf_sym__type(sym) == STT_FUNC && + sym->st_name != 0 && + sym->st_shndx != SHN_UNDEF; +} + +static inline const char *elf_sym__name(const GElf_Sym *sym, + const Elf_Data *symstrs) +{ + return symstrs->d_buf + sym->st_name; +} + +static Elf_Scn *elf_section_by_name(Elf *elf, GElf_Ehdr *ep, + GElf_Shdr *shp, const char *name, + size_t *index) +{ + Elf_Scn *sec = NULL; + size_t cnt = 1; + + while ((sec = elf_nextscn(elf, sec)) != NULL) { + char *str; + + gelf_getshdr(sec, shp); + str = elf_strptr(elf, ep->e_shstrndx, shp->sh_name); + if (!strcmp(name, str)) { + if (index) + *index = cnt; + break; + } + ++cnt; + } + + return sec; +} + static int dso__load(struct dso *self) { - /* FIXME */ - return 0; + int fd = open(self->name, O_RDONLY), err = -1; + + if (fd == -1) + return -1; + + Elf *elf = elf_begin(fd, ELF_C_READ_MMAP, NULL); + if (elf == NULL) { + fprintf(stderr, "%s: cannot read %s ELF file.\n", + __func__, self->name); + goto out_close; + } + + GElf_Ehdr ehdr; + if (gelf_getehdr(elf, &ehdr) == NULL) { + fprintf(stderr, "%s: cannot get elf header.\n", __func__); + goto out_elf_end; + } + + GElf_Shdr shdr; + Elf_Scn *sec = elf_section_by_name(elf, &ehdr, &shdr, ".symtab", NULL); + if (sec == NULL) + sec = elf_section_by_name(elf, &ehdr, &shdr, ".dynsym", NULL); + + if (sec == NULL) + goto out_elf_end; + + if (gelf_getshdr(sec, &shdr) == NULL) + goto out_elf_end; + + Elf_Data *syms = elf_getdata(sec, NULL); + if (syms == NULL) + goto out_elf_end; + + sec = elf_getscn(elf, shdr.sh_link); + if (sec == NULL) + goto out_elf_end; + + Elf_Data *symstrs = elf_getdata(sec, NULL); + if (symstrs == NULL) + goto out_elf_end; + + const uint32_t nr_syms = shdr.sh_size / shdr.sh_entsize; + + GElf_Sym sym; + uint32_t index; + elf_symtab__for_each_symbol(syms, nr_syms, index, sym) { + if (!elf_sym__is_function(&sym)) + continue; + struct symbol *f = symbol__new(sym.st_value, sym.st_size, + elf_sym__name(&sym, symstrs)); + if (f == NULL) + goto out_elf_end; + + dso__insert_symbol(self, f); + } + + err = 0; +out_elf_end: + elf_end(elf); +out_close: + close(fd); + return err; } static size_t dso__fprintf(struct dso *self, FILE *fp) @@ -614,6 +730,8 @@ int cmd_report(int argc, char **argv) int ret, rc = EXIT_FAILURE; unsigned long total = 0; + elf_version(EV_CURRENT); + page_size = getpagesize(); process_options(argc, argv); -- 2.11.0