summaryrefslogtreecommitdiffstats
path: root/components/tracing/startup_tracing.cc
blob: 321b95092a43ef15857474cf3474dafb0d3e1d7b (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
// Copyright (c) 2015 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 "components/tracing/startup_tracing.h"

#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/path_service.h"
#include "base/trace_event/trace_event.h"

namespace tracing {

namespace {

// Maximum trace config file size that will be loaded, in bytes.
const size_t kTraceConfigFileSizeLimit = 64 * 1024;

// Trace config file path:
// - Android: /data/local/.config/chrome-trace-config.json
// - POSIX other than Android: $HOME/.config/chrome-trace-config.json
// - Win: %USERPROFILE%/.config/chrome-trace-config.json
#if defined(OS_ANDROID)
const base::FilePath::CharType kAndroidTraceConfigDir[] =
    FILE_PATH_LITERAL("/data/local");
#endif

const base::FilePath::CharType kChromeConfigDir[] =
    FILE_PATH_LITERAL(".config");
const base::FilePath::CharType kTraceConfigFileName[] =
    FILE_PATH_LITERAL("chrome-trace-config.json");

base::FilePath GetTraceConfigFilePath() {
#if defined(OS_ANDROID)
  base::FilePath path(kAndroidTraceConfigDir);
#elif defined(OS_POSIX) || defined(OS_WIN)
  base::FilePath path;
  PathService::Get(base::DIR_HOME, &path);
#else
  base::FilePath path;
#endif
  path = path.Append(kChromeConfigDir);
  path = path.Append(kTraceConfigFileName);
  return path;
}

} // namespace

void EnableStartupTracingIfConfigFileExists() {
  base::FilePath trace_config_file_path = GetTraceConfigFilePath();
  if (!base::PathExists(trace_config_file_path))
    return;

  std::string trace_config_str;
  if (!base::ReadFileToString(trace_config_file_path,
                              &trace_config_str,
                              kTraceConfigFileSizeLimit)) {
    return;
  }

  base::trace_event::TraceConfig trace_config(trace_config_str);
  base::trace_event::TraceLog::GetInstance()->SetEnabled(
      trace_config, base::trace_event::TraceLog::RECORDING_MODE);
}

}  // namespace tracing