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