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/installer/util/google_update_settings.h"
16 #if defined(OS_MACOSX)
17 #include "breakpad/src/common/simple_string_dictionary.h"
19 #include "breakpad/src/client/windows/common/ipc_protocol.h"
22 namespace crash_keys {
24 // A small crash key, guaranteed to never be split into multiple pieces.
25 const size_t kSmallSize = 63;
27 // A medium crash key, which will be chunked on certain platforms but not
28 // others. Guaranteed to never be more than four chunks.
29 const size_t kMediumSize = kSmallSize * 4;
31 // A large crash key, which will be chunked on all platforms. This should be
33 const size_t kLargeSize = kSmallSize * 16;
35 // The maximum lengths specified by breakpad include the trailing NULL, so
36 // the actual length of the string is one less.
37 #if defined(OS_MACOSX)
38 static const size_t kSingleChunkLength =
39 google_breakpad::SimpleStringDictionary::value_size - 1;
41 static const size_t kSingleChunkLength =
42 google_breakpad::CustomInfoEntry::kValueMaxLength - 1;
44 static const size_t kSingleChunkLength = 63;
47 // Guarantees for crash key sizes.
48 COMPILE_ASSERT(kSmallSize <= kSingleChunkLength,
49 crash_key_chunk_size_too_small);
50 #if defined(OS_MACOSX)
51 COMPILE_ASSERT(kMediumSize <= kSingleChunkLength,
52 mac_has_medium_size_crash_key_chunks);
55 const char kClientID[] = "guid";
57 const char kChannel[] = "channel";
59 const char kActiveURL[] = "url-chunk";
61 const char kSwitch[] = "switch-%" PRIuS;
62 const char kNumSwitches[] = "num-switches";
64 const char kNumVariations[] = "num-experiments";
65 const char kVariations[] = "variations";
67 const char kExtensionID[] = "extension-%" PRIuS;
68 const char kNumExtensionsCount[] = "num-extensions";
70 const char kNumberOfViews[] = "num-views";
72 #if !defined(OS_ANDROID)
73 const char kGPUVendorID[] = "gpu-venid";
74 const char kGPUDeviceID[] = "gpu-devid";
76 const char kGPUDriverVersion[] = "gpu-driver";
77 const char kGPUPixelShaderVersion[] = "gpu-psver";
78 const char kGPUVertexShaderVersion[] = "gpu-vsver";
79 #if defined(OS_MACOSX)
80 const char kGPUGLVersion[] = "gpu-glver";
81 #elif defined(OS_POSIX)
82 const char kGPUVendor[] = "gpu-gl-vendor";
83 const char kGPURenderer[] = "gpu-gl-renderer";
86 const char kPrinterInfo[] = "prn-info-%" PRIuS;
88 #if defined(OS_MACOSX)
91 const char kFirstNSException[] = "firstexception";
92 const char kFirstNSExceptionTrace[] = "firstexception_bt";
94 const char kLastNSException[] = "lastexception";
95 const char kLastNSExceptionTrace[] = "lastexception_bt";
97 const char kNSException[] = "nsexception";
98 const char kNSExceptionTrace[] = "nsexception_bt";
100 const char kSendAction[] = "sendaction";
102 const char kZombie[] = "zombie";
103 const char kZombieTrace[] = "zombie_dealloc_bt";
108 size_t RegisterChromeCrashKeys() {
109 // The following keys may be chunked by the underlying crash logging system,
110 // but ultimately constitute a single key-value pair.
111 base::debug::CrashKey fixed_keys[] = {
112 { kClientID, kSmallSize },
113 { kChannel, kSmallSize },
114 { kActiveURL, kLargeSize },
115 { kNumSwitches, kSmallSize },
116 { kNumVariations, kSmallSize },
117 { kVariations, kLargeSize },
118 { kNumExtensionsCount, kSmallSize },
119 { kNumberOfViews, kSmallSize },
120 #if !defined(OS_ANDROID)
121 { kGPUVendorID, kSmallSize },
122 { kGPUDeviceID, kSmallSize },
124 { kGPUDriverVersion, kSmallSize },
125 { kGPUPixelShaderVersion, kSmallSize },
126 { kGPUVertexShaderVersion, kSmallSize },
127 #if defined(OS_MACOSX)
128 { kGPUGLVersion, kSmallSize },
129 #elif defined(OS_POSIX)
130 { kGPUVendor, kSmallSize },
131 { kGPURenderer, kSmallSize },
135 { "ppapi_path", kMediumSize },
136 { "subresource_url", kLargeSize },
137 #if defined(OS_MACOSX)
138 { mac::kFirstNSException, kMediumSize },
139 { mac::kFirstNSExceptionTrace, kMediumSize },
140 { mac::kLastNSException, kMediumSize },
141 { mac::kLastNSExceptionTrace, kMediumSize },
142 { mac::kNSException, kMediumSize },
143 { mac::kNSExceptionTrace, kMediumSize },
144 { mac::kSendAction, kMediumSize },
145 { mac::kZombie, kMediumSize },
146 { mac::kZombieTrace, kMediumSize },
148 { "channel_error_bt", kMediumSize },
149 { "remove_route_bt", kMediumSize },
150 { "rwhvm_window", kMediumSize },
152 { "VideoCaptureDeviceQTKit", kSmallSize },
156 // This dynamic set of keys is used for sets of key value pairs when gathering
157 // a collection of data, like command line switches or extension IDs.
158 std::vector<base::debug::CrashKey> keys(
159 fixed_keys, fixed_keys + arraysize(fixed_keys));
161 // Register the switches.
163 // The fixed_keys names are string constants. Use static storage for
164 // formatted key names as well, since they will persist for the duration of
166 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitch) + 1] =
168 const size_t formatted_key_len = sizeof(formatted_keys[0]);
169 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
170 // Name the keys using 1-based indexing.
171 int n = base::snprintf(
172 formatted_keys[i], formatted_key_len, kSwitch, i + 1);
174 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
175 keys.push_back(crash_key);
179 // Register the extension IDs.
181 static char formatted_keys[kExtensionIDMaxCount][sizeof(kExtensionID) + 1] =
183 const size_t formatted_key_len = sizeof(formatted_keys[0]);
184 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
185 int n = base::snprintf(
186 formatted_keys[i], formatted_key_len, kExtensionID, i);
188 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
189 keys.push_back(crash_key);
193 // Register the printer info.
195 static char formatted_keys[kPrinterInfoCount][sizeof(kPrinterInfo) + 1] =
197 const size_t formatted_key_len = sizeof(formatted_keys[0]);
198 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
199 // Key names are 1-indexed.
200 int n = base::snprintf(
201 formatted_keys[i], formatted_key_len, kPrinterInfo, i + 1);
203 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
204 keys.push_back(crash_key);
208 return base::debug::InitCrashKeys(&keys.at(0), keys.size(),
212 void SetClientID(const std::string& client_id) {
213 std::string guid(client_id);
214 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
215 ReplaceSubstringsAfterOffset(&guid, 0, "-", "");
219 base::debug::SetCrashKeyValue(kClientID, guid);
220 GoogleUpdateSettings::SetMetricsId(guid);
223 static bool IsBoringSwitch(const std::string& flag) {
225 return StartsWithASCII(flag, "--channel=", true) ||
227 // No point to including this since we already have a ptype field.
228 StartsWithASCII(flag, "--type=", true) ||
230 // Not particularly interesting
231 StartsWithASCII(flag, "--flash-broker=", true) ||
233 // Just about everything has this, don't bother.
234 StartsWithASCII(flag, "/prefetch:", true) ||
236 // We handle the plugin path separately since it is usually too big
237 // to fit in the switches (limited to 63 characters).
238 StartsWithASCII(flag, "--plugin-path=", true) ||
240 // This is too big so we end up truncating it anyway.
241 StartsWithASCII(flag, "--force-fieldtrials=", true) ||
243 // These surround the flags that were added by about:flags, it lets
244 // you distinguish which flags were added manually via the command
245 // line versus those added through about:flags. For the most part
246 // we don't care how an option was enabled, so we strip these.
247 // (If you need to know can always look at the PEB).
248 flag == "--flag-switches-begin" ||
249 flag == "--flag-switches-end";
255 void SetSwitchesFromCommandLine(const CommandLine* command_line) {
256 DCHECK(command_line);
260 const CommandLine::StringVector& argv = command_line->argv();
262 // Set the number of switches in case size > kNumSwitches.
263 base::debug::SetCrashKeyValue(kNumSwitches,
264 base::StringPrintf("%" PRIuS, argv.size() - 1));
266 size_t key_i = 1; // Key names are 1-indexed.
268 // Go through the argv, skipping the exec path.
269 for (size_t i = 1; i < argv.size(); ++i) {
271 std::string switch_str = base::WideToUTF8(argv[i]);
273 std::string switch_str = argv[i];
276 // Skip uninteresting switches.
277 if (IsBoringSwitch(switch_str))
280 // Stop if there are too many switches.
281 if (i > crash_keys::kSwitchesMaxCount)
284 std::string key = base::StringPrintf(kSwitch, key_i++);
285 base::debug::SetCrashKeyValue(key, switch_str);
288 // Clear any remaining switches.
289 for (; key_i <= kSwitchesMaxCount; ++key_i) {
290 base::debug::ClearCrashKey(base::StringPrintf(kSwitch, key_i));
294 void SetVariationsList(const std::vector<std::string>& variations) {
295 base::debug::SetCrashKeyValue(kNumVariations,
296 base::StringPrintf("%" PRIuS, variations.size()));
298 std::string variations_string;
299 variations_string.reserve(kLargeSize);
301 for (size_t i = 0; i < variations.size(); ++i) {
302 const std::string& variation = variations[i];
303 // Do not truncate an individual experiment.
304 if (variations_string.size() + variation.size() >= kLargeSize)
306 variations_string += variation;
307 variations_string += ",";
310 base::debug::SetCrashKeyValue(kVariations, variations_string);
313 void SetActiveExtensions(const std::set<std::string>& extensions) {
314 base::debug::SetCrashKeyValue(kNumExtensionsCount,
315 base::StringPrintf("%" PRIuS, extensions.size()));
317 std::set<std::string>::const_iterator it = extensions.begin();
318 for (size_t i = 0; i < kExtensionIDMaxCount; ++i) {
319 std::string key = base::StringPrintf(kExtensionID, i);
320 if (it == extensions.end()) {
321 base::debug::ClearCrashKey(key);
323 base::debug::SetCrashKeyValue(key, *it);
329 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece& data) {
330 std::vector<std::string> info;
331 base::SplitString(data.as_string(), ';', &info);
332 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
333 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
337 base::debug::SetCrashKeyValue(key, value);
341 ScopedPrinterInfo::~ScopedPrinterInfo() {
342 for (size_t i = 0; i < kPrinterInfoCount; ++i) {
343 std::string key = base::StringPrintf(kPrinterInfo, i + 1);
344 base::debug::ClearCrashKey(key);
348 } // namespace crash_keys