Update V8 to version 4.6.6.
[chromium-blink-merge.git] / chrome / common / crash_keys.cc
blob6e6daa4844a1e91e89b0620ccbaa46d0cd071040
1 // Copyright (c) 2013 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 "chrome/common/crash_keys.h"
7 #include "base/command_line.h"
8 #include "base/format_macros.h"
9 #include "base/logging.h"
10 #include "base/strings/string_split.h"
11 #include "base/strings/string_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
14 #include "chrome/common/chrome_switches.h"
15 #include "content/public/common/content_switches.h"
16 #include "ipc/ipc_switches.h"
18 #if defined(OS_MACOSX)
19 #include "breakpad/src/common/simple_string_dictionary.h"
20 #elif defined(OS_WIN)
21 #include "breakpad/src/client/windows/common/ipc_protocol.h"
22 #elif defined(OS_CHROMEOS)
23 #include "chrome/common/chrome_switches.h"
24 #include "gpu/command_buffer/service/gpu_switches.h"
25 #include "ui/gl/gl_switches.h"
26 #endif
28 namespace crash_keys {
30 // A small crash key, guaranteed to never be split into multiple pieces.
31 const size_t kSmallSize = 63;
33 // A medium crash key, which will be chunked on certain platforms but not
34 // others. Guaranteed to never be more than four chunks.
35 const size_t kMediumSize = kSmallSize * 4;
37 // A large crash key, which will be chunked on all platforms. This should be
38 // used sparingly.
39 const size_t kLargeSize = kSmallSize * 16;
41 // The maximum lengths specified by breakpad include the trailing NULL, so
42 // the actual length of the string is one less.
43 #if defined(OS_MACOSX)
44 static const size_t kSingleChunkLength =
45 google_breakpad::SimpleStringDictionary::value_size - 1;
46 #elif defined(OS_WIN)
47 static const size_t kSingleChunkLength =
48 google_breakpad::CustomInfoEntry::kValueMaxLength - 1;
49 #else
50 static const size_t kSingleChunkLength = 63;
51 #endif
53 // Guarantees for crash key sizes.
54 static_assert(kSmallSize <= kSingleChunkLength,
55 "crash key chunk size too small");
56 #if defined(OS_MACOSX)
57 static_assert(kMediumSize <= kSingleChunkLength,
58 "mac has medium size crash key chunks");
59 #endif
61 #if defined(OS_MACOSX)
62 // Crashpad owns the "guid" key. Chrome's metrics client ID is a separate ID
63 // carried in a distinct "metrics_client_id" field.
64 const char kMetricsClientId[] = "metrics_client_id";
65 #else
66 const char kClientId[] = "guid";
67 #endif
69 const char kChannel[] = "channel";
71 const char kActiveURL[] = "url-chunk";
73 const char kFontKeyName[] = "font_key_name";
75 const char kSwitch[] = "switch-%" PRIuS;
76 const char kNumSwitches[] = "num-switches";
78 const char kNumVariations[] = "num-experiments";
79 const char kVariations[] = "variations";
81 const char kExtensionID[] = "extension-%" PRIuS;
82 const char kNumExtensionsCount[] = "num-extensions";
84 const char kShutdownType[] = "shutdown-type";
86 #if !defined(OS_ANDROID)
87 const char kGPUVendorID[] = "gpu-venid";
88 const char kGPUDeviceID[] = "gpu-devid";
89 #endif
90 const char kGPUDriverVersion[] = "gpu-driver";
91 const char kGPUPixelShaderVersion[] = "gpu-psver";
92 const char kGPUVertexShaderVersion[] = "gpu-vsver";
93 #if defined(OS_MACOSX)
94 const char kGPUGLVersion[] = "gpu-glver";
95 #elif defined(OS_POSIX)
96 const char kGPUVendor[] = "gpu-gl-vendor";
97 const char kGPURenderer[] = "gpu-gl-renderer";
98 #endif
100 const char kPrinterInfo[] = "prn-info-%" PRIuS;
102 #if defined(OS_CHROMEOS)
103 const char kNumberOfUsers[] = "num-users";
104 #endif
106 #if defined(OS_MACOSX)
107 namespace mac {
109 const char kFirstNSException[] = "firstexception";
110 const char kFirstNSExceptionTrace[] = "firstexception_bt";
112 const char kLastNSException[] = "lastexception";
113 const char kLastNSExceptionTrace[] = "lastexception_bt";
115 const char kNSException[] = "nsexception";
116 const char kNSExceptionTrace[] = "nsexception_bt";
118 const char kSendAction[] = "sendaction";
120 const char kZombie[] = "zombie";
121 const char kZombieTrace[] = "zombie_dealloc_bt";
123 } // namespace mac
124 #endif
126 #if defined(KASKO)
127 const char kKaskoGuid[] = "kasko-guid";
128 const char kKaskoEquivalentGuid[] = "kasko-equivalent-guid";
129 #endif
131 // Used to help investigate bug 464926. NOTE: This value is defined multiple
132 // places in the codebase due to layering issues. DO NOT change the value here
133 // without changing it in all other places that it is defined in the codebase
134 // (search for |kBug464926CrashKey|).
135 const char kBug464926CrashKey[] = "bug-464926-info";
137 const char kViewCount[] = "view-count";
139 size_t RegisterChromeCrashKeys() {
140 // The following keys may be chunked by the underlying crash logging system,
141 // but ultimately constitute a single key-value pair.
142 base::debug::CrashKey fixed_keys[] = {
143 #if defined(OS_MACOSX)
144 { kMetricsClientId, kSmallSize },
145 #else
146 { kClientId, kSmallSize },
147 #endif
148 { kChannel, kSmallSize },
149 { kActiveURL, kLargeSize },
150 { kNumSwitches, kSmallSize },
151 { kNumVariations, kSmallSize },
152 { kVariations, kLargeSize },
153 { kNumExtensionsCount, kSmallSize },
154 { kShutdownType, kSmallSize },
155 #if !defined(OS_ANDROID)
156 { kGPUVendorID, kSmallSize },
157 { kGPUDeviceID, kSmallSize },
158 #endif
159 { kGPUDriverVersion, kSmallSize },
160 { kGPUPixelShaderVersion, kSmallSize },
161 { kGPUVertexShaderVersion, kSmallSize },
162 #if defined(OS_MACOSX)
163 { kGPUGLVersion, kSmallSize },
164 #elif defined(OS_POSIX)
165 { kGPUVendor, kSmallSize },
166 { kGPURenderer, kSmallSize },
167 #endif
169 // content/:
170 { "discardable-memory-allocated", kSmallSize },
171 { "discardable-memory-free", kSmallSize },
172 { kFontKeyName, kSmallSize},
173 { "ppapi_path", kMediumSize },
174 { "subresource_url", kLargeSize },
175 { "total-discardable-memory-allocated", kSmallSize },
176 #if defined(OS_CHROMEOS)
177 { kNumberOfUsers, kSmallSize },
178 #endif
179 #if defined(OS_MACOSX)
180 { mac::kFirstNSException, kMediumSize },
181 { mac::kFirstNSExceptionTrace, kMediumSize },
182 { mac::kLastNSException, kMediumSize },
183 { mac::kLastNSExceptionTrace, kMediumSize },
184 { mac::kNSException, kMediumSize },
185 { mac::kNSExceptionTrace, kMediumSize },
186 { mac::kSendAction, kMediumSize },
187 { mac::kZombie, kMediumSize },
188 { mac::kZombieTrace, kMediumSize },
189 // content/:
190 { "channel_error_bt", kMediumSize },
191 { "remove_route_bt", kMediumSize },
192 { "rwhvm_window", kMediumSize },
193 // media/:
194 { "VideoCaptureDeviceQTKit", kSmallSize },
195 #endif
196 #if defined(KASKO)
197 { kKaskoGuid, kSmallSize },
198 { kKaskoEquivalentGuid, kSmallSize },
199 #endif
200 { kBug464926CrashKey, kSmallSize },
201 { kViewCount, kSmallSize },
204 // This dynamic set of keys is used for sets of key value pairs when gathering
205 // a collection of data, like command line switches or extension IDs.
206 std::vector<base::debug::CrashKey> keys(
207 fixed_keys, fixed_keys + arraysize(fixed_keys));
209 // Register the switches.
211 // The fixed_keys names are string constants. Use static storage for
212 // formatted key names as well, since they will persist for the duration of
213 // the program.
214 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitch) + 1] =
215 {{ 0 }};
216 const size_t formatted_key_len = sizeof(formatted_keys[0]);
217 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
218 // Name the keys using 1-based indexing.
219 int n = base::snprintf(
220 formatted_keys[i], formatted_key_len, kSwitch, i + 1);
221 DCHECK_GT(n, 0);
222 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
223 keys.push_back(crash_key);
227 // Register the extension IDs.
229 static char formatted_keys[kExtensionIDMaxCount][sizeof(kExtensionID) + 1] =
230 {{ 0 }};
231 const size_t formatted_key_len = sizeof(formatted_keys[0]);
232 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
233 int n = base::snprintf(
234 formatted_keys[i], formatted_key_len, kExtensionID, i + 1);
235 DCHECK_GT(n, 0);
236 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
237 keys.push_back(crash_key);
241 // Register the printer info.
243 static char formatted_keys[kPrinterInfoCount][sizeof(kPrinterInfo) + 1] =
244 {{ 0 }};
245 const size_t formatted_key_len = sizeof(formatted_keys[0]);
246 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
247 // Key names are 1-indexed.
248 int n = base::snprintf(
249 formatted_keys[i], formatted_key_len, kPrinterInfo, i + 1);
250 DCHECK_GT(n, 0);
251 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
252 keys.push_back(crash_key);
256 return base::debug::InitCrashKeys(&keys.at(0), keys.size(),
257 kSingleChunkLength);
260 void SetMetricsClientIdFromGUID(const std::string& metrics_client_guid) {
261 std::string stripped_guid(metrics_client_guid);
262 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
263 base::ReplaceSubstringsAfterOffset(
264 &stripped_guid, 0, "-", base::StringPiece());
265 if (stripped_guid.empty())
266 return;
268 #if defined(OS_MACOSX)
269 // The crash client ID is maintained by Crashpad and is distinct from the
270 // metrics client ID, which is carried in its own key.
271 base::debug::SetCrashKeyValue(kMetricsClientId, stripped_guid);
272 #else
273 // The crash client ID is set by the application when Breakpad is in use.
274 // The same ID as the metrics client ID is used.
275 base::debug::SetCrashKeyValue(kClientId, stripped_guid);
276 #endif
279 void ClearMetricsClientId() {
280 #if defined(OS_MACOSX)
281 // Crashpad always monitors for crashes, but doesn't upload them when
282 // crash reporting is disabled. The preference to upload crash reports is
283 // linked to the preference for metrics reporting. When metrics reporting is
284 // disabled, don't put the metrics client ID into crash dumps. This way, crash
285 // reports that are saved but not uploaded will not have a metrics client ID
286 // from the time that metrics reporting was disabled even if they are uploaded
287 // by user action at a later date.
289 // Breakpad cannot be enabled or disabled without an application restart, and
290 // it needs to use the metrics client ID as its stable crash client ID, so
291 // leave its client ID intact even when metrics reporting is disabled while
292 // the application is running.
293 base::debug::ClearCrashKey(kMetricsClientId);
294 #endif
297 static bool IsBoringSwitch(const std::string& flag) {
298 static const char* const kIgnoreSwitches[] = {
299 switches::kEnableLogging,
300 switches::kFlagSwitchesBegin,
301 switches::kFlagSwitchesEnd,
302 switches::kLoggingLevel,
303 #if defined(OS_WIN)
304 // This file is linked into both chrome.dll and chrome.exe. However //ipc
305 // is only in the .dll, so this needs to be a literal rather than the
306 // constant.
307 "channel", // switches::kProcessChannelID
308 #else
309 switches::kProcessChannelID,
310 #endif
311 switches::kProcessType,
312 switches::kV,
313 switches::kVModule,
314 #if defined(OS_WIN)
315 switches::kForceFieldTrials,
316 switches::kPluginPath,
317 #elif defined(OS_MACOSX)
318 switches::kMetricsClientID,
319 #elif defined(OS_CHROMEOS)
320 switches::kPpapiFlashArgs,
321 switches::kPpapiFlashPath,
322 switches::kRegisterPepperPlugins,
323 switches::kUIPrioritizeInGpuProcess,
324 switches::kUseGL,
325 switches::kUserDataDir,
326 // Cros/CC flags are specified as raw strings to avoid dependency.
327 "child-wallpaper-large",
328 "child-wallpaper-small",
329 "default-wallpaper-large",
330 "default-wallpaper-small",
331 "guest-wallpaper-large",
332 "guest-wallpaper-small",
333 "enterprise-enable-forced-re-enrollment",
334 "enterprise-enrollment-initial-modulus",
335 "enterprise-enrollment-modulus-limit",
336 "login-profile",
337 "login-user",
338 "max-tiles-for-interest-area",
339 "max-unused-resource-memory-usage-percentage",
340 "termination-message-file",
341 "use-cras",
342 #endif
345 #if defined(OS_WIN)
346 // Just about everything has this, don't bother.
347 if (base::StartsWith(flag, "/prefetch:", base::CompareCase::SENSITIVE))
348 return true;
349 #endif
351 if (!base::StartsWith(flag, "--", base::CompareCase::SENSITIVE))
352 return false;
353 size_t end = flag.find("=");
354 size_t len = (end == std::string::npos) ? flag.length() - 2 : end - 2;
355 for (size_t i = 0; i < arraysize(kIgnoreSwitches); ++i) {
356 if (flag.compare(2, len, kIgnoreSwitches[i]) == 0)
357 return true;
359 return false;
362 void SetSwitchesFromCommandLine(const base::CommandLine* command_line) {
363 DCHECK(command_line);
364 if (!command_line)
365 return;
367 const base::CommandLine::StringVector& argv = command_line->argv();
369 // Set the number of switches in case size > kNumSwitches.
370 base::debug::SetCrashKeyValue(kNumSwitches,
371 base::StringPrintf("%" PRIuS, argv.size() - 1));
373 size_t key_i = 1; // Key names are 1-indexed.
375 // Go through the argv, skipping the exec path.
376 for (size_t i = 1; i < argv.size(); ++i) {
377 #if defined(OS_WIN)
378 std::string switch_str = base::WideToUTF8(argv[i]);
379 #else
380 std::string switch_str = argv[i];
381 #endif
383 // Skip uninteresting switches.
384 if (IsBoringSwitch(switch_str))
385 continue;
387 // Stop if there are too many switches.
388 if (i > crash_keys::kSwitchesMaxCount)
389 break;
391 std::string key = base::StringPrintf(kSwitch, key_i++);
392 base::debug::SetCrashKeyValue(key, switch_str);
395 // Clear any remaining switches.
396 for (; key_i <= kSwitchesMaxCount; ++key_i) {
397 base::debug::ClearCrashKey(base::StringPrintf(kSwitch, key_i));
401 void SetVariationsList(const std::vector<std::string>& variations) {
402 base::debug::SetCrashKeyValue(kNumVariations,
403 base::StringPrintf("%" PRIuS, variations.size()));
405 std::string variations_string;
406 variations_string.reserve(kLargeSize);
408 for (size_t i = 0; i < variations.size(); ++i) {
409 const std::string& variation = variations[i];
410 // Do not truncate an individual experiment.
411 if (variations_string.size() + variation.size() >= kLargeSize)
412 break;
413 variations_string += variation;
414 variations_string += ",";
417 base::debug::SetCrashKeyValue(kVariations, variations_string);
420 void SetActiveExtensions(const std::set<std::string>& extensions) {
421 base::debug::SetCrashKeyValue(kNumExtensionsCount,
422 base::StringPrintf("%" PRIuS, extensions.size()));
424 std::set<std::string>::const_iterator it = extensions.begin();
425 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
426 std::string key = base::StringPrintf(kExtensionID, i + 1);
427 if (it == extensions.end()) {
428 base::debug::ClearCrashKey(key);
429 } else {
430 base::debug::SetCrashKeyValue(key, *it);
431 ++it;
436 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece& data) {
437 std::vector<std::string> info;
438 base::SplitString(data.as_string(), ';', &info);
439 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
440 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
441 std::string value;
442 if (i < info.size())
443 value = info[i];
444 base::debug::SetCrashKeyValue(key, value);
448 ScopedPrinterInfo::~ScopedPrinterInfo() {
449 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
450 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
451 base::debug::ClearCrashKey(key);
455 } // namespace crash_keys