src/SDL_assert.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 13 Jan 2010 08:30:34 +0000
changeset 3656 f17ea6f49745
parent 3655 1cc7f0143c12
child 3657 eaea59cee6f2
permissions -rw-r--r--
Assume _exit() is available. It may be, even if unix isn't defined.
If we really have to, we can add a configure check for it.
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
slouken@3647
    45
static void 
slouken@3647
    46
debug_print(const char *fmt, ...)
icculus@3648
    47
#ifdef __GNUC__
icculus@3648
    48
__attribute__((format (printf, 1, 2)))
icculus@3648
    49
#endif
icculus@3648
    50
;
icculus@3648
    51
icculus@3648
    52
static void
icculus@3648
    53
debug_print(const char *fmt, ...)
slouken@3647
    54
{
slouken@3647
    55
#ifdef _WINDOWS
slouken@3647
    56
    /* Format into a buffer for OutputDebugStringA(). */
slouken@3647
    57
    char buf[1024];
slouken@3647
    58
    char *startptr;
slouken@3647
    59
    char *ptr;
slouken@3647
    60
    int len;
slouken@3647
    61
    va_list ap;
slouken@3647
    62
    va_start(ap, fmt);
slouken@3647
    63
    len = (int) SDL_vsnprintf(buf, sizeof (buf), fmt, ap);
slouken@3647
    64
    va_end(ap);
slouken@3647
    65
slouken@3647
    66
    /* Visual C's vsnprintf() may not null-terminate the buffer. */
slouken@3647
    67
    if ((len >= sizeof (buf)) || (len < 0)) {
slouken@3647
    68
        buf[sizeof (buf) - 1] = '\0';
slouken@3647
    69
    }
slouken@3647
    70
slouken@3647
    71
    /* Write it, sorting out the Unix newlines... */
slouken@3647
    72
    startptr = buf;
slouken@3647
    73
    for (ptr = startptr; *ptr; ptr++) {
slouken@3647
    74
        if (*ptr == '\n') {
slouken@3647
    75
            *ptr = '\0';
slouken@3647
    76
            OutputDebugStringA(startptr);
slouken@3647
    77
            OutputDebugStringA("\r\n");
slouken@3647
    78
            startptr = ptr+1;
slouken@3647
    79
        }
slouken@3647
    80
    }
slouken@3647
    81
slouken@3647
    82
    /* catch that last piece if it didn't have a newline... */
slouken@3647
    83
    if (startptr != ptr) {
slouken@3647
    84
        OutputDebugStringA(startptr);
slouken@3647
    85
    }
slouken@3647
    86
#else
slouken@3647
    87
    /* Unix has it easy. Just dump it to stderr. */
slouken@3647
    88
    va_list ap;
slouken@3647
    89
    va_start(ap, fmt);
slouken@3647
    90
    fprintf(stderr, fmt, ap);
slouken@3647
    91
    va_end(ap);
slouken@3647
    92
    fflush(stderr);
slouken@3647
    93
#endif
slouken@3647
    94
}
slouken@3647
    95
slouken@3647
    96
slouken@3647
    97
#ifdef _WINDOWS
slouken@3647
    98
static SDL_assert_state SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
slouken@3647
    99
static const SDL_assert_data *SDL_Windows_AssertData = NULL;
slouken@3647
   100
slouken@3647
   101
static LRESULT CALLBACK
slouken@3647
   102
SDL_Assertion_WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
slouken@3647
   103
{
slouken@3647
   104
    switch (msg)
slouken@3647
   105
    {
slouken@3647
   106
        case WM_CREATE:
slouken@3647
   107
        {
slouken@3647
   108
            /* !!! FIXME: all this code stinks. */
slouken@3647
   109
            const SDL_assert_data *data = SDL_Windows_AssertData;
slouken@3647
   110
            char buf[1024];
slouken@3647
   111
            const int w = 100;
slouken@3647
   112
            const int h = 25;
slouken@3647
   113
            const int gap = 10;
slouken@3647
   114
            int x = gap;
slouken@3647
   115
            int y = 50;
slouken@3647
   116
            int len;
slouken@3647
   117
            int i;
slouken@3647
   118
            static const struct { 
slouken@3647
   119
                const char *name;
slouken@3647
   120
                SDL_assert_state state;
slouken@3647
   121
            } buttons[] = {
slouken@3647
   122
                {"Abort", SDL_ASSERTION_ABORT },
slouken@3647
   123
                {"Break", SDL_ASSERTION_BREAK },
slouken@3647
   124
                {"Retry", SDL_ASSERTION_RETRY },
slouken@3647
   125
                {"Ignore", SDL_ASSERTION_IGNORE },
slouken@3647
   126
                {"Always Ignore", SDL_ASSERTION_ALWAYS_IGNORE },
slouken@3647
   127
            };
slouken@3647
   128
slouken@3647
   129
            len = (int) SDL_snprintf(buf, sizeof (buf), 
slouken@3647
   130
                         "Assertion failure at %s (%s:%d), triggered %u time%s:\r\n  '%s'",
slouken@3647
   131
                         data->function, data->filename, data->linenum,
slouken@3647
   132
                         data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   133
                         data->condition);
slouken@3647
   134
            if ((len < 0) || (len >= sizeof (buf))) {
slouken@3647
   135
                buf[sizeof (buf) - 1] = '\0';
slouken@3647
   136
            }
slouken@3647
   137
slouken@3647
   138
            CreateWindowA("STATIC", buf,
slouken@3647
   139
                         WS_VISIBLE | WS_CHILD | SS_LEFT,
slouken@3647
   140
                         x, y, 550, 100,
slouken@3647
   141
                         hwnd, (HMENU) 1, NULL, NULL);
slouken@3647
   142
            y += 110;
slouken@3647
   143
slouken@3647
   144
            for (i = 0; i < (sizeof (buttons) / sizeof (buttons[0])); i++) {
slouken@3647
   145
                CreateWindowA("BUTTON", buttons[i].name,
slouken@3647
   146
                         WS_VISIBLE | WS_CHILD,
slouken@3647
   147
                         x, y, w, h,
slouken@3647
   148
                         hwnd, (HMENU) buttons[i].state, NULL, NULL);
slouken@3647
   149
                x += w + gap;
slouken@3647
   150
            }
slouken@3647
   151
            break;
slouken@3647
   152
        }
slouken@3647
   153
slouken@3647
   154
        case WM_COMMAND:
slouken@3647
   155
            SDL_Windows_AssertChoice = ((SDL_assert_state) (LOWORD(wParam)));
slouken@3647
   156
            SDL_Windows_AssertData = NULL;
slouken@3647
   157
            break;
slouken@3647
   158
slouken@3647
   159
        case WM_DESTROY:
slouken@3647
   160
            SDL_Windows_AssertData = NULL;
slouken@3647
   161
            break;
slouken@3647
   162
    }
slouken@3647
   163
slouken@3647
   164
    return DefWindowProc(hwnd, msg, wParam, lParam);
slouken@3647
   165
}
slouken@3647
   166
slouken@3647
   167
static SDL_assert_state
slouken@3647
   168
SDL_PromptAssertion_windows(const SDL_assert_data *data)
slouken@3647
   169
{
slouken@3647
   170
    HINSTANCE hInstance = 0;  /* !!! FIXME? */
slouken@3647
   171
    HWND hwnd;
slouken@3647
   172
    MSG msg;
slouken@3647
   173
    WNDCLASS wc = {0};
slouken@3647
   174
slouken@3647
   175
    SDL_Windows_AssertChoice = SDL_ASSERTION_ABORT;
slouken@3647
   176
    SDL_Windows_AssertData = data;
slouken@3647
   177
slouken@3647
   178
    wc.lpszClassName = TEXT("SDL_assert");
slouken@3647
   179
    wc.hInstance = hInstance ;
slouken@3647
   180
    wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
slouken@3647
   181
    wc.lpfnWndProc = SDL_Assertion_WndProc;
slouken@3647
   182
    wc.hCursor = LoadCursor(0, IDC_ARROW);
slouken@3647
   183
  
slouken@3647
   184
    RegisterClass(&wc);
slouken@3647
   185
    hwnd = CreateWindow(wc.lpszClassName, TEXT("SDL assertion failure"),
slouken@3647
   186
                 WS_OVERLAPPEDWINDOW | WS_VISIBLE,
slouken@3647
   187
                 150, 150, 570, 260, 0, 0, hInstance, 0);  
slouken@3647
   188
slouken@3647
   189
    while (GetMessage(&msg, NULL, 0, 0) && (SDL_Windows_AssertData != NULL)) {
slouken@3647
   190
        TranslateMessage(&msg);
slouken@3647
   191
        DispatchMessage(&msg);
slouken@3647
   192
    }
slouken@3647
   193
slouken@3647
   194
    DestroyWindow(hwnd);
slouken@3647
   195
    UnregisterClass(wc.lpszClassName, hInstance);
slouken@3647
   196
    return SDL_Windows_AssertChoice;
slouken@3647
   197
}
slouken@3647
   198
#endif
slouken@3647
   199
slouken@3647
   200
slouken@3647
   201
static void SDL_AddAssertionToReport(SDL_assert_data *data)
slouken@3647
   202
{
slouken@3647
   203
#if !SDL_ASSERTION_REPORT_DISABLED
slouken@3647
   204
    /* (data) is always a static struct defined with the assert macros, so
slouken@3647
   205
       we don't have to worry about copying or allocating them. */
slouken@3647
   206
    if (data->next == NULL) {  /* not yet added? */
slouken@3647
   207
        data->next = triggered_assertions;
slouken@3647
   208
        triggered_assertions = data;
slouken@3647
   209
    }
slouken@3647
   210
#endif
slouken@3647
   211
}
slouken@3647
   212
slouken@3647
   213
static void SDL_GenerateAssertionReport(void)
slouken@3647
   214
{
slouken@3647
   215
#if !SDL_ASSERTION_REPORT_DISABLED
slouken@3647
   216
    if (triggered_assertions != &assertion_list_terminator)
slouken@3647
   217
    {
slouken@3647
   218
        SDL_assert_data *item = triggered_assertions;
slouken@3647
   219
slouken@3647
   220
        debug_print("\n\nSDL assertion report.\n");
slouken@3647
   221
        debug_print("All SDL assertions between last init/quit:\n\n");
slouken@3647
   222
slouken@3647
   223
        while (item != &assertion_list_terminator) {
slouken@3647
   224
            debug_print(
slouken@3647
   225
                "'%s'\n"
slouken@3647
   226
                "    * %s (%s:%d)\n"
slouken@3647
   227
                "    * triggered %u time%s.\n"
slouken@3647
   228
                "    * always ignore: %s.\n",
slouken@3647
   229
                item->condition, item->function, item->filename,
slouken@3647
   230
                item->linenum, item->trigger_count,
slouken@3647
   231
                (item->trigger_count == 1) ? "" : "s",
slouken@3647
   232
                item->always_ignore ? "yes" : "no");
slouken@3647
   233
            item = item->next;
slouken@3647
   234
        }
slouken@3647
   235
        debug_print("\n");
slouken@3647
   236
slouken@3647
   237
        triggered_assertions = &assertion_list_terminator;
slouken@3647
   238
    }
slouken@3647
   239
#endif
slouken@3647
   240
}
slouken@3647
   241
slouken@3647
   242
slouken@3647
   243
static void SDL_AbortAssertion(void)
slouken@3647
   244
{
slouken@3647
   245
    SDL_Quit();
slouken@3647
   246
#ifdef _WINDOWS
slouken@3647
   247
    ExitProcess(42);
slouken@3656
   248
#else
slouken@3647
   249
    _exit(42);
slouken@3647
   250
#endif
slouken@3647
   251
}
slouken@3647
   252
    
slouken@3647
   253
slouken@3647
   254
static SDL_assert_state SDL_PromptAssertion(const SDL_assert_data *data)
slouken@3647
   255
{
slouken@3647
   256
    const char *envr;
slouken@3647
   257
slouken@3647
   258
    debug_print("\n\n"
slouken@3647
   259
                "Assertion failure at %s (%s:%d), triggered %u time%s:\n"
slouken@3647
   260
                "  '%s'\n"
slouken@3647
   261
                "\n",
slouken@3647
   262
                data->function, data->filename, data->linenum,
slouken@3647
   263
                data->trigger_count, (data->trigger_count == 1) ? "" : "s",
slouken@3647
   264
                data->condition);
slouken@3647
   265
slouken@3655
   266
    /* let env. variable override, so unit tests won't block in a GUI. */
slouken@3647
   267
    envr = SDL_getenv("SDL_ASSERT");
slouken@3647
   268
    if (envr != NULL) {
slouken@3647
   269
        if (SDL_strcmp(envr, "abort") == 0) {
slouken@3647
   270
            return SDL_ASSERTION_ABORT;
slouken@3647
   271
        } else if (SDL_strcmp(envr, "break") == 0) {
slouken@3647
   272
            return SDL_ASSERTION_BREAK;
slouken@3647
   273
        } else if (SDL_strcmp(envr, "retry") == 0) {
slouken@3647
   274
            return SDL_ASSERTION_RETRY;
slouken@3647
   275
        } else if (SDL_strcmp(envr, "ignore") == 0) {
slouken@3647
   276
            return SDL_ASSERTION_IGNORE;
slouken@3647
   277
        } else if (SDL_strcmp(envr, "always_ignore") == 0) {
slouken@3647
   278
            return SDL_ASSERTION_ALWAYS_IGNORE;
slouken@3647
   279
        } else {
slouken@3647
   280
            return SDL_ASSERTION_ABORT;  /* oh well. */
slouken@3647
   281
        }
slouken@3647
   282
    }
slouken@3647
   283
slouken@3647
   284
    /* platform-specific UI... */
slouken@3647
   285
slouken@3647
   286
#ifdef _WINDOWS
slouken@3647
   287
    return SDL_PromptAssertion_windows(data);
slouken@3647
   288
slouken@3647
   289
#elif __APPLE__
slouken@3647
   290
    /* This has to be done in an Objective-C (*.m) file, so we call out. */
slouken@3647
   291
    extern SDL_assert_state SDL_PromptAssertion_cocoa(const SDL_assert_data *);
slouken@3647
   292
    return SDL_PromptAssertion_cocoa(data);
slouken@3647
   293
slouken@3647
   294
#elif unix
slouken@3647
   295
    /* this is a little hacky. */
slouken@3647
   296
    for ( ; ; ) {
slouken@3647
   297
        char buf[32];
slouken@3647
   298
        fprintf(stderr, "Abort/Break/Retry/Ignore/AlwaysIgnore? [abriA] : ");
slouken@3647
   299
        fflush(stderr);
slouken@3647
   300
        if (fgets(buf, sizeof (buf), stdin) == NULL) {
slouken@3647
   301
            return SDL_ASSERTION_ABORT;
slouken@3647
   302
        }
slouken@3647
   303
slouken@3647
   304
        if (SDL_strcmp(buf, "a") == 0) {
slouken@3647
   305
            return SDL_ASSERTION_ABORT;
slouken@3647
   306
        } else if (SDL_strcmp(envr, "b") == 0) {
slouken@3647
   307
            return SDL_ASSERTION_BREAK;
slouken@3647
   308
        } else if (SDL_strcmp(envr, "r") == 0) {
slouken@3647
   309
            return SDL_ASSERTION_RETRY;
slouken@3647
   310
        } else if (SDL_strcmp(envr, "i") == 0) {
slouken@3647
   311
            return SDL_ASSERTION_IGNORE;
slouken@3647
   312
        } else if (SDL_strcmp(envr, "A") == 0) {
slouken@3647
   313
            return SDL_ASSERTION_ALWAYS_IGNORE;
slouken@3647
   314
        }
slouken@3647
   315
    }
slouken@3647
   316
slouken@3647
   317
#else
slouken@3647
   318
    #error Please define your platform or set SDL_ASSERT_LEVEL to 0.
slouken@3647
   319
#endif
slouken@3647
   320
slouken@3647
   321
    return SDL_ASSERTION_ABORT;
slouken@3647
   322
}
slouken@3647
   323
slouken@3647
   324
slouken@3647
   325
static SDL_mutex *assertion_mutex = NULL;
slouken@3647
   326
slouken@3647
   327
SDL_assert_state
slouken@3655
   328
SDL_ReportAssertion(SDL_assert_data *data, const char *func, const char *file,
slouken@3655
   329
                    int line)
slouken@3647
   330
{
slouken@3647
   331
    SDL_assert_state state;
slouken@3647
   332
slouken@3647
   333
    if (SDL_LockMutex(assertion_mutex) < 0) {
slouken@3647
   334
        return SDL_ASSERTION_IGNORE;   /* oh well, I guess. */
slouken@3647
   335
    }
slouken@3647
   336
slouken@3647
   337
    /* doing this because Visual C is upset over assigning in the macro. */
slouken@3647
   338
    if (data->trigger_count == 0) {
slouken@3647
   339
        data->function = func;
slouken@3655
   340
        data->filename = file;
slouken@3655
   341
        data->linenum = line;
slouken@3647
   342
    }
slouken@3647
   343
slouken@3647
   344
    SDL_AddAssertionToReport(data);
slouken@3647
   345
slouken@3647
   346
    data->trigger_count++;
slouken@3647
   347
    if (data->always_ignore) {
slouken@3647
   348
        SDL_UnlockMutex(assertion_mutex);
slouken@3647
   349
        return SDL_ASSERTION_IGNORE;
slouken@3647
   350
    }
slouken@3647
   351
slouken@3647
   352
    state = SDL_PromptAssertion(data);
slouken@3647
   353
slouken@3647
   354
    switch (state)
slouken@3647
   355
    {
slouken@3647
   356
        case SDL_ASSERTION_ABORT:
slouken@3647
   357
            SDL_UnlockMutex(assertion_mutex);  /* in case we assert in quit. */
slouken@3647
   358
            SDL_AbortAssertion();
slouken@3647
   359
            return SDL_ASSERTION_IGNORE;  /* shouldn't return, but oh well. */
slouken@3647
   360
slouken@3647
   361
        case SDL_ASSERTION_ALWAYS_IGNORE:
slouken@3647
   362
            state = SDL_ASSERTION_IGNORE;
slouken@3647
   363
            data->always_ignore = 1;
slouken@3647
   364
            break;
slouken@3647
   365
slouken@3647
   366
        case SDL_ASSERTION_IGNORE:
slouken@3647
   367
        case SDL_ASSERTION_RETRY:
slouken@3647
   368
        case SDL_ASSERTION_BREAK:
slouken@3647
   369
            break;  /* macro handles these. */
slouken@3647
   370
    }
slouken@3647
   371
slouken@3647
   372
    SDL_UnlockMutex(assertion_mutex);
slouken@3647
   373
slouken@3647
   374
    return state;
slouken@3647
   375
}
slouken@3647
   376
slouken@3647
   377
#endif  /* SDL_ASSERT_LEVEL > 0 */
slouken@3647
   378
slouken@3647
   379
slouken@3647
   380
int SDL_AssertionsInit(void)
slouken@3647
   381
{
slouken@3647
   382
#if (SDL_ASSERT_LEVEL > 0)
slouken@3647
   383
    assertion_mutex = SDL_CreateMutex();
slouken@3647
   384
    if (assertion_mutex == NULL) {
slouken@3647
   385
        return -1;
slouken@3647
   386
    }
slouken@3647
   387
#endif
slouken@3647
   388
    return 0;
slouken@3647
   389
}
slouken@3647
   390
slouken@3647
   391
void SDL_AssertionsQuit(void)
slouken@3647
   392
{
slouken@3647
   393
#if (SDL_ASSERT_LEVEL > 0)
slouken@3647
   394
    SDL_GenerateAssertionReport();
slouken@3647
   395
    SDL_DestroyMutex(assertion_mutex);
slouken@3647
   396
    assertion_mutex = NULL;
slouken@3647
   397
#endif
slouken@3647
   398
}
slouken@3647
   399
slouken@3647
   400
/* vi: set ts=4 sw=4 expandtab: */