SDL_assert.c 13 KB

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