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