Repository: google/pprof
Pull request #924 (2025-01-23 00:55:39)

handle demangling OSX names with underscore prefix
See discussion at https://github.com/google/pprof/issues/477#issuecomment-2600988101

codecov-commenter: ## [Codecov](https://app.codecov.io/gh/google/pprof/pull/924?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 66.96%. Comparing base [(`0ed6a68`)](https://app.codecov.io/gh/google/pprof/commit/0ed6a68c8d9e6d5df69a1f2694c7b5c3297fdf4f?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) to head [(`85c67d0`)](https://app.codecov.io/gh/google/pprof/commit/85c67d0d01b516ebf2d5404f0910f62c9bed7b13?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> Report is 61 commits behind head on main.

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #924      +/-   ##
==========================================
+ Coverage   66.86%   66.96%   +0.09%     
==========================================
  Files          44       44              
  Lines        9824     9864      +40     
==========================================
+ Hits         6569     6605      +36     
- Misses       2794     2820      +26     
+ Partials      461      439      -22     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/google/pprof/pull/924?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).

alk: The failure looks like a flake. The failed test looks unrelated to my change. But do let me know if I am mistaken. And if there is anything I can do to help.
aalexand: > The failure looks like a flake. The failed test looks unrelated to my change. But do let me know if I am mistaken. And if there is anything I can do to help.

Right, it's a known recent flaky error, I filed #925 to track it. I restarted the failed test.
alk: Thanks. Test passed now. So what happens next ?

Patch:
diff --git a/internal/symbolizer/symbolizer.go b/internal/symbolizer/symbolizer.go
index fd6e015..95c15b1 100644
--- a/internal/symbolizer/symbolizer.go
+++ b/internal/symbolizer/symbolizer.go
@@ -292,6 +292,16 @@ func demangleSingleFunction(fn *profile.Function, options []demangle.Option) {
 		fn.Name = demangled
 		return
 	}
+
+	// OSX has all the symbols prefixed with extra '_' so lets try
+	// once more without it
+	if strings.HasPrefix(fn.SystemName, "_") {
+		if demangled := demangle.Filter(fn.SystemName[1:], o...); demangled != fn.SystemName {
+			fn.Name = demangled
+			return
+		}
+	}
+
 	// Could not demangle. Apply heuristics in case the name is
 	// already demangled.
 	name := fn.SystemName


Test patch:
diff --git a/internal/symbolizer/symbolizer_test.go b/internal/symbolizer/symbolizer_test.go
index 88bc1dc..ae2a99e 100644
--- a/internal/symbolizer/symbolizer_test.go
+++ b/internal/symbolizer/symbolizer_test.go
@@ -375,6 +375,11 @@ func TestDemangleSingleFunction(t *testing.T) {
 			symbol: "_ZdaPv",
 			want:   "operator delete[]",
 		},
+		{
+			// OSX prepends extra '_', which we're not able to remove. But we handle it when demangling.
+			symbol: "__ZdaPv",
+			want:   "operator delete[]",
+		},
 		{
 			// Already demangled.
 			symbol: "operator delete[](void*)",