| 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 <cmath> |
| 8 #include <unordered_map> | 8 #include <unordered_map> |
| 9 | 9 |
| 10 #include "base/debug/stack_trace.h" | 10 #include "base/debug/stack_trace.h" |
| (...skipping 14 matching lines...) Expand all Loading... |
| 25 typedef std::unordered_map<base::StringPiece, CrashKey, base::StringPieceHash> | 25 typedef std::unordered_map<base::StringPiece, CrashKey, base::StringPieceHash> |
| 26 CrashKeyMap; | 26 CrashKeyMap; |
| 27 CrashKeyMap* g_crash_keys_ = NULL; | 27 CrashKeyMap* g_crash_keys_ = NULL; |
| 28 | 28 |
| 29 // The maximum length of a single chunk. | 29 // The maximum length of a single chunk. |
| 30 size_t g_chunk_max_length_ = 0; | 30 size_t g_chunk_max_length_ = 0; |
| 31 | 31 |
| 32 // String used to format chunked key names. | 32 // String used to format chunked key names. |
| 33 const char kChunkFormatString[] = "%s-%" PRIuS; | 33 const char kChunkFormatString[] = "%s-%" PRIuS; |
| 34 | 34 |
| 35 bool g_ignore_unregistered_keys_ = false; |
| 36 |
| 35 // The functions that are called to actually set the key-value pairs in the | 37 // The functions that are called to actually set the key-value pairs in the |
| 36 // crash reportng system. | 38 // crash reportng system. |
| 37 SetCrashKeyValueFuncT g_set_key_func_ = NULL; | 39 SetCrashKeyValueFuncT g_set_key_func_ = NULL; |
| 38 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; | 40 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; |
| 39 | 41 |
| 40 // For a given |length|, computes the number of chunks a value of that size | 42 // For a given |length|, computes the number of chunks a value of that size |
| 41 // will occupy. | 43 // will occupy. |
| 42 size_t NumChunksForLength(size_t length) { | 44 size_t NumChunksForLength(size_t length) { |
| 43 // Compute (length / g_chunk_max_length_), rounded up. | 45 // Compute (length / g_chunk_max_length_), rounded up. |
| 44 return (length + g_chunk_max_length_ - 1) / g_chunk_max_length_; | 46 return (length + g_chunk_max_length_ - 1) / g_chunk_max_length_; |
| 45 } | 47 } |
| 46 | 48 |
| 47 // The longest max_length allowed by the system. | 49 // The longest max_length allowed by the system. |
| 48 const size_t kLargestValueAllowed = 2048; | 50 const size_t kLargestValueAllowed = 2048; |
| 49 | 51 |
| 50 } // namespace | 52 } // namespace |
| 51 | 53 |
| 52 void SetCrashKeyValue(const base::StringPiece& key, | 54 void SetCrashKeyValue(const base::StringPiece& key, |
| 53 const base::StringPiece& value) { | 55 const base::StringPiece& value) { |
| 54 if (!g_set_key_func_ || !g_crash_keys_) | 56 if (!g_set_key_func_ || !g_crash_keys_) |
| 55 return; | 57 return; |
| 56 | 58 |
| 57 const CrashKey* crash_key = LookupCrashKey(key); | 59 const CrashKey* crash_key = LookupCrashKey(key); |
| 58 | 60 |
| 61 if (g_ignore_unregistered_keys_ && !crash_key) { |
| 62 return; |
| 63 } |
| 64 |
| 59 DCHECK(crash_key) << "All crash keys must be registered before use " | 65 DCHECK(crash_key) << "All crash keys must be registered before use " |
| 60 << "(key = " << key << ")"; | 66 << "(key = " << key << ")"; |
| 61 | 67 |
| 62 // Handle the un-chunked case. | 68 // Handle the un-chunked case. |
| 63 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { | 69 if (!crash_key || crash_key->max_length <= g_chunk_max_length_) { |
| 64 g_set_key_func_(key, value); | 70 g_set_key_func_(key, value); |
| 65 return; | 71 return; |
| 66 } | 72 } |
| 67 | 73 |
| 68 // Unset the unused chunks. | 74 // Unset the unused chunks. |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 137 : key_(key.as_string()) { | 143 : key_(key.as_string()) { |
| 138 SetCrashKeyValue(key, value); | 144 SetCrashKeyValue(key, value); |
| 139 } | 145 } |
| 140 | 146 |
| 141 ScopedCrashKey::~ScopedCrashKey() { | 147 ScopedCrashKey::~ScopedCrashKey() { |
| 142 ClearCrashKey(key_); | 148 ClearCrashKey(key_); |
| 143 } | 149 } |
| 144 | 150 |
| 145 size_t InitCrashKeys(const CrashKey* const keys, size_t count, | 151 size_t InitCrashKeys(const CrashKey* const keys, size_t count, |
| 146 size_t chunk_max_length) { | 152 size_t chunk_max_length) { |
| 153 return InitCrashKeys(keys, count, chunk_max_length, |
| 154 false /* ignore_unregistered_keys */); |
| 155 } |
| 156 |
| 157 size_t InitCrashKeys(const CrashKey* const keys, |
| 158 size_t count, |
| 159 size_t chunk_max_length, |
| 160 bool ignore_unregistered_keys) { |
| 147 DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once"; | 161 DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once"; |
| 162 g_ignore_unregistered_keys_ = ignore_unregistered_keys; |
| 148 if (!keys) { | 163 if (!keys) { |
| 149 delete g_crash_keys_; | 164 delete g_crash_keys_; |
| 150 g_crash_keys_ = NULL; | 165 g_crash_keys_ = NULL; |
| 151 return 0; | 166 return 0; |
| 152 } | 167 } |
| 153 | 168 |
| 154 g_crash_keys_ = new CrashKeyMap; | 169 g_crash_keys_ = new CrashKeyMap; |
| 155 g_chunk_max_length_ = chunk_max_length; | 170 g_chunk_max_length_ = chunk_max_length; |
| 156 | 171 |
| 157 size_t total_keys = 0; | 172 size_t total_keys = 0; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 194 } | 209 } |
| 195 return chunks; | 210 return chunks; |
| 196 } | 211 } |
| 197 | 212 |
| 198 void ResetCrashLoggingForTesting() { | 213 void ResetCrashLoggingForTesting() { |
| 199 delete g_crash_keys_; | 214 delete g_crash_keys_; |
| 200 g_crash_keys_ = NULL; | 215 g_crash_keys_ = NULL; |
| 201 g_chunk_max_length_ = 0; | 216 g_chunk_max_length_ = 0; |
| 202 g_set_key_func_ = NULL; | 217 g_set_key_func_ = NULL; |
| 203 g_clear_key_func_ = NULL; | 218 g_clear_key_func_ = NULL; |
| 219 g_ignore_unregistered_keys_ = false; |
| 204 } | 220 } |
| 205 | 221 |
| 206 } // namespace debug | 222 } // namespace debug |
| 207 } // namespace base | 223 } // namespace base |
| OLD | NEW |