blob: 2ea2ccdacae998b6bc07213d01e87f21727145b6 [file] [log] [blame]
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <vector>
#include "base/callback.h"
#include "base/macros.h"
#include "base/values.h"
#include "third_party/perfetto/include/perfetto/tracing/core/consumer.h"
#include "third_party/perfetto/include/perfetto/tracing/core/tracing_service.h"
namespace perfetto {
namespace protos {
class ChromeTracedValue;
} // namespace protos
} // namespace perfetto
namespace tracing {
// Serializes the supplied proto into JSON, using the same
// format as TracedValue::AppendAsTraceFormat.
void AppendProtoDictAsJSON(std::string* out,
const perfetto::protos::ChromeTracedValue& dict);
// This is a Perfetto consumer which will enable Perfetto tracing
// and subscribe to ChromeTraceEvent data sources. Any received
// protos will be converted to the legacy JSON Chrome Tracing
// format.
class JSONTraceExporter : public perfetto::Consumer {
// The owner of JSONTraceExporter should make sure to destroy
// |service| before destroying this.
JSONTraceExporter(const std::string& config,
perfetto::TracingService* service);
~JSONTraceExporter() override;
using OnTraceEventJSONCallback =
base::RepeatingCallback<void(const std::string& json,
base::DictionaryValue* metadata,
bool has_more)>;
void StopAndFlush(OnTraceEventJSONCallback callback);
// perfetto::Consumer implementation.
// This gets called by the Perfetto service as control signals,
// and to send finished protobufs over.
void OnConnect() override;
void OnDisconnect() override;
void OnTracingDisabled() override;
void OnTraceData(std::vector<perfetto::TracePacket> packets,
bool has_more) override;
void OnDetach(bool success) override;
void OnAttach(bool success, const perfetto::TraceConfig&) override;
void OnTraceStats(bool success, const perfetto::TraceStats&) override;
OnTraceEventJSONCallback json_callback_;
bool has_output_json_preamble_ = false;
bool has_output_first_event_ = false;
std::string config_;
std::unique_ptr<base::DictionaryValue> metadata_;
std::string legacy_system_ftrace_output_;
std::string legacy_system_trace_events_;
// Keep last to avoid edge-cases where its callbacks come in mid-destruction.
} // namespace tracing