| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "content/browser/tracing/etw_tracing_agent_win.h" | 5 #include "content/browser/tracing/etw_tracing_agent_win.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include <utility> |
| 10 |
| 9 #include "base/base64.h" | 11 #include "base/base64.h" |
| 10 #include "base/json/json_string_value_serializer.h" | 12 #include "base/json/json_string_value_serializer.h" |
| 11 #include "base/lazy_instance.h" | 13 #include "base/lazy_instance.h" |
| 12 #include "base/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
| 13 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 14 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
| 15 #include "base/threading/thread_task_runner_handle.h" | 17 #include "base/threading/thread_task_runner_handle.h" |
| 16 #include "base/time/time.h" | 18 #include "base/time/time.h" |
| 17 #include "base/trace_event/trace_event_impl.h" | 19 #include "base/trace_event/trace_event_impl.h" |
| 20 #include "base/values.h" |
| 18 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 19 | 22 |
| 20 namespace content { | 23 namespace content { |
| 21 | 24 |
| 22 namespace { | 25 namespace { |
| 23 | 26 |
| 24 const char kETWTracingAgentName[] = "etw"; | 27 const char kETWTracingAgentName[] = "etw"; |
| 25 const char kETWTraceLabel[] = "systemTraceEvents"; | 28 const char kETWTraceLabel[] = "systemTraceEvents"; |
| 26 | 29 |
| 27 const int kEtwBufferSizeInKBytes = 16; | 30 const int kEtwBufferSizeInKBytes = 16; |
| (...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // Add fields to the event. | 170 // Add fields to the event. |
| 168 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 171 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 169 value->Set("guid", new base::Value("ClockSync")); | 172 value->Set("guid", new base::Value("ClockSync")); |
| 170 value->Set("walltime", | 173 value->Set("walltime", |
| 171 new base::Value(base::StringPrintf( | 174 new base::Value(base::StringPrintf( |
| 172 "%08X%08X", walltime_in_us.HighPart, walltime_in_us.LowPart))); | 175 "%08X%08X", walltime_in_us.HighPart, walltime_in_us.LowPart))); |
| 173 value->Set("tick", new base::Value(base::StringPrintf( | 176 value->Set("tick", new base::Value(base::StringPrintf( |
| 174 "%08X%08X", now_in_us.HighPart, now_in_us.LowPart))); | 177 "%08X%08X", now_in_us.HighPart, now_in_us.LowPart))); |
| 175 | 178 |
| 176 // Append it to the events buffer. | 179 // Append it to the events buffer. |
| 177 events_->Append(value.release()); | 180 events_->Append(std::move(value)); |
| 178 } | 181 } |
| 179 | 182 |
| 180 void EtwTracingAgent::AppendEventToBuffer(EVENT_TRACE* event) { | 183 void EtwTracingAgent::AppendEventToBuffer(EVENT_TRACE* event) { |
| 181 using base::Value; | 184 using base::Value; |
| 182 | 185 |
| 183 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); | 186 std::unique_ptr<base::DictionaryValue> value(new base::DictionaryValue()); |
| 184 | 187 |
| 185 // Add header fields to the event. | 188 // Add header fields to the event. |
| 186 LARGE_INTEGER ts_us; | 189 LARGE_INTEGER ts_us; |
| 187 ts_us.QuadPart = event->Header.TimeStamp.QuadPart / 10; | 190 ts_us.QuadPart = event->Header.TimeStamp.QuadPart / 10; |
| 188 value->Set("ts", new base::Value(base::StringPrintf( | 191 value->Set("ts", new base::Value(base::StringPrintf( |
| 189 "%08X%08X", ts_us.HighPart, ts_us.LowPart))); | 192 "%08X%08X", ts_us.HighPart, ts_us.LowPart))); |
| 190 | 193 |
| 191 value->Set("guid", new base::Value(GuidToString(event->Header.Guid))); | 194 value->Set("guid", new base::Value(GuidToString(event->Header.Guid))); |
| 192 | 195 |
| 193 value->Set("op", new Value(event->Header.Class.Type)); | 196 value->Set("op", new Value(event->Header.Class.Type)); |
| 194 value->Set("ver", new Value(event->Header.Class.Version)); | 197 value->Set("ver", new Value(event->Header.Class.Version)); |
| 195 value->Set("pid", new Value(static_cast<int>(event->Header.ProcessId))); | 198 value->Set("pid", new Value(static_cast<int>(event->Header.ProcessId))); |
| 196 value->Set("tid", new Value(static_cast<int>(event->Header.ThreadId))); | 199 value->Set("tid", new Value(static_cast<int>(event->Header.ThreadId))); |
| 197 value->Set("cpu", new Value(event->BufferContext.ProcessorNumber)); | 200 value->Set("cpu", new Value(event->BufferContext.ProcessorNumber)); |
| 198 | 201 |
| 199 // Base64 encode the payload bytes. | 202 // Base64 encode the payload bytes. |
| 200 base::StringPiece buffer(static_cast<const char*>(event->MofData), | 203 base::StringPiece buffer(static_cast<const char*>(event->MofData), |
| 201 event->MofLength); | 204 event->MofLength); |
| 202 std::string payload; | 205 std::string payload; |
| 203 base::Base64Encode(buffer, &payload); | 206 base::Base64Encode(buffer, &payload); |
| 204 value->Set("payload", new base::Value(payload)); | 207 value->Set("payload", new base::Value(payload)); |
| 205 | 208 |
| 206 // Append it to the events buffer. | 209 // Append it to the events buffer. |
| 207 events_->Append(value.release()); | 210 events_->Append(std::move(value)); |
| 208 } | 211 } |
| 209 | 212 |
| 210 void EtwTracingAgent::TraceAndConsumeOnThread() { | 213 void EtwTracingAgent::TraceAndConsumeOnThread() { |
| 211 // Create the events buffer. | 214 // Create the events buffer. |
| 212 events_.reset(new base::ListValue()); | 215 events_.reset(new base::ListValue()); |
| 213 | 216 |
| 214 // Output a clock sync event. | 217 // Output a clock sync event. |
| 215 AddSyncEventToBuffer(); | 218 AddSyncEventToBuffer(); |
| 216 | 219 |
| 217 HRESULT hr = OpenRealtimeSession(KERNEL_LOGGER_NAME); | 220 HRESULT hr = OpenRealtimeSession(KERNEL_LOGGER_NAME); |
| 218 if (FAILED(hr)) | 221 if (FAILED(hr)) |
| 219 return; | 222 return; |
| 220 Consume(); | 223 Consume(); |
| 221 Close(); | 224 Close(); |
| 222 } | 225 } |
| 223 | 226 |
| 224 void EtwTracingAgent::FlushOnThread( | 227 void EtwTracingAgent::FlushOnThread( |
| 225 const StopAgentTracingCallback& callback) { | 228 const StopAgentTracingCallback& callback) { |
| 226 // Add the header information to the stream. | 229 // Add the header information to the stream. |
| 227 std::unique_ptr<base::DictionaryValue> header(new base::DictionaryValue()); | 230 std::unique_ptr<base::DictionaryValue> header(new base::DictionaryValue()); |
| 228 header->Set("name", new base::Value("ETW")); | 231 header->Set("name", new base::Value("ETW")); |
| 229 | 232 |
| 230 // Release and pass the events buffer. | 233 // Release and pass the events buffer. |
| 231 header->Set("content", events_.release()); | 234 header->Set("content", std::move(events_)); |
| 232 | 235 |
| 233 // Serialize the results as a JSon string. | 236 // Serialize the results as a JSon string. |
| 234 std::string output; | 237 std::string output; |
| 235 JSONStringValueSerializer serializer(&output); | 238 JSONStringValueSerializer serializer(&output); |
| 236 serializer.Serialize(*header.get()); | 239 serializer.Serialize(*header.get()); |
| 237 | 240 |
| 238 // Pass the result to the UI Thread. | 241 // Pass the result to the UI Thread. |
| 239 scoped_refptr<base::RefCountedString> result = | 242 scoped_refptr<base::RefCountedString> result = |
| 240 base::RefCountedString::TakeString(&output); | 243 base::RefCountedString::TakeString(&output); |
| 241 BrowserThread::PostTask( | 244 BrowserThread::PostTask( |
| 242 BrowserThread::UI, FROM_HERE, | 245 BrowserThread::UI, FROM_HERE, |
| 243 base::Bind(&EtwTracingAgent::OnStopSystemTracingDone, | 246 base::Bind(&EtwTracingAgent::OnStopSystemTracingDone, |
| 244 base::Unretained(this), | 247 base::Unretained(this), |
| 245 callback, | 248 callback, |
| 246 result)); | 249 result)); |
| 247 } | 250 } |
| 248 | 251 |
| 249 } // namespace content | 252 } // namespace content |
| OLD | NEW |