Search
lxdream.org :: lxdream/src/gtkui/gtk_debug.c
lxdream 0.9.1
released Jun 29
Download Now
filename src/gtkui/gtk_debug.c
changeset 1000:6e4cabe2b32b
prev998:1754a8c6a9cf
next1065:bc1cc0c54917
author nkeynes
date Thu Jun 25 21:21:18 2009 +0000 (12 years ago)
permissions -rw-r--r--
last change Add quick state bits to the menus
view annotate diff log raw
     1 /**
     2  * $Id$
     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 <string.h>
    21 #include <math.h>
    22 #include <gdk/gdkkeysyms.h>
    23 #include "mem.h"
    24 #include "cpu.h"
    25 #include "dreamcast.h"
    26 #include "gtkui/gtkui.h"
    27 #include "sh4/sh4.h"
    28 #include "aica/armdasm.h"
    30 GdkColor *msg_colors[] = { &gui_colour_error, &gui_colour_error, &gui_colour_warn, 
    31         &gui_colour_normal,&gui_colour_debug, &gui_colour_trace };
    33 const cpu_desc_t cpu_list[4] = { &sh4_cpu_desc, &arm_cpu_desc, &armt_cpu_desc, NULL };
    35 void init_register_list( debug_window_t data );
    36 uint32_t row_to_address( debug_window_t data, int row );
    37 int address_to_row( debug_window_t data, uint32_t address );
    38 void set_disassembly_pc( debug_window_t data, unsigned int pc, gboolean select );
    39 void set_disassembly_region( debug_window_t data, unsigned int page );
    40 void set_disassembly_cpu( debug_window_t data, const gchar *cpu );
    42 void on_mode_field_changed ( GtkEditable *editable, gpointer user_data);
    43 gboolean on_page_field_key_press_event( GtkWidget * widget, GdkEventKey *event,
    44                                         gpointer user_data);
    45 void on_jump_pc_btn_clicked( GtkButton *button, gpointer user_data);
    46 void on_disasm_list_select_row (GtkCList *clist, gint row, gint column,
    47                                 GdkEvent *event, gpointer user_data);
    48 void on_disasm_list_unselect_row (GtkCList *clist, gint row, gint column,
    49                                   GdkEvent *event, gpointer user_data);
    50 gboolean on_debug_delete_event(GtkWidget *widget, GdkEvent event, gpointer user_data);
    52 struct debug_window_info {
    53     int disasm_from;
    54     int disasm_to;
    55     int disasm_pc;
    56     const struct cpu_desc_struct *cpu;
    57     const cpu_desc_t *cpu_list;
    58     GtkCList *regs_list;
    59     GtkCList *disasm_list;
    60     GtkEntry *page_field;
    61     GtkWidget *window;
    62     GtkWidget *statusbar;
    63     char saved_regs[0];
    64 };
    66 debug_window_t debug_window_new( const gchar *title, GtkWidget *menubar, 
    67                                  GtkWidget *toolbar, GtkAccelGroup *accel_group )
    68 {
    69     debug_window_t data = g_malloc0( sizeof(struct debug_window_info) + cpu_list[0]->regs_size );
    70     GtkWidget *vbox;
    72     data->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    73     gtk_window_set_default_size (GTK_WINDOW (data->window), 700, 480);
    74     gtk_window_set_title( GTK_WINDOW(data->window), title );
    75     gtk_window_add_accel_group (GTK_WINDOW (data->window), accel_group);
    77     gtk_toolbar_set_style( GTK_TOOLBAR(toolbar), GTK_TOOLBAR_ICONS );
    79     data->statusbar = gtk_statusbar_new();
    81     GtkWidget *hpaned = gtk_hpaned_new ();
    82     gtk_paned_set_position (GTK_PANED (hpaned), 500);
    84     GtkWidget *disasm_box = gtk_vbox_new(FALSE,0);
    85     gtk_paned_pack1 (GTK_PANED (hpaned), disasm_box, TRUE, TRUE);
    87     GtkWidget *hbox1 = gtk_hbox_new (FALSE, 0);
    88     gtk_box_pack_start (GTK_BOX (disasm_box), hbox1, FALSE, FALSE, 3);
    89     gtk_box_pack_start (GTK_BOX (hbox1), gtk_label_new (_("Page")), FALSE, FALSE, 4);
    91     data->page_field = GTK_ENTRY(gtk_entry_new ());
    92     gtk_box_pack_start (GTK_BOX (hbox1), GTK_WIDGET(data->page_field), FALSE, TRUE, 0);
    94     GtkWidget *jump_pc_btn = gtk_button_new_with_mnemonic (_(" Jump to PC "));
    95     gtk_box_pack_start (GTK_BOX (hbox1), jump_pc_btn, FALSE, FALSE, 4);
    97     gtk_box_pack_start (GTK_BOX (hbox1), gtk_label_new(_("Mode")), FALSE, FALSE, 5);
    99     GtkWidget *mode_box = gtk_combo_new ();
   100     gtk_box_pack_start (GTK_BOX (hbox1), mode_box, FALSE, FALSE, 0);
   101     GList *mode_box_items = NULL;
   102     mode_box_items = g_list_append (mode_box_items, (gpointer) _("SH4"));
   103     mode_box_items = g_list_append (mode_box_items, (gpointer) _("ARM7"));
   104     mode_box_items = g_list_append (mode_box_items, (gpointer) _("ARM7T"));
   105     gtk_combo_set_popdown_strings (GTK_COMBO (mode_box), mode_box_items);
   106     g_list_free (mode_box_items);
   108     GtkWidget *mode_field = GTK_COMBO (mode_box)->entry;
   109     gtk_editable_set_editable (GTK_EDITABLE (mode_field), FALSE);
   111     GtkWidget *disasm_scroll = gtk_scrolled_window_new (NULL, NULL);
   112     gtk_box_pack_start (GTK_BOX (disasm_box), disasm_scroll, TRUE, TRUE, 0);
   113     gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (disasm_scroll), GTK_SHADOW_IN);
   114     data->disasm_list = GTK_CLIST(gtk_clist_new (4));
   115     gtk_clist_set_column_width (GTK_CLIST (data->disasm_list), 0, 80);
   116     gtk_clist_set_column_width (GTK_CLIST (data->disasm_list), 2, 80);
   117     gtk_clist_set_column_width (GTK_CLIST (data->disasm_list), 3, 80);
   118     gtk_clist_set_column_width( data->disasm_list, 1, 16 );
   119     gtk_clist_column_titles_hide (GTK_CLIST (data->disasm_list));
   120     gtk_container_add (GTK_CONTAINER (disasm_scroll), GTK_WIDGET(data->disasm_list));
   122     GtkWidget *reg_scroll = gtk_scrolled_window_new (NULL, NULL);
   123     gtk_paned_pack2 (GTK_PANED (hpaned), reg_scroll, FALSE, TRUE);
   124     gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (reg_scroll), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   125     gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (reg_scroll), GTK_SHADOW_IN);
   127     data->regs_list = GTK_CLIST(gtk_clist_new (2));
   128     gtk_container_add (GTK_CONTAINER (reg_scroll), GTK_WIDGET(data->regs_list));
   129     gtk_clist_set_column_width (GTK_CLIST (data->regs_list), 0, 80);
   130     gtk_clist_set_column_width (GTK_CLIST (data->regs_list), 1, 80);
   131     gtk_clist_column_titles_hide (GTK_CLIST (data->regs_list));
   132     gtk_widget_modify_font( GTK_WIDGET(data->regs_list), gui_fixed_font );
   134     vbox = gtk_vbox_new(FALSE, 0);
   135     gtk_container_add( GTK_CONTAINER(data->window), vbox );
   136     gtk_box_pack_start( GTK_BOX(vbox), menubar, FALSE, FALSE, 0 );
   137     gtk_box_pack_start( GTK_BOX(vbox), toolbar, FALSE, FALSE, 0 );
   138     gtk_box_pack_start( GTK_BOX(vbox), hpaned, TRUE, TRUE, 0 );
   139     gtk_box_pack_start( GTK_BOX(vbox), data->statusbar, FALSE, FALSE, 0 );
   141     g_signal_connect ((gpointer) data->page_field, "key_press_event",
   142                       G_CALLBACK (on_page_field_key_press_event),
   143                       data);
   144     g_signal_connect ((gpointer) jump_pc_btn, "clicked",
   145                       G_CALLBACK (on_jump_pc_btn_clicked),
   146                       data);
   147     g_signal_connect ((gpointer) mode_field, "changed",
   148                       G_CALLBACK (on_mode_field_changed),
   149                       data);
   150     g_signal_connect ((gpointer) data->disasm_list, "select_row",
   151                       G_CALLBACK (on_disasm_list_select_row),
   152                       data);
   153     g_signal_connect ((gpointer) data->disasm_list, "unselect_row",
   154                       G_CALLBACK (on_disasm_list_unselect_row),
   155                       data);
   156     g_signal_connect ((gpointer) data->window, "delete_event",
   157                       G_CALLBACK (on_debug_delete_event),
   158                       data);
   160     data->disasm_from = -1;
   161     data->disasm_to = -1;
   162     data->disasm_pc = -1;
   163     data->cpu = cpu_list[0];
   164     data->cpu_list = cpu_list;
   166     init_register_list( data );
   167     gtk_object_set_data( GTK_OBJECT(data->window), "debug_data", data );
   168     set_disassembly_pc( data, *data->cpu->pc, FALSE );
   169     debug_window_set_running( data, FALSE );
   171     gtk_widget_show_all( data->window );
   172     return data;
   173 }
   175 void debug_window_show( debug_window_t data, gboolean show )
   176 {
   177     if( show ) {
   178         gtk_widget_show( data->window );
   179     } else {
   180         gtk_widget_hide( data->window );
   181     }
   182 }
   184 int debug_window_get_selected_row( debug_window_t data )
   185 {
   186     if( data->disasm_list->selection == NULL ) {
   187         return -1;
   188     } else {
   189         return GPOINTER_TO_INT(data->disasm_list->selection->data);
   190     }
   191 }
   193 void init_register_list( debug_window_t data ) 
   194 {
   195     int i;
   196     char buf[20];
   197     char *arr[2];
   199     gtk_clist_clear( data->regs_list );
   200     arr[1] = buf;
   201     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
   202         arr[0] = data->cpu->regs_info[i].name;
   203         void *value = data->cpu->get_register(i);
   204         if( value != NULL ) {
   205             if( data->cpu->regs_info->type == REG_INT ) {
   206                 sprintf( buf, "%08X", *((uint32_t *)value) );
   207                 gtk_clist_append( data->regs_list, arr );
   208             } else if( data->cpu->regs_info->type == REG_FLOAT ) {
   209                 sprintf( buf, "%f", *((float *)value) );
   210                 gtk_clist_append( data->regs_list, arr );
   211             }
   212         }
   213     }
   214 }
   216 /*
   217  * Check for changed registers and update the display
   218  */
   219 void debug_window_update( debug_window_t data )
   220 {
   221     int i;
   222     int posn = 0;
   223     for( i=0; data->cpu->regs_info[i].name != NULL; i++ ) {
   224         void *value = data->cpu->get_register(i);
   225         if( value != NULL ) {
   226             if( data->cpu->regs_info[i].type == REG_INT ) {
   227                 /* Yes this _is_ probably fairly evil */
   228                 if( *((uint32_t *)value) !=
   229                     *((uint32_t *)((char *)data->saved_regs + ((char *)value - (char *)data->cpu->regs))) ) {
   230                     char buf[20];
   231                     sprintf( buf, "%08X", *((uint32_t *)value) );
   232                     gtk_clist_set_text( data->regs_list, posn, 1, buf );
   233                     gtk_clist_set_foreground( data->regs_list, posn, &gui_colour_changed );
   234                 } else {
   235                     gtk_clist_set_foreground( data->regs_list, posn, &gui_colour_normal );
   236                 }
   237                 posn++;
   238             } else {
   239                 if( *((float *)value) !=
   240                     *((float *)((char *)data->saved_regs + ((char *)value - (char *)data->cpu->regs))) ) {
   241                     char buf[20];
   242                     sprintf( buf, "%f", *((float *)value) );
   243                     gtk_clist_set_text( data->regs_list, i, posn, buf );
   244                     gtk_clist_set_foreground( data->regs_list, posn, &gui_colour_changed );
   245                 } else {
   246                     gtk_clist_set_foreground( data->regs_list, posn, &gui_colour_normal );
   247                 }
   248                 posn++;
   249             }
   250         }
   251     }
   253     set_disassembly_pc( data, *data->cpu->pc, TRUE );
   254     memcpy( data->saved_regs, data->cpu->regs, data->cpu->regs_size );
   255 }
   257 void set_disassembly_region( debug_window_t data, unsigned int page )
   258 {
   259     uint32_t i, posn, next;
   260     char buf[80];
   261     char addr[10];
   262     char opcode[16] = "";
   263     char *arr[4] = { addr, " ", opcode, buf };
   264     unsigned int from = page & 0xFFFFF000;
   265     unsigned int to = from + 4096;
   267     gtk_clist_clear(data->disasm_list);
   269     sprintf( addr, "%08X", from );
   270     gtk_entry_set_text( data->page_field, addr );
   272     if( !data->cpu->is_valid_page_func( from ) ) {
   273         arr[3] = (char *)_("This page is currently unmapped");
   274         gtk_clist_append( data->disasm_list, arr );
   275         gtk_clist_set_foreground( data->disasm_list, 0, &gui_colour_error );
   276     } else {
   277         for( i=from; i<to; i = next ) {
   278             next = data->cpu->disasm_func( i, buf, sizeof(buf), opcode );
   279             sprintf( addr, "%08X", i );
   280             posn = gtk_clist_append( data->disasm_list, arr );
   281             if( buf[0] == '?' )
   282                 gtk_clist_set_foreground( data->disasm_list, posn, &gui_colour_warn );
   283             if( data->cpu->get_breakpoint != NULL ) {
   284                 int type = data->cpu->get_breakpoint( i );
   285                 switch(type) {
   286                 case BREAK_ONESHOT:
   287                     gtk_clist_set_background( data->disasm_list, posn, &gui_colour_temp_break );
   288                     break;
   289                 case BREAK_KEEP:
   290                     gtk_clist_set_background( data->disasm_list, posn, &gui_colour_break );
   291                     break;
   292                 }
   293             }
   294         }
   295         if( data->disasm_pc != -1 && data->disasm_pc >= from && data->disasm_pc < to )
   296             gtk_clist_set_foreground( data->disasm_list, address_to_row(data, data->disasm_pc),
   297                     &gui_colour_pc );
   298     }
   300     if( page != from ) { /* not a page boundary */
   301         gtk_clist_moveto( data->disasm_list, (page-from)>>1, 0, 0.5, 0.0 );
   302     }
   303     data->disasm_from = from;
   304     data->disasm_to = to;
   305 }
   307 void jump_to_disassembly( debug_window_t data, unsigned int addr, gboolean select )
   308 {
   309     int row;
   311     if( addr < data->disasm_from || addr >= data->disasm_to )
   312         set_disassembly_region(data,addr);
   314     row = address_to_row( data, addr );
   315     if(select) {
   316         gtk_clist_select_row( data->disasm_list, row, 0 );
   317     }
   318     if( gtk_clist_row_is_visible( data->disasm_list, row ) != GTK_VISIBILITY_FULL ){
   319         gtk_clist_moveto( data->disasm_list, row, 0, 0.5, 0.0 );
   320     }
   321 }
   323 void jump_to_pc( debug_window_t data, gboolean select )
   324 {
   325     jump_to_disassembly( data, *data->cpu->pc, select );
   326 }
   328 void set_disassembly_pc( debug_window_t data, unsigned int pc, gboolean select )
   329 {
   330     int row;
   332     jump_to_disassembly( data, pc, select );
   333     if( data->disasm_pc != -1 && data->disasm_pc >= data->disasm_from && 
   334             data->disasm_pc < data->disasm_to )
   335         gtk_clist_set_foreground( data->disasm_list, 
   336                 (data->disasm_pc - data->disasm_from) / data->cpu->instr_size,
   337                 &gui_colour_normal );
   338     row = address_to_row( data, pc );
   339     gtk_clist_set_foreground( data->disasm_list, row, &gui_colour_pc );
   340     data->disasm_pc = pc;
   341 }
   343 void set_disassembly_cpu( debug_window_t data, const gchar *cpu )
   344 {
   345     int i;
   346     for( i=0; data->cpu_list[i] != NULL; i++ ) {
   347         if( strcmp( data->cpu_list[i]->name, cpu ) == 0 ) {
   348             if( data->cpu != data->cpu_list[i] ) {
   349                 data->cpu = data->cpu_list[i];
   350                 data->disasm_from = data->disasm_to = -1; /* Force reload */
   351                 set_disassembly_pc( data, *data->cpu->pc, FALSE );
   352                 init_register_list( data );
   353             }
   354             return;
   355         }
   356     }
   357 }
   359 void debug_window_toggle_breakpoint( debug_window_t data, int row )
   360 {
   361     uint32_t pc = row_to_address( data, row );
   362     int oldType = data->cpu->get_breakpoint( pc );
   363     if( oldType != BREAK_NONE ) {
   364         data->cpu->clear_breakpoint( pc, oldType );
   365         gtk_clist_set_background( data->disasm_list, row, &gui_colour_white );
   366     } else {
   367         data->cpu->set_breakpoint( pc, BREAK_KEEP );
   368         gtk_clist_set_background( data->disasm_list, row, &gui_colour_break );
   369     }
   370 }
   372 void debug_window_set_oneshot_breakpoint( debug_window_t data, int row )
   373 {
   374     uint32_t pc = row_to_address( data, row );
   375     data->cpu->clear_breakpoint( pc, BREAK_ONESHOT );
   376     data->cpu->set_breakpoint( pc, BREAK_ONESHOT );
   377     gtk_clist_set_background( data->disasm_list, row, &gui_colour_temp_break );
   378 }
   380 /**
   381  * Execute a single instruction using the current CPU mode.
   382  */
   383 void debug_window_single_step( debug_window_t data )
   384 {
   385     data->cpu->step_func();
   386     gtk_gui_update();
   387 }
   389 uint32_t row_to_address( debug_window_t data, int row ) {
   390     return data->cpu->instr_size * row + data->disasm_from;
   391 }
   393 int address_to_row( debug_window_t data, uint32_t address ) {
   394     if( data->disasm_from > address || data->disasm_to <= address )
   395         return -1;
   396     return (address - data->disasm_from) / data->cpu->instr_size;
   397 }
   399 debug_window_t get_debug_info( GtkWidget *widget ) {
   401     GtkWidget *win = gtk_widget_get_toplevel(widget);
   402     debug_window_t data = (debug_window_t)gtk_object_get_data( GTK_OBJECT(win), "debug_data" );
   403     return data;
   404 }
   406 void debug_window_set_running( debug_window_t data, gboolean isRunning ) 
   407 {
   408     if( data != NULL ) {
   409         gtk_gui_enable_action( "SingleStep", !isRunning );
   410         gtk_gui_enable_action( "RunTo", !isRunning && dreamcast_can_run() );
   411     }
   412 }
   414 void on_mode_field_changed ( GtkEditable *editable, gpointer user_data)
   415 {
   416     const gchar *text = gtk_entry_get_text( GTK_ENTRY(editable) );
   417     set_disassembly_cpu( gtk_gui_get_debugger(), text );
   418 }
   421 gboolean on_page_field_key_press_event( GtkWidget * widget, GdkEventKey *event,
   422                                         gpointer user_data)
   423 {
   424     if( event->keyval == GDK_Return || event->keyval == GDK_Linefeed ) {
   425         debug_window_t data = get_debug_info(widget);
   426         const gchar *text = gtk_entry_get_text( GTK_ENTRY(widget) );
   427         gchar *endptr;
   428         unsigned int val = strtoul( text, &endptr, 16 );
   429         if( text == endptr ) { /* invalid input */
   430             char buf[10];
   431             sprintf( buf, "%08X", row_to_address(data,0) );
   432             gtk_entry_set_text( GTK_ENTRY(widget), buf );
   433         } else {
   434             set_disassembly_region(data, val);
   435         }
   436     }
   437     return FALSE;
   438 }
   441 void on_jump_pc_btn_clicked( GtkButton *button, gpointer user_data)
   442 {
   443     debug_window_t data = get_debug_info( GTK_WIDGET(button) );
   444     jump_to_pc( data, TRUE );
   445 }
   447 void on_disasm_list_select_row (GtkCList *clist, gint row, gint column,
   448                                 GdkEvent *event, gpointer user_data)
   449 {
   450     gtk_gui_enable_action( "SetBreakpoint", TRUE );
   451     gtk_gui_enable_action( "RunTo", dreamcast_can_run() );
   452 }
   454 void on_disasm_list_unselect_row (GtkCList *clist, gint row, gint column,
   455                                   GdkEvent *event, gpointer user_data)
   456 {
   457     gtk_gui_enable_action( "SetBreakpoint", FALSE );
   458     gtk_gui_enable_action( "RunTo", FALSE );
   459 }
   461 gboolean on_debug_delete_event(GtkWidget *widget, GdkEvent event, gpointer user_data)
   462 {
   463     gtk_widget_hide( widget );
   464     return TRUE;
   465 }
.