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 // 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 | 28 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | 
| 29 #include <cxxabi.h> | 29 #include <cxxabi.h> | 
| 30 #include <execinfo.h> | 30 #include <execinfo.h> | 
| 31 #endif | 31 #endif | 
| 32 #if V8_OS_MACOSX | 32 #if V8_OS_MACOSX | 
| 33 #include <AvailabilityMacros.h> | 33 #include <AvailabilityMacros.h> | 
| 34 #endif | 34 #endif | 
| 35 | 35 | 
| 36 #include "src/base/build_config.h" | 36 #include "src/base/build_config.h" | 
| 37 #include "src/base/free_deleter.h" | 37 #include "src/base/free_deleter.h" | 
| 38 #include "src/base/logging.h" | 38 #include "src/base/logging.h" | 
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 70 | 70 | 
| 71 // Demangles C++ symbols in the given text. Example: | 71 // Demangles C++ symbols in the given text. Example: | 
| 72 // | 72 // | 
| 73 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 73 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" | 
| 74 // => | 74 // => | 
| 75 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 75 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" | 
| 76 void DemangleSymbols(std::string* text) { | 76 void DemangleSymbols(std::string* text) { | 
| 77 // Note: code in this function is NOT async-signal safe (std::string uses | 77 // Note: code in this function is NOT async-signal safe (std::string uses | 
| 78 // malloc internally). | 78 // malloc internally). | 
| 79 | 79 | 
| 80 #if V8_LIBC_GLIBC || V8_OS_BSD | 80 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | 
| 81 | 81 | 
| 82 std::string::size_type search_from = 0; | 82 std::string::size_type search_from = 0; | 
| 83 while (search_from < text->size()) { | 83 while (search_from < text->size()) { | 
| 84 // Look for the start of a mangled symbol, from search_from. | 84 // Look for the start of a mangled symbol, from search_from. | 
| 85 std::string::size_type mangled_start = | 85 std::string::size_type mangled_start = | 
| 86 text->find(kMangledSymbolPrefix, search_from); | 86 text->find(kMangledSymbolPrefix, search_from); | 
| 87 if (mangled_start == std::string::npos) { | 87 if (mangled_start == std::string::npos) { | 
| 88 break; // Mangled symbol not found. | 88 break; // Mangled symbol not found. | 
| 89 } | 89 } | 
| 90 | 90 | 
| (...skipping 16 matching lines...) Expand all Loading... | |
| 107 // Insert the demangled symbol. | 107 // Insert the demangled symbol. | 
| 108 text->insert(mangled_start, demangled_symbol.get()); | 108 text->insert(mangled_start, demangled_symbol.get()); | 
| 109 // Next time, we'll start right after the demangled symbol we inserted. | 109 // Next time, we'll start right after the demangled symbol we inserted. | 
| 110 search_from = mangled_start + strlen(demangled_symbol.get()); | 110 search_from = mangled_start + strlen(demangled_symbol.get()); | 
| 111 } else { | 111 } else { | 
| 112 // Failed to demangle. Retry after the "_Z" we just found. | 112 // Failed to demangle. Retry after the "_Z" we just found. | 
| 113 search_from = mangled_start + 2; | 113 search_from = mangled_start + 2; | 
| 114 } | 114 } | 
| 115 } | 115 } | 
| 116 | 116 | 
| 117 #endif // V8_LIBC_GLIBC || V8_OS_BSD | 117 #endif // V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | 
| 118 } | 118 } | 
| 119 | 119 | 
| 120 class BacktraceOutputHandler { | 120 class BacktraceOutputHandler { | 
| 121 public: | 121 public: | 
| 122 virtual void HandleOutput(const char* output) = 0; | 122 virtual void HandleOutput(const char* output) = 0; | 
| 123 | 123 | 
| 124 protected: | 124 protected: | 
| 125 virtual ~BacktraceOutputHandler() {} | 125 virtual ~BacktraceOutputHandler() {} | 
| 126 }; | 126 }; | 
| 127 | 127 | 
| 128 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | |
| 
 
rmcilroy
2016/09/07 11:06:22
Why is this change needed (and the similar changes
 
 | |
| 128 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { | 129 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { | 
| 129 // This should be more than enough to store a 64-bit number in hex: | 130 // This should be more than enough to store a 64-bit number in hex: | 
| 130 // 16 hex digits + 1 for null-terminator. | 131 // 16 hex digits + 1 for null-terminator. | 
| 131 char buf[17] = {'\0'}; | 132 char buf[17] = {'\0'}; | 
| 132 handler->HandleOutput("0x"); | 133 handler->HandleOutput("0x"); | 
| 133 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, | 134 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, | 
| 134 12); | 135 12); | 
| 135 handler->HandleOutput(buf); | 136 handler->HandleOutput(buf); | 
| 136 } | 137 } | 
| 137 | 138 | 
| (...skipping 26 matching lines...) Expand all Loading... | |
| 164 } | 165 } | 
| 165 | 166 | 
| 166 if (!printed) { | 167 if (!printed) { | 
| 167 for (size_t i = 0; i < size; ++i) { | 168 for (size_t i = 0; i < size; ++i) { | 
| 168 handler->HandleOutput(" ["); | 169 handler->HandleOutput(" ["); | 
| 169 OutputPointer(trace[i], handler); | 170 OutputPointer(trace[i], handler); | 
| 170 handler->HandleOutput("]\n"); | 171 handler->HandleOutput("]\n"); | 
| 171 } | 172 } | 
| 172 } | 173 } | 
| 173 } | 174 } | 
| 175 #endif // V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | |
| 174 | 176 | 
| 175 void PrintToStderr(const char* output) { | 177 void PrintToStderr(const char* output) { | 
| 176 // NOTE: This code MUST be async-signal safe (it's used by in-process | 178 // NOTE: This code MUST be async-signal safe (it's used by in-process | 
| 177 // stack dumping signal handler). NO malloc or stdio is allowed here. | 179 // stack dumping signal handler). NO malloc or stdio is allowed here. | 
| 178 ssize_t return_val = write(STDERR_FILENO, output, strlen(output)); | 180 ssize_t return_val = write(STDERR_FILENO, output, strlen(output)); | 
| 179 USE(return_val); | 181 USE(return_val); | 
| 180 } | 182 } | 
| 181 | 183 | 
| 182 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 184 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 
| 183 // NOTE: This code MUST be async-signal safe. | 185 // NOTE: This code MUST be async-signal safe. | 
| (...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 352 } | 354 } | 
| 353 | 355 | 
| 354 void DisableSignalStackDump() { | 356 void DisableSignalStackDump() { | 
| 355 dump_stack_in_signal_handler = false; | 357 dump_stack_in_signal_handler = false; | 
| 356 } | 358 } | 
| 357 | 359 | 
| 358 StackTrace::StackTrace() { | 360 StackTrace::StackTrace() { | 
| 359 // NOTE: This code MUST be async-signal safe (it's used by in-process | 361 // NOTE: This code MUST be async-signal safe (it's used by in-process | 
| 360 // stack dumping signal handler). NO malloc or stdio is allowed here. | 362 // stack dumping signal handler). NO malloc or stdio is allowed here. | 
| 361 | 363 | 
| 364 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | |
| 362 // Though the backtrace API man page does not list any possible negative | 365 // Though the backtrace API man page does not list any possible negative | 
| 363 // return values, we take no chance. | 366 // return values, we take no chance. | 
| 364 count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 367 count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_))); | 
| 368 #else | |
| 369 count_ = 0; | |
| 370 #endif | |
| 365 } | 371 } | 
| 366 | 372 | 
| 367 void StackTrace::Print() const { | 373 void StackTrace::Print() const { | 
| 368 // NOTE: This code MUST be async-signal safe (it's used by in-process | 374 // NOTE: This code MUST be async-signal safe (it's used by in-process | 
| 369 // stack dumping signal handler). NO malloc or stdio is allowed here. | 375 // stack dumping signal handler). NO malloc or stdio is allowed here. | 
| 370 | 376 | 
| 377 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | |
| 371 PrintBacktraceOutputHandler handler; | 378 PrintBacktraceOutputHandler handler; | 
| 372 ProcessBacktrace(trace_, count_, &handler); | 379 ProcessBacktrace(trace_, count_, &handler); | 
| 380 #endif | |
| 373 } | 381 } | 
| 374 | 382 | 
| 383 #if V8_LIBC_GLIBC || V8_OS_BSD || V8_OS_SOLARIS | |
| 375 void StackTrace::OutputToStream(std::ostream* os) const { | 384 void StackTrace::OutputToStream(std::ostream* os) const { | 
| 376 StreamBacktraceOutputHandler handler(os); | 385 StreamBacktraceOutputHandler handler(os); | 
| 377 ProcessBacktrace(trace_, count_, &handler); | 386 ProcessBacktrace(trace_, count_, &handler); | 
| 378 } | 387 } | 
| 388 #endif | |
| 379 | 389 | 
| 380 namespace internal { | 390 namespace internal { | 
| 381 | 391 | 
| 382 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 392 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. | 
| 383 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { | 393 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { | 
| 384 // Make sure we can write at least one NUL byte. | 394 // Make sure we can write at least one NUL byte. | 
| 385 size_t n = 1; | 395 size_t n = 1; | 
| 386 if (n > sz) return NULL; | 396 if (n > sz) return NULL; | 
| 387 | 397 | 
| 388 if (base < 2 || base > 16) { | 398 if (base < 2 || base > 16) { | 
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 437 *start++ = ch; | 447 *start++ = ch; | 
| 438 } | 448 } | 
| 439 return buf; | 449 return buf; | 
| 440 } | 450 } | 
| 441 | 451 | 
| 442 } // namespace internal | 452 } // namespace internal | 
| 443 | 453 | 
| 444 } // namespace debug | 454 } // namespace debug | 
| 445 } // namespace base | 455 } // namespace base | 
| 446 } // namespace v8 | 456 } // namespace v8 | 
| OLD | NEW |