• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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/util/trace_type.h"
18 
19 #include <algorithm>
20 #include <cstddef>
21 #include <cstdint>
22 #include <string>
23 
24 #include "perfetto/base/logging.h"
25 #include "perfetto/ext/base/string_utils.h"
26 
27 namespace perfetto::trace_processor {
28 namespace {
29 // Fuchsia traces have a magic number as documented here:
30 // https://fuchsia.googlesource.com/fuchsia/+/HEAD/docs/development/tracing/trace-format/README.md#magic-number-record-trace-info-type-0
31 constexpr uint64_t kFuchsiaMagicNumber = 0x0016547846040010;
32 constexpr char kPerfMagic[] = "PERFILE2";
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 }  // namespace
44 
ToString(TraceType trace_type)45 const char* ToString(TraceType trace_type) {
46   switch (trace_type) {
47     case kJsonTraceType:
48       return "JSON trace";
49     case kProtoTraceType:
50       return "proto trace";
51     case kNinjaLogTraceType:
52       return "ninja log";
53     case kFuchsiaTraceType:
54       return "fuchsia trace";
55     case kSystraceTraceType:
56       return "systrace trace";
57     case kGzipTraceType:
58       return "gzip trace";
59     case kCtraceTraceType:
60       return "ctrace trace";
61     case kZipFile:
62       return "ZIP file";
63     case kPerfDataTraceType:
64       return "perf data";
65     case kUnknownTraceType:
66       return "unknown trace";
67   }
68   PERFETTO_FATAL("For GCC");
69 }
70 
GuessTraceType(const uint8_t * data,size_t size)71 TraceType GuessTraceType(const uint8_t* data, size_t size) {
72   if (size == 0)
73     return kUnknownTraceType;
74   std::string start(reinterpret_cast<const char*>(data),
75                     std::min<size_t>(size, kGuessTraceMaxLookahead));
76   if (size >= 8) {
77     uint64_t first_word;
78     memcpy(&first_word, data, sizeof(first_word));
79     if (first_word == kFuchsiaMagicNumber)
80       return kFuchsiaTraceType;
81   }
82   if (base::StartsWith(start, kPerfMagic)) {
83     return kPerfDataTraceType;
84   }
85   std::string start_minus_white_space = RemoveWhitespace(start);
86   if (base::StartsWith(start_minus_white_space, "{\""))
87     return kJsonTraceType;
88   if (base::StartsWith(start_minus_white_space, "[{\""))
89     return kJsonTraceType;
90 
91   // Systrace with header but no leading HTML.
92   if (base::Contains(start, "# tracer"))
93     return kSystraceTraceType;
94 
95   // Systrace with leading HTML.
96   // Both: <!DOCTYPE html> and <!DOCTYPE HTML> have been observed.
97   std::string lower_start = base::ToLower(start);
98   if (base::StartsWith(lower_start, "<!doctype html>") ||
99       base::StartsWith(lower_start, "<html>"))
100     return kSystraceTraceType;
101 
102   // Traces obtained from atrace -z (compress).
103   // They all have the string "TRACE:" followed by 78 9C which is a zlib header
104   // for "deflate, default compression, window size=32K" (see b/208691037)
105   if (base::Contains(start, "TRACE:\n\x78\x9c"))
106     return kCtraceTraceType;
107 
108   // Traces obtained from atrace without -z (no compression).
109   if (base::Contains(start, "TRACE:\n"))
110     return kSystraceTraceType;
111 
112   // Ninja's build log (.ninja_log).
113   if (base::StartsWith(start, "# ninja log"))
114     return kNinjaLogTraceType;
115 
116   // Systrace with no header or leading HTML.
117   if (base::StartsWith(start, " "))
118     return kSystraceTraceType;
119 
120   // gzip'ed trace containing one of the other formats.
121   if (base::StartsWith(start, "\x1f\x8b"))
122     return kGzipTraceType;
123 
124   if (base::StartsWith(start, "\x0a"))
125     return kProtoTraceType;
126 
127   if (base::StartsWith(start, "PK\x03\x04"))
128     return kZipFile;
129 
130   return kUnknownTraceType;
131 }
132 
133 }  // namespace perfetto::trace_processor
134