| OLD | NEW |
| 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/stack_trace.h" | 5 #include "base/debug/stack_trace.h" |
| 6 | 6 |
| 7 #include <errno.h> | 7 #include <errno.h> |
| 8 #include <execinfo.h> | |
| 9 #include <fcntl.h> | 8 #include <fcntl.h> |
| 10 #include <signal.h> | 9 #include <signal.h> |
| 11 #include <stdio.h> | 10 #include <stdio.h> |
| 12 #include <stdlib.h> | 11 #include <stdlib.h> |
| 13 #include <sys/param.h> | 12 #include <sys/param.h> |
| 14 #include <sys/stat.h> | 13 #include <sys/stat.h> |
| 15 #include <sys/types.h> | 14 #include <sys/types.h> |
| 16 #include <unistd.h> | 15 #include <unistd.h> |
| 17 | 16 |
| 18 #include <map> | 17 #include <map> |
| 19 #include <ostream> | 18 #include <ostream> |
| 20 #include <string> | 19 #include <string> |
| 21 #include <vector> | 20 #include <vector> |
| 22 | 21 |
| 23 #if defined(__GLIBCXX__) | 22 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) |
| 24 #include <cxxabi.h> | 23 #include <cxxabi.h> |
| 24 #include <execinfo.h> |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 #if defined(OS_MACOSX) | 27 #if defined(OS_MACOSX) |
| 28 #include <AvailabilityMacros.h> | 28 #include <AvailabilityMacros.h> |
| 29 #endif | 29 #endif |
| 30 | 30 |
| 31 #include "base/basictypes.h" | 31 #include "base/basictypes.h" |
| 32 #include "base/debug/debugger.h" | 32 #include "base/debug/debugger.h" |
| 33 #include "base/debug/proc_maps_linux.h" | 33 #include "base/debug/proc_maps_linux.h" |
| 34 #include "base/logging.h" | 34 #include "base/logging.h" |
| (...skipping 29 matching lines...) Expand all Loading... |
| 64 #if !defined(USE_SYMBOLIZE) | 64 #if !defined(USE_SYMBOLIZE) |
| 65 // Demangles C++ symbols in the given text. Example: | 65 // Demangles C++ symbols in the given text. Example: |
| 66 // | 66 // |
| 67 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 67 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
| 68 // => | 68 // => |
| 69 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 69 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
| 70 void DemangleSymbols(std::string* text) { | 70 void DemangleSymbols(std::string* text) { |
| 71 // Note: code in this function is NOT async-signal safe (std::string uses | 71 // Note: code in this function is NOT async-signal safe (std::string uses |
| 72 // malloc internally). | 72 // malloc internally). |
| 73 | 73 |
| 74 #if defined(__GLIBCXX__) | 74 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) |
| 75 | 75 |
| 76 std::string::size_type search_from = 0; | 76 std::string::size_type search_from = 0; |
| 77 while (search_from < text->size()) { | 77 while (search_from < text->size()) { |
| 78 // Look for the start of a mangled symbol, from search_from. | 78 // Look for the start of a mangled symbol, from search_from. |
| 79 std::string::size_type mangled_start = | 79 std::string::size_type mangled_start = |
| 80 text->find(kMangledSymbolPrefix, search_from); | 80 text->find(kMangledSymbolPrefix, search_from); |
| 81 if (mangled_start == std::string::npos) { | 81 if (mangled_start == std::string::npos) { |
| 82 break; // Mangled symbol not found. | 82 break; // Mangled symbol not found. |
| 83 } | 83 } |
| 84 | 84 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 101 // Insert the demangled symbol. | 101 // Insert the demangled symbol. |
| 102 text->insert(mangled_start, demangled_symbol.get()); | 102 text->insert(mangled_start, demangled_symbol.get()); |
| 103 // Next time, we'll start right after the demangled symbol we inserted. | 103 // Next time, we'll start right after the demangled symbol we inserted. |
| 104 search_from = mangled_start + strlen(demangled_symbol.get()); | 104 search_from = mangled_start + strlen(demangled_symbol.get()); |
| 105 } else { | 105 } else { |
| 106 // Failed to demangle. Retry after the "_Z" we just found. | 106 // Failed to demangle. Retry after the "_Z" we just found. |
| 107 search_from = mangled_start + 2; | 107 search_from = mangled_start + 2; |
| 108 } | 108 } |
| 109 } | 109 } |
| 110 | 110 |
| 111 #endif // defined(__GLIBCXX__) | 111 #endif // defined(__GLIBCXX__) && !defined(__UCLIBC__) |
| 112 } | 112 } |
| 113 #endif // !defined(USE_SYMBOLIZE) | 113 #endif // !defined(USE_SYMBOLIZE) |
| 114 | 114 |
| 115 class BacktraceOutputHandler { | 115 class BacktraceOutputHandler { |
| 116 public: | 116 public: |
| 117 virtual void HandleOutput(const char* output) = 0; | 117 virtual void HandleOutput(const char* output) = 0; |
| 118 | 118 |
| 119 protected: | 119 protected: |
| 120 virtual ~BacktraceOutputHandler() {} | 120 virtual ~BacktraceOutputHandler() {} |
| 121 }; | 121 }; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 // Subtract by one as return address of function may be in the next | 160 // Subtract by one as return address of function may be in the next |
| 161 // function when a function is annotated as noreturn. | 161 // function when a function is annotated as noreturn. |
| 162 void* address = static_cast<char*>(trace[i]) - 1; | 162 void* address = static_cast<char*>(trace[i]) - 1; |
| 163 if (google::Symbolize(address, buf, sizeof(buf))) | 163 if (google::Symbolize(address, buf, sizeof(buf))) |
| 164 handler->HandleOutput(buf); | 164 handler->HandleOutput(buf); |
| 165 else | 165 else |
| 166 handler->HandleOutput("<unknown>"); | 166 handler->HandleOutput("<unknown>"); |
| 167 | 167 |
| 168 handler->HandleOutput("\n"); | 168 handler->HandleOutput("\n"); |
| 169 } | 169 } |
| 170 #else | 170 #elif defined(__GLIBCXX__) && !defined(__UCLIBC__) |
| 171 bool printed = false; | 171 bool printed = false; |
| 172 | 172 |
| 173 // Below part is async-signal unsafe (uses malloc), so execute it only | 173 // Below part is async-signal unsafe (uses malloc), so execute it only |
| 174 // when we are not executing the signal handler. | 174 // when we are not executing the signal handler. |
| 175 if (in_signal_handler == 0) { | 175 if (in_signal_handler == 0) { |
| 176 scoped_ptr<char*, FreeDeleter> | 176 scoped_ptr<char*, FreeDeleter> |
| 177 trace_symbols(backtrace_symbols(trace, size)); | 177 trace_symbols(backtrace_symbols(trace, size)); |
| 178 if (trace_symbols.get()) { | 178 if (trace_symbols.get()) { |
| 179 for (size_t i = 0; i < size; ++i) { | 179 for (size_t i = 0; i < size; ++i) { |
| 180 std::string trace_symbol = trace_symbols.get()[i]; | 180 std::string trace_symbol = trace_symbols.get()[i]; |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 272 PrintToStderr(" <unknown> "); | 272 PrintToStderr(" <unknown> "); |
| 273 } | 273 } |
| 274 if (signal == SIGBUS || signal == SIGFPE || | 274 if (signal == SIGBUS || signal == SIGFPE || |
| 275 signal == SIGILL || signal == SIGSEGV) { | 275 signal == SIGILL || signal == SIGSEGV) { |
| 276 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), | 276 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), |
| 277 buf, sizeof(buf), 16, 12); | 277 buf, sizeof(buf), 16, 12); |
| 278 PrintToStderr(buf); | 278 PrintToStderr(buf); |
| 279 } | 279 } |
| 280 PrintToStderr("\n"); | 280 PrintToStderr("\n"); |
| 281 | 281 |
| 282 #if !defined(__UCLIBC__) |
| 282 debug::StackTrace().Print(); | 283 debug::StackTrace().Print(); |
| 284 #endif |
| 283 | 285 |
| 284 #if defined(OS_LINUX) | 286 #if defined(OS_LINUX) |
| 285 #if ARCH_CPU_X86_FAMILY | 287 #if ARCH_CPU_X86_FAMILY |
| 286 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 288 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
| 287 const struct { | 289 const struct { |
| 288 const char* label; | 290 const char* label; |
| 289 greg_t value; | 291 greg_t value; |
| 290 } registers[] = { | 292 } registers[] = { |
| 291 #if ARCH_CPU_32_BITS | 293 #if ARCH_CPU_32_BITS |
| 292 { " gs: ", context->uc_mcontext.gregs[REG_GS] }, | 294 { " gs: ", context->uc_mcontext.gregs[REG_GS] }, |
| (...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 731 success &= (sigaction(SIGSYS, &action, NULL) == 0); | 733 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
| 732 #endif // !defined(OS_LINUX) | 734 #endif // !defined(OS_LINUX) |
| 733 | 735 |
| 734 return success; | 736 return success; |
| 735 } | 737 } |
| 736 | 738 |
| 737 StackTrace::StackTrace() { | 739 StackTrace::StackTrace() { |
| 738 // NOTE: This code MUST be async-signal safe (it's used by in-process | 740 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 739 // stack dumping signal handler). NO malloc or stdio is allowed here. | 741 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 740 | 742 |
| 743 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) |
| 741 // Though the backtrace API man page does not list any possible negative | 744 // Though the backtrace API man page does not list any possible negative |
| 742 // return values, we take no chance. | 745 // return values, we take no chance. |
| 743 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 746 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
| 747 #endif |
| 744 } | 748 } |
| 745 | 749 |
| 750 #if !defined(__UCLIBC__) |
| 746 void StackTrace::Print() const { | 751 void StackTrace::Print() const { |
| 747 // NOTE: This code MUST be async-signal safe (it's used by in-process | 752 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 748 // stack dumping signal handler). NO malloc or stdio is allowed here. | 753 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 749 | 754 |
| 750 PrintBacktraceOutputHandler handler; | 755 PrintBacktraceOutputHandler handler; |
| 751 ProcessBacktrace(trace_, count_, &handler); | 756 ProcessBacktrace(trace_, count_, &handler); |
| 752 } | 757 } |
| 753 | 758 |
| 754 void StackTrace::OutputToStream(std::ostream* os) const { | 759 void StackTrace::OutputToStream(std::ostream* os) const { |
| 755 StreamBacktraceOutputHandler handler(os); | 760 StreamBacktraceOutputHandler handler(os); |
| 756 ProcessBacktrace(trace_, count_, &handler); | 761 ProcessBacktrace(trace_, count_, &handler); |
| 757 } | 762 } |
| 763 #endif |
| 758 | 764 |
| 759 namespace internal { | 765 namespace internal { |
| 760 | 766 |
| 761 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 767 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
| 762 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { | 768 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { |
| 763 // Make sure we can write at least one NUL byte. | 769 // Make sure we can write at least one NUL byte. |
| 764 size_t n = 1; | 770 size_t n = 1; |
| 765 if (n > sz) | 771 if (n > sz) |
| 766 return NULL; | 772 return NULL; |
| 767 | 773 |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 816 *ptr = *start; | 822 *ptr = *start; |
| 817 *start++ = ch; | 823 *start++ = ch; |
| 818 } | 824 } |
| 819 return buf; | 825 return buf; |
| 820 } | 826 } |
| 821 | 827 |
| 822 } // namespace internal | 828 } // namespace internal |
| 823 | 829 |
| 824 } // namespace debug | 830 } // namespace debug |
| 825 } // namespace base | 831 } // namespace base |
| OLD | NEW |