SDL_assert.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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. /**
  19. * \file SDL_assert.h
  20. *
  21. * Header file for assertion SDL API functions
  22. */
  23. #ifndef SDL_assert_h_
  24. #define SDL_assert_h_
  25. #include <SDL3/SDL_stdinc.h>
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. #ifndef SDL_ASSERT_LEVEL
  32. #ifdef SDL_DEFAULT_ASSERT_LEVEL
  33. #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
  34. #elif defined(_DEBUG) || defined(DEBUG) || \
  35. (defined(__GNUC__) && !defined(__OPTIMIZE__))
  36. #define SDL_ASSERT_LEVEL 2
  37. #else
  38. #define SDL_ASSERT_LEVEL 1
  39. #endif
  40. #endif /* SDL_ASSERT_LEVEL */
  41. /*
  42. These are macros and not first class functions so that the debugger breaks
  43. on the assertion line and not in some random guts of SDL, and so each
  44. assert can have unique static variables associated with it.
  45. */
  46. #ifdef _MSC_VER
  47. /* Don't include intrin.h here because it contains C++ code */
  48. extern void __cdecl __debugbreak(void);
  49. #define SDL_TriggerBreakpoint() __debugbreak()
  50. #elif defined(ANDROID)
  51. #include <assert.h>
  52. #define SDL_TriggerBreakpoint() assert(0)
  53. #elif SDL_HAS_BUILTIN(__builtin_debugtrap)
  54. #define SDL_TriggerBreakpoint() __builtin_debugtrap()
  55. #elif (defined(__GNUC__) || defined(__clang__)) && (defined(__i386__) || defined(__x86_64__))
  56. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
  57. #elif (defined(__GNUC__) || defined(__clang__)) && defined(__riscv)
  58. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "ebreak\n\t" )
  59. #elif ( defined(SDL_PLATFORM_APPLE) && (defined(__arm64__) || defined(__aarch64__)) ) /* this might work on other ARM targets, but this is a known quantity... */
  60. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "brk #22\n\t" )
  61. #elif defined(SDL_PLATFORM_APPLE) && defined(__arm__)
  62. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "bkpt #22\n\t" )
  63. #elif defined(__386__) && defined(__WATCOMC__)
  64. #define SDL_TriggerBreakpoint() { _asm { int 0x03 } }
  65. #elif defined(HAVE_SIGNAL_H) && !defined(__WATCOMC__)
  66. #include <signal.h>
  67. #define SDL_TriggerBreakpoint() raise(SIGTRAP)
  68. #else
  69. /* How do we trigger breakpoints on this platform? */
  70. #define SDL_TriggerBreakpoint()
  71. #endif
  72. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 supports __func__ as a standard. */
  73. # define SDL_FUNCTION __func__
  74. #elif ((defined(__GNUC__) && (__GNUC__ >= 2)) || defined(_MSC_VER) || defined (__WATCOMC__))
  75. # define SDL_FUNCTION __FUNCTION__
  76. #else
  77. # define SDL_FUNCTION "???"
  78. #endif
  79. #define SDL_FILE __FILE__
  80. #define SDL_LINE __LINE__
  81. /*
  82. sizeof (x) makes the compiler still parse the expression even without
  83. assertions enabled, so the code is always checked at compile time, but
  84. doesn't actually generate code for it, so there are no side effects or
  85. expensive checks at run time, just the constant size of what x WOULD be,
  86. which presumably gets optimized out as unused.
  87. This also solves the problem of...
  88. int somevalue = blah();
  89. SDL_assert(somevalue == 1);
  90. ...which would cause compiles to complain that somevalue is unused if we
  91. disable assertions.
  92. */
  93. /* "while (0,0)" fools Microsoft's compiler's /W4 warning level into thinking
  94. this condition isn't constant. And looks like an owl's face! */
  95. #ifdef _MSC_VER /* stupid /W4 warnings. */
  96. #define SDL_NULL_WHILE_LOOP_CONDITION (0,0)
  97. #else
  98. #define SDL_NULL_WHILE_LOOP_CONDITION (0)
  99. #endif
  100. #define SDL_disabled_assert(condition) \
  101. do { (void) sizeof ((condition)); } while (SDL_NULL_WHILE_LOOP_CONDITION)
  102. /**
  103. * Possible outcomes from a triggered assertion.
  104. *
  105. * When an enabled assertion triggers, it may call the assertion handler
  106. * (possibly one provided by the app via SDL_SetAssertionHandler), which will
  107. * return one of these values, possibly after asking the user.
  108. *
  109. * Then SDL will respond based on this outcome (loop around to retry the
  110. * condition, try to break in a debugger, kill the program, or ignore the
  111. * problem).
  112. *
  113. * \since This enum is available since SDL 3.0.0.
  114. */
  115. typedef enum SDL_AssertState
  116. {
  117. SDL_ASSERTION_RETRY, /**< Retry the assert immediately. */
  118. SDL_ASSERTION_BREAK, /**< Make the debugger trigger a breakpoint. */
  119. SDL_ASSERTION_ABORT, /**< Terminate the program. */
  120. SDL_ASSERTION_IGNORE, /**< Ignore the assert. */
  121. SDL_ASSERTION_ALWAYS_IGNORE /**< Ignore the assert from now on. */
  122. } SDL_AssertState;
  123. /**
  124. * Information about an assertion failure.
  125. *
  126. * This structure is filled in with information about a triggered assertion,
  127. * used by the assertion handler, then added to the assertion report. This is
  128. * returned as a linked list from SDL_GetAssertionReport().
  129. *
  130. * \since This struct is available since SDL 3.0.0.
  131. */
  132. typedef struct SDL_AssertData
  133. {
  134. SDL_bool always_ignore;
  135. unsigned int trigger_count;
  136. const char *condition;
  137. const char *filename;
  138. int linenum;
  139. const char *function;
  140. const struct SDL_AssertData *next;
  141. } SDL_AssertData;
  142. /**
  143. * Never call this directly.
  144. *
  145. * Use the SDL_assert* macros.
  146. *
  147. * \param data assert data structure
  148. * \param func function name
  149. * \param file file name
  150. * \param line line number
  151. * \returns assert state
  152. *
  153. * \since This function is available since SDL 3.0.0.
  154. */
  155. extern DECLSPEC SDL_AssertState SDLCALL SDL_ReportAssertion(SDL_AssertData *data,
  156. const char *func,
  157. const char *file, int line)
  158. #ifdef __clang__
  159. #if __has_feature(attribute_analyzer_noreturn)
  160. __attribute__((analyzer_noreturn))
  161. #endif
  162. #endif
  163. ;
  164. /* Previous 'analyzer_noreturn' attribute tells Clang's static analysis that we're a custom assert function,
  165. and that the analyzer should assume the condition was always true past this
  166. SDL_assert test. */
  167. /* Define the trigger breakpoint call used in asserts */
  168. #ifndef SDL_AssertBreakpoint
  169. #if defined(ANDROID) && defined(assert)
  170. /* Define this as empty in case assert() is defined as SDL_assert */
  171. #define SDL_AssertBreakpoint()
  172. #else
  173. #define SDL_AssertBreakpoint() SDL_TriggerBreakpoint()
  174. #endif
  175. #endif /* !SDL_AssertBreakpoint */
  176. /* the do {} while(0) avoids dangling else problems:
  177. if (x) SDL_assert(y); else blah();
  178. ... without the do/while, the "else" could attach to this macro's "if".
  179. We try to handle just the minimum we need here in a macro...the loop,
  180. the static vars, and break points. The heavy lifting is handled in
  181. SDL_ReportAssertion(), in SDL_assert.c.
  182. */
  183. #define SDL_enabled_assert(condition) \
  184. do { \
  185. while ( !(condition) ) { \
  186. static struct SDL_AssertData sdl_assert_data = { 0, 0, #condition, 0, 0, 0, 0 }; \
  187. const SDL_AssertState sdl_assert_state = SDL_ReportAssertion(&sdl_assert_data, SDL_FUNCTION, SDL_FILE, SDL_LINE); \
  188. if (sdl_assert_state == SDL_ASSERTION_RETRY) { \
  189. continue; /* go again. */ \
  190. } else if (sdl_assert_state == SDL_ASSERTION_BREAK) { \
  191. SDL_AssertBreakpoint(); \
  192. } \
  193. break; /* not retrying. */ \
  194. } \
  195. } while (SDL_NULL_WHILE_LOOP_CONDITION)
  196. /* Enable various levels of assertions. */
  197. #ifdef SDL_WIKI_DOCUMENTATION_SECTION
  198. /**
  199. * An assertion test that is normally performed only in debug builds.
  200. *
  201. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 2, otherwise it is
  202. * disabled. This is meant to only do these tests in debug builds, so they can
  203. * tend to be more expensive, and they are meant to bring everything to a halt
  204. * when they fail, with the programmer there to assess the problem.
  205. *
  206. * In short: you can sprinkle these around liberally and assume they will
  207. * evaporate out of the build when building for end-users.
  208. *
  209. * When assertions are disabled, this wraps `condition` in a `sizeof`
  210. * operator, which means any function calls and side effects will not run, but
  211. * the compiler will not complain about any otherwise-unused variables that
  212. * are only referenced in the assertion.
  213. *
  214. * One can set the environment variable "SDL_ASSERT" to one of several strings
  215. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  216. * behavior, which may be desirable for automation purposes. If your platform
  217. * requires GUI interfaces to happen on the main thread but you're debugging
  218. * an assertion in a background thread, it might be desirable to set this to
  219. * "break" so that your debugger takes control as soon as assert is triggered,
  220. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  221. *
  222. * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
  223. * Please refer to your platform's documentation for how to set it!
  224. *
  225. * \param condition boolean value to test
  226. *
  227. * \since This macro is available since SDL 3.0.0.
  228. */
  229. #define SDL_assert(condition) if (assertion_enabled && (condition)) { trigger_assertion; }
  230. /**
  231. * An assertion test that is performed even in release builds.
  232. *
  233. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 1, otherwise it is
  234. * disabled. This is meant to be for tests that are cheap to make and
  235. * extremely unlikely to fail; generally it is frowned upon to have an
  236. * assertion failure in a release build, so these assertions generally need to
  237. * be of more than life-and-death importance if there's a chance they might
  238. * trigger. You should almost always consider handling these cases more
  239. * gracefully than an assert allows.
  240. *
  241. * When assertions are disabled, this wraps `condition` in a `sizeof`
  242. * operator, which means any function calls and side effects will not run, but
  243. * the compiler will not complain about any otherwise-unused variables that
  244. * are only referenced in the assertion.
  245. *
  246. * One can set the environment variable "SDL_ASSERT" to one of several strings
  247. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  248. * behavior, which may be desirable for automation purposes. If your platform
  249. * requires GUI interfaces to happen on the main thread but you're debugging
  250. * an assertion in a background thread, it might be desirable to set this to
  251. * "break" so that your debugger takes control as soon as assert is triggered,
  252. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  253. *
  254. * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
  255. * Please refer to your platform's documentation for how to set it!
  256. *
  257. * \param condition boolean value to test
  258. *
  259. * \since This macro is available since SDL 3.0.0.
  260. */
  261. #define SDL_assert_release(condition) SDL_disabled_assert(condition)
  262. /**
  263. * An assertion test that is performed only when built with paranoid settings.
  264. *
  265. * This macro is enabled when the SDL_ASSERT_LEVEL is >= 3, otherwise it is
  266. * disabled. This is a higher level than both release and debug, so these
  267. * tests are meant to be expensive and only run when specifically looking for
  268. * extremely unexpected failure cases in a special build.
  269. *
  270. * When assertions are disabled, this wraps `condition` in a `sizeof`
  271. * operator, which means any function calls and side effects will not run, but
  272. * the compiler will not complain about any otherwise-unused variables that
  273. * are only referenced in the assertion.
  274. *
  275. * One can set the environment variable "SDL_ASSERT" to one of several strings
  276. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  277. * behavior, which may be desirable for automation purposes. If your platform
  278. * requires GUI interfaces to happen on the main thread but you're debugging
  279. * an assertion in a background thread, it might be desirable to set this to
  280. * "break" so that your debugger takes control as soon as assert is triggered,
  281. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  282. *
  283. * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
  284. * Please refer to your platform's documentation for how to set it!
  285. *
  286. * \param condition boolean value to test
  287. *
  288. * \since This macro is available since SDL 3.0.0.
  289. */
  290. #define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  291. #endif
  292. #if SDL_ASSERT_LEVEL == 0 /* assertions disabled */
  293. # define SDL_assert(condition) SDL_disabled_assert(condition)
  294. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  295. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  296. #elif SDL_ASSERT_LEVEL == 1 /* release settings. */
  297. # define SDL_assert(condition) SDL_disabled_assert(condition)
  298. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  299. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  300. #elif SDL_ASSERT_LEVEL == 2 /* debug settings. */
  301. # define SDL_assert(condition) SDL_enabled_assert(condition)
  302. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  303. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  304. #elif SDL_ASSERT_LEVEL == 3 /* paranoid settings. */
  305. # define SDL_assert(condition) SDL_enabled_assert(condition)
  306. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  307. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  308. #else
  309. # error Unknown assertion level.
  310. #endif
  311. /**
  312. * An assertion test that always performed.
  313. *
  314. * This macro is always enabled no matter what SDL_ASSERT_LEVEL is set to. You
  315. * almost never want to use this, as it could trigger on an end-user's system,
  316. * crashing your program.
  317. *
  318. * One can set the environment variable "SDL_ASSERT" to one of several strings
  319. * ("abort", "break", "retry", "ignore", "always_ignore") to force a default
  320. * behavior, which may be desirable for automation purposes. If your platform
  321. * requires GUI interfaces to happen on the main thread but you're debugging
  322. * an assertion in a background thread, it might be desirable to set this to
  323. * "break" so that your debugger takes control as soon as assert is triggered,
  324. * instead of risking a bad UI interaction (deadlock, etc) in the application.
  325. *
  326. * Note that SDL_ASSERT is an _environment variable_ and not an SDL hint!
  327. * Please refer to your platform's documentation for how to set it!
  328. *
  329. * \param condition boolean value to test
  330. *
  331. * \since This macro is available since SDL 3.0.0.
  332. */
  333. #define SDL_assert_always(condition) SDL_enabled_assert(condition)
  334. /**
  335. * A callback that fires when an SDL assertion fails.
  336. *
  337. * \param data a pointer to the SDL_AssertData structure corresponding to the
  338. * current assertion
  339. * \param userdata what was passed as `userdata` to SDL_SetAssertionHandler()
  340. * \returns an SDL_AssertState value indicating how to handle the failure.
  341. */
  342. typedef SDL_AssertState (SDLCALL *SDL_AssertionHandler)(
  343. const SDL_AssertData* data, void* userdata);
  344. /**
  345. * Set an application-defined assertion handler.
  346. *
  347. * This function allows an application to show its own assertion UI and/or
  348. * force the response to an assertion failure. If the application doesn't
  349. * provide this, SDL will try to do the right thing, popping up a
  350. * system-specific GUI dialog, and probably minimizing any fullscreen windows.
  351. *
  352. * This callback may fire from any thread, but it runs wrapped in a mutex, so
  353. * it will only fire from one thread at a time.
  354. *
  355. * This callback is NOT reset to SDL's internal handler upon SDL_Quit()!
  356. *
  357. * \param handler the SDL_AssertionHandler function to call when an assertion
  358. * fails or NULL for the default handler
  359. * \param userdata a pointer that is passed to `handler`
  360. *
  361. * \since This function is available since SDL 3.0.0.
  362. *
  363. * \sa SDL_GetAssertionHandler
  364. */
  365. extern DECLSPEC void SDLCALL SDL_SetAssertionHandler(
  366. SDL_AssertionHandler handler,
  367. void *userdata);
  368. /**
  369. * Get the default assertion handler.
  370. *
  371. * This returns the function pointer that is called by default when an
  372. * assertion is triggered. This is an internal function provided by SDL, that
  373. * is used for assertions when SDL_SetAssertionHandler() hasn't been used to
  374. * provide a different function.
  375. *
  376. * \returns the default SDL_AssertionHandler that is called when an assert
  377. * triggers.
  378. *
  379. * \since This function is available since SDL 3.0.0.
  380. *
  381. * \sa SDL_GetAssertionHandler
  382. */
  383. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetDefaultAssertionHandler(void);
  384. /**
  385. * Get the current assertion handler.
  386. *
  387. * This returns the function pointer that is called when an assertion is
  388. * triggered. This is either the value last passed to
  389. * SDL_SetAssertionHandler(), or if no application-specified function is set,
  390. * is equivalent to calling SDL_GetDefaultAssertionHandler().
  391. *
  392. * The parameter `puserdata` is a pointer to a void*, which will store the
  393. * "userdata" pointer that was passed to SDL_SetAssertionHandler(). This value
  394. * will always be NULL for the default handler. If you don't care about this
  395. * data, it is safe to pass a NULL pointer to this function to ignore it.
  396. *
  397. * \param puserdata pointer which is filled with the "userdata" pointer that
  398. * was passed to SDL_SetAssertionHandler()
  399. * \returns the SDL_AssertionHandler that is called when an assert triggers.
  400. *
  401. * \since This function is available since SDL 3.0.0.
  402. *
  403. * \sa SDL_SetAssertionHandler
  404. */
  405. extern DECLSPEC SDL_AssertionHandler SDLCALL SDL_GetAssertionHandler(void **puserdata);
  406. /**
  407. * Get a list of all assertion failures.
  408. *
  409. * This function gets all assertions triggered since the last call to
  410. * SDL_ResetAssertionReport(), or the start of the program.
  411. *
  412. * The proper way to examine this data looks something like this:
  413. *
  414. * ```c
  415. * const SDL_AssertData *item = SDL_GetAssertionReport();
  416. * while (item) {
  417. * printf("'%s', %s (%s:%d), triggered %u times, always ignore: %s.\\n",
  418. * item->condition, item->function, item->filename,
  419. * item->linenum, item->trigger_count,
  420. * item->always_ignore ? "yes" : "no");
  421. * item = item->next;
  422. * }
  423. * ```
  424. *
  425. * \returns a list of all failed assertions or NULL if the list is empty. This
  426. * memory should not be modified or freed by the application.
  427. *
  428. * \since This function is available since SDL 3.0.0.
  429. *
  430. * \sa SDL_ResetAssertionReport
  431. */
  432. extern DECLSPEC const SDL_AssertData * SDLCALL SDL_GetAssertionReport(void);
  433. /**
  434. * Clear the list of all assertion failures.
  435. *
  436. * This function will clear the list of all assertions triggered up to that
  437. * point. Immediately following this call, SDL_GetAssertionReport will return
  438. * no items. In addition, any previously-triggered assertions will be reset to
  439. * a trigger_count of zero, and their always_ignore state will be false.
  440. *
  441. * \since This function is available since SDL 3.0.0.
  442. *
  443. * \sa SDL_GetAssertionReport
  444. */
  445. extern DECLSPEC void SDLCALL SDL_ResetAssertionReport(void);
  446. /* Ends C function definitions when using C++ */
  447. #ifdef __cplusplus
  448. }
  449. #endif
  450. #include <SDL3/SDL_close_code.h>
  451. #endif /* SDL_assert_h_ */