Skip to content

Commit

Permalink
fix(stacktrace): support package name parsing for Go 1.20+ (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
greywolve authored Oct 9, 2023
1 parent 4c8f48a commit 592c7db
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 3 deletions.
4 changes: 1 addition & 3 deletions stacktrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ func callerFunctionName() string {
// It replicates https://golang.org/pkg/debug/gosym/#Sym.PackageName, avoiding a
// dependency on debug/gosym.
func packageName(name string) string {
// A prefix of "type." and "go." is a compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.") {
if isCompilerGeneratedSymbol(name) {
return ""
}

Expand Down
15 changes: 15 additions & 0 deletions stacktrace_below_go1.20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build !go1.20

package sentry

import "strings"

func isCompilerGeneratedSymbol(name string) bool {
// In versions of Go below 1.20 a prefix of "type." and "go." is a
// compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go.") || strings.HasPrefix(name, "type.") {
return true
}
return false
}
32 changes: 32 additions & 0 deletions stacktrace_below_go1.20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build !go1.20

package sentry

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestFilterCompilerGeneratedSymbols(t *testing.T) {
tests := []struct {
symbol string
expectedPackageName string
}{
{"type..eq.[9]debug/elf.intName", ""},
{"type..hash.debug/elf.ProgHeader", ""},
{"type..eq.runtime._panic", ""},
{"type..hash.struct { runtime.gList; runtime.n int32 }", ""},
{"go.(*struct { sync.Mutex; math/big.table [64]math/big", ""},
{"github.com/getsentry/sentry-go.Test.func2.1.1", "github.com/getsentry/sentry-go"},
}

for _, tt := range tests {
t.Run(tt.symbol, func(t *testing.T) {
packageName := packageName(tt.symbol)
if diff := cmp.Diff(tt.expectedPackageName, packageName); diff != "" {
t.Errorf("Package name mismatch (-want +got):\n%s", diff)
}
})
}
}
15 changes: 15 additions & 0 deletions stacktrace_go1.20.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//go:build go1.20

package sentry

import "strings"

func isCompilerGeneratedSymbol(name string) bool {
// In versions of Go 1.20 and above a prefix of "type:" and "go:" is a
// compiler-generated symbol that doesn't belong to any package.
// See variable reservedimports in cmd/compile/internal/gc/subr.go
if strings.HasPrefix(name, "go:") || strings.HasPrefix(name, "type:") {
return true
}
return false
}
32 changes: 32 additions & 0 deletions stacktrace_go1.20_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build go1.20

package sentry

import (
"testing"

"github.com/google/go-cmp/cmp"
)

func TestFilterCompilerGeneratedSymbols(t *testing.T) {
tests := []struct {
symbol string
expectedPackageName string
}{
{"type:.eq.[9]debug/elf.intName", ""},
{"type:.hash.debug/elf.ProgHeader", ""},
{"type:.eq.runtime._panic", ""},
{"type:.hash.struct { runtime.gList; runtime.n int32 }", ""},
{"go:(*struct { sync.Mutex; math/big.table [64]math/big", ""},
{"go.uber.org/zap/buffer.(*Buffer).AppendString", "go.uber.org/zap/buffer"},
}

for _, tt := range tests {
t.Run(tt.symbol, func(t *testing.T) {
packageName := packageName(tt.symbol)
if diff := cmp.Diff(tt.expectedPackageName, packageName); diff != "" {
t.Errorf("Package name mismatch (-want +got):\n%s", diff)
}
})
}
}

0 comments on commit 592c7db

Please sign in to comment.