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