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