| 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 // Slightly adapted for inclusion in V8. | 5 // Slightly adapted for inclusion in V8. |
| 6 // Copyright 2016 the V8 project authors. All rights reserved. | 6 // Copyright 2016 the V8 project authors. All rights reserved. |
| 7 | 7 |
| 8 #include "src/base/debug/stack_trace.h" | 8 #include "src/base/debug/stack_trace.h" |
| 9 | 9 |
| 10 #include <errno.h> | 10 #include <errno.h> |
| 11 #include <fcntl.h> | 11 #include <fcntl.h> |
| 12 #include <signal.h> | 12 #include <signal.h> |
| 13 #include <stddef.h> | 13 #include <stddef.h> |
| 14 #include <stdint.h> | 14 #include <stdint.h> |
| 15 #include <stdio.h> | 15 #include <stdio.h> |
| 16 #include <stdlib.h> | 16 #include <stdlib.h> |
| 17 #include <sys/param.h> | 17 #include <sys/param.h> |
| 18 #include <sys/stat.h> | 18 #include <sys/stat.h> |
| 19 #include <sys/types.h> | 19 #include <sys/types.h> |
| 20 #include <unistd.h> | 20 #include <unistd.h> |
| 21 | 21 |
| 22 #include <map> | 22 #include <map> |
| 23 #include <memory> | 23 #include <memory> |
| 24 #include <ostream> | 24 #include <ostream> |
| 25 #include <string> | 25 #include <string> |
| 26 #include <vector> | 26 #include <vector> |
| 27 | 27 |
| 28 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_LIBC_UCLIBC | 28 #if V8_LIBC_GLIBC || V8_LIBC_BSD || V8_LIBC_UCLIBC || V8_OS_SOLARIS |
| 29 #define HAVE_EXECINFO_H 1 |
| 30 #endif |
| 31 |
| 32 #if HAVE_EXECINFO_H |
| 29 #include <cxxabi.h> | 33 #include <cxxabi.h> |
| 30 #include <execinfo.h> | 34 #include <execinfo.h> |
| 31 #endif | 35 #endif |
| 32 #if V8_OS_MACOSX | 36 #if V8_OS_MACOSX |
| 33 #include <AvailabilityMacros.h> | 37 #include <AvailabilityMacros.h> |
| 34 #endif | 38 #endif |
| 35 | 39 |
| 36 #include "src/base/build_config.h" | 40 #include "src/base/build_config.h" |
| 37 #include "src/base/free_deleter.h" | 41 #include "src/base/free_deleter.h" |
| 38 #include "src/base/logging.h" | 42 #include "src/base/logging.h" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 | 74 |
| 71 // Demangles C++ symbols in the given text. Example: | 75 // Demangles C++ symbols in the given text. Example: |
| 72 // | 76 // |
| 73 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 77 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
| 74 // => | 78 // => |
| 75 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 79 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
| 76 void DemangleSymbols(std::string* text) { | 80 void DemangleSymbols(std::string* text) { |
| 77 // Note: code in this function is NOT async-signal safe (std::string uses | 81 // Note: code in this function is NOT async-signal safe (std::string uses |
| 78 // malloc internally). | 82 // malloc internally). |
| 79 | 83 |
| 80 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_LIBC_UCLIBC | 84 #if HAVE_EXECINFO_H |
| 81 | 85 |
| 82 std::string::size_type search_from = 0; | 86 std::string::size_type search_from = 0; |
| 83 while (search_from < text->size()) { | 87 while (search_from < text->size()) { |
| 84 // Look for the start of a mangled symbol, from search_from. | 88 // Look for the start of a mangled symbol, from search_from. |
| 85 std::string::size_type mangled_start = | 89 std::string::size_type mangled_start = |
| 86 text->find(kMangledSymbolPrefix, search_from); | 90 text->find(kMangledSymbolPrefix, search_from); |
| 87 if (mangled_start == std::string::npos) { | 91 if (mangled_start == std::string::npos) { |
| 88 break; // Mangled symbol not found. | 92 break; // Mangled symbol not found. |
| 89 } | 93 } |
| 90 | 94 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 107 // Insert the demangled symbol. | 111 // Insert the demangled symbol. |
| 108 text->insert(mangled_start, demangled_symbol.get()); | 112 text->insert(mangled_start, demangled_symbol.get()); |
| 109 // Next time, we'll start right after the demangled symbol we inserted. | 113 // Next time, we'll start right after the demangled symbol we inserted. |
| 110 search_from = mangled_start + strlen(demangled_symbol.get()); | 114 search_from = mangled_start + strlen(demangled_symbol.get()); |
| 111 } else { | 115 } else { |
| 112 // Failed to demangle. Retry after the "_Z" we just found. | 116 // Failed to demangle. Retry after the "_Z" we just found. |
| 113 search_from = mangled_start + 2; | 117 search_from = mangled_start + 2; |
| 114 } | 118 } |
| 115 } | 119 } |
| 116 | 120 |
| 117 #endif // V8_LIBC_GLIBC || V8_OS_BSD || V8_LIBC_UCLIBC | 121 #endif // HAVE_EXECINFO_H |
| 118 } | 122 } |
| 119 | 123 |
| 120 class BacktraceOutputHandler { | 124 class BacktraceOutputHandler { |
| 121 public: | 125 public: |
| 122 virtual void HandleOutput(const char* output) = 0; | 126 virtual void HandleOutput(const char* output) = 0; |
| 123 | 127 |
| 124 protected: | 128 protected: |
| 125 virtual ~BacktraceOutputHandler() {} | 129 virtual ~BacktraceOutputHandler() {} |
| 126 }; | 130 }; |
| 127 | 131 |
| 128 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { | 132 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { |
| 129 // This should be more than enough to store a 64-bit number in hex: | 133 // This should be more than enough to store a 64-bit number in hex: |
| 130 // 16 hex digits + 1 for null-terminator. | 134 // 16 hex digits + 1 for null-terminator. |
| 131 char buf[17] = {'\0'}; | 135 char buf[17] = {'\0'}; |
| 132 handler->HandleOutput("0x"); | 136 handler->HandleOutput("0x"); |
| 133 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, | 137 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, |
| 134 12); | 138 12); |
| 135 handler->HandleOutput(buf); | 139 handler->HandleOutput(buf); |
| 136 } | 140 } |
| 137 | 141 |
| 138 #if !V8_OS_AIX | 142 #if HAVE_EXECINFO_H |
| 139 void ProcessBacktrace(void* const* trace, size_t size, | 143 void ProcessBacktrace(void* const* trace, size_t size, |
| 140 BacktraceOutputHandler* handler) { | 144 BacktraceOutputHandler* handler) { |
| 141 // NOTE: This code MUST be async-signal safe (it's used by in-process | 145 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 142 // stack dumping signal handler). NO malloc or stdio is allowed here. | 146 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 143 handler->HandleOutput("\n"); | 147 handler->HandleOutput("\n"); |
| 144 handler->HandleOutput("==== C stack trace ===============================\n"); | 148 handler->HandleOutput("==== C stack trace ===============================\n"); |
| 145 handler->HandleOutput("\n"); | 149 handler->HandleOutput("\n"); |
| 146 | 150 |
| 147 bool printed = false; | 151 bool printed = false; |
| 148 | 152 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 165 } | 169 } |
| 166 | 170 |
| 167 if (!printed) { | 171 if (!printed) { |
| 168 for (size_t i = 0; i < size; ++i) { | 172 for (size_t i = 0; i < size; ++i) { |
| 169 handler->HandleOutput(" ["); | 173 handler->HandleOutput(" ["); |
| 170 OutputPointer(trace[i], handler); | 174 OutputPointer(trace[i], handler); |
| 171 handler->HandleOutput("]\n"); | 175 handler->HandleOutput("]\n"); |
| 172 } | 176 } |
| 173 } | 177 } |
| 174 } | 178 } |
| 175 #endif // !V8_OS_AIX | 179 #endif // HAVE_EXECINFO_H |
| 176 | 180 |
| 177 void PrintToStderr(const char* output) { | 181 void PrintToStderr(const char* output) { |
| 178 // NOTE: This code MUST be async-signal safe (it's used by in-process | 182 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 179 // stack dumping signal handler). NO malloc or stdio is allowed here. | 183 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 180 ssize_t return_val = write(STDERR_FILENO, output, strlen(output)); | 184 ssize_t return_val = write(STDERR_FILENO, output, strlen(output)); |
| 181 USE(return_val); | 185 USE(return_val); |
| 182 } | 186 } |
| 183 | 187 |
| 184 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 188 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { |
| 185 // NOTE: This code MUST be async-signal safe. | 189 // NOTE: This code MUST be async-signal safe. |
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 354 } | 358 } |
| 355 | 359 |
| 356 void DisableSignalStackDump() { | 360 void DisableSignalStackDump() { |
| 357 dump_stack_in_signal_handler = false; | 361 dump_stack_in_signal_handler = false; |
| 358 } | 362 } |
| 359 | 363 |
| 360 StackTrace::StackTrace() { | 364 StackTrace::StackTrace() { |
| 361 // NOTE: This code MUST be async-signal safe (it's used by in-process | 365 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 362 // stack dumping signal handler). NO malloc or stdio is allowed here. | 366 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 363 | 367 |
| 364 #if !V8_OS_AIX | 368 #if HAVE_EXECINFO_H |
| 365 // Though the backtrace API man page does not list any possible negative | 369 // Though the backtrace API man page does not list any possible negative |
| 366 // return values, we take no chance. | 370 // return values, we take no chance. |
| 367 count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 371 count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
| 368 #else | 372 #else |
| 369 count_ = 0; | 373 count_ = 0; |
| 370 #endif | 374 #endif |
| 371 } | 375 } |
| 372 | 376 |
| 373 void StackTrace::Print() const { | 377 void StackTrace::Print() const { |
| 374 // NOTE: This code MUST be async-signal safe (it's used by in-process | 378 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 375 // stack dumping signal handler). NO malloc or stdio is allowed here. | 379 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 376 | 380 |
| 377 #if !V8_OS_AIX | 381 #if HAVE_EXECINFO_H |
| 378 PrintBacktraceOutputHandler handler; | 382 PrintBacktraceOutputHandler handler; |
| 379 ProcessBacktrace(trace_, count_, &handler); | 383 ProcessBacktrace(trace_, count_, &handler); |
| 380 #endif | 384 #endif |
| 381 } | 385 } |
| 382 | 386 |
| 383 #if !V8_OS_AIX | |
| 384 void StackTrace::OutputToStream(std::ostream* os) const { | 387 void StackTrace::OutputToStream(std::ostream* os) const { |
| 388 #if HAVE_EXECINFO_H |
| 385 StreamBacktraceOutputHandler handler(os); | 389 StreamBacktraceOutputHandler handler(os); |
| 386 ProcessBacktrace(trace_, count_, &handler); | 390 ProcessBacktrace(trace_, count_, &handler); |
| 391 #endif |
| 387 } | 392 } |
| 388 #endif | |
| 389 | 393 |
| 390 namespace internal { | 394 namespace internal { |
| 391 | 395 |
| 392 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 396 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
| 393 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { | 397 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { |
| 394 // Make sure we can write at least one NUL byte. | 398 // Make sure we can write at least one NUL byte. |
| 395 size_t n = 1; | 399 size_t n = 1; |
| 396 if (n > sz) return NULL; | 400 if (n > sz) return NULL; |
| 397 | 401 |
| 398 if (base < 2 || base > 16) { | 402 if (base < 2 || base > 16) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 *start++ = ch; | 451 *start++ = ch; |
| 448 } | 452 } |
| 449 return buf; | 453 return buf; |
| 450 } | 454 } |
| 451 | 455 |
| 452 } // namespace internal | 456 } // namespace internal |
| 453 | 457 |
| 454 } // namespace debug | 458 } // namespace debug |
| 455 } // namespace base | 459 } // namespace base |
| 456 } // namespace v8 | 460 } // namespace v8 |
| OLD | NEW |