OLD | NEW |
| (Empty) |
1 // Copyright 2010 Google Inc. | |
2 // | |
3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 // you may not use this file except in compliance with the License. | |
5 // You may obtain a copy of the License at | |
6 // | |
7 // http://www.apache.org/licenses/LICENSE-2.0 | |
8 // | |
9 // Unless required by applicable law or agreed to in writing, software | |
10 // distributed under the License is distributed on an "AS IS" BASIS, | |
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 // See the License for the specific language governing permissions and | |
13 // limitations under the License. | |
14 // ======================================================================== | |
15 // | |
16 // Declaration of a Windows event trace controller class. | |
17 // The controller takes care of creating and manipulating event trace | |
18 // sessions. | |
19 // | |
20 // Event tracing for Windows is a system-provided service that provides | |
21 // logging control and high-performance transport for generic, binary trace | |
22 // events. Event trace providers register with the system by their name, | |
23 // which is a GUID, and can from that point forward receive callbacks that | |
24 // start or end tracing and that change their trace level and enable mask. | |
25 // | |
26 // A trace controller can create an event tracing session, which either | |
27 // sends events to a binary file, or to a realtime consumer, or both. | |
28 // | |
29 // A trace consumer consumes events from zero or one realtime session, | |
30 // as well as potentially from multiple binary trace files. | |
31 #ifndef BASE_EVENT_TRACE_CONTROLLER_H_ | |
32 #define BASE_EVENT_TRACE_CONTROLLER_H_ | |
33 | |
34 #include <windows.h> | |
35 #include <wmistr.h> | |
36 #include <evntrace.h> | |
37 #include <string> | |
38 #include "base/basictypes.h" | |
39 | |
40 namespace omaha { | |
41 | |
42 // Utility class to make it easier to work with EVENT_TRACE_PROPERTIES. | |
43 // The EVENT_TRACE_PROPERTIES structure contains information about an | |
44 // event tracing session. | |
45 class EtwTraceProperties { | |
46 public: | |
47 EtwTraceProperties() { | |
48 memset(buffer_, 0, sizeof(buffer_)); | |
49 EVENT_TRACE_PROPERTIES* prop = get(); | |
50 | |
51 prop->Wnode.BufferSize = sizeof(buffer_); | |
52 prop->Wnode.Flags = WNODE_FLAG_TRACED_GUID; | |
53 prop->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES); | |
54 prop->LogFileNameOffset = sizeof(EVENT_TRACE_PROPERTIES) + | |
55 sizeof(wchar_t) * kMaxStringLen; | |
56 } | |
57 | |
58 EVENT_TRACE_PROPERTIES* get() { | |
59 return &properties_; | |
60 } | |
61 const EVENT_TRACE_PROPERTIES* get() const { | |
62 return reinterpret_cast<const EVENT_TRACE_PROPERTIES*>(&properties_); | |
63 } | |
64 | |
65 const wchar_t* GetLoggerName() const { | |
66 return reinterpret_cast<const wchar_t *>(buffer_ + get()->LoggerNameOffset); | |
67 } | |
68 | |
69 HRESULT SetLoggerName(const wchar_t* logger_name) { | |
70 size_t len = wcslen(logger_name) + 1; | |
71 if (kMaxStringLen < len) | |
72 return E_INVALIDARG; | |
73 | |
74 memcpy(buffer_ + get()->LoggerNameOffset, | |
75 logger_name, | |
76 sizeof(wchar_t) * len); | |
77 return S_OK; | |
78 } | |
79 | |
80 const wchar_t* GetLoggerFileName() const { | |
81 return reinterpret_cast<const wchar_t*>(buffer_ + get()->LogFileNameOffset); | |
82 } | |
83 | |
84 HRESULT SetLoggerFileName(const wchar_t* logger_file_name) { | |
85 size_t len = wcslen(logger_file_name) + 1; | |
86 if (kMaxStringLen < len) | |
87 return E_INVALIDARG; | |
88 | |
89 memcpy(buffer_ + get()->LogFileNameOffset, | |
90 logger_file_name, | |
91 sizeof(wchar_t) * len); | |
92 return S_OK; | |
93 } | |
94 | |
95 // Max string len for name and session name is 1024 per documentation. | |
96 static const size_t kMaxStringLen = 1024; | |
97 // Properties buffer allocates space for header and for | |
98 // max length for name and session name. | |
99 static const size_t kBufSize = sizeof(EVENT_TRACE_PROPERTIES) | |
100 + 2 * sizeof(wchar_t) * (kMaxStringLen); | |
101 | |
102 private: | |
103 // The EVENT_TRACE_PROPERTIES structure needs to be overlaid on a | |
104 // larger buffer to allow storing the logger name and logger file | |
105 // name contiguously with the structure. | |
106 union { | |
107 public: | |
108 // Our properties header. | |
109 EVENT_TRACE_PROPERTIES properties_; | |
110 // The actual size of the buffer is forced by this member. | |
111 char buffer_[kBufSize]; | |
112 }; | |
113 | |
114 DISALLOW_COPY_AND_ASSIGN(EtwTraceProperties); | |
115 }; | |
116 | |
117 // This class implements an ETW controller, which knows how to start and | |
118 // stop event tracing sessions, as well as controlling ETW provider | |
119 // log levels and enable bit masks under the session. | |
120 class EtwTraceController { | |
121 public: | |
122 EtwTraceController(); | |
123 ~EtwTraceController(); | |
124 | |
125 // Start a session with given name and properties. | |
126 HRESULT Start(const wchar_t* session_name, EtwTraceProperties* prop); | |
127 | |
128 // Starts a session tracing to a file with some default properties. | |
129 HRESULT StartFileSession(const wchar_t* session_name, | |
130 const wchar_t* logfile_path, | |
131 bool realtime = false); | |
132 | |
133 // Starts a realtime session with some default properties. | |
134 HRESULT StartRealtimeSession(const wchar_t* session_name, | |
135 size_t buffer_size); | |
136 | |
137 // Enables "provider" at "level" for this session. | |
138 // This will cause all providers registered with the GUID | |
139 // "provider" to start tracing at the new level, systemwide. | |
140 HRESULT EnableProvider(const GUID& provider, UCHAR level, | |
141 ULONG flags = 0xFFFFFFFF); | |
142 // Disables "provider". | |
143 HRESULT DisableProvider(const GUID& provider); | |
144 | |
145 // Stops our session and retrieve the new properties of the session, | |
146 // properties may be NULL. | |
147 HRESULT Stop(EtwTraceProperties* properties); | |
148 | |
149 // Flushes our session and retrieve the current properties, | |
150 // properties may be NULL. | |
151 HRESULT Flush(EtwTraceProperties* properties); | |
152 | |
153 // Static utility functions for controlling | |
154 // sessions we don't necessarily own. | |
155 static HRESULT Start(const wchar_t* session_name, | |
156 EtwTraceProperties* properties, | |
157 TRACEHANDLE* session_handle); | |
158 | |
159 static HRESULT Query(const wchar_t* session_name, | |
160 EtwTraceProperties* properties); | |
161 | |
162 static HRESULT Update(const wchar_t* session_name, | |
163 EtwTraceProperties* properties); | |
164 | |
165 static HRESULT Stop(const wchar_t* session_name, | |
166 EtwTraceProperties* properties); | |
167 static HRESULT Flush(const wchar_t* session_name, | |
168 EtwTraceProperties* properties); | |
169 | |
170 // Accessors. | |
171 TRACEHANDLE session() const { return session_; } | |
172 const wchar_t* session_name() const { return session_name_.c_str(); } | |
173 | |
174 private: | |
175 std::wstring session_name_; | |
176 TRACEHANDLE session_; | |
177 | |
178 DISALLOW_COPY_AND_ASSIGN(EtwTraceController); | |
179 }; | |
180 | |
181 } // namespace omaha | |
182 | |
183 #endif // BASE_EVENT_TRACE_CONTROLLER_H_ | |
OLD | NEW |