• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_tokenizer.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 ProtoTraceTokenizer(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         return util::ErrStatus("Unknown trace type provided");
134     }
135   }
136 
137   return reader_->Parse(std::move(data), size);
138 }
139 
NotifyEndOfFile()140 void ForwardingTraceParser::NotifyEndOfFile() {
141   reader_->NotifyEndOfFile();
142 }
143 
GuessTraceType(const uint8_t * data,size_t size)144 TraceType GuessTraceType(const uint8_t* data, size_t size) {
145   if (size == 0)
146     return kUnknownTraceType;
147   std::string start(reinterpret_cast<const char*>(data),
148                     std::min<size_t>(size, 20));
149   if (size >= 8) {
150     uint64_t first_word;
151     memcpy(&first_word, data, sizeof(first_word));
152     if (first_word == kFuchsiaMagicNumber)
153       return kFuchsiaTraceType;
154   }
155   std::string start_minus_white_space = RemoveWhitespace(start);
156   if (base::StartsWith(start_minus_white_space, "{"))
157     return kJsonTraceType;
158   if (base::StartsWith(start_minus_white_space, "[{"))
159     return kJsonTraceType;
160 
161   // Systrace with header but no leading HTML.
162   if (base::StartsWith(start, "# tracer"))
163     return kSystraceTraceType;
164 
165   // Systrace with leading HTML.
166   if (base::StartsWith(start, "<!DOCTYPE html>") ||
167       base::StartsWith(start, "<html>"))
168     return kSystraceTraceType;
169 
170   // Ctrace is deflate'ed systrace.
171   if (start.find("TRACE:") != std::string::npos)
172     return kCtraceTraceType;
173 
174   // Ninja's buils log (.ninja_log).
175   if (base::StartsWith(start, "# ninja log"))
176     return kNinjaLogTraceType;
177 
178   // Systrace with no header or leading HTML.
179   if (base::StartsWith(start, " "))
180     return kSystraceTraceType;
181 
182   // gzip'ed trace containing one of the other formats.
183   if (base::StartsWith(start, "\x1f\x8b"))
184     return kGzipTraceType;
185 
186   return kProtoTraceType;
187 }
188 
189 }  // namespace trace_processor
190 }  // namespace perfetto
191