Index: base/debug/blame_context.cc |
diff --git a/base/debug/blame_context.cc b/base/debug/blame_context.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d45161b9b131a78443a1ceccc0bd516c7792444c |
--- /dev/null |
+++ b/base/debug/blame_context.cc |
@@ -0,0 +1,102 @@ |
+// Copyright 2016 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/debug/blame_context.h" |
+ |
+#include "base/strings/stringprintf.h" |
+#include "base/trace_event/trace_event_argument.h" |
+ |
+namespace base { |
+namespace debug { |
+ |
+BlameContext::BlameContext(const char* category, |
+ const char* name, |
+ const char* scope, |
+ int64_t id) |
+ : category_(category), |
+ name_(name), |
+ scope_(scope), |
+ id_(id), |
+ parent_scope_(nullptr), |
+ parent_id_(0) { |
+ Initialize(); |
+} |
+ |
+BlameContext::BlameContext(const char* category, |
+ const char* name, |
+ const char* scope, |
+ int64_t id, |
+ const BlameContext& parent_context) |
+ : category_(category), |
+ name_(name), |
+ scope_(scope), |
+ id_(id), |
+ parent_scope_(parent_context.scope()), |
+ parent_id_(parent_context.id()) { |
+ DCHECK(!std::strcmp(name_, parent_context.name())) |
+ << "Parent blame context must have the same name"; |
benjhayden
2016/03/10 23:18:43
I thought that a WebViewImpl context could be a pa
Sami
2016/03/14 16:59:14
Yeah, I've been going back and forth about this pa
|
+ Initialize(); |
+} |
+ |
+void BlameContext::Enter() { |
+ TRACE_EVENT_API_ADD_TRACE_EVENT( |
+ TRACE_EVENT_PHASE_ENTER_CONTEXT, category_group_enabled_, name_, scope_, |
+ id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
benjhayden
2016/03/10 23:18:43
Should the first nullptr should be a 0 for num_arg
Sami
2016/03/14 16:59:14
Hmm, no? The first nullptr is the array of arg_nam
benjhayden
2016/03/14 17:16:08
Oh, sorry! Codesearch showed me the trace-event.h
|
+} |
+ |
+void BlameContext::Leave() { |
+ TRACE_EVENT_API_ADD_TRACE_EVENT( |
+ TRACE_EVENT_PHASE_LEAVE_CONTEXT, category_group_enabled_, name_, scope_, |
+ id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
+} |
+ |
+void BlameContext::TakeSnapshot() { |
+ if (!*category_group_enabled_) |
+ return; |
+ scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); |
+ AsValueInto(snapshot.get()); |
+ const char* arg_name = "snapshot"; |
+ const int num_args = 1; |
+ unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; |
+ scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { |
+ std::move(snapshot)}; |
+ TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, |
+ category_group_enabled_, name_, scope_, id_, |
+ num_args, &arg_name, arg_types, nullptr, |
+ arg_values, TRACE_EVENT_FLAG_HAS_ID); |
+} |
+ |
+void BlameContext::OnTraceLogEnabled() { |
+ TakeSnapshot(); |
+} |
+ |
+void BlameContext::OnTraceLogDisabled() {} |
+ |
+BlameContext::~BlameContext() { |
+ TRACE_EVENT_API_ADD_TRACE_EVENT( |
+ TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, name_, scope_, |
+ id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
+ trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
+} |
+ |
+void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
+ if (!parent_id_) |
+ return; |
+ state->BeginDictionary("parent"); |
+ state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
+ state->SetString("scope", parent_scope_); |
+ state->EndDictionary(); |
+} |
+ |
+void BlameContext::Initialize() { |
+ category_group_enabled_ = |
+ TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); |
+ TRACE_EVENT_API_ADD_TRACE_EVENT( |
+ TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, name_, scope_, |
+ id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
+ trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
+} |
+ |
+} // namespace debug |
+} // namespace base |