OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/profiling/profiling_main.h" | 5 #include "chrome/profiling/profiling_main.h" |
6 | 6 |
7 #include "base/base_paths.h" | |
8 #include "base/command_line.h" | 7 #include "base/command_line.h" |
9 #include "base/files/file_path.h" | |
10 #include "base/path_service.h" | |
11 #include "base/process/launch.h" | |
12 #include "base/process/process.h" | |
13 #include "base/process/process_metrics.h" | |
14 #include "base/strings/string_number_conversions.h" | |
15 #include "base/strings/string_util.h" | |
16 #include "base/strings/utf_string_conversions.h" | |
17 #include "base/third_party/dynamic_annotations/dynamic_annotations.h" | |
18 #include "build/build_config.h" | 8 #include "build/build_config.h" |
19 #include "chrome/common/chrome_switches.h" | 9 #include "chrome/common/chrome_switches.h" |
20 #include "chrome/profiling/profiling_globals.h" | 10 #include "chrome/profiling/profiling_globals.h" |
21 #include "mojo/edk/embedder/embedder.h" | 11 #include "mojo/edk/embedder/embedder.h" |
22 #include "mojo/edk/embedder/scoped_ipc_support.h" | 12 #include "mojo/edk/embedder/scoped_ipc_support.h" |
23 | 13 |
24 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
25 #include "base/win/win_util.h" | 15 #include "base/win/win_util.h" |
26 #endif | 16 #endif |
27 | 17 |
28 namespace profiling { | 18 namespace profiling { |
29 | 19 |
30 namespace { | |
31 | |
32 base::CommandLine MakeBrowserCommandLine(const base::CommandLine& cmdline, | |
33 const std::string& pipe_id) { | |
34 const base::CommandLine::StringVector& our_argv = cmdline.argv(); | |
35 | |
36 base::CommandLine::StringVector browser_argv; | |
37 browser_argv.reserve(our_argv.size()); | |
38 | |
39 // Program name. | |
40 base::FilePath child_path; | |
41 #if defined(OS_LINUX) | |
42 // Use /proc/self/exe rather than our known binary path so updates | |
43 // can't swap out the binary from underneath us. | |
44 // When running under Valgrind, forking /proc/self/exe ends up forking the | |
45 // Valgrind executable, which then crashes. However, it's almost safe to | |
46 // assume that the updates won't happen while testing with Valgrind tools. | |
47 if (!RunningOnValgrind()) | |
48 child_path = base::FilePath(base::kProcSelfExe); | |
49 #endif | |
50 | |
51 if (child_path.empty()) | |
52 base::PathService::Get(base::FILE_EXE, &child_path); | |
53 browser_argv.push_back(child_path.value()); // Program name. | |
54 | |
55 // Remove all memlog flags. | |
56 for (size_t i = 1; i < our_argv.size(); i++) { | |
57 if (!base::StartsWith(our_argv[i], FILE_PATH_LITERAL("--memlog"), | |
58 base::CompareCase::SENSITIVE)) | |
59 browser_argv.push_back(our_argv[i]); | |
60 } | |
61 | |
62 // Append the pipe ID. | |
63 std::string pipe_switch("--"); | |
64 pipe_switch.append(switches::kMemlogPipe); | |
65 pipe_switch.push_back('='); | |
66 pipe_switch.append(pipe_id); | |
67 #if defined(OS_WIN) | |
68 browser_argv.push_back(base::ASCIIToUTF16(pipe_switch)); | |
69 #else | |
70 browser_argv.push_back(pipe_switch); | |
71 #endif | |
72 | |
73 return base::CommandLine(browser_argv); | |
74 } | |
75 | |
76 bool LaunchBrowser(const base::CommandLine& our_cmdline, | |
77 const std::string& pipe_id) { | |
78 base::CommandLine browser_cmdline = | |
79 MakeBrowserCommandLine(our_cmdline, pipe_id); | |
80 | |
81 base::LaunchOptions options; | |
82 base::Process process = base::LaunchProcess(browser_cmdline, options); | |
83 | |
84 return true; | |
85 } | |
86 | |
87 } // namespace | |
88 | |
89 int ProfilingMain(const base::CommandLine& cmdline) { | 20 int ProfilingMain(const base::CommandLine& cmdline) { |
90 ProfilingGlobals* globals = ProfilingGlobals::Get(); | 21 ProfilingGlobals* globals = ProfilingGlobals::Get(); |
91 | 22 |
92 mojo::edk::Init(); | 23 mojo::edk::Init(); |
93 mojo::edk::ScopedIPCSupport ipc_support( | 24 mojo::edk::ScopedIPCSupport ipc_support( |
94 globals->GetIORunner(), | 25 globals->GetIORunner(), |
95 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN); | 26 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN); |
96 | 27 |
97 base::Process process = base::Process::Current(); | 28 std::string pipe_id = cmdline.GetSwitchValueASCII(switches::kMemlogPipe); |
98 std::string pipe_id = base::IntToString(static_cast<int>(process.Pid())); | |
99 | |
100 globals->GetMemlogConnectionManager()->StartConnections(pipe_id); | 29 globals->GetMemlogConnectionManager()->StartConnections(pipe_id); |
101 | 30 |
102 if (!LaunchBrowser(cmdline, pipe_id)) | |
103 return 1; | |
104 | |
105 ProfilingGlobals::Get()->RunMainMessageLoop(); | 31 ProfilingGlobals::Get()->RunMainMessageLoop(); |
106 | 32 |
107 #if defined(OS_WIN) | 33 #if defined(OS_WIN) |
108 base::win::SetShouldCrashOnProcessDetach(false); | 34 base::win::SetShouldCrashOnProcessDetach(false); |
109 #endif | 35 #endif |
110 return 0; | 36 return 0; |
111 } | 37 } |
112 | 38 |
113 } // namespace profiling | 39 } // namespace profiling |
OLD | NEW |