1 /*
2 * Copyright (C) 2019 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/trace_processor/read_trace.h"
18 #include <cstdint>
19 #include <functional>
20 #include <memory>
21 #include <utility>
22 #include <vector>
23
24 #include "perfetto/base/logging.h"
25 #include "perfetto/base/status.h"
26 #include "perfetto/protozero/proto_utils.h"
27 #include "perfetto/trace_processor/trace_blob_view.h"
28 #include "perfetto/trace_processor/trace_processor.h"
29 #include "src/trace_processor/importers/archive/gzip_trace_parser.h"
30 #include "src/trace_processor/importers/common/chunked_trace_reader.h"
31 #include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
32 #include "src/trace_processor/read_trace_internal.h"
33 #include "src/trace_processor/util/gzip_utils.h"
34 #include "src/trace_processor/util/status_macros.h"
35 #include "src/trace_processor/util/trace_type.h"
36
37 #include "protos/perfetto/trace/trace.pbzero.h"
38 #include "protos/perfetto/trace/trace_packet.pbzero.h"
39
40 namespace perfetto::trace_processor {
41 namespace {
42
43 class SerializingProtoTraceReader : public ChunkedTraceReader {
44 public:
SerializingProtoTraceReader(std::vector<uint8_t> * output)45 explicit SerializingProtoTraceReader(std::vector<uint8_t>* output)
46 : output_(output) {}
47
Parse(TraceBlobView blob)48 base::Status Parse(TraceBlobView blob) override {
49 return tokenizer_.Tokenize(std::move(blob), [this](TraceBlobView packet) {
50 uint8_t buffer[protozero::proto_utils::kMaxSimpleFieldEncodedSize];
51
52 uint8_t* pos = buffer;
53 pos = protozero::proto_utils::WriteVarInt(kTracePacketTag, pos);
54 pos = protozero::proto_utils::WriteVarInt(packet.length(), pos);
55 output_->insert(output_->end(), buffer, pos);
56
57 output_->insert(output_->end(), packet.data(),
58 packet.data() + packet.length());
59 return base::OkStatus();
60 });
61 }
62
NotifyEndOfFile()63 base::Status NotifyEndOfFile() override { return base::OkStatus(); }
64
65 private:
66 static constexpr uint8_t kTracePacketTag =
67 protozero::proto_utils::MakeTagLengthDelimited(
68 protos::pbzero::Trace::kPacketFieldNumber);
69
70 ProtoTraceTokenizer tokenizer_;
71 std::vector<uint8_t>* output_;
72 };
73
74 } // namespace
75
ReadTrace(TraceProcessor * tp,const char * filename,const std::function<void (uint64_t parsed_size)> & progress_callback)76 base::Status ReadTrace(
77 TraceProcessor* tp,
78 const char* filename,
79 const std::function<void(uint64_t parsed_size)>& progress_callback) {
80 RETURN_IF_ERROR(ReadTraceUnfinalized(tp, filename, progress_callback));
81 return tp->NotifyEndOfFile();
82 }
83
DecompressTrace(const uint8_t * data,size_t size,std::vector<uint8_t> * output)84 base::Status DecompressTrace(const uint8_t* data,
85 size_t size,
86 std::vector<uint8_t>* output) {
87 TraceType type = GuessTraceType(data, size);
88 if (type != TraceType::kGzipTraceType && type != TraceType::kProtoTraceType) {
89 return base::ErrStatus(
90 "Only GZIP and proto trace types are supported by DecompressTrace");
91 }
92
93 if (type == TraceType::kGzipTraceType) {
94 std::unique_ptr<ChunkedTraceReader> reader(
95 new SerializingProtoTraceReader(output));
96 GzipTraceParser parser(std::move(reader));
97 RETURN_IF_ERROR(parser.ParseUnowned(data, size));
98 return parser.NotifyEndOfFile();
99 }
100
101 PERFETTO_CHECK(type == TraceType::kProtoTraceType);
102
103 protos::pbzero::Trace::Decoder decoder(data, size);
104 util::GzipDecompressor decompressor;
105 if (size > 0 && !decoder.packet()) {
106 return base::ErrStatus("Trace does not contain valid packets");
107 }
108 for (auto it = decoder.packet(); it; ++it) {
109 protos::pbzero::TracePacket::Decoder packet(*it);
110 if (!packet.has_compressed_packets()) {
111 it->SerializeAndAppendTo(output);
112 continue;
113 }
114
115 // Make sure that to reset the stream between the gzip streams.
116 auto bytes = packet.compressed_packets();
117 decompressor.Reset();
118 using ResultCode = util::GzipDecompressor::ResultCode;
119 ResultCode ret = decompressor.FeedAndExtract(
120 bytes.data, bytes.size, [&output](const uint8_t* buf, size_t buf_len) {
121 output->insert(output->end(), buf, buf + buf_len);
122 });
123 if (ret == ResultCode::kError || ret == ResultCode::kNeedsMoreInput) {
124 return base::ErrStatus("Failed while decompressing stream");
125 }
126 }
127 return base::OkStatus();
128 }
129
130 } // namespace perfetto::trace_processor
131