1 // Copyright 2012 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 // 5 // Declaration of a Windows event trace consumer base class. 6 #ifndef BASE_WIN_EVENT_TRACE_CONSUMER_H_ 7 #define BASE_WIN_EVENT_TRACE_CONSUMER_H_ 8 9 #include <windows.h> 10 11 #include <evntrace.h> 12 #include <stddef.h> 13 #include <wmistr.h> 14 15 #include <vector> 16 17 #include "base/threading/scoped_blocking_call.h" 18 19 namespace base { 20 namespace win { 21 22 // This class is a base class that makes it easier to consume events 23 // from realtime or file sessions. Concrete consumers need to subclass 24 // a specialization of this class and override the ProcessEvent and/or 25 // the ProcessBuffer methods to implement the event consumption logic. 26 // Usage might look like: 27 // class MyConsumer: public EtwTraceConsumerBase<MyConsumer, 1> { 28 // protected: 29 // static VOID WINAPI ProcessEvent(PEVENT_TRACE event); 30 // }; 31 // 32 // MyConsumer consumer; 33 // consumer.OpenFileSession(file_path); 34 // consumer.Consume(); 35 template <class ImplClass> 36 class EtwTraceConsumerBase { 37 public: 38 // Constructs a closed consumer. 39 EtwTraceConsumerBase() = default; 40 41 EtwTraceConsumerBase(const EtwTraceConsumerBase&) = delete; 42 EtwTraceConsumerBase& operator=(const EtwTraceConsumerBase&) = delete; 43 ~EtwTraceConsumerBase()44 ~EtwTraceConsumerBase() { Close(); } 45 46 // Opens the named realtime session, which must be existent. 47 // Note: You can use OpenRealtimeSession or OpenFileSession 48 // to open as many as MAXIMUM_WAIT_OBJECTS (63) sessions at 49 // any one time, though only one of them may be a realtime 50 // session. 51 HRESULT OpenRealtimeSession(const wchar_t* session_name); 52 53 // Opens the event trace log in "file_name", which must be a full or 54 // relative path to an existing event trace log file. 55 // Note: You can use OpenRealtimeSession or OpenFileSession 56 // to open as many as kNumSessions at any one time. 57 HRESULT OpenFileSession(const wchar_t* file_name); 58 59 // Consume all open sessions from beginning to end. 60 HRESULT Consume(); 61 62 // Close all open sessions. 63 HRESULT Close(); 64 65 protected: 66 // Override in subclasses to handle events. ProcessEvent(EVENT_TRACE * event)67 static void ProcessEvent(EVENT_TRACE* event) {} 68 // Override in subclasses to handle buffers. ProcessBuffer(EVENT_TRACE_LOGFILE * buffer)69 static bool ProcessBuffer(EVENT_TRACE_LOGFILE* buffer) { 70 return true; // keep going 71 } 72 73 protected: 74 // Currently open sessions. 75 std::vector<TRACEHANDLE> trace_handles_; 76 77 private: 78 // These delegate to ImplClass callbacks with saner signatures. ProcessEventCallback(EVENT_TRACE * event)79 static void WINAPI ProcessEventCallback(EVENT_TRACE* event) { 80 ImplClass::ProcessEvent(event); 81 } ProcessBufferCallback(PEVENT_TRACE_LOGFILE buffer)82 static ULONG WINAPI ProcessBufferCallback(PEVENT_TRACE_LOGFILE buffer) { 83 return ImplClass::ProcessBuffer(buffer); 84 } 85 }; 86 87 template <class ImplClass> OpenRealtimeSession(const wchar_t * session_name)88inline HRESULT EtwTraceConsumerBase<ImplClass>::OpenRealtimeSession( 89 const wchar_t* session_name) { 90 EVENT_TRACE_LOGFILE logfile = {}; 91 logfile.LoggerName = const_cast<wchar_t*>(session_name); 92 logfile.LogFileMode = EVENT_TRACE_REAL_TIME_MODE; 93 logfile.BufferCallback = &ProcessBufferCallback; 94 logfile.EventCallback = &ProcessEventCallback; 95 logfile.Context = this; 96 TRACEHANDLE trace_handle = ::OpenTrace(&logfile); 97 if (reinterpret_cast<TRACEHANDLE>(INVALID_HANDLE_VALUE) == trace_handle) 98 return HRESULT_FROM_WIN32(::GetLastError()); 99 100 trace_handles_.push_back(trace_handle); 101 return S_OK; 102 } 103 104 template <class ImplClass> OpenFileSession(const wchar_t * file_name)105inline HRESULT EtwTraceConsumerBase<ImplClass>::OpenFileSession( 106 const wchar_t* file_name) { 107 EVENT_TRACE_LOGFILE logfile = {}; 108 logfile.LogFileName = const_cast<wchar_t*>(file_name); 109 logfile.BufferCallback = &ProcessBufferCallback; 110 logfile.EventCallback = &ProcessEventCallback; 111 logfile.Context = this; 112 TRACEHANDLE trace_handle = ::OpenTrace(&logfile); 113 if (reinterpret_cast<TRACEHANDLE>(INVALID_HANDLE_VALUE) == trace_handle) 114 return HRESULT_FROM_WIN32(::GetLastError()); 115 116 trace_handles_.push_back(trace_handle); 117 return S_OK; 118 } 119 120 template <class ImplClass> Consume()121inline HRESULT EtwTraceConsumerBase<ImplClass>::Consume() { 122 base::ScopedBlockingCall scoped_blocking_call(FROM_HERE, 123 base::BlockingType::MAY_BLOCK); 124 ULONG err = ::ProcessTrace(&trace_handles_[0], 125 static_cast<ULONG>(trace_handles_.size()), nullptr, 126 nullptr); 127 return HRESULT_FROM_WIN32(err); 128 } 129 130 template <class ImplClass> Close()131inline HRESULT EtwTraceConsumerBase<ImplClass>::Close() { 132 HRESULT hr = S_OK; 133 for (size_t i = 0; i < trace_handles_.size(); ++i) { 134 if (NULL != trace_handles_[i]) { 135 ULONG ret = ::CloseTrace(trace_handles_[i]); 136 trace_handles_[i] = NULL; 137 138 if (FAILED(HRESULT_FROM_WIN32(ret))) 139 hr = HRESULT_FROM_WIN32(ret); 140 } 141 } 142 trace_handles_.clear(); 143 144 return hr; 145 } 146 147 } // namespace win 148 } // namespace base 149 150 #endif // BASE_WIN_EVENT_TRACE_CONSUMER_H_ 151