-
Notifications
You must be signed in to change notification settings - Fork 20
/
options.go
154 lines (134 loc) · 6.16 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
//+build windows
package etw
/*
#include "windows.h"
*/
import "C"
// 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
// 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
}
// Option 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 Option 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) Option {
return func(cfg *SessionOptions) {
cfg.Name = name
}
}
// 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) Option {
return func(cfg *SessionOptions) {
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 SessionOptions docs. To query keywords defined
// by specific provider identified by <GUID> try:
// logman query providers <GUID>
func WithMatchKeywords(anyKeyword, allKeyword uint64) Option {
return func(cfg *SessionOptions) {
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) Option {
return func(cfg *SessionOptions) {
cfg.EnableProperties = append(cfg.EnableProperties, p)
}
}
// 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 C.UCHAR
//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 C.ULONG
//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)
)