OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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_util.h" | 5 #include "base/debug_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <stdlib.h> | 10 #include <stdlib.h> |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 search_from = mangled_start + 2; | 92 search_from = mangled_start + 2; |
93 } | 93 } |
94 } | 94 } |
95 | 95 |
96 #endif // defined(__GLIBCXX__) | 96 #endif // defined(__GLIBCXX__) |
97 } | 97 } |
98 #endif // !defined(USE_SYMBOLIZE) | 98 #endif // !defined(USE_SYMBOLIZE) |
99 | 99 |
100 // Gets the backtrace as a vector of strings. If possible, resolve symbol | 100 // Gets the backtrace as a vector of strings. If possible, resolve symbol |
101 // names and attach these. Otherwise just use raw addresses. Returns true | 101 // names and attach these. Otherwise just use raw addresses. Returns true |
102 // if any symbol name is resolved. | 102 // if any symbol name is resolved. Returns false on error and *may* fill |
| 103 // in |error_message| if an error message is available. |
103 bool GetBacktraceStrings(void **trace, int size, | 104 bool GetBacktraceStrings(void **trace, int size, |
104 std::vector<std::string>* trace_strings) { | 105 std::vector<std::string>* trace_strings, |
| 106 std::string* error_message) { |
105 bool symbolized = false; | 107 bool symbolized = false; |
106 | 108 |
107 #if defined(USE_SYMBOLIZE) | 109 #if defined(USE_SYMBOLIZE) |
108 for (int i = 0; i < size; ++i) { | 110 for (int i = 0; i < size; ++i) { |
109 char symbol[1024]; | 111 char symbol[1024]; |
110 // Subtract by one as return address of function may be in the next | 112 // Subtract by one as return address of function may be in the next |
111 // function when a function is annotated as noreturn. | 113 // function when a function is annotated as noreturn. |
112 if (google::Symbolize(static_cast<char *>(trace[i]) - 1, | 114 if (google::Symbolize(static_cast<char *>(trace[i]) - 1, |
113 symbol, sizeof(symbol))) { | 115 symbol, sizeof(symbol))) { |
114 // Don't call DemangleSymbols() here as the symbol is demangled by | 116 // Don't call DemangleSymbols() here as the symbol is demangled by |
115 // google::Symbolize(). | 117 // google::Symbolize(). |
116 trace_strings->push_back( | 118 trace_strings->push_back( |
117 base::StringPrintf("%s [%p]", symbol, trace[i])); | 119 base::StringPrintf("%s [%p]", symbol, trace[i])); |
118 symbolized = true; | 120 symbolized = true; |
119 } else { | 121 } else { |
120 trace_strings->push_back(base::StringPrintf("%p", trace[i])); | 122 trace_strings->push_back(base::StringPrintf("%p", trace[i])); |
121 } | 123 } |
122 } | 124 } |
123 #else | 125 #else |
124 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size)); | 126 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size)); |
125 if (trace_symbols.get()) { | 127 if (trace_symbols.get()) { |
126 for (int i = 0; i < size; ++i) { | 128 for (int i = 0; i < size; ++i) { |
127 std::string trace_symbol = trace_symbols.get()[i]; | 129 std::string trace_symbol = trace_symbols.get()[i]; |
128 DemangleSymbols(&trace_symbol); | 130 DemangleSymbols(&trace_symbol); |
129 trace_strings->push_back(trace_symbol); | 131 trace_strings->push_back(trace_symbol); |
130 } | 132 } |
131 symbolized = true; | 133 symbolized = true; |
132 } else { | 134 } else { |
| 135 if (error_message) |
| 136 *error_message = safe_strerror(errno); |
133 for (int i = 0; i < size; ++i) { | 137 for (int i = 0; i < size; ++i) { |
134 trace_strings->push_back(base::StringPrintf("%p", trace[i])); | 138 trace_strings->push_back(base::StringPrintf("%p", trace[i])); |
135 } | 139 } |
136 } | 140 } |
137 #endif // defined(USE_SYMBOLIZE) | 141 #endif // defined(USE_SYMBOLIZE) |
138 | 142 |
139 return symbolized; | 143 return symbolized; |
140 } | 144 } |
141 | 145 |
142 } // namespace | 146 } // namespace |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
275 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | 279 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
276 } | 280 } |
277 | 281 |
278 void StackTrace::PrintBacktrace() { | 282 void StackTrace::PrintBacktrace() { |
279 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | 283 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
280 if (backtrace_symbols_fd == NULL) | 284 if (backtrace_symbols_fd == NULL) |
281 return; | 285 return; |
282 #endif | 286 #endif |
283 fflush(stderr); | 287 fflush(stderr); |
284 std::vector<std::string> trace_strings; | 288 std::vector<std::string> trace_strings; |
285 GetBacktraceStrings(trace_, count_, &trace_strings); | 289 GetBacktraceStrings(trace_, count_, &trace_strings, NULL); |
286 for (size_t i = 0; i < trace_strings.size(); ++i) { | 290 for (size_t i = 0; i < trace_strings.size(); ++i) { |
287 std::cerr << "\t" << trace_strings[i] << "\n"; | 291 std::cerr << "\t" << trace_strings[i] << "\n"; |
288 } | 292 } |
289 } | 293 } |
290 | 294 |
291 void StackTrace::OutputToStream(std::ostream* os) { | 295 void StackTrace::OutputToStream(std::ostream* os) { |
292 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 | 296 #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
293 if (backtrace_symbols == NULL) | 297 if (backtrace_symbols == NULL) |
294 return; | 298 return; |
295 #endif | 299 #endif |
296 std::vector<std::string> trace_strings; | 300 std::vector<std::string> trace_strings; |
297 if (GetBacktraceStrings(trace_, count_, &trace_strings)) { | 301 std::string error_message; |
| 302 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) { |
298 (*os) << "Backtrace:\n"; | 303 (*os) << "Backtrace:\n"; |
299 } else { | 304 } else { |
300 (*os) << "Unable get symbols for backtrace (" << safe_strerror(errno) | 305 if (!error_message.empty()) |
301 << "). Dumping raw addresses in trace:\n"; | 306 error_message = " (" + error_message + ")"; |
| 307 (*os) << "Unable to get symbols for backtrace" << error_message << ". " |
| 308 << "Dumping raw addresses in trace:\n"; |
302 } | 309 } |
303 | 310 |
304 for (size_t i = 0; i < trace_strings.size(); ++i) { | 311 for (size_t i = 0; i < trace_strings.size(); ++i) { |
305 (*os) << "\t" << trace_strings[i] << "\n"; | 312 (*os) << "\t" << trace_strings[i] << "\n"; |
306 } | 313 } |
307 } | 314 } |
OLD | NEW |