debug.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #pragma once
  17. #include <iostream>
  18. #include <exception>
  19. #include <cassert>
  20. #include "gettime.h"
  21. #include "log.h"
  22. #ifdef _WIN32
  23. #ifndef _WIN32_WINNT
  24. #define _WIN32_WINNT 0x0501
  25. #endif
  26. #include <windows.h>
  27. #ifdef _MSC_VER
  28. #include <eh.h>
  29. #endif
  30. #define NORETURN __declspec(noreturn)
  31. #define FUNCTION_NAME __FUNCTION__
  32. #else
  33. #define NORETURN __attribute__ ((__noreturn__))
  34. #define FUNCTION_NAME __PRETTY_FUNCTION__
  35. #endif
  36. // Whether to catch all std::exceptions.
  37. // When "catching", the program will abort with an error message.
  38. // In debug mode, leave these for the debugger and don't catch them.
  39. #ifdef NDEBUG
  40. #define CATCH_UNHANDLED_EXCEPTIONS 1
  41. #else
  42. #define CATCH_UNHANDLED_EXCEPTIONS 0
  43. #endif
  44. /* Abort program execution immediately
  45. */
  46. NORETURN extern void fatal_error_fn(
  47. const char *msg, const char *file,
  48. unsigned int line, const char *function);
  49. #define FATAL_ERROR(msg) \
  50. fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME)
  51. #define FATAL_ERROR_IF(expr, msg) \
  52. ((expr) \
  53. ? fatal_error_fn((msg), __FILE__, __LINE__, FUNCTION_NAME) \
  54. : (void)(0))
  55. /*
  56. sanity_check()
  57. Equivalent to assert() but persists in Release builds (i.e. when NDEBUG is
  58. defined)
  59. */
  60. NORETURN extern void sanity_check_fn(
  61. const char *assertion, const char *file,
  62. unsigned int line, const char *function);
  63. #define SANITY_CHECK(expr) \
  64. ((expr) \
  65. ? (void)(0) \
  66. : sanity_check_fn(#expr, __FILE__, __LINE__, FUNCTION_NAME))
  67. #define sanity_check(expr) SANITY_CHECK(expr)
  68. void debug_set_exception_handler();
  69. /*
  70. These should be put into every thread
  71. */
  72. #if CATCH_UNHANDLED_EXCEPTIONS == 1
  73. #define BEGIN_DEBUG_EXCEPTION_HANDLER try {
  74. #define END_DEBUG_EXCEPTION_HANDLER \
  75. } catch (std::exception &e) { \
  76. errorstream << "An unhandled exception occurred: " \
  77. << e.what() << std::endl; \
  78. FATAL_ERROR(e.what()); \
  79. }
  80. #else
  81. // Dummy ones
  82. #define BEGIN_DEBUG_EXCEPTION_HANDLER
  83. #define END_DEBUG_EXCEPTION_HANDLER
  84. #endif