summaryrefslogtreecommitdiff
path: root/internal/globalfilter/filter.go
blob: 2bc96a794be3c06f5ee43ab449af99cb11c89f34 (plain)
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
package globalfilter

import (
	"strings"
)

type CompareOp int

const (
	OpEq CompareOp = iota
	OpNeq
	OpGt
	OpGte
	OpLt
	OpLte
)

type NumericFilter struct {
	// Op is the comparison operator applied when matching a candidate value.
	Op CompareOp
	// Value is the reference operand compared against the candidate value.
	Value int64
}

// NewEqFilter creates an equality NumericFilter for a positive value.
// Returns nil if value is not positive.
func NewEqFilter(value int64) *NumericFilter {
	if value <= 0 {
		return nil
	}
	return &NumericFilter{Op: OpEq, Value: value}
}

// EqValue returns the filter's positive equality value if the filter
// represents an exact-match constraint (Op == OpEq and Value > 0).
// Returns (0, false) when the filter is nil, uses a different operator,
// or has a non-positive value.
// The return type is int64 to avoid silent truncation on 32-bit architectures
// where converting int64 to int would silently discard the high 32 bits for
// values that exceed math.MaxInt32.
func (f *NumericFilter) EqValue() (int64, bool) {
	if f == nil || f.Op != OpEq || f.Value <= 0 {
		return 0, false
	}
	return f.Value, true
}

type StringFilter struct {
	// Pattern is the substring (or anchored prefix/suffix) matched against the
	// candidate string value; matching is case-insensitive.
	Pattern string
}

type Candidate interface {
	SyscallValue() string
	CommValue() string
	FileValue() string
	PIDValue() uint32
	TIDValue() uint32
	FDValue() int32
	LatencyValue() uint64
	GapValue() uint64
	BytesValue() uint64
	ReturnValue() int64
	ErrorValue() bool
}

type Filter struct {
	// Syscall filters events by syscall/tracepoint name substring.
	Syscall *StringFilter
	// Comm filters events by process command name substring.
	Comm *StringFilter
	// File filters events by the file path involved in the syscall.
	File *StringFilter
	// PID filters events by process ID using a numeric comparison.
	PID *NumericFilter
	// TID filters events by thread ID using a numeric comparison.
	TID *NumericFilter
	// FD filters events by file descriptor number using a numeric comparison.
	FD *NumericFilter
	// LatencyNs filters events by syscall latency in nanoseconds.
	LatencyNs *NumericFilter
	// GapNs filters events by inter-syscall gap duration in nanoseconds.
	GapNs *NumericFilter
	// Bytes filters events by the number of bytes transferred.
	Bytes *NumericFilter
	// RetVal filters events by the syscall return value.
	RetVal *NumericFilter
	// ErrorsOnly restricts the filter to events where the syscall returned an error.
	ErrorsOnly bool
}

func (f Filter) Clone() Filter {
	out := f
	out.Syscall = cloneFilter(f.Syscall)
	out.Comm = cloneFilter(f.Comm)
	out.File = cloneFilter(f.File)
	out.PID = cloneFilter(f.PID)
	out.TID = cloneFilter(f.TID)
	out.FD = cloneFilter(f.FD)
	out.LatencyNs = cloneFilter(f.LatencyNs)
	out.GapNs = cloneFilter(f.GapNs)
	out.Bytes = cloneFilter(f.Bytes)
	out.RetVal = cloneFilter(f.RetVal)
	return out
}

func (f Filter) Equal(other Filter) bool {
	return sameFilter(f.Syscall, other.Syscall) &&
		sameFilter(f.Comm, other.Comm) &&
		sameFilter(f.File, other.File) &&
		sameFilter(f.PID, other.PID) &&
		sameFilter(f.TID, other.TID) &&
		sameFilter(f.FD, other.FD) &&
		sameFilter(f.LatencyNs, other.LatencyNs) &&
		sameFilter(f.GapNs, other.GapNs) &&
		sameFilter(f.Bytes, other.Bytes) &&
		sameFilter(f.RetVal, other.RetVal) &&
		f.ErrorsOnly == other.ErrorsOnly
}

func (f Filter) Matches(candidate Candidate) bool {
	if candidate == nil {
		return false
	}
	if f.ErrorsOnly && !candidate.ErrorValue() {
		return false
	}
	if !matchString(f.Syscall, candidate.SyscallValue()) {
		return false
	}
	if !matchString(f.Comm, candidate.CommValue()) {
		return false
	}
	if !matchString(f.File, candidate.FileValue()) {
		return false
	}
	if !matchNumeric(f.PID, int64(candidate.PIDValue())) {
		return false
	}
	if !matchNumeric(f.TID, int64(candidate.TIDValue())) {
		return false
	}
	if !matchNumeric(f.FD, int64(candidate.FDValue())) {
		return false
	}
	if !matchNumeric(f.LatencyNs, int64(candidate.LatencyValue())) {
		return false
	}
	if !matchNumeric(f.GapNs, int64(candidate.GapValue())) {
		return false
	}
	if !matchNumeric(f.Bytes, int64(candidate.BytesValue())) {
		return false
	}
	if !matchNumeric(f.RetVal, candidate.ReturnValue()) {
		return false
	}
	return true
}

func (f Filter) IsActive() bool {
	if f.ErrorsOnly {
		return true
	}
	for _, sf := range []*StringFilter{f.Syscall, f.Comm, f.File} {
		if sf != nil && strings.TrimSpace(sf.Pattern) != "" {
			return true
		}
	}
	for _, nf := range []*NumericFilter{f.PID, f.TID, f.FD, f.LatencyNs, f.GapNs, f.Bytes, f.RetVal} {
		if nf != nil {
			return true
		}
	}
	return false
}

func matchString(sf *StringFilter, value string) bool {
	if sf == nil {
		return true
	}
	pattern := strings.ToLower(strings.TrimSpace(sf.Pattern))
	if pattern == "" {
		return true
	}
	value = strings.ToLower(value)
	anchoredStart := strings.HasPrefix(pattern, "^")
	anchoredEnd := strings.HasSuffix(pattern, "$")
	if anchoredStart {
		pattern = pattern[1:]
	}
	if anchoredEnd && len(pattern) > 0 {
		pattern = pattern[:len(pattern)-1]
	}
	switch {
	case anchoredStart && anchoredEnd:
		return value == pattern
	case anchoredStart:
		return strings.HasPrefix(value, pattern)
	case anchoredEnd:
		return strings.HasSuffix(value, pattern)
	default:
		return strings.Contains(value, pattern)
	}
}

func matchNumeric(nf *NumericFilter, value int64) bool {
	if nf == nil {
		return true
	}
	switch nf.Op {
	case OpEq:
		return value == nf.Value
	case OpNeq:
		return value != nf.Value
	case OpGt:
		return value > nf.Value
	case OpGte:
		return value >= nf.Value
	case OpLt:
		return value < nf.Value
	case OpLte:
		return value <= nf.Value
	default:
		return false
	}
}

func cloneFilter[T any](in *T) *T {
	if in == nil {
		return nil
	}
	out := *in
	return &out
}

func sameFilter[T comparable](left, right *T) bool {
	switch {
	case left == nil && right == nil:
		return true
	case left == nil || right == nil:
		return false
	default:
		return *left == *right
	}
}