| 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" |
| 7 #include "base/command_line.h" | 8 #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" |
| 8 #include "build/build_config.h" | 18 #include "build/build_config.h" |
| 19 #include "chrome/common/chrome_switches.h" |
| 20 #include "chrome/profiling/profiling_globals.h" |
| 21 #include "mojo/edk/embedder/embedder.h" |
| 22 #include "mojo/edk/embedder/scoped_ipc_support.h" |
| 9 | 23 |
| 10 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
| 11 #include "base/win/win_util.h" | 25 #include "base/win/win_util.h" |
| 12 #endif | 26 #endif |
| 13 | 27 |
| 14 namespace profiling { | 28 namespace profiling { |
| 15 | 29 |
| 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 |
| 16 int ProfilingMain(const base::CommandLine& cmdline) { | 89 int ProfilingMain(const base::CommandLine& cmdline) { |
| 17 // TODO(brettw) implement this. | 90 ProfilingGlobals* globals = ProfilingGlobals::Get(); |
| 91 |
| 92 mojo::edk::Init(); |
| 93 mojo::edk::ScopedIPCSupport ipc_support( |
| 94 globals->GetIORunner(), |
| 95 mojo::edk::ScopedIPCSupport::ShutdownPolicy::CLEAN); |
| 96 |
| 97 base::Process process = base::Process::Current(); |
| 98 std::string pipe_id = base::IntToString(static_cast<int>(process.Pid())); |
| 99 |
| 100 if (!LaunchBrowser(cmdline, pipe_id)) |
| 101 return 1; |
| 102 |
| 103 ProfilingGlobals::Get()->RunMainMessageLoop(); |
| 18 | 104 |
| 19 #if defined(OS_WIN) | 105 #if defined(OS_WIN) |
| 20 base::win::SetShouldCrashOnProcessDetach(false); | 106 base::win::SetShouldCrashOnProcessDetach(false); |
| 21 #endif | 107 #endif |
| 22 return 0; | 108 return 0; |
| 23 } | 109 } |
| 24 | 110 |
| 25 } // namespace profiling | 111 } // namespace profiling |
| OLD | NEW |