1
0

assert.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. #include "precomp_wasabi_bfc.h"
  2. #ifdef WIN32
  3. #include <windows.h>
  4. #endif
  5. #ifdef __APPLE__
  6. #include <unistd.h>
  7. #endif
  8. #include "bfc_assert.h"
  9. #ifdef ASSERTS_ENABLED
  10. static int in_assert = 0;
  11. // we try to use wsprintf because it can make for a smaller .exe
  12. #ifdef WIN32
  13. #define ASSERT_SPRINTF wsprintfA
  14. #elif defined(LINUX)
  15. #define ASSERT_SPRINTF sprintf
  16. #elif defined(__APPLE__)
  17. #define ASSERT_SPRINTF sprintf
  18. #else
  19. #error port me!
  20. #endif
  21. #ifdef WIN32
  22. static int isDebuggerPresent() {
  23. HINSTANCE kernel32 = LoadLibrary(L"kernel32.dll");
  24. if (kernel32 == NULL) return 0;
  25. BOOL (WINAPI *IsDebuggerPresent)() = NULL;
  26. IsDebuggerPresent = (BOOL (WINAPI *)())GetProcAddress(kernel32, "IsDebuggerPresent");
  27. if (IsDebuggerPresent == NULL) return 0;
  28. int ret = (*IsDebuggerPresent)();
  29. FreeLibrary(kernel32);
  30. return ret;
  31. }
  32. #endif
  33. static void assert_kill()
  34. {
  35. #ifdef _WIN32
  36. #ifndef _WIN64
  37. __asm { int 3 };
  38. #else
  39. ExitProcess(0);
  40. #endif
  41. #endif
  42. }
  43. void _assert_handler(const char *reason, const char *file, int line) {
  44. char msg[4096] = {0};
  45. ASSERT_SPRINTF(msg, "Expression: %s\nFile: %s\nLine: %d\n", reason, file, line);
  46. #ifdef WIN32
  47. OutputDebugStringA(msg);
  48. if (in_assert)
  49. {
  50. assert_kill();
  51. }
  52. in_assert = 1;
  53. ATOM a = AddAtom(L"BYPASS_DEACTIVATE_MGR"); // so we don't need to call api->appdeactivation_setbypass
  54. if (isDebuggerPresent() || MessageBoxA(NULL, msg, "Assertion failed", MB_OKCANCEL|MB_TASKMODAL) == IDCANCEL) {
  55. assert_kill();
  56. } else
  57. ExitProcess(0);
  58. DeleteAtom(a);
  59. #elif defined(LINUX)
  60. kill(getpid(), SIGSEGV );
  61. #elif defined(__APPLE__)
  62. kill(getpid(), SIGSEGV );
  63. #else
  64. #error port me!
  65. #endif
  66. in_assert = 0;
  67. }
  68. void _assert_handler_str(const char *string, const char *reason, const char *file, int line)
  69. {
  70. char msg[4096] = {0};
  71. ASSERT_SPRINTF(msg, "\"%s\"\nExpression: %s\nFile: %s\nLine: %d\n", string, reason ? reason : "", file, line);
  72. #ifdef WIN32
  73. OutputDebugStringA(msg);
  74. if (in_assert) assert_kill();
  75. in_assert = 1;
  76. ATOM a = AddAtom(L"BYPASS_DEACTIVATE_MGR");
  77. if (isDebuggerPresent() || MessageBoxA(NULL, msg, "Assertion failed", MB_OKCANCEL|MB_TASKMODAL) == IDCANCEL) {
  78. assert_kill();
  79. } else
  80. ExitProcess(0);
  81. DeleteAtom(a);
  82. #elif defined(LINUX)
  83. kill(getpid(), SIGSEGV );
  84. #elif defined(__APPLE__)
  85. kill(getpid(), SIGSEGV );
  86. #else
  87. #error port me!
  88. #endif
  89. in_assert = 0;
  90. }
  91. #endif