SDL_assert.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2014 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(__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. static SDL_assert_state
  41. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
  42. /*
  43. * We keep all triggered assertions in a singly-linked list so we can
  44. * generate a report later.
  45. */
  46. static SDL_assert_data *triggered_assertions = NULL;
  47. static SDL_mutex *assertion_mutex = NULL;
  48. static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
  49. static void *assertion_userdata = NULL;
  50. #ifdef __GNUC__
  51. static void
  52. debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
  53. #endif
  54. static void
  55. debug_print(const char *fmt, ...)
  56. {
  57. va_list ap;
  58. va_start(ap, fmt);
  59. SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
  60. va_end(ap);
  61. }
  62. static void SDL_AddAssertionToReport(SDL_assert_data *data)
  63. {
  64. /* (data) is always a static struct defined with the assert macros, so
  65. we don't have to worry about copying or allocating them. */
  66. data->trigger_count++;
  67. if (data->trigger_count == 1) { /* not yet added? */
  68. data->next = triggered_assertions;
  69. triggered_assertions = data;
  70. }
  71. }
  72. static void SDL_GenerateAssertionReport(void)
  73. {
  74. const SDL_assert_data *item = triggered_assertions;
  75. /* only do this if the app hasn't assigned an assertion handler. */
  76. if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
  77. debug_print("\n\nSDL assertion report.\n");
  78. debug_print("All SDL assertions between last init/quit:\n\n");
  79. while (item != NULL) {
  80. debug_print(
  81. "'%s'\n"
  82. " * %s (%s:%d)\n"
  83. " * triggered %u time%s.\n"
  84. " * always ignore: %s.\n",
  85. item->condition, item->function, item->filename,
  86. item->linenum, item->trigger_count,
  87. (item->trigger_count == 1) ? "" : "s",
  88. item->always_ignore ? "yes" : "no");
  89. item = item->next;
  90. }
  91. debug_print("\n");
  92. SDL_ResetAssertionReport();
  93. }
  94. }
  95. static void SDL_ExitProcess(int exitcode)
  96. {
  97. #ifdef __WIN32__
  98. ExitProcess(exitcode);
  99. #else
  100. _exit(exitcode);
  101. #endif
  102. }
  103. static void SDL_AbortAssertion(void)
  104. {
  105. SDL_Quit();
  106. SDL_ExitProcess(42);
  107. }
  108. static SDL_assert_state
  109. SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
  110. {
  111. #ifdef __WIN32__
  112. #define ENDLINE "\r\n"
  113. #else
  114. #define ENDLINE "\n"
  115. #endif
  116. const char *envr;
  117. SDL_assert_state state = SDL_ASSERTION_ABORT;
  118. SDL_Window *window;
  119. SDL_MessageBoxData messagebox;
  120. SDL_MessageBoxButtonData buttons[] = {
  121. { 0, SDL_ASSERTION_RETRY, "Retry" },
  122. { 0, SDL_ASSERTION_BREAK, "Break" },
  123. { 0, SDL_ASSERTION_ABORT, "Abort" },
  124. { SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
  125. SDL_ASSERTION_IGNORE, "Ignore" },
  126. { SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
  127. SDL_ASSERTION_ALWAYS_IGNORE, "Always Ignore" }
  128. };
  129. char *message;
  130. int selected;
  131. (void) userdata; /* unused in default handler. */
  132. message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
  133. if (!message) {
  134. /* Uh oh, we're in real trouble now... */
  135. return SDL_ASSERTION_ABORT;
  136. }
  137. SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
  138. "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
  139. " '%s'",
  140. data->function, data->filename, data->linenum,
  141. data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
  142. data->condition);
  143. debug_print("\n\n%s\n\n", message);
  144. /* let env. variable override, so unit tests won't block in a GUI. */
  145. envr = SDL_getenv("SDL_ASSERT");
  146. if (envr != NULL) {
  147. SDL_stack_free(message);
  148. if (SDL_strcmp(envr, "abort") == 0) {
  149. return SDL_ASSERTION_ABORT;
  150. } else if (SDL_strcmp(envr, "break") == 0) {
  151. return SDL_ASSERTION_BREAK;
  152. } else if (SDL_strcmp(envr, "retry") == 0) {
  153. return SDL_ASSERTION_RETRY;
  154. } else if (SDL_strcmp(envr, "ignore") == 0) {
  155. return SDL_ASSERTION_IGNORE;
  156. } else if (SDL_strcmp(envr, "always_ignore") == 0) {
  157. return SDL_ASSERTION_ALWAYS_IGNORE;
  158. } else {
  159. return SDL_ASSERTION_ABORT; /* oh well. */
  160. }
  161. }
  162. /* Leave fullscreen mode, if possible (scary!) */
  163. window = SDL_GetFocusWindow();
  164. if (window) {
  165. if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
  166. SDL_MinimizeWindow(window);
  167. } else {
  168. /* !!! FIXME: ungrab the input if we're not fullscreen? */
  169. /* No need to mess with the window */
  170. window = NULL;
  171. }
  172. }
  173. /* Show a messagebox if we can, otherwise fall back to stdio */
  174. SDL_zero(messagebox);
  175. messagebox.flags = SDL_MESSAGEBOX_WARNING;
  176. messagebox.window = window;
  177. messagebox.title = "Assertion Failed";
  178. messagebox.message = message;
  179. messagebox.numbuttons = SDL_arraysize(buttons);
  180. messagebox.buttons = buttons;
  181. if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
  182. if (selected == -1) {
  183. state = SDL_ASSERTION_IGNORE;
  184. } else {
  185. state = (SDL_assert_state)selected;
  186. }
  187. }
  188. #ifdef HAVE_STDIO_H
  189. else
  190. {
  191. /* this is a little hacky. */
  192. for ( ; ; ) {
  193. char buf[32];
  194. fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
  195. fflush(stderr);
  196. if (fgets(buf, sizeof (buf), stdin) == NULL) {
  197. break;
  198. }
  199. if (SDL_strcmp(buf, "a") == 0) {
  200. state = SDL_ASSERTION_ABORT;
  201. break;
  202. } else if (SDL_strcmp(buf, "b") == 0) {
  203. state = SDL_ASSERTION_BREAK;
  204. break;
  205. } else if (SDL_strcmp(buf, "r") == 0) {
  206. state = SDL_ASSERTION_RETRY;
  207. break;
  208. } else if (SDL_strcmp(buf, "i") == 0) {
  209. state = SDL_ASSERTION_IGNORE;
  210. break;
  211. } else if (SDL_strcmp(buf, "A") == 0) {
  212. state = SDL_ASSERTION_ALWAYS_IGNORE;
  213. break;
  214. }
  215. }
  216. }
  217. #endif /* HAVE_STDIO_H */
  218. /* Re-enter fullscreen mode */
  219. if (window) {
  220. SDL_RestoreWindow(window);
  221. }
  222. SDL_stack_free(message);
  223. return state;
  224. }
  225. SDL_assert_state
  226. SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
  227. int line)
  228. {
  229. static int assertion_running = 0;
  230. static SDL_SpinLock spinlock = 0;
  231. SDL_assert_state state = SDL_ASSERTION_IGNORE;
  232. SDL_AtomicLock(&spinlock);
  233. if (assertion_mutex == NULL) { /* never called SDL_Init()? */
  234. assertion_mutex = SDL_CreateMutex();
  235. if (assertion_mutex == NULL) {
  236. SDL_AtomicUnlock(&spinlock);
  237. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  238. }
  239. }
  240. SDL_AtomicUnlock(&spinlock);
  241. if (SDL_LockMutex(assertion_mutex) < 0) {
  242. return SDL_ASSERTION_IGNORE; /* oh well, I guess. */
  243. }
  244. /* doing this because Visual C is upset over assigning in the macro. */
  245. if (data->trigger_count == 0) {
  246. data->function = func;
  247. data->filename = file;
  248. data->linenum = line;
  249. }
  250. SDL_AddAssertionToReport(data);
  251. assertion_running++;
  252. if (assertion_running > 1) { /* assert during assert! Abort. */
  253. if (assertion_running == 2) {
  254. SDL_AbortAssertion();
  255. } else if (assertion_running == 3) { /* Abort asserted! */
  256. SDL_ExitProcess(42);
  257. } else {
  258. while (1) { /* do nothing but spin; what else can you do?! */ }
  259. }
  260. }
  261. if (!data->always_ignore) {
  262. state = assertion_handler(data, assertion_userdata);
  263. }
  264. switch (state)
  265. {
  266. case SDL_ASSERTION_ABORT:
  267. SDL_AbortAssertion();
  268. return SDL_ASSERTION_IGNORE; /* shouldn't return, but oh well. */
  269. case SDL_ASSERTION_ALWAYS_IGNORE:
  270. state = SDL_ASSERTION_IGNORE;
  271. data->always_ignore = 1;
  272. break;
  273. case SDL_ASSERTION_IGNORE:
  274. case SDL_ASSERTION_RETRY:
  275. case SDL_ASSERTION_BREAK:
  276. break; /* macro handles these. */
  277. }
  278. assertion_running--;
  279. SDL_UnlockMutex(assertion_mutex);
  280. return state;
  281. }
  282. void SDL_AssertionsQuit(void)
  283. {
  284. SDL_GenerateAssertionReport();
  285. if (assertion_mutex != NULL) {
  286. SDL_DestroyMutex(assertion_mutex);
  287. assertion_mutex = NULL;
  288. }
  289. }
  290. void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
  291. {
  292. if (handler != NULL) {
  293. assertion_handler = handler;
  294. assertion_userdata = userdata;
  295. } else {
  296. assertion_handler = SDL_PromptAssertion;
  297. assertion_userdata = NULL;
  298. }
  299. }
  300. const SDL_assert_data *SDL_GetAssertionReport(void)
  301. {
  302. return triggered_assertions;
  303. }
  304. void SDL_ResetAssertionReport(void)
  305. {
  306. SDL_assert_data *next = NULL;
  307. SDL_assert_data *item;
  308. for (item = triggered_assertions; item != NULL; item = next) {
  309. next = (SDL_assert_data *) item->next;
  310. item->always_ignore = SDL_FALSE;
  311. item->trigger_count = 0;
  312. item->next = NULL;
  313. }
  314. triggered_assertions = NULL;
  315. }
  316. SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
  317. {
  318. return SDL_PromptAssertion;
  319. }
  320. SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
  321. {
  322. if (userdata != NULL) {
  323. *userdata = assertion_userdata;
  324. }
  325. return assertion_handler;
  326. }
  327. /* vi: set ts=4 sw=4 expandtab: */