-
Notifications
You must be signed in to change notification settings - Fork 0
/
PayloadFilter.cs
244 lines (221 loc) · 8.85 KB
/
PayloadFilter.cs
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace etwlib
{
using static TraceLogger;
using static NativeTraceConsumer;
using static etwlib.NativeTraceControl;
public class PayloadFilter : IDisposable
{
private Guid m_Provider;
private readonly EVENT_DESCRIPTOR m_Event;
private bool m_MatchAny;
private List<PAYLOAD_FILTER_PREDICATE> m_Predicates;
private nint m_Filter;
private bool m_Disposed;
public PayloadFilter(Guid Provider, EVENT_DESCRIPTOR Event, bool MatchAny)
{
//
// Important: Caller must first load the manifest for Provider or Tdh*
// calls will likely fail. Use ManifestParser.cs
//
m_Provider = Provider;
m_MatchAny = MatchAny;
m_Event = Event;
m_Predicates = new List<PAYLOAD_FILTER_PREDICATE>();
}
~PayloadFilter()
{
Dispose(false);
}
protected virtual void Dispose(bool disposing)
{
if (m_Disposed)
{
return;
}
Trace(TraceLoggerType.EtwManifestParser,
TraceEventType.Information,
"Disposing ManifestParser");
m_Disposed = true;
if (m_Filter != nint.Zero)
{
var result = TdhDeletePayloadFilter(ref m_Filter);
Debug.Assert(result == ERROR_SUCCESS);
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void AddPredicate(PAYLOAD_FILTER_PREDICATE Predicate)
{
AddPredicate(Predicate.PayloadFieldName, Predicate.CompareOp, Predicate.Value);
}
public void AddPredicate(string Field, PAYLOAD_OPERATOR Operator, string Value)
{
ValidatePredicate(Field, Operator, Value);
m_Predicates.Add(new PAYLOAD_FILTER_PREDICATE
{
PayloadFieldName = Field,
CompareOp = Operator,
Value = Value,
});
}
public static void ValidatePredicate(string Field, PAYLOAD_OPERATOR Operator, string Value)
{
if (string.IsNullOrEmpty(Field) || string.IsNullOrEmpty(Value))
{
throw new Exception("A field name and value is required.");
}
if (Operator >= PAYLOAD_OPERATOR.Max)
{
throw new Exception("Invalid payload operator.");
}
//
// Notes:
// - All types are derived by ETW from the provider's published manifest. If
// the manifest doesn't exist, the EnableTraceEx will fail.
// - All values must be a simple string, a Guid as a string including
// the braces "{<GUID>}", an integer, or a FILETIME as an integer.
// - Binary or structured types are not supported by ETW payload filtering.
// - For operators that require two integer arguments, they must be
// provided as a string with a comma in between - eg, "1,2"
// - To compare GUIDs, you must use Is or IsNot operators.
//
switch (Operator)
{
case PAYLOAD_OPERATOR.Equal:
case PAYLOAD_OPERATOR.NotEqual:
case PAYLOAD_OPERATOR.LessOrEqual:
case PAYLOAD_OPERATOR.GreaterOrEqual:
case PAYLOAD_OPERATOR.LessThan:
case PAYLOAD_OPERATOR.GreaterThan:
case PAYLOAD_OPERATOR.Modulo:
{
_ = Utilities.StringToInteger(Value);
break;
}
case PAYLOAD_OPERATOR.Between:
case PAYLOAD_OPERATOR.NotBetween:
{
_ = Utilities.GetBetweenArguments(Value);
break;
}
//
// Is, IsNot, Contains, NotContains operators all require a single string value.
//
default:
{
break;
}
}
}
public nint Create()
{
var eventPointer = nint.Zero;
var predicatesPointer = nint.Zero;
Trace(TraceLoggerType.RealTimeTrace,
TraceEventType.Information,
"Creating TDH payload filters...");
if (m_Predicates.Count == 0)
{
throw new Exception("No predicates specified");
}
try
{
eventPointer = Marshal.AllocHGlobal(
Marshal.SizeOf(typeof(EVENT_DESCRIPTOR)));
if (eventPointer == nint.Zero)
{
throw new Exception("Out of memory");
}
Marshal.StructureToPtr(m_Event, eventPointer, false);
var predicateSize = Marshal.SizeOf(typeof(PAYLOAD_FILTER_PREDICATE));
predicatesPointer = Marshal.AllocHGlobal(
predicateSize * m_Predicates.Count);
if (predicatesPointer == nint.Zero)
{
throw new Exception("Out of memory");
}
var pointer = predicatesPointer;
foreach (var predicate in m_Predicates)
{
Marshal.StructureToPtr(predicate, pointer, false);
pointer = nint.Add(pointer, predicateSize);
}
var status = TdhCreatePayloadFilter(
ref m_Provider,
eventPointer,
m_MatchAny,
(uint)m_Predicates.Count,
predicatesPointer,
ref m_Filter);
switch (status)
{
case ERROR_SUCCESS:
{
Debug.Assert(m_Filter != nint.Zero);
Trace(TraceLoggerType.RealTimeTrace,
TraceEventType.Information,
"Payload filter created successfully.");
return m_Filter;
}
case ERROR_INSUFFICIENT_BUFFER:
{
throw new Exception($"Payload is too large - " +
$"max {MaxEventFilterPayloadSize} bytes");
}
case ERROR_NOT_FOUND:
case ERROR_FILE_NOT_FOUND:
{
throw new Exception($"ETW was unable to locate " +
$"the manifest or schema for provider {m_Provider}. " +
$"Usually this is caused by an incorrect field in the " +
$"input event descriptor, such as incorrect event ID or " +
$"version number.");
}
case ERROR_INVALID_PARAMETER:
{
throw new Exception($"TdhCreatePayloadFilter returned " +
$"STATUS_INVALID_PARAMETER, which usually means " +
$"a field name defined in one or more predicates " +
$"is invalid for the provider manifest.");
}
default:
{
throw new Exception($"TdhCreatePayloadFilter failed: 0x{status:X}");
}
}
}
finally
{
if (eventPointer != nint.Zero)
{
Marshal.FreeHGlobal(eventPointer);
}
if (predicatesPointer != nint.Zero)
{
Marshal.FreeHGlobal(predicatesPointer);
}
}
}
}
}