SDL_assert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2018 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 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. #elif defined(__EMSCRIPTEN__)
  111. emscripten_cancel_main_loop(); /* this should "kill" the app. */
  112. emscripten_force_exit(exitcode); /* this should "kill" the app. */
  113. exit(exitcode);
  114. #else
  115. _exit(exitcode);
  116. #endif
  117. }
  118. #if defined(__WATCOMC__)
  119. #pragma aux SDL_AbortAssertion aborts;
  120. #endif
  121. static void SDL_AbortAssertion(void)
  122. {
  123. SDL_Quit();
  124. SDL_ExitProcess(42);
  125. }
  126. static SDL_assert_state SDLCALL
  127. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
  128. {
  129. #ifdef __WIN32__
  130. #define ENDLINE "\r\n"
  131. #else
  132. #define ENDLINE "\n"
  133. #endif
  134. const char *envr;
  135. SDL_assert_state state = SDL_ASSERTION_ABORT;
  136. SDL_Window *window;
  137. SDL_MessageBoxData messagebox;
  138. SDL_MessageBoxButtonData buttons[] = {
  139. { 0, SDL_ASSERTION_RETRY, "Retry" },
  140. { 0, SDL_ASSERTION_BREAK, "Break" },
  141. { 0, SDL_ASSERTION_ABORT, "Abort" },
  142. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
  143. SDL_ASSERTION_IGNORE, "Ignore" },
  144. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
  145. SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" }
  146. };
  147. char *message;
  148. int selected;
  149. (void) userdata; /* unused in default handler. */
  150. message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
  151. if (!message) {
  152. /* Uh oh, we're in real trouble now... */
  153. return SDL_ASSERTION_ABORT;
  154. }
  155. SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
  156. "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
  157. " '%s'",
  158. data->function, data->filename, data->linenum,
  159. data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
  160. data->condition);
  161. debug_print("\n\n%s\n\n", message);
  162. /* let env. variable override, so unit tests won't block in a GUI. */
  163. envr = SDL_getenv("SDL_ASSERT");
  164. if (envr != NULL) {
  165. SDL_stack_free(message);
  166. if (SDL_strcmp(envr, "abort") == 0) {
  167. return SDL_ASSERTION_ABORT;
  168. } else if (SDL_strcmp(envr, "break") == 0) {
  169. return SDL_ASSERTION_BREAK;
  170. } else if (SDL_strcmp(envr, "retry") == 0) {
  171. return SDL_ASSERTION_RETRY;
  172. } else if (SDL_strcmp(envr, "ignore") == 0) {
  173. return SDL_ASSERTION_IGNORE;
  174. } else if (SDL_strcmp(envr, "always_ignore") == 0) {
  175. return SDL_ASSERTION_ALWAYS_IGNORE;
  176. } else {
  177. return SDL_ASSERTION_ABORT; /* oh well. */
  178. }
  179. }
  180. /* Leave fullscreen mode, if possible (scary!) */
  181. window = SDL_GetFocusWindow();
  182. if (window) {
  183. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  184. SDL_MinimizeWindow(window);
  185. } else {
  186. /* !!! FIXME: ungrab the input if we're not fullscreen? */
  187. /* No need to mess with the window */
  188. window = NULL;
  189. }
  190. }
  191. /* Show a messagebox if we can, otherwise fall back to stdio */
  192. SDL_zero(messagebox);
  193. messagebox.flags = SDL_MESSAGEBOX_WARNING;
  194. messagebox.window = window;
  195. messagebox.title = "Assertion Failed";
  196. messagebox.message = message;
  197. messagebox.numbuttons = SDL_arraysize(buttons);
  198. messagebox.buttons = buttons;
  199. if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
  200. if (selected == -1) {
  201. state = SDL_ASSERTION_IGNORE;
  202. } else {
  203. state = (SDL_assert_state)selected;
  204. }
  205. }
  206. else
  207. {
  208. #if defined(__EMSCRIPTEN__)
  209. /* This is nasty, but we can't block on a custom UI. */
  210. for ( ; ; ) {
  211. SDL_bool okay = SDL_TRUE;
  212. char *buf = (char *) EM_ASM_INT({
  213. var str =
  214. Pointer_stringify($0) + '\n\n' +
  215. 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
  216. var reply = window.prompt(str, "i");
  217. if (reply === null) {
  218. reply = "i";
  219. }
  220. return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
  221. }, message);
  222. if (SDL_strcmp(buf, "a") == 0) {
  223. state = SDL_ASSERTION_ABORT;
  224. /* (currently) no break functionality on Emscripten
  225. } else if (SDL_strcmp(buf, "b") == 0) {
  226. state = SDL_ASSERTION_BREAK; */
  227. } else if (SDL_strcmp(buf, "r") == 0) {
  228. state = SDL_ASSERTION_RETRY;
  229. } else if (SDL_strcmp(buf, "i") == 0) {
  230. state = SDL_ASSERTION_IGNORE;
  231. } else if (SDL_strcmp(buf, "A") == 0) {
  232. state = SDL_ASSERTION_ALWAYS_IGNORE;
  233. } else {
  234. okay = SDL_FALSE;
  235. }
  236. free(buf);
  237. if (okay) {
  238. break;
  239. }
  240. }
  241. #elif defined(HAVE_STDIO_H)
  242. /* this is a little hacky. */
  243. for ( ; ; ) {
  244. char buf[32];
  245. fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
  246. fflush(stderr);
  247. if (fgets(buf, sizeof (buf), stdin) == NULL) {
  248. break;
  249. }
  250. if (SDL_strncmp(buf, "a", 1) == 0) {
  251. state = SDL_ASSERTION_ABORT;
  252. break;
  253. } else if (SDL_strncmp(buf, "b", 1) == 0) {
  254. state = SDL_ASSERTION_BREAK;
  255. break;
  256. } else if (SDL_strncmp(buf, "r", 1) == 0) {
  257. state = SDL_ASSERTION_RETRY;
  258. break;
  259. } else if (SDL_strncmp(buf, "i", 1) == 0) {
  260. state = SDL_ASSERTION_IGNORE;
  261. break;
  262. } else if (SDL_strncmp(buf, "A", 1) == 0) {
  263. state = SDL_ASSERTION_ALWAYS_IGNORE;
  264. break;
  265. }
  266. }
  267. #endif /* HAVE_STDIO_H */
  268. }
  269. /* Re-enter fullscreen mode */
  270. if (window) {
  271. SDL_RestoreWindow(window);
  272. }
  273. SDL_stack_free(message);
  274. return state;
  275. }
  276. SDL_assert_state
  277. SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
  278. int line)
  279. {
  280. SDL_assert_state state = SDL_ASSERTION_IGNORE;
  281. static int assertion_running = 0;
  282. #ifndef SDL_THREADS_DISABLED
  283. static SDL_SpinLock spinlock = 0;
  284. SDL_AtomicLock(&spinlock);
  285. if (assertion_mutex == NULL) { /* never called SDL_Init()? */
  286. assertion_mutex = SDL_CreateMutex();
  287. if (assertion_mutex == NULL) {
  288. SDL_AtomicUnlock(&spinlock);
  289. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  290. }
  291. }
  292. SDL_AtomicUnlock(&spinlock);
  293. if (SDL_LockMutex(assertion_mutex) < 0) {
  294. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  295. }
  296. #endif
  297. /* doing this because Visual C is upset over assigning in the macro. */
  298. if (data->trigger_count == 0) {
  299. data->function = func;
  300. data->filename = file;
  301. data->linenum = line;
  302. }
  303. SDL_AddAssertionToReport(data);
  304. assertion_running++;
  305. if (assertion_running > 1) { /* assert during assert! Abort. */
  306. if (assertion_running == 2) {
  307. SDL_AbortAssertion();
  308. } else if (assertion_running == 3) { /* Abort asserted! */
  309. SDL_ExitProcess(42);
  310. } else {
  311. while (1) { /* do nothing but spin; what else can you do?! */ }
  312. }
  313. }
  314. if (!data->always_ignore) {
  315. state = assertion_handler(data, assertion_userdata);
  316. }
  317. switch (state)
  318. {
  319. case SDL_ASSERTION_ABORT:
  320. SDL_AbortAssertion();
  321. return SDL_ASSERTION_IGNORE; /* shouldn't return, but oh well. */
  322. case SDL_ASSERTION_ALWAYS_IGNORE:
  323. state = SDL_ASSERTION_IGNORE;
  324. data->always_ignore = 1;
  325. break;
  326. case SDL_ASSERTION_IGNORE:
  327. case SDL_ASSERTION_RETRY:
  328. case SDL_ASSERTION_BREAK:
  329. break; /* macro handles these. */
  330. }
  331. assertion_running--;
  332. #ifndef SDL_THREADS_DISABLED
  333. SDL_UnlockMutex(assertion_mutex);
  334. #endif
  335. return state;
  336. }
  337. void SDL_AssertionsQuit(void)
  338. {
  339. SDL_GenerateAssertionReport();
  340. #ifndef SDL_THREADS_DISABLED
  341. if (assertion_mutex != NULL) {
  342. SDL_DestroyMutex(assertion_mutex);
  343. assertion_mutex = NULL;
  344. }
  345. #endif
  346. }
  347. void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
  348. {
  349. if (handler != NULL) {
  350. assertion_handler = handler;
  351. assertion_userdata = userdata;
  352. } else {
  353. assertion_handler = SDL_PromptAssertion;
  354. assertion_userdata = NULL;
  355. }
  356. }
  357. const SDL_assert_data *SDL_GetAssertionReport(void)
  358. {
  359. return triggered_assertions;
  360. }
  361. void SDL_ResetAssertionReport(void)
  362. {
  363. SDL_assert_data *next = NULL;
  364. SDL_assert_data *item;
  365. for (item = triggered_assertions; item != NULL; item = next) {
  366. next = (SDL_assert_data *) item->next;
  367. item->always_ignore = SDL_FALSE;
  368. item->trigger_count = 0;
  369. item->next = NULL;
  370. }
  371. triggered_assertions = NULL;
  372. }
  373. SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
  374. {
  375. return SDL_PromptAssertion;
  376. }
  377. SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
  378. {
  379. if (userdata != NULL) {
  380. *userdata = assertion_userdata;
  381. }
  382. return assertion_handler;
  383. }
  384. /* vi: set ts=4 sw=4 expandtab: */