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<std::string, CrashKey>* g_crash_keys_; | |
|
Scott Hess - ex-Googler
2013/01/04 18:59:39
Would prefer =NULL, if it's the default, the compi
Robert Sesek
2013/01/04 19:25:38
Done.
| |
| 20 | |
| 21 const char kChunkFormatString[] = "%s-%zu"; | |
| 22 | |
| 23 SetCrashKeyValueFuncT g_set_key_func_ = NULL; | |
| 24 ClearCrashKeyValueFuncT g_clear_key_func_ = NULL; | |
| 25 | |
| 26 } // namespace | |
|
Scott Hess - ex-Googler
2013/01/04 18:59:39
This should be outside the base::debug::, I think?
Robert Sesek
2013/01/04 19:25:38
I don't think it matters.
| |
| 17 | 27 |
| 18 void SetCrashKeyValue(const base::StringPiece& key, | 28 void SetCrashKeyValue(const base::StringPiece& key, |
| 19 const base::StringPiece& value) { | 29 const base::StringPiece& value) { |
| 20 if (g_set_key_func_) | 30 if (!g_set_key_func_) |
| 31 return; | |
| 32 | |
| 33 const CrashKey* crash_key = LookupCrashKey(key); | |
| 34 | |
| 35 // TODO(rsesek): Do this: | |
| 36 //DCHECK(crash_key) << "All crash keys must be registered before use " | |
| 37 // << "(key = " << key << ")"; | |
| 38 | |
| 39 // Handle the un-chunked case. | |
| 40 if (!crash_key || crash_key->num_chunks == 1) { | |
| 21 g_set_key_func_(key, value); | 41 g_set_key_func_(key, value); |
| 42 return; | |
| 43 } | |
| 44 | |
| 45 // Unset the unused chunks. | |
| 46 std::vector<std::string> chunks = ChunkCrashKeyValue(*crash_key, value); | |
| 47 for (size_t i = chunks.size(); i < crash_key->num_chunks; ++i) { | |
| 48 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1)); | |
| 49 } | |
| 50 | |
| 51 // Set the chunked keys. | |
| 52 for (size_t i = 0; i < chunks.size(); ++i) { | |
| 53 g_set_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1), | |
| 54 chunks[i]); | |
| 55 } | |
| 22 } | 56 } |
| 23 | 57 |
| 24 void ClearCrashKey(const base::StringPiece& key) { | 58 void ClearCrashKey(const base::StringPiece& key) { |
| 25 if (g_clear_key_func_) | 59 if (!g_clear_key_func_) |
| 60 return; | |
| 61 | |
| 62 const CrashKey* crash_key = LookupCrashKey(key); | |
| 63 | |
| 64 // Handle the un-checked case. | |
| 65 if (!crash_key || crash_key->num_chunks == 1) { | |
| 26 g_clear_key_func_(key); | 66 g_clear_key_func_(key); |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 for (size_t i = 1; i <= crash_key->num_chunks; ++i) { | |
| 71 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i)); | |
| 72 } | |
| 27 } | 73 } |
| 28 | 74 |
| 29 void SetCrashKeyToStackTrace(const base::StringPiece& key, | 75 void SetCrashKeyToStackTrace(const base::StringPiece& key, |
| 30 const StackTrace& trace) { | 76 const StackTrace& trace) { |
| 31 size_t count = 0; | 77 size_t count = 0; |
| 32 const void* const* addresses = trace.Addresses(&count); | 78 const void* const* addresses = trace.Addresses(&count); |
| 33 SetCrashKeyFromAddresses(key, addresses, count); | 79 SetCrashKeyFromAddresses(key, addresses, count); |
| 34 } | 80 } |
| 35 | 81 |
| 36 void SetCrashKeyFromAddresses(const base::StringPiece& key, | 82 void SetCrashKeyFromAddresses(const base::StringPiece& key, |
| (...skipping 26 matching lines...) Expand all Loading... | |
| 63 ScopedCrashKey::ScopedCrashKey(const base::StringPiece& key, | 109 ScopedCrashKey::ScopedCrashKey(const base::StringPiece& key, |
| 64 const base::StringPiece& value) | 110 const base::StringPiece& value) |
| 65 : key_(key.as_string()) { | 111 : key_(key.as_string()) { |
| 66 SetCrashKeyValue(key, value); | 112 SetCrashKeyValue(key, value); |
| 67 } | 113 } |
| 68 | 114 |
| 69 ScopedCrashKey::~ScopedCrashKey() { | 115 ScopedCrashKey::~ScopedCrashKey() { |
| 70 ClearCrashKey(key_); | 116 ClearCrashKey(key_); |
| 71 } | 117 } |
| 72 | 118 |
| 119 void InitCrashKeys(const CrashKey* const keys, size_t count, | |
| 120 InitCrashKeysCallbackFuncT completion_callback) { | |
| 121 if (!keys) { | |
| 122 delete g_crash_keys_; | |
| 123 g_crash_keys_ = NULL; | |
| 124 return; | |
| 125 } | |
| 126 | |
| 127 g_crash_keys_ = new std::map<std::string, CrashKey>; | |
|
Scott Hess - ex-Googler
2013/01/04 18:59:39
This won't work great for modules which want to ma
Robert Sesek
2013/01/04 19:25:38
Because Windows enforces the requirement of a fixe
| |
| 128 | |
| 129 std::vector<std::string> all_keys; | |
| 130 all_keys.reserve(count); | |
| 131 for (size_t i = 0; i < count; ++i) { | |
| 132 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i])); | |
| 133 if (keys[i].num_chunks == 1) { | |
| 134 all_keys.push_back(keys[i].key_name); | |
| 135 } else { | |
| 136 for (size_t j = 1; j <= keys[i].num_chunks; ++j) { | |
| 137 all_keys.push_back( | |
| 138 base::StringPrintf(kChunkFormatString, keys[i].key_name, j)); | |
| 139 } | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 if (completion_callback) | |
| 144 completion_callback(all_keys); | |
| 145 } | |
| 146 | |
| 147 const CrashKey* LookupCrashKey(const base::StringPiece& key) { | |
| 148 std::map<std::string, CrashKey>::const_iterator it = | |
| 149 g_crash_keys_->find(key.as_string()); | |
|
Scott Hess - ex-Googler
2013/01/04 18:59:39
Once we're calling as_string(), StringPiece isn't
Robert Sesek
2013/01/04 19:25:38
If we do it by const char* though, won't using the
Scott Hess - ex-Googler
2013/01/05 00:22:05
That's actually what I meant - kKeyName would be t
| |
| 150 if (it == g_crash_keys_->end()) | |
| 151 return NULL; | |
| 152 return &(it->second); | |
| 153 } | |
| 154 | |
| 73 void SetCrashKeyReportingFunctions( | 155 void SetCrashKeyReportingFunctions( |
| 74 SetCrashKeyValueFuncT set_key_func, | 156 SetCrashKeyValueFuncT set_key_func, |
| 75 ClearCrashKeyValueFuncT clear_key_func) { | 157 ClearCrashKeyValueFuncT clear_key_func) { |
| 76 g_set_key_func_ = set_key_func; | 158 g_set_key_func_ = set_key_func; |
| 77 g_clear_key_func_ = clear_key_func; | 159 g_clear_key_func_ = clear_key_func; |
| 78 } | 160 } |
| 79 | 161 |
| 162 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key, | |
| 163 const base::StringPiece& value) { | |
| 164 std::string value_string = value.as_string(); | |
| 165 std::vector<std::string> chunks; | |
| 166 size_t offset = 0; | |
| 167 for (size_t i = 0; | |
| 168 i < crash_key.num_chunks && offset < value_string.length(); | |
| 169 ++i) { | |
| 170 std::string chunk = value_string.substr(offset, crash_key.max_length); | |
| 171 chunks.push_back(chunk); | |
| 172 offset += chunk.length(); | |
| 173 } | |
| 174 return chunks; | |
| 175 } | |
| 176 | |
| 177 void ResetCrashLoggingForTesting() { | |
| 178 delete g_crash_keys_; | |
| 179 g_crash_keys_ = NULL; | |
| 180 g_set_key_func_ = NULL; | |
| 181 g_clear_key_func_ = NULL; | |
| 182 } | |
| 183 | |
| 80 } // namespace debug | 184 } // namespace debug |
| 81 } // namespace base | 185 } // namespace base |
| OLD | NEW |