-
Notifications
You must be signed in to change notification settings - Fork 66
/
debuginfo.go
393 lines (342 loc) · 11.6 KB
/
debuginfo.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
// Copyright 2021 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 (
"bytes"
"context"
"debug/elf"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"sync"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/containerd/containerd/sys/reaper"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
lru "github.com/hashicorp/golang-lru"
"github.com/parca-dev/parca/pkg/symbol/elfutils"
"github.com/parca-dev/parca-agent/pkg/objectfile"
)
var errNotFound = errors.New("not found")
type Client interface {
Exists(ctx context.Context, buildID string) (bool, error)
Upload(ctx context.Context, buildID string, f io.Reader) (uint64, error)
}
type NoopClient struct{}
func (c *NoopClient) Exists(ctx context.Context, buildID string) (bool, error) {
return true, nil
}
func (c *NoopClient) Upload(ctx context.Context, buildID string, f io.Reader) (uint64, error) {
return 0, nil
}
func NewNoopClient() Client {
return &NoopClient{}
}
type Extractor struct {
logger log.Logger
client Client
dbgFileCache *lru.ARCCache
tmpDir string
pool sync.Pool
}
// TODO(kakkoyun): Split extract and upload into separate layers.
// - Use debuginfo_file for extraction related operations.
func NewExtractor(logger log.Logger, client Client, tmpDir string) *Extractor {
cache, err := lru.NewARC(128) // Arbitrary cache size.
if err != nil {
level.Warn(logger).Log("msg", "failed to initialize debug file cache", "err", err)
}
return &Extractor{
logger: logger,
client: client,
tmpDir: tmpDir,
dbgFileCache: cache,
pool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
},
}
}
func (di *Extractor) Upload(ctx context.Context, objFilePaths map[string]string) error {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
for buildID, pth := range objFilePaths {
exists, err := di.client.Exists(ctx, buildID)
if err != nil {
level.Error(di.logger).Log("msg", "failed to check whether build ID symbol exists", "err", err)
continue
}
if !exists {
level.Debug(di.logger).Log("msg", "could not find symbols in server", "buildid", buildID)
hasDebugInfo, err := checkIfFileHasDebugInfo(pth)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to determine whether file has debug symbols", "file", pth, "err", err)
continue
}
if !hasDebugInfo {
level.Debug(di.logger).Log("msg", "file does not have debug information, skipping", "file", pth, "err", err)
continue
}
debugInfoFile, err := di.extract(ctx, buildID, pth)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to extract debug information", "buildid", buildID, "file", pth, "err", err)
continue
}
if err := di.uploadDebugInfo(ctx, buildID, debugInfoFile); err != nil {
os.Remove(debugInfoFile)
level.Error(di.logger).Log("msg", "failed to upload debug information", "buildid", buildID, "file", pth, "err", err)
continue
}
os.Remove(debugInfoFile)
level.Info(di.logger).Log("msg", "debug information uploaded successfully", "buildid", buildID, "file", pth)
continue
}
level.Info(di.logger).Log("msg", "debug information already exist in server", "buildid", buildID)
}
return nil
}
func (di *Extractor) Extract(ctx context.Context, objFilePaths map[string]string) ([]string, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
files := []string{}
for buildID, path := range objFilePaths {
debugInfoFile, err := di.extract(ctx, buildID, path)
if err != nil {
level.Error(di.logger).Log("msg", "failed to extract debug information", "buildid", buildID, "file", path, "err", err)
continue
}
files = append(files, debugInfoFile)
}
return files, nil
}
func (di *Extractor) EnsureUploaded(ctx context.Context, objFiles []*objectfile.MappedObjectFile) {
for _, objFile := range objFiles {
buildID := objFile.BuildID
exists, err := di.client.Exists(ctx, buildID)
if err != nil {
level.Warn(di.logger).Log("msg", "failed to check whether build ID symbol exists", "err", err)
continue
}
if !exists {
level.Debug(di.logger).Log("msg", "could not find symbols in server", "buildid", buildID)
var dbgInfoFile *debugInfoFile
if di.dbgFileCache != nil {
if val, ok := di.dbgFileCache.Get(buildID); ok {
dbgInfoFile = val.(*debugInfoFile)
} else {
f, err := newDebugInfoFile(objFile)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to create debug information file", "buildid", buildID, "err", err)
continue
}
di.dbgFileCache.Add(buildID, f)
dbgInfoFile = f
}
}
objFilePath := objFile.Path
if !dbgInfoFile.hasDebugInfo {
// The object does not have debug symbols, but maybe debuginfos
// have been installed separately, typically in /usr/lib/debug, so
// we try to discover if there is a debuginfo file, that has the
// same build ID as the object.
level.Debug(di.logger).Log(
"msg", "could not find symbols in binary, checking for additional debug info files on the system",
"buildid", objFile.BuildID, "file", objFilePath,
)
if dbgInfoFile.localDebugInfoPath == "" {
// Binary does not have debug symbols, and we could not find any on the system. Nothing to do here.
continue
}
objFilePath = dbgInfoFile.localDebugInfoPath
}
extractedDbgInfo, err := di.extract(ctx, buildID, objFilePath)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to extract debug information", "buildid", buildID, "file", objFilePath, "err", err)
continue
}
if err := di.uploadDebugInfo(ctx, buildID, extractedDbgInfo); err != nil {
os.Remove(extractedDbgInfo)
level.Warn(di.logger).Log("msg", "failed to upload debug information", "buildid", buildID, "file", objFilePath, "err", err)
continue
}
os.Remove(extractedDbgInfo)
level.Debug(di.logger).Log("msg", "debug information uploaded successfully", "buildid", buildID, "file", objFilePath)
continue
}
level.Debug(di.logger).Log("msg", "debug information already exist in server", "buildid", buildID)
}
}
func (di *Extractor) extract(ctx context.Context, buildID, file string) (string, error) {
tmpDir := path.Join(di.tmpDir, buildID)
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
return "", fmt.Errorf("failed to create temp dir for debug information extraction: %w", err)
}
hasDWARF, err := elfutils.HasDWARF(file)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to determine if binary has DWARF sections", "path", file, "err", err)
}
isGo, err := elfutils.IsSymbolizableGoObjFile(file)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to determine if binary is a Go binary", "path", file, "err", err)
}
toRemove, err := sectionsToRemove(file)
if err != nil {
level.Debug(di.logger).Log("msg", "failed to determine sections to remove", "path", file, "err", err)
}
outFile := path.Join(tmpDir, "debuginfo")
interimDir, err := ioutil.TempDir(di.tmpDir, "*")
if err != nil {
return "", err
}
defer func() { os.RemoveAll(interimDir) }()
var cmd *exec.Cmd
switch {
case hasDWARF:
cmd = di.strip(ctx, interimDir, file, outFile, toRemove)
case isGo:
cmd = di.objcopy(ctx, file, outFile, toRemove)
default:
cmd = di.strip(ctx, interimDir, file, outFile, toRemove)
}
const msg = "failed to extract debug information from binary"
if err := di.run(cmd); err != nil {
return "", fmt.Errorf(msg+": %w", err)
}
// Check if the debug information file is actually created.
if exists, err := exists(outFile); !exists {
if err != nil {
return "", fmt.Errorf(msg+": %w", err)
}
return "", fmt.Errorf(msg+": %s", "debug information file is not created")
}
return outFile, nil
}
func (di *Extractor) run(cmd *exec.Cmd) error {
level.Debug(di.logger).Log("msg", "running external binary utility command", "cmd", strings.Join(cmd.Args, " "))
b := di.pool.Get().(*bytes.Buffer)
defer func() {
b.Reset()
di.pool.Put(b)
}()
cmd.Stdout = b
cmd.Stderr = b
c, err := reaper.Default.Start(cmd)
if err != nil {
return err
}
const msg = "external binary utility command failed"
status, err := reaper.Default.Wait(cmd, c)
if err != nil {
level.Debug(di.logger).Log("msg", msg, "cmd", cmd.Args, "output", b.String(), "err", err)
return err
}
if status != 0 {
level.Debug(di.logger).Log("msg", msg, "cmd", cmd.Args, "output", b.String())
return errors.New(msg)
}
return nil
}
func (di *Extractor) strip(ctx context.Context, tmpDir, file, outFile string, toRemove []string) *exec.Cmd {
level.Debug(di.logger).Log("msg", "using eu-strip", "file", file)
// Extract debug symbols.
// If we have DWARF symbols, they are enough for us to symbolize the profiles.
// We observed that having DWARF debug symbols and symbol table together caused us problem in certain cases.
// As DWARF symbols enough on their own we just extract those.
// eu-strip --strip-debug extracts the .debug/.zdebug sections from the object files.
args := []string{"--strip-debug"}
for _, s := range toRemove {
args = append(args, "--remove-section", s)
}
args = append(args,
"-f", outFile,
"-o", path.Join(tmpDir, "binary.stripped"),
file,
)
return exec.CommandContext(ctx, "eu-strip", args...)
}
func (di *Extractor) objcopy(ctx context.Context, file, outFile string, toRemove []string) *exec.Cmd {
level.Debug(di.logger).Log("msg", "using objcopy", "file", file)
// Go binaries has a special case. They use ".gopclntab" section to symbolize addresses.
// We need to keep ".note.go.buildid", ".symtab" and ".gopclntab",
// however it doesn't hurt to keep rather small sections.
args := []string{}
toRemove = append(toRemove, ".text", ".rodata*")
for _, s := range toRemove {
args = append(args, "--remove-section", s)
}
args = append(args,
file, // source
outFile, // destination
)
return exec.CommandContext(ctx, "objcopy", args...)
}
func (di *Extractor) uploadDebugInfo(ctx context.Context, buildID, file string) error {
f, err := os.Open(file)
if err != nil {
return fmt.Errorf("failed to open temp file for debug information: %w", err)
}
expBackOff := backoff.NewExponentialBackOff()
expBackOff.InitialInterval = time.Second
expBackOff.MaxElapsedTime = time.Minute
err = backoff.Retry(func() error {
if _, err := di.client.Upload(ctx, buildID, f); err != nil {
di.logger.Log(
"msg", "failed to upload debug information",
"retry", time.Second,
"err", err,
)
}
return err
}, expBackOff)
if err != nil {
return fmt.Errorf("failed to upload debug information: %w", err)
}
return nil
}
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
func sectionsToRemove(path string) ([]string, error) {
var sections []string
f, err := elf.Open(path)
if err != nil {
return sections, fmt.Errorf("failed to open elf file: %w", err)
}
defer f.Close()
for _, sec := range f.Sections {
if dwarfSuffix(sec) != "" && sec.Type == elf.SHT_NOBITS { // causes some trouble when it's set to SHT_NOBITS
sections = append(sections, sec.Name)
}
}
return sections, nil
}