-
Notifications
You must be signed in to change notification settings - Fork 66
/
extract.go
276 lines (249 loc) · 7.64 KB
/
extract.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
// 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 (
"bytes"
"context"
"debug/elf"
"errors"
"fmt"
"io/ioutil"
"os"
"os/exec"
"path"
"strings"
"sync"
"github.com/containerd/containerd/sys/reaper"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/hashicorp/go-multierror"
"github.com/parca-dev/parca/pkg/symbol/elfutils"
)
// Extractor extracts debug information from a binary.
type Extractor struct {
logger log.Logger
client Client
pool sync.Pool
tmpDir string
}
// NewExtractor creates a new Extractor.
func NewExtractor(logger log.Logger, client Client, tmpDir string) *Extractor {
return &Extractor{
logger: log.With(logger, "component", "extractor"),
client: client,
tmpDir: tmpDir,
pool: sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
},
}
}
// ExtractAll extracts debug information from the given executables.
// It consumes a map of build id to executable path and returns a map of build id to extracted debug information path.
func (e *Extractor) ExtractAll(ctx context.Context, objFilePaths map[string]string) (map[string]string, error) {
files := map[string]string{}
var result *multierror.Error
for buildID, filePath := range objFilePaths {
debugInfoFile, err := e.Extract(ctx, buildID, filePath)
if err != nil {
level.Warn(e.logger).Log(
"msg", "failed to extract debug information",
"buildid", buildID, "file", filePath, "err", err,
)
result = multierror.Append(result, err)
files[buildID] = ""
}
files[buildID] = debugInfoFile
}
if len(result.Errors) == len(objFilePaths) {
return nil, result.ErrorOrNil()
}
return files, nil
}
// Extract extracts debug information from the given executable.
// Cleaning up the temporary directory and the interim file is the caller's responsibility.
func (e *Extractor) Extract(ctx context.Context, buildID, filePath string) (string, error) {
select {
case <-ctx.Done():
return "", ctx.Err()
default:
}
tmpDir := path.Join(e.tmpDir, buildID)
if err := os.MkdirAll(tmpDir, 0o755); err != nil {
return "", fmt.Errorf("failed to create temp dir for debug information extraction: %w", err)
}
hasSymtab, err := hasSymbols(filePath)
if err != nil {
level.Debug(e.logger).Log(
"msg", "failed to determine whether file has symbol sections",
"file", filePath, "err", err,
)
}
hasDWARF, err := elfutils.HasDWARF(filePath)
if err != nil {
level.Debug(e.logger).Log(
"msg", "failed to determine if binary has DWARF sections",
"path", filePath, "err", err,
)
}
isGo, err := elfutils.IsSymbolizableGoObjFile(filePath)
if err != nil {
level.Debug(e.logger).Log("msg", "failed to determine if binary is a Go binary", "path", filePath, "err", err)
}
toRemove, err := sectionsToRemove(filePath)
if err != nil {
level.Debug(e.logger).Log("msg", "failed to determine sections to remove", "path", filePath, "err", err)
}
outFile := path.Join(tmpDir, "debuginfo")
interimDir, err := ioutil.TempDir(e.tmpDir, "*")
if err != nil {
return "", err
}
defer func() { os.RemoveAll(interimDir) }()
var cmd *exec.Cmd
switch {
case hasDWARF:
cmd = e.strip(ctx, interimDir, filePath, outFile, toRemove)
case isGo:
cmd = e.objcopy(ctx, filePath, outFile, toRemove)
case hasSymtab:
cmd = e.objcopy(ctx, filePath, outFile, toRemove)
default:
return "", errors.New("no debug information found in the binary")
}
const msg = "failed to extract debug information from binary"
if err := e.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 (e *Extractor) run(cmd *exec.Cmd) error {
level.Debug(e.logger).Log(
"msg", "running external binary utility command", "cmd",
strings.Join(cmd.Args, " "),
)
b := e.pool.Get().(*bytes.Buffer)
defer func() {
b.Reset()
e.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(e.logger).Log("msg", msg, "cmd", cmd.Args, "output", b.String(), "err", err)
return err
}
if status != 0 {
level.Debug(e.logger).Log("msg", msg, "cmd", cmd.Args, "output", b.String())
return errors.New(msg)
}
return nil
}
func (e *Extractor) strip(ctx context.Context, tmpDir, file, outFile string, toRemove []string) *exec.Cmd {
level.Debug(e.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 (e *Extractor) objcopy(ctx context.Context, file, outFile string, toRemove []string) *exec.Cmd {
level.Debug(e.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...)
}
var dwarfSuffix = func(s *elf.Section) string {
switch {
case strings.HasPrefix(s.Name, ".debug_"):
return s.Name[7:]
case strings.HasPrefix(s.Name, ".zdebug_"):
return s.Name[8:]
case strings.HasPrefix(s.Name, "__debug_"): // macos
return s.Name[8:]
default:
return ""
}
}
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
}
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 hasSymbols(filePath string) (bool, error) {
ef, err := elf.Open(filePath)
if err != nil {
return false, fmt.Errorf("failed to open elf: %w", err)
}
defer ef.Close()
for _, section := range ef.Sections {
if section.Type == elf.SHT_SYMTAB || section.Type == elf.SHT_DYNSYM {
return true, nil
}
}
return false, nil
}