linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] perf tools: Fix memory leak in addr2inlines()
@ 2017-10-31  2:06 Namhyung Kim
  2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Namhyung Kim @ 2017-10-31  2:06 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, kernel-team,
	Jin Yao, Milian Wolff

When libbfd is not used, addr2inlines() executes `addr2line -i` and
process output line by line.  But it resets filename to NULL in the loop
so getline() allocates additional memory everytime instead of realloc.

Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Milian Wolff <milian.wolff@kdab.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/srcline.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index c143c3bc1ef8..51dc49c65476 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -456,20 +456,17 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	while (getline(&filename, &len, fp) != -1) {
 		char *srcline;
 
-		if (filename_split(filename, &line_nr) != 1) {
-			free(filename);
+		if (filename_split(filename, &line_nr) != 1)
 			goto out;
-		}
 
 		srcline = srcline_from_fileline(filename, line_nr);
 		if (inline_list__append(sym, srcline, node) != 0)
 			goto out;
-
-		filename = NULL;
 	}
 
 out:
 	pclose(fp);
+	free(filename);
 
 	return node;
 }
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [PATCH 2/2] perf tools: Show correct function name for srcline of callchains
  2017-10-31  2:06 [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Namhyung Kim
@ 2017-10-31  2:06 ` Namhyung Kim
       [not found]   ` <2208660.mvSdWdKhUF@milian-kdab2>
                     ` (2 more replies)
  2017-11-01 12:13 ` [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Jiri Olsa
  2017-11-03 14:23 ` [tip:perf/core] perf srcline: " tip-bot for Namhyung Kim
  2 siblings, 3 replies; 9+ messages in thread
From: Namhyung Kim @ 2017-10-31  2:06 UTC (permalink / raw)
  To: Arnaldo Carvalho de Melo
  Cc: Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML, kernel-team,
	Jin Yao, Milian Wolff

When libbfd is not used, it doesn't show proper function name and reuse
the original symbol of the sample.  That's because it passes the
original sym to inline_list__append().  As `addr2line -f` returns
function names as well, use that to create ad inline_sym and pass it to
inline_list__append().

For example, following data shows that inlined entries of main have same
name (main).

Before:
  $ perf report -g srcline -q | head
      45.22%  inlining     libm-2.26.so      [.] __hypot_finite
              |
              ---__hypot_finite ??:0
                 |
                 |--44.15%--hypot ??:0
                 |          main complex:589
                 |          main complex:597
                 |          main complex:654
                 |          main complex:664
                 |          main inlining.cpp:14

After:
  $ perf report -g srcline -q | head
      45.22%  inlining     libm-2.26.so      [.] __hypot_finite
              |
              ---__hypot_finite
                 |
                 |--44.15%--hypot
                 |          std::__complex_abs complex:589 (inlined)
                 |          std::abs<double> complex:597 (inlined)
                 |          std::_Norm_helper<true>::_S_do_it<double> complex:654 (inlined)
                 |          std::norm<double> complex:664 (inlined)
                 |          main inlining.cpp:14

Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Milian Wolff <milian.wolff@kdab.com>
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
---
 tools/perf/util/srcline.c | 95 +++++++++++++++++++++++++++--------------------
 1 file changed, 55 insertions(+), 40 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index 51dc49c65476..ad1b46f1f2cf 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -10,7 +10,7 @@
 #include "util/debug.h"
 #include "util/callchain.h"
 #include "srcline.h"
-
+#include "string2.h"
 #include "symbol.h"
 
 bool srcline_full_filename;
@@ -77,6 +77,41 @@ static char *srcline_from_fileline(const char *file, unsigned int line)
 	return srcline;
 }
 
+static struct symbol *new_inline_sym(struct dso *dso,
+				     struct symbol *base_sym,
+				     const char *funcname)
+{
+	struct symbol *inline_sym;
+	char *demangled = NULL;
+
+	if (dso) {
+		demangled = dso__demangle_sym(dso, 0, funcname);
+		if (demangled)
+			funcname = demangled;
+	}
+
+	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
+		/* reuse the real, existing symbol */
+		inline_sym = base_sym;
+		/* ensure that we don't alias an inlined symbol, which could
+		 * lead to double frees in inline_node__delete
+		 */
+		assert(!base_sym->inlined);
+	} else {
+		/* create a fake symbol for the inline frame */
+		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
+					 base_sym ? base_sym->end : 0,
+					 base_sym ? base_sym->binding : 0,
+					 funcname);
+		if (inline_sym)
+			inline_sym->inlined = 1;
+	}
+
+	free(demangled);
+
+	return inline_sym;
+}
+
 #ifdef HAVE_LIBBFD_SUPPORT
 
 /*
@@ -219,41 +254,6 @@ static void addr2line_cleanup(struct a2l_data *a2l)
 
 #define MAX_INLINE_NEST 1024
 
-static struct symbol *new_inline_sym(struct dso *dso,
-				     struct symbol *base_sym,
-				     const char *funcname)
-{
-	struct symbol *inline_sym;
-	char *demangled = NULL;
-
-	if (dso) {
-		demangled = dso__demangle_sym(dso, 0, funcname);
-		if (demangled)
-			funcname = demangled;
-	}
-
-	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
-		/* reuse the real, existing symbol */
-		inline_sym = base_sym;
-		/* ensure that we don't alias an inlined symbol, which could
-		 * lead to double frees in inline_node__delete
-		 */
-		assert(!base_sym->inlined);
-	} else {
-		/* create a fake symbol for the inline frame */
-		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
-					 base_sym ? base_sym->end : 0,
-					 base_sym ? base_sym->binding : 0,
-					 funcname);
-		if (inline_sym)
-			inline_sym->inlined = 1;
-	}
-
-	free(demangled);
-
-	return inline_sym;
-}
-
 static int inline_list__append_dso_a2l(struct dso *dso,
 				       struct inline_node *node,
 				       struct symbol *sym)
@@ -432,10 +432,11 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	char cmd[PATH_MAX];
 	struct inline_node *node;
 	char *filename = NULL;
-	size_t len;
+	char *funcname = NULL;
+	size_t filelen, funclen;
 	unsigned int line_nr = 0;
 
-	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
+	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
 		  dso_name, addr);
 
 	fp = popen(cmd, "r");
@@ -453,20 +454,34 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	INIT_LIST_HEAD(&node->val);
 	node->addr = addr;
 
-	while (getline(&filename, &len, fp) != -1) {
+	/* addr2line -f generates two lines for each inlined functions */
+	while (getline(&funcname, &funclen, fp) != -1) {
 		char *srcline;
+		struct symbol *inline_sym;
+
+		rtrim(funcname);
+
+		if (getline(&filename, &filelen, fp) == -1)
+			goto out;
 
 		if (filename_split(filename, &line_nr) != 1)
 			goto out;
 
 		srcline = srcline_from_fileline(filename, line_nr);
-		if (inline_list__append(sym, srcline, node) != 0)
+		inline_sym = new_inline_sym(dso, sym, funcname);
+
+		if (inline_list__append(inline_sym, srcline, node) != 0) {
+			free(srcline);
+			if (inline_sym && inline_sym->inlined)
+				symbol__delete(inline_sym);
 			goto out;
+		}
 	}
 
 out:
 	pclose(fp);
 	free(filename);
+	free(funcname);
 
 	return node;
 }
-- 
2.14.3

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] perf tools: Show correct function name for srcline of callchains
       [not found]   ` <2208660.mvSdWdKhUF@milian-kdab2>
@ 2017-11-01 12:05     ` Namhyung Kim
  2017-11-01 14:41       ` Arnaldo Carvalho de Melo
  0 siblings, 1 reply; 9+ messages in thread
From: Namhyung Kim @ 2017-11-01 12:05 UTC (permalink / raw)
  To: Milian Wolff
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, kernel-team, Jin Yao

Hi Milian,

On Wed, Nov 01, 2017 at 10:57:12AM +0100, Milian Wolff wrote:
> On Tuesday, October 31, 2017 3:06:54 AM CET Namhyung Kim wrote:
> > When libbfd is not used, it doesn't show proper function name and reuse
> > the original symbol of the sample.  That's because it passes the
> > original sym to inline_list__append().  As `addr2line -f` returns
> > function names as well, use that to create ad inline_sym and pass it to
> > inline_list__append().
> 
> Typo above: "ad" -> "and"

I think it's "an" instead of "and". :)

> 
> Otherwise these patches look fine to me
> 
> Reviewed-by: Milian Wolff <milian.wolff@kdab.com>

Thanks,
Namhyung

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] perf tools: Fix memory leak in addr2inlines()
  2017-10-31  2:06 [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Namhyung Kim
  2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
@ 2017-11-01 12:13 ` Jiri Olsa
  2017-11-01 14:42   ` Arnaldo Carvalho de Melo
  2017-11-03 14:23 ` [tip:perf/core] perf srcline: " tip-bot for Namhyung Kim
  2 siblings, 1 reply; 9+ messages in thread
From: Jiri Olsa @ 2017-11-01 12:13 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, kernel-team, Jin Yao, Milian Wolff

On Tue, Oct 31, 2017 at 11:06:53AM +0900, Namhyung Kim wrote:
> When libbfd is not used, addr2inlines() executes `addr2line -i` and
> process output line by line.  But it resets filename to NULL in the loop
> so getline() allocates additional memory everytime instead of realloc.
> 
> Cc: Jin Yao <yao.jin@linux.intel.com>
> Cc: Milian Wolff <milian.wolff@kdab.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

Acked-by: Jiri Olsa <jolsa@kernel.org>

jirka

> ---
>  tools/perf/util/srcline.c | 7 ++-----
>  1 file changed, 2 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
> index c143c3bc1ef8..51dc49c65476 100644
> --- a/tools/perf/util/srcline.c
> +++ b/tools/perf/util/srcline.c
> @@ -456,20 +456,17 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
>  	while (getline(&filename, &len, fp) != -1) {
>  		char *srcline;
>  
> -		if (filename_split(filename, &line_nr) != 1) {
> -			free(filename);
> +		if (filename_split(filename, &line_nr) != 1)
>  			goto out;
> -		}
>  
>  		srcline = srcline_from_fileline(filename, line_nr);
>  		if (inline_list__append(sym, srcline, node) != 0)
>  			goto out;
> -
> -		filename = NULL;
>  	}
>  
>  out:
>  	pclose(fp);
> +	free(filename);
>  
>  	return node;
>  }
> -- 
> 2.14.3
> 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] perf tools: Show correct function name for srcline of callchains
  2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
       [not found]   ` <2208660.mvSdWdKhUF@milian-kdab2>
@ 2017-11-01 12:14   ` Jiri Olsa
  2017-11-03 14:24   ` [tip:perf/core] perf srcline: " tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: Jiri Olsa @ 2017-11-01 12:14 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Arnaldo Carvalho de Melo, Ingo Molnar, Peter Zijlstra, Jiri Olsa,
	LKML, kernel-team, Jin Yao, Milian Wolff

On Tue, Oct 31, 2017 at 11:06:54AM +0900, Namhyung Kim wrote:
> When libbfd is not used, it doesn't show proper function name and reuse
> the original symbol of the sample.  That's because it passes the
> original sym to inline_list__append().  As `addr2line -f` returns
> function names as well, use that to create ad inline_sym and pass it to
> inline_list__append().
> 
> For example, following data shows that inlined entries of main have same
> name (main).
> 
> Before:
>   $ perf report -g srcline -q | head
>       45.22%  inlining     libm-2.26.so      [.] __hypot_finite
>               |
>               ---__hypot_finite ??:0
>                  |
>                  |--44.15%--hypot ??:0
>                  |          main complex:589
>                  |          main complex:597
>                  |          main complex:654
>                  |          main complex:664
>                  |          main inlining.cpp:14
> 
> After:
>   $ perf report -g srcline -q | head
>       45.22%  inlining     libm-2.26.so      [.] __hypot_finite
>               |
>               ---__hypot_finite
>                  |
>                  |--44.15%--hypot
>                  |          std::__complex_abs complex:589 (inlined)
>                  |          std::abs<double> complex:597 (inlined)
>                  |          std::_Norm_helper<true>::_S_do_it<double> complex:654 (inlined)
>                  |          std::norm<double> complex:664 (inlined)
>                  |          main inlining.cpp:14
> 
> Cc: Jin Yao <yao.jin@linux.intel.com>
> Cc: Milian Wolff <milian.wolff@kdab.com>
> Signed-off-by: Namhyung Kim <namhyung@kernel.org>

looks ok

Reviewed-by: Jiri Olsa <jolsa@kernel.org>

jirka

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 2/2] perf tools: Show correct function name for srcline of callchains
  2017-11-01 12:05     ` Namhyung Kim
@ 2017-11-01 14:41       ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 9+ messages in thread
From: Arnaldo Carvalho de Melo @ 2017-11-01 14:41 UTC (permalink / raw)
  To: Namhyung Kim
  Cc: Milian Wolff, Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML,
	kernel-team, Jin Yao

Em Wed, Nov 01, 2017 at 09:05:59PM +0900, Namhyung Kim escreveu:
> Hi Milian,
> 
> On Wed, Nov 01, 2017 at 10:57:12AM +0100, Milian Wolff wrote:
> > On Tuesday, October 31, 2017 3:06:54 AM CET Namhyung Kim wrote:
> > > When libbfd is not used, it doesn't show proper function name and reuse
> > > the original symbol of the sample.  That's because it passes the
> > > original sym to inline_list__append().  As `addr2line -f` returns
> > > function names as well, use that to create ad inline_sym and pass it to
> > > inline_list__append().
> > 
> > Typo above: "ad" -> "and"
> 
> I think it's "an" instead of "and". :)

I didn't catch that, bummer! 8-) Ok, made the ad -> and -> an change :-)

And added Milian's Reviewed-by,

- Arnaldo
 
> > Otherwise these patches look fine to me
> > 
> > Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
> 
> Thanks,
> Namhyung

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH 1/2] perf tools: Fix memory leak in addr2inlines()
  2017-11-01 12:13 ` [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Jiri Olsa
@ 2017-11-01 14:42   ` Arnaldo Carvalho de Melo
  0 siblings, 0 replies; 9+ messages in thread
From: Arnaldo Carvalho de Melo @ 2017-11-01 14:42 UTC (permalink / raw)
  To: Jiri Olsa
  Cc: Namhyung Kim, Ingo Molnar, Peter Zijlstra, Jiri Olsa, LKML,
	kernel-team, Jin Yao, Milian Wolff

Em Wed, Nov 01, 2017 at 01:13:57PM +0100, Jiri Olsa escreveu:
> On Tue, Oct 31, 2017 at 11:06:53AM +0900, Namhyung Kim wrote:
> > When libbfd is not used, addr2inlines() executes `addr2line -i` and
> > process output line by line.  But it resets filename to NULL in the loop
> > so getline() allocates additional memory everytime instead of realloc.
> > 
> > Cc: Jin Yao <yao.jin@linux.intel.com>
> > Cc: Milian Wolff <milian.wolff@kdab.com>
> > Signed-off-by: Namhyung Kim <namhyung@kernel.org>
> 
> Acked-by: Jiri Olsa <jolsa@kernel.org>

Thanks, added this and the Reviewed-by to 2/2.

- Arnaldo
 
> jirka
> 
> > ---
> >  tools/perf/util/srcline.c | 7 ++-----
> >  1 file changed, 2 insertions(+), 5 deletions(-)
> > 
> > diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
> > index c143c3bc1ef8..51dc49c65476 100644
> > --- a/tools/perf/util/srcline.c
> > +++ b/tools/perf/util/srcline.c
> > @@ -456,20 +456,17 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
> >  	while (getline(&filename, &len, fp) != -1) {
> >  		char *srcline;
> >  
> > -		if (filename_split(filename, &line_nr) != 1) {
> > -			free(filename);
> > +		if (filename_split(filename, &line_nr) != 1)
> >  			goto out;
> > -		}
> >  
> >  		srcline = srcline_from_fileline(filename, line_nr);
> >  		if (inline_list__append(sym, srcline, node) != 0)
> >  			goto out;
> > -
> > -		filename = NULL;
> >  	}
> >  
> >  out:
> >  	pclose(fp);
> > +	free(filename);
> >  
> >  	return node;
> >  }
> > -- 
> > 2.14.3
> > 

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [tip:perf/core] perf srcline: Fix memory leak in addr2inlines()
  2017-10-31  2:06 [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Namhyung Kim
  2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
  2017-11-01 12:13 ` [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Jiri Olsa
@ 2017-11-03 14:23 ` tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: tip-bot for Namhyung Kim @ 2017-11-03 14:23 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: peterz, namhyung, linux-kernel, mingo, jolsa, milian.wolff,
	yao.jin, tglx, acme, hpa

Commit-ID:  b7b75a60b291cc699ca9bb2a8517a1b3b08bbeb1
Gitweb:     https://git.kernel.org/tip/b7b75a60b291cc699ca9bb2a8517a1b3b08bbeb1
Author:     Namhyung Kim <namhyung@kernel.org>
AuthorDate: Tue, 31 Oct 2017 11:06:53 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 1 Nov 2017 11:43:56 -0300

perf srcline: Fix memory leak in addr2inlines()

When libbfd is not used, addr2inlines() executes `addr2line -i` and
process output line by line.  But it resets filename to NULL in the loop
so getline() allocates additional memory everytime instead of realloc.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@kernel.org>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Milian Wolff <milian.wolff@kdab.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: kernel-team@lge.com
Link: http://lkml.kernel.org/r/20171031020654.31163-1-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/srcline.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index c143c3b..51dc49c 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -456,20 +456,17 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	while (getline(&filename, &len, fp) != -1) {
 		char *srcline;
 
-		if (filename_split(filename, &line_nr) != 1) {
-			free(filename);
+		if (filename_split(filename, &line_nr) != 1)
 			goto out;
-		}
 
 		srcline = srcline_from_fileline(filename, line_nr);
 		if (inline_list__append(sym, srcline, node) != 0)
 			goto out;
-
-		filename = NULL;
 	}
 
 out:
 	pclose(fp);
+	free(filename);
 
 	return node;
 }

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* [tip:perf/core] perf srcline: Show correct function name for srcline of callchains
  2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
       [not found]   ` <2208660.mvSdWdKhUF@milian-kdab2>
  2017-11-01 12:14   ` Jiri Olsa
@ 2017-11-03 14:24   ` tip-bot for Namhyung Kim
  2 siblings, 0 replies; 9+ messages in thread
From: tip-bot for Namhyung Kim @ 2017-11-03 14:24 UTC (permalink / raw)
  To: linux-tip-commits
  Cc: linux-kernel, tglx, yao.jin, acme, jolsa, hpa, mingo, peterz,
	milian.wolff, namhyung

Commit-ID:  7285cf3325b4a1dfb336d31eebc27dfbc30fb9aa
Gitweb:     https://git.kernel.org/tip/7285cf3325b4a1dfb336d31eebc27dfbc30fb9aa
Author:     Namhyung Kim <namhyung@kernel.org>
AuthorDate: Tue, 31 Oct 2017 11:06:54 +0900
Committer:  Arnaldo Carvalho de Melo <acme@redhat.com>
CommitDate: Wed, 1 Nov 2017 11:44:38 -0300

perf srcline: Show correct function name for srcline of callchains

When libbfd is not used, it doesn't show proper function name and reuse
the original symbol of the sample.  That's because it passes the
original sym to inline_list__append().  As `addr2line -f` returns
function names as well, use that to create an inline_sym and pass it to
inline_list__append().

For example, following data shows that inlined entries of main have same
name (main).

Before:
  $ perf report -g srcline -q | head
      45.22%  inlining     libm-2.26.so      [.] __hypot_finite
              |
              ---__hypot_finite ??:0
                 |
                 |--44.15%--hypot ??:0
                 |          main complex:589
                 |          main complex:597
                 |          main complex:654
                 |          main complex:664
                 |          main inlining.cpp:14

After:
  $ perf report -g srcline -q | head
      45.22%  inlining     libm-2.26.so      [.] __hypot_finite
              |
              ---__hypot_finite
                 |
                 |--44.15%--hypot
                 |          std::__complex_abs complex:589 (inlined)
                 |          std::abs<double> complex:597 (inlined)
                 |          std::_Norm_helper<true>::_S_do_it<double> complex:654 (inlined)
                 |          std::norm<double> complex:664 (inlined)
                 |          main inlining.cpp:14

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Reviewed-by: Jiri Olsa <jolsa@kernel.org>
Reviewed-by: Milian Wolff <milian.wolff@kdab.com>
Cc: Jin Yao <yao.jin@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: kernel-team@lge.com
Link: http://lkml.kernel.org/r/20171031020654.31163-2-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
---
 tools/perf/util/srcline.c | 95 +++++++++++++++++++++++++++--------------------
 1 file changed, 55 insertions(+), 40 deletions(-)

diff --git a/tools/perf/util/srcline.c b/tools/perf/util/srcline.c
index 51dc49c..ad1b46f 100644
--- a/tools/perf/util/srcline.c
+++ b/tools/perf/util/srcline.c
@@ -10,7 +10,7 @@
 #include "util/debug.h"
 #include "util/callchain.h"
 #include "srcline.h"
-
+#include "string2.h"
 #include "symbol.h"
 
 bool srcline_full_filename;
@@ -77,6 +77,41 @@ static char *srcline_from_fileline(const char *file, unsigned int line)
 	return srcline;
 }
 
+static struct symbol *new_inline_sym(struct dso *dso,
+				     struct symbol *base_sym,
+				     const char *funcname)
+{
+	struct symbol *inline_sym;
+	char *demangled = NULL;
+
+	if (dso) {
+		demangled = dso__demangle_sym(dso, 0, funcname);
+		if (demangled)
+			funcname = demangled;
+	}
+
+	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
+		/* reuse the real, existing symbol */
+		inline_sym = base_sym;
+		/* ensure that we don't alias an inlined symbol, which could
+		 * lead to double frees in inline_node__delete
+		 */
+		assert(!base_sym->inlined);
+	} else {
+		/* create a fake symbol for the inline frame */
+		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
+					 base_sym ? base_sym->end : 0,
+					 base_sym ? base_sym->binding : 0,
+					 funcname);
+		if (inline_sym)
+			inline_sym->inlined = 1;
+	}
+
+	free(demangled);
+
+	return inline_sym;
+}
+
 #ifdef HAVE_LIBBFD_SUPPORT
 
 /*
@@ -219,41 +254,6 @@ static void addr2line_cleanup(struct a2l_data *a2l)
 
 #define MAX_INLINE_NEST 1024
 
-static struct symbol *new_inline_sym(struct dso *dso,
-				     struct symbol *base_sym,
-				     const char *funcname)
-{
-	struct symbol *inline_sym;
-	char *demangled = NULL;
-
-	if (dso) {
-		demangled = dso__demangle_sym(dso, 0, funcname);
-		if (demangled)
-			funcname = demangled;
-	}
-
-	if (base_sym && strcmp(funcname, base_sym->name) == 0) {
-		/* reuse the real, existing symbol */
-		inline_sym = base_sym;
-		/* ensure that we don't alias an inlined symbol, which could
-		 * lead to double frees in inline_node__delete
-		 */
-		assert(!base_sym->inlined);
-	} else {
-		/* create a fake symbol for the inline frame */
-		inline_sym = symbol__new(base_sym ? base_sym->start : 0,
-					 base_sym ? base_sym->end : 0,
-					 base_sym ? base_sym->binding : 0,
-					 funcname);
-		if (inline_sym)
-			inline_sym->inlined = 1;
-	}
-
-	free(demangled);
-
-	return inline_sym;
-}
-
 static int inline_list__append_dso_a2l(struct dso *dso,
 				       struct inline_node *node,
 				       struct symbol *sym)
@@ -432,10 +432,11 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	char cmd[PATH_MAX];
 	struct inline_node *node;
 	char *filename = NULL;
-	size_t len;
+	char *funcname = NULL;
+	size_t filelen, funclen;
 	unsigned int line_nr = 0;
 
-	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i %016"PRIx64,
+	scnprintf(cmd, sizeof(cmd), "addr2line -e %s -i -f %016"PRIx64,
 		  dso_name, addr);
 
 	fp = popen(cmd, "r");
@@ -453,20 +454,34 @@ static struct inline_node *addr2inlines(const char *dso_name, u64 addr,
 	INIT_LIST_HEAD(&node->val);
 	node->addr = addr;
 
-	while (getline(&filename, &len, fp) != -1) {
+	/* addr2line -f generates two lines for each inlined functions */
+	while (getline(&funcname, &funclen, fp) != -1) {
 		char *srcline;
+		struct symbol *inline_sym;
+
+		rtrim(funcname);
+
+		if (getline(&filename, &filelen, fp) == -1)
+			goto out;
 
 		if (filename_split(filename, &line_nr) != 1)
 			goto out;
 
 		srcline = srcline_from_fileline(filename, line_nr);
-		if (inline_list__append(sym, srcline, node) != 0)
+		inline_sym = new_inline_sym(dso, sym, funcname);
+
+		if (inline_list__append(inline_sym, srcline, node) != 0) {
+			free(srcline);
+			if (inline_sym && inline_sym->inlined)
+				symbol__delete(inline_sym);
 			goto out;
+		}
 	}
 
 out:
 	pclose(fp);
 	free(filename);
+	free(funcname);
 
 	return node;
 }

^ permalink raw reply related	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2017-11-03 14:26 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-31  2:06 [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Namhyung Kim
2017-10-31  2:06 ` [PATCH 2/2] perf tools: Show correct function name for srcline of callchains Namhyung Kim
     [not found]   ` <2208660.mvSdWdKhUF@milian-kdab2>
2017-11-01 12:05     ` Namhyung Kim
2017-11-01 14:41       ` Arnaldo Carvalho de Melo
2017-11-01 12:14   ` Jiri Olsa
2017-11-03 14:24   ` [tip:perf/core] perf srcline: " tip-bot for Namhyung Kim
2017-11-01 12:13 ` [PATCH 1/2] perf tools: Fix memory leak in addr2inlines() Jiri Olsa
2017-11-01 14:42   ` Arnaldo Carvalho de Melo
2017-11-03 14:23 ` [tip:perf/core] perf srcline: " tip-bot for Namhyung Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).