[email protected] | 695f8dd | 2012-02-03 04:02:18 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 4 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 5 | #include "base/debug/stack_trace.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 6 | |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | bed545e | 2010-12-30 00:16:19 | [diff] [blame] | 8 | #include <execinfo.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 9 | #include <fcntl.h> |
[email protected] | d1fc437 | 2009-01-16 22:06:19 | [diff] [blame] | 10 | #include <stdio.h> |
[email protected] | 54823b0f | 2010-03-18 04:30:14 | [diff] [blame] | 11 | #include <stdlib.h> |
[email protected] | 0c6f3b0c | 2011-04-15 06:15:04 | [diff] [blame] | 12 | #include <sys/param.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 13 | #include <sys/stat.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 14 | #include <sys/types.h> |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 15 | #include <unistd.h> |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 16 | |
[email protected] | 6de328f | 2010-10-02 03:21:46 | [diff] [blame] | 17 | #include <string> |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 18 | #include <vector> |
| 19 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 20 | #if defined(__GLIBCXX__) |
| 21 | #include <cxxabi.h> |
| 22 | #endif |
| 23 | |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 24 | #if defined(OS_MACOSX) |
| 25 | #include <AvailabilityMacros.h> |
| 26 | #endif |
| 27 | |
[email protected] | e6f45624 | 2008-10-08 15:27:40 | [diff] [blame] | 28 | #include "base/basictypes.h" |
[email protected] | 157c61b | 2009-05-01 21:37:31 | [diff] [blame] | 29 | #include "base/eintr_wrapper.h" |
[email protected] | 0dfc81b | 2008-08-25 03:44:40 | [diff] [blame] | 30 | #include "base/logging.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 31 | #include "base/memory/scoped_ptr.h" |
[email protected] | 57b76567 | 2009-10-13 18:27:40 | [diff] [blame] | 32 | #include "base/safe_strerror_posix.h" |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 33 | #include "base/string_piece.h" |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 34 | #include "base/stringprintf.h" |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 35 | |
| 36 | #if defined(USE_SYMBOLIZE) |
| 37 | #include "base/third_party/symbolize/symbolize.h" |
| 38 | #endif |
[email protected] | 1ffe08c1 | 2008-08-13 11:15:11 | [diff] [blame] | 39 | |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 40 | namespace base { |
| 41 | namespace debug { |
| 42 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 43 | namespace { |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 44 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 45 | // The prefix used for mangled symbols, per the Itanium C++ ABI: |
| 46 | // http://www.codesourcery.com/cxx-abi/abi.html#mangling |
| 47 | const char kMangledSymbolPrefix[] = "_Z"; |
| 48 | |
| 49 | // Characters that can be used for symbols, generated by Ruby: |
| 50 | // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join |
| 51 | const char kSymbolCharacters[] = |
| 52 | "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
| 53 | |
[email protected] | ffa59d6 | 2010-09-13 18:17:41 | [diff] [blame] | 54 | #if !defined(USE_SYMBOLIZE) |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 55 | // Demangles C++ symbols in the given text. Example: |
| 56 | // |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 57 | // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 58 | // => |
[email protected] | 016498e | 2010-12-03 00:59:23 | [diff] [blame] | 59 | // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 60 | void DemangleSymbols(std::string* text) { |
| 61 | #if defined(__GLIBCXX__) |
| 62 | |
| 63 | std::string::size_type search_from = 0; |
| 64 | while (search_from < text->size()) { |
| 65 | // Look for the start of a mangled symbol, from search_from. |
| 66 | std::string::size_type mangled_start = |
| 67 | text->find(kMangledSymbolPrefix, search_from); |
| 68 | if (mangled_start == std::string::npos) { |
| 69 | break; // Mangled symbol not found. |
| 70 | } |
| 71 | |
| 72 | // Look for the end of the mangled symbol. |
| 73 | std::string::size_type mangled_end = |
| 74 | text->find_first_not_of(kSymbolCharacters, mangled_start); |
| 75 | if (mangled_end == std::string::npos) { |
| 76 | mangled_end = text->size(); |
| 77 | } |
| 78 | std::string mangled_symbol = |
| 79 | text->substr(mangled_start, mangled_end - mangled_start); |
| 80 | |
| 81 | // Try to demangle the mangled symbol candidate. |
| 82 | int status = 0; |
| 83 | scoped_ptr_malloc<char> demangled_symbol( |
| 84 | abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status)); |
| 85 | if (status == 0) { // Demangling is successful. |
| 86 | // Remove the mangled symbol. |
| 87 | text->erase(mangled_start, mangled_end - mangled_start); |
| 88 | // Insert the demangled symbol. |
| 89 | text->insert(mangled_start, demangled_symbol.get()); |
| 90 | // Next time, we'll start right after the demangled symbol we inserted. |
| 91 | search_from = mangled_start + strlen(demangled_symbol.get()); |
| 92 | } else { |
| 93 | // Failed to demangle. Retry after the "_Z" we just found. |
| 94 | search_from = mangled_start + 2; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | #endif // defined(__GLIBCXX__) |
| 99 | } |
[email protected] | ffa59d6 | 2010-09-13 18:17:41 | [diff] [blame] | 100 | #endif // !defined(USE_SYMBOLIZE) |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 101 | |
| 102 | // Gets the backtrace as a vector of strings. If possible, resolve symbol |
| 103 | // names and attach these. Otherwise just use raw addresses. Returns true |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 104 | // if any symbol name is resolved. Returns false on error and *may* fill |
| 105 | // in |error_message| if an error message is available. |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame] | 106 | bool GetBacktraceStrings(void *const *trace, int size, |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 107 | std::vector<std::string>* trace_strings, |
| 108 | std::string* error_message) { |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 109 | bool symbolized = false; |
| 110 | |
| 111 | #if defined(USE_SYMBOLIZE) |
| 112 | for (int i = 0; i < size; ++i) { |
| 113 | char symbol[1024]; |
| 114 | // Subtract by one as return address of function may be in the next |
| 115 | // function when a function is annotated as noreturn. |
| 116 | if (google::Symbolize(static_cast<char *>(trace[i]) - 1, |
| 117 | symbol, sizeof(symbol))) { |
| 118 | // Don't call DemangleSymbols() here as the symbol is demangled by |
| 119 | // google::Symbolize(). |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 120 | trace_strings->push_back( |
| 121 | base::StringPrintf("%s [%p]", symbol, trace[i])); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 122 | symbolized = true; |
| 123 | } else { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 124 | trace_strings->push_back(base::StringPrintf("%p", trace[i])); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | #else |
| 128 | scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size)); |
| 129 | if (trace_symbols.get()) { |
| 130 | for (int i = 0; i < size; ++i) { |
| 131 | std::string trace_symbol = trace_symbols.get()[i]; |
| 132 | DemangleSymbols(&trace_symbol); |
| 133 | trace_strings->push_back(trace_symbol); |
| 134 | } |
| 135 | symbolized = true; |
| 136 | } else { |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 137 | if (error_message) |
| 138 | *error_message = safe_strerror(errno); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 139 | for (int i = 0; i < size; ++i) { |
[email protected] | f163393 | 2010-08-17 23:05:28 | [diff] [blame] | 140 | trace_strings->push_back(base::StringPrintf("%p", trace[i])); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | #endif // defined(USE_SYMBOLIZE) |
| 144 | |
| 145 | return symbolized; |
| 146 | } |
| 147 | |
[email protected] | 79b6fa6 | 2009-10-14 03:01:44 | [diff] [blame] | 148 | } // namespace |
| 149 | |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 150 | StackTrace::StackTrace() { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 151 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame] | 152 | if (backtrace == NULL) { |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 153 | count_ = 0; |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 154 | return; |
[email protected] | 9543af05 | 2009-09-15 22:42:59 | [diff] [blame] | 155 | } |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 156 | #endif |
| 157 | // Though the backtrace API man page does not list any possible negative |
| 158 | // return values, we take no chance. |
| 159 | count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame] | 162 | void StackTrace::PrintBacktrace() const { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 163 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame] | 164 | if (backtrace_symbols_fd == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 165 | return; |
| 166 | #endif |
| 167 | fflush(stderr); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 168 | std::vector<std::string> trace_strings; |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 169 | GetBacktraceStrings(trace_, count_, &trace_strings, NULL); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 170 | for (size_t i = 0; i < trace_strings.size(); ++i) { |
[email protected] | 695f8dd | 2012-02-03 04:02:18 | [diff] [blame] | 171 | fprintf(stderr, "\t%s\n", trace_strings[i].c_str()); |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 172 | } |
[email protected] | 5485cdc | 2009-01-16 21:17:30 | [diff] [blame] | 173 | } |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 174 | |
[email protected] | 501dfc4 | 2011-04-14 16:34:00 | [diff] [blame] | 175 | void StackTrace::OutputToStream(std::ostream* os) const { |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 176 | #if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5 |
[email protected] | f358a0a | 2009-09-18 15:27:56 | [diff] [blame] | 177 | if (backtrace_symbols == NULL) |
[email protected] | 6e4bf0b | 2009-09-17 16:12:36 | [diff] [blame] | 178 | return; |
| 179 | #endif |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 180 | std::vector<std::string> trace_strings; |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 181 | std::string error_message; |
| 182 | if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) { |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 183 | (*os) << "Backtrace:\n"; |
| 184 | } else { |
[email protected] | 6adad20 | 2010-09-29 23:41:59 | [diff] [blame] | 185 | if (!error_message.empty()) |
| 186 | error_message = " (" + error_message + ")"; |
| 187 | (*os) << "Unable to get symbols for backtrace" << error_message << ". " |
| 188 | << "Dumping raw addresses in trace:\n"; |
[email protected] | 48c27f7 | 2010-01-26 06:26:26 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | for (size_t i = 0; i < trace_strings.size(); ++i) { |
| 192 | (*os) << "\t" << trace_strings[i] << "\n"; |
[email protected] | 96fd003 | 2009-04-24 00:13:08 | [diff] [blame] | 193 | } |
| 194 | } |
[email protected] | 5858035 | 2010-10-26 04:07:50 | [diff] [blame] | 195 | |
| 196 | } // namespace debug |
| 197 | } // namespace base |