-
Notifications
You must be signed in to change notification settings - Fork 66
/
find.go
120 lines (106 loc) · 2.7 KB
/
find.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright (c) 2022 The Parca Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package debuginfo
import (
"context"
"errors"
"fmt"
"os"
"path"
"path/filepath"
"strings"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
lru "github.com/hashicorp/golang-lru"
"github.com/parca-dev/parca-agent/pkg/buildid"
)
// Finder finds the additional debug information on the system.
type Finder struct {
logger log.Logger
cache *lru.ARCCache
}
// NewFinder creates a new Finder.
func NewFinder(logger log.Logger) *Finder {
logger = log.With(logger, "component", "finder")
cache, err := lru.NewARC(128) // Arbitrary cache size.
if err != nil {
level.Warn(logger).Log("msg", "failed to initialize finder cache", "err", err)
}
return &Finder{
logger: logger,
cache: cache,
}
}
// Find finds the debug information for the given build ID.
func (f *Finder) Find(ctx context.Context, buildID, root string) (string, error) {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
if val, ok := f.cache.Get(buildID); ok {
switch v := val.(type) {
case string:
return v, nil
case error:
return "", v
default:
// We didn't put you there?!
return "", errors.New("unexpected type")
}
}
objFile, err := find(buildID, root)
if err != nil {
if errors.Is(err, errNotFound) {
f.cache.Add(buildID, err)
return "", err
}
}
f.cache.Add(buildID, objFile)
return objFile, nil
}
func find(buildID, root string) (string, error) {
var (
found = false
file string
)
// TODO(kakkoyun): Distros may have different locations for debuginfo files.
// Add support for all of them. Add an issue fir this.
err := filepath.Walk(path.Join(root, "/usr/lib/debug"), func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
id, err := buildid.BuildID(path)
if err != nil {
return fmt.Errorf("failed to extract elf build ID, %w", err)
}
if strings.EqualFold(id, buildID) {
found = true
file = path
}
}
return nil
})
if err != nil {
if os.IsNotExist(err) {
return "", errNotFound
}
return "", fmt.Errorf("failed to walk debug files: %w", err)
}
if !found {
return "", errNotFound
}
return file, nil
}