diff options
Diffstat (limited to 'src/trace_processor/util/protozero_to_text.cc')
-rw-r--r-- | src/trace_processor/util/protozero_to_text.cc | 122 |
1 files changed, 37 insertions, 85 deletions
diff --git a/src/trace_processor/util/protozero_to_text.cc b/src/trace_processor/util/protozero_to_text.cc index 43cf9b5f0..cc0f6592e 100644 --- a/src/trace_processor/util/protozero_to_text.cc +++ b/src/trace_processor/util/protozero_to_text.cc @@ -1,6 +1,5 @@ #include "src/trace_processor/util/protozero_to_text.h" -#include "perfetto/ext/base/string_utils.h" #include "perfetto/ext/base/string_view.h" #include "perfetto/protozero/proto_decoder.h" #include "perfetto/protozero/proto_utils.h" @@ -8,28 +7,12 @@ #include "src/trace_processor/util/descriptors.h" // This is the highest level that this protozero to text supports. -#include "src/trace_processor/importers/track_event.descriptor.h" +#include "src/trace_processor/importers/proto/track_event.descriptor.h" namespace perfetto { namespace trace_processor { -namespace protozero_to_text { - namespace { -std::string BytesToHexEncodedString(const std::string& bytes) { - // Each byte becomes four chars 'A' -> "\x41" + 1 for trailing null. - std::string value(4 * bytes.size() + 1, 'Z'); - for (size_t i = 0; i < bytes.size(); ++i) { - // snprintf prints 5 characters: '\x', then two hex digits, and finally a - // null byte. As we write left to right, we keep overwriting the null - // byte, except for the last call to snprintf. - snprintf(&(value[4 * i]), 5, "\\x%02hhx", bytes[i]); - } - // Trim trailing null. - value.resize(4 * bytes.size()); - return value; -} - // Recursively determine the size of all the string like things passed in the // parameter pack |rest|. size_t SizeOfStr() { @@ -70,7 +53,7 @@ void ConvertProtoTypeToFieldAndValueString(const FieldDescriptor& fd, const protozero::Field& field, const std::string& separator, const std::string& indent, - const DescriptorPool& pool, + DescriptorPool* pool, std::string* out) { using FieldDescriptorProto = protos::pbzero::FieldDescriptorProto; switch (fd.type()) { @@ -114,22 +97,15 @@ void ConvertProtoTypeToFieldAndValueString(const FieldDescriptor& fd, StrAppend(out, separator, indent, fd.name(), ": ", std::to_string(field.as_float())); return; - case FieldDescriptorProto::TYPE_STRING: { - auto s = base::QuoteAndEscapeControlCodes(field.as_std_string()); - StrAppend(out, separator, indent, fd.name(), ": ", s); - return; - } - case FieldDescriptorProto::TYPE_BYTES: { - std::string value = BytesToHexEncodedString(field.as_std_string()); - StrAppend(out, separator, indent, fd.name(), ": \"", value, "\""); + case FieldDescriptorProto::TYPE_STRING: + StrAppend(out, separator, indent, fd.name(), ": ", field.as_std_string()); return; - } case FieldDescriptorProto::TYPE_ENUM: { auto opt_enum_descriptor_idx = - pool.FindDescriptorIdx(fd.resolved_type_name()); + pool->FindDescriptorIdx(fd.resolved_type_name()); PERFETTO_DCHECK(opt_enum_descriptor_idx); auto opt_enum_string = - pool.descriptors()[*opt_enum_descriptor_idx].FindEnumString( + pool->descriptors()[*opt_enum_descriptor_idx].FindEnumString( field.as_int32()); PERFETTO_DCHECK(opt_enum_string); StrAppend(out, separator, indent, fd.name(), ": ", *opt_enum_string); @@ -142,6 +118,7 @@ void ConvertProtoTypeToFieldAndValueString(const FieldDescriptor& fd, fd.name().c_str(), fd.resolved_type_name().c_str(), fd.type()); } } + return; } void IncreaseIndents(std::string* out) { @@ -157,28 +134,26 @@ void DecreaseIndents(std::string* out) { // |type| and will use |pool| to look up the |type|. All output will be placed // in |output| and between fields |separator| will be placed. When called for // |indents| will be increased by 2 spaces to improve readability. -void ProtozeroToTextInternal(const std::string& type, - protozero::ConstBytes protobytes, - NewLinesMode new_lines_mode, - const DescriptorPool& pool, - std::string* indents, - std::string* output) { - auto opt_proto_descriptor_idx = pool.FindDescriptorIdx(type); +void ProtozeroToText(const std::string& type, + protozero::ConstBytes protobytes, + bool include_new_lines, + DescriptorPool* pool, + std::string* indents, + std::string* output) { + auto opt_proto_descriptor_idx = pool->FindDescriptorIdx(type); PERFETTO_DCHECK(opt_proto_descriptor_idx); - auto& proto_descriptor = pool.descriptors()[*opt_proto_descriptor_idx]; - bool include_new_lines = new_lines_mode == kIncludeNewLines; + auto& proto_descriptor = pool->descriptors()[*opt_proto_descriptor_idx]; protozero::ProtoDecoder decoder(protobytes.data, protobytes.size); for (auto field = decoder.ReadField(); field.valid(); field = decoder.ReadField()) { - auto opt_field_descriptor = proto_descriptor.FindFieldByTag(field.id()); - if (!opt_field_descriptor) { - StrAppend( - output, output->empty() ? "" : "\n", *indents, - "# Ignoring unknown field with id: ", std::to_string(field.id())); - continue; - } - const auto& field_descriptor = *opt_field_descriptor; + // Since this is only used in debugging or tests we should always have a + // valid compiled in binary descriptor. + auto opt_field_descriptor_idx = + proto_descriptor.FindFieldIdxByTag(field.id()); + PERFETTO_DCHECK(opt_field_descriptor_idx); + const auto& field_descriptor = + proto_descriptor.fields()[*opt_field_descriptor_idx]; if (field_descriptor.type() == protos::pbzero::FieldDescriptorProto::TYPE_MESSAGE) { @@ -190,9 +165,8 @@ void ProtozeroToTextInternal(const std::string& type, StrAppend(output, output->empty() ? "" : " ", field_descriptor.name(), ": {"); } - ProtozeroToTextInternal(field_descriptor.resolved_type_name(), - field.as_bytes(), new_lines_mode, pool, indents, - output); + ProtozeroToText(field_descriptor.resolved_type_name(), field.as_bytes(), + include_new_lines, pool, indents, output); if (include_new_lines) { DecreaseIndents(indents); StrAppend(output, "\n", *indents, "}"); @@ -209,36 +183,28 @@ void ProtozeroToTextInternal(const std::string& type, PERFETTO_DCHECK(decoder.bytes_left() == 0); } -} // namespace - -std::string ProtozeroToText(const DescriptorPool& pool, - const std::string& type, +std::string ProtozeroToText(const std::string& type, protozero::ConstBytes protobytes, - NewLinesMode new_lines_mode) { + bool include_new_lines) { std::string indent = ""; std::string final_result; - ProtozeroToTextInternal(type, protobytes, new_lines_mode, pool, &indent, - &final_result); - return final_result; -} - -std::string DebugTrackEventProtozeroToText(const std::string& type, - protozero::ConstBytes protobytes) { DescriptorPool pool; auto status = pool.AddFromFileDescriptorSet(kTrackEventDescriptor.data(), kTrackEventDescriptor.size()); PERFETTO_DCHECK(status.ok()); - return ProtozeroToText(pool, type, protobytes, kIncludeNewLines); + ProtozeroToText(type, protobytes, include_new_lines, &pool, &indent, + &final_result); + return final_result; } +} // namespace -std::string ShortDebugTrackEventProtozeroToText( - const std::string& type, - protozero::ConstBytes protobytes) { - DescriptorPool pool; - auto status = pool.AddFromFileDescriptorSet(kTrackEventDescriptor.data(), - kTrackEventDescriptor.size()); - PERFETTO_DCHECK(status.ok()); - return ProtozeroToText(pool, type, protobytes, kSkipNewLines); +std::string DebugProtozeroToText(const std::string& type, + protozero::ConstBytes protobytes) { + return ProtozeroToText(type, protobytes, /* include_new_lines = */ true); +} +std::string ShortDebugProtozeroToText(const std::string& type, + protozero::ConstBytes protobytes) { + return ProtozeroToText(type, protobytes, /* include_new_lines = */ false); } std::string ProtozeroEnumToText(const std::string& type, int32_t enum_value) { @@ -260,19 +226,5 @@ std::string ProtozeroEnumToText(const std::string& type, int32_t enum_value) { return *opt_enum_string; } -std::string ProtozeroToText(const DescriptorPool& pool, - const std::string& type, - const std::vector<uint8_t>& protobytes, - NewLinesMode new_lines_mode) { - return ProtozeroToText( - pool, type, protozero::ConstBytes{protobytes.data(), protobytes.size()}, - new_lines_mode); -} - -std::string BytesToHexEncodedStringForTesting(const std::string& s) { - return BytesToHexEncodedString(s); -} - -} // namespace protozero_to_text } // namespace trace_processor } // namespace perfetto |