OLD | NEW |
| (Empty) |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | |
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 controller class. | |
6 // The controller takes care of creating and manipulating event trace | |
7 // sessions. | |
8 // | |
9 // Event tracing for Windows is a system-provided service that provides | |
10 // logging control and high-performance transport for generic, binary trace | |
11 // events. Event trace providers register with the system by their name, | |
12 // which is a GUID, and can from that point forward receive callbacks that | |
13 // start or end tracing and that change their trace level and enable mask. | |
14 // | |
15 // A trace controller can create an event tracing session, which either | |
16 // sends events to a binary file, or to a realtime consumer, or both. | |
17 // | |
18 // A trace consumer consumes events from zero or one realtime session, | |
19 // as well as potentially from multiple binary trace files. | |
20 #ifndef BASE_EVENT_TRACE_CONTROLLER_WIN_H_ | |
21 #define BASE_EVENT_TRACE_CONTROLLER_WIN_H_ | |
22 #pragma once | |
23 | |
24 #include <windows.h> | |
25 #include <wmistr.h> | |
26 #include <evntrace.h> | |
27 #include <string> | |
28 #include "base/basictypes.h" | |
29 | |
30 // Utility class to make it easier to work with EVENT_TRACE_PROPERTIES. | |
31 // The EVENT_TRACE_PROPERTIES structure contains information about an | |
32 // event tracing session. | |
33 class EtwTraceProperties { | |
34 public: | |
35 EtwTraceProperties(); | |
36 | |
37 EVENT_TRACE_PROPERTIES* get() { | |
38 return &properties_; | |
39 } | |
40 | |
41 const EVENT_TRACE_PROPERTIES* get() const { | |
42 return reinterpret_cast<const EVENT_TRACE_PROPERTIES*>(&properties_); | |
43 } | |
44 | |
45 const wchar_t* GetLoggerName() const { | |
46 return reinterpret_cast<const wchar_t *>(buffer_ + get()->LoggerNameOffset); | |
47 } | |
48 | |
49 // Copies logger_name to the properties structure. | |
50 HRESULT SetLoggerName(const wchar_t* logger_name); | |
51 const wchar_t* GetLoggerFileName() const { | |
52 return reinterpret_cast<const wchar_t*>(buffer_ + get()->LogFileNameOffset); | |
53 } | |
54 | |
55 // Copies logger_file_name to the properties structure. | |
56 HRESULT SetLoggerFileName(const wchar_t* logger_file_name); | |
57 | |
58 // Max string len for name and session name is 1024 per documentation. | |
59 static const size_t kMaxStringLen = 1024; | |
60 // Properties buffer allocates space for header and for | |
61 // max length for name and session name. | |
62 static const size_t kBufSize = sizeof(EVENT_TRACE_PROPERTIES) | |
63 + 2 * sizeof(wchar_t) * (kMaxStringLen); | |
64 | |
65 private: | |
66 // The EVENT_TRACE_PROPERTIES structure needs to be overlaid on a | |
67 // larger buffer to allow storing the logger name and logger file | |
68 // name contiguously with the structure. | |
69 union { | |
70 public: | |
71 // Our properties header. | |
72 EVENT_TRACE_PROPERTIES properties_; | |
73 // The actual size of the buffer is forced by this member. | |
74 char buffer_[kBufSize]; | |
75 }; | |
76 | |
77 DISALLOW_COPY_AND_ASSIGN(EtwTraceProperties); | |
78 }; | |
79 | |
80 // This class implements an ETW controller, which knows how to start and | |
81 // stop event tracing sessions, as well as controlling ETW provider | |
82 // log levels and enable bit masks under the session. | |
83 class EtwTraceController { | |
84 public: | |
85 EtwTraceController(); | |
86 ~EtwTraceController(); | |
87 | |
88 // Start a session with given name and properties. | |
89 HRESULT Start(const wchar_t* session_name, EtwTraceProperties* prop); | |
90 | |
91 // Starts a session tracing to a file with some default properties. | |
92 HRESULT StartFileSession(const wchar_t* session_name, | |
93 const wchar_t* logfile_path, | |
94 bool realtime = false); | |
95 | |
96 // Starts a realtime session with some default properties. | |
97 HRESULT StartRealtimeSession(const wchar_t* session_name, | |
98 size_t buffer_size); | |
99 | |
100 // Enables "provider" at "level" for this session. | |
101 // This will cause all providers registered with the GUID | |
102 // "provider" to start tracing at the new level, systemwide. | |
103 HRESULT EnableProvider(const GUID& provider, UCHAR level, | |
104 ULONG flags = 0xFFFFFFFF); | |
105 // Disables "provider". | |
106 HRESULT DisableProvider(const GUID& provider); | |
107 | |
108 // Stops our session and retrieve the new properties of the session, | |
109 // properties may be NULL. | |
110 HRESULT Stop(EtwTraceProperties* properties); | |
111 | |
112 // Flushes our session and retrieve the current properties, | |
113 // properties may be NULL. | |
114 HRESULT Flush(EtwTraceProperties* properties); | |
115 | |
116 // Static utility functions for controlling | |
117 // sessions we don't necessarily own. | |
118 static HRESULT Start(const wchar_t* session_name, | |
119 EtwTraceProperties* properties, | |
120 TRACEHANDLE* session_handle); | |
121 | |
122 static HRESULT Query(const wchar_t* session_name, | |
123 EtwTraceProperties* properties); | |
124 | |
125 static HRESULT Update(const wchar_t* session_name, | |
126 EtwTraceProperties* properties); | |
127 | |
128 static HRESULT Stop(const wchar_t* session_name, | |
129 EtwTraceProperties* properties); | |
130 static HRESULT Flush(const wchar_t* session_name, | |
131 EtwTraceProperties* properties); | |
132 | |
133 // Accessors. | |
134 TRACEHANDLE session() const { return session_; } | |
135 const wchar_t* session_name() const { return session_name_.c_str(); } | |
136 | |
137 private: | |
138 std::wstring session_name_; | |
139 TRACEHANDLE session_; | |
140 | |
141 DISALLOW_COPY_AND_ASSIGN(EtwTraceController); | |
142 }; | |
143 | |
144 #endif // BASE_EVENT_TRACE_CONTROLLER_WIN_H_ | |
OLD | NEW |