Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(31)

Side by Side Diff: components/crash/core/common/crash_keys.cc

Issue 1481933002: Move crash keys for command-line switches to components/crash so they can be set (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@installer_crash_keys
Patch Set: typedef -> using, %lu -> PRIuS Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "components/crash/core/common/crash_keys.h" 5 #include "components/crash/core/common/crash_keys.h"
6 6
7 #include "base/command_line.h"
7 #include "base/debug/crash_logging.h" 8 #include "base/debug/crash_logging.h"
8 #include "base/format_macros.h" 9 #include "base/format_macros.h"
10 #include "base/logging.h"
9 #include "base/strings/string_piece.h" 11 #include "base/strings/string_piece.h"
10 #include "base/strings/string_util.h" 12 #include "base/strings/string_util.h"
11 #include "base/strings/stringprintf.h" 13 #include "base/strings/stringprintf.h"
14 #include "base/strings/utf_string_conversions.h"
12 15
13 namespace crash_keys { 16 namespace crash_keys {
14 17
15 #if defined(OS_MACOSX) 18 #if defined(OS_MACOSX)
16 // Crashpad owns the "guid" key. Chrome's metrics client ID is a separate ID 19 // Crashpad owns the "guid" key. Chrome's metrics client ID is a separate ID
17 // carried in a distinct "metrics_client_id" field. 20 // carried in a distinct "metrics_client_id" field.
18 const char kMetricsClientId[] = "metrics_client_id"; 21 const char kMetricsClientId[] = "metrics_client_id";
19 #else 22 #else
20 const char kClientId[] = "guid"; 23 const char kClientId[] = "guid";
21 #endif 24 #endif
22 25
23 const char kChannel[] = "channel"; 26 const char kChannel[] = "channel";
24 27
25 const char kNumVariations[] = "num-experiments"; 28 const char kNumVariations[] = "num-experiments";
26 const char kVariations[] = "variations"; 29 const char kVariations[] = "variations";
27 30
31 const char kSwitchFormat[] = "switch-%" PRIuS;
32 const char kNumSwitches[] = "num-switches";
33
28 const char kBug464926CrashKey[] = "bug-464926-info"; 34 const char kBug464926CrashKey[] = "bug-464926-info";
29 35
30 #if defined(OS_MACOSX) 36 #if defined(OS_MACOSX)
31 namespace mac { 37 namespace mac {
32 38
33 const char kZombie[] = "zombie"; 39 const char kZombie[] = "zombie";
34 const char kZombieTrace[] = "zombie_dealloc_bt"; 40 const char kZombieTrace[] = "zombie_dealloc_bt";
35 41
36 } // namespace mac 42 } // namespace mac
37 #endif 43 #endif
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
85 // Do not truncate an individual experiment. 91 // Do not truncate an individual experiment.
86 if (variations_string.size() + variation.size() >= kLargeSize) 92 if (variations_string.size() + variation.size() >= kLargeSize)
87 break; 93 break;
88 variations_string += variation; 94 variations_string += variation;
89 variations_string += ","; 95 variations_string += ",";
90 } 96 }
91 97
92 base::debug::SetCrashKeyValue(kVariations, variations_string); 98 base::debug::SetCrashKeyValue(kVariations, variations_string);
93 } 99 }
94 100
101 void GetCrashKeysForCommandLineSwitches(
102 std::vector<base::debug::CrashKey>* keys) {
103 DCHECK(keys);
104 base::debug::CrashKey crash_key = { kNumSwitches, kSmallSize };
105 keys->push_back(crash_key);
106
107 // Use static storage for formatted key names, since they will persist for
108 // the duration of the program.
109 static char formatted_keys[kSwitchesMaxCount][sizeof(kSwitchFormat) + 1] =
110 {{ 0 }};
111 const size_t formatted_key_len = sizeof(formatted_keys[0]);
112
113 // sizeof(kSwitchFormat) is platform-dependent, so make sure formatted_keys
114 // actually have space for a 2-digit switch number plus null-terminator.
115 static_assert(formatted_key_len >= 10,
116 "insufficient space for \"switch-NN\"");
117
118 for (size_t i = 0; i < kSwitchesMaxCount; ++i) {
119 // Name the keys using 1-based indexing.
120 int n = base::snprintf(formatted_keys[i], formatted_key_len, kSwitchFormat,
121 i + 1);
122 DCHECK_GT(n, 0);
123 base::debug::CrashKey crash_key = { formatted_keys[i], kSmallSize };
124 keys->push_back(crash_key);
125 }
126 }
127
128 void SetSwitchesFromCommandLine(const base::CommandLine& command_line,
129 SwitchFilterFunction skip_filter) {
130 const base::CommandLine::StringVector& argv = command_line.argv();
131
132 // Set the number of switches in case size > kNumSwitches.
133 base::debug::SetCrashKeyValue(kNumSwitches,
134 base::StringPrintf("%" PRIuS, argv.size() - 1));
135
136 size_t key_i = 1; // Key names are 1-indexed.
137
138 // Go through the argv, skipping the exec path. Stop if there are too many
139 // switches to hold in crash keys.
140 for (size_t i = 1; i < argv.size() && key_i <= crash_keys::kSwitchesMaxCount;
141 ++i) {
142 #if defined(OS_WIN)
143 std::string switch_str = base::WideToUTF8(argv[i]);
144 #else
145 std::string switch_str = argv[i];
146 #endif
147
148 // Skip uninteresting switches.
149 if (skip_filter && (*skip_filter)(switch_str))
150 continue;
151
152 std::string key = base::StringPrintf(kSwitchFormat, key_i++);
153 base::debug::SetCrashKeyValue(key, switch_str);
154 }
155
156 // Clear any remaining switches.
157 for (; key_i <= kSwitchesMaxCount; ++key_i)
158 base::debug::ClearCrashKey(base::StringPrintf(kSwitchFormat, key_i));
159 }
160
95 } // namespace crash_keys 161 } // namespace crash_keys
OLDNEW
« no previous file with comments | « components/crash/core/common/crash_keys.h ('k') | components/crash/core/common/crash_keys_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698