1 // Copyright 2015 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 "components/crash_keys/crash_keys.h"
7 #include "base/debug/crash_logging.h"
8 #include "base/format_macros.h"
9 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h"
13 namespace crash_keys
{
15 #if defined(OS_MACOSX)
16 // Crashpad owns the "guid" key. Chrome's metrics client ID is a separate ID
17 // carried in a distinct "metrics_client_id" field.
18 const char kMetricsClientId
[] = "metrics_client_id";
20 const char kClientId
[] = "guid";
23 const char kChannel
[] = "channel";
25 const char kNumVariations
[] = "num-experiments";
26 const char kVariations
[] = "variations";
28 const char kBug464926CrashKey
[] = "bug-464926-info";
30 #if defined(OS_MACOSX)
33 const char kZombie
[] = "zombie";
34 const char kZombieTrace
[] = "zombie_dealloc_bt";
39 void SetMetricsClientIdFromGUID(const std::string
& metrics_client_guid
) {
40 std::string
stripped_guid(metrics_client_guid
);
41 // Remove all instance of '-' char from the GUID. So BCD-WXY becomes BCDWXY.
42 base::ReplaceSubstringsAfterOffset(
43 &stripped_guid
, 0, "-", base::StringPiece());
44 if (stripped_guid
.empty())
47 #if defined(OS_MACOSX)
48 // The crash client ID is maintained by Crashpad and is distinct from the
49 // metrics client ID, which is carried in its own key.
50 base::debug::SetCrashKeyValue(kMetricsClientId
, stripped_guid
);
52 // The crash client ID is set by the application when Breakpad is in use.
53 // The same ID as the metrics client ID is used.
54 base::debug::SetCrashKeyValue(kClientId
, stripped_guid
);
58 void ClearMetricsClientId() {
59 #if defined(OS_MACOSX)
60 // Crashpad always monitors for crashes, but doesn't upload them when
61 // crash reporting is disabled. The preference to upload crash reports is
62 // linked to the preference for metrics reporting. When metrics reporting is
63 // disabled, don't put the metrics client ID into crash dumps. This way, crash
64 // reports that are saved but not uploaded will not have a metrics client ID
65 // from the time that metrics reporting was disabled even if they are uploaded
66 // by user action at a later date.
68 // Breakpad cannot be enabled or disabled without an application restart, and
69 // it needs to use the metrics client ID as its stable crash client ID, so
70 // leave its client ID intact even when metrics reporting is disabled while
71 // the application is running.
72 base::debug::ClearCrashKey(kMetricsClientId
);
76 void SetVariationsList(const std::vector
<std::string
>& variations
) {
77 base::debug::SetCrashKeyValue(kNumVariations
,
78 base::StringPrintf("%" PRIuS
, variations
.size()));
80 std::string variations_string
;
81 variations_string
.reserve(kLargeSize
);
83 for (size_t i
= 0; i
< variations
.size(); ++i
) {
84 const std::string
& variation
= variations
[i
];
85 // Do not truncate an individual experiment.
86 if (variations_string
.size() + variation
.size() >= kLargeSize
)
88 variations_string
+= variation
;
89 variations_string
+= ",";
92 base::debug::SetCrashKeyValue(kVariations
, variations_string
);
95 } // namespace crash_keys