forked from bi-zone/etw
-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.go
301 lines (258 loc) · 12.3 KB
/
options.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
//go:build windows
// +build windows
package etw
// SessionOptions describes Session subscription options.
//
// Most of options will be passed to EnableTraceEx2 and could be refined in
// its docs: https://docs.microsoft.com/en-us/windows/win32/api/evntrace/nf-evntrace-enabletraceex2
type SessionOptions struct {
// Name specifies a name of ETW session being created. Further a session
// could be controlled from other processed by it's name, so it should be
// unique.
Name string
// Ignore any event map information that might have to be parsed from the provider manifest.
// This can speed up event formatting considerably, but enums or bit maps will no longer
// be formatted.
IgnoreMapInfo bool
// Flags to enable on the session. This is only meaningful for a kernel session.
Flags []EnableFlag
LogFileModes []LogFileMode
Kernel bool
// Maximum size of the file used to buffer events, in MB
MaximumFileSize uint32
}
// SessionOption is any function that modifies SessionOptions. Options will be called
// on default config in NewSession. Subsequent options that modifies same
// fields will override each other.
type SessionOption func(cfg *SessionOptions)
// WithName specifies a provided @name for the creating session. Further that
// session could be controlled from other processed by it's name, so it should be
// unique.
func WithName(name string) SessionOption {
return func(cfg *SessionOptions) {
cfg.Name = name
}
}
// WithName specifies a maximum file size for the created session. A session may use
// disk space up to the specified amount in MB.
func WithMaximumFileSize(maximumFileSize uint32) SessionOption {
return func(cfg *SessionOptions) {
cfg.MaximumFileSize = maximumFileSize
}
}
// IgnoreMapInfo specifies whether event map information should be processed.
// SessionOptions.IgnoreMapInfo has further information on this.
func IgnoreMapInfo(ignoreMapInfo bool) SessionOption {
return func(cfg *SessionOptions) {
cfg.IgnoreMapInfo = ignoreMapInfo
}
}
// EnableFlags enables specific flags that specify which events to receive from a kernel
// session. This option is ignored for non-kernel sessions.
func EnableFlags(flags ...EnableFlag) SessionOption {
return func(cfg *SessionOptions) {
cfg.Flags = append(cfg.Flags, flags...)
}
}
// EnableLogModes sets flags that specify properties of the session.
func EnableLogModes(modes ...LogFileMode) SessionOption {
return func(cfg *SessionOptions) {
cfg.LogFileModes = append(cfg.LogFileModes, modes...)
}
}
// ProviderOptions describes subscription options for a single provider.
type ProviderOptions struct {
// Level represents provider-defined value that specifies the level of
// detail included in the event. Higher levels imply that you get lower
// levels as well. For example, with TRACE_LEVEL_ERROR you'll get all
// events except ones with level critical. Check `EventDescriptor.Level`
// values for current event verbosity level.
Level TraceLevel
// MatchAnyKeyword is a bitmask of keywords that determine the category of
// events that you want the provider to write. The provider writes the
// event if any of the event's keyword bits match any of the bits set in
// this mask.
//
// If MatchAnyKeyword is not set the session will receive ALL possible
// events (which is equivalent setting all 64 bits to 1).
//
// Passed as is to EnableTraceEx2. Refer to its remarks for more info:
// https://docs.microsoft.com/en-us/windows/win32/api/evntrace/nf-evntrace-enabletraceex2#remarks
MatchAnyKeyword uint64
// MatchAllKeyword is an optional bitmask that further restricts the
// category of events that you want the provider to write. If the event's
// keyword meets the MatchAnyKeyword condition, the provider will write the
// event only if all of the bits in this mask exist in the event's keyword.
//
// This mask is not used if MatchAnyKeyword is zero.
//
// Passed as is to EnableTraceEx2. Refer to its remarks for more info:
// https://docs.microsoft.com/en-us/windows/win32/api/evntrace/nf-evntrace-enabletraceex2#remarks
MatchAllKeyword uint64
// EnableProperties defines a set of provider properties consumer wants to
// enable. Properties adds fields to ExtendedEventInfo or asks provider to
// sent more events.
//
// For more info about available properties check EnableProperty doc and
// original API reference:
// https://docs.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-enable_trace_parameters
EnableProperties []EnableProperty
// Filters defines a set of filters that limit the events which are sent by the provider.
// For a full list of possible filters, see here:
// https://docs.microsoft.com/en-us/windows/win32/api/evntprov/ns-evntprov-event_filter_descriptor
// If multiple Filters with the same filter type are specified, they are merged; check the filter
// type on what can be merged.
Filters []EventFilter
// TriggerRundown requests a log of the provider's state information. This typically causes a number
// of rundown events to be sent at the provider's start.
TriggerRundown bool
}
// ProviderOption is any function that modifies ProviderOptions. Options will be called
// on default config in NewSession. Subsequent options that modifies same
// fields will override each other.
type ProviderOption func(cfg *ProviderOptions)
// WithLevel specifies a maximum level consumer is interested in. Higher levels
// imply that you get lower levels as well. For example, with TRACE_LEVEL_ERROR
// you'll get all events except ones with level critical.
func WithLevel(lvl TraceLevel) ProviderOption {
return func(cfg *ProviderOptions) {
cfg.Level = lvl
}
}
// WithMatchKeywords allows to specify keywords of receiving events. Each event
// has a set of keywords associated with it. That keywords are encoded as bit
// masks and matched with provided @anyKeyword and @allKeyword values.
//
// A session will receive only those events whose keywords masks has ANY of
// @anyKeyword and ALL of @allKeyword bits sets.
//
// For more info take a look a ProviderOptions docs. To query keywords defined
// by specific provider identified by <GUID> try:
//
// logman query providers <GUID>
func WithMatchKeywords(anyKeyword, allKeyword uint64) ProviderOption {
return func(cfg *ProviderOptions) {
cfg.MatchAnyKeyword = anyKeyword
cfg.MatchAllKeyword = allKeyword
}
}
// WithProperty enables additional provider feature toggled by @p. Subsequent
// WithProperty options will enable all provided options.
//
// For more info about available properties check EnableProperty doc and
// original API reference:
// https://docs.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-enable_trace_parameters
func WithProperty(p EnableProperty) ProviderOption {
return func(cfg *ProviderOptions) {
cfg.EnableProperties = append(cfg.EnableProperties, p)
}
}
// WithFilter limits the events that the provider sends. Multiple filters can
// be specified to limit the events even further.
// If multiple Filters with the same filter type are specified, they are merged; check the filter
// type on what can be merged.
func WithFilter(f EventFilter) ProviderOption {
return func(cfg *ProviderOptions) {
cfg.Filters = append(cfg.Filters, f)
}
}
// WithRundown requests a rundown of the current provider state when the provider is added.
func WithRundown(withRundown bool) ProviderOption {
return func(cfg *ProviderOptions) {
cfg.TriggerRundown = withRundown
}
}
// TraceLevel represents provider-defined value that specifies the level of
// detail included in the event. Higher levels imply that you get lower
// levels as well.
type TraceLevel uint8
//nolint:golint,stylecheck // We keep original names to underline that it's an external constants.
const (
TRACE_LEVEL_CRITICAL = TraceLevel(1)
TRACE_LEVEL_ERROR = TraceLevel(2)
TRACE_LEVEL_WARNING = TraceLevel(3)
TRACE_LEVEL_INFORMATION = TraceLevel(4)
TRACE_LEVEL_VERBOSE = TraceLevel(5)
)
// EnableProperty enables a property of a provider session is subscribing for.
//
// For more info about available properties check original API reference:
// https://docs.microsoft.com/en-us/windows/win32/api/evntrace/ns-evntrace-enable_trace_parameters
type EnableProperty uint32
//nolint:golint,stylecheck // We keep original names to underline that it's an external constants.
const (
// Include in the ExtendedEventInfo the security identifier (SID) of the user.
EVENT_ENABLE_PROPERTY_SID = EnableProperty(0x001)
// Include in the ExtendedEventInfo the terminal session identifier.
EVENT_ENABLE_PROPERTY_TS_ID = EnableProperty(0x002)
// Include in the ExtendedEventInfo a call stack trace for events written
// using EventWrite.
EVENT_ENABLE_PROPERTY_STACK_TRACE = EnableProperty(0x004)
// Filters out all events that do not have a non-zero keyword specified.
// By default events with 0 keywords are accepted.
EVENT_ENABLE_PROPERTY_IGNORE_KEYWORD_0 = EnableProperty(0x010)
// Filters out all events that are either marked as an InPrivate event or
// come from a process that is marked as InPrivate. InPrivate implies that
// the event or process contains some data that would be considered private
// or personal. It is up to the process or event to designate itself as
// InPrivate for this to work.
EVENT_ENABLE_PROPERTY_EXCLUDE_INPRIVATE = EnableProperty(0x200)
)
type EnableFlag uint32
const (
EVENT_TRACE_FLAG_PROCESS EnableFlag = 0x00000001
EVENT_TRACE_FLAG_THREAD EnableFlag = 0x00000002
EVENT_TRACE_FLAG_IMAGE_LOAD EnableFlag = 0x00000004
EVENT_TRACE_FLAG_DISK_IO EnableFlag = 0x00000100
EVENT_TRACE_FLAG_DISK_FILE_IO EnableFlag = 0x00000200
EVENT_TRACE_FLAG_MEMORY_PAGE_FAULTS EnableFlag = 0x00001000
EVENT_TRACE_FLAG_MEMORY_HARD_FAULTS EnableFlag = 0x00002000
EVENT_TRACE_FLAG_NETWORK_TCPIP EnableFlag = 0x00010000
EVENT_TRACE_FLAG_REGISTRY EnableFlag = 0x00020000
EVENT_TRACE_FLAG_DBGPRINT EnableFlag = 0x00040000
EVENT_TRACE_FLAG_PROCESS_COUNTERS EnableFlag = 0x00000008
EVENT_TRACE_FLAG_CSWITCH EnableFlag = 0x00000010
EVENT_TRACE_FLAG_DPC EnableFlag = 0x00000020
EVENT_TRACE_FLAG_INTERRUPT EnableFlag = 0x00000040
EVENT_TRACE_FLAG_SYSTEMCALL EnableFlag = 0x00000080
EVENT_TRACE_FLAG_DISK_IO_INIT EnableFlag = 0x00000400
EVENT_TRACE_FLAG_ALPC EnableFlag = 0x00100000
EVENT_TRACE_FLAG_SPLIT_IO EnableFlag = 0x00200000
EVENT_TRACE_FLAG_DRIVER EnableFlag = 0x00800000
EVENT_TRACE_FLAG_PROFILE EnableFlag = 0x01000000
EVENT_TRACE_FLAG_FILE_IO EnableFlag = 0x02000000
EVENT_TRACE_FLAG_FILE_IO_INIT EnableFlag = 0x04000000
EVENT_TRACE_FLAG_DISPATCHER EnableFlag = 0x00000800
EVENT_TRACE_FLAG_VIRTUAL_ALLOC EnableFlag = 0x00004000
EVENT_TRACE_FLAG_VAMAP EnableFlag = 0x00008000
EVENT_TRACE_FLAG_NO_SYSCONFIG EnableFlag = 0x10000000
EVENT_TRACE_FLAG_JOB EnableFlag = 0x00080000
EVENT_TRACE_FLAG_DEBUG_EVENTS EnableFlag = 0x00400000
EVENT_TRACE_FLAG_EXTENSION EnableFlag = 0x80000000
EVENT_TRACE_FLAG_FORWARD_WMI EnableFlag = 0x40000000
EVENT_TRACE_FLAG_ENABLE_RESERVE EnableFlag = 0x20000000
EVENT_TRACE_FLAG_OBTRACE EnableFlag = 0x80000040
// EVENT_TRACE_FLAG_RUNDOWN is not a real flag, but another undefined behavior.
// Use to cause a call TraceSetInformation with an empty mask, which activates rundown events.
EVENT_TRACE_FLAG_RUNDOWN EnableFlag = 0x00000000
)
// Map with all events that must be set via the 5th byte in TraceSetInformation instead of the normal EnableFlags
var traceSetInformationFlags = map[EnableFlag]bool{
EVENT_TRACE_FLAG_OBTRACE: true,
}
type LogFileMode uint32
const (
// EVENT_TRACE_SECURE_MODE specifies that secure mode should be enabled on the session.
// This restricts who may log events to the session.
EVENT_TRACE_SECURE_MODE = LogFileMode(0x00000080)
// EVENT_TRACE_SYSTEM_LOGGER_MODE specifies that the session will receive events from the
// SystemTraceProvider.
EVENT_TRACE_SYSTEM_LOGGER_MODE = LogFileMode(0x02000000)
// EVENT_TRACE_INDEPENDENT_SESSION_MODE specifies that this session should not be affected
// by failures in other ETW sessions.
EVENT_TRACE_INDEPENDENT_SESSION_MODE = LogFileMode(0x08000000)
)
const (
// kernelLoggerName is the name of the kernel logging ETW session that exists on older machines.
kernelLoggerName = "NT Kernel Logger"
)