| 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 provider class, to allow using | |
| 17 // Windows Event Tracing for logging transport and control. | |
| 18 #ifndef BASE_EVENT_TRACE_PROVIDER_H_ | |
| 19 #define BASE_EVENT_TRACE_PROVIDER_H_ | |
| 20 | |
| 21 #include <windows.h> | |
| 22 #include <wmistr.h> | |
| 23 #include <evntrace.h> | |
| 24 #include "omaha/base/debug.h" | |
| 25 #include "base/basictypes.h" | |
| 26 | |
| 27 namespace omaha { | |
| 28 | |
| 29 typedef GUID EtwEventClass; | |
| 30 typedef UCHAR EtwEventType; | |
| 31 typedef UCHAR EtwEventLevel; | |
| 32 typedef USHORT EtwEventVersion; | |
| 33 typedef ULONG EtwEventFlags; | |
| 34 | |
| 35 // Base class is a POD for correctness. | |
| 36 template <size_t N> struct EtwMofEventBase { | |
| 37 EVENT_TRACE_HEADER header; | |
| 38 MOF_FIELD fields[N]; | |
| 39 }; | |
| 40 | |
| 41 // Utility class to auto-initialize event trace header structures. | |
| 42 template <size_t N> class EtwMofEvent: public EtwMofEventBase<N> { | |
| 43 public: | |
| 44 typedef EtwMofEventBase<N> Super; | |
| 45 | |
| 46 EtwMofEvent() { | |
| 47 memset(static_cast<Super*>(this), 0, sizeof(Super)); | |
| 48 } | |
| 49 | |
| 50 EtwMofEvent(const EtwEventClass& event_class, EtwEventType type, | |
| 51 EtwEventLevel level) { | |
| 52 memset(static_cast<Super*>(this), 0, sizeof(Super)); | |
| 53 header.Size = sizeof(Super); | |
| 54 header.Guid = event_class; | |
| 55 header.Class.Type = type; | |
| 56 header.Class.Level = level; | |
| 57 header.Flags = WNODE_FLAG_TRACED_GUID | WNODE_FLAG_USE_MOF_PTR; | |
| 58 } | |
| 59 | |
| 60 EtwMofEvent(const EtwEventClass& event_class, EtwEventType type, | |
| 61 EtwEventVersion version, EtwEventLevel level) { | |
| 62 memset(static_cast<Super*>(this), 0, sizeof(Super)); | |
| 63 header.Size = sizeof(Super); | |
| 64 header.Guid = event_class; | |
| 65 header.Class.Type = type; | |
| 66 header.Class.Version = version; | |
| 67 header.Class.Level = level; | |
| 68 header.Flags = WNODE_FLAG_TRACED_GUID | WNODE_FLAG_USE_MOF_PTR; | |
| 69 } | |
| 70 | |
| 71 void SetField(int field, size_t size, const void *data) { | |
| 72 ASSERT1(field < N); | |
| 73 if ((field < N) && (size <= kuint32max)) { | |
| 74 fields[field].DataPtr = reinterpret_cast<ULONG_PTR>(data); | |
| 75 fields[field].Length = static_cast<ULONG>(size); | |
| 76 } | |
| 77 } | |
| 78 | |
| 79 EVENT_TRACE_HEADER* get() { return& header; } | |
| 80 | |
| 81 private: | |
| 82 DISALLOW_COPY_AND_ASSIGN(EtwMofEvent); | |
| 83 }; | |
| 84 | |
| 85 // Trace provider with Event Tracing for Windows. The trace provider | |
| 86 // registers with ETW by its name which is a GUID. ETW calls back to | |
| 87 // the object whenever the trace level or enable flags for this provider | |
| 88 // name changes. | |
| 89 // Users of this class can test whether logging is currently enabled at | |
| 90 // a particular trace level, and whether particular enable flags are set, | |
| 91 // before other resources are consumed to generate and issue the log | |
| 92 // messages themselves. | |
| 93 class EtwTraceProvider { | |
| 94 public: | |
| 95 // Creates an event trace provider identified by provider_name, which | |
| 96 // will be the name registered with Event Tracing for Windows (ETW). | |
| 97 explicit EtwTraceProvider(const GUID& provider_name); | |
| 98 | |
| 99 // Creates an unnamed event trace provider, the provider must be given | |
| 100 // a name before registration. | |
| 101 EtwTraceProvider(); | |
| 102 virtual ~EtwTraceProvider(); | |
| 103 | |
| 104 // Registers the trace provider with Event Tracing for Windows. | |
| 105 // Note: from this point forward ETW may call the provider's control | |
| 106 // callback. If the provider's name is enabled in some trace session | |
| 107 // already, the callback may occur recursively from this call, so | |
| 108 // call this only when you're ready to handle callbacks. | |
| 109 ULONG Register(); | |
| 110 // Unregisters the trace provider with ETW. | |
| 111 ULONG Unregister(); | |
| 112 | |
| 113 // Accessors. | |
| 114 void set_provider_name(const GUID& provider_name) { | |
| 115 provider_name_ = provider_name; | |
| 116 } | |
| 117 const GUID& provider_name() const { return provider_name_; } | |
| 118 TRACEHANDLE registration_handle() const { return registration_handle_; } | |
| 119 TRACEHANDLE session_handle() const { return session_handle_; } | |
| 120 EtwEventFlags enable_flags() const { return enable_flags_; } | |
| 121 EtwEventLevel enable_level() const { return enable_level_; } | |
| 122 | |
| 123 // Returns true iff logging should be performed for "level" and "flags". | |
| 124 // Note: flags is treated as a bitmask, and should normally have a single | |
| 125 // bit set, to test whether to log for a particular sub "facility". | |
| 126 bool ShouldLog(EtwEventLevel level, EtwEventFlags flags) { | |
| 127 return NULL != session_handle_ && level >= enable_level_ && | |
| 128 (0 != (flags & enable_flags_)); | |
| 129 } | |
| 130 | |
| 131 // Simple wrappers to log Unicode and ANSI strings. | |
| 132 // Do nothing if !ShouldLog(level, 0xFFFFFFFF). | |
| 133 ULONG Log(const EtwEventClass& event_class, EtwEventType type, | |
| 134 EtwEventLevel level, const char *message); | |
| 135 ULONG Log(const EtwEventClass& event_class, EtwEventType type, | |
| 136 EtwEventLevel level, const wchar_t *message); | |
| 137 | |
| 138 // Log the provided event. | |
| 139 ULONG Log(EVENT_TRACE_HEADER* event); | |
| 140 | |
| 141 protected: | |
| 142 // These are called after events have been enabled or disabled. | |
| 143 // Override them if you want to do processing at the start or | |
| 144 // end of collection. | |
| 145 // Note: These may be called ETW's thread and they may be racy. | |
| 146 virtual void OnEventsEnabled() {} | |
| 147 virtual void OnEventsDisabled() {} | |
| 148 | |
| 149 private: | |
| 150 ULONG EnableEvents(PVOID buffer); | |
| 151 ULONG DisableEvents(); | |
| 152 ULONG Callback(WMIDPREQUESTCODE request, PVOID buffer); | |
| 153 static ULONG WINAPI ControlCallback(WMIDPREQUESTCODE request, PVOID context, | |
| 154 ULONG *reserved, PVOID buffer); | |
| 155 | |
| 156 GUID provider_name_; | |
| 157 TRACEHANDLE registration_handle_; | |
| 158 TRACEHANDLE session_handle_; | |
| 159 EtwEventFlags enable_flags_; | |
| 160 EtwEventLevel enable_level_; | |
| 161 | |
| 162 // We don't use this, but on XP we're obliged to pass one in to | |
| 163 // RegisterTraceGuids. Non-const, because that's how the API needs it. | |
| 164 static TRACE_GUID_REGISTRATION obligatory_guid_registration_; | |
| 165 | |
| 166 DISALLOW_COPY_AND_ASSIGN(EtwTraceProvider); | |
| 167 }; | |
| 168 | |
| 169 } // namespace omaha | |
| 170 | |
| 171 #endif // BASE_EVENT_TRACE_PROVIDER_H_ | |
| OLD | NEW |