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> |
8 #include <fcntl.h> | 9 #include <fcntl.h> |
9 #include <signal.h> | 10 #include <signal.h> |
10 #include <stdio.h> | 11 #include <stdio.h> |
11 #include <stdlib.h> | 12 #include <stdlib.h> |
12 #include <sys/param.h> | 13 #include <sys/param.h> |
13 #include <sys/stat.h> | 14 #include <sys/stat.h> |
14 #include <sys/types.h> | 15 #include <sys/types.h> |
15 #include <unistd.h> | 16 #include <unistd.h> |
16 | 17 |
17 #include <map> | 18 #include <map> |
18 #include <ostream> | 19 #include <ostream> |
19 #include <string> | 20 #include <string> |
20 #include <vector> | 21 #include <vector> |
21 | 22 |
22 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) | 23 #if defined(__GLIBCXX__) |
23 #include <cxxabi.h> | 24 #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__) && !defined(__UCLIBC__) | 74 #if defined(__GLIBCXX__) |
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__) && !defined(__UCLIBC__) | 111 #endif // defined(__GLIBCXX__) |
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 #elif defined(__GLIBCXX__) && !defined(__UCLIBC__) | 170 #else |
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__) | |
283 debug::StackTrace().Print(); | 282 debug::StackTrace().Print(); |
284 #endif | |
285 | 283 |
286 #if defined(OS_LINUX) | 284 #if defined(OS_LINUX) |
287 #if ARCH_CPU_X86_FAMILY | 285 #if ARCH_CPU_X86_FAMILY |
288 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 286 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
289 const struct { | 287 const struct { |
290 const char* label; | 288 const char* label; |
291 greg_t value; | 289 greg_t value; |
292 } registers[] = { | 290 } registers[] = { |
293 #if ARCH_CPU_32_BITS | 291 #if ARCH_CPU_32_BITS |
294 { " gs: ", context->uc_mcontext.gregs[REG_GS] }, | 292 { " gs: ", context->uc_mcontext.gregs[REG_GS] }, |
(...skipping 438 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
733 success &= (sigaction(SIGSYS, &action, NULL) == 0); | 731 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
734 #endif // !defined(OS_LINUX) | 732 #endif // !defined(OS_LINUX) |
735 | 733 |
736 return success; | 734 return success; |
737 } | 735 } |
738 | 736 |
739 StackTrace::StackTrace() { | 737 StackTrace::StackTrace() { |
740 // 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 |
741 // stack dumping signal handler). NO malloc or stdio is allowed here. | 739 // stack dumping signal handler). NO malloc or stdio is allowed here. |
742 | 740 |
743 #if defined(__GLIBCXX__) && !defined(__UCLIBC__) | |
744 // Though the backtrace API man page does not list any possible negative | 741 // Though the backtrace API man page does not list any possible negative |
745 // return values, we take no chance. | 742 // return values, we take no chance. |
746 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 743 count_ = base::saturated_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
747 #endif | |
748 } | 744 } |
749 | 745 |
750 #if !defined(__UCLIBC__) | |
751 void StackTrace::Print() const { | 746 void StackTrace::Print() const { |
752 // NOTE: This code MUST be async-signal safe (it's used by in-process | 747 // NOTE: This code MUST be async-signal safe (it's used by in-process |
753 // stack dumping signal handler). NO malloc or stdio is allowed here. | 748 // stack dumping signal handler). NO malloc or stdio is allowed here. |
754 | 749 |
755 PrintBacktraceOutputHandler handler; | 750 PrintBacktraceOutputHandler handler; |
756 ProcessBacktrace(trace_, count_, &handler); | 751 ProcessBacktrace(trace_, count_, &handler); |
757 } | 752 } |
758 | 753 |
759 void StackTrace::OutputToStream(std::ostream* os) const { | 754 void StackTrace::OutputToStream(std::ostream* os) const { |
760 StreamBacktraceOutputHandler handler(os); | 755 StreamBacktraceOutputHandler handler(os); |
761 ProcessBacktrace(trace_, count_, &handler); | 756 ProcessBacktrace(trace_, count_, &handler); |
762 } | 757 } |
763 #endif | |
764 | 758 |
765 namespace internal { | 759 namespace internal { |
766 | 760 |
767 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 761 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
768 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { | 762 char *itoa_r(intptr_t i, char *buf, size_t sz, int base, size_t padding) { |
769 // Make sure we can write at least one NUL byte. | 763 // Make sure we can write at least one NUL byte. |
770 size_t n = 1; | 764 size_t n = 1; |
771 if (n > sz) | 765 if (n > sz) |
772 return NULL; | 766 return NULL; |
773 | 767 |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
822 *ptr = *start; | 816 *ptr = *start; |
823 *start++ = ch; | 817 *start++ = ch; |
824 } | 818 } |
825 return buf; | 819 return buf; |
826 } | 820 } |
827 | 821 |
828 } // namespace internal | 822 } // namespace internal |
829 | 823 |
830 } // namespace debug | 824 } // namespace debug |
831 } // namespace base | 825 } // namespace base |
OLD | NEW |