Skip to content

Improve handling of Go symbols with type parameters #717

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions internal/graph/dotgraph.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,9 @@ func multilinePrintableName(info *NodeInfo) string {
infoCopy := *info
infoCopy.Name = escapeForDot(ShortenFunctionName(infoCopy.Name))
infoCopy.Name = strings.Replace(infoCopy.Name, "::", `\n`, -1)
// Go type parameters are reported as "[...]" by Go pprof profiles.
// Keep this ellipsis rather than replacing with newlines below.
infoCopy.Name = strings.Replace(infoCopy.Name, "[...]", "[…]", -1)
infoCopy.Name = strings.Replace(infoCopy.Name, ".", `\n`, -1)
if infoCopy.File != "" {
infoCopy.File = filepath.Base(infoCopy.File)
Expand Down
4 changes: 4 additions & 0 deletions internal/graph/graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,10 @@ func TestShortenFunctionName(t *testing.T) {
"github.com/BlahBlah/foo.Foo",
"foo.Foo",
},
{
"github.com/BlahBlah/foo.Foo[...]",
"foo.Foo[...]",
},
{
"github.com/blah-blah/foo_bar.(*FooBar).Foo",
"foo_bar.(*FooBar).Foo",
Expand Down
61 changes: 38 additions & 23 deletions internal/symbolizer/symbolizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,49 +205,64 @@ func Demangle(prof *profile.Profile, force bool, demanglerMode string) {
}
}

var options []demangle.Option
options := demanglerModeToOptions(demanglerMode)
for _, fn := range prof.Function {
demangleSingleFunction(fn, options)
}
}

func demanglerModeToOptions(demanglerMode string) []demangle.Option {
switch demanglerMode {
case "": // demangled, simplified: no parameters, no templates, no return type
options = []demangle.Option{demangle.NoParams, demangle.NoTemplateParams}
return []demangle.Option{demangle.NoParams, demangle.NoTemplateParams}
case "templates": // demangled, simplified: no parameters, no return type
options = []demangle.Option{demangle.NoParams}
return []demangle.Option{demangle.NoParams}
case "full":
options = []demangle.Option{demangle.NoClones}
return []demangle.Option{demangle.NoClones}
case "none": // no demangling
return
return []demangle.Option{}
}

panic(fmt.Sprintf("unknown demanglerMode %s", demanglerMode))
}

func demangleSingleFunction(fn *profile.Function, options []demangle.Option) {
if fn.Name != "" && fn.SystemName != fn.Name {
return // Already demangled.
}
// Copy the options because they may be updated by the call.
o := make([]demangle.Option, len(options))
for _, fn := range prof.Function {
if fn.Name != "" && fn.SystemName != fn.Name {
continue // Already demangled.
}
copy(o, options)
if demangled := demangle.Filter(fn.SystemName, o...); demangled != fn.SystemName {
fn.Name = demangled
continue
}
// Could not demangle. Apply heuristics in case the name is
// already demangled.
name := fn.SystemName
if looksLikeDemangledCPlusPlus(name) {
if demanglerMode == "" || demanglerMode == "templates" {
copy(o, options)
if demangled := demangle.Filter(fn.SystemName, o...); demangled != fn.SystemName {
fn.Name = demangled
return
}
// Could not demangle. Apply heuristics in case the name is
// already demangled.
name := fn.SystemName
if looksLikeDemangledCPlusPlus(name) {
for _, o := range options {
switch o {
case demangle.NoParams:
name = removeMatching(name, '(', ')')
}
if demanglerMode == "" {
case demangle.NoTemplateParams:
name = removeMatching(name, '<', '>')
}
}
fn.Name = name
}
fn.Name = name
}

// looksLikeDemangledCPlusPlus is a heuristic to decide if a name is
// the result of demangling C++. If so, further heuristics will be
// applied to simplify the name.
func looksLikeDemangledCPlusPlus(demangled string) bool {
if strings.Contains(demangled, ".<") { // Skip java names of the form "class.<init>"
// Skip java names of the form "class.<init>".
if strings.Contains(demangled, ".<") {
return false
}
// Skip Go names of the form "foo.(*Bar[...]).Method".
if strings.Contains(demangled, "]).") {
return false
}
return strings.ContainsAny(demangled, "<>[]") || strings.Contains(demangled, "::")
Expand Down
112 changes: 112 additions & 0 deletions internal/symbolizer/symbolizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,118 @@ func frame(fname, file string, line int) plugin.Frame {
Line: line}
}

func TestDemangleSingleFunction(t *testing.T) {
// All tests with default mode.
demanglerMode := ""
options := demanglerModeToOptions(demanglerMode)

cases := []struct {
symbol string
want string
}{
{
// Trivial C symbol.
symbol: "printf",
want: "printf",
},
{
// foo::bar(int)
symbol: "_ZN3foo3barEi",
want: "foo::bar",
},
{
// Already demangled.
symbol: "foo::bar(int)",
want: "foo::bar",
},
{
// int foo::baz<double>(double)
symbol: "_ZN3foo3bazIdEEiT",
want: "foo::baz",
},
{
// Already demangled.
//
// TODO: The demangled form of this is actually
// 'int foo::baz<double>(double)', but our heuristic
// can't strip the return type. Should it be able to?
symbol: "foo::baz<double>(double)",
want: "foo::baz",
},
{
// operator delete[](void*)
symbol: "_ZdaPv",
want: "operator delete[]",
},
{
// Already demangled.
symbol: "operator delete[](void*)",
want: "operator delete[]",
},
{
// bar(int (*) [5])
symbol: "_Z3barPA5_i",
want: "bar",
},
{
// Already demangled.
symbol: "bar(int (*) [5])",
want: "bar",
},
// Java symbols, do not demangle.
{
symbol: "java.lang.Float.parseFloat",
want: "java.lang.Float.parseFloat",
},
{
symbol: "java.lang.Float.<init>",
want: "java.lang.Float.<init>",
},
// Go symbols, do not demangle.
{
symbol: "example.com/foo.Bar",
want: "example.com/foo.Bar",
},
{
symbol: "example.com/foo.(*Bar).Bat",
want: "example.com/foo.(*Bar).Bat",
},
{
// Method on type with type parameters, as reported by
// Go pprof profiles (simplified symbol name).
symbol: "example.com/foo.(*Bar[...]).Bat",
want: "example.com/foo.(*Bar[...]).Bat",
},
{
// Method on type with type parameters, as reported by
// perf profiles (actual symbol name).
symbol: "example.com/foo.(*Bar[go.shape.string_0,go.shape.int_1]).Bat",
want: "example.com/foo.(*Bar[go.shape.string_0,go.shape.int_1]).Bat",
},
{
// Function with type parameters, as reported by Go
// pprof profiles (simplified symbol name).
symbol: "example.com/foo.Bar[...]",
want: "example.com/foo.Bar[...]",
},
{
// Function with type parameters, as reported by perf
// profiles (actual symbol name).
symbol: "example.com/foo.Bar[go.shape.string_0,go.shape.int_1]",
want: "example.com/foo.Bar[go.shape.string_0,go.shape.int_1]",
},
}
for _, tc := range cases {
fn := &profile.Function{
SystemName: tc.symbol,
}
demangleSingleFunction(fn, options)
if fn.Name != tc.want {
t.Errorf("demangleSingleFunction(%s) got %s want %s", tc.symbol, fn.Name, tc.want)
}
}
}

type mockObjTool struct{}

func (mockObjTool) Open(file string, start, limit, offset uint64, relocationSymbol string) (plugin.ObjFile, error) {
Expand Down