1 // Copyright (c) 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "components/tracing/startup_tracing.h"
7 #include "base/files/file_path.h"
8 #include "base/files/file_util.h"
9 #include "base/path_service.h"
10 #include "base/trace_event/trace_event.h"
16 // Maximum trace config file size that will be loaded, in bytes.
17 const size_t kTraceConfigFileSizeLimit
= 64 * 1024;
19 // Trace config file path:
20 // - Android: /data/local/.config/chrome-trace-config.json
21 // - POSIX other than Android: $HOME/.config/chrome-trace-config.json
22 // - Win: %USERPROFILE%/.config/chrome-trace-config.json
23 #if defined(OS_ANDROID)
24 const base::FilePath::CharType kAndroidTraceConfigDir
[] =
25 FILE_PATH_LITERAL("/data/local");
28 const base::FilePath::CharType kChromeConfigDir
[] =
29 FILE_PATH_LITERAL(".config");
30 const base::FilePath::CharType kTraceConfigFileName
[] =
31 FILE_PATH_LITERAL("chrome-trace-config.json");
33 base::FilePath
GetTraceConfigFilePath() {
34 #if defined(OS_ANDROID)
35 base::FilePath
path(kAndroidTraceConfigDir
);
36 #elif defined(OS_POSIX) || defined(OS_WIN)
38 PathService::Get(base::DIR_HOME
, &path
);
42 path
= path
.Append(kChromeConfigDir
);
43 path
= path
.Append(kTraceConfigFileName
);
49 void EnableStartupTracingIfConfigFileExists() {
50 base::FilePath trace_config_file_path
= GetTraceConfigFilePath();
51 if (!base::PathExists(trace_config_file_path
))
54 std::string trace_config_str
;
55 if (!base::ReadFileToString(trace_config_file_path
,
57 kTraceConfigFileSizeLimit
)) {
61 base::trace_event::TraceConfig
trace_config(trace_config_str
);
62 base::trace_event::TraceLog::GetInstance()->SetEnabled(
63 trace_config
, base::trace_event::TraceLog::RECORDING_MODE
);
66 } // namespace tracing