SDL_assert.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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, etc. */
  34. #include <stdio.h>
  35. #include <stdlib.h>
  36. #endif
  37. #if defined(__EMSCRIPTEN__)
  38. #include <emscripten.h>
  39. #endif
  40. /* The size of the stack buffer to use for rendering assert messages. */
  41. #define SDL_MAX_ASSERT_MESSAGE_STACK 256
  42. static SDL_assert_state SDLCALL
  43. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
  44. /*
  45. * We keep all triggered assertions in a singly-linked list so we can
  46. * generate a report later.
  47. */
  48. static SDL_assert_data *triggered_assertions = NULL;
  49. #ifndef SDL_THREADS_DISABLED
  50. static SDL_mutex *assertion_mutex = NULL;
  51. #endif
  52. static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
  53. static void *assertion_userdata = NULL;
  54. #ifdef __GNUC__
  55. static void
  56. debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
  57. #endif
  58. static void
  59. debug_print(const char *fmt, ...)
  60. {
  61. va_list ap;
  62. va_start(ap, fmt);
  63. SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
  64. va_end(ap);
  65. }
  66. static void SDL_AddAssertionToReport(SDL_assert_data *data)
  67. {
  68. /* (data) is always a static struct defined with the assert macros, so
  69. we don't have to worry about copying or allocating them. */
  70. data->trigger_count++;
  71. if (data->trigger_count == 1) { /* not yet added? */
  72. data->next = triggered_assertions;
  73. triggered_assertions = data;
  74. }
  75. }
  76. static void SDL_GenerateAssertionReport(void)
  77. {
  78. const SDL_assert_data *item = triggered_assertions;
  79. /* only do this if the app hasn't assigned an assertion handler. */
  80. if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
  81. debug_print("\n\nSDL assertion report.\n");
  82. debug_print("All SDL assertions between last init/quit:\n\n");
  83. while (item != NULL) {
  84. debug_print(
  85. "'%s'\n"
  86. " * %s (%s:%d)\n"
  87. " * triggered %u time%s.\n"
  88. " * always ignore: %s.\n",
  89. item->condition, item->function, item->filename,
  90. item->linenum, item->trigger_count,
  91. (item->trigger_count == 1) ? "" : "s",
  92. item->always_ignore ? "yes" : "no");
  93. item = item->next;
  94. }
  95. debug_print("\n");
  96. SDL_ResetAssertionReport();
  97. }
  98. }
  99. /* This is not declared in any header, although it is shared between some
  100. parts of SDL, because we don't want anything calling it without an
  101. extremely good reason. */
  102. #if defined(__WATCOMC__)
  103. extern void SDL_ExitProcess(int exitcode);
  104. #pragma aux SDL_ExitProcess aborts;
  105. #endif
  106. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  107. #if defined(__WATCOMC__)
  108. static void SDL_AbortAssertion (void);
  109. #pragma aux SDL_AbortAssertion aborts;
  110. #endif
  111. static SDL_NORETURN void SDL_AbortAssertion(void)
  112. {
  113. SDL_Quit();
  114. SDL_ExitProcess(42);
  115. }
  116. static SDL_assert_state SDLCALL
  117. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
  118. {
  119. #ifdef __WIN32__
  120. #define ENDLINE "\r\n"
  121. #else
  122. #define ENDLINE "\n"
  123. #endif
  124. const char *envr;
  125. SDL_assert_state state = SDL_ASSERTION_ABORT;
  126. SDL_Window *window;
  127. SDL_MessageBoxData messagebox;
  128. SDL_MessageBoxButtonData buttons[] = {
  129. { 0, SDL_ASSERTION_RETRY, "Retry" },
  130. { 0, SDL_ASSERTION_BREAK, "Break" },
  131. { 0, SDL_ASSERTION_ABORT, "Abort" },
  132. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
  133. SDL_ASSERTION_IGNORE, "Ignore" },
  134. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
  135. SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" }
  136. };
  137. int selected;
  138. char stack_buf[SDL_MAX_ASSERT_MESSAGE_STACK];
  139. char *message = stack_buf;
  140. size_t buf_len = sizeof(stack_buf);
  141. size_t len;
  142. (void) userdata; /* unused in default handler. */
  143. do {
  144. /* Assume the output will fit... */
  145. len = SDL_snprintf(message, buf_len,
  146. "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE " '%s'",
  147. data->function, data->filename, data->linenum,
  148. data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
  149. data->condition);
  150. /* .. and if it didn't, allocate as much room as we actually need. */
  151. if (len >= buf_len && message == stack_buf) {
  152. buf_len = len + 1;
  153. message = (char *)SDL_malloc(buf_len);
  154. if (!message) {
  155. /* Uh oh, we're in real trouble now... */
  156. return SDL_ASSERTION_ABORT;
  157. }
  158. len = 0;
  159. }
  160. } while (len == 0);
  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. if (message != stack_buf) {
  166. SDL_free(message);
  167. }
  168. if (SDL_strcmp(envr, "abort") == 0) {
  169. return SDL_ASSERTION_ABORT;
  170. } else if (SDL_strcmp(envr, "break") == 0) {
  171. return SDL_ASSERTION_BREAK;
  172. } else if (SDL_strcmp(envr, "retry") == 0) {
  173. return SDL_ASSERTION_RETRY;
  174. } else if (SDL_strcmp(envr, "ignore") == 0) {
  175. return SDL_ASSERTION_IGNORE;
  176. } else if (SDL_strcmp(envr, "always_ignore") == 0) {
  177. return SDL_ASSERTION_ALWAYS_IGNORE;
  178. } else {
  179. return SDL_ASSERTION_ABORT; /* oh well. */
  180. }
  181. }
  182. /* Leave fullscreen mode, if possible (scary!) */
  183. window = SDL_GetFocusWindow();
  184. if (window) {
  185. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  186. SDL_MinimizeWindow(window);
  187. } else {
  188. /* !!! FIXME: ungrab the input if we're not fullscreen? */
  189. /* No need to mess with the window */
  190. window = NULL;
  191. }
  192. }
  193. /* Show a messagebox if we can, otherwise fall back to stdio */
  194. SDL_zero(messagebox);
  195. messagebox.flags = SDL_MESSAGEBOX_WARNING;
  196. messagebox.window = window;
  197. messagebox.title = "Assertion Failed";
  198. messagebox.message = message;
  199. messagebox.numbuttons = SDL_arraysize(buttons);
  200. messagebox.buttons = buttons;
  201. if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
  202. if (selected == -1) {
  203. state = SDL_ASSERTION_IGNORE;
  204. } else {
  205. state = (SDL_assert_state)selected;
  206. }
  207. }
  208. else
  209. {
  210. #if defined(__EMSCRIPTEN__)
  211. /* This is nasty, but we can't block on a custom UI. */
  212. for ( ; ; ) {
  213. SDL_bool okay = SDL_TRUE;
  214. char *buf = (char *) EM_ASM_INT({
  215. var str =
  216. UTF8ToString($0) + '\n\n' +
  217. 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
  218. var reply = window.prompt(str, "i");
  219. if (reply === null) {
  220. reply = "i";
  221. }
  222. return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
  223. }, message);
  224. if (SDL_strcmp(buf, "a") == 0) {
  225. state = SDL_ASSERTION_ABORT;
  226. /* (currently) no break functionality on Emscripten
  227. } else if (SDL_strcmp(buf, "b") == 0) {
  228. state = SDL_ASSERTION_BREAK; */
  229. } else if (SDL_strcmp(buf, "r") == 0) {
  230. state = SDL_ASSERTION_RETRY;
  231. } else if (SDL_strcmp(buf, "i") == 0) {
  232. state = SDL_ASSERTION_IGNORE;
  233. } else if (SDL_strcmp(buf, "A") == 0) {
  234. state = SDL_ASSERTION_ALWAYS_IGNORE;
  235. } else {
  236. okay = SDL_FALSE;
  237. }
  238. free(buf);
  239. if (okay) {
  240. break;
  241. }
  242. }
  243. #elif defined(HAVE_STDIO_H)
  244. /* this is a little hacky. */
  245. for ( ; ; ) {
  246. char buf[32];
  247. fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
  248. fflush(stderr);
  249. if (fgets(buf, sizeof (buf), stdin) == NULL) {
  250. break;
  251. }
  252. if (SDL_strncmp(buf, "a", 1) == 0) {
  253. state = SDL_ASSERTION_ABORT;
  254. break;
  255. } else if (SDL_strncmp(buf, "b", 1) == 0) {
  256. state = SDL_ASSERTION_BREAK;
  257. break;
  258. } else if (SDL_strncmp(buf, "r", 1) == 0) {
  259. state = SDL_ASSERTION_RETRY;
  260. break;
  261. } else if (SDL_strncmp(buf, "i", 1) == 0) {
  262. state = SDL_ASSERTION_IGNORE;
  263. break;
  264. } else if (SDL_strncmp(buf, "A", 1) == 0) {
  265. state = SDL_ASSERTION_ALWAYS_IGNORE;
  266. break;
  267. }
  268. }
  269. #endif /* HAVE_STDIO_H */
  270. }
  271. /* Re-enter fullscreen mode */
  272. if (window) {
  273. SDL_RestoreWindow(window);
  274. }
  275. if (message != stack_buf) {
  276. SDL_free(message);
  277. }
  278. return state;
  279. }
  280. SDL_assert_state
  281. SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
  282. int line)
  283. {
  284. SDL_assert_state state = SDL_ASSERTION_IGNORE;
  285. static int assertion_running = 0;
  286. #ifndef SDL_THREADS_DISABLED
  287. static SDL_SpinLock spinlock = 0;
  288. SDL_AtomicLock(&spinlock);
  289. if (assertion_mutex == NULL) { /* never called SDL_Init()? */
  290. assertion_mutex = SDL_CreateMutex();
  291. if (assertion_mutex == NULL) {
  292. SDL_AtomicUnlock(&spinlock);
  293. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  294. }
  295. }
  296. SDL_AtomicUnlock(&spinlock);
  297. if (SDL_LockMutex(assertion_mutex) < 0) {
  298. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  299. }
  300. #endif
  301. /* doing this because Visual C is upset over assigning in the macro. */
  302. if (data->trigger_count == 0) {
  303. data->function = func;
  304. data->filename = file;
  305. data->linenum = line;
  306. }
  307. SDL_AddAssertionToReport(data);
  308. assertion_running++;
  309. if (assertion_running > 1) { /* assert during assert! Abort. */
  310. if (assertion_running == 2) {
  311. SDL_AbortAssertion();
  312. } else if (assertion_running == 3) { /* Abort asserted! */
  313. SDL_ExitProcess(42);
  314. } else {
  315. while (1) { /* do nothing but spin; what else can you do?! */ }
  316. }
  317. }
  318. if (!data->always_ignore) {
  319. state = assertion_handler(data, assertion_userdata);
  320. }
  321. switch (state)
  322. {
  323. case SDL_ASSERTION_ALWAYS_IGNORE:
  324. state = SDL_ASSERTION_IGNORE;
  325. data->always_ignore = 1;
  326. break;
  327. case SDL_ASSERTION_IGNORE:
  328. case SDL_ASSERTION_RETRY:
  329. case SDL_ASSERTION_BREAK:
  330. break; /* macro handles these. */
  331. case SDL_ASSERTION_ABORT:
  332. SDL_AbortAssertion();
  333. /*break; ...shouldn't return, but oh well. */
  334. }
  335. assertion_running--;
  336. #ifndef SDL_THREADS_DISABLED
  337. SDL_UnlockMutex(assertion_mutex);
  338. #endif
  339. return state;
  340. }
  341. void SDL_AssertionsQuit(void)
  342. {
  343. SDL_GenerateAssertionReport();
  344. #ifndef SDL_THREADS_DISABLED
  345. if (assertion_mutex != NULL) {
  346. SDL_DestroyMutex(assertion_mutex);
  347. assertion_mutex = NULL;
  348. }
  349. #endif
  350. }
  351. void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
  352. {
  353. if (handler != NULL) {
  354. assertion_handler = handler;
  355. assertion_userdata = userdata;
  356. } else {
  357. assertion_handler = SDL_PromptAssertion;
  358. assertion_userdata = NULL;
  359. }
  360. }
  361. const SDL_assert_data *SDL_GetAssertionReport(void)
  362. {
  363. return triggered_assertions;
  364. }
  365. void SDL_ResetAssertionReport(void)
  366. {
  367. SDL_assert_data *next = NULL;
  368. SDL_assert_data *item;
  369. for (item = triggered_assertions; item != NULL; item = next) {
  370. next = (SDL_assert_data *) item->next;
  371. item->always_ignore = SDL_FALSE;
  372. item->trigger_count = 0;
  373. item->next = NULL;
  374. }
  375. triggered_assertions = NULL;
  376. }
  377. SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
  378. {
  379. return SDL_PromptAssertion;
  380. }
  381. SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
  382. {
  383. if (userdata != NULL) {
  384. *userdata = assertion_userdata;
  385. }
  386. return assertion_handler;
  387. }
  388. /* vi: set ts=4 sw=4 expandtab: */