src/SDL_assert.c
author Ryan C. Gordon <icculus@icculus.org>
Fri, 19 May 2017 12:54:17 -0400
changeset 11015 b00c4088f687
parent 10737 3406a0f8b041
child 11017 411bf9f7f908
permissions -rw-r--r--
assert: allow assertions to work on platforms without threads.

Partially fixes Bugzilla #3459.
slouken@3647
     1
/*
slouken@5535
     2
  Simple DirectMedia Layer
slouken@10737
     3
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@3647
     4
slouken@5535
     5
  This software is provided 'as-is', without any express or implied
slouken@5535
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     7
  arising from the use of this software.
slouken@3647
     8
slouken@5535
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
    10
  including commercial applications, and to alter it and redistribute it
slouken@5535
    11
  freely, subject to the following restrictions:
slouken@3647
    12
slouken@5535
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@5535
    14
     claim that you wrote the original software. If you use this software
slouken@5535
    15
     in a product, an acknowledgment in the product documentation would be
slouken@5535
    16
     appreciated but is not required.
slouken@5535
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@5535
    18
     misrepresented as being the original software.
slouken@5535
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@3647
    20
*/
icculus@8093
    21
#include "./SDL_internal.h"
slouken@3647
    22
slouken@7828
    23
#if defined(__WIN32__)
slouken@7828
    24
#include "core/windows/SDL_windows.h"
slouken@7828
    25
#endif
slouken@7828
    26
slouken@3651
    27
#include "SDL.h"
slouken@5006
    28
#include "SDL_atomic.h"
slouken@6621
    29
#include "SDL_messagebox.h"
slouken@6621
    30
#include "SDL_video.h"
slouken@3647
    31
#include "SDL_assert.h"
slouken@4472
    32
#include "SDL_assert_c.h"
slouken@3671
    33
#include "video/SDL_sysvideo.h"
slouken@3647
    34
slouken@5086
    35
#ifdef __WIN32__
slouken@5086
    36
#ifndef WS_OVERLAPPEDWINDOW
slouken@5086
    37
#define WS_OVERLAPPEDWINDOW 0
slouken@5086
    38
#endif
slouken@3647
    39
#else  /* fprintf, _exit(), etc. */
slouken@3647
    40
#include <stdio.h>
slouken@3647
    41
#include <stdlib.h>
dludwig@8341
    42
#if ! defined(__WINRT__)
icculus@3648
    43
#include <unistd.h>
slouken@3647
    44
#endif
dludwig@8341
    45
#endif
slouken@3647
    46
icculus@3670
    47
static SDL_assert_state
icculus@3670
    48
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata);
icculus@3670
    49
icculus@3670
    50
/*
icculus@3670
    51
 * We keep all triggered assertions in a singly-linked list so we can
slouken@3647
    52
 *  generate a report later.
slouken@3647
    53
 */
icculus@5541
    54
static SDL_assert_data *triggered_assertions = NULL;
icculus@3670
    55
icculus@11015
    56
#ifndef SDL_THREADS_DISABLED
icculus@3670
    57
static SDL_mutex *assertion_mutex = NULL;
icculus@11015
    58
#endif
icculus@11015
    59
icculus@3670
    60
static SDL_AssertionHandler assertion_handler = SDL_PromptAssertion;
icculus@3670
    61
static void *assertion_userdata = NULL;
slouken@3647
    62
icculus@3648
    63
#ifdef __GNUC__
icculus@3661
    64
static void
icculus@3661
    65
debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
icculus@3648
    66
#endif
icculus@3648
    67
icculus@3648
    68
static void
icculus@3648
    69
debug_print(const char *fmt, ...)
slouken@3647
    70
{
slouken@3647
    71
    va_list ap;
slouken@3647
    72
    va_start(ap, fmt);
slouken@6621
    73
    SDL_LogMessageV(SDL_LOG_CATEGORY_ASSERT, SDL_LOG_PRIORITY_WARN, fmt, ap);
slouken@3647
    74
    va_end(ap);
slouken@3647
    75
}
slouken@3647
    76
slouken@3647
    77
slouken@3647
    78
static void SDL_AddAssertionToReport(SDL_assert_data *data)
slouken@3647
    79
{
slouken@3647
    80
    /* (data) is always a static struct defined with the assert macros, so
slouken@3647
    81
       we don't have to worry about copying or allocating them. */
icculus@5541
    82
    data->trigger_count++;
icculus@5541
    83
    if (data->trigger_count == 1) {  /* not yet added? */
slouken@3647
    84
        data->next = triggered_assertions;
slouken@3647
    85
        triggered_assertions = data;
slouken@3647
    86
    }
slouken@3647
    87
}
slouken@3647
    88
icculus@3670
    89
slouken@3647
    90
static void SDL_GenerateAssertionReport(void)
slouken@3647
    91
{
icculus@5541
    92
    const SDL_assert_data *item = triggered_assertions;
icculus@3670
    93
icculus@3670
    94
    /* only do this if the app hasn't assigned an assertion handler. */
icculus@5541
    95
    if ((item != NULL) && (assertion_handler != SDL_PromptAssertion)) {
slouken@3647
    96
        debug_print("\n\nSDL assertion report.\n");
slouken@3647
    97
        debug_print("All SDL assertions between last init/quit:\n\n");
slouken@3647
    98
icculus@5541
    99
        while (item != NULL) {
slouken@3647
   100
            debug_print(
slouken@3647
   101
                "'%s'\n"
slouken@3647
   102
                "    * %s (%s:%d)\n"
slouken@3647
   103
                "    * triggered %u time%s.\n"
slouken@3647
   104
                "    * always ignore: %s.\n",
slouken@3647
   105
                item->condition, item->function, item->filename,
slouken@3647
   106
                item->linenum, item->trigger_count,
slouken@3647
   107
                (item->trigger_count == 1) ? "" : "s",
slouken@3647
   108
                item->always_ignore ? "yes" : "no");
slouken@3647
   109
            item = item->next;
slouken@3647
   110
        }
slouken@3647
   111
        debug_print("\n");
slouken@3647
   112
icculus@3670
   113
        SDL_ResetAssertionReport();
slouken@3647
   114
    }
slouken@3647
   115
}
slouken@3647
   116
slouken@10616
   117
slouken@10616
   118
static SDL_NORETURN void SDL_ExitProcess(int exitcode)
slouken@3647
   119
{
slouken@5086
   120
#ifdef __WIN32__
icculus@6305
   121
    ExitProcess(exitcode);
slouken@3656
   122
#else
icculus@6305
   123
    _exit(exitcode);
slouken@3647
   124
#endif
slouken@3647
   125
}
icculus@3661
   126
slouken@10616
   127
slouken@10616
   128
static SDL_NORETURN void SDL_AbortAssertion(void)
icculus@3661
   129
{
icculus@3661
   130
    SDL_Quit();
icculus@3661
   131
    SDL_ExitProcess(42);
icculus@3661
   132
}
icculus@3661
   133
slouken@3647
   134
icculus@3670
   135
static SDL_assert_state
icculus@3670
   136
SDL_PromptAssertion(const SDL_assert_data *data, void *userdata)
slouken@3647
   137
{
icculus@6759
   138
#ifdef __WIN32__
icculus@6759
   139
    #define ENDLINE "\r\n"
icculus@6759
   140
#else
icculus@6759
   141
    #define ENDLINE "\n"
icculus@6759
   142
#endif
icculus@6759
   143
slouken@3647
   144
    const char *envr;
slouken@3657
   145
    SDL_assert_state state = SDL_ASSERTION_ABORT;
slouken@3685
   146
    SDL_Window *window;
slouken@6621
   147
    SDL_MessageBoxData messagebox;
slouken@6621
   148
    SDL_MessageBoxButtonData buttons[] = {
slouken@6621
   149
        {   0,  SDL_ASSERTION_RETRY,            "Retry" },
slouken@6621
   150
        {   0,  SDL_ASSERTION_BREAK,            "Break" },
slouken@6621
   151
        {   0,  SDL_ASSERTION_ABORT,            "Abort" },
slouken@6621
   152
        {   SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT,
slouken@6621
   153
                SDL_ASSERTION_IGNORE,           "Ignore" },
slouken@6621
   154
        {   SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT,
slouken@6621
   155
                SDL_ASSERTION_ALWAYS_IGNORE,    "Always Ignore" }
slouken@6621
   156
    };
slouken@6621
   157
    char *message;
slouken@6621
   158
    int selected;
slouken@3647
   159
icculus@3670
   160
    (void) userdata;  /* unused in default handler. */
icculus@3670
   161
slouken@6621
   162
    message = SDL_stack_alloc(char, SDL_MAX_LOG_MESSAGE);
slouken@6621
   163
    if (!message) {
slouken@6621
   164
        /* Uh oh, we're in real trouble now... */
slouken@6621
   165
        return SDL_ASSERTION_ABORT;
slouken@6621
   166
    }
slouken@6621
   167
    SDL_snprintf(message, SDL_MAX_LOG_MESSAGE,
icculus@6759
   168
                 "Assertion failure at %s (%s:%d), triggered %u %s:" ENDLINE
icculus@6759
   169
                    "  '%s'",
slouken@6621
   170
                 data->function, data->filename, data->linenum,
slouken@6621
   171
                 data->trigger_count, (data->trigger_count == 1) ? "time" : "times",
slouken@6621
   172
                 data->condition);
slouken@6621
   173
slouken@6621
   174
    debug_print("\n\n%s\n\n", message);
slouken@3647
   175
slouken@3655
   176
    /* let env. variable override, so unit tests won't block in a GUI. */
slouken@3647
   177
    envr = SDL_getenv("SDL_ASSERT");
slouken@3647
   178
    if (envr != NULL) {
slouken@6621
   179
        SDL_stack_free(message);
slouken@6621
   180
slouken@3647
   181
        if (SDL_strcmp(envr, "abort") == 0) {
slouken@3647
   182
            return SDL_ASSERTION_ABORT;
slouken@3647
   183
        } else if (SDL_strcmp(envr, "break") == 0) {
slouken@3647
   184
            return SDL_ASSERTION_BREAK;
slouken@3647
   185
        } else if (SDL_strcmp(envr, "retry") == 0) {
slouken@3647
   186
            return SDL_ASSERTION_RETRY;
slouken@3647
   187
        } else if (SDL_strcmp(envr, "ignore") == 0) {
slouken@3647
   188
            return SDL_ASSERTION_IGNORE;
slouken@3647
   189
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
slouken@3647
   190
            return SDL_ASSERTION_ALWAYS_IGNORE;
slouken@3647
   191
        } else {
slouken@3647
   192
            return SDL_ASSERTION_ABORT;  /* oh well. */
slouken@3647
   193
        }
slouken@3647
   194
    }
slouken@3647
   195
slouken@3657
   196
    /* Leave fullscreen mode, if possible (scary!) */
slouken@3657
   197
    window = SDL_GetFocusWindow();
slouken@3657
   198
    if (window) {
slouken@3657
   199
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@3657
   200
            SDL_MinimizeWindow(window);
slouken@3657
   201
        } else {
icculus@3670
   202
            /* !!! FIXME: ungrab the input if we're not fullscreen? */
slouken@3657
   203
            /* No need to mess with the window */
slouken@6621
   204
            window = NULL;
slouken@3657
   205
        }
slouken@3657
   206
    }
slouken@3657
   207
slouken@6621
   208
    /* Show a messagebox if we can, otherwise fall back to stdio */
slouken@6621
   209
    SDL_zero(messagebox);
slouken@6621
   210
    messagebox.flags = SDL_MESSAGEBOX_WARNING;
slouken@6621
   211
    messagebox.window = window;
slouken@6621
   212
    messagebox.title = "Assertion Failed";
slouken@6621
   213
    messagebox.message = message;
slouken@6621
   214
    messagebox.numbuttons = SDL_arraysize(buttons);
slouken@6621
   215
    messagebox.buttons = buttons;
slouken@3647
   216
slouken@6621
   217
    if (SDL_ShowMessageBox(&messagebox, &selected) == 0) {
slouken@6621
   218
        if (selected == -1) {
slouken@3657
   219
            state = SDL_ASSERTION_IGNORE;
slouken@6621
   220
        } else {
slouken@6621
   221
            state = (SDL_assert_state)selected;
slouken@3647
   222
        }
slouken@3647
   223
    }
slouken@6621
   224
#ifdef HAVE_STDIO_H
slouken@6621
   225
    else
slouken@6621
   226
    {
slouken@6621
   227
        /* this is a little hacky. */
slouken@6621
   228
        for ( ; ; ) {
slouken@6621
   229
            char buf[32];
slouken@6621
   230
            fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
slouken@6621
   231
            fflush(stderr);
slouken@6621
   232
            if (fgets(buf, sizeof (buf), stdin) == NULL) {
slouken@6621
   233
                break;
slouken@6621
   234
            }
slouken@6621
   235
slouken@6621
   236
            if (SDL_strcmp(buf, "a") == 0) {
slouken@6621
   237
                state = SDL_ASSERTION_ABORT;
slouken@6621
   238
                break;
slouken@6621
   239
            } else if (SDL_strcmp(buf, "b") == 0) {
slouken@6621
   240
                state = SDL_ASSERTION_BREAK;
slouken@6621
   241
                break;
slouken@6621
   242
            } else if (SDL_strcmp(buf, "r") == 0) {
slouken@6621
   243
                state = SDL_ASSERTION_RETRY;
slouken@6621
   244
                break;
slouken@6621
   245
            } else if (SDL_strcmp(buf, "i") == 0) {
slouken@6621
   246
                state = SDL_ASSERTION_IGNORE;
slouken@6621
   247
                break;
slouken@6621
   248
            } else if (SDL_strcmp(buf, "A") == 0) {
slouken@6621
   249
                state = SDL_ASSERTION_ALWAYS_IGNORE;
slouken@6621
   250
                break;
slouken@6621
   251
            }
slouken@6621
   252
        }
slouken@6621
   253
    }
slouken@6621
   254
#endif /* HAVE_STDIO_H */
slouken@3647
   255
slouken@3657
   256
    /* Re-enter fullscreen mode */
slouken@3657
   257
    if (window) {
slouken@3657
   258
        SDL_RestoreWindow(window);
slouken@3657
   259
    }
slouken@3657
   260
slouken@6621
   261
    SDL_stack_free(message);
slouken@6621
   262
slouken@3657
   263
    return state;
slouken@3647
   264
}
slouken@3647
   265
slouken@3647
   266
slouken@3647
   267
SDL_assert_state
slouken@3655
   268
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
slouken@3655
   269
                    int line)
slouken@3647
   270
{
icculus@11015
   271
    SDL_assert_state state = SDL_ASSERTION_IGNORE;
icculus@3661
   272
    static int assertion_running = 0;
icculus@11015
   273
icculus@11015
   274
#ifndef SDL_THREADS_DISABLED
icculus@3662
   275
    static SDL_SpinLock spinlock = 0;
icculus@3662
   276
    SDL_AtomicLock(&spinlock);
icculus@3662
   277
    if (assertion_mutex == NULL) { /* never called SDL_Init()? */
icculus@3662
   278
        assertion_mutex = SDL_CreateMutex();
icculus@3662
   279
        if (assertion_mutex == NULL) {
icculus@3662
   280
            SDL_AtomicUnlock(&spinlock);
icculus@3662
   281
            return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
icculus@3662
   282
        }
icculus@3662
   283
    }
icculus@3662
   284
    SDL_AtomicUnlock(&spinlock);
icculus@3662
   285
slouken@3647
   286
    if (SDL_LockMutex(assertion_mutex) < 0) {
slouken@3647
   287
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
slouken@3647
   288
    }
icculus@11015
   289
#endif
slouken@3647
   290
slouken@3647
   291
    /* doing this because Visual C is upset over assigning in the macro. */
slouken@3647
   292
    if (data->trigger_count == 0) {
slouken@3647
   293
        data->function = func;
slouken@3655
   294
        data->filename = file;
slouken@3655
   295
        data->linenum = line;
slouken@3647
   296
    }
slouken@3647
   297
slouken@3647
   298
    SDL_AddAssertionToReport(data);
slouken@3647
   299
icculus@3661
   300
    assertion_running++;
icculus@3661
   301
    if (assertion_running > 1) {   /* assert during assert! Abort. */
icculus@3661
   302
        if (assertion_running == 2) {
icculus@3661
   303
            SDL_AbortAssertion();
icculus@3661
   304
        } else if (assertion_running == 3) {  /* Abort asserted! */
icculus@3661
   305
            SDL_ExitProcess(42);
icculus@3661
   306
        } else {
icculus@3661
   307
            while (1) { /* do nothing but spin; what else can you do?! */ }
icculus@3661
   308
        }
slouken@3647
   309
    }
slouken@3647
   310
icculus@3661
   311
    if (!data->always_ignore) {
icculus@3670
   312
        state = assertion_handler(data, assertion_userdata);
icculus@3661
   313
    }
slouken@3647
   314
slouken@3647
   315
    switch (state)
slouken@3647
   316
    {
slouken@3647
   317
        case SDL_ASSERTION_ABORT:
slouken@3647
   318
            SDL_AbortAssertion();
slouken@3647
   319
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
slouken@3647
   320
slouken@3647
   321
        case SDL_ASSERTION_ALWAYS_IGNORE:
slouken@3647
   322
            state = SDL_ASSERTION_IGNORE;
slouken@3647
   323
            data->always_ignore = 1;
slouken@3647
   324
            break;
slouken@3647
   325
slouken@3647
   326
        case SDL_ASSERTION_IGNORE:
slouken@3647
   327
        case SDL_ASSERTION_RETRY:
slouken@3647
   328
        case SDL_ASSERTION_BREAK:
slouken@3647
   329
            break;  /* macro handles these. */
slouken@3647
   330
    }
slouken@3647
   331
icculus@3661
   332
    assertion_running--;
icculus@11015
   333
icculus@11015
   334
#ifndef SDL_THREADS_DISABLED
slouken@3647
   335
    SDL_UnlockMutex(assertion_mutex);
icculus@11015
   336
#endif
slouken@3647
   337
slouken@3647
   338
    return state;
slouken@3647
   339
}
slouken@3647
   340
slouken@3647
   341
slouken@3647
   342
void SDL_AssertionsQuit(void)
slouken@3647
   343
{
slouken@3647
   344
    SDL_GenerateAssertionReport();
icculus@11015
   345
#ifndef SDL_THREADS_DISABLED
icculus@3664
   346
    if (assertion_mutex != NULL) {
icculus@3664
   347
        SDL_DestroyMutex(assertion_mutex);
icculus@3664
   348
        assertion_mutex = NULL;
icculus@3664
   349
    }
icculus@11015
   350
#endif
icculus@3670
   351
}
icculus@3670
   352
icculus@3670
   353
void SDL_SetAssertionHandler(SDL_AssertionHandler handler, void *userdata)
icculus@3670
   354
{
icculus@3670
   355
    if (handler != NULL) {
icculus@3670
   356
        assertion_handler = handler;
icculus@3670
   357
        assertion_userdata = userdata;
icculus@3670
   358
    } else {
icculus@3670
   359
        assertion_handler = SDL_PromptAssertion;
icculus@3670
   360
        assertion_userdata = NULL;
icculus@3670
   361
    }
icculus@3670
   362
}
icculus@3670
   363
icculus@3670
   364
const SDL_assert_data *SDL_GetAssertionReport(void)
icculus@3670
   365
{
icculus@3670
   366
    return triggered_assertions;
icculus@3670
   367
}
icculus@3670
   368
icculus@3670
   369
void SDL_ResetAssertionReport(void)
icculus@3670
   370
{
icculus@3670
   371
    SDL_assert_data *next = NULL;
icculus@5541
   372
    SDL_assert_data *item;
icculus@5541
   373
    for (item = triggered_assertions; item != NULL; item = next) {
icculus@3670
   374
        next = (SDL_assert_data *) item->next;
icculus@3670
   375
        item->always_ignore = SDL_FALSE;
icculus@3670
   376
        item->trigger_count = 0;
icculus@3670
   377
        item->next = NULL;
icculus@3670
   378
    }
icculus@3670
   379
icculus@5541
   380
    triggered_assertions = NULL;
slouken@3647
   381
}
slouken@3647
   382
icculus@8167
   383
SDL_AssertionHandler SDL_GetDefaultAssertionHandler(void)
icculus@8167
   384
{
icculus@8167
   385
    return SDL_PromptAssertion;
icculus@8167
   386
}
icculus@8167
   387
icculus@8167
   388
SDL_AssertionHandler SDL_GetAssertionHandler(void **userdata)
icculus@8167
   389
{
icculus@8167
   390
    if (userdata != NULL) {
icculus@8167
   391
        *userdata = assertion_userdata;
icculus@8167
   392
    }
icculus@8167
   393
    return assertion_handler;
icculus@8167
   394
}
icculus@8167
   395
slouken@3647
   396
/* vi: set ts=4 sw=4 expandtab: */