blob: 163f471021dfa12cc3591773a55b89a0d235b63c [file] [log] [blame]
[email protected]695f8dd2012-02-03 04:02:181// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
[email protected]1ffe08c12008-08-13 11:15:114
[email protected]58580352010-10-26 04:07:505#include "base/debug/stack_trace.h"
[email protected]1ffe08c12008-08-13 11:15:116
[email protected]96fd0032009-04-24 00:13:087#include <errno.h>
[email protected]bed545e2010-12-30 00:16:198#include <execinfo.h>
[email protected]e6f456242008-10-08 15:27:409#include <fcntl.h>
[email protected]d1fc4372009-01-16 22:06:1910#include <stdio.h>
[email protected]54823b0f2010-03-18 04:30:1411#include <stdlib.h>
[email protected]0c6f3b0c2011-04-15 06:15:0412#include <sys/param.h>
[email protected]e6f456242008-10-08 15:27:4013#include <sys/stat.h>
[email protected]1ffe08c12008-08-13 11:15:1114#include <sys/types.h>
[email protected]e6f456242008-10-08 15:27:4015#include <unistd.h>
[email protected]1ffe08c12008-08-13 11:15:1116
[email protected]6de328f2010-10-02 03:21:4617#include <string>
[email protected]f1633932010-08-17 23:05:2818#include <vector>
19
[email protected]79b6fa62009-10-14 03:01:4420#if defined(__GLIBCXX__)
21#include <cxxabi.h>
22#endif
23
[email protected]6e4bf0b2009-09-17 16:12:3624#if defined(OS_MACOSX)
25#include <AvailabilityMacros.h>
26#endif
27
[email protected]e6f456242008-10-08 15:27:4028#include "base/basictypes.h"
[email protected]157c61b2009-05-01 21:37:3129#include "base/eintr_wrapper.h"
[email protected]0dfc81b2008-08-25 03:44:4030#include "base/logging.h"
[email protected]3b63f8f42011-03-28 01:54:1531#include "base/memory/scoped_ptr.h"
[email protected]57b765672009-10-13 18:27:4032#include "base/safe_strerror_posix.h"
[email protected]1ffe08c12008-08-13 11:15:1133#include "base/string_piece.h"
[email protected]f1633932010-08-17 23:05:2834#include "base/stringprintf.h"
[email protected]48c27f72010-01-26 06:26:2635
36#if defined(USE_SYMBOLIZE)
37#include "base/third_party/symbolize/symbolize.h"
38#endif
[email protected]1ffe08c12008-08-13 11:15:1139
[email protected]58580352010-10-26 04:07:5040namespace base {
41namespace debug {
42
[email protected]79b6fa62009-10-14 03:01:4443namespace {
[email protected]58580352010-10-26 04:07:5044
[email protected]79b6fa62009-10-14 03:01:4445// The prefix used for mangled symbols, per the Itanium C++ ABI:
46// http://www.codesourcery.com/cxx-abi/abi.html#mangling
47const 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
51const char kSymbolCharacters[] =
52 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
53
[email protected]ffa59d62010-09-13 18:17:4154#if !defined(USE_SYMBOLIZE)
[email protected]79b6fa62009-10-14 03:01:4455// Demangles C++ symbols in the given text. Example:
56//
[email protected]016498e2010-12-03 00:59:2357// "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]"
[email protected]79b6fa62009-10-14 03:01:4458// =>
[email protected]016498e2010-12-03 00:59:2359// "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]"
[email protected]79b6fa62009-10-14 03:01:4460void 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]ffa59d62010-09-13 18:17:41100#endif // !defined(USE_SYMBOLIZE)
[email protected]48c27f72010-01-26 06:26:26101
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]6adad202010-09-29 23:41:59104// 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]501dfc42011-04-14 16:34:00106bool GetBacktraceStrings(void *const *trace, int size,
[email protected]6adad202010-09-29 23:41:59107 std::vector<std::string>* trace_strings,
108 std::string* error_message) {
[email protected]48c27f72010-01-26 06:26:26109 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]f1633932010-08-17 23:05:28120 trace_strings->push_back(
121 base::StringPrintf("%s [%p]", symbol, trace[i]));
[email protected]48c27f72010-01-26 06:26:26122 symbolized = true;
123 } else {
[email protected]f1633932010-08-17 23:05:28124 trace_strings->push_back(base::StringPrintf("%p", trace[i]));
[email protected]48c27f72010-01-26 06:26:26125 }
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]6adad202010-09-29 23:41:59137 if (error_message)
138 *error_message = safe_strerror(errno);
[email protected]48c27f72010-01-26 06:26:26139 for (int i = 0; i < size; ++i) {
[email protected]f1633932010-08-17 23:05:28140 trace_strings->push_back(base::StringPrintf("%p", trace[i]));
[email protected]48c27f72010-01-26 06:26:26141 }
142 }
143#endif // defined(USE_SYMBOLIZE)
144
145 return symbolized;
146}
147
[email protected]79b6fa62009-10-14 03:01:44148} // namespace
149
[email protected]5485cdc2009-01-16 21:17:30150StackTrace::StackTrace() {
[email protected]6e4bf0b2009-09-17 16:12:36151#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56152 if (backtrace == NULL) {
[email protected]9543af052009-09-15 22:42:59153 count_ = 0;
[email protected]6e4bf0b2009-09-17 16:12:36154 return;
[email protected]9543af052009-09-15 22:42:59155 }
[email protected]6e4bf0b2009-09-17 16:12:36156#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]5485cdc2009-01-16 21:17:30160}
161
[email protected]501dfc42011-04-14 16:34:00162void StackTrace::PrintBacktrace() const {
[email protected]6e4bf0b2009-09-17 16:12:36163#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56164 if (backtrace_symbols_fd == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36165 return;
166#endif
167 fflush(stderr);
[email protected]48c27f72010-01-26 06:26:26168 std::vector<std::string> trace_strings;
[email protected]6adad202010-09-29 23:41:59169 GetBacktraceStrings(trace_, count_, &trace_strings, NULL);
[email protected]48c27f72010-01-26 06:26:26170 for (size_t i = 0; i < trace_strings.size(); ++i) {
[email protected]695f8dd2012-02-03 04:02:18171 fprintf(stderr, "\t%s\n", trace_strings[i].c_str());
[email protected]48c27f72010-01-26 06:26:26172 }
[email protected]5485cdc2009-01-16 21:17:30173}
[email protected]96fd0032009-04-24 00:13:08174
[email protected]501dfc42011-04-14 16:34:00175void StackTrace::OutputToStream(std::ostream* os) const {
[email protected]6e4bf0b2009-09-17 16:12:36176#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56177 if (backtrace_symbols == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36178 return;
179#endif
[email protected]48c27f72010-01-26 06:26:26180 std::vector<std::string> trace_strings;
[email protected]6adad202010-09-29 23:41:59181 std::string error_message;
182 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) {
[email protected]48c27f72010-01-26 06:26:26183 (*os) << "Backtrace:\n";
184 } else {
[email protected]6adad202010-09-29 23:41:59185 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]48c27f72010-01-26 06:26:26189 }
190
191 for (size_t i = 0; i < trace_strings.size(); ++i) {
192 (*os) << "\t" << trace_strings[i] << "\n";
[email protected]96fd0032009-04-24 00:13:08193 }
194}
[email protected]58580352010-10-26 04:07:50195
196} // namespace debug
197} // namespace base