SIGUSR1 is used to examine the current callstack, without aborting. (useful when the process seems stuck) TODO: Only use async-signal-safe operations in the signal handler.
| 409 | // (useful when the process seems stuck) |
| 410 | // TODO: Only use async-signal-safe operations in the signal handler. |
| 411 | static void TrapHandler(int sig_num, siginfo_t *info, void *ucontext) { |
| 412 | std::ostringstream oops; |
| 413 | auto *uc = static_cast<ucontext_t *>(ucontext); |
| 414 | bool is_fatal = (sig_num != SIGUSR1); |
| 415 | static volatile bool already_trapped = false; |
| 416 | |
| 417 | // avoid recursive traps |
| 418 | if (is_fatal && !__sync_bool_compare_and_swap(&already_trapped, false, true)) |
| 419 | return; |
| 420 | |
| 421 | #if __i386 |
| 422 | trap_ip = reinterpret_cast<void *>(uc->uc_mcontext.gregs[REG_EIP]); |
| 423 | #elif __x86_64 |
| 424 | trap_ip = reinterpret_cast<void *>(uc->uc_mcontext.gregs[REG_RIP]); |
| 425 | #else |
| 426 | #error neither x86 or x86-64 |
| 427 | #endif |
| 428 | |
| 429 | if (is_fatal) { |
| 430 | oops << "A critical error has occured. Aborting..." << std::endl; |
| 431 | } |
| 432 | |
| 433 | oops << "Signal: " << sig_num << " (" << strsignal(sig_num) |
| 434 | << "), si_code: " << info->si_code << " (" |
| 435 | << si_code_to_str(sig_num, info->si_code) << ")" << std::endl; |
| 436 | |
| 437 | oops << "pid: " << getpid() << ", tid: " << (pid_t)syscall(SYS_gettid) |
| 438 | << ", address: " << info->si_addr << ", IP: " << trap_ip << std::endl; |
| 439 | |
| 440 | if (is_fatal) { |
| 441 | oops << DumpStack(); |
| 442 | oops_msg = oops.str(); |
| 443 | GoPanic(); |
| 444 | // Never reaches here. LOG(FATAL) will terminate the process. |
| 445 | } else { |
| 446 | LOG(INFO) << oops.str() << DumpStack(); |
| 447 | trap_ip = nullptr; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void SetTrapHandler() { |
| 452 | const int signals[] = { |
nothing calls this directly
no test coverage detected