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
|
package tracepoints
import (
"fmt"
"sort"
"strings"
"ior/internal/types"
)
// DimensionSelectorConfig holds attach-time syscall-dimension selection inputs.
// Each field accepts comma-separated values.
type DimensionSelectorConfig struct {
TraceFamilies string
TraceKinds string
TraceSyscalls string
NoTraceFamilies string
NoTraceKinds string
NoTraceSyscalls string
}
// hasAnySelector reports whether any positive or negative dimension selector
// field is populated. When all fields are empty the caller provided no
// -trace-* / -no-trace-* flags.
func (d DimensionSelectorConfig) hasAnySelector() bool {
return d.TraceFamilies != "" ||
d.TraceKinds != "" ||
d.TraceSyscalls != "" ||
d.NoTraceFamilies != "" ||
d.NoTraceKinds != "" ||
d.NoTraceSyscalls != ""
}
// ParseSelectorWithDimensions compiles regex-based attach/exclude filters and
// applies attach-time syscall dimension gating.
//
// Legacy semantics: when the caller supplies an explicit -tps regex but no
// -trace-* / -no-trace-* dimension selectors, the syscall allowlist is skipped
// entirely so that non-FS tracepoints matched by the regex are still attached.
func ParseSelectorWithDimensions(attach, exclude string, dims DimensionSelectorConfig) (Selector, error) {
sel, err := ParseSelector(attach, exclude)
if err != nil {
return Selector{}, err
}
// When an explicit -tps regex is provided without any dimension selectors,
// preserve legacy behaviour: the regex alone controls attachment and the
// implicit FS-only default is not applied.
if attach != "" && !dims.hasAnySelector() {
return sel, nil
}
allow, err := buildAllowedSyscalls(dims)
if err != nil {
return Selector{}, err
}
sel.RestrictSyscalls = true
sel.Syscalls = allow
return sel, nil
}
func buildAllowedSyscalls(dims DimensionSelectorConfig) (map[string]struct{}, error) {
knownSyscalls := allKnownSyscalls()
knownKinds := allKnownKinds()
includeFamilies, familyFilterProvided, err := parseFamiliesCSV(dims.TraceFamilies)
if err != nil {
return nil, err
}
includeKinds, kindFilterProvided, err := parseKindsCSV(dims.TraceKinds, knownKinds)
if err != nil {
return nil, err
}
includeSyscalls, syscallFilterProvided, err := parseSyscallsCSV(dims.TraceSyscalls, knownSyscalls)
if err != nil {
return nil, err
}
allow := make(map[string]struct{})
hasPositive := familyFilterProvided || kindFilterProvided || syscallFilterProvided
if hasPositive {
for syscall, family := range syscallFamilies {
if _, ok := includeFamilies[family]; ok {
allow[syscall] = struct{}{}
}
}
for syscall, kind := range syscallKinds {
if _, ok := includeKinds[kind]; ok {
allow[syscall] = struct{}{}
}
}
for syscall := range includeSyscalls {
allow[syscall] = struct{}{}
}
} else {
// Backward compatibility default: keep existing file-I/O coverage on and
// leave newly-expanded non-IO families disabled unless explicitly opted in.
for syscall, family := range syscallFamilies {
if family == string(types.FamilyFS) {
allow[syscall] = struct{}{}
}
}
}
excludeFamilies, _, err := parseFamiliesCSV(dims.NoTraceFamilies)
if err != nil {
return nil, err
}
excludeKinds, _, err := parseKindsCSV(dims.NoTraceKinds, knownKinds)
if err != nil {
return nil, err
}
excludeSyscalls, _, err := parseSyscallsCSV(dims.NoTraceSyscalls, knownSyscalls)
if err != nil {
return nil, err
}
for syscall := range allow {
if _, ok := excludeSyscalls[syscall]; ok {
delete(allow, syscall)
continue
}
if family, ok := syscallFamilies[syscall]; ok {
if _, excluded := excludeFamilies[family]; excluded {
delete(allow, syscall)
continue
}
}
if kind, ok := syscallKinds[syscall]; ok {
if _, excluded := excludeKinds[kind]; excluded {
delete(allow, syscall)
}
}
}
return allow, nil
}
func parseFamiliesCSV(raw string) (map[string]struct{}, bool, error) {
values, provided := splitCSV(raw)
if !provided {
return map[string]struct{}{}, false, nil
}
out := make(map[string]struct{}, len(values))
for _, value := range values {
family, ok := types.ParseSyscallFamily(value)
if !ok {
return nil, false, fmt.Errorf("invalid syscall family in trace selector: %q", value)
}
out[string(family)] = struct{}{}
}
return out, true, nil
}
func parseKindsCSV(raw string, knownKinds map[string]struct{}) (map[string]struct{}, bool, error) {
values, provided := splitCSV(raw)
if !provided {
return map[string]struct{}{}, false, nil
}
out := make(map[string]struct{}, len(values))
for _, value := range values {
kind := normalizeKind(value)
if _, ok := knownKinds[kind]; !ok {
return nil, false, fmt.Errorf("invalid syscall kind in trace selector: %q", value)
}
out[kind] = struct{}{}
}
return out, true, nil
}
func parseSyscallsCSV(raw string, knownSyscalls map[string]struct{}) (map[string]struct{}, bool, error) {
values, provided := splitCSV(raw)
if !provided {
return map[string]struct{}{}, false, nil
}
out := make(map[string]struct{}, len(values))
for _, value := range values {
syscall := strings.ToLower(strings.TrimSpace(value))
if _, ok := knownSyscalls[syscall]; !ok {
return nil, false, fmt.Errorf("invalid syscall in trace selector: %q", value)
}
out[syscall] = struct{}{}
}
return out, true, nil
}
func splitCSV(raw string) ([]string, bool) {
raw = strings.TrimSpace(raw)
if raw == "" {
return nil, false
}
parts := strings.Split(raw, ",")
values := make([]string, 0, len(parts))
for _, part := range parts {
part = strings.TrimSpace(part)
if part == "" {
continue
}
values = append(values, part)
}
if len(values) == 0 {
return nil, false
}
return values, true
}
func normalizeKind(raw string) string {
normalized := strings.ToLower(strings.TrimSpace(raw))
normalized = strings.ReplaceAll(normalized, "_", "-")
return normalized
}
func allKnownSyscalls() map[string]struct{} {
out := make(map[string]struct{}, len(syscallFamilies))
for syscall := range syscallFamilies {
out[syscall] = struct{}{}
}
return out
}
func allKnownKinds() map[string]struct{} {
out := make(map[string]struct{})
for _, kind := range syscallKinds {
if kind == "" {
continue
}
out[kind] = struct{}{}
}
return out
}
// KnownKinds returns the sorted, normalized attach-time kind names accepted by
// -trace-kinds and -no-trace-kinds.
func KnownKinds() []string {
kindSet := allKnownKinds()
kinds := make([]string, 0, len(kindSet))
for kind := range kindSet {
kinds = append(kinds, kind)
}
sort.Strings(kinds)
return kinds
}
|