| 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_system_event_consumer_win.h" | 5 #include "content/browser/tracing/etw_system_event_consumer_win.h" |
| 6 | 6 |
| 7 #include "base/base64.h" | 7 #include "base/base64.h" |
| 8 #include "base/json/json_string_value_serializer.h" | 8 #include "base/json/json_string_value_serializer.h" |
| 9 #include "base/lazy_instance.h" | 9 #include "base/lazy_instance.h" |
| 10 #include "base/memory/singleton.h" | 10 #include "base/memory/singleton.h" |
| 11 #include "base/strings/stringprintf.h" | 11 #include "base/strings/stringprintf.h" |
| 12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
| 13 #include "base/trace_event/trace_event_impl.h" | 13 #include "base/trace_event/trace_event_impl.h" |
| 14 #include "content/public/browser/browser_thread.h" | 14 #include "content/public/browser/browser_thread.h" |
| 15 | 15 |
| 16 namespace content { | 16 namespace content { |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 const char kETWTracingAgentName[] = "etw"; |
| 21 const char kETWTraceLabel[] = "systemTraceEvents"; |
| 22 |
| 20 const int kEtwBufferSizeInKBytes = 16; | 23 const int kEtwBufferSizeInKBytes = 16; |
| 21 const int kEtwBufferFlushTimeoutInSeconds = 1; | 24 const int kEtwBufferFlushTimeoutInSeconds = 1; |
| 22 | 25 |
| 23 std::string GuidToString(const GUID& guid) { | 26 std::string GuidToString(const GUID& guid) { |
| 24 return base::StringPrintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", | 27 return base::StringPrintf("%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X", |
| 25 guid.Data1, guid.Data2, guid.Data3, | 28 guid.Data1, guid.Data2, guid.Data3, |
| 26 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], | 29 guid.Data4[0], guid.Data4[1], guid.Data4[2], guid.Data4[3], |
| 27 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); | 30 guid.Data4[4], guid.Data4[5], guid.Data4[6], guid.Data4[7]); |
| 28 } | 31 } |
| 29 | 32 |
| 30 } // namespace | 33 } // namespace |
| 31 | 34 |
| 32 EtwSystemEventConsumer::EtwSystemEventConsumer() | 35 EtwSystemEventConsumer::EtwSystemEventConsumer() |
| 33 : thread_("EtwConsumerThread") { | 36 : thread_("EtwConsumerThread") { |
| 34 } | 37 } |
| 35 | 38 |
| 36 EtwSystemEventConsumer::~EtwSystemEventConsumer() { | 39 EtwSystemEventConsumer::~EtwSystemEventConsumer() { |
| 37 } | 40 } |
| 38 | 41 |
| 39 bool EtwSystemEventConsumer::StartSystemTracing() { | 42 std::string EtwSystemEventConsumer::GetTracingAgentName() { |
| 43 return kETWTracingAgentName; |
| 44 } |
| 40 | 45 |
| 46 std::string EtwSystemEventConsumer::GetTraceEventLabel() { |
| 47 return kETWTraceLabel; |
| 48 } |
| 49 |
| 50 bool EtwSystemEventConsumer::StartAgentTracing( |
| 51 const base::trace_event::TraceConfig& trace_config) { |
| 41 // Activate kernel tracing. | 52 // Activate kernel tracing. |
| 42 if (!StartKernelSessionTracing()) | 53 if (!StartKernelSessionTracing()) |
| 43 return false; | 54 return false; |
| 44 | 55 |
| 45 // Start the consumer thread and start consuming events. | 56 // Start the consumer thread and start consuming events. |
| 46 thread_.Start(); | 57 thread_.Start(); |
| 47 thread_.message_loop()->PostTask( | 58 thread_.message_loop()->PostTask( |
| 48 FROM_HERE, | 59 FROM_HERE, |
| 49 base::Bind(&EtwSystemEventConsumer::TraceAndConsumeOnThread, | 60 base::Bind(&EtwSystemEventConsumer::TraceAndConsumeOnThread, |
| 50 base::Unretained(this))); | 61 base::Unretained(this))); |
| 51 | 62 |
| 52 return true; | 63 return true; |
| 53 } | 64 } |
| 54 | 65 |
| 55 void EtwSystemEventConsumer::StopSystemTracing(const OutputCallback& callback) { | 66 void EtwSystemEventConsumer::StopAgentTracing( |
| 67 const StopAgentTracingCallback& callback) { |
| 56 // Deactivate kernel tracing. | 68 // Deactivate kernel tracing. |
| 57 if (!StopKernelSessionTracing()) { | 69 if (!StopKernelSessionTracing()) { |
| 58 LOG(FATAL) << "Could not stop system tracing."; | 70 LOG(FATAL) << "Could not stop system tracing."; |
| 59 } | 71 } |
| 60 | 72 |
| 61 // Stop consuming and flush events. | 73 // Stop consuming and flush events. |
| 62 OutputCallback on_stop_system_tracing_done_callback = | |
| 63 base::Bind(&EtwSystemEventConsumer::OnStopSystemTracingDone, | |
| 64 base::Unretained(this), | |
| 65 callback); | |
| 66 thread_.message_loop()->PostTask(FROM_HERE, | 74 thread_.message_loop()->PostTask(FROM_HERE, |
| 67 base::Bind(&EtwSystemEventConsumer::FlushOnThread, | 75 base::Bind(&EtwSystemEventConsumer::FlushOnThread, |
| 68 base::Unretained(this), on_stop_system_tracing_done_callback)); | 76 base::Unretained(this), |
| 77 callback)); |
| 69 } | 78 } |
| 70 | 79 |
| 71 void EtwSystemEventConsumer::OnStopSystemTracingDone( | 80 void EtwSystemEventConsumer::OnStopSystemTracingDone( |
| 72 const OutputCallback& callback, | 81 const StopAgentTracingCallback& callback, |
| 73 const scoped_refptr<base::RefCountedString>& result) { | 82 const scoped_refptr<base::RefCountedString>& result) { |
| 74 | 83 |
| 75 // Stop the consumer thread. | 84 // Stop the consumer thread. |
| 76 thread_.Stop(); | 85 thread_.Stop(); |
| 77 | 86 |
| 78 // Pass the serialized events. | 87 // Pass the serialized events. |
| 79 callback.Run(result); | 88 callback.Run(GetTracingAgentName(), GetTraceEventLabel(), result); |
| 80 } | 89 } |
| 81 | 90 |
| 82 bool EtwSystemEventConsumer::StartKernelSessionTracing() { | 91 bool EtwSystemEventConsumer::StartKernelSessionTracing() { |
| 83 // Enabled flags (tracing facilities). | 92 // Enabled flags (tracing facilities). |
| 84 uint32 enabled_flags = EVENT_TRACE_FLAG_IMAGE_LOAD | | 93 uint32 enabled_flags = EVENT_TRACE_FLAG_IMAGE_LOAD | |
| 85 EVENT_TRACE_FLAG_PROCESS | | 94 EVENT_TRACE_FLAG_PROCESS | |
| 86 EVENT_TRACE_FLAG_THREAD | | 95 EVENT_TRACE_FLAG_THREAD | |
| 87 EVENT_TRACE_FLAG_CSWITCH; | 96 EVENT_TRACE_FLAG_CSWITCH; |
| 88 | 97 |
| 89 EVENT_TRACE_PROPERTIES& p = *properties_.get(); | 98 EVENT_TRACE_PROPERTIES& p = *properties_.get(); |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 202 // Output a clock sync event. | 211 // Output a clock sync event. |
| 203 AddSyncEventToBuffer(); | 212 AddSyncEventToBuffer(); |
| 204 | 213 |
| 205 HRESULT hr = OpenRealtimeSession(KERNEL_LOGGER_NAME); | 214 HRESULT hr = OpenRealtimeSession(KERNEL_LOGGER_NAME); |
| 206 if (FAILED(hr)) | 215 if (FAILED(hr)) |
| 207 return; | 216 return; |
| 208 Consume(); | 217 Consume(); |
| 209 Close(); | 218 Close(); |
| 210 } | 219 } |
| 211 | 220 |
| 212 void EtwSystemEventConsumer::FlushOnThread(const OutputCallback& callback) { | 221 void EtwSystemEventConsumer::FlushOnThread( |
| 222 const StopAgentTracingCallback& callback) { |
| 213 // Add the header information to the stream. | 223 // Add the header information to the stream. |
| 214 scoped_ptr<base::DictionaryValue> header(new base::DictionaryValue()); | 224 scoped_ptr<base::DictionaryValue> header(new base::DictionaryValue()); |
| 215 header->Set("name", new base::StringValue("ETW")); | 225 header->Set("name", new base::StringValue("ETW")); |
| 216 | 226 |
| 217 // Release and pass the events buffer. | 227 // Release and pass the events buffer. |
| 218 header->Set("content", events_.release()); | 228 header->Set("content", events_.release()); |
| 219 | 229 |
| 220 // Serialize the results as a JSon string. | 230 // Serialize the results as a JSon string. |
| 221 std::string output; | 231 std::string output; |
| 222 JSONStringValueSerializer serializer(&output); | 232 JSONStringValueSerializer serializer(&output); |
| 223 serializer.Serialize(*header.get()); | 233 serializer.Serialize(*header.get()); |
| 224 | 234 |
| 225 // Pass the result to the UI Thread. | 235 // Pass the result to the UI Thread. |
| 226 scoped_refptr<base::RefCountedString> result = | 236 scoped_refptr<base::RefCountedString> result = |
| 227 base::RefCountedString::TakeString(&output); | 237 base::RefCountedString::TakeString(&output); |
| 228 BrowserThread::PostTask(BrowserThread::UI, FROM_HERE, | 238 BrowserThread::PostTask( |
| 229 base::Bind(callback, result)); | 239 BrowserThread::UI, FROM_HERE, |
| 240 base::Bind(&EtwSystemEventConsumer::OnStopSystemTracingDone, |
| 241 base::Unretained(this), |
| 242 callback, |
| 243 result)); |
| 230 } | 244 } |
| 231 | 245 |
| 232 } // namespace content | 246 } // namespace content |
| OLD | NEW |