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