Chromium Code Reviews| 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 550 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 731 success &= (sigaction(SIGSYS, &action, NULL) == 0); | 731 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
| 732 #endif // !defined(OS_LINUX) | 732 #endif // !defined(OS_LINUX) |
| 733 | 733 |
| 734 return success; | 734 return success; |
| 735 } | 735 } |
| 736 | 736 |
| 737 StackTrace::StackTrace() { | 737 StackTrace::StackTrace() { |
| 738 // NOTE: This code MUST be async-signal safe (it's used by in-process | 738 // 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. | 739 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 740 | 740 |
| 741 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) | |
| 741 // Though the backtrace API man page does not list any possible negative | 742 // Though the backtrace API man page does not list any possible negative |
| 742 // return values, we take no chance. | 743 // return values, we take no chance. |
| 743 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 744 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
| 745 #endif | |
| 744 } | 746 } |
| 745 | 747 |
| 746 void StackTrace::Print() const { | 748 void StackTrace::Print() const { |
| 747 // NOTE: This code MUST be async-signal safe (it's used by in-process | 749 // 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. | 750 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 749 | 751 |
| 750 PrintBacktraceOutputHandler handler; | 752 PrintBacktraceOutputHandler handler; |
|
Mark Mentovai
2014/04/07 20:37:12
Do you want to wrap the implementation of Print an
Mostyn Bramley-Moore
2014/04/07 21:00:56
Done in patchset 2, using !uclibc checks rather th
| |
| 751 ProcessBacktrace(trace_, count_, &handler); | 753 ProcessBacktrace(trace_, count_, &handler); |
| 752 } | 754 } |
| 753 | 755 |
| 754 void StackTrace::OutputToStream(std::ostream* os) const { | 756 void StackTrace::OutputToStream(std::ostream* os) const { |
| 755 StreamBacktraceOutputHandler handler(os); | 757 StreamBacktraceOutputHandler handler(os); |
| 756 ProcessBacktrace(trace_, count_, &handler); | 758 ProcessBacktrace(trace_, count_, &handler); |
| 757 } | 759 } |
| 758 | 760 |
| 759 namespace internal { | 761 namespace internal { |
| 760 | 762 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 816 *ptr = *start; | 818 *ptr = *start; |
| 817 *start++ = ch; | 819 *start++ = ch; |
| 818 } | 820 } |
| 819 return buf; | 821 return buf; |
| 820 } | 822 } |
| 821 | 823 |
| 822 } // namespace internal | 824 } // namespace internal |
| 823 | 825 |
| 824 } // namespace debug | 826 } // namespace debug |
| 825 } // namespace base | 827 } // namespace base |
| OLD | NEW |