OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/trace_event/blame_context.h" |
| 6 |
| 7 #include "base/strings/stringprintf.h" |
| 8 #include "base/trace_event/trace_event.h" |
| 9 #include "base/trace_event/trace_event_argument.h" |
| 10 |
| 11 namespace base { |
| 12 namespace trace_event { |
| 13 |
| 14 BlameContext::BlameContext(const char* category, |
| 15 const char* name, |
| 16 const char* type, |
| 17 const char* scope, |
| 18 int64_t id, |
| 19 const BlameContext* parent_context) |
| 20 : category_(category), |
| 21 name_(name), |
| 22 type_(type), |
| 23 scope_(scope), |
| 24 id_(id), |
| 25 parent_scope_(parent_context ? parent_context->scope() : nullptr), |
| 26 parent_id_(parent_context ? parent_context->id() : 0), |
| 27 category_group_enabled_(nullptr) { |
| 28 DCHECK(!parent_context || !std::strcmp(name_, parent_context->name())) |
| 29 << "Parent blame context must have the same name"; |
| 30 } |
| 31 |
| 32 BlameContext::~BlameContext() { |
| 33 DCHECK(WasInitialized()); |
| 34 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 35 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, |
| 36 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 37 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| 38 } |
| 39 |
| 40 void BlameContext::Enter() { |
| 41 DCHECK(WasInitialized()); |
| 42 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, |
| 43 category_group_enabled_, name_, scope_, id_, |
| 44 0 /* num_args */, nullptr, nullptr, nullptr, |
| 45 nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 46 } |
| 47 |
| 48 void BlameContext::Leave() { |
| 49 DCHECK(WasInitialized()); |
| 50 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, |
| 51 category_group_enabled_, name_, scope_, id_, |
| 52 0 /* num_args */, nullptr, nullptr, nullptr, |
| 53 nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 54 } |
| 55 |
| 56 void BlameContext::TakeSnapshot() { |
| 57 DCHECK(WasInitialized()); |
| 58 if (!*category_group_enabled_) |
| 59 return; |
| 60 scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); |
| 61 AsValueInto(snapshot.get()); |
| 62 static const char* kArgName = "snapshot"; |
| 63 const int kNumArgs = 1; |
| 64 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; |
| 65 scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { |
| 66 std::move(snapshot)}; |
| 67 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, |
| 68 category_group_enabled_, type_, scope_, id_, |
| 69 kNumArgs, &kArgName, arg_types, nullptr, |
| 70 arg_values, TRACE_EVENT_FLAG_HAS_ID); |
| 71 } |
| 72 |
| 73 void BlameContext::OnTraceLogEnabled() { |
| 74 DCHECK(WasInitialized()); |
| 75 TakeSnapshot(); |
| 76 } |
| 77 |
| 78 void BlameContext::OnTraceLogDisabled() {} |
| 79 |
| 80 void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
| 81 DCHECK(WasInitialized()); |
| 82 if (!parent_id_) |
| 83 return; |
| 84 state->BeginDictionary("parent"); |
| 85 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
| 86 state->SetString("scope", parent_scope_); |
| 87 state->EndDictionary(); |
| 88 } |
| 89 |
| 90 void BlameContext::Initialize() { |
| 91 category_group_enabled_ = |
| 92 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); |
| 93 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 94 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, |
| 95 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 96 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 97 TakeSnapshot(); |
| 98 } |
| 99 |
| 100 bool BlameContext::WasInitialized() const { |
| 101 return category_group_enabled_ != nullptr; |
| 102 } |
| 103 |
| 104 } // namespace trace_event |
| 105 } // namespace base |
OLD | NEW |