src/SDL_assert.c
author Ryan C. Gordon <icculus@icculus.org>
Wed, 13 Jan 2010 09:13:37 +0000
changeset 3661 22b6a0c7ea6e
parent 3657 eaea59cee6f2
child 3662 6a0b3048f271
permissions -rw-r--r--
Handle assertion failures during assertion handler.
slouken@3647
     1
/*
slouken@3647
     2
    SDL - Simple DirectMedia Layer
slouken@3647
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@3647
     4
slouken@3647
     5
    This library is free software; you can redistribute it and/or
slouken@3647
     6
    modify it under the terms of the GNU Lesser General Public
slouken@3647
     7
    License as published by the Free Software Foundation; either
slouken@3647
     8
    version 2.1 of the License, or (at your option) any later version.
slouken@3647
     9
slouken@3647
    10
    This library is distributed in the hope that it will be useful,
slouken@3647
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@3647
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@3647
    13
    Lesser General Public License for more details.
slouken@3647
    14
slouken@3647
    15
    You should have received a copy of the GNU Lesser General Public
slouken@3647
    16
    License along with this library; if not, write to the Free Software
slouken@3647
    17
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
slouken@3647
    18
slouken@3647
    19
    Sam Lantinga
slouken@3647
    20
    slouken@libsdl.org
slouken@3647
    21
*/
slouken@3647
    22
slouken@3651
    23
#include "SDL.h"
slouken@3647
    24
#include "SDL_assert.h"
slouken@3647
    25
slouken@3647
    26
#if (SDL_ASSERT_LEVEL > 0)
slouken@3647
    27
slouken@3647
    28
#ifdef _WINDOWS
slouken@3647
    29
#define WIN32_LEAN_AND_MEAN 1
slouken@3647
    30
#include <windows.h>
slouken@3647
    31
#else  /* fprintf, _exit(), etc. */
slouken@3647
    32
#include <stdio.h>
slouken@3647
    33
#include <stdlib.h>
icculus@3648
    34
#include <unistd.h>
slouken@3647
    35
#endif
slouken@3647
    36
slouken@3647
    37
/* We can keep all triggered assertions in a singly-linked list so we can
slouken@3647
    38
 *  generate a report later.
slouken@3647
    39
 */
slouken@3647
    40
#if !SDL_ASSERTION_REPORT_DISABLED
slouken@3647
    41
static SDL_assert_data assertion_list_terminator = { 0, 0, 0, 0, 0, 0, 0 };
slouken@3647
    42
static SDL_assert_data *triggered_assertions = &assertion_list_terminator;
slouken@3647
    43
#endif
slouken@3647
    44
icculus@3648
    45
#ifdef __GNUC__
icculus@3661
    46
static void
icculus@3661
    47
debug_print(const char *fmt, ...) __attribute__((format (printf, 1, 2)));
icculus@3648
    48
#endif
icculus@3648
    49
icculus@3648
    50
static void
icculus@3648
    51
debug_print(const char *fmt, ...)
slouken@3647
    52
{
slouken@3647
    53
#ifdef _WINDOWS
slouken@3647
    54
    /* Format into a buffer for OutputDebugStringA(). */
slouken@3647
    55
    char buf[1024];
slouken@3647
    56
    char *startptr;
slouken@3647
    57
    char *ptr;
slouken@3647
    58
    int len;
slouken@3647
    59
    va_list ap;
slouken@3647
    60
    va_start(ap, fmt);
slouken@3647
    61
    len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
slouken@3647
    62
    va_end(ap);
slouken@3647
    63
slouken@3647
    64
    /* Visual C's vsnprintf() may not null-terminate the buffer. */
slouken@3647
    65
    if ((len >= sizeof (buf)) || (len < 0)) {
slouken@3647
    66
        buf[sizeof (buf) - 1] = '\0';
slouken@3647
    67
    }
slouken@3647
    68
slouken@3647
    69
    /* Write it, sorting out the Unix newlines... */
slouken@3647
    70
    startptr = buf;
slouken@3647
    71
    for (ptr = startptr; *ptr; ptr++) {
slouken@3647
    72
        if (*ptr == '\n') {
slouken@3647
    73
            *ptr = '\0';
slouken@3647
    74
            OutputDebugStringA(startptr);
slouken@3647
    75
            OutputDebugStringA("\r\n");
slouken@3647
    76
            startptr = ptr+1;
slouken@3647
    77
        }
slouken@3647
    78
    }
slouken@3647
    79
slouken@3647
    80
    /* catch that last piece if it didn't have a newline... */
slouken@3647
    81
    if (startptr != ptr) {
slouken@3647
    82
        OutputDebugStringA(startptr);
slouken@3647
    83
    }
slouken@3647
    84
#else
slouken@3647
    85
    /* Unix has it easy. Just dump it to stderr. */
slouken@3647
    86
    va_list ap;
slouken@3647
    87
    va_start(ap, fmt);
slouken@3647
    88
    fprintf(stderr, fmt, ap);
slouken@3647
    89
    va_end(ap);
slouken@3647
    90
    fflush(stderr);
slouken@3647
    91
#endif
slouken@3647
    92
}
slouken@3647
    93
slouken@3647
    94
slouken@3647
    95
#ifdef _WINDOWS
slouken@3647
    96
static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
slouken@3647
    97
static const SDL_assert_data *SDL_Windows_AssertData = NULL;
slouken@3647
    98
slouken@3647
    99
static LRESULT CALLBACK
slouken@3647
   100
SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@3647
   101
{
slouken@3647
   102
    switch (msg)
slouken@3647
   103
    {
slouken@3647
   104
        case WM_CREATE:
slouken@3647
   105
        {
slouken@3647
   106
            /* !!! FIXME: all this code stinks. */
slouken@3647
   107
            const SDL_assert_data *data = SDL_Windows_AssertData;
slouken@3647
   108
            char buf[1024];
slouken@3647
   109
            const int w = 100;
slouken@3647
   110
            const int h = 25;
slouken@3647
   111
            const int gap = 10;
slouken@3647
   112
            int x = gap;
slouken@3647
   113
            int y = 50;
slouken@3647
   114
            int len;
slouken@3647
   115
            int i;
slouken@3647
   116
            static const struct { 
slouken@3647
   117
                const char *name;
slouken@3647
   118
                SDL_assert_state state;
slouken@3647
   119
            } buttons[] = {
slouken@3647
   120
                {"Abort", SDL_ASSERTION_ABORT },
slouken@3647
   121
                {"Break", SDL_ASSERTION_BREAK },
slouken@3647
   122
                {"Retry", SDL_ASSERTION_RETRY },
slouken@3647
   123
                {"Ignore", SDL_ASSERTION_IGNORE },
slouken@3647
   124
                {"Always Ignore", SDL_ASSERTION_ALWAYS_IGNORE },
slouken@3647
   125
            };
slouken@3647
   126
slouken@3647
   127
            len = (int) SDL_snprintf(buf, sizeof (buf), 
slouken@3647
   128
                         "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
slouken@3647
   129
                         data->function, data->filename, data->linenum,
slouken@3647
   130
                         data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   131
                         data->condition);
slouken@3647
   132
            if ((len < 0) || (len >= sizeof (buf))) {
slouken@3647
   133
                buf[sizeof (buf) - 1] = '\0';
slouken@3647
   134
            }
slouken@3647
   135
slouken@3647
   136
            CreateWindowA("STATIC", buf,
slouken@3647
   137
                         WS_VISIBLE | WS_CHILD | SS_LEFT,
slouken@3647
   138
                         x, y, 550, 100,
slouken@3647
   139
                         hwnd, (HMENU) 1, NULL, NULL);
slouken@3647
   140
            y += 110;
slouken@3647
   141
slouken@3647
   142
            for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
slouken@3647
   143
                CreateWindowA("BUTTON", buttons[i].name,
slouken@3647
   144
                         WS_VISIBLE | WS_CHILD,
slouken@3647
   145
                         x, y, w, h,
slouken@3647
   146
                         hwnd, (HMENU) buttons[i].state, NULL, NULL);
slouken@3647
   147
                x += w + gap;
slouken@3647
   148
            }
slouken@3647
   149
            break;
slouken@3647
   150
        }
slouken@3647
   151
slouken@3647
   152
        case WM_COMMAND:
slouken@3647
   153
            SDL_Windows_AssertChoice = ((SDL_assert_state) (LOWORD(wParam)));
slouken@3647
   154
            SDL_Windows_AssertData = NULL;
slouken@3647
   155
            break;
slouken@3647
   156
slouken@3647
   157
        case WM_DESTROY:
slouken@3647
   158
            SDL_Windows_AssertData = NULL;
slouken@3647
   159
            break;
slouken@3647
   160
    }
slouken@3647
   161
slouken@3647
   162
    return DefWindowProc(hwnd, msg, wParam, lParam);
slouken@3647
   163
}
slouken@3647
   164
slouken@3647
   165
static SDL_assert_state
slouken@3647
   166
SDL_PromptAssertion_windows(const SDL_assert_data *data)
slouken@3647
   167
{
slouken@3647
   168
    HINSTANCE hInstance = 0;  /* !!! FIXME? */
slouken@3647
   169
    HWND hwnd;
slouken@3647
   170
    MSG msg;
slouken@3647
   171
    WNDCLASS wc = {0};
slouken@3647
   172
slouken@3647
   173
    SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
slouken@3647
   174
    SDL_Windows_AssertData = data;
slouken@3647
   175
slouken@3647
   176
    wc.lpszClassName = TEXT("SDL_assert");
slouken@3647
   177
    wc.hInstance = hInstance ;
slouken@3647
   178
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
slouken@3647
   179
    wc.lpfnWndProc = SDL_Assertion_WndProc;
slouken@3647
   180
    wc.hCursor = LoadCursor(0, IDC_ARROW);
slouken@3647
   181
  
slouken@3647
   182
    RegisterClass(&wc);
slouken@3647
   183
    hwnd = CreateWindow(wc.lpszClassName, TEXT("SDL assertion failure"),
slouken@3647
   184
                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
slouken@3647
   185
                 150, 150, 570, 260, 0, 0, hInstance, 0);  
slouken@3647
   186
slouken@3647
   187
    while (GetMessage(&msg, NULL, 0, 0) && (SDL_Windows_AssertData != NULL)) {
slouken@3647
   188
        TranslateMessage(&msg);
slouken@3647
   189
        DispatchMessage(&msg);
slouken@3647
   190
    }
slouken@3647
   191
slouken@3647
   192
    DestroyWindow(hwnd);
slouken@3647
   193
    UnregisterClass(wc.lpszClassName, hInstance);
slouken@3647
   194
    return SDL_Windows_AssertChoice;
slouken@3647
   195
}
slouken@3647
   196
#endif
slouken@3647
   197
slouken@3647
   198
slouken@3647
   199
static void SDL_AddAssertionToReport(SDL_assert_data *data)
slouken@3647
   200
{
slouken@3647
   201
#if !SDL_ASSERTION_REPORT_DISABLED
slouken@3647
   202
    /* (data) is always a static struct defined with the assert macros, so
slouken@3647
   203
       we don't have to worry about copying or allocating them. */
slouken@3647
   204
    if (data->next == NULL) {  /* not yet added? */
slouken@3647
   205
        data->next = triggered_assertions;
slouken@3647
   206
        triggered_assertions = data;
slouken@3647
   207
    }
slouken@3647
   208
#endif
slouken@3647
   209
}
slouken@3647
   210
slouken@3647
   211
static void SDL_GenerateAssertionReport(void)
slouken@3647
   212
{
slouken@3647
   213
#if !SDL_ASSERTION_REPORT_DISABLED
slouken@3647
   214
    if (triggered_assertions != &assertion_list_terminator)
slouken@3647
   215
    {
slouken@3647
   216
        SDL_assert_data *item = triggered_assertions;
slouken@3647
   217
slouken@3647
   218
        debug_print("\n\nSDL assertion report.\n");
slouken@3647
   219
        debug_print("All SDL assertions between last init/quit:\n\n");
slouken@3647
   220
slouken@3647
   221
        while (item != &assertion_list_terminator) {
slouken@3647
   222
            debug_print(
slouken@3647
   223
                "'%s'\n"
slouken@3647
   224
                "    * %s (%s:%d)\n"
slouken@3647
   225
                "    * triggered %u time%s.\n"
slouken@3647
   226
                "    * always ignore: %s.\n",
slouken@3647
   227
                item->condition, item->function, item->filename,
slouken@3647
   228
                item->linenum, item->trigger_count,
slouken@3647
   229
                (item->trigger_count == 1) ? "" : "s",
slouken@3647
   230
                item->always_ignore ? "yes" : "no");
slouken@3647
   231
            item = item->next;
slouken@3647
   232
        }
slouken@3647
   233
        debug_print("\n");
slouken@3647
   234
slouken@3647
   235
        triggered_assertions = &assertion_list_terminator;
slouken@3647
   236
    }
slouken@3647
   237
#endif
slouken@3647
   238
}
slouken@3647
   239
icculus@3661
   240
static void SDL_ExitProcess(int exitcode)
slouken@3647
   241
{
slouken@3647
   242
#ifdef _WINDOWS
slouken@3647
   243
    ExitProcess(42);
slouken@3656
   244
#else
slouken@3647
   245
    _exit(42);
slouken@3647
   246
#endif
slouken@3647
   247
}
icculus@3661
   248
icculus@3661
   249
static void SDL_AbortAssertion(void)
icculus@3661
   250
{
icculus@3661
   251
    SDL_Quit();
icculus@3661
   252
    SDL_ExitProcess(42);
icculus@3661
   253
}
icculus@3661
   254
slouken@3647
   255
slouken@3647
   256
static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data)
slouken@3647
   257
{
slouken@3647
   258
    const char *envr;
slouken@3657
   259
    SDL_assert_state state = SDL_ASSERTION_ABORT;
slouken@3657
   260
    SDL_WindowID window;
slouken@3647
   261
slouken@3647
   262
    debug_print("\n\n"
slouken@3647
   263
                "Assertion failure at %s (%s:%d), triggered %u time%s:\n"
slouken@3647
   264
                "  '%s'\n"
slouken@3647
   265
                "\n",
slouken@3647
   266
                data->function, data->filename, data->linenum,
slouken@3647
   267
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   268
                data->condition);
slouken@3647
   269
slouken@3655
   270
    /* let env. variable override, so unit tests won't block in a GUI. */
slouken@3647
   271
    envr = SDL_getenv("SDL_ASSERT");
slouken@3647
   272
    if (envr != NULL) {
slouken@3647
   273
        if (SDL_strcmp(envr, "abort") == 0) {
slouken@3647
   274
            return SDL_ASSERTION_ABORT;
slouken@3647
   275
        } else if (SDL_strcmp(envr, "break") == 0) {
slouken@3647
   276
            return SDL_ASSERTION_BREAK;
slouken@3647
   277
        } else if (SDL_strcmp(envr, "retry") == 0) {
slouken@3647
   278
            return SDL_ASSERTION_RETRY;
slouken@3647
   279
        } else if (SDL_strcmp(envr, "ignore") == 0) {
slouken@3647
   280
            return SDL_ASSERTION_IGNORE;
slouken@3647
   281
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
slouken@3647
   282
            return SDL_ASSERTION_ALWAYS_IGNORE;
slouken@3647
   283
        } else {
slouken@3647
   284
            return SDL_ASSERTION_ABORT;  /* oh well. */
slouken@3647
   285
        }
slouken@3647
   286
    }
slouken@3647
   287
slouken@3657
   288
    /* Leave fullscreen mode, if possible (scary!) */
slouken@3657
   289
    window = SDL_GetFocusWindow();
slouken@3657
   290
    if (window) {
slouken@3657
   291
        if (SDL_GetWindowFlags(window) & SDL_WINDOW_FULLSCREEN) {
slouken@3657
   292
            SDL_MinimizeWindow(window);
slouken@3657
   293
        } else {
slouken@3657
   294
            /* No need to mess with the window */
slouken@3657
   295
            window = 0;
slouken@3657
   296
        }
slouken@3657
   297
    }
slouken@3657
   298
slouken@3647
   299
    /* platform-specific UI... */
slouken@3647
   300
slouken@3647
   301
#ifdef _WINDOWS
slouken@3657
   302
    state = SDL_PromptAssertion_windows(data);
slouken@3647
   303
slouken@3647
   304
#elif __APPLE__
slouken@3647
   305
    /* This has to be done in an Objective-C (*.m) file, so we call out. */
slouken@3647
   306
    extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);
slouken@3657
   307
    state = SDL_PromptAssertion_cocoa(data);
slouken@3647
   308
slouken@3657
   309
#else
slouken@3647
   310
    /* this is a little hacky. */
slouken@3647
   311
    for ( ; ; ) {
slouken@3647
   312
        char buf[32];
slouken@3647
   313
        fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
slouken@3647
   314
        fflush(stderr);
slouken@3647
   315
        if (fgets(buf, sizeof (buf), stdin) == NULL) {
slouken@3657
   316
            break;
slouken@3647
   317
        }
slouken@3647
   318
slouken@3647
   319
        if (SDL_strcmp(buf, "a") == 0) {
slouken@3657
   320
            state = SDL_ASSERTION_ABORT;
slouken@3657
   321
            break;
slouken@3647
   322
        } else if (SDL_strcmp(envr, "b") == 0) {
slouken@3657
   323
            state = SDL_ASSERTION_BREAK;
slouken@3657
   324
            break;
slouken@3647
   325
        } else if (SDL_strcmp(envr, "r") == 0) {
slouken@3657
   326
            state = SDL_ASSERTION_RETRY;
slouken@3657
   327
            break;
slouken@3647
   328
        } else if (SDL_strcmp(envr, "i") == 0) {
slouken@3657
   329
            state = SDL_ASSERTION_IGNORE;
slouken@3657
   330
            break;
slouken@3647
   331
        } else if (SDL_strcmp(envr, "A") == 0) {
slouken@3657
   332
            state = SDL_ASSERTION_ALWAYS_IGNORE;
slouken@3657
   333
            break;
slouken@3647
   334
        }
slouken@3647
   335
    }
slouken@3647
   336
#endif
slouken@3647
   337
slouken@3657
   338
    /* Re-enter fullscreen mode */
slouken@3657
   339
    if (window) {
slouken@3657
   340
        SDL_RestoreWindow(window);
slouken@3657
   341
    }
slouken@3657
   342
slouken@3657
   343
    return state;
slouken@3647
   344
}
slouken@3647
   345
slouken@3647
   346
slouken@3647
   347
static SDL_mutex *assertion_mutex = NULL;
slouken@3647
   348
slouken@3647
   349
SDL_assert_state
slouken@3655
   350
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
slouken@3655
   351
                    int line)
slouken@3647
   352
{
icculus@3661
   353
    static int assertion_running = 0;
icculus@3661
   354
    SDL_assert_state state = SDL_ASSERTION_IGNORE;
slouken@3647
   355
slouken@3647
   356
    if (SDL_LockMutex(assertion_mutex) < 0) {
slouken@3647
   357
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
slouken@3647
   358
    }
slouken@3647
   359
slouken@3647
   360
    /* doing this because Visual C is upset over assigning in the macro. */
slouken@3647
   361
    if (data->trigger_count == 0) {
slouken@3647
   362
        data->function = func;
slouken@3655
   363
        data->filename = file;
slouken@3655
   364
        data->linenum = line;
slouken@3647
   365
    }
slouken@3647
   366
slouken@3647
   367
    SDL_AddAssertionToReport(data);
slouken@3647
   368
slouken@3647
   369
    data->trigger_count++;
icculus@3661
   370
icculus@3661
   371
    assertion_running++;
icculus@3661
   372
    if (assertion_running > 1) {   /* assert during assert! Abort. */
icculus@3661
   373
        if (assertion_running == 2) {
icculus@3661
   374
            SDL_AbortAssertion();
icculus@3661
   375
        } else if (assertion_running == 3) {  /* Abort asserted! */
icculus@3661
   376
            SDL_ExitProcess(42);
icculus@3661
   377
        } else {
icculus@3661
   378
            while (1) { /* do nothing but spin; what else can you do?! */ }
icculus@3661
   379
        }
slouken@3647
   380
    }
slouken@3647
   381
icculus@3661
   382
    if (!data->always_ignore) {
icculus@3661
   383
        state = SDL_PromptAssertion(data);
icculus@3661
   384
    }
slouken@3647
   385
slouken@3647
   386
    switch (state)
slouken@3647
   387
    {
slouken@3647
   388
        case SDL_ASSERTION_ABORT:
slouken@3647
   389
            SDL_AbortAssertion();
slouken@3647
   390
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
slouken@3647
   391
slouken@3647
   392
        case SDL_ASSERTION_ALWAYS_IGNORE:
slouken@3647
   393
            state = SDL_ASSERTION_IGNORE;
slouken@3647
   394
            data->always_ignore = 1;
slouken@3647
   395
            break;
slouken@3647
   396
slouken@3647
   397
        case SDL_ASSERTION_IGNORE:
slouken@3647
   398
        case SDL_ASSERTION_RETRY:
slouken@3647
   399
        case SDL_ASSERTION_BREAK:
slouken@3647
   400
            break;  /* macro handles these. */
slouken@3647
   401
    }
slouken@3647
   402
icculus@3661
   403
    assertion_running--;
slouken@3647
   404
    SDL_UnlockMutex(assertion_mutex);
slouken@3647
   405
slouken@3647
   406
    return state;
slouken@3647
   407
}
slouken@3647
   408
slouken@3647
   409
#endif  /* SDL_ASSERT_LEVEL > 0 */
slouken@3647
   410
slouken@3647
   411
slouken@3647
   412
int SDL_AssertionsInit(void)
slouken@3647
   413
{
slouken@3647
   414
#if (SDL_ASSERT_LEVEL > 0)
slouken@3647
   415
    assertion_mutex = SDL_CreateMutex();
slouken@3647
   416
    if (assertion_mutex == NULL) {
slouken@3647
   417
        return -1;
slouken@3647
   418
    }
slouken@3647
   419
#endif
slouken@3647
   420
    return 0;
slouken@3647
   421
}
slouken@3647
   422
slouken@3647
   423
void SDL_AssertionsQuit(void)
slouken@3647
   424
{
slouken@3647
   425
#if (SDL_ASSERT_LEVEL > 0)
slouken@3647
   426
    SDL_GenerateAssertionReport();
slouken@3647
   427
    SDL_DestroyMutex(assertion_mutex);
slouken@3647
   428
    assertion_mutex = NULL;
slouken@3647
   429
#endif
slouken@3647
   430
}
slouken@3647
   431
slouken@3647
   432
/* vi: set ts=4 sw=4 expandtab: */