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