1 /*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "perfetto/tracing/traced_value.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/tracing/debug_annotation.h"
21 #include "protos/perfetto/trace/track_event/debug_annotation.pbzero.h"
22
23 namespace perfetto {
24
25 namespace internal {
26
CreateTracedValueFromProto(protos::pbzero::DebugAnnotation * context)27 TracedValue CreateTracedValueFromProto(
28 protos::pbzero::DebugAnnotation* context) {
29 return TracedValue::CreateFromProto(context);
30 }
31
32 } // namespace internal
33
34 // static
CreateFromProto(protos::pbzero::DebugAnnotation * context)35 TracedValue TracedValue::CreateFromProto(
36 protos::pbzero::DebugAnnotation* context) {
37 return TracedValue(context, nullptr);
38 }
39
WriteInt64(int64_t value)40 void TracedValue::WriteInt64(int64_t value) && {
41 PERFETTO_DCHECK(checked_scope_.is_active());
42 context_->set_int_value(value);
43 }
44
WriteUInt64(uint64_t value)45 void TracedValue::WriteUInt64(uint64_t value) && {
46 PERFETTO_DCHECK(checked_scope_.is_active());
47 context_->set_uint_value(value);
48 }
49
WriteDouble(double value)50 void TracedValue::WriteDouble(double value) && {
51 PERFETTO_DCHECK(checked_scope_.is_active());
52 context_->set_double_value(value);
53 }
54
WriteBoolean(bool value)55 void TracedValue::WriteBoolean(bool value) && {
56 PERFETTO_DCHECK(checked_scope_.is_active());
57 context_->set_bool_value(value);
58 }
59
WriteString(const char * value)60 void TracedValue::WriteString(const char* value) && {
61 PERFETTO_DCHECK(checked_scope_.is_active());
62 context_->set_string_value(value);
63 }
64
WriteString(const char * value,size_t len)65 void TracedValue::WriteString(const char* value, size_t len) && {
66 PERFETTO_DCHECK(checked_scope_.is_active());
67 context_->set_string_value(value, len);
68 }
69
WriteString(const std::string & value)70 void TracedValue::WriteString(const std::string& value) && {
71 PERFETTO_DCHECK(checked_scope_.is_active());
72 context_->set_string_value(value);
73 }
74
WritePointer(const void * value)75 void TracedValue::WritePointer(const void* value) && {
76 PERFETTO_DCHECK(checked_scope_.is_active());
77 context_->set_pointer_value(reinterpret_cast<uint64_t>(value));
78 }
79
WriteDictionary()80 TracedDictionary TracedValue::WriteDictionary() && {
81 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
82 // picked up later by the new TracedDictionary.
83 PERFETTO_DCHECK(checked_scope_.is_active());
84 checked_scope_.Reset();
85
86 PERFETTO_DCHECK(!context_->is_finalized());
87 return TracedDictionary(context_, checked_scope_.parent_scope());
88 }
89
WriteArray()90 TracedArray TracedValue::WriteArray() && {
91 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
92 // picked up later by the new TracedDictionary.
93 PERFETTO_DCHECK(checked_scope_.is_active());
94 checked_scope_.Reset();
95
96 PERFETTO_DCHECK(!context_->is_finalized());
97 return TracedArray(context_, checked_scope_.parent_scope());
98 }
99
AppendItem()100 TracedValue TracedArray::AppendItem() {
101 PERFETTO_DCHECK(checked_scope_.is_active());
102 return TracedValue(context_->add_array_values(), &checked_scope_);
103 }
104
AppendDictionary()105 TracedDictionary TracedArray::AppendDictionary() {
106 PERFETTO_DCHECK(checked_scope_.is_active());
107 return AppendItem().WriteDictionary();
108 }
109
AppendArray()110 TracedArray TracedArray::AppendArray() {
111 PERFETTO_DCHECK(checked_scope_.is_active());
112 return AppendItem().WriteArray();
113 }
114
AddItem(StaticString key)115 TracedValue TracedDictionary::AddItem(StaticString key) {
116 PERFETTO_DCHECK(checked_scope_.is_active());
117 protos::pbzero::DebugAnnotation* item = context_->add_dict_entries();
118 item->set_name(key.value);
119 return TracedValue(item, &checked_scope_);
120 }
121
AddItem(DynamicString key)122 TracedValue TracedDictionary::AddItem(DynamicString key) {
123 PERFETTO_DCHECK(checked_scope_.is_active());
124 protos::pbzero::DebugAnnotation* item = context_->add_dict_entries();
125 item->set_name(key.value);
126 return TracedValue(item, &checked_scope_);
127 }
128
AddDictionary(StaticString key)129 TracedDictionary TracedDictionary::AddDictionary(StaticString key) {
130 PERFETTO_DCHECK(checked_scope_.is_active());
131 return AddItem(key).WriteDictionary();
132 }
133
AddDictionary(DynamicString key)134 TracedDictionary TracedDictionary::AddDictionary(DynamicString key) {
135 PERFETTO_DCHECK(checked_scope_.is_active());
136 return AddItem(key).WriteDictionary();
137 }
138
AddArray(StaticString key)139 TracedArray TracedDictionary::AddArray(StaticString key) {
140 PERFETTO_DCHECK(checked_scope_.is_active());
141 return AddItem(key).WriteArray();
142 }
143
AddArray(DynamicString key)144 TracedArray TracedDictionary::AddArray(DynamicString key) {
145 PERFETTO_DCHECK(checked_scope_.is_active());
146 return AddItem(key).WriteArray();
147 }
148
149 } // namespace perfetto
150