SDL_assert.c 13 KB

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