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 45:f99236f0632e
prev43:0cf3e339cc59
next69:2e8634272a98
author nkeynes
date Tue Dec 27 08:41:22 2005 +0000 (14 years ago)
permissions -rw-r--r--
last change Fix output list click
Fix mmr column widths
Fix run-to for ARM
file annotate diff log raw
nkeynes@10
     1
/**
nkeynes@45
     2
 * $Id: debug_win.c,v 1.14 2005-12-27 08:41:22 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@2
    19
#include <gnome.h>
nkeynes@2
    20
#include <math.h>
nkeynes@43
    21
#include "gui/gui.h"
nkeynes@2
    22
#include "mem.h"
nkeynes@30
    23
#include "cpu.h"
nkeynes@2
    24
nkeynes@2
    25
GdkColor *msg_colors[] = { &clrError, &clrError, &clrWarn, &clrNormal,
nkeynes@2
    26
                           &clrDebug, &clrTrace };
nkeynes@2
    27
nkeynes@45
    28
void init_register_list( debug_info_t data );
nkeynes@45
    29
nkeynes@9
    30
struct debug_info_struct {
nkeynes@9
    31
    int disasm_from;
nkeynes@9
    32
    int disasm_to;
nkeynes@9
    33
    int disasm_pc;
nkeynes@9
    34
    struct cpu_desc_struct *cpu;
nkeynes@11
    35
    struct cpu_desc_struct **cpu_list;
nkeynes@9
    36
    GtkCList *msgs_list;
nkeynes@9
    37
    GtkCList *regs_list;
nkeynes@9
    38
    GtkCList *disasm_list;
nkeynes@9
    39
    GtkEntry *page_field;
nkeynes@28
    40
    GtkWidget *win;
nkeynes@9
    41
    GtkProgressBar *icounter;
nkeynes@9
    42
    char icounter_text[16];
nkeynes@9
    43
    char saved_regs[0];
nkeynes@8
    44
};
nkeynes@8
    45
nkeynes@30
    46
debug_info_t init_debug_win(GtkWidget *win, const cpu_desc_t *cpu_list )
nkeynes@2
    47
{
nkeynes@2
    48
    GnomeAppBar *appbar;
nkeynes@9
    49
    
nkeynes@11
    50
    debug_info_t data = g_malloc0( sizeof(struct debug_info_struct) + cpu_list[0]->regs_size );
nkeynes@9
    51
    data->disasm_from = -1;
nkeynes@9
    52
    data->disasm_to = -1;
nkeynes@9
    53
    data->disasm_pc = -1;
nkeynes@11
    54
    data->cpu = cpu_list[0];
nkeynes@11
    55
    data->cpu_list = cpu_list;
nkeynes@9
    56
    
nkeynes@9
    57
    data->regs_list= gtk_object_get_data(GTK_OBJECT(win), "reg_list");
nkeynes@28
    58
    data->win = win;
nkeynes@9
    59
    gtk_widget_modify_font( GTK_WIDGET(data->regs_list), fixed_list_font );
nkeynes@11
    60
    init_register_list( data );
nkeynes@9
    61
    data->msgs_list = gtk_object_get_data(GTK_OBJECT(win), "output_list");
nkeynes@9
    62
    data->disasm_list = gtk_object_get_data(GTK_OBJECT(win), "disasm_list");
nkeynes@9
    63
    gtk_clist_set_column_width( data->disasm_list, 1, 16 );
nkeynes@9
    64
    data->page_field = gtk_object_get_data(GTK_OBJECT(win), "page_field");
nkeynes@2
    65
nkeynes@2
    66
    appbar = gtk_object_get_data(GTK_OBJECT(win), "debug_appbar");
nkeynes@9
    67
    data->icounter = gnome_appbar_get_progress( appbar );
nkeynes@9
    68
    gtk_progress_bar_set_text(data->icounter, "1");
nkeynes@10
    69
    
nkeynes@9
    70
    gtk_object_set_data( GTK_OBJECT(win), "debug_data", data );
nkeynes@29
    71
    set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@28
    72
    debug_win_set_running( data, FALSE );
nkeynes@9
    73
    return data;
nkeynes@2
    74
}
nkeynes@2
    75
nkeynes@11
    76
void init_register_list( debug_info_t data ) 
nkeynes@11
    77
{
nkeynes@11
    78
    int i;
nkeynes@11
    79
    char buf[20];
nkeynes@11
    80
    char *arr[2];
nkeynes@11
    81
nkeynes@11
    82
    gtk_clist_clear( data->regs_list );
nkeynes@11
    83
    arr[1] = buf;
nkeynes@11
    84
    for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
nkeynes@11
    85
        arr[0] = data->cpu->regs_info[i].name;
nkeynes@11
    86
        if( data->cpu->regs_info->type == REG_INT )
nkeynes@11
    87
            sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
nkeynes@11
    88
        else
nkeynes@11
    89
            sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
nkeynes@11
    90
        gtk_clist_append( data->regs_list, arr );
nkeynes@11
    91
    }
nkeynes@11
    92
}
nkeynes@11
    93
nkeynes@2
    94
/*
nkeynes@2
    95
 * Check for changed registers and update the display
nkeynes@2
    96
 */
nkeynes@9
    97
void update_registers( debug_info_t data )
nkeynes@2
    98
{
nkeynes@2
    99
    int i;
nkeynes@9
   100
    for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
nkeynes@9
   101
        if( data->cpu->regs_info[i].type == REG_INT ) {
nkeynes@2
   102
            /* Yes this _is_ probably fairly evil */
nkeynes@9
   103
            if( *((uint32_t *)data->cpu->regs_info[i].value) !=
nkeynes@9
   104
                *((uint32_t *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
nkeynes@2
   105
                char buf[20];
nkeynes@9
   106
                sprintf( buf, "%08X", *((uint32_t *)data->cpu->regs_info[i].value) );
nkeynes@9
   107
                gtk_clist_set_text( data->regs_list, i, 1, buf );
nkeynes@9
   108
                gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
nkeynes@2
   109
            } else {
nkeynes@9
   110
                gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
nkeynes@2
   111
            }
nkeynes@2
   112
        } else {
nkeynes@9
   113
            if( *((float *)data->cpu->regs_info[i].value) !=
nkeynes@9
   114
                *((float *)((char *)data->saved_regs + ((char *)data->cpu->regs_info[i].value - (char *)data->cpu->regs))) ) {
nkeynes@2
   115
                char buf[20];
nkeynes@9
   116
                sprintf( buf, "%f", *((float *)data->cpu->regs_info[i].value) );
nkeynes@9
   117
                gtk_clist_set_text( data->regs_list, i, 1, buf );
nkeynes@9
   118
                gtk_clist_set_foreground( data->regs_list, i, &clrChanged );
nkeynes@2
   119
            } else {
nkeynes@9
   120
                gtk_clist_set_foreground( data->regs_list, i, &clrNormal );
nkeynes@2
   121
            }
nkeynes@2
   122
        }
nkeynes@2
   123
    }
nkeynes@9
   124
nkeynes@9
   125
    set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@9
   126
    memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
nkeynes@2
   127
}
nkeynes@2
   128
nkeynes@9
   129
void update_icount( debug_info_t data )
nkeynes@2
   130
{
nkeynes@9
   131
    sprintf( data->icounter_text, "%d", *data->cpu->icount );
nkeynes@9
   132
    gtk_progress_bar_set_text( data->icounter, data->icounter_text );
nkeynes@2
   133
}
nkeynes@2
   134
nkeynes@9
   135
void set_disassembly_region( debug_info_t data, unsigned int page )
nkeynes@2
   136
{
nkeynes@11
   137
    uint32_t i, posn, next;
nkeynes@2
   138
    uint16_t op;
nkeynes@2
   139
    char buf[80];
nkeynes@2
   140
    char addr[10];
nkeynes@11
   141
    char opcode[16] = "";
nkeynes@2
   142
    char *arr[4] = { addr, " ", opcode, buf };
nkeynes@2
   143
    unsigned int from = page & 0xFFFFF000;
nkeynes@2
   144
    unsigned int to = from + 4096;
nkeynes@2
   145
    
nkeynes@9
   146
    gtk_clist_clear(data->disasm_list);
nkeynes@2
   147
nkeynes@2
   148
    sprintf( addr, "%08X", from );
nkeynes@9
   149
    gtk_entry_set_text( data->page_field, addr );
nkeynes@2
   150
nkeynes@14
   151
    if( !data->cpu->is_valid_page_func( from ) ) {
nkeynes@2
   152
        arr[3] = "This page is currently unmapped";
nkeynes@9
   153
        gtk_clist_append( data->disasm_list, arr );
nkeynes@9
   154
        gtk_clist_set_foreground( data->disasm_list, 0, &clrError );
nkeynes@2
   155
    } else {
nkeynes@11
   156
        for( i=from; i<to; i = next ) {
nkeynes@11
   157
	    next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
nkeynes@2
   158
            sprintf( addr, "%08X", i );
nkeynes@10
   159
            op = sh4_read_phys_word(i);
nkeynes@9
   160
            posn = gtk_clist_append( data->disasm_list, arr );
nkeynes@2
   161
            if( buf[0] == '?' )
nkeynes@9
   162
                gtk_clist_set_foreground( data->disasm_list, posn, &clrWarn );
nkeynes@43
   163
	    if( data->cpu->get_breakpoint != NULL ) {
nkeynes@43
   164
		int type = data->cpu->get_breakpoint( i );
nkeynes@43
   165
		switch(type) {
nkeynes@43
   166
		case BREAK_ONESHOT:
nkeynes@43
   167
		    gtk_clist_set_background( data->disasm_list, posn, &clrTempBreak );
nkeynes@43
   168
		    break;
nkeynes@43
   169
		case BREAK_KEEP:
nkeynes@43
   170
		    gtk_clist_set_background( data->disasm_list, posn, &clrBreak );
nkeynes@43
   171
		    break;
nkeynes@43
   172
		}
nkeynes@43
   173
	    }
nkeynes@2
   174
        }
nkeynes@9
   175
        if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
nkeynes@9
   176
            gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
nkeynes@2
   177
                                      &clrPC );
nkeynes@2
   178
    }
nkeynes@2
   179
nkeynes@2
   180
    if( page != from ) { /* not a page boundary */
nkeynes@9
   181
        gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
nkeynes@2
   182
    }
nkeynes@9
   183
    data->disasm_from = from;
nkeynes@9
   184
    data->disasm_to = to;
nkeynes@2
   185
}
nkeynes@2
   186
nkeynes@9
   187
void jump_to_disassembly( debug_info_t data, unsigned int addr, gboolean select )
nkeynes@2
   188
{
nkeynes@2
   189
    int row;
nkeynes@2
   190
    
nkeynes@9
   191
    if( addr < data->disasm_from || addr >= data->disasm_to )
nkeynes@9
   192
        set_disassembly_region(data,addr);
nkeynes@2
   193
nkeynes@9
   194
    row = address_to_row( data, addr );
nkeynes@2
   195
    if(select) {
nkeynes@9
   196
        gtk_clist_select_row( data->disasm_list, row, 0 );
nkeynes@2
   197
    }
nkeynes@9
   198
    if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
nkeynes@9
   199
        gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
nkeynes@2
   200
    }
nkeynes@2
   201
}
nkeynes@2
   202
nkeynes@9
   203
void jump_to_pc( debug_info_t data, gboolean select )
nkeynes@9
   204
{
nkeynes@9
   205
    jump_to_disassembly( data, *data->cpu->pc, select );
nkeynes@9
   206
}
nkeynes@9
   207
nkeynes@9
   208
void set_disassembly_pc( debug_info_t data, unsigned int pc, gboolean select )
nkeynes@2
   209
{
nkeynes@2
   210
    int row;
nkeynes@2
   211
    
nkeynes@9
   212
    jump_to_disassembly( data, pc, select );
nkeynes@9
   213
    if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
nkeynes@9
   214
	data->disasm_pc < data->disasm_to )
nkeynes@9
   215
        gtk_clist_set_foreground( data->disasm_list, 
nkeynes@9
   216
				  (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
nkeynes@2
   217
                                  &clrNormal );
nkeynes@9
   218
    row = address_to_row( data, pc );
nkeynes@9
   219
    gtk_clist_set_foreground( data->disasm_list, row, &clrPC );
nkeynes@9
   220
    data->disasm_pc = pc;
nkeynes@2
   221
}
nkeynes@2
   222
nkeynes@26
   223
void set_disassembly_cpu( debug_info_t data, const gchar *cpu )
nkeynes@10
   224
{
nkeynes@11
   225
    int i;
nkeynes@11
   226
    for( i=0; data->cpu_list[i] != NULL; i++ ) {
nkeynes@11
   227
	if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
nkeynes@11
   228
	    if( data->cpu != data->cpu_list[i] ) {
nkeynes@11
   229
		data->cpu = data->cpu_list[i];
nkeynes@30
   230
		set_disassembly_pc( data, *data->cpu->pc, FALSE );
nkeynes@11
   231
		init_register_list( data );
nkeynes@33
   232
		update_icount( data );
nkeynes@11
   233
	    }
nkeynes@11
   234
	    return;
nkeynes@11
   235
	}
nkeynes@11
   236
    }
nkeynes@10
   237
}
nkeynes@10
   238
nkeynes@45
   239
void debug_win_toggle_breakpoint( debug_info_t data, int row )
nkeynes@43
   240
{
nkeynes@43
   241
    uint32_t pc = row_to_address( data, row );
nkeynes@43
   242
    int oldType = data->cpu->get_breakpoint( pc );
nkeynes@43
   243
    if( oldType != BREAK_NONE ) {
nkeynes@43
   244
	data->cpu->clear_breakpoint( pc, oldType );
nkeynes@45
   245
	gtk_clist_set_background( data->disasm_list, row, &clrWhite );
nkeynes@43
   246
    } else {
nkeynes@45
   247
	data->cpu->set_breakpoint( pc, BREAK_KEEP );
nkeynes@45
   248
	gtk_clist_set_background( data->disasm_list, row, &clrBreak );
nkeynes@43
   249
    }
nkeynes@45
   250
}
nkeynes@45
   251
nkeynes@45
   252
void debug_win_set_oneshot_breakpoint( debug_info_t data, int row )
nkeynes@45
   253
{
nkeynes@45
   254
    uint32_t pc = row_to_address( data, row );
nkeynes@45
   255
    data->cpu->clear_breakpoint( pc, BREAK_ONESHOT );
nkeynes@45
   256
    data->cpu->set_breakpoint( pc, BREAK_ONESHOT );
nkeynes@45
   257
    gtk_clist_set_background( data->disasm_list, row, &clrTempBreak );
nkeynes@43
   258
}
nkeynes@43
   259
nkeynes@30
   260
/**
nkeynes@30
   261
 * Execute a single instruction using the current CPU mode.
nkeynes@30
   262
 */
nkeynes@30
   263
void debug_win_single_step( debug_info_t data )
nkeynes@30
   264
{
nkeynes@30
   265
    data->cpu->step_func();
nkeynes@30
   266
    gtk_gui_update();
nkeynes@30
   267
}
nkeynes@30
   268
nkeynes@9
   269
uint32_t row_to_address( debug_info_t data, int row ) {
nkeynes@9
   270
    return data->cpu->instr_size * row + data->disasm_from;
nkeynes@9
   271
}
nkeynes@2
   272
nkeynes@9
   273
int address_to_row( debug_info_t data, uint32_t address ) {
nkeynes@9
   274
    if( data->disasm_from > address || data->disasm_to <= address )
nkeynes@9
   275
	return -1;
nkeynes@9
   276
    return (address - data->disasm_from) / data->cpu->instr_size;
nkeynes@9
   277
}
nkeynes@9
   278
nkeynes@10
   279
nkeynes@35
   280
void emit( void *ptr, int level, const gchar *source, const char *msg, ... )
nkeynes@2
   281
{
nkeynes@2
   282
    char buf[20], addr[10] = "", *p;
nkeynes@35
   283
    const char *arr[4] = {buf, source, addr};
nkeynes@2
   284
    int posn;
nkeynes@2
   285
    time_t tm = time(NULL);
nkeynes@2
   286
    va_list ap;
nkeynes@9
   287
    debug_info_t data;
nkeynes@9
   288
    if( ptr == NULL )
nkeynes@9
   289
	data = main_debug;
nkeynes@9
   290
    else data = (debug_info_t)ptr;
nkeynes@2
   291
nkeynes@2
   292
    va_start(ap, msg);
nkeynes@2
   293
    p = g_strdup_vprintf( msg, ap );
nkeynes@2
   294
    strftime( buf, sizeof(buf), "%H:%M:%S", localtime(&tm) );
nkeynes@35
   295
    sprintf( addr, "%08X", *data->cpu->pc );
nkeynes@35
   296
    arr[3] = p;
nkeynes@9
   297
    posn = gtk_clist_append(data->msgs_list, arr);
nkeynes@2
   298
    free(p);
nkeynes@2
   299
    va_end(ap);
nkeynes@2
   300
nkeynes@9
   301
    gtk_clist_set_foreground( data->msgs_list, posn, msg_colors[level] );
nkeynes@9
   302
    gtk_clist_moveto( data->msgs_list, posn, 0, 1.0, 0.0 );
nkeynes@2
   303
nkeynes@2
   304
    /* emit _really_ slows down the emu, to the point where the gui can be
nkeynes@2
   305
     * completely unresponsive if I don't include this:
nkeynes@2
   306
     */
nkeynes@2
   307
    while( gtk_events_pending() )
nkeynes@2
   308
        gtk_main_iteration();
nkeynes@2
   309
}
nkeynes@9
   310
nkeynes@9
   311
debug_info_t get_debug_info( GtkWidget *widget ) {
nkeynes@9
   312
    
nkeynes@9
   313
    GtkWidget *win = gtk_widget_get_toplevel(widget);
nkeynes@9
   314
    debug_info_t data = (debug_info_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
nkeynes@9
   315
    return data;
nkeynes@9
   316
}
nkeynes@9
   317
nkeynes@28
   318
void debug_win_enable_widget( debug_info_t data, const char *name, 
nkeynes@28
   319
			      gboolean enabled )
nkeynes@28
   320
{
nkeynes@28
   321
    GtkWidget *widget = GTK_WIDGET(gtk_object_get_data(GTK_OBJECT(data->win), name));
nkeynes@28
   322
    gtk_widget_set_sensitive( widget, enabled );
nkeynes@28
   323
}    
nkeynes@28
   324
nkeynes@28
   325
void debug_win_set_running( debug_info_t data, gboolean isRunning ) 
nkeynes@28
   326
{
nkeynes@28
   327
    debug_win_enable_widget( data, "stop_btn", isRunning );
nkeynes@28
   328
    debug_win_enable_widget( data, "step_btn", !isRunning );
nkeynes@28
   329
    debug_win_enable_widget( data, "run_btn", !isRunning );
nkeynes@28
   330
    debug_win_enable_widget( data, "runto_btn", !isRunning );
nkeynes@28
   331
}
.