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
WritePointer(const void * value)81 void TracedValue::WritePointer(const void* value) && {
82 PERFETTO_DCHECK(checked_scope_.is_active());
83 annotation_->set_pointer_value(reinterpret_cast<uint64_t>(value));
84 }
85
WriteDictionary()86 TracedDictionary TracedValue::WriteDictionary() && {
87 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
88 // picked up later by the new TracedDictionary.
89 PERFETTO_DCHECK(checked_scope_.is_active());
90 checked_scope_.Reset();
91
92 PERFETTO_DCHECK(!annotation_->is_finalized());
93 return TracedDictionary(annotation_,
94 protos::pbzero::DebugAnnotation::kDictEntries,
95 event_context_, checked_scope_.parent_scope());
96 }
97
WriteArray()98 TracedArray TracedValue::WriteArray() && {
99 // Note: this passes |checked_scope_.is_active_| bit to the parent to be
100 // picked up later by the new TracedDictionary.
101 PERFETTO_DCHECK(checked_scope_.is_active());
102 checked_scope_.Reset();
103
104 PERFETTO_DCHECK(!annotation_->is_finalized());
105 return TracedArray(annotation_, event_context_,
106 checked_scope_.parent_scope());
107 }
108
WriteProtoInternal(const char * name)109 protozero::Message* TracedValue::WriteProtoInternal(const char* name) {
110 if (event_context_) {
111 annotation_->set_proto_type_name_iid(
112 internal::InternedDebugAnnotationValueTypeName::Get(event_context_,
113 name));
114 } else {
115 annotation_->set_proto_type_name(name);
116 }
117 return annotation_->template BeginNestedMessage<protozero::Message>(
118 protos::pbzero::DebugAnnotation::kProtoValueFieldNumber);
119 }
120
TracedArray(TracedValue annotation)121 TracedArray::TracedArray(TracedValue annotation)
122 : TracedArray(std::move(annotation).WriteArray()) {}
123
AppendItem()124 TracedValue TracedArray::AppendItem() {
125 PERFETTO_DCHECK(checked_scope_.is_active());
126 return TracedValue(annotation_->add_array_values(), event_context_,
127 &checked_scope_);
128 }
129
AppendDictionary()130 TracedDictionary TracedArray::AppendDictionary() {
131 PERFETTO_DCHECK(checked_scope_.is_active());
132 return AppendItem().WriteDictionary();
133 }
134
AppendArray()135 TracedArray TracedArray::AppendArray() {
136 PERFETTO_DCHECK(checked_scope_.is_active());
137 return AppendItem().WriteArray();
138 }
139
TracedDictionary(TracedValue annotation)140 TracedDictionary::TracedDictionary(TracedValue annotation)
141 : TracedDictionary(std::move(annotation).WriteDictionary()) {}
142
AddItem(StaticString key)143 TracedValue TracedDictionary::AddItem(StaticString key) {
144 PERFETTO_DCHECK(checked_scope_.is_active());
145 protos::pbzero::DebugAnnotation* item =
146 message_->BeginNestedMessage<protos::pbzero::DebugAnnotation>(field_id_);
147 item->set_name(key.value);
148 return TracedValue(item, event_context_, &checked_scope_);
149 }
150
AddItem(DynamicString key)151 TracedValue TracedDictionary::AddItem(DynamicString key) {
152 PERFETTO_DCHECK(checked_scope_.is_active());
153 protos::pbzero::DebugAnnotation* item =
154 message_->BeginNestedMessage<protos::pbzero::DebugAnnotation>(field_id_);
155 item->set_name(key.value);
156 return TracedValue(item, event_context_, &checked_scope_);
157 }
158
AddDictionary(StaticString key)159 TracedDictionary TracedDictionary::AddDictionary(StaticString key) {
160 PERFETTO_DCHECK(checked_scope_.is_active());
161 return AddItem(key).WriteDictionary();
162 }
163
AddDictionary(DynamicString key)164 TracedDictionary TracedDictionary::AddDictionary(DynamicString key) {
165 PERFETTO_DCHECK(checked_scope_.is_active());
166 return AddItem(key).WriteDictionary();
167 }
168
AddArray(StaticString key)169 TracedArray TracedDictionary::AddArray(StaticString key) {
170 PERFETTO_DCHECK(checked_scope_.is_active());
171 return AddItem(key).WriteArray();
172 }
173
AddArray(DynamicString key)174 TracedArray TracedDictionary::AddArray(DynamicString key) {
175 PERFETTO_DCHECK(checked_scope_.is_active());
176 return AddItem(key).WriteArray();
177 }
178
179 } // namespace perfetto
180