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

Side by Side Diff: base/debug/crash_logging.cc

Issue 11761030: Create the crash key registration system and register some Mac-specific keys. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address comments Created 7 years, 11 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 | Annotate | Revision Log
« no previous file with comments | « base/debug/crash_logging.h ('k') | base/debug/crash_logging_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 <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;
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
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-chunked 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 = 0; i < crash_key->num_chunks; ++i) {
71 g_clear_key_func_(base::StringPrintf(kChunkFormatString, key.data(), i+1));
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
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 size_t InitCrashKeys(const CrashKey* const keys, size_t count) {
120 DCHECK(!g_crash_keys_) << "Crash logging may only be initialized once";
121 if (!keys) {
122 delete g_crash_keys_;
123 g_crash_keys_ = NULL;
124 return 0;
125 }
126
127 g_crash_keys_ = new std::map<base::StringPiece, CrashKey>;
128
129 size_t total_keys = 0;
130 for (size_t i = 0; i < count; ++i) {
131 g_crash_keys_->insert(std::make_pair(keys[i].key_name, keys[i]));
132 total_keys += keys[i].num_chunks;
133 }
Scott Hess - ex-Googler 2013/01/09 23:40:19 Doing DCHECK_EQ(count, g_crash_keys_->size()) woul
Robert Sesek 2013/01/10 16:52:50 Good idea. Done.
134
135 return total_keys;
136 }
137
138 const CrashKey* LookupCrashKey(const base::StringPiece& key) {
139 std::map<base::StringPiece, CrashKey>::const_iterator it =
140 g_crash_keys_->find(key.as_string());
141 if (it == g_crash_keys_->end())
142 return NULL;
143 return &(it->second);
144 }
145
73 void SetCrashKeyReportingFunctions( 146 void SetCrashKeyReportingFunctions(
74 SetCrashKeyValueFuncT set_key_func, 147 SetCrashKeyValueFuncT set_key_func,
75 ClearCrashKeyValueFuncT clear_key_func) { 148 ClearCrashKeyValueFuncT clear_key_func) {
76 g_set_key_func_ = set_key_func; 149 g_set_key_func_ = set_key_func;
77 g_clear_key_func_ = clear_key_func; 150 g_clear_key_func_ = clear_key_func;
78 } 151 }
79 152
153 std::vector<std::string> ChunkCrashKeyValue(const CrashKey& crash_key,
154 const base::StringPiece& value) {
155 std::string value_string = value.as_string();
156 std::vector<std::string> chunks;
157 for (size_t i = 0, offset = 0;
158 i < crash_key.num_chunks && offset < value_string.length();
159 ++i) {
160 std::string chunk = value_string.substr(offset, crash_key.max_length);
161 chunks.push_back(chunk);
162 offset += chunk.length();
163 }
164 return chunks;
165 }
166
167 void ResetCrashLoggingForTesting() {
168 delete g_crash_keys_;
169 g_crash_keys_ = NULL;
170 g_set_key_func_ = NULL;
171 g_clear_key_func_ = NULL;
172 }
173
80 } // namespace debug 174 } // namespace debug
81 } // namespace base 175 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/crash_logging.h ('k') | base/debug/crash_logging_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698