OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 // Slightly adapted for inclusion in V8. |
| 6 // Copyright 2016 the V8 project authors. All rights reserved. |
| 7 |
| 8 #include "src/base/debug/stack_trace.h" |
| 9 |
| 10 #include <errno.h> |
| 11 #include <fcntl.h> |
| 12 #include <signal.h> |
| 13 #include <stddef.h> |
| 14 #include <stdint.h> |
| 15 #include <stdio.h> |
| 16 #include <stdlib.h> |
| 17 #include <sys/param.h> |
| 18 #include <sys/stat.h> |
| 19 #include <sys/types.h> |
| 20 #include <unistd.h> |
| 21 |
| 22 #include <map> |
| 23 #include <memory> |
| 24 #include <ostream> |
| 25 #include <string> |
| 26 #include <vector> |
| 27 |
| 28 #if V8_LIBC_GLIBC || V8_OS_BSD |
| 29 #include <cxxabi.h> |
| 30 #include <execinfo.h> |
| 31 #endif |
| 32 #if V8_OS_MACOSX |
| 33 #include <AvailabilityMacros.h> |
| 34 #endif |
| 35 |
| 36 #include "src/base/build_config.h" |
| 37 #include "src/base/free_deleter.h" |
| 38 #include "src/base/logging.h" |
| 39 #include "src/base/macros.h" |
| 40 |
| 41 namespace v8 { |
| 42 namespace base { |
| 43 namespace debug { |
| 44 |
| 45 namespace internal { |
| 46 |
| 47 // POSIX doesn't define any async-signal safe function for converting |
| 48 // an integer to ASCII. We'll have to define our own version. |
| 49 // itoa_r() converts a (signed) integer to ASCII. It returns "buf", if the |
| 50 // conversion was successful or NULL otherwise. It never writes more than "sz" |
| 51 // bytes. Output will be truncated as needed, and a NUL character is always |
| 52 // appended. |
| 53 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding); |
| 54 |
| 55 } // namespace internal |
| 56 |
| 57 namespace { |
| 58 |
| 59 volatile sig_atomic_t in_signal_handler = 0; |
| 60 bool dump_stack_in_signal_handler = 1; |
| 61 |
| 62 // The prefix used for mangled symbols, per the Itanium C++ ABI: |
| 63 // http://www.codesourcery.com/cxx-abi/abi.html#mangling |
| 64 const char kMangledSymbolPrefix[] = "_Z"; |
| 65 |
| 66 // Characters that can be used for symbols, generated by Ruby: |
| 67 // (('a'..'z').to_a+('A'..'Z').to_a+('0'..'9').to_a + ['_']).join |
| 68 const char kSymbolCharacters[] = |
| 69 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_"; |
| 70 |
| 71 // Demangles C++ symbols in the given text. Example: |
| 72 // |
| 73 // "out/Debug/base_unittests(_ZN10StackTraceC1Ev+0x20) [0x817778c]" |
| 74 // => |
| 75 // "out/Debug/base_unittests(StackTrace::StackTrace()+0x20) [0x817778c]" |
| 76 void DemangleSymbols(std::string* text) { |
| 77 // Note: code in this function is NOT async-signal safe (std::string uses |
| 78 // malloc internally). |
| 79 |
| 80 #if V8_LIBC_GLIBC || V8_OS_BSD |
| 81 |
| 82 std::string::size_type search_from = 0; |
| 83 while (search_from < text->size()) { |
| 84 // Look for the start of a mangled symbol, from search_from. |
| 85 std::string::size_type mangled_start = |
| 86 text->find(kMangledSymbolPrefix, search_from); |
| 87 if (mangled_start == std::string::npos) { |
| 88 break; // Mangled symbol not found. |
| 89 } |
| 90 |
| 91 // Look for the end of the mangled symbol. |
| 92 std::string::size_type mangled_end = |
| 93 text->find_first_not_of(kSymbolCharacters, mangled_start); |
| 94 if (mangled_end == std::string::npos) { |
| 95 mangled_end = text->size(); |
| 96 } |
| 97 std::string mangled_symbol = |
| 98 text->substr(mangled_start, mangled_end - mangled_start); |
| 99 |
| 100 // Try to demangle the mangled symbol candidate. |
| 101 int status = 0; |
| 102 std::unique_ptr<char, FreeDeleter> demangled_symbol( |
| 103 abi::__cxa_demangle(mangled_symbol.c_str(), NULL, 0, &status)); |
| 104 if (status == 0) { // Demangling is successful. |
| 105 // Remove the mangled symbol. |
| 106 text->erase(mangled_start, mangled_end - mangled_start); |
| 107 // Insert the demangled symbol. |
| 108 text->insert(mangled_start, demangled_symbol.get()); |
| 109 // Next time, we'll start right after the demangled symbol we inserted. |
| 110 search_from = mangled_start + strlen(demangled_symbol.get()); |
| 111 } else { |
| 112 // Failed to demangle. Retry after the "_Z" we just found. |
| 113 search_from = mangled_start + 2; |
| 114 } |
| 115 } |
| 116 |
| 117 #endif // V8_LIBC_GLIBC || V8_OS_BSD |
| 118 } |
| 119 |
| 120 class BacktraceOutputHandler { |
| 121 public: |
| 122 virtual void HandleOutput(const char* output) = 0; |
| 123 |
| 124 protected: |
| 125 virtual ~BacktraceOutputHandler() {} |
| 126 }; |
| 127 |
| 128 void OutputPointer(void* pointer, BacktraceOutputHandler* handler) { |
| 129 // This should be more than enough to store a 64-bit number in hex: |
| 130 // 16 hex digits + 1 for null-terminator. |
| 131 char buf[17] = {'\0'}; |
| 132 handler->HandleOutput("0x"); |
| 133 internal::itoa_r(reinterpret_cast<intptr_t>(pointer), buf, sizeof(buf), 16, |
| 134 12); |
| 135 handler->HandleOutput(buf); |
| 136 } |
| 137 |
| 138 void ProcessBacktrace(void* const* trace, size_t size, |
| 139 BacktraceOutputHandler* handler) { |
| 140 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 141 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 142 handler->HandleOutput("\n"); |
| 143 handler->HandleOutput("==== C stack trace ===============================\n"); |
| 144 handler->HandleOutput("\n"); |
| 145 |
| 146 bool printed = false; |
| 147 |
| 148 // Below part is async-signal unsafe (uses malloc), so execute it only |
| 149 // when we are not executing the signal handler. |
| 150 if (in_signal_handler == 0) { |
| 151 std::unique_ptr<char*, FreeDeleter> trace_symbols( |
| 152 backtrace_symbols(trace, static_cast<int>(size))); |
| 153 if (trace_symbols.get()) { |
| 154 for (size_t i = 0; i < size; ++i) { |
| 155 std::string trace_symbol = trace_symbols.get()[i]; |
| 156 DemangleSymbols(&trace_symbol); |
| 157 handler->HandleOutput(" "); |
| 158 handler->HandleOutput(trace_symbol.c_str()); |
| 159 handler->HandleOutput("\n"); |
| 160 } |
| 161 |
| 162 printed = true; |
| 163 } |
| 164 } |
| 165 |
| 166 if (!printed) { |
| 167 for (size_t i = 0; i < size; ++i) { |
| 168 handler->HandleOutput(" ["); |
| 169 OutputPointer(trace[i], handler); |
| 170 handler->HandleOutput("]\n"); |
| 171 } |
| 172 } |
| 173 } |
| 174 |
| 175 void PrintToStderr(const char* output) { |
| 176 // 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. |
| 178 write(STDERR_FILENO, output, strlen(output)); |
| 179 } |
| 180 |
| 181 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { |
| 182 // NOTE: This code MUST be async-signal safe. |
| 183 // NO malloc or stdio is allowed here. |
| 184 |
| 185 // Record the fact that we are in the signal handler now, so that the rest |
| 186 // of StackTrace can behave in an async-signal-safe manner. |
| 187 in_signal_handler = 1; |
| 188 |
| 189 PrintToStderr("Received signal "); |
| 190 char buf[1024] = {0}; |
| 191 internal::itoa_r(signal, buf, sizeof(buf), 10, 0); |
| 192 PrintToStderr(buf); |
| 193 if (signal == SIGBUS) { |
| 194 if (info->si_code == BUS_ADRALN) |
| 195 PrintToStderr(" BUS_ADRALN "); |
| 196 else if (info->si_code == BUS_ADRERR) |
| 197 PrintToStderr(" BUS_ADRERR "); |
| 198 else if (info->si_code == BUS_OBJERR) |
| 199 PrintToStderr(" BUS_OBJERR "); |
| 200 else |
| 201 PrintToStderr(" <unknown> "); |
| 202 } else if (signal == SIGFPE) { |
| 203 if (info->si_code == FPE_FLTDIV) |
| 204 PrintToStderr(" FPE_FLTDIV "); |
| 205 else if (info->si_code == FPE_FLTINV) |
| 206 PrintToStderr(" FPE_FLTINV "); |
| 207 else if (info->si_code == FPE_FLTOVF) |
| 208 PrintToStderr(" FPE_FLTOVF "); |
| 209 else if (info->si_code == FPE_FLTRES) |
| 210 PrintToStderr(" FPE_FLTRES "); |
| 211 else if (info->si_code == FPE_FLTSUB) |
| 212 PrintToStderr(" FPE_FLTSUB "); |
| 213 else if (info->si_code == FPE_FLTUND) |
| 214 PrintToStderr(" FPE_FLTUND "); |
| 215 else if (info->si_code == FPE_INTDIV) |
| 216 PrintToStderr(" FPE_INTDIV "); |
| 217 else if (info->si_code == FPE_INTOVF) |
| 218 PrintToStderr(" FPE_INTOVF "); |
| 219 else |
| 220 PrintToStderr(" <unknown> "); |
| 221 } else if (signal == SIGILL) { |
| 222 if (info->si_code == ILL_BADSTK) |
| 223 PrintToStderr(" ILL_BADSTK "); |
| 224 else if (info->si_code == ILL_COPROC) |
| 225 PrintToStderr(" ILL_COPROC "); |
| 226 else if (info->si_code == ILL_ILLOPN) |
| 227 PrintToStderr(" ILL_ILLOPN "); |
| 228 else if (info->si_code == ILL_ILLADR) |
| 229 PrintToStderr(" ILL_ILLADR "); |
| 230 else if (info->si_code == ILL_ILLTRP) |
| 231 PrintToStderr(" ILL_ILLTRP "); |
| 232 else if (info->si_code == ILL_PRVOPC) |
| 233 PrintToStderr(" ILL_PRVOPC "); |
| 234 else if (info->si_code == ILL_PRVREG) |
| 235 PrintToStderr(" ILL_PRVREG "); |
| 236 else |
| 237 PrintToStderr(" <unknown> "); |
| 238 } else if (signal == SIGSEGV) { |
| 239 if (info->si_code == SEGV_MAPERR) |
| 240 PrintToStderr(" SEGV_MAPERR "); |
| 241 else if (info->si_code == SEGV_ACCERR) |
| 242 PrintToStderr(" SEGV_ACCERR "); |
| 243 else |
| 244 PrintToStderr(" <unknown> "); |
| 245 } |
| 246 if (signal == SIGBUS || signal == SIGFPE || signal == SIGILL || |
| 247 signal == SIGSEGV) { |
| 248 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), buf, |
| 249 sizeof(buf), 16, 12); |
| 250 PrintToStderr(buf); |
| 251 } |
| 252 PrintToStderr("\n"); |
| 253 if (dump_stack_in_signal_handler) { |
| 254 debug::StackTrace().Print(); |
| 255 PrintToStderr("[end of stack trace]\n"); |
| 256 } |
| 257 |
| 258 if (::signal(signal, SIG_DFL) == SIG_ERR) _exit(1); |
| 259 } |
| 260 |
| 261 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { |
| 262 public: |
| 263 PrintBacktraceOutputHandler() {} |
| 264 |
| 265 void HandleOutput(const char* output) override { |
| 266 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 267 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 268 PrintToStderr(output); |
| 269 } |
| 270 |
| 271 private: |
| 272 DISALLOW_COPY_AND_ASSIGN(PrintBacktraceOutputHandler); |
| 273 }; |
| 274 |
| 275 class StreamBacktraceOutputHandler : public BacktraceOutputHandler { |
| 276 public: |
| 277 explicit StreamBacktraceOutputHandler(std::ostream* os) : os_(os) {} |
| 278 |
| 279 void HandleOutput(const char* output) override { (*os_) << output; } |
| 280 |
| 281 private: |
| 282 std::ostream* os_; |
| 283 |
| 284 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); |
| 285 }; |
| 286 |
| 287 void WarmUpBacktrace() { |
| 288 // Warm up stack trace infrastructure. It turns out that on the first |
| 289 // call glibc initializes some internal data structures using pthread_once, |
| 290 // and even backtrace() can call malloc(), leading to hangs. |
| 291 // |
| 292 // Example stack trace snippet (with tcmalloc): |
| 293 // |
| 294 // #8 0x0000000000a173b5 in tc_malloc |
| 295 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 |
| 296 // #9 0x00007ffff7de7900 in _dl_map_object_deps at dl-deps.c:517 |
| 297 // #10 0x00007ffff7ded8a9 in dl_open_worker at dl-open.c:262 |
| 298 // #11 0x00007ffff7de9176 in _dl_catch_error at dl-error.c:178 |
| 299 // #12 0x00007ffff7ded31a in _dl_open (file=0x7ffff625e298 "libgcc_s.so.1") |
| 300 // at dl-open.c:639 |
| 301 // #13 0x00007ffff6215602 in do_dlopen at dl-libc.c:89 |
| 302 // #14 0x00007ffff7de9176 in _dl_catch_error at dl-error.c:178 |
| 303 // #15 0x00007ffff62156c4 in dlerror_run at dl-libc.c:48 |
| 304 // #16 __GI___libc_dlopen_mode at dl-libc.c:165 |
| 305 // #17 0x00007ffff61ef8f5 in init |
| 306 // at ../sysdeps/x86_64/../ia64/backtrace.c:53 |
| 307 // #18 0x00007ffff6aad400 in pthread_once |
| 308 // at ../nptl/sysdeps/unix/sysv/linux/x86_64/pthread_once.S:104 |
| 309 // #19 0x00007ffff61efa14 in __GI___backtrace |
| 310 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 |
| 311 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace |
| 312 // at base/debug/stack_trace_posix.cc:175 |
| 313 // #21 0x00000000007a4ae5 in |
| 314 // base::(anonymous namespace)::StackDumpSignalHandler |
| 315 // at base/process_util_posix.cc:172 |
| 316 // #22 <signal handler called> |
| 317 StackTrace stack_trace; |
| 318 } |
| 319 |
| 320 } // namespace |
| 321 |
| 322 bool EnableInProcessStackDumping() { |
| 323 // When running in an application, our code typically expects SIGPIPE |
| 324 // to be ignored. Therefore, when testing that same code, it should run |
| 325 // with SIGPIPE ignored as well. |
| 326 struct sigaction sigpipe_action; |
| 327 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); |
| 328 sigpipe_action.sa_handler = SIG_IGN; |
| 329 sigemptyset(&sigpipe_action.sa_mask); |
| 330 bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0); |
| 331 |
| 332 // Avoid hangs during backtrace initialization, see above. |
| 333 WarmUpBacktrace(); |
| 334 |
| 335 struct sigaction action; |
| 336 memset(&action, 0, sizeof(action)); |
| 337 action.sa_flags = SA_RESETHAND | SA_SIGINFO; |
| 338 action.sa_sigaction = &StackDumpSignalHandler; |
| 339 sigemptyset(&action.sa_mask); |
| 340 |
| 341 success &= (sigaction(SIGILL, &action, NULL) == 0); |
| 342 success &= (sigaction(SIGABRT, &action, NULL) == 0); |
| 343 success &= (sigaction(SIGFPE, &action, NULL) == 0); |
| 344 success &= (sigaction(SIGBUS, &action, NULL) == 0); |
| 345 success &= (sigaction(SIGSEGV, &action, NULL) == 0); |
| 346 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
| 347 |
| 348 dump_stack_in_signal_handler = true; |
| 349 |
| 350 return success; |
| 351 } |
| 352 |
| 353 void DisableSignalStackDump() { |
| 354 dump_stack_in_signal_handler = false; |
| 355 } |
| 356 |
| 357 StackTrace::StackTrace() { |
| 358 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 359 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 360 |
| 361 // Though the backtrace API man page does not list any possible negative |
| 362 // return values, we take no chance. |
| 363 count_ = static_cast<size_t>(backtrace(trace_, arraysize(trace_))); |
| 364 } |
| 365 |
| 366 void StackTrace::Print() const { |
| 367 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 368 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 369 |
| 370 PrintBacktraceOutputHandler handler; |
| 371 ProcessBacktrace(trace_, count_, &handler); |
| 372 } |
| 373 |
| 374 void StackTrace::OutputToStream(std::ostream* os) const { |
| 375 StreamBacktraceOutputHandler handler(os); |
| 376 ProcessBacktrace(trace_, count_, &handler); |
| 377 } |
| 378 |
| 379 namespace internal { |
| 380 |
| 381 // NOTE: code from sandbox/linux/seccomp-bpf/demo.cc. |
| 382 char* itoa_r(intptr_t i, char* buf, size_t sz, int base, size_t padding) { |
| 383 // Make sure we can write at least one NUL byte. |
| 384 size_t n = 1; |
| 385 if (n > sz) return NULL; |
| 386 |
| 387 if (base < 2 || base > 16) { |
| 388 buf[0] = '\000'; |
| 389 return NULL; |
| 390 } |
| 391 |
| 392 char* start = buf; |
| 393 |
| 394 uintptr_t j = i; |
| 395 |
| 396 // Handle negative numbers (only for base 10). |
| 397 if (i < 0 && base == 10) { |
| 398 // This does "j = -i" while avoiding integer overflow. |
| 399 j = static_cast<uintptr_t>(-(i + 1)) + 1; |
| 400 |
| 401 // Make sure we can write the '-' character. |
| 402 if (++n > sz) { |
| 403 buf[0] = '\000'; |
| 404 return NULL; |
| 405 } |
| 406 *start++ = '-'; |
| 407 } |
| 408 |
| 409 // Loop until we have converted the entire number. Output at least one |
| 410 // character (i.e. '0'). |
| 411 char* ptr = start; |
| 412 do { |
| 413 // Make sure there is still enough space left in our output buffer. |
| 414 if (++n > sz) { |
| 415 buf[0] = '\000'; |
| 416 return NULL; |
| 417 } |
| 418 |
| 419 // Output the next digit. |
| 420 *ptr++ = "0123456789abcdef"[j % base]; |
| 421 j /= base; |
| 422 |
| 423 if (padding > 0) padding--; |
| 424 } while (j > 0 || padding > 0); |
| 425 |
| 426 // Terminate the output with a NUL character. |
| 427 *ptr = '\000'; |
| 428 |
| 429 // Conversion to ASCII actually resulted in the digits being in reverse |
| 430 // order. We can't easily generate them in forward order, as we can't tell |
| 431 // the number of characters needed until we are done converting. |
| 432 // So, now, we reverse the string (except for the possible "-" sign). |
| 433 while (--ptr > start) { |
| 434 char ch = *ptr; |
| 435 *ptr = *start; |
| 436 *start++ = ch; |
| 437 } |
| 438 return buf; |
| 439 } |
| 440 |
| 441 } // namespace internal |
| 442 |
| 443 } // namespace debug |
| 444 } // namespace base |
| 445 } // namespace v8 |
OLD | NEW |