Skip to contentSkip to conversation

perf script: Add an option to print the source line number

PATCH Superseded Adrian Hunter 4 participants 21 messages Dec 3, 2013 – Dec 10, 2013 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Adrian Hunter

author reviewer
#0 lore raw

cover perf script: Add an option to print the source line number

Hi

Here are some patches to add an option to perf script that will print the source file name and line number.

Adrian Hunter (7):
perf script: Do not call perf_event__preprocess_sample() twice) perf script: Add an option to print the source line number perf tools: Use asprintf instead of malloc plus snprintf perf tools: Retain bfd reference to lookup source line numbers perf tools: Retain symbol source file name to lookup source line numbers perf tools: Do not need to read symbols for source line lookup perf tools: Do not disable source line lookup just because of 1 failure

 tools/perf/Documentation/perf-script.txt |   2 +-
 tools/perf/builtin-script.c              |  40 +++++++++--
 tools/perf/util/dso.c                    |   3 +
 tools/perf/util/dso.h                    |   5 ++
 tools/perf/util/session.c                |  33 +++++----
 tools/perf/util/session.h                |   3 +-
 tools/perf/util/srcline.c                | 119 +++++++++++++------------------
 tools/perf/util/symbol.c                 |   2 +
 8 files changed, 113 insertions(+), 94 deletions(-)

Regards
Adrian

author reviewer
#1 lore raw

PATCH 1/7 perf script: Do not call perf_event__preprocess_sample() twice)

perf_event__preprocess_sample() is called in
process_sample_event(). Instead of calling it
again in perf_evsel__print_ip(), pass though
the resultant addr_location.

---
 tools/perf/builtin-script.c | 11 ++++++-----
 tools/perf/util/session.c   | 33 ++++++++++++++++-----------------
 tools/perf/util/session.h   |  3 ++-
 3 files changed, 24 insertions(+), 23 deletions(-)
tools/perf/builtin-script.c +6 -5
index 952dce9..5b865a9 100644
@@ -414,7 +414,8 @@ static void print_sample_bts(union perf_event *event,
 			     struct perf_sample *sample,
 			     struct perf_evsel *evsel,
 			     struct machine *machine,
-			     struct thread *thread)
+			     struct thread *thread,
+			     struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -424,7 +425,7 @@ static void print_sample_bts(union perf_event *event,
 			printf(" ");
 		else
 			printf("\n");
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, event, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
@@ -443,7 +444,7 @@ static void print_sample_bts(union perf_event *event,
 static void process_event(union perf_event *event, struct perf_sample *sample,
 			  struct perf_evsel *evsel, struct machine *machine,
 			  struct thread *thread,
-			  struct addr_location *al __maybe_unused)
+			  struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -458,7 +459,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 	}
 
 	if (is_bts_event(attr)) {
-		print_sample_bts(event, sample, evsel, machine, thread);
+		print_sample_bts(event, sample, evsel, machine, thread, al);
 		return;
 	}
 
@@ -474,7 +475,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 		else
 			printf("\n");
 
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, event, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
tools/perf/util/session.c +16 -17
index 4ce146b..bc821b3 100644
@@ -1487,11 +1487,12 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 	return NULL;
 }
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
+void perf_evsel__print_ip(struct perf_evsel *evsel,
+			  union perf_event *event __maybe_unused,
 			  struct perf_sample *sample, struct machine *machine,
-			  unsigned int print_opts, unsigned int stack_depth)
+			  struct addr_location *al, unsigned int print_opts,
+			  unsigned int stack_depth)
 {
-	struct addr_location al;
 	struct callchain_cursor_node *node;
 	int print_ip = print_opts & PRINT_IP_OPT_IP;
 	int print_sym = print_opts & PRINT_IP_OPT_SYM;
@@ -1500,15 +1501,10 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 	int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
 	char s = print_oneline ? ' ' : '\t';
 
-	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
-		error("problem processing %d event, skipping it.\n",
-			event->header.type);
-		return;
-	}
-
 	if (symbol_conf.use_callchain && sample->callchain) {
+		struct addr_location node_al;
 
-		if (machine__resolve_callchain(machine, evsel, al.thread,
+		if (machine__resolve_callchain(machine, evsel, al->thread,
 					       sample, NULL, NULL,
 					       PERF_MAX_STACK_DEPTH) != 0) {
 			if (verbose)
@@ -1517,6 +1513,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 		}
 		callchain_cursor_commit(&callchain_cursor);
 
+		if (print_symoffset)
+			node_al = *al;
+
 		while (stack_depth) {
 			node = callchain_cursor_current(&callchain_cursor);
 			if (!node)
@@ -1531,9 +1530,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 			if (print_sym) {
 				printf(" ");
 				if (print_symoffset) {
-					al.addr = node->ip;
-					al.map  = node->map;
-					symbol__fprintf_symname_offs(node->sym, &al, stdout);
+					node_al.addr = node->ip;
+					node_al.map  = node->map;
+					symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
 				} else
 					symbol__fprintf_symname(node->sym, stdout);
 			}
@@ -1553,7 +1552,7 @@ next:
 		}
 
 	} else {
-		if (al.sym && al.sym->ignore)
+		if (al->sym && al->sym->ignore)
 			return;
 
 		if (print_ip)
@@ -1562,15 +1561,15 @@ next:
 		if (print_sym) {
 			printf(" ");
 			if (print_symoffset)
-				symbol__fprintf_symname_offs(al.sym, &al,
+				symbol__fprintf_symname_offs(al->sym, al,
 							     stdout);
 			else
-				symbol__fprintf_symname(al.sym, stdout);
+				symbol__fprintf_symname(al->sym, stdout);
 		}
 
 		if (print_dso) {
 			printf(" (");
-			map__fprintf_dsoname(al.map, stdout);
+			map__fprintf_dsoname(al->map, stdout);
 			printf(")");
 		}
 	}
tools/perf/util/session.h +2 -1
index 50f6409..ee88500 100644
@@ -107,7 +107,8 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 
 void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 			  struct perf_sample *sample, struct machine *machine,
-			  unsigned int print_opts, unsigned int stack_depth);
+			  struct addr_location *al, unsigned int print_opts,
+			  unsigned int stack_depth);
 
 int perf_session__cpu_bitmap(struct perf_session *session,
 			     const char *cpu_list, unsigned long *cpu_bitmap);
Signature · 1 line
1.7.11.7
#2 lore raw

Re: [PATCH 1/7] perf script: Do not call perf_event__preprocess_sample() twice)

Em Tue, Dec 03, 2013 at 09:23:04AM +0200, Adrian Hunter escreveu:

Quoted 4 lines

perf_event__preprocess_sample() is called in
process_sample_event(). Instead of calling it
again in perf_evsel__print_ip(), pass though
the resultant addr_location.

<SNIP>

tools/perf/util/session.c struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, · line 1491

Quoted 8 lines

+++ b/tools/perf/util/session.c

tools/perf/util/session.c +2 -1
@@ -1487,11 +1487,12 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 	return NULL;
 }
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
+void perf_evsel__print_ip(struct perf_evsel *evsel,
+			  union perf_event *event __maybe_unused,

Why do we have to keep this parameter?

- Arnaldo

author reviewer
#3 lore raw

Re: [PATCH 1/7] perf script: Do not call perf_event__preprocess_sample() twice)

Quoted 12 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 03, 2013 at 09:23:04AM +0200, Adrian Hunter escreveu:

Quoted 4 lines

perf_event__preprocess_sample() is called in
process_sample_event(). Instead of calling it
again in perf_evsel__print_ip(), pass though
the resultant addr_location.

<SNIP>

Quoted 5 lines

+++ b/tools/perf/util/session.c

tools/perf/util/session.c +0 -0
@@ -1487,11 +1487,12 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 	return NULL;
 }
 
Quoted 5 lines

tools/perf/util/session.c struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session, · line 1491

Quoted 3 lines
tools/perf/util/session.c +2 -1
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
+void perf_evsel__print_ip(struct perf_evsel *evsel,
+			  union perf_event *event __maybe_unused,

Why do we have to keep this parameter?

You are right - it is not needed.

author reviewer
#4 lore raw

Re: [PATCH V2 1/7] perf script: Do not call perf_event__preprocess_sample() twice)

perf_event__preprocess_sample() is called in
process_sample_event(). Instead of calling it
again in perf_evsel__print_ip(), pass though
the resultant addr_location.

---

V2: removed unused 'event' parameter from perf_evsel__print_ip()

 tools/perf/builtin-script.c | 11 ++++++-----
 tools/perf/util/session.c   | 31 ++++++++++++++-----------------
 tools/perf/util/session.h   |  4 ++--
 3 files changed, 22 insertions(+), 24 deletions(-)
tools/perf/builtin-script.c +6 -5
index 952dce9..c555bdd 100644
@@ -414,7 +414,8 @@ static void print_sample_bts(union perf_event *event,
 			     struct perf_sample *sample,
 			     struct perf_evsel *evsel,
 			     struct machine *machine,
-			     struct thread *thread)
+			     struct thread *thread,
+			     struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -424,7 +425,7 @@ static void print_sample_bts(union perf_event *event,
 			printf(" ");
 		else
 			printf("\n");
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
@@ -443,7 +444,7 @@ static void print_sample_bts(union perf_event *event,
 static void process_event(union perf_event *event, struct perf_sample *sample,
 			  struct perf_evsel *evsel, struct machine *machine,
 			  struct thread *thread,
-			  struct addr_location *al __maybe_unused)
+			  struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -458,7 +459,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 	}
 
 	if (is_bts_event(attr)) {
-		print_sample_bts(event, sample, evsel, machine, thread);
+		print_sample_bts(event, sample, evsel, machine, thread, al);
 		return;
 	}
 
@@ -474,7 +475,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 		else
 			printf("\n");
 
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
tools/perf/util/session.c +14 -17
index 4ce146b..8a7da6f 100644
@@ -1487,11 +1487,10 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 	return NULL;
 }
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
-			  struct perf_sample *sample, struct machine *machine,
+void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
+			  struct machine *machine, struct addr_location *al,
 			  unsigned int print_opts, unsigned int stack_depth)
 {
-	struct addr_location al;
 	struct callchain_cursor_node *node;
 	int print_ip = print_opts & PRINT_IP_OPT_IP;
 	int print_sym = print_opts & PRINT_IP_OPT_SYM;
@@ -1500,15 +1499,10 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 	int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
 	char s = print_oneline ? ' ' : '\t';
 
-	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
-		error("problem processing %d event, skipping it.\n",
-			event->header.type);
-		return;
-	}
-
 	if (symbol_conf.use_callchain && sample->callchain) {
+		struct addr_location node_al;
 
-		if (machine__resolve_callchain(machine, evsel, al.thread,
+		if (machine__resolve_callchain(machine, evsel, al->thread,
 					       sample, NULL, NULL,
 					       PERF_MAX_STACK_DEPTH) != 0) {
 			if (verbose)
@@ -1517,6 +1511,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 		}
 		callchain_cursor_commit(&callchain_cursor);
 
+		if (print_symoffset)
+			node_al = *al;
+
 		while (stack_depth) {
 			node = callchain_cursor_current(&callchain_cursor);
 			if (!node)
@@ -1531,9 +1528,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 			if (print_sym) {
 				printf(" ");
 				if (print_symoffset) {
-					al.addr = node->ip;
-					al.map  = node->map;
-					symbol__fprintf_symname_offs(node->sym, &al, stdout);
+					node_al.addr = node->ip;
+					node_al.map  = node->map;
+					symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
 				} else
 					symbol__fprintf_symname(node->sym, stdout);
 			}
@@ -1553,7 +1550,7 @@ next:
 		}
 
 	} else {
-		if (al.sym && al.sym->ignore)
+		if (al->sym && al->sym->ignore)
 			return;
 
 		if (print_ip)
@@ -1562,15 +1559,15 @@ next:
 		if (print_sym) {
 			printf(" ");
 			if (print_symoffset)
-				symbol__fprintf_symname_offs(al.sym, &al,
+				symbol__fprintf_symname_offs(al->sym, al,
 							     stdout);
 			else
-				symbol__fprintf_symname(al.sym, stdout);
+				symbol__fprintf_symname(al->sym, stdout);
 		}
 
 		if (print_dso) {
 			printf(" (");
-			map__fprintf_dsoname(al.map, stdout);
+			map__fprintf_dsoname(al->map, stdout);
 			printf(")");
 		}
 	}
tools/perf/util/session.h +2 -2
index 50f6409..7e5d4398 100644
@@ -105,8 +105,8 @@ size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp);
 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 					    unsigned int type);
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
-			  struct perf_sample *sample, struct machine *machine,
+void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
+			  struct machine *machine, struct addr_location *al,
 			  unsigned int print_opts, unsigned int stack_depth);
 
 int perf_session__cpu_bitmap(struct perf_session *session,
Signature · 1 line
1.7.11.7
#5 lore raw

[tip:perf/core] perf script: Do not call perf_event__preprocess_sample() twice)

perf script: Do not call perf_event__preprocess_sample() twice)
Commit-ID:  a2cb3cf20e06ef119ae541c1a08dc1977f7f0fff
Gitweb:     http://git.kernel.org/tip/a2cb3cf20e06ef119ae541c1a08dc1977f7f0fff
Author:     Adrian Hunter <[email protected]>
AuthorDate: Wed, 4 Dec 2013 16:16:36 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 4 Dec 2013 15:09:53 -0300

perf script: Do not call perf_event__preprocess_sample() twice)

The perf_event__preprocess_sample() function is called in process_sample_event(). Instead of calling it again in perf_evsel__print_ip(), pass through the resultant addr_location.

Cc Andi Kleen <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/builtin-script.c | 11 ++++++-----
 tools/perf/util/session.c   | 31 ++++++++++++++-----------------
 tools/perf/util/session.h   |  4 ++--
 3 files changed, 22 insertions(+), 24 deletions(-)
tools/perf/builtin-script.c +6 -5
index 952dce9..c555bdd 100644
@@ -414,7 +414,8 @@ static void print_sample_bts(union perf_event *event,
 			     struct perf_sample *sample,
 			     struct perf_evsel *evsel,
 			     struct machine *machine,
-			     struct thread *thread)
+			     struct thread *thread,
+			     struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -424,7 +425,7 @@ static void print_sample_bts(union perf_event *event,
 			printf(" ");
 		else
 			printf("\n");
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
@@ -443,7 +444,7 @@ static void print_sample_bts(union perf_event *event,
 static void process_event(union perf_event *event, struct perf_sample *sample,
 			  struct perf_evsel *evsel, struct machine *machine,
 			  struct thread *thread,
-			  struct addr_location *al __maybe_unused)
+			  struct addr_location *al)
 {
 	struct perf_event_attr *attr = &evsel->attr;
 
@@ -458,7 +459,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 	}
 
 	if (is_bts_event(attr)) {
-		print_sample_bts(event, sample, evsel, machine, thread);
+		print_sample_bts(event, sample, evsel, machine, thread, al);
 		return;
 	}
 
@@ -474,7 +475,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 		else
 			printf("\n");
 
-		perf_evsel__print_ip(evsel, event, sample, machine,
+		perf_evsel__print_ip(evsel, sample, machine, al,
 				     output[attr->type].print_ip_opts,
 				     PERF_MAX_STACK_DEPTH);
 	}
tools/perf/util/session.c +14 -17
index 4ce146b..8a7da6f 100644
@@ -1487,11 +1487,10 @@ struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 	return NULL;
 }
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
-			  struct perf_sample *sample, struct machine *machine,
+void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
+			  struct machine *machine, struct addr_location *al,
 			  unsigned int print_opts, unsigned int stack_depth)
 {
-	struct addr_location al;
 	struct callchain_cursor_node *node;
 	int print_ip = print_opts & PRINT_IP_OPT_IP;
 	int print_sym = print_opts & PRINT_IP_OPT_SYM;
@@ -1500,15 +1499,10 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 	int print_oneline = print_opts & PRINT_IP_OPT_ONELINE;
 	char s = print_oneline ? ' ' : '\t';
 
-	if (perf_event__preprocess_sample(event, machine, &al, sample) < 0) {
-		error("problem processing %d event, skipping it.\n",
-			event->header.type);
-		return;
-	}
-
 	if (symbol_conf.use_callchain && sample->callchain) {
+		struct addr_location node_al;
 
-		if (machine__resolve_callchain(machine, evsel, al.thread,
+		if (machine__resolve_callchain(machine, evsel, al->thread,
 					       sample, NULL, NULL,
 					       PERF_MAX_STACK_DEPTH) != 0) {
 			if (verbose)
@@ -1517,6 +1511,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 		}
 		callchain_cursor_commit(&callchain_cursor);
 
+		if (print_symoffset)
+			node_al = *al;
+
 		while (stack_depth) {
 			node = callchain_cursor_current(&callchain_cursor);
 			if (!node)
@@ -1531,9 +1528,9 @@ void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
 			if (print_sym) {
 				printf(" ");
 				if (print_symoffset) {
-					al.addr = node->ip;
-					al.map  = node->map;
-					symbol__fprintf_symname_offs(node->sym, &al, stdout);
+					node_al.addr = node->ip;
+					node_al.map  = node->map;
+					symbol__fprintf_symname_offs(node->sym, &node_al, stdout);
 				} else
 					symbol__fprintf_symname(node->sym, stdout);
 			}
@@ -1553,7 +1550,7 @@ next:
 		}
 
 	} else {
-		if (al.sym && al.sym->ignore)
+		if (al->sym && al->sym->ignore)
 			return;
 
 		if (print_ip)
@@ -1562,15 +1559,15 @@ next:
 		if (print_sym) {
 			printf(" ");
 			if (print_symoffset)
-				symbol__fprintf_symname_offs(al.sym, &al,
+				symbol__fprintf_symname_offs(al->sym, al,
 							     stdout);
 			else
-				symbol__fprintf_symname(al.sym, stdout);
+				symbol__fprintf_symname(al->sym, stdout);
 		}
 
 		if (print_dso) {
 			printf(" (");
-			map__fprintf_dsoname(al.map, stdout);
+			map__fprintf_dsoname(al->map, stdout);
 			printf(")");
 		}
 	}
tools/perf/util/session.h +2 -2
index 50f6409..7e5d4398 100644
@@ -105,8 +105,8 @@ size_t perf_session__fprintf_nr_events(struct perf_session *session, FILE *fp);
 struct perf_evsel *perf_session__find_first_evtype(struct perf_session *session,
 					    unsigned int type);
 
-void perf_evsel__print_ip(struct perf_evsel *evsel, union perf_event *event,
-			  struct perf_sample *sample, struct machine *machine,
+void perf_evsel__print_ip(struct perf_evsel *evsel, struct perf_sample *sample,
+			  struct machine *machine, struct addr_location *al,
 			  unsigned int print_opts, unsigned int stack_depth);
 
 int perf_session__cpu_bitmap(struct perf_session *session,
author reviewer
#6 lore raw

PATCH 2/7 perf script: Add an option to print the source line number

In reply to Adrian Hunter

Add field 'srcline' that displays the source file name and line number associated with the sample ip. The information displayed is the same as from addr2line.

---
 tools/perf/Documentation/perf-script.txt |  2 +-
 tools/perf/builtin-script.c              | 29 +++++++++++++++++++++++++++--
 2 files changed, 28 insertions(+), 3 deletions(-)
tools/perf/Documentation/perf-script.txt +1 -1
index cfdbb1e..c2a5071 100644
@@ -115,7 +115,7 @@ OPTIONS
 -f::
 --fields::
         Comma separated list of fields to print. Options are:
-        comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff.
+        comm, tid, pid, time, cpu, event, trace, ip, sym, dso, addr, symoff, srcline.
         Field list can be prepended with the type, trace, sw or hw,
         to indicate to which event type the field list applies.
         e.g., -f sw:comm,tid,time,ip,sym  and -f trace:time,cpu,trace
tools/perf/builtin-script.c +27 -2
index 5b865a9..9d0febc 100644
@@ -43,6 +43,7 @@ enum perf_output_field {
 	PERF_OUTPUT_DSO             = 1U << 9,
 	PERF_OUTPUT_ADDR            = 1U << 10,
 	PERF_OUTPUT_SYMOFFSET       = 1U << 11,
+	PERF_OUTPUT_SRCLINE         = 1U << 12,
 };
 
 struct output_option {
@@ -61,6 +62,7 @@ struct output_option {
 	{.str = "dso",   .field = PERF_OUTPUT_DSO},
 	{.str = "addr",  .field = PERF_OUTPUT_ADDR},
 	{.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
+	{.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
 };
 
 /* default set to maintain compatibility with current format */
@@ -210,6 +212,11 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
 		       "to DSO.\n");
 		return -EINVAL;
 	}
+	if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
+		pr_err("Display of source line number requested but sample IP is not\n"
+		       "selected. Hence, no address to lookup the source line number.\n");
+		return -EINVAL;
+	}
 
 	if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
 		perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
@@ -348,6 +355,24 @@ static void print_sample_start(struct perf_sample *sample,
 	}
 }
 
+static void print_sample_finish(struct perf_evsel *evsel,
+				struct addr_location *al)
+{
+	struct perf_event_attr *attr = &evsel->attr;
+
+	if (PRINT_FIELD(SRCLINE) && al->map && al->map->dso) {
+		char *srcline;
+
+		srcline = get_srcline(al->map->dso,
+				      map__rip_2objdump(al->map, al->addr));
+		if (srcline != SRCLINE_UNKNOWN)
+			printf("\n  %s", srcline);
+		free_srcline(srcline);
+	}
+
+	printf("\n");
+}
+
 static bool is_bts_event(struct perf_event_attr *attr)
 {
 	return ((attr->type == PERF_TYPE_HARDWARE) &&
@@ -438,7 +463,7 @@ static void print_sample_bts(union perf_event *event,
 	     !output[attr->type].user_set))
 		print_sample_addr(event, sample, machine, thread, attr);
 
-	printf("\n");
+	print_sample_finish(evsel, al);
 }
 
 static void process_event(union perf_event *event, struct perf_sample *sample,
@@ -480,7 +505,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 				     PERF_MAX_STACK_DEPTH);
 	}
 
-	printf("\n");
+	print_sample_finish(evsel, al);
 }
 
 static int default_start_script(const char *script __maybe_unused,
Signature · 1 line
1.7.11.7
#7 lore raw
Quoted 35 lines · Adrian Hunter
tools/perf/builtin-script.c +19 -1
@@ -348,6 +355,24 @@ static void print_sample_start(struct perf_sample *sample,
  	}
  }
 
+static void print_sample_finish(struct perf_evsel *evsel,
+				struct addr_location *al)
+{
+	struct perf_event_attr *attr = &evsel->attr;
+
+	if (PRINT_FIELD(SRCLINE) && al->map && al->map->dso) {
+		char *srcline;
+
+		srcline = get_srcline(al->map->dso,
+				      map__rip_2objdump(al->map, al->addr));
+		if (srcline != SRCLINE_UNKNOWN)
+			printf("\n  %s", srcline);
+		free_srcline(srcline);
+	}
+
+	printf("\n");
+}
+
  static bool is_bts_event(struct perf_event_attr *attr)
  {
  	return ((attr->type == PERF_TYPE_HARDWARE) &&
@@ -438,7 +463,7 @@ static void print_sample_bts(union perf_event *event,
  	     !output[attr->type].user_set))
  		print_sample_addr(event, sample, machine, thread, attr);
 
-	printf("\n");
+	print_sample_finish(evsel, al);
  }
 
  static void process_event(union perf_event *event, struct perf_sample *sample,

tools/perf/builtin-script.c static void process_event(union perf_event *event, struct perf_sample *sample, · line 511

Quoted 8 lines
tools/perf/builtin-script.c +1 -1
@@ -480,7 +505,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
  				     PERF_MAX_STACK_DEPTH);
  	}
 
-	printf("\n");
+	print_sample_finish(evsel, al);
  }
 
  static int default_start_script(const char *script __maybe_unused,

What about callchains?

David

#8 lore raw
Quoted 1 line · David Ahern

What about callchains?

I think you can handle callcahins and IP by moving the srcline printing into perf_evsel__print_ip() and adding a PRINT_IP_OPT_SRCLINE option.

David

author reviewer
#9 lore raw

PATCH 3/7 perf tools: Use asprintf instead of malloc plus snprintf

In reply to Adrian Hunter

asprintf is equivalent to malloc plus snprintf so
use it because it is simpler.

---
 tools/perf/util/srcline.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
tools/perf/util/srcline.c +1 -8
index d11aefb..4c8e816 100644
@@ -227,7 +227,6 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	unsigned line = 0;
 	char *srcline;
 	char *dso_name = dso->long_name;
-	size_t size;
 
 	if (!dso->has_srcline)
 		return SRCLINE_UNKNOWN;
@@ -241,13 +240,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!addr2line(dso_name, addr, &file, &line))
 		goto out;
 
-	/* just calculate actual length */
-	size = snprintf(NULL, 0, "%s:%u", file, line) + 1;
-
-	srcline = malloc(size);
-	if (srcline)
-		snprintf(srcline, size, "%s:%u", file, line);
-	else
+	if (asprintf(&srcline, "%s:%u", file, line) < 0)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
Signature · 1 line
1.7.11.7
#10 lore raw

[tip:perf/core] perf tools: Use asprintf instead of malloc plus snprintf

perf tools: Use asprintf instead of malloc plus snprintf
Commit-ID:  d88938ebc97df72320609b33aa0cf9a9a98accb1
Gitweb:     http://git.kernel.org/tip/d88938ebc97df72320609b33aa0cf9a9a98accb1
Author:     Adrian Hunter <[email protected]>
AuthorDate: Tue, 3 Dec 2013 09:23:06 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 4 Dec 2013 13:46:36 -0300

perf tools: Use asprintf instead of malloc plus snprintf

The asprintf library function is equivalent to malloc plus snprintf so use it because it is simpler.

Cc Andi Kleen <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/srcline.c | 9 +--------
 1 file changed, 1 insertion(+), 8 deletions(-)
tools/perf/util/srcline.c +1 -8
index d11aefb..4c8e816 100644
@@ -227,7 +227,6 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	unsigned line = 0;
 	char *srcline;
 	char *dso_name = dso->long_name;
-	size_t size;
 
 	if (!dso->has_srcline)
 		return SRCLINE_UNKNOWN;
@@ -241,13 +240,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!addr2line(dso_name, addr, &file, &line))
 		goto out;
 
-	/* just calculate actual length */
-	size = snprintf(NULL, 0, "%s:%u", file, line) + 1;
-
-	srcline = malloc(size);
-	if (srcline)
-		snprintf(srcline, size, "%s:%u", file, line);
-	else
+	if (asprintf(&srcline, "%s:%u", file, line) < 0)
 		srcline = SRCLINE_UNKNOWN;
 
 	free(file);
author reviewer
#11 lore raw

PATCH 4/7 perf tools: Retain bfd reference to lookup source line numbers

In reply to Adrian Hunter

Closng and re-opening for every lookup when using
libbfd to lookup source file name and line number
is very very slow. Instead keep the reference on
struct dso.

---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  3 +++
 tools/perf/util/srcline.c | 36 ++++++++++++++++++++++++++++++------
 3 files changed, 34 insertions(+), 6 deletions(-)
tools/perf/util/dso.c +1 -0
index af4c687c..68aa55a 100644
@@ -469,6 +469,7 @@ void dso__delete(struct dso *dso)
 	if (dso->lname_alloc)
 		free(dso->long_name);
 	dso_cache__free(&dso->cache);
+	dso__free_a2l(dso);
 	free(dso);
 }
 
tools/perf/util/dso.h +3 -0
index 9ac666a..d8613dc 100644
@@ -77,6 +77,7 @@ struct dso {
 	struct rb_root	 symbols[MAP__NR_TYPES];
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
 	struct rb_root	 cache;
+	void		 *a2l;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
@@ -166,4 +167,6 @@ static inline bool dso__is_kcore(struct dso *dso)
 	       dso->data_type == DSO_BINARY_TYPE__GUEST_KCORE;
 }
 
+void dso__free_a2l(struct dso *dso);
+
 #endif /* __PERF_DSO */
tools/perf/util/srcline.c +30 -6
index 4c8e816..25b85b2 100644
@@ -146,18 +146,24 @@ static void addr2line_cleanup(struct a2l_data *a2l)
 }
 
 static int addr2line(const char *dso_name, unsigned long addr,
-		     char **file, unsigned int *line)
+		     char **file, unsigned int *line, struct dso *dso)
 {
 	int ret = 0;
-	struct a2l_data *a2l;
+	struct a2l_data *a2l = dso->a2l;
+
+	if (!a2l) {
+		dso->a2l = addr2line_init(dso_name);
+		a2l = dso->a2l;
+	}
 
-	a2l = addr2line_init(dso_name);
 	if (a2l == NULL) {
 		pr_warning("addr2line_init failed for %s\n", dso_name);
 		return 0;
 	}
 
 	a2l->addr = addr;
+	a2l->found = false;
+
 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
 
 	if (a2l->found && a2l->filename) {
@@ -168,14 +174,26 @@ static int addr2line(const char *dso_name, unsigned long addr,
 			ret = 1;
 	}
 
-	addr2line_cleanup(a2l);
 	return ret;
 }
 
+void dso__free_a2l(struct dso *dso)
+{
+	struct a2l_data *a2l = dso->a2l;
+
+	if (!a2l)
+		return;
+
+	addr2line_cleanup(a2l);
+
+	dso->a2l = NULL;
+}
+
 #else /* HAVE_LIBBFD_SUPPORT */
 
 static int addr2line(const char *dso_name, unsigned long addr,
-		     char **file, unsigned int *line_nr)
+		     char **file, unsigned int *line_nr,
+		     struct dso *dso __maybe_unused)
 {
 	FILE *fp;
 	char cmd[PATH_MAX];
@@ -219,6 +237,11 @@ out:
 	pclose(fp);
 	return ret;
 }
+
+void dso__free_a2l(struct dso *dso __maybe_unused)
+{
+}
+
 #endif /* HAVE_LIBBFD_SUPPORT */
 
 char *get_srcline(struct dso *dso, unsigned long addr)
@@ -237,7 +260,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!strncmp(dso_name, "/tmp/perf-", 10))
 		goto out;
 
-	if (!addr2line(dso_name, addr, &file, &line))
+	if (!addr2line(dso_name, addr, &file, &line, dso))
 		goto out;
 
 	if (asprintf(&srcline, "%s:%u", file, line) < 0)
@@ -248,6 +271,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 
 out:
 	dso->has_srcline = 0;
+	dso__free_a2l(dso);
 	return SRCLINE_UNKNOWN;
 }
 
Signature · 1 line
1.7.11.7
#12 lore raw

[tip:perf/core] perf symbols: Retain bfd reference to lookup source line numbers

perf symbols: Retain bfd reference to lookup source line numbers
Commit-ID:  454ff00f969e515c4cbfd52718ec5e01c7d9aeef
Gitweb:     http://git.kernel.org/tip/454ff00f969e515c4cbfd52718ec5e01c7d9aeef
Author:     Adrian Hunter <[email protected]>
AuthorDate: Tue, 3 Dec 2013 09:23:07 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 4 Dec 2013 13:46:36 -0300

perf symbols: Retain bfd reference to lookup source line numbers

Closng and re-opening for every lookup when using libbfd to lookup source file name and line number is very very slow. Instead keep the reference on struct dso.

Cc Andi Kleen <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  3 +++
 tools/perf/util/srcline.c | 36 ++++++++++++++++++++++++++++++------
 3 files changed, 34 insertions(+), 6 deletions(-)
tools/perf/util/dso.c +1 -0
index af4c687c..68aa55a 100644
@@ -469,6 +469,7 @@ void dso__delete(struct dso *dso)
 	if (dso->lname_alloc)
 		free(dso->long_name);
 	dso_cache__free(&dso->cache);
+	dso__free_a2l(dso);
 	free(dso);
 }
 
tools/perf/util/dso.h +3 -0
index 9ac666a..d8613dc 100644
@@ -77,6 +77,7 @@ struct dso {
 	struct rb_root	 symbols[MAP__NR_TYPES];
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
 	struct rb_root	 cache;
+	void		 *a2l;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
@@ -166,4 +167,6 @@ static inline bool dso__is_kcore(struct dso *dso)
 	       dso->data_type == DSO_BINARY_TYPE__GUEST_KCORE;
 }
 
+void dso__free_a2l(struct dso *dso);
+
 #endif /* __PERF_DSO */
tools/perf/util/srcline.c +30 -6
index 4c8e816..25b85b2 100644
@@ -146,18 +146,24 @@ static void addr2line_cleanup(struct a2l_data *a2l)
 }
 
 static int addr2line(const char *dso_name, unsigned long addr,
-		     char **file, unsigned int *line)
+		     char **file, unsigned int *line, struct dso *dso)
 {
 	int ret = 0;
-	struct a2l_data *a2l;
+	struct a2l_data *a2l = dso->a2l;
+
+	if (!a2l) {
+		dso->a2l = addr2line_init(dso_name);
+		a2l = dso->a2l;
+	}
 
-	a2l = addr2line_init(dso_name);
 	if (a2l == NULL) {
 		pr_warning("addr2line_init failed for %s\n", dso_name);
 		return 0;
 	}
 
 	a2l->addr = addr;
+	a2l->found = false;
+
 	bfd_map_over_sections(a2l->abfd, find_address_in_section, a2l);
 
 	if (a2l->found && a2l->filename) {
@@ -168,14 +174,26 @@ static int addr2line(const char *dso_name, unsigned long addr,
 			ret = 1;
 	}
 
-	addr2line_cleanup(a2l);
 	return ret;
 }
 
+void dso__free_a2l(struct dso *dso)
+{
+	struct a2l_data *a2l = dso->a2l;
+
+	if (!a2l)
+		return;
+
+	addr2line_cleanup(a2l);
+
+	dso->a2l = NULL;
+}
+
 #else /* HAVE_LIBBFD_SUPPORT */
 
 static int addr2line(const char *dso_name, unsigned long addr,
-		     char **file, unsigned int *line_nr)
+		     char **file, unsigned int *line_nr,
+		     struct dso *dso __maybe_unused)
 {
 	FILE *fp;
 	char cmd[PATH_MAX];
@@ -219,6 +237,11 @@ out:
 	pclose(fp);
 	return ret;
 }
+
+void dso__free_a2l(struct dso *dso __maybe_unused)
+{
+}
+
 #endif /* HAVE_LIBBFD_SUPPORT */
 
 char *get_srcline(struct dso *dso, unsigned long addr)
@@ -237,7 +260,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!strncmp(dso_name, "/tmp/perf-", 10))
 		goto out;
 
-	if (!addr2line(dso_name, addr, &file, &line))
+	if (!addr2line(dso_name, addr, &file, &line, dso))
 		goto out;
 
 	if (asprintf(&srcline, "%s:%u", file, line) < 0)
@@ -248,6 +271,7 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 
 out:
 	dso->has_srcline = 0;
+	dso__free_a2l(dso);
 	return SRCLINE_UNKNOWN;
 }
 
author reviewer
#13 lore raw

PATCH 5/7 perf tools: Retain symbol source file name to lookup source line numbers

In reply to Adrian Hunter

Currently, lookup of an ip's source file name and line number is done using the dso file name. Instead retain the file name used to lookup the dso's symbols and use that.

---
 tools/perf/util/dso.c     | 1 +
 tools/perf/util/dso.h     | 1 +
 tools/perf/util/srcline.c | 7 ++++++-
 tools/perf/util/symbol.c  | 2 ++
 4 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +1 -0
index 68aa55a..49da968 100644
@@ -470,6 +470,7 @@ void dso__delete(struct dso *dso)
 		free(dso->long_name);
 	dso_cache__free(&dso->cache);
 	dso__free_a2l(dso);
+	free(dso->symsrc_filename);
 	free(dso);
 }
 
tools/perf/util/dso.h +1 -0
index d8613dc..7142e52 100644
@@ -78,6 +78,7 @@ struct dso {
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
 	struct rb_root	 cache;
 	void		 *a2l;
+	char		 *symsrc_filename;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
tools/perf/util/srcline.c +6 -1
index 25b85b2..93795f9 100644
@@ -249,11 +249,16 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	char *file = NULL;
 	unsigned line = 0;
 	char *srcline;
-	char *dso_name = dso->long_name;
+	char *dso_name;
 
 	if (!dso->has_srcline)
 		return SRCLINE_UNKNOWN;
 
+	if (dso->symsrc_filename)
+		dso_name = dso->symsrc_filename;
+	else
+		dso_name = dso->long_name;
+
 	if (dso_name[0] == '[')
 		goto out;
 
tools/perf/util/symbol.c +2 -0
index 360eefe..de87dba 100644
@@ -1336,6 +1336,8 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
 		if (!syms_ss && symsrc__has_symtab(ss)) {
 			syms_ss = ss;
 			next_slot = true;
+			if (!dso->symsrc_filename)
+				dso->symsrc_filename = strdup(name);
 		}
 
 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
Signature · 1 line
1.7.11.7
#14 lore raw

[tip:perf/core] perf symbols: Retain symbol source file name to lookup source line numbers

perf symbols: Retain symbol source file name to lookup source line numbers
Commit-ID:  0058aef65eda9c9dde8253af702d542ba7eef697
Gitweb:     http://git.kernel.org/tip/0058aef65eda9c9dde8253af702d542ba7eef697
Author:     Adrian Hunter <[email protected]>
AuthorDate: Tue, 3 Dec 2013 09:23:08 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 4 Dec 2013 13:46:36 -0300

perf symbols: Retain symbol source file name to lookup source line numbers

Currently, lookup of an ip's source file name and line number is done using the dso file name.

Instead retain the file name used to lookup the dso's symbols and use that.

Cc Andi Kleen <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/dso.c     | 1 +
 tools/perf/util/dso.h     | 1 +
 tools/perf/util/srcline.c | 7 ++++++-
 tools/perf/util/symbol.c  | 2 ++
 4 files changed, 10 insertions(+), 1 deletion(-)
tools/perf/util/dso.c +1 -0
index 68aa55a..49da968 100644
@@ -470,6 +470,7 @@ void dso__delete(struct dso *dso)
 		free(dso->long_name);
 	dso_cache__free(&dso->cache);
 	dso__free_a2l(dso);
+	free(dso->symsrc_filename);
 	free(dso);
 }
 
tools/perf/util/dso.h +1 -0
index d8613dc..7142e52 100644
@@ -78,6 +78,7 @@ struct dso {
 	struct rb_root	 symbol_names[MAP__NR_TYPES];
 	struct rb_root	 cache;
 	void		 *a2l;
+	char		 *symsrc_filename;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
tools/perf/util/srcline.c +6 -1
index 25b85b2..93795f9 100644
@@ -249,11 +249,16 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	char *file = NULL;
 	unsigned line = 0;
 	char *srcline;
-	char *dso_name = dso->long_name;
+	char *dso_name;
 
 	if (!dso->has_srcline)
 		return SRCLINE_UNKNOWN;
 
+	if (dso->symsrc_filename)
+		dso_name = dso->symsrc_filename;
+	else
+		dso_name = dso->long_name;
+
 	if (dso_name[0] == '[')
 		goto out;
 
tools/perf/util/symbol.c +2 -0
index 360eefe..de87dba 100644
@@ -1336,6 +1336,8 @@ int dso__load(struct dso *dso, struct map *map, symbol_filter_t filter)
 		if (!syms_ss && symsrc__has_symtab(ss)) {
 			syms_ss = ss;
 			next_slot = true;
+			if (!dso->symsrc_filename)
+				dso->symsrc_filename = strdup(name);
 		}
 
 		if (!runtime_ss && symsrc__possibly_runtime(ss)) {
author reviewer
#15 lore raw

PATCH 6/7 perf tools: Do not need to read symbols for source line lookup

In reply to Adrian Hunter

When using libbfd to lookup source filename and line number, libbfd does not need the symbol table. Removing the symbol table reading, doubles the speed of 'perf script' with the 'srcline' option.

---
 tools/perf/util/srcline.c | 53 +----------------------------------------------
 1 file changed, 1 insertion(+), 52 deletions(-)
tools/perf/util/srcline.c +1 -52
index 93795f9..e09c9e0 100644
@@ -29,54 +29,6 @@ struct a2l_data {
 	asymbol 	**syms;
 };
 
-static int bfd_error(const char *string)
-{
-	const char *errmsg;
-
-	errmsg = bfd_errmsg(bfd_get_error());
-	fflush(stdout);
-
-	if (string)
-		pr_debug("%s: %s\n", string, errmsg);
-	else
-		pr_debug("%s\n", errmsg);
-
-	return -1;
-}
-
-static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
-{
-	long storage;
-	long symcount;
-	asymbol **syms;
-	bfd_boolean dynamic = FALSE;
-
-	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
-		return bfd_error(bfd_get_filename(abfd));
-
-	storage = bfd_get_symtab_upper_bound(abfd);
-	if (storage == 0L) {
-		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
-		dynamic = TRUE;
-	}
-	if (storage < 0L)
-		return bfd_error(bfd_get_filename(abfd));
-
-	syms = malloc(storage);
-	if (dynamic)
-		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
-	else
-		symcount = bfd_canonicalize_symtab(abfd, syms);
-
-	if (symcount < 0) {
-		free(syms);
-		return bfd_error(bfd_get_filename(abfd));
-	}
-
-	a2l->syms = syms;
-	return 0;
-}
-
 static void find_address_in_section(bfd *abfd, asection *section, void *data)
 {
 	bfd_vma pc, vma;
@@ -96,7 +48,7 @@ static void find_address_in_section(bfd *abfd, asection *section, void *data)
 	if (pc < vma || pc >= vma + size)
 		return;
 
-	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
+	a2l->found = bfd_find_nearest_line(abfd, section, NULL, pc - vma,
 					   &a2l->filename, &a2l->funcname,
 					   &a2l->line);
 }
@@ -122,9 +74,6 @@ static struct a2l_data *addr2line_init(const char *path)
 	if (a2l->input == NULL)
 		goto out;
 
-	if (slurp_symtab(abfd, a2l))
-		goto out;
-
 	return a2l;
 
 out:
Signature · 1 line
1.7.11.7
#16 lore raw

Re: [PATCH 6/7] perf tools: Do not need to read symbols for source line lookup

Em Tue, Dec 03, 2013 at 09:23:09AM +0200, Adrian Hunter escreveu:

Quoted 4 lines

When using libbfd to lookup source filename and line number, libbfd does not need the symbol table. Removing the symbol table reading, doubles the speed of 'perf script' with the 'srcline' option.

[root@zoo ~]# gdb perf
run report -s srcline

b00000m

bt

#0  0x000000000050ac60 in read_unsigned_leb128 ()
#1  0x00000000005189f7 in find_abstract_instance_name.isra.20 ()
#2  0x00000000005190e1 in scan_unit_for_symbols ()
#3  0x0000000000519a69 in comp_unit_find_nearest_line ()
#4  0x000000000051a6ff in find_line.part.26 ()
#5  0x0000000000531063 in _bfd_elf_find_nearest_line_discriminator ()
#6  0x000000000053117b in _bfd_elf_find_nearest_line ()
#7  0x00000000004bf22d in find_address_in_section (abfd=0x39de5f0, section=0x3a32c38, data=0x3901950) at util/srcline.c:51
#8  0x000000000050ccdc in bfd_map_over_sections ()
#9  0x00000000004bf48b in addr2line (dso_name=0x39fb830 "/lib/modules/3.13.0-rc1+/kernel/drivers/gpu/drm/drm.ko", addr=20832, 
    file=0x7fffffffcc00, line=0x7fffffffcbfc, dso=0x90f9e0) at util/srcline.c:116

#10 0x00000000004bf608 in get_srcline (dso=0x90f9e0, addr=20832) at util/srcline.c:217
#11 0x00000000004b00fe in sort__srcline_cmp (left=0x1dc76f0, right=0x7fffffffcd70) at util/sort.c:268 #12 0x00000000004b3942 in hist_entry__cmp (left=0x1dc76f0, right=0x7fffffffcd70) at util/hist.c:450
#13 0x00000000004b35f4 in add_hist_entry (hists=0x90f278, entry=0x7fffffffcd70, al=0x7fffffffcf90, period=399301, weight=0)

    at util/hist.c:367
#14 0x00000000004b38d8 in __hists__add_entry (hists=0x90f278, al=0x7fffffffcf90, sym_parent=0x0, bi=0x0, mi=0x0, period=399301, 
    weight=0, transaction=0) at util/hist.c:440
#15 0x000000000042c9da in perf_evsel__add_hist_entry (tool=0x7fffffffd510, evsel=0x90f1d0, al=0x7fffffffcf90, sample=0x7fffffffd140, 
    machine=0x90e6b8) at builtin-report.c:274
#16 0x000000000042cd4f in process_sample_event (tool=0x7fffffffd510, event=0x7ffff17aea30, sample=0x7fffffffd140, evsel=0x90f1d0, 
    machine=0x90e6b8) at builtin-report.c:347
#17 0x000000000049df43 in perf_session__deliver_sample (session=0x90e5e0, tool=0x7fffffffd510, event=0x7ffff17aea30, 
    sample=0x7fffffffd140, evsel=0x90f1d0, machine=0x90e6b8) at util/session.c:930
#18 0x000000000049e107 in perf_session_deliver_event (session=0x90e5e0, event=0x7ffff17aea30, sample=0x7fffffffd140, 
    tool=0x7fffffffd510, file_offset=821808) at util/session.c:984
#19 0x000000000049cfb4 in flush_sample_queue (s=0x90e5e0, tool=0x7fffffffd510) at util/session.c:526
#20 0x000000000049ef53 in __perf_session__process_events (session=0x90e5e0, data_offset=216, data_size=832400, file_size=832616, 
    tool=0x7fffffffd510) at util/session.c:1371

#21 0x000000000049f02e in perf_session__process_events (session=0x90e5e0, tool=0x7fffffffd510) at util/session.c:1389 #22 0x000000000042d605 in __cmd_report (rep=0x7fffffffd510) at builtin-report.c:527
---Type <return> to continue, or q <return> to quit---

Applied all except this one and the other one David commented on, waiting for that discussion to reach some conclusion.

But with the ones applied 'perf report -s srcline' is _much_ faster, thanks!

- Arnaldo

Quoted 85 lines
---
 tools/perf/util/srcline.c | 53 +----------------------------------------------
 1 file changed, 1 insertion(+), 52 deletions(-)
tools/perf/util/srcline.c +1 -52
index 93795f9..e09c9e0 100644
@@ -29,54 +29,6 @@ struct a2l_data {
 	asymbol 	**syms;
 };
 
-static int bfd_error(const char *string)
-{
-	const char *errmsg;
-
-	errmsg = bfd_errmsg(bfd_get_error());
-	fflush(stdout);
-
-	if (string)
-		pr_debug("%s: %s\n", string, errmsg);
-	else
-		pr_debug("%s\n", errmsg);
-
-	return -1;
-}
-
-static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
-{
-	long storage;
-	long symcount;
-	asymbol **syms;
-	bfd_boolean dynamic = FALSE;
-
-	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
-		return bfd_error(bfd_get_filename(abfd));
-
-	storage = bfd_get_symtab_upper_bound(abfd);
-	if (storage == 0L) {
-		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
-		dynamic = TRUE;
-	}
-	if (storage < 0L)
-		return bfd_error(bfd_get_filename(abfd));
-
-	syms = malloc(storage);
-	if (dynamic)
-		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
-	else
-		symcount = bfd_canonicalize_symtab(abfd, syms);
-
-	if (symcount < 0) {
-		free(syms);
-		return bfd_error(bfd_get_filename(abfd));
-	}
-
-	a2l->syms = syms;
-	return 0;
-}
-
 static void find_address_in_section(bfd *abfd, asection *section, void *data)
 {
 	bfd_vma pc, vma;
@@ -96,7 +48,7 @@ static void find_address_in_section(bfd *abfd, asection *section, void *data)
 	if (pc < vma || pc >= vma + size)
 		return;
 
-	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
+	a2l->found = bfd_find_nearest_line(abfd, section, NULL, pc - vma,
 					   &a2l->filename, &a2l->funcname,
 					   &a2l->line);
 }
@@ -122,9 +74,6 @@ static struct a2l_data *addr2line_init(const char *path)
 	if (a2l->input == NULL)
 		goto out;
 
-	if (slurp_symtab(abfd, a2l))
-		goto out;
-
 	return a2l;
 
 out:
Signature · 1 line
1.7.11.7
author reviewer
#17 lore raw

Re: [PATCH 6/7] perf tools: Do not need to read symbols for source line lookup

Quoted 10 lines · Arnaldo Carvalho de Melo

Em Tue, Dec 03, 2013 at 09:23:09AM +0200, Adrian Hunter escreveu:

Quoted 4 lines

When using libbfd to lookup source filename and line number, libbfd does not need the symbol table. Removing the symbol table reading, doubles the speed of 'perf script' with the 'srcline' option.

[root@zoo ~]# gdb perf
run report -s srcline

b00000m

I wasn't able to reproduce this but I noticed the line numbers were not found for kernel modules with this patch - so it seems some binaries need the symbol information and some don't.

So please disregard this patch, sorry for your trouble.

Quoted 130 lines

bt

#0  0x000000000050ac60 in read_unsigned_leb128 ()
#1  0x00000000005189f7 in find_abstract_instance_name.isra.20 ()
#2  0x00000000005190e1 in scan_unit_for_symbols ()
#3  0x0000000000519a69 in comp_unit_find_nearest_line ()
#4  0x000000000051a6ff in find_line.part.26 ()
#5  0x0000000000531063 in _bfd_elf_find_nearest_line_discriminator ()
#6  0x000000000053117b in _bfd_elf_find_nearest_line ()
#7  0x00000000004bf22d in find_address_in_section (abfd=0x39de5f0, section=0x3a32c38, data=0x3901950) at util/srcline.c:51
#8  0x000000000050ccdc in bfd_map_over_sections ()
#9  0x00000000004bf48b in addr2line (dso_name=0x39fb830 "/lib/modules/3.13.0-rc1+/kernel/drivers/gpu/drm/drm.ko", addr=20832, 
    file=0x7fffffffcc00, line=0x7fffffffcbfc, dso=0x90f9e0) at util/srcline.c:116

#10 0x00000000004bf608 in get_srcline (dso=0x90f9e0, addr=20832) at util/srcline.c:217
#11 0x00000000004b00fe in sort__srcline_cmp (left=0x1dc76f0, right=0x7fffffffcd70) at util/sort.c:268 #12 0x00000000004b3942 in hist_entry__cmp (left=0x1dc76f0, right=0x7fffffffcd70) at util/hist.c:450
#13 0x00000000004b35f4 in add_hist_entry (hists=0x90f278, entry=0x7fffffffcd70, al=0x7fffffffcf90, period=399301, weight=0)

    at util/hist.c:367
#14 0x00000000004b38d8 in __hists__add_entry (hists=0x90f278, al=0x7fffffffcf90, sym_parent=0x0, bi=0x0, mi=0x0, period=399301, 
    weight=0, transaction=0) at util/hist.c:440
#15 0x000000000042c9da in perf_evsel__add_hist_entry (tool=0x7fffffffd510, evsel=0x90f1d0, al=0x7fffffffcf90, sample=0x7fffffffd140, 
    machine=0x90e6b8) at builtin-report.c:274
#16 0x000000000042cd4f in process_sample_event (tool=0x7fffffffd510, event=0x7ffff17aea30, sample=0x7fffffffd140, evsel=0x90f1d0, 
    machine=0x90e6b8) at builtin-report.c:347
#17 0x000000000049df43 in perf_session__deliver_sample (session=0x90e5e0, tool=0x7fffffffd510, event=0x7ffff17aea30, 
    sample=0x7fffffffd140, evsel=0x90f1d0, machine=0x90e6b8) at util/session.c:930
#18 0x000000000049e107 in perf_session_deliver_event (session=0x90e5e0, event=0x7ffff17aea30, sample=0x7fffffffd140, 
    tool=0x7fffffffd510, file_offset=821808) at util/session.c:984
#19 0x000000000049cfb4 in flush_sample_queue (s=0x90e5e0, tool=0x7fffffffd510) at util/session.c:526
#20 0x000000000049ef53 in __perf_session__process_events (session=0x90e5e0, data_offset=216, data_size=832400, file_size=832616, 
    tool=0x7fffffffd510) at util/session.c:1371

#21 0x000000000049f02e in perf_session__process_events (session=0x90e5e0, tool=0x7fffffffd510) at util/session.c:1389 #22 0x000000000042d605 in __cmd_report (rep=0x7fffffffd510) at builtin-report.c:527
---Type <return> to continue, or q <return> to quit---

Applied all except this one and the other one David commented on, waiting for that discussion to reach some conclusion.

But with the ones applied 'perf report -s srcline' is _much_ faster, thanks!

- Arnaldo

Quoted 85 lines
---
 tools/perf/util/srcline.c | 53 +----------------------------------------------
 1 file changed, 1 insertion(+), 52 deletions(-)
tools/perf/util/srcline.c +1 -52
index 93795f9..e09c9e0 100644
@@ -29,54 +29,6 @@ struct a2l_data {
 	asymbol 	**syms;
 };
 
-static int bfd_error(const char *string)
-{
-	const char *errmsg;
-
-	errmsg = bfd_errmsg(bfd_get_error());
-	fflush(stdout);
-
-	if (string)
-		pr_debug("%s: %s\n", string, errmsg);
-	else
-		pr_debug("%s\n", errmsg);
-
-	return -1;
-}
-
-static int slurp_symtab(bfd *abfd, struct a2l_data *a2l)
-{
-	long storage;
-	long symcount;
-	asymbol **syms;
-	bfd_boolean dynamic = FALSE;
-
-	if ((bfd_get_file_flags(abfd) & HAS_SYMS) == 0)
-		return bfd_error(bfd_get_filename(abfd));
-
-	storage = bfd_get_symtab_upper_bound(abfd);
-	if (storage == 0L) {
-		storage = bfd_get_dynamic_symtab_upper_bound(abfd);
-		dynamic = TRUE;
-	}
-	if (storage < 0L)
-		return bfd_error(bfd_get_filename(abfd));
-
-	syms = malloc(storage);
-	if (dynamic)
-		symcount = bfd_canonicalize_dynamic_symtab(abfd, syms);
-	else
-		symcount = bfd_canonicalize_symtab(abfd, syms);
-
-	if (symcount < 0) {
-		free(syms);
-		return bfd_error(bfd_get_filename(abfd));
-	}
-
-	a2l->syms = syms;
-	return 0;
-}
-
 static void find_address_in_section(bfd *abfd, asection *section, void *data)
 {
 	bfd_vma pc, vma;
@@ -96,7 +48,7 @@ static void find_address_in_section(bfd *abfd, asection *section, void *data)
 	if (pc < vma || pc >= vma + size)
 		return;
 
-	a2l->found = bfd_find_nearest_line(abfd, section, a2l->syms, pc - vma,
+	a2l->found = bfd_find_nearest_line(abfd, section, NULL, pc - vma,
 					   &a2l->filename, &a2l->funcname,
 					   &a2l->line);
 }
@@ -122,9 +74,6 @@ static struct a2l_data *addr2line_init(const char *path)
 	if (a2l->input == NULL)
 		goto out;
 
-	if (slurp_symtab(abfd, a2l))
-		goto out;
-
 	return a2l;
 
 out:
Signature · 1 line
1.7.11.7
#18 lore raw

Re: [PATCH 6/7] perf tools: Do not need to read symbols for source line lookup

Em Wed, Dec 04, 2013 at 04:00:18PM +0200, Adrian Hunter escreveu:

Quoted 10 lines · Arnaldo Carvalho de Melo
Quoted 9 lines

Em Tue, Dec 03, 2013 at 09:23:09AM +0200, Adrian Hunter escreveu:

Quoted 4 lines

When using libbfd to lookup source filename and line number, libbfd does not need the symbol table. Removing the symbol table reading, doubles the speed of 'perf script' with the 'srcline' option.

[root@zoo ~]# gdb perf
run report -s srcline

Quoted 7 lines
Quoted 1 line

b00000m

I wasn't able to reproduce this but I noticed the line numbers were not found for kernel modules with this patch - so it seems some binaries need the symbol information and some don't.

So please disregard this patch, sorry for your trouble.

No problem, disregarding it.

- Arnaldo

author reviewer
#19 lore raw

PATCH 7/7 perf tools: Do not disable source line lookup just because of 1 failure

In reply to Adrian Hunter

Looking up an ip's source file name and line number does not succeed always. Current logic disables the lookup for a dso entirely on any failure. Change it so that disabling never happens if there has ever been a successful lookup for that dso but disable if the first 123 lookups fail.

---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  1 +
 tools/perf/util/srcline.c | 20 ++++++++++++++++----
 3 files changed, 18 insertions(+), 4 deletions(-)
tools/perf/util/dso.c +1 -0
index 49da968..a0c7c59 100644
@@ -451,6 +451,7 @@ struct dso *dso__new(const char *name)
 		dso->sorted_by_name = 0;
 		dso->has_build_id = 0;
 		dso->has_srcline = 1;
+		dso->a2l_fails = 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 7142e52..384f2d9 100644
@@ -79,6 +79,7 @@ struct dso {
 	struct rb_root	 cache;
 	void		 *a2l;
 	char		 *symsrc_filename;
+	unsigned int	 a2l_fails;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
tools/perf/util/srcline.c +16 -4
index e09c9e0..d351d62 100644
@@ -193,6 +193,12 @@ void dso__free_a2l(struct dso *dso __maybe_unused)
 
 #endif /* HAVE_LIBBFD_SUPPORT */
 
+/*
+ * Number of addr2line failures (without success) before disabling it for that
+ * dso.
+ */
+#define A2L_FAIL_LIMIT 123
+
 char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file = NULL;
@@ -217,15 +223,21 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!addr2line(dso_name, addr, &file, &line, dso))
 		goto out;
 
-	if (asprintf(&srcline, "%s:%u", file, line) < 0)
-		srcline = SRCLINE_UNKNOWN;
+	if (asprintf(&srcline, "%s:%u", file, line) < 0) {
+		free(file);
+		goto out;
+	}
+
+	dso->a2l_fails = 0;
 
 	free(file);
 	return srcline;
 
 out:
-	dso->has_srcline = 0;
-	dso__free_a2l(dso);
+	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
+		dso->has_srcline = 0;
+		dso__free_a2l(dso);
+	}
 	return SRCLINE_UNKNOWN;
 }
 
Signature · 1 line
1.7.11.7
#20 lore raw

[tip:perf/core] perf tools: Do not disable source line lookup just because of 1 failure

perf tools: Do not disable source line lookup just because of 1 failure
Commit-ID:  906049c8276eb99af997f73d602649a98e360035
Gitweb:     http://git.kernel.org/tip/906049c8276eb99af997f73d602649a98e360035
Author:     Adrian Hunter <[email protected]>
AuthorDate: Tue, 3 Dec 2013 09:23:10 +0200
Committer:  Arnaldo Carvalho de Melo <[email protected]>
CommitDate: Wed, 4 Dec 2013 13:46:36 -0300

perf tools: Do not disable source line lookup just because of 1 failure

Looking up an ip's source file name and line number does not succeed always. Current logic disables the lookup for a dso entirely on any failure. Change it so that disabling never happens if there has ever been a successful lookup for that dso but disable if the first 123 lookups fail.

Cc Andi Kleen <[email protected]>
Cc David Ahern <[email protected]>
Cc Frederic Weisbecker <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Jiri Olsa <[email protected]>
Cc Mike Galbraith <[email protected]>
Cc Namhyung Kim <[email protected]>
Cc Paul Mackerras <[email protected]>
Cc Peter Zijlstra <[email protected]>
Cc Stephane Eranian <[email protected]>
---
 tools/perf/util/dso.c     |  1 +
 tools/perf/util/dso.h     |  1 +
 tools/perf/util/srcline.c | 20 ++++++++++++++++----
 3 files changed, 18 insertions(+), 4 deletions(-)
tools/perf/util/dso.c +1 -0
index 49da968..a0c7c59 100644
@@ -451,6 +451,7 @@ struct dso *dso__new(const char *name)
 		dso->sorted_by_name = 0;
 		dso->has_build_id = 0;
 		dso->has_srcline = 1;
+		dso->a2l_fails = 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 7142e52..384f2d9 100644
@@ -79,6 +79,7 @@ struct dso {
 	struct rb_root	 cache;
 	void		 *a2l;
 	char		 *symsrc_filename;
+	unsigned int	 a2l_fails;
 	enum dso_kernel_type	kernel;
 	enum dso_swap_type	needs_swap;
 	enum dso_binary_type	symtab_type;
tools/perf/util/srcline.c +16 -4
index 93795f9..0c07556 100644
@@ -244,6 +244,12 @@ void dso__free_a2l(struct dso *dso __maybe_unused)
 
 #endif /* HAVE_LIBBFD_SUPPORT */
 
+/*
+ * Number of addr2line failures (without success) before disabling it for that
+ * dso.
+ */
+#define A2L_FAIL_LIMIT 123
+
 char *get_srcline(struct dso *dso, unsigned long addr)
 {
 	char *file = NULL;
@@ -268,15 +274,21 @@ char *get_srcline(struct dso *dso, unsigned long addr)
 	if (!addr2line(dso_name, addr, &file, &line, dso))
 		goto out;
 
-	if (asprintf(&srcline, "%s:%u", file, line) < 0)
-		srcline = SRCLINE_UNKNOWN;
+	if (asprintf(&srcline, "%s:%u", file, line) < 0) {
+		free(file);
+		goto out;
+	}
+
+	dso->a2l_fails = 0;
 
 	free(file);
 	return srcline;
 
 out:
-	dso->has_srcline = 0;
-	dso__free_a2l(dso);
+	if (dso->a2l_fails && ++dso->a2l_fails > A2L_FAIL_LIMIT) {
+		dso->has_srcline = 0;
+		dso__free_a2l(dso);
+	}
 	return SRCLINE_UNKNOWN;
 }