-
Notifications
You must be signed in to change notification settings - Fork 319
/
fieldtype.go
250 lines (218 loc) · 4.71 KB
/
fieldtype.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
// SPDX-License-Identifier: Apache-2.0
// Copyright Authors of Tetragon
package tracepoint
import (
"fmt"
"strconv"
"strings"
)
type IntTyBase int
const (
IntTyChar IntTyBase = iota
IntTyShort
IntTyInt
IntTyLong
IntTyLongLong
IntTyInt8
IntTyInt16
IntTyInt32
IntTyInt64
)
// integer type
type IntTy struct {
Base IntTyBase
Unsigned bool
}
type BoolTy struct{}
// pid_t type
type PidTy struct{}
// pid_t type
type SizeTy struct{}
// void type
type VoidTy struct{}
// dma_addr_t
type DmaAddrTy struct{}
type PointerTy struct {
Ty interface{}
Const bool
}
type ArrayTy struct {
Ty interface{}
Size uint
}
type Field struct {
Name string
Type interface{}
}
type ParseError struct {
r string
}
func (e *ParseError) Error() string {
return fmt.Sprintf("failed to parse field: %s", e.r)
}
func parseTy(tyFields []string) (interface{}, error) {
fidx := 0
nfields := len(tyFields)
isConst := false
nextField := func() string {
ret := tyFields[fidx]
fidx++
return ret
}
peekField := func() string {
return tyFields[fidx]
}
lastField := func() bool {
return fidx == nfields
}
if peekField() == "const" {
isConst = true
nextField()
}
ty := nextField()
unsigned := false
if ty == "unsigned" {
// type just contains unsigned
if lastField() {
return IntTy{
Base: IntTyInt,
Unsigned: true,
}, nil
}
// unsigned is a qualifier
unsigned = true
ty = nextField()
}
var retTy interface{}
switch {
case ty == "char":
retTy = IntTy{Base: IntTyChar, Unsigned: unsigned}
case ty == "short":
retTy = IntTy{Base: IntTyShort, Unsigned: unsigned}
case ty == "int":
retTy = IntTy{Base: IntTyInt, Unsigned: unsigned}
case ty == "long":
if !lastField() && peekField() == "long" {
retTy = IntTy{Base: IntTyLongLong, Unsigned: unsigned}
nextField()
} else {
retTy = IntTy{Base: IntTyLong, Unsigned: unsigned}
}
case unsigned:
// we are doing something wrong if we hit this because we are ignoring the unsigned qualifier
return nil, &ParseError{r: "unexpected unsigned"}
case ty == "u8":
retTy = IntTy{Base: IntTyInt8, Unsigned: true}
case ty == "u16":
retTy = IntTy{Base: IntTyInt16, Unsigned: true}
case ty == "u32":
retTy = IntTy{Base: IntTyInt32, Unsigned: true}
case ty == "u64":
retTy = IntTy{Base: IntTyInt64, Unsigned: true}
case ty == "bool":
retTy = BoolTy{}
case ty == "pid_t":
retTy = PidTy{}
case ty == "size_t":
retTy = SizeTy{}
case ty == "void":
retTy = VoidTy{}
case ty == "dma_addr_t":
retTy = DmaAddrTy{}
default:
return nil, &ParseError{r: fmt.Sprintf("unknown type:%s", ty)}
}
if lastField() {
return retTy, nil
}
// Linux 5.16 started placing attributes in tracepoint format definitions
// Let's just ignore them here for now
if strings.HasPrefix(peekField(), "__attribute__") {
nextField()
}
rest := nextField()
if rest == "*" {
retTy = PointerTy{Ty: retTy, Const: isConst}
} else {
return nil, &ParseError{r: "parsing failed"}
}
if !lastField() {
return nil, &ParseError{r: "did not process all fields"}
}
return retTy, nil
}
func parseField(s string) (*Field, error) {
fields := strings.Fields(s)
nfields := len(fields)
if nfields < 2 {
return nil, &ParseError{r: "expecting at least two fields"}
}
tyFields := fields[0 : nfields-1]
retTy, err := parseTy(tyFields)
if err != nil {
return nil, err
}
name := fields[nfields-1]
if bOpen := strings.Index(name, "["); bOpen != -1 {
var size uint64
if !strings.HasSuffix(name, "]") {
return nil, &ParseError{r: "could not parse array structure"}
}
substrings := strings.Split(name, "[")
size_s := strings.TrimSuffix(substrings[1], "]")
size, err = strconv.ParseUint(size_s, 10, 32)
if err != nil {
return nil, &ParseError{r: fmt.Sprintf("failed to parse size: %s", err)}
}
retTy = ArrayTy{
Ty: retTy,
Size: uint(size),
}
name = substrings[0]
}
return &Field{
Name: name,
Type: retTy,
}, nil
}
// NBytes retruns the number of bytes of an integer type
func (ty *IntTy) NBytes() (int, error) {
var ret int
switch ty.Base {
case IntTyChar:
ret = 1
case IntTyShort:
ret = 2
case IntTyInt:
ret = 4
case IntTyLong:
ret = 8
case IntTyLongLong:
ret = 8
case IntTyInt8:
ret = 1
case IntTyInt16:
ret = 2
case IntTyInt32:
ret = 4
case IntTyInt64:
ret = 8
default:
return ret, fmt.Errorf("unknown base: %d", ty.Base)
}
return ret, nil
}
// NBytes returns the number of bytes if an array type
// TODO: expand for types other than Int as needed
func (ty *ArrayTy) NBytes() (int, error) {
switch x := ty.Ty.(type) {
case IntTy:
intBytes, err := x.NBytes()
if err != nil {
return 0, err
}
return intBytes * int(ty.Size), nil
default:
return 0, fmt.Errorf("NBytes: unknown type: %T", ty)
}
}