| 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 <execinfo.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <signal.h> | 10 #include <signal.h> |
| (...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 172 } | 172 } |
| 173 #endif // defined(USE_SYMBOLIZE) | 173 #endif // defined(USE_SYMBOLIZE) |
| 174 } | 174 } |
| 175 | 175 |
| 176 void PrintToStderr(const char* output) { | 176 void PrintToStderr(const char* output) { |
| 177 // NOTE: This code MUST be async-signal safe (it's used by in-process | 177 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 178 // stack dumping signal handler). NO malloc or stdio is allowed here. | 178 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 179 ignore_result(HANDLE_EINTR(write(STDERR_FILENO, output, strlen(output)))); | 179 ignore_result(HANDLE_EINTR(write(STDERR_FILENO, output, strlen(output)))); |
| 180 } | 180 } |
| 181 | 181 |
| 182 #if !defined(OS_IOS) |
| 182 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 183 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { |
| 183 // NOTE: This code MUST be async-signal safe. | 184 // NOTE: This code MUST be async-signal safe. |
| 184 // NO malloc or stdio is allowed here. | 185 // NO malloc or stdio is allowed here. |
| 185 | 186 |
| 186 // Record the fact that we are in the signal handler now, so that the rest | 187 // Record the fact that we are in the signal handler now, so that the rest |
| 187 // of StackTrace can behave in an async-signal-safe manner. | 188 // of StackTrace can behave in an async-signal-safe manner. |
| 188 in_signal_handler = 1; | 189 in_signal_handler = 1; |
| 189 | 190 |
| 190 if (BeingDebugged()) | 191 if (BeingDebugged()) |
| 191 BreakDebugger(); | 192 BreakDebugger(); |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 364 context->uc_mcontext->__ss.__cs, | 365 context->uc_mcontext->__ss.__cs, |
| 365 context->uc_mcontext->__ss.__ds, | 366 context->uc_mcontext->__ss.__ds, |
| 366 context->uc_mcontext->__ss.__es, | 367 context->uc_mcontext->__ss.__es, |
| 367 context->uc_mcontext->__ss.__fs, | 368 context->uc_mcontext->__ss.__fs, |
| 368 context->uc_mcontext->__ss.__gs)); | 369 context->uc_mcontext->__ss.__gs)); |
| 369 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); | 370 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); |
| 370 #endif // ARCH_CPU_32_BITS | 371 #endif // ARCH_CPU_32_BITS |
| 371 #endif // defined(OS_MACOSX) | 372 #endif // defined(OS_MACOSX) |
| 372 _exit(1); | 373 _exit(1); |
| 373 } | 374 } |
| 375 #endif // !defined(OS_IOS) |
| 374 | 376 |
| 375 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { | 377 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { |
| 376 public: | 378 public: |
| 377 PrintBacktraceOutputHandler() {} | 379 PrintBacktraceOutputHandler() {} |
| 378 | 380 |
| 379 virtual void HandleOutput(const char* output) OVERRIDE { | 381 virtual void HandleOutput(const char* output) OVERRIDE { |
| 380 // NOTE: This code MUST be async-signal safe (it's used by in-process | 382 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 381 // stack dumping signal handler). NO malloc or stdio is allowed here. | 383 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 382 PrintToStderr(output); | 384 PrintToStderr(output); |
| 383 } | 385 } |
| (...skipping 10 matching lines...) Expand all Loading... |
| 394 virtual void HandleOutput(const char* output) OVERRIDE { | 396 virtual void HandleOutput(const char* output) OVERRIDE { |
| 395 (*os_) << output; | 397 (*os_) << output; |
| 396 } | 398 } |
| 397 | 399 |
| 398 private: | 400 private: |
| 399 std::ostream* os_; | 401 std::ostream* os_; |
| 400 | 402 |
| 401 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); | 403 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); |
| 402 }; | 404 }; |
| 403 | 405 |
| 406 #if !defined(OS_IOS) |
| 404 void WarmUpBacktrace() { | 407 void WarmUpBacktrace() { |
| 405 // Warm up stack trace infrastructure. It turns out that on the first | 408 // Warm up stack trace infrastructure. It turns out that on the first |
| 406 // call glibc initializes some internal data structures using pthread_once, | 409 // call glibc initializes some internal data structures using pthread_once, |
| 407 // and even backtrace() can call malloc(), leading to hangs. | 410 // and even backtrace() can call malloc(), leading to hangs. |
| 408 // | 411 // |
| 409 // Example stack trace snippet (with tcmalloc): | 412 // Example stack trace snippet (with tcmalloc): |
| 410 // | 413 // |
| 411 // #8 0x0000000000a173b5 in tc_malloc | 414 // #8 0x0000000000a173b5 in tc_malloc |
| 412 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 | 415 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 |
| 413 // #9 0x00007ffff7de7900 in _dl_map_object_deps at dl-deps.c:517 | 416 // #9 0x00007ffff7de7900 in _dl_map_object_deps at dl-deps.c:517 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 426 // #19 0x00007ffff61efa14 in __GI___backtrace | 429 // #19 0x00007ffff61efa14 in __GI___backtrace |
| 427 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 | 430 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 |
| 428 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace | 431 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace |
| 429 // at base/debug/stack_trace_posix.cc:175 | 432 // at base/debug/stack_trace_posix.cc:175 |
| 430 // #21 0x00000000007a4ae5 in | 433 // #21 0x00000000007a4ae5 in |
| 431 // base::(anonymous namespace)::StackDumpSignalHandler | 434 // base::(anonymous namespace)::StackDumpSignalHandler |
| 432 // at base/process_util_posix.cc:172 | 435 // at base/process_util_posix.cc:172 |
| 433 // #22 <signal handler called> | 436 // #22 <signal handler called> |
| 434 StackTrace stack_trace; | 437 StackTrace stack_trace; |
| 435 } | 438 } |
| 439 #endif // !defined(OS_IOS) |
| 436 | 440 |
| 437 } // namespace | 441 } // namespace |
| 438 | 442 |
| 439 #if !defined(OS_IOS) | 443 #if !defined(OS_IOS) |
| 440 bool EnableInProcessStackDumping() { | 444 bool EnableInProcessStackDumping() { |
| 441 // When running in an application, our code typically expects SIGPIPE | 445 // When running in an application, our code typically expects SIGPIPE |
| 442 // to be ignored. Therefore, when testing that same code, it should run | 446 // to be ignored. Therefore, when testing that same code, it should run |
| 443 // with SIGPIPE ignored as well. | 447 // with SIGPIPE ignored as well. |
| 444 struct sigaction sigpipe_action; | 448 struct sigaction sigpipe_action; |
| 445 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); | 449 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 549 *ptr = *start; | 553 *ptr = *start; |
| 550 *start++ = ch; | 554 *start++ = ch; |
| 551 } | 555 } |
| 552 return buf; | 556 return buf; |
| 553 } | 557 } |
| 554 | 558 |
| 555 } // namespace internal | 559 } // namespace internal |
| 556 | 560 |
| 557 } // namespace debug | 561 } // namespace debug |
| 558 } // namespace base | 562 } // namespace base |
| OLD | NEW |