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 BlameContext::~BlameContext() { | |
48 TRACE_EVENT_API_ADD_TRACE_EVENT( | |
49 TRACE_EVENT_PHASE_DELETE_OBJECT, category_group_enabled_, type_, scope_, | |
50 id_, 0, nullptr, nullptr, nullptr, nullptr, TRACE_EVENT_FLAG_HAS_ID); | |
51 trace_event::TraceLog::GetInstance()->RemoveEnabledStateObserver(this); | |
52 } | |
53 | |
54 void BlameContext::Enter() { | |
55 int num_args = 0; | |
56 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_ENTER_CONTEXT, | |
57 category_group_enabled_, name_, scope_, id_, | |
58 num_args, nullptr, nullptr, nullptr, nullptr, | |
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
or just 0 /* num_args */ if you prefer :)
Sami
2016/03/21 15:05:08
Done.
| |
59 TRACE_EVENT_FLAG_HAS_ID); | |
60 } | |
61 | |
62 void BlameContext::Leave() { | |
63 int num_args = 0; | |
64 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_LEAVE_CONTEXT, | |
65 category_group_enabled_, name_, scope_, id_, | |
66 num_args, nullptr, nullptr, nullptr, nullptr, | |
67 TRACE_EVENT_FLAG_HAS_ID); | |
68 } | |
69 | |
70 void BlameContext::TakeSnapshot() { | |
71 if (!*category_group_enabled_) | |
72 return; | |
73 scoped_ptr<trace_event::TracedValue> snapshot(new trace_event::TracedValue); | |
74 AsValueInto(snapshot.get()); | |
75 static const char* kArgName = "snapshot"; | |
76 int num_args = 1; | |
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
nit: kNumArgs?
Sami
2016/03/21 15:05:08
Done.
| |
77 unsigned char arg_types[1] = {TRACE_VALUE_TYPE_CONVERTABLE}; | |
78 scoped_ptr<trace_event::ConvertableToTraceFormat> arg_values[1] = { | |
79 std::move(snapshot)}; | |
80 TRACE_EVENT_API_ADD_TRACE_EVENT(TRACE_EVENT_PHASE_SNAPSHOT_OBJECT, | |
81 category_group_enabled_, type_, scope_, id_, | |
82 num_args, &kArgName, arg_types, nullptr, | |
83 arg_values, TRACE_EVENT_FLAG_HAS_ID); | |
84 } | |
85 | |
86 void BlameContext::OnTraceLogEnabled() { | |
87 TakeSnapshot(); | |
Primiano Tucci (use gerrit)
2016/03/21 14:26:29
What is the reason for this? Is it to make it so t
Sami
2016/03/21 15:05:08
Changed to require users to call Initialize() like
| |
88 } | |
89 | |
90 void BlameContext::OnTraceLogDisabled() {} | |
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 |