debug.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "porting.h"
  17. #include "debug.h"
  18. #include "exceptions.h"
  19. #include <cstdio>
  20. #include <cstdlib>
  21. #include <cstring>
  22. #include <map>
  23. #include <sstream>
  24. #include <thread>
  25. #include "threading/mutex_auto_lock.h"
  26. #include "config.h"
  27. #ifdef _MSC_VER
  28. #include <dbghelp.h>
  29. #include <windows.h>
  30. #include <eh.h>
  31. #include "version.h"
  32. #include "filesys.h"
  33. #endif
  34. #if USE_CURSES
  35. #include "terminal_chat_console.h"
  36. #endif
  37. /*
  38. Assert
  39. */
  40. void sanity_check_fn(const char *assertion, const char *file,
  41. unsigned int line, const char *function)
  42. {
  43. #if USE_CURSES
  44. g_term_console.stopAndWaitforThread();
  45. #endif
  46. errorstream << std::endl << "In thread " << std::hex
  47. << std::this_thread::get_id() << ":" << std::endl;
  48. errorstream << file << ":" << line << ": " << function
  49. << ": An engine assumption '" << assertion << "' failed." << std::endl;
  50. abort();
  51. }
  52. void fatal_error_fn(const char *msg, const char *file,
  53. unsigned int line, const char *function)
  54. {
  55. #if USE_CURSES
  56. g_term_console.stopAndWaitforThread();
  57. #endif
  58. errorstream << std::endl << "In thread " << std::hex
  59. << std::this_thread::get_id() << ":" << std::endl;
  60. errorstream << file << ":" << line << ": " << function
  61. << ": A fatal error occurred: " << msg << std::endl;
  62. abort();
  63. }
  64. std::string debug_describe_exc(const std::exception &e)
  65. {
  66. if (dynamic_cast<const std::bad_alloc*>(&e))
  67. return "C++ out of memory";
  68. return std::string("\"").append(e.what()).append("\"");
  69. }
  70. #ifdef _MSC_VER
  71. const char *Win32ExceptionCodeToString(DWORD exception_code)
  72. {
  73. switch (exception_code) {
  74. case EXCEPTION_ACCESS_VIOLATION:
  75. return "Access violation";
  76. case EXCEPTION_DATATYPE_MISALIGNMENT:
  77. return "Misaligned data access";
  78. case EXCEPTION_BREAKPOINT:
  79. return "Breakpoint reached";
  80. case EXCEPTION_SINGLE_STEP:
  81. return "Single debug step";
  82. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  83. return "Array access out of bounds";
  84. case EXCEPTION_FLT_DENORMAL_OPERAND:
  85. return "Denormal floating point operand";
  86. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  87. return "Floating point division by zero";
  88. case EXCEPTION_FLT_INEXACT_RESULT:
  89. return "Inaccurate floating point result";
  90. case EXCEPTION_FLT_INVALID_OPERATION:
  91. return "Invalid floating point operation";
  92. case EXCEPTION_FLT_OVERFLOW:
  93. return "Floating point exponent overflow";
  94. case EXCEPTION_FLT_STACK_CHECK:
  95. return "Floating point stack overflow or underflow";
  96. case EXCEPTION_FLT_UNDERFLOW:
  97. return "Floating point exponent underflow";
  98. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  99. return "Integer division by zero";
  100. case EXCEPTION_INT_OVERFLOW:
  101. return "Integer overflow";
  102. case EXCEPTION_PRIV_INSTRUCTION:
  103. return "Privileged instruction executed";
  104. case EXCEPTION_IN_PAGE_ERROR:
  105. return "Could not access or load page";
  106. case EXCEPTION_ILLEGAL_INSTRUCTION:
  107. return "Illegal instruction encountered";
  108. case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  109. return "Attempted to continue after fatal exception";
  110. case EXCEPTION_STACK_OVERFLOW:
  111. return "Stack overflow";
  112. case EXCEPTION_INVALID_DISPOSITION:
  113. return "Invalid disposition returned to the exception dispatcher";
  114. case EXCEPTION_GUARD_PAGE:
  115. return "Attempted guard page access";
  116. case EXCEPTION_INVALID_HANDLE:
  117. return "Invalid handle";
  118. }
  119. return "Unknown exception";
  120. }
  121. long WINAPI Win32ExceptionHandler(struct _EXCEPTION_POINTERS *pExceptInfo)
  122. {
  123. char buf[512];
  124. MINIDUMP_EXCEPTION_INFORMATION mdei;
  125. MINIDUMP_USER_STREAM_INFORMATION mdusi;
  126. MINIDUMP_USER_STREAM mdus;
  127. bool minidump_created = false;
  128. std::string dumpfile = porting::path_user + DIR_DELIM PROJECT_NAME ".dmp";
  129. std::string version_str(PROJECT_NAME " ");
  130. version_str += g_version_hash;
  131. HANDLE hFile = CreateFileA(dumpfile.c_str(), GENERIC_WRITE,
  132. FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  133. if (hFile == INVALID_HANDLE_VALUE)
  134. goto minidump_failed;
  135. if (SetEndOfFile(hFile) == FALSE)
  136. goto minidump_failed;
  137. mdei.ClientPointers = NULL;
  138. mdei.ExceptionPointers = pExceptInfo;
  139. mdei.ThreadId = GetCurrentThreadId();
  140. mdus.Type = CommentStreamA;
  141. mdus.BufferSize = version_str.size();
  142. mdus.Buffer = (PVOID)version_str.c_str();
  143. mdusi.UserStreamArray = &mdus;
  144. mdusi.UserStreamCount = 1;
  145. if (MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile,
  146. MiniDumpNormal, &mdei, &mdusi, NULL) == FALSE)
  147. goto minidump_failed;
  148. minidump_created = true;
  149. minidump_failed:
  150. CloseHandle(hFile);
  151. DWORD excode = pExceptInfo->ExceptionRecord->ExceptionCode;
  152. _snprintf(buf, sizeof(buf),
  153. " >> === FATAL ERROR ===\n"
  154. " >> %s (Exception 0x%08X) at 0x%p\n",
  155. Win32ExceptionCodeToString(excode), excode,
  156. pExceptInfo->ExceptionRecord->ExceptionAddress);
  157. dstream << buf;
  158. if (minidump_created)
  159. dstream << " >> Saved dump to " << dumpfile << std::endl;
  160. else
  161. dstream << " >> Failed to save dump" << std::endl;
  162. return EXCEPTION_EXECUTE_HANDLER;
  163. }
  164. #endif
  165. void debug_set_exception_handler()
  166. {
  167. #ifdef _MSC_VER
  168. SetUnhandledExceptionFilter(Win32ExceptionHandler);
  169. #endif
  170. }