You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

signalhandler.cpp 12 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include <algorithm>
  2. #include <csignal>
  3. #include <cstring>
  4. #include <ctime>
  5. #include <mutex>
  6. #include <sstream>
  7. #include <thread>
  8. extern "C" {
  9. #define UNW_LOCAL_ONLY
  10. #include <libunwind.h>
  11. }
  12. void func_backtrace(void)
  13. {
  14. unw_context_t ctx = {0};
  15. unw_cursor_t cursor = {0};
  16. unw_word_t offset = 0;
  17. unw_word_t pc = 0;
  18. int8_t func_name[128] = {0};
  19. int32_t ret = 0;
  20. printf("\n----------------------stack backtrace----------------------------\n");
  21. ret = unw_getcontext(&ctx);
  22. if(0 != ret)
  23. {
  24. printf("get context failed\n");
  25. return;
  26. }
  27. ret = unw_init_local(&cursor, &ctx);
  28. if(0 != ret)
  29. {
  30. printf("init local cursor failed\n");
  31. return;
  32. }
  33. while(0 < unw_step(&cursor))
  34. {
  35. ret = unw_get_proc_name(&cursor, func_name, sizeof(func_name), &offset);
  36. if(0 != ret)
  37. {
  38. printf("can not get func name\n");
  39. }
  40. else
  41. {
  42. unw_get_reg(&cursor, UNW_REG_IP, &pc);
  43. printf("0x%lx:(%s+0x%lx)\n", pc, func_name, offset);
  44. }
  45. }
  46. }
  47. namespace xlog {
  48. namespace {
  49. // NOTE(anjingyu): Copied from glog 0.6.0
  50. //
  51. // We'll install the failure signal handler for these signals. We could
  52. // use strsignal() to get signal names, but we don't use it to avoid
  53. // introducing yet another #ifdef complication.
  54. //
  55. // The list should be synced with the comment in signalhandler.h.
  56. const struct {
  57. int number;
  58. const char* name;
  59. } kFailureSignals[] = {
  60. {SIGSEGV, "SIGSEGV"}, {SIGILL, "SIGILL"},
  61. {SIGFPE, "SIGFPE"}, {SIGABRT, "SIGABRT"},
  62. #if !defined(OS_WINDOWS)
  63. {SIGBUS, "SIGBUS"},
  64. #endif
  65. {SIGTERM, "SIGTERM"},
  66. };
  67. static bool kFailureSignalHandlerInstalled = false;
  68. // The class is used for formatting error messages.
  69. // We don't use printf() as it's not async signal safe.
  70. class MinimalFormatter {
  71. public:
  72. MinimalFormatter(char* buffer, size_t size)
  73. : buffer_(buffer), cursor_(buffer), end_(buffer + size) {}
  74. // Returns the number of bytes written in the buffer.
  75. std::size_t num_bytes_written() const {
  76. return static_cast<std::size_t>(cursor_ - buffer_);
  77. }
  78. // Appends string from "str" and updates the internal cursor.
  79. void AppendString(const char* str) {
  80. ptrdiff_t i = 0;
  81. while (str[i] != '\0' && cursor_ + i < end_) {
  82. cursor_[i] = str[i];
  83. ++i;
  84. }
  85. cursor_ += i;
  86. }
  87. // Formats "number" in "radix" and updates the internal cursor.
  88. // Lowercase letters are used for 'a' - 'z'.
  89. void AppendUint64(uint64 number, unsigned radix) {
  90. unsigned i = 0;
  91. while (cursor_ + i < end_) {
  92. const uint64 tmp = number % radix;
  93. number /= radix;
  94. cursor_[i] = static_cast<char>(tmp < 10 ? '0' + tmp : 'a' + tmp - 10);
  95. ++i;
  96. if (number == 0) {
  97. break;
  98. }
  99. }
  100. // Reverse the bytes written.
  101. std::reverse(cursor_, cursor_ + i);
  102. cursor_ += i;
  103. }
  104. // Formats "number" as hexadecimal number, and updates the internal
  105. // cursor. Padding will be added in front if needed.
  106. void AppendHexWithPadding(uint64 number, int width) {
  107. char* start = cursor_;
  108. AppendString("0x");
  109. AppendUint64(number, 16);
  110. // Move to right and add padding in front if needed.
  111. if (cursor_ < start + width) {
  112. const int64 delta = start + width - cursor_;
  113. std::copy(start, cursor_, start + delta);
  114. std::fill(start, start + delta, ' ');
  115. cursor_ = start + width;
  116. }
  117. }
  118. private:
  119. char* buffer_;
  120. char* cursor_;
  121. const char* const end_;
  122. };
  123. // Writes the given data with the size to the standard error.
  124. void WriteToStderr(const char* data, size_t size) {
  125. if (write(fileno(stderr), data, size) < 0) {
  126. // Ignore errors.
  127. }
  128. }
  129. // The writer function can be changed by InstallFailureWriter().
  130. void (*g_failure_writer)(const char* data, size_t size) = WriteToStderr;
  131. // Dumps time information. We don't dump human-readable time information
  132. // as localtime() is not guaranteed to be async signal safe.
  133. void DumpTimeInfo() {
  134. time_t time_in_sec = time(nullptr);
  135. char buf[256]; // Big enough for time info.
  136. MinimalFormatter formatter(buf, sizeof(buf));
  137. formatter.AppendString("*** Aborted at ");
  138. formatter.AppendUint64(static_cast<uint64>(time_in_sec), 10);
  139. formatter.AppendString(" (unix time)");
  140. formatter.AppendString(" try \"date -d @");
  141. formatter.AppendUint64(static_cast<uint64>(time_in_sec), 10);
  142. formatter.AppendString("\" if you are using GNU date ***\n");
  143. g_failure_writer(buf, formatter.num_bytes_written());
  144. }
  145. // TODO(hamaji): Use signal instead of sigaction?
  146. #if defined(HAVE_SIGACTION)
  147. // Dumps information about the signal to STDERR.
  148. void DumpSignalInfo(int signal_number, siginfo_t* siginfo) {
  149. // Get the signal name.
  150. const char* signal_name = nullptr;
  151. for (auto kFailureSignal : kFailureSignals) {
  152. if (signal_number == kFailureSignal.number) {
  153. signal_name = kFailureSignal.name;
  154. }
  155. }
  156. char buf[256]; // Big enough for signal info.
  157. MinimalFormatter formatter(buf, sizeof(buf));
  158. formatter.AppendString("*** ");
  159. if (signal_name) {
  160. formatter.AppendString(signal_name);
  161. } else {
  162. // Use the signal number if the name is unknown. The signal name
  163. // should be known, but just in case.
  164. formatter.AppendString("Signal ");
  165. formatter.AppendUint64(static_cast<uint64>(signal_number), 10);
  166. }
  167. formatter.AppendString(" (@0x");
  168. formatter.AppendUint64(reinterpret_cast<uintptr_t>(siginfo->si_addr), 16);
  169. formatter.AppendString(")");
  170. formatter.AppendString(" received by PID ");
  171. formatter.AppendUint64(static_cast<uint64>(getpid()), 10);
  172. formatter.AppendString(" (TID ");
  173. std::ostringstream oss;
  174. oss << std::showbase << std::hex << std::this_thread::get_id();
  175. formatter.AppendString(oss.str().c_str());
  176. formatter.AppendString(") ");
  177. // Only linux has the PID of the signal sender in si_pid.
  178. #if defined(OS_LINUX)
  179. formatter.AppendString("from PID ");
  180. formatter.AppendUint64(static_cast<uint64>(siginfo->si_pid), 10);
  181. formatter.AppendString("; ");
  182. #endif /* OS_LINUX */
  183. formatter.AppendString("stack trace: ***\n");
  184. g_failure_writer(buf, formatter.num_bytes_written());
  185. }
  186. #endif // HAVE_SIGACTION
  187. // Dumps information about the stack frame to STDERR.
  188. void DumpStackFrameInfo(const char* prefix, void* pc) {
  189. // Get the symbol name.
  190. const char* symbol = "(unknown)";
  191. #if defined(HAVE_SYMBOLIZE)
  192. char symbolized[1024]; // Big enough for a sane symbol.
  193. // Symbolizes the previous address of pc because pc may be in the
  194. // next function.
  195. if (Symbolize(reinterpret_cast<char*>(pc) - 1, symbolized,
  196. sizeof(symbolized))) {
  197. symbol = symbolized;
  198. }
  199. #else
  200. # pragma message( \
  201. "Symbolize functionality is not available for target platform: stack dump will contain empty frames.")
  202. #endif // defined(HAVE_SYMBOLIZE)
  203. char buf[1024]; // Big enough for stack frame info.
  204. MinimalFormatter formatter(buf, sizeof(buf));
  205. formatter.AppendString(prefix);
  206. formatter.AppendString("@ ");
  207. const int width = 2 * sizeof(void*) + 2; // + 2 for "0x".
  208. formatter.AppendHexWithPadding(reinterpret_cast<uintptr_t>(pc), width);
  209. formatter.AppendString(" ");
  210. formatter.AppendString(symbol);
  211. formatter.AppendString("\n");
  212. g_failure_writer(buf, formatter.num_bytes_written());
  213. }
  214. // Invoke the default signal handler.
  215. void InvokeDefaultSignalHandler(int signal_number) {
  216. #ifdef HAVE_SIGACTION
  217. struct sigaction sig_action;
  218. memset(&sig_action, 0, sizeof(sig_action));
  219. sigemptyset(&sig_action.sa_mask);
  220. sig_action.sa_handler = SIG_DFL;
  221. sigaction(signal_number, &sig_action, nullptr);
  222. kill(getpid(), signal_number);
  223. #elif defined(OS_WINDOWS)
  224. signal(signal_number, SIG_DFL);
  225. raise(signal_number);
  226. #endif
  227. }
  228. // This variable is used for protecting FailureSignalHandler() from dumping
  229. // stuff while another thread is doing it. Our policy is to let the first
  230. // thread dump stuff and let other threads do nothing.
  231. // See also comments in FailureSignalHandler().
  232. static std::once_flag signaled;
  233. static void HandleSignal(int signal_number
  234. #if !defined(OS_WINDOWS)
  235. ,
  236. siginfo_t* signal_info, void* ucontext
  237. #endif
  238. ) {
  239. // This is the first time we enter the signal handler. We are going to
  240. // do some interesting stuff from here.
  241. // TODO(satorux): We might want to set timeout here using alarm(), but
  242. // mixing alarm() and sleep() can be a bad idea.
  243. // First dump time info.
  244. DumpTimeInfo();
  245. #if !defined(OS_WINDOWS)
  246. // Get the program counter from ucontext.
  247. void* pc = GetPC(ucontext);
  248. DumpStackFrameInfo("PC: ", pc);
  249. #endif
  250. #ifdef HAVE_STACKTRACE
  251. // Get the stack traces.
  252. void* stack[32];
  253. // +1 to exclude this function.
  254. const int depth = GetStackTrace(stack, ARRAYSIZE(stack), 1);
  255. # ifdef HAVE_SIGACTION
  256. DumpSignalInfo(signal_number, signal_info);
  257. # elif !defined(OS_WINDOWS)
  258. (void)signal_info;
  259. # endif
  260. // Dump the stack traces.
  261. for (int i = 0; i < depth; ++i) {
  262. DumpStackFrameInfo(" ", stack[i]);
  263. }
  264. #elif !defined(OS_WINDOWS)
  265. (void)signal_info;
  266. #endif
  267. // *** TRANSITION ***
  268. //
  269. // BEFORE this point, all code must be async-termination-safe!
  270. // (See WARNING above.)
  271. //
  272. // AFTER this point, we do unsafe things, like using LOG()!
  273. // The process could be terminated or hung at any time. We try to
  274. // do more useful things first and riskier things later.
  275. // Flush the logs before we do anything in case 'anything'
  276. // causes problems.
  277. FlushLogFilesUnsafe(GLOG_INFO);
  278. // Kill ourself by the default signal handler.
  279. InvokeDefaultSignalHandler(signal_number);
  280. }
  281. // Dumps signal and stack frame information, and invokes the default
  282. // signal handler once our job is done.
  283. #if defined(OS_WINDOWS)
  284. void FailureSignalHandler(int signal_number)
  285. #else
  286. void FailureSignalHandler(int signal_number, siginfo_t* signal_info,
  287. void* ucontext)
  288. #endif
  289. {
  290. std::call_once(signaled, &HandleSignal, signal_number
  291. #if !defined(OS_WINDOWS)
  292. ,
  293. signal_info, ucontext
  294. #endif
  295. );
  296. }
  297. } // namespace
  298. bool IsFailureSignalHandlerInstalled() {
  299. #ifdef HAVE_SIGACTION
  300. // TODO(andschwa): Return kFailureSignalHandlerInstalled?
  301. struct sigaction sig_action;
  302. memset(&sig_action, 0, sizeof(sig_action));
  303. sigemptyset(&sig_action.sa_mask);
  304. sigaction(SIGABRT, nullptr, &sig_action);
  305. if (sig_action.sa_sigaction == &FailureSignalHandler) {
  306. return true;
  307. }
  308. #elif defined(OS_WINDOWS)
  309. return kFailureSignalHandlerInstalled;
  310. #endif // HAVE_SIGACTION
  311. return false;
  312. }
  313. void InstallFailureSignalHandler() {
  314. #ifdef HAVE_SIGACTION
  315. // Build the sigaction struct.
  316. struct sigaction sig_action;
  317. memset(&sig_action, 0, sizeof(sig_action));
  318. sigemptyset(&sig_action.sa_mask);
  319. sig_action.sa_flags |= SA_SIGINFO;
  320. sig_action.sa_sigaction = &FailureSignalHandler;
  321. for (auto kFailureSignal : kFailureSignals) {
  322. CHECK_ERR(sigaction(kFailureSignal.number, &sig_action, nullptr));
  323. }
  324. kFailureSignalHandlerInstalled = true;
  325. #elif defined(OS_WINDOWS)
  326. for (size_t i = 0; i < ARRAYSIZE(kFailureSignals); ++i) {
  327. CHECK_NE(signal(kFailureSignals[i].number, &FailureSignalHandler), SIG_ERR);
  328. }
  329. kFailureSignalHandlerInstalled = true;
  330. #endif // HAVE_SIGACTION
  331. }
  332. void InstallFailureWriter(void (*writer)(const char* data, size_t size)) {
  333. #if defined(HAVE_SIGACTION) || defined(GLOG_OS_WINDOWS)
  334. g_failure_writer = writer;
  335. #endif // HAVE_SIGACTION
  336. }