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 #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) | |
| 183 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { | 182 void StackDumpSignalHandler(int signal, siginfo_t* info, void* void_context) { |
| 184 // NOTE: This code MUST be async-signal safe. | 183 // NOTE: This code MUST be async-signal safe. |
| 185 // NO malloc or stdio is allowed here. | 184 // NO malloc or stdio is allowed here. |
| 186 | 185 |
| 187 // Record the fact that we are in the signal handler now, so that the rest | 186 // Record the fact that we are in the signal handler now, so that the rest |
| 188 // of StackTrace can behave in an async-signal-safe manner. | 187 // of StackTrace can behave in an async-signal-safe manner. |
| 189 in_signal_handler = 1; | 188 in_signal_handler = 1; |
| 190 | 189 |
| 191 if (BeingDebugged()) | 190 if (BeingDebugged()) |
| 192 BreakDebugger(); | 191 BreakDebugger(); |
| 193 | 192 |
| 194 PrintToStderr("Received signal "); | 193 PrintToStderr("Received signal "); |
| 195 char buf[1024] = { 0 }; | 194 char buf[1024] = { 0 }; |
| 196 internal::itoa_r(signal, buf, sizeof(buf), 10, 0); | 195 internal::itoa_r(signal, buf, sizeof(buf), 10, 0); |
| 197 PrintToStderr(buf); | 196 PrintToStderr(buf); |
| 197 #if !defined(OS_IOS) | |
| 198 // NOTE: on iOS, info and void_context are incorrect. Ignore them. | |
|
Mark Mentovai
2014/01/15 19:14:06
If you’ve got a reference (link?) to point to here
| |
| 198 if (signal == SIGBUS) { | 199 if (signal == SIGBUS) { |
| 199 if (info->si_code == BUS_ADRALN) | 200 if (info->si_code == BUS_ADRALN) |
| 200 PrintToStderr(" BUS_ADRALN "); | 201 PrintToStderr(" BUS_ADRALN "); |
| 201 else if (info->si_code == BUS_ADRERR) | 202 else if (info->si_code == BUS_ADRERR) |
| 202 PrintToStderr(" BUS_ADRERR "); | 203 PrintToStderr(" BUS_ADRERR "); |
| 203 else if (info->si_code == BUS_OBJERR) | 204 else if (info->si_code == BUS_OBJERR) |
| 204 PrintToStderr(" BUS_OBJERR "); | 205 PrintToStderr(" BUS_OBJERR "); |
| 205 else | 206 else |
| 206 PrintToStderr(" <unknown> "); | 207 PrintToStderr(" <unknown> "); |
| 207 } else if (signal == SIGFPE) { | 208 } else if (signal == SIGFPE) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 247 PrintToStderr(" SEGV_ACCERR "); | 248 PrintToStderr(" SEGV_ACCERR "); |
| 248 else | 249 else |
| 249 PrintToStderr(" <unknown> "); | 250 PrintToStderr(" <unknown> "); |
| 250 } | 251 } |
| 251 if (signal == SIGBUS || signal == SIGFPE || | 252 if (signal == SIGBUS || signal == SIGFPE || |
| 252 signal == SIGILL || signal == SIGSEGV) { | 253 signal == SIGILL || signal == SIGSEGV) { |
| 253 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), | 254 internal::itoa_r(reinterpret_cast<intptr_t>(info->si_addr), |
| 254 buf, sizeof(buf), 16, 12); | 255 buf, sizeof(buf), 16, 12); |
| 255 PrintToStderr(buf); | 256 PrintToStderr(buf); |
| 256 } | 257 } |
| 258 #endif // !defined(OS_IOS) | |
| 257 PrintToStderr("\n"); | 259 PrintToStderr("\n"); |
| 258 | 260 |
| 259 debug::StackTrace().Print(); | 261 debug::StackTrace().Print(); |
| 260 | 262 |
| 261 #if defined(OS_LINUX) | 263 #if defined(OS_LINUX) |
| 262 #if ARCH_CPU_X86_FAMILY | 264 #if ARCH_CPU_X86_FAMILY |
| 263 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 265 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
| 264 const struct { | 266 const struct { |
| 265 const char* label; | 267 const char* label; |
| 266 greg_t value; | 268 greg_t value; |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 322 PrintToStderr(registers[i].label); | 324 PrintToStderr(registers[i].label); |
| 323 internal::itoa_r(registers[i].value, buf, sizeof(buf), | 325 internal::itoa_r(registers[i].value, buf, sizeof(buf), |
| 324 16, kRegisterPadding); | 326 16, kRegisterPadding); |
| 325 PrintToStderr(buf); | 327 PrintToStderr(buf); |
| 326 | 328 |
| 327 if ((i + 1) % 4 == 0) | 329 if ((i + 1) % 4 == 0) |
| 328 PrintToStderr("\n"); | 330 PrintToStderr("\n"); |
| 329 } | 331 } |
| 330 PrintToStderr("\n"); | 332 PrintToStderr("\n"); |
| 331 #endif | 333 #endif |
| 332 #elif defined(OS_MACOSX) | 334 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
|
Mark Mentovai
2014/01/15 19:14:06
You probably don’t need !defined(OS_IOS) here, bec
| |
| 333 // TODO(shess): Port to 64-bit. | 335 // TODO(shess): Port to 64-bit. |
| 334 #if ARCH_CPU_X86_FAMILY && ARCH_CPU_32_BITS | 336 #if ARCH_CPU_X86_FAMILY && ARCH_CPU_32_BITS |
| 335 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 337 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
| 336 size_t len; | 338 size_t len; |
| 337 | 339 |
| 338 // NOTE: Even |snprintf()| is not on the approved list for signal | 340 // NOTE: Even |snprintf()| is not on the approved list for signal |
| 339 // handlers, but buffered I/O is definitely not on the list due to | 341 // handlers, but buffered I/O is definitely not on the list due to |
| 340 // potential for |malloc()|. | 342 // potential for |malloc()|. |
| 341 len = static_cast<size_t>( | 343 len = static_cast<size_t>( |
| 342 snprintf(buf, sizeof(buf), | 344 snprintf(buf, sizeof(buf), |
| (...skipping 19 matching lines...) Expand all Loading... | |
| 362 snprintf(buf, sizeof(buf), | 364 snprintf(buf, sizeof(buf), |
| 363 "ip: %x, cs: %x, ds: %x, es: %x, fs: %x, gs: %x\n", | 365 "ip: %x, cs: %x, ds: %x, es: %x, fs: %x, gs: %x\n", |
| 364 context->uc_mcontext->__ss.__eip, | 366 context->uc_mcontext->__ss.__eip, |
| 365 context->uc_mcontext->__ss.__cs, | 367 context->uc_mcontext->__ss.__cs, |
| 366 context->uc_mcontext->__ss.__ds, | 368 context->uc_mcontext->__ss.__ds, |
| 367 context->uc_mcontext->__ss.__es, | 369 context->uc_mcontext->__ss.__es, |
| 368 context->uc_mcontext->__ss.__fs, | 370 context->uc_mcontext->__ss.__fs, |
| 369 context->uc_mcontext->__ss.__gs)); | 371 context->uc_mcontext->__ss.__gs)); |
| 370 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); | 372 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); |
| 371 #endif // ARCH_CPU_32_BITS | 373 #endif // ARCH_CPU_32_BITS |
| 372 #endif // defined(OS_MACOSX) | 374 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 373 _exit(1); | 375 _exit(1); |
| 374 } | 376 } |
| 375 #endif // !defined(OS_IOS) | |
| 376 | 377 |
| 377 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { | 378 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { |
| 378 public: | 379 public: |
| 379 PrintBacktraceOutputHandler() {} | 380 PrintBacktraceOutputHandler() {} |
| 380 | 381 |
| 381 virtual void HandleOutput(const char* output) OVERRIDE { | 382 virtual void HandleOutput(const char* output) OVERRIDE { |
| 382 // NOTE: This code MUST be async-signal safe (it's used by in-process | 383 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 383 // stack dumping signal handler). NO malloc or stdio is allowed here. | 384 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 384 PrintToStderr(output); | 385 PrintToStderr(output); |
| 385 } | 386 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 396 virtual void HandleOutput(const char* output) OVERRIDE { | 397 virtual void HandleOutput(const char* output) OVERRIDE { |
| 397 (*os_) << output; | 398 (*os_) << output; |
| 398 } | 399 } |
| 399 | 400 |
| 400 private: | 401 private: |
| 401 std::ostream* os_; | 402 std::ostream* os_; |
| 402 | 403 |
| 403 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); | 404 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); |
| 404 }; | 405 }; |
| 405 | 406 |
| 406 #if !defined(OS_IOS) | |
| 407 void WarmUpBacktrace() { | 407 void WarmUpBacktrace() { |
| 408 // 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 |
| 409 // call glibc initializes some internal data structures using pthread_once, | 409 // call glibc initializes some internal data structures using pthread_once, |
| 410 // and even backtrace() can call malloc(), leading to hangs. | 410 // and even backtrace() can call malloc(), leading to hangs. |
| 411 // | 411 // |
| 412 // Example stack trace snippet (with tcmalloc): | 412 // Example stack trace snippet (with tcmalloc): |
| 413 // | 413 // |
| 414 // #8 0x0000000000a173b5 in tc_malloc | 414 // #8 0x0000000000a173b5 in tc_malloc |
| 415 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 | 415 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 |
| 416 // #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... | |
| 429 // #19 0x00007ffff61efa14 in __GI___backtrace | 429 // #19 0x00007ffff61efa14 in __GI___backtrace |
| 430 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 | 430 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 |
| 431 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace | 431 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace |
| 432 // at base/debug/stack_trace_posix.cc:175 | 432 // at base/debug/stack_trace_posix.cc:175 |
| 433 // #21 0x00000000007a4ae5 in | 433 // #21 0x00000000007a4ae5 in |
| 434 // base::(anonymous namespace)::StackDumpSignalHandler | 434 // base::(anonymous namespace)::StackDumpSignalHandler |
| 435 // at base/process_util_posix.cc:172 | 435 // at base/process_util_posix.cc:172 |
| 436 // #22 <signal handler called> | 436 // #22 <signal handler called> |
| 437 StackTrace stack_trace; | 437 StackTrace stack_trace; |
| 438 } | 438 } |
| 439 #endif // !defined(OS_IOS) | |
| 440 | 439 |
| 441 } // namespace | 440 } // namespace |
| 442 | 441 |
| 443 #if !defined(OS_IOS) | |
| 444 bool EnableInProcessStackDumping() { | 442 bool EnableInProcessStackDumping() { |
| 445 // When running in an application, our code typically expects SIGPIPE | 443 // When running in an application, our code typically expects SIGPIPE |
| 446 // to be ignored. Therefore, when testing that same code, it should run | 444 // to be ignored. Therefore, when testing that same code, it should run |
| 447 // with SIGPIPE ignored as well. | 445 // with SIGPIPE ignored as well. |
| 448 struct sigaction sigpipe_action; | 446 struct sigaction sigpipe_action; |
| 449 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); | 447 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); |
| 450 sigpipe_action.sa_handler = SIG_IGN; | 448 sigpipe_action.sa_handler = SIG_IGN; |
| 451 sigemptyset(&sigpipe_action.sa_mask); | 449 sigemptyset(&sigpipe_action.sa_mask); |
| 452 bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0); | 450 bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0); |
| 453 | 451 |
| 454 // Avoid hangs during backtrace initialization, see above. | 452 // Avoid hangs during backtrace initialization, see above. |
| 455 WarmUpBacktrace(); | 453 WarmUpBacktrace(); |
| 456 | 454 |
| 457 struct sigaction action; | 455 struct sigaction action; |
| 458 memset(&action, 0, sizeof(action)); | 456 memset(&action, 0, sizeof(action)); |
| 459 action.sa_flags = SA_RESETHAND | SA_SIGINFO; | 457 action.sa_flags = SA_RESETHAND | SA_SIGINFO; |
| 460 action.sa_sigaction = &StackDumpSignalHandler; | 458 action.sa_sigaction = &StackDumpSignalHandler; |
| 461 sigemptyset(&action.sa_mask); | 459 sigemptyset(&action.sa_mask); |
| 462 | 460 |
| 463 success &= (sigaction(SIGILL, &action, NULL) == 0); | 461 success &= (sigaction(SIGILL, &action, NULL) == 0); |
| 464 success &= (sigaction(SIGABRT, &action, NULL) == 0); | 462 success &= (sigaction(SIGABRT, &action, NULL) == 0); |
| 465 success &= (sigaction(SIGFPE, &action, NULL) == 0); | 463 success &= (sigaction(SIGFPE, &action, NULL) == 0); |
| 466 success &= (sigaction(SIGBUS, &action, NULL) == 0); | 464 success &= (sigaction(SIGBUS, &action, NULL) == 0); |
| 467 success &= (sigaction(SIGSEGV, &action, NULL) == 0); | 465 success &= (sigaction(SIGSEGV, &action, NULL) == 0); |
| 468 success &= (sigaction(SIGSYS, &action, NULL) == 0); | 466 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
| 469 | 467 |
| 470 return success; | 468 return success; |
| 471 } | 469 } |
| 472 #endif // !defined(OS_IOS) | |
| 473 | 470 |
| 474 StackTrace::StackTrace() { | 471 StackTrace::StackTrace() { |
| 475 // NOTE: This code MUST be async-signal safe (it's used by in-process | 472 // NOTE: This code MUST be async-signal safe (it's used by in-process |
| 476 // stack dumping signal handler). NO malloc or stdio is allowed here. | 473 // stack dumping signal handler). NO malloc or stdio is allowed here. |
| 477 | 474 |
| 478 // Though the backtrace API man page does not list any possible negative | 475 // Though the backtrace API man page does not list any possible negative |
| 479 // return values, we take no chance. | 476 // return values, we take no chance. |
| 480 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | 477 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
| 481 } | 478 } |
| 482 | 479 |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 553 *ptr = *start; | 550 *ptr = *start; |
| 554 *start++ = ch; | 551 *start++ = ch; |
| 555 } | 552 } |
| 556 return buf; | 553 return buf; |
| 557 } | 554 } |
| 558 | 555 |
| 559 } // namespace internal | 556 } // namespace internal |
| 560 | 557 |
| 561 } // namespace debug | 558 } // namespace debug |
| 562 } // namespace base | 559 } // namespace base |
| OLD | NEW |