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"
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"
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
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;
44 static const size_t kSingleChunkLength
=
45 google_breakpad::CustomInfoEntry::kValueMaxLength
- 1;
47 static const size_t kSingleChunkLength
= 63;
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
);
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";
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";
93 const char kPrinterInfo
[] = "prn-info-%" PRIuS
;
95 #if defined(OS_CHROMEOS)
96 const char kNumberOfUsers
[] = "num-users";
99 #if defined(OS_MACOSX)
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";
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
},
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
},
147 { "dm-usage", kSmallSize
},
149 { kFontKeyName
, kSmallSize
},
150 { "ppapi_path", kMediumSize
},
151 { "subresource_url", kLargeSize
},
152 #if defined(OS_CHROMEOS)
153 { kNumberOfUsers
, kSmallSize
},
155 #if defined(OS_MACOSX)
156 { mac::kFirstNSException
, kMediumSize
},
157 { mac::kFirstNSExceptionTrace
, kMediumSize
},
158 { mac::kLastNSException
, kMediumSize
},
159 { mac::kLastNSExceptionTrace
, kMediumSize
},
160 { mac::kNSException
, kMediumSize
},
161 { mac::kNSExceptionTrace
, kMediumSize
},
162 { mac::kSendAction
, kMediumSize
},
163 { mac::kZombie
, kMediumSize
},
164 { mac::kZombieTrace
, kMediumSize
},
166 { "channel_error_bt", kMediumSize
},
167 { "remove_route_bt", kMediumSize
},
168 { "rwhvm_window", kMediumSize
},
169 // The following keys are for diagnosing crashes in http://crbug.com/369661.
170 // They will not be permanent.
171 { "renderer_page_id", kSmallSize
},
172 { "browser_page_id", kSmallSize
},
173 { "last_committed_page_id", kSmallSize
},
174 // End http://crbug.com/369661
176 { "VideoCaptureDeviceQTKit", kSmallSize
},
180 // This dynamic set of keys is used for sets of key value pairs when gathering
181 // a collection of data, like command line switches or extension IDs.
182 std::vector
<base::debug::CrashKey
> keys(
183 fixed_keys
, fixed_keys
+ arraysize(fixed_keys
));
185 // Register the switches.
187 // The fixed_keys names are string constants. Use static storage for
188 // formatted key names as well, since they will persist for the duration of
190 static char formatted_keys
[kSwitchesMaxCount
][sizeof(kSwitch
) + 1] =
192 const size_t formatted_key_len
= sizeof(formatted_keys
[0]);
193 for (size_t i
= 0; i
< kSwitchesMaxCount
; ++i
) {
194 // Name the keys using 1-based indexing.
195 int n
= base::snprintf(
196 formatted_keys
[i
], formatted_key_len
, kSwitch
, i
+ 1);
198 base::debug::CrashKey crash_key
= { formatted_keys
[i
], kSmallSize
};
199 keys
.push_back(crash_key
);
203 // Register the extension IDs.
205 static char formatted_keys
[kExtensionIDMaxCount
][sizeof(kExtensionID
) + 1] =
207 const size_t formatted_key_len
= sizeof(formatted_keys
[0]);
208 for (size_t i
= 0; i
< kExtensionIDMaxCount
; ++i
) {
209 int n
= base::snprintf(
210 formatted_keys
[i
], formatted_key_len
, kExtensionID
, i
+ 1);
212 base::debug::CrashKey crash_key
= { formatted_keys
[i
], kSmallSize
};
213 keys
.push_back(crash_key
);
217 // Register the printer info.
219 static char formatted_keys
[kPrinterInfoCount
][sizeof(kPrinterInfo
) + 1] =
221 const size_t formatted_key_len
= sizeof(formatted_keys
[0]);
222 for (size_t i
= 0; i
< kPrinterInfoCount
; ++i
) {
223 // Key names are 1-indexed.
224 int n
= base::snprintf(
225 formatted_keys
[i
], formatted_key_len
, kPrinterInfo
, i
+ 1);
227 base::debug::CrashKey crash_key
= { formatted_keys
[i
], kSmallSize
};
228 keys
.push_back(crash_key
);
232 return base::debug::InitCrashKeys(&keys
.at(0), keys
.size(),
236 void SetCrashClientIdFromGUID(const std::string
& client_guid
) {
237 std::string
stripped_guid(client_guid
);
238 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
239 ReplaceSubstringsAfterOffset(&stripped_guid
, 0, "-", "");
240 if (stripped_guid
.empty())
243 base::debug::SetCrashKeyValue(kClientId
, stripped_guid
);
246 static bool IsBoringSwitch(const std::string
& flag
) {
248 return StartsWithASCII(flag
, "--channel=", true) ||
250 // No point to including this since we already have a ptype field.
251 StartsWithASCII(flag
, "--type=", true) ||
253 // Not particularly interesting
254 StartsWithASCII(flag
, "--flash-broker=", true) ||
256 // Just about everything has this, don't bother.
257 StartsWithASCII(flag
, "/prefetch:", true) ||
259 // We handle the plugin path separately since it is usually too big
260 // to fit in the switches (limited to 63 characters).
261 StartsWithASCII(flag
, "--plugin-path=", true) ||
263 // This is too big so we end up truncating it anyway.
264 StartsWithASCII(flag
, "--force-fieldtrials=", true) ||
266 // These surround the flags that were added by about:flags, it lets
267 // you distinguish which flags were added manually via the command
268 // line versus those added through about:flags. For the most part
269 // we don't care how an option was enabled, so we strip these.
270 // (If you need to know can always look at the PEB).
271 flag
== "--flag-switches-begin" ||
272 flag
== "--flag-switches-end";
273 #elif defined(OS_CHROMEOS)
274 static const char* kIgnoreSwitches
[] = {
275 ::switches::kEnableImplSidePainting
,
276 ::switches::kEnableLogging
,
277 ::switches::kFlagSwitchesBegin
,
278 ::switches::kFlagSwitchesEnd
,
279 ::switches::kLoggingLevel
,
280 ::switches::kPpapiFlashArgs
,
281 ::switches::kPpapiFlashPath
,
282 ::switches::kRegisterPepperPlugins
,
283 ::switches::kUIPrioritizeInGpuProcess
,
285 ::switches::kUserDataDir
,
287 ::switches::kVModule
,
288 // Cros/CC flgas are specified as raw strings to avoid dependency.
289 "ash-default-wallpaper-large",
290 "ash-default-wallpaper-small",
291 "ash-guest-wallpaper-large",
292 "ash-guest-wallpaper-small",
293 "enterprise-enable-forced-re-enrollment",
294 "enterprise-enrollment-initial-modulus",
295 "enterprise-enrollment-modulus-limit",
298 "max-tiles-for-interest-area",
299 "max-unused-resource-memory-usage-percentage",
300 "termination-message-file",
303 if (!StartsWithASCII(flag
, "--", true))
305 std::size_t end
= flag
.find("=");
306 int len
= (end
== std::string::npos
) ? flag
.length() - 2 : end
- 2;
307 for (size_t i
= 0; i
< arraysize(kIgnoreSwitches
); ++i
) {
308 if (flag
.compare(2, len
, kIgnoreSwitches
[i
]) == 0)
317 void SetSwitchesFromCommandLine(const CommandLine
* command_line
) {
318 DCHECK(command_line
);
322 const CommandLine::StringVector
& argv
= command_line
->argv();
324 // Set the number of switches in case size > kNumSwitches.
325 base::debug::SetCrashKeyValue(kNumSwitches
,
326 base::StringPrintf("%" PRIuS
, argv
.size() - 1));
328 size_t key_i
= 1; // Key names are 1-indexed.
330 // Go through the argv, skipping the exec path.
331 for (size_t i
= 1; i
< argv
.size(); ++i
) {
333 std::string switch_str
= base::WideToUTF8(argv
[i
]);
335 std::string switch_str
= argv
[i
];
338 // Skip uninteresting switches.
339 if (IsBoringSwitch(switch_str
))
342 // Stop if there are too many switches.
343 if (i
> crash_keys::kSwitchesMaxCount
)
346 std::string key
= base::StringPrintf(kSwitch
, key_i
++);
347 base::debug::SetCrashKeyValue(key
, switch_str
);
350 // Clear any remaining switches.
351 for (; key_i
<= kSwitchesMaxCount
; ++key_i
) {
352 base::debug::ClearCrashKey(base::StringPrintf(kSwitch
, key_i
));
356 void SetVariationsList(const std::vector
<std::string
>& variations
) {
357 base::debug::SetCrashKeyValue(kNumVariations
,
358 base::StringPrintf("%" PRIuS
, variations
.size()));
360 std::string variations_string
;
361 variations_string
.reserve(kLargeSize
);
363 for (size_t i
= 0; i
< variations
.size(); ++i
) {
364 const std::string
& variation
= variations
[i
];
365 // Do not truncate an individual experiment.
366 if (variations_string
.size() + variation
.size() >= kLargeSize
)
368 variations_string
+= variation
;
369 variations_string
+= ",";
372 base::debug::SetCrashKeyValue(kVariations
, variations_string
);
375 void SetActiveExtensions(const std::set
<std::string
>& extensions
) {
376 base::debug::SetCrashKeyValue(kNumExtensionsCount
,
377 base::StringPrintf("%" PRIuS
, extensions
.size()));
379 std::set
<std::string
>::const_iterator it
= extensions
.begin();
380 for (size_t i
= 0; i
< kExtensionIDMaxCount
; ++i
) {
381 std::string key
= base::StringPrintf(kExtensionID
, i
+ 1);
382 if (it
== extensions
.end()) {
383 base::debug::ClearCrashKey(key
);
385 base::debug::SetCrashKeyValue(key
, *it
);
391 ScopedPrinterInfo::ScopedPrinterInfo(const base::StringPiece
& data
) {
392 std::vector
<std::string
> info
;
393 base::SplitString(data
.as_string(), ';', &info
);
394 for (size_t i
= 0; i
< kPrinterInfoCount
; ++i
) {
395 std::string key
= base::StringPrintf(kPrinterInfo
, i
+ 1);
399 base::debug::SetCrashKeyValue(key
, value
);
403 ScopedPrinterInfo::~ScopedPrinterInfo() {
404 for (size_t i
= 0; i
< kPrinterInfoCount
; ++i
) {
405 std::string key
= base::StringPrintf(kPrinterInfo
, i
+ 1);
406 base::debug::ClearCrashKey(key
);
410 } // namespace crash_keys