Skip to content
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

Merge release-0.2 to main #359

Merged
merged 1 commit into from
Oct 23, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
66 changes: 39 additions & 27 deletions pkg/symbol/addr2line/go.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,56 @@ import (
"errors"
"fmt"

"github.com/go-kit/log"
"github.com/google/pprof/profile"
)

func Go(path string) (func(addr uint64) ([]profile.Line, error), error) {
type goLiner struct {
logger log.Logger

symtab *gosym.Table
}

func Go(logger log.Logger, path string) (*goLiner, error) {
tab, err := gosymtab(path)
if err != nil {
return nil, fmt.Errorf("failed to create go symbtab: %w", err)
}

return func(addr uint64) (lines []profile.Line, err error) {
defer func() {
// PCToLine panics with "invalid memory address or nil pointer dereference",
// - when it refers to an address that doesn't actually exist.
if r := recover(); r != nil {
err = fmt.Errorf("recovering from panic in go binary add2line: %v", r)
}
}()

file, line, fn := tab.PCToLine(addr)
name := "?"
if fn != nil {
name = fn.Name
} else {
file = "?"
line = 0
return &goLiner{
logger: logger,
symtab: tab,
}, nil
}

func (gl *goLiner) PCToLines(addr uint64) (lines []profile.Line, err error) {
defer func() {
// PCToLine panics with "invalid memory address or nil pointer dereference",
// - when it refers to an address that doesn't actually exist.
if r := recover(); r != nil {
err = fmt.Errorf("recovering from panic in go binary add2line: %v", r)
}
}()

file, line, fn := gl.symtab.PCToLine(addr)
name := "?"
if fn != nil {
name = fn.Name
} else {
file = "?"
line = 0
}

// TODO(kakkoyun): Find a way to symbolize inline functions.
lines = append(lines, profile.Line{
Line: int64(line),
Function: &profile.Function{
Name: name,
Filename: file,
},
})
// TODO(kakkoyun): Find a way to symbolize inline functions.
lines = append(lines, profile.Line{
Line: int64(line),
Function: &profile.Function{
Name: name,
Filename: file,
},
})

return lines, nil
}, nil
return lines, nil
}

func gosymtab(path string) (*gosym.Table, error) {
Expand Down
10 changes: 3 additions & 7 deletions pkg/symbol/symbol.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ type liner interface {
PCToLines(pc uint64) ([]profile.Line, error)
}

type funcLiner func(addr uint64) ([]profile.Line, error)

func (f funcLiner) PCToLines(pc uint64) ([]profile.Line, error) { return f(pc) }

func NewSymbolizer(logger log.Logger, opts ...Option) (*Symbolizer, error) {
log.With(logger, "component", "symbolizer")

Expand Down Expand Up @@ -104,7 +100,7 @@ func (s *Symbolizer) NewLiner(m *profile.Mapping, path string) (liner, error) {
lnr, err := s.newLiner(m, path)
if err != nil {
s.failed[h] = struct{}{}
s.cache.Invalidate(hash)
s.cache.Invalidate(h)
return nil, err
}

Expand Down Expand Up @@ -157,10 +153,10 @@ func (s *Symbolizer) newLiner(m *profile.Mapping, path string) (liner, error) {
// Right now, this uses "debug/gosym" package, and it won't work for inlined functions,
// so this is just a best-effort implementation, in case we don't have DWARF.
level.Debug(s.logger).Log("msg", "symbolizing a Go binary", "file", path)
f, err := addr2line.Go(path)
lnr, err := addr2line.Go(s.logger, path)
if err == nil {
level.Debug(s.logger).Log("msg", "using go liner to resolve symbols", "file", path)
return funcLiner(f), nil
return lnr, nil
}
level.Error(s.logger).Log(
"msg", "failed to create go liner, falling back to binary liner",
Expand Down