Skip to content

Commit

Permalink
pkg/buildid: Refactor buildid api
Browse files Browse the repository at this point in the history
- unify path and elf file into an ElfFile struct type
- update references to and tests for BuildID() accordingly

Signed-off-by: Sumera Priyadarsini <sylphrenadin@gmail.com>
  • Loading branch information
Sylfrena committed Nov 24, 2022
1 parent c07d9d3 commit 01831ed
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 23 deletions.
30 changes: 11 additions & 19 deletions pkg/buildid/buildid.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,36 @@ import (
"github.com/parca-dev/parca-agent/pkg/elfreader"
)

func BuildID(f *elf.File, path string) (string, error) {
/*f, err := elf.Open(path)
if err != nil {
return "", fmt.Errorf("failed to open elf: %w", err)
}
defer f.Close()
*/
type ElfFile struct {
Path string
File *elf.File
}

func BuildID(f *ElfFile) (string, error) {
hasGoBuildIDSection := false
for _, s := range f.Sections {
for _, s := range f.File.Sections {
if s.Name == ".note.go.buildid" {
hasGoBuildIDSection = true
}
}

if hasGoBuildIDSection {
if id, err := fastGoBuildID(f); err == nil && len(id) > 0 {
if id, err := fastGoBuildID(f.File); err == nil && len(id) > 0 {
return hex.EncodeToString(id), nil
}

id, err := gobuildid.ReadFile(path)
id, err := gobuildid.ReadFile(f.Path)
if err != nil {
return elfBuildID(path)
return elfBuildID(f.Path)
}

return hex.EncodeToString([]byte(id)), nil
}
if id, err := fastGNUBuildID(f); err == nil && len(id) > 0 {
if id, err := fastGNUBuildID(f.File); err == nil && len(id) > 0 {
return hex.EncodeToString(id), nil
}

return elfBuildID(path)
return elfBuildID(f.Path)
}

func fastGoBuildID(f *elf.File) ([]byte, error) {
Expand Down Expand Up @@ -105,12 +103,6 @@ func fastGNUBuildID(f *elf.File) ([]byte, error) {
}

func extractNote(f *elf.File, section string, findBuildID func(notes []elfreader.ElfNote) ([]byte, error)) ([]byte, error) {
/*f, err := elf.Open(path)
if err != nil {
return nil, fmt.Errorf("failed to open elf: %w", err)
}
defer f.Close()*/

s := f.Section(section)
if s == nil {
return nil, fmt.Errorf("failed to find %s section", section)
Expand Down
4 changes: 3 additions & 1 deletion pkg/buildid/buildid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ func TestBuildID(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
file, err := elf.Open(tt.args.path)
require.NoError(t, err)
got, err := BuildID(file, tt.args.path)
elfFile := &ElfFile{Path: tt.args.path, File: file}

got, err := BuildID(elfFile)
if tt.wantErr {
require.Error(t, err)
} else {
Expand Down
2 changes: 1 addition & 1 deletion pkg/metadata/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Compiler() Provider {
}
defer elf.Close()

buildID, err := buildid.BuildID(elf, path)
buildID, err := buildid.BuildID(&buildid.ElfFile{Path: path, File: elf})
if err != nil {
return nil, fmt.Errorf("buildID failed")
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/objectfile/object_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func open(filePath string, start, limit, offset uint64, relocationSymbol string)
defer f.Close()

buildID := ""
if id, err := buildid.BuildID(f, filePath); err == nil {
if id, err := buildid.BuildID(&buildid.ElfFile{Path: filePath, File: f}); err == nil {
buildID = id
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/process/maps.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (c *mappingFileCache) mappingForPID(pid int) ([]*profile.Mapping, error) {
}
defer fElf.Close()

m.BuildID, err = buildid.BuildID(fElf, abs)
m.BuildID, err = buildid.BuildID(&buildid.ElfFile{Path: abs, File: fElf})
if err != nil {
if !errors.Is(err, os.ErrNotExist) {
level.Debug(c.logger).Log("msg", "failed to read object build ID", "object", abs, "err", err)
Expand Down

0 comments on commit 01831ed

Please sign in to comment.