File: background_tracing_utils.cc

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (215 lines) | stat: -rw-r--r-- 7,512 bytes parent folder | download | duplicates (4)
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/tracing/common/background_tracing_utils.h"

#include <memory>
#include <string>
#include <utility>

#include "base/check.h"
#include "base/command_line.h"
#include "base/feature_list.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/task/task_traits.h"
#include "base/task/thread_pool.h"
#include "components/tracing/common/background_tracing_state_manager.h"
#include "components/tracing/common/tracing_scenarios_config.h"
#include "components/tracing/common/tracing_switches.h"
#include "content/public/browser/background_tracing_manager.h"
#include "content/public/browser/browser_thread.h"

namespace tracing {
namespace {

bool BlockingWriteTraceToFile(const base::FilePath& output_file,
                              std::string file_contents) {
  if (base::WriteFile(output_file, file_contents)) {
    LOG(ERROR) << "Background trace written to "
               << output_file.LossyDisplayName();
    return true;
  }
  LOG(ERROR) << "Failed to write background trace to "
             << output_file.LossyDisplayName();
  return false;
}

void WriteTraceToFile(
    const base::FilePath& output_path,
    const std::string& file_name,
    std::string file_contents,
    content::BackgroundTracingManager::FinishedProcessingCallback
        done_callback) {
  DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
  base::FilePath output_file = output_path.AppendASCII(file_name);
  base::ThreadPool::PostTaskAndReplyWithResult(
      FROM_HERE, {base::MayBlock()},
      base::BindOnce(&BlockingWriteTraceToFile, output_file,
                     std::move(file_contents)),
      std::move(done_callback));
}
}

bool SetupBackgroundTracingFromProtoConfigFile(
    const base::FilePath& config_file) {
  std::optional<perfetto::protos::gen::ChromeFieldTracingConfig> config;
  std::string config_text;
  if (base::ReadFileToString(config_file, &config_text) &&
      !config_text.empty()) {
    if (base::FilePath::CompareEqualIgnoreCase(config_file.Extension(),
                                               FILE_PATH_LITERAL(".pb"))) {
      config = tracing::ParseSerializedTracingScenariosConfig(
          base::as_byte_span(config_text));
    } else {
      config = tracing::ParseEncodedTracingScenariosConfig(config_text);
    }
  } else {
    LOG(ERROR) << "Failed to read field tracing config file "
               << config_file.value() << ".";
    return false;
  }

  if (!config) {
    LOG(ERROR) << "Failed to parse field tracing config file "
               << config_file.value() << "."
               << "Make sure to provide a proto (.pb) or base64 encoded (.txt)"
               << " file that contains scenarios config.";
    return false;
  }

  // NO_DATA_FILTERING is set because the trace is saved to a local output file
  // instead of being uploaded to a metrics server, so there are no PII
  // concerns.
  auto scenarios =
      content::BackgroundTracingManager::GetInstance().AddPresetScenarios(
          std::move(*config),
          content::BackgroundTracingManager::NO_DATA_FILTERING);

  return content::BackgroundTracingManager::GetInstance().SetEnabledScenarios(
      scenarios);
}

bool SetupBackgroundTracingFromCommandLine() {
  auto* command_line = base::CommandLine::ForCurrentProcess();

  if (tracing::HasBackgroundTracingOutputPath() &&
      !tracing::SetBackgroundTracingOutputPath()) {
    return false;
  }

  if (!IsBackgroundTracingEnabledFromCommandLine()) {
    return false;
  }

  if (command_line->GetSwitchValueNative(switches::kEnableBackgroundTracing)
          .empty()) {
    LOG(ERROR) << "--enable-background-tracing needs a config file path";
    return false;
  }
  return SetupBackgroundTracingFromProtoConfigFile(
      command_line->GetSwitchValuePath(switches::kEnableBackgroundTracing));
}

bool SetupPresetTracingFromFieldTrial() {
  if (IsBackgroundTracingEnabledFromCommandLine()) {
    return false;
  }

  auto& config = BackgroundTracingStateManager::GetInstance();
  const auto& enabled_scenarios = config.enabled_scenarios();
  if (enabled_scenarios.empty()) {
    return false;
  }
  auto& manager = content::BackgroundTracingManager::GetInstance();
  auto tracing_scenarios_config = GetPresetTracingScenariosConfig();
  if (tracing_scenarios_config) {
    content::BackgroundTracingManager::DataFiltering data_filtering =
        config.privacy_filter_enabled()
            ? content::BackgroundTracingManager::ANONYMIZE_DATA
            : content::BackgroundTracingManager::NO_DATA_FILTERING;
    manager.AddPresetScenarios(std::move(*tracing_scenarios_config),
                               data_filtering);
    return manager.SetEnabledScenarios(enabled_scenarios);
  }
  return false;
}

bool SetupSystemTracingFromFieldTrial() {
  if (IsBackgroundTracingEnabledFromCommandLine()) {
    return false;
  }

  auto& manager = content::BackgroundTracingManager::GetInstance();
  auto trigger_config = tracing::GetTracingTriggerRulesConfig();
  if (!trigger_config) {
    return false;
  }
  return manager.InitializePerfettoTriggerRules(std::move(*trigger_config));
}

bool SetupFieldTracingFromFieldTrial() {
  if (IsBackgroundTracingEnabledFromCommandLine()) {
    return false;
  }

  bool local_scenarios = false;
  if (tracing::HasBackgroundTracingOutputPath()) {
    local_scenarios = true;
    if (!tracing::SetBackgroundTracingOutputPath()) {
      return false;
    }
  } else if (!kFieldTracingAnonymized.Get()) {
    local_scenarios = true;
  }

  auto& manager = content::BackgroundTracingManager::GetInstance();
  auto tracing_scenarios_config = tracing::GetFieldTracingScenariosConfig();
  if (!tracing_scenarios_config) {
    return false;
  }

  if (local_scenarios) {
    auto& config = BackgroundTracingStateManager::GetInstance();
    content::BackgroundTracingManager::DataFiltering data_filtering =
        config.privacy_filter_enabled()
            ? content::BackgroundTracingManager::ANONYMIZE_DATA
            : content::BackgroundTracingManager::NO_DATA_FILTERING;
    auto scenarios = manager.AddPresetScenarios(
        std::move(*tracing_scenarios_config), data_filtering);
    if (config.enabled_scenarios().empty()) {
      return manager.SetEnabledScenarios(scenarios);
    }
    return false;
  }
  return manager.InitializeFieldScenarios(
      std::move(*tracing_scenarios_config),
      content::BackgroundTracingManager::ANONYMIZE_DATA,
      kFieldTracingForceUploads.Get(), kFieldTracingUploadLimitKb.Get());
}

bool SetBackgroundTracingOutputPath() {
  auto* command_line = base::CommandLine::ForCurrentProcess();
  if (command_line->GetSwitchValuePath(switches::kBackgroundTracingOutputPath)
          .empty()) {
    LOG(ERROR) << "--background-tracing-output-path needs an output path";
    return false;
  }
  auto output_path =
      command_line->GetSwitchValuePath(switches::kBackgroundTracingOutputPath);

  auto receive_callback = base::BindRepeating(&WriteTraceToFile, output_path);
  content::BackgroundTracingManager::GetInstance().SetReceiveCallback(
      std::move(receive_callback));
  return true;
}

bool HasBackgroundTracingOutputPath() {
  auto* command_line = base::CommandLine::ForCurrentProcess();
  return command_line->HasSwitch(switches::kBackgroundTracingOutputPath);
}

}  // namespace tracing