Skip to contentSkip to conversation

2 memory fixes and a build fix

PATCH Superseded Ian Rogers 4 participants 18 messages Sep 24, 2024 – Oct 2, 2024 perf events

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Ian Rogers

author reviewer
#0 lore raw

cover 2 memory fixes and a build fix

I was looking into some lsan regressions and a latent issue with libdw, creating these fixes.

A thought, we should probably simplify the libdw logic but rather than do it here I'll do it as a separate series on top of these. The issues I see are:

1) dwfl_thread_getframes is used to test for the presence of libdw-dwarf-unwind. The blame date on this function is 2013-05-30. As the function is 10 years old I think having libdw implies having dwfl_thread_getframes and so we can just merge the two pieces of logic instead of having different feature tests and ifdefs.

2) similarly, dwarf_getlocations has a blame date of 2013-08-23 so let's just make libdw tests test for this and make having libdw imply dwarf_getlocations support.

3) similarly, dwarf_getcfi has a blame date of 2009-06-24 so let's just make libdw tests test for this and make having libdw imply dwarf_getcfi support.

4) in Makefie.config feature-dwarf is a synonym for libdw support. I think using the name libdw is more intention revealing as dwarf can mean multiple things. Let's change HAVE_DWARF_SUPPORT to HAVE_LIBDW_SUPPORT and all similar dwarf vs libdw names.

5) We have "#if _ELFUTILS_PREREQ(0, 142)" testing for elfutils version 0.142. Elfutils 0.142 was released around 2009-06-13 (via git blame on the NEWS file). Let's remove the #if and ensure elfutils feature tests for at least 0.142. If someone were using an incredibly old version then they'd lose some elfutils support, but given the 15 year old age of the library I find it unlikely anyone is doing this. They can also just move to a newer version.

From the mailing list I notice also overlap with the last patch and this series:
https://lore.kernel.org/lkml/[email protected]/
Simplifying the libdw support will address some of those issues too.

Ian Rogers (3):
perf disasm: Fix capstone memory leak perf probe: Fix libdw memory leak
perf build: Fix !HAVE_DWARF_GETLOCATIONS_SUPPORT

 tools/perf/Makefile.config     |  6 ++++++
 tools/perf/util/disasm.c       | 11 +++++++----
 tools/perf/util/dwarf-aux.h    |  1 +
 tools/perf/util/probe-finder.c |  5 +++++
 4 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.0.792.g87dc391469-goog
author reviewer
#1 lore raw

PATCH 1/3 perf disasm: Fix capstone memory leak

The insn argument passed to cs_disasm needs freeing. To support accurately having count, add an additional free_count variable.

Fixes c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
---
 tools/perf/util/disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/disasm.c +7 -4
index f05ba7739c1e..2c8063660f2e 100644
@@ -1627,12 +1627,12 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	u64 start = map__rip_2objdump(map, sym->start);
 	u64 len;
 	u64 offset;
-	int i, count;
+	int i, count, free_count;
 	bool is_64bit = false;
 	bool needs_cs_close = false;
 	u8 *buf = NULL;
 	csh handle;
-	cs_insn *insn;
+	cs_insn *insn = NULL;
 	char disasm_buf[512];
 	struct disasm_line *dl;
 
@@ -1664,7 +1664,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 
 	needs_cs_close = true;
 
-	count = cs_disasm(handle, buf, len, start, len, &insn);
+	free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
 	for (i = 0, offset = 0; i < count; i++) {
 		int printed;
 
@@ -1702,8 +1702,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	}
 
 out:
-	if (needs_cs_close)
+	if (needs_cs_close) {
 		cs_close(&handle);
+		if (free_count > 0)
+			cs_free(insn, free_count);
+	}
 	free(buf);
 	return count < 0 ? count : 0;
 
Signature · 1 line
2.46.0.792.g87dc391469-goog
maintainer
#2 lore raw

Re: [PATCH v1 1/3] perf disasm: Fix capstone memory leak

Quoted 38 lines · Ian Rogers

The insn argument passed to cs_disasm needs freeing. To support accurately having count, add an additional free_count variable.

Fixes c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
---
 tools/perf/util/disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/disasm.c +3 -3
index f05ba7739c1e..2c8063660f2e 100644
@@ -1627,12 +1627,12 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	u64 start = map__rip_2objdump(map, sym->start);
 	u64 len;
 	u64 offset;
-	int i, count;
+	int i, count, free_count;
 	bool is_64bit = false;
 	bool needs_cs_close = false;
 	u8 *buf = NULL;
 	csh handle;
-	cs_insn *insn;
+	cs_insn *insn = NULL;
 	char disasm_buf[512];
 	struct disasm_line *dl;
 
@@ -1664,7 +1664,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 
 	needs_cs_close = true;
 
-	count = cs_disasm(handle, buf, len, start, len, &insn);
+	free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
 	for (i = 0, offset = 0; i < count; i++) {
 		int printed;
 

tools/perf/util/disasm.c static int symbol__disassemble_capstone(char *filename, struct symbol *sym, · line 1708

Quoted 8 lines
tools/perf/util/disasm.c +3 -1
@@ -1702,8 +1702,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 	}
 
 out:
-	if (needs_cs_close)
+	if (needs_cs_close) {
 		cs_close(&handle);
+		if (free_count > 0)
+			cs_free(insn, free_count);

It seems cs_free() can handle NULL insn and 0 free_count (like regular free) so we can call it unconditionally.

Thanks,
Namhyung

Quoted 7 lines
tools/perf/util/disasm.c +1 -0
+	}
 	free(buf);
 	return count < 0 ? count : 0;
 
Signature · 1 line
2.46.0.792.g87dc391469-goog
author reviewer
#3 lore raw

Re: [PATCH v1 1/3] perf disasm: Fix capstone memory leak

Quoted 42 lines · Namhyung Kim
Quoted 40 lines · Ian Rogers

The insn argument passed to cs_disasm needs freeing. To support accurately having count, add an additional free_count variable.

Fixes c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
---
 tools/perf/util/disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/disasm.c +3 -3
index f05ba7739c1e..2c8063660f2e 100644
@@ -1627,12 +1627,12 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
      u64 start = map__rip_2objdump(map, sym->start);
      u64 len;
      u64 offset;
-     int i, count;
+     int i, count, free_count;
      bool is_64bit = false;
      bool needs_cs_close = false;
      u8 *buf = NULL;
      csh handle;
-     cs_insn *insn;
+     cs_insn *insn = NULL;
      char disasm_buf[512];
      struct disasm_line *dl;
 
@@ -1664,7 +1664,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 
      needs_cs_close = true;
 
-     count = cs_disasm(handle, buf, len, start, len, &insn);
+     free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
      for (i = 0, offset = 0; i < count; i++) {
              int printed;
 
@@ -1702,8 +1702,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
      }
 
Quoted 9 lines

tools/perf/util/disasm.c static int symbol__disassemble_capstone(char *filename, struct symbol *sym, · line 1708

Quoted 6 lines
tools/perf/util/disasm.c +3 -1
 out:
-     if (needs_cs_close)
+     if (needs_cs_close) {
              cs_close(&handle);
+             if (free_count > 0)
+                     cs_free(insn, free_count);

It seems cs_free() can handle NULL insn and 0 free_count (like regular free) so we can call it unconditionally.

No, on error from cs_disasm free_count gets assigned -1 and my experience was things crashing.

Thanks,
Ian

Quoted 8 lines
Quoted 8 lines
tools/perf/util/disasm.c +1 -1
+     }
      free(buf);
      return count < 0 ? count : 0;
 
--

2.46.0.792.g87dc391469-goog

maintainer
#4 lore raw

Re: [PATCH v1 1/3] perf disasm: Fix capstone memory leak

Quoted 53 lines · Ian Rogers
Quoted 42 lines · Namhyung Kim
Quoted 40 lines · Ian Rogers

The insn argument passed to cs_disasm needs freeing. To support accurately having count, add an additional free_count variable.

Fixes c5d60de1813a ("perf annotate: Add support to use libcapstone in powerpc")
---
 tools/perf/util/disasm.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
tools/perf/util/disasm.c +3 -3
index f05ba7739c1e..2c8063660f2e 100644
@@ -1627,12 +1627,12 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
      u64 start = map__rip_2objdump(map, sym->start);
      u64 len;
      u64 offset;
-     int i, count;
+     int i, count, free_count;
      bool is_64bit = false;
      bool needs_cs_close = false;
      u8 *buf = NULL;
      csh handle;
-     cs_insn *insn;
+     cs_insn *insn = NULL;
      char disasm_buf[512];
      struct disasm_line *dl;
 
@@ -1664,7 +1664,7 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
 
      needs_cs_close = true;
 
-     count = cs_disasm(handle, buf, len, start, len, &insn);
+     free_count = count = cs_disasm(handle, buf, len, start, len, &insn);
      for (i = 0, offset = 0; i < count; i++) {
              int printed;
 
@@ -1702,8 +1702,11 @@ static int symbol__disassemble_capstone(char *filename, struct symbol *sym,
      }
 
Quoted 9 lines

tools/perf/util/disasm.c static int symbol__disassemble_capstone(char *filename, struct symbol *sym, · line 1708

Quoted 6 lines
tools/perf/util/disasm.c +3 -1
 out:
-     if (needs_cs_close)
+     if (needs_cs_close) {
              cs_close(&handle);
+             if (free_count > 0)
+                     cs_free(insn, free_count);

It seems cs_free() can handle NULL insn and 0 free_count (like regular free) so we can call it unconditionally.

Quoted 2 lines

No, on error from cs_disasm free_count gets assigned -1 and my experience was things crashing.

Ok then, I thought it returns 0 on error.

Thanks,
Namhyung

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
tools/perf/util/disasm.c +1 -1
+     }
      free(buf);
      return count < 0 ? count : 0;
 
--

2.46.0.792.g87dc391469-goog

author reviewer
#5 lore raw

PATCH 2/3 perf probe: Fix libdw memory leak

In reply to Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
 tools/perf/util/probe-finder.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +5 -0
index 630e16c54ed5..78f34fa0c391 100644
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
 	if (ret >= 0 && tf.pf.skip_empty_arg)
 		ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
+#if _ELFUTILS_PREREQ(0, 142)
+	dwarf_cfi_end(tf.pf.cfi_eh);
+	dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif
+
 	if (ret < 0 || tf.ntevs == 0) {
 		for (i = 0; i < tf.ntevs; i++)
 			clear_probe_trace_event(&tf.tevs[i]);
Signature · 1 line
2.46.0.792.g87dc391469-goog
reviewer
#6 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 15 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
  tools/perf/util/probe-finder.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +0 -0
index 630e16c54ed5..78f34fa0c391 100644

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1386

Quoted 8 lines
tools/perf/util/probe-finder.c +5 -0
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
  	if (ret >= 0 && tf.pf.skip_empty_arg)
  		ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
+#if _ELFUTILS_PREREQ(0, 142)
+	dwarf_cfi_end(tf.pf.cfi_eh);
+	dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif
+

I noticed that c06547d converted an _ELFUTILS_PREREQ(0, 142) into HAVE_DWARF_CFI_SUPPORT. But there is still a mixture of both in the code (unrelated to this patch). The commit message doesn't say why it is better, just that it could be changed, so I'm not sure which one is right.

maintainer
#7 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 22 lines · James Clark
Quoted 19 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
  tools/perf/util/probe-finder.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +2 -0
index 630e16c54ed5..78f34fa0c391 100644
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
  	if (ret >= 0 && tf.pf.skip_empty_arg)
  		ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
+#if _ELFUTILS_PREREQ(0, 142)
+	dwarf_cfi_end(tf.pf.cfi_eh);
Quoted 8 lines

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1386

Quoted 3 lines
tools/perf/util/probe-finder.c +3 -0
+	dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif
+

I noticed that c06547d converted an _ELFUTILS_PREREQ(0, 142) into HAVE_DWARF_CFI_SUPPORT. But there is still a mixture of both in the code (unrelated to this patch). The commit message doesn't say why it is better, just that it could be changed, so I'm not sure which one is right.

I think HAVE_DWARF_CFI_SUPPORT is better since it reveals the intention clearly.

Thanks,
Namhyung

author reviewer
#8 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 33 lines · Namhyung Kim
Quoted 22 lines · James Clark
Quoted 19 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
  tools/perf/util/probe-finder.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +2 -0
index 630e16c54ed5..78f34fa0c391 100644
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
    if (ret >= 0 && tf.pf.skip_empty_arg)
            ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
+#if _ELFUTILS_PREREQ(0, 142)
+   dwarf_cfi_end(tf.pf.cfi_eh);
Quoted 8 lines

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1386

Quoted 3 lines
tools/perf/util/probe-finder.c +3 -0
+   dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif
+

I noticed that c06547d converted an _ELFUTILS_PREREQ(0, 142) into HAVE_DWARF_CFI_SUPPORT. But there is still a mixture of both in the code (unrelated to this patch). The commit message doesn't say why it is better, just that it could be changed, so I'm not sure which one is right.

Quoted 2 lines

I think HAVE_DWARF_CFI_SUPPORT is better since it reveals the intention clearly.

Let's just nuke the conditional compilation, the CFI support is in libdw (calling it dwarf is just actively confusing) is 15 years old:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Thanks,
Ian

maintainer
#9 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 34 lines · Ian Rogers
Quoted 33 lines · Namhyung Kim
Quoted 30 lines · James Clark
Quoted 15 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
  tools/perf/util/probe-finder.c | 5 +++++
  1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +0 -0
index 630e16c54ed5..78f34fa0c391 100644

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1385

Quoted 7 lines
tools/perf/util/probe-finder.c +5 -0
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
    if (ret >= 0 && tf.pf.skip_empty_arg)
            ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
+#if _ELFUTILS_PREREQ(0, 142)
+   dwarf_cfi_end(tf.pf.cfi_eh);
+   dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif
+

I noticed that c06547d converted an _ELFUTILS_PREREQ(0, 142) into HAVE_DWARF_CFI_SUPPORT. But there is still a mixture of both in the code (unrelated to this patch). The commit message doesn't say why it is better, just that it could be changed, so I'm not sure which one is right.

Quoted 7 lines
Quoted 2 lines

I think HAVE_DWARF_CFI_SUPPORT is better since it reveals the intention clearly.

Let's just nuke the conditional compilation, the CFI support is in libdw (calling it dwarf is just actively confusing) is 15 years old:
https://lore.kernel.org/lkml/[email protected]/
https://lore.kernel.org/lkml/[email protected]/

Sounds good as long as we checks the version or the API in the feature test.

Thanks,
Namhyung

#10 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

In reply to Ian Rogers
Quoted 16 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
 tools/perf/util/probe-finder.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +0 -0
index 630e16c54ed5..78f34fa0c391 100644

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1385

Quoted 7 lines
tools/perf/util/probe-finder.c +4 -0
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
        if (ret >= 0 && tf.pf.skip_empty_arg)
                ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
+#if _ELFUTILS_PREREQ(0, 142)
+       dwarf_cfi_end(tf.pf.cfi_eh);
+       dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif

This is causing another problem. Now vfs_getname tests are failing because perf probe aborts.

Thanks,
Namhyung

Quoted 7 lines
tools/perf/util/probe-finder.c +1 -1
+
        if (ret < 0 || tf.ntevs == 0) {
                for (i = 0; i < tf.ntevs; i++)
                        clear_probe_trace_event(&tf.tevs[i]);
--

2.46.0.792.g87dc391469-goog

author reviewer
#11 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 21 lines · Namhyung Kim
Quoted 19 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
 tools/perf/util/probe-finder.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +0 -0
index 630e16c54ed5..78f34fa0c391 100644
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
        if (ret >= 0 && tf.pf.skip_empty_arg)
                ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
Quoted 7 lines

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1385

Quoted 4 lines
tools/perf/util/probe-finder.c +4 -0
+#if _ELFUTILS_PREREQ(0, 142)
+       dwarf_cfi_end(tf.pf.cfi_eh);
+       dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif

This is causing another problem. Now vfs_getname tests are failing because perf probe aborts.

I wasn't able to reproduce but largely as the test skips. The variable is out of scope after the function so I'm struggling to see what the issue is.

Thanks,
Ian

maintainer
#12 lore raw

Re: [PATCH v1 2/3] perf probe: Fix libdw memory leak

Quoted 30 lines · Ian Rogers
Quoted 21 lines · Namhyung Kim
Quoted 19 lines · Ian Rogers

Add missing dwarf_cfi_end to free memory associated with probe_finder cfi_eh or cfi_dbg. This addresses leak sanitizer issues seen in:
tools/perf/tests/shell/test_uprobe_from_different_cu.sh

Fixes 270bde1e76f4 ("perf probe: Search both .eh_frame and .debug_frame sections for probe location")
---
 tools/perf/util/probe-finder.c | 5 +++++
 1 file changed, 5 insertions(+)
tools/perf/util/probe-finder.c +0 -0
index 630e16c54ed5..78f34fa0c391 100644
@@ -1379,6 +1379,11 @@ int debuginfo__find_trace_events(struct debuginfo *dbg,
        if (ret >= 0 && tf.pf.skip_empty_arg)
                ret = fill_empty_trace_arg(pev, tf.tevs, tf.ntevs);
 
Quoted 7 lines

tools/perf/util/probe-finder.c int debuginfo__find_trace_events(struct debuginfo *dbg, · line 1385

Quoted 4 lines
tools/perf/util/probe-finder.c +4 -0
+#if _ELFUTILS_PREREQ(0, 142)
+       dwarf_cfi_end(tf.pf.cfi_eh);
+       dwarf_cfi_end(tf.pf.cfi_dbg);
+#endif

This is causing another problem. Now vfs_getname tests are failing because perf probe aborts.

Quoted 3 lines

I wasn't able to reproduce but largely as the test skips. The variable is out of scope after the function so I'm struggling to see what the issue is.

I'm seeing this.

  $ sudo ./perf test -v vfs
   91: Add vfs_getname probe to get syscall args filenames:
  --- start ---
  test child forked, pid 3013362
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013380 Aborted                 perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string"
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013381 Aborted                 perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring"
  ---- end(-1) ----
   91: Add vfs_getname probe to get syscall args filenames             : FAILED!
   93: Use vfs_getname probe to get syscall args filenames:
  --- start ---
  test child forked, pid 3013479
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013502 Aborted                 perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string"
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013514 Aborted                 perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring"
  ---- end(-1) ----
   93: Use vfs_getname probe to get syscall args filenames             : FAILED!
  127: Check open filename arg using perf trace + vfs_getname:
  --- start ---
  test child forked, pid 3013528
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013547 Aborted                 perf probe -q "vfs_getname=getname_flags:${line} pathname=result->name:string"
  free(): invalid pointer
  linux/tools/perf/tests/shell/lib/probe_vfs_getname.sh: line 13: 3013548 Aborted                 perf probe $add_probe_verbose "vfs_getname=getname_flags:${line} pathname=filename:ustring"
  ---- end(-1) ----
  127: Check open filename arg using perf trace + vfs_getname          : FAILED!

Dropping the series from tmp.perf-tools-next for now.

Thanks,
Namhyung

author reviewer
#13 lore raw

PATCH 3/3 perf build: Fix !HAVE_DWARF_GETLOCATIONS_SUPPORT

In reply to Ian Rogers

Testing with a LIBDW_DIR showed some issues. In dwarf-aux.h if HAVE_DWARF_GETLOCATIONS_SUPPORT isn't defined then the code uses an undefined errno value, so add errno.h.
In Makefile.config the dwarf feature tests need the LIBDW_DIR setting in the CFLAGS/LDFLAGS.

---
 tools/perf/Makefile.config  | 6 ++++++
 tools/perf/util/dwarf-aux.h | 1 +
 2 files changed, 7 insertions(+)
tools/perf/Makefile.config +6 -0
index 4dcf7a0fd235..5e26d3a91b36 100644
@@ -159,8 +159,14 @@ ifeq ($(findstring -static,${LDFLAGS}),-static)
     DWARFLIBS += -lebl
   endif
 endif
+FEATURE_CHECK_CFLAGS-dwarf := $(LIBDW_CFLAGS)
+FEATURE_CHECK_LDFLAGS-dwarf := $(LIBDW_LDFLAGS) $(DWARFLIBS)
 FEATURE_CHECK_CFLAGS-libdw-dwarf-unwind := $(LIBDW_CFLAGS)
 FEATURE_CHECK_LDFLAGS-libdw-dwarf-unwind := $(LIBDW_LDFLAGS) $(DWARFLIBS)
+FEATURE_CHECK_CFLAGS-dwarf_getlocations := $(LIBDW_CFLAGS)
+FEATURE_CHECK_LDFLAGS-dwarf_getlocations := $(LIBDW_LDFLAGS) $(DWARFLIBS)
+FEATURE_CHECK_CFLAGS-dwarf_getcfi := $(LIBDW_CFLAGS)
+FEATURE_CHECK_LDFLAGS-dwarf_getcfi := $(LIBDW_LDFLAGS) $(DWARFLIBS)
 
 # for linking with debug library, run like:
 # make DEBUG=1 LIBBABELTRACE_DIR=/opt/libbabeltrace/
tools/perf/util/dwarf-aux.h +1 -0
index 336a3a183a78..925a9bb9fb15 100644
@@ -177,6 +177,7 @@ void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types);
 void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_types);
 
 #else /*  HAVE_DWARF_GETLOCATIONS_SUPPORT */
+#include <errno.h>
 
 static inline int die_get_var_range(Dwarf_Die *sp_die __maybe_unused,
 				    Dwarf_Die *vr_die __maybe_unused,
Signature · 1 line
2.46.0.792.g87dc391469-goog
reviewer Reviewed-by on the v1 series
#14 lore raw
In reply to Ian Rogers
Quoted 45 lines · Ian Rogers

I was looking into some lsan regressions and a latent issue with libdw, creating these fixes.

A thought, we should probably simplify the libdw logic but rather than do it here I'll do it as a separate series on top of these. The issues I see are:

1) dwfl_thread_getframes is used to test for the presence of libdw-dwarf-unwind. The blame date on this function is 2013-05-30. As the function is 10 years old I think having libdw implies having dwfl_thread_getframes and so we can just merge the two pieces of logic instead of having different feature tests and ifdefs.

2) similarly, dwarf_getlocations has a blame date of 2013-08-23 so let's just make libdw tests test for this and make having libdw imply dwarf_getlocations support.

3) similarly, dwarf_getcfi has a blame date of 2009-06-24 so let's just make libdw tests test for this and make having libdw imply dwarf_getcfi support.

4) in Makefie.config feature-dwarf is a synonym for libdw support. I think using the name libdw is more intention revealing as dwarf can mean multiple things. Let's change HAVE_DWARF_SUPPORT to HAVE_LIBDW_SUPPORT and all similar dwarf vs libdw names.

5) We have "#if _ELFUTILS_PREREQ(0, 142)" testing for elfutils version 0.142. Elfutils 0.142 was released around 2009-06-13 (via git blame on the NEWS file). Let's remove the #if and ensure elfutils feature tests for at least 0.142. If someone were using an incredibly old version then they'd lose some elfutils support, but given the 15 year old age of the library I find it unlikely anyone is doing this. They can also just move to a newer version.

From the mailing list I notice also overlap with the last patch and
this series:
https://lore.kernel.org/lkml/[email protected]/
Simplifying the libdw support will address some of those issues too.

Ian Rogers (3):
perf disasm: Fix capstone memory leak perf probe: Fix libdw memory leak
perf build: Fix !HAVE_DWARF_GETLOCATIONS_SUPPORT

Quoted 6 lines
  tools/perf/Makefile.config     |  6 ++++++
  tools/perf/util/disasm.c       | 11 +++++++----
  tools/perf/util/dwarf-aux.h    |  1 +
  tools/perf/util/probe-finder.c |  5 +++++
  4 files changed, 19 insertions(+), 4 deletions(-)
maintainer
#15 lore raw
In reply to Ian Rogers
Quoted 27 lines · Ian Rogers

I was looking into some lsan regressions and a latent issue with libdw, creating these fixes.

A thought, we should probably simplify the libdw logic but rather than do it here I'll do it as a separate series on top of these. The issues I see are:

1) dwfl_thread_getframes is used to test for the presence of libdw-dwarf-unwind. The blame date on this function is 2013-05-30. As the function is 10 years old I think having libdw implies having dwfl_thread_getframes and so we can just merge the two pieces of logic instead of having different feature tests and ifdefs.

2) similarly, dwarf_getlocations has a blame date of 2013-08-23 so let's just make libdw tests test for this and make having libdw imply dwarf_getlocations support.

3) similarly, dwarf_getcfi has a blame date of 2009-06-24 so let's just make libdw tests test for this and make having libdw imply dwarf_getcfi support.

4) in Makefie.config feature-dwarf is a synonym for libdw support. I think using the name libdw is more intention revealing as dwarf can mean multiple things. Let's change HAVE_DWARF_SUPPORT to HAVE_LIBDW_SUPPORT and all similar dwarf vs libdw names.

Quoted 7 lines

5) We have "#if _ELFUTILS_PREREQ(0, 142)" testing for elfutils version 0.142. Elfutils 0.142 was released around 2009-06-13 (via git blame on the NEWS file). Let's remove the #if and ensure elfutils feature tests for at least 0.142. If someone were using an incredibly old version then they'd lose some elfutils support, but given the 15 year old age of the library I find it unlikely anyone is doing this. They can also just move to a newer version.

Looking at the map file in libdw, the latest addition was 0.158 for dwfl_thread_getframes(). Probably we can add the version check to the feature test to make sure if it has all the required APIs.

https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdw/libdw.map;h=552588a94c0c1a1f2fd5b973553c784026e6de14;hb=HEAD#l274

Quoted 5 lines

From the mailing list I notice also overlap with the last patch and this series:
https://lore.kernel.org/lkml/[email protected]/
Simplifying the libdw support will address some of those issues too.

Yeah I noticed that too and feel like it should go to perf-tools tree. Probably it doesn't clash with this so I think it's ok to have this in perf-tools-next.

Thanks,
Namhyung

Quoted 15 lines

Ian Rogers (3):
perf disasm: Fix capstone memory leak perf probe: Fix libdw memory leak
perf build: Fix !HAVE_DWARF_GETLOCATIONS_SUPPORT

 tools/perf/Makefile.config     |  6 ++++++
 tools/perf/util/disasm.c       | 11 +++++++----
 tools/perf/util/dwarf-aux.h    |  1 +
 tools/perf/util/probe-finder.c |  5 +++++
 4 files changed, 19 insertions(+), 4 deletions(-)
Signature · 1 line
2.46.0.792.g87dc391469-goog
author reviewer
#16 lore raw
Quoted 44 lines · Namhyung Kim
Quoted 27 lines · Ian Rogers

I was looking into some lsan regressions and a latent issue with libdw, creating these fixes.

A thought, we should probably simplify the libdw logic but rather than do it here I'll do it as a separate series on top of these. The issues I see are:

1) dwfl_thread_getframes is used to test for the presence of libdw-dwarf-unwind. The blame date on this function is 2013-05-30. As the function is 10 years old I think having libdw implies having dwfl_thread_getframes and so we can just merge the two pieces of logic instead of having different feature tests and ifdefs.

2) similarly, dwarf_getlocations has a blame date of 2013-08-23 so let's just make libdw tests test for this and make having libdw imply dwarf_getlocations support.

3) similarly, dwarf_getcfi has a blame date of 2009-06-24 so let's just make libdw tests test for this and make having libdw imply dwarf_getcfi support.

4) in Makefie.config feature-dwarf is a synonym for libdw support. I think using the name libdw is more intention revealing as dwarf can mean multiple things. Let's change HAVE_DWARF_SUPPORT to HAVE_LIBDW_SUPPORT and all similar dwarf vs libdw names.

Quoted 7 lines

5) We have "#if _ELFUTILS_PREREQ(0, 142)" testing for elfutils version 0.142. Elfutils 0.142 was released around 2009-06-13 (via git blame on the NEWS file). Let's remove the #if and ensure elfutils feature tests for at least 0.142. If someone were using an incredibly old version then they'd lose some elfutils support, but given the 15 year old age of the library I find it unlikely anyone is doing this. They can also just move to a newer version.

Looking at the map file in libdw, the latest addition was 0.158 for dwfl_thread_getframes(). Probably we can add the version check to the feature test to make sure if it has all the required APIs.

https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdw/libdw.map;h=552588a94c0c1a1f2fd5b973553c784026e6de14;hb=HEAD#l274

Quoted 1 line
Quoted 8 lines
Quoted 4 lines

From the mailing list I notice also overlap with the last patch and this series:
https://lore.kernel.org/lkml/[email protected]/
Simplifying the libdw support will address some of those issues too.

Yeah I noticed that too and feel like it should go to perf-tools tree. Probably it doesn't clash with this so I think it's ok to have this in perf-tools-next.

I think the comments wrt libdw are covered in the series cleaning up libdw:
https://lore.kernel.org/lkml/[email protected]/
so these fixes should be good to land?

Thanks,
Ian

maintainer
#17 lore raw
Quoted 50 lines · Ian Rogers
Quoted 49 lines · Namhyung Kim
Quoted 27 lines · Ian Rogers

I was looking into some lsan regressions and a latent issue with libdw, creating these fixes.

A thought, we should probably simplify the libdw logic but rather than do it here I'll do it as a separate series on top of these. The issues I see are:

1) dwfl_thread_getframes is used to test for the presence of libdw-dwarf-unwind. The blame date on this function is 2013-05-30. As the function is 10 years old I think having libdw implies having dwfl_thread_getframes and so we can just merge the two pieces of logic instead of having different feature tests and ifdefs.

2) similarly, dwarf_getlocations has a blame date of 2013-08-23 so let's just make libdw tests test for this and make having libdw imply dwarf_getlocations support.

3) similarly, dwarf_getcfi has a blame date of 2009-06-24 so let's just make libdw tests test for this and make having libdw imply dwarf_getcfi support.

4) in Makefie.config feature-dwarf is a synonym for libdw support. I think using the name libdw is more intention revealing as dwarf can mean multiple things. Let's change HAVE_DWARF_SUPPORT to HAVE_LIBDW_SUPPORT and all similar dwarf vs libdw names.

Quoted 7 lines

5) We have "#if _ELFUTILS_PREREQ(0, 142)" testing for elfutils version 0.142. Elfutils 0.142 was released around 2009-06-13 (via git blame on the NEWS file). Let's remove the #if and ensure elfutils feature tests for at least 0.142. If someone were using an incredibly old version then they'd lose some elfutils support, but given the 15 year old age of the library I find it unlikely anyone is doing this. They can also just move to a newer version.

Looking at the map file in libdw, the latest addition was 0.158 for dwfl_thread_getframes(). Probably we can add the version check to the feature test to make sure if it has all the required APIs.

https://sourceware.org/git/?p=elfutils.git;a=blob;f=libdw/libdw.map;h=552588a94c0c1a1f2fd5b973553c784026e6de14;hb=HEAD#l274

Quoted 5 lines

From the mailing list I notice also overlap with the last patch and this series:
https://lore.kernel.org/lkml/[email protected]/
Simplifying the libdw support will address some of those issues too.

Quoted 7 lines
Quoted 3 lines

Yeah I noticed that too and feel like it should go to perf-tools tree. Probably it doesn't clash with this so I think it's ok to have this in perf-tools-next.

I think the comments wrt libdw are covered in the series cleaning up libdw:
https://lore.kernel.org/lkml/[email protected]/
so these fixes should be good to land?

Sure, I'll just adjust the errno.h part as it's picked up by Arnaldo to the perf-tools already. I'll merge the branch later.

Thanks,
Namhyung