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 "perfetto/tracing/internal/track_event_interned_fields.h"
22 #include "protos/perfetto/trace/track_event/debug_annotation.pbzero.h"
23
24 namespace perfetto {
25
26 namespace internal {
27
CreateTracedValueFromProto(protos::pbzero::DebugAnnotation * annotation,EventContext * event_context)28 TracedValue CreateTracedValueFromProto(
29 protos::pbzero::DebugAnnotation* annotation,
30 EventContext* event_context) {
31 return TracedValue::CreateFromProto(annotation, event_context);
32 }
33
34 } // namespace internal
35
36 // static
CreateFromProto(protos::pbzero::DebugAnnotation * annotation,EventContext * event_context)37 TracedValue TracedValue::CreateFromProto(
38 protos::pbzero::DebugAnnotation* annotation,
39 EventContext* event_context) {
40 return TracedValue(annotation, event_context, nullptr);
41 }
42
43 TracedValue::TracedValue(TracedValue&&) = default;
44 TracedValue::~TracedValue() = default;
45
WriteInt64(int64_t value)46 void TracedValue::WriteInt64(int64_t value) && {
47 PERFETTO_DCHECK(checked_scope_.is_active());
48 annotation_->set_int_value(value);
49 }
50
WriteUInt64(uint64_t value)51 void TracedValue::WriteUInt64(uint64_t value) && {
52 PERFETTO_DCHECK(checked_scope_.is_active());
53 annotation_->set_uint_value(value);
54 }
55
WriteDouble(double value)56 void TracedValue::WriteDouble(double value) && {
57 PERFETTO_DCHECK(checked_scope_.is_active());
58 annotation_->set_double_value(value);
59 }
60
WriteBoolean(bool value)61 void TracedValue::WriteBoolean(bool value) && {
62 PERFETTO_DCHECK(checked_scope_.is_active());
63 annotation_->set_bool_value(value);
64 }
65
WriteString(const char * value)66 void TracedValue::WriteString(const char* value) && {
67 PERFETTO_DCHECK(checked_scope_.is_active());
68 annotation_->set_string_value(value);
69 }
70
WriteString(const char * value,size_t len)71 void TracedValue::WriteString(const char* value, size_t len) && {
72 PERFETTO_DCHECK(checked_scope_.is_active());
73 annotation_->set_string_value(value, len);
74 }
75
WriteString(const std::string & value)76 void TracedValue::WriteString(const std::string& value) && {
77 PERFETTO_DCHECK(checked_scope_.is_active());
78 annotation_->set_string_value(value);
79 }
80
WriteString(std::string_view value)81 void TracedValue::WriteString(std::string_view value) && {
82 PERFETTO_DCHECK(checked_scope_.is_active());
83 annotation_->set_string_value(value.data(), value.size());
84 }
85
WritePointer(const void * value)86 void TracedValue::WritePointer(const void* value) && {
87 PERFETTO_DCHECK(checked_scope_.is_active());
88 annotation_->set_pointer_value(reinterpret_cast<uint64_t>(value));
89 }
90
WriteDictionary()91 TracedDictionary TracedValue::WriteDictionary() && {
92 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
93 // picked up later by the new TracedDictionary.
94 PERFETTO_DCHECK(checked_scope_.is_active());
95 checked_scope_.Reset();
96
97 PERFETTO_DCHECK(!annotation_->is_finalized());
98 return TracedDictionary(annotation_,
99 protos::pbzero::DebugAnnotation::kDictEntries,
100 event_context_, checked_scope_.parent_scope());
101 }
102
WriteArray()103 TracedArray TracedValue::WriteArray() && {
104 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
105 // picked up later by the new TracedDictionary.
106 PERFETTO_DCHECK(checked_scope_.is_active());
107 checked_scope_.Reset();
108
109 PERFETTO_DCHECK(!annotation_->is_finalized());
110 return TracedArray(annotation_, event_context_,
111 checked_scope_.parent_scope());
112 }
113
WriteProtoInternal(const char * name)114 protozero::Message* TracedValue::WriteProtoInternal(const char* name) {
115 if (event_context_) {
116 annotation_->set_proto_type_name_iid(
117 internal::InternedDebugAnnotationValueTypeName::Get(event_context_,
118 name));
119 } else {
120 annotation_->set_proto_type_name(name);
121 }
122 return annotation_->template BeginNestedMessage<protozero::Message>(
123 protos::pbzero::DebugAnnotation::kProtoValueFieldNumber);
124 }
125
TracedArray(TracedValue annotation)126 TracedArray::TracedArray(TracedValue annotation)
127 : TracedArray(std::move(annotation).WriteArray()) {}
128
AppendItem()129 TracedValue TracedArray::AppendItem() {
130 PERFETTO_DCHECK(checked_scope_.is_active());
131 return TracedValue(annotation_->add_array_values(), event_context_,
132 &checked_scope_);
133 }
134
AppendDictionary()135 TracedDictionary TracedArray::AppendDictionary() {
136 PERFETTO_DCHECK(checked_scope_.is_active());
137 return AppendItem().WriteDictionary();
138 }
139
AppendArray()140 TracedArray TracedArray::AppendArray() {
141 PERFETTO_DCHECK(checked_scope_.is_active());
142 return AppendItem().WriteArray();
143 }
144
TracedDictionary(TracedValue annotation)145 TracedDictionary::TracedDictionary(TracedValue annotation)
146 : TracedDictionary(std::move(annotation).WriteDictionary()) {}
147
AddItem(StaticString key)148 TracedValue TracedDictionary::AddItem(StaticString key) {
149 PERFETTO_DCHECK(checked_scope_.is_active());
150 protos::pbzero::DebugAnnotation* item =
151 message_->BeginNestedMessage<protos::pbzero::DebugAnnotation>(field_id_);
152 item->set_name(key.value);
153 return TracedValue(item, event_context_, &checked_scope_);
154 }
155
AddItem(DynamicString key)156 TracedValue TracedDictionary::AddItem(DynamicString key) {
157 PERFETTO_DCHECK(checked_scope_.is_active());
158 protos::pbzero::DebugAnnotation* item =
159 message_->BeginNestedMessage<protos::pbzero::DebugAnnotation>(field_id_);
160 item->set_name(key.value);
161 return TracedValue(item, event_context_, &checked_scope_);
162 }
163
AddDictionary(StaticString key)164 TracedDictionary TracedDictionary::AddDictionary(StaticString key) {
165 PERFETTO_DCHECK(checked_scope_.is_active());
166 return AddItem(key).WriteDictionary();
167 }
168
AddDictionary(DynamicString key)169 TracedDictionary TracedDictionary::AddDictionary(DynamicString key) {
170 PERFETTO_DCHECK(checked_scope_.is_active());
171 return AddItem(key).WriteDictionary();
172 }
173
AddArray(StaticString key)174 TracedArray TracedDictionary::AddArray(StaticString key) {
175 PERFETTO_DCHECK(checked_scope_.is_active());
176 return AddItem(key).WriteArray();
177 }
178
AddArray(DynamicString key)179 TracedArray TracedDictionary::AddArray(DynamicString key) {
180 PERFETTO_DCHECK(checked_scope_.is_active());
181 return AddItem(key).WriteArray();
182 }
183
184 } // namespace perfetto
185