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