Chromium Code Reviews| 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 <map> | |
| 8 | |
| 7 #include "base/debug/stack_trace.h" | 9 #include "base/debug/stack_trace.h" |
| 8 #include "base/logging.h" | 10 #include "base/logging.h" |
| 9 #include "base/string_util.h" | 11 #include "base/string_util.h" |
| 10 #include "base/stringprintf.h" | 12 #include "base/stringprintf.h" |
| 11 | 13 |
| 12 namespace base { | 14 namespace base { |
| 13 namespace debug { | 15 namespace debug { |
| 14 | 16 |
| 15 static SetCrashKeyValueFuncT g_set_key_func_ = NULL; | 17 namespace { |
| 16 static ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; | 18 |
| 19 std::map<base::StringPiece, CrashKey>* g_crash_keys_ = NULL; | |
|
Mark Mentovai
2013/01/15 21:31:03
Typedef for this type, to reduce ugliness below?
Robert Sesek
2013/01/15 21:40:50
Done.
| |
| 20 | |
| 21 size_t g_chunk_max_length_ = 0; | |
| 22 | |
| 23 #if defined(OS_WIN) | |
| 24 const char kChunkFormatString[] = "%s-%Iu"; | |
|
Mark Mentovai
2013/01/15 21:31:03
Use PRIuS instead of "Iu" and "zu". #include "base
Robert Sesek
2013/01/15 21:40:50
Had no idea. Done.
| |
| 25 #else | |
| 26 const char kChunkFormatString[] = "%s-%zu"; | |
| 27 #endif | |
| 28 | |
| 29 SetCrashKeyValueFuncT g_set_key_func_ = NULL; | |
| 30 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; | |
| 31 | |
| 32 } // namespace | |
| 17 | 33 |
| 18 void SetCrashKeyValue(const base::StringPiece& key, | 34 void SetCrashKeyValue(const base::StringPiece& key, |
| 19 const base::StringPiece& value) { | 35 const base::StringPiece& value) { |
| 20 if (g_set_key_func_) | 36 if (!g_set_key_func_) |
| 37 return; | |
| 38 | |
| 39 const CrashKey* crash_key = LookupCrashKey(key); | |
| 40 | |
| 41 // TODO(rsesek): Do this: | |
| 42 //DCHECK(crash_key) << "All crash keys must be registered before use " | |
| 43 // << "(key = " << key << ")"; | |
| 44 | |
| 45 // Handle the un-chunked case. | |
| 46 if (!crash_key || crash_key->num_chunks == 1) { | |
| 21 g_set_key_func_(key, value); | 47 g_set_key_func_(key, value); |
| 48 return; | |
| 49 } | |
| 50 | |
| 51 // Unset the unused chunks. | |
| 52 std::vector<std::string> chunks = | |
| 53 ChunkCrashKeyValue(*crash_key, value, g_chunk_max_length_); | |
| 54 for (size_t i = chunks.size(); i < crash_key->num_chunks; ++i) { | |
| 55 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); | |
| 56 } | |
| 57 | |
| 58 // Set the chunked keys. | |
| 59 for (size_t i = 0; i < chunks.size(); ++i) { | |
| 60 g_set_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1), | |
| 61 chunks[i]); | |
| 62 } | |
| 22 } | 63 } |
| 23 | 64 |
| 24 void ClearCrashKey(const base::StringPiece& key) { | 65 void ClearCrashKey(const base::StringPiece& key) { |
| 25 if (g_clear_key_func_) | 66 if (!g_clear_key_func_) |
| 67 return; | |
| 68 | |
| 69 const CrashKey* crash_key = LookupCrashKey(key); | |
| 70 | |
| 71 // Handle the un-chunked case. | |
| 72 if (!crash_key || crash_key->num_chunks == 1) { | |
| 26 g_clear_key_func_(key); | 73 g_clear_key_func_(key); |
| 74 return; | |
| 75 } | |
| 76 | |
| 77 for (size_t i = 0; i < crash_key->num_chunks; ++i) { | |
| 78 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); | |
| 79 } | |
| 27 } | 80 } |
| 28 | 81 |
| 29 void SetCrashKeyToStackTrace(const base::StringPiece& key, | 82 void SetCrashKeyToStackTrace(const base::StringPiece& key, |
| 30 const StackTrace& trace) { | 83 const StackTrace& trace) { |
| 31 size_t count = 0; | 84 size_t count = 0; |
| 32 const void* const* addresses = trace.Addresses(&count); | 85 const void* const* addresses = trace.Addresses(&count); |
| 33 SetCrashKeyFromAddresses(key, addresses, count); | 86 SetCrashKeyFromAddresses(key, addresses, count); |
| 34 } | 87 } |
| 35 | 88 |
| 36 void SetCrashKeyFromAddresses(const base::StringPiece& key, | 89 void SetCrashKeyFromAddresses(const base::StringPiece& key, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 63 ScopedCrashKey::ScopedCrashKey(const base::StringPiece& key, | 116 ScopedCrashKey::ScopedCrashKey(const base::StringPiece& key, |
| 64 const base::StringPiece& value) | 117 const base::StringPiece& value) |
| 65 : key_(key.as_string()) { | 118 : key_(key.as_string()) { |
| 66 SetCrashKeyValue(key, value); | 119 SetCrashKeyValue(key, value); |
| 67 } | 120 } |
| 68 | 121 |
| 69 ScopedCrashKey::~ScopedCrashKey() { | 122 ScopedCrashKey::~ScopedCrashKey() { |
| 70 ClearCrashKey(key_); | 123 ClearCrashKey(key_); |
| 71 } | 124 } |
| 72 | 125 |
| 126 size_t InitCrashKeys(const CrashKey* const keys, size_t count, | |
| 127 size_t chunk_max_length) { | |
| 128 DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once"; | |
| 129 if (!keys) { | |
| 130 delete g_crash_keys_; | |
| 131 g_crash_keys_ = NULL; | |
| 132 return 0; | |
| 133 } | |
| 134 | |
| 135 g_crash_keys_ = new std::map<base::StringPiece, CrashKey>; | |
| 136 g_chunk_max_length_ = chunk_max_length; | |
| 137 | |
| 138 size_t total_keys = 0; | |
| 139 for (size_t i = 0; i < count; ++i) { | |
| 140 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i])); | |
| 141 total_keys += keys[i].num_chunks; | |
| 142 } | |
| 143 DCHECK_EQ(count, g_crash_keys_->size()) | |
| 144 << "Duplicate crash keys were registered"; | |
| 145 | |
| 146 return total_keys; | |
| 147 } | |
| 148 | |
| 149 const CrashKey* LookupCrashKey(const base::StringPiece& key) { | |
| 150 std::map<base::StringPiece, CrashKey>::const_iterator it = | |
| 151 g_crash_keys_->find(key.as_string()); | |
| 152 if (it == g_crash_keys_->end()) | |
| 153 return NULL; | |
| 154 return &(it->second); | |
| 155 } | |
| 156 | |
| 73 void SetCrashKeyReportingFunctions( | 157 void SetCrashKeyReportingFunctions( |
| 74 SetCrashKeyValueFuncT set_key_func, | 158 SetCrashKeyValueFuncT set_key_func, |
| 75 ClearCrashKeyValueFuncT clear_key_func) { | 159 ClearCrashKeyValueFuncT clear_key_func) { |
| 76 g_set_key_func_ = set_key_func; | 160 g_set_key_func_ = set_key_func; |
| 77 g_clear_key_func_ = clear_key_func; | 161 g_clear_key_func_ = clear_key_func; |
| 78 } | 162 } |
| 79 | 163 |
| 164 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key, | |
| 165 const base::StringPiece& value, | |
| 166 size_t chunk_max_length) { | |
| 167 std::string value_string = value.as_string(); | |
| 168 std::vector<std::string> chunks; | |
| 169 for (size_t i = 0, offset = 0; | |
| 170 i < crash_key.num_chunks && offset < value_string.length(); | |
| 171 ++i) { | |
| 172 std::string chunk = value_string.substr(offset, chunk_max_length); | |
| 173 chunks.push_back(chunk); | |
| 174 offset += chunk.length(); | |
| 175 } | |
| 176 return chunks; | |
| 177 } | |
| 178 | |
| 179 void ResetCrashLoggingForTesting() { | |
| 180 delete g_crash_keys_; | |
| 181 g_crash_keys_ = NULL; | |
| 182 g_chunk_max_length_ = 0; | |
| 183 g_set_key_func_ = NULL; | |
| 184 g_clear_key_func_ = NULL; | |
| 185 } | |
| 186 | |
| 80 } // namespace debug | 187 } // namespace debug |
| 81 } // namespace base | 188 } // namespace base |
| OLD | NEW |