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