1 // Copyright 2009 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 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 // Implementation of a Windows event trace controller class.
11
12 #include "base/win/event_trace_controller.h"
13
14 #include "base/check.h"
15 #include "base/numerics/checked_math.h"
16
17 constexpr size_t kDefaultRealtimeBufferSizeKb = 16;
18
19 namespace base {
20 namespace win {
21
EtwTraceProperties()22 EtwTraceProperties::EtwTraceProperties() {
23 memset(buffer_, 0, sizeof(buffer_));
24 EVENT_TRACE_PROPERTIES* prop = get();
25
26 prop->Wnode.BufferSize = sizeof(buffer_);
27 prop->Wnode.Flags = WNODE_FLAG_TRACED_GUID;
28 prop->LoggerNameOffset = sizeof(EVENT_TRACE_PROPERTIES);
29 prop->LogFileNameOffset =
30 sizeof(EVENT_TRACE_PROPERTIES) + sizeof(wchar_t) * kMaxStringLen;
31 }
32
SetLoggerName(const wchar_t * logger_name)33 HRESULT EtwTraceProperties::SetLoggerName(const wchar_t* logger_name) {
34 size_t len = wcslen(logger_name) + 1;
35 if (kMaxStringLen < len)
36 return E_INVALIDARG;
37
38 memcpy(buffer_ + get()->LoggerNameOffset, logger_name, sizeof(wchar_t) * len);
39 return S_OK;
40 }
41
SetLoggerFileName(const wchar_t * logger_file_name)42 HRESULT EtwTraceProperties::SetLoggerFileName(const wchar_t* logger_file_name) {
43 size_t len = wcslen(logger_file_name) + 1;
44 if (kMaxStringLen < len)
45 return E_INVALIDARG;
46
47 memcpy(buffer_ + get()->LogFileNameOffset, logger_file_name,
48 sizeof(wchar_t) * len);
49 return S_OK;
50 }
51
52 EtwTraceController::EtwTraceController() = default;
53
~EtwTraceController()54 EtwTraceController::~EtwTraceController() {
55 if (session_)
56 Stop(nullptr);
57 }
58
Start(const wchar_t * session_name,EtwTraceProperties * prop)59 HRESULT EtwTraceController::Start(const wchar_t* session_name,
60 EtwTraceProperties* prop) {
61 DCHECK(NULL == session_ && session_name_.empty());
62 EtwTraceProperties ignore;
63 if (prop == nullptr)
64 prop = &ignore;
65
66 HRESULT hr = Start(session_name, prop, &session_);
67 if (SUCCEEDED(hr))
68 session_name_ = session_name;
69
70 return hr;
71 }
72
StartFileSession(const wchar_t * session_name,const wchar_t * logfile_path,bool realtime)73 HRESULT EtwTraceController::StartFileSession(const wchar_t* session_name,
74 const wchar_t* logfile_path,
75 bool realtime) {
76 DCHECK(NULL == session_ && session_name_.empty());
77
78 EtwTraceProperties prop;
79 prop.SetLoggerFileName(logfile_path);
80 EVENT_TRACE_PROPERTIES& p = *prop.get();
81 p.Wnode.ClientContext = 1; // QPC timer accuracy.
82 p.LogFileMode = EVENT_TRACE_FILE_MODE_SEQUENTIAL; // Sequential log.
83 if (realtime)
84 p.LogFileMode |= EVENT_TRACE_REAL_TIME_MODE;
85
86 p.MaximumFileSize = 100; // 100M file size.
87 p.FlushTimer = 30; // 30 seconds flush lag.
88 return Start(session_name, &prop);
89 }
90
StartRealtimeSession(const wchar_t * session_name,size_t buffer_size)91 HRESULT EtwTraceController::StartRealtimeSession(const wchar_t* session_name,
92 size_t buffer_size) {
93 DCHECK(NULL == session_ && session_name_.empty());
94
95 EtwTraceProperties prop;
96 EVENT_TRACE_PROPERTIES& p = *prop.get();
97 p.LogFileMode = EVENT_TRACE_REAL_TIME_MODE | EVENT_TRACE_USE_PAGED_MEMORY;
98 p.FlushTimer = 1; // flush every second.
99 p.BufferSize = checked_cast<ULONG>(
100 buffer_size ? buffer_size : kDefaultRealtimeBufferSizeKb);
101 p.LogFileNameOffset = 0;
102 return Start(session_name, &prop);
103 }
104
EnableProvider(REFGUID provider,UCHAR level,ULONG flags)105 HRESULT EtwTraceController::EnableProvider(REFGUID provider,
106 UCHAR level,
107 ULONG flags) {
108 ULONG error = ::EnableTrace(TRUE, flags, level, &provider, session_);
109 return HRESULT_FROM_WIN32(error);
110 }
111
DisableProvider(REFGUID provider)112 HRESULT EtwTraceController::DisableProvider(REFGUID provider) {
113 ULONG error = ::EnableTrace(FALSE, 0, 0, &provider, session_);
114 return HRESULT_FROM_WIN32(error);
115 }
116
Stop(EtwTraceProperties * properties)117 HRESULT EtwTraceController::Stop(EtwTraceProperties* properties) {
118 EtwTraceProperties ignore;
119 if (properties == nullptr)
120 properties = &ignore;
121
122 ULONG error = ::ControlTrace(session_, nullptr, properties->get(),
123 EVENT_TRACE_CONTROL_STOP);
124 if (ERROR_SUCCESS != error)
125 return HRESULT_FROM_WIN32(error);
126
127 session_ = NULL;
128 session_name_.clear();
129 return S_OK;
130 }
131
Flush(EtwTraceProperties * properties)132 HRESULT EtwTraceController::Flush(EtwTraceProperties* properties) {
133 EtwTraceProperties ignore;
134 if (properties == nullptr)
135 properties = &ignore;
136
137 ULONG error = ::ControlTrace(session_, nullptr, properties->get(),
138 EVENT_TRACE_CONTROL_FLUSH);
139 if (ERROR_SUCCESS != error)
140 return HRESULT_FROM_WIN32(error);
141
142 return S_OK;
143 }
144
Start(const wchar_t * session_name,EtwTraceProperties * properties,TRACEHANDLE * session_handle)145 HRESULT EtwTraceController::Start(const wchar_t* session_name,
146 EtwTraceProperties* properties,
147 TRACEHANDLE* session_handle) {
148 DCHECK(properties != nullptr);
149 ULONG err = ::StartTrace(session_handle, session_name, properties->get());
150 return HRESULT_FROM_WIN32(err);
151 }
152
Query(const wchar_t * session_name,EtwTraceProperties * properties)153 HRESULT EtwTraceController::Query(const wchar_t* session_name,
154 EtwTraceProperties* properties) {
155 ULONG err = ::ControlTrace(NULL, session_name, properties->get(),
156 EVENT_TRACE_CONTROL_QUERY);
157 return HRESULT_FROM_WIN32(err);
158 }
159
Update(const wchar_t * session_name,EtwTraceProperties * properties)160 HRESULT EtwTraceController::Update(const wchar_t* session_name,
161 EtwTraceProperties* properties) {
162 DCHECK(properties != nullptr);
163 ULONG err = ::ControlTrace(NULL, session_name, properties->get(),
164 EVENT_TRACE_CONTROL_UPDATE);
165 return HRESULT_FROM_WIN32(err);
166 }
167
Stop(const wchar_t * session_name,EtwTraceProperties * properties)168 HRESULT EtwTraceController::Stop(const wchar_t* session_name,
169 EtwTraceProperties* properties) {
170 DCHECK(properties != nullptr);
171 ULONG err = ::ControlTrace(NULL, session_name, properties->get(),
172 EVENT_TRACE_CONTROL_STOP);
173 return HRESULT_FROM_WIN32(err);
174 }
175
Flush(const wchar_t * session_name,EtwTraceProperties * properties)176 HRESULT EtwTraceController::Flush(const wchar_t* session_name,
177 EtwTraceProperties* properties) {
178 DCHECK(properties != nullptr);
179 ULONG err = ::ControlTrace(NULL, session_name, properties->get(),
180 EVENT_TRACE_CONTROL_FLUSH);
181 return HRESULT_FROM_WIN32(err);
182 }
183
184 } // namespace win
185 } // namespace base
186