#include "services/tracing/public/cpp/perfetto/perfetto_traced_process.h"
#include "base/command_line.h"
#include "base/functional/callback_helpers.h"
#include "base/memory/raw_ptr.h"
#include "base/no_destructor.h"
#include "base/run_loop.h"
#include "base/task/sequenced_task_runner.h"
#include "base/trace_event/trace_config.h"
#include "base/trace_event/trace_log.h"
#include "base/tracing/perfetto_platform.h"
#include "build/build_config.h"
#include "services/tracing/public/cpp/perfetto/custom_event_recorder.h"
#include "services/tracing/public/cpp/perfetto/dummy_producer.h"
#include "services/tracing/public/cpp/perfetto/metadata_data_source.h"
#include "services/tracing/public/cpp/perfetto/perfetto_tracing_backend.h"
#include "services/tracing/public/cpp/perfetto/producer_client.h"
#include "services/tracing/public/cpp/perfetto/trace_event_data_source.h"
#include "services/tracing/public/cpp/perfetto/track_name_recorder.h"
#include "services/tracing/public/cpp/stack_sampling/tracing_sampler_profiler.h"
#include "services/tracing/public/cpp/trace_startup.h"
#include "services/tracing/public/cpp/traced_process_impl.h"
#include "services/tracing/public/cpp/tracing_features.h"
#include "services/tracing/public/cpp/triggers_data_source.h"
#include "services/tracing/public/mojom/tracing_service.mojom.h"
#include "third_party/perfetto/include/perfetto/tracing/tracing.h"
#if BUILDFLAG(IS_WIN)
#include "components/tracing/common/etw_system_data_source_win.h"
#endif
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#include "services/tracing/public/cpp/perfetto/posix_system_producer.h"
#include "third_party/perfetto/include/perfetto/tracing/default_socket.h"
#endif
namespace tracing {
namespace {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
static bool g_system_producer_enabled = …;
#endif
std::unique_ptr<SystemProducer> NewSystemProducer(
base::tracing::PerfettoTaskRunner* runner,
const char* socket_name) { … }
const char* MaybeSocket() { … }
void OnPerfettoLogMessage(perfetto::base::LogMessageCallbackArgs args) { … }
#if BUILDFLAG(IS_POSIX) && !BUILDFLAG(IS_ANDROID)
void ConnectProducerSocketViaMojo(perfetto::CreateSocketCallback cb,
uint32_t retry_delay_ms) { … }
void ConnectProducerSocketAsync(perfetto::CreateSocketCallback cb) { … }
#endif
}
PerfettoTracedProcess::DataSourceBase::DataSourceBase(const std::string& name)
: … { … }
PerfettoTracedProcess::DataSourceBase::~DataSourceBase() = default;
void PerfettoTracedProcess::DataSourceBase::StartTracing(
uint64_t data_source_id,
PerfettoProducer* producer,
const perfetto::DataSourceConfig& data_source_config) { … }
void PerfettoTracedProcess::DataSourceBase::StopTracing(
base::OnceClosure stop_complete_callback) { … }
void PerfettoTracedProcess::DataSourceBase::StartTracingImpl(
PerfettoProducer* producer,
const perfetto::DataSourceConfig& data_source_config) { … }
void PerfettoTracedProcess::DataSourceBase::StopTracingImpl(
base::OnceClosure stop_complete_callback) { … }
void PerfettoTracedProcess::DataSourceBase::Flush(
base::RepeatingClosure flush_complete_callback) { … }
base::SequencedTaskRunner*
PerfettoTracedProcess::DataSourceBase::GetTaskRunner() { … }
PerfettoTracedProcess* PerfettoTracedProcess::Get() { … }
PerfettoTracedProcess::PerfettoTracedProcess()
: … { … }
PerfettoTracedProcess::~PerfettoTracedProcess() { … }
void PerfettoTracedProcess::SetConsumerConnectionFactory(
ConsumerConnectionFactory factory,
scoped_refptr<base::SequencedTaskRunner> task_runner) { … }
void PerfettoTracedProcess::ConnectProducer(
mojo::PendingRemote<mojom::PerfettoService> perfetto_service) { … }
void PerfettoTracedProcess::ClearDataSourcesForTesting() { … }
std::unique_ptr<ProducerClient>
PerfettoTracedProcess::SetProducerClientForTesting(
std::unique_ptr<ProducerClient> client) { … }
std::unique_ptr<SystemProducer>
PerfettoTracedProcess::SetSystemProducerForTesting(
std::unique_ptr<SystemProducer> producer) { … }
base::tracing::PerfettoTaskRunner* PerfettoTracedProcess::GetTaskRunner() { … }
std::unique_ptr<PerfettoTracedProcess::TestHandle>
PerfettoTracedProcess::SetupForTesting(
scoped_refptr<base::SequencedTaskRunner> task_runner) { … }
void PerfettoTracedProcess::AddDataSource(DataSourceBase* data_source) { … }
std::set<raw_ptr<PerfettoTracedProcess::DataSourceBase, SetExperimental>>
PerfettoTracedProcess::data_sources() { … }
bool PerfettoTracedProcess::SetupStartupTracing(
PerfettoProducer* producer,
const base::trace_event::TraceConfig& trace_config,
bool privacy_filtering_enabled) { … }
void PerfettoTracedProcess::RequestStartupTracing(
const perfetto::TraceConfig& config,
const perfetto::Tracing::SetupStartupTracingOpts& opts) { … }
void PerfettoTracedProcess::SetupClientLibrary(bool enable_consumer) { … }
void PerfettoTracedProcess::OnThreadPoolAvailable(bool enable_consumer) { … }
void PerfettoTracedProcess::SetAllowSystemTracingConsumerCallback(
base::RepeatingCallback<bool()> callback) { … }
void PerfettoTracedProcess::SetAllowSystemTracingConsumerForTesting(
bool enabled) { … }
void PerfettoTracedProcess::ShouldAllowConsumerSession(
const perfetto::TracingPolicy::ShouldAllowConsumerSessionArgs& args) { … }
void PerfettoTracedProcess::ShouldAllowSystemConsumerSession(
std::function<void(bool)> result_callback) { … }
void PerfettoTracedProcess::SetSystemProducerEnabledForTesting(bool enabled) { … }
void PerfettoTracedProcess::SetupSystemTracing(
std::optional<const char*> system_socket) { … }
bool PerfettoTracedProcess::CanStartTracing(
PerfettoProducer* producer,
base::OnceCallback<void()> start_tracing) { … }
void PerfettoTracedProcess::ActivateSystemTriggers(
const std::vector<std::string>& triggers) { … }
ProducerClient* PerfettoTracedProcess::producer_client() const { … }
SystemProducer* PerfettoTracedProcess::system_producer() const { … }
PerfettoTracedProcess::TestHandle::~TestHandle() { … }
}