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