mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: tip-bot for Arnaldo Carvalho de Melo <acme@redhat.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, paulus@samba.org, acme@redhat.com,
	hpa@zytor.com, mingo@redhat.com, efault@gmx.de,
	peterz@infradead.org, fweisbec@gmail.com, tglx@linutronix.de,
	mingo@elte.hu
Subject: [tip:perf/core] perf symbols: Factor out buildid reading routine
Date: Wed, 4 Nov 2009 11:10:54 GMT	[thread overview]
Message-ID: <tip-2643ce11457a99a85c5bed8dd631e35968e6ca5a@git.kernel.org> (raw)
In-Reply-To: <1257291970-8208-1-git-send-email-acme@infradead.org>

Commit-ID:  2643ce11457a99a85c5bed8dd631e35968e6ca5a
Gitweb:     http://git.kernel.org/tip/2643ce11457a99a85c5bed8dd631e35968e6ca5a
Author:     Arnaldo Carvalho de Melo <acme@redhat.com>
AuthorDate: Tue, 3 Nov 2009 21:46:10 -0200
Committer:  Ingo Molnar <mingo@elte.hu>
CommitDate: Wed, 4 Nov 2009 12:03:32 +0100

perf symbols: Factor out buildid reading routine

So that we can run it without having a DSO instance.

Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Paul Mackerras <paulus@samba.org>
Cc: Mike Galbraith <efault@gmx.de>
LKML-Reference: <1257291970-8208-1-git-send-email-acme@infradead.org>
Signed-off-by: Ingo Molnar <mingo@elte.hu>
---
 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);

      reply	other threads:[~2009-11-04 11:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-03 23:46 [PATCH 1/1] " Arnaldo Carvalho de Melo
2009-11-04 11:10 ` tip-bot for Arnaldo Carvalho de Melo [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-2643ce11457a99a85c5bed8dd631e35968e6ca5a@git.kernel.org \
    --to=acme@redhat.com \
    --cc=efault@gmx.de \
    --cc=fweisbec@gmail.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=mingo@elte.hu \
    --cc=mingo@redhat.com \
    --cc=paulus@samba.org \
    --cc=peterz@infradead.org \
    --cc=tglx@linutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®