SDL_assert.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  20. #include "core/windows/SDL_windows.h"
  21. #endif
  22. #include "SDL_assert_c.h"
  23. #include "video/SDL_sysvideo.h"
  24. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  25. #ifndef WS_OVERLAPPEDWINDOW
  26. #define WS_OVERLAPPEDWINDOW 0
  27. #endif
  28. #endif
  29. #ifdef SDL_PLATFORM_EMSCRIPTEN
  30. #include <emscripten.h>
  31. /* older Emscriptens don't have this, but we need to for wasm64 compatibility. */
  32. #ifndef MAIN_THREAD_EM_ASM_PTR
  33. #ifdef __wasm64__
  34. #error You need to upgrade your Emscripten compiler to support wasm64
  35. #else
  36. #define MAIN_THREAD_EM_ASM_PTR MAIN_THREAD_EM_ASM_INT
  37. #endif
  38. #endif
  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_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, void *userdata);
  43. /*
  44. * We keep all triggered assertions in a singly-linked list so we can
  45. * generate a report later.
  46. */
  47. static SDL_AssertData *triggered_assertions = NULL;
  48. #ifndef SDL_THREADS_DISABLED
  49. static SDL_Mutex *assertion_mutex = NULL;
  50. #endif
  51. static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
  52. static void *assertion_userdata = NULL;
  53. #ifdef __GNUC__
  54. static void debug_print(const char *fmt, ...) __attribute__((format(printf, 1, 2)));
  55. #endif
  56. static void debug_print(const char *fmt, ...)
  57. {
  58. va_list ap;
  59. va_start(ap, fmt);
  60. SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
  61. va_end(ap);
  62. }
  63. static void SDL_AddAssertionToReport(SDL_AssertData *data)
  64. {
  65. /* (data) is always a static struct defined with the assert macros, so
  66. we don't have to worry about copying or allocating them. */
  67. data->trigger_count++;
  68. if (data->trigger_count == 1) { /* not yet added? */
  69. data->next = triggered_assertions;
  70. triggered_assertions = data;
  71. }
  72. }
  73. #if defined(SDL_PLATFORM_WIN32) || defined(SDL_PLATFORM_GDK)
  74. #define ENDLINE "\r\n"
  75. #else
  76. #define ENDLINE "\n"
  77. #endif
  78. static int SDL_RenderAssertMessage(char *buf, size_t buf_len, const SDL_AssertData *data)
  79. {
  80. return SDL_snprintf(buf, buf_len,
  81. "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE " '%s'",
  82. data->function, data->filename, data->linenum,
  83. data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
  84. data->condition);
  85. }
  86. static void SDL_GenerateAssertionReport(void)
  87. {
  88. const SDL_AssertData *item = triggered_assertions;
  89. /* only do this if the app hasn't assigned an assertion handler. */
  90. if ((item) && (assertion_handler != SDL_PromptAssertion)) {
  91. debug_print("\n\nSDL assertion report.\n");
  92. debug_print("All SDL assertions between last init/quit:\n\n");
  93. while (item) {
  94. debug_print(
  95. "'%s'\n"
  96. " * %s (%s:%d)\n"
  97. " * triggered %u time%s.\n"
  98. " * always ignore: %s.\n",
  99. item->condition, item->function, item->filename,
  100. item->linenum, item->trigger_count,
  101. (item->trigger_count == 1) ? "" : "s",
  102. item->always_ignore ? "yes" : "no");
  103. item = item->next;
  104. }
  105. debug_print("\n");
  106. SDL_ResetAssertionReport();
  107. }
  108. }
  109. /* This is not declared in any header, although it is shared between some
  110. parts of SDL, because we don't want anything calling it without an
  111. extremely good reason. */
  112. #ifdef __WATCOMC__
  113. extern void SDL_ExitProcess(int exitcode);
  114. #pragma aux SDL_ExitProcess aborts;
  115. #endif
  116. extern SDL_NORETURN void SDL_ExitProcess(int exitcode);
  117. #ifdef __WATCOMC__
  118. static void SDL_AbortAssertion(void);
  119. #pragma aux SDL_AbortAssertion aborts;
  120. #endif
  121. static SDL_NORETURN void SDL_AbortAssertion(void)
  122. {
  123. SDL_Quit();
  124. SDL_ExitProcess(42);
  125. }
  126. static SDL_AssertState SDLCALL SDL_PromptAssertion(const SDL_AssertData *data, void *userdata)
  127. {
  128. const char *envr;
  129. SDL_AssertState state = SDL_ASSERTION_ABORT;
  130. SDL_Window *window;
  131. SDL_MessageBoxData messagebox;
  132. SDL_MessageBoxButtonData buttons[] = {
  133. { 0, SDL_ASSERTION_RETRY, "Retry" },
  134. { 0, SDL_ASSERTION_BREAK, "Break" },
  135. { 0, SDL_ASSERTION_ABORT, "Abort" },
  136. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
  137. SDL_ASSERTION_IGNORE, "Ignore" },
  138. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
  139. SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" }
  140. };
  141. int selected;
  142. char stack_buf[SDL_MAX_ASSERT_MESSAGE_STACK];
  143. char *message = stack_buf;
  144. size_t buf_len = sizeof(stack_buf);
  145. int len;
  146. (void)userdata; /* unused in default handler. */
  147. /* Assume the output will fit... */
  148. len = SDL_RenderAssertMessage(message, buf_len, data);
  149. /* .. and if it didn't, try to allocate as much room as we actually need. */
  150. if (len >= (int)buf_len) {
  151. if (SDL_size_add_overflow(len, 1, &buf_len) == 0) {
  152. message = (char *)SDL_malloc(buf_len);
  153. if (message) {
  154. len = SDL_RenderAssertMessage(message, buf_len, data);
  155. } else {
  156. message = stack_buf;
  157. }
  158. }
  159. }
  160. /* Something went very wrong */
  161. if (len < 0) {
  162. if (message != stack_buf) {
  163. SDL_free(message);
  164. }
  165. return SDL_ASSERTION_ABORT;
  166. }
  167. debug_print("\n\n%s\n\n", message);
  168. /* let env. variable override, so unit tests won't block in a GUI. */
  169. envr = SDL_getenv("SDL_ASSERT");
  170. if (envr) {
  171. if (message != stack_buf) {
  172. SDL_free(message);
  173. }
  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_GetToplevelForKeyboardFocus();
  190. if (window) {
  191. if (window->fullscreen_exclusive) {
  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_AssertState)selected;
  212. }
  213. } else {
  214. #ifdef SDL_PLATFORM_EMSCRIPTEN
  215. /* This is nasty, but we can't block on a custom UI. */
  216. for (;;) {
  217. SDL_bool okay = SDL_TRUE;
  218. /* *INDENT-OFF* */ /* clang-format off */
  219. char *buf = (char *) MAIN_THREAD_EM_ASM_PTR({
  220. var str =
  221. UTF8ToString($0) + '\n\n' +
  222. 'Abort/Retry/Ignore/AlwaysIgnore? [ariA] :';
  223. var reply = window.prompt(str, "i");
  224. if (reply === null) {
  225. reply = "i";
  226. }
  227. return allocate(intArrayFromString(reply), 'i8', ALLOC_NORMAL);
  228. }, message);
  229. /* *INDENT-ON* */ /* clang-format on */
  230. if (SDL_strcmp(buf, "a") == 0) {
  231. state = SDL_ASSERTION_ABORT;
  232. #if 0 /* (currently) no break functionality on Emscripten */
  233. } else if (SDL_strcmp(buf, "b") == 0) {
  234. state = SDL_ASSERTION_BREAK;
  235. #endif
  236. } else if (SDL_strcmp(buf, "r") == 0) {
  237. state = SDL_ASSERTION_RETRY;
  238. } else if (SDL_strcmp(buf, "i") == 0) {
  239. state = SDL_ASSERTION_IGNORE;
  240. } else if (SDL_strcmp(buf, "A") == 0) {
  241. state = SDL_ASSERTION_ALWAYS_IGNORE;
  242. } else {
  243. okay = SDL_FALSE;
  244. }
  245. free(buf); /* This should NOT be SDL_free() */
  246. if (okay) {
  247. break;
  248. }
  249. }
  250. #elif defined(HAVE_STDIO_H)
  251. /* this is a little hacky. */
  252. for (;;) {
  253. char buf[32];
  254. (void)fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
  255. (void)fflush(stderr);
  256. if (fgets(buf, sizeof(buf), stdin) == NULL) {
  257. break;
  258. }
  259. if (SDL_strncmp(buf, "a", 1) == 0) {
  260. state = SDL_ASSERTION_ABORT;
  261. break;
  262. } else if (SDL_strncmp(buf, "b", 1) == 0) {
  263. state = SDL_ASSERTION_BREAK;
  264. break;
  265. } else if (SDL_strncmp(buf, "r", 1) == 0) {
  266. state = SDL_ASSERTION_RETRY;
  267. break;
  268. } else if (SDL_strncmp(buf, "i", 1) == 0) {
  269. state = SDL_ASSERTION_IGNORE;
  270. break;
  271. } else if (SDL_strncmp(buf, "A", 1) == 0) {
  272. state = SDL_ASSERTION_ALWAYS_IGNORE;
  273. break;
  274. }
  275. }
  276. #endif /* HAVE_STDIO_H */
  277. }
  278. /* Re-enter fullscreen mode */
  279. if (window) {
  280. SDL_RestoreWindow(window);
  281. }
  282. if (message != stack_buf) {
  283. SDL_free(message);
  284. }
  285. return state;
  286. }
  287. SDL_AssertState SDL_ReportAssertion(SDL_AssertData *data, const char *func, const char *file, int line)
  288. {
  289. SDL_AssertState state = SDL_ASSERTION_IGNORE;
  290. static int assertion_running = 0;
  291. #ifndef SDL_THREADS_DISABLED
  292. static SDL_SpinLock spinlock = 0;
  293. SDL_LockSpinlock(&spinlock);
  294. if (!assertion_mutex) { /* never called SDL_Init()? */
  295. assertion_mutex = SDL_CreateMutex();
  296. if (!assertion_mutex) {
  297. SDL_UnlockSpinlock(&spinlock);
  298. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  299. }
  300. }
  301. SDL_UnlockSpinlock(&spinlock);
  302. SDL_LockMutex(assertion_mutex);
  303. #endif /* !SDL_THREADS_DISABLED */
  304. /* doing this because Visual C is upset over assigning in the macro. */
  305. if (data->trigger_count == 0) {
  306. data->function = func;
  307. data->filename = file;
  308. data->linenum = line;
  309. }
  310. SDL_AddAssertionToReport(data);
  311. assertion_running++;
  312. if (assertion_running > 1) { /* assert during assert! Abort. */
  313. if (assertion_running == 2) {
  314. SDL_AbortAssertion();
  315. } else if (assertion_running == 3) { /* Abort asserted! */
  316. SDL_ExitProcess(42);
  317. } else {
  318. while (1) { /* do nothing but spin; what else can you do?! */
  319. }
  320. }
  321. }
  322. if (!data->always_ignore) {
  323. state = assertion_handler(data, assertion_userdata);
  324. }
  325. switch (state) {
  326. case SDL_ASSERTION_ALWAYS_IGNORE:
  327. state = SDL_ASSERTION_IGNORE;
  328. data->always_ignore = 1;
  329. break;
  330. case SDL_ASSERTION_IGNORE:
  331. case SDL_ASSERTION_RETRY:
  332. case SDL_ASSERTION_BREAK:
  333. break; /* macro handles these. */
  334. case SDL_ASSERTION_ABORT:
  335. SDL_AbortAssertion();
  336. /*break; ...shouldn't return, but oh well. */
  337. }
  338. assertion_running--;
  339. #ifndef SDL_THREADS_DISABLED
  340. SDL_UnlockMutex(assertion_mutex);
  341. #endif
  342. return state;
  343. }
  344. void SDL_AssertionsQuit(void)
  345. {
  346. #if SDL_ASSERT_LEVEL > 0
  347. SDL_GenerateAssertionReport();
  348. #ifndef SDL_THREADS_DISABLED
  349. if (assertion_mutex) {
  350. SDL_DestroyMutex(assertion_mutex);
  351. assertion_mutex = NULL;
  352. }
  353. #endif
  354. #endif /* SDL_ASSERT_LEVEL > 0 */
  355. }
  356. void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
  357. {
  358. if (handler != NULL) {
  359. assertion_handler = handler;
  360. assertion_userdata = userdata;
  361. } else {
  362. assertion_handler = SDL_PromptAssertion;
  363. assertion_userdata = NULL;
  364. }
  365. }
  366. const SDL_AssertData *SDL_GetAssertionReport(void)
  367. {
  368. return triggered_assertions;
  369. }
  370. void SDL_ResetAssertionReport(void)
  371. {
  372. SDL_AssertData *next = NULL;
  373. SDL_AssertData *item;
  374. for (item = triggered_assertions; item; item = next) {
  375. next = (SDL_AssertData *)item->next;
  376. item->always_ignore = SDL_FALSE;
  377. item->trigger_count = 0;
  378. item->next = NULL;
  379. }
  380. triggered_assertions = NULL;
  381. }
  382. SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
  383. {
  384. return SDL_PromptAssertion;
  385. }
  386. SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
  387. {
  388. if (userdata) {
  389. *userdata = assertion_userdata;
  390. }
  391. return assertion_handler;
  392. }