SDL_assert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2019 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "./SDL_internal.h"
  19. #if defined(__WIN32__)
  20. #include "core/windows/SDL_windows.h"
  21. #endif
  22. #include "SDL.h"
  23. #include "SDL_atomic.h"
  24. #include "SDL_messagebox.h"
  25. #include "SDL_video.h"
  26. #include "SDL_assert.h"
  27. #include "SDL_assert_c.h"
  28. #include "video/SDL_sysvideo.h"
  29. #ifdef __WIN32__
  30. #ifndef WS_OVERLAPPEDWINDOW
  31. #define WS_OVERLAPPEDWINDOW 0
  32. #endif
  33. #else /* fprintf, _exit(), etc. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #if ! defined(__WINRT__)
  37. #include <unistd.h>
  38. #endif
  39. #endif
  40. #if defined(__EMSCRIPTEN__)
  41. #include <emscripten.h>
  42. #endif
  43. static SDL_assert_state SDLCALL
  44. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
  45. /*
  46. * We keep all triggered assertions in a singly-linked list so we can
  47. * generate a report later.
  48. */
  49. static SDL_assert_data *triggered_assertions = NULL;
  50. #ifndef SDL_THREADS_DISABLED
  51. static SDL_mutex *assertion_mutex = NULL;
  52. #endif
  53. static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
  54. static void *assertion_userdata = NULL;
  55. #ifdef __GNUC__
  56. static void
  57. debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
  58. #endif
  59. static void
  60. debug_print(const char *fmt, ...)
  61. {
  62. va_list ap;
  63. va_start(ap, fmt);
  64. SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
  65. va_end(ap);
  66. }
  67. static void SDL_AddAssertionToReport(SDL_assert_data *data)
  68. {
  69. /* (data) is always a static struct defined with the assert macros, so
  70. we don't have to worry about copying or allocating them. */
  71. data->trigger_count++;
  72. if (data->trigger_count == 1) { /* not yet added? */
  73. data->next = triggered_assertions;
  74. triggered_assertions = data;
  75. }
  76. }
  77. static void SDL_GenerateAssertionReport(void)
  78. {
  79. const SDL_assert_data *item = triggered_assertions;
  80. /* only do this if the app hasn't assigned an assertion handler. */
  81. if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
  82. debug_print("\n\nSDL assertion report.\n");
  83. debug_print("All SDL assertions between last init/quit:\n\n");
  84. while (item != NULL) {
  85. debug_print(
  86. "'%s'\n"
  87. " * %s (%s:%d)\n"
  88. " * triggered %u time%s.\n"
  89. " * always ignore: %s.\n",
  90. item->condition, item->function, item->filename,
  91. item->linenum, item->trigger_count,
  92. (item->trigger_count == 1) ? "" : "s",
  93. item->always_ignore ? "yes" : "no");
  94. item = item->next;
  95. }
  96. debug_print("\n");
  97. SDL_ResetAssertionReport();
  98. }
  99. }
  100. #if defined(__WATCOMC__)
  101. #pragma aux SDL_ExitProcess aborts;
  102. #endif
  103. static SDL_NORETURN void SDL_ExitProcess(int exitcode)
  104. {
  105. #ifdef __WIN32__
  106. /* "if you do not know the state of all threads in your process, it is
  107. better to call TerminateProcess than ExitProcess"
  108. https://msdn.microsoft.com/en-us/library/windows/desktop/ms682658(v=vs.85).aspx */
  109. TerminateProcess(GetCurrentProcess(), exitcode);
  110. /* MingW doesn't have TerminateProcess marked as noreturn, so add an
  111. ExitProcess here that will never be reached but make MingW happy. */
  112. ExitProcess(exitcode);
  113. #elif defined(__EMSCRIPTEN__)
  114. emscripten_cancel_main_loop(); /* this should "kill" the app. */
  115. emscripten_force_exit(exitcode); /* this should "kill" the app. */
  116. exit(exitcode);
  117. #elif defined(__HAIKU__) /* Haiku has _Exit, but it's not marked noreturn. */
  118. _exit(exitcode);
  119. #elif defined(HAVE__EXIT) /* Upper case _Exit() */
  120. _Exit(exitcode);
  121. #else
  122. _exit(exitcode);
  123. #endif
  124. }
  125. #if defined(__WATCOMC__)
  126. #pragma aux SDL_AbortAssertion aborts;
  127. #endif
  128. static SDL_NORETURN void SDL_AbortAssertion(void)
  129. {
  130. SDL_Quit();
  131. SDL_ExitProcess(42);
  132. }
  133. static SDL_assert_state SDLCALL
  134. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
  135. {
  136. #ifdef __WIN32__
  137. #define ENDLINE "\r\n"
  138. #else
  139. #define ENDLINE "\n"
  140. #endif
  141. const char *envr;
  142. SDL_assert_state state = SDL_ASSERTION_ABORT;
  143. SDL_Window *window;
  144. SDL_MessageBoxData messagebox;
  145. SDL_MessageBoxButtonData buttons[] = {
  146. { 0, SDL_ASSERTION_RETRY, "Retry" },
  147. { 0, SDL_ASSERTION_BREAK, "Break" },
  148. { 0, SDL_ASSERTION_ABORT, "Abort" },
  149. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
  150. SDL_ASSERTION_IGNORE, "Ignore" },
  151. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
  152. SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" }
  153. };
  154. char *message;
  155. int selected;
  156. (void) userdata; /* unused in default handler. */
  157. /* !!! FIXME: why is this using SDL_stack_alloc and not just "char message[SDL_MAX_LOG_MESSAGE];" ? */
  158. message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
  159. if (!message) {
  160. /* Uh oh, we're in real trouble now... */
  161. return SDL_ASSERTION_ABORT;
  162. }
  163. SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
  164. "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
  165. " '%s'",
  166. data->function, data->filename, data->linenum,
  167. data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
  168. data->condition);
  169. debug_print("\n\n%s\n\n", message);
  170. /* let env. variable override, so unit tests won't block in a GUI. */
  171. envr = SDL_getenv("SDL_ASSERT");
  172. if (envr != NULL) {
  173. SDL_stack_free(message);
  174. if (SDL_strcmp(envr, "abort") == 0) {
  175. return SDL_ASSERTION_ABORT;
  176. } else if (SDL_strcmp(envr, "break") == 0) {
  177. return SDL_ASSERTION_BREAK;
  178. } else if (SDL_strcmp(envr, "retry") == 0) {
  179. return SDL_ASSERTION_RETRY;
  180. } else if (SDL_strcmp(envr, "ignore") == 0) {
  181. return SDL_ASSERTION_IGNORE;
  182. } else if (SDL_strcmp(envr, "always_ignore") == 0) {
  183. return SDL_ASSERTION_ALWAYS_IGNORE;
  184. } else {
  185. return SDL_ASSERTION_ABORT; /* oh well. */
  186. }
  187. }
  188. /* Leave fullscreen mode, if possible (scary!) */
  189. window = SDL_GetFocusWindow();
  190. if (window) {
  191. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  192. SDL_MinimizeWindow(window);
  193. } else {
  194. /* !!! FIXME: ungrab the input if we're not fullscreen? */
  195. /* No need to mess with the window */
  196. window = NULL;
  197. }
  198. }
  199. /* Show a messagebox if we can, otherwise fall back to stdio */
  200. SDL_zero(messagebox);
  201. messagebox.flags = SDL_MESSAGEBOX_WARNING;
  202. messagebox.window = window;
  203. messagebox.title = "Assertion Failed";
  204. messagebox.message = message;
  205. messagebox.numbuttons = SDL_arraysize(buttons);
  206. messagebox.buttons = buttons;
  207. if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
  208. if (selected == -1) {
  209. state = SDL_ASSERTION_IGNORE;
  210. } else {
  211. state = (SDL_assert_state)selected;
  212. }
  213. }
  214. else
  215. {
  216. #if defined(__EMSCRIPTEN__)
  217. /* This is nasty, but we can't block on a custom UI. */
  218. for ( ; ; ) {
  219. SDL_bool okay = SDL_TRUE;
  220. char *buf = (char *) EM_ASM_INT({
  221. var str =
  222. UTF8ToString($0) + '\n\n' +
  223. 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
  224. var reply = window.prompt(str, "i");
  225. if (reply === null) {
  226. reply = "i";
  227. }
  228. return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
  229. }, message);
  230. if (SDL_strcmp(buf, "a") == 0) {
  231. state = SDL_ASSERTION_ABORT;
  232. /* (currently) no break functionality on Emscripten
  233. } else if (SDL_strcmp(buf, "b") == 0) {
  234. state = SDL_ASSERTION_BREAK; */
  235. } else if (SDL_strcmp(buf, "r") == 0) {
  236. state = SDL_ASSERTION_RETRY;
  237. } else if (SDL_strcmp(buf, "i") == 0) {
  238. state = SDL_ASSERTION_IGNORE;
  239. } else if (SDL_strcmp(buf, "A") == 0) {
  240. state = SDL_ASSERTION_ALWAYS_IGNORE;
  241. } else {
  242. okay = SDL_FALSE;
  243. }
  244. free(buf);
  245. if (okay) {
  246. break;
  247. }
  248. }
  249. #elif defined(HAVE_STDIO_H)
  250. /* this is a little hacky. */
  251. for ( ; ; ) {
  252. char buf[32];
  253. fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
  254. fflush(stderr);
  255. if (fgets(buf, sizeof (buf), stdin) == NULL) {
  256. break;
  257. }
  258. if (SDL_strncmp(buf, "a", 1) == 0) {
  259. state = SDL_ASSERTION_ABORT;
  260. break;
  261. } else if (SDL_strncmp(buf, "b", 1) == 0) {
  262. state = SDL_ASSERTION_BREAK;
  263. break;
  264. } else if (SDL_strncmp(buf, "r", 1) == 0) {
  265. state = SDL_ASSERTION_RETRY;
  266. break;
  267. } else if (SDL_strncmp(buf, "i", 1) == 0) {
  268. state = SDL_ASSERTION_IGNORE;
  269. break;
  270. } else if (SDL_strncmp(buf, "A", 1) == 0) {
  271. state = SDL_ASSERTION_ALWAYS_IGNORE;
  272. break;
  273. }
  274. }
  275. #endif /* HAVE_STDIO_H */
  276. }
  277. /* Re-enter fullscreen mode */
  278. if (window) {
  279. SDL_RestoreWindow(window);
  280. }
  281. SDL_stack_free(message);
  282. return state;
  283. }
  284. SDL_assert_state
  285. SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
  286. int line)
  287. {
  288. SDL_assert_state state = SDL_ASSERTION_IGNORE;
  289. static int assertion_running = 0;
  290. #ifndef SDL_THREADS_DISABLED
  291. static SDL_SpinLock spinlock = 0;
  292. SDL_AtomicLock(&spinlock);
  293. if (assertion_mutex == NULL) { /* never called SDL_Init()? */
  294. assertion_mutex = SDL_CreateMutex();
  295. if (assertion_mutex == NULL) {
  296. SDL_AtomicUnlock(&spinlock);
  297. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  298. }
  299. }
  300. SDL_AtomicUnlock(&spinlock);
  301. if (SDL_LockMutex(assertion_mutex) < 0) {
  302. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  303. }
  304. #endif
  305. /* doing this because Visual C is upset over assigning in the macro. */
  306. if (data->trigger_count == 0) {
  307. data->function = func;
  308. data->filename = file;
  309. data->linenum = line;
  310. }
  311. SDL_AddAssertionToReport(data);
  312. assertion_running++;
  313. if (assertion_running > 1) { /* assert during assert! Abort. */
  314. if (assertion_running == 2) {
  315. SDL_AbortAssertion();
  316. } else if (assertion_running == 3) { /* Abort asserted! */
  317. SDL_ExitProcess(42);
  318. } else {
  319. while (1) { /* do nothing but spin; what else can you do?! */ }
  320. }
  321. }
  322. if (!data->always_ignore) {
  323. state = assertion_handler(data, assertion_userdata);
  324. }
  325. switch (state)
  326. {
  327. case SDL_ASSERTION_ALWAYS_IGNORE:
  328. state = SDL_ASSERTION_IGNORE;
  329. data->always_ignore = 1;
  330. break;
  331. case SDL_ASSERTION_IGNORE:
  332. case SDL_ASSERTION_RETRY:
  333. case SDL_ASSERTION_BREAK:
  334. break; /* macro handles these. */
  335. case SDL_ASSERTION_ABORT:
  336. SDL_AbortAssertion();
  337. /*break; ...shouldn't return, but oh well. */
  338. }
  339. assertion_running--;
  340. #ifndef SDL_THREADS_DISABLED
  341. SDL_UnlockMutex(assertion_mutex);
  342. #endif
  343. return state;
  344. }
  345. void SDL_AssertionsQuit(void)
  346. {
  347. SDL_GenerateAssertionReport();
  348. #ifndef SDL_THREADS_DISABLED
  349. if (assertion_mutex != NULL) {
  350. SDL_DestroyMutex(assertion_mutex);
  351. assertion_mutex = NULL;
  352. }
  353. #endif
  354. }
  355. void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
  356. {
  357. if (handler != NULL) {
  358. assertion_handler = handler;
  359. assertion_userdata = userdata;
  360. } else {
  361. assertion_handler = SDL_PromptAssertion;
  362. assertion_userdata = NULL;
  363. }
  364. }
  365. const SDL_assert_data *SDL_GetAssertionReport(void)
  366. {
  367. return triggered_assertions;
  368. }
  369. void SDL_ResetAssertionReport(void)
  370. {
  371. SDL_assert_data *next = NULL;
  372. SDL_assert_data *item;
  373. for (item = triggered_assertions; item != NULL; item = next) {
  374. next = (SDL_assert_data *) item->next;
  375. item->always_ignore = SDL_FALSE;
  376. item->trigger_count = 0;
  377. item->next = NULL;
  378. }
  379. triggered_assertions = NULL;
  380. }
  381. SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
  382. {
  383. return SDL_PromptAssertion;
  384. }
  385. SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
  386. {
  387. if (userdata != NULL) {
  388. *userdata = assertion_userdata;
  389. }
  390. return assertion_handler;
  391. }
  392. /* vi: set ts=4 sw=4 expandtab: */