blob: 64a400bba9905d3c002a49576b20ef5be45fb3d6 [file] [log] [blame]
Eric Seckler8f70bbf2019-10-09 09:37:43 +01001/*
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
Lalit Maganti1272d4c2020-08-28 14:14:10 +010019#include "perfetto/base/logging.h"
Primiano Tucciab293f52020-12-08 11:46:52 +010020#include "perfetto/ext/base/file_utils.h"
Eric Seckler8f70bbf2019-10-09 09:37:43 +010021#include "perfetto/ext/base/scoped_file.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000022#include "perfetto/ext/base/utils.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010023#include "perfetto/protozero/proto_utils.h"
Eric Seckler8f70bbf2019-10-09 09:37:43 +010024#include "perfetto/trace_processor/trace_processor.h"
25
Primiano Tucci3264b592021-11-08 18:20:51 +000026#include "perfetto/trace_processor/trace_blob.h"
27#include "perfetto/trace_processor/trace_blob_view.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010028#include "src/trace_processor/forwarding_trace_parser.h"
29#include "src/trace_processor/importers/gzip/gzip_trace_parser.h"
Lalit Maganti1caf3492020-09-10 21:00:08 +010030#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
Anna Mayznerec507902022-07-07 15:40:14 +000031#include "src/trace_processor/read_trace_internal.h"
Lalit Maganti69216ec2021-05-21 14:10:42 +010032#include "src/trace_processor/util/gzip_utils.h"
Lalit Maganti1272d4c2020-08-28 14:14:10 +010033#include "src/trace_processor/util/status_macros.h"
Lalit Maganti9d538bd2020-03-12 23:48:16 +000034
35#include "protos/perfetto/trace/trace.pbzero.h"
36#include "protos/perfetto/trace/trace_packet.pbzero.h"
37
Primiano Tucci0eeaf962021-11-08 20:05:04 +000038#if TRACE_PROCESSOR_HAS_MMAP()
39#include <stdlib.h>
40#include <sys/mman.h>
41#include <unistd.h>
Eric Seckler8f70bbf2019-10-09 09:37:43 +010042#endif
43
44namespace perfetto {
45namespace trace_processor {
Lalit Maganti1272d4c2020-08-28 14:14:10 +010046namespace {
47
Lalit Maganti1caf3492020-09-10 21:00:08 +010048class SerializingProtoTraceReader : public ChunkedTraceReader {
49 public:
Primiano Tucci3264b592021-11-08 18:20:51 +000050 explicit SerializingProtoTraceReader(std::vector<uint8_t>* output)
51 : output_(output) {}
Lalit Maganti1caf3492020-09-10 21:00:08 +010052
Primiano Tucci3264b592021-11-08 18:20:51 +000053 util::Status Parse(TraceBlobView blob) override {
54 return tokenizer_.Tokenize(std::move(blob), [this](TraceBlobView packet) {
55 uint8_t buffer[protozero::proto_utils::kMaxSimpleFieldEncodedSize];
Lalit Maganti1caf3492020-09-10 21:00:08 +010056
Primiano Tucci3264b592021-11-08 18:20:51 +000057 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);
Lalit Maganti1caf3492020-09-10 21:00:08 +010061
Primiano Tucci3264b592021-11-08 18:20:51 +000062 output_->insert(output_->end(), packet.data(),
63 packet.data() + packet.length());
64 return util::OkStatus();
65 });
Lalit Maganti1caf3492020-09-10 21:00:08 +010066 }
67
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
Lalit Maganti1272d4c2020-08-28 14:14:10 +010079} // namespace
Eric Seckler8f70bbf2019-10-09 09:37:43 +010080
81util::Status ReadTrace(
82 TraceProcessor* tp,
83 const char* filename,
84 const std::function<void(uint64_t parsed_size)>& progress_callback) {
Anna Mayznerec507902022-07-07 15:40:14 +000085 ReadTraceUnfinalized(tp, filename, progress_callback);
Eric Seckler8f70bbf2019-10-09 09:37:43 +010086 tp->NotifyEndOfFile();
Eric Seckler8f70bbf2019-10-09 09:37:43 +010087 return util::OkStatus();
88}
89
Lalit Maganti9d538bd2020-03-12 23:48:16 +000090util::Status DecompressTrace(const uint8_t* data,
91 size_t size,
92 std::vector<uint8_t>* output) {
Lalit Maganti1caf3492020-09-10 21:00:08 +010093 TraceType type = GuessTraceType(data, size);
94 if (type != TraceType::kGzipTraceType && type != TraceType::kProtoTraceType) {
Lalit Maganti9d538bd2020-03-12 23:48:16 +000095 return util::ErrStatus(
Lalit Maganti1caf3492020-09-10 21:00:08 +010096 "Only GZIP and proto trace types are supported by DecompressTrace");
Lalit Maganti9d538bd2020-03-12 23:48:16 +000097 }
98
Lalit Maganti1caf3492020-09-10 21:00:08 +010099 if (type == TraceType::kGzipTraceType) {
Lalit Maganti9d06f192020-10-02 16:12:58 +0100100 std::unique_ptr<ChunkedTraceReader> reader(
101 new SerializingProtoTraceReader(output));
102 GzipTraceParser parser(std::move(reader));
Lalit Maganti1caf3492020-09-10 21:00:08 +0100103
Lalit Maganti9d06f192020-10-02 16:12:58 +0100104 RETURN_IF_ERROR(parser.ParseUnowned(data, size));
105 if (parser.needs_more_input())
Lalit Maganti1caf3492020-09-10 21:00:08 +0100106 return util::ErrStatus("Cannot decompress partial trace file");
107
Lalit Maganti9d06f192020-10-02 16:12:58 +0100108 parser.NotifyEndOfFile();
Lalit Maganti1caf3492020-09-10 21:00:08 +0100109 return util::OkStatus();
110 }
111
112 PERFETTO_CHECK(type == TraceType::kProtoTraceType);
113
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000114 protos::pbzero::Trace::Decoder decoder(data, size);
Lalit Maganti69216ec2021-05-21 14:10:42 +0100115 util::GzipDecompressor decompressor;
Hector Dearman4aed97a2020-09-09 15:24:23 +0100116 if (size > 0 && !decoder.packet()) {
117 return util::ErrStatus("Trace does not contain valid packets");
118 }
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000119 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();
Lalit Maganti69216ec2021-05-21 14:10:42 +0100129 using ResultCode = util::GzipDecompressor::ResultCode;
Mohit Saini5bc6f2c2022-02-11 02:09:59 +0000130 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");
Lalit Maganti9d538bd2020-03-12 23:48:16 +0000136 }
137 }
138 return util::OkStatus();
139}
140
Eric Seckler8f70bbf2019-10-09 09:37:43 +0100141} // namespace trace_processor
142} // namespace perfetto