ozone: evdev: Sync caps lock LED state to evdev
[chromium-blink-merge.git] / chrome / common / crash_keys.cc
blob6bc23204fc532406e050c951fe3e6cd69a879735
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 #if defined(SYZYASAN)
118 const char kKaskoGuid[] = "kasko-guid";
119 const char kKaskoEquivalentGuid[] = "kasko-equivalent-guid";
120 #endif
122 size_t RegisterChromeCrashKeys() {
123 // The following keys may be chunked by the underlying crash logging system,
124 // but ultimately constitute a single key-value pair.
125 base::debug::CrashKey fixed_keys[] = {
126 { kClientId, kSmallSize },
127 { kChannel, kSmallSize },
128 { kActiveURL, kLargeSize },
129 { kNumSwitches, kSmallSize },
130 { kNumVariations, kSmallSize },
131 { kVariations, kLargeSize },
132 { kNumExtensionsCount, kSmallSize },
133 { kShutdownType, kSmallSize },
134 #if !defined(OS_ANDROID)
135 { kGPUVendorID, kSmallSize },
136 { kGPUDeviceID, kSmallSize },
137 #endif
138 { kGPUDriverVersion, kSmallSize },
139 { kGPUPixelShaderVersion, kSmallSize },
140 { kGPUVertexShaderVersion, kSmallSize },
141 #if defined(OS_MACOSX)
142 { kGPUGLVersion, kSmallSize },
143 #elif defined(OS_POSIX)
144 { kGPUVendor, kSmallSize },
145 { kGPURenderer, kSmallSize },
146 #endif
148 // base/:
149 { "dm-usage", kSmallSize },
150 { "total-dm-usage", kSmallSize },
151 // content/:
152 { kFontKeyName, kSmallSize},
153 { "ppapi_path", kMediumSize },
154 { "subresource_url", kLargeSize },
155 #if defined(OS_CHROMEOS)
156 { kNumberOfUsers, kSmallSize },
157 #endif
158 #if defined(OS_MACOSX)
159 { mac::kFirstNSException, kMediumSize },
160 { mac::kFirstNSExceptionTrace, kMediumSize },
161 { mac::kLastNSException, kMediumSize },
162 { mac::kLastNSExceptionTrace, kMediumSize },
163 { mac::kNSException, kMediumSize },
164 { mac::kNSExceptionTrace, kMediumSize },
165 { mac::kSendAction, kMediumSize },
166 { mac::kZombie, kMediumSize },
167 { mac::kZombieTrace, kMediumSize },
168 // content/:
169 { "channel_error_bt", kMediumSize },
170 { "remove_route_bt", kMediumSize },
171 { "rwhvm_window", kMediumSize },
172 // media/:
173 { "VideoCaptureDeviceQTKit", kSmallSize },
174 #endif
175 #if defined(SYZYASAN)
176 { kKaskoGuid, kSmallSize },
177 { kKaskoEquivalentGuid, kSmallSize },
178 #endif
181 // This dynamic set of keys is used for sets of key value pairs when gathering
182 // a collection of data, like command line switches or extension IDs.
183 std::vector<base::debug::CrashKey> keys(
184 fixed_keys, fixed_keys + arraysize(fixed_keys));
186 // Register the switches.
188 // The fixed_keys names are string constants. Use static storage for
189 // formatted key names as well, since they will persist for the duration of
190 // the program.
191 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitch) + 1] =
192 {{ 0 }};
193 const size_t formatted_key_len = sizeof(formatted_keys[0]);
194 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
195 // Name the keys using 1-based indexing.
196 int n = base::snprintf(
197 formatted_keys[i], formatted_key_len, kSwitch, i + 1);
198 DCHECK_GT(n, 0);
199 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
200 keys.push_back(crash_key);
204 // Register the extension IDs.
206 static char formatted_keys[kExtensionIDMaxCount][sizeof(kExtensionID) + 1] =
207 {{ 0 }};
208 const size_t formatted_key_len = sizeof(formatted_keys[0]);
209 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
210 int n = base::snprintf(
211 formatted_keys[i], formatted_key_len, kExtensionID, i + 1);
212 DCHECK_GT(n, 0);
213 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
214 keys.push_back(crash_key);
218 // Register the printer info.
220 static char formatted_keys[kPrinterInfoCount][sizeof(kPrinterInfo) + 1] =
221 {{ 0 }};
222 const size_t formatted_key_len = sizeof(formatted_keys[0]);
223 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
224 // Key names are 1-indexed.
225 int n = base::snprintf(
226 formatted_keys[i], formatted_key_len, kPrinterInfo, i + 1);
227 DCHECK_GT(n, 0);
228 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
229 keys.push_back(crash_key);
233 return base::debug::InitCrashKeys(&keys.at(0), keys.size(),
234 kSingleChunkLength);
237 void SetCrashClientIdFromGUID(const std::string& client_guid) {
238 std::string stripped_guid(client_guid);
239 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
240 ReplaceSubstringsAfterOffset(&stripped_guid, 0, "-", "");
241 if (stripped_guid.empty())
242 return;
244 base::debug::SetCrashKeyValue(kClientId, stripped_guid);
247 static bool IsBoringSwitch(const std::string& flag) {
248 #if defined(OS_WIN)
249 return StartsWithASCII(flag, "--channel=", true) ||
251 // No point to including this since we already have a ptype field.
252 StartsWithASCII(flag, "--type=", true) ||
254 // Not particularly interesting
255 StartsWithASCII(flag, "--flash-broker=", true) ||
257 // Just about everything has this, don't bother.
258 StartsWithASCII(flag, "/prefetch:", true) ||
260 // We handle the plugin path separately since it is usually too big
261 // to fit in the switches (limited to 63 characters).
262 StartsWithASCII(flag, "--plugin-path=", true) ||
264 // This is too big so we end up truncating it anyway.
265 StartsWithASCII(flag, "--force-fieldtrials=", true) ||
267 // These surround the flags that were added by about:flags, it lets
268 // you distinguish which flags were added manually via the command
269 // line versus those added through about:flags. For the most part
270 // we don't care how an option was enabled, so we strip these.
271 // (If you need to know can always look at the PEB).
272 flag == "--flag-switches-begin" ||
273 flag == "--flag-switches-end";
274 #elif defined(OS_CHROMEOS)
275 static const char* const kIgnoreSwitches[] = {
276 ::switches::kEnableLogging,
277 ::switches::kFlagSwitchesBegin,
278 ::switches::kFlagSwitchesEnd,
279 ::switches::kLoggingLevel,
280 ::switches::kPpapiFlashArgs,
281 ::switches::kPpapiFlashPath,
282 ::switches::kRegisterPepperPlugins,
283 ::switches::kUIPrioritizeInGpuProcess,
284 ::switches::kUseGL,
285 ::switches::kUserDataDir,
286 ::switches::kV,
287 ::switches::kVModule,
288 // Cros/CC flgas are specified as raw strings to avoid dependency.
289 "child-wallpaper-large",
290 "child-wallpaper-small",
291 "default-wallpaper-large",
292 "default-wallpaper-small",
293 "guest-wallpaper-large",
294 "guest-wallpaper-small",
295 "enterprise-enable-forced-re-enrollment",
296 "enterprise-enrollment-initial-modulus",
297 "enterprise-enrollment-modulus-limit",
298 "login-profile",
299 "login-user",
300 "max-tiles-for-interest-area",
301 "max-unused-resource-memory-usage-percentage",
302 "termination-message-file",
303 "use-cras",
305 if (!StartsWithASCII(flag, "--", true))
306 return false;
307 std::size_t end = flag.find("=");
308 int len = (end == std::string::npos) ? flag.length() - 2 : end - 2;
309 for (size_t i = 0; i < arraysize(kIgnoreSwitches); ++i) {
310 if (flag.compare(2, len, kIgnoreSwitches[i]) == 0)
311 return true;
313 return false;
314 #else
315 return false;
316 #endif
319 void SetSwitchesFromCommandLine(const base::CommandLine* command_line) {
320 DCHECK(command_line);
321 if (!command_line)
322 return;
324 const base::CommandLine::StringVector& argv = command_line->argv();
326 // Set the number of switches in case size > kNumSwitches.
327 base::debug::SetCrashKeyValue(kNumSwitches,
328 base::StringPrintf("%" PRIuS, argv.size() - 1));
330 size_t key_i = 1; // Key names are 1-indexed.
332 // Go through the argv, skipping the exec path.
333 for (size_t i = 1; i < argv.size(); ++i) {
334 #if defined(OS_WIN)
335 std::string switch_str = base::WideToUTF8(argv[i]);
336 #else
337 std::string switch_str = argv[i];
338 #endif
340 // Skip uninteresting switches.
341 if (IsBoringSwitch(switch_str))
342 continue;
344 // Stop if there are too many switches.
345 if (i > crash_keys::kSwitchesMaxCount)
346 break;
348 std::string key = base::StringPrintf(kSwitch, key_i++);
349 base::debug::SetCrashKeyValue(key, switch_str);
352 // Clear any remaining switches.
353 for (; key_i <= kSwitchesMaxCount; ++key_i) {
354 base::debug::ClearCrashKey(base::StringPrintf(kSwitch, key_i));
358 void SetVariationsList(const std::vector<std::string>& variations) {
359 base::debug::SetCrashKeyValue(kNumVariations,
360 base::StringPrintf("%" PRIuS, variations.size()));
362 std::string variations_string;
363 variations_string.reserve(kLargeSize);
365 for (size_t i = 0; i < variations.size(); ++i) {
366 const std::string& variation = variations[i];
367 // Do not truncate an individual experiment.
368 if (variations_string.size() + variation.size() >= kLargeSize)
369 break;
370 variations_string += variation;
371 variations_string += ",";
374 base::debug::SetCrashKeyValue(kVariations, variations_string);
377 void SetActiveExtensions(const std::set<std::string>& extensions) {
378 base::debug::SetCrashKeyValue(kNumExtensionsCount,
379 base::StringPrintf("%" PRIuS, extensions.size()));
381 std::set<std::string>::const_iterator it = extensions.begin();
382 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
383 std::string key = base::StringPrintf(kExtensionID, i + 1);
384 if (it == extensions.end()) {
385 base::debug::ClearCrashKey(key);
386 } else {
387 base::debug::SetCrashKeyValue(key, *it);
388 ++it;
393 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece& data) {
394 std::vector<std::string> info;
395 base::SplitString(data.as_string(), ';', &info);
396 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
397 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
398 std::string value;
399 if (i < info.size())
400 value = info[i];
401 base::debug::SetCrashKeyValue(key, value);
405 ScopedPrinterInfo::~ScopedPrinterInfo() {
406 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
407 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
408 base::debug::ClearCrashKey(key);
412 } // namespace crash_keys