Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752992AbZKCXqY (ORCPT ); Tue, 3 Nov 2009 18:46:24 -0500 Received: (majordomo@vger.kernel.org) by vger.kernel.org id S1752567AbZKCXqV (ORCPT ); Tue, 3 Nov 2009 18:46:21 -0500 Received: from bombadil.infradead.org ([18.85.46.34]:32850 "EHLO bombadil.infradead.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752722AbZKCXqS (ORCPT ); Tue, 3 Nov 2009 18:46:18 -0500 From: Arnaldo Carvalho de Melo To: Ingo Molnar Cc: linux-kernel@vger.kernel.org, Arnaldo Carvalho de Melo , Frederic Weisbecker , Peter Zijlstra , Paul Mackerras , Mike Galbraith Subject: [PATCH 1/1] perf symbols: Factor out buildid reading routine Date: Tue, 3 Nov 2009 21:46:10 -0200 Message-Id: <1257291970-8208-1-git-send-email-acme@infradead.org> X-Mailer: git-send-email 1.6.2.5 X-SRS-Rewrite: SMTP reverse-path rewritten from by bombadil.infradead.org See http://www.infradead.org/rpr.html Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3445 Lines: 131 From: Arnaldo Carvalho de Melo So that we can run it without having a DSO instance. Cc: Frederic Weisbecker Cc: Peter Zijlstra Cc: Paul Mackerras Cc: Mike Galbraith Signed-off-by: Arnaldo Carvalho de Melo --- tools/perf/util/symbol.c | 51 ++++++++++++++++++++++++++++++--------------- tools/perf/util/symbol.h | 2 + 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/tools/perf/util/symbol.c b/tools/perf/util/symbol.c index ac94d7b..e7c7cdb 100644 --- a/tools/perf/util/symbol.c +++ b/tools/perf/util/symbol.c @@ -825,27 +825,27 @@ out_close: return err; } -#define BUILD_ID_SIZE 128 +#define BUILD_ID_SIZE 20 -static char *dso__read_build_id(struct dso *self) +int filename__read_build_id(const char *filename, void *bf, size_t size) { - int i; + int fd, err = -1; GElf_Ehdr ehdr; GElf_Shdr shdr; Elf_Data *build_id_data; Elf_Scn *sec; - char *build_id = NULL, *bid; - unsigned char *raw; Elf *elf; - int fd = open(self->long_name, O_RDONLY); + if (size < BUILD_ID_SIZE) + goto out; + + fd = open(filename, O_RDONLY); if (fd < 0) goto out; elf = elf_begin(fd, PERF_ELF_C_READ_MMAP, NULL); if (elf == NULL) { - pr_err("%s: cannot read %s ELF file.\n", __func__, - self->long_name); + pr_err("%s: cannot read %s ELF file.\n", __func__, filename); goto out_close; } @@ -854,29 +854,46 @@ static char *dso__read_build_id(struct dso *self) goto out_elf_end; } - sec = elf_section_by_name(elf, &ehdr, &shdr, ".note.gnu.build-id", NULL); + sec = elf_section_by_name(elf, &ehdr, &shdr, + ".note.gnu.build-id", NULL); if (sec == NULL) goto out_elf_end; build_id_data = elf_getdata(sec, NULL); if (build_id_data == NULL) goto out_elf_end; - build_id = malloc(BUILD_ID_SIZE); + memcpy(bf, build_id_data->d_buf + 16, BUILD_ID_SIZE); + err = BUILD_ID_SIZE; +out_elf_end: + elf_end(elf); +out_close: + close(fd); +out: + return err; +} + +static char *dso__read_build_id(struct dso *self) +{ + int i, len; + char *build_id = NULL, *bid; + unsigned char rawbf[BUILD_ID_SIZE], *raw; + + len = filename__read_build_id(self->long_name, rawbf, sizeof(rawbf)); + if (len < 0) + goto out; + + build_id = malloc(len * 2 + 1); if (build_id == NULL) - goto out_elf_end; - raw = build_id_data->d_buf + 16; + goto out; bid = build_id; - for (i = 0; i < 20; ++i) { + raw = rawbf; + for (i = 0; i < len; ++i) { sprintf(bid, "%02x", *raw); ++raw; bid += 2; } pr_debug2("%s(%s): %s\n", __func__, self->long_name, build_id); -out_elf_end: - elf_end(elf); -out_close: - close(fd); out: return build_id; } diff --git a/tools/perf/util/symbol.h b/tools/perf/util/symbol.h index 0884330..e0d4a58 100644 --- a/tools/perf/util/symbol.h +++ b/tools/perf/util/symbol.h @@ -82,6 +82,8 @@ void dsos__fprintf(FILE *fp); size_t dso__fprintf(struct dso *self, FILE *fp); char dso__symtab_origin(const struct dso *self); +int filename__read_build_id(const char *filename, void *bf, size_t size); + int load_kernel(symbol_filter_t filter); void symbol__init(unsigned int priv_size); -- 1.6.2.5 -- To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Please read the FAQ at http://www.tux.org/lkml/