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

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

Powered by Google App Engine
This is Rietveld 408576698