OLD | NEW |
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 <map> | 8 #include <map> |
8 | 9 |
9 #include "base/debug/stack_trace.h" | 10 #include "base/debug/stack_trace.h" |
10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/string_util.h" | 13 #include "base/string_util.h" |
13 #include "base/stringprintf.h" | 14 #include "base/stringprintf.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 namespace debug { | 17 namespace debug { |
17 | 18 |
18 namespace { | 19 namespace { |
19 | 20 |
| 21 // Global map of crash key names to registration entries. |
20 typedef std::map<base::StringPiece, CrashKey> CrashKeyMap; | 22 typedef std::map<base::StringPiece, CrashKey> CrashKeyMap; |
21 CrashKeyMap* g_crash_keys_ = NULL; | 23 CrashKeyMap* g_crash_keys_ = NULL; |
22 | 24 |
| 25 // The maximum length of a single chunk. |
23 size_t g_chunk_max_length_ = 0; | 26 size_t g_chunk_max_length_ = 0; |
24 | 27 |
| 28 // String used to format chunked key names. |
25 const char kChunkFormatString[] = "%s-%" PRIuS; | 29 const char kChunkFormatString[] = "%s-%" PRIuS; |
26 | 30 |
| 31 // The functions that are called to actually set the key-value pairs in the |
| 32 // crash reportng system. |
27 SetCrashKeyValueFuncT g_set_key_func_ = NULL; | 33 SetCrashKeyValueFuncT g_set_key_func_ = NULL; |
28 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; | 34 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; |
29 | 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 return std::ceil(length / static_cast<float>(g_chunk_max_length_)); |
| 40 } |
| 41 |
| 42 // The longest max_length allowed by the system. |
| 43 const size_t kLargestValueAllowed = 1024; |
| 44 |
30 } // namespace | 45 } // namespace |
31 | 46 |
32 void SetCrashKeyValue(const base::StringPiece& key, | 47 void SetCrashKeyValue(const base::StringPiece& key, |
33 const base::StringPiece& value) { | 48 const base::StringPiece& value) { |
34 if (!g_set_key_func_) | 49 if (!g_set_key_func_) |
35 return; | 50 return; |
36 | 51 |
37 const CrashKey* crash_key = LookupCrashKey(key); | 52 const CrashKey* crash_key = LookupCrashKey(key); |
38 | 53 |
39 // TODO(rsesek): Do this: | 54 // TODO(rsesek): Do this: |
40 //DCHECK(crash_key) << "All crash keys must be registered before use " | 55 //DCHECK(crash_key) << "All crash keys must be registered before use " |
41 // << "(key = " << key << ")"; | 56 // << "(key = " << key << ")"; |
42 | 57 |
43 // Handle the un-chunked case. | 58 // Handle the un-chunked case. |
44 if (!crash_key || crash_key->num_chunks == 1) { | 59 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { |
45 g_set_key_func_(key, value); | 60 g_set_key_func_(key, value); |
46 return; | 61 return; |
47 } | 62 } |
48 | 63 |
49 // Unset the unused chunks. | 64 // Unset the unused chunks. |
50 std::vector<std::string> chunks = | 65 std::vector<std::string> chunks = |
51 ChunkCrashKeyValue(*crash_key, value, g_chunk_max_length_); | 66 ChunkCrashKeyValue(*crash_key, value, g_chunk_max_length_); |
52 for (size_t i = chunks.size(); i < crash_key->num_chunks; ++i) { | 67 for (size_t i = chunks.size(); |
| 68 i < NumChunksForLength(crash_key->max_length); |
| 69 ++i) { |
53 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); | 70 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); |
54 } | 71 } |
55 | 72 |
56 // Set the chunked keys. | 73 // Set the chunked keys. |
57 for (size_t i = 0; i < chunks.size(); ++i) { | 74 for (size_t i = 0; i < chunks.size(); ++i) { |
58 g_set_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1), | 75 g_set_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1), |
59 chunks[i]); | 76 chunks[i]); |
60 } | 77 } |
61 } | 78 } |
62 | 79 |
63 void ClearCrashKey(const base::StringPiece& key) { | 80 void ClearCrashKey(const base::StringPiece& key) { |
64 if (!g_clear_key_func_) | 81 if (!g_clear_key_func_) |
65 return; | 82 return; |
66 | 83 |
67 const CrashKey* crash_key = LookupCrashKey(key); | 84 const CrashKey* crash_key = LookupCrashKey(key); |
68 | 85 |
69 // Handle the un-chunked case. | 86 // Handle the un-chunked case. |
70 if (!crash_key || crash_key->num_chunks == 1) { | 87 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { |
71 g_clear_key_func_(key); | 88 g_clear_key_func_(key); |
72 return; | 89 return; |
73 } | 90 } |
74 | 91 |
75 for (size_t i = 0; i < crash_key->num_chunks; ++i) { | 92 for (size_t i = 0; i < NumChunksForLength(crash_key->max_length); ++i) { |
76 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); | 93 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); |
77 } | 94 } |
78 } | 95 } |
79 | 96 |
80 void SetCrashKeyToStackTrace(const base::StringPiece& key, | 97 void SetCrashKeyToStackTrace(const base::StringPiece& key, |
81 const StackTrace& trace) { | 98 const StackTrace& trace) { |
82 size_t count = 0; | 99 size_t count = 0; |
83 const void* const* addresses = trace.Addresses(&count); | 100 const void* const* addresses = trace.Addresses(&count); |
84 SetCrashKeyFromAddresses(key, addresses, count); | 101 SetCrashKeyFromAddresses(key, addresses, count); |
85 } | 102 } |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
129 g_crash_keys_ = NULL; | 146 g_crash_keys_ = NULL; |
130 return 0; | 147 return 0; |
131 } | 148 } |
132 | 149 |
133 g_crash_keys_ = new CrashKeyMap; | 150 g_crash_keys_ = new CrashKeyMap; |
134 g_chunk_max_length_ = chunk_max_length; | 151 g_chunk_max_length_ = chunk_max_length; |
135 | 152 |
136 size_t total_keys = 0; | 153 size_t total_keys = 0; |
137 for (size_t i = 0; i < count; ++i) { | 154 for (size_t i = 0; i < count; ++i) { |
138 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i])); | 155 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i])); |
139 total_keys += keys[i].num_chunks; | 156 total_keys += NumChunksForLength(keys[i].max_length); |
| 157 DCHECK_LT(keys[i].max_length, kLargestValueAllowed); |
140 } | 158 } |
141 DCHECK_EQ(count, g_crash_keys_->size()) | 159 DCHECK_EQ(count, g_crash_keys_->size()) |
142 << "Duplicate crash keys were registered"; | 160 << "Duplicate crash keys were registered"; |
143 | 161 |
144 return total_keys; | 162 return total_keys; |
145 } | 163 } |
146 | 164 |
147 const CrashKey* LookupCrashKey(const base::StringPiece& key) { | 165 const CrashKey* LookupCrashKey(const base::StringPiece& key) { |
148 CrashKeyMap::const_iterator it = g_crash_keys_->find(key.as_string()); | 166 CrashKeyMap::const_iterator it = g_crash_keys_->find(key.as_string()); |
149 if (it == g_crash_keys_->end()) | 167 if (it == g_crash_keys_->end()) |
150 return NULL; | 168 return NULL; |
151 return &(it->second); | 169 return &(it->second); |
152 } | 170 } |
153 | 171 |
154 void SetCrashKeyReportingFunctions( | 172 void SetCrashKeyReportingFunctions( |
155 SetCrashKeyValueFuncT set_key_func, | 173 SetCrashKeyValueFuncT set_key_func, |
156 ClearCrashKeyValueFuncT clear_key_func) { | 174 ClearCrashKeyValueFuncT clear_key_func) { |
157 g_set_key_func_ = set_key_func; | 175 g_set_key_func_ = set_key_func; |
158 g_clear_key_func_ = clear_key_func; | 176 g_clear_key_func_ = clear_key_func; |
159 } | 177 } |
160 | 178 |
161 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key, | 179 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key, |
162 const base::StringPiece& value, | 180 const base::StringPiece& value, |
163 size_t chunk_max_length) { | 181 size_t chunk_max_length) { |
164 std::string value_string = value.as_string(); | 182 std::string value_string = value.substr(0, crash_key.max_length).as_string(); |
165 std::vector<std::string> chunks; | 183 std::vector<std::string> chunks; |
166 for (size_t i = 0, offset = 0; | 184 for (size_t offset = 0; offset < value_string.length(); ) { |
167 i < crash_key.num_chunks && offset < value_string.length(); | |
168 ++i) { | |
169 std::string chunk = value_string.substr(offset, chunk_max_length); | 185 std::string chunk = value_string.substr(offset, chunk_max_length); |
170 chunks.push_back(chunk); | 186 chunks.push_back(chunk); |
171 offset += chunk.length(); | 187 offset += chunk.length(); |
172 } | 188 } |
173 return chunks; | 189 return chunks; |
174 } | 190 } |
175 | 191 |
176 void ResetCrashLoggingForTesting() { | 192 void ResetCrashLoggingForTesting() { |
177 delete g_crash_keys_; | 193 delete g_crash_keys_; |
178 g_crash_keys_ = NULL; | 194 g_crash_keys_ = NULL; |
179 g_chunk_max_length_ = 0; | 195 g_chunk_max_length_ = 0; |
180 g_set_key_func_ = NULL; | 196 g_set_key_func_ = NULL; |
181 g_clear_key_func_ = NULL; | 197 g_clear_key_func_ = NULL; |
182 } | 198 } |
183 | 199 |
184 } // namespace debug | 200 } // namespace debug |
185 } // namespace base | 201 } // namespace base |
OLD | NEW |