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"
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"
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
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;
47 static const size_t kSingleChunkLength
=
48 google_breakpad::CustomInfoEntry::kValueMaxLength
- 1;
50 static const size_t kSingleChunkLength
= 63;
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");
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";
66 const char kClientId
[] = "guid";
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";
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";
100 const char kPrinterInfo
[] = "prn-info-%" PRIuS
;
102 #if defined(OS_CHROMEOS)
103 const char kNumberOfUsers
[] = "num-users";
106 #if defined(OS_MACOSX)
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";
127 const char kKaskoGuid
[] = "kasko-guid";
128 const char kKaskoEquivalentGuid
[] = "kasko-equivalent-guid";
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
},
146 { kClientId
, kSmallSize
},
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
},
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
},
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
},
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
},
190 { "channel_error_bt", kMediumSize
},
191 { "remove_route_bt", kMediumSize
},
192 { "rwhvm_window", kMediumSize
},
194 { "VideoCaptureDeviceQTKit", kSmallSize
},
197 { kKaskoGuid
, kSmallSize
},
198 { kKaskoEquivalentGuid
, kSmallSize
},
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
214 static char formatted_keys
[kSwitchesMaxCount
][sizeof(kSwitch
) + 1] =
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);
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] =
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);
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] =
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);
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(),
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())
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
);
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
);
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
);
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
,
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
307 "channel", // switches::kProcessChannelID
309 switches::kProcessChannelID
,
311 switches::kProcessType
,
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
,
325 switches::kUserDataDir
,
326 // Cros/CC flgas 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",
338 "max-tiles-for-interest-area",
339 "max-unused-resource-memory-usage-percentage",
340 "termination-message-file",
346 // Just about everything has this, don't bother.
347 if (base::StartsWithASCII(flag
, "/prefetch:", true))
351 if (!base::StartsWithASCII(flag
, "--", true))
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)
362 void SetSwitchesFromCommandLine(const base::CommandLine
* command_line
) {
363 DCHECK(command_line
);
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
) {
378 std::string switch_str
= base::WideToUTF8(argv
[i
]);
380 std::string switch_str
= argv
[i
];
383 // Skip uninteresting switches.
384 if (IsBoringSwitch(switch_str
))
387 // Stop if there are too many switches.
388 if (i
> crash_keys::kSwitchesMaxCount
)
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
)
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
);
430 base::debug::SetCrashKeyValue(key
, *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);
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