1 // Copyright 2013 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // TODO(ananta/scottmg)
6 // Add test coverage for Crashpad.
7 #include "chrome/app/chrome_crash_reporter_client_win.h"
19 #include "base/command_line.h"
20 #include "base/debug/leak_annotations.h"
21 #include "base/files/file_path.h"
22 #include "base/format_macros.h"
23 #include "base/notreached.h"
24 #include "base/rand_util.h"
25 #include "base/strings/utf_string_conversions.h"
26 #include "base/win/current_module.h"
27 #include "chrome/chrome_elf/chrome_elf_constants.h"
28 #include "chrome/common/chrome_result_codes.h"
29 #include "chrome/install_static/install_util.h"
30 #include "chrome/install_static/user_data_dir.h"
31 #include "components/crash/core/app/crashpad.h"
32 #include "components/version_info/channel.h"
34 ChromeCrashReporterClient::ChromeCrashReporterClient() {}
36 ChromeCrashReporterClient::~ChromeCrashReporterClient() {}
38 #if !defined(NACL_WIN64)
40 void ChromeCrashReporterClient::InitializeCrashReportingForProcess() {
41 static ChromeCrashReporterClient* instance = nullptr;
45 instance = new ChromeCrashReporterClient();
46 ANNOTATE_LEAKING_OBJECT_PTR(instance);
48 std::wstring process_type = install_static::GetSwitchValueFromCommandLine(
49 ::GetCommandLine(), install_static::kProcessType);
50 // Don't set up Crashpad crash reporting in the Crashpad handler itself, nor
51 // in the fallback crash handler for the Crashpad handler process.
52 if (process_type != install_static::kCrashpadHandler &&
53 process_type != install_static::kFallbackHandler) {
54 crash_reporter::SetCrashReporterClient(instance);
56 std::wstring user_data_dir;
57 if (process_type.empty())
58 install_static::GetUserDataDirectory(&user_data_dir, nullptr);
60 // TODO(wfh): Add a DCHECK for success. See https://crbug.com/1329269.
61 std::ignore = crash_reporter::InitializeCrashpadWithEmbeddedHandler(
62 /*initial_client=*/process_type.empty(),
63 install_static::WideToUTF8(process_type),
64 install_static::WideToUTF8(user_data_dir), base::FilePath());
67 #endif // !defined(NACL_WIN64)
69 bool ChromeCrashReporterClient::GetAlternativeCrashDumpLocation(
70 std::wstring* crash_dir) {
71 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
72 // location to write breakpad crash dumps can be set.
73 *crash_dir = install_static::GetEnvironmentString(L"BREAKPAD_DUMP_LOCATION");
74 return !crash_dir->empty();
77 void ChromeCrashReporterClient::GetProductNameAndVersion(
78 const std::wstring& exe_path,
79 std::wstring* product_name,
80 std::wstring* version,
81 std::wstring* special_build,
82 std::wstring* channel_name) {
85 assert(special_build);
88 install_static::GetExecutableVersionDetails(exe_path, product_name, version,
89 special_build, channel_name);
92 bool ChromeCrashReporterClient::ShouldShowRestartDialog(std::wstring* title,
93 std::wstring* message,
94 bool* is_rtl_locale) {
95 if (!install_static::HasEnvironmentVariable(install_static::kShowRestart) ||
96 !install_static::HasEnvironmentVariable(install_static::kRestartInfo)) {
100 std::wstring restart_info =
101 install_static::GetEnvironmentString(install_static::kRestartInfo);
103 // The CHROME_RESTART var contains the dialog strings separated by '|'.
104 // See ChromeBrowserMainPartsWin::PrepareRestartOnCrashEnviroment()
106 std::vector<std::wstring> dlg_strings = install_static::TokenizeString(
107 restart_info, L'|', true); // true = Trim whitespace.
109 if (dlg_strings.size() < 3)
112 *title = dlg_strings[0];
113 *message = dlg_strings[1];
114 *is_rtl_locale = dlg_strings[2] == install_static::kRtlLocale;
118 bool ChromeCrashReporterClient::AboutToRestart() {
119 if (!install_static::HasEnvironmentVariable(install_static::kRestartInfo))
122 install_static::SetEnvironmentString(install_static::kShowRestart, L"1");
126 bool ChromeCrashReporterClient::GetIsPerUserInstall() {
127 return !install_static::IsSystemInstall();
130 bool ChromeCrashReporterClient::GetShouldDumpLargerDumps() {
131 // Capture larger dumps for Google Chrome beta, dev, and canary channels, and
132 // Chromium builds. The Google Chrome stable channel uses smaller dumps.
133 return install_static::GetChromeChannel() != version_info::Channel::STABLE;
136 int ChromeCrashReporterClient::GetResultCodeRespawnFailed() {
137 return chrome::RESULT_CODE_RESPAWN_FAILED;
140 bool ChromeCrashReporterClient::ReportingIsEnforcedByPolicy(
141 bool* crashpad_enabled) {
142 // Determine whether configuration management allows loading the crash
144 // Since the configuration management infrastructure is not initialized at
145 // this point, we read the corresponding registry key directly. The return
146 // status indicates whether policy data was successfully read. If it is true,
147 // |breakpad_enabled| contains the value set by policy.
148 return install_static::ReportingIsEnforcedByPolicy(crashpad_enabled);
151 bool ChromeCrashReporterClient::GetCrashDumpLocation(std::wstring* crash_dir) {
152 // By setting the BREAKPAD_DUMP_LOCATION environment variable, an alternate
153 // location to write breakpad crash dumps can be set.
154 // If this environment variable exists, then for the time being,
155 // short-circuit how it's handled on Windows. Honoring this
156 // variable is required in order to symbolize stack traces in
157 // Telemetry based tests: http://crbug.com/561763.
158 if (GetAlternativeCrashDumpLocation(crash_dir))
161 *crash_dir = install_static::GetCrashDumpLocation();
162 return !crash_dir->empty();
165 bool ChromeCrashReporterClient::GetCrashMetricsLocation(
166 std::wstring* metrics_dir) {
167 if (!GetCollectStatsConsent())
169 install_static::GetUserDataDirectory(metrics_dir, nullptr);
170 return !metrics_dir->empty();
173 bool ChromeCrashReporterClient::IsRunningUnattended() {
174 return install_static::HasEnvironmentVariable(install_static::kHeadless);
177 bool ChromeCrashReporterClient::GetCollectStatsConsent() {
178 return install_static::GetCollectStatsConsent();
181 bool ChromeCrashReporterClient::GetCollectStatsInSample() {
182 return install_static::GetCollectStatsInSample();
185 bool ChromeCrashReporterClient::ShouldMonitorCrashHandlerExpensively() {
186 // The expensive mechanism dedicates a process to be crashpad_handler's own
187 // crashpad_handler. In Google Chrome, scale back on this in the more stable
188 // channels. There's a fallback crash handler that can catch crashes when this
189 // expensive mechanism isn't used, although the fallback crash handler has
190 // different characteristics so it's desirable to use the expensive mechanism
191 // at least some of the time.
193 switch (install_static::GetChromeChannel()) {
194 case version_info::Channel::STABLE:
197 case version_info::Channel::BETA:
201 case version_info::Channel::DEV:
210 return base::RandDouble() < probability;
213 bool ChromeCrashReporterClient::EnableBreakpadForProcess(
214 const std::string& process_type) {
215 // This is not used by Crashpad (at least on Windows).
220 std::wstring ChromeCrashReporterClient::GetWerRuntimeExceptionModule() {
221 // We require that chrome_wer.dll is installed next to chrome_elf.dll. This
222 // approach means we don't need to check for the dll's existence on disk early
223 // in the process lifetime - we never load this dll ourselves - it is only
224 // loaded by WerFault.exe after certain crashes. We do not use base::FilePath
225 // and friends as chrome_elf will eventually not depend on //base.
227 wchar_t elf_file[MAX_PATH];
228 DWORD len = GetModuleFileName(CURRENT_MODULE(), elf_file, MAX_PATH);
229 // On error return an empty path to indicate than a module is not to be
230 // registered. This is harmless.
231 if (len == 0 || len == MAX_PATH)
232 return std::wstring();
234 wchar_t elf_dir[MAX_PATH];
235 wchar_t* file_start = nullptr;
236 DWORD dir_len = GetFullPathName(elf_file, MAX_PATH, elf_dir, &file_start);
237 if (dir_len == 0 || dir_len > len || !file_start)
238 return std::wstring();
240 // file_start points to the start of the filename in the elf_dir buffer.
241 return std::wstring(elf_dir, file_start).append(chrome::kWerDll);