-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecoder.go
382 lines (344 loc) · 10.8 KB
/
decoder.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
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package socket_tracer
import (
"errors"
"fmt"
"reflect"
"sort"
"strconv"
"strings"
"unsafe"
)
// This sets a limit on struct decoder's greedy fields. 2048 is not a problem
// as the kernel tracing subsystem won't let us dump more than that anyway.
const maxRawCopySize = 2048
// Decoder decodes a raw event into an usable type.
type Decoder interface {
Decode(raw []byte, meta Metadata) (interface{}, error)
}
type mapDecoder []Field
// NewMapDecoder creates a new decoder that will parse raw tracing events
// into a map[string]interface{}. This decoder will decode all the fields
// described in the format.
// The map keys are the field names as given in the format.
// The map values are fixed-size integers for integer fields:
// uint8, uint16, uint32, uint64, or, for signed fields, their signed counterpart.
// For string fields, the value is a string.
// Null string fields will be the null interface.
func NewMapDecoder(format ProbeDescription) Decoder {
fields := make([]Field, 0, len(format.Fields))
for _, field := range format.Fields {
fields = append(fields, field)
}
sort.Slice(fields, func(i, j int) bool {
return fields[i].Offset < fields[j].Offset
})
return mapDecoder(fields)
}
func (f mapDecoder) Decode(raw []byte, meta Metadata) (mapIf interface{}, err error) {
n := len(raw)
m := make(map[string]interface{}, len(f)+1)
m["meta"] = meta
for _, field := range f {
if field.Offset+field.Size > n {
return nil, fmt.Errorf("perf event field %s overflows message of size %d", field.Name, n)
}
var value interface{}
ptr := unsafe.Pointer(&raw[field.Offset])
switch field.Type {
case FieldTypeInteger:
if value, err = readInt(ptr, uint8(field.Size), field.Signed); err != nil {
return nil, fmt.Errorf("bad size=%d for integer field=%s", field.Size, field.Name)
}
case FieldTypeString:
offset := int(MachineEndian.Uint16(raw[field.Offset:]))
len := int(MachineEndian.Uint16(raw[field.Offset+2:]))
if offset+len > n {
return nil, fmt.Errorf("perf event string data for field %s overflows message of size %d", field.Name, n)
}
// (null) strings have data offset equal to string description offset
if len != 0 || offset != field.Offset {
if len > 0 && raw[offset+len-1] == 0 {
len--
}
value = string(raw[offset : offset+len])
}
}
m[field.Name] = value
}
return m, nil
}
// AllocateFn is the type of a function that allocates a custom struct
// to be used with StructDecoder. This function must return a pointer to
// a struct.
type AllocateFn func() interface{}
type fieldDecoder struct {
typ FieldType
src uintptr
dst uintptr
len uintptr
name string
}
type structDecoder struct {
alloc AllocateFn
fields []fieldDecoder
}
var intFields = map[reflect.Kind]struct{}{
reflect.Int: {},
reflect.Int8: {},
reflect.Int16: {},
reflect.Int32: {},
reflect.Int64: {},
reflect.Uint: {},
reflect.Uint8: {},
reflect.Uint16: {},
reflect.Uint32: {},
reflect.Uint64: {},
reflect.Uintptr: {},
}
const maxIntSizeBytes = 8
// NewMapDecoder creates a new decoder that will parse raw tracing events
// into a struct.
//
// This custom struct has to be annotated so that the required KProbeFormat
// fields are stored in the appropriate struct fields, as in:
//
// type myStruct struct {
// Type uint16 `kprobe:"common_type"`
// Flags uint8 `kprobe:"common_flags"`
// PCount uint8 `kprobe:"common_preempt_count"`
// PID uint32 `kprobe:"common_pid"`
// IP uint64 `kprobe:"__probe_ip"`
// Exe string `kprobe:"exe"`
// Fd uint64 `kprobe:"fd"`
// Arg3 uint64 `kprobe:"arg3"`
// Arg4 uint32 `kprobe:"arg4"`
// Arg5 uint16 `kprobe:"arg5"`
// Arg6 uint8 `kprobe:"arg6"`
// }
//
// There's no need to map all fields in the event.
//
// The custom allocator has to return a pointer to the struct. There's no actual
// need to allocate a new struct each time, as long as the consumer of a perf
// event channel manages the lifetime of the returned structs.
//
// This decoder is faster than the map decoder and results in fewer allocations:
// Only string fields need to be allocated, plus the allocation by allocFn.
func NewStructDecoder(desc ProbeDescription, allocFn AllocateFn) (Decoder, error) {
dec := new(structDecoder)
dec.alloc = allocFn
sample := allocFn()
tSample := reflect.TypeOf(sample)
if tSample.Kind() != reflect.Ptr {
return nil, errors.New("allocator function doesn't return a pointer")
}
tSample = tSample.Elem()
if tSample.Kind() != reflect.Struct {
return nil, errors.New("allocator function doesn't return a pointer to a struct")
}
var inFieldsByOffset map[int]Field
for i := 0; i < tSample.NumField(); i++ {
outField := tSample.Field(i)
values, found := outField.Tag.Lookup("kprobe")
if !found {
// Untagged field
continue
}
var name string
var greedy bool
for idx, param := range strings.Split(values, ",") {
switch param {
case "greedy":
greedy = true
default:
if idx != 0 {
return nil, fmt.Errorf("bad parameter '%s' in kprobe tag for field '%s'", param, outField.Name)
}
name = param
}
}
if name == "metadata" {
if outField.Type != reflect.TypeOf(Metadata{}) {
return nil, errors.New("bad type for meta field")
}
dec.fields = append(dec.fields, fieldDecoder{
name: name,
typ: FieldTypeMeta,
dst: outField.Offset,
// src&len are unused, this avoids checking len against actual payload
src: 0,
len: 0,
})
continue
}
inField, found := desc.Fields[name]
if !found {
return nil, fmt.Errorf("field '%s' not found in kprobe format description", name)
}
if greedy {
// When greedy is used for the first time, build a map of kprobe's
// fields by the offset they appear.
if inFieldsByOffset == nil {
inFieldsByOffset = make(map[int]Field)
for _, v := range desc.Fields {
inFieldsByOffset[v.Offset] = v
}
}
greedySize := uintptr(inField.Size)
nextOffset := inField.Offset + inField.Size
nextFieldID := -1
for {
nextField, found := inFieldsByOffset[nextOffset]
if !found {
break
}
if strings.Index(nextField.Name, "arg") != 0 {
break
}
fieldID, err := strconv.Atoi(nextField.Name[3:])
if err != nil {
break
}
if nextFieldID != -1 && nextFieldID != fieldID {
break
}
greedySize += uintptr(nextField.Size)
nextOffset += nextField.Size
nextFieldID = fieldID + 1
}
if greedySize > maxRawCopySize {
return nil, fmt.Errorf("greedy field '%s' exceeds limit of %d bytes", outField.Name, maxRawCopySize)
}
if curSize := outField.Type.Size(); curSize != greedySize {
return nil, fmt.Errorf("greedy field '%s' size is %d but greedy requires %d", outField.Name, curSize, greedySize)
}
dec.fields = append(dec.fields, fieldDecoder{
name: name,
typ: FieldTypeRaw,
src: uintptr(inField.Offset),
dst: outField.Offset,
len: greedySize,
})
continue
}
switch inField.Type {
case FieldTypeInteger:
if _, found := intFields[outField.Type.Kind()]; !found {
return nil, fmt.Errorf("wrong struct field type for field '%s', fixed size integer required", name)
}
if outField.Type.Size() != uintptr(inField.Size) {
return nil, fmt.Errorf("wrong struct field size for field '%s', got=%d required=%d",
name, outField.Type.Size(), inField.Size)
}
// Paranoid
if inField.Size > maxIntSizeBytes {
return nil, fmt.Errorf("fix me: unexpected integer of size %d in field `%s`",
inField.Size, name)
}
case FieldTypeString:
if outField.Type.Kind() != reflect.String {
return nil, fmt.Errorf("wrong struct field type for field '%s', it should be string", name)
}
default:
// Should not happen
return nil, fmt.Errorf("unexpected field type for field '%s'", name)
}
dec.fields = append(dec.fields, fieldDecoder{
typ: inField.Type,
src: uintptr(inField.Offset),
dst: outField.Offset,
len: uintptr(inField.Size),
name: name,
})
}
sort.Slice(dec.fields, func(i, j int) bool {
return dec.fields[i].src < dec.fields[j].src
})
return dec, nil
}
func (d *structDecoder) Decode(raw []byte, meta Metadata) (s interface{}, err error) {
n := uintptr(len(raw))
// Allocate a new struct to fill
s = d.alloc()
// Get a raw pointer to the struct
uptr := reflect.ValueOf(s).Pointer()
for _, dec := range d.fields {
if dec.src+dec.len > n {
return nil, fmt.Errorf("perf event field %s overflows message of size %d", dec.name, n)
}
switch dec.typ {
case FieldTypeInteger:
dst := unsafe.Pointer(uptr + dec.dst)
src := unsafe.Pointer(&raw[dec.src])
if err := copyInt(dst, src, uint8(dec.len)); err != nil {
return nil, fmt.Errorf("bad size=%d for integer field=%s", dec.len, dec.name)
}
case FieldTypeString:
offset := uintptr(MachineEndian.Uint16(raw[dec.src:]))
len := uintptr(MachineEndian.Uint16(raw[dec.src+2:]))
if offset+len > n {
return nil, fmt.Errorf("perf event string data for field %s overflows message of size %d", dec.name, n)
}
if len > 0 && raw[offset+len-1] == 0 {
len--
}
*((*string)(unsafe.Pointer(uptr + dec.dst))) = string(raw[offset : offset+len])
case FieldTypeMeta:
*(*Metadata)(unsafe.Pointer(uptr + dec.dst)) = meta
case FieldTypeRaw:
copy((*(*[maxRawCopySize]byte)(unsafe.Pointer(uptr + dec.dst)))[:dec.len], raw[dec.src:dec.src+dec.len])
}
}
return s, nil
}
type dumpDecoder struct {
start int
end int
}
// NewDumpDecoder returns a new decoder that will dump all the arguments
// as a byte slice. Useful for memory dumps. Arguments must be:
// - unnamed, so they get an automatic argNN name.
// - integer of 64bit (u64 / s64).
// - dump consecutive memory.
func NewDumpDecoder(format ProbeDescription) (Decoder, error) {
var fields []Field
for name, field := range format.Fields {
if strings.Index(name, "arg") != 0 {
continue
}
if field.Type != FieldTypeInteger {
return nil, fmt.Errorf("field '%s' is not an integer", name)
}
if field.Size != 8 {
return nil, fmt.Errorf("field '%s' length is not 8", name)
}
fields = append(fields, field)
}
if len(fields) == 0 {
return nil, errors.New("no fields to decode")
}
sort.Slice(fields, func(i, j int) bool {
return fields[i].Offset < fields[j].Offset
})
base := fields[0].Offset
end := base
for _, field := range fields {
if field.Offset != end {
return nil, fmt.Errorf("gap before field '%s'", field.Name)
}
end += field.Size
}
return &dumpDecoder{
start: base,
end: end,
}, nil
}
func (d *dumpDecoder) Decode(raw []byte, _ Metadata) (interface{}, error) {
if len(raw) < d.end {
return nil, errors.New("record too short for dump")
}
return append([]byte(nil), raw[d.start:d.end]...), nil
}