Search
lxdream.org :: lxdream/src/gui/debug_win.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gui/debug_win.c
changeset 392:39e596b3b6dd
prev144:7f0714e89aaa
next415:5e57f29bfd4d
author nkeynes
date Tue Sep 18 10:48:57 2007 +0000 (14 years ago)
permissions -rw-r--r--
last change Remove debugger from default startup - now only shown if started with -D
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@392
     2
 * $Id: debug_win.c,v 1.20 2007-09-18 10:48:57 nkeynes Exp $
nkeynes@10
     3
 * This file is responsible for the main debugger gui frame.
nkeynes@10
     4
 *
nkeynes@10
     5
 * Copyright (c) 2005 Nathan Keynes.
nkeynes@10
     6
 *
nkeynes@10
     7
 * This program is free software; you can redistribute it and/or modify
nkeynes@10
     8
 * it under the terms of the GNU General Public License as published by
nkeynes@10
     9
 * the Free Software Foundation; either version 2 of the License, or
nkeynes@10
    10
 * (at your option) any later version.
nkeynes@10
    11
 *
nkeynes@10
    12
 * This program is distributed in the hope that it will be useful,
nkeynes@10
    13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
nkeynes@10
    14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
nkeynes@10
    15
 * GNU General Public License for more details.
nkeynes@10
    16
 */
nkeynes@2
    17
#include <stdlib.h>
nkeynes@2
    18
#include <stdarg.h>
nkeynes@69
    19
#include <stdio.h>
nkeynes@2
    20
#include <gnome.h>
nkeynes@2
    21
#include <math.h>
nkeynes@392
    22
#include "sh4/sh4dasm.h"
nkeynes@43
    23
#include "gui/gui.h"
nkeynes@2
    24
#include "mem.h"
nkeynes@30
    25
#include "cpu.h"
nkeynes@144
    26
#include "display.h"
nkeynes@2
    27
nkeynes@2
    28
GdkColor *msg_colors[] = { &clrError, &clrError, &clrWarn, &clrNormal,
nkeynes@2
    29
                           &clrDebug, &clrTrace };
nkeynes@392
    30
char *msg_levels[] = { "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE" };
nkeynes@2
    31
nkeynes@45
    32
void init_register_list( debug_info_t data );
nkeynes@45
    33
nkeynes@9
    34
struct debug_info_struct {
nkeynes@9
    35
    int disasm_from;
nkeynes@9
    36
    int disasm_to;
nkeynes@9
    37
    int disasm_pc;
nkeynes@9
    38
    struct cpu_desc_struct *cpu;
nkeynes@392
    39
    const cpu_desc_t *cpu_list;
nkeynes@9
    40
    GtkCList *msgs_list;
nkeynes@9
    41
    GtkCList *regs_list;
nkeynes@9
    42
    GtkCList *disasm_list;
nkeynes@9
    43
    GtkEntry *page_field;
nkeynes@28
    44
    GtkWidget *win;
nkeynes@9
    45
    GtkProgressBar *icounter;
nkeynes@9
    46
    char icounter_text[16];
nkeynes@9
    47
    char saved_regs[0];
nkeynes@8
    48
};
nkeynes@8
    49
nkeynes@30
    50
debug_info_t init_debug_win(GtkWidget *win, const cpu_desc_t *cpu_list )
nkeynes@2
    51
{
nkeynes@2
    52
    GnomeAppBar *appbar;
nkeynes@9
    53
    
nkeynes@11
    54
    debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
nkeynes@9
    55
    data->disasm_from = -1;
nkeynes@9
    56
    data->disasm_to = -1;
nkeynes@9
    57
    data->disasm_pc = -1;
nkeynes@11
    58
    data->cpu = cpu_list[0];
nkeynes@11
    59
    data->cpu_list = cpu_list;
nkeynes@9
    60
    
nkeynes@9
    61
    data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
nkeynes@28
    62
    data->win = win;
nkeynes@9
    63
    gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
nkeynes@11
    64
    init_register_list( data );
nkeynes@9
    65
    data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
nkeynes@9
    66
    data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
nkeynes@9
    67
    gtk_clist_set_column_width( data->disasm_list, 1, 16 );
nkeynes@9
    68
    data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
nkeynes@2
    69
nkeynes@2
    70
    appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
nkeynes@9
    71
    data->icounter = gnome_appbar_get_progress( appbar );
nkeynes@9
    72
    gtk_progress_bar_set_text(data->icounter, "1");
nkeynes@10
    73
    
nkeynes@9
    74
    gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
nkeynes@29
    75
    set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@28
    76
    debug_win_set_running( data, FALSE );
nkeynes@9
    77
    return data;
nkeynes@2
    78
}
nkeynes@2
    79
nkeynes@11
    80
void init_register_list( debug_info_t data ) 
nkeynes@11
    81
{
nkeynes@11
    82
    int i;
nkeynes@11
    83
    char buf[20];
nkeynes@11
    84
    char *arr[2];
nkeynes@11
    85
nkeynes@11
    86
    gtk_clist_clear( data->regs_list );
nkeynes@11
    87
    arr[1] = buf;
nkeynes@11
    88
    for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
nkeynes@11
    89
        arr[0] = data->cpu->regs_info[i].name;
nkeynes@11
    90
        if( data->cpu->regs_info->type == REG_INT )
nkeynes@11
    91
            sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
nkeynes@11
    92
        else
nkeynes@11
    93
            sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
nkeynes@11
    94
        gtk_clist_append( data->regs_list, arr );
nkeynes@11
    95
    }
nkeynes@11
    96
}
nkeynes@11
    97
nkeynes@2
    98
/*
nkeynes@2
    99
 * Check for changed registers and update the display
nkeynes@2
   100
 */
nkeynes@9
   101
void update_registers( debug_info_t data )
nkeynes@2
   102
{
nkeynes@2
   103
    int i;
nkeynes@9
   104
    for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
nkeynes@9
   105
        if( data->cpu->regs_info[i].type == REG_INT ) {
nkeynes@2
   106
            /* Yes this _is_ probably fairly evil */
nkeynes@9
   107
            if( *((uint32_t *)data->cpu->regs_info[i].value) !=
nkeynes@9
   108
                *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
nkeynes@2
   109
                char buf[20];
nkeynes@9
   110
                sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
nkeynes@9
   111
                gtk_clist_set_text( data->regs_list, i, 1, buf );
nkeynes@9
   112
                gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
nkeynes@2
   113
            } else {
nkeynes@9
   114
                gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
nkeynes@2
   115
            }
nkeynes@2
   116
        } else {
nkeynes@9
   117
            if( *((float *)data->cpu->regs_info[i].value) !=
nkeynes@9
   118
                *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
nkeynes@2
   119
                char buf[20];
nkeynes@9
   120
                sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
nkeynes@9
   121
                gtk_clist_set_text( data->regs_list, i, 1, buf );
nkeynes@9
   122
                gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
nkeynes@2
   123
            } else {
nkeynes@9
   124
                gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
nkeynes@2
   125
            }
nkeynes@2
   126
        }
nkeynes@2
   127
    }
nkeynes@9
   128
nkeynes@9
   129
    set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@9
   130
    memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
nkeynes@2
   131
}
nkeynes@2
   132
nkeynes@9
   133
void update_icount( debug_info_t data )
nkeynes@2
   134
{
nkeynes@392
   135
    if( data != NULL ) {
nkeynes@392
   136
	//    sprintf( data->icounter_text, "%d", *data->cpu->icount );
nkeynes@392
   137
	sprintf( data->icounter_text, "%d", pvr2_get_frame_count() );
nkeynes@392
   138
	gtk_progress_bar_set_text( data->icounter, data->icounter_text );
nkeynes@392
   139
    }
nkeynes@2
   140
}
nkeynes@2
   141
nkeynes@9
   142
void set_disassembly_region( debug_info_t data, unsigned int page )
nkeynes@2
   143
{
nkeynes@11
   144
    uint32_t i, posn, next;
nkeynes@2
   145
    char buf[80];
nkeynes@2
   146
    char addr[10];
nkeynes@11
   147
    char opcode[16] = "";
nkeynes@2
   148
    char *arr[4] = { addr, " ", opcode, buf };
nkeynes@2
   149
    unsigned int from = page & 0xFFFFF000;
nkeynes@2
   150
    unsigned int to = from + 4096;
nkeynes@2
   151
    
nkeynes@9
   152
    gtk_clist_clear(data->disasm_list);
nkeynes@2
   153
nkeynes@2
   154
    sprintf( addr, "%08X", from );
nkeynes@9
   155
    gtk_entry_set_text( data->page_field, addr );
nkeynes@2
   156
nkeynes@14
   157
    if( !data->cpu->is_valid_page_func( from ) ) {
nkeynes@2
   158
        arr[3] = "This page is currently unmapped";
nkeynes@9
   159
        gtk_clist_append( data->disasm_list, arr );
nkeynes@9
   160
        gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
nkeynes@2
   161
    } else {
nkeynes@11
   162
        for( i=from; i<to; i = next ) {
nkeynes@11
   163
	    next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
nkeynes@2
   164
            sprintf( addr, "%08X", i );
nkeynes@9
   165
            posn = gtk_clist_append( data->disasm_list, arr );
nkeynes@2
   166
            if( buf[0] == '?' )
nkeynes@9
   167
                gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
nkeynes@43
   168
	    if( data->cpu->get_breakpoint != NULL ) {
nkeynes@43
   169
		int type = data->cpu->get_breakpoint( i );
nkeynes@43
   170
		switch(type) {
nkeynes@43
   171
		case BREAK_ONESHOT:
nkeynes@43
   172
		    gtk_clist_set_background( data->disasm_list, posn, &clrTempBreak );
nkeynes@43
   173
		    break;
nkeynes@43
   174
		case BREAK_KEEP:
nkeynes@43
   175
		    gtk_clist_set_background( data->disasm_list, posn, &clrBreak );
nkeynes@43
   176
		    break;
nkeynes@43
   177
		}
nkeynes@43
   178
	    }
nkeynes@2
   179
        }
nkeynes@9
   180
        if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
nkeynes@9
   181
            gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
nkeynes@2
   182
                                      &clrPC );
nkeynes@2
   183
    }
nkeynes@2
   184
nkeynes@2
   185
    if( page != from ) { /* not a page boundary */
nkeynes@9
   186
        gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
nkeynes@2
   187
    }
nkeynes@9
   188
    data->disasm_from = from;
nkeynes@9
   189
    data->disasm_to = to;
nkeynes@2
   190
}
nkeynes@2
   191
nkeynes@9
   192
void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
nkeynes@2
   193
{
nkeynes@2
   194
    int row;
nkeynes@2
   195
    
nkeynes@9
   196
    if( addr < data->disasm_from || addr >= data->disasm_to )
nkeynes@9
   197
        set_disassembly_region(data,addr);
nkeynes@2
   198
nkeynes@9
   199
    row = address_to_row( data, addr );
nkeynes@2
   200
    if(select) {
nkeynes@9
   201
        gtk_clist_select_row( data->disasm_list, row, 0 );
nkeynes@2
   202
    }
nkeynes@9
   203
    if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
nkeynes@9
   204
        gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
nkeynes@2
   205
    }
nkeynes@2
   206
}
nkeynes@2
   207
nkeynes@9
   208
void jump_to_pc( debug_info_t data, gboolean select )
nkeynes@9
   209
{
nkeynes@9
   210
    jump_to_disassembly( data, *data->cpu->pc, select );
nkeynes@9
   211
}
nkeynes@9
   212
nkeynes@9
   213
void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
nkeynes@2
   214
{
nkeynes@2
   215
    int row;
nkeynes@2
   216
    
nkeynes@9
   217
    jump_to_disassembly( data, pc, select );
nkeynes@9
   218
    if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
nkeynes@9
   219
	data->disasm_pc < data->disasm_to )
nkeynes@9
   220
        gtk_clist_set_foreground( data->disasm_list, 
nkeynes@9
   221
				  (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
nkeynes@2
   222
                                  &clrNormal );
nkeynes@9
   223
    row = address_to_row( data, pc );
nkeynes@9
   224
    gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
nkeynes@9
   225
    data->disasm_pc = pc;
nkeynes@2
   226
}
nkeynes@2
   227
nkeynes@26
   228
void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
nkeynes@10
   229
{
nkeynes@11
   230
    int i;
nkeynes@11
   231
    for( i=0; data->cpu_list[i] != NULL; i++ ) {
nkeynes@11
   232
	if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
nkeynes@11
   233
	    if( data->cpu != data->cpu_list[i] ) {
nkeynes@11
   234
		data->cpu = data->cpu_list[i];
nkeynes@69
   235
		data->disasm_from = data->disasm_to = -1; /* Force reload */
nkeynes@30
   236
		set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@11
   237
		init_register_list( data );
nkeynes@33
   238
		update_icount( data );
nkeynes@11
   239
	    }
nkeynes@11
   240
	    return;
nkeynes@11
   241
	}
nkeynes@11
   242
    }
nkeynes@10
   243
}
nkeynes@10
   244
nkeynes@45
   245
void debug_win_toggle_breakpoint( debug_info_t data, int row )
nkeynes@43
   246
{
nkeynes@43
   247
    uint32_t pc = row_to_address( data, row );
nkeynes@43
   248
    int oldType = data->cpu->get_breakpoint( pc );
nkeynes@43
   249
    if( oldType != BREAK_NONE ) {
nkeynes@43
   250
	data->cpu->clear_breakpoint( pc, oldType );
nkeynes@45
   251
	gtk_clist_set_background( data->disasm_list, row, &clrWhite );
nkeynes@43
   252
    } else {
nkeynes@45
   253
	data->cpu->set_breakpoint( pc, BREAK_KEEP );
nkeynes@45
   254
	gtk_clist_set_background( data->disasm_list, row, &clrBreak );
nkeynes@43
   255
    }
nkeynes@45
   256
}
nkeynes@45
   257
nkeynes@45
   258
void debug_win_set_oneshot_breakpoint( debug_info_t data, int row )
nkeynes@45
   259
{
nkeynes@45
   260
    uint32_t pc = row_to_address( data, row );
nkeynes@45
   261
    data->cpu->clear_breakpoint( pc, BREAK_ONESHOT );
nkeynes@45
   262
    data->cpu->set_breakpoint( pc, BREAK_ONESHOT );
nkeynes@45
   263
    gtk_clist_set_background( data->disasm_list, row, &clrTempBreak );
nkeynes@43
   264
}
nkeynes@43
   265
nkeynes@30
   266
/**
nkeynes@30
   267
 * Execute a single instruction using the current CPU mode.
nkeynes@30
   268
 */
nkeynes@30
   269
void debug_win_single_step( debug_info_t data )
nkeynes@30
   270
{
nkeynes@30
   271
    data->cpu->step_func();
nkeynes@30
   272
    gtk_gui_update();
nkeynes@30
   273
}
nkeynes@30
   274
nkeynes@9
   275
uint32_t row_to_address( debug_info_t data, int row ) {
nkeynes@9
   276
    return data->cpu->instr_size * row + data->disasm_from;
nkeynes@9
   277
}
nkeynes@2
   278
nkeynes@9
   279
int address_to_row( debug_info_t data, uint32_t address ) {
nkeynes@9
   280
    if( data->disasm_from > address || data->disasm_to <= address )
nkeynes@9
   281
	return -1;
nkeynes@9
   282
    return (address - data->disasm_from) / data->cpu->instr_size;
nkeynes@9
   283
}
nkeynes@9
   284
nkeynes@10
   285
nkeynes@35
   286
void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@2
   287
{
nkeynes@2
   288
    char buf[20], addr[10] = "", *p;
nkeynes@35
   289
    const char *arr[4] = {buf, source, addr};
nkeynes@2
   290
    int posn;
nkeynes@2
   291
    time_t tm = time(NULL);
nkeynes@2
   292
    va_list ap;
nkeynes@9
   293
    debug_info_t data;
nkeynes@9
   294
    if( ptr == NULL )
nkeynes@9
   295
	data = main_debug;
nkeynes@9
   296
    else data = (debug_info_t)ptr;
nkeynes@69
   297
    va_start(ap, msg);
nkeynes@2
   298
nkeynes@392
   299
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@392
   300
nkeynes@69
   301
    if( data == NULL ) {
nkeynes@392
   302
	fprintf( stderr, "%s %08X %-5s ", buf, *sh4_cpu_desc.pc, msg_levels[level] );
nkeynes@69
   303
	vfprintf( stderr, msg, ap );
nkeynes@97
   304
	fprintf( stderr, "\n" );
nkeynes@69
   305
	va_end(ap);
nkeynes@69
   306
	return;
nkeynes@69
   307
    }
nkeynes@69
   308
nkeynes@2
   309
    p = g_strdup_vprintf( msg, ap );
nkeynes@35
   310
    sprintf( addr, "%08X", *data->cpu->pc );
nkeynes@35
   311
    arr[3] = p;
nkeynes@9
   312
    posn = gtk_clist_append(data->msgs_list, arr);
nkeynes@2
   313
    free(p);
nkeynes@2
   314
    va_end(ap);
nkeynes@2
   315
nkeynes@9
   316
    gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
nkeynes@9
   317
    gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
nkeynes@2
   318
nkeynes@2
   319
    /* emit _really_ slows down the emu, to the point where the gui can be
nkeynes@2
   320
     * completely unresponsive if I don't include this:
nkeynes@2
   321
     */
nkeynes@2
   322
    while( gtk_events_pending() )
nkeynes@2
   323
        gtk_main_iteration();
nkeynes@2
   324
}
nkeynes@9
   325
nkeynes@9
   326
debug_info_t get_debug_info( GtkWidget *widget ) {
nkeynes@9
   327
    
nkeynes@9
   328
    GtkWidget *win = gtk_widget_get_toplevel(widget);
nkeynes@9
   329
    debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
nkeynes@9
   330
    return data;
nkeynes@9
   331
}
nkeynes@9
   332
nkeynes@28
   333
void debug_win_enable_widget( debug_info_t data, const char *name, 
nkeynes@28
   334
			      gboolean enabled )
nkeynes@28
   335
{
nkeynes@28
   336
    GtkWidget *widget = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(data->win), name));
nkeynes@28
   337
    gtk_widget_set_sensitive( widget, enabled );
nkeynes@28
   338
}    
nkeynes@28
   339
nkeynes@28
   340
void debug_win_set_running( debug_info_t data, gboolean isRunning ) 
nkeynes@28
   341
{
nkeynes@392
   342
    if( data != NULL ) {
nkeynes@392
   343
	debug_win_enable_widget( data, "stop_btn", isRunning );
nkeynes@392
   344
	debug_win_enable_widget( data, "step_btn", !isRunning );
nkeynes@392
   345
	debug_win_enable_widget( data, "run_btn", !isRunning );
nkeynes@392
   346
	debug_win_enable_widget( data, "runto_btn", !isRunning );
nkeynes@392
   347
    }
nkeynes@28
   348
}
.