| 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 <fcntl.h> | 8 #include <fcntl.h> |
| 9 #include <signal.h> | 9 #include <signal.h> |
| 10 #include <stdio.h> | 10 #include <stdio.h> |
| 11 #include <stdlib.h> | 11 #include <stdlib.h> |
| 12 #include <sys/param.h> | 12 #include <sys/param.h> |
| 13 #include <sys/stat.h> | 13 #include <sys/stat.h> |
| 14 #include <sys/types.h> | 14 #include <sys/types.h> |
| 15 #include <unistd.h> | 15 #include <unistd.h> |
| 16 | 16 |
| 17 #include <map> | 17 #include <map> |
| 18 #include <ostream> | 18 #include <ostream> |
| 19 #include <string> | 19 #include <string> |
| 20 #include <vector> | 20 #include <vector> |
| 21 | 21 |
| 22 #if defined(__GLIBCXX__) | 22 #if defined(__GLIBCXX__) |
| 23 #include <cxxabi.h> | 23 #include <cxxabi.h> |
| 24 #endif | 24 #endif |
| 25 #if !defined(__UCLIBC__) | 25 #if !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 26 #include <execinfo.h> | 26 #include <execinfo.h> |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 #if defined(OS_MACOSX) | 29 #if defined(OS_MACOSX) |
| 30 #include <AvailabilityMacros.h> | 30 #include <AvailabilityMacros.h> |
| 31 #endif | 31 #endif |
| 32 | 32 |
| 33 #include "base/basictypes.h" | 33 #include "base/basictypes.h" |
| 34 #include "base/debug/debugger.h" | 34 #include "base/debug/debugger.h" |
| 35 #include "base/debug/proc_maps_linux.h" | 35 #include "base/debug/proc_maps_linux.h" |
| (...skipping 30 matching lines...) Expand all Loading... |
| 66 #if !defined(USE_SYMBOLIZE) | 66 #if !defined(USE_SYMBOLIZE) |
| 67 // Demangles C++ symbols in the given text. Example: | 67 // Demangles C++ symbols in the given text. Example: |
| 68 // | 68 // |
| 69 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 69 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
| 70 // => | 70 // => |
| 71 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 71 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
| 72 void DemangleSymbols(std::string* text) { | 72 void DemangleSymbols(std::string* text) { |
| 73 // Note: code in this function is NOT async-signal safe (std::string uses | 73 // Note: code in this function is NOT async-signal safe (std::string uses |
| 74 // malloc internally). | 74 // malloc internally). |
| 75 | 75 |
| 76 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) | 76 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 77 | 77 |
| 78 std::string::size_type search_from = 0; | 78 std::string::size_type search_from = 0; |
| 79 while (search_from < text->size()) { | 79 while (search_from < text->size()) { |
| 80 // Look for the start of a mangled symbol, from search_from. | 80 // Look for the start of a mangled symbol, from search_from. |
| 81 std::string::size_type mangled_start = | 81 std::string::size_type mangled_start = |
| 82 text->find(kMangledSymbolPrefix, search_from); | 82 text->find(kMangledSymbolPrefix, search_from); |
| 83 if (mangled_start == std::string::npos) { | 83 if (mangled_start == std::string::npos) { |
| 84 break; // Mangled symbol not found. | 84 break; // Mangled symbol not found. |
| 85 } | 85 } |
| 86 | 86 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 103 // Insert the demangled symbol. | 103 // Insert the demangled symbol. |
| 104 text->insert(mangled_start, demangled_symbol.get()); | 104 text->insert(mangled_start, demangled_symbol.get()); |
| 105 // Next time, we'll start right after the demangled symbol we inserted. | 105 // Next time, we'll start right after the demangled symbol we inserted. |
| 106 search_from = mangled_start + strlen(demangled_symbol.get()); | 106 search_from = mangled_start + strlen(demangled_symbol.get()); |
| 107 } else { | 107 } else { |
| 108 // Failed to demangle. Retry after the "_Z" we just found. | 108 // Failed to demangle. Retry after the "_Z" we just found. |
| 109 search_from = mangled_start + 2; | 109 search_from = mangled_start + 2; |
| 110 } | 110 } |
| 111 } | 111 } |
| 112 | 112 |
| 113 #endif // defined(__GLIBCXX__) && !defined(__UCLIBC__) | 113 #endif // defined(__GLIBCXX__) && !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 114 } | 114 } |
| 115 #endif // !defined(USE_SYMBOLIZE) | 115 #endif // !defined(USE_SYMBOLIZE) |
| 116 | 116 |
| 117 class BacktraceOutputHandler { | 117 class BacktraceOutputHandler { |
| 118 public: | 118 public: |
| 119 virtual void HandleOutput(const char* output) = 0; | 119 virtual void HandleOutput(const char* output) = 0; |
| 120 | 120 |
| 121 protected: | 121 protected: |
| 122 virtual ~BacktraceOutputHandler() {} | 122 virtual ~BacktraceOutputHandler() {} |
| 123 }; | 123 }; |
| 124 | 124 |
| 125 #if !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 125 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { | 126 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { |
| 126 // This should be more than enough to store a 64-bit number in hex: | 127 // This should be more than enough to store a 64-bit number in hex: |
| 127 // 16 hex digits + 1 for null-terminator. | 128 // 16 hex digits + 1 for null-terminator. |
| 128 char buf[17] = { '\0' }; | 129 char buf[17] = { '\0' }; |
| 129 handler->HandleOutput("0x"); | 130 handler->HandleOutput("0x"); |
| 130 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), | 131 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), |
| 131 buf, sizeof(buf), 16, 12); | 132 buf, sizeof(buf), 16, 12); |
| 132 handler->HandleOutput(buf); | 133 handler->HandleOutput(buf); |
| 133 } | 134 } |
| 134 | 135 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 162 // Subtract by one as return address of function may be in the next | 163 // Subtract by one as return address of function may be in the next |
| 163 // function when a function is annotated as noreturn. | 164 // function when a function is annotated as noreturn. |
| 164 void* address = static_cast<char*>(trace[i]) - 1; | 165 void* address = static_cast<char*>(trace[i]) - 1; |
| 165 if (google::Symbolize(address, buf, sizeof(buf))) | 166 if (google::Symbolize(address, buf, sizeof(buf))) |
| 166 handler->HandleOutput(buf); | 167 handler->HandleOutput(buf); |
| 167 else | 168 else |
| 168 handler->HandleOutput("<unknown>"); | 169 handler->HandleOutput("<unknown>"); |
| 169 | 170 |
| 170 handler->HandleOutput("\n"); | 171 handler->HandleOutput("\n"); |
| 171 } | 172 } |
| 172 #elif !defined(__UCLIBC__) | 173 #elif !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 173 bool printed = false; | 174 bool printed = false; |
| 174 | 175 |
| 175 // Below part is async-signal unsafe (uses malloc), so execute it only | 176 // Below part is async-signal unsafe (uses malloc), so execute it only |
| 176 // when we are not executing the signal handler. | 177 // when we are not executing the signal handler. |
| 177 if (in_signal_handler == 0) { | 178 if (in_signal_handler == 0) { |
| 178 scoped_ptr<char*, FreeDeleter> | 179 scoped_ptr<char*, FreeDeleter> |
| 179 trace_symbols(backtrace_symbols(trace, size)); | 180 trace_symbols(backtrace_symbols(trace, size)); |
| 180 if (trace_symbols.get()) { | 181 if (trace_symbols.get()) { |
| 181 for (size_t i = 0; i < size; ++i) { | 182 for (size_t i = 0; i < size; ++i) { |
| 182 std::string trace_symbol = trace_symbols.get()[i]; | 183 std::string trace_symbol = trace_symbols.get()[i]; |
| 183 DemangleSymbols(&trace_symbol); | 184 DemangleSymbols(&trace_symbol); |
| 184 handler->HandleOutput(trace_symbol.c_str()); | 185 handler->HandleOutput(trace_symbol.c_str()); |
| 185 handler->HandleOutput("\n"); | 186 handler->HandleOutput("\n"); |
| 186 } | 187 } |
| 187 | 188 |
| 188 printed = true; | 189 printed = true; |
| 189 } | 190 } |
| 190 } | 191 } |
| 191 | 192 |
| 192 if (!printed) { | 193 if (!printed) { |
| 193 for (size_t i = 0; i < size; ++i) { | 194 for (size_t i = 0; i < size; ++i) { |
| 194 handler->HandleOutput(" ["); | 195 handler->HandleOutput(" ["); |
| 195 OutputPointer(trace[i], handler); | 196 OutputPointer(trace[i], handler); |
| 196 handler->HandleOutput("]\n"); | 197 handler->HandleOutput("]\n"); |
| 197 } | 198 } |
| 198 } | 199 } |
| 199 #endif // defined(USE_SYMBOLIZE) | 200 #endif // defined(USE_SYMBOLIZE) |
| 200 } | 201 } |
| 202 #endif |
| 201 | 203 |
| 202 void PrintToStderr(const char* output) { | 204 void PrintToStderr(const char* output) { |
| 203 // NOTE: This code MUST be async-signal safe (it's used by in-process | 205 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 204 // stack dumping signal handler). NO malloc or stdio is allowed here. | 206 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 205 ignore_result(HANDLE_EINTR(write(STDERR_FILENO, output, strlen(output)))); | 207 ignore_result(HANDLE_EINTR(write(STDERR_FILENO, output, strlen(output)))); |
| 206 } | 208 } |
| 207 | 209 |
| 208 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 210 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { |
| 209 // NOTE: This code MUST be async-signal safe. | 211 // NOTE: This code MUST be async-signal safe. |
| 210 // NO malloc or stdio is allowed here. | 212 // NO malloc or stdio is allowed here. |
| (...skipping 520 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 |
| 741 #if !defined(__UCLIBC__) | 743 #if !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 742 // 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 |
| 743 // return values, we take no chance. | 745 // return values, we take no chance. |
| 744 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 746 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
| 745 #else | 747 #else |
| 746 count_ = 0; | 748 count_ = 0; |
| 747 #endif | 749 #endif |
| 748 } | 750 } |
| 749 | 751 |
| 750 void StackTrace::Print() const { | 752 void StackTrace::Print() const { |
| 751 // NOTE: This code MUST be async-signal safe (it's used by in-process | 753 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 752 // stack dumping signal handler). NO malloc or stdio is allowed here. | 754 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 753 | 755 |
| 754 #if !defined(__UCLIBC__) | 756 #if !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 755 PrintBacktraceOutputHandler handler; | 757 PrintBacktraceOutputHandler handler; |
| 756 ProcessBacktrace(trace_, count_, &handler); | 758 ProcessBacktrace(trace_, count_, &handler); |
| 757 #endif | 759 #endif |
| 758 } | 760 } |
| 759 | 761 |
| 760 #if !defined(__UCLIBC__) | 762 #if !defined(__UCLIBC__) && !defined(FNL_MUSL) |
| 761 void StackTrace::OutputToStream(std::ostream* os) const { | 763 void StackTrace::OutputToStream(std::ostream* os) const { |
| 762 StreamBacktraceOutputHandler handler(os); | 764 StreamBacktraceOutputHandler handler(os); |
| 763 ProcessBacktrace(trace_, count_, &handler); | 765 ProcessBacktrace(trace_, count_, &handler); |
| 764 } | 766 } |
| 765 #endif | 767 #endif |
| 766 | 768 |
| 767 namespace internal { | 769 namespace internal { |
| 768 | 770 |
| 769 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 771 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
| 770 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { | 772 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 824 *ptr = *start; | 826 *ptr = *start; |
| 825 *start++ = ch; | 827 *start++ = ch; |
| 826 } | 828 } |
| 827 return buf; | 829 return buf; |
| 828 } | 830 } |
| 829 | 831 |
| 830 } // namespace internal | 832 } // namespace internal |
| 831 | 833 |
| 832 } // namespace debug | 834 } // namespace debug |
| 833 } // namespace base | 835 } // namespace base |
| OLD | NEW |