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

Side by Side Diff: base/debug/crash_logging.cc

Issue 1368703002: Use a class instead of several separate globals in crash_logging.cc. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address comments Created 5 years, 3 months 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
« no previous file with comments | « base/debug/crash_logging.h ('k') | base/debug/crash_logging_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "base/debug/crash_logging.h" 5 #include "base/debug/crash_logging.h"
6 6
7 #include <cmath>
8 #include <map> 7 #include <map>
9 8
10 #include "base/debug/stack_trace.h" 9 #include "base/debug/stack_trace.h"
11 #include "base/format_macros.h" 10 #include "base/format_macros.h"
12 #include "base/logging.h" 11 #include "base/logging.h"
12 #include "base/memory/singleton.h"
13 #include "base/strings/string_util.h" 13 #include "base/strings/string_util.h"
14 #include "base/strings/stringprintf.h" 14 #include "base/strings/stringprintf.h"
15 15
16 namespace base { 16 namespace base {
17 namespace debug { 17 namespace debug {
18 18
19 namespace { 19 namespace {
20 20
21 // Global map of crash key names to registration entries.
22 typedef std::map<base::StringPiece, CrashKey> CrashKeyMap;
23 CrashKeyMap* g_crash_keys_ = NULL;
24
25 // The maximum length of a single chunk.
26 size_t g_chunk_max_length_ = 0;
27
28 // String used to format chunked key names. 21 // String used to format chunked key names.
29 const char kChunkFormatString[] = "%s-%" PRIuS; 22 const char kChunkFormatString[] = "%s-%" PRIuS;
30 23
31 // The functions that are called to actually set the key-value pairs in the
32 // crash reportng system.
33 SetCrashKeyValueFuncT g_set_key_func_ = NULL;
34 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL;
35
36 // For a given |length|, computes the number of chunks a value of that size
37 // will occupy.
38 size_t NumChunksForLength(size_t length) {
39 // Compute (length / g_chunk_max_length_), rounded up.
40 return (length + g_chunk_max_length_ - 1) / g_chunk_max_length_;
41 }
42
43 // The longest max_length allowed by the system. 24 // The longest max_length allowed by the system.
44 const size_t kLargestValueAllowed = 1024; 25 const size_t kLargestValueAllowed = 1024;
45 26
27 // The CrashKeyRegistry is a singleton that holds the global state of the crash
28 // key logging system.
29 class CrashKeyRegistry {
30 public:
31 static CrashKeyRegistry* GetInstance() {
32 return Singleton<CrashKeyRegistry>::get();
33 }
34
35 // Initializes the set of allowed crash keys.
36 size_t Init(const CrashKey* const keys, size_t count,
37 size_t chunk_max_length) {
38 DCHECK(!initialized_) << "Crash logging may only be initialized once";
39
40 chunk_max_length_ = chunk_max_length;
41
42 size_t total_keys = 0;
43 for (size_t i = 0; i < count; ++i) {
44 crash_keys_.insert(std::make_pair(keys[i].key_name, keys[i]));
45 total_keys += NumChunksForLength(keys[i].max_length);
46 DCHECK_LT(keys[i].max_length, kLargestValueAllowed);
47 }
48
49 DCHECK_EQ(count, crash_keys_.size())
50 << "Duplicate crash keys were registered";
51 initialized_ = true;
52
53 return total_keys;
54 }
55
56 // Sets the function pointers to integrate with the platform-specific crash
57 // reporting system.
58 void SetFunctions(SetCrashKeyValueFuncT set_key_func,
59 ClearCrashKeyValueFuncT clear_key_func) {
60 set_key_func_ = set_key_func;
61 clear_key_func_ = clear_key_func;
62 }
63
64 // Looks up the CrashKey object by key.
65 const CrashKey* LookupCrashKey(const base::StringPiece& key) {
66 CrashKeyMap::const_iterator it = crash_keys_.find(key.as_string());
67 if (it == crash_keys_.end())
68 return nullptr;
69 return &(it->second);
70 }
71
72 // For a given |length|, computes the number of chunks a value of that size
73 // will occupy.
74 size_t NumChunksForLength(size_t length) const {
75 // Compute (length / g_chunk_max_length_), rounded up.
76 return (length + chunk_max_length_ - 1) / chunk_max_length_;
77 }
78
79 void SetKeyValue(const base::StringPiece& key,
80 const base::StringPiece& value) {
81 set_key_func_(key, value);
82 }
83
84 void ClearKey(const base::StringPiece& key) {
85 clear_key_func_(key);
86 }
87
88 bool is_initialized() const { return initialized_ && set_key_func_; }
89
90 size_t chunk_max_length() const { return chunk_max_length_; }
91
92 private:
93 friend struct DefaultSingletonTraits<CrashKeyRegistry>;
94
95 CrashKeyRegistry()
96 : initialized_(false),
97 chunk_max_length_(0),
98 set_key_func_(nullptr),
99 clear_key_func_(nullptr) {
100 }
101
102 ~CrashKeyRegistry() {}
103
104 bool initialized_;
105
106 // Map of crash key names to registration entries.
107 using CrashKeyMap = std::map<base::StringPiece, CrashKey>;
108 CrashKeyMap crash_keys_;
109
110 size_t chunk_max_length_;
111
112 // The functions that are called to actually set the key-value pairs in the
113 // crash reportng system.
114 SetCrashKeyValueFuncT set_key_func_;
115 ClearCrashKeyValueFuncT clear_key_func_;
116
117 DISALLOW_COPY_AND_ASSIGN(CrashKeyRegistry);
118 };
119
46 } // namespace 120 } // namespace
47 121
48 void SetCrashKeyValue(const base::StringPiece& key, 122 void SetCrashKeyValue(const base::StringPiece& key,
49 const base::StringPiece& value) { 123 const base::StringPiece& value) {
50 if (!g_set_key_func_ || !g_crash_keys_) 124 CrashKeyRegistry* registry = CrashKeyRegistry::GetInstance();
125 if (!registry->is_initialized())
51 return; 126 return;
52 127
53 const CrashKey* crash_key = LookupCrashKey(key); 128 const CrashKey* crash_key = registry->LookupCrashKey(key);
54 129
55 DCHECK(crash_key) << "All crash keys must be registered before use " 130 DCHECK(crash_key) << "All crash keys must be registered before use "
56 << "(key = " << key << ")"; 131 << "(key = " << key << ")";
57 132
58 // Handle the un-chunked case. 133 // Handle the un-chunked case.
59 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { 134 if (!crash_key || crash_key->max_length <= registry->chunk_max_length()) {
60 g_set_key_func_(key, value); 135 registry->SetKeyValue(key, value);
61 return; 136 return;
62 } 137 }
63 138
64 // Unset the unused chunks. 139 // Unset the unused chunks.
65 std::vector<std::string> chunks = 140 std::vector<std::string> chunks =
66 ChunkCrashKeyValue(*crash_key, value, g_chunk_max_length_); 141 ChunkCrashKeyValue(*crash_key, value, registry->chunk_max_length());
67 for (size_t i = chunks.size(); 142 for (size_t i = chunks.size();
68 i < NumChunksForLength(crash_key->max_length); 143 i < registry->NumChunksForLength(crash_key->max_length);
69 ++i) { 144 ++i) {
70 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); 145 registry->ClearKey(base::StringPrintf(kChunkFormatString, key.data(), i+1));
71 } 146 }
72 147
73 // Set the chunked keys. 148 // Set the chunked keys.
74 for (size_t i = 0; i < chunks.size(); ++i) { 149 for (size_t i = 0; i < chunks.size(); ++i) {
75 g_set_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1), 150 registry->SetKeyValue(
76 chunks[i]); 151 base::StringPrintf(kChunkFormatString, key.data(), i+1),
152 chunks[i]);
77 } 153 }
78 } 154 }
79 155
80 void ClearCrashKey(const base::StringPiece& key) { 156 void ClearCrashKey(const base::StringPiece& key) {
81 if (!g_clear_key_func_ || !g_crash_keys_) 157 CrashKeyRegistry* registry = CrashKeyRegistry::GetInstance();
158 if (!registry->is_initialized())
82 return; 159 return;
83 160
84 const CrashKey* crash_key = LookupCrashKey(key); 161 const CrashKey* crash_key = registry->LookupCrashKey(key);
85 162
86 // Handle the un-chunked case. 163 // Handle the un-chunked case.
87 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { 164 if (!crash_key || crash_key->max_length <= registry->chunk_max_length()) {
88 g_clear_key_func_(key); 165 registry->ClearKey(key);
89 return; 166 return;
90 } 167 }
91 168
92 for (size_t i = 0; i < NumChunksForLength(crash_key->max_length); ++i) { 169 for (size_t i = 0;
93 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); 170 i < registry->NumChunksForLength(crash_key->max_length);
171 ++i) {
172 registry->ClearKey(base::StringPrintf(kChunkFormatString, key.data(), i+1));
94 } 173 }
95 } 174 }
96 175
97 void SetCrashKeyToStackTrace(const base::StringPiece& key, 176 void SetCrashKeyToStackTrace(const base::StringPiece& key,
98 const StackTrace& trace) { 177 const StackTrace& trace) {
99 size_t count = 0; 178 size_t count = 0;
100 const void* const* addresses = trace.Addresses(&count); 179 const void* const* addresses = trace.Addresses(&count);
101 SetCrashKeyFromAddresses(key, addresses, count); 180 SetCrashKeyFromAddresses(key, addresses, count);
102 } 181 }
103 182
(...skipping 29 matching lines...) Expand all
133 : key_(key.as_string()) { 212 : key_(key.as_string()) {
134 SetCrashKeyValue(key, value); 213 SetCrashKeyValue(key, value);
135 } 214 }
136 215
137 ScopedCrashKey::~ScopedCrashKey() { 216 ScopedCrashKey::~ScopedCrashKey() {
138 ClearCrashKey(key_); 217 ClearCrashKey(key_);
139 } 218 }
140 219
141 size_t InitCrashKeys(const CrashKey* const keys, size_t count, 220 size_t InitCrashKeys(const CrashKey* const keys, size_t count,
142 size_t chunk_max_length) { 221 size_t chunk_max_length) {
143 DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once"; 222 return CrashKeyRegistry::GetInstance()->Init(keys, count, chunk_max_length);
144 if (!keys) {
145 delete g_crash_keys_;
146 g_crash_keys_ = NULL;
147 return 0;
148 }
149
150 g_crash_keys_ = new CrashKeyMap;
151 g_chunk_max_length_ = chunk_max_length;
152
153 size_t total_keys = 0;
154 for (size_t i = 0; i < count; ++i) {
155 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i]));
156 total_keys += NumChunksForLength(keys[i].max_length);
157 DCHECK_LT(keys[i].max_length, kLargestValueAllowed);
158 }
159 DCHECK_EQ(count, g_crash_keys_->size())
160 << "Duplicate crash keys were registered";
161
162 return total_keys;
163 } 223 }
164 224
165 const CrashKey* LookupCrashKey(const base::StringPiece& key) { 225 const CrashKey* LookupCrashKey(const base::StringPiece& key) {
166 if (!g_crash_keys_) 226 return CrashKeyRegistry::GetInstance()->LookupCrashKey(key);
167 return NULL;
168 CrashKeyMap::const_iterator it = g_crash_keys_->find(key.as_string());
169 if (it == g_crash_keys_->end())
170 return NULL;
171 return &(it->second);
172 } 227 }
173 228
174 void SetCrashKeyReportingFunctions( 229 void SetCrashKeyReportingFunctions(
175 SetCrashKeyValueFuncT set_key_func, 230 SetCrashKeyValueFuncT set_key_func,
176 ClearCrashKeyValueFuncT clear_key_func) { 231 ClearCrashKeyValueFuncT clear_key_func) {
177 g_set_key_func_ = set_key_func; 232 CrashKeyRegistry::GetInstance()->SetFunctions(set_key_func, clear_key_func);
178 g_clear_key_func_ = clear_key_func;
179 } 233 }
180 234
181 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key, 235 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key,
182 const base::StringPiece& value, 236 const base::StringPiece& value,
183 size_t chunk_max_length) { 237 size_t chunk_max_length) {
184 std::string value_string = value.substr(0, crash_key.max_length).as_string(); 238 std::string value_string = value.substr(0, crash_key.max_length).as_string();
185 std::vector<std::string> chunks; 239 std::vector<std::string> chunks;
186 for (size_t offset = 0; offset < value_string.length(); ) { 240 for (size_t offset = 0; offset < value_string.length(); ) {
187 std::string chunk = value_string.substr(offset, chunk_max_length); 241 std::string chunk = value_string.substr(offset, chunk_max_length);
188 chunks.push_back(chunk); 242 chunks.push_back(chunk);
189 offset += chunk.length(); 243 offset += chunk.length();
190 } 244 }
191 return chunks; 245 return chunks;
192 } 246 }
193 247
194 void ResetCrashLoggingForTesting() {
195 delete g_crash_keys_;
196 g_crash_keys_ = NULL;
197 g_chunk_max_length_ = 0;
198 g_set_key_func_ = NULL;
199 g_clear_key_func_ = NULL;
200 }
201
202 } // namespace debug 248 } // namespace debug
203 } // namespace base 249 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/crash_logging.h ('k') | base/debug/crash_logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698