1 /* Hey EMACS -*- linux-c -*- */
2 /* $Id: dbg_all.c 2832 2009-05-08 10:56:40Z roms $ */
3 
4 /*  TiEmu - Tiemu Is an EMUlator
5  *
6  *  Copyright (c) 2000-2001, Thomas Corvazier, Romain Lievin
7  *  Copyright (c) 2001-2003, Romain Lievin
8  *  Copyright (c) 2003, Julien Blache
9  *  Copyright (c) 2004, Romain Li�vin
10  *  Copyright (c) 2005-2008, Romain Li�vin, Kevin Kofler
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or
15  *  (at your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; if not, write to the Free Software
24  *  Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.
25  */
26 
27 #ifdef HAVE_CONFIG_H
28 #  include <config.h>
29 #endif
30 
31 #include <glib.h>
32 #include <gtk/gtk.h>
33 #ifdef __WIN32__
34 #include <gdk/gdk.h>
35 #include <gdk/gdkwin32.h>
36 #endif
37 #ifdef __MINGW32__
38 #include <windows.h>
39 #endif
40 
41 #include "ti68k_int.h"
42 #include "struct.h"
43 #include "dbg_all.h"
44 #include "dbg_wnds.h"
45 #include "dbg_dock.h"
46 #include "support.h"
47 #include "paths.h"
48 #include "engine.h"
49 #include "dboxes.h"
50 #ifndef NO_GDB
51 #include "gdbcall.h"
52 
53 void gdbtk_hide_insight(void);
54 void gdbtk_show_insight(void);
55 void delete_command(void *, int);
56 void symbol_file_clear(int);
57 void gdbtk_clear_file(void);
58 void exec_build_section_table(void);
59 
60 gchar *symfile;
61 #endif
62 
63 int dbg_on = 0;
64 int dbg_load = 0;
65 
66 /* Functions applicable to the whole debugger */
67 
68 // create windows but don't show them yet
gtk_debugger_preload(void)69 void gtk_debugger_preload(void)
70 {
71 	WND_TMR_START();
72 
73 	dbgw.regs  = dbgregs_create_window();
74 	dbgw.mem   = dbgmem_create_window();
75 	dbgw.bkpts = dbgbkpts_create_window();
76 	dbgw.pclog = dbgpclog_create_window();
77     dbgw.stack = dbgstack_create_window();
78 	dbgw.heap  = dbgheap_create_window();
79 	dbgw.iop   = dbgiop_create_window();
80 	dbgw.code  = dbgcode_create_window();
81 	if(options3.dbg_dock)	//must be launched as last
82 		dbgw.dock  = dbgdock_create_window();
83 
84 	dbg_load = !0;
85 
86 	WND_TMR_STOP("Debugger Preload Time");
87 }
88 
gtk_debugger_refresh(void)89 void gtk_debugger_refresh(void)
90 {
91 	WND_TMR_START();
92 
93 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.regs))
94 		dbgregs_refresh_window();
95 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.mem))
96 		dbgmem_refresh_window();
97 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.bkpts))
98 		dbgbkpts_refresh_window();
99 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.pclog))
100 		dbgpclog_refresh_window();
101 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.code))
102 		dbgcode_refresh_window();
103     if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.stack))
104 		dbgstack_refresh_window();
105 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.heap))
106 		dbgheap_refresh_window();
107 	if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.iop))
108 		dbgiop_refresh_window();
109 
110 	WND_TMR_STOP("Debugger Refresh Time");
111 	printf("\n");
112 }
113 
gtk_debugger_display(void)114 void gtk_debugger_display(void)
115 {
116 	WND_TMR_START();
117 
118 	// display debugger windows (if not)
119 	if(options3.dbg_dock)
120 	{
121 		dbgdock_display_window();
122 		dbgiop_display_window();
123 		dbgpclog_display_window();
124 	}
125 	else
126 	{
127 		dbgregs_display_window();
128 		dbgmem_display_window();
129 		dbgbkpts_display_window();
130 		dbgpclog_display_window();
131 		dbgstack_display_window();
132 		dbgheap_display_window();
133 		dbgiop_display_window();
134 		dbgcode_display_window();	// the last has focus
135 	}
136 
137 	WND_TMR_STOP("Display Time");
138 }
139 
140 // show previously created window
gtk_debugger_enter(int context)141 int gtk_debugger_enter(int context)
142 {
143 #ifndef NO_GDB
144 	gint type, id, mode;
145 
146 	if (!dbg_on) gdbtk_show_insight();
147 #endif
148 
149 	// debugger is open
150 	dbg_on = !0;
151 
152     // show breakpoint source (modal)
153     switch(context)
154     {
155     case DBG_TRACE:
156         break;
157     case DBG_BREAK:
158         break;
159     }
160 
161     // display debugger windows (if not)
162 	gtk_debugger_display();
163 	gtk_debugger_refresh();
164 
165 	// enable the debugger if GDB disabled it
166 	if (!options3.dbg_dock && !GTK_WIDGET_SENSITIVE(dbgw.regs))
167 		gtk_debugger_enable();
168 
169 	// handle automatic debugging requests
170 #ifndef NO_GDB
171 	if (symfile)
172 	{
173 		// get context
174 		ti68k_bkpt_get_cause(&type, &mode, &id);
175 
176 		if(type == BK_TYPE_PGMENTRY)
177 		{
178 			uint16_t handle, offset;
179 			uint32_t pc;
180 
181 			ti68k_bkpt_get_pgmentry_offset(id, &handle, &offset);
182 			ti68k_bkpt_del_pgmentry(handle);
183 			if(options3.dbg_dock || GTK_WIDGET_VISIBLE(dbgw.bkpts))
184 				dbgbkpts_refresh_window();
185 
186 			delete_command(NULL, 0);
187 			symbol_file_clear(0);
188 			gdbtk_clear_file ();
189 			ti68k_register_get_pc(&pc);
190 			pc -= offset-2;
191 			gdb_add_symbol_file(symfile, pc);
192 			g_free (symfile);
193 			symfile = NULL;
194 			exec_build_section_table();
195 
196 			ti68k_unprotect_64KB_range(pc);
197 
198 			gdb_hbreak("__main");
199 		}
200 	}
201 #endif
202 
203 	return 0;
204 }
205 
gtk_debugger_close(void)206 void gtk_debugger_close (void)
207 {
208 #ifndef NO_GDB
209 	// hide all windows
210 	gdbtk_hide_insight();
211 	dbg_on = 0;
212 	if(options3.dbg_dock)
213 		dbgdock_hide_all(!0);
214 	else
215 		dbgwnds_hide_all(!0);
216 
217     // and restarts the emulator
218 	ti68k_bkpt_set_cause(0, 0, 0);
219 	dbgbkpts_erase_context();
220 
221     if (engine_is_stopped()) gdbcall_continue();
222 #else
223 	// hide all windows
224 	dbg_on = 0;
225 	if(options3.dbg_dock)
226 		dbgdock_hide_all(!0);
227 	else
228 		dbgwnds_hide_all(!0);
229 
230     // and restarts the emulator
231 	ti68k_bkpt_set_cause(0, 0, 0);
232 	dbgbkpts_erase_context();
233 
234     engine_start();
235 #endif
236 }
237 
close_debugger_wrapper(gpointer data)238 static gint close_debugger_wrapper(gpointer data)
239 {
240 	gtk_debugger_close();
241 	return FALSE;
242 }
243 
gtk_debugger_close_async(void)244 void gtk_debugger_close_async (void)
245 {
246 	g_idle_add(close_debugger_wrapper, NULL);
247 }
248 
gtk_debugger_disable(void)249 void gtk_debugger_disable(void)
250 {
251 	if(options3.dbg_dock)
252 		dbgdock_set_sensitivity(FALSE);
253 	else
254 		dbgwnds_set_sensitivity(FALSE);
255 }
256 
gtk_debugger_enable(void)257 void gtk_debugger_enable(void)
258 {
259 	if(options3.dbg_dock)
260 		dbgdock_set_sensitivity(TRUE);
261 	else
262 		dbgwnds_set_sensitivity(TRUE);
263 }
264