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