• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 "tools/trace_to_text/trace_to_text.h"
18 
19 #include <google/protobuf/compiler/importer.h>
20 #include <google/protobuf/dynamic_message.h>
21 #include <google/protobuf/io/zero_copy_stream_impl.h>
22 #include <google/protobuf/text_format.h>
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/scoped_file.h"
26 #include "tools/trace_to_text/proto_full_utils.h"
27 #include "tools/trace_to_text/utils.h"
28 
29 #include "protos/perfetto/trace/trace.pbzero.h"
30 #include "protos/perfetto/trace/trace_packet.pbzero.h"
31 
32 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
33 #include <zlib.h>
34 #endif
35 
36 namespace perfetto {
37 namespace trace_to_text {
38 
39 namespace {
40 using google::protobuf::Descriptor;
41 using google::protobuf::DynamicMessageFactory;
42 using google::protobuf::FieldDescriptor;
43 using google::protobuf::FileDescriptor;
44 using google::protobuf::Message;
45 using google::protobuf::Reflection;
46 using google::protobuf::TextFormat;
47 using google::protobuf::compiler::DiskSourceTree;
48 using google::protobuf::compiler::Importer;
49 using google::protobuf::io::OstreamOutputStream;
50 using google::protobuf::io::ZeroCopyOutputStream;
51 
WriteToZeroCopyOutput(ZeroCopyOutputStream * output,const char * str,size_t length)52 inline void WriteToZeroCopyOutput(ZeroCopyOutputStream* output,
53                                   const char* str,
54                                   size_t length) {
55   if (length == 0)
56     return;
57 
58   void* data;
59   int size = 0;
60   size_t bytes_to_copy = 0;
61   while (length) {
62     output->Next(&data, &size);
63     bytes_to_copy = std::min(length, static_cast<size_t>(size));
64     memcpy(data, str, bytes_to_copy);
65     length -= bytes_to_copy;
66     str += bytes_to_copy;
67   }
68   output->BackUp(size - static_cast<int>(bytes_to_copy));
69 }
70 
71 constexpr char kCompressedPacketsPrefix[] = "compressed_packets {\n";
72 constexpr char kCompressedPacketsSuffix[] = "}\n";
73 
74 constexpr char kIndentedPacketPrefix[] = "  packet {\n";
75 constexpr char kIndentedPacketSuffix[] = "  }\n";
76 
77 constexpr char kPacketPrefix[] = "packet {\n";
78 constexpr char kPacketSuffix[] = "}\n";
79 
PrintCompressedPackets(const std::string & packets,Message * compressed_msg_scratch,ZeroCopyOutputStream * output)80 void PrintCompressedPackets(const std::string& packets,
81                             Message* compressed_msg_scratch,
82                             ZeroCopyOutputStream* output) {
83 #if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
84   uint8_t out[4096];
85   std::vector<uint8_t> data;
86 
87   z_stream stream{};
88   stream.next_in =
89       const_cast<uint8_t*>(reinterpret_cast<const uint8_t*>(packets.data()));
90   stream.avail_in = static_cast<unsigned int>(packets.length());
91 
92   if (inflateInit(&stream) != Z_OK) {
93     PERFETTO_ELOG("Error when initiliazing zlib to decompress packets");
94     return;
95   }
96 
97   int ret;
98   do {
99     stream.next_out = out;
100     stream.avail_out = sizeof(out);
101     ret = inflate(&stream, Z_NO_FLUSH);
102     if (ret != Z_STREAM_END && ret != Z_OK) {
103       PERFETTO_ELOG("Error when decompressing packets");
104       return;
105     }
106     data.insert(data.end(), out, out + (sizeof(out) - stream.avail_out));
107   } while (ret != Z_STREAM_END);
108   inflateEnd(&stream);
109 
110   protos::pbzero::Trace::Decoder decoder(data.data(), data.size());
111   WriteToZeroCopyOutput(output, kCompressedPacketsPrefix,
112                         sizeof(kCompressedPacketsPrefix) - 1);
113   TextFormat::Printer printer;
114   printer.SetInitialIndentLevel(2);
115   for (auto it = decoder.packet(); it; ++it) {
116     protozero::ConstBytes cb = *it;
117     compressed_msg_scratch->ParseFromArray(cb.data, static_cast<int>(cb.size));
118     WriteToZeroCopyOutput(output, kIndentedPacketPrefix,
119                           sizeof(kIndentedPacketPrefix) - 1);
120     printer.Print(*compressed_msg_scratch, output);
121     WriteToZeroCopyOutput(output, kIndentedPacketSuffix,
122                           sizeof(kIndentedPacketSuffix) - 1);
123   }
124   WriteToZeroCopyOutput(output, kCompressedPacketsSuffix,
125                         sizeof(kCompressedPacketsSuffix) - 1);
126 #else
127   base::ignore_result(packets);
128   base::ignore_result(compressed_msg_scratch);
129   base::ignore_result(kIndentedPacketPrefix);
130   base::ignore_result(kIndentedPacketSuffix);
131   WriteToZeroCopyOutput(output, kCompressedPacketsPrefix,
132                         sizeof(kCompressedPacketsPrefix) - 1);
133   static const char kErrMsg[] =
134       "Cannot decode compressed packets. zlib not enabled in the build config";
135   WriteToZeroCopyOutput(output, kErrMsg, sizeof(kErrMsg) - 1);
136   WriteToZeroCopyOutput(output, kCompressedPacketsSuffix,
137                         sizeof(kCompressedPacketsSuffix) - 1);
138   static bool log_once = [] {
139     PERFETTO_ELOG("%s", kErrMsg);
140     return true;
141   }();
142   base::ignore_result(log_once);
143 #endif  // PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
144 }
145 
146 }  // namespace
147 
TraceToText(std::istream * input,std::ostream * output)148 int TraceToText(std::istream* input, std::ostream* output) {
149   const std::string proto_path = "protos/perfetto/trace/trace_packet.proto";
150 
151   if (!base::OpenFile(proto_path, O_RDONLY)) {
152     PERFETTO_ELOG("Cannot open %s.", proto_path.c_str());
153     PERFETTO_ELOG(
154         "Text mode only works from the perfetto directory. Googlers, see "
155         "b/131425913");
156     return 1;
157   }
158 
159   DiskSourceTree dst;
160   dst.MapPath("", "");
161   MultiFileErrorCollectorImpl mfe;
162   Importer importer(&dst, &mfe);
163   const FileDescriptor* parsed_file =
164       importer.Import("protos/perfetto/trace/trace_packet.proto");
165 
166   DynamicMessageFactory dmf;
167   const Descriptor* trace_descriptor = parsed_file->message_type(0);
168   const Message* root = dmf.GetPrototype(trace_descriptor);
169   OstreamOutputStream zero_copy_output(output);
170   OstreamOutputStream* zero_copy_output_ptr = &zero_copy_output;
171   Message* msg = root->New();
172 
173   constexpr uint32_t kCompressedPacketFieldDescriptor = 50;
174   const Reflection* reflect = msg->GetReflection();
175   const FieldDescriptor* compressed_desc =
176       trace_descriptor->FindFieldByNumber(kCompressedPacketFieldDescriptor);
177   Message* compressed_msg_scratch = root->New();
178   std::string compressed_packet_scratch;
179 
180   TextFormat::Printer printer;
181   printer.SetInitialIndentLevel(1);
182   ForEachPacketBlobInTrace(
183       input, [msg, reflect, compressed_desc, zero_copy_output_ptr,
184               &compressed_packet_scratch, compressed_msg_scratch,
185               &printer](std::unique_ptr<char[]> buf, size_t size) {
186         if (!msg->ParseFromArray(buf.get(), static_cast<int>(size))) {
187           PERFETTO_ELOG("Skipping invalid packet");
188           return;
189         }
190         if (reflect->HasField(*msg, compressed_desc)) {
191           const auto& compressed_packets = reflect->GetStringReference(
192               *msg, compressed_desc, &compressed_packet_scratch);
193           PrintCompressedPackets(compressed_packets, compressed_msg_scratch,
194                                  zero_copy_output_ptr);
195         } else {
196           WriteToZeroCopyOutput(zero_copy_output_ptr, kPacketPrefix,
197                                 sizeof(kPacketPrefix) - 1);
198           printer.Print(*msg, zero_copy_output_ptr);
199           WriteToZeroCopyOutput(zero_copy_output_ptr, kPacketSuffix,
200                                 sizeof(kPacketSuffix) - 1);
201         }
202       });
203   return 0;
204 }
205 
206 }  // namespace trace_to_text
207 }  // namespace perfetto
208