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

unixcrashhandler.cpp 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Copyright (c) 2010-2016 OTClient <https://github.com/edubart/otclient>
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. #if !defined(WIN32) && defined(CRASH_HANDLER)
  23. #include "crashhandler.h"
  24. #include <framework/global.h>
  25. #include <framework/core/application.h>
  26. #ifndef __USE_GNU
  27. #define __USE_GNU
  28. #endif
  29. #include <execinfo.h>
  30. #include <ucontext.h>
  31. #include <signal.h>
  32. #define MAX_BACKTRACE_DEPTH 128
  33. #define DEMANGLE_BACKTRACE_SYMBOLS
  34. void crashHandler(int signum, siginfo_t* info, void* secret)
  35. {
  36. g_logger.error("Application crashed");
  37. std::stringstream ss;
  38. ss << stdext::format("app name: %s\n", g_app.getName());
  39. ss << stdext::format("app version: %s\n", g_app.getVersion());
  40. ss << stdext::format("build compiler: %s\n", BUILD_COMPILER);
  41. ss << stdext::format("build date: %s\n", __DATE__);
  42. ss << stdext::format("build type: %s\n", BUILD_TYPE);
  43. ss << stdext::format("build revision: %s (%s)\n", BUILD_REVISION, BUILD_COMMIT);
  44. ss << stdext::format("crash date: %s\n", stdext::date_time_string());
  45. ss.flags(std::ios::hex | std::ios::showbase);
  46. ucontext_t context = *(ucontext_t*)secret;
  47. #if __WORDSIZE == 64
  48. ss << " at rip = " << context.uc_mcontext.gregs[REG_RIP] << std::endl;
  49. ss << " rax = " << context.uc_mcontext.gregs[REG_RAX] << std::endl;
  50. ss << " rbx = " << context.uc_mcontext.gregs[REG_RBX] << std::endl;
  51. ss << " rcx = " << context.uc_mcontext.gregs[REG_RCX] << std::endl;
  52. ss << " rdx = " << context.uc_mcontext.gregs[REG_RDX] << std::endl;
  53. ss << " rsi = " << context.uc_mcontext.gregs[REG_RSI] << std::endl;
  54. ss << " rdi = " << context.uc_mcontext.gregs[REG_RDI] << std::endl;
  55. ss << " rbp = " << context.uc_mcontext.gregs[REG_RBP] << std::endl;
  56. ss << " rsp = " << context.uc_mcontext.gregs[REG_RSP] << std::endl;
  57. ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
  58. ss << std::endl;
  59. #elif defined(REG_EIP)
  60. ss << " at eip = " << context.uc_mcontext.gregs[REG_EIP] << std::endl;
  61. ss << " eax = " << context.uc_mcontext.gregs[REG_EAX] << std::endl;
  62. ss << " ebx = " << context.uc_mcontext.gregs[REG_EBX] << std::endl;
  63. ss << " ecx = " << context.uc_mcontext.gregs[REG_ECX] << std::endl;
  64. ss << " edx = " << context.uc_mcontext.gregs[REG_EDX] << std::endl;
  65. ss << " esi = " << context.uc_mcontext.gregs[REG_ESI] << std::endl;
  66. ss << " edi = " << context.uc_mcontext.gregs[REG_EDI] << std::endl;
  67. ss << " ebp = " << context.uc_mcontext.gregs[REG_EBP] << std::endl;
  68. ss << " esp = " << context.uc_mcontext.gregs[REG_ESP] << std::endl;
  69. ss << " efl = " << context.uc_mcontext.gregs[REG_EFL] << std::endl;
  70. ss << std::endl;
  71. #endif
  72. ss.flags(std::ios::dec);
  73. ss << " backtrace:" << std::endl;
  74. void* buffer[MAX_BACKTRACE_DEPTH];
  75. int numLevels = backtrace(buffer, MAX_BACKTRACE_DEPTH);
  76. char **tracebackBuffer = backtrace_symbols(buffer, numLevels);
  77. if(tracebackBuffer) {
  78. for(int i = 2; i < numLevels; i++) {
  79. std::string line = tracebackBuffer[i];
  80. if(line.find("__libc_start_main") != std::string::npos)
  81. break;
  82. #ifdef DEMANGLE_BACKTRACE_SYMBOLS
  83. std::size_t demanglePos = line.find("(_Z");
  84. if(demanglePos != std::string::npos) {
  85. demanglePos++;
  86. int len = std::min(line.find_first_of("+", demanglePos), line.find_first_of(")", demanglePos)) - demanglePos;
  87. std::string funcName = line.substr(demanglePos, len);
  88. line.replace(demanglePos, len, stdext::demangle_name(funcName.c_str()));
  89. }
  90. #endif
  91. ss << " " << i-1 << ": " << line << std::endl;
  92. }
  93. free(tracebackBuffer);
  94. }
  95. g_logger.info(ss.str());
  96. std::string fileName = "crash_report.log";
  97. std::ofstream fout(fileName.c_str(), std::ios::out | std::ios::app);
  98. if(fout.is_open() && fout.good()) {
  99. fout << "== application crashed\n";
  100. fout << ss.str();
  101. fout << "\n";
  102. fout.close();
  103. g_logger.info(stdext::format("Crash report saved to file %s", fileName));
  104. } else
  105. g_logger.error("Failed to save crash report!");
  106. signal(SIGILL, SIG_DFL);
  107. signal(SIGSEGV, SIG_DFL);
  108. signal(SIGFPE, SIG_DFL);
  109. signal(SIGABRT, SIG_DFL);
  110. }
  111. void installCrashHandler()
  112. {
  113. struct sigaction sa;
  114. sa.sa_sigaction = &crashHandler;
  115. sigemptyset (&sa.sa_mask);
  116. sa.sa_flags = SA_RESTART | SA_SIGINFO;
  117. sigaction(SIGILL, &sa, NULL); // illegal instruction
  118. sigaction(SIGSEGV, &sa, NULL); // segmentation fault
  119. sigaction(SIGFPE, &sa, NULL); // floating-point exception
  120. sigaction(SIGABRT, &sa, NULL); // process aborted (asserts)
  121. }
  122. #endif