Skip to contentSkip to conversation

perf tools: Enhance and correct srcline behavior (v2)

PATCH New Namhyung Kim 2 participants 19 messages Sep 11, 2013 – Oct 15, 2013 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

cover perf tools: Enhance and correct srcline behavior (v2)

Hello,

This patchset tries to fix and enhance current srcline behavior.

Firstly it doesn't actually sort by srcline info but by ip. I suspect it was because of a performance reason to run external addr2line utility. It showed the srcline info after hist entries were collapsed. Thanks to Roberto, we now have internal implementation of addr2line using libbfd so can sort/compare by srcline of entries.

Second problem for me was it sometimes printed "??:0" and sometimes printed a raw ip value for unknown srcline info. I changed to print the former consistently.

While at it, I found some bugs/leaks in srcline handling. Patch 1-3 are fixes for those and can be merged separately.

* v2 changes
- add reviewed-by tags from Jiri
- rebased on top of current acme/perf/core

You can get this series on my 'perf/srcline-v2' branch in my tree at:

git://git.kernel.org/pub/scm/linux/kernel/git/namhyung/linux-perf.git

Any comments are welcome, thanks.
Namhyung

Namhyung Kim (8):
perf sort: Fix a memory leak on srcline
perf annotate: Reuse path from the result of addr2line perf hists: Free srcline when freeing hist_entry perf tools: Factor out get/free_srcline()
perf tools: Do not try to call addr2line for non-binary files perf tools: Pass dso instead of dso_name to get_srcline() perf tools: Save failed result of get_srcline() perf tools: Fix srcline sort key behavior

Roberto Vitillo (1):
perf tools: Implement addr2line directly using libbfd

 tools/perf/Makefile        |   1 +
 tools/perf/config/Makefile |   4 +
 tools/perf/util/annotate.c |  33 +-----
 tools/perf/util/dso.c      |   1 +
 tools/perf/util/dso.h      |   2 +
 tools/perf/util/hist.c     |   1 +
 tools/perf/util/sort.c     |  58 ++++------
 tools/perf/util/srcline.c  | 265 +++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/util.h     |   8 ++
 9 files changed, 308 insertions(+), 65 deletions(-)
 create mode 100644 tools/perf/util/srcline.c
Signature · 1 line
1.7.11.7
author maintainer
#1 lore raw

PATCH 1/9 perf sort: Fix a memory leak on srcline

From: Namhyung Kim <[email protected]>

In the hist_entry__srcline_snprintf(), path and self->srcline are pointing the same memory region, but they are doubly allocated.

---
 tools/perf/util/sort.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
tools/perf/util/sort.c +1 -4
index 5f118a089519..5ba29fc679c5 100644
@@ -269,10 +269,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!fp)
 		goto out_ip;
 
-	if (getline(&path, &line_len, fp) < 0 || !line_len)
-		goto out_ip;
-	self->srcline = strdup(path);
-	if (self->srcline == NULL)
+	if (getline(&self->srcline, &line_len, fp) < 0 || !line_len)
 		goto out_ip;
 
 	nl = strchr(self->srcline, '\n');
Signature · 1 line
1.7.11.7
#2 lore raw

[tip:perf/core] perf sort: Fix a memory leak on srcline

perf sort: Fix a memory leak on srcline
Commit-ID:  963ba5fd5d04f36d6a5c9a94562484a4f270c1de
Gitweb:     http://git.kernel.org/tip/963ba5fd5d04f36d6a5c9a94562484a4f270c1de
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:25 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 15:58:07 -0300

perf sort: Fix a memory leak on srcline

In the hist_entry__srcline_snprintf(), path and self->srcline are pointing the same memory region, but they are doubly allocated.

---
 tools/perf/util/sort.c | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)
tools/perf/util/sort.c +1 -4
index b4ecc0e..97cf3ef 100644
@@ -269,10 +269,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!fp)
 		goto out_ip;
 
-	if (getline(&path, &line_len, fp) < 0 || !line_len)
-		goto out_ip;
-	self->srcline = strdup(path);
-	if (self->srcline == NULL)
+	if (getline(&self->srcline, &line_len, fp) < 0 || !line_len)
 		goto out_ip;
 
 	nl = strchr(self->srcline, '\n');
author maintainer
#3 lore raw

PATCH 2/9 perf annotate: Reuse path from the result of addr2line

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

In the symbol__get_source_line(), path and src_line->path will have same value, but they were allocated separately, and leaks one. Just share path to src_line->path.

---
 tools/perf/util/annotate.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
tools/perf/util/annotate.c +1 -5
index bfc5a27597d6..4f97ae3ec9c0 100644
@@ -1143,11 +1143,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 		if (getline(&path, &line_len, fp) < 0 || !line_len)
 			goto next_close;
 
-		src_line->path = malloc(sizeof(char) * line_len + 1);
-		if (!src_line->path)
-			goto next_close;
-
-		strcpy(src_line->path, path);
+		src_line->path = path;
 		insert_source_line(&tmp_root, src_line);
 
 	next_close:
Signature · 1 line
1.7.11.7
#4 lore raw

[tip:perf/core] perf annotate: Reuse path from the result of addr2line

perf annotate: Reuse path from the result of addr2line
Commit-ID:  89da393c171926d3372f573d752be5ced98038eb
Gitweb:     http://git.kernel.org/tip/89da393c171926d3372f573d752be5ced98038eb
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:26 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 15:58:20 -0300

perf annotate: Reuse path from the result of addr2line

In the symbol__get_source_line(), path and src_line->path will have same value, but they were allocated separately, and leaks one. Just share path to src_line->path.

---
 tools/perf/util/annotate.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
tools/perf/util/annotate.c +1 -5
index 7eae548..c6fd187 100644
@@ -1143,11 +1143,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 		if (getline(&path, &line_len, fp) < 0 || !line_len)
 			goto next_close;
 
-		src_line->path = malloc(sizeof(char) * line_len + 1);
-		if (!src_line->path)
-			goto next_close;
-
-		strcpy(src_line->path, path);
+		src_line->path = path;
 		insert_source_line(&tmp_root, src_line);
 
 	next_close:
author maintainer
#5 lore raw

PATCH 3/9 perf hists: Free srcline when freeing hist_entry

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

We've been leaked srcline of hist_entry, it should be freed also.

---
 tools/perf/util/hist.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/hist.c +1 -0
index 46a0d35a05e1..e6c11ccf138c 100644
@@ -530,6 +530,7 @@ void hist_entry__free(struct hist_entry *he)
 {
 	free(he->branch_info);
 	free(he->mem_info);
+	free(he->srcline);
 	free(he);
 }
 
Signature · 1 line
1.7.11.7
#6 lore raw

[tip:perf/core] perf hists: Free srcline when freeing hist_entry

perf hists: Free srcline when freeing hist_entry
Commit-ID:  909b143162de7af310d2a9351220030260ebe728
Gitweb:     http://git.kernel.org/tip/909b143162de7af310d2a9351220030260ebe728
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:27 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 15:58:28 -0300

perf hists: Free srcline when freeing hist_entry

We've been leaked srcline of hist_entry, it should be freed also.

---
 tools/perf/util/hist.c | 1 +
 1 file changed, 1 insertion(+)
tools/perf/util/hist.c +1 -0
index f3278a3..e6fc38a 100644
@@ -535,6 +535,7 @@ void hist_entry__free(struct hist_entry *he)
 {
 	free(he->branch_info);
 	free(he->mem_info);
+	free(he->srcline);
 	free(he);
 }
 
author maintainer
#7 lore raw

PATCH 4/9 perf tools: Factor out get/free_srcline()

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Currently external addr2line tool is used for srcline sort key and annotate with srcline info. Separate the common code to prepare upcoming enhancements.

---
 tools/perf/Makefile        |  1 +
 tools/perf/util/annotate.c | 20 ++---------
 tools/perf/util/hist.c     |  2 +-
 tools/perf/util/sort.c     | 17 ++--------
 tools/perf/util/srcline.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/util.h     |  6 ++++
 6 files changed, 97 insertions(+), 32 deletions(-)
 create mode 100644 tools/perf/util/srcline.c
tools/perf/Makefile +1 -0
index c5dc1ad1b8d7..3a10a85c3604 100644
@@ -361,6 +361,7 @@ LIB_OBJS += $(OUTPUT)util/intlist.o
 LIB_OBJS += $(OUTPUT)util/vdso.o
 LIB_OBJS += $(OUTPUT)util/stat.o
 LIB_OBJS += $(OUTPUT)util/record.o
+LIB_OBJS += $(OUTPUT)util/srcline.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/annotate.c +3 -17
index 4f97ae3ec9c0..e51c8360f2ad 100644
@@ -1070,7 +1070,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
 			  (sizeof(src_line->p) * (src_line->nr_pcnt - 1));
 
 	for (i = 0; i < len; i++) {
-		free(src_line->path);
+		free_srcline(src_line->path);
 		src_line = (void *)src_line + sizeof_src_line;
 	}
 
@@ -1087,7 +1087,6 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 	u64 start;
 	int i, k;
 	int evidx = evsel->idx;
-	char cmd[PATH_MAX * 2];
 	struct source_line *src_line;
 	struct annotation *notes = symbol__annotation(sym);
 	struct sym_hist *h = annotation__histogram(notes, evidx);
@@ -1115,10 +1114,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 	start = map__rip_2objdump(map, sym->start);
 
 	for (i = 0; i < len; i++) {
-		char *path = NULL;
-		size_t line_len;
 		u64 offset;
-		FILE *fp;
 		double percent_max = 0.0;
 
 		src_line->nr_pcnt = nr_pcnt;
@@ -1135,19 +1131,9 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 			goto next;
 
 		offset = start + i;
-		sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
-		fp = popen(cmd, "r");
-		if (!fp)
-			goto next;
-
-		if (getline(&path, &line_len, fp) < 0 || !line_len)
-			goto next_close;
-
-		src_line->path = path;
+		src_line->path = get_srcline(filename, offset);
 		insert_source_line(&tmp_root, src_line);
 
-	next_close:
-		pclose(fp);
 	next:
 		src_line = (void *)src_line + sizeof_src_line;
 	}
@@ -1188,7 +1174,7 @@ static void print_summary(struct rb_root *root, const char *filename)
 
 		path = src_line->path;
 		color = get_percent_color(percent_max);
-		color_fprintf(stdout, color, " %s", path);
+		color_fprintf(stdout, color, " %s\n", path);
 
 		node = rb_next(node);
 	}
tools/perf/util/hist.c +1 -1
index e6c11ccf138c..fa695ce1662a 100644
@@ -530,7 +530,7 @@ void hist_entry__free(struct hist_entry *he)
 {
 	free(he->branch_info);
 	free(he->mem_info);
-	free(he->srcline);
+	free_srcline(he->srcline);
 	free(he);
 }
 
tools/perf/util/sort.c +3 -14
index 5ba29fc679c5..0d633a059df4 100644
@@ -251,8 +251,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					unsigned int width __maybe_unused)
 {
 	FILE *fp = NULL;
-	char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
-	size_t line_len;
+	char *path = self->srcline;
 
 	if (path != NULL)
 		goto out_path;
@@ -263,19 +262,9 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
 		goto out_ip;
 
-	snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
-		 self->ms.map->dso->long_name, self->ip);
-	fp = popen(cmd, "r");
-	if (!fp)
-		goto out_ip;
-
-	if (getline(&self->srcline, &line_len, fp) < 0 || !line_len)
-		goto out_ip;
+	path = get_srcline(self->ms.map->dso->long_name, self->ip);
+	self->srcline = path;
 
-	nl = strchr(self->srcline, '\n');
-	if (nl != NULL)
-		*nl = '\0';
-	path = self->srcline;
 out_path:
 	if (fp)
 		pclose(fp);
tools/perf/util/srcline.c new file +83 -0

83 lines not on this page · raw message

tools/perf/util/util.h +6 -0
index a53535949043..8c99d732960c 100644
@@ -281,4 +281,10 @@ void dump_stack(void);
 extern unsigned int page_size;
 
 void get_term_dimensions(struct winsize *ws);
+
+#define SRCLINE_UNKNOWN  ((char *) "??:0")
+
+char *get_srcline(const char *dso_name, unsigned long addr);
+void free_srcline(char *srcline);
+
 #endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.7.11.7
#8 lore raw

[tip:perf/core] perf annotate: Factor out get/free_srcline()

perf annotate: Factor out get/free_srcline()
Commit-ID:  f048d548f803b57ee1dbf66702f398ba69657450
Gitweb:     http://git.kernel.org/tip/f048d548f803b57ee1dbf66702f398ba69657450
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:28 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 15:59:39 -0300

perf annotate: Factor out get/free_srcline()

Currently external addr2line tool is used for srcline sort key and annotate with srcline info. Separate the common code to prepare upcoming enhancements.

---
 tools/perf/Makefile.perf   |  1 +
 tools/perf/util/annotate.c | 20 ++---------
 tools/perf/util/hist.c     |  2 +-
 tools/perf/util/sort.c     | 17 ++--------
 tools/perf/util/srcline.c  | 83 ++++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/util.h     |  5 +++
 6 files changed, 96 insertions(+), 32 deletions(-)
tools/perf/Makefile.perf +1 -0
index 40c39c3..1f13615 100644
@@ -363,6 +363,7 @@ LIB_OBJS += $(OUTPUT)util/intlist.o
 LIB_OBJS += $(OUTPUT)util/vdso.o
 LIB_OBJS += $(OUTPUT)util/stat.o
 LIB_OBJS += $(OUTPUT)util/record.o
+LIB_OBJS += $(OUTPUT)util/srcline.o
 
 LIB_OBJS += $(OUTPUT)ui/setup.o
 LIB_OBJS += $(OUTPUT)ui/helpline.o
tools/perf/util/annotate.c +3 -17
index c6fd187..d48297d 100644
@@ -1070,7 +1070,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
 			  (sizeof(src_line->p) * (src_line->nr_pcnt - 1));
 
 	for (i = 0; i < len; i++) {
-		free(src_line->path);
+		free_srcline(src_line->path);
 		src_line = (void *)src_line + sizeof_src_line;
 	}
 
@@ -1087,7 +1087,6 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 	u64 start;
 	int i, k;
 	int evidx = evsel->idx;
-	char cmd[PATH_MAX * 2];
 	struct source_line *src_line;
 	struct annotation *notes = symbol__annotation(sym);
 	struct sym_hist *h = annotation__histogram(notes, evidx);
@@ -1115,10 +1114,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 	start = map__rip_2objdump(map, sym->start);
 
 	for (i = 0; i < len; i++) {
-		char *path = NULL;
-		size_t line_len;
 		u64 offset;
-		FILE *fp;
 		double percent_max = 0.0;
 
 		src_line->nr_pcnt = nr_pcnt;
@@ -1135,19 +1131,9 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 			goto next;
 
 		offset = start + i;
-		sprintf(cmd, "addr2line -e %s %016" PRIx64, filename, offset);
-		fp = popen(cmd, "r");
-		if (!fp)
-			goto next;
-
-		if (getline(&path, &line_len, fp) < 0 || !line_len)
-			goto next_close;
-
-		src_line->path = path;
+		src_line->path = get_srcline(filename, offset);
 		insert_source_line(&tmp_root, src_line);
 
-	next_close:
-		pclose(fp);
 	next:
 		src_line = (void *)src_line + sizeof_src_line;
 	}
@@ -1188,7 +1174,7 @@ static void print_summary(struct rb_root *root, const char *filename)
 
 		path = src_line->path;
 		color = get_percent_color(percent_max);
-		color_fprintf(stdout, color, " %s", path);
+		color_fprintf(stdout, color, " %s\n", path);
 
 		node = rb_next(node);
 	}
tools/perf/util/hist.c +1 -1
index e6fc38a..cca03831 100644
@@ -535,7 +535,7 @@ void hist_entry__free(struct hist_entry *he)
 {
 	free(he->branch_info);
 	free(he->mem_info);
-	free(he->srcline);
+	free_srcline(he->srcline);
 	free(he);
 }
 
tools/perf/util/sort.c +3 -14
index 97cf3ef..b7e0ef0 100644
@@ -251,8 +251,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					unsigned int width __maybe_unused)
 {
 	FILE *fp = NULL;
-	char cmd[PATH_MAX + 2], *path = self->srcline, *nl;
-	size_t line_len;
+	char *path = self->srcline;
 
 	if (path != NULL)
 		goto out_path;
@@ -263,19 +262,9 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
 		goto out_ip;
 
-	snprintf(cmd, sizeof(cmd), "addr2line -e %s %016" PRIx64,
-		 self->ms.map->dso->long_name, self->ip);
-	fp = popen(cmd, "r");
-	if (!fp)
-		goto out_ip;
-
-	if (getline(&self->srcline, &line_len, fp) < 0 || !line_len)
-		goto out_ip;
+	path = get_srcline(self->ms.map->dso->long_name, self->ip);
+	self->srcline = path;
 
-	nl = strchr(self->srcline, '\n');
-	if (nl != NULL)
-		*nl = '\0';
-	path = self->srcline;
 out_path:
 	if (fp)
 		pclose(fp);
tools/perf/util/srcline.c new file +83 -0

83 lines not on this page · raw message

tools/perf/util/util.h +5 -0
index 7fd840b..7c8b43f 100644
@@ -297,4 +297,9 @@ struct parse_tag {
 };
 
 unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
+
+#define SRCLINE_UNKNOWN  ((char *) "??:0")
+
+char *get_srcline(const char *dso_name, unsigned long addr);
+void free_srcline(char *srcline);
 #endif /* GIT_COMPAT_UTIL_H */
author maintainer
#9 lore raw

PATCH 5/9 perf tools: Do not try to call addr2line for non-binary files

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

No need to call addr2line since they don't have such information.

---
 tools/perf/util/sort.c    |  3 ---
 tools/perf/util/srcline.c | 11 +++++++++--
 2 files changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/sort.c +0 -3
index 0d633a059df4..113aa92b1191 100644
@@ -259,9 +259,6 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!self->ms.map)
 		goto out_ip;
 
-	if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
-		goto out_ip;
-
 	path = get_srcline(self->ms.map->dso->long_name, self->ip);
 	self->srcline = path;
 
tools/perf/util/srcline.c +9 -2
index 7e92cca6f502..777f91880cdb 100644
@@ -57,11 +57,17 @@ char *get_srcline(const char *dso_name, unsigned long addr)
 {
 	char *file;
 	unsigned line;
-	char *srcline;
+	char *srcline = SRCLINE_UNKNOWN;
 	size_t size;
 
+	if (dso_name[0] == '[')
+		goto out;
+
+	if (!strncmp(dso_name, "/tmp/perf-", 10))
+		goto out;
+
 	if (!addr2line(dso_name, addr, &file, &line))
-		return SRCLINE_UNKNOWN;
+		goto out;
 
 	/* just calculate actual length */
 	size = snprintf(NULL, 0, "%s:%u", file, line) + 1;
@@ -73,6 +79,7 @@ char *get_srcline(const char *dso_name, unsigned long addr)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
+out:
 	return srcline;
 }
 
Signature · 1 line
1.7.11.7
#10 lore raw

[tip:perf/core] perf tools: Do not try to call addr2line on non-binary files

perf tools: Do not try to call addr2line on non-binary files
Commit-ID:  58d91a0068694a5ba3efc99e88ce6b4b0dd0d085
Gitweb:     http://git.kernel.org/tip/58d91a0068694a5ba3efc99e88ce6b4b0dd0d085
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:29 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 16:01:05 -0300

perf tools: Do not try to call addr2line on non-binary files

No need to call addr2line since they don't have such information.

---
 tools/perf/util/sort.c    |  3 ---
 tools/perf/util/srcline.c | 11 +++++++++--
 2 files changed, 9 insertions(+), 5 deletions(-)
tools/perf/util/sort.c +0 -3
index b7e0ef0..d443593 100644
@@ -259,9 +259,6 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!self->ms.map)
 		goto out_ip;
 
-	if (!strncmp(self->ms.map->dso->long_name, "/tmp/perf-", 10))
-		goto out_ip;
-
 	path = get_srcline(self->ms.map->dso->long_name, self->ip);
 	self->srcline = path;
 
tools/perf/util/srcline.c +9 -2
index 7e92cca..777f918 100644
@@ -57,11 +57,17 @@ char *get_srcline(const char *dso_name, unsigned long addr)
 {
 	char *file;
 	unsigned line;
-	char *srcline;
+	char *srcline = SRCLINE_UNKNOWN;
 	size_t size;
 
+	if (dso_name[0] == '[')
+		goto out;
+
+	if (!strncmp(dso_name, "/tmp/perf-", 10))
+		goto out;
+
 	if (!addr2line(dso_name, addr, &file, &line))
-		return SRCLINE_UNKNOWN;
+		goto out;
 
 	/* just calculate actual length */
 	size = snprintf(NULL, 0, "%s:%u", file, line) + 1;
@@ -73,6 +79,7 @@ char *get_srcline(const char *dso_name, unsigned long addr)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
+out:
 	return srcline;
 }
 
author maintainer
#11 lore raw

PATCH 6/9 perf tools: Pass dso instead of dso_name to get_srcline()

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

This is a preparation of next change. No functional changes are intended.

---
 tools/perf/util/annotate.c | 11 ++++-------
 tools/perf/util/dso.h      |  1 +
 tools/perf/util/sort.c     |  2 +-
 tools/perf/util/srcline.c  |  4 +++-
 tools/perf/util/util.h     |  4 +++-
 5 files changed, 12 insertions(+), 10 deletions(-)
tools/perf/util/annotate.c +4 -7
index e51c8360f2ad..f7bdc01d10bb 100644
@@ -1081,8 +1081,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
 /* Get the filename:line for the colored entries */
 static int symbol__get_source_line(struct symbol *sym, struct map *map,
 				   struct perf_evsel *evsel,
-				   struct rb_root *root, int len,
-				   const char *filename)
+				   struct rb_root *root, int len)
 {
 	u64 start;
 	int i, k;
@@ -1131,7 +1130,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 			goto next;
 
 		offset = start + i;
-		src_line->path = get_srcline(filename, offset);
+		src_line->path = get_srcline(map->dso, offset);
 		insert_source_line(&tmp_root, src_line);
 
 	next:
@@ -1338,7 +1337,6 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 			 bool full_paths, int min_pcnt, int max_lines)
 {
 	struct dso *dso = map->dso;
-	const char *filename = dso->long_name;
 	struct rb_root source_line = RB_ROOT;
 	u64 len;
 
@@ -1348,9 +1346,8 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 	len = symbol__size(sym);
 
 	if (print_lines) {
-		symbol__get_source_line(sym, map, evsel, &source_line,
-					len, filename);
-		print_summary(&source_line, filename);
+		symbol__get_source_line(sym, map, evsel, &source_line, len);
+		print_summary(&source_line, dso->long_name);
 	}
 
 	symbol__annotate_printf(sym, map, evsel, full_paths,
tools/perf/util/dso.h +1 -0
index b793053335d6..477b3b68daf4 100644
@@ -6,6 +6,7 @@
 #include <stdbool.h>
 #include "types.h"
 #include "map.h"
+#include "build-id.h"
 
 enum dso_binary_type {
 	DSO_BINARY_TYPE__KALLSYMS = 0,
tools/perf/util/sort.c +1 -1
index 113aa92b1191..58cfe69d3a6c 100644
@@ -259,7 +259,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!self->ms.map)
 		goto out_ip;
 
-	path = get_srcline(self->ms.map->dso->long_name, self->ip);
+	path = get_srcline(self->ms.map->dso, self->ip);
 	self->srcline = path;
 
 out_path:
tools/perf/util/srcline.c +3 -1
index 777f91880cdb..c736d9428cf2 100644
@@ -4,6 +4,7 @@
 
 #include <linux/kernel.h>
 
+#include "util/dso.h"
 #include "util/util.h"
 #include "util/debug.h"
 
@@ -53,11 +54,12 @@ out:
 	return ret;
 }
 
-char *get_srcline(const char *dso_name, unsigned long addr)
+char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file;
 	unsigned line;
 	char *srcline = SRCLINE_UNKNOWN;
+	char *dso_name = dso->long_name;
 	size_t size;
 
 	if (dso_name[0] == '[')
tools/perf/util/util.h +3 -1
index 8c99d732960c..f1b1b0764b1c 100644
@@ -284,7 +284,9 @@ void get_term_dimensions(struct winsize *ws);
 
 #define SRCLINE_UNKNOWN  ((char *) "??:0")
 
-char *get_srcline(const char *dso_name, unsigned long addr);
+struct dso;
+
+char *get_srcline(struct dso *dso, unsigned long addr);
 void free_srcline(char *srcline);
 
 #endif /* GIT_COMPAT_UTIL_H */
Signature · 1 line
1.7.11.7
#12 lore raw

[tip:perf/core] perf annotate: Pass dso instead of dso_name to get_srcline()

perf annotate: Pass dso instead of dso_name to get_srcline()
Commit-ID:  86c98cab5a3137376ea7df5ffa5bd52e545fee95
Gitweb:     http://git.kernel.org/tip/86c98cab5a3137376ea7df5ffa5bd52e545fee95
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:30 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 16:01:44 -0300

perf annotate: Pass dso instead of dso_name to get_srcline()

This is a preparation of next change. No functional changes are intended.

---
 tools/perf/util/annotate.c | 11 ++++-------
 tools/perf/util/dso.h      |  1 +
 tools/perf/util/sort.c     |  2 +-
 tools/perf/util/srcline.c  |  4 +++-
 tools/perf/util/util.h     |  4 +++-
 5 files changed, 12 insertions(+), 10 deletions(-)
tools/perf/util/annotate.c +4 -7
index d48297d..d73e800 100644
@@ -1081,8 +1081,7 @@ static void symbol__free_source_line(struct symbol *sym, int len)
 /* Get the filename:line for the colored entries */
 static int symbol__get_source_line(struct symbol *sym, struct map *map,
 				   struct perf_evsel *evsel,
-				   struct rb_root *root, int len,
-				   const char *filename)
+				   struct rb_root *root, int len)
 {
 	u64 start;
 	int i, k;
@@ -1131,7 +1130,7 @@ static int symbol__get_source_line(struct symbol *sym, struct map *map,
 			goto next;
 
 		offset = start + i;
-		src_line->path = get_srcline(filename, offset);
+		src_line->path = get_srcline(map->dso, offset);
 		insert_source_line(&tmp_root, src_line);
 
 	next:
@@ -1338,7 +1337,6 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 			 bool full_paths, int min_pcnt, int max_lines)
 {
 	struct dso *dso = map->dso;
-	const char *filename = dso->long_name;
 	struct rb_root source_line = RB_ROOT;
 	u64 len;
 
@@ -1348,9 +1346,8 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 	len = symbol__size(sym);
 
 	if (print_lines) {
-		symbol__get_source_line(sym, map, evsel, &source_line,
-					len, filename);
-		print_summary(&source_line, filename);
+		symbol__get_source_line(sym, map, evsel, &source_line, len);
+		print_summary(&source_line, dso->long_name);
 	}
 
 	symbol__annotate_printf(sym, map, evsel, full_paths,
tools/perf/util/dso.h +1 -0
index dbd9241..72eedd6 100644
@@ -6,6 +6,7 @@
 #include <stdbool.h>
 #include "types.h"
 #include "map.h"
+#include "build-id.h"
 
 enum dso_binary_type {
 	DSO_BINARY_TYPE__KALLSYMS = 0,
tools/perf/util/sort.c +1 -1
index d443593..f732120 100644
@@ -259,7 +259,7 @@ static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 	if (!self->ms.map)
 		goto out_ip;
 
-	path = get_srcline(self->ms.map->dso->long_name, self->ip);
+	path = get_srcline(self->ms.map->dso, self->ip);
 	self->srcline = path;
 
 out_path:
tools/perf/util/srcline.c +3 -1
index 777f918..c736d94 100644
@@ -4,6 +4,7 @@
 
 #include <linux/kernel.h>
 
+#include "util/dso.h"
 #include "util/util.h"
 #include "util/debug.h"
 
@@ -53,11 +54,12 @@ out:
 	return ret;
 }
 
-char *get_srcline(const char *dso_name, unsigned long addr)
+char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file;
 	unsigned line;
 	char *srcline = SRCLINE_UNKNOWN;
+	char *dso_name = dso->long_name;
 	size_t size;
 
 	if (dso_name[0] == '[')
tools/perf/util/util.h +3 -1
index 7c8b43f..1f06ba4 100644
@@ -300,6 +300,8 @@ unsigned long parse_tag_value(const char *str, struct parse_tag *tags);
 
 #define SRCLINE_UNKNOWN  ((char *) "??:0")
 
-char *get_srcline(const char *dso_name, unsigned long addr);
+struct dso;
+
+char *get_srcline(struct dso *dso, unsigned long addr);
 void free_srcline(char *srcline);
 #endif /* GIT_COMPAT_UTIL_H */
author maintainer
#13 lore raw

PATCH 7/9 perf tools: Save failed result of get_srcline()

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Some dso's lack srcline info, so there's no point to keep trying on them. Just save failture status and skip them.

---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  1 +
 tools/perf/util/srcline.c | 10 ++++++++--
 3 files changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/dso.c +1 -0
index e3c1ff8512c8..1f81dd88afc4 100644
@@ -427,6 +427,7 @@ struct dso *dso__new(const char *name)
 		dso->rel = 0;
 		dso->sorted_by_name = 0;
 		dso->has_build_id = 0;
+		dso->has_srcline = 1;
 		dso->kernel = DSO_TYPE_USER;
 		dso->needs_swap = DSO_SWAP__UNSET;
 		INIT_LIST_HEAD(&dso->node);
tools/perf/util/dso.h +1 -0
index 477b3b68daf4..fa36677c6fd6 100644
@@ -82,6 +82,7 @@ struct dso {
 	enum dso_binary_type	data_type;
 	u8		 adjust_symbols:1;
 	u8		 has_build_id:1;
+	u8		 has_srcline:1;
 	u8		 hit:1;
 	u8		 annotate_warned:1;
 	u8		 sname_alloc:1;
tools/perf/util/srcline.c +8 -2
index c736d9428cf2..dcff10bed7da 100644
@@ -58,10 +58,13 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file;
 	unsigned line;
-	char *srcline = SRCLINE_UNKNOWN;
+	char *srcline;
 	char *dso_name = dso->long_name;
 	size_t size;
 
+	if (!dso->has_srcline)
+		return SRCLINE_UNKNOWN;
+
 	if (dso_name[0] == '[')
 		goto out;
 
@@ -81,8 +84,11 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
-out:
 	return srcline;
+
+out:
+	dso->has_srcline = 0;
+	return SRCLINE_UNKNOWN;
 }
 
 void free_srcline(char *srcline)
Signature · 1 line
1.7.11.7
#14 lore raw

[tip:perf/core] perf tools: Save failed result of get_srcline()

perf tools: Save failed result of get_srcline()
Commit-ID:  2cc9d0ef577975abb3ebce7d5978559ec1c73633
Gitweb:     http://git.kernel.org/tip/2cc9d0ef577975abb3ebce7d5978559ec1c73633
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:31 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 16:02:02 -0300

perf tools: Save failed result of get_srcline()

Some dso's lack srcline info, so there's no point to keep trying on them. Just save failture status and skip them.

---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  1 +
 tools/perf/util/srcline.c | 10 ++++++++--
 3 files changed, 10 insertions(+), 2 deletions(-)
tools/perf/util/dso.c +1 -0
index 6bfc8aa..af4c687c 100644
@@ -450,6 +450,7 @@ struct dso *dso__new(const char *name)
 		dso->rel = 0;
 		dso->sorted_by_name = 0;
 		dso->has_build_id = 0;
+		dso->has_srcline = 1;
 		dso->kernel = DSO_TYPE_USER;
 		dso->needs_swap = DSO_SWAP__UNSET;
 		INIT_LIST_HEAD(&dso->node);
tools/perf/util/dso.h +1 -0
index 72eedd6..9ac666a 100644
@@ -83,6 +83,7 @@ struct dso {
 	enum dso_binary_type	data_type;
 	u8		 adjust_symbols:1;
 	u8		 has_build_id:1;
+	u8		 has_srcline:1;
 	u8		 hit:1;
 	u8		 annotate_warned:1;
 	u8		 sname_alloc:1;
tools/perf/util/srcline.c +8 -2
index c736d94..dcff10b 100644
@@ -58,10 +58,13 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file;
 	unsigned line;
-	char *srcline = SRCLINE_UNKNOWN;
+	char *srcline;
 	char *dso_name = dso->long_name;
 	size_t size;
 
+	if (!dso->has_srcline)
+		return SRCLINE_UNKNOWN;
+
 	if (dso_name[0] == '[')
 		goto out;
 
@@ -81,8 +84,11 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
-out:
 	return srcline;
+
+out:
+	dso->has_srcline = 0;
+	return SRCLINE_UNKNOWN;
 }
 
 void free_srcline(char *srcline)
author maintainer
#15 lore raw

PATCH 8/9 perf tools: Implement addr2line directly using libbfd

In reply to Namhyung Kim

From: Roberto Vitillo <[email protected]>

When the srcline sort key is used , the external addr2line utility needs to be run for each hist entry to get the srcline info. This can consume quite a time if one has a huge perf.data file.

So rather than executing the external utility, implement it internally and just call it. We can do it since we've linked with libbfd already.

[[email protected]: Use a2l_data struct instead of static globals]
---
 tools/perf/config/Makefile |   4 ++
 tools/perf/util/srcline.c  | 167 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
tools/perf/config/Makefile +4 -0
index 214e17e97e5c..128d580ca397 100644
@@ -396,6 +396,10 @@ else
   endif
 endif
 
+ifndef ($(filter -lbfd,$(EXTLIBS)),)
+  CFLAGS += -DLIBBFD_SUPPORT
+endif
+
 ifndef NO_STRLCPY
   ifeq ($(call try-cc,$(SOURCE_STRLCPY),,-DHAVE_STRLCPY),y)
     CFLAGS += -DHAVE_STRLCPY
tools/perf/util/srcline.c +167 -0

179 lines not on this page · raw message

Signature · 1 line
1.7.11.7
#16 lore raw

[tip:perf/core] perf tools: Implement addr2line directly using libbfd

perf tools: Implement addr2line directly using libbfd
Commit-ID:  2f48fcd84e9e68392e29c59204a4a434311d49e9
Gitweb:     http://git.kernel.org/tip/2f48fcd84e9e68392e29c59204a4a434311d49e9
Author:     Roberto Vitillo <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:32 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 16:30:14 -0300

perf tools: Implement addr2line directly using libbfd

When the srcline sort key is used , the external addr2line utility needs to be run for each hist entry to get the srcline info. This can consume quite a time if one has a huge perf.data file.

So rather than executing the external utility, implement it internally and just call it. We can do it since we've linked with libbfd already.

Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
[ Use a2l_data struct instead of static globals ]
---
 tools/perf/config/Makefile |   4 ++
 tools/perf/util/srcline.c  | 167 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 171 insertions(+)
tools/perf/config/Makefile +4 -0
index d9bba8d..cf6ad5d 100644
@@ -523,6 +523,10 @@ ifndef NO_LIBNUMA
   endif
 endif
 
+ifndef ($(filter -lbfd,$(EXTLIBS)),)
+  CFLAGS += -DHAVE_LIBBFD_SUPPORT
+endif
+
 # Among the variables below, these:
 #   perfexecdir
 #   template_dir
tools/perf/util/srcline.c +167 -0

179 lines not on this page · raw message

author maintainer
#17 lore raw

PATCH 9/9 perf tools: Fix srcline sort key behavior

In reply to Namhyung Kim

From: Namhyung Kim <[email protected]>

Currently the srcline sort key compares ip rather than srcline info. I guess this was due to a performance reason to run external addr2line utility. Now we have implemented the functionality inside, use the srcline info when comparing hist entries.

Also constantly print "??:0" string for unknown srcline rather than printing ip.

---
 tools/perf/util/sort.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)
tools/perf/util/sort.c +20 -21
index 58cfe69d3a6c..082480579861 100644
@@ -243,33 +243,32 @@ struct sort_entry sort_sym = {
 static int64_t
 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-	return (int64_t)(right->ip - left->ip);
+	if (!left->srcline) {
+		if (!left->ms.map)
+			left->srcline = SRCLINE_UNKNOWN;
+		else {
+			struct map *map = left->ms.map;
+			left->srcline = get_srcline(map->dso,
+					    map__rip_2objdump(map, left->ip));
+		}
+	}
+	if (!right->srcline) {
+		if (!right->ms.map)
+			right->srcline = SRCLINE_UNKNOWN;
+		else {
+			struct map *map = right->ms.map;
+			right->srcline = get_srcline(map->dso,
+					    map__rip_2objdump(map, right->ip));
+		}
+	}
+	return strcmp(left->srcline, right->srcline);
 }
 
 static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					size_t size,
 					unsigned int width __maybe_unused)
 {
-	FILE *fp = NULL;
-	char *path = self->srcline;
-
-	if (path != NULL)
-		goto out_path;
-
-	if (!self->ms.map)
-		goto out_ip;
-
-	path = get_srcline(self->ms.map->dso, self->ip);
-	self->srcline = path;
-
-out_path:
-	if (fp)
-		pclose(fp);
-	return repsep_snprintf(bf, size, "%s", path);
-out_ip:
-	if (fp)
-		pclose(fp);
-	return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
+	return repsep_snprintf(bf, size, "%s", self->srcline);
 }
 
 struct sort_entry sort_srcline = {
Signature · 1 line
1.7.11.7
#18 lore raw

[tip:perf/core] perf tools: Fix srcline sort key behavior

perf tools: Fix srcline sort key behavior
Commit-ID:  4adcc43003024354b45edadfad4ea2fa205f135f
Gitweb:     http://git.kernel.org/tip/4adcc43003024354b45edadfad4ea2fa205f135f
Author:     Namhyung Kim <[email protected]>
AuthorDate: Wed, 11 Sep 2013 14:09:33 +0900
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 9 Oct 2013 17:26:42 -0300

perf tools: Fix srcline sort key behavior

Currently the srcline sort key compares ip rather than srcline info. I guess this was due to a performance reason to run external addr2line utility. Now we have implemented the functionality inside, use the srcline info when comparing hist entries.

Also constantly print "??:0" string for unknown srcline rather than printing ip.

---
 tools/perf/util/sort.c | 41 ++++++++++++++++++++---------------------
 1 file changed, 20 insertions(+), 21 deletions(-)
tools/perf/util/sort.c +20 -21
index f732120..32c5637 100644
@@ -243,33 +243,32 @@ struct sort_entry sort_sym = {
 static int64_t
 sort__srcline_cmp(struct hist_entry *left, struct hist_entry *right)
 {
-	return (int64_t)(right->ip - left->ip);
+	if (!left->srcline) {
+		if (!left->ms.map)
+			left->srcline = SRCLINE_UNKNOWN;
+		else {
+			struct map *map = left->ms.map;
+			left->srcline = get_srcline(map->dso,
+					    map__rip_2objdump(map, left->ip));
+		}
+	}
+	if (!right->srcline) {
+		if (!right->ms.map)
+			right->srcline = SRCLINE_UNKNOWN;
+		else {
+			struct map *map = right->ms.map;
+			right->srcline = get_srcline(map->dso,
+					    map__rip_2objdump(map, right->ip));
+		}
+	}
+	return strcmp(left->srcline, right->srcline);
 }
 
 static int hist_entry__srcline_snprintf(struct hist_entry *self, char *bf,
 					size_t size,
 					unsigned int width __maybe_unused)
 {
-	FILE *fp = NULL;
-	char *path = self->srcline;
-
-	if (path != NULL)
-		goto out_path;
-
-	if (!self->ms.map)
-		goto out_ip;
-
-	path = get_srcline(self->ms.map->dso, self->ip);
-	self->srcline = path;
-
-out_path:
-	if (fp)
-		pclose(fp);
-	return repsep_snprintf(bf, size, "%s", path);
-out_ip:
-	if (fp)
-		pclose(fp);
-	return repsep_snprintf(bf, size, "%-#*llx", BITS_PER_LONG / 4, self->ip);
+	return repsep_snprintf(bf, size, "%s", self->srcline);
 }
 
 struct sort_entry sort_srcline = {