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 | |
9 #if !defined(__UCLIBC__) | |
8 #include <execinfo.h> | 10 #include <execinfo.h> |
11 #endif | |
12 | |
9 #include <fcntl.h> | 13 #include <fcntl.h> |
10 #include <signal.h> | 14 #include <signal.h> |
11 #include <stdio.h> | 15 #include <stdio.h> |
12 #include <stdlib.h> | 16 #include <stdlib.h> |
13 #include <sys/param.h> | 17 #include <sys/param.h> |
14 #include <sys/stat.h> | 18 #include <sys/stat.h> |
15 #include <sys/types.h> | 19 #include <sys/types.h> |
16 #include <unistd.h> | 20 #include <unistd.h> |
17 | 21 |
18 #include <map> | 22 #include <map> |
19 #include <ostream> | 23 #include <ostream> |
20 #include <string> | 24 #include <string> |
21 #include <vector> | 25 #include <vector> |
22 | 26 |
23 #if defined(__GLIBCXX__) | 27 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) |
24 #include <cxxabi.h> | 28 #include <cxxabi.h> |
25 #endif | 29 #endif |
26 | 30 |
27 #if defined(OS_MACOSX) | 31 #if defined(OS_MACOSX) |
28 #include <AvailabilityMacros.h> | 32 #include <AvailabilityMacros.h> |
29 #endif | 33 #endif |
30 | 34 |
31 #include "base/basictypes.h" | 35 #include "base/basictypes.h" |
32 #include "base/debug/debugger.h" | 36 #include "base/debug/debugger.h" |
33 #include "base/debug/proc_maps_linux.h" | 37 #include "base/debug/proc_maps_linux.h" |
(...skipping 30 matching lines...) Expand all Loading... | |
64 #if !defined(USE_SYMBOLIZE) | 68 #if !defined(USE_SYMBOLIZE) |
65 // Demangles C++ symbols in the given text. Example: | 69 // Demangles C++ symbols in the given text. Example: |
66 // | 70 // |
67 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 71 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
68 // => | 72 // => |
69 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 73 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
70 void DemangleSymbols(std::string* text) { | 74 void DemangleSymbols(std::string* text) { |
71 // Note: code in this function is NOT async-signal safe (std::string uses | 75 // Note: code in this function is NOT async-signal safe (std::string uses |
72 // malloc internally). | 76 // malloc internally). |
73 | 77 |
74 #if defined(__GLIBCXX__) | 78 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) |
75 | 79 |
76 std::string::size_type search_from = 0; | 80 std::string::size_type search_from = 0; |
77 while (search_from < text->size()) { | 81 while (search_from < text->size()) { |
78 // Look for the start of a mangled symbol, from search_from. | 82 // Look for the start of a mangled symbol, from search_from. |
79 std::string::size_type mangled_start = | 83 std::string::size_type mangled_start = |
80 text->find(kMangledSymbolPrefix, search_from); | 84 text->find(kMangledSymbolPrefix, search_from); |
81 if (mangled_start == std::string::npos) { | 85 if (mangled_start == std::string::npos) { |
82 break; // Mangled symbol not found. | 86 break; // Mangled symbol not found. |
83 } | 87 } |
84 | 88 |
(...skipping 16 matching lines...) Expand all Loading... | |
101 // Insert the demangled symbol. | 105 // Insert the demangled symbol. |
102 text->insert(mangled_start, demangled_symbol.get()); | 106 text->insert(mangled_start, demangled_symbol.get()); |
103 // Next time, we'll start right after the demangled symbol we inserted. | 107 // Next time, we'll start right after the demangled symbol we inserted. |
104 search_from = mangled_start + strlen(demangled_symbol.get()); | 108 search_from = mangled_start + strlen(demangled_symbol.get()); |
105 } else { | 109 } else { |
106 // Failed to demangle. Retry after the "_Z" we just found. | 110 // Failed to demangle. Retry after the "_Z" we just found. |
107 search_from = mangled_start + 2; | 111 search_from = mangled_start + 2; |
108 } | 112 } |
109 } | 113 } |
110 | 114 |
111 #endif // defined(__GLIBCXX__) | 115 #endif // defined(__GLIBCXX__) && !defined(__UCLIBC__) |
112 } | 116 } |
113 #endif // !defined(USE_SYMBOLIZE) | 117 #endif // !defined(USE_SYMBOLIZE) |
114 | 118 |
115 class BacktraceOutputHandler { | 119 class BacktraceOutputHandler { |
116 public: | 120 public: |
117 virtual void HandleOutput(const char* output) = 0; | 121 virtual void HandleOutput(const char* output) = 0; |
118 | 122 |
119 protected: | 123 protected: |
120 virtual ~BacktraceOutputHandler() {} | 124 virtual ~BacktraceOutputHandler() {} |
121 }; | 125 }; |
(...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 | 164 // Subtract by one as return address of function may be in the next |
161 // function when a function is annotated as noreturn. | 165 // function when a function is annotated as noreturn. |
162 void* address = static_cast<char*>(trace[i]) - 1; | 166 void* address = static_cast<char*>(trace[i]) - 1; |
163 if (google::Symbolize(address, buf, sizeof(buf))) | 167 if (google::Symbolize(address, buf, sizeof(buf))) |
164 handler->HandleOutput(buf); | 168 handler->HandleOutput(buf); |
165 else | 169 else |
166 handler->HandleOutput("<unknown>"); | 170 handler->HandleOutput("<unknown>"); |
167 | 171 |
168 handler->HandleOutput("\n"); | 172 handler->HandleOutput("\n"); |
169 } | 173 } |
170 #else | 174 #elif !defined(__UCLIBC__) |
171 bool printed = false; | 175 bool printed = false; |
172 | 176 |
173 // Below part is async-signal unsafe (uses malloc), so execute it only | 177 // Below part is async-signal unsafe (uses malloc), so execute it only |
174 // when we are not executing the signal handler. | 178 // when we are not executing the signal handler. |
175 if (in_signal_handler == 0) { | 179 if (in_signal_handler == 0) { |
176 scoped_ptr<char*, FreeDeleter> | 180 scoped_ptr<char*, FreeDeleter> |
177 trace_symbols(backtrace_symbols(trace, size)); | 181 trace_symbols(backtrace_symbols(trace, size)); |
178 if (trace_symbols.get()) { | 182 if (trace_symbols.get()) { |
179 for (size_t i = 0; i < size; ++i) { | 183 for (size_t i = 0; i < size; ++i) { |
180 std::string trace_symbol = trace_symbols.get()[i]; | 184 std::string trace_symbol = trace_symbols.get()[i]; |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
272 PrintToStderr(" <unknown> "); | 276 PrintToStderr(" <unknown> "); |
273 } | 277 } |
274 if (signal == SIGBUS || signal == SIGFPE || | 278 if (signal == SIGBUS || signal == SIGFPE || |
275 signal == SIGILL || signal == SIGSEGV) { | 279 signal == SIGILL || signal == SIGSEGV) { |
276 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), | 280 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), |
277 buf, sizeof(buf), 16, 12); | 281 buf, sizeof(buf), 16, 12); |
278 PrintToStderr(buf); | 282 PrintToStderr(buf); |
279 } | 283 } |
280 PrintToStderr("\n"); | 284 PrintToStderr("\n"); |
281 | 285 |
286 #if !defined(__UCLIBC__) | |
282 debug::StackTrace().Print(); | 287 debug::StackTrace().Print(); |
288 #endif | |
283 | 289 |
284 #if defined(OS_LINUX) | 290 #if defined(OS_LINUX) |
285 #if ARCH_CPU_X86_FAMILY | 291 #if ARCH_CPU_X86_FAMILY |
286 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 292 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
287 const struct { | 293 const struct { |
288 const char* label; | 294 const char* label; |
289 greg_t value; | 295 greg_t value; |
290 } registers[] = { | 296 } registers[] = { |
291 #if ARCH_CPU_32_BITS | 297 #if ARCH_CPU_32_BITS |
292 { " gs: ", context->uc_mcontext.gregs[REG_GS] }, | 298 { " 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); | 737 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
732 #endif // !defined(OS_LINUX) | 738 #endif // !defined(OS_LINUX) |
733 | 739 |
734 return success; | 740 return success; |
735 } | 741 } |
736 | 742 |
737 StackTrace::StackTrace() { | 743 StackTrace::StackTrace() { |
738 // NOTE: This code MUST be async-signal safe (it's used by in-process | 744 // 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. | 745 // stack dumping signal handler). NO malloc or stdio is allowed here. |
740 | 746 |
747 #if !defined(__UCLIBC__) | |
741 // Though the backtrace API man page does not list any possible negative | 748 // Though the backtrace API man page does not list any possible negative |
742 // return values, we take no chance. | 749 // return values, we take no chance. |
743 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 750 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
751 #endif | |
earthdok
2014/04/08 15:28:30
#else
count_ = 0;
#endif
Mostyn Bramley-Moore
2014/04/08 15:40:46
Done.
| |
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 |