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 "src/trace_processor/forwarding_trace_parser.h"
18
19 #include "perfetto/base/logging.h"
20 #include "perfetto/ext/base/string_utils.h"
21 #include "src/trace_processor/importers/common/process_tracker.h"
22 #include "src/trace_processor/importers/ninja/ninja_log_parser.h"
23 #include "src/trace_processor/importers/proto/proto_trace_parser.h"
24 #include "src/trace_processor/importers/proto/proto_trace_reader.h"
25 #include "src/trace_processor/trace_sorter.h"
26
27 namespace perfetto {
28 namespace trace_processor {
29 namespace {
30
31 const char kNoZlibErr[] =
32 "Cannot open compressed trace. zlib not enabled in the build config";
33
isspace(unsigned char c)34 inline bool isspace(unsigned char c) {
35 return ::isspace(c);
36 }
37
RemoveWhitespace(std::string str)38 std::string RemoveWhitespace(std::string str) {
39 str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
40 return str;
41 }
42
43 // Fuchsia traces have a magic number as documented here:
44 // https://fuchsia.googlesource.com/fuchsia/+/HEAD/docs/development/tracing/trace-format/README.md#magic-number-record-trace-info-type-0
45 constexpr uint64_t kFuchsiaMagicNumber = 0x0016547846040010;
46
47 } // namespace
48
ForwardingTraceParser(TraceProcessorContext * context)49 ForwardingTraceParser::ForwardingTraceParser(TraceProcessorContext* context)
50 : context_(context) {}
51
~ForwardingTraceParser()52 ForwardingTraceParser::~ForwardingTraceParser() {}
53
Parse(std::unique_ptr<uint8_t[]> data,size_t size)54 util::Status ForwardingTraceParser::Parse(std::unique_ptr<uint8_t[]> data,
55 size_t size) {
56 // If this is the first Parse() call, guess the trace type and create the
57 // appropriate parser.
58 static const int64_t kMaxWindowSize = std::numeric_limits<int64_t>::max();
59 if (!reader_) {
60 TraceType trace_type;
61 {
62 auto scoped_trace = context_->storage->TraceExecutionTimeIntoStats(
63 stats::guess_trace_type_duration_ns);
64 trace_type = GuessTraceType(data.get(), size);
65 }
66 switch (trace_type) {
67 case kJsonTraceType: {
68 PERFETTO_DLOG("JSON trace detected");
69 if (context_->json_trace_tokenizer && context_->json_trace_parser) {
70 reader_ = std::move(context_->json_trace_tokenizer);
71
72 // JSON traces have no guarantees about the order of events in them.
73 context_->sorter.reset(new TraceSorter(
74 std::move(context_->json_trace_parser), kMaxWindowSize));
75 } else {
76 return util::ErrStatus("JSON support is disabled");
77 }
78 break;
79 }
80 case kProtoTraceType: {
81 PERFETTO_DLOG("Proto trace detected");
82 // This will be reduced once we read the trace config and we see flush
83 // period being set.
84 reader_.reset(new ProtoTraceReader(context_));
85 context_->sorter.reset(new TraceSorter(
86 std::unique_ptr<TraceParser>(new ProtoTraceParser(context_)),
87 kMaxWindowSize));
88 context_->process_tracker->SetPidZeroIgnoredForIdleProcess();
89 break;
90 }
91 case kNinjaLogTraceType: {
92 PERFETTO_DLOG("Ninja log detected");
93 reader_.reset(new NinjaLogParser(context_));
94 break;
95 }
96 case kFuchsiaTraceType: {
97 PERFETTO_DLOG("Fuchsia trace detected");
98 if (context_->fuchsia_trace_parser &&
99 context_->fuchsia_trace_tokenizer) {
100 reader_ = std::move(context_->fuchsia_trace_tokenizer);
101
102 // Fuschia traces can have massively out of order events.
103 context_->sorter.reset(new TraceSorter(
104 std::move(context_->fuchsia_trace_parser), kMaxWindowSize));
105 } else {
106 return util::ErrStatus("Fuchsia support is disabled");
107 }
108 break;
109 }
110 case kSystraceTraceType:
111 PERFETTO_DLOG("Systrace trace detected");
112 context_->process_tracker->SetPidZeroIgnoredForIdleProcess();
113 if (context_->systrace_trace_parser) {
114 reader_ = std::move(context_->systrace_trace_parser);
115 break;
116 } else {
117 return util::ErrStatus("Systrace support is disabled");
118 }
119 case kGzipTraceType:
120 case kCtraceTraceType:
121 if (trace_type == kGzipTraceType) {
122 PERFETTO_DLOG("gzip trace detected");
123 } else {
124 PERFETTO_DLOG("ctrace trace detected");
125 }
126 if (context_->gzip_trace_parser) {
127 reader_ = std::move(context_->gzip_trace_parser);
128 break;
129 } else {
130 return util::ErrStatus(kNoZlibErr);
131 }
132 case kUnknownTraceType:
133 // If renaming this error message don't remove the "(ERR:fmt)" part.
134 // The UI's error_dialog.ts uses it to make the dialog more graceful.
135 return util::ErrStatus("Unknown trace type provided (ERR:fmt)");
136 }
137 }
138
139 return reader_->Parse(std::move(data), size);
140 }
141
NotifyEndOfFile()142 void ForwardingTraceParser::NotifyEndOfFile() {
143 reader_->NotifyEndOfFile();
144 }
145
GuessTraceType(const uint8_t * data,size_t size)146 TraceType GuessTraceType(const uint8_t* data, size_t size) {
147 if (size == 0)
148 return kUnknownTraceType;
149 std::string start(reinterpret_cast<const char*>(data),
150 std::min<size_t>(size, 20));
151 if (size >= 8) {
152 uint64_t first_word;
153 memcpy(&first_word, data, sizeof(first_word));
154 if (first_word == kFuchsiaMagicNumber)
155 return kFuchsiaTraceType;
156 }
157 std::string start_minus_white_space = RemoveWhitespace(start);
158 if (base::StartsWith(start_minus_white_space, "{"))
159 return kJsonTraceType;
160 if (base::StartsWith(start_minus_white_space, "[{"))
161 return kJsonTraceType;
162
163 // Systrace with header but no leading HTML.
164 if (base::Contains(start, "# tracer"))
165 return kSystraceTraceType;
166
167 // Systrace with leading HTML.
168 if (base::StartsWith(start, "<!DOCTYPE html>") ||
169 base::StartsWith(start, "<html>"))
170 return kSystraceTraceType;
171
172 // Ctrace is deflate'ed systrace.
173 if (base::Contains(start, "TRACE:"))
174 return kCtraceTraceType;
175
176 // Ninja's buils log (.ninja_log).
177 if (base::StartsWith(start, "# ninja log"))
178 return kNinjaLogTraceType;
179
180 // Systrace with no header or leading HTML.
181 if (base::StartsWith(start, " "))
182 return kSystraceTraceType;
183
184 // gzip'ed trace containing one of the other formats.
185 if (base::StartsWith(start, "\x1f\x8b"))
186 return kGzipTraceType;
187
188 if (base::StartsWith(start, "\x0a"))
189 return kProtoTraceType;
190
191 return kUnknownTraceType;
192 }
193
194 } // namespace trace_processor
195 } // namespace perfetto
196