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