aboutsummaryrefslogtreecommitdiff
path: root/src/trace_processor/forwarding_trace_parser.cc
blob: 89798f2fbc0dcd5987bcc263f02ef9ec465d63e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "src/trace_processor/forwarding_trace_parser.h"

#include "perfetto/base/logging.h"
#include "perfetto/ext/base/string_utils.h"
#include "src/trace_processor/gzip_trace_parser.h"
#include "src/trace_processor/importers/proto/proto_trace_parser.h"
#include "src/trace_processor/importers/proto/proto_trace_tokenizer.h"
#include "src/trace_processor/trace_sorter.h"

#if PERFETTO_BUILDFLAG(PERFETTO_TP_FUCHSIA)
#include "src/trace_processor/importers/fuchsia/fuchsia_trace_parser.h"
#include "src/trace_processor/importers/fuchsia/fuchsia_trace_tokenizer.h"
#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FUCHSIA)

// JSON parsing and exporting is only supported in the standalone and
// Chromium builds.
#if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON_IMPORT)
#include "src/trace_processor/importers/json/json_trace_parser.h"
#include "src/trace_processor/importers/json/json_trace_tokenizer.h"
#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON_IMPORT)

namespace perfetto {
namespace trace_processor {
namespace {

#if !PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
const char kNoZlibErr[] =
    "Cannot open compressed trace. zlib not enabled in the build config";
#endif

inline bool isspace(unsigned char c) {
  return ::isspace(c);
}

std::string RemoveWhitespace(const std::string& input) {
  std::string str(input);
  str.erase(std::remove_if(str.begin(), str.end(), isspace), str.end());
  return str;
}

// Fuchsia traces have a magic number as documented here:
// https://fuchsia.googlesource.com/fuchsia/+/HEAD/docs/development/tracing/trace-format/README.md#magic-number-record-trace-info-type-0
constexpr uint64_t kFuchsiaMagicNumber = 0x0016547846040010;

}  // namespace

ForwardingTraceParser::ForwardingTraceParser(TraceProcessorContext* context)
    : context_(context) {}

ForwardingTraceParser::~ForwardingTraceParser() {}

util::Status ForwardingTraceParser::Parse(std::unique_ptr<uint8_t[]> data,
                                          size_t size) {
  // If this is the first Parse() call, guess the trace type and create the
  // appropriate parser.

  if (!reader_) {
    TraceType trace_type;
    {
      auto scoped_trace = context_->storage->TraceExecutionTimeIntoStats(
          stats::guess_trace_type_duration_ns);
      trace_type = GuessTraceType(data.get(), size);
    }
    switch (trace_type) {
      case kJsonTraceType: {
        PERFETTO_DLOG("JSON trace detected");
#if PERFETTO_BUILDFLAG(PERFETTO_TP_JSON_IMPORT)
        reader_.reset(new JsonTraceTokenizer(context_));
        // JSON traces have no guarantees about the order of events in them.
        int64_t window_size_ns = std::numeric_limits<int64_t>::max();
        context_->sorter.reset(new TraceSorter(context_, window_size_ns));
        context_->parser.reset(new JsonTraceParser(context_));
#else   // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON_IMPORT)
        PERFETTO_FATAL("JSON traces not supported.");
#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_JSON_IMPORT)
        break;
      }
      case kProtoTraceType: {
        PERFETTO_DLOG("Proto trace detected");
        // This will be reduced once we read the trace config and we see flush
        // period being set.
        int64_t window_size_ns = std::numeric_limits<int64_t>::max();
        reader_.reset(new ProtoTraceTokenizer(context_));
        context_->sorter.reset(new TraceSorter(context_, window_size_ns));
        context_->parser.reset(new ProtoTraceParser(context_));
        break;
      }
      case kFuchsiaTraceType: {
#if PERFETTO_BUILDFLAG(PERFETTO_TP_FUCHSIA)
        PERFETTO_DLOG("Fuchsia trace detected");
        // Fuschia traces can have massively out of order events.
        int64_t window_size_ns = std::numeric_limits<int64_t>::max();
        reader_.reset(new FuchsiaTraceTokenizer(context_));
        context_->sorter.reset(new TraceSorter(context_, window_size_ns));
        context_->parser.reset(new FuchsiaTraceParser(context_));
#else   // PERFETTO_BUILDFLAG(PERFETTO_TP_FUCHSIA)
        PERFETTO_FATAL("Fuchsia traces not supported.");
#endif  // PERFETTO_BUILDFLAG(PERFETTO_TP_FUCHSIA)
        break;
      }
      case kSystraceTraceType:
        PERFETTO_DLOG("Systrace trace detected");
        if (context_->systrace_trace_parser) {
          reader_ = std::move(context_->systrace_trace_parser);
          break;
        } else {
          return util::ErrStatus("Systrace support is disabled");
        }
      case kGzipTraceType:
        PERFETTO_DLOG("gzip trace detected");
#if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
        reader_.reset(new GzipTraceParser(context_));
        break;
#else
        return util::ErrStatus(kNoZlibErr);
#endif
      case kCtraceTraceType:
        PERFETTO_DLOG("ctrace trace detected");
#if PERFETTO_BUILDFLAG(PERFETTO_ZLIB)
        reader_.reset(new GzipTraceParser(context_));
        break;
#else
        return util::ErrStatus(kNoZlibErr);
#endif
      case kUnknownTraceType:
        return util::ErrStatus("Unknown trace type provided");
    }
  }

  return reader_->Parse(std::move(data), size);
}

TraceType GuessTraceType(const uint8_t* data, size_t size) {
  if (size == 0)
    return kUnknownTraceType;
  std::string start(reinterpret_cast<const char*>(data),
                    std::min<size_t>(size, 20));
  std::string start_minus_white_space = RemoveWhitespace(start);
  if (base::StartsWith(start_minus_white_space, "{\"traceEvents\":["))
    return kJsonTraceType;
  if (base::StartsWith(start_minus_white_space, "[{"))
    return kJsonTraceType;
  if (size >= 8) {
    uint64_t first_word = *reinterpret_cast<const uint64_t*>(data);
    if (first_word == kFuchsiaMagicNumber)
      return kFuchsiaTraceType;
  }

  // Systrace with header but no leading HTML.
  if (base::StartsWith(start, "# tracer"))
    return kSystraceTraceType;

  // Systrace with leading HTML.
  if (base::StartsWith(start, "<!DOCTYPE html>") ||
      base::StartsWith(start, "<html>"))
    return kSystraceTraceType;

  // Ctrace is deflate'ed systrace.
  if (start.find("TRACE:") != std::string::npos)
    return kCtraceTraceType;

  // Systrace with no header or leading HTML.
  if (base::StartsWith(start, " "))
    return kSystraceTraceType;

  // gzip'ed trace containing one of the other formats.
  if (base::StartsWith(start, "\x1f\x8b"))
    return kGzipTraceType;

  return kProtoTraceType;
}

}  // namespace trace_processor
}  // namespace perfetto