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 : category_(category), |
| 20 name_(name), |
| 21 type_(type), |
| 22 scope_(scope), |
| 23 id_(id), |
| 24 parent_scope_(nullptr), |
| 25 parent_id_(0) { |
| 26 Initialize(); |
| 27 } |
| 28 |
| 29 BlameContext::BlameContext(const char* category, |
| 30 const char* name, |
| 31 const char* type, |
| 32 const char* scope, |
| 33 int64_t id, |
| 34 const BlameContext& parent_context) |
| 35 : category_(category), |
| 36 name_(name), |
| 37 type_(type), |
| 38 scope_(scope), |
| 39 id_(id), |
| 40 parent_scope_(parent_context.scope()), |
| 41 parent_id_(parent_context.id()) { |
| 42 DCHECK(!std::strcmp(name_, parent_context.name())) |
| 43 << "Parent blame context must have the same name"; |
| 44 Initialize(); |
| 45 } |
| 46 |
| 47 void BlameContext::Enter() { |
| 48 int num_args = 0; |
| 49 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, |
| 50 category_group_enabled_, name_, scope_, id_, |
| 51 num_args, nullptr, nullptr, nullptr, nullptr, |
| 52 TRACE_EVENT_FLAG_HAS_ID); |
| 53 } |
| 54 |
| 55 void BlameContext::Leave() { |
| 56 int num_args = 0; |
| 57 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, |
| 58 category_group_enabled_, name_, scope_, id_, |
| 59 num_args, nullptr, nullptr, nullptr, nullptr, |
| 60 TRACE_EVENT_FLAG_HAS_ID); |
| 61 } |
| 62 |
| 63 void BlameContext::TakeSnapshot() { |
| 64 if (!*category_group_enabled_) |
| 65 return; |
| 66 scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); |
| 67 AsValueInto(snapshot.get()); |
| 68 static const char* kArgName = "snapshot"; |
| 69 int num_args = 1; |
| 70 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; |
| 71 scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { |
| 72 std::move(snapshot)}; |
| 73 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, |
| 74 category_group_enabled_, type_, scope_, id_, |
| 75 num_args, &kArgName, arg_types, nullptr, |
| 76 arg_values, TRACE_EVENT_FLAG_HAS_ID); |
| 77 } |
| 78 |
| 79 void BlameContext::OnTraceLogEnabled() { |
| 80 TakeSnapshot(); |
| 81 } |
| 82 |
| 83 void BlameContext::OnTraceLogDisabled() {} |
| 84 |
| 85 BlameContext::~BlameContext() { |
| 86 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 87 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, |
| 88 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 89 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); |
| 90 } |
| 91 |
| 92 void BlameContext::AsValueInto(trace_event::TracedValue* state) { |
| 93 if (!parent_id_) |
| 94 return; |
| 95 state->BeginDictionary("parent"); |
| 96 state->SetString("id_ref", StringPrintf("0x%" PRIx64, parent_id_)); |
| 97 state->SetString("scope", parent_scope_); |
| 98 state->EndDictionary(); |
| 99 } |
| 100 |
| 101 void BlameContext::Initialize() { |
| 102 category_group_enabled_ = |
| 103 TRACE_EVENT_API_GET_CATEGORY_GROUP_ENABLED(category_); |
| 104 TRACE_EVENT_API_ADD_TRACE_EVENT( |
| 105 TRACE_EVENT_PHASE_CREATE_OBJECT, category_group_enabled_, type_, scope_, |
| 106 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); |
| 107 trace_event::TraceLog::GetInstance()->AddEnabledStateObserver(this); |
| 108 } |
| 109 |
| 110 } // namespace trace_event |
| 111 } // namespace base |
OLD | NEW |