Skip to contentSkip to conversation

perf dwarf-aux: Check .eh_frame section for CFA

PATCH New Namhyung Kim 2 participants 2 messages Oct 2 – Oct 3 perf events

lore
Share & tools
lore

Namhyung Kim

author maintainer
#0 lore raw

PATCH 1/1 perf dwarf-aux: Check .eh_frame section for CFA

The die_get_cfa() only calls dwarf_getcfi() which handles .debug_frame section. But most binaries have .eh_frame instead so it would fail. Let's call dwarf_getcfi_elf() first to try .eh_frame section and then fallback to .debug_frame.

Cc Masami Hiramatsu <[email protected]>
---
 tools/perf/util/dwarf-aux.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
tools/perf/util/dwarf-aux.c +8 -3
index 54f8b5ec74a2fe70..af3913e661fce79e 100644
@@ -1886,9 +1886,14 @@ int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset)
 	Dwarf_Op *ops = NULL;
 	size_t nops;
 
-	cfi = dwarf_getcfi(dwarf);
-	if (cfi == NULL)
-		return -1;
+	/* try to get .eh_frame section */
+	cfi = dwarf_getcfi_elf(dwarf_getelf(dwarf));
+	if (cfi == NULL) {
+		/* and then try .debug_frame section */
+		cfi = dwarf_getcfi(dwarf);
+		if (cfi == NULL)
+			return -1;
+	}
 
 	if (!dwarf_cfi_addrframe(cfi, pc, &frame) &&
 	    !dwarf_frame_cfa(frame, &ops, &nops) &&
Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] perf: memory leak of Dwarf_CFI handle in die_get_cfa() - [Medium] perf: severe performance…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] perf: memory leak of Dwarf_CFI handle in die_get_cfa()
- [Medium] perf: severe performance degradation from repeatedly parsing .eh_frame --

commit 63cd38e63157cedd873e0aa6adbf63746489d27e
Author: Namhyung Kim <[email protected]>

perf dwarf-aux: Check .eh_frame section for CFA

This commit modifies die_get_cfa() to first attempt to retrieve the .eh_frame section using dwarf_getcfi_elf() before falling back to the .debug_frame section.

[Severity: Medium]
Does this code leak the dynamically allocated Dwarf_CFI object?

In tools/perf/util/dwarf-aux.c within die_get_cfa(), dwarf_getcfi_elf() is called to get the .eh_frame section. Unlike dwarf_getcfi(), which returns a structure owned by the Dwarf context, dwarf_getcfi_elf() allocates a new Dwarf_CFI object that the caller must free using dwarf_cfi_end().

However, die_get_cfa() returns without freeing the cfi pointer on both the success path and the error path. Since this function is called repeatedly during data annotation, could this lead to rapid memory consumption?

[Severity: Medium]
Is there a risk of severe performance degradation here?

Calling dwarf_getcfi_elf() reads and parses the .eh_frame section to dynamically build a search index for the module. Because die_get_cfa() is a heavily utilized helper function often called repeatedly, rebuilding this index and allocating the structures from scratch on every single invocation could cause massive CPU overhead.

Would it be possible to cache the .eh_frame Dwarf_CFI object to avoid rebuilding it on every call?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1