security/sandbox/chromium/base/win/event_trace_provider.h

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

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

mercurial