Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 1148:8e75fab17be8
prev1092:7c4ffe27e7b5
next1239:be3121267597
author nkeynes
date Wed Nov 10 08:23:05 2010 +1000 (11 years ago)
permissions -rw-r--r--
last change Add variant gdb invocation for OS X (which ships with a much older gdb).
TODO: detect gdb version at configure time and work off that instead
file annotate diff log raw
nkeynes@31
     1
/**
nkeynes@561
     2
 * $Id$
nkeynes@31
     3
 *
nkeynes@31
     4
 * Miscellaneous utility functions.
nkeynes@31
     5
 *
nkeynes@31
     6
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@31
     7
 *
nkeynes@31
     8
 * This program is free software; you can redistribute it and/or modify
nkeynes@31
     9
 * it under the terms of the GNU General Public License as published by
nkeynes@31
    10
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@31
    11
 * (at your option) any later version.
nkeynes@31
    12
 *
nkeynes@31
    13
 * This program is distributed in the hope that it will be useful,
nkeynes@31
    14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@31
    15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@31
    16
 * GNU General Public License for more details.
nkeynes@31
    17
 */
nkeynes@31
    18
nkeynes@495
    19
#define HAVE_EXECINFO_H 1
nkeynes@495
    20
nkeynes@477
    21
#include <assert.h>
nkeynes@425
    22
#include <ctype.h>
nkeynes@437
    23
#include <stdarg.h>
nkeynes@437
    24
#include <stdio.h>
nkeynes@437
    25
#include <stdlib.h>
nkeynes@495
    26
#include <signal.h>
nkeynes@437
    27
#include <time.h>
nkeynes@477
    28
#include <zlib.h>
nkeynes@477
    29
#include <glib.h>
nkeynes@477
    30
#include <png.h>
nkeynes@17
    31
#include "dream.h"
nkeynes@477
    32
#include "display.h"
nkeynes@768
    33
#include "dreamcast.h"
nkeynes@481
    34
#include "gui.h"
nkeynes@564
    35
#include "sh4/sh4.h"
nkeynes@437
    36
nkeynes@437
    37
char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
nkeynes@502
    38
int global_msg_level = EMIT_WARN;
nkeynes@495
    39
nkeynes@495
    40
static void report_crash( int signo, siginfo_t *info, void *ptr )
nkeynes@495
    41
{
nkeynes@495
    42
    char buf[128];
nkeynes@495
    43
nkeynes@495
    44
    fprintf( stderr, "--- Aborting with signal %d ---\n", signo );
nkeynes@1091
    45
    sh4_crashdump();
nkeynes@495
    46
    // Get gdb to print a nice backtrace for us
nkeynes@1148
    47
#ifdef APPLE_BUILD
nkeynes@1148
    48
    snprintf( buf, 128, "echo bt | gdb --quiet --pid=%d", getpid() );
nkeynes@1148
    49
#else
nkeynes@495
    50
    snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
nkeynes@1148
    51
#endif
nkeynes@495
    52
    system(buf);
nkeynes@495
    53
nkeynes@495
    54
    abort();
nkeynes@495
    55
}
nkeynes@495
    56
nkeynes@495
    57
void install_crash_handler(void)
nkeynes@495
    58
{
nkeynes@495
    59
    struct sigaction sa;
nkeynes@495
    60
nkeynes@495
    61
    sa.sa_sigaction = report_crash;
nkeynes@495
    62
    sigemptyset(&sa.sa_mask);
nkeynes@495
    63
    sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
nkeynes@495
    64
    sigaction( SIGSEGV, &sa, NULL );
nkeynes@1092
    65
    sigaction( SIGILL, &sa, NULL );
nkeynes@1092
    66
    sigaction( SIGBUS, &sa, NULL );
nkeynes@495
    67
}
nkeynes@495
    68
nkeynes@17
    69
nkeynes@422
    70
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    71
{
nkeynes@17
    72
    uint32_t len = 0;
nkeynes@17
    73
    if( s == NULL ) {
nkeynes@736
    74
        fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    75
    } else {
nkeynes@736
    76
        len = strlen(s)+1;
nkeynes@736
    77
        fwrite( &len, sizeof(len), 1, f );
nkeynes@736
    78
        fwrite( s, len, 1, f );
nkeynes@17
    79
    }
nkeynes@17
    80
}
nkeynes@17
    81
nkeynes@17
    82
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    83
{
nkeynes@17
    84
    uint32_t len;
nkeynes@17
    85
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    86
    if( len != 0 ) {
nkeynes@736
    87
        fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    88
    }
nkeynes@17
    89
    return len;
nkeynes@17
    90
}
nkeynes@117
    91
nkeynes@1042
    92
int fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
    93
{
nkeynes@477
    94
    uLongf size = sz*count;
nkeynes@477
    95
    uLongf csize = ((int)(size*1.001))+13;
nkeynes@477
    96
    unsigned char *tmp = g_malloc0( csize );
nkeynes@477
    97
    int status = compress( tmp, &csize, p, size );
nkeynes@477
    98
    assert( status == Z_OK );
nkeynes@674
    99
    uint32_t wsize = (uint32_t)csize;
nkeynes@674
   100
    fwrite( &wsize, sizeof(wsize), 1, f );
nkeynes@1042
   101
    int written = fwrite( tmp, csize, 1, f );
nkeynes@477
   102
    g_free(tmp);
nkeynes@1042
   103
    
nkeynes@1042
   104
    /* Could be finer-grained, but this is enough to know it succeeded/failed */
nkeynes@1042
   105
    if( written == 1 ) {
nkeynes@1042
   106
        return count;
nkeynes@1042
   107
    } else {
nkeynes@1042
   108
        return 0;
nkeynes@1042
   109
    }
nkeynes@477
   110
}
nkeynes@477
   111
nkeynes@477
   112
int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
   113
{
nkeynes@477
   114
    uLongf size = sz*count;
nkeynes@674
   115
    uint32_t csize;
nkeynes@477
   116
    unsigned char *tmp;
nkeynes@477
   117
nkeynes@477
   118
    fread( &csize, sizeof(csize), 1, f );
nkeynes@477
   119
    assert( csize <= (size*2) );
nkeynes@477
   120
    tmp = g_malloc0( csize );
nkeynes@477
   121
    fread( tmp, csize, 1, f );
nkeynes@477
   122
    int status = uncompress( p, &size, tmp, csize );
nkeynes@477
   123
    g_free(tmp);
nkeynes@477
   124
    if( status == Z_OK ) {
nkeynes@736
   125
        return count;
nkeynes@477
   126
    } else {
nkeynes@736
   127
        fprintf( stderr, "Error reading compressed data\n" );
nkeynes@736
   128
        return 0;
nkeynes@477
   129
    }
nkeynes@477
   130
}
nkeynes@477
   131
nkeynes@117
   132
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
   133
{
nkeynes@117
   134
    unsigned int i, j;
nkeynes@117
   135
    for( i =0; i<length; i+=16 ) {
nkeynes@736
   136
        fprintf( f, "%08X:", i);
nkeynes@736
   137
        for( j=i; j<i+16; j++ ) {
nkeynes@736
   138
            if( (j % 4) == 0 )
nkeynes@736
   139
                fprintf( f, " " );
nkeynes@736
   140
            if( j < length )
nkeynes@736
   141
                fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@736
   142
            else
nkeynes@736
   143
                fprintf( f, "   " );
nkeynes@736
   144
        }
nkeynes@736
   145
        fprintf( f, "  " );
nkeynes@736
   146
        for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@736
   147
            fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@736
   148
        }
nkeynes@736
   149
        fprintf( f, "\n" );
nkeynes@117
   150
    }
nkeynes@117
   151
}
nkeynes@187
   152
nkeynes@187
   153
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
   154
{
nkeynes@220
   155
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
   156
}
nkeynes@220
   157
nkeynes@220
   158
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
   159
{
nkeynes@187
   160
    unsigned int i, j;
nkeynes@220
   161
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@736
   162
        fprintf( f, "%08X:", i);
nkeynes@736
   163
        for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@736
   164
            if( j < length )
nkeynes@736
   165
                fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@736
   166
            else
nkeynes@736
   167
                fprintf( f, "         " );
nkeynes@736
   168
        }
nkeynes@736
   169
        fprintf( f, "\n" );
nkeynes@187
   170
    }
nkeynes@187
   171
}
nkeynes@437
   172
nkeynes@477
   173
gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
nkeynes@477
   174
{
nkeynes@477
   175
    int coltype, i;
nkeynes@477
   176
    png_bytep p;
nkeynes@477
   177
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
nkeynes@477
   178
    if (!png_ptr) {
nkeynes@736
   179
        return FALSE;
nkeynes@477
   180
    }
nkeynes@477
   181
nkeynes@477
   182
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   183
    if (!info_ptr) {
nkeynes@736
   184
        png_destroy_write_struct(&png_ptr, NULL);
nkeynes@736
   185
        return FALSE;
nkeynes@477
   186
    }
nkeynes@736
   187
nkeynes@477
   188
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   189
        png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@736
   190
        return FALSE;
nkeynes@477
   191
    }
nkeynes@477
   192
    png_init_io( png_ptr, f );
nkeynes@477
   193
    switch( buffer->colour_format ) {
nkeynes@477
   194
    case COLFMT_BGR888:
nkeynes@736
   195
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   196
        break;
nkeynes@477
   197
    case COLFMT_BGRA8888:
nkeynes@736
   198
        coltype = PNG_COLOR_TYPE_RGB_ALPHA;
nkeynes@736
   199
        break;
nkeynes@477
   200
    case COLFMT_BGR0888:
nkeynes@736
   201
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   202
        break;
nkeynes@477
   203
    default:
nkeynes@736
   204
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   205
    }
nkeynes@477
   206
    png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
nkeynes@736
   207
                 8, coltype, PNG_INTERLACE_NONE, 
nkeynes@736
   208
                 PNG_COMPRESSION_TYPE_DEFAULT, 
nkeynes@736
   209
                 PNG_FILTER_TYPE_DEFAULT );
nkeynes@477
   210
    png_write_info(png_ptr, info_ptr);
nkeynes@477
   211
    if( buffer->colour_format == COLFMT_BGR0888 ) {
nkeynes@736
   212
        png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
nkeynes@477
   213
    }
nkeynes@477
   214
    png_set_bgr(png_ptr);
nkeynes@477
   215
    if( buffer->inverted ) {
nkeynes@736
   216
        p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
nkeynes@736
   217
        for(i=0; i<buffer->height; i++ ) {
nkeynes@736
   218
            png_write_row(png_ptr, p);
nkeynes@736
   219
            p-=buffer->rowstride;
nkeynes@736
   220
        }
nkeynes@477
   221
    } else {
nkeynes@736
   222
        p = (png_bytep)buffer->data;
nkeynes@736
   223
        for(i=0; i<buffer->height; i++ ) {
nkeynes@736
   224
            png_write_row(png_ptr, p);
nkeynes@736
   225
            p+=buffer->rowstride;
nkeynes@736
   226
        }
nkeynes@477
   227
    }
nkeynes@477
   228
    png_write_end(png_ptr, info_ptr);
nkeynes@477
   229
    png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@477
   230
    return TRUE;
nkeynes@477
   231
}
nkeynes@477
   232
nkeynes@477
   233
frame_buffer_t read_png_from_stream( FILE *f )
nkeynes@477
   234
{
nkeynes@477
   235
    png_bytep p;
nkeynes@477
   236
    int i;
nkeynes@477
   237
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
nkeynes@736
   238
            NULL, NULL, NULL);
nkeynes@477
   239
    if (!png_ptr) {
nkeynes@736
   240
        return NULL;
nkeynes@477
   241
    }
nkeynes@477
   242
nkeynes@477
   243
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   244
    if (!info_ptr) {
nkeynes@736
   245
        png_destroy_read_struct(&png_ptr, NULL, NULL);
nkeynes@736
   246
        return NULL;
nkeynes@477
   247
    }
nkeynes@736
   248
nkeynes@477
   249
    png_infop end_info = png_create_info_struct(png_ptr);
nkeynes@477
   250
    if (!end_info) {
nkeynes@736
   251
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
nkeynes@736
   252
        return NULL;
nkeynes@477
   253
    }
nkeynes@477
   254
nkeynes@477
   255
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   256
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@736
   257
        return NULL;
nkeynes@477
   258
    }
nkeynes@477
   259
nkeynes@477
   260
    png_init_io(png_ptr, f);
nkeynes@477
   261
    png_read_info(png_ptr, info_ptr);
nkeynes@736
   262
nkeynes@477
   263
    png_uint_32 width, height;
nkeynes@477
   264
    int bit_depth, color_type, interlace_type,
nkeynes@736
   265
    compression_type, filter_method;
nkeynes@477
   266
    png_get_IHDR(png_ptr, info_ptr, &width, &height,
nkeynes@736
   267
                 &bit_depth, &color_type, &interlace_type,
nkeynes@736
   268
                 &compression_type, &filter_method);
nkeynes@477
   269
    assert( interlace_type == PNG_INTERLACE_NONE );
nkeynes@477
   270
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
nkeynes@477
   271
    int channels = png_get_channels(png_ptr, info_ptr);
nkeynes@477
   272
    frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
nkeynes@502
   273
    buffer->data = (unsigned char *)(buffer+1);
nkeynes@477
   274
    buffer->width = width;
nkeynes@477
   275
    buffer->height = height;
nkeynes@477
   276
    buffer->rowstride = rowbytes;
nkeynes@477
   277
    buffer->address = -1;
nkeynes@477
   278
    buffer->size = rowbytes*height;
nkeynes@477
   279
    buffer->inverted = FALSE;
nkeynes@477
   280
    if( channels == 4 ) {
nkeynes@736
   281
        buffer->colour_format = COLFMT_BGRA8888;
nkeynes@477
   282
    } else if( channels == 3 ) {
nkeynes@736
   283
        buffer->colour_format = COLFMT_RGB888;
nkeynes@477
   284
    }
nkeynes@736
   285
nkeynes@481
   286
    p = (png_bytep)buffer->data;
nkeynes@477
   287
    for( i=0; i<height; i++ ) {
nkeynes@736
   288
        png_read_row(png_ptr, p, NULL );
nkeynes@736
   289
        p += rowbytes;
nkeynes@477
   290
    }
nkeynes@477
   291
nkeynes@477
   292
    png_read_end(png_ptr, end_info);
nkeynes@477
   293
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@477
   294
    return buffer;
nkeynes@477
   295
}
nkeynes@437
   296
nkeynes@556
   297
int get_log_level_from_string( const gchar *str )
nkeynes@556
   298
{
nkeynes@556
   299
    switch( tolower(str[0]) ) {
nkeynes@556
   300
    case 'd': return EMIT_DEBUG;
nkeynes@556
   301
    case 'e': return EMIT_ERR;
nkeynes@556
   302
    case 'f': return EMIT_FATAL;
nkeynes@556
   303
    case 'i': return EMIT_INFO;
nkeynes@556
   304
    case 't': return EMIT_TRACE;
nkeynes@556
   305
    case 'w': return EMIT_WARN;
nkeynes@556
   306
    default: return -1;
nkeynes@556
   307
    }
nkeynes@556
   308
}
nkeynes@556
   309
nkeynes@556
   310
gboolean set_global_log_level( const gchar *str ) 
nkeynes@556
   311
{
nkeynes@556
   312
    int l = get_log_level_from_string(str);
nkeynes@556
   313
    if( l == -1 ) {
nkeynes@736
   314
        return FALSE;
nkeynes@556
   315
    } else {
nkeynes@736
   316
        global_msg_level = l;
nkeynes@736
   317
        return TRUE;
nkeynes@556
   318
    }
nkeynes@556
   319
}
nkeynes@556
   320
nkeynes@437
   321
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@437
   322
{
nkeynes@481
   323
    char buf[20];
nkeynes@437
   324
    time_t tm = time(NULL);
nkeynes@437
   325
    va_list ap;
nkeynes@437
   326
nkeynes@437
   327
    if( level > global_msg_level ) {
nkeynes@736
   328
        return; // ignored
nkeynes@437
   329
    }
nkeynes@447
   330
nkeynes@437
   331
    va_start(ap, msg);
nkeynes@535
   332
    gchar *text = g_strdup_vprintf( msg, ap );
nkeynes@535
   333
    va_end(ap);
nkeynes@736
   334
nkeynes@447
   335
    if( level <= EMIT_ERR ) {
nkeynes@736
   336
        if( gui_error_dialog( text ) ) {
nkeynes@736
   337
            g_free(text);
nkeynes@768
   338
            // If we're running, halt on error to avoid potentially flooding
nkeynes@768
   339
            // the user with error messages.
nkeynes@768
   340
            if( dreamcast_is_running() ) {
nkeynes@768
   341
                dreamcast_stop();
nkeynes@768
   342
            }
nkeynes@736
   343
            return;
nkeynes@736
   344
        }
nkeynes@447
   345
    }
nkeynes@447
   346
nkeynes@447
   347
nkeynes@437
   348
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@535
   349
    fprintf( stderr, "%s %08X %-5s %s\n", buf, sh4r.pc, msg_levels[level], text );
nkeynes@535
   350
    g_free(text);
nkeynes@437
   351
}
.