public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Taeung Song <treeze.taeung@gmail.com>
To: Arnaldo Carvalho de Melo <acme@kernel.org>
Cc: linux-kernel@vger.kernel.org, Jiri Olsa <jolsa@kernel.org>,
	Namhyung Kim <namhyung@kernel.org>,
	Ingo Molnar <mingo@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Wang Nan <wangnan0@huawei.com>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Taeung Song <treeze.taeung@gmail.com>,
	Jiri Olsa <jolsa@redhat.com>
Subject: [PATCH 4/4] perf annotate: Introduce source_code to collect actual code
Date: Wed, 22 Feb 2017 19:08:23 +0900	[thread overview]
Message-ID: <1487758103-7953-5-git-send-email-treeze.taeung@gmail.com> (raw)
In-Reply-To: <1487758103-7953-1-git-send-email-treeze.taeung@gmail.com>

The output of perf-annotate has a problem.
It is so confusing that the output is mixed with
both source code and assembly code.
IMHO, we need readable annotate view based on source code,
not mixed view. (not depending on 'objdump -S')

And to do that, we can collect actual source code per function(sym)
using addr2line() and we can handle 'struct source_code'
that contains each line of code.

In near future, it would be used for new annotate view based on
actual source code per function(sym).

Cc: Namhyung Kim <namhyung@kernel.org>
Cc: Jiri Olsa <jolsa@redhat.com>
Signed-off-by: Taeung Song <treeze.taeung@gmail.com>
---
 tools/perf/util/annotate.c | 117 +++++++++++++++++++++++++++++++++++++++++++++
 tools/perf/util/annotate.h |   7 +++
 2 files changed, 124 insertions(+)

diff --git a/tools/perf/util/annotate.c b/tools/perf/util/annotate.c
index acd500b..93abc1e 100644
--- a/tools/perf/util/annotate.c
+++ b/tools/perf/util/annotate.c
@@ -1590,6 +1590,116 @@ static void symbol__free_source_line(struct symbol *sym, int len)
 	zfree(&notes->src->lines);
 }
 
+static int symbol__free_source_code(struct symbol *sym)
+{
+	struct annotation *notes = symbol__annotation(sym);
+	struct source_code *pos, *tmp;
+
+	if (&notes->src->code == NULL)
+		return -1;
+
+	list_for_each_entry_safe(pos, tmp, &notes->src->code, node) {
+		list_del(&pos->node);
+		zfree(&pos->code_line);
+		free(pos);
+	}
+	return 0;
+}
+
+static int parse_srcline(char *srcline, char **path, int *line_nr)
+{
+	char *sep;
+
+	if (!strcmp(srcline, SRCLINE_UNKNOWN))
+		return -1;
+
+	sep = strchr(srcline, ':');
+	if (sep) {
+		*sep = '\0';
+		*path = srcline;
+		*line_nr = strtoul(++sep, NULL, 0);
+	} else
+		return -1;
+
+	return 0;
+}
+
+static bool symbol__get_source_code(struct symbol *sym, struct map *map)
+{
+	FILE *file;
+	int first_linenr, start_linenr, end_linenr;
+	size_t len;
+	char *line = NULL, *path, *start_srcline, *end_srcline;
+	u64 start = map__rip_2objdump(map, sym->start);
+	u64 end = map__rip_2objdump(map, sym->end - 1);
+	bool bef_fullpath = srcline_full_filename;
+	bool ret = false;
+	struct annotation *notes = symbol__annotation(sym);
+
+	srcline_full_filename = true;
+	start_srcline = get_srcline(map->dso, start, NULL, false);
+	end_srcline = get_srcline(map->dso, end, NULL, false);
+	srcline_full_filename = bef_fullpath;
+
+	if (parse_srcline(start_srcline, &path, &start_linenr) < 0)
+		return false;
+	if (parse_srcline(end_srcline, &path, &end_linenr) < 0)
+		return false;
+
+	/* To read a function header for the sym */
+	if (start_linenr > 4)
+		first_linenr = start_linenr - 4;
+	else
+		first_linenr = 1;
+
+	if (access(path, R_OK) != 0)
+		return false;
+
+	file = fopen(path, "r");
+	if (!file)
+		return false;
+
+	INIT_LIST_HEAD(&notes->src->code);
+
+	while (!feof(file)) {
+		int nr;
+		char *c, *parsed_line;
+		struct source_code *code;
+
+		if (getline(&line, &len, file) < 0) {
+			symbol__free_source_code(sym);
+			break;
+		}
+
+		if (++nr < first_linenr)
+			continue;
+
+		parsed_line = rtrim(line);
+		c = strchr(parsed_line, '\n');
+		if (c)
+			*c = '\0';
+		code = zalloc(sizeof(*code));
+		if (!code) {
+			symbol__free_source_code(sym);
+			break;
+		}
+		code->nr = nr;
+		code->code_line = strdup(parsed_line);
+		list_add_tail(&code->node, &notes->src->code);
+
+		if (nr == end_linenr) {
+			ret = true;
+			break;
+		}
+	}
+
+	free(line);
+	fclose(file);
+	free_srcline(start_srcline);
+	free_srcline(end_srcline);
+	return ret;
+}
+
 /* Get the filename:line for the colored entries */
 static int symbol__get_source_line(struct symbol *sym, struct map *map,
 				   struct perf_evsel *evsel,
@@ -1862,6 +1972,7 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 	struct dso *dso = map->dso;
 	struct rb_root source_line = RB_ROOT;
 	u64 len;
+	bool has_code = false;
 
 	if (symbol__disassemble(sym, map, perf_evsel__env_arch(evsel), 0) < 0)
 		return -1;
@@ -1874,11 +1985,17 @@ int symbol__tty_annotate(struct symbol *sym, struct map *map,
 		print_summary(&source_line, dso->long_name);
 	}
 
+	if (symbol_conf.annotate_src)
+		has_code = symbol__get_source_code(sym, map);
+
 	symbol__annotate_printf(sym, map, evsel, full_paths,
 				min_pcnt, max_lines, 0);
 	if (print_lines)
 		symbol__free_source_line(sym, len);
 
+	if (has_code)
+		symbol__free_source_code(sym);
+
 	disasm__purge(&symbol__annotation(sym)->src->source);
 
 	return 0;
diff --git a/tools/perf/util/annotate.h b/tools/perf/util/annotate.h
index 09776b5..7231f46 100644
--- a/tools/perf/util/annotate.h
+++ b/tools/perf/util/annotate.h
@@ -95,6 +95,12 @@ struct cyc_hist {
 	u16	reset;
 };
 
+struct source_code {
+	struct list_head node;
+	int nr;
+	char *code_line;
+};
+
 struct source_line_samples {
 	double		percent;
 	double		percent_sum;
@@ -123,6 +129,7 @@ struct source_line {
  */
 struct annotated_source {
 	struct list_head   source;
+	struct list_head   code;
 	struct source_line *lines;
 	int    		   nr_histograms;
 	size_t		   sizeof_sym_hist;
-- 
2.7.4

  parent reply	other threads:[~2017-02-22 10:09 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-22 10:08 [PATCH 0/4] perf annotate: Fixes for line numbers and Introduce source_code Taeung Song
2017-02-22 10:08 ` [PATCH 1/4] perf annotate: Remove needless regular expression for filename:linenr Taeung Song
2017-02-22 10:47   ` Namhyung Kim
2017-02-22 16:00     ` Taeung Song
2017-02-22 10:08 ` [PATCH 2/4] perf annotate: Align filename:linenr and more correct summary Taeung Song
2017-02-22 11:12   ` Namhyung Kim
2017-02-22 11:22   ` Namhyung Kim
2017-02-22 16:31     ` Taeung Song
2017-02-22 10:08 ` [PATCH 3/4] perf annotate: Change the method counting line numbers Taeung Song
2017-02-22 10:08 ` Taeung Song [this message]
2017-02-22 11:27   ` [PATCH 4/4] perf annotate: Introduce source_code to collect actual code Namhyung Kim
2017-02-22 16:41     ` Taeung Song
2017-02-24  5:57   ` Ravi Bangoria

Reply instructions:

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

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

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

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

  git send-email \
    --in-reply-to=1487758103-7953-5-git-send-email-treeze.taeung@gmail.com \
    --to=treeze.taeung@gmail.com \
    --cc=acme@kernel.org \
    --cc=jolsa@kernel.org \
    --cc=jolsa@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=mingo@kernel.org \
    --cc=namhyung@kernel.org \
    --cc=peterz@infradead.org \
    --cc=wangnan0@huawei.com \
    /path/to/YOUR_REPLY

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

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