Revert "Only store leading 13 bits of password hash."
[chromium-blink-merge.git] / chrome / common / crash_keys.cc
blob1b9d8ef855665b6b19a309fd100e952bf6e78229
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"
15 #if defined(OS_MACOSX)
16 #include "breakpad/src/common/simple_string_dictionary.h"
17 #elif defined(OS_WIN)
18 #include "breakpad/src/client/windows/common/ipc_protocol.h"
19 #elif defined(OS_CHROMEOS)
20 #include "chrome/common/chrome_switches.h"
21 #include "gpu/command_buffer/service/gpu_switches.h"
22 #include "ui/gl/gl_switches.h"
23 #endif
25 namespace crash_keys {
27 // A small crash key, guaranteed to never be split into multiple pieces.
28 const size_t kSmallSize = 63;
30 // A medium crash key, which will be chunked on certain platforms but not
31 // others. Guaranteed to never be more than four chunks.
32 const size_t kMediumSize = kSmallSize * 4;
34 // A large crash key, which will be chunked on all platforms. This should be
35 // used sparingly.
36 const size_t kLargeSize = kSmallSize * 16;
38 // The maximum lengths specified by breakpad include the trailing NULL, so
39 // the actual length of the string is one less.
40 #if defined(OS_MACOSX)
41 static const size_t kSingleChunkLength =
42 google_breakpad::SimpleStringDictionary::value_size - 1;
43 #elif defined(OS_WIN)
44 static const size_t kSingleChunkLength =
45 google_breakpad::CustomInfoEntry::kValueMaxLength - 1;
46 #else
47 static const size_t kSingleChunkLength = 63;
48 #endif
50 // Guarantees for crash key sizes.
51 static_assert(kSmallSize <= kSingleChunkLength,
52 "crash key chunk size too small");
53 #if defined(OS_MACOSX)
54 static_assert(kMediumSize <= kSingleChunkLength,
55 "mac has medium size crash key chunks");
56 #endif
58 const char kClientId[] = "guid";
60 const char kChannel[] = "channel";
62 const char kActiveURL[] = "url-chunk";
64 const char kFontKeyName[] = "font_key_name";
66 const char kSwitch[] = "switch-%" PRIuS;
67 const char kNumSwitches[] = "num-switches";
69 const char kNumVariations[] = "num-experiments";
70 const char kVariations[] = "variations";
72 const char kExtensionID[] = "extension-%" PRIuS;
73 const char kNumExtensionsCount[] = "num-extensions";
75 const char kShutdownType[] = "shutdown-type";
77 #if !defined(OS_ANDROID)
78 const char kGPUVendorID[] = "gpu-venid";
79 const char kGPUDeviceID[] = "gpu-devid";
80 #endif
81 const char kGPUDriverVersion[] = "gpu-driver";
82 const char kGPUPixelShaderVersion[] = "gpu-psver";
83 const char kGPUVertexShaderVersion[] = "gpu-vsver";
84 #if defined(OS_MACOSX)
85 const char kGPUGLVersion[] = "gpu-glver";
86 #elif defined(OS_POSIX)
87 const char kGPUVendor[] = "gpu-gl-vendor";
88 const char kGPURenderer[] = "gpu-gl-renderer";
89 #endif
91 const char kPrinterInfo[] = "prn-info-%" PRIuS;
93 #if defined(OS_CHROMEOS)
94 const char kNumberOfUsers[] = "num-users";
95 #endif
97 #if defined(OS_MACOSX)
98 namespace mac {
100 const char kFirstNSException[] = "firstexception";
101 const char kFirstNSExceptionTrace[] = "firstexception_bt";
103 const char kLastNSException[] = "lastexception";
104 const char kLastNSExceptionTrace[] = "lastexception_bt";
106 const char kNSException[] = "nsexception";
107 const char kNSExceptionTrace[] = "nsexception_bt";
109 const char kSendAction[] = "sendaction";
111 const char kZombie[] = "zombie";
112 const char kZombieTrace[] = "zombie_dealloc_bt";
114 } // namespace mac
115 #endif
117 size_t RegisterChromeCrashKeys() {
118 // The following keys may be chunked by the underlying crash logging system,
119 // but ultimately constitute a single key-value pair.
120 base::debug::CrashKey fixed_keys[] = {
121 { kClientId, kSmallSize },
122 { kChannel, kSmallSize },
123 { kActiveURL, kLargeSize },
124 { kNumSwitches, kSmallSize },
125 { kNumVariations, kSmallSize },
126 { kVariations, kLargeSize },
127 { kNumExtensionsCount, kSmallSize },
128 { kShutdownType, kSmallSize },
129 #if !defined(OS_ANDROID)
130 { kGPUVendorID, kSmallSize },
131 { kGPUDeviceID, kSmallSize },
132 #endif
133 { kGPUDriverVersion, kSmallSize },
134 { kGPUPixelShaderVersion, kSmallSize },
135 { kGPUVertexShaderVersion, kSmallSize },
136 #if defined(OS_MACOSX)
137 { kGPUGLVersion, kSmallSize },
138 #elif defined(OS_POSIX)
139 { kGPUVendor, kSmallSize },
140 { kGPURenderer, kSmallSize },
141 #endif
143 // base/:
144 { "dm-usage", kSmallSize },
145 { "total-dm-usage", kSmallSize },
146 // content/:
147 { kFontKeyName, kSmallSize},
148 { "ppapi_path", kMediumSize },
149 { "subresource_url", kLargeSize },
150 #if defined(OS_CHROMEOS)
151 { kNumberOfUsers, kSmallSize },
152 #endif
153 #if defined(OS_MACOSX)
154 { mac::kFirstNSException, kMediumSize },
155 { mac::kFirstNSExceptionTrace, kMediumSize },
156 { mac::kLastNSException, kMediumSize },
157 { mac::kLastNSExceptionTrace, kMediumSize },
158 { mac::kNSException, kMediumSize },
159 { mac::kNSExceptionTrace, kMediumSize },
160 { mac::kSendAction, kMediumSize },
161 { mac::kZombie, kMediumSize },
162 { mac::kZombieTrace, kMediumSize },
163 // content/:
164 { "channel_error_bt", kMediumSize },
165 { "remove_route_bt", kMediumSize },
166 { "rwhvm_window", kMediumSize },
167 // media/:
168 { "VideoCaptureDeviceQTKit", kSmallSize },
169 #endif
172 // This dynamic set of keys is used for sets of key value pairs when gathering
173 // a collection of data, like command line switches or extension IDs.
174 std::vector<base::debug::CrashKey> keys(
175 fixed_keys, fixed_keys + arraysize(fixed_keys));
177 // Register the switches.
179 // The fixed_keys names are string constants. Use static storage for
180 // formatted key names as well, since they will persist for the duration of
181 // the program.
182 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitch) + 1] =
183 {{ 0 }};
184 const size_t formatted_key_len = sizeof(formatted_keys[0]);
185 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
186 // Name the keys using 1-based indexing.
187 int n = base::snprintf(
188 formatted_keys[i], formatted_key_len, kSwitch, i + 1);
189 DCHECK_GT(n, 0);
190 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
191 keys.push_back(crash_key);
195 // Register the extension IDs.
197 static char formatted_keys[kExtensionIDMaxCount][sizeof(kExtensionID) + 1] =
198 {{ 0 }};
199 const size_t formatted_key_len = sizeof(formatted_keys[0]);
200 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
201 int n = base::snprintf(
202 formatted_keys[i], formatted_key_len, kExtensionID, i + 1);
203 DCHECK_GT(n, 0);
204 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
205 keys.push_back(crash_key);
209 // Register the printer info.
211 static char formatted_keys[kPrinterInfoCount][sizeof(kPrinterInfo) + 1] =
212 {{ 0 }};
213 const size_t formatted_key_len = sizeof(formatted_keys[0]);
214 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
215 // Key names are 1-indexed.
216 int n = base::snprintf(
217 formatted_keys[i], formatted_key_len, kPrinterInfo, i + 1);
218 DCHECK_GT(n, 0);
219 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
220 keys.push_back(crash_key);
224 return base::debug::InitCrashKeys(&keys.at(0), keys.size(),
225 kSingleChunkLength);
228 void SetCrashClientIdFromGUID(const std::string& client_guid) {
229 std::string stripped_guid(client_guid);
230 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
231 ReplaceSubstringsAfterOffset(&stripped_guid, 0, "-", "");
232 if (stripped_guid.empty())
233 return;
235 base::debug::SetCrashKeyValue(kClientId, stripped_guid);
238 static bool IsBoringSwitch(const std::string& flag) {
239 #if defined(OS_WIN)
240 return StartsWithASCII(flag, "--channel=", true) ||
242 // No point to including this since we already have a ptype field.
243 StartsWithASCII(flag, "--type=", true) ||
245 // Not particularly interesting
246 StartsWithASCII(flag, "--flash-broker=", true) ||
248 // Just about everything has this, don't bother.
249 StartsWithASCII(flag, "/prefetch:", true) ||
251 // We handle the plugin path separately since it is usually too big
252 // to fit in the switches (limited to 63 characters).
253 StartsWithASCII(flag, "--plugin-path=", true) ||
255 // This is too big so we end up truncating it anyway.
256 StartsWithASCII(flag, "--force-fieldtrials=", true) ||
258 // These surround the flags that were added by about:flags, it lets
259 // you distinguish which flags were added manually via the command
260 // line versus those added through about:flags. For the most part
261 // we don't care how an option was enabled, so we strip these.
262 // (If you need to know can always look at the PEB).
263 flag == "--flag-switches-begin" ||
264 flag == "--flag-switches-end";
265 #elif defined(OS_CHROMEOS)
266 static const char* const kIgnoreSwitches[] = {
267 ::switches::kEnableLogging,
268 ::switches::kFlagSwitchesBegin,
269 ::switches::kFlagSwitchesEnd,
270 ::switches::kLoggingLevel,
271 ::switches::kPpapiFlashArgs,
272 ::switches::kPpapiFlashPath,
273 ::switches::kRegisterPepperPlugins,
274 ::switches::kUIPrioritizeInGpuProcess,
275 ::switches::kUseGL,
276 ::switches::kUserDataDir,
277 ::switches::kV,
278 ::switches::kVModule,
279 // Cros/CC flgas are specified as raw strings to avoid dependency.
280 "child-wallpaper-large",
281 "child-wallpaper-small",
282 "default-wallpaper-large",
283 "default-wallpaper-small",
284 "guest-wallpaper-large",
285 "guest-wallpaper-small",
286 "enterprise-enable-forced-re-enrollment",
287 "enterprise-enrollment-initial-modulus",
288 "enterprise-enrollment-modulus-limit",
289 "login-profile",
290 "login-user",
291 "max-tiles-for-interest-area",
292 "max-unused-resource-memory-usage-percentage",
293 "termination-message-file",
294 "use-cras",
296 if (!StartsWithASCII(flag, "--", true))
297 return false;
298 std::size_t end = flag.find("=");
299 int len = (end == std::string::npos) ? flag.length() - 2 : end - 2;
300 for (size_t i = 0; i < arraysize(kIgnoreSwitches); ++i) {
301 if (flag.compare(2, len, kIgnoreSwitches[i]) == 0)
302 return true;
304 return false;
305 #else
306 return false;
307 #endif
310 void SetSwitchesFromCommandLine(const base::CommandLine* command_line) {
311 DCHECK(command_line);
312 if (!command_line)
313 return;
315 const base::CommandLine::StringVector& argv = command_line->argv();
317 // Set the number of switches in case size > kNumSwitches.
318 base::debug::SetCrashKeyValue(kNumSwitches,
319 base::StringPrintf("%" PRIuS, argv.size() - 1));
321 size_t key_i = 1; // Key names are 1-indexed.
323 // Go through the argv, skipping the exec path.
324 for (size_t i = 1; i < argv.size(); ++i) {
325 #if defined(OS_WIN)
326 std::string switch_str = base::WideToUTF8(argv[i]);
327 #else
328 std::string switch_str = argv[i];
329 #endif
331 // Skip uninteresting switches.
332 if (IsBoringSwitch(switch_str))
333 continue;
335 // Stop if there are too many switches.
336 if (i > crash_keys::kSwitchesMaxCount)
337 break;
339 std::string key = base::StringPrintf(kSwitch, key_i++);
340 base::debug::SetCrashKeyValue(key, switch_str);
343 // Clear any remaining switches.
344 for (; key_i <= kSwitchesMaxCount; ++key_i) {
345 base::debug::ClearCrashKey(base::StringPrintf(kSwitch, key_i));
349 void SetVariationsList(const std::vector<std::string>& variations) {
350 base::debug::SetCrashKeyValue(kNumVariations,
351 base::StringPrintf("%" PRIuS, variations.size()));
353 std::string variations_string;
354 variations_string.reserve(kLargeSize);
356 for (size_t i = 0; i < variations.size(); ++i) {
357 const std::string& variation = variations[i];
358 // Do not truncate an individual experiment.
359 if (variations_string.size() + variation.size() >= kLargeSize)
360 break;
361 variations_string += variation;
362 variations_string += ",";
365 base::debug::SetCrashKeyValue(kVariations, variations_string);
368 void SetActiveExtensions(const std::set<std::string>& extensions) {
369 base::debug::SetCrashKeyValue(kNumExtensionsCount,
370 base::StringPrintf("%" PRIuS, extensions.size()));
372 std::set<std::string>::const_iterator it = extensions.begin();
373 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
374 std::string key = base::StringPrintf(kExtensionID, i + 1);
375 if (it == extensions.end()) {
376 base::debug::ClearCrashKey(key);
377 } else {
378 base::debug::SetCrashKeyValue(key, *it);
379 ++it;
384 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece& data) {
385 std::vector<std::string> info;
386 base::SplitString(data.as_string(), ';', &info);
387 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
388 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
389 std::string value;
390 if (i < info.size())
391 value = info[i];
392 base::debug::SetCrashKeyValue(key, value);
396 ScopedPrinterInfo::~ScopedPrinterInfo() {
397 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
398 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
399 base::debug::ClearCrashKey(key);
403 } // namespace crash_keys