Updated Spanish translation
[anjuta.git] / plugins / debug-manager / memory.c
blob71a0415f3c30842fdaf2fecb61709ca4fb319485
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 memory.c
4 Copyright (C) 2006 Sebastien Granjoux
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #include <sys/stat.h>
26 #include <unistd.h>
27 #include <string.h>
29 #include <gtk/gtk.h>
30 #include <glib.h>
31 #include <gdk/gdk.h>
33 /*#define DEBUG*/
34 #include <libanjuta/anjuta-debug.h>
36 #include "memory.h"
38 #include "data_view.h"
39 #include "plugin.h"
40 #include "queue.h"
42 /* Constants
43 *---------------------------------------------------------------------------*/
45 /* Types
46 *---------------------------------------------------------------------------*/
48 struct _DmaMemory
50 DmaDebuggerQueue *debugger;
51 AnjutaPlugin *plugin;
52 GtkWidget *window;
53 DmaDataView *dataview;
54 DmaDataBuffer *buffer;
55 GtkWidget *menu;
58 /* Private functions
59 *---------------------------------------------------------------------------*/
61 static void
62 on_memory_block_read (const IAnjutaDebuggerMemoryBlock *block, DmaMemory *mem, GError *err)
64 const gchar* tag;
66 if (block == NULL)
68 /* Command has been cancelled */
69 return;
72 guint length = block->length;
73 gchar *data = block->data;
74 gulong address = block->address;
76 tag = data + length;
77 while (length != 0)
79 const gchar* start;
81 /* Skip undefined data */
82 for (start = tag; *tag == 0; tag++)
84 length--;
85 if (length == 0) return;
87 data += tag - start;
88 address += tag - start;
90 /* Compute length of defined data */
91 for (start = tag; (length != 0) && (*tag != 0); tag++)
93 length--;
96 dma_data_buffer_set_data (mem->buffer, address, tag - start, data);
97 data += tag - start;
98 address += tag - start;
102 static void
103 read_memory_block (gulong address, gulong length, gpointer user_data)
105 DmaMemory *mem = (DmaMemory *)user_data;
107 if (mem->debugger != NULL)
109 dma_queue_inspect_memory (
110 mem->debugger,
111 address,
112 (guint)length,
113 (IAnjutaDebuggerCallback)on_memory_block_read,
114 mem);
118 static void
119 dma_memory_update (DmaMemory *mem)
121 dma_data_buffer_invalidate(mem->buffer);
122 dma_data_view_refresh(mem->dataview);
125 static void
126 on_program_stopped (DmaMemory *mem)
128 dma_memory_update (mem);
131 static void
132 destroy_memory_gui (DmaMemory *mem)
134 /* Destroy menu */
135 if (mem->menu != NULL)
137 gtk_widget_destroy (mem->menu);
140 if (mem->window != NULL)
142 gtk_widget_destroy (mem->window);
143 mem->window = NULL;
144 mem->dataview = NULL;
145 dma_data_buffer_remove_all_page (mem->buffer);
148 /* Remove buffer */
149 if (mem->buffer != NULL)
151 g_object_unref (mem->buffer);
152 mem->buffer = NULL;
156 static void
157 on_debugger_stopped (DmaMemory *mem)
160 g_signal_handlers_disconnect_by_func (mem->plugin, G_CALLBACK (on_debugger_stopped), mem);
161 g_signal_handlers_disconnect_by_func (mem->plugin, G_CALLBACK (on_program_stopped), mem);
163 destroy_memory_gui (mem);
166 static gboolean
167 create_memory_gui (DmaMemory *mem)
169 g_return_val_if_fail (mem->buffer == NULL, FALSE);
170 g_return_val_if_fail (mem->window == NULL, FALSE);
172 mem->buffer = dma_data_buffer_new (0x0000, 0xFFFFFFFFU, read_memory_block, NULL, mem);
173 if (mem->buffer == NULL) return FALSE;
175 mem->dataview = (DmaDataView*)dma_data_view_new_with_buffer (mem->buffer);
177 mem->window = gtk_frame_new (NULL);
178 gtk_frame_set_shadow_type (GTK_FRAME (mem->window), GTK_SHADOW_IN);
179 gtk_container_add (GTK_CONTAINER (mem->window),
180 GTK_WIDGET (mem->dataview));
182 gtk_widget_show_all (mem->window);
183 anjuta_shell_add_widget (mem->plugin->shell,
184 mem->window,
185 "AnjutaDebuggerMemory", _("Memory"),
186 "debugger-memory", ANJUTA_SHELL_PLACEMENT_NONE,
187 NULL);
189 return TRUE;
192 static void
193 on_debugger_started (DmaMemory *mem)
195 /* Keep graphical interface if already existing, could happens after
196 * unloading a program in the debugger */
197 if (mem->window) return;
199 if (!dma_debugger_queue_is_supported (mem->debugger, HAS_MEMORY)) return;
201 if (!create_memory_gui (mem)) return;
203 /* Connect signals */
204 g_signal_connect_swapped (mem->plugin, "debugger-stopped", G_CALLBACK (on_debugger_stopped), mem);
205 g_signal_connect_swapped (mem->plugin, "program-stopped", G_CALLBACK (on_program_stopped), mem);
208 /* Constructor & Destructor
209 *---------------------------------------------------------------------------*/
211 DmaMemory*
212 dma_memory_new(DebugManagerPlugin *plugin)
214 DmaMemory* mem;
216 mem = g_new0 (DmaMemory, 1);
218 mem->debugger = dma_debug_manager_get_queue (plugin);
219 mem->plugin = ANJUTA_PLUGIN (plugin);
221 g_signal_connect_swapped (mem->plugin, "debugger-started", G_CALLBACK (on_debugger_started), mem);
223 return mem;
226 void
227 dma_memory_free(DmaMemory* mem)
229 g_return_if_fail (mem != NULL);
231 g_signal_handlers_disconnect_matched (mem->plugin, G_SIGNAL_MATCH_DATA, 0, 0, NULL, NULL, mem);
233 destroy_memory_gui (mem);
235 g_free(mem);