blob: e4b0ef2923ce38aa78c9b71b1972e3c5db827aed [file] [log] [blame]
[email protected]f1633932010-08-17 23:05:281// Copyright (c) 2010 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]e6f456242008-10-08 15:27:408#include <fcntl.h>
[email protected]d1fc4372009-01-16 22:06:199#include <stdio.h>
[email protected]54823b0f2010-03-18 04:30:1410#include <stdlib.h>
[email protected]e6f456242008-10-08 15:27:4011#include <sys/stat.h>
[email protected]1ffe08c12008-08-13 11:15:1112#include <sys/sysctl.h>
13#include <sys/types.h>
[email protected]e6f456242008-10-08 15:27:4014#include <unistd.h>
[email protected]1ffe08c12008-08-13 11:15:1115
[email protected]6de328f2010-10-02 03:21:4616#include <string>
[email protected]f1633932010-08-17 23:05:2817#include <vector>
18
[email protected]79b6fa62009-10-14 03:01:4419#if defined(__GLIBCXX__)
20#include <cxxabi.h>
21#endif
22
[email protected]6e4bf0b2009-09-17 16:12:3623#if defined(OS_MACOSX)
24#include <AvailabilityMacros.h>
25#endif
26
[email protected]54823b0f2010-03-18 04:30:1427#include <iostream>
[email protected]54823b0f2010-03-18 04:30:1428
[email protected]e6f456242008-10-08 15:27:4029#include "base/basictypes.h"
[email protected]9543af052009-09-15 22:42:5930#include "base/compat_execinfo.h"
[email protected]157c61b2009-05-01 21:37:3131#include "base/eintr_wrapper.h"
[email protected]0dfc81b2008-08-25 03:44:4032#include "base/logging.h"
[email protected]57b765672009-10-13 18:27:4033#include "base/safe_strerror_posix.h"
[email protected]96fd0032009-04-24 00:13:0834#include "base/scoped_ptr.h"
[email protected]1ffe08c12008-08-13 11:15:1135#include "base/string_piece.h"
[email protected]f1633932010-08-17 23:05:2836#include "base/stringprintf.h"
[email protected]48c27f72010-01-26 06:26:2637
38#if defined(USE_SYMBOLIZE)
39#include "base/third_party/symbolize/symbolize.h"
40#endif
[email protected]1ffe08c12008-08-13 11:15:1141
[email protected]58580352010-10-26 04:07:5042namespace base {
43namespace debug {
44
[email protected]79b6fa62009-10-14 03:01:4445namespace {
[email protected]58580352010-10-26 04:07:5046
[email protected]79b6fa62009-10-14 03:01:4447// The prefix used for mangled symbols, per the Itanium C++ ABI:
48// http://www.codesourcery.com/cxx-abi/abi.html#mangling
49const char kMangledSymbolPrefix[] = "_Z";
50
51// Characters that can be used for symbols, generated by Ruby:
52// (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join
53const char kSymbolCharacters[] =
54 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_";
55
[email protected]ffa59d62010-09-13 18:17:4156#if !defined(USE_SYMBOLIZE)
[email protected]79b6fa62009-10-14 03:01:4457// Demangles C++ symbols in the given text. Example:
58//
59// "sconsbuild/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]"
60// =>
61// "sconsbuild/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]"
62void DemangleSymbols(std::string* text) {
63#if defined(__GLIBCXX__)
64
65 std::string::size_type search_from = 0;
66 while (search_from < text->size()) {
67 // Look for the start of a mangled symbol, from search_from.
68 std::string::size_type mangled_start =
69 text->find(kMangledSymbolPrefix, search_from);
70 if (mangled_start == std::string::npos) {
71 break; // Mangled symbol not found.
72 }
73
74 // Look for the end of the mangled symbol.
75 std::string::size_type mangled_end =
76 text->find_first_not_of(kSymbolCharacters, mangled_start);
77 if (mangled_end == std::string::npos) {
78 mangled_end = text->size();
79 }
80 std::string mangled_symbol =
81 text->substr(mangled_start, mangled_end - mangled_start);
82
83 // Try to demangle the mangled symbol candidate.
84 int status = 0;
85 scoped_ptr_malloc<char> demangled_symbol(
86 abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status));
87 if (status == 0) { // Demangling is successful.
88 // Remove the mangled symbol.
89 text->erase(mangled_start, mangled_end - mangled_start);
90 // Insert the demangled symbol.
91 text->insert(mangled_start, demangled_symbol.get());
92 // Next time, we'll start right after the demangled symbol we inserted.
93 search_from = mangled_start + strlen(demangled_symbol.get());
94 } else {
95 // Failed to demangle. Retry after the "_Z" we just found.
96 search_from = mangled_start + 2;
97 }
98 }
99
100#endif // defined(__GLIBCXX__)
101}
[email protected]ffa59d62010-09-13 18:17:41102#endif // !defined(USE_SYMBOLIZE)
[email protected]48c27f72010-01-26 06:26:26103
104// Gets the backtrace as a vector of strings. If possible, resolve symbol
105// names and attach these. Otherwise just use raw addresses. Returns true
[email protected]6adad202010-09-29 23:41:59106// if any symbol name is resolved. Returns false on error and *may* fill
107// in |error_message| if an error message is available.
[email protected]48c27f72010-01-26 06:26:26108bool GetBacktraceStrings(void **trace, int size,
[email protected]6adad202010-09-29 23:41:59109 std::vector<std::string>* trace_strings,
110 std::string* error_message) {
[email protected]48c27f72010-01-26 06:26:26111 bool symbolized = false;
112
113#if defined(USE_SYMBOLIZE)
114 for (int i = 0; i < size; ++i) {
115 char symbol[1024];
116 // Subtract by one as return address of function may be in the next
117 // function when a function is annotated as noreturn.
118 if (google::Symbolize(static_cast<char *>(trace[i]) - 1,
119 symbol, sizeof(symbol))) {
120 // Don't call DemangleSymbols() here as the symbol is demangled by
121 // google::Symbolize().
[email protected]f1633932010-08-17 23:05:28122 trace_strings->push_back(
123 base::StringPrintf("%s [%p]", symbol, trace[i]));
[email protected]48c27f72010-01-26 06:26:26124 symbolized = true;
125 } else {
[email protected]f1633932010-08-17 23:05:28126 trace_strings->push_back(base::StringPrintf("%p", trace[i]));
[email protected]48c27f72010-01-26 06:26:26127 }
128 }
129#else
130 scoped_ptr_malloc<char*> trace_symbols(backtrace_symbols(trace, size));
131 if (trace_symbols.get()) {
132 for (int i = 0; i < size; ++i) {
133 std::string trace_symbol = trace_symbols.get()[i];
134 DemangleSymbols(&trace_symbol);
135 trace_strings->push_back(trace_symbol);
136 }
137 symbolized = true;
138 } else {
[email protected]6adad202010-09-29 23:41:59139 if (error_message)
140 *error_message = safe_strerror(errno);
[email protected]48c27f72010-01-26 06:26:26141 for (int i = 0; i < size; ++i) {
[email protected]f1633932010-08-17 23:05:28142 trace_strings->push_back(base::StringPrintf("%p", trace[i]));
[email protected]48c27f72010-01-26 06:26:26143 }
144 }
145#endif // defined(USE_SYMBOLIZE)
146
147 return symbolized;
148}
149
[email protected]79b6fa62009-10-14 03:01:44150} // namespace
151
[email protected]5485cdc2009-01-16 21:17:30152StackTrace::StackTrace() {
[email protected]6e4bf0b2009-09-17 16:12:36153#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56154 if (backtrace == NULL) {
[email protected]9543af052009-09-15 22:42:59155 count_ = 0;
[email protected]6e4bf0b2009-09-17 16:12:36156 return;
[email protected]9543af052009-09-15 22:42:59157 }
[email protected]6e4bf0b2009-09-17 16:12:36158#endif
159 // Though the backtrace API man page does not list any possible negative
160 // return values, we take no chance.
161 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0);
[email protected]5485cdc2009-01-16 21:17:30162}
163
164void StackTrace::PrintBacktrace() {
[email protected]6e4bf0b2009-09-17 16:12:36165#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56166 if (backtrace_symbols_fd == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36167 return;
168#endif
169 fflush(stderr);
[email protected]48c27f72010-01-26 06:26:26170 std::vector<std::string> trace_strings;
[email protected]6adad202010-09-29 23:41:59171 GetBacktraceStrings(trace_, count_, &trace_strings, NULL);
[email protected]48c27f72010-01-26 06:26:26172 for (size_t i = 0; i < trace_strings.size(); ++i) {
173 std::cerr << "\t" << trace_strings[i] << "\n";
174 }
[email protected]5485cdc2009-01-16 21:17:30175}
[email protected]96fd0032009-04-24 00:13:08176
177void StackTrace::OutputToStream(std::ostream* os) {
[email protected]6e4bf0b2009-09-17 16:12:36178#if defined(OS_MACOSX) && MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
[email protected]f358a0a2009-09-18 15:27:56179 if (backtrace_symbols == NULL)
[email protected]6e4bf0b2009-09-17 16:12:36180 return;
181#endif
[email protected]48c27f72010-01-26 06:26:26182 std::vector<std::string> trace_strings;
[email protected]6adad202010-09-29 23:41:59183 std::string error_message;
184 if (GetBacktraceStrings(trace_, count_, &trace_strings, &error_message)) {
[email protected]48c27f72010-01-26 06:26:26185 (*os) << "Backtrace:\n";
186 } else {
[email protected]6adad202010-09-29 23:41:59187 if (!error_message.empty())
188 error_message = " (" + error_message + ")";
189 (*os) << "Unable to get symbols for backtrace" << error_message << ". "
190 << "Dumping raw addresses in trace:\n";
[email protected]48c27f72010-01-26 06:26:26191 }
192
193 for (size_t i = 0; i < trace_strings.size(); ++i) {
194 (*os) << "\t" << trace_strings[i] << "\n";
[email protected]96fd0032009-04-24 00:13:08195 }
196}
[email protected]58580352010-10-26 04:07:50197
198} // namespace debug
199} // namespace base