-
Notifications
You must be signed in to change notification settings - Fork 75
/
inspector.go
264 lines (231 loc) · 5.61 KB
/
inspector.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
// Copyright The OpenTelemetry 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 inspect
import (
"context"
"errors"
"github.com/docker/docker/client"
"github.com/go-logr/logr"
"github.com/hashicorp/go-version"
"golang.org/x/sync/errgroup"
"go.opentelemetry.io/auto/internal/pkg/structfield"
)
const defaultNWorkers = 20
// Inspector inspects structure of Go packages.
type Inspector struct {
NWorkers int
Cache *Cache
log logr.Logger
client *client.Client
jobs []job
}
// New returns an Inspector that configured to inspect offsets defined in the
// manifests.
//
// If cache is non-nil, offsets will first be looked up there. Otherwise, the
// offsets will be found by building the applicatiions in the manifests and
// inspecting the produced binaries.
func New(l logr.Logger, cache *Cache, manifests ...Manifest) (*Inspector, error) {
logger := l.WithName("inspector")
if cache == nil {
logger.Info("using empty cache")
cache = newCache(l)
}
cli, err := client.NewClientWithOpts(
client.FromEnv,
client.WithAPIVersionNegotiation(),
)
if err != nil {
return nil, err
}
i := &Inspector{
NWorkers: defaultNWorkers,
log: logger,
Cache: cache,
client: cli,
}
for _, m := range manifests {
err := i.AddManifest(m)
if err != nil {
return nil, err
}
}
return i, nil
}
// AddManifest adds the manifest to the Inspector's set of Manifests to
// inspect.
func (i *Inspector) AddManifest(manifest Manifest) error {
if err := manifest.validate(); err != nil {
return err
}
i.log.V(2).Info("adding manifest", "manifest", manifest)
goVer := manifest.Application.GoVerions
if goVer == nil {
// Passsing nil to newBuilder will mean the application is built with
// the latest version of Go.
b := newBuilder(i.log, i.client, nil)
for _, ver := range manifest.Application.Versions {
v := ver
i.jobs = append(i.jobs, job{
Renderer: manifest.Application.Renderer,
Builder: b,
AppVer: v,
Fields: manifest.StructFields,
})
}
return nil
}
if manifest.Application.Versions == nil {
for _, gVer := range goVer {
v := gVer
i.jobs = append(i.jobs, job{
Renderer: manifest.Application.Renderer,
Builder: newBuilder(i.log, i.client, v),
AppVer: v,
Fields: manifest.StructFields,
})
}
return nil
}
for _, gV := range goVer {
b := newBuilder(i.log, i.client, gV)
for _, ver := range manifest.Application.Versions {
v := ver
i.jobs = append(i.jobs, job{
Renderer: manifest.Application.Renderer,
Builder: b,
AppVer: v,
Fields: manifest.StructFields,
})
}
}
return nil
}
type job struct {
Renderer Renderer
Builder *builder
AppVer *version.Version
Fields []structfield.ID
}
// Do performs the inspections and returns all found offsets.
func (i *Inspector) Do(ctx context.Context) (*structfield.Index, error) {
g, ctx := errgroup.WithContext(ctx)
todo := make(chan job)
g.Go(func() error {
defer close(todo)
for _, j := range i.jobs {
select {
case todo <- j:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
c := make(chan []result)
for n := 0; n < max(1, i.NWorkers-1); n++ {
g.Go(func() error {
for m := range todo {
out, err := i.do(ctx, m)
if err != nil {
return err
}
select {
case c <- out:
case <-ctx.Done():
return ctx.Err()
}
}
return nil
})
}
go func() {
_ = g.Wait()
close(c)
}()
index := structfield.NewIndex()
for results := range c {
for _, r := range results {
i.logResult(r)
index.PutOffset(r.StructField, r.Version, r.Offset, r.Valid)
}
}
if err := g.Wait(); err != nil {
return nil, err
}
return index, nil
}
func max(a, b int) int {
if a > b {
return a
}
return b
}
type result struct {
StructField structfield.ID
Version *version.Version
Offset uint64
// Valid is true if the offset is valid for the struct field at the specified version.
Valid bool
}
func (i *Inspector) do(ctx context.Context, j job) (out []result, err error) {
var uncachedIndices []int
for _, f := range j.Fields {
o, ok := i.Cache.GetOffset(j.AppVer, f)
out = append(out, result{
StructField: f,
Version: j.AppVer,
Offset: o.Offset,
Valid: o.Valid,
})
if !ok {
uncachedIndices = append(uncachedIndices, len(out)-1)
}
}
if len(uncachedIndices) == 0 {
return out, nil
}
app, err := newApp(ctx, i.log, j)
buildErr := &errBuild{}
if errors.As(err, &buildErr) {
i.log.V(1).Info(
"failed to build app, skipping",
"version", j.AppVer,
"src", j.Renderer.src,
"Go", j.Builder.GoImage,
"rc", buildErr.ReturnCode,
"stdout", buildErr.Stdout,
"stderr", buildErr.Stderr,
)
return out, nil
} else if err != nil {
return nil, err
}
defer app.Close()
for _, i := range uncachedIndices {
out[i].Offset, out[i].Valid = app.GetOffset(out[i].StructField)
}
return out, nil
}
func (i *Inspector) logResult(r result) {
msg := "offset "
kv := []interface{}{"version", r.Version, "id", r.StructField}
if !r.Valid {
msg += "not found"
} else {
msg += "found"
kv = append(kv, "offset", r.Offset)
}
i.log.Info(msg, kv...)
}