debug.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "version.h"
  30. #include "filesys.h"
  31. #endif
  32. #if USE_CURSES
  33. #include "terminal_chat_console.h"
  34. #endif
  35. /*
  36. Assert
  37. */
  38. void sanity_check_fn(const char *assertion, const char *file,
  39. unsigned int line, const char *function)
  40. {
  41. #if USE_CURSES
  42. g_term_console.stopAndWaitforThread();
  43. #endif
  44. errorstream << std::endl << "In thread " << std::hex
  45. << std::this_thread::get_id() << ":" << std::endl;
  46. errorstream << file << ":" << line << ": " << function
  47. << ": An engine assumption '" << assertion << "' failed." << std::endl;
  48. abort();
  49. }
  50. void fatal_error_fn(const char *msg, const char *file,
  51. unsigned int line, const char *function)
  52. {
  53. #if USE_CURSES
  54. g_term_console.stopAndWaitforThread();
  55. #endif
  56. errorstream << std::endl << "In thread " << std::hex
  57. << std::this_thread::get_id() << ":" << std::endl;
  58. errorstream << file << ":" << line << ": " << function
  59. << ": A fatal error occured: " << msg << std::endl;
  60. abort();
  61. }
  62. #ifdef _MSC_VER
  63. const char *Win32ExceptionCodeToString(DWORD exception_code)
  64. {
  65. switch (exception_code) {
  66. case EXCEPTION_ACCESS_VIOLATION:
  67. return "Access violation";
  68. case EXCEPTION_DATATYPE_MISALIGNMENT:
  69. return "Misaligned data access";
  70. case EXCEPTION_BREAKPOINT:
  71. return "Breakpoint reached";
  72. case EXCEPTION_SINGLE_STEP:
  73. return "Single debug step";
  74. case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
  75. return "Array access out of bounds";
  76. case EXCEPTION_FLT_DENORMAL_OPERAND:
  77. return "Denormal floating point operand";
  78. case EXCEPTION_FLT_DIVIDE_BY_ZERO:
  79. return "Floating point division by zero";
  80. case EXCEPTION_FLT_INEXACT_RESULT:
  81. return "Inaccurate floating point result";
  82. case EXCEPTION_FLT_INVALID_OPERATION:
  83. return "Invalid floating point operation";
  84. case EXCEPTION_FLT_OVERFLOW:
  85. return "Floating point exponent overflow";
  86. case EXCEPTION_FLT_STACK_CHECK:
  87. return "Floating point stack overflow or underflow";
  88. case EXCEPTION_FLT_UNDERFLOW:
  89. return "Floating point exponent underflow";
  90. case EXCEPTION_INT_DIVIDE_BY_ZERO:
  91. return "Integer division by zero";
  92. case EXCEPTION_INT_OVERFLOW:
  93. return "Integer overflow";
  94. case EXCEPTION_PRIV_INSTRUCTION:
  95. return "Privileged instruction executed";
  96. case EXCEPTION_IN_PAGE_ERROR:
  97. return "Could not access or load page";
  98. case EXCEPTION_ILLEGAL_INSTRUCTION:
  99. return "Illegal instruction encountered";
  100. case EXCEPTION_NONCONTINUABLE_EXCEPTION:
  101. return "Attempted to continue after fatal exception";
  102. case EXCEPTION_STACK_OVERFLOW:
  103. return "Stack overflow";
  104. case EXCEPTION_INVALID_DISPOSITION:
  105. return "Invalid disposition returned to the exception dispatcher";
  106. case EXCEPTION_GUARD_PAGE:
  107. return "Attempted guard page access";
  108. case EXCEPTION_INVALID_HANDLE:
  109. return "Invalid handle";
  110. }
  111. return "Unknown exception";
  112. }
  113. long WINAPI Win32ExceptionHandler(struct _EXCEPTION_POINTERS *pExceptInfo)
  114. {
  115. char buf[512];
  116. MINIDUMP_EXCEPTION_INFORMATION mdei;
  117. MINIDUMP_USER_STREAM_INFORMATION mdusi;
  118. MINIDUMP_USER_STREAM mdus;
  119. bool minidump_created = false;
  120. std::string dumpfile = porting::path_user + DIR_DELIM PROJECT_NAME ".dmp";
  121. std::string version_str(PROJECT_NAME " ");
  122. version_str += g_version_hash;
  123. HANDLE hFile = CreateFileA(dumpfile.c_str(), GENERIC_WRITE,
  124. FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  125. if (hFile == INVALID_HANDLE_VALUE)
  126. goto minidump_failed;
  127. if (SetEndOfFile(hFile) == FALSE)
  128. goto minidump_failed;
  129. mdei.ClientPointers = NULL;
  130. mdei.ExceptionPointers = pExceptInfo;
  131. mdei.ThreadId = GetCurrentThreadId();
  132. mdus.Type = CommentStreamA;
  133. mdus.BufferSize = version_str.size();
  134. mdus.Buffer = (PVOID)version_str.c_str();
  135. mdusi.UserStreamArray = &mdus;
  136. mdusi.UserStreamCount = 1;
  137. if (MiniDumpWriteDump(GetCurrentProcess(), GetCurrentProcessId(), hFile,
  138. MiniDumpNormal, &mdei, &mdusi, NULL) == FALSE)
  139. goto minidump_failed;
  140. minidump_created = true;
  141. minidump_failed:
  142. CloseHandle(hFile);
  143. DWORD excode = pExceptInfo->ExceptionRecord->ExceptionCode;
  144. _snprintf(buf, sizeof(buf),
  145. " >> === FATAL ERROR ===\n"
  146. " >> %s (Exception 0x%08X) at 0x%p\n",
  147. Win32ExceptionCodeToString(excode), excode,
  148. pExceptInfo->ExceptionRecord->ExceptionAddress);
  149. dstream << buf;
  150. if (minidump_created)
  151. dstream << " >> Saved dump to " << dumpfile << std::endl;
  152. else
  153. dstream << " >> Failed to save dump" << std::endl;
  154. return EXCEPTION_EXECUTE_HANDLER;
  155. }
  156. #endif
  157. void debug_set_exception_handler()
  158. {
  159. #ifdef _MSC_VER
  160. SetUnhandledExceptionFilter(Win32ExceptionHandler);
  161. #endif
  162. }