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(); |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 internal::itoa_r(registers[i].value, buf, sizeof(buf), | 322 internal::itoa_r(registers[i].value, buf, sizeof(buf), |
324 16, kRegisterPadding); | 323 16, kRegisterPadding); |
325 PrintToStderr(buf); | 324 PrintToStderr(buf); |
326 | 325 |
327 if ((i + 1) % 4 == 0) | 326 if ((i + 1) % 4 == 0) |
328 PrintToStderr("\n"); | 327 PrintToStderr("\n"); |
329 } | 328 } |
330 PrintToStderr("\n"); | 329 PrintToStderr("\n"); |
331 #endif | 330 #endif |
332 #elif defined(OS_MACOSX) | 331 #elif defined(OS_MACOSX) |
333 // TODO(shess): Port to 64-bit. | 332 // TODO(shess): Port to 64-bit, and ARM architecture (32 and 64-bit). |
334 #if ARCH_CPU_X86_FAMILY && ARCH_CPU_32_BITS | 333 #if ARCH_CPU_X86_FAMILY && ARCH_CPU_32_BITS |
335 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); | 334 ucontext_t* context = reinterpret_cast<ucontext_t*>(void_context); |
336 size_t len; | 335 size_t len; |
337 | 336 |
338 // NOTE: Even |snprintf()| is not on the approved list for signal | 337 // 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 | 338 // handlers, but buffered I/O is definitely not on the list due to |
340 // potential for |malloc()|. | 339 // potential for |malloc()|. |
341 len = static_cast<size_t>( | 340 len = static_cast<size_t>( |
342 snprintf(buf, sizeof(buf), | 341 snprintf(buf, sizeof(buf), |
343 "ax: %x, bx: %x, cx: %x, dx: %x\n", | 342 "ax: %x, bx: %x, cx: %x, dx: %x\n", |
(...skipping 21 matching lines...) Expand all Loading... |
365 context->uc_mcontext->__ss.__cs, | 364 context->uc_mcontext->__ss.__cs, |
366 context->uc_mcontext->__ss.__ds, | 365 context->uc_mcontext->__ss.__ds, |
367 context->uc_mcontext->__ss.__es, | 366 context->uc_mcontext->__ss.__es, |
368 context->uc_mcontext->__ss.__fs, | 367 context->uc_mcontext->__ss.__fs, |
369 context->uc_mcontext->__ss.__gs)); | 368 context->uc_mcontext->__ss.__gs)); |
370 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); | 369 write(STDERR_FILENO, buf, std::min(len, sizeof(buf) - 1)); |
371 #endif // ARCH_CPU_32_BITS | 370 #endif // ARCH_CPU_32_BITS |
372 #endif // defined(OS_MACOSX) | 371 #endif // defined(OS_MACOSX) |
373 _exit(1); | 372 _exit(1); |
374 } | 373 } |
375 #endif // !defined(OS_IOS) | |
376 | 374 |
377 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { | 375 class PrintBacktraceOutputHandler : public BacktraceOutputHandler { |
378 public: | 376 public: |
379 PrintBacktraceOutputHandler() {} | 377 PrintBacktraceOutputHandler() {} |
380 | 378 |
381 virtual void HandleOutput(const char* output) OVERRIDE { | 379 virtual void HandleOutput(const char* output) OVERRIDE { |
382 // NOTE: This code MUST be async-signal safe (it's used by in-process | 380 // 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. | 381 // stack dumping signal handler). NO malloc or stdio is allowed here. |
384 PrintToStderr(output); | 382 PrintToStderr(output); |
385 } | 383 } |
(...skipping 10 matching lines...) Expand all Loading... |
396 virtual void HandleOutput(const char* output) OVERRIDE { | 394 virtual void HandleOutput(const char* output) OVERRIDE { |
397 (*os_) << output; | 395 (*os_) << output; |
398 } | 396 } |
399 | 397 |
400 private: | 398 private: |
401 std::ostream* os_; | 399 std::ostream* os_; |
402 | 400 |
403 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); | 401 DISALLOW_COPY_AND_ASSIGN(StreamBacktraceOutputHandler); |
404 }; | 402 }; |
405 | 403 |
406 #if !defined(OS_IOS) | |
407 void WarmUpBacktrace() { | 404 void WarmUpBacktrace() { |
408 // Warm up stack trace infrastructure. It turns out that on the first | 405 // Warm up stack trace infrastructure. It turns out that on the first |
409 // call glibc initializes some internal data structures using pthread_once, | 406 // call glibc initializes some internal data structures using pthread_once, |
410 // and even backtrace() can call malloc(), leading to hangs. | 407 // and even backtrace() can call malloc(), leading to hangs. |
411 // | 408 // |
412 // Example stack trace snippet (with tcmalloc): | 409 // Example stack trace snippet (with tcmalloc): |
413 // | 410 // |
414 // #8 0x0000000000a173b5 in tc_malloc | 411 // #8 0x0000000000a173b5 in tc_malloc |
415 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 | 412 // at ./third_party/tcmalloc/chromium/src/debugallocation.cc:1161 |
416 // #9 0x00007ffff7de7900 in _dl_map_object_deps at dl-deps.c:517 | 413 // #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 | 426 // #19 0x00007ffff61efa14 in __GI___backtrace |
430 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 | 427 // at ../sysdeps/x86_64/../ia64/backtrace.c:104 |
431 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace | 428 // #20 0x0000000000752a54 in base::debug::StackTrace::StackTrace |
432 // at base/debug/stack_trace_posix.cc:175 | 429 // at base/debug/stack_trace_posix.cc:175 |
433 // #21 0x00000000007a4ae5 in | 430 // #21 0x00000000007a4ae5 in |
434 // base::(anonymous namespace)::StackDumpSignalHandler | 431 // base::(anonymous namespace)::StackDumpSignalHandler |
435 // at base/process_util_posix.cc:172 | 432 // at base/process_util_posix.cc:172 |
436 // #22 <signal handler called> | 433 // #22 <signal handler called> |
437 StackTrace stack_trace; | 434 StackTrace stack_trace; |
438 } | 435 } |
439 #endif // !defined(OS_IOS) | |
440 | 436 |
441 } // namespace | 437 } // namespace |
442 | 438 |
443 #if !defined(OS_IOS) | |
444 bool EnableInProcessStackDumping() { | 439 bool EnableInProcessStackDumping() { |
445 // When running in an application, our code typically expects SIGPIPE | 440 // When running in an application, our code typically expects SIGPIPE |
446 // to be ignored. Therefore, when testing that same code, it should run | 441 // to be ignored. Therefore, when testing that same code, it should run |
447 // with SIGPIPE ignored as well. | 442 // with SIGPIPE ignored as well. |
448 struct sigaction sigpipe_action; | 443 struct sigaction sigpipe_action; |
449 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); | 444 memset(&sigpipe_action, 0, sizeof(sigpipe_action)); |
450 sigpipe_action.sa_handler = SIG_IGN; | 445 sigpipe_action.sa_handler = SIG_IGN; |
451 sigemptyset(&sigpipe_action.sa_mask); | 446 sigemptyset(&sigpipe_action.sa_mask); |
452 bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0); | 447 bool success = (sigaction(SIGPIPE, &sigpipe_action, NULL) == 0); |
453 | 448 |
454 // Avoid hangs during backtrace initialization, see above. | 449 // Avoid hangs during backtrace initialization, see above. |
455 WarmUpBacktrace(); | 450 WarmUpBacktrace(); |
456 | 451 |
457 struct sigaction action; | 452 struct sigaction action; |
458 memset(&action, 0, sizeof(action)); | 453 memset(&action, 0, sizeof(action)); |
459 action.sa_flags = SA_RESETHAND | SA_SIGINFO; | 454 action.sa_flags = SA_RESETHAND | SA_SIGINFO; |
460 action.sa_sigaction = &StackDumpSignalHandler; | 455 action.sa_sigaction = &StackDumpSignalHandler; |
461 sigemptyset(&action.sa_mask); | 456 sigemptyset(&action.sa_mask); |
462 | 457 |
463 success &= (sigaction(SIGILL, &action, NULL) == 0); | 458 success &= (sigaction(SIGILL, &action, NULL) == 0); |
464 success &= (sigaction(SIGABRT, &action, NULL) == 0); | 459 success &= (sigaction(SIGABRT, &action, NULL) == 0); |
465 success &= (sigaction(SIGFPE, &action, NULL) == 0); | 460 success &= (sigaction(SIGFPE, &action, NULL) == 0); |
466 success &= (sigaction(SIGBUS, &action, NULL) == 0); | 461 success &= (sigaction(SIGBUS, &action, NULL) == 0); |
467 success &= (sigaction(SIGSEGV, &action, NULL) == 0); | 462 success &= (sigaction(SIGSEGV, &action, NULL) == 0); |
468 success &= (sigaction(SIGSYS, &action, NULL) == 0); | 463 success &= (sigaction(SIGSYS, &action, NULL) == 0); |
469 | 464 |
470 return success; | 465 return success; |
471 } | 466 } |
472 #endif // !defined(OS_IOS) | |
473 | 467 |
474 StackTrace::StackTrace() { | 468 StackTrace::StackTrace() { |
475 // NOTE: This code MUST be async-signal safe (it's used by in-process | 469 // 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. | 470 // stack dumping signal handler). NO malloc or stdio is allowed here. |
477 | 471 |
478 // Though the backtrace API man page does not list any possible negative | 472 // Though the backtrace API man page does not list any possible negative |
479 // return values, we take no chance. | 473 // return values, we take no chance. |
480 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); | 474 count_ = std::max(backtrace(trace_, arraysize(trace_)), 0); |
481 } | 475 } |
482 | 476 |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
553 *ptr = *start; | 547 *ptr = *start; |
554 *start++ = ch; | 548 *start++ = ch; |
555 } | 549 } |
556 return buf; | 550 return buf; |
557 } | 551 } |
558 | 552 |
559 } // namespace internal | 553 } // namespace internal |
560 | 554 |
561 } // namespace debug | 555 } // namespace debug |
562 } // namespace base | 556 } // namespace base |
OLD | NEW |