Explicitly add python-numpy dependency to install-build-deps.
[chromium-blink-merge.git] / chrome / common / crash_keys.cc
blobb96a3ac36c8c8f0112af854b54c242f0f70cb350
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 COMPILE_ASSERT(kSmallSize <= kSingleChunkLength,
52 crash_key_chunk_size_too_small);
53 #if defined(OS_MACOSX)
54 COMPILE_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 kNumberOfViews[] = "num-views";
77 const char kShutdownType[] = "shutdown-type";
79 #if !defined(OS_ANDROID)
80 const char kGPUVendorID[] = "gpu-venid";
81 const char kGPUDeviceID[] = "gpu-devid";
82 #endif
83 const char kGPUDriverVersion[] = "gpu-driver";
84 const char kGPUPixelShaderVersion[] = "gpu-psver";
85 const char kGPUVertexShaderVersion[] = "gpu-vsver";
86 #if defined(OS_MACOSX)
87 const char kGPUGLVersion[] = "gpu-glver";
88 #elif defined(OS_POSIX)
89 const char kGPUVendor[] = "gpu-gl-vendor";
90 const char kGPURenderer[] = "gpu-gl-renderer";
91 #endif
93 const char kPrinterInfo[] = "prn-info-%" PRIuS;
95 #if defined(OS_CHROMEOS)
96 const char kNumberOfUsers[] = "num-users";
97 #endif
99 #if defined(OS_MACOSX)
100 namespace mac {
102 const char kFirstNSException[] = "firstexception";
103 const char kFirstNSExceptionTrace[] = "firstexception_bt";
105 const char kLastNSException[] = "lastexception";
106 const char kLastNSExceptionTrace[] = "lastexception_bt";
108 const char kNSException[] = "nsexception";
109 const char kNSExceptionTrace[] = "nsexception_bt";
111 const char kSendAction[] = "sendaction";
113 const char kZombie[] = "zombie";
114 const char kZombieTrace[] = "zombie_dealloc_bt";
116 } // namespace mac
117 #endif
119 size_t RegisterChromeCrashKeys() {
120 // The following keys may be chunked by the underlying crash logging system,
121 // but ultimately constitute a single key-value pair.
122 base::debug::CrashKey fixed_keys[] = {
123 { kClientId, kSmallSize },
124 { kChannel, kSmallSize },
125 { kActiveURL, kLargeSize },
126 { kNumSwitches, kSmallSize },
127 { kNumVariations, kSmallSize },
128 { kVariations, kLargeSize },
129 { kNumExtensionsCount, kSmallSize },
130 { kNumberOfViews, kSmallSize },
131 { kShutdownType, kSmallSize },
132 #if !defined(OS_ANDROID)
133 { kGPUVendorID, kSmallSize },
134 { kGPUDeviceID, kSmallSize },
135 #endif
136 { kGPUDriverVersion, kSmallSize },
137 { kGPUPixelShaderVersion, kSmallSize },
138 { kGPUVertexShaderVersion, kSmallSize },
139 #if defined(OS_MACOSX)
140 { kGPUGLVersion, kSmallSize },
141 #elif defined(OS_POSIX)
142 { kGPUVendor, kSmallSize },
143 { kGPURenderer, kSmallSize },
144 #endif
146 // base/:
147 { "dm-usage", kSmallSize },
148 { "total-dm-usage", kSmallSize },
149 // content/:
150 { kFontKeyName, kSmallSize},
151 { "ppapi_path", kMediumSize },
152 { "subresource_url", kLargeSize },
153 #if defined(OS_CHROMEOS)
154 { kNumberOfUsers, kSmallSize },
155 #endif
156 #if defined(OS_MACOSX)
157 { mac::kFirstNSException, kMediumSize },
158 { mac::kFirstNSExceptionTrace, kMediumSize },
159 { mac::kLastNSException, kMediumSize },
160 { mac::kLastNSExceptionTrace, kMediumSize },
161 { mac::kNSException, kMediumSize },
162 { mac::kNSExceptionTrace, kMediumSize },
163 { mac::kSendAction, kMediumSize },
164 { mac::kZombie, kMediumSize },
165 { mac::kZombieTrace, kMediumSize },
166 // content/:
167 { "channel_error_bt", kMediumSize },
168 { "remove_route_bt", kMediumSize },
169 { "rwhvm_window", kMediumSize },
170 // media/:
171 { "VideoCaptureDeviceQTKit", kSmallSize },
172 #endif
175 // This dynamic set of keys is used for sets of key value pairs when gathering
176 // a collection of data, like command line switches or extension IDs.
177 std::vector<base::debug::CrashKey> keys(
178 fixed_keys, fixed_keys + arraysize(fixed_keys));
180 // Register the switches.
182 // The fixed_keys names are string constants. Use static storage for
183 // formatted key names as well, since they will persist for the duration of
184 // the program.
185 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitch) + 1] =
186 {{ 0 }};
187 const size_t formatted_key_len = sizeof(formatted_keys[0]);
188 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
189 // Name the keys using 1-based indexing.
190 int n = base::snprintf(
191 formatted_keys[i], formatted_key_len, kSwitch, i + 1);
192 DCHECK_GT(n, 0);
193 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
194 keys.push_back(crash_key);
198 // Register the extension IDs.
200 static char formatted_keys[kExtensionIDMaxCount][sizeof(kExtensionID) + 1] =
201 {{ 0 }};
202 const size_t formatted_key_len = sizeof(formatted_keys[0]);
203 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
204 int n = base::snprintf(
205 formatted_keys[i], formatted_key_len, kExtensionID, i + 1);
206 DCHECK_GT(n, 0);
207 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
208 keys.push_back(crash_key);
212 // Register the printer info.
214 static char formatted_keys[kPrinterInfoCount][sizeof(kPrinterInfo) + 1] =
215 {{ 0 }};
216 const size_t formatted_key_len = sizeof(formatted_keys[0]);
217 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
218 // Key names are 1-indexed.
219 int n = base::snprintf(
220 formatted_keys[i], formatted_key_len, kPrinterInfo, i + 1);
221 DCHECK_GT(n, 0);
222 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
223 keys.push_back(crash_key);
227 return base::debug::InitCrashKeys(&keys.at(0), keys.size(),
228 kSingleChunkLength);
231 void SetCrashClientIdFromGUID(const std::string& client_guid) {
232 std::string stripped_guid(client_guid);
233 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
234 ReplaceSubstringsAfterOffset(&stripped_guid, 0, "-", "");
235 if (stripped_guid.empty())
236 return;
238 base::debug::SetCrashKeyValue(kClientId, stripped_guid);
241 static bool IsBoringSwitch(const std::string& flag) {
242 #if defined(OS_WIN)
243 return StartsWithASCII(flag, "--channel=", true) ||
245 // No point to including this since we already have a ptype field.
246 StartsWithASCII(flag, "--type=", true) ||
248 // Not particularly interesting
249 StartsWithASCII(flag, "--flash-broker=", true) ||
251 // Just about everything has this, don't bother.
252 StartsWithASCII(flag, "/prefetch:", true) ||
254 // We handle the plugin path separately since it is usually too big
255 // to fit in the switches (limited to 63 characters).
256 StartsWithASCII(flag, "--plugin-path=", true) ||
258 // This is too big so we end up truncating it anyway.
259 StartsWithASCII(flag, "--force-fieldtrials=", true) ||
261 // These surround the flags that were added by about:flags, it lets
262 // you distinguish which flags were added manually via the command
263 // line versus those added through about:flags. For the most part
264 // we don't care how an option was enabled, so we strip these.
265 // (If you need to know can always look at the PEB).
266 flag == "--flag-switches-begin" ||
267 flag == "--flag-switches-end";
268 #elif defined(OS_CHROMEOS)
269 static const char* const kIgnoreSwitches[] = {
270 ::switches::kEnableImplSidePainting,
271 ::switches::kEnableLogging,
272 ::switches::kFlagSwitchesBegin,
273 ::switches::kFlagSwitchesEnd,
274 ::switches::kLoggingLevel,
275 ::switches::kPpapiFlashArgs,
276 ::switches::kPpapiFlashPath,
277 ::switches::kRegisterPepperPlugins,
278 ::switches::kUIPrioritizeInGpuProcess,
279 ::switches::kUseGL,
280 ::switches::kUserDataDir,
281 ::switches::kV,
282 ::switches::kVModule,
283 // Cros/CC flgas are specified as raw strings to avoid dependency.
284 "ash-default-wallpaper-large",
285 "ash-default-wallpaper-small",
286 "ash-guest-wallpaper-large",
287 "ash-guest-wallpaper-small",
288 "enterprise-enable-forced-re-enrollment",
289 "enterprise-enrollment-initial-modulus",
290 "enterprise-enrollment-modulus-limit",
291 "login-profile",
292 "login-user",
293 "max-tiles-for-interest-area",
294 "max-unused-resource-memory-usage-percentage",
295 "termination-message-file",
296 "use-cras",
298 if (!StartsWithASCII(flag, "--", true))
299 return false;
300 std::size_t end = flag.find("=");
301 int len = (end == std::string::npos) ? flag.length() - 2 : end - 2;
302 for (size_t i = 0; i < arraysize(kIgnoreSwitches); ++i) {
303 if (flag.compare(2, len, kIgnoreSwitches[i]) == 0)
304 return true;
306 return false;
307 #else
308 return false;
309 #endif
312 void SetSwitchesFromCommandLine(const CommandLine* command_line) {
313 DCHECK(command_line);
314 if (!command_line)
315 return;
317 const CommandLine::StringVector& argv = command_line->argv();
319 // Set the number of switches in case size > kNumSwitches.
320 base::debug::SetCrashKeyValue(kNumSwitches,
321 base::StringPrintf("%" PRIuS, argv.size() - 1));
323 size_t key_i = 1; // Key names are 1-indexed.
325 // Go through the argv, skipping the exec path.
326 for (size_t i = 1; i < argv.size(); ++i) {
327 #if defined(OS_WIN)
328 std::string switch_str = base::WideToUTF8(argv[i]);
329 #else
330 std::string switch_str = argv[i];
331 #endif
333 // Skip uninteresting switches.
334 if (IsBoringSwitch(switch_str))
335 continue;
337 // Stop if there are too many switches.
338 if (i > crash_keys::kSwitchesMaxCount)
339 break;
341 std::string key = base::StringPrintf(kSwitch, key_i++);
342 base::debug::SetCrashKeyValue(key, switch_str);
345 // Clear any remaining switches.
346 for (; key_i <= kSwitchesMaxCount; ++key_i) {
347 base::debug::ClearCrashKey(base::StringPrintf(kSwitch, key_i));
351 void SetVariationsList(const std::vector<std::string>& variations) {
352 base::debug::SetCrashKeyValue(kNumVariations,
353 base::StringPrintf("%" PRIuS, variations.size()));
355 std::string variations_string;
356 variations_string.reserve(kLargeSize);
358 for (size_t i = 0; i < variations.size(); ++i) {
359 const std::string& variation = variations[i];
360 // Do not truncate an individual experiment.
361 if (variations_string.size() + variation.size() >= kLargeSize)
362 break;
363 variations_string += variation;
364 variations_string += ",";
367 base::debug::SetCrashKeyValue(kVariations, variations_string);
370 void SetActiveExtensions(const std::set<std::string>& extensions) {
371 base::debug::SetCrashKeyValue(kNumExtensionsCount,
372 base::StringPrintf("%" PRIuS, extensions.size()));
374 std::set<std::string>::const_iterator it = extensions.begin();
375 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
376 std::string key = base::StringPrintf(kExtensionID, i + 1);
377 if (it == extensions.end()) {
378 base::debug::ClearCrashKey(key);
379 } else {
380 base::debug::SetCrashKeyValue(key, *it);
381 ++it;
386 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece& data) {
387 std::vector<std::string> info;
388 base::SplitString(data.as_string(), ';', &info);
389 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
390 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
391 std::string value;
392 if (i < info.size())
393 value = info[i];
394 base::debug::SetCrashKeyValue(key, value);
398 ScopedPrinterInfo::~ScopedPrinterInfo() {
399 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
400 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
401 base::debug::ClearCrashKey(key);
405 } // namespace crash_keys