Search
lxdream.org :: lxdream/src/util.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/util.c
changeset 1091:186558374345
prev1065:bc1cc0c54917
next1092:7c4ffe27e7b5
author nkeynes
date Tue Dec 15 08:46:37 2009 +1000 (12 years ago)
permissions -rw-r--r--
last change Add side-by-side x86+sh4 disassembly output
Print SH4 state information and disassembly of the current block when
crashing.
Fix delay slot instruction in conditional branch not being marked as a
delay-slot instruction in the branch-not-taken path.
Rename REG_* defines in cpu.h to avoid conflict with translation defs
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@495
    47
    snprintf( buf, 128, "gdb -batch -f --quiet --pid=%d -ex bt", getpid() );
nkeynes@495
    48
    system(buf);
nkeynes@495
    49
nkeynes@495
    50
    abort();
nkeynes@495
    51
}
nkeynes@495
    52
nkeynes@495
    53
void install_crash_handler(void)
nkeynes@495
    54
{
nkeynes@495
    55
    struct sigaction sa;
nkeynes@495
    56
nkeynes@495
    57
    sa.sa_sigaction = report_crash;
nkeynes@495
    58
    sigemptyset(&sa.sa_mask);
nkeynes@495
    59
    sa.sa_flags = SA_RESETHAND|SA_SIGINFO;
nkeynes@495
    60
    sigaction( SIGSEGV, &sa, NULL );
nkeynes@495
    61
}
nkeynes@495
    62
nkeynes@17
    63
nkeynes@422
    64
void fwrite_string( const char *s, FILE *f )
nkeynes@17
    65
{
nkeynes@17
    66
    uint32_t len = 0;
nkeynes@17
    67
    if( s == NULL ) {
nkeynes@736
    68
        fwrite( &len, sizeof(len), 1, f );
nkeynes@17
    69
    } else {
nkeynes@736
    70
        len = strlen(s)+1;
nkeynes@736
    71
        fwrite( &len, sizeof(len), 1, f );
nkeynes@736
    72
        fwrite( s, len, 1, f );
nkeynes@17
    73
    }
nkeynes@17
    74
}
nkeynes@17
    75
nkeynes@17
    76
int fread_string( char *s, int maxlen, FILE *f ) 
nkeynes@17
    77
{
nkeynes@17
    78
    uint32_t len;
nkeynes@17
    79
    fread( &len, sizeof(len), 1, f );
nkeynes@17
    80
    if( len != 0 ) {
nkeynes@736
    81
        fread( s, len > maxlen ? maxlen : len, 1, f );
nkeynes@17
    82
    }
nkeynes@17
    83
    return len;
nkeynes@17
    84
}
nkeynes@117
    85
nkeynes@1042
    86
int fwrite_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
    87
{
nkeynes@477
    88
    uLongf size = sz*count;
nkeynes@477
    89
    uLongf csize = ((int)(size*1.001))+13;
nkeynes@477
    90
    unsigned char *tmp = g_malloc0( csize );
nkeynes@477
    91
    int status = compress( tmp, &csize, p, size );
nkeynes@477
    92
    assert( status == Z_OK );
nkeynes@674
    93
    uint32_t wsize = (uint32_t)csize;
nkeynes@674
    94
    fwrite( &wsize, sizeof(wsize), 1, f );
nkeynes@1042
    95
    int written = fwrite( tmp, csize, 1, f );
nkeynes@477
    96
    g_free(tmp);
nkeynes@1042
    97
    
nkeynes@1042
    98
    /* Could be finer-grained, but this is enough to know it succeeded/failed */
nkeynes@1042
    99
    if( written == 1 ) {
nkeynes@1042
   100
        return count;
nkeynes@1042
   101
    } else {
nkeynes@1042
   102
        return 0;
nkeynes@1042
   103
    }
nkeynes@477
   104
}
nkeynes@477
   105
nkeynes@477
   106
int fread_gzip( void *p, size_t sz, size_t count, FILE *f )
nkeynes@477
   107
{
nkeynes@477
   108
    uLongf size = sz*count;
nkeynes@674
   109
    uint32_t csize;
nkeynes@477
   110
    unsigned char *tmp;
nkeynes@477
   111
nkeynes@477
   112
    fread( &csize, sizeof(csize), 1, f );
nkeynes@477
   113
    assert( csize <= (size*2) );
nkeynes@477
   114
    tmp = g_malloc0( csize );
nkeynes@477
   115
    fread( tmp, csize, 1, f );
nkeynes@477
   116
    int status = uncompress( p, &size, tmp, csize );
nkeynes@477
   117
    g_free(tmp);
nkeynes@477
   118
    if( status == Z_OK ) {
nkeynes@736
   119
        return count;
nkeynes@477
   120
    } else {
nkeynes@736
   121
        fprintf( stderr, "Error reading compressed data\n" );
nkeynes@736
   122
        return 0;
nkeynes@477
   123
    }
nkeynes@477
   124
}
nkeynes@477
   125
nkeynes@117
   126
void fwrite_dump( unsigned char *data, unsigned int length, FILE *f ) 
nkeynes@117
   127
{
nkeynes@117
   128
    unsigned int i, j;
nkeynes@117
   129
    for( i =0; i<length; i+=16 ) {
nkeynes@736
   130
        fprintf( f, "%08X:", i);
nkeynes@736
   131
        for( j=i; j<i+16; j++ ) {
nkeynes@736
   132
            if( (j % 4) == 0 )
nkeynes@736
   133
                fprintf( f, " " );
nkeynes@736
   134
            if( j < length )
nkeynes@736
   135
                fprintf( f, " %02X", (unsigned int)(data[j]) );
nkeynes@736
   136
            else
nkeynes@736
   137
                fprintf( f, "   " );
nkeynes@736
   138
        }
nkeynes@736
   139
        fprintf( f, "  " );
nkeynes@736
   140
        for( j=i; j<i+16 && j<length; j++ ) {
nkeynes@736
   141
            fprintf( f, "%c", isprint(data[j]) ? data[j] : '.' );
nkeynes@736
   142
        }
nkeynes@736
   143
        fprintf( f, "\n" );
nkeynes@117
   144
    }
nkeynes@117
   145
}
nkeynes@187
   146
nkeynes@187
   147
void fwrite_dump32( unsigned int *data, unsigned int length, FILE *f ) 
nkeynes@187
   148
{
nkeynes@220
   149
    fwrite_dump32v( data, length, 8, f );
nkeynes@220
   150
}
nkeynes@220
   151
nkeynes@220
   152
void fwrite_dump32v( unsigned int *data, unsigned int length, int wordsPerLine, FILE *f ) 
nkeynes@220
   153
{
nkeynes@187
   154
    unsigned int i, j;
nkeynes@220
   155
    for( i =0; i<length>>2; i+=wordsPerLine ) {
nkeynes@736
   156
        fprintf( f, "%08X:", i);
nkeynes@736
   157
        for( j=i; j<i+wordsPerLine; j++ ) {
nkeynes@736
   158
            if( j < length )
nkeynes@736
   159
                fprintf( f, " %08X", (unsigned int)(data[j]) );
nkeynes@736
   160
            else
nkeynes@736
   161
                fprintf( f, "         " );
nkeynes@736
   162
        }
nkeynes@736
   163
        fprintf( f, "\n" );
nkeynes@187
   164
    }
nkeynes@187
   165
}
nkeynes@437
   166
nkeynes@477
   167
gboolean write_png_to_stream( FILE *f, frame_buffer_t buffer )
nkeynes@477
   168
{
nkeynes@477
   169
    int coltype, i;
nkeynes@477
   170
    png_bytep p;
nkeynes@477
   171
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
nkeynes@477
   172
    if (!png_ptr) {
nkeynes@736
   173
        return FALSE;
nkeynes@477
   174
    }
nkeynes@477
   175
nkeynes@477
   176
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   177
    if (!info_ptr) {
nkeynes@736
   178
        png_destroy_write_struct(&png_ptr, NULL);
nkeynes@736
   179
        return FALSE;
nkeynes@477
   180
    }
nkeynes@736
   181
nkeynes@477
   182
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   183
        png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@736
   184
        return FALSE;
nkeynes@477
   185
    }
nkeynes@477
   186
    png_init_io( png_ptr, f );
nkeynes@477
   187
    switch( buffer->colour_format ) {
nkeynes@477
   188
    case COLFMT_BGR888:
nkeynes@736
   189
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   190
        break;
nkeynes@477
   191
    case COLFMT_BGRA8888:
nkeynes@736
   192
        coltype = PNG_COLOR_TYPE_RGB_ALPHA;
nkeynes@736
   193
        break;
nkeynes@477
   194
    case COLFMT_BGR0888:
nkeynes@736
   195
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@736
   196
        break;
nkeynes@477
   197
    default:
nkeynes@736
   198
        coltype = PNG_COLOR_TYPE_RGB;
nkeynes@477
   199
    }
nkeynes@477
   200
    png_set_IHDR(png_ptr, info_ptr, buffer->width, buffer->height,
nkeynes@736
   201
                 8, coltype, PNG_INTERLACE_NONE, 
nkeynes@736
   202
                 PNG_COMPRESSION_TYPE_DEFAULT, 
nkeynes@736
   203
                 PNG_FILTER_TYPE_DEFAULT );
nkeynes@477
   204
    png_write_info(png_ptr, info_ptr);
nkeynes@477
   205
    if( buffer->colour_format == COLFMT_BGR0888 ) {
nkeynes@736
   206
        png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
nkeynes@477
   207
    }
nkeynes@477
   208
    png_set_bgr(png_ptr);
nkeynes@477
   209
    if( buffer->inverted ) {
nkeynes@736
   210
        p = (png_bytep)(buffer->data + (buffer->height*buffer->rowstride) - buffer->rowstride);
nkeynes@736
   211
        for(i=0; i<buffer->height; i++ ) {
nkeynes@736
   212
            png_write_row(png_ptr, p);
nkeynes@736
   213
            p-=buffer->rowstride;
nkeynes@736
   214
        }
nkeynes@477
   215
    } else {
nkeynes@736
   216
        p = (png_bytep)buffer->data;
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
    }
nkeynes@477
   222
    png_write_end(png_ptr, info_ptr);
nkeynes@477
   223
    png_destroy_write_struct(&png_ptr, &info_ptr);
nkeynes@477
   224
    return TRUE;
nkeynes@477
   225
}
nkeynes@477
   226
nkeynes@477
   227
frame_buffer_t read_png_from_stream( FILE *f )
nkeynes@477
   228
{
nkeynes@477
   229
    png_bytep p;
nkeynes@477
   230
    int i;
nkeynes@477
   231
    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 
nkeynes@736
   232
            NULL, NULL, NULL);
nkeynes@477
   233
    if (!png_ptr) {
nkeynes@736
   234
        return NULL;
nkeynes@477
   235
    }
nkeynes@477
   236
nkeynes@477
   237
    png_infop info_ptr = png_create_info_struct(png_ptr);
nkeynes@477
   238
    if (!info_ptr) {
nkeynes@736
   239
        png_destroy_read_struct(&png_ptr, NULL, NULL);
nkeynes@736
   240
        return NULL;
nkeynes@477
   241
    }
nkeynes@736
   242
nkeynes@477
   243
    png_infop end_info = png_create_info_struct(png_ptr);
nkeynes@477
   244
    if (!end_info) {
nkeynes@736
   245
        png_destroy_read_struct(&png_ptr, &info_ptr, NULL );
nkeynes@736
   246
        return NULL;
nkeynes@477
   247
    }
nkeynes@477
   248
nkeynes@477
   249
    if( setjmp(png_jmpbuf(png_ptr)) ) {
nkeynes@736
   250
        png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@736
   251
        return NULL;
nkeynes@477
   252
    }
nkeynes@477
   253
nkeynes@477
   254
    png_init_io(png_ptr, f);
nkeynes@477
   255
    png_read_info(png_ptr, info_ptr);
nkeynes@736
   256
nkeynes@477
   257
    png_uint_32 width, height;
nkeynes@477
   258
    int bit_depth, color_type, interlace_type,
nkeynes@736
   259
    compression_type, filter_method;
nkeynes@477
   260
    png_get_IHDR(png_ptr, info_ptr, &width, &height,
nkeynes@736
   261
                 &bit_depth, &color_type, &interlace_type,
nkeynes@736
   262
                 &compression_type, &filter_method);
nkeynes@477
   263
    assert( interlace_type == PNG_INTERLACE_NONE );
nkeynes@477
   264
    int rowbytes = png_get_rowbytes(png_ptr, info_ptr);
nkeynes@477
   265
    int channels = png_get_channels(png_ptr, info_ptr);
nkeynes@477
   266
    frame_buffer_t buffer = g_malloc( sizeof(struct frame_buffer) + rowbytes*height );
nkeynes@502
   267
    buffer->data = (unsigned char *)(buffer+1);
nkeynes@477
   268
    buffer->width = width;
nkeynes@477
   269
    buffer->height = height;
nkeynes@477
   270
    buffer->rowstride = rowbytes;
nkeynes@477
   271
    buffer->address = -1;
nkeynes@477
   272
    buffer->size = rowbytes*height;
nkeynes@477
   273
    buffer->inverted = FALSE;
nkeynes@477
   274
    if( channels == 4 ) {
nkeynes@736
   275
        buffer->colour_format = COLFMT_BGRA8888;
nkeynes@477
   276
    } else if( channels == 3 ) {
nkeynes@736
   277
        buffer->colour_format = COLFMT_RGB888;
nkeynes@477
   278
    }
nkeynes@736
   279
nkeynes@481
   280
    p = (png_bytep)buffer->data;
nkeynes@477
   281
    for( i=0; i<height; i++ ) {
nkeynes@736
   282
        png_read_row(png_ptr, p, NULL );
nkeynes@736
   283
        p += rowbytes;
nkeynes@477
   284
    }
nkeynes@477
   285
nkeynes@477
   286
    png_read_end(png_ptr, end_info);
nkeynes@477
   287
    png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
nkeynes@477
   288
    return buffer;
nkeynes@477
   289
}
nkeynes@437
   290
nkeynes@556
   291
int get_log_level_from_string( const gchar *str )
nkeynes@556
   292
{
nkeynes@556
   293
    switch( tolower(str[0]) ) {
nkeynes@556
   294
    case 'd': return EMIT_DEBUG;
nkeynes@556
   295
    case 'e': return EMIT_ERR;
nkeynes@556
   296
    case 'f': return EMIT_FATAL;
nkeynes@556
   297
    case 'i': return EMIT_INFO;
nkeynes@556
   298
    case 't': return EMIT_TRACE;
nkeynes@556
   299
    case 'w': return EMIT_WARN;
nkeynes@556
   300
    default: return -1;
nkeynes@556
   301
    }
nkeynes@556
   302
}
nkeynes@556
   303
nkeynes@556
   304
gboolean set_global_log_level( const gchar *str ) 
nkeynes@556
   305
{
nkeynes@556
   306
    int l = get_log_level_from_string(str);
nkeynes@556
   307
    if( l == -1 ) {
nkeynes@736
   308
        return FALSE;
nkeynes@556
   309
    } else {
nkeynes@736
   310
        global_msg_level = l;
nkeynes@736
   311
        return TRUE;
nkeynes@556
   312
    }
nkeynes@556
   313
}
nkeynes@556
   314
nkeynes@437
   315
void log_message( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@437
   316
{
nkeynes@481
   317
    char buf[20];
nkeynes@437
   318
    time_t tm = time(NULL);
nkeynes@437
   319
    va_list ap;
nkeynes@437
   320
nkeynes@437
   321
    if( level > global_msg_level ) {
nkeynes@736
   322
        return; // ignored
nkeynes@437
   323
    }
nkeynes@447
   324
nkeynes@437
   325
    va_start(ap, msg);
nkeynes@535
   326
    gchar *text = g_strdup_vprintf( msg, ap );
nkeynes@535
   327
    va_end(ap);
nkeynes@736
   328
nkeynes@447
   329
    if( level <= EMIT_ERR ) {
nkeynes@736
   330
        if( gui_error_dialog( text ) ) {
nkeynes@736
   331
            g_free(text);
nkeynes@768
   332
            // If we're running, halt on error to avoid potentially flooding
nkeynes@768
   333
            // the user with error messages.
nkeynes@768
   334
            if( dreamcast_is_running() ) {
nkeynes@768
   335
                dreamcast_stop();
nkeynes@768
   336
            }
nkeynes@736
   337
            return;
nkeynes@736
   338
        }
nkeynes@447
   339
    }
nkeynes@447
   340
nkeynes@447
   341
nkeynes@437
   342
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@535
   343
    fprintf( stderr, "%s %08X %-5s %s\n", buf, sh4r.pc, msg_levels[level], text );
nkeynes@535
   344
    g_free(text);
nkeynes@437
   345
}
.